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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
a05746458702f97a8600c16caf04bbb6e6d61373 | 4d2583807a5ac6caaffd3d7a5f646d61ca85d532 | /src/algebra/indicator_function.lean | cb13f9284b4d682fbcc88eede8f0dae5d83f0d57 | [
"Apache-2.0"
] | permissive | AntoineChambert-Loir/mathlib | 64aabb896129885f12296a799818061bc90da1ff | 07be904260ab6e36a5769680b6012f03a4727134 | refs/heads/master | 1,693,187,631,771 | 1,636,719,886,000 | 1,636,719,886,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 21,940 | lean | /-
Copyright (c) 2020 Zhouhang Zhou. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Zhouhang Zhou
-/
import algebra.support
/-!
# Indicator function
- `indicator (s : set Ξ±) (f : Ξ± β Ξ²) (a : Ξ±)` is `f a` if `a β s` and is `0` otherwise.
- `mul_indicator (s : set Ξ±) (f : Ξ± β Ξ²) (a : Ξ±)` is `f a` if `a β s` and is `1` otherwise.
## Implementation note
In mathematics, an indicator function or a characteristic function is a function
used to indicate membership of an element in a set `s`,
having the value `1` for all elements of `s` and the value `0` otherwise.
But since it is usually used to restrict a function to a certain set `s`,
we let the indicator function take the value `f x` for some function `f`, instead of `1`.
If the usual indicator function is needed, just set `f` to be the constant function `Ξ»x, 1`.
## Tags
indicator, characteristic
-/
noncomputable theory
open_locale classical big_operators
open function
variables {Ξ± Ξ² ΞΉ M N : Type*}
namespace set
section has_one
variables [has_one M] [has_one N] {s t : set Ξ±} {f g : Ξ± β M} {a : Ξ±}
/-- `indicator s f a` is `f a` if `a β s`, `0` otherwise. -/
def indicator {M} [has_zero M] (s : set Ξ±) (f : Ξ± β M) : Ξ± β M := Ξ» x, if x β s then f x else 0
/-- `mul_indicator s f a` is `f a` if `a β s`, `1` otherwise. -/
@[to_additive]
def mul_indicator (s : set Ξ±) (f : Ξ± β M) : Ξ± β M := Ξ» x, if x β s then f x else 1
@[simp, to_additive] lemma piecewise_eq_mul_indicator : s.piecewise f 1 = s.mul_indicator f := rfl
@[to_additive] lemma mul_indicator_apply (s : set Ξ±) (f : Ξ± β M) (a : Ξ±) :
mul_indicator s f a = if a β s then f a else 1 := rfl
@[simp, to_additive] lemma mul_indicator_of_mem (h : a β s) (f : Ξ± β M) :
mul_indicator s f a = f a := if_pos h
@[simp, to_additive] lemma mul_indicator_of_not_mem (h : a β s) (f : Ξ± β M) :
mul_indicator s f a = 1 := if_neg h
@[to_additive] lemma mul_indicator_eq_one_or_self (s : set Ξ±) (f : Ξ± β M) (a : Ξ±) :
mul_indicator s f a = 1 β¨ mul_indicator s f a = f a :=
if h : a β s then or.inr (mul_indicator_of_mem h f) else or.inl (mul_indicator_of_not_mem h f)
@[simp, to_additive] lemma mul_indicator_apply_eq_self :
s.mul_indicator f a = f a β (a β s β f a = 1) :=
ite_eq_left_iff.trans $ by rw [@eq_comm _ (f a)]
@[simp, to_additive] lemma mul_indicator_eq_self : s.mul_indicator f = f β mul_support f β s :=
by simp only [funext_iff, subset_def, mem_mul_support, mul_indicator_apply_eq_self, not_imp_comm]
@[to_additive] lemma mul_indicator_eq_self_of_superset (h1 : s.mul_indicator f = f) (h2 : s β t) :
t.mul_indicator f = f :=
by { rw mul_indicator_eq_self at h1 β’, exact subset.trans h1 h2 }
@[simp, to_additive] lemma mul_indicator_apply_eq_one :
mul_indicator s f a = 1 β (a β s β f a = 1) :=
ite_eq_right_iff
@[simp, to_additive] lemma mul_indicator_eq_one :
mul_indicator s f = (Ξ» x, 1) β disjoint (mul_support f) s :=
by simp only [funext_iff, mul_indicator_apply_eq_one, set.disjoint_left, mem_mul_support,
not_imp_not]
@[simp, to_additive] lemma mul_indicator_eq_one' :
mul_indicator s f = 1 β disjoint (mul_support f) s :=
mul_indicator_eq_one
@[to_additive] lemma mul_indicator_eq_one_iff (a : Ξ±) :
s.mul_indicator f a β 1 β a β s β© mul_support f :=
begin
split; intro h,
{ by_contra hmem,
simp only [set.mem_inter_eq, not_and, not_not, function.mem_mul_support] at hmem,
refine h _,
by_cases a β s,
{ simp_rw [set.mul_indicator, if_pos h],
exact hmem h },
{ simp_rw [set.mul_indicator, if_neg h] } },
{ simp_rw [set.mul_indicator, if_pos h.1],
exact h.2 }
end
@[simp, to_additive] lemma mul_support_mul_indicator :
function.mul_support (s.mul_indicator f) = s β© function.mul_support f :=
ext $ Ξ» x, by simp [function.mem_mul_support, mul_indicator_apply_eq_one]
/-- If a multiplicative indicator function is not equal to one at a point, then that
point is in the set. -/
@[to_additive] lemma mem_of_mul_indicator_ne_one (h : mul_indicator s f a β 1) : a β s :=
not_imp_comm.1 (Ξ» hn, mul_indicator_of_not_mem hn f) h
@[to_additive] lemma eq_on_mul_indicator : eq_on (mul_indicator s f) f s :=
Ξ» x hx, mul_indicator_of_mem hx f
@[to_additive] lemma mul_support_mul_indicator_subset : mul_support (s.mul_indicator f) β s :=
Ξ» x hx, hx.imp_symm (Ξ» h, mul_indicator_of_not_mem h f)
@[simp, to_additive] lemma mul_indicator_mul_support : mul_indicator (mul_support f) f = f :=
mul_indicator_eq_self.2 subset.rfl
@[simp, to_additive] lemma mul_indicator_range_comp {ΞΉ : Sort*} (f : ΞΉ β Ξ±) (g : Ξ± β M) :
mul_indicator (range f) g β f = g β f :=
piecewise_range_comp _ _ _
@[to_additive] lemma mul_indicator_congr (h : eq_on f g s) :
mul_indicator s f = mul_indicator s g :=
funext $ Ξ»x, by { simp only [mul_indicator], split_ifs, { exact h h_1 }, refl }
@[simp, to_additive] lemma mul_indicator_univ (f : Ξ± β M) : mul_indicator (univ : set Ξ±) f = f :=
mul_indicator_eq_self.2 $ subset_univ _
@[simp, to_additive] lemma mul_indicator_empty (f : Ξ± β M) : mul_indicator (β
: set Ξ±) f = Ξ»a, 1 :=
mul_indicator_eq_one.2 $ disjoint_empty _
@[to_additive] lemma mul_indicator_empty' (f : Ξ± β M) : mul_indicator (β
: set Ξ±) f = 1 :=
mul_indicator_empty f
variable (M)
@[simp, to_additive] lemma mul_indicator_one (s : set Ξ±) :
mul_indicator s (Ξ»x, (1:M)) = Ξ»x, (1:M) :=
mul_indicator_eq_one.2 $ by simp only [mul_support_one, empty_disjoint]
@[simp, to_additive] lemma mul_indicator_one' {s : set Ξ±} : s.mul_indicator (1 : Ξ± β M) = 1 :=
mul_indicator_one M s
variable {M}
@[to_additive] lemma mul_indicator_mul_indicator (s t : set Ξ±) (f : Ξ± β M) :
mul_indicator s (mul_indicator t f) = mul_indicator (s β© t) f :=
funext $ Ξ»x, by { simp only [mul_indicator], split_ifs, repeat {simp * at * {contextual := tt}} }
@[simp, to_additive] lemma mul_indicator_inter_mul_support (s : set Ξ±) (f : Ξ± β M) :
mul_indicator (s β© mul_support f) f = mul_indicator s f :=
by rw [β mul_indicator_mul_indicator, mul_indicator_mul_support]
@[to_additive] lemma comp_mul_indicator (h : M β Ξ²) (f : Ξ± β M) {s : set Ξ±} {x : Ξ±} :
h (s.mul_indicator f x) = s.piecewise (h β f) (const Ξ± (h 1)) x :=
s.apply_piecewise _ _ (Ξ» _, h)
@[to_additive] lemma mul_indicator_comp_right {s : set Ξ±} (f : Ξ² β Ξ±) {g : Ξ± β M} {x : Ξ²} :
mul_indicator (f β»ΒΉ' s) (g β f) x = mul_indicator s g (f x) :=
by { simp only [mul_indicator], split_ifs; refl }
@[to_additive] lemma mul_indicator_comp_of_one {g : M β N} (hg : g 1 = 1) :
mul_indicator s (g β f) = g β (mul_indicator s f) :=
begin
funext,
simp only [mul_indicator],
split_ifs; simp [*]
end
@[to_additive] lemma comp_mul_indicator_const (c : M) (f : M β N) (hf : f 1 = 1) :
(Ξ» x, f (s.mul_indicator (Ξ» x, c) x)) = s.mul_indicator (Ξ» x, f c) :=
(mul_indicator_comp_of_one hf).symm
@[to_additive] lemma mul_indicator_preimage (s : set Ξ±) (f : Ξ± β M) (B : set M) :
(mul_indicator s f)β»ΒΉ' B = s.ite (f β»ΒΉ' B) (1 β»ΒΉ' B) :=
piecewise_preimage s f 1 B
@[to_additive] lemma mul_indicator_preimage_of_not_mem (s : set Ξ±) (f : Ξ± β M)
{t : set M} (ht : (1:M) β t) :
(mul_indicator s f)β»ΒΉ' t = f β»ΒΉ' t β© s :=
by simp [mul_indicator_preimage, pi.one_def, set.preimage_const_of_not_mem ht]
@[to_additive] lemma mem_range_mul_indicator {r : M} {s : set Ξ±} {f : Ξ± β M} :
r β range (mul_indicator s f) β (r = 1 β§ s β univ) β¨ (r β f '' s) :=
by simp [mul_indicator, ite_eq_iff, exists_or_distrib, eq_univ_iff_forall, and_comm, or_comm,
@eq_comm _ r 1]
@[to_additive] lemma mul_indicator_rel_mul_indicator {r : M β M β Prop} (h1 : r 1 1)
(ha : a β s β r (f a) (g a)) :
r (mul_indicator s f a) (mul_indicator s g a) :=
by { simp only [mul_indicator], split_ifs with has has, exacts [ha has, h1] }
end has_one
section monoid
variables [mul_one_class M] {s t : set Ξ±} {f g : Ξ± β M} {a : Ξ±}
@[to_additive] lemma mul_indicator_union_mul_inter_apply (f : Ξ± β M) (s t : set Ξ±) (a : Ξ±) :
mul_indicator (s βͺ t) f a * mul_indicator (s β© t) f a =
mul_indicator s f a * mul_indicator t f a :=
by by_cases hs : a β s; by_cases ht : a β t; simp *
@[to_additive] lemma mul_indicator_union_mul_inter (f : Ξ± β M) (s t : set Ξ±) :
mul_indicator (s βͺ t) f * mul_indicator (s β© t) f = mul_indicator s f * mul_indicator t f :=
funext $ mul_indicator_union_mul_inter_apply f s t
@[to_additive] lemma mul_indicator_union_of_not_mem_inter (h : a β s β© t) (f : Ξ± β M) :
mul_indicator (s βͺ t) f a = mul_indicator s f a * mul_indicator t f a :=
by rw [β mul_indicator_union_mul_inter_apply f s t, mul_indicator_of_not_mem h, mul_one]
@[to_additive] lemma mul_indicator_union_of_disjoint (h : disjoint s t) (f : Ξ± β M) :
mul_indicator (s βͺ t) f = Ξ»a, mul_indicator s f a * mul_indicator t f a :=
funext $ Ξ»a, mul_indicator_union_of_not_mem_inter (Ξ» ha, h ha) _
@[to_additive] lemma mul_indicator_mul (s : set Ξ±) (f g : Ξ± β M) :
mul_indicator s (Ξ»a, f a * g a) = Ξ»a, mul_indicator s f a * mul_indicator s g a :=
by { funext, simp only [mul_indicator], split_ifs, { refl }, rw mul_one }
@[simp, to_additive] lemma mul_indicator_compl_mul_self_apply (s : set Ξ±) (f : Ξ± β M) (a : Ξ±) :
mul_indicator sαΆ f a * mul_indicator s f a = f a :=
classical.by_cases (Ξ» ha : a β s, by simp [ha]) (Ξ» ha, by simp [ha])
@[simp, to_additive] lemma mul_indicator_compl_mul_self (s : set Ξ±) (f : Ξ± β M) :
mul_indicator sαΆ f * mul_indicator s f = f :=
funext $ mul_indicator_compl_mul_self_apply s f
@[simp, to_additive] lemma mul_indicator_self_mul_compl_apply (s : set Ξ±) (f : Ξ± β M) (a : Ξ±) :
mul_indicator s f a * mul_indicator sαΆ f a = f a :=
classical.by_cases (Ξ» ha : a β s, by simp [ha]) (Ξ» ha, by simp [ha])
@[simp, to_additive] lemma mul_indicator_self_mul_compl (s : set Ξ±) (f : Ξ± β M) :
mul_indicator s f * mul_indicator sαΆ f = f :=
funext $ mul_indicator_self_mul_compl_apply s f
@[to_additive] lemma mul_indicator_mul_eq_left {f g : Ξ± β M}
(h : disjoint (mul_support f) (mul_support g)) :
(mul_support f).mul_indicator (f * g) = f :=
begin
refine (mul_indicator_congr $ Ξ» x hx, _).trans mul_indicator_mul_support,
have : g x = 1, from nmem_mul_support.1 (disjoint_left.1 h hx),
rw [pi.mul_apply, this, mul_one]
end
@[to_additive] lemma mul_indicator_mul_eq_right {f g : Ξ± β M}
(h : disjoint (mul_support f) (mul_support g)) :
(mul_support g).mul_indicator (f * g) = g :=
begin
refine (mul_indicator_congr $ Ξ» x hx, _).trans mul_indicator_mul_support,
have : f x = 1, from nmem_mul_support.1 (disjoint_right.1 h hx),
rw [pi.mul_apply, this, one_mul]
end
/-- `set.mul_indicator` as a `monoid_hom`. -/
@[to_additive "`set.indicator` as an `add_monoid_hom`."]
def mul_indicator_hom {Ξ±} (M) [mul_one_class M] (s : set Ξ±) : (Ξ± β M) β* (Ξ± β M) :=
{ to_fun := mul_indicator s,
map_one' := mul_indicator_one M s,
map_mul' := mul_indicator_mul s }
end monoid
section distrib_mul_action
variables {A : Type*} [add_monoid A] [monoid M] [distrib_mul_action M A]
lemma indicator_smul_apply (s : set Ξ±) (r : M) (f : Ξ± β A) (x : Ξ±) :
indicator s (Ξ» x, r β’ f x) x = r β’ indicator s f x :=
by { dunfold indicator, split_ifs, exacts [rfl, (smul_zero r).symm] }
lemma indicator_smul (s : set Ξ±) (r : M) (f : Ξ± β A) :
indicator s (Ξ» (x : Ξ±), r β’ f x) = Ξ» (x : Ξ±), r β’ indicator s f x :=
funext $ indicator_smul_apply s r f
end distrib_mul_action
section group
variables {G : Type*} [group G] {s t : set Ξ±} {f g : Ξ± β G} {a : Ξ±}
@[to_additive] lemma mul_indicator_inv' (s : set Ξ±) (f : Ξ± β G) :
mul_indicator s (fβ»ΒΉ) = (mul_indicator s f)β»ΒΉ :=
(mul_indicator_hom G s).map_inv f
@[to_additive] lemma mul_indicator_inv (s : set Ξ±) (f : Ξ± β G) :
mul_indicator s (Ξ»a, (f a)β»ΒΉ) = Ξ»a, (mul_indicator s f a)β»ΒΉ :=
mul_indicator_inv' s f
lemma indicator_sub {G} [add_group G] (s : set Ξ±) (f g : Ξ± β G) :
indicator s (Ξ»a, f a - g a) = Ξ»a, indicator s f a - indicator s g a :=
(indicator_hom G s).map_sub f g
@[to_additive indicator_compl'] lemma mul_indicator_compl (s : set Ξ±) (f : Ξ± β G) :
mul_indicator sαΆ f = f * (mul_indicator s f)β»ΒΉ :=
eq_mul_inv_of_mul_eq $ s.mul_indicator_compl_mul_self f
lemma indicator_compl {G} [add_group G] (s : set Ξ±) (f : Ξ± β G) :
indicator sαΆ f = f - indicator s f :=
by rw [sub_eq_add_neg, indicator_compl']
@[to_additive indicator_diff'] lemma mul_indicator_diff (h : s β t) (f : Ξ± β G) :
mul_indicator (t \ s) f = mul_indicator t f * (mul_indicator s f)β»ΒΉ :=
eq_mul_inv_of_mul_eq $ by rw [pi.mul_def, β mul_indicator_union_of_disjoint disjoint_diff.symm f,
diff_union_self, union_eq_self_of_subset_right h]
lemma indicator_diff {G : Type*} [add_group G] {s t : set Ξ±} (h : s β t) (f : Ξ± β G) :
indicator (t \ s) f = indicator t f - indicator s f :=
by rw [indicator_diff' h, sub_eq_add_neg]
end group
section comm_monoid
variables [comm_monoid M]
/-- Consider a product of `g i (f i)` over a `finset`. Suppose `g` is a
function such as `pow`, which maps a second argument of `1` to
`1`. Then if `f` is replaced by the corresponding multiplicative indicator
function, the `finset` may be replaced by a possibly larger `finset`
without changing the value of the sum. -/
@[to_additive] lemma prod_mul_indicator_subset_of_eq_one [has_one N] (f : Ξ± β N)
(g : Ξ± β N β M) {s t : finset Ξ±} (h : s β t) (hg : β a, g a 1 = 1) :
β i in s, g i (f i) = β i in t, g i (mul_indicator βs f i) :=
begin
rw β finset.prod_subset h _,
{ apply finset.prod_congr rfl,
intros i hi,
congr,
symmetry,
exact mul_indicator_of_mem hi _ },
{ refine Ξ» i hi hn, _,
convert hg i,
exact mul_indicator_of_not_mem hn _ }
end
/-- Consider a sum of `g i (f i)` over a `finset`. Suppose `g` is a
function such as multiplication, which maps a second argument of 0 to
0. (A typical use case would be a weighted sum of `f i * h i` or `f i
β’ h i`, where `f` gives the weights that are multiplied by some other
function `h`.) Then if `f` is replaced by the corresponding indicator
function, the `finset` may be replaced by a possibly larger `finset`
without changing the value of the sum. -/
add_decl_doc set.sum_indicator_subset_of_eq_zero
@[to_additive] lemma prod_mul_indicator_subset (f : Ξ± β M) {s t : finset Ξ±} (h : s β t) :
β i in s, f i = β i in t, mul_indicator βs f i :=
prod_mul_indicator_subset_of_eq_one _ (Ξ» a b, b) h (Ξ» _, rfl)
/-- Summing an indicator function over a possibly larger `finset` is
the same as summing the original function over the original
`finset`. -/
add_decl_doc sum_indicator_subset
@[to_additive] lemma _root_.finset.prod_mul_indicator_eq_prod_filter
(s : finset ΞΉ) (f : ΞΉ β Ξ± β M) (t : ΞΉ β set Ξ±) (g : ΞΉ β Ξ±) :
β i in s, mul_indicator (t i) (f i) (g i) = β i in s.filter (Ξ» i, g i β t i), f i (g i) :=
begin
refine (finset.prod_filter_mul_prod_filter_not s (Ξ» i, g i β t i) _).symm.trans _,
refine eq.trans _ (mul_one _),
exact congr_arg2 (*)
(finset.prod_congr rfl $ Ξ» x hx, mul_indicator_of_mem (finset.mem_filter.1 hx).2 _)
(finset.prod_eq_one $ Ξ» x hx, mul_indicator_of_not_mem (finset.mem_filter.1 hx).2 _)
end
@[to_additive] lemma mul_indicator_finset_prod (I : finset ΞΉ) (s : set Ξ±) (f : ΞΉ β Ξ± β M) :
mul_indicator s (β i in I, f i) = β i in I, mul_indicator s (f i) :=
(mul_indicator_hom M s).map_prod _ _
@[to_additive] lemma mul_indicator_finset_bUnion {ΞΉ} (I : finset ΞΉ)
(s : ΞΉ β set Ξ±) {f : Ξ± β M} : (β (i β I) (j β I), i β j β disjoint (s i) (s j)) β
mul_indicator (β i β I, s i) f = Ξ» a, β i in I, mul_indicator (s i) f a :=
begin
refine finset.induction_on I _ _,
{ intro h, funext, simp },
assume a I haI ih hI,
funext,
rw [finset.prod_insert haI, finset.set_bUnion_insert, mul_indicator_union_of_not_mem_inter, ih _],
{ assume i hi j hj hij,
exact hI i (finset.mem_insert_of_mem hi) j (finset.mem_insert_of_mem hj) hij },
simp only [not_exists, exists_prop, mem_Union, mem_inter_eq, not_and],
assume hx a' ha',
refine disjoint_left.1 (hI a (finset.mem_insert_self _ _) a' (finset.mem_insert_of_mem ha') _) hx,
exact (ne_of_mem_of_not_mem ha' haI).symm
end
end comm_monoid
section mul_zero_class
variables [mul_zero_class M] {s t : set Ξ±} {f g : Ξ± β M} {a : Ξ±}
lemma indicator_mul (s : set Ξ±) (f g : Ξ± β M) :
indicator s (Ξ»a, f a * g a) = Ξ»a, indicator s f a * indicator s g a :=
by { funext, simp only [indicator], split_ifs, { refl }, rw mul_zero }
lemma indicator_mul_left (s : set Ξ±) (f g : Ξ± β M) :
indicator s (Ξ»a, f a * g a) a = indicator s f a * g a :=
by { simp only [indicator], split_ifs, { refl }, rw [zero_mul] }
lemma indicator_mul_right (s : set Ξ±) (f g : Ξ± β M) :
indicator s (Ξ»a, f a * g a) a = f a * indicator s g a :=
by { simp only [indicator], split_ifs, { refl }, rw [mul_zero] }
lemma inter_indicator_mul {t1 t2 : set Ξ±} (f g : Ξ± β M) (x : Ξ±) :
(t1 β© t2).indicator (Ξ» x, f x * g x) x = t1.indicator f x * t2.indicator g x :=
by { rw [β set.indicator_indicator], simp [indicator] }
end mul_zero_class
section monoid_with_zero
variables [monoid_with_zero M]
lemma indicator_prod_one {s : set Ξ±} {t : set Ξ²} {x : Ξ±} {y : Ξ²} :
(s.prod t).indicator (1 : _ β M) (x, y) = s.indicator 1 x * t.indicator 1 y :=
by simp [indicator, β ite_and]
end monoid_with_zero
section order
variables [has_one M] [preorder M] {s t : set Ξ±} {f g : Ξ± β M} {a : Ξ±} {y : M}
@[to_additive] lemma mul_indicator_apply_le' (hfg : a β s β f a β€ y) (hg : a β s β 1 β€ y) :
mul_indicator s f a β€ y :=
if ha : a β s then by simpa [ha] using hfg ha else by simpa [ha] using hg ha
@[to_additive] lemma mul_indicator_le' (hfg : β a β s, f a β€ g a) (hg : β a β s, 1 β€ g a) :
mul_indicator s f β€ g :=
Ξ» a, mul_indicator_apply_le' (hfg _) (hg _)
@[to_additive] lemma le_mul_indicator_apply {y} (hfg : a β s β y β€ g a) (hf : a β s β y β€ 1) :
y β€ mul_indicator s g a :=
@mul_indicator_apply_le' Ξ± (order_dual M) βΉ_βΊ _ _ _ _ _ hfg hf
@[to_additive] lemma le_mul_indicator (hfg : β a β s, f a β€ g a) (hf : β a β s, f a β€ 1) :
f β€ mul_indicator s g :=
Ξ» a, le_mul_indicator_apply (hfg _) (hf _)
@[to_additive indicator_apply_nonneg]
lemma one_le_mul_indicator_apply (h : a β s β 1 β€ f a) : 1 β€ mul_indicator s f a :=
le_mul_indicator_apply h (Ξ» _, le_rfl)
@[to_additive indicator_nonneg]
lemma one_le_mul_indicator (h : β a β s, 1 β€ f a) (a : Ξ±) : 1 β€ mul_indicator s f a :=
one_le_mul_indicator_apply (h a)
@[to_additive] lemma mul_indicator_apply_le_one (h : a β s β f a β€ 1) : mul_indicator s f a β€ 1 :=
mul_indicator_apply_le' h (Ξ» _, le_rfl)
@[to_additive] lemma mul_indicator_le_one (h : β a β s, f a β€ 1) (a : Ξ±) :
mul_indicator s f a β€ 1 :=
mul_indicator_apply_le_one (h a)
@[to_additive] lemma mul_indicator_le_mul_indicator (h : f a β€ g a) :
mul_indicator s f a β€ mul_indicator s g a :=
mul_indicator_rel_mul_indicator (le_refl _) (Ξ» _, h)
attribute [mono] mul_indicator_le_mul_indicator indicator_le_indicator
@[to_additive] lemma mul_indicator_le_mul_indicator_of_subset (h : s β t) (hf : β a, 1 β€ f a)
(a : Ξ±) :
mul_indicator s f a β€ mul_indicator t f a :=
mul_indicator_apply_le' (Ξ» ha, le_mul_indicator_apply (Ξ» _, le_rfl) (Ξ» hat, (hat $ h ha).elim))
(Ξ» ha, one_le_mul_indicator_apply (Ξ» _, hf _))
@[to_additive] lemma mul_indicator_le_self' (hf : β x β s, 1 β€ f x) : mul_indicator s f β€ f :=
mul_indicator_le' (Ξ» _ _, le_refl _) hf
@[to_additive] lemma mul_indicator_Union_apply {ΞΉ M} [complete_lattice M] [has_one M]
(h1 : (β₯:M) = 1) (s : ΞΉ β set Ξ±) (f : Ξ± β M) (x : Ξ±) :
mul_indicator (β i, s i) f x = β¨ i, mul_indicator (s i) f x :=
begin
by_cases hx : x β β i, s i,
{ rw [mul_indicator_of_mem hx],
rw [mem_Union] at hx,
refine le_antisymm _ (supr_le $ Ξ» i, mul_indicator_le_self' (Ξ» x hx, h1 βΈ bot_le) x),
rcases hx with β¨i, hiβ©,
exact le_supr_of_le i (ge_of_eq $ mul_indicator_of_mem hi _) },
{ rw [mul_indicator_of_not_mem hx],
simp only [mem_Union, not_exists] at hx,
simp [hx, β h1] }
end
end order
section canonically_ordered_monoid
variables [canonically_ordered_monoid M]
@[to_additive] lemma mul_indicator_le_self (s : set Ξ±) (f : Ξ± β M) :
mul_indicator s f β€ f :=
mul_indicator_le_self' $ Ξ» _ _, one_le _
@[to_additive] lemma mul_indicator_apply_le {a : Ξ±} {s : set Ξ±} {f g : Ξ± β M}
(hfg : a β s β f a β€ g a) :
mul_indicator s f a β€ g a :=
mul_indicator_apply_le' hfg $ Ξ» _, one_le _
@[to_additive] lemma mul_indicator_le {s : set Ξ±} {f g : Ξ± β M} (hfg : β a β s, f a β€ g a) :
mul_indicator s f β€ g :=
mul_indicator_le' hfg $ Ξ» _ _, one_le _
end canonically_ordered_monoid
lemma indicator_le_indicator_nonneg {Ξ²} [linear_order Ξ²] [has_zero Ξ²] (s : set Ξ±) (f : Ξ± β Ξ²) :
s.indicator f β€ {x | 0 β€ f x}.indicator f :=
begin
intro x,
simp_rw indicator_apply,
split_ifs,
{ exact le_rfl, },
{ exact (not_le.mp h_1).le, },
{ exact h_1, },
{ exact le_rfl, },
end
lemma indicator_nonpos_le_indicator {Ξ²} [linear_order Ξ²] [has_zero Ξ²] (s : set Ξ±) (f : Ξ± β Ξ²) :
{x | f x β€ 0}.indicator f β€ s.indicator f :=
@indicator_le_indicator_nonneg Ξ± (order_dual Ξ²) _ _ s f
end set
@[to_additive] lemma monoid_hom.map_mul_indicator {M N : Type*} [monoid M] [monoid N] (f : M β* N)
(s : set Ξ±) (g : Ξ± β M) (x : Ξ±) :
f (s.mul_indicator g x) = s.mul_indicator (f β g) x :=
congr_fun (set.mul_indicator_comp_of_one f.map_one).symm x
|
1f6e459c1ffb2afb220396f9afad11fd5d0bb53f | 4727251e0cd73359b15b664c3170e5d754078599 | /test/conv/apply_congr.lean | ed8b88f80122615d3b4eaffa12dd29d78631ed08 | [
"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 | 3,162 | lean | /-
Copyright (c) 2019 Lucas Allen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Lucas Allen, Scott Morrison
-/
import algebra.big_operators.basic
import data.finsupp.basic
import tactic.converter.apply_congr
import tactic.interactive
example (f g : β€ β β€) (S : finset β€) (h : β m β S, f m = g m) :
finset.sum S f = finset.sum S g :=
begin
conv_lhs {
-- If we just call `congr` here, in the second goal we're helpless,
-- because we are only given the opportunity to rewrite `f`.
-- However `apply_congr` uses the appropriate `@[congr]` lemma,
-- so we get to rewrite `f x`, in the presence of the crucial `H : x β S` hypothesis.
apply_congr,
skip,
simp [h, H], }
end
-- Again, with some `guard` statements.
example (f g : β€ β β€) (S : finset β€) (h : β m β S, f m = g m) :
finset.sum S f = finset.sum S g :=
begin
conv_lhs {
apply_congr finset.sum_congr,
-- (See the note about get_goals/set_goals inside apply_congr)
(do ng β tactic.num_goals, guard $ ng = 2),
guard_target S,
skip,
guard_target f x,
simp [h, H] }
end
-- Verify we can `rw` as well as `simp`.
example (f g : β€ β β€) (S : finset β€) (h : β m β S, f m = g m) :
finset.sum S f = finset.sum S g :=
by conv_lhs { apply_congr, skip, rw h x H, }
-- Check that the appropriate `@[congr]` lemma is automatically selected.
example (f g : β€ β β€) (S : finset β€) (h : β m β S, f m = g m) :
finset.prod S f = finset.prod S g :=
by conv_lhs { apply_congr, skip, simp [h, H], }
example (f g : β€ β β€) (S : finset β€) (h : β m β S, f m = g m) :
finset.fold (+) 0 f S = finset.fold (+) 0 g S :=
begin
-- This time, the automatically selected congruence lemma is "too good"!
-- `finset.sum_congr` matches, and so the `conv` block actually
-- rewrites the left hand side into a `finset.sum`.
conv_lhs { apply_congr, skip, simp [h, H], },
-- So we need a `refl` to identify that we're done.
refl,
end
-- This can be avoided by selecting the congruence lemma by hand.
example (f g : β€ β β€) (S : finset β€) (h : β m β S, f m = g m) :
finset.fold (+) 0 f S = finset.fold (+) 0 g S :=
begin
conv_lhs { apply_congr finset.fold_congr, simp [h, H], },
end
example (f : β€ β β€) (S : finset β€) (h : β m β S, f m = 0) :
finset.sum S f = 0 :=
begin
conv_lhs { apply_congr, skip, simp [h, H], },
simp,
end
-- An example using `finsupp.sum`
open_locale classical
example {k G : Type} [semiring k] [group G]
(g : G ββ k) (aβ x : G) (bβ : k)
(t : β (aβ : G), aβ * aβ = x β aββ»ΒΉ * x = aβ) :
g.sum (Ξ» (aβ : G) (bβ : k), ite (aβ * aβ = x) (bβ * bβ) 0) = bβ * g (aββ»ΒΉ * x) :=
begin
-- In fact, `congr` works fine here, because our rewrite works globally.
conv_lhs { apply_congr, skip, dsimp, rw t, },
rw finset.sum_ite_eq g.support, -- it's a pity we can't just use `simp` here.
split_ifs,
{ refl, },
{ simp [finsupp.not_mem_support_iff.1 h], },
end
example : true :=
begin
success_if_fail { conv { apply_congr, }, },
trivial
end
|
0093067cf911c5fb025b74a8062c5b42391572eb | 9be442d9ec2fcf442516ed6e9e1660aa9071b7bd | /tests/lean/run/944.lean | 0bf6ecba1ec7743bef54c2e769914ba7f1a717d2 | [
"Apache-2.0",
"LLVM-exception",
"NCSA",
"LGPL-3.0-only",
"LicenseRef-scancode-inner-net-2.0",
"BSD-3-Clause",
"LGPL-2.0-or-later",
"Spencer-94",
"LGPL-2.1-or-later",
"HPND",
"LicenseRef-scancode-pcre",
"ISC",
"LGPL-2.1-only",
"LicenseRef-scancode-other-permissive",
"SunPro",
"CMU-Mach"... | permissive | EdAyers/lean4 | 57ac632d6b0789cb91fab2170e8c9e40441221bd | 37ba0df5841bde51dbc2329da81ac23d4f6a4de4 | refs/heads/master | 1,676,463,245,298 | 1,660,619,433,000 | 1,660,619,433,000 | 183,433,437 | 1 | 0 | Apache-2.0 | 1,657,612,672,000 | 1,556,196,574,000 | Lean | UTF-8 | Lean | false | false | 805 | lean | import Lean
def f1 (x : Nat) : Except String Nat :=
if x > 0 then
.ok x
else
.error "argument is zero"
namespace Lean.Elab
open Lsp
def identOf : Info β Option (RefIdent Γ Bool)
| .ofTermInfo ti => match ti.expr with
| .const n .. => some (.const n, ti.isBinder)
| .fvar id .. => some (.fvar id, ti.isBinder)
| _ => none
| .ofFieldInfo fi => some (.const fi.projName, false)
| _ => none
def isConst (e : Expr) : Bool :=
e matches .const ..
def isImplicit (bi : BinderInfo) : Bool :=
bi matches .implicit
end Lean.Elab
def f2 (xs : List Nat) : List Nat :=
.map (Β· + 1) xs
def f2' (xs : List Nat) : List Nat :=
.map .succ xs
def f3 : Nat :=
.zero
def f4 (x : Nat) : Nat :=
.succ x
example (xs : List Ξ±) : Std.RBTree Ξ± ord :=
xs.foldl .insert β
|
5cec13c41434446638540502c5efba2f9d3e4532 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/algebra/direct_sum/internal.lean | 2ceb49d35e64596a35381a075be0080a698eb55e | [
"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,198 | lean | /-
Copyright (c) 2021 Eric Wieser. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Eric Wieser, Kevin Buzzard, Jujian Zhang
-/
import algebra.algebra.operations
import algebra.algebra.subalgebra.basic
import algebra.direct_sum.algebra
/-!
# Internally graded rings and algebras
This module provides `gsemiring` and `gcomm_semiring` instances for a collection of subobjects `A`
when a `set_like.graded_monoid` instance is available:
* `set_like.gnon_unital_non_assoc_semiring`
* `set_like.gsemiring`
* `set_like.gcomm_semiring`
With these instances in place, it provides the bundled canonical maps out of a direct sum of
subobjects into their carrier type:
* `direct_sum.coe_ring_hom` (a `ring_hom` version of `direct_sum.coe_add_monoid_hom`)
* `direct_sum.coe_alg_hom` (an `alg_hom` version of `direct_sum.submodule_coe`)
Strictly the definitions in this file are not sufficient to fully define an "internal" direct sum;
to represent this case, `(h : direct_sum.is_internal A) [set_like.graded_monoid A]` is
needed. In the future there will likely be a data-carrying, constructive, typeclass version of
`direct_sum.is_internal` for providing an explicit decomposition function.
When `complete_lattice.independent (set.range A)` (a weaker condition than
`direct_sum.is_internal A`), these provide a grading of `β¨ i, A i`, and the
mapping `β¨ i, A i β+ β¨ i, A i` can be obtained as
`direct_sum.to_monoid (Ξ» i, add_submonoid.inclusion $ le_supr A i)`.
## tags
internally graded ring
-/
open_locale direct_sum big_operators
variables {ΞΉ : Type*} {Ο S R : Type*}
instance add_comm_monoid.of_submonoid_on_semiring [semiring R] [set_like Ο R]
[add_submonoid_class Ο R] (A : ΞΉ β Ο) : β i, add_comm_monoid (A i) :=
Ξ» i, by apply_instance
instance add_comm_group.of_subgroup_on_ring [ring R] [set_like Ο R]
[add_subgroup_class Ο R] (A : ΞΉ β Ο) : β i, add_comm_group (A i) :=
Ξ» i, by apply_instance
lemma set_like.algebra_map_mem_graded [has_zero ΞΉ]
[comm_semiring S] [semiring R] [algebra S R]
(A : ΞΉ β submodule S R) [set_like.has_graded_one A] (s : S) : algebra_map S R s β A 0 :=
begin
rw algebra.algebra_map_eq_smul_one,
exact ((A 0).smul_mem s $ set_like.one_mem_graded _),
end
lemma set_like.nat_cast_mem_graded [has_zero ΞΉ] [add_monoid_with_one R]
[set_like Ο R] [add_submonoid_class Ο R] (A : ΞΉ β Ο) [set_like.has_graded_one A] (n : β) :
(n : R) β A 0 :=
begin
induction n,
{ rw nat.cast_zero,
exact zero_mem (A 0), },
{ rw nat.cast_succ,
exact add_mem n_ih (set_like.one_mem_graded _), },
end
lemma set_like.int_cast_mem_graded [has_zero ΞΉ] [add_group_with_one R]
[set_like Ο R] [add_subgroup_class Ο R] (A : ΞΉ β Ο) [set_like.has_graded_one A] (z : β€) :
(z : R) β A 0:=
begin
induction z,
{ rw int.cast_of_nat,
exact set_like.nat_cast_mem_graded _ _, },
{ rw int.cast_neg_succ_of_nat,
exact neg_mem (set_like.nat_cast_mem_graded _ _), },
end
section direct_sum
variables [decidable_eq ΞΉ]
/-! #### From `add_submonoid`s and `add_subgroup`s -/
namespace set_like
/-- Build a `gnon_unital_non_assoc_semiring` instance for a collection of additive submonoids. -/
instance gnon_unital_non_assoc_semiring [has_add ΞΉ] [non_unital_non_assoc_semiring R]
[set_like Ο R] [add_submonoid_class Ο R]
(A : ΞΉ β Ο) [set_like.has_graded_mul A] :
direct_sum.gnon_unital_non_assoc_semiring (Ξ» i, A i) :=
{ mul_zero := Ξ» i j _, subtype.ext (mul_zero _),
zero_mul := Ξ» i j _, subtype.ext (zero_mul _),
mul_add := Ξ» i j _ _ _, subtype.ext (mul_add _ _ _),
add_mul := Ξ» i j _ _ _, subtype.ext (add_mul _ _ _),
..set_like.ghas_mul A }
/-- Build a `gsemiring` instance for a collection of additive submonoids. -/
instance gsemiring [add_monoid ΞΉ] [semiring R] [set_like Ο R] [add_submonoid_class Ο R]
(A : ΞΉ β Ο) [set_like.graded_monoid A] :
direct_sum.gsemiring (Ξ» i, A i) :=
{ mul_zero := Ξ» i j _, subtype.ext (mul_zero _),
zero_mul := Ξ» i j _, subtype.ext (zero_mul _),
mul_add := Ξ» i j _ _ _, subtype.ext (mul_add _ _ _),
add_mul := Ξ» i j _ _ _, subtype.ext (add_mul _ _ _),
nat_cast := Ξ» n, β¨n, set_like.nat_cast_mem_graded _ _β©,
nat_cast_zero := subtype.ext nat.cast_zero,
nat_cast_succ := Ξ» n, subtype.ext (nat.cast_succ n),
..set_like.gmonoid A }
/-- Build a `gcomm_semiring` instance for a collection of additive submonoids. -/
instance gcomm_semiring [add_comm_monoid ΞΉ] [comm_semiring R] [set_like Ο R]
[add_submonoid_class Ο R] (A : ΞΉ β Ο) [set_like.graded_monoid A] :
direct_sum.gcomm_semiring (Ξ» i, A i) :=
{ ..set_like.gcomm_monoid A,
..set_like.gsemiring A, }
/-- Build a `gring` instance for a collection of additive subgroups. -/
instance gring [add_monoid ΞΉ] [ring R] [set_like Ο R] [add_subgroup_class Ο R]
(A : ΞΉ β Ο) [set_like.graded_monoid A] :
direct_sum.gring (Ξ» i, A i) :=
{ int_cast := Ξ» z, β¨z, set_like.int_cast_mem_graded _ _β©,
int_cast_of_nat := Ξ» n, subtype.ext $ int.cast_of_nat _,
int_cast_neg_succ_of_nat := Ξ» n, subtype.ext $ int.cast_neg_succ_of_nat n,
..set_like.gsemiring A }
/-- Build a `gcomm_semiring` instance for a collection of additive submonoids. -/
instance gcomm_ring [add_comm_monoid ΞΉ] [comm_ring R] [set_like Ο R]
[add_subgroup_class Ο R] (A : ΞΉ β Ο) [set_like.graded_monoid A] :
direct_sum.gcomm_ring (Ξ» i, A i) :=
{ ..set_like.gcomm_monoid A,
..set_like.gring A, }
end set_like
namespace direct_sum
section coe
variables [semiring R] [set_like Ο R] [add_submonoid_class Ο R] (A : ΞΉ β Ο)
/-- The canonical ring isomorphism between `β¨ i, A i` and `R`-/
def coe_ring_hom [add_monoid ΞΉ] [set_like.graded_monoid A] : (β¨ i, A i) β+* R :=
direct_sum.to_semiring (Ξ» i, add_submonoid_class.subtype (A i)) rfl (Ξ» _ _ _ _, rfl)
/-- The canonical ring isomorphism between `β¨ i, A i` and `R`-/
@[simp] lemma coe_ring_hom_of [add_monoid ΞΉ] [set_like.graded_monoid A] (i : ΞΉ)
(x : A i) : (coe_ring_hom A : _ β+* R) (of (Ξ» i, A i) i x) = x :=
direct_sum.to_semiring_of _ _ _ _ _
lemma coe_mul_apply [add_monoid ΞΉ] [set_like.graded_monoid A]
[Ξ (i : ΞΉ) (x : A i), decidable (x β 0)] (r r' : β¨ i, A i) (n : ΞΉ) :
((r * r') n : R) =
β ij in (r.support ΓΛ’ r'.support).filter (Ξ» ij : ΞΉ Γ ΞΉ, ij.1 + ij.2 = n), r ij.1 * r' ij.2 :=
begin
rw [mul_eq_sum_support_ghas_mul, dfinsupp.finset_sum_apply, add_submonoid_class.coe_finset_sum],
simp_rw [coe_of_apply, βfinset.sum_filter, set_like.coe_ghas_mul],
end
lemma coe_mul_apply_eq_dfinsupp_sum [add_monoid ΞΉ] [set_like.graded_monoid A]
[Ξ (i : ΞΉ) (x : A i), decidable (x β 0)] (r r' : β¨ i, A i) (n : ΞΉ) :
((r * r') n : R) = r.sum (Ξ» i ri, r'.sum (Ξ» j rj, if i + j = n then ri * rj else 0)) :=
begin
simp only [mul_eq_dfinsupp_sum, dfinsupp.sum_apply],
iterate 2 { rw [dfinsupp.sum, add_submonoid_class.coe_finset_sum], congr, ext },
dsimp only, split_ifs,
{ subst h, rw of_eq_same, refl },
{ rw of_eq_of_ne _ _ _ _ h, refl },
end
lemma coe_of_mul_apply_aux [add_monoid ΞΉ] [set_like.graded_monoid A] {i : ΞΉ}
(r : A i) (r' : β¨ i, A i) {j n : ΞΉ} (H : β (x : ΞΉ), i + x = n β x = j) :
((of _ i r * r') n : R) = r * r' j :=
begin
classical,
rw coe_mul_apply_eq_dfinsupp_sum,
apply (dfinsupp.sum_single_index _).trans, swap,
{ simp_rw [zero_mem_class.coe_zero, zero_mul, if_t_t], exact dfinsupp.sum_zero },
simp_rw [dfinsupp.sum, H, finset.sum_ite_eq'],
split_ifs, refl,
rw [dfinsupp.not_mem_support_iff.mp h, zero_mem_class.coe_zero, mul_zero],
end
lemma coe_mul_of_apply_aux [add_monoid ΞΉ] [set_like.graded_monoid A]
(r : β¨ i, A i) {i : ΞΉ} (r' : A i) {j n : ΞΉ} (H : β (x : ΞΉ), x + i = n β x = j) :
((r * of _ i r') n : R) = r j * r' :=
begin
classical,
rw [coe_mul_apply_eq_dfinsupp_sum, dfinsupp.sum_comm],
apply (dfinsupp.sum_single_index _).trans, swap,
{ simp_rw [zero_mem_class.coe_zero, mul_zero, if_t_t], exact dfinsupp.sum_zero },
simp_rw [dfinsupp.sum, H, finset.sum_ite_eq'],
split_ifs, refl,
rw [dfinsupp.not_mem_support_iff.mp h, zero_mem_class.coe_zero, zero_mul],
end
lemma coe_of_mul_apply_add [add_left_cancel_monoid ΞΉ] [set_like.graded_monoid A]
{i : ΞΉ} (r : A i) (r' : β¨ i, A i) (j : ΞΉ) :
((of _ i r * r') (i + j) : R) = r * r' j :=
coe_of_mul_apply_aux _ _ _ (Ξ» x, β¨Ξ» h, add_left_cancel h, Ξ» h, h βΈ rflβ©)
lemma coe_mul_of_apply_add [add_right_cancel_monoid ΞΉ] [set_like.graded_monoid A]
(r : β¨ i, A i) {i : ΞΉ} (r' : A i) (j : ΞΉ) :
((r * of _ i r') (j + i) : R) = r j * r' :=
coe_mul_of_apply_aux _ _ _ (Ξ» x, β¨Ξ» h, add_right_cancel h, Ξ» h, h βΈ rflβ©)
end coe
section canonically_ordered_add_monoid
variables [semiring R] [set_like Ο R] [add_submonoid_class Ο R] (A : ΞΉ β Ο)
variables [canonically_ordered_add_monoid ΞΉ] [set_like.graded_monoid A]
lemma coe_of_mul_apply_of_not_le
{i : ΞΉ} (r : A i) (r' : β¨ i, A i) (n : ΞΉ)
(h : Β¬ i β€ n) : ((of _ i r * r') n : R) = 0 :=
begin
classical,
rw coe_mul_apply_eq_dfinsupp_sum,
apply (dfinsupp.sum_single_index _).trans, swap,
{ simp_rw [zero_mem_class.coe_zero, zero_mul, if_t_t], exact dfinsupp.sum_zero },
{ rw [dfinsupp.sum, finset.sum_ite_of_false _ _ (Ξ» x _ H, _), finset.sum_const_zero],
exact h ((self_le_add_right i x).trans_eq H) },
end
lemma coe_mul_of_apply_of_not_le
(r : β¨ i, A i) {i : ΞΉ} (r' : A i) (n : ΞΉ)
(h : Β¬ i β€ n) : ((r * of _ i r') n : R) = 0 :=
begin
classical,
rw [coe_mul_apply_eq_dfinsupp_sum, dfinsupp.sum_comm],
apply (dfinsupp.sum_single_index _).trans, swap,
{ simp_rw [zero_mem_class.coe_zero, mul_zero, if_t_t], exact dfinsupp.sum_zero },
{ rw [dfinsupp.sum, finset.sum_ite_of_false _ _ (Ξ» x _ H, _), finset.sum_const_zero],
exact h ((self_le_add_left i x).trans_eq H) },
end
variables [has_sub ΞΉ] [has_ordered_sub ΞΉ] [contravariant_class ΞΉ ΞΉ (+) (β€)]
/- The following two lemmas only require the same hypotheses as `eq_tsub_iff_add_eq_of_le`, but we
state them for `canonically_ordered_add_monoid` + the above three typeclasses for convenience. -/
lemma coe_mul_of_apply_of_le (r : β¨ i, A i) {i : ΞΉ} (r' : A i) (n : ΞΉ)
(h : i β€ n) : ((r * of _ i r') n : R) = r (n - i) * r' :=
coe_mul_of_apply_aux _ _ _ (Ξ» x, (eq_tsub_iff_add_eq_of_le h).symm)
lemma coe_of_mul_apply_of_le {i : ΞΉ} (r : A i) (r' : β¨ i, A i) (n : ΞΉ)
(h : i β€ n) : ((of _ i r * r') n : R) = r * r' (n - i) :=
coe_of_mul_apply_aux _ _ _ (Ξ» x, by rw [eq_tsub_iff_add_eq_of_le h, add_comm])
lemma coe_mul_of_apply (r : β¨ i, A i) {i : ΞΉ} (r' : A i) (n : ΞΉ) [decidable (i β€ n)] :
((r * of _ i r') n : R) = if i β€ n then r (n - i) * r' else 0 :=
by { split_ifs, exacts [coe_mul_of_apply_of_le _ _ _ n h, coe_mul_of_apply_of_not_le _ _ _ n h] }
lemma coe_of_mul_apply {i : ΞΉ} (r : A i) (r' : β¨ i, A i) (n : ΞΉ) [decidable (i β€ n)] :
((of _ i r * r') n : R) = if i β€ n then r * r' (n - i) else 0 :=
by { split_ifs, exacts [coe_of_mul_apply_of_le _ _ _ n h, coe_of_mul_apply_of_not_le _ _ _ n h] }
end canonically_ordered_add_monoid
end direct_sum
/-! #### From `submodule`s -/
namespace submodule
/-- Build a `galgebra` instance for a collection of `submodule`s. -/
instance galgebra [add_monoid ΞΉ]
[comm_semiring S] [semiring R] [algebra S R]
(A : ΞΉ β submodule S R) [set_like.graded_monoid A] :
direct_sum.galgebra S (Ξ» i, A i) :=
{ to_fun := ((algebra.linear_map S R).cod_restrict (A 0) $
set_like.algebra_map_mem_graded A).to_add_monoid_hom,
map_one := subtype.ext $ by exact (algebra_map S R).map_one,
map_mul := Ξ» x y, sigma.subtype_ext (add_zero 0).symm $ (algebra_map S R).map_mul _ _,
commutes := Ξ» r β¨i, xiβ©,
sigma.subtype_ext ((zero_add i).trans (add_zero i).symm) $ algebra.commutes _ _,
smul_def := Ξ» r β¨i, xiβ©, sigma.subtype_ext (zero_add i).symm $ algebra.smul_def _ _ }
@[simp] lemma set_like.coe_galgebra_to_fun [add_monoid ΞΉ]
[comm_semiring S] [semiring R] [algebra S R]
(A : ΞΉ β submodule S R) [set_like.graded_monoid A] (s : S) :
β(@direct_sum.galgebra.to_fun _ S (Ξ» i, A i) _ _ _ _ _ _ _ s) = (algebra_map S R s : R) := rfl
/-- A direct sum of powers of a submodule of an algebra has a multiplicative structure. -/
instance nat_power_graded_monoid
[comm_semiring S] [semiring R] [algebra S R] (p : submodule S R) :
set_like.graded_monoid (Ξ» i : β, p ^ i) :=
{ one_mem := by { rw [βone_le, pow_zero], exact le_rfl },
mul_mem := Ξ» i j p q hp hq, by { rw pow_add, exact submodule.mul_mem_mul hp hq } }
end submodule
/-- The canonical algebra isomorphism between `β¨ i, A i` and `R`. -/
def direct_sum.coe_alg_hom [add_monoid ΞΉ]
[comm_semiring S] [semiring R] [algebra S R]
(A : ΞΉ β submodule S R) [set_like.graded_monoid A] : (β¨ i, A i) ββ[S] R :=
direct_sum.to_algebra S _ (Ξ» i, (A i).subtype) rfl (Ξ» _ _ _ _, rfl) (Ξ» _, rfl)
/-- The supremum of submodules that form a graded monoid is a subalgebra, and equal to the range of
`direct_sum.coe_alg_hom`. -/
lemma submodule.supr_eq_to_submodule_range [add_monoid ΞΉ]
[comm_semiring S] [semiring R] [algebra S R] (A : ΞΉ β submodule S R) [set_like.graded_monoid A] :
(β¨ i, A i) = (direct_sum.coe_alg_hom A).range.to_submodule :=
(submodule.supr_eq_range_dfinsupp_lsum A).trans $ set_like.coe_injective rfl
@[simp] lemma direct_sum.coe_alg_hom_of [add_monoid ΞΉ]
[comm_semiring S] [semiring R] [algebra S R]
(A : ΞΉ β submodule S R) [set_like.graded_monoid A] (i : ΞΉ) (x : A i) :
direct_sum.coe_alg_hom A (direct_sum.of (Ξ» i, A i) i x) = x :=
direct_sum.to_semiring_of _ rfl (Ξ» _ _ _ _, rfl) _ _
end direct_sum
section homogeneous_element
lemma set_like.is_homogeneous_zero_submodule [has_zero ΞΉ]
[semiring S] [add_comm_monoid R] [module S R]
(A : ΞΉ β submodule S R) : set_like.is_homogeneous A (0 : R) :=
β¨0, submodule.zero_mem _β©
lemma set_like.is_homogeneous.smul [comm_semiring S] [semiring R] [algebra S R]
{A : ΞΉ β submodule S R} {s : S}
{r : R} (hr : set_like.is_homogeneous A r) : set_like.is_homogeneous A (s β’ r) :=
let β¨i, hiβ© := hr in β¨i, submodule.smul_mem _ _ hiβ©
end homogeneous_element
|
77f3bf8bfde9e9b070a0a8307e7a8ad23be19c02 | c777c32c8e484e195053731103c5e52af26a25d1 | /src/analysis/normed_space/star/multiplier.lean | 039c59151145bccaa3684797273163273915768f | [
"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 | 25,927 | lean | /-
Copyright (c) 2022 Jireh Loreaux. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jireh Loreaux, Jon Bannon
-/
import algebra.star.star_alg_hom
import analysis.normed_space.star.basic
import analysis.normed_space.operator_norm
import analysis.special_functions.pow
import analysis.normed_space.star.mul
/-!
# Multiplier Algebra of a Cβ-algebra
Define the multiplier algebra of a Cβ-algebra as the algebra (over `π`) of double centralizers,
for which we provide the localized notation `π(π, A)`. A double centralizer is a pair of
continuous linear maps `L R : A βL[π] A` satisfying the intertwining condition `R x * y = x * L y`.
There is a natural embedding `A β π(π, A)` which sends `a : A` to the continuous linear maps
`L R : A βL[π] A` given by left and right multiplication by `a`, and we provide this map as a
coercion.
The multiplier algebra corresponds to a non-commutative StoneβΔech compactification in the sense
that when the algebra `A` is commutative, it can be identified with `Cβ(X, β)` for some locally
compact Hausdorff space `X`, and in that case `π(π, A)` can be identified with `C(Ξ² X, β)`.
## Implementation notes
We make the hypotheses on `π` as weak as possible so that, in particular, this construction works
for both `π = β` and `π = β`.
The reader familiar with Cβ-algebra theory may recognize that one
only needs `L` and `R` to be functions instead of continuous linear maps, at least when `A` is a
Cβ-algebra. Our intention is simply to eventually provide a constructor for this situation.
We pull back the `normed_algebra` structure (and everything contained therein) through the
ring (even algebra) homomorphism
`double_centralizer.to_prod_mul_opposite_hom : π(π, A) β+* (A βL[π] A) Γ (A βL[π] A)α΅α΅α΅` which
sends `a : π(π, A)` to `(a.fst, mul_opposite.op a.snd)`. The star structure is provided
separately.
## References
* https://en.wikipedia.org/wiki/Multiplier_algebra
## TODO
+ Define a type synonym for `π(π, A)` which is equipped with the strict uniform space structure
and show it is complete
+ Show that the image of `A` in `π(π, A)` is an essential ideal
+ Prove the universal property of `π(π, A)`
+ Construct a double centralizer from a pair of maps (not necessarily linear or continuous)
`L : A β A`, `R : A β A` satisfying the centrality condition `β x y, R x * y = x * L y`.
+ Show that if `A` is unital, then `A βββ[π] π(π, A)`.
-/
open_locale nnreal ennreal
open nnreal continuous_linear_map mul_opposite
universes u v
/-- The type of *double centralizers*, also known as the *multiplier algebra* and denoted by
`π(π, A)`, of a non-unital normed algebra.
If `x : π(π, A)`, then `x.fst` and `x.snd` are what is usually referred to as $L$ and $R$. -/
@[ext]
structure double_centralizer (π : Type u) (A : Type v) [nontrivially_normed_field π]
[non_unital_normed_ring A] [normed_space π A] [smul_comm_class π A A] [is_scalar_tower π A A]
extends (A βL[π] A) Γ (A βL[π] A) :=
(central : β x y : A, snd x * y = x * fst y)
localized "notation `π(` π `, ` A `)` := double_centralizer π A" in multiplier_algebra
namespace double_centralizer
section nontrivially_normed
variables (π A : Type*) [nontrivially_normed_field π] [non_unital_normed_ring A]
variables [normed_space π A] [smul_comm_class π A A] [is_scalar_tower π A A]
/-!
### Algebraic structure
Because the multiplier algebra is defined as the algebra of double centralizers, there is a natural
injection `double_centralizer.to_prod_mul_opposite : π(π, A) β (A βL[π] A) Γ (A βL[π] A)α΅α΅α΅`
defined by `Ξ» a, (a.fst, mul_opposite.op a.snd)`. We use this map to pull back the ring, module and
algebra structure from `(A βL[π] A) Γ (A βL[π] A)α΅α΅α΅` to `π(π, A)`. -/
variables {π A}
lemma range_to_prod : set.range to_prod = {lr : (A βL[π] A) Γ _ | β x y, lr.2 x * y = x * lr.1 y} :=
set.ext $ Ξ» x, β¨by {rintro β¨a, rflβ©, exact a.central}, Ξ» hx, β¨β¨x, hxβ©, rflβ©β©
instance : has_add π(π, A) :=
{ add := Ξ» a b,
{ to_prod := a.to_prod + b.to_prod,
central := Ξ» x y, show (a.snd + b.snd) x * y = x * (a.fst + b.fst) y,
by simp only [continuous_linear_map.add_apply, mul_add, add_mul, central] } }
instance : has_zero π(π, A) :=
{ zero :=
{ to_prod := 0,
central := Ξ» x y, (zero_mul y).trans (mul_zero x).symm } }
instance : has_neg π(π, A) :=
{ neg := Ξ» a,
{ to_prod := -a.to_prod,
central := Ξ» x y, show -a.snd x * y = x * -a.fst y,
by simp only [continuous_linear_map.neg_apply, neg_mul, mul_neg, central] } }
instance : has_sub π(π, A) :=
{ sub := Ξ» a b,
{ to_prod := a.to_prod - b.to_prod,
central := Ξ» x y, show (a.snd - b.snd) x * y = x * (a.fst - b.fst) y,
by simp only [continuous_linear_map.sub_apply, sub_mul, mul_sub, central] } }
section scalars
variables {S : Type*} [monoid S] [distrib_mul_action S A] [smul_comm_class π S A]
[has_continuous_const_smul S A] [is_scalar_tower S A A] [smul_comm_class S A A]
instance : has_smul S π(π, A) :=
{ smul := Ξ» s a,
{ to_prod := s β’ a.to_prod,
central := Ξ» x y, show (s β’ a.snd) x * y = x * (s β’ a.fst) y,
by simp only [continuous_linear_map.smul_apply, mul_smul_comm, smul_mul_assoc, central] } }
@[simp] lemma smul_to_prod (s : S) (a : π(π, A)) : (s β’ a).to_prod = s β’ a.to_prod := rfl
lemma smul_fst (s : S) (a : π(π, A)) : (s β’ a).fst = s β’ a.fst := rfl
lemma smul_snd (s : S) (a : π(π, A)) : (s β’ a).snd = s β’ a.snd := rfl
variables {T : Type*} [monoid T] [distrib_mul_action T A] [smul_comm_class π T A]
[has_continuous_const_smul T A] [is_scalar_tower T A A] [smul_comm_class T A A]
instance [has_smul S T] [is_scalar_tower S T A] : is_scalar_tower S T π(π, A) :=
{ smul_assoc := Ξ» _ _ a, ext _ _ $ smul_assoc _ _ a.to_prod }
instance [smul_comm_class S T A] : smul_comm_class S T π(π, A) :=
{ smul_comm := Ξ» _ _ a, ext _ _ $ smul_comm _ _ a.to_prod }
instance {R : Type*} [semiring R] [module R A] [smul_comm_class π R A]
[has_continuous_const_smul R A] [is_scalar_tower R A A] [smul_comm_class R A A]
[module Rα΅α΅α΅ A] [is_central_scalar R A] : is_central_scalar R π(π, A) :=
{ op_smul_eq_smul := Ξ» _ a, ext _ _ $ op_smul_eq_smul _ a.to_prod }
end scalars
instance : has_one π(π, A) := β¨β¨1, Ξ» x y, rflβ©β©
instance : has_mul π(π, A) :=
{ mul := Ξ» a b,
{ to_prod := (a.fst.comp b.fst, b.snd.comp a.snd),
central := Ξ» x y, show b.snd (a.snd x) * y = x * a.fst (b.fst y),
by simp only [central] } }
instance : has_nat_cast π(π, A) :=
{ nat_cast := Ξ» n, β¨n, Ξ» x y,
begin
rw [prod.snd_nat_cast, prod.fst_nat_cast],
simp only [βnat.smul_one_eq_coe, smul_apply, one_apply, mul_smul_comm, smul_mul_assoc],
endβ© }
instance : has_int_cast π(π, A) :=
{ int_cast := Ξ» n, β¨n, Ξ» x y,
begin
rw [prod.snd_int_cast, prod.fst_int_cast],
simp only [βint.smul_one_eq_coe, smul_apply, one_apply, mul_smul_comm, smul_mul_assoc],
endβ© }
instance : has_pow π(π, A) β :=
{ pow := Ξ» a n, β¨a.to_prod ^ n, Ξ» x y,
begin
induction n with k hk generalizing x y,
{ refl },
{ rw [prod.pow_snd, prod.pow_fst] at hk β’,
rw [pow_succ a.snd, mul_apply, a.central, hk, pow_succ' a.fst, mul_apply] },
endβ© }
instance : inhabited π(π, A) := β¨0β©
@[simp] lemma add_to_prod (a b : π(π, A)) : (a + b).to_prod = a.to_prod + b.to_prod := rfl
@[simp] lemma zero_to_prod : (0 : π(π, A)).to_prod = 0 := rfl
@[simp] lemma neg_to_prod (a : π(π, A)) : (-a).to_prod = -a.to_prod := rfl
@[simp] lemma sub_to_prod (a b : π(π, A)) : (a - b).to_prod = a.to_prod - b.to_prod := rfl
@[simp] lemma one_to_prod : (1 : π(π, A)).to_prod = 1 := rfl
@[simp] lemma nat_cast_to_prod (n : β) : (n : π(π , A)).to_prod = n := rfl
@[simp] lemma int_cast_to_prod (n : β€) : (n : π(π , A)).to_prod = n := rfl
@[simp] lemma pow_to_prod (n : β) (a : π(π, A)) : (a ^ n).to_prod = a.to_prod ^ n := rfl
lemma add_fst (a b : π(π, A)) : (a + b).fst = a.fst + b.fst := rfl
lemma add_snd (a b : π(π, A)) : (a + b).snd = a.snd + b.snd := rfl
lemma zero_fst : (0 : π(π, A)).fst = 0 := rfl
lemma zero_snd : (0 : π(π, A)).snd = 0 := rfl
lemma neg_fst (a : π(π, A)) : (-a).fst = -a.fst := rfl
lemma neg_snd (a : π(π, A)) : (-a).snd = -a.snd := rfl
lemma sub_fst (a b : π(π, A)) : (a - b).fst = a.fst - b.fst := rfl
lemma sub_snd (a b : π(π, A)) : (a - b).snd = a.snd - b.snd := rfl
lemma one_fst : (1 : π(π, A)).fst = 1 := rfl
lemma one_snd : (1 : π(π, A)).snd = 1 := rfl
@[simp] lemma mul_fst (a b : π(π, A)) : (a * b).fst = a.fst * b.fst := rfl
@[simp] lemma mul_snd (a b : π(π, A)) : (a * b).snd = b.snd * a.snd := rfl
lemma nat_cast_fst (n : β) : (n : π(π , A)).fst = n := rfl
lemma nat_cast_snd (n : β) : (n : π(π , A)).snd = n := rfl
lemma int_cast_fst (n : β€) : (n : π(π , A)).fst = n := rfl
lemma int_cast_snd (n : β€) : (n : π(π , A)).snd = n := rfl
lemma pow_fst (n : β) (a : π(π, A)) : (a ^ n).fst = a.fst ^ n := rfl
lemma pow_snd (n : β) (a : π(π, A)) : (a ^ n).snd = a.snd ^ n := rfl
/-- The natural injection from `double_centralizer.to_prod` except the second coordinate inherits
`mul_opposite.op`. The ring structure on `π(π, A)` is the pullback under this map. -/
def to_prod_mul_opposite : π(π, A) β (A βL[π] A) Γ (A βL[π] A)α΅α΅α΅ :=
Ξ» a, (a.fst, mul_opposite.op a.snd)
lemma to_prod_mul_opposite_injective :
function.injective (to_prod_mul_opposite : π(π, A) β (A βL[π] A) Γ (A βL[π] A)α΅α΅α΅) :=
Ξ» a b h, let h' := prod.ext_iff.mp h in ext _ _ $ prod.ext h'.1 $ mul_opposite.op_injective h'.2
lemma range_to_prod_mul_opposite :
set.range to_prod_mul_opposite = {lr : (A βL[π] A) Γ _ | β x y, unop lr.2 x * y = x * lr.1 y} :=
set.ext $ Ξ» x,
β¨by {rintro β¨a, rflβ©, exact a.central}, Ξ» hx, β¨β¨(x.1, unop x.2), hxβ©, prod.ext rfl rflβ©β©
/-- The ring structure is inherited as the pullback under the injective map
`double_centralizer.to_prod_mul_opposite : π(π, A) β (A βL[π] A) Γ (A βL[π] A)α΅α΅α΅` -/
instance : ring π(π, A) :=
to_prod_mul_opposite_injective.ring _
rfl rfl (Ξ» _ _, rfl) (Ξ» _ _, rfl) (Ξ» _, rfl) (Ξ» _ _, rfl)
(Ξ» x n, prod.ext rfl $ mul_opposite.op_smul _ _)
(Ξ» x n, prod.ext rfl $ mul_opposite.op_smul _ _)
(Ξ» x n, prod.ext rfl $ mul_opposite.op_pow _ _)
(Ξ» _, rfl) (Ξ» _, rfl)
/-- The canonical map `double_centralizer.to_prod` as an additive group homomorphism. -/
@[simps]
def to_prod_hom : π(π, A) β+ (A βL[π] A) Γ (A βL[π] A) :=
{ to_fun := to_prod,
map_zero' := rfl,
map_add' := Ξ» x y, rfl }
/-- The canonical map `double_centralizer.to_prod_mul_opposite` as a ring homomorphism. -/
@[simps]
def to_prod_mul_opposite_hom : π(π, A) β+* (A βL[π] A) Γ (A βL[π] A)α΅α΅α΅ :=
{ to_fun := to_prod_mul_opposite,
map_zero' := rfl,
map_one' := rfl,
map_add' := Ξ» x y, rfl,
map_mul' := Ξ» x y, rfl }
/-- The module structure is inherited as the pullback under the additive group monomorphism
`double_centralizer.to_prod : π(π, A) β+ (A βL[π] A) Γ (A βL[π] A)` -/
instance {S : Type*} [semiring S] [module S A] [smul_comm_class π S A]
[has_continuous_const_smul S A] [is_scalar_tower S A A] [smul_comm_class S A A] :
module S π(π, A) :=
function.injective.module S to_prod_hom ext (Ξ» x y, rfl)
-- TODO: generalize to `algebra S π(π, A)` once `continuous_linear_map.algebra` is generalized.
instance : algebra π π(π, A) :=
{ to_fun := Ξ» k,
{ to_prod := algebra_map π ((A βL[π] A) Γ (A βL[π] A)) k,
central := Ξ» x y, by simp_rw [prod.algebra_map_apply, algebra.algebra_map_eq_smul_one,
smul_apply, one_apply, mul_smul_comm, smul_mul_assoc] },
map_one' := ext _ _ $ map_one $ algebra_map π ((A βL[π] A) Γ (A βL[π] A)),
map_mul' := Ξ» kβ kβ, ext _ _ $ prod.ext (map_mul (algebra_map π (A βL[π] A)) _ _)
((map_mul (algebra_map π (A βL[π] A)) _ _).trans (algebra.commutes _ _)),
map_zero' := ext _ _ $ map_zero $ algebra_map π ((A βL[π] A) Γ (A βL[π] A)),
map_add' := Ξ» _ _, ext _ _ $ map_add (algebra_map π ((A βL[π] A) Γ (A βL[π] A))) _ _,
commutes' := Ξ» _ _, ext _ _ $ prod.ext (algebra.commutes _ _) (algebra.commutes _ _).symm,
smul_def' := Ξ» _ _, ext _ _ $ prod.ext (algebra.smul_def _ _)
((algebra.smul_def _ _).trans $ algebra.commutes _ _) }
@[simp] lemma algebra_map_to_prod (k : π) :
(algebra_map π π(π, A) k).to_prod = algebra_map π _ k := rfl
lemma algebra_map_fst (k : π) : (algebra_map π π(π, A) k).fst = algebra_map π _ k := rfl
lemma algebra_map_snd (k : π) : (algebra_map π π(π, A) k).snd = algebra_map π _ k := rfl
/-!
### Star structure
-/
section star
variables [star_ring π] [star_ring A] [star_module π A] [normed_star_group A]
/-- The star operation on `a : π(π, A)` is given by
`(star a).to_prod = (star β a.snd β star, star β a.fst β star)`. -/
instance : has_star π(π, A) :=
{ star := Ξ» a,
{ fst := (((starβα΅’ π : A ββα΅’β[π] A) : A βLβ[π] A).comp a.snd).comp
((starβα΅’ π : A ββα΅’β[π] A) : A βLβ[π] A),
snd := (((starβα΅’ π : A ββα΅’β[π] A) : A βLβ[π] A).comp a.fst).comp
((starβα΅’ π : A ββα΅’β[π] A) : A βLβ[π] A),
central := Ξ» x y, by simpa only [star_mul, star_star]
using (congr_arg star (a.central (star y) (star x))).symm } }
@[simp] lemma star_fst (a : π(π, A)) (b : A) : (star a).fst b = star (a.snd (star b)) := rfl
@[simp] lemma star_snd (a : π(π, A)) (b : A) : (star a).snd b = star (a.fst (star b)) := rfl
instance : star_add_monoid π(π, A) :=
{ star_involutive := Ξ» x, by {ext; simp only [star_fst, star_snd, star_star]},
star_add := Ξ» x y, by {ext; simp only [star_fst, star_snd, add_fst, add_snd,
continuous_linear_map.add_apply, star_add]},
.. double_centralizer.has_star }
instance : star_ring π(π, A) :=
{ star_mul := Ξ» a b, by {ext; simp only [star_fst, star_snd, mul_fst, mul_snd, star_star,
continuous_linear_map.coe_mul, function.comp_app]},
.. double_centralizer.star_add_monoid }
instance : star_module π π(π, A) :=
{ star_smul := Ξ» k a, by {ext; exact star_smul _ _},
.. double_centralizer.star_add_monoid }
end star
/-!
### Coercion from an algebra into its multiplier algebra
-/
/-- The natural coercion of `A` into `π(π, A)` given by sending `a : A` to the pair of linear
maps `Lβ Rβ : A βL[π] A` given by left- and right-multiplication by `a`, respectively.
Warning: if `A = π`, then this is a coercion which is not definitionally equal to the
`algebra_map π π(π, π)` coercion, but these are propositionally equal. See
`double_centralizer.coe_eq_algebra_map` below. -/
noncomputable instance : has_coe_t A π(π, A) :=
{ coe := Ξ» a,
{ fst := continuous_linear_map.mul π A a,
snd := (continuous_linear_map.mul π A).flip a,
central := Ξ» x y, mul_assoc _ _ _ } }
@[simp, norm_cast]
lemma coe_fst (a : A) : (a : π(π, A)).fst = continuous_linear_map.mul π A a := rfl
@[simp, norm_cast]
lemma coe_snd (a : A) : (a : π(π, A)).snd = (continuous_linear_map.mul π A).flip a := rfl
lemma coe_eq_algebra_map : (coe : π β π(π, π)) = algebra_map π π(π, π) :=
begin
ext;
simp only [coe_fst, mul_apply', mul_one, algebra_map_to_prod, prod.algebra_map_apply, coe_snd,
flip_apply, one_mul];
simp only [algebra.algebra_map_eq_smul_one, smul_apply, one_apply, smul_eq_mul, mul_one],
end
/-- The coercion of an algebra into its multiplier algebra as a non-unital star algebra
homomorphism. -/
@[simps]
noncomputable def coe_hom [star_ring π] [star_ring A] [star_module π A] [normed_star_group A] :
A ββββ[π] π(π, A) :=
{ to_fun := Ξ» a, a,
map_smul' := Ξ» k a, by ext; simp only [coe_fst, coe_snd, continuous_linear_map.map_smul,
smul_fst, smul_snd],
map_zero' := by ext; simp only [coe_fst, coe_snd, map_zero, zero_fst, zero_snd],
map_add' := Ξ» a b, by ext; simp only [coe_fst, coe_snd, map_add, add_fst, add_snd],
map_mul' := Ξ» a b, by ext; simp only [coe_fst, coe_snd, mul_apply', flip_apply, mul_fst, mul_snd,
continuous_linear_map.coe_mul, function.comp_app, mul_assoc],
map_star' := Ξ» a, by ext; simp only [coe_fst, coe_snd, mul_apply', star_fst, star_snd,
flip_apply, star_mul, star_star] }
/-!
### Norm structures
We define the norm structure on `π(π, A)` as the pullback under
`double_centralizer.to_prod_mul_opposite_hom : π(π, A) β+* (A βL[π] A) Γ (A βL[π] A)α΅α΅α΅`, which
provides a definitional isometric embedding. Consequently, completeness of `π(π, A)` is obtained
by proving that the range of this map is closed.
In addition, we prove that `π(π, A)` is a normed algebra, and, when `A` is a Cβ-algebra, we show
that `π(π, A)` is also a Cβ-algebra. Moreover, in this case, for `a : π(π, A)`,
`βaβ = βa.fstβ = βa.sndβ`. -/
/-- The normed group structure is inherited as the pullback under the ring monomoprhism
`double_centralizer.to_prod_mul_opposite_hom : π(π, A) β+* (A βL[π] A) Γ (A βL[π] A)α΅α΅α΅`. -/
noncomputable instance : normed_ring π(π, A) :=
normed_ring.induced _ _ (to_prod_mul_opposite_hom : π(π, A) β+* (A βL[π] A) Γ (A βL[π] A)α΅α΅α΅)
to_prod_mul_opposite_injective
-- even though the definition is actually in terms of `double_centralizer.to_prod_mul_opposite`, we
-- choose to see through that here to avoid `mul_opposite.op` appearing.
lemma norm_def (a : π(π, A)) : βaβ = βa.to_prod_homβ := rfl
lemma nnnorm_def (a : π(π, A)) : βaββ = βa.to_prod_homββ := rfl
lemma norm_def' (a : π(π, A)) : βaβ = βa.to_prod_mul_opposite_homβ := rfl
lemma nnnorm_def' (a : π(π, A)) : βaββ = βa.to_prod_mul_opposite_homββ := rfl
instance : normed_space π π(π, A) :=
{ norm_smul_le := Ξ» k a, norm_smul_le k a.to_prod_mul_opposite,
.. double_centralizer.module }
instance : normed_algebra π π(π, A) :=
{ ..double_centralizer.algebra, ..double_centralizer.normed_space }
lemma uniform_embedding_to_prod_mul_opposite :
uniform_embedding (@to_prod_mul_opposite π A _ _ _ _ _) :=
uniform_embedding_comap to_prod_mul_opposite_injective
instance [complete_space A] : complete_space π(π, A) :=
begin
rw complete_space_iff_is_complete_range
uniform_embedding_to_prod_mul_opposite.to_uniform_inducing,
apply is_closed.is_complete,
simp only [range_to_prod_mul_opposite, set.set_of_forall],
refine is_closed_Inter (Ξ» x, is_closed_Inter $ Ξ» y, is_closed_eq _ _),
exact ((continuous_linear_map.apply π A _).continuous.comp $
continuous_unop.comp continuous_snd).mul continuous_const,
exact continuous_const.mul ((continuous_linear_map.apply π A _).continuous.comp continuous_fst),
end
variables [star_ring A] [cstar_ring A]
/-- For `a : π(π, A)`, the norms of `a.fst` and `a.snd` coincide, and hence these
also coincide with `βaβ` which is `max (βa.fstβ) (βa.sndβ)`. -/
lemma norm_fst_eq_snd (a : π(π, A)) : βa.fstβ = βa.sndβ :=
begin
-- a handy lemma for this proof
have h0 : β f : A βL[π] A, β C : ββ₯0, (β b : A, βf bββ ^ 2 β€ C * βf bββ * βbββ) β βfββ β€ C,
{ intros f C h,
have h1 : β b, C * βf bββ * βbββ β€ C * βfββ * βbββ ^ 2,
{ intros b,
convert mul_le_mul_right' (mul_le_mul_left' (f.le_op_nnnorm b) C) (βbββ) using 1,
ring, },
have := div_le_of_le_mul (f.op_nnnorm_le_bound _ (by simpa only [sqrt_sq, sqrt_mul]
using (Ξ» b, sqrt_le_sqrt_iff.mpr ((h b).trans (h1 b))))),
convert rpow_le_rpow this two_pos.le,
{ simp only [rpow_two, div_pow, sq_sqrt], simp only [sq, mul_self_div_self] },
{ simp only [rpow_two, sq_sqrt] } },
have h1 : β b, βa.fst bββ ^ 2 β€ βa.sndββ * βa.fst bββ * βbββ,
{ intros b,
calc βa.fst bββ ^ 2
= βstar (a.fst b) * (a.fst b)ββ
: by simpa only [βsq] using (cstar_ring.nnnorm_star_mul_self).symm
... β€ βa.snd (star (a.fst b))ββ * βbββ : a.central (star (a.fst b)) b βΈ nnnorm_mul_le _ _
... β€ βa.sndββ * βa.fst bββ * βbββ
: nnnorm_star (a.fst b) βΈ mul_le_mul_right' (a.snd.le_op_nnnorm _) _},
have h2 : β b, βa.snd bββ ^ 2 β€ βa.fstββ * βa.snd bββ * βbββ,
{ intros b,
calc βa.snd bββ ^ 2
= βa.snd b * star (a.snd b)ββ
: by simpa only [βsq] using (cstar_ring.nnnorm_self_mul_star).symm
... β€ βbββ * βa.fst (star (a.snd b))ββ
: (a.central b (star (a.snd b))).symm βΈ nnnorm_mul_le _ _
... = βa.fst (star (a.snd b))ββ * βbββ : mul_comm _ _
... β€ βa.fstββ * βa.snd bββ * βbββ
: nnnorm_star (a.snd b) βΈ mul_le_mul_right' (a.fst.le_op_nnnorm _) _ },
exact le_antisymm (h0 _ _ h1) (h0 _ _ h2),
end
lemma nnnorm_fst_eq_snd (a : π(π, A)) : βa.fstββ = βa.sndββ := subtype.ext $ norm_fst_eq_snd a
@[simp] lemma norm_fst (a : π(π, A)) : βa.fstβ = βaβ :=
by simp only [norm_def, to_prod_hom_apply, prod.norm_def, norm_fst_eq_snd, max_eq_right,
eq_self_iff_true]
@[simp] lemma norm_snd (a : π(π, A)) : βa.sndβ = βaβ := by rw [βnorm_fst, norm_fst_eq_snd]
@[simp] lemma nnnorm_fst (a : π(π, A)) : βa.fstββ = βaββ := subtype.ext (norm_fst a)
@[simp] lemma nnnorm_snd (a : π(π, A)) : βa.sndββ = βaββ := subtype.ext (norm_snd a)
end nontrivially_normed
section densely_normed
variables {π A : Type*} [densely_normed_field π] [star_ring π]
variables [non_unital_normed_ring A] [star_ring A] [cstar_ring A]
variables [normed_space π A] [smul_comm_class π A A] [is_scalar_tower π A A] [star_module π A]
instance : cstar_ring π(π, A) :=
{ norm_star_mul_self := Ξ» a, congr_arg (coe : ββ₯0 β β) $ show βstar a * aββ = βaββ * βaββ, from
begin
/- The essence of the argument is this: let `a = (L,R)` and recall `βaβ = βLβ`.
`star a = (star β R β star, star β L β star)`. Then for any `x y : A`, we have
`βstar a * aβ = β(star a * a).sndβ = βR (star (L (star x))) * yβ = βstar (L (star x)) * L yβ`
Now, on the one hand,
`βstar (L (star x)) * L yβ β€ βstar (L (star x))β * βL yβ = βL (star x)β * βL yβ β€ βLβ ^ 2`
whenever `βxβ, βyβ β€ 1`, so the supremum over all such `x, y` is at most `βLβ ^ 2`.
On the other hand, for any `βzβ β€ 1`, we may choose `x := star z` and `y := z` to get:
`βstar (L (star x)) * L yβ = βstar (L z) * (L z)β = βL zβ ^ 2`, and taking the supremum over
all such `z` yields that the supremum is at least `βLβ ^ 2`. It is the latter part of the
argument where `densely_normed_field π` is required (for `Sup_closed_unit_ball_eq_nnnorm`). -/
have hball : (metric.closed_ball (0 : A) 1).nonempty :=
metric.nonempty_closed_ball.2 (zero_le_one),
have key : β x y, βxββ β€ 1 β βyββ β€ 1 β βa.snd (star (a.fst (star x))) * yββ β€ βaββ * βaββ,
{ intros x y hx hy,
rw [a.central],
calc βstar (a.fst (star x)) * a.fst yββ β€ βa.fst (star x)ββ * βa.fst yββ
: nnnorm_star (a.fst (star x)) βΈ nnnorm_mul_le _ _
... β€ (βa.fstββ * 1) * (βa.fstββ * 1)
: mul_le_mul' (a.fst.le_op_norm_of_le ((nnnorm_star x).trans_le hx))
(a.fst.le_op_norm_of_le hy)
... β€ βaββ * βaββ : by simp only [mul_one, nnnorm_fst] },
rw βnnnorm_snd,
simp only [mul_snd, βSup_closed_unit_ball_eq_nnnorm, star_snd, mul_apply],
simp only [β@op_nnnorm_mul π A],
simp only [βSup_closed_unit_ball_eq_nnnorm, mul_apply'],
refine cSup_eq_of_forall_le_of_forall_lt_exists_gt (hball.image _) _ (Ξ» r hr, _),
{ rintro - β¨x, hx, rflβ©,
refine cSup_le (hball.image _) _,
rintro - β¨y, hy, rflβ©,
exact key x y (mem_closed_ball_zero_iff.1 hx) (mem_closed_ball_zero_iff.1 hy) },
{ simp only [set.mem_image, set.mem_set_of_eq, exists_prop, exists_exists_and_eq_and],
have hr' : r.sqrt < βaββ := (βaββ).sqrt_mul_self βΈ nnreal.sqrt_lt_sqrt_iff.2 hr,
simp_rw [βnnnorm_fst, βSup_closed_unit_ball_eq_nnnorm] at hr',
obtain β¨_, β¨x, hx, rflβ©, hxrβ© := exists_lt_of_lt_cSup (hball.image _) hr',
have hx' : βxββ β€ 1 := mem_closed_ball_zero_iff.1 hx,
refine β¨star x, mem_closed_ball_zero_iff.2 ((nnnorm_star x).trans_le hx'), _β©,
refine lt_cSup_of_lt _ β¨x, hx, rflβ© _,
{ refine β¨βaββ * βaββ, _β©,
rintros - β¨y, hy, rflβ©,
exact key (star x) y ((nnnorm_star x).trans_le hx') (mem_closed_ball_zero_iff.1 hy) },
{ simpa only [a.central, star_star, cstar_ring.nnnorm_star_mul_self, nnreal.sq_sqrt, βsq]
using pow_lt_pow_of_lt_left hxr zero_le' two_pos } }
end }
end densely_normed
end double_centralizer
|
208d630b19be0db0afce25b938371df12054ae57 | 5d166a16ae129621cb54ca9dde86c275d7d2b483 | /library/init/data/int/comp_lemmas.lean | c9a339cc45758de2f3318021e0b7e5ae3f2ba8e9 | [
"Apache-2.0"
] | permissive | jcarlson23/lean | b00098763291397e0ac76b37a2dd96bc013bd247 | 8de88701247f54d325edd46c0eed57aeacb64baf | refs/heads/master | 1,611,571,813,719 | 1,497,020,963,000 | 1,497,021,515,000 | 93,882,536 | 1 | 0 | null | 1,497,029,896,000 | 1,497,029,896,000 | null | UTF-8 | Lean | false | false | 4,832 | 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
Auxiliary lemmas used to compare int numerals.
-/
prelude
import init.data.int.order
namespace int
/- Auxiliary lemmas for proving that to int numerals are different -/
/- 1. Lemmas for reducing the problem to the case where the numerals are positive -/
protected lemma ne_neg_of_ne {a b : β€} : a β b β -a β -b :=
Ξ» hβ hβ, absurd (neg_inj hβ) hβ
protected lemma neg_ne_zero_of_ne {a : β€} : a β 0 β -a β 0 :=
Ξ» hβ hβ,
have -a = -0, by rwa neg_zero,
have a = 0, from neg_inj this,
by contradiction
protected lemma zero_ne_neg_of_ne {a : β€} (h : 0 β a) : 0 β -a :=
ne.symm (int.neg_ne_zero_of_ne (ne.symm h))
protected lemma neg_ne_of_pos {a b : β€} : a > 0 β b > 0 β -a β b :=
Ξ» hβ hβ h,
begin
rw [-h] at hβ,
exact absurd (le_of_lt hβ) (not_le_of_gt (neg_of_neg_pos hβ))
end
protected lemma ne_neg_of_pos {a b : β€} : a > 0 β b > 0 β a β -b :=
Ξ» hβ hβ, ne.symm (int.neg_ne_of_pos hβ hβ)
/- 2. Lemmas for proving that positive int numerals are nonneg and positive -/
protected lemma one_pos : (1:int) > 0 :=
zero_lt_one
protected lemma bit0_pos {a : β€} : a > 0 β bit0 a > 0 :=
Ξ» h, add_pos h h
protected lemma bit1_pos {a : β€} : a β₯ 0 β bit1 a > 0 :=
Ξ» h, lt_add_of_le_of_pos (add_nonneg h h) zero_lt_one
protected lemma zero_nonneg : (0:int) β₯ 0 :=
le_refl 0
protected lemma one_nonneg : (1:int) β₯ 0 :=
le_of_lt (zero_lt_one)
protected lemma bit0_nonneg {a : β€} : a β₯ 0 β bit0 a β₯ 0 :=
Ξ» h, add_nonneg h h
protected lemma bit1_nonneg {a : β€} : a β₯ 0 β bit1 a β₯ 0 :=
Ξ» h, le_of_lt (int.bit1_pos h)
protected lemma nonneg_of_pos {a : β€} : a > 0 β a β₯ 0 :=
le_of_lt
/- 3. nat_abs auxiliary lemmas -/
lemma neg_succ_of_nat_lt_zero (n : β) : neg_succ_of_nat n < 0 :=
@lt.intro _ _ n (by simp [neg_succ_of_nat_coe, int.coe_nat_succ, int.coe_nat_add, int.coe_nat_one])
lemma of_nat_ge_zero (n : β) : of_nat n β₯ 0 :=
@le.intro _ _ n (by rw [zero_add, int.coe_nat_eq])
lemma of_nat_nat_abs_eq_of_nonneg : β {a : β€}, a β₯ 0 β of_nat (nat_abs a) = a
| (of_nat n) h := rfl
| (neg_succ_of_nat n) h := absurd (neg_succ_of_nat_lt_zero n) (not_lt_of_ge h)
lemma ne_of_nat_abs_ne_nat_abs_of_nonneg {a b : β€} (ha : a β₯ 0) (hb : b β₯ 0) (h : nat_abs a β nat_abs b) : a β b :=
assume h,
have of_nat (nat_abs a) = of_nat (nat_abs b), by rwa [of_nat_nat_abs_eq_of_nonneg ha, of_nat_nat_abs_eq_of_nonneg hb],
begin injection this, contradiction end
protected lemma ne_of_nat_ne_nonneg_case {a b : β€} {n m : nat} (ha : a β₯ 0) (hb : b β₯ 0) (e1 : nat_abs a = n) (e2 : nat_abs b = m) (h : n β m) : a β b :=
have nat_abs a β nat_abs b, by rwa [e1, e2],
ne_of_nat_abs_ne_nat_abs_of_nonneg ha hb this
/- 4. Aux lemmas for pushing nat_abs inside numerals
nat_abs_zero and nat_abs_one are defined at init/data/int/basic.lean -/
lemma nat_abs_of_nat_core (n : β) : nat_abs (of_nat n) = n :=
rfl
lemma nat_abs_of_neg_succ_of_nat (n : β) : nat_abs (neg_succ_of_nat n) = nat.succ n :=
rfl
protected lemma nat_abs_add_nonneg : β {a b : int}, a β₯ 0 β b β₯ 0 β nat_abs (a + b) = nat_abs a + nat_abs b
| (of_nat n) (of_nat m) hβ hβ :=
have of_nat n + of_nat m = of_nat (n + m), from rfl,
by simp [nat_abs_of_nat_core, this]
| _ (neg_succ_of_nat m) hβ hβ := absurd (neg_succ_of_nat_lt_zero m) (not_lt_of_ge hβ)
| (neg_succ_of_nat n) _ hβ hβ := absurd (neg_succ_of_nat_lt_zero n) (not_lt_of_ge hβ)
protected lemma nat_abs_add_neg : β {a b : int}, a < 0 β b < 0 β nat_abs (a + b) = nat_abs a + nat_abs b
| (neg_succ_of_nat n) (neg_succ_of_nat m) hβ hβ :=
have -[1+ n] + -[1+ m] = -[1+ nat.succ (n + m)], from rfl,
begin simp [nat_abs_of_neg_succ_of_nat, this, nat.succ_add, nat.add_succ] end
protected lemma nat_abs_bit0 : β (a : int), nat_abs (bit0 a) = bit0 (nat_abs a)
| (of_nat n) := int.nat_abs_add_nonneg (of_nat_ge_zero n) (of_nat_ge_zero n)
| (neg_succ_of_nat n) := int.nat_abs_add_neg (neg_succ_of_nat_lt_zero n) (neg_succ_of_nat_lt_zero n)
protected lemma nat_abs_bit0_step {a : int} {n : nat} (h : nat_abs a = n) : nat_abs (bit0 a) = bit0 n :=
begin rw [-h], apply int.nat_abs_bit0 end
protected lemma nat_abs_bit1_nonneg {a : int} (h : a β₯ 0) : nat_abs (bit1 a) = bit1 (nat_abs a) :=
show nat_abs (bit0 a + 1) = bit0 (nat_abs a) + nat_abs 1, from
by rw [int.nat_abs_add_nonneg (int.bit0_nonneg h) (le_of_lt (zero_lt_one)), int.nat_abs_bit0]
protected lemma nat_abs_bit1_nonneg_step {a : int} {n : nat} (hβ : a β₯ 0) (hβ : nat_abs a = n) : nat_abs (bit1 a) = bit1 n :=
begin rw [-hβ], apply int.nat_abs_bit1_nonneg hβ end
end int
|
04836b225f50729af95f1e3619553ed469f44880 | d1a52c3f208fa42c41df8278c3d280f075eb020c | /stage0/src/Init/Notation.lean | e8769744f3111672c428df9e0ba3151c94053bce | [
"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 | 20,889 | lean | /-
Copyright (c) 2020 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
Notation for operators defined at Prelude.lean
-/
prelude
import Init.Prelude
-- DSL for specifying parser precedences and priorities
namespace Lean.Parser.Syntax
syntax:65 (name := addPrec) prec " + " prec:66 : prec
syntax:65 (name := subPrec) prec " - " prec:66 : prec
syntax:65 (name := addPrio) prio " + " prio:66 : prio
syntax:65 (name := subPrio) prio " - " prio:66 : prio
end Lean.Parser.Syntax
macro "max" : prec => `(1024) -- maximum precedence used in term parsers, in particular for terms in function position (`ident`, `paren`, ...)
macro "arg" : prec => `(1023) -- precedence used for application arguments (`do`, `by`, ...)
macro "lead" : prec => `(1022) -- precedence used for terms not supposed to be used as arguments (`let`, `have`, ...)
macro "(" p:prec ")" : prec => p
macro "min" : prec => `(10) -- minimum precedence used in term parsers
macro "min1" : prec => `(11) -- `(min+1) we can only `min+1` after `Meta.lean`
/-
`max:prec` as a term. It is equivalent to `eval_prec max` for `eval_prec` defined at `Meta.lean`.
We use `max_prec` to workaround bootstrapping issues. -/
macro "max_prec" : term => `(1024)
macro "default" : prio => `(1000)
macro "low" : prio => `(100)
macro "mid" : prio => `(1000)
macro "high" : prio => `(10000)
macro "(" p:prio ")" : prio => p
-- Basic notation for defining parsers
-- NOTE: precedence must be at least `arg` to be used in `macro` without parentheses
syntax:arg stx:max "+" : stx
syntax:arg stx:max "*" : stx
syntax:arg stx:max "?" : stx
syntax:2 stx:2 " <|> " stx:1 : stx
macro_rules
| `(stx| $p +) => `(stx| many1($p))
| `(stx| $p *) => `(stx| many($p))
| `(stx| $p ?) => `(stx| optional($p))
| `(stx| $pβ <|> $pβ) => `(stx| orelse($pβ, $pβ))
/- Comma-separated sequence. -/
macro:arg x:stx:max ",*" : stx => `(stx| sepBy($x, ",", ", "))
macro:arg x:stx:max ",+" : stx => `(stx| sepBy1($x, ",", ", "))
/- Comma-separated sequence with optional trailing comma. -/
macro:arg x:stx:max ",*,?" : stx => `(stx| sepBy($x, ",", ", ", allowTrailingSep))
macro:arg x:stx:max ",+,?" : stx => `(stx| sepBy1($x, ",", ", ", allowTrailingSep))
macro:arg "!" x:stx:max : stx => `(stx| notFollowedBy($x))
syntax (name := rawNatLit) "nat_lit " num : term
infixr:90 " β " => Function.comp
infixr:35 " Γ " => Prod
infixl:55 " ||| " => HOr.hOr
infixl:58 " ^^^ " => HXor.hXor
infixl:60 " &&& " => HAnd.hAnd
infixl:65 " + " => HAdd.hAdd
infixl:65 " - " => HSub.hSub
infixl:70 " * " => HMul.hMul
infixl:70 " / " => HDiv.hDiv
infixl:70 " % " => HMod.hMod
infixl:75 " <<< " => HShiftLeft.hShiftLeft
infixl:75 " >>> " => HShiftRight.hShiftRight
infixr:80 " ^ " => HPow.hPow
infixl:65 " ++ " => HAppend.hAppend
prefix:100 "-" => Neg.neg
prefix:100 "~~~" => Complement.complement
/-
Remark: the infix commands above ensure a delaborator is generated for each relations.
We redefine the macros below to be able to use the auxiliary `binop%` elaboration helper for binary operators.
It addresses issue #382. -/
macro_rules | `($x ||| $y) => `(binop% HOr.hOr $x $y)
macro_rules | `($x ^^^ $y) => `(binop% HXor.hXor $x $y)
macro_rules | `($x &&& $y) => `(binop% HAnd.hAnd $x $y)
macro_rules | `($x + $y) => `(binop% HAdd.hAdd $x $y)
macro_rules | `($x - $y) => `(binop% HSub.hSub $x $y)
macro_rules | `($x * $y) => `(binop% HMul.hMul $x $y)
macro_rules | `($x / $y) => `(binop% HDiv.hDiv $x $y)
macro_rules | `($x ++ $y) => `(binop% HAppend.hAppend $x $y)
-- declare ASCII alternatives first so that the latter Unicode unexpander wins
infix:50 " <= " => LE.le
infix:50 " β€ " => LE.le
infix:50 " < " => LT.lt
infix:50 " >= " => GE.ge
infix:50 " β₯ " => GE.ge
infix:50 " > " => GT.gt
infix:50 " = " => Eq
infix:50 " == " => BEq.beq
/-
Remark: the infix commands above ensure a delaborator is generated for each relations.
We redefine the macros below to be able to use the auxiliary `binrel%` elaboration helper for binary relations.
It has better support for applying coercions. For example, suppose we have `binrel% Eq n i` where `n : Nat` and
`i : Int`. The default elaborator fails because we don't have a coercion from `Int` to `Nat`, but
`binrel%` succeeds because it also tries a coercion from `Nat` to `Int` even when the nat occurs before the int. -/
macro_rules | `($x <= $y) => `(binrel% LE.le $x $y)
macro_rules | `($x β€ $y) => `(binrel% LE.le $x $y)
macro_rules | `($x < $y) => `(binrel% LT.lt $x $y)
macro_rules | `($x > $y) => `(binrel% GT.gt $x $y)
macro_rules | `($x >= $y) => `(binrel% GE.ge $x $y)
macro_rules | `($x β₯ $y) => `(binrel% GE.ge $x $y)
macro_rules | `($x = $y) => `(binrel% Eq $x $y)
macro_rules | `($x == $y) => `(binrel_no_prop% BEq.beq $x $y)
infixr:35 " /\\ " => And
infixr:35 " β§ " => And
infixr:30 " \\/ " => Or
infixr:30 " β¨ " => Or
notation:max "Β¬" p:40 => Not p
infixl:35 " && " => and
infixl:30 " || " => or
notation:max "!" b:40 => not b
infixr:67 " :: " => List.cons
syntax:20 term:21 " <|> " term:20 : term
syntax:60 term:61 " >> " term:60 : term
infixl:55 " >>= " => Bind.bind
notation:60 a:60 " <*> " b:61 => Seq.seq a fun _ : Unit => b
notation:60 a:60 " <* " b:61 => SeqLeft.seqLeft a fun _ : Unit => b
notation:60 a:60 " *> " b:61 => SeqRight.seqRight a fun _ : Unit => b
infixr:100 " <$> " => Functor.map
macro_rules | `($x <|> $y) => `(binop_lazy% HOrElse.hOrElse $x $y)
macro_rules | `($x >> $y) => `(binop_lazy% HAndThen.hAndThen $x $y)
syntax (name := termDepIfThenElse)
ppRealGroup(ppRealFill(ppIndent("if " ident " : " term " then") ppSpace term)
ppDedent(ppSpace) ppRealFill("else " term)) : term
macro_rules
| `(if $h:ident : $c then $t:term else $e:term) => `(let_mvar% ?m := $c; wait_if_type_mvar% ?m; dite ?m (fun $h:ident => $t) (fun $h:ident => $e))
syntax (name := termIfThenElse)
ppRealGroup(ppRealFill(ppIndent("if " term " then") ppSpace term)
ppDedent(ppSpace) ppRealFill("else " term)) : term
macro_rules
| `(if $c then $t:term else $e:term) => `(let_mvar% ?m := $c; wait_if_type_mvar% ?m; ite ?m $t $e)
macro "if " "let " pat:term " := " d:term " then " t:term " else " e:term : term =>
`(match $d:term with | $pat:term => $t | _ => $e)
syntax:min term " <| " term:min : term
macro_rules
| `($f $args* <| $a) => let args := args.push a; `($f $args*)
| `($f <| $a) => `($f $a)
syntax:min term " |> " term:min1 : term
macro_rules
| `($a |> $f $args*) => let args := args.push a; `($f $args*)
| `($a |> $f) => `($f $a)
-- Haskell-like pipe <|
-- Note that we have a whitespace after `$` to avoid an ambiguity with the antiquotations.
syntax:min term atomic(" $" ws) term:min : term
macro_rules
| `($f $args* $ $a) => let args := args.push a; `($f $args*)
| `($f $ $a) => `($f $a)
syntax "{ " ident (" : " term)? " // " term " }" : term
macro_rules
| `({ $x : $type // $p }) => ``(Subtype (fun ($x:ident : $type) => $p))
| `({ $x // $p }) => ``(Subtype (fun ($x:ident : _) => $p))
/-
`without_expected_type t` instructs Lean to elaborate `t` without an expected type.
Recall that terms such as `match ... with ...` and `β¨...β©` will postpone elaboration until
expected type is known. So, `without_expected_type` is not effective in this case. -/
macro "without_expected_type " x:term : term => `(let aux := $x; aux)
syntax "[" term,* "]" : term
syntax "%[" term,* "|" term "]" : term -- auxiliary notation for creating big list literals
namespace Lean
macro_rules
| `([ $elems,* ]) => do
let rec expandListLit (i : Nat) (skip : Bool) (result : Syntax) : MacroM Syntax := do
match i, skip with
| 0, _ => pure result
| i+1, true => expandListLit i false result
| i+1, false => expandListLit i true (β ``(List.cons $(elems.elemsAndSeps[i]) $result))
if elems.elemsAndSeps.size < 64 then
expandListLit elems.elemsAndSeps.size false (β ``(List.nil))
else
`(%[ $elems,* | List.nil ])
notation:50 e:51 " matches " p:51 => match e with | p => true | _ => false
-- Declare `this` as a keyword that unhygienically binds to a scope-less `this` assumption (or other binding).
-- The keyword prevents declaring a `this` binding except through metapgrogramming, as is done by `have`/`show`.
/-- Special identifier introduced by "anonymous" `have : ...`, `suffices p ...` etc. -/
macro tk:"this" : term => Syntax.ident tk.getHeadInfo "this".toSubstring `this []
namespace Parser.Tactic
/--
Introduce one or more hypotheses, optionally naming and/or pattern-matching them.
For each hypothesis to be introduced, the remaining main goal's target type must be a `let` or function type.
* `intro` by itself introduces one anonymous hypothesis, which can be accessed by e.g. `assumption`.
* `intro x y` introduces two hypotheses and names them. Individual hypotheses can be anonymized via `_`,
or matched against a pattern:
```lean
-- ... β’ Ξ± Γ Ξ² β ...
intro (a, b)
-- ..., a : Ξ±, b : Ξ² β’ ...
```
* Alternatively, `intro` can be combined with pattern matching much like `fun`:
```lean
intro
| n + 1, 0 => tac
| ...
```
-/
syntax (name := intro) "intro " notFollowedBy("|") (colGt term:max)* : tactic
/-- `intros x...` behaves like `intro x...`, but then keeps introducing (anonymous) hypotheses until goal is not of a function type. -/
syntax (name := intros) "intros " (colGt (ident <|> "_"))* : tactic
/--
`rename t => x` renames the most recent hypothesis whose type matches `t` (which may contain placeholders) to `x`,
or fails if no such hypothesis could be found. -/
syntax (name := rename) "rename " term " => " ident : tactic
/-- `revert x...` is the inverse of `intro x...`: it moves the given hypotheses into the main goal's target type. -/
syntax (name := revert) "revert " (colGt term:max)+ : tactic
/-- `clear x...` removes the given hypotheses, or fails if there are remaining references to a hypothesis. -/
syntax (name := clear) "clear " (colGt term:max)+ : tactic
/--
`subst x...` substitutes each `x` with `e` in the goal if there is a hypothesis of type `x = e` or `e = x`.
If `x` is itself a hypothesis of type `y = e` or `e = y`, `y` is substituted instead. -/
syntax (name := subst) "subst " (colGt term:max)+ : tactic
/--
`assumption` tries to solve the main goal using a hypothesis of compatible type, or else fails.
Note also the `βΉtβΊ` term notation, which is a shorthand for `show t by assumption`. -/
syntax (name := assumption) "assumption" : tactic
/--
`contradiction` closes the main goal if its hypotheses are "trivially contradictory".
```lean
example (h : False) : p := by contradiction -- inductive type/family with no applicable constructors
example (h : none = some true) : p := by contradiction -- injectivity of constructors
example (h : 2 + 2 = 3) : p := by contradiction -- decidable false proposition
example (h : p) (h' : Β¬ p) : q := by contradiction
example (x : Nat) (h : x β x) : p := by contradiction
```
-/
syntax (name := contradiction) "contradiction" : tactic
/--
`apply e` tries to match the current goal against the conclusion of `e`'s type.
If it succeeds, then the tactic returns as many subgoals as the number of premises that
have not been fixed by type inference or type class resolution.
Non-dependent premises are added before dependent ones.
The `apply` tactic uses higher-order pattern matching, type class resolution, and first-order unification with dependent types.
-/
syntax (name := apply) "apply " term : tactic
/--
`exact e` closes the main goal if its target type matches that of `e`.
-/
syntax (name := exact) "exact " term : tactic
/--
`refine e` behaves like `exact e`, except that named (`?x`) or unnamed (`?_`) holes in `e` that are not solved
by unification with the main goal's target type are converted into new goals, using the hole's name, if any, as the goal case name.
-/
syntax (name := refine) "refine " term : tactic
/-- `refine' e` behaves like `refine e`, except that unsolved placeholders (`_`) and implicit parameters are also converted into new goals. -/
syntax (name := refine') "refine' " term : tactic
/-- If the main goal's target type is an inductive type, `constructor` solves it with the first matching constructor, or else fails. -/
syntax (name := constructor) "constructor" : tactic
/--
`case tag => tac` focuses on the goal with case name `tag` and solves it using `tac`, or else fails.
`case tag xβ ... xβ => tac` additionally renames the `n` most recent hypotheses with inaccessible names to the given names. -/
syntax (name := case) "case " (ident <|> "_") (ident <|> "_")* " => " tacticSeq : tactic
/--
`next => tac` focuses on the next goal solves it using `tac`, or else fails.
`next xβ ... xβ => tac` additionally renames the `n` most recent hypotheses with inaccessible names to the given names. -/
macro "next " args:(ident <|> "_")* " => " tac:tacticSeq : tactic => `(tactic| case _ $(args.getArgs)* => $tac)
/-- `allGoals tac` runs `tac` on each goal, concatenating the resulting goals, if any. -/
syntax (name := allGoals) "all_goals " tacticSeq : tactic
/-- `anyGoals tac` applies the tactic `tac` to every goal, and succeeds if at least one application succeeds. -/
syntax (name := anyGoals) "any_goals " tacticSeq : tactic
/--
`focus tac` focuses on the main goal, suppressing all other goals, and runs `tac` on it.
Usually `Β· tac`, which enforces that the goal is closed by `tac`, should be preferred. -/
syntax (name := focus) "focus " tacticSeq : tactic
/-- `skip` does nothing. -/
syntax (name := skip) "skip" : tactic
/-- `done` succeeds iff there are no remaining goals. -/
syntax (name := done) "done" : tactic
syntax (name := traceState) "trace_state" : tactic
syntax (name := failIfSuccess) "fail_if_success " tacticSeq : tactic
syntax (name := paren) "(" tacticSeq ")" : tactic
syntax (name := withReducible) "with_reducible " tacticSeq : tactic
syntax (name := withReducibleAndInstances) "with_reducible_and_instances " tacticSeq : tactic
/-- `first | tac | ...` runs each `tac` until one succeeds, or else fails. -/
syntax (name := first) "first " withPosition((group(colGe "|" tacticSeq))+) : tactic
syntax (name := rotateLeft) "rotate_left" (num)? : tactic
syntax (name := rotateRight) "rotate_right" (num)? : tactic
/-- `try tac` runs `tac` and succeeds even if `tac` failed. -/
macro "try " t:tacticSeq : tactic => `(first | $t | skip)
/-- `tac <;> tac'` runs `tac` on the main goal and `tac'` on each produced goal, concatenating all goals produced by `tac'`. -/
macro:1 x:tactic " <;> " y:tactic:0 : tactic => `(tactic| focus ($x:tactic; all_goals $y:tactic))
/-- `Β· tac` focuses on the main goal and tries to solve it using `tac`, or else fails. -/
macro dot:("Β·" <|> ".") ts:tacticSeq : tactic => `(tactic| {%$dot ($ts:tacticSeq) })
/-- `rfl` is a shorthand for `exact rfl`. -/
macro "rfl" : tactic => `(exact rfl)
/-- `admit` is a shorthand for `exact sorry`. -/
macro "admit" : tactic => `(exact sorry)
/-- The `sorry` tactic is a shorthand for `exact sorry`. -/
macro "sorry" : tactic => `(exact sorry)
macro "infer_instance" : tactic => `(exact inferInstance)
/-- Optional configuration option for tactics -/
syntax config := atomic("(" &"config") " := " term ")"
syntax locationWildcard := "*"
syntax locationHyp := (colGt term:max)+ ("β’" <|> "|-")?
syntax location := withPosition(" at " (locationWildcard <|> locationHyp))
syntax (name := change) "change " term (location)? : tactic
syntax (name := changeWith) "change " term " with " term (location)? : tactic
syntax rwRule := ("β " <|> "<- ")? term
syntax rwRuleSeq := "[" rwRule,+,? "]"
syntax (name := rewriteSeq) "rewrite " (config)? rwRuleSeq (location)? : tactic
syntax (name := rwSeq) "rw " (config)? rwRuleSeq (location)? : tactic
def rwWithRfl (kind : SyntaxNodeKind) (atom : String) (stx : Syntax) : MacroM Syntax := do
-- We show the `rfl` state on `]`
let seq := stx[2]
let rbrak := seq[2]
-- Replace `]` token with one without position information in the expanded tactic
let seq := seq.setArg 2 (mkAtom "]")
let tac := stx.setKind kind |>.setArg 0 (mkAtomFrom stx atom) |>.setArg 2 seq
`(tactic| $tac; try (with_reducible rfl%$rbrak))
@[macro rwSeq] def expandRwSeq : Macro :=
rwWithRfl ``Lean.Parser.Tactic.rewriteSeq "rewrite"
syntax (name := injection) "injection " term (" with " (colGt (ident <|> "_"))+)? : tactic
syntax (name := injections) "injections" : tactic
syntax discharger := atomic("(" (&"discharger" <|> &"disch")) " := " tacticSeq ")"
syntax simpPre := "β"
syntax simpPost := "β"
syntax simpLemma := (simpPre <|> simpPost)? ("β " <|> "<- ")? term
syntax simpErase := "-" term:max
syntax simpStar := "*"
syntax (name := simp) "simp " (config)? (discharger)? (&"only ")? ("[" (simpStar <|> simpErase <|> simpLemma),* "]")? (location)? : tactic
syntax (name := simpAll) "simp_all " (config)? (discharger)? (&"only ")? ("[" (simpErase <|> simpLemma),* "]")? : tactic
/--
Delta expand the given definition.
This is a low-level tactic, it will expose how recursive definitions have been compiled by Lean. -/
syntax (name := delta) "delta " ident (location)? : tactic
-- Auxiliary macro for lifting have/suffices/let/...
-- It makes sure the "continuation" `?_` is the main goal after refining
macro "refine_lift " e:term : tactic => `(focus (refine no_implicit_lambda% $e; rotate_right))
macro "have " d:haveDecl : tactic => `(refine_lift have $d:haveDecl; ?_)
/- We use a priority > default, to avoid ambiguity with previous `have` notation -/
macro (priority := high) "have" x:ident " := " p:term : tactic => `(have $x:ident : _ := $p)
macro "suffices " d:sufficesDecl : tactic => `(refine_lift suffices $d:sufficesDecl; ?_)
macro "let " d:letDecl : tactic => `(refine_lift let $d:letDecl; ?_)
macro "show " e:term : tactic => `(refine_lift show $e:term from ?_)
syntax (name := letrec) withPosition(atomic(group("let " &"rec ")) letRecDecls) : tactic
macro_rules
| `(tactic| let rec $d:letRecDecls) => `(tactic| refine_lift let rec $d:letRecDecls; ?_)
-- Similar to `refineLift`, but using `refine'`
macro "refine_lift' " e:term : tactic => `(focus (refine' no_implicit_lambda% $e; rotate_right))
macro "have' " d:haveDecl : tactic => `(refine_lift' have $d:haveDecl; ?_)
macro (priority := high) "have'" x:ident " := " p:term : tactic => `(have' $x:ident : _ := $p)
macro "let' " d:letDecl : tactic => `(refine_lift' let $d:letDecl; ?_)
syntax inductionAlt := ppDedent(ppLine) "| " (group("@"? ident) <|> "_") (ident <|> "_")* " => " (hole <|> syntheticHole <|> tacticSeq)
syntax inductionAlts := "with " (tactic)? withPosition( (colGe inductionAlt)+)
syntax (name := induction) "induction " term,+ (" using " ident)? ("generalizing " term:max+)? (inductionAlts)? : tactic
syntax generalizeArg := atomic(ident " : ")? term:51 " = " ident
/--
`generalize ([h :] e = x),+` replaces all occurrences `e`s in the main goal with a fresh hypothesis `x`s.
If `h` is given, `h : e = x` is introduced as well. -/
syntax (name := generalize) "generalize " generalizeArg,+ : tactic
syntax casesTarget := atomic(ident " : ")? term
syntax (name := cases) "cases " casesTarget,+ (" using " ident)? (inductionAlts)? : tactic
syntax (name := existsIntro) "exists " term : tactic
/-- `rename_i x_1 ... x_n` renames the last `n` inaccessible names using the given names. -/
syntax (name := renameI) "rename_i " (colGt (ident <|> "_"))+ : tactic
syntax "repeat " tacticSeq : tactic
macro_rules
| `(tactic| repeat $seq) => `(tactic| first | ($seq); repeat $seq | skip)
syntax "trivial" : tactic
syntax (name := split) "split " (colGt term)? (location)? : tactic
/--
The tactic `specialize h aβ ... aβ` works on local hypothesis `h`.
The premises of this hypothesis, either universal quantifications or non-dependent implications,
are instantiated by concrete terms coming either from arguments `aβ` ... `aβ`.
The tactic adds a new hypothesis with the same name `h := h aβ ... aβ` and tries to clear the previous one.
-/
syntax (name := specialize) "specialize " term : tactic
macro_rules | `(tactic| trivial) => `(tactic| assumption)
macro_rules | `(tactic| trivial) => `(tactic| rfl)
macro_rules | `(tactic| trivial) => `(tactic| contradiction)
macro_rules | `(tactic| trivial) => `(tactic| apply True.intro)
macro_rules | `(tactic| trivial) => `(tactic| apply And.intro <;> trivial)
macro "unhygienic " t:tacticSeq : tactic => `(set_option tactic.hygienic false in $t:tacticSeq)
end Tactic
namespace Attr
-- simp attribute syntax
syntax (name := simp) "simp" (Tactic.simpPre <|> Tactic.simpPost)? (prio)? : attr
end Attr
end Parser
end Lean
macro "βΉ" type:term "βΊ" : term => `((by assumption : $type))
|
b5b28626779fb2d9611cccda6c4ac73478bdc551 | 2102833783742d9cd20d98b382ecb3af427d8071 | /library/init/meta/default.lean | 0a137e8ede032f834ad195f256cf2d2649822c25 | [
"Apache-2.0"
] | permissive | bryangingechen/lean | b63dd2b7d2ed46c13c6226148691b7d88282e9b2 | ec76117504723667bdc0dda4b93aa1b31536147d | refs/heads/master | 1,592,587,806,769 | 1,568,328,715,000 | 1,568,328,715,000 | 196,801,202 | 0 | 0 | Apache-2.0 | 1,563,085,022,000 | 1,563,085,022,000 | null | UTF-8 | Lean | false | false | 938 | 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.name init.meta.options init.meta.format init.meta.rb_map
import init.meta.level init.meta.expr init.meta.environment init.meta.attribute
import init.meta.tactic init.meta.contradiction_tactic init.meta.constructor_tactic
import init.meta.injection_tactic init.meta.relation_tactics init.meta.fun_info
import init.meta.congr_lemma init.meta.match_tactic init.meta.ac_tactics
import init.meta.backward init.meta.rewrite_tactic
import init.meta.derive init.meta.mk_dec_eq_instance
import init.meta.simp_tactic init.meta.set_get_option_tactics
import init.meta.interactive init.meta.converter init.meta.vm
import init.meta.comp_value_tactics init.meta.smt
import init.meta.async_tactic init.meta.ref
import init.meta.hole_command init.meta.congr_tactic
|
7432e3bfe421ef01bb6184b6ad810996d6915efa | cf39355caa609c0f33405126beee2739aa3cb77e | /library/init/data/string/default.lean | e76c416d355cb4a5608915eafaded6d8dd5b7f78 | [
"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 | 223 | lean | /-
Copyright (c) 2016 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
import init.data.string.basic init.data.string.ops
|
7f71750806879fcb05e638aae7ecb5c0da836d66 | 491068d2ad28831e7dade8d6dff871c3e49d9431 | /tests/lean/run/elab_bug1.lean | edca3f166a7a5e4cfb507dd882bfdcb9dc3f8984 | [
"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 | 2,794 | lean | ----------------------------------------------------------------------------------------------------
--- Copyright (c) 2014 Microsoft Corporation. All rights reserved.
--- Released under Apache 2.0 license as described in the file LICENSE.
--- Author: Jeremy Avigad
----------------------------------------------------------------------------------------------------
import logic
open eq
open function
namespace congruence
-- TODO: move this somewhere else
definition reflexive {T : Type} (R : T β T β Prop) : Prop := βx, R x x
-- Congruence classes for unary and binary functions
-- -------------------------------------------------
inductive congruence [class] {T1 : Type} {T2 : Type} (R1 : T1 β T1 β Prop) (R2 : T2 β T2 β Prop)
(f : T1 β T2) : Prop :=
mk : (βx y : T1, R1 x y β R2 (f x) (f y)) β congruence R1 R2 f
-- to trigger class inference
theorem congr_app {T1 : Type} {T2 : Type} (R1 : T1 β T1 β Prop) (R2 : T2 β T2 β Prop)
(f : T1 β T2) {C : congruence R1 R2 f} {x y : T1} : R1 x y β R2 (f x) (f y) :=
congruence.rec id C x y
-- General tools to build instances
-- --------------------------------
theorem congr_trivial [instance] {T : Type} (R : T β T β Prop) : congruence R R id :=
congruence.mk (take x y H, H)
theorem congr_const {T2 : Type} (R2 : T2 β T2 β Prop) (H : reflexive R2) :
β(T1 : Type) (R1 : T1 β T1 β Prop) (c : T2), congruence R1 R2 (const T1 c) :=
take T1 R1 c, congruence.mk (take x y H1, H c)
-- congruences for logic
theorem congr_const_iff [instance] (T1 : Type) (R1 : T1 β T1 β Prop) (c : Prop) :
congruence R1 iff (const T1 c) := congr_const iff iff.refl T1 R1 c
theorem congr_or [instance] (T : Type) (R : T β T β Prop) (f1 f2 : T β Prop)
[H1 : congruence R iff f1] [H2 : congruence R iff f2] :
congruence R iff (Ξ»x, f1 x β¨ f2 x) := sorry
theorem congr_implies [instance] (T : Type) (R : T β T β Prop) (f1 f2 : T β Prop)
[H1 : congruence R iff f1] [H2 : congruence R iff f2] :
congruence R iff (Ξ»x, f1 x β f2 x) := sorry
theorem congr_iff [instance] (T : Type) (R : T β T β Prop) (f1 f2 : T β Prop)
[H1 : congruence R iff f1] [H2 : congruence R iff f2] :
congruence R iff (Ξ»x, f1 x β f2 x) := sorry
theorem congr_not [instance] (T : Type) (R : T β T β Prop) (f : T β Prop)
[H : congruence R iff f] :
congruence R iff (Ξ»x, Β¬ f x) := sorry
theorem subst_iff {T : Type} {R : T β T β Prop} {P : T β Prop} [C : congruence R iff P]
{a b : T} (H : R a b) (H1 : P a) : P b :=
-- iff_mp_left (congruence.rec id C a b H) H1
iff.elim_left (@congr_app _ _ R iff P C a b H) H1
theorem test2 (a b c d e : Prop) (H1 : a β b) (H2 : a β¨ c β Β¬(d β a)) : b β¨ c β Β¬(d β b) :=
subst_iff H1 H2
end congruence
|
0e181b56a5d59f419c0cb5bf7614e07a642c2ee6 | ee8cdbabf07f77e7be63a449b8483ce308d37218 | /lean/src/valid/mathd-algebra-104.lean | bee73800d3607f7a368734bbf2523201938efc66 | [
"MIT",
"Apache-2.0"
] | permissive | zeta1999/miniF2F | 6d66c75d1c18152e224d07d5eed57624f731d4b7 | c1ba9629559c5273c92ec226894baa0c1ce27861 | refs/heads/main | 1,681,897,460,642 | 1,620,646,361,000 | 1,620,646,361,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 251 | lean | /-
Copyright (c) 2021 OpenAI. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kunhao Zheng
-/
import data.real.basic
example (x : β) (hβ : 125 / 8 = x / 12) : x = 375 / 2 :=
begin
linarith,
end
|
02ec28daa2d878059bc36afc49bd854670950977 | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/algebra/lie/base_change.lean | 5042d94bf98c67ebc702f1d731f0b6f5d9457969 | [
"Apache-2.0"
] | permissive | jjgarzella/mathlib | 96a345378c4e0bf26cf604aed84f90329e4896a2 | 395d8716c3ad03747059d482090e2bb97db612c8 | refs/heads/master | 1,686,480,124,379 | 1,625,163,323,000 | 1,625,163,323,000 | 281,190,421 | 2 | 0 | Apache-2.0 | 1,595,268,170,000 | 1,595,268,169,000 | null | UTF-8 | Lean | false | false | 5,860 | lean | /-
Copyright (c) 2021 Oliver Nash. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Oliver Nash
-/
import algebra.lie.tensor_product
import ring_theory.tensor_product
/-!
# Extension and restriction of scalars for Lie algebras
Lie algebras have a well-behaved theory of extension and restriction of scalars.
## Main definitions
* `lie_algebra.extend_scalars.lie_algebra`
* `lie_algebra.restrict_scalars.lie_algebra`
## Tags
lie ring, lie algebra, extension of scalars, restriction of scalars, base change
-/
universes u v w wβ wβ wβ
open_locale tensor_product
variables (R : Type u) (A : Type w) (L : Type v)
namespace lie_algebra
namespace extend_scalars
variables [comm_ring R] [comm_ring A] [algebra R A] [lie_ring L] [lie_algebra R L]
/-- The Lie bracket on the extension of a Lie algebra `L` over `R` by an algebra `A` over `R`.
In fact this bracket is fully `A`-bilinear but without a significant upgrade to our mixed-scalar
support in the tensor product library, it is far easier to bootstrap like this, starting with the
definition below. -/
private def bracket' : (A β[R] L) ββ[R] (A β[R] L) ββ[R] A β[R] L :=
tensor_product.curry $
(tensor_product.map (algebra.lmul' R) (lie_module.to_module_hom R L L : L β[R] L ββ[R] L)).comp $
tensor_product.tensor_tensor_tensor_comm R A L A L
@[simp] private lemma bracket'_tmul (s t : A) (x y : L) :
bracket' R A L (s ββ[R] x) (t ββ[R] y) = (s*t) ββ β
x, yβ :=
by simp [bracket']
instance : has_bracket (A β[R] L) (A β[R] L) := { bracket := Ξ» x y, bracket' R A L x y, }
private lemma bracket_def (x y : A β[R] L) : β
x, yβ = bracket' R A L x y := rfl
@[simp] lemma bracket_tmul (s t : A) (x y : L) : β
s ββ[R] x, t ββ[R] yβ = (s*t) ββ β
x, yβ :=
by rw [bracket_def, bracket'_tmul]
private lemma bracket_lie_self (x : A β[R] L) : β
x, xβ = 0 :=
begin
simp only [bracket_def],
apply x.induction_on,
{ simp only [linear_map.map_zero, eq_self_iff_true, linear_map.zero_apply], },
{ intros a l,
simp only [bracket'_tmul, tensor_product.tmul_zero, eq_self_iff_true, lie_self], },
{ intros zβ zβ hβ hβ,
suffices : bracket' R A L zβ zβ + bracket' R A L zβ zβ = 0,
{ rw [linear_map.map_add, linear_map.map_add, linear_map.add_apply, linear_map.add_apply,
hβ, hβ, zero_add, add_zero, add_comm, this], },
apply zβ.induction_on,
{ simp only [linear_map.map_zero, add_zero, linear_map.zero_apply], },
{ intros aβ lβ, apply zβ.induction_on,
{ simp only [linear_map.map_zero, add_zero, linear_map.zero_apply], },
{ intros aβ lβ,
simp only [β lie_skew lβ lβ, mul_comm aβ aβ, tensor_product.tmul_neg, bracket'_tmul,
add_right_neg], },
{ intros yβ yβ hyβ hyβ,
simp only [hyβ, hyβ, add_add_add_comm, add_zero, linear_map.add_apply,
linear_map.map_add], }, },
{ intros yβ yβ hyβ hyβ,
simp only [add_add_add_comm, hyβ, hyβ, add_zero, linear_map.add_apply,
linear_map.map_add], }, },
end
private lemma bracket_leibniz_lie (x y z : A β[R] L) : β
x, β
y, zββ = β
β
x, yβ, zβ + β
y, β
x, zββ :=
begin
simp only [bracket_def],
apply x.induction_on,
{ simp only [linear_map.map_zero, add_zero, eq_self_iff_true, linear_map.zero_apply], },
{ intros aβ lβ,
apply y.induction_on,
{ simp only [linear_map.map_zero, add_zero, eq_self_iff_true, linear_map.zero_apply], },
{ intros aβ lβ,
apply z.induction_on,
{ simp only [linear_map.map_zero, add_zero], },
{ intros aβ lβ, simp only [bracket'_tmul],
rw [mul_left_comm aβ aβ aβ, mul_assoc, leibniz_lie, tensor_product.tmul_add], },
{ intros uβ uβ hβ hβ,
simp only [add_add_add_comm, hβ, hβ, linear_map.map_add], }, },
{ intros uβ uβ hβ hβ,
simp only [add_add_add_comm, hβ, hβ, linear_map.add_apply, linear_map.map_add], }, },
{ intros uβ uβ hβ hβ,
simp only [add_add_add_comm, hβ, hβ, linear_map.add_apply, linear_map.map_add], },
end
instance : lie_ring (A β[R] L) :=
{ add_lie := Ξ» x y z, by simp only [bracket_def, linear_map.add_apply, linear_map.map_add],
lie_add := Ξ» x y z, by simp only [bracket_def, linear_map.map_add],
lie_self := bracket_lie_self R A L,
leibniz_lie := bracket_leibniz_lie R A L, }
private lemma bracket_lie_smul (a : A) (x y : A β[R] L) : β
x, a β’ yβ = a β’ β
x, yβ :=
begin
apply x.induction_on,
{ simp only [zero_lie, smul_zero], },
{ intros aβ lβ, apply y.induction_on,
{ simp only [lie_zero, smul_zero], },
{ intros aβ lβ,
simp only [bracket_def, bracket', tensor_product.smul_tmul', mul_left_comm aβ a aβ,
tensor_product.curry_apply, algebra.lmul'_apply, algebra.id.smul_eq_mul, function.comp_app,
linear_equiv.coe_coe, linear_map.coe_comp, tensor_product.map_tmul,
tensor_product.tensor_tensor_tensor_comm_tmul], },
{ intros zβ zβ hβ hβ,
simp only [hβ, hβ, smul_add, lie_add], }, },
{ intros zβ zβ hβ hβ,
simp only [hβ, hβ, smul_add, add_lie], },
end
instance lie_algebra : lie_algebra A (A β[R] L) :=
{ lie_smul := bracket_lie_smul R A L, }
end extend_scalars
namespace restrict_scalars
open restrict_scalars
variables [h : lie_ring L]
include h
instance : lie_ring (restrict_scalars R A L) := h
variables [comm_ring A] [lie_algebra A L]
@[nolint unused_arguments]
instance lie_algebra [comm_ring R] [algebra R A] : lie_algebra R (restrict_scalars R A L) :=
{ lie_smul := Ξ» t x y, (lie_smul _ (show L, from x) (show L, from y) : _),
.. (by apply_instance : module R (restrict_scalars R A L)), }
end restrict_scalars
end lie_algebra
|
fda5c51377c650e1fc7cd90f1e1882605bc21e83 | 82e44445c70db0f03e30d7be725775f122d72f3e | /src/measure_theory/pi_system.lean | 262d81663811c72ff646c1f96b2f54034a50c77c | [
"Apache-2.0"
] | permissive | stjordanis/mathlib | 51e286d19140e3788ef2c470bc7b953e4991f0c9 | 2568d41bca08f5d6bf39d915434c8447e21f42ee | refs/heads/master | 1,631,748,053,501 | 1,627,938,886,000 | 1,627,938,886,000 | 228,728,358 | 0 | 0 | Apache-2.0 | 1,576,630,588,000 | 1,576,630,587,000 | null | UTF-8 | Lean | false | false | 17,862 | lean | /-
Copyright (c) 2021 Martin Zinkevich. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes HΓΆlzl, Martin Zinkevich
-/
import measure_theory.measurable_space_def
import data.equiv.encodable.lattice
/-!
# Induction principles for measurable sets, related to Ο-systems and Ξ»-systems.
## Main statements
* The main theorem of this file is Dynkin's Ο-Ξ» theorem, which appears
here as an induction principle `induction_on_inter`. Suppose `s` is a
collection of subsets of `Ξ±` such that the intersection of two members
of `s` belongs to `s` whenever it is nonempty. Let `m` be the Ο-algebra
generated by `s`. In order to check that a predicate `C` holds on every
member of `m`, it suffices to check that `C` holds on the members of `s` and
that `C` is preserved by complementation and *disjoint* countable
unions.
* The proof of this theorem relies on the notion of `is_pi_system`, i.e., a collection of sets
which is closed under binary non-empty intersections. Note that this is a small variation around
the usual notion in the literature, which often requires that a Ο-system is non-empty, and closed
also under disjoint intersections. This variation turns out to be convenient for the
formalization.
* The proof of Dynkin's Ο-Ξ» theorem also requires the notion of `dynkin_system`, i.e., a collection
of sets which contains the empty set, is closed under complementation and under countable union
of pairwise disjoint sets. The disjointness condition is the only difference with `Ο`-algebras.
* `generate_pi_system g` gives the minimal Ο-system containing `g`.
This can be considered a Galois insertion into both measurable spaces and sets.
* `generate_from_generate_pi_system_eq` proves that if you start from a collection of sets `g`,
take the generated Ο-system, and then the generated Ο-algebra, you get the same result as
the Ο-algebra generated from `g`. This is useful because there are connections between
independent sets that are Ο-systems and the generated independent spaces.
* `mem_generate_pi_system_Union_elim` and `mem_generate_pi_system_Union_elim'` show that any
element of the Ο-system generated from the union of a set of Ο-systems can be
represented as the intersection of a finite number of elements from these sets.
## Implementation details
* `is_pi_system` is a predicate, not a type. Thus, we don't explicitly define the galois
insertion, nor do we define a complete lattice. In theory, we could define a complete
lattice and galois insertion on the subtype corresponding to `is_pi_system`.
-/
open measurable_space set
open_locale classical
/-- A Ο-system is a collection of subsets of `Ξ±` that is closed under binary intersection of
non-disjoint sets. Usually it is also required that the collection is nonempty, but we don't do
that here. -/
def is_pi_system {Ξ±} (C : set (set Ξ±)) : Prop :=
β s t β C, (s β© t : set Ξ±).nonempty β s β© t β C
namespace measurable_space
lemma is_pi_system_measurable_set {Ξ±:Type*} [measurable_space Ξ±] :
is_pi_system {s : set Ξ± | measurable_set s} :=
Ξ» s t hs ht _, hs.inter ht
end measurable_space
lemma is_pi_system.singleton {Ξ±} (S : set Ξ±) : is_pi_system ({S} : set (set Ξ±)) :=
begin
intros s t h_s h_t h_ne,
rw [set.mem_singleton_iff.1 h_s, set.mem_singleton_iff.1 h_t, set.inter_self,
set.mem_singleton_iff],
end
/-- Given a collection `S` of subsets of `Ξ±`, then `generate_pi_system S` is the smallest
Ο-system containing `S`. -/
inductive generate_pi_system {Ξ±} (S : set (set Ξ±)) : set (set Ξ±)
| base {s : set Ξ±} (h_s : s β S) : generate_pi_system s
| inter {s t : set Ξ±} (h_s : generate_pi_system s) (h_t : generate_pi_system t)
(h_nonempty : (s β© t).nonempty) : generate_pi_system (s β© t)
lemma is_pi_system_generate_pi_system {Ξ±} (S : set (set Ξ±)) :
is_pi_system (generate_pi_system S) :=
Ξ» s t h_s h_t h_nonempty, generate_pi_system.inter h_s h_t h_nonempty
lemma subset_generate_pi_system_self {Ξ±} (S : set (set Ξ±)) : S β generate_pi_system S :=
Ξ» s, generate_pi_system.base
lemma generate_pi_system_subset_self {Ξ±} {S : set (set Ξ±)} (h_S : is_pi_system S) :
generate_pi_system S β S :=
begin
intros x h,
induction h with s h_s s u h_gen_s h_gen_u h_nonempty h_s h_u,
{ exact h_s, },
{ exact h_S _ _ h_s h_u h_nonempty, },
end
lemma generate_pi_system_eq {Ξ±} {S : set (set Ξ±)} (h_pi : is_pi_system S) :
generate_pi_system S = S :=
set.subset.antisymm (generate_pi_system_subset_self h_pi) (subset_generate_pi_system_self S)
lemma generate_pi_system_mono {Ξ±} {S T : set (set Ξ±)} (hST : S β T) :
generate_pi_system S β generate_pi_system T :=
begin
intros t ht,
induction ht with s h_s s u h_gen_s h_gen_u h_nonempty h_s h_u,
{ exact generate_pi_system.base (set.mem_of_subset_of_mem hST h_s),},
{ exact is_pi_system_generate_pi_system T _ _ h_s h_u h_nonempty, },
end
lemma generate_pi_system_measurable_set {Ξ±} [M : measurable_space Ξ±] {S : set (set Ξ±)}
(h_meas_S : β s β S, measurable_set s) (t : set Ξ±)
(h_in_pi : t β generate_pi_system S) : measurable_set t :=
begin
induction h_in_pi with s h_s s u h_gen_s h_gen_u h_nonempty h_s h_u,
{ apply h_meas_S _ h_s, },
{ apply measurable_set.inter h_s h_u, },
end
lemma generate_from_measurable_set_of_generate_pi_system {Ξ±} {g : set (set Ξ±)} (t : set Ξ±)
(ht : t β generate_pi_system g) :
(generate_from g).measurable_set' t :=
@generate_pi_system_measurable_set Ξ± (generate_from g) g
(Ξ» s h_s_in_g, measurable_set_generate_from h_s_in_g) t ht
lemma generate_from_generate_pi_system_eq {Ξ±} {g : set (set Ξ±)} :
generate_from (generate_pi_system g) = generate_from g :=
begin
apply le_antisymm; apply generate_from_le,
{ exact Ξ» t h_t, generate_from_measurable_set_of_generate_pi_system t h_t, },
{ exact Ξ» t h_t, measurable_set_generate_from (generate_pi_system.base h_t), },
end
/- Every element of the Ο-system generated by the union of a family of Ο-systems
is a finite intersection of elements from the Ο-systems.
For an indexed union version, see `mem_generate_pi_system_Union_elim'`. -/
lemma mem_generate_pi_system_Union_elim {Ξ± Ξ²} {g : Ξ² β set (set Ξ±)}
(h_pi : β b, is_pi_system (g b)) (t : set Ξ±) (h_t : t β generate_pi_system (β b, g b)) :
β (T : finset Ξ²) (f : Ξ² β set Ξ±), (t = β b β T, f b) β§ (β b β T, f b β g b) :=
begin
induction h_t with s h_s s t' h_gen_s h_gen_t' h_nonempty h_s h_t',
{ rcases h_s with β¨t', β¨β¨b, rflβ©, h_s_in_t'β©β©,
refine β¨{b}, (Ξ» _, s), _β©,
simpa using h_s_in_t', },
{ rcases h_t' with β¨T_t', β¨f_t', β¨rfl, h_t'β©β©β©,
rcases h_s with β¨T_s, β¨f_s, β¨rfl, h_sβ© β© β©,
use [(T_s βͺ T_t'), (Ξ» (b:Ξ²),
if (b β T_s) then (if (b β T_t') then (f_s b β© (f_t' b)) else (f_s b))
else (if (b β T_t') then (f_t' b) else (β
: set Ξ±)))],
split,
{ ext a,
simp_rw [set.mem_inter_iff, set.mem_Inter, finset.mem_union, or_imp_distrib],
rw β forall_and_distrib,
split; intros h1 b; by_cases hbs : b β T_s; by_cases hbt : b β T_t'; specialize h1 b;
simp only [hbs, hbt, if_true, if_false, true_implies_iff, and_self, false_implies_iff,
and_true, true_and] at h1 β’,
all_goals { exact h1, }, },
intros b h_b,
split_ifs with hbs hbt hbt,
{ refine h_pi b (f_s b) (f_t' b) (h_s b hbs) (h_t' b hbt) (set.nonempty.mono _ h_nonempty),
exact set.inter_subset_inter (set.bInter_subset_of_mem hbs) (set.bInter_subset_of_mem hbt), },
{ exact h_s b hbs, },
{ exact h_t' b hbt, },
{ rw finset.mem_union at h_b,
apply false.elim (h_b.elim hbs hbt), }, },
end
/- Every element of the Ο-system generated by an indexed union of a family of Ο-systems
is a finite intersection of elements from the Ο-systems.
For a total union version, see `mem_generate_pi_system_Union_elim`. -/
lemma mem_generate_pi_system_Union_elim' {Ξ± Ξ²} {g : Ξ² β set (set Ξ±)} {s: set Ξ²}
(h_pi : β b β s, is_pi_system (g b)) (t : set Ξ±) (h_t : t β generate_pi_system (β b β s, g b)) :
β (T : finset Ξ²) (f : Ξ² β set Ξ±), (βT β s) β§ (t = β b β T, f b) β§ (β b β T, f b β g b) :=
begin
have : t β generate_pi_system (β (b : subtype s), (g β subtype.val) b),
{ suffices h1 : (β (b : subtype s), (g β subtype.val) b) = (β b (H : b β s), g b), by rwa h1,
ext x,
simp only [exists_prop, set.mem_Union, function.comp_app, subtype.exists, subtype.coe_mk],
refl },
rcases @mem_generate_pi_system_Union_elim Ξ± (subtype s) (g β subtype.val)
(Ξ» b, h_pi b.val b.property) t this with β¨T, β¨f,β¨ rfl, h_t'β©β©β©,
refine β¨T.image subtype.val, function.extend subtype.val f (Ξ» (b:Ξ²), (β
: set Ξ±)), by simp, _, _β©,
{ ext a, split;
{ simp only [set.mem_Inter, subtype.forall, finset.set_bInter_finset_image],
intros h1 b h_b h_b_in_T,
have h2 := h1 b h_b h_b_in_T,
revert h2,
rw function.extend_apply subtype.val_injective,
apply id } },
{ intros b h_b,
simp_rw [finset.mem_image, exists_prop, subtype.exists,
exists_and_distrib_right, exists_eq_right] at h_b,
cases h_b,
have h_b_alt : b = (subtype.mk b h_b_w).val := rfl,
rw [h_b_alt, function.extend_apply subtype.val_injective],
apply h_t',
apply h_b_h },
end
namespace measurable_space
variable {Ξ± : Type*}
/-- A Dynkin system is a collection of subsets of a type `Ξ±` that contains the empty set,
is closed under complementation and under countable union of pairwise disjoint sets.
The disjointness condition is the only difference with `Ο`-algebras.
The main purpose of Dynkin systems is to provide a powerful induction rule for Ο-algebras
generated by a collection of sets which is stable under intersection.
A Dynkin system is also known as a "Ξ»-system" or a "d-system".
-/
structure dynkin_system (Ξ± : Type*) :=
(has : set Ξ± β Prop)
(has_empty : has β
)
(has_compl : β {a}, has a β has aαΆ)
(has_Union_nat : β {f : β β set Ξ±}, pairwise (disjoint on f) β (β i, has (f i)) β has (β i, f i))
namespace dynkin_system
@[ext] lemma ext : β {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
variable (d : dynkin_system Ξ±)
lemma has_compl_iff {a} : d.has aαΆ β d.has a :=
β¨Ξ» h, by simpa using d.has_compl h, Ξ» h, d.has_compl hβ©
lemma has_univ : d.has univ :=
by simpa using d.has_compl d.has_empty
theorem has_Union {Ξ²} [encodable Ξ²] {f : Ξ² β set Ξ±}
(hd : pairwise (disjoint on f)) (h : β i, d.has (f i)) : d.has (β i, f i) :=
by { rw β encodable.Union_decodeβ, exact
d.has_Union_nat (encodable.Union_decodeβ_disjoint_on hd)
(Ξ» n, encodable.Union_decodeβ_cases d.has_empty h) }
theorem has_union {sβ sβ : set Ξ±}
(hβ : d.has sβ) (hβ : d.has sβ) (h : sβ β© sβ β β
) : d.has (sβ βͺ sβ) :=
by { rw union_eq_Union, exact
d.has_Union (pairwise_disjoint_on_bool.2 h) (bool.forall_bool.2 β¨hβ, hββ©) }
lemma has_diff {sβ sβ : set Ξ±} (hβ : d.has sβ) (hβ : d.has sβ) (h : sβ β sβ) : d.has (sβ \ sβ) :=
begin
apply d.has_compl_iff.1,
simp [diff_eq, compl_inter],
exact d.has_union (d.has_compl hβ) hβ (Ξ» x β¨hβ, hββ©, hβ (h hβ)),
end
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β, ext $ assume s, β¨hβ s, hβ sβ© }
/-- Every measurable space (Ο-algebra) forms a Dynkin system -/
def of_measurable_space (m : measurable_space Ξ±) : dynkin_system Ξ± :=
{ has := m.measurable_set',
has_empty := m.measurable_set_empty,
has_compl := m.measurable_set_compl,
has_Union_nat := assume f _ hf, m.measurable_set_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.rfl
/-- The least Dynkin system containing a collection of basic sets.
This inductive type gives the underlying collection of 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 Ξ±}, pairwise (disjoint on f) β
(β i, generate_has (f i)) β generate_has (β i, f i)
lemma generate_has_compl {C : set (set Ξ±)} {s : set Ξ±} : generate_has C sαΆ β generate_has C s :=
by { refine β¨_, generate_has.complβ©, intro h, convert generate_has.compl h, simp }
/-- The least Dynkin system containing a collection of basic sets. -/
def generate (s : set (set Ξ±)) : dynkin_system Ξ± :=
{ has := generate_has s,
has_empty := generate_has.empty,
has_compl := assume a, generate_has.compl,
has_Union_nat := assume f, generate_has.Union }
lemma generate_has_def {C : set (set Ξ±)} : (generate C).has = generate_has C := rfl
instance : inhabited (dynkin_system Ξ±) := β¨generate univβ©
/-- If a Dynkin system is closed under binary intersection, then it forms a `Ο`-algebra. -/
def to_measurable_space (h_inter : β sβ sβ, d.has sβ β d.has sβ β d.has (sβ β© sβ)) :=
{ measurable_space .
measurable_set' := d.has,
measurable_set_empty := d.has_empty,
measurable_set_compl := assume s h, d.has_compl h,
measurable_set_Union := assume f hf,
have β n, d.has (disjointed f n),
from assume n, disjointed_induct (hf n)
(assume t i h, h_inter _ _ h $ d.has_compl $ hf i),
have d.has (β n, disjointed f n), from d.has_Union disjoint_disjointed this,
by rwa [Union_disjointed] 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 :=
ext $ assume s, iff.rfl
/-- If `s` is in a Dynkin system `d`, we can form the new Dynkin system `{s β© t | t β d}`. -/
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], exact d.has_diff (d.has_compl hts) (d.has_compl h)
(compl_subset_compl.mpr $ inter_subset_right _ _) },
has_Union_nat := assume f hd hf,
begin
rw [inter_comm, inter_Union],
apply d.has_Union_nat,
{ exact Ξ» i j h x β¨β¨_, hββ©, _, hββ©, hd i j h β¨hβ, hββ© },
{ simpa [inter_comm] using hf },
end }
lemma generate_le {s : set (set Ξ±)} (h : β t β s, d.has t) : generate s β€ d :=
Ξ» 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)
lemma generate_has_subset_generate_measurable {C : set (set Ξ±)} {s : set Ξ±}
(hs : (generate C).has s) : (generate_from C).measurable_set' s :=
generate_le (of_measurable_space (generate_from C)) (Ξ» t, measurable_set_generate_from) s hs
lemma generate_inter {s : set (set Ξ±)}
(hs : is_pi_system 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
(sβ β© sβ).eq_empty_or_nonempty.elim
(Ξ» h, h.symm βΈ generate_has.empty)
(Ξ» h, generate_has.basic _ (hs _ _ hsβ hsβ h)),
have (generate s).has (tβ β© sβ), from this _ htβ,
show (generate s).has (sβ β© tβ), by rwa [inter_comm],
this _ htβ
/--
**Dynkin's Ο-Ξ» theorem**:
Given a collection of sets closed under binary intersections, then the Dynkin system it
generates is equal to the Ο-algebra it generates.
This result is known as the Ο-Ξ» theorem.
A collection of sets closed under binary intersection is called a Ο-system (often requiring
additionnally that is is non-empty, but we drop this condition in the formalization).
-/
lemma generate_from_eq {s : set (set Ξ±)} (hs : is_pi_system s) :
generate_from s = (generate s).to_measurable_space (Ξ» 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],
exact (generate_le _ $ assume t ht, measurable_set_generate_from ht) })
end dynkin_system
theorem induction_on_inter {C : set Ξ± β Prop} {s : set (set Ξ±)} [m : measurable_space Ξ±]
(h_eq : m = generate_from s) (h_inter : is_pi_system s)
(h_empty : C β
) (h_basic : β t β s, C t) (h_compl : β t, measurable_set t β C t β C tαΆ)
(h_union : β f : β β set Ξ±, pairwise (disjoint on f) β
(β i, measurable_set (f i)) β (β i, C (f i)) β C (β i, f i)) :
β β¦tβ¦, measurable_set t β C t :=
have eq : measurable_set = 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
|
fb1a65ef55d8ce380253a1a6c96539ab40ea5e75 | a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91 | /tests/lean/run/back_chaining1.lean | fd6cccda714012eac5fe70b8212e68e0251b2847 | [
"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 | 599 | lean | -- Backward chaining with tagged rules
constants {P Q R S T U : Prop} (Hpq : P β Q) (Hqr : Q β R) (Hrs : R β S) (Hst : S β T)
constants (Huq : U β Q) (Hur : U β R) (Hus : U β S) (Hut : U β T)
attribute Hpq [intro]
attribute Hqr [intro]
attribute Hrs [intro]
attribute Hst [intro]
attribute Huq [intro]
attribute Hur [intro]
attribute Hus [intro]
attribute Hut [intro]
open tactic
definition lemma1 (p : P) : Q := by back_chaining
definition lemma2 (p : P) : R := by back_chaining
definition lemma3 (p : P) : S := by back_chaining
definition lemma4 (p : P) : T := by back_chaining
|
20398a6785d03f754fc259583cccb3db2d7873df | 69d4931b605e11ca61881fc4f66db50a0a875e39 | /src/measure_theory/integration.lean | 126cba98fdedc1c09bd03cc41e8084ab91b54690 | [
"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 | 84,990 | lean | /-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro, Johannes HΓΆlzl
-/
import measure_theory.measure_space
import measure_theory.borel_space
import data.indicator_function
import data.support
/-!
# Lebesgue integral for `ββ₯0β`-valued functions
We define simple functions and show that each Borel measurable function on `ββ₯0β` can be
approximated by a sequence of simple functions.
To prove something for an arbitrary measurable function into `ββ₯0β`, the theorem
`measurable.ennreal_induction` shows that is it sufficient to show that the property holds for
(multiples of) characteristic functions and is closed under addition and supremum of increasing
sequences of functions.
## Notation
We introduce the following notation for the lower Lebesgue integral of a function `f : Ξ± β ββ₯0β`.
* `β«β» x, f x βΞΌ`: integral of a function `f : Ξ± β ββ₯0β` with respect to a measure `ΞΌ`;
* `β«β» x, f x`: integral of a function `f : Ξ± β ββ₯0β` with respect to the canonical measure
`volume` on `Ξ±`;
* `β«β» x in s, f x βΞΌ`: integral of a function `f : Ξ± β ββ₯0β` over a set `s` with respect
to a measure `ΞΌ`, defined as `β«β» x, f x β(ΞΌ.restrict s)`;
* `β«β» x in s, f x`: integral of a function `f : Ξ± β ββ₯0β` over a set `s` with respect
to the canonical measure `volume`, defined as `β«β» x, f x β(volume.restrict s)`.
-/
noncomputable theory
open set (hiding restrict restrict_apply) filter ennreal function (support)
open_locale classical topological_space big_operators nnreal ennreal
namespace measure_theory
variables {Ξ± Ξ² Ξ³ Ξ΄ : Type*}
/-- A function `f` from a measurable space to any type is called *simple*,
if every preimage `f β»ΒΉ' {x}` is measurable, and the range is finite. This structure bundles
a function with these properties. -/
structure {u v} simple_func (Ξ± : Type u) [measurable_space Ξ±] (Ξ² : Type v) :=
(to_fun : Ξ± β Ξ²)
(measurable_set_fiber' : β x, measurable_set (to_fun β»ΒΉ' {x}))
(finite_range' : (set.range to_fun).finite)
local infixr ` ββ `:25 := simple_func
namespace simple_func
section measurable
variables [measurable_space Ξ±]
instance has_coe_to_fun : has_coe_to_fun (Ξ± ββ Ξ²) := β¨_, to_funβ©
lemma coe_injective β¦f g : Ξ± ββ Ξ²β¦ (H : βf = g) : f = g :=
by cases f; cases g; congr; exact H
@[ext] theorem ext {f g : Ξ± ββ Ξ²} (H : β a, f a = g a) : f = g :=
coe_injective $ funext H
lemma finite_range (f : Ξ± ββ Ξ²) : (set.range f).finite := f.finite_range'
lemma measurable_set_fiber (f : Ξ± ββ Ξ²) (x : Ξ²) : measurable_set (f β»ΒΉ' {x}) :=
f.measurable_set_fiber' x
/-- Range of a simple function `Ξ± ββ Ξ²` as a `finset Ξ²`. -/
protected def range (f : Ξ± ββ Ξ²) : finset Ξ² := f.finite_range.to_finset
@[simp] theorem mem_range {f : Ξ± ββ Ξ²} {b} : b β f.range β b β range f :=
finite.mem_to_finset _
theorem mem_range_self (f : Ξ± ββ Ξ²) (x : Ξ±) : f x β f.range := mem_range.2 β¨x, rflβ©
@[simp] lemma coe_range (f : Ξ± ββ Ξ²) : (βf.range : set Ξ²) = set.range f :=
f.finite_range.coe_to_finset
theorem mem_range_of_measure_ne_zero {f : Ξ± ββ Ξ²} {x : Ξ²} {ΞΌ : measure Ξ±} (H : ΞΌ (f β»ΒΉ' {x}) β 0) :
x β f.range :=
let β¨a, haβ© := nonempty_of_measure_ne_zero H in
mem_range.2 β¨a, haβ©
lemma forall_range_iff {f : Ξ± ββ Ξ²} {p : Ξ² β Prop} :
(β y β f.range, p y) β β x, p (f x) :=
by simp only [mem_range, set.forall_range_iff]
lemma exists_range_iff {f : Ξ± ββ Ξ²} {p : Ξ² β Prop} :
(β y β f.range, p y) β β x, p (f x) :=
by simpa only [mem_range, exists_prop] using set.exists_range_iff
lemma preimage_eq_empty_iff (f : Ξ± ββ Ξ²) (b : Ξ²) : f β»ΒΉ' {b} = β
β b β f.range :=
preimage_singleton_eq_empty.trans $ not_congr mem_range.symm
lemma exists_forall_le [nonempty Ξ²] [directed_order Ξ²] (f : Ξ± ββ Ξ²) :
β C, β x, f x β€ C :=
f.range.exists_le.imp $ Ξ» C, forall_range_iff.1
/-- Constant function as a `simple_func`. -/
def const (Ξ±) {Ξ²} [measurable_space Ξ±] (b : Ξ²) : Ξ± ββ Ξ² :=
β¨Ξ» a, b, Ξ» x, measurable_set.const _, finite_range_constβ©
instance [inhabited Ξ²] : inhabited (Ξ± ββ Ξ²) := β¨const _ (default _)β©
theorem const_apply (a : Ξ±) (b : Ξ²) : (const Ξ± b) a = b := rfl
@[simp] theorem coe_const (b : Ξ²) : β(const Ξ± b) = function.const Ξ± b := rfl
@[simp] lemma range_const (Ξ±) [measurable_space Ξ±] [nonempty Ξ±] (b : Ξ²) :
(const Ξ± b).range = {b} :=
finset.coe_injective $ by simp
lemma measurable_set_cut (r : Ξ± β Ξ² β Prop) (f : Ξ± ββ Ξ²)
(h : βb, measurable_set {a | r a b}) : measurable_set {a | r a (f a)} :=
begin
have : {a | r a (f a)} = β b β range f, {a | r a b} β© f β»ΒΉ' {b},
{ ext a,
suffices : r a (f a) β β i, r a (f i) β§ f a = f i, by simpa,
exact β¨Ξ» h, β¨a, β¨h, rflβ©β©, Ξ» β¨a', β¨h', eβ©β©, e.symm βΈ h'β© },
rw this,
exact measurable_set.bUnion f.finite_range.countable
(Ξ» b _, measurable_set.inter (h b) (f.measurable_set_fiber _))
end
theorem measurable_set_preimage (f : Ξ± ββ Ξ²) (s) : measurable_set (f β»ΒΉ' s) :=
measurable_set_cut (Ξ» _ b, b β s) f (Ξ» b, measurable_set.const (b β s))
/-- A simple function is measurable -/
protected theorem measurable [measurable_space Ξ²] (f : Ξ± ββ Ξ²) : measurable f :=
Ξ» s _, measurable_set_preimage f s
protected theorem ae_measurable [measurable_space Ξ²] {ΞΌ : measure Ξ±} (f : Ξ± ββ Ξ²) :
ae_measurable f ΞΌ :=
f.measurable.ae_measurable
protected lemma sum_measure_preimage_singleton (f : Ξ± ββ Ξ²) {ΞΌ : measure Ξ±} (s : finset Ξ²) :
β y in s, ΞΌ (f β»ΒΉ' {y}) = ΞΌ (f β»ΒΉ' βs) :=
sum_measure_preimage_singleton _ (Ξ» _ _, f.measurable_set_fiber _)
lemma sum_range_measure_preimage_singleton (f : Ξ± ββ Ξ²) (ΞΌ : measure Ξ±) :
β y in f.range, ΞΌ (f β»ΒΉ' {y}) = ΞΌ univ :=
by rw [f.sum_measure_preimage_singleton, coe_range, preimage_range]
/-- If-then-else as a `simple_func`. -/
def piecewise (s : set Ξ±) (hs : measurable_set s) (f g : Ξ± ββ Ξ²) : Ξ± ββ Ξ² :=
β¨s.piecewise f g,
Ξ» x, by letI : measurable_space Ξ² := β€; exact
f.measurable.piecewise hs g.measurable trivial,
(f.finite_range.union g.finite_range).subset range_ite_subsetβ©
@[simp] theorem coe_piecewise {s : set Ξ±} (hs : measurable_set s) (f g : Ξ± ββ Ξ²) :
β(piecewise s hs f g) = s.piecewise f g :=
rfl
theorem piecewise_apply {s : set Ξ±} (hs : measurable_set s) (f g : Ξ± ββ Ξ²) (a) :
piecewise s hs f g a = if a β s then f a else g a :=
rfl
@[simp] lemma piecewise_compl {s : set Ξ±} (hs : measurable_set sαΆ) (f g : Ξ± ββ Ξ²) :
piecewise sαΆ hs f g = piecewise s hs.of_compl g f :=
coe_injective $ by simp [hs]
@[simp] lemma piecewise_univ (f g : Ξ± ββ Ξ²) : piecewise univ measurable_set.univ f g = f :=
coe_injective $ by simp
@[simp] lemma piecewise_empty (f g : Ξ± ββ Ξ²) : piecewise β
measurable_set.empty f g = g :=
coe_injective $ by simp
lemma measurable_bind [measurable_space Ξ³] (f : Ξ± ββ Ξ²) (g : Ξ² β Ξ± β Ξ³)
(hg : β b, measurable (g b)) : measurable (Ξ» a, g (f a) a) :=
Ξ» s hs, f.measurable_set_cut (Ξ» a b, g b a β s) $ Ξ» b, hg b hs
/-- If `f : Ξ± ββ Ξ²` is a simple function and `g : Ξ² β Ξ± ββ Ξ³` is a family of simple functions,
then `f.bind g` binds the first argument of `g` to `f`. In other words, `f.bind g a = g (f a) a`. -/
def bind (f : Ξ± ββ Ξ²) (g : Ξ² β Ξ± ββ Ξ³) : Ξ± ββ Ξ³ :=
β¨Ξ»a, g (f a) a,
Ξ» c, f.measurable_set_cut (Ξ» a b, g b a = c) $ Ξ» b, (g b).measurable_set_preimage {c},
(f.finite_range.bUnion (Ξ» b _, (g b).finite_range)).subset $
by rintro _ β¨a, rflβ©; simp; exact β¨a, a, rflβ©β©
@[simp] theorem bind_apply (f : Ξ± ββ Ξ²) (g : Ξ² β Ξ± ββ Ξ³) (a) :
f.bind g a = g (f a) a := rfl
/-- Given a function `g : Ξ² β Ξ³` and a simple function `f : Ξ± ββ Ξ²`, `f.map g` return the simple
function `g β f : Ξ± ββ Ξ³` -/
def map (g : Ξ² β Ξ³) (f : Ξ± ββ Ξ²) : Ξ± ββ Ξ³ := bind f (const Ξ± β g)
theorem map_apply (g : Ξ² β Ξ³) (f : Ξ± ββ Ξ²) (a) : f.map g a = g (f a) := rfl
theorem map_map (g : Ξ² β Ξ³) (h: Ξ³ β Ξ΄) (f : Ξ± ββ Ξ²) : (f.map g).map h = f.map (h β g) := rfl
@[simp] theorem coe_map (g : Ξ² β Ξ³) (f : Ξ± ββ Ξ²) : (f.map g : Ξ± β Ξ³) = g β f := rfl
@[simp] theorem range_map [decidable_eq Ξ³] (g : Ξ² β Ξ³) (f : Ξ± ββ Ξ²) :
(f.map g).range = f.range.image g :=
finset.coe_injective $ by simp [range_comp]
@[simp] theorem map_const (g : Ξ² β Ξ³) (b : Ξ²) : (const Ξ± b).map g = const Ξ± (g b) := rfl
lemma map_preimage (f : Ξ± ββ Ξ²) (g : Ξ² β Ξ³) (s : set Ξ³) :
(f.map g) β»ΒΉ' s = f β»ΒΉ' β(f.range.filter (Ξ»b, g b β s)) :=
by { simp only [coe_range, sep_mem_eq, set.mem_range, function.comp_app, coe_map, finset.coe_filter,
β mem_preimage, inter_comm, preimage_inter_range], apply preimage_comp }
lemma map_preimage_singleton (f : Ξ± ββ Ξ²) (g : Ξ² β Ξ³) (c : Ξ³) :
(f.map g) β»ΒΉ' {c} = f β»ΒΉ' β(f.range.filter (Ξ» b, g b = c)) :=
map_preimage _ _ _
/-- Composition of a `simple_fun` and a measurable function is a `simple_func`. -/
def comp [measurable_space Ξ²] (f : Ξ² ββ Ξ³) (g : Ξ± β Ξ²) (hgm : measurable g) : Ξ± ββ Ξ³ :=
{ to_fun := f β g,
finite_range' := f.finite_range.subset $ set.range_comp_subset_range _ _,
measurable_set_fiber' := Ξ» z, hgm (f.measurable_set_fiber z) }
@[simp] lemma coe_comp [measurable_space Ξ²] (f : Ξ² ββ Ξ³) {g : Ξ± β Ξ²} (hgm : measurable g) :
β(f.comp g hgm) = f β g :=
rfl
lemma range_comp_subset_range [measurable_space Ξ²] (f : Ξ² ββ Ξ³) {g : Ξ± β Ξ²} (hgm : measurable g) :
(f.comp g hgm).range β f.range :=
finset.coe_subset.1 $ by simp only [coe_range, coe_comp, set.range_comp_subset_range]
/-- If `f` is a simple function taking values in `Ξ² β Ξ³` and `g` is another simple function
with the same domain and codomain `Ξ²`, then `f.seq g = f a (g a)`. -/
def seq (f : Ξ± ββ (Ξ² β Ξ³)) (g : Ξ± ββ Ξ²) : Ξ± ββ Ξ³ := f.bind (Ξ»f, g.map f)
@[simp] lemma seq_apply (f : Ξ± ββ (Ξ² β Ξ³)) (g : Ξ± ββ Ξ²) (a : Ξ±) : f.seq g a = f a (g a) := rfl
/-- Combine two simple functions `f : Ξ± ββ Ξ²` and `g : Ξ± ββ Ξ²`
into `Ξ» a, (f a, g a)`. -/
def pair (f : Ξ± ββ Ξ²) (g : Ξ± ββ Ξ³) : Ξ± ββ (Ξ² Γ Ξ³) := (f.map prod.mk).seq g
@[simp] lemma pair_apply (f : Ξ± ββ Ξ²) (g : Ξ± ββ Ξ³) (a) : pair f g a = (f a, g a) := rfl
lemma pair_preimage (f : Ξ± ββ Ξ²) (g : Ξ± ββ Ξ³) (s : set Ξ²) (t : set Ξ³) :
(pair f g) β»ΒΉ' (set.prod s t) = (f β»ΒΉ' s) β© (g β»ΒΉ' t) := rfl
/- A special form of `pair_preimage` -/
lemma pair_preimage_singleton (f : Ξ± ββ Ξ²) (g : Ξ± ββ Ξ³) (b : Ξ²) (c : Ξ³) :
(pair f g) β»ΒΉ' {(b, c)} = (f β»ΒΉ' {b}) β© (g β»ΒΉ' {c}) :=
by { rw β singleton_prod_singleton, exact pair_preimage _ _ _ _ }
theorem bind_const (f : Ξ± ββ Ξ²) : f.bind (const Ξ±) = f := by ext; simp
instance [has_zero Ξ²] : has_zero (Ξ± ββ Ξ²) := β¨const Ξ± 0β©
instance [has_add Ξ²] : has_add (Ξ± ββ Ξ²) := β¨Ξ»f g, (f.map (+)).seq gβ©
instance [has_mul Ξ²] : has_mul (Ξ± ββ Ξ²) := β¨Ξ»f g, (f.map (*)).seq gβ©
instance [has_sup Ξ²] : has_sup (Ξ± ββ Ξ²) := β¨Ξ»f g, (f.map (β)).seq gβ©
instance [has_inf Ξ²] : has_inf (Ξ± ββ Ξ²) := β¨Ξ»f g, (f.map (β)).seq gβ©
instance [has_le Ξ²] : has_le (Ξ± ββ Ξ²) := β¨Ξ»f g, βa, f a β€ g aβ©
@[simp, norm_cast] lemma coe_zero [has_zero Ξ²] : β(0 : Ξ± ββ Ξ²) = 0 := rfl
@[simp] lemma const_zero [has_zero Ξ²] : const Ξ± (0:Ξ²) = 0 := rfl
@[simp, norm_cast] lemma coe_add [has_add Ξ²] (f g : Ξ± ββ Ξ²) : β(f + g) = f + g := rfl
@[simp, norm_cast] lemma coe_mul [has_mul Ξ²] (f g : Ξ± ββ Ξ²) : β(f * g) = f * g := rfl
@[simp, norm_cast] lemma coe_le [preorder Ξ²] {f g : Ξ± ββ Ξ²} : (f : Ξ± β Ξ²) β€ g β f β€ g := iff.rfl
@[simp] lemma range_zero [nonempty Ξ±] [has_zero Ξ²] : (0 : Ξ± ββ Ξ²).range = {0} :=
finset.ext $ Ξ» x, by simp [eq_comm]
lemma eq_zero_of_mem_range_zero [has_zero Ξ²] : β {y : Ξ²}, y β (0 : Ξ± ββ Ξ²).range β y = 0 :=
forall_range_iff.2 $ Ξ» x, rfl
lemma sup_apply [has_sup Ξ²] (f g : Ξ± ββ Ξ²) (a : Ξ±) : (f β g) a = f a β g a := rfl
lemma mul_apply [has_mul Ξ²] (f g : Ξ± ββ Ξ²) (a : Ξ±) : (f * g) a = f a * g a := rfl
lemma add_apply [has_add Ξ²] (f g : Ξ± ββ Ξ²) (a : Ξ±) : (f + g) a = f a + g a := rfl
lemma add_eq_mapβ [has_add Ξ²] (f g : Ξ± ββ Ξ²) : f + g = (pair f g).map (Ξ»p:Ξ²ΓΞ², p.1 + p.2) :=
rfl
lemma mul_eq_mapβ [has_mul Ξ²] (f g : Ξ± ββ Ξ²) : f * g = (pair f g).map (Ξ»p:Ξ²ΓΞ², p.1 * p.2) :=
rfl
lemma sup_eq_mapβ [has_sup Ξ²] (f g : Ξ± ββ Ξ²) : f β g = (pair f g).map (Ξ»p:Ξ²ΓΞ², p.1 β p.2) :=
rfl
lemma const_mul_eq_map [has_mul Ξ²] (f : Ξ± ββ Ξ²) (b : Ξ²) : const Ξ± b * f = f.map (Ξ»a, b * a) := rfl
theorem map_add [has_add Ξ²] [has_add Ξ³] {g : Ξ² β Ξ³}
(hg : β x y, g (x + y) = g x + g y) (fβ fβ : Ξ± ββ Ξ²) : (fβ + fβ).map g = fβ.map g + fβ.map g :=
ext $ Ξ» x, hg _ _
instance [add_monoid Ξ²] : add_monoid (Ξ± ββ Ξ²) :=
function.injective.add_monoid (Ξ» f, show Ξ± β Ξ², from f) coe_injective coe_zero coe_add
instance add_comm_monoid [add_comm_monoid Ξ²] : add_comm_monoid (Ξ± ββ Ξ²) :=
function.injective.add_comm_monoid (Ξ» f, show Ξ± β Ξ², from f) coe_injective coe_zero coe_add
instance [has_neg Ξ²] : has_neg (Ξ± ββ Ξ²) := β¨Ξ»f, f.map (has_neg.neg)β©
@[simp, norm_cast] lemma coe_neg [has_neg Ξ²] (f : Ξ± ββ Ξ²) : β(-f) = -f := rfl
instance [has_sub Ξ²] : has_sub (Ξ± ββ Ξ²) := β¨Ξ»f g, (f.map (has_sub.sub)).seq gβ©
@[simp, norm_cast] lemma coe_sub [has_sub Ξ²] (f g : Ξ± ββ Ξ²) : β(f - g) = f - g :=
rfl
lemma sub_apply [has_sub Ξ²] (f g : Ξ± ββ Ξ²) (x : Ξ±) : (f - g) x = f x - g x := rfl
instance [add_group Ξ²] : add_group (Ξ± ββ Ξ²) :=
function.injective.add_group (Ξ» f, show Ξ± β Ξ², from f) coe_injective
coe_zero coe_add coe_neg coe_sub
instance [add_comm_group Ξ²] : add_comm_group (Ξ± ββ Ξ²) :=
function.injective.add_comm_group (Ξ» f, show Ξ± β Ξ², from f) coe_injective
coe_zero coe_add coe_neg coe_sub
variables {K : Type*}
instance [has_scalar K Ξ²] : has_scalar K (Ξ± ββ Ξ²) := β¨Ξ»k f, f.map ((β’) k)β©
@[simp] lemma coe_smul [has_scalar K Ξ²] (c : K) (f : Ξ± ββ Ξ²) : β(c β’ f) = c β’ f := rfl
lemma smul_apply [has_scalar K Ξ²] (k : K) (f : Ξ± ββ Ξ²) (a : Ξ±) : (k β’ f) a = k β’ f a := rfl
instance [semiring K] [add_comm_monoid Ξ²] [module K Ξ²] : module K (Ξ± ββ Ξ²) :=
function.injective.module K β¨Ξ» f, show Ξ± β Ξ², from f, coe_zero, coe_addβ©
coe_injective coe_smul
lemma smul_eq_map [has_scalar K Ξ²] (k : K) (f : Ξ± ββ Ξ²) : k β’ f = f.map ((β’) k) := rfl
instance [preorder Ξ²] : preorder (Ξ± ββ Ξ²) :=
{ le_refl := Ξ»f a, le_refl _,
le_trans := Ξ»f g h hfg hgh a, le_trans (hfg _) (hgh a),
.. simple_func.has_le }
instance [partial_order Ξ²] : partial_order (Ξ± ββ Ξ²) :=
{ le_antisymm := assume f g hfg hgf, ext $ assume a, le_antisymm (hfg a) (hgf a),
.. simple_func.preorder }
instance [order_bot Ξ²] : order_bot (Ξ± ββ Ξ²) :=
{ bot := const Ξ± β₯, bot_le := Ξ»f a, bot_le, .. simple_func.partial_order }
instance [order_top Ξ²] : order_top (Ξ± ββ Ξ²) :=
{ top := const Ξ± β€, le_top := Ξ»f a, le_top, .. simple_func.partial_order }
instance [semilattice_inf Ξ²] : semilattice_inf (Ξ± ββ Ξ²) :=
{ inf := (β),
inf_le_left := assume f g a, inf_le_left,
inf_le_right := assume f g a, inf_le_right,
le_inf := assume f g h hfh hgh a, le_inf (hfh a) (hgh a),
.. simple_func.partial_order }
instance [semilattice_sup Ξ²] : semilattice_sup (Ξ± ββ Ξ²) :=
{ sup := (β),
le_sup_left := assume f g a, le_sup_left,
le_sup_right := assume f g a, le_sup_right,
sup_le := assume f g h hfh hgh a, sup_le (hfh a) (hgh a),
.. simple_func.partial_order }
instance [semilattice_sup_bot Ξ²] : semilattice_sup_bot (Ξ± ββ Ξ²) :=
{ .. simple_func.semilattice_sup,.. simple_func.order_bot }
instance [lattice Ξ²] : lattice (Ξ± ββ Ξ²) :=
{ .. simple_func.semilattice_sup,.. simple_func.semilattice_inf }
instance [bounded_lattice Ξ²] : bounded_lattice (Ξ± ββ Ξ²) :=
{ .. simple_func.lattice, .. simple_func.order_bot, .. simple_func.order_top }
lemma finset_sup_apply [semilattice_sup_bot Ξ²] {f : Ξ³ β Ξ± ββ Ξ²} (s : finset Ξ³) (a : Ξ±) :
s.sup f a = s.sup (Ξ»c, f c a) :=
begin
refine finset.induction_on s rfl _,
assume a s hs ih,
rw [finset.sup_insert, finset.sup_insert, sup_apply, ih]
end
section restrict
variables [has_zero Ξ²]
/-- Restrict a simple function `f : Ξ± ββ Ξ²` to a set `s`. If `s` is measurable,
then `f.restrict s a = if a β s then f a else 0`, otherwise `f.restrict s = const Ξ± 0`. -/
def restrict (f : Ξ± ββ Ξ²) (s : set Ξ±) : Ξ± ββ Ξ² :=
if hs : measurable_set s then piecewise s hs f 0 else 0
theorem restrict_of_not_measurable {f : Ξ± ββ Ξ²} {s : set Ξ±}
(hs : Β¬measurable_set s) :
restrict f s = 0 :=
dif_neg hs
@[simp] theorem coe_restrict (f : Ξ± ββ Ξ²) {s : set Ξ±} (hs : measurable_set s) :
β(restrict f s) = indicator s f :=
by { rw [restrict, dif_pos hs], refl }
@[simp] theorem restrict_univ (f : Ξ± ββ Ξ²) : restrict f univ = f :=
by simp [restrict]
@[simp] theorem restrict_empty (f : Ξ± ββ Ξ²) : restrict f β
= 0 :=
by simp [restrict]
theorem map_restrict_of_zero [has_zero Ξ³] {g : Ξ² β Ξ³} (hg : g 0 = 0) (f : Ξ± ββ Ξ²) (s : set Ξ±) :
(f.restrict s).map g = (f.map g).restrict s :=
ext $ Ξ» x,
if hs : measurable_set s then by simp [hs, set.indicator_comp_of_zero hg]
else by simp [restrict_of_not_measurable hs, hg]
theorem map_coe_ennreal_restrict (f : Ξ± ββ ββ₯0) (s : set Ξ±) :
(f.restrict s).map (coe : ββ₯0 β ββ₯0β) = (f.map coe).restrict s :=
map_restrict_of_zero ennreal.coe_zero _ _
theorem map_coe_nnreal_restrict (f : Ξ± ββ ββ₯0) (s : set Ξ±) :
(f.restrict s).map (coe : ββ₯0 β β) = (f.map coe).restrict s :=
map_restrict_of_zero nnreal.coe_zero _ _
theorem restrict_apply (f : Ξ± ββ Ξ²) {s : set Ξ±} (hs : measurable_set s) (a) :
restrict f s a = indicator s f a :=
by simp only [f.coe_restrict hs]
theorem restrict_preimage (f : Ξ± ββ Ξ²) {s : set Ξ±} (hs : measurable_set s)
{t : set Ξ²} (ht : (0:Ξ²) β t) : restrict f s β»ΒΉ' t = s β© f β»ΒΉ' t :=
by simp [hs, indicator_preimage_of_not_mem _ _ ht, inter_comm]
theorem restrict_preimage_singleton (f : Ξ± ββ Ξ²) {s : set Ξ±} (hs : measurable_set s)
{r : Ξ²} (hr : r β 0) : restrict f s β»ΒΉ' {r} = s β© f β»ΒΉ' {r} :=
f.restrict_preimage hs hr.symm
lemma mem_restrict_range {r : Ξ²} {s : set Ξ±} {f : Ξ± ββ Ξ²} (hs : measurable_set s) :
r β (restrict f s).range β (r = 0 β§ s β univ) β¨ (r β f '' s) :=
by rw [β finset.mem_coe, coe_range, coe_restrict _ hs, mem_range_indicator]
lemma mem_image_of_mem_range_restrict {r : Ξ²} {s : set Ξ±} {f : Ξ± ββ Ξ²}
(hr : r β (restrict f s).range) (h0 : r β 0) :
r β f '' s :=
if hs : measurable_set s then by simpa [mem_restrict_range hs, h0] using hr
else by { rw [restrict_of_not_measurable hs] at hr,
exact (h0 $ eq_zero_of_mem_range_zero hr).elim }
@[mono] lemma restrict_mono [preorder Ξ²] (s : set Ξ±) {f g : Ξ± ββ Ξ²} (H : f β€ g) :
f.restrict s β€ g.restrict s :=
if hs : measurable_set s then Ξ» x, by simp only [coe_restrict _ hs, indicator_le_indicator (H x)]
else by simp only [restrict_of_not_measurable hs, le_refl]
end restrict
section approx
section
variables [semilattice_sup_bot Ξ²] [has_zero Ξ²]
/-- Fix a sequence `i : β β Ξ²`. Given a function `Ξ± β Ξ²`, its `n`-th approximation
by simple functions is defined so that in case `Ξ² = ββ₯0β` it sends each `a` to the supremum
of the set `{i k | k β€ n β§ i k β€ f a}`, see `approx_apply` and `supr_approx_apply` for details. -/
def approx (i : β β Ξ²) (f : Ξ± β Ξ²) (n : β) : Ξ± ββ Ξ² :=
(finset.range n).sup (Ξ»k, restrict (const Ξ± (i k)) {a:Ξ± | i k β€ f a})
lemma approx_apply [topological_space Ξ²] [order_closed_topology Ξ²] [measurable_space Ξ²]
[opens_measurable_space Ξ²] {i : β β Ξ²} {f : Ξ± β Ξ²} {n : β} (a : Ξ±) (hf : measurable f) :
(approx i f n : Ξ± ββ Ξ²) a = (finset.range n).sup (Ξ»k, if i k β€ f a then i k else 0) :=
begin
dsimp only [approx],
rw [finset_sup_apply],
congr,
funext k,
rw [restrict_apply],
refl,
exact (hf measurable_set_Ici)
end
lemma monotone_approx (i : β β Ξ²) (f : Ξ± β Ξ²) : monotone (approx i f) :=
assume n m h, finset.sup_mono $ finset.range_subset.2 h
lemma approx_comp [topological_space Ξ²] [order_closed_topology Ξ²] [measurable_space Ξ²]
[opens_measurable_space Ξ²] [measurable_space Ξ³]
{i : β β Ξ²} {f : Ξ³ β Ξ²} {g : Ξ± β Ξ³} {n : β} (a : Ξ±)
(hf : measurable f) (hg : measurable g) :
(approx i (f β g) n : Ξ± ββ Ξ²) a = (approx i f n : Ξ³ ββ Ξ²) (g a) :=
by rw [approx_apply _ hf, approx_apply _ (hf.comp hg)]
end
lemma supr_approx_apply [topological_space Ξ²] [complete_lattice Ξ²] [order_closed_topology Ξ²]
[has_zero Ξ²] [measurable_space Ξ²] [opens_measurable_space Ξ²]
(i : β β Ξ²) (f : Ξ± β Ξ²) (a : Ξ±) (hf : measurable f) (h_zero : (0 : Ξ²) = β₯) :
(β¨n, (approx i f n : Ξ± ββ Ξ²) a) = (β¨k (h : i k β€ f a), i k) :=
begin
refine le_antisymm (supr_le $ assume n, _) (supr_le $ assume k, supr_le $ assume hk, _),
{ rw [approx_apply a hf, h_zero],
refine finset.sup_le (assume k hk, _),
split_ifs,
exact le_supr_of_le k (le_supr _ h),
exact bot_le },
{ refine le_supr_of_le (k+1) _,
rw [approx_apply a hf],
have : k β finset.range (k+1) := finset.mem_range.2 (nat.lt_succ_self _),
refine le_trans (le_of_eq _) (finset.le_sup this),
rw [if_pos hk] }
end
end approx
section eapprox
/-- A sequence of `ββ₯0β`s such that its range is the set of non-negative rational numbers. -/
def ennreal_rat_embed (n : β) : ββ₯0β :=
ennreal.of_real ((encodable.decode β n).get_or_else (0 : β))
lemma ennreal_rat_embed_encode (q : β) :
ennreal_rat_embed (encodable.encode q) = nnreal.of_real q :=
by rw [ennreal_rat_embed, encodable.encodek]; refl
/-- Approximate a function `Ξ± β ββ₯0β` by a sequence of simple functions. -/
def eapprox : (Ξ± β ββ₯0β) β β β Ξ± ββ ββ₯0β :=
approx ennreal_rat_embed
@[mono] lemma monotone_eapprox (f : Ξ± β ββ₯0β) : monotone (eapprox f) :=
monotone_approx _ f
lemma supr_eapprox_apply (f : Ξ± β ββ₯0β) (hf : measurable f) (a : Ξ±) :
(β¨n, (eapprox f n : Ξ± ββ ββ₯0β) a) = f a :=
begin
rw [eapprox, supr_approx_apply ennreal_rat_embed f a hf rfl],
refine le_antisymm (supr_le $ assume i, supr_le $ assume hi, hi) (le_of_not_gt _),
assume h,
rcases ennreal.lt_iff_exists_rat_btwn.1 h with β¨q, hq, lt_q, q_ltβ©,
have : (nnreal.of_real q : ββ₯0β) β€
(β¨ (k : β) (h : ennreal_rat_embed k β€ f a), ennreal_rat_embed k),
{ refine le_supr_of_le (encodable.encode q) _,
rw [ennreal_rat_embed_encode q],
refine le_supr_of_le (le_of_lt q_lt) _,
exact le_refl _ },
exact lt_irrefl _ (lt_of_le_of_lt this lt_q)
end
lemma eapprox_comp [measurable_space Ξ³] {f : Ξ³ β ββ₯0β} {g : Ξ± β Ξ³} {n : β}
(hf : measurable f) (hg : measurable g) :
(eapprox (f β g) n : Ξ± β ββ₯0β) = (eapprox f n : Ξ³ ββ ββ₯0β) β g :=
funext $ assume a, approx_comp a hf hg
end eapprox
end measurable
section measure
variables [measurable_space Ξ±] {ΞΌ : measure Ξ±}
/-- Integral of a simple function whose codomain is `ββ₯0β`. -/
def lintegral (f : Ξ± ββ ββ₯0β) (ΞΌ : measure Ξ±) : ββ₯0β :=
β x in f.range, x * ΞΌ (f β»ΒΉ' {x})
lemma lintegral_eq_of_subset (f : Ξ± ββ ββ₯0β) {s : finset ββ₯0β}
(hs : β x, f x β 0 β ΞΌ (f β»ΒΉ' {f x}) β 0 β f x β s) :
f.lintegral ΞΌ = β x in s, x * ΞΌ (f β»ΒΉ' {x}) :=
begin
refine finset.sum_bij_ne_zero (Ξ»r _ _, r) _ _ _ _,
{ simpa only [forall_range_iff, mul_ne_zero_iff, and_imp] },
{ intros, assumption },
{ intros b _ hb,
refine β¨b, _, hb, rflβ©,
rw [mem_range, β preimage_singleton_nonempty],
exact nonempty_of_measure_ne_zero (mul_ne_zero_iff.1 hb).2 },
{ intros, refl }
end
/-- Calculate the integral of `(g β f)`, where `g : Ξ² β ββ₯0β` and `f : Ξ± ββ Ξ²`. -/
lemma map_lintegral (g : Ξ² β ββ₯0β) (f : Ξ± ββ Ξ²) :
(f.map g).lintegral ΞΌ = β x in f.range, g x * ΞΌ (f β»ΒΉ' {x}) :=
begin
simp only [lintegral, range_map],
refine finset.sum_image' _ (assume b hb, _),
rcases mem_range.1 hb with β¨a, rflβ©,
rw [map_preimage_singleton, β f.sum_measure_preimage_singleton, finset.mul_sum],
refine finset.sum_congr _ _,
{ congr },
{ assume x, simp only [finset.mem_filter], rintro β¨_, hβ©, rw h },
end
lemma add_lintegral (f g : Ξ± ββ ββ₯0β) : (f + g).lintegral ΞΌ = f.lintegral ΞΌ + g.lintegral ΞΌ :=
calc (f + g).lintegral ΞΌ =
β x in (pair f g).range, (x.1 * ΞΌ (pair f g β»ΒΉ' {x}) + x.2 * ΞΌ (pair f g β»ΒΉ' {x})) :
by rw [add_eq_mapβ, map_lintegral]; exact finset.sum_congr rfl (assume a ha, add_mul _ _ _)
... = β x in (pair f g).range, x.1 * ΞΌ (pair f g β»ΒΉ' {x}) +
β x in (pair f g).range, x.2 * ΞΌ (pair f g β»ΒΉ' {x}) : by rw [finset.sum_add_distrib]
... = ((pair f g).map prod.fst).lintegral ΞΌ + ((pair f g).map prod.snd).lintegral ΞΌ :
by rw [map_lintegral, map_lintegral]
... = lintegral f ΞΌ + lintegral g ΞΌ : rfl
lemma const_mul_lintegral (f : Ξ± ββ ββ₯0β) (x : ββ₯0β) :
(const Ξ± x * f).lintegral ΞΌ = x * f.lintegral ΞΌ :=
calc (f.map (Ξ»a, x * a)).lintegral ΞΌ = β r in f.range, x * r * ΞΌ (f β»ΒΉ' {r}) :
map_lintegral _ _
... = β r in f.range, x * (r * ΞΌ (f β»ΒΉ' {r})) :
finset.sum_congr rfl (assume a ha, mul_assoc _ _ _)
... = x * f.lintegral ΞΌ :
finset.mul_sum.symm
/-- Integral of a simple function `Ξ± ββ ββ₯0β` as a bilinear map. -/
def lintegralβ : (Ξ± ββ ββ₯0β) ββ[ββ₯0β] measure Ξ± ββ[ββ₯0β] ββ₯0β :=
{ to_fun := Ξ» f,
{ to_fun := lintegral f,
map_add' := by simp [lintegral, mul_add, finset.sum_add_distrib],
map_smul' := Ξ» c ΞΌ, by simp [lintegral, mul_left_comm _ c, finset.mul_sum] },
map_add' := Ξ» f g, linear_map.ext (Ξ» ΞΌ, add_lintegral f g),
map_smul' := Ξ» c f, linear_map.ext (Ξ» ΞΌ, const_mul_lintegral f c) }
@[simp] lemma zero_lintegral : (0 : Ξ± ββ ββ₯0β).lintegral ΞΌ = 0 :=
linear_map.ext_iff.1 lintegralβ.map_zero ΞΌ
lemma lintegral_add {Ξ½} (f : Ξ± ββ ββ₯0β) : f.lintegral (ΞΌ + Ξ½) = f.lintegral ΞΌ + f.lintegral Ξ½ :=
(lintegralβ f).map_add ΞΌ Ξ½
lemma lintegral_smul (f : Ξ± ββ ββ₯0β) (c : ββ₯0β) :
f.lintegral (c β’ ΞΌ) = c β’ f.lintegral ΞΌ :=
(lintegralβ f).map_smul c ΞΌ
@[simp] lemma lintegral_zero (f : Ξ± ββ ββ₯0β) :
f.lintegral 0 = 0 :=
(lintegralβ f).map_zero
lemma lintegral_sum {ΞΉ} (f : Ξ± ββ ββ₯0β) (ΞΌ : ΞΉ β measure Ξ±) :
f.lintegral (measure.sum ΞΌ) = β' i, f.lintegral (ΞΌ i) :=
begin
simp only [lintegral, measure.sum_apply, f.measurable_set_preimage, β finset.tsum_subtype,
β ennreal.tsum_mul_left],
apply ennreal.tsum_comm
end
lemma restrict_lintegral (f : Ξ± ββ ββ₯0β) {s : set Ξ±} (hs : measurable_set s) :
(restrict f s).lintegral ΞΌ = β r in f.range, r * ΞΌ (f β»ΒΉ' {r} β© s) :=
calc (restrict f s).lintegral ΞΌ = β r in f.range, r * ΞΌ (restrict f s β»ΒΉ' {r}) :
lintegral_eq_of_subset _ $ Ξ» x hx, if hxs : x β s
then Ξ» _, by simp only [f.restrict_apply hs, indicator_of_mem hxs, mem_range_self]
else false.elim $ hx $ by simp [*]
... = β r in f.range, r * ΞΌ (f β»ΒΉ' {r} β© s) :
finset.sum_congr rfl $ forall_range_iff.2 $ Ξ» b, if hb : f b = 0 then by simp only [hb, zero_mul]
else by rw [restrict_preimage_singleton _ hs hb, inter_comm]
lemma lintegral_restrict (f : Ξ± ββ ββ₯0β) (s : set Ξ±) (ΞΌ : measure Ξ±) :
f.lintegral (ΞΌ.restrict s) = β y in f.range, y * ΞΌ (f β»ΒΉ' {y} β© s) :=
by simp only [lintegral, measure.restrict_apply, f.measurable_set_preimage]
lemma restrict_lintegral_eq_lintegral_restrict (f : Ξ± ββ ββ₯0β) {s : set Ξ±}
(hs : measurable_set s) :
(restrict f s).lintegral ΞΌ = f.lintegral (ΞΌ.restrict s) :=
by rw [f.restrict_lintegral hs, lintegral_restrict]
lemma const_lintegral (c : ββ₯0β) : (const Ξ± c).lintegral ΞΌ = c * ΞΌ univ :=
begin
rw [lintegral],
by_cases ha : nonempty Ξ±,
{ resetI, simp [preimage_const_of_mem] },
{ simp [ΞΌ.eq_zero_of_not_nonempty ha] }
end
lemma const_lintegral_restrict (c : ββ₯0β) (s : set Ξ±) :
(const Ξ± c).lintegral (ΞΌ.restrict s) = c * ΞΌ s :=
by rw [const_lintegral, measure.restrict_apply measurable_set.univ, univ_inter]
lemma restrict_const_lintegral (c : ββ₯0β) {s : set Ξ±} (hs : measurable_set s) :
((const Ξ± c).restrict s).lintegral ΞΌ = c * ΞΌ s :=
by rw [restrict_lintegral_eq_lintegral_restrict _ hs, const_lintegral_restrict]
lemma le_sup_lintegral (f g : Ξ± ββ ββ₯0β) : f.lintegral ΞΌ β g.lintegral ΞΌ β€ (f β g).lintegral ΞΌ :=
calc f.lintegral ΞΌ β g.lintegral ΞΌ =
((pair f g).map prod.fst).lintegral ΞΌ β ((pair f g).map prod.snd).lintegral ΞΌ : rfl
... β€ β x in (pair f g).range, (x.1 β x.2) * ΞΌ (pair f g β»ΒΉ' {x}) :
begin
rw [map_lintegral, map_lintegral],
refine sup_le _ _;
refine finset.sum_le_sum (Ξ» a _, canonically_ordered_semiring.mul_le_mul _ (le_refl _)),
exact le_sup_left,
exact le_sup_right
end
... = (f β g).lintegral ΞΌ : by rw [sup_eq_mapβ, map_lintegral]
/-- `simple_func.lintegral` is monotone both in function and in measure. -/
@[mono] lemma lintegral_mono {f g : Ξ± ββ ββ₯0β} (hfg : f β€ g) {ΞΌ Ξ½ : measure Ξ±} (hΞΌΞ½ : ΞΌ β€ Ξ½) :
f.lintegral ΞΌ β€ g.lintegral Ξ½ :=
calc f.lintegral ΞΌ β€ f.lintegral ΞΌ β g.lintegral ΞΌ : le_sup_left
... β€ (f β g).lintegral ΞΌ : le_sup_lintegral _ _
... = g.lintegral ΞΌ : by rw [sup_of_le_right hfg]
... β€ g.lintegral Ξ½ : finset.sum_le_sum $ Ξ» y hy, ennreal.mul_left_mono $
hΞΌΞ½ _ (g.measurable_set_preimage _)
/-- `simple_func.lintegral` depends only on the measures of `f β»ΒΉ' {y}`. -/
lemma lintegral_eq_of_measure_preimage [measurable_space Ξ²] {f : Ξ± ββ ββ₯0β} {g : Ξ² ββ ββ₯0β}
{Ξ½ : measure Ξ²} (H : β y, ΞΌ (f β»ΒΉ' {y}) = Ξ½ (g β»ΒΉ' {y})) :
f.lintegral ΞΌ = g.lintegral Ξ½ :=
begin
simp only [lintegral, β H],
apply lintegral_eq_of_subset,
simp only [H],
intros,
exact mem_range_of_measure_ne_zero βΉ_βΊ
end
/-- If two simple functions are equal a.e., then their `lintegral`s are equal. -/
lemma lintegral_congr {f g : Ξ± ββ ββ₯0β} (h : f =α΅[ΞΌ] g) :
f.lintegral ΞΌ = g.lintegral ΞΌ :=
lintegral_eq_of_measure_preimage $ Ξ» y, measure_congr $
eventually.set_eq $ h.mono $ Ξ» x hx, by simp [hx]
lemma lintegral_map {Ξ²} [measurable_space Ξ²] {ΞΌ' : measure Ξ²} (f : Ξ± ββ ββ₯0β) (g : Ξ² ββ ββ₯0β)
(m : Ξ± β Ξ²) (eq : βa:Ξ±, f a = g (m a)) (h : βs:set Ξ², measurable_set s β ΞΌ' s = ΞΌ (m β»ΒΉ' s)) :
f.lintegral ΞΌ = g.lintegral ΞΌ' :=
lintegral_eq_of_measure_preimage $ Ξ» y,
by { simp only [preimage, eq], exact (h (g β»ΒΉ' {y}) (g.measurable_set_preimage _)).symm }
/-- The `lintegral` of simple functions transforms appropriately under a measurable equivalence.
(Compare `lintegral_map`, which applies to a broader class of transformations of the domain, but
requires measurability of the function being integrated.) -/
lemma lintegral_map_equiv {Ξ²} [measurable_space Ξ²] (g : Ξ² ββ ββ₯0β) (m : Ξ± βα΅ Ξ²) :
(g.comp m m.measurable).lintegral ΞΌ = g.lintegral (measure.map m ΞΌ) :=
begin
simp [simple_func.lintegral],
have : (g.comp m m.measurable).range = g.range,
{ refine le_antisymm _ _,
{ exact g.range_comp_subset_range m.measurable },
convert (g.comp m m.measurable).range_comp_subset_range m.symm.measurable,
apply simple_func.ext,
intros a,
exact congr_arg g (congr_fun m.self_comp_symm.symm a) },
rw this,
congr' 1,
funext,
rw [m.map_apply (g β»ΒΉ' {x})],
refl,
end
end measure
section fin_meas_supp
variables [measurable_space Ξ±] [has_zero Ξ²] [has_zero Ξ³] {ΞΌ : measure Ξ±}
open finset function
lemma support_eq (f : Ξ± ββ Ξ²) : support f = β y β f.range.filter (Ξ» y, y β 0), f β»ΒΉ' {y} :=
set.ext $ Ξ» x, by simp only [finset.set_bUnion_preimage_singleton, mem_support, set.mem_preimage,
finset.mem_coe, mem_filter, mem_range_self, true_and]
/-- A `simple_func` has finite measure support if it is equal to `0` outside of a set of finite
measure. -/
protected def fin_meas_supp (f : Ξ± ββ Ξ²) (ΞΌ : measure Ξ±) : Prop :=
f =αΆ [ΞΌ.cofinite] 0
lemma fin_meas_supp_iff_support {f : Ξ± ββ Ξ²} {ΞΌ : measure Ξ±} :
f.fin_meas_supp ΞΌ β ΞΌ (support f) < β :=
iff.rfl
lemma fin_meas_supp_iff {f : Ξ± ββ Ξ²} {ΞΌ : measure Ξ±} :
f.fin_meas_supp ΞΌ β β y β 0, ΞΌ (f β»ΒΉ' {y}) < β :=
begin
split,
{ refine Ξ» h y hy, lt_of_le_of_lt (measure_mono _) h,
exact Ξ» x hx (H : f x = 0), hy $ H βΈ eq.symm hx },
{ intro H,
rw [fin_meas_supp_iff_support, support_eq],
refine lt_of_le_of_lt (measure_bUnion_finset_le _ _) (sum_lt_top _),
exact Ξ» y hy, H y (finset.mem_filter.1 hy).2 }
end
namespace fin_meas_supp
lemma meas_preimage_singleton_ne_zero {f : Ξ± ββ Ξ²} (h : f.fin_meas_supp ΞΌ) {y : Ξ²} (hy : y β 0) :
ΞΌ (f β»ΒΉ' {y}) < β :=
fin_meas_supp_iff.1 h y hy
protected lemma map {f : Ξ± ββ Ξ²} {g : Ξ² β Ξ³} (hf : f.fin_meas_supp ΞΌ) (hg : g 0 = 0) :
(f.map g).fin_meas_supp ΞΌ :=
flip lt_of_le_of_lt hf (measure_mono $ support_comp_subset hg f)
lemma of_map {f : Ξ± ββ Ξ²} {g : Ξ² β Ξ³} (h : (f.map g).fin_meas_supp ΞΌ) (hg : βb, g b = 0 β b = 0) :
f.fin_meas_supp ΞΌ :=
flip lt_of_le_of_lt h $ measure_mono $ support_subset_comp hg _
lemma map_iff {f : Ξ± ββ Ξ²} {g : Ξ² β Ξ³} (hg : β {b}, g b = 0 β b = 0) :
(f.map g).fin_meas_supp ΞΌ β f.fin_meas_supp ΞΌ :=
β¨Ξ» h, h.of_map $ Ξ» b, hg.1, Ξ» h, h.map $ hg.2 rflβ©
protected lemma pair {f : Ξ± ββ Ξ²} {g : Ξ± ββ Ξ³} (hf : f.fin_meas_supp ΞΌ) (hg : g.fin_meas_supp ΞΌ) :
(pair f g).fin_meas_supp ΞΌ :=
calc ΞΌ (support $ pair f g) = ΞΌ (support f βͺ support g) : congr_arg ΞΌ $ support_prod_mk f g
... β€ ΞΌ (support f) + ΞΌ (support g) : measure_union_le _ _
... < _ : add_lt_top.2 β¨hf, hgβ©
protected lemma mapβ [has_zero Ξ΄] {ΞΌ : measure Ξ±} {f : Ξ± ββ Ξ²} (hf : f.fin_meas_supp ΞΌ)
{g : Ξ± ββ Ξ³} (hg : g.fin_meas_supp ΞΌ) {op : Ξ² β Ξ³ β Ξ΄} (H : op 0 0 = 0) :
((pair f g).map (function.uncurry op)).fin_meas_supp ΞΌ :=
(hf.pair hg).map H
protected lemma add {Ξ²} [add_monoid Ξ²] {f g : Ξ± ββ Ξ²} (hf : f.fin_meas_supp ΞΌ)
(hg : g.fin_meas_supp ΞΌ) :
(f + g).fin_meas_supp ΞΌ :=
by { rw [add_eq_mapβ], exact hf.mapβ hg (zero_add 0) }
protected lemma mul {Ξ²} [monoid_with_zero Ξ²] {f g : Ξ± ββ Ξ²} (hf : f.fin_meas_supp ΞΌ)
(hg : g.fin_meas_supp ΞΌ) :
(f * g).fin_meas_supp ΞΌ :=
by { rw [mul_eq_mapβ], exact hf.mapβ hg (zero_mul 0) }
lemma lintegral_lt_top {f : Ξ± ββ ββ₯0β} (hm : f.fin_meas_supp ΞΌ) (hf : βα΅ a βΞΌ, f a < β) :
f.lintegral ΞΌ < β :=
begin
refine sum_lt_top (Ξ» a ha, _),
rcases eq_or_lt_of_le (le_top : a β€ β) with rfl|ha,
{ simp only [ae_iff, lt_top_iff_ne_top, ne.def, not_not] at hf,
simp [set.preimage, hf] },
{ by_cases ha0 : a = 0,
{ subst a, rwa [zero_mul] },
{ exact mul_lt_top ha (fin_meas_supp_iff.1 hm _ ha0) } }
end
lemma of_lintegral_lt_top {f : Ξ± ββ ββ₯0β} (h : f.lintegral ΞΌ < β) : f.fin_meas_supp ΞΌ :=
begin
refine fin_meas_supp_iff.2 (Ξ» b hb, _),
rw [lintegral, sum_lt_top_iff] at h,
by_cases b_mem : b β f.range,
{ rw ennreal.lt_top_iff_ne_top,
have h : Β¬ _ = β := ennreal.lt_top_iff_ne_top.1 (h b b_mem),
simp only [mul_eq_top, not_or_distrib, not_and_distrib] at h,
rcases h with β¨h, h'β©,
refine or.elim h (Ξ»h, by contradiction) (Ξ»h, h) },
{ rw β preimage_eq_empty_iff at b_mem,
rw [b_mem, measure_empty],
exact with_top.zero_lt_top }
end
lemma iff_lintegral_lt_top {f : Ξ± ββ ββ₯0β} (hf : βα΅ a βΞΌ, f a < β) :
f.fin_meas_supp ΞΌ β f.lintegral ΞΌ < β :=
β¨Ξ» h, h.lintegral_lt_top hf, Ξ» h, of_lintegral_lt_top hβ©
end fin_meas_supp
end fin_meas_supp
/-- To prove something for an arbitrary simple function, it suffices to show
that the property holds for (multiples of) characteristic functions and is closed under
addition (of functions with disjoint support).
It is possible to make the hypotheses in `h_add` a bit stronger, and such conditions can be added
once we need them (for example it is only necessary to consider the case where `g` is a multiple
of a characteristic function, and that this multiple doesn't appear in the image of `f`) -/
@[elab_as_eliminator]
protected lemma induction {Ξ± Ξ³} [measurable_space Ξ±] [add_monoid Ξ³] {P : simple_func Ξ± Ξ³ β Prop}
(h_ind : β c {s} (hs : measurable_set s),
P (simple_func.piecewise s hs (simple_func.const _ c) (simple_func.const _ 0)))
(h_add : β β¦f g : simple_func Ξ± Ξ³β¦, disjoint (support f) (support g) β P f β P g β P (f + g))
(f : simple_func Ξ± Ξ³) : P f :=
begin
generalize' h : f.range \ {0} = s,
rw [β finset.coe_inj, finset.coe_sdiff, finset.coe_singleton, simple_func.coe_range] at h,
revert s f h, refine finset.induction _ _,
{ intros f hf, rw [finset.coe_empty, diff_eq_empty, range_subset_singleton] at hf,
convert h_ind 0 measurable_set.univ, ext x, simp [hf] },
{ intros x s hxs ih f hf,
have mx := f.measurable_set_preimage {x},
let g := simple_func.piecewise (f β»ΒΉ' {x}) mx 0 f,
have Pg : P g,
{ apply ih, simp only [g, simple_func.coe_piecewise, range_piecewise],
rw [image_compl_preimage, union_diff_distrib, diff_diff_comm, hf, finset.coe_insert,
insert_diff_self_of_not_mem, diff_eq_empty.mpr, set.empty_union],
{ rw [set.image_subset_iff], convert set.subset_univ _,
exact preimage_const_of_mem (mem_singleton _) },
{ rwa [finset.mem_coe] }},
convert h_add _ Pg (h_ind x mx),
{ ext1 y, by_cases hy : y β f β»ΒΉ' {x}; [simpa [hy], simp [hy]] },
rintro y, by_cases hy : y β f β»ΒΉ' {x}; simp [hy] }
end
end simple_func
section lintegral
open simple_func
variables [measurable_space Ξ±] {ΞΌ : measure Ξ±}
/-- The lower Lebesgue integral of a function `f` with respect to a measure `ΞΌ`. -/
def lintegral (ΞΌ : measure Ξ±) (f : Ξ± β ββ₯0β) : ββ₯0β :=
β¨ (g : Ξ± ββ ββ₯0β) (hf : βg β€ f), g.lintegral ΞΌ
/-! In the notation for integrals, an expression like `β«β» x, g β₯xβ₯ βΞΌ` will not be parsed correctly,
and needs parentheses. We do not set the binding power of `r` to `0`, because then
`β«β» x, f x = 0` will be parsed incorrectly. -/
notation `β«β»` binders `, ` r:(scoped:60 f, f) ` β` ΞΌ:70 := lintegral ΞΌ r
notation `β«β»` binders `, ` r:(scoped:60 f, lintegral volume f) := r
notation `β«β»` binders ` in ` s `, ` r:(scoped:60 f, f) ` β` ΞΌ:70 :=
lintegral (measure.restrict ΞΌ s) r
notation `β«β»` binders ` in ` s `, ` r:(scoped:60 f, lintegral (measure.restrict volume s) f) := r
theorem simple_func.lintegral_eq_lintegral (f : Ξ± ββ ββ₯0β) (ΞΌ : measure Ξ±) :
β«β» a, f a β ΞΌ = f.lintegral ΞΌ :=
le_antisymm
(bsupr_le $ Ξ» g hg, lintegral_mono hg $ le_refl _)
(le_supr_of_le f $ le_supr_of_le (le_refl _) (le_refl _))
@[mono] lemma lintegral_mono' β¦ΞΌ Ξ½ : measure Ξ±β¦ (hΞΌΞ½ : ΞΌ β€ Ξ½) β¦f g : Ξ± β ββ₯0ββ¦ (hfg : f β€ g) :
β«β» a, f a βΞΌ β€ β«β» a, g a βΞ½ :=
supr_le_supr $ Ξ» Ο, supr_le_supr2 $ Ξ» hΟ, β¨le_trans hΟ hfg, lintegral_mono (le_refl Ο) hΞΌΞ½β©
lemma lintegral_mono β¦f g : Ξ± β ββ₯0ββ¦ (hfg : f β€ g) :
β«β» a, f a βΞΌ β€ β«β» a, g a βΞΌ :=
lintegral_mono' (le_refl ΞΌ) hfg
lemma lintegral_mono_nnreal {f g : Ξ± β ββ₯0} (h : f β€ g) :
β«β» a, f a βΞΌ β€ β«β» a, g a βΞΌ :=
begin
refine lintegral_mono _,
intro a,
rw ennreal.coe_le_coe,
exact h a,
end
lemma monotone_lintegral (ΞΌ : measure Ξ±) : monotone (lintegral ΞΌ) :=
lintegral_mono
@[simp] lemma lintegral_const (c : ββ₯0β) : β«β» a, c βΞΌ = c * ΞΌ univ :=
by rw [β simple_func.const_lintegral, β simple_func.lintegral_eq_lintegral, simple_func.coe_const]
@[simp] lemma lintegral_one : β«β» a, (1 : ββ₯0β) βΞΌ = ΞΌ univ :=
by rw [lintegral_const, one_mul]
lemma set_lintegral_const (s : set Ξ±) (c : ββ₯0β) : β«β» a in s, c βΞΌ = c * ΞΌ s :=
by rw [lintegral_const, measure.restrict_apply_univ]
lemma set_lintegral_one (s) : β«β» a in s, 1 βΞΌ = ΞΌ s :=
by rw [set_lintegral_const, one_mul]
/-- `β«β» a in s, f a βΞΌ` is defined as the supremum of integrals of simple functions
`Ο : Ξ± ββ ββ₯0β` such that `Ο β€ f`. This lemma says that it suffices to take
functions `Ο : Ξ± ββ ββ₯0`. -/
lemma lintegral_eq_nnreal (f : Ξ± β ββ₯0β) (ΞΌ : measure Ξ±) :
(β«β» a, f a βΞΌ) = (β¨ (Ο : Ξ± ββ ββ₯0) (hf : β x, β(Ο x) β€ f x),
(Ο.map (coe : ββ₯0 β ββ₯0β)).lintegral ΞΌ) :=
begin
refine le_antisymm
(bsupr_le $ assume Ο hΟ, _)
(supr_le_supr2 $ Ξ» Ο, β¨Ο.map (coe : ββ₯0 β ββ₯0β), le_refl _β©),
by_cases h : βα΅ a βΞΌ, Ο a β β,
{ let Ο := Ο.map ennreal.to_nnreal,
replace h : Ο.map (coe : ββ₯0 β ββ₯0β) =α΅[ΞΌ] Ο :=
h.mono (Ξ» a, ennreal.coe_to_nnreal),
have : β x, β(Ο x) β€ f x := Ξ» x, le_trans ennreal.coe_to_nnreal_le_self (hΟ x),
exact le_supr_of_le (Ο.map ennreal.to_nnreal)
(le_supr_of_le this (ge_of_eq $ lintegral_congr h)) },
{ have h_meas : ΞΌ (Ο β»ΒΉ' {β}) β 0, from mt measure_zero_iff_ae_nmem.1 h,
refine le_trans le_top (ge_of_eq $ (supr_eq_top _).2 $ Ξ» b hb, _),
obtain β¨n, hnβ© : β n : β, b < n * ΞΌ (Ο β»ΒΉ' {β}), from exists_nat_mul_gt h_meas (ne_of_lt hb),
use (const Ξ± (n : ββ₯0)).restrict (Ο β»ΒΉ' {β}),
simp only [lt_supr_iff, exists_prop, coe_restrict, Ο.measurable_set_preimage, coe_const,
ennreal.coe_indicator, map_coe_ennreal_restrict, map_const, ennreal.coe_nat,
restrict_const_lintegral],
refine β¨indicator_le (Ξ» x hx, le_trans _ (hΟ _)), hnβ©,
simp only [mem_preimage, mem_singleton_iff] at hx,
simp only [hx, le_top] }
end
lemma exists_simple_func_forall_lintegral_sub_lt_of_pos {f : Ξ± β ββ₯0β} (h : β«β» x, f x βΞΌ < β)
{Ξ΅ : ββ₯0β} (hΞ΅ : 0 < Ξ΅) :
β Ο : Ξ± ββ ββ₯0, (β x, β(Ο x) β€ f x) β§ β Ο : Ξ± ββ ββ₯0, (β x, β(Ο x) β€ f x) β
(map coe (Ο - Ο)).lintegral ΞΌ < Ξ΅ :=
begin
rw lintegral_eq_nnreal at h,
have := ennreal.lt_add_right h hΞ΅,
erw ennreal.bsupr_add at this; [skip, exact β¨0, Ξ» x, by simpβ©],
simp_rw [lt_supr_iff, supr_lt_iff, supr_le_iff] at this,
rcases this with β¨Ο, hle : β x, β(Ο x) β€ f x, b, hbΟ, hbβ©,
refine β¨Ο, hle, Ξ» Ο hΟ, _β©,
have : (map coe Ο).lintegral ΞΌ < β, from (le_bsupr Ο hle).trans_lt h,
rw [β add_lt_add_iff_left this, β add_lintegral, β map_add @ennreal.coe_add],
refine (hb _ (Ξ» x, le_trans _ (max_le (hle x) (hΟ x)))).trans_lt hbΟ,
norm_cast,
simp only [add_apply, sub_apply, nnreal.add_sub_eq_max]
end
theorem supr_lintegral_le {ΞΉ : Sort*} (f : ΞΉ β Ξ± β ββ₯0β) :
(β¨i, β«β» a, f i a βΞΌ) β€ (β«β» a, β¨i, f i a βΞΌ) :=
begin
simp only [β supr_apply],
exact (monotone_lintegral ΞΌ).le_map_supr
end
theorem supr2_lintegral_le {ΞΉ : Sort*} {ΞΉ' : ΞΉ β Sort*} (f : Ξ i, ΞΉ' i β Ξ± β ββ₯0β) :
(β¨i (h : ΞΉ' i), β«β» a, f i h a βΞΌ) β€ (β«β» a, β¨i (h : ΞΉ' i), f i h a βΞΌ) :=
by { convert (monotone_lintegral ΞΌ).le_map_supr2 f, ext1 a, simp only [supr_apply] }
theorem le_infi_lintegral {ΞΉ : Sort*} (f : ΞΉ β Ξ± β ββ₯0β) :
(β«β» a, β¨
i, f i a βΞΌ) β€ (β¨
i, β«β» a, f i a βΞΌ) :=
by { simp only [β infi_apply], exact (monotone_lintegral ΞΌ).map_infi_le }
theorem le_infi2_lintegral {ΞΉ : Sort*} {ΞΉ' : ΞΉ β Sort*} (f : Ξ i, ΞΉ' i β Ξ± β ββ₯0β) :
(β«β» a, β¨
i (h : ΞΉ' i), f i h a βΞΌ) β€ (β¨
i (h : ΞΉ' i), β«β» a, f i h a βΞΌ) :=
by { convert (monotone_lintegral ΞΌ).map_infi2_le f, ext1 a, simp only [infi_apply] }
lemma lintegral_mono_ae {f g : Ξ± β ββ₯0β} (h : βα΅ a βΞΌ, f a β€ g a) :
(β«β» a, f a βΞΌ) β€ (β«β» a, g a βΞΌ) :=
begin
rcases exists_measurable_superset_of_null h with β¨t, hts, ht, ht0β©,
have : βα΅ x βΞΌ, x β t := measure_zero_iff_ae_nmem.1 ht0,
refine (supr_le $ assume s, supr_le $ assume hfs,
le_supr_of_le (s.restrict tαΆ) $ le_supr_of_le _ _),
{ assume a,
by_cases a β t;
simp [h, restrict_apply, ht.compl],
exact le_trans (hfs a) (by_contradiction $ assume hnfg, h (hts hnfg)) },
{ refine le_of_eq (simple_func.lintegral_congr $ this.mono $ Ξ» a hnt, _),
by_cases hat : a β t; simp [hat, ht.compl],
exact (hnt hat).elim }
end
lemma lintegral_congr_ae {f g : Ξ± β ββ₯0β} (h : f =α΅[ΞΌ] g) :
(β«β» a, f a βΞΌ) = (β«β» a, g a βΞΌ) :=
le_antisymm (lintegral_mono_ae $ h.le) (lintegral_mono_ae $ h.symm.le)
lemma lintegral_congr {f g : Ξ± β ββ₯0β} (h : β a, f a = g a) :
(β«β» a, f a βΞΌ) = (β«β» a, g a βΞΌ) :=
by simp only [h]
lemma set_lintegral_congr {f : Ξ± β ββ₯0β} {s t : set Ξ±} (h : s =α΅[ΞΌ] t) :
β«β» x in s, f x βΞΌ = β«β» x in t, f x βΞΌ :=
by rw [restrict_congr_set h]
/-- Monotone convergence theorem -- sometimes called Beppo-Levi convergence.
See `lintegral_supr_directed` for a more general form. -/
theorem lintegral_supr
{f : β β Ξ± β ββ₯0β} (hf : βn, measurable (f n)) (h_mono : monotone f) :
(β«β» a, β¨n, f n a βΞΌ) = (β¨n, β«β» a, f n a βΞΌ) :=
begin
set c : ββ₯0 β ββ₯0β := coe,
set F := Ξ» a:Ξ±, β¨n, f n a,
have hF : measurable F := measurable_supr hf,
refine le_antisymm _ (supr_lintegral_le _),
rw [lintegral_eq_nnreal],
refine supr_le (assume s, supr_le (assume hsf, _)),
refine ennreal.le_of_forall_lt_one_mul_le (assume a ha, _),
rcases ennreal.lt_iff_exists_coe.1 ha with β¨r, rfl, haβ©,
have ha : r < 1 := ennreal.coe_lt_coe.1 ha,
let rs := s.map (Ξ»a, r * a),
have eq_rs : (const Ξ± r : Ξ± ββ ββ₯0β) * map c s = rs.map c,
{ ext1 a, exact ennreal.coe_mul.symm },
have eq : βp, (rs.map c) β»ΒΉ' {p} = (βn, (rs.map c) β»ΒΉ' {p} β© {a | p β€ f n a}),
{ assume p,
rw [β inter_Union, β inter_univ ((map c rs) β»ΒΉ' {p})] {occs := occurrences.pos [1]},
refine set.ext (assume x, and_congr_right $ assume hx, (true_iff _).2 _),
by_cases p_eq : p = 0, { simp [p_eq] },
simp at hx, subst hx,
have : r * s x β 0, { rwa [(β ), β ennreal.coe_eq_zero] },
have : s x β 0, { refine mt _ this, assume h, rw [h, mul_zero] },
have : (rs.map c) x < β¨ (n : β), f n x,
{ refine lt_of_lt_of_le (ennreal.coe_lt_coe.2 (_)) (hsf x),
suffices : r * s x < 1 * s x, simpa [rs],
exact mul_lt_mul_of_pos_right ha (pos_iff_ne_zero.2 this) },
rcases lt_supr_iff.1 this with β¨i, hiβ©,
exact mem_Union.2 β¨i, le_of_lt hiβ© },
have mono : βr:ββ₯0β, monotone (Ξ»n, (rs.map c) β»ΒΉ' {r} β© {a | r β€ f n a}),
{ assume r i j h,
refine inter_subset_inter (subset.refl _) _,
assume x hx, exact le_trans hx (h_mono h x) },
have h_meas : βn, measurable_set {a : Ξ± | β(map c rs) a β€ f n a} :=
assume n, measurable_set_le (simple_func.measurable _) (hf n),
calc (r:ββ₯0β) * (s.map c).lintegral ΞΌ = β r in (rs.map c).range, r * ΞΌ ((rs.map c) β»ΒΉ' {r}) :
by rw [β const_mul_lintegral, eq_rs, simple_func.lintegral]
... β€ β r in (rs.map c).range, r * ΞΌ (βn, (rs.map c) β»ΒΉ' {r} β© {a | r β€ f n a}) :
le_of_eq (finset.sum_congr rfl $ assume x hx, by rw β eq)
... β€ β r in (rs.map c).range, (β¨n, r * ΞΌ ((rs.map c) β»ΒΉ' {r} β© {a | r β€ f n a})) :
le_of_eq (finset.sum_congr rfl $ assume x hx,
begin
rw [measure_Union_eq_supr _ (directed_of_sup $ mono x), ennreal.mul_supr],
{ assume i,
refine ((rs.map c).measurable_set_preimage _).inter _,
exact hf i measurable_set_Ici }
end)
... β€ β¨n, β r in (rs.map c).range, r * ΞΌ ((rs.map c) β»ΒΉ' {r} β© {a | r β€ f n a}) :
begin
refine le_of_eq _,
rw [ennreal.finset_sum_supr_nat],
assume p i j h,
exact canonically_ordered_semiring.mul_le_mul (le_refl _) (measure_mono $ mono p h)
end
... β€ (β¨n:β, ((rs.map c).restrict {a | (rs.map c) a β€ f n a}).lintegral ΞΌ) :
begin
refine supr_le_supr (assume n, _),
rw [restrict_lintegral _ (h_meas n)],
{ refine le_of_eq (finset.sum_congr rfl $ assume r hr, _),
congr' 2 with a,
refine and_congr_right _,
simp {contextual := tt} }
end
... β€ (β¨n, β«β» a, f n a βΞΌ) :
begin
refine supr_le_supr (assume n, _),
rw [β simple_func.lintegral_eq_lintegral],
refine lintegral_mono (assume a, _),
simp only [map_apply] at h_meas,
simp only [coe_map, restrict_apply _ (h_meas _), (β)],
exact indicator_apply_le id,
end
end
/-- Monotone convergence theorem -- sometimes called Beppo-Levi convergence. Version with
ae_measurable functions. -/
theorem lintegral_supr' {f : β β Ξ± β ββ₯0β} (hf : βn, ae_measurable (f n) ΞΌ)
(h_mono : βα΅ x βΞΌ, monotone (Ξ» n, f n x)) :
(β«β» a, β¨n, f n a βΞΌ) = (β¨n, β«β» a, f n a βΞΌ) :=
begin
simp_rw βsupr_apply,
let p : Ξ± β (β β ββ₯0β) β Prop := Ξ» x f', monotone f',
have hp : βα΅ x βΞΌ, p x (Ξ» i, f i x), from h_mono,
have h_ae_seq_mono : monotone (ae_seq hf p),
{ intros n m hnm x,
by_cases hx : x β ae_seq_set hf p,
{ exact ae_seq.prop_of_mem_ae_seq_set hf hx hnm, },
{ simp only [ae_seq, hx, if_false],
exact le_refl _, }, },
rw lintegral_congr_ae (ae_seq.supr hf hp).symm,
simp_rw supr_apply,
rw @lintegral_supr _ _ ΞΌ _ (ae_seq.measurable hf p) h_ae_seq_mono,
congr,
exact funext (Ξ» n, lintegral_congr_ae (ae_seq.ae_seq_n_eq_fun_n_ae hf hp n)),
end
lemma lintegral_eq_supr_eapprox_lintegral {f : Ξ± β ββ₯0β} (hf : measurable f) :
(β«β» a, f a βΞΌ) = (β¨n, (eapprox f n).lintegral ΞΌ) :=
calc (β«β» a, f a βΞΌ) = (β«β» a, β¨n, (eapprox f n : Ξ± β ββ₯0β) a βΞΌ) :
by congr; ext a; rw [supr_eapprox_apply f hf]
... = (β¨n, β«β» a, (eapprox f n : Ξ± β ββ₯0β) a βΞΌ) :
begin
rw [lintegral_supr],
{ assume n, exact (eapprox f n).measurable },
{ assume i j h, exact (monotone_eapprox f h) }
end
... = (β¨n, (eapprox f n).lintegral ΞΌ) : by congr; ext n; rw [(eapprox f n).lintegral_eq_lintegral]
/-- If `f` has finite integral, then `β«β» x in s, f x βΞΌ` is absolutely continuous in `s`: it tends
to zero as `ΞΌ s` tends to zero. This lemma states states this fact in terms of `Ξ΅` and `Ξ΄`. -/
lemma exists_pos_set_lintegral_lt_of_measure_lt {f : Ξ± β ββ₯0β} (h : β«β» x, f x βΞΌ < β)
{Ξ΅ : ββ₯0β} (hΞ΅ : 0 < Ξ΅) :
β Ξ΄ > 0, β s, ΞΌ s < Ξ΄ β β«β» x in s, f x βΞΌ < Ξ΅ :=
begin
rcases exists_between hΞ΅ with β¨Ξ΅β, hΞ΅β0, hΞ΅βΞ΅β©, rcases exists_between hΞ΅β0 with β¨Ξ΅β, hΞ΅β0, hΞ΅βββ©,
rcases exists_simple_func_forall_lintegral_sub_lt_of_pos h hΞ΅β0 with β¨Ο, hle, hΟβ©,
rcases Ο.exists_forall_le with β¨C, hCβ©,
use [(Ξ΅β - Ξ΅β) / C, ennreal.div_pos_iff.2 β¨(zero_lt_sub_iff_lt.2 hΞ΅ββ).ne', ennreal.coe_ne_topβ©],
intros s hs,
simp only [lintegral_eq_nnreal, supr_lt_iff, supr_le_iff],
refine β¨Ξ΅β, hΞ΅βΞ΅, Ξ» Ο hΟ, _β©,
calc (map coe Ο).lintegral (ΞΌ.restrict s)
β€ (map coe Ο).lintegral (ΞΌ.restrict s) + (map coe (Ο - Ο)).lintegral (ΞΌ.restrict s) :
begin
rw [β simple_func.add_lintegral, β simple_func.map_add @ennreal.coe_add],
refine simple_func.lintegral_mono (Ξ» x, _) le_rfl,
simp [-ennreal.coe_add, nnreal.add_sub_eq_max, le_max_right]
end
... β€ (map coe Ο).lintegral (ΞΌ.restrict s) + Ξ΅β :
begin
refine add_le_add le_rfl (le_trans _ (hΟ _ hΟ).le),
exact simple_func.lintegral_mono le_rfl measure.restrict_le_self
end
... β€ (simple_func.const Ξ± (C : ββ₯0β)).lintegral (ΞΌ.restrict s) + Ξ΅β :
by { mono*, exacts [Ξ» x, coe_le_coe.2 (hC x), le_rfl, le_rfl] }
... = C * ΞΌ s + Ξ΅β : by simp [β simple_func.lintegral_eq_lintegral]
... β€ C * ((Ξ΅β - Ξ΅β) / C) + Ξ΅β : by { mono*, exacts [le_rfl, hs.le, le_rfl] }
... β€ (Ξ΅β - Ξ΅β) + Ξ΅β : add_le_add mul_div_le le_rfl
... = Ξ΅β : sub_add_cancel_of_le hΞ΅ββ.le,
end
/-- If `f` has finite integral, then `β«β» x in s, f x βΞΌ` is absolutely continuous in `s`: it tends
to zero as `ΞΌ s` tends to zero. -/
lemma tendsto_set_lintegral_zero {ΞΉ} {f : Ξ± β ββ₯0β} (h : β«β» x, f x βΞΌ < β)
{l : filter ΞΉ} {s : ΞΉ β set Ξ±} (hl : tendsto (ΞΌ β s) l (π 0)) :
tendsto (Ξ» i, β«β» x in s i, f x βΞΌ) l (π 0) :=
begin
simp only [ennreal.nhds_zero, tendsto_infi, tendsto_principal, mem_Iio, β pos_iff_ne_zero]
at hl β’,
intros Ξ΅ Ξ΅0,
rcases exists_pos_set_lintegral_lt_of_measure_lt h Ξ΅0 with β¨Ξ΄, Ξ΄0, hΞ΄β©,
exact (hl Ξ΄ Ξ΄0).mono (Ξ» i, hΞ΄ _)
end
@[simp] lemma lintegral_add {f g : Ξ± β ββ₯0β} (hf : measurable f) (hg : measurable g) :
(β«β» a, f a + g a βΞΌ) = (β«β» a, f a βΞΌ) + (β«β» a, g a βΞΌ) :=
calc (β«β» a, f a + g a βΞΌ) =
(β«β» a, (β¨n, (eapprox f n : Ξ± β ββ₯0β) a) + (β¨n, (eapprox g n : Ξ± β ββ₯0β) a) βΞΌ) :
by simp only [supr_eapprox_apply, hf, hg]
... = (β«β» a, (β¨n, (eapprox f n + eapprox g n : Ξ± β ββ₯0β) a) βΞΌ) :
begin
congr, funext a,
rw [ennreal.supr_add_supr_of_monotone], { refl },
{ assume i j h, exact monotone_eapprox _ h a },
{ assume i j h, exact monotone_eapprox _ h a },
end
... = (β¨n, (eapprox f n).lintegral ΞΌ + (eapprox g n).lintegral ΞΌ) :
begin
rw [lintegral_supr],
{ congr,
funext n, rw [β simple_func.add_lintegral, β simple_func.lintegral_eq_lintegral],
refl },
{ assume n, exact measurable.add (eapprox f n).measurable (eapprox g n).measurable },
{ assume i j h a, exact add_le_add (monotone_eapprox _ h _) (monotone_eapprox _ h _) }
end
... = (β¨n, (eapprox f n).lintegral ΞΌ) + (β¨n, (eapprox g n).lintegral ΞΌ) :
by refine (ennreal.supr_add_supr_of_monotone _ _).symm;
{ assume i j h, exact simple_func.lintegral_mono (monotone_eapprox _ h) (le_refl ΞΌ) }
... = (β«β» a, f a βΞΌ) + (β«β» a, g a βΞΌ) :
by rw [lintegral_eq_supr_eapprox_lintegral hf, lintegral_eq_supr_eapprox_lintegral hg]
lemma lintegral_add' {f g : Ξ± β ββ₯0β} (hf : ae_measurable f ΞΌ) (hg : ae_measurable g ΞΌ) :
(β«β» a, f a + g a βΞΌ) = (β«β» a, f a βΞΌ) + (β«β» a, g a βΞΌ) :=
calc (β«β» a, f a + g a βΞΌ) = (β«β» a, hf.mk f a + hg.mk g a βΞΌ) :
lintegral_congr_ae (eventually_eq.add hf.ae_eq_mk hg.ae_eq_mk)
... = (β«β» a, hf.mk f a βΞΌ) + (β«β» a, hg.mk g a βΞΌ) : lintegral_add hf.measurable_mk hg.measurable_mk
... = (β«β» a, f a βΞΌ) + (β«β» a, g a βΞΌ) : begin
congr' 1,
{ exact lintegral_congr_ae hf.ae_eq_mk.symm },
{ exact lintegral_congr_ae hg.ae_eq_mk.symm },
end
lemma lintegral_zero : (β«β» a:Ξ±, 0 βΞΌ) = 0 := by simp
lemma lintegral_zero_fun : (β«β» a:Ξ±, (0 : Ξ± β ββ₯0β) a βΞΌ) = 0 := by simp
@[simp] lemma lintegral_smul_measure (c : ββ₯0β) (f : Ξ± β ββ₯0β) :
β«β» a, f a β (c β’ ΞΌ) = c * β«β» a, f a βΞΌ :=
by simp only [lintegral, supr_subtype', simple_func.lintegral_smul, ennreal.mul_supr, smul_eq_mul]
@[simp] lemma lintegral_sum_measure {ΞΉ} (f : Ξ± β ββ₯0β) (ΞΌ : ΞΉ β measure Ξ±) :
β«β» a, f a β(measure.sum ΞΌ) = β' i, β«β» a, f a β(ΞΌ i) :=
begin
simp only [lintegral, supr_subtype', simple_func.lintegral_sum, ennreal.tsum_eq_supr_sum],
rw [supr_comm],
congr, funext s,
induction s using finset.induction_on with i s hi hs, { apply bot_unique, simp },
simp only [finset.sum_insert hi, β hs],
refine (ennreal.supr_add_supr _).symm,
intros Ο Ο,
exact β¨β¨Ο β Ο, Ξ» x, sup_le (Ο.2 x) (Ο.2 x)β©,
add_le_add (simple_func.lintegral_mono le_sup_left (le_refl _))
(finset.sum_le_sum $ Ξ» j hj, simple_func.lintegral_mono le_sup_right (le_refl _))β©
end
@[simp] lemma lintegral_add_measure (f : Ξ± β ββ₯0β) (ΞΌ Ξ½ : measure Ξ±) :
β«β» a, f a β (ΞΌ + Ξ½) = β«β» a, f a βΞΌ + β«β» a, f a βΞ½ :=
by simpa [tsum_fintype] using lintegral_sum_measure f (Ξ» b, cond b ΞΌ Ξ½)
@[simp] lemma lintegral_zero_measure (f : Ξ± β ββ₯0β) : β«β» a, f a β0 = 0 :=
bot_unique $ by simp [lintegral]
lemma lintegral_finset_sum (s : finset Ξ²) {f : Ξ² β Ξ± β ββ₯0β} (hf : β b β s, measurable (f b)) :
(β«β» a, β b in s, f b a βΞΌ) = β b in s, β«β» a, f b a βΞΌ :=
begin
induction s using finset.induction_on with a s has ih,
{ simp },
{ simp only [finset.sum_insert has],
rw [finset.forall_mem_insert] at hf,
rw [lintegral_add hf.1 (s.measurable_sum hf.2), ih hf.2] }
end
@[simp] lemma lintegral_const_mul (r : ββ₯0β) {f : Ξ± β ββ₯0β} (hf : measurable f) :
(β«β» a, r * f a βΞΌ) = r * (β«β» a, f a βΞΌ) :=
calc (β«β» a, r * f a βΞΌ) = (β«β» a, (β¨n, (const Ξ± r * eapprox f n) a) βΞΌ) :
by { congr, funext a, rw [β supr_eapprox_apply f hf, ennreal.mul_supr], refl }
... = (β¨n, r * (eapprox f n).lintegral ΞΌ) :
begin
rw [lintegral_supr],
{ congr, funext n,
rw [β simple_func.const_mul_lintegral, β simple_func.lintegral_eq_lintegral] },
{ assume n, exact simple_func.measurable _ },
{ assume i j h a, exact canonically_ordered_semiring.mul_le_mul (le_refl _)
(monotone_eapprox _ h _) }
end
... = r * (β«β» a, f a βΞΌ) : by rw [β ennreal.mul_supr, lintegral_eq_supr_eapprox_lintegral hf]
lemma lintegral_const_mul'' (r : ββ₯0β) {f : Ξ± β ββ₯0β} (hf : ae_measurable f ΞΌ) :
(β«β» a, r * f a βΞΌ) = r * (β«β» a, f a βΞΌ) :=
begin
have A : β«β» a, f a βΞΌ = β«β» a, hf.mk f a βΞΌ := lintegral_congr_ae hf.ae_eq_mk,
have B : β«β» a, r * f a βΞΌ = β«β» a, r * hf.mk f a βΞΌ :=
lintegral_congr_ae (eventually_eq.fun_comp hf.ae_eq_mk _),
rw [A, B, lintegral_const_mul _ hf.measurable_mk],
end
lemma lintegral_const_mul_le (r : ββ₯0β) (f : Ξ± β ββ₯0β) :
r * (β«β» a, f a βΞΌ) β€ (β«β» a, r * f a βΞΌ) :=
begin
rw [lintegral, ennreal.mul_supr],
refine supr_le (Ξ»s, _),
rw [ennreal.mul_supr],
simp only [supr_le_iff, ge_iff_le],
assume hs,
rw β simple_func.const_mul_lintegral,
refine le_supr_of_le (const Ξ± r * s) (le_supr_of_le (Ξ»x, _) (le_refl _)),
exact canonically_ordered_semiring.mul_le_mul (le_refl _) (hs x)
end
lemma lintegral_const_mul' (r : ββ₯0β) (f : Ξ± β ββ₯0β) (hr : r β β) :
(β«β» a, r * f a βΞΌ) = r * (β«β» a, f a βΞΌ) :=
begin
by_cases h : r = 0,
{ simp [h] },
apply le_antisymm _ (lintegral_const_mul_le r f),
have rinv : r * rβ»ΒΉ = 1 := ennreal.mul_inv_cancel h hr,
have rinv' : r β»ΒΉ * r = 1, by { rw mul_comm, exact rinv },
have := lintegral_const_mul_le (rβ»ΒΉ) (Ξ»x, r * f x),
simp [(mul_assoc _ _ _).symm, rinv'] at this,
simpa [(mul_assoc _ _ _).symm, rinv]
using canonically_ordered_semiring.mul_le_mul (le_refl r) this
end
lemma lintegral_mul_const (r : ββ₯0β) {f : Ξ± β ββ₯0β} (hf : measurable f) :
β«β» a, f a * r βΞΌ = β«β» a, f a βΞΌ * r :=
by simp_rw [mul_comm, lintegral_const_mul r hf]
lemma lintegral_mul_const'' (r : ββ₯0β) {f : Ξ± β ββ₯0β} (hf : ae_measurable f ΞΌ) :
β«β» a, f a * r βΞΌ = β«β» a, f a βΞΌ * r :=
by simp_rw [mul_comm, lintegral_const_mul'' r hf]
lemma lintegral_mul_const_le (r : ββ₯0β) (f : Ξ± β ββ₯0β) :
β«β» a, f a βΞΌ * r β€ β«β» a, f a * r βΞΌ :=
by simp_rw [mul_comm, lintegral_const_mul_le r f]
lemma lintegral_mul_const' (r : ββ₯0β) (f : Ξ± β ββ₯0β) (hr : r β β):
β«β» a, f a * r βΞΌ = β«β» a, f a βΞΌ * r :=
by simp_rw [mul_comm, lintegral_const_mul' r f hr]
/- A double integral of a product where each factor contains only one variable
is a product of integrals -/
lemma lintegral_lintegral_mul {Ξ²} [measurable_space Ξ²] {Ξ½ : measure Ξ²}
{f : Ξ± β ββ₯0β} {g : Ξ² β ββ₯0β} (hf : ae_measurable f ΞΌ) (hg : ae_measurable g Ξ½) :
β«β» x, β«β» y, f x * g y βΞ½ βΞΌ = β«β» x, f x βΞΌ * β«β» y, g y βΞ½ :=
by simp [lintegral_const_mul'' _ hg, lintegral_mul_const'' _ hf]
-- TODO: Need a better way of rewriting inside of a integral
lemma lintegral_rwβ {f f' : Ξ± β Ξ²} (h : f =α΅[ΞΌ] f') (g : Ξ² β ββ₯0β) :
(β«β» a, g (f a) βΞΌ) = (β«β» a, g (f' a) βΞΌ) :=
lintegral_congr_ae $ h.mono $ Ξ» a h, by rw h
-- TODO: Need a better way of rewriting inside of a integral
lemma lintegral_rwβ {fβ fβ' : Ξ± β Ξ²} {fβ fβ' : Ξ± β Ξ³} (hβ : fβ =α΅[ΞΌ] fβ')
(hβ : fβ =α΅[ΞΌ] fβ') (g : Ξ² β Ξ³ β ββ₯0β) :
(β«β» a, g (fβ a) (fβ a) βΞΌ) = (β«β» a, g (fβ' a) (fβ' a) βΞΌ) :=
lintegral_congr_ae $ hβ.mp $ hβ.mono $ Ξ» _ hβ hβ, by rw [hβ, hβ]
@[simp] lemma lintegral_indicator (f : Ξ± β ββ₯0β) {s : set Ξ±} (hs : measurable_set s) :
β«β» a, s.indicator f a βΞΌ = β«β» a in s, f a βΞΌ :=
begin
simp only [lintegral, β restrict_lintegral_eq_lintegral_restrict _ hs, supr_subtype'],
apply le_antisymm; refine supr_le_supr2 (subtype.forall.2 $ Ξ» Ο hΟ, _),
{ refine β¨β¨Ο, le_trans hΟ (indicator_le_self _ _)β©, _β©,
refine simple_func.lintegral_mono (Ξ» x, _) (le_refl _),
by_cases hx : x β s,
{ simp [hx, hs, le_refl] },
{ apply le_trans (hΟ x),
simp [hx, hs, le_refl] } },
{ refine β¨β¨Ο.restrict s, Ξ» x, _β©, le_refl _β©,
simp [hΟ x, hs, indicator_le_indicator] }
end
/-- Chebyshev's inequality -/
lemma mul_meas_ge_le_lintegral {f : Ξ± β ββ₯0β} (hf : measurable f) (Ξ΅ : ββ₯0β) :
Ξ΅ * ΞΌ {x | Ξ΅ β€ f x} β€ β«β» a, f a βΞΌ :=
begin
have : measurable_set {a : Ξ± | Ξ΅ β€ f a }, from hf measurable_set_Ici,
rw [β simple_func.restrict_const_lintegral _ this, β simple_func.lintegral_eq_lintegral],
refine lintegral_mono (Ξ» a, _),
simp only [restrict_apply _ this],
exact indicator_apply_le id
end
lemma meas_ge_le_lintegral_div {f : Ξ± β ββ₯0β} (hf : measurable f) {Ξ΅ : ββ₯0β}
(hΞ΅ : Ξ΅ β 0) (hΞ΅' : Ξ΅ β β) :
ΞΌ {x | Ξ΅ β€ f x} β€ (β«β» a, f a βΞΌ) / Ξ΅ :=
(ennreal.le_div_iff_mul_le (or.inl hΞ΅) (or.inl hΞ΅')).2 $
by { rw [mul_comm], exact mul_meas_ge_le_lintegral hf Ξ΅ }
@[simp] lemma lintegral_eq_zero_iff {f : Ξ± β ββ₯0β} (hf : measurable f) :
β«β» a, f a βΞΌ = 0 β (f =α΅[ΞΌ] 0) :=
begin
refine iff.intro (assume h, _) (assume h, _),
{ have : βn:β, βα΅ a βΞΌ, f a < nβ»ΒΉ,
{ assume n,
rw [ae_iff, β nonpos_iff_eq_zero, β @ennreal.zero_div nβ»ΒΉ,
ennreal.le_div_iff_mul_le, mul_comm],
simp only [not_lt],
-- TODO: why `rw β h` fails with "not an equality or an iff"?
exacts [h βΈ mul_meas_ge_le_lintegral hf nβ»ΒΉ,
or.inl (ennreal.inv_ne_zero.2 ennreal.coe_nat_ne_top),
or.inr ennreal.zero_ne_top] },
refine (ae_all_iff.2 this).mono (Ξ» a ha, _),
by_contradiction h,
rcases ennreal.exists_inv_nat_lt h with β¨n, hnβ©,
exact (lt_irrefl _ $ lt_trans hn $ ha n).elim },
{ calc β«β» a, f a βΞΌ = β«β» a, 0 βΞΌ : lintegral_congr_ae h
... = 0 : lintegral_zero }
end
@[simp] lemma lintegral_eq_zero_iff' {f : Ξ± β ββ₯0β} (hf : ae_measurable f ΞΌ) :
β«β» a, f a βΞΌ = 0 β (f =α΅[ΞΌ] 0) :=
begin
have : β«β» a, f a βΞΌ = β«β» a, hf.mk f a βΞΌ := lintegral_congr_ae hf.ae_eq_mk,
rw [this, lintegral_eq_zero_iff hf.measurable_mk],
exact β¨Ξ» H, hf.ae_eq_mk.trans H, Ξ» H, hf.ae_eq_mk.symm.trans Hβ©
end
lemma lintegral_pos_iff_support {f : Ξ± β ββ₯0β} (hf : measurable f) :
0 < β«β» a, f a βΞΌ β 0 < ΞΌ (function.support f) :=
by simp [pos_iff_ne_zero, hf, filter.eventually_eq, ae_iff, function.support]
/-- Weaker version of the monotone convergence theorem-/
lemma lintegral_supr_ae {f : β β Ξ± β ββ₯0β} (hf : βn, measurable (f n))
(h_mono : βn, βα΅ a βΞΌ, f n a β€ f n.succ a) :
(β«β» a, β¨n, f n a βΞΌ) = (β¨n, β«β» a, f n a βΞΌ) :=
let β¨s, hsβ© := exists_measurable_superset_of_null
(ae_iff.1 (ae_all_iff.2 h_mono)) in
let g := Ξ» n a, if a β s then 0 else f n a in
have g_eq_f : βα΅ a βΞΌ, βn, g n a = f n a,
from (measure_zero_iff_ae_nmem.1 hs.2.2).mono (assume a ha n, if_neg ha),
calc
β«β» a, β¨n, f n a βΞΌ = β«β» a, β¨n, g n a βΞΌ :
lintegral_congr_ae $ g_eq_f.mono $ Ξ» a ha, by simp only [ha]
... = β¨n, (β«β» a, g n a βΞΌ) :
lintegral_supr
(assume n, measurable_const.piecewise hs.2.1 (hf n))
(monotone_of_monotone_nat $ assume n a, classical.by_cases
(assume h : a β s, by simp [g, if_pos h])
(assume h : a β s,
begin
simp only [g, if_neg h], have := hs.1, rw subset_def at this, have := mt (this a) h,
simp only [not_not, mem_set_of_eq] at this, exact this n
end))
... = β¨n, (β«β» a, f n a βΞΌ) :
by simp only [lintegral_congr_ae (g_eq_f.mono $ Ξ» a ha, ha _)]
lemma lintegral_sub {f g : Ξ± β ββ₯0β} (hf : measurable f) (hg : measurable g)
(hg_fin : β«β» a, g a βΞΌ < β) (h_le : g β€α΅[ΞΌ] f) :
β«β» a, f a - g a βΞΌ = β«β» a, f a βΞΌ - β«β» a, g a βΞΌ :=
begin
rw [β ennreal.add_left_inj hg_fin,
ennreal.sub_add_cancel_of_le (lintegral_mono_ae h_le),
β lintegral_add (hf.sub hg) hg],
refine lintegral_congr_ae (h_le.mono $ Ξ» x hx, _),
exact ennreal.sub_add_cancel_of_le hx
end
/-- Monotone convergence theorem for nonincreasing sequences of functions -/
lemma lintegral_infi_ae
{f : β β Ξ± β ββ₯0β} (h_meas : βn, measurable (f n))
(h_mono : βn:β, f n.succ β€α΅[ΞΌ] f n) (h_fin : β«β» a, f 0 a βΞΌ < β) :
β«β» a, β¨
n, f n a βΞΌ = β¨
n, β«β» a, f n a βΞΌ :=
have fn_le_f0 : β«β» a, β¨
n, f n a βΞΌ β€ β«β» a, f 0 a βΞΌ, from
lintegral_mono (assume a, infi_le_of_le 0 (le_refl _)),
have fn_le_f0' : (β¨
n, β«β» a, f n a βΞΌ) β€ β«β» a, f 0 a βΞΌ, from infi_le_of_le 0 (le_refl _),
(ennreal.sub_right_inj h_fin fn_le_f0 fn_le_f0').1 $
show β«β» a, f 0 a βΞΌ - β«β» a, β¨
n, f n a βΞΌ = β«β» a, f 0 a βΞΌ - (β¨
n, β«β» a, f n a βΞΌ), from
calc
β«β» a, f 0 a βΞΌ - (β«β» a, β¨
n, f n a βΞΌ) = β«β» a, f 0 a - β¨
n, f n a βΞΌ:
(lintegral_sub (h_meas 0) (measurable_infi h_meas)
(calc
(β«β» a, β¨
n, f n a βΞΌ) β€ β«β» a, f 0 a βΞΌ : lintegral_mono (assume a, infi_le _ _)
... < β : h_fin )
(ae_of_all _ $ assume a, infi_le _ _)).symm
... = β«β» a, β¨n, f 0 a - f n a βΞΌ : congr rfl (funext (assume a, ennreal.sub_infi))
... = β¨n, β«β» a, f 0 a - f n a βΞΌ :
lintegral_supr_ae
(assume n, (h_meas 0).sub (h_meas n))
(assume n, (h_mono n).mono $ assume a ha, ennreal.sub_le_sub (le_refl _) ha)
... = β¨n, β«β» a, f 0 a βΞΌ - β«β» a, f n a βΞΌ :
have h_mono : βα΅ a βΞΌ, βn:β, f n.succ a β€ f n a := ae_all_iff.2 h_mono,
have h_mono : βn, βα΅ a βΞΌ, f n a β€ f 0 a := assume n, h_mono.mono $ assume a h,
begin
induction n with n ih,
{exact le_refl _}, {exact le_trans (h n) ih}
end,
congr rfl (funext $ assume n, lintegral_sub (h_meas _) (h_meas _)
(calc
β«β» a, f n a βΞΌ β€ β«β» a, f 0 a βΞΌ : lintegral_mono_ae $ h_mono n
... < β : h_fin)
(h_mono n))
... = β«β» a, f 0 a βΞΌ - β¨
n, β«β» a, f n a βΞΌ : ennreal.sub_infi.symm
/-- Monotone convergence theorem for nonincreasing sequences of functions -/
lemma lintegral_infi
{f : β β Ξ± β ββ₯0β} (h_meas : βn, measurable (f n))
(h_mono : β β¦m nβ¦, m β€ n β f n β€ f m) (h_fin : β«β» a, f 0 a βΞΌ < β) :
β«β» a, β¨
n, f n a βΞΌ = β¨
n, β«β» a, f n a βΞΌ :=
lintegral_infi_ae h_meas (Ξ» n, ae_of_all _ $ h_mono $ le_of_lt n.lt_succ_self) h_fin
/-- Known as Fatou's lemma, version with `ae_measurable` functions -/
lemma lintegral_liminf_le' {f : β β Ξ± β ββ₯0β} (h_meas : βn, ae_measurable (f n) ΞΌ) :
β«β» a, liminf at_top (Ξ» n, f n a) βΞΌ β€ liminf at_top (Ξ» n, β«β» a, f n a βΞΌ) :=
calc
β«β» a, liminf at_top (Ξ» n, f n a) βΞΌ = β«β» a, β¨n:β, β¨
iβ₯n, f i a βΞΌ :
by simp only [liminf_eq_supr_infi_of_nat]
... = β¨n:β, β«β» a, β¨
iβ₯n, f i a βΞΌ :
lintegral_supr'
(assume n, ae_measurable_binfi _ (countable_encodable _) h_meas)
(ae_of_all ΞΌ (assume a n m hnm, infi_le_infi_of_subset $ Ξ» i hi, le_trans hnm hi))
... β€ β¨n:β, β¨
iβ₯n, β«β» a, f i a βΞΌ :
supr_le_supr $ Ξ» n, le_infi2_lintegral _
... = at_top.liminf (Ξ» n, β«β» a, f n a βΞΌ) : filter.liminf_eq_supr_infi_of_nat.symm
/-- Known as Fatou's lemma -/
lemma lintegral_liminf_le {f : β β Ξ± β ββ₯0β} (h_meas : βn, measurable (f n)) :
β«β» a, liminf at_top (Ξ» n, f n a) βΞΌ β€ liminf at_top (Ξ» n, β«β» a, f n a βΞΌ) :=
lintegral_liminf_le' (Ξ» n, (h_meas n).ae_measurable)
lemma limsup_lintegral_le {f : β β Ξ± β ββ₯0β} {g : Ξ± β ββ₯0β}
(hf_meas : β n, measurable (f n)) (h_bound : βn, f n β€α΅[ΞΌ] g) (h_fin : β«β» a, g a βΞΌ < β) :
limsup at_top (Ξ»n, β«β» a, f n a βΞΌ) β€ β«β» a, limsup at_top (Ξ»n, f n a) βΞΌ :=
calc
limsup at_top (Ξ»n, β«β» a, f n a βΞΌ) = β¨
n:β, β¨iβ₯n, β«β» a, f i a βΞΌ :
limsup_eq_infi_supr_of_nat
... β€ β¨
n:β, β«β» a, β¨iβ₯n, f i a βΞΌ :
infi_le_infi $ assume n, supr2_lintegral_le _
... = β«β» a, β¨
n:β, β¨iβ₯n, f i a βΞΌ :
begin
refine (lintegral_infi _ _ _).symm,
{ assume n, exact measurable_bsupr _ (countable_encodable _) hf_meas },
{ assume n m hnm a, exact (supr_le_supr_of_subset $ Ξ» i hi, le_trans hnm hi) },
{ refine lt_of_le_of_lt (lintegral_mono_ae _) h_fin,
refine (ae_all_iff.2 h_bound).mono (Ξ» n hn, _),
exact supr_le (Ξ» i, supr_le $ Ξ» hi, hn i) }
end
... = β«β» a, limsup at_top (Ξ»n, f n a) βΞΌ :
by simp only [limsup_eq_infi_supr_of_nat]
/-- Dominated convergence theorem for nonnegative functions -/
lemma tendsto_lintegral_of_dominated_convergence
{F : β β Ξ± β ββ₯0β} {f : Ξ± β ββ₯0β} (bound : Ξ± β ββ₯0β)
(hF_meas : βn, measurable (F n)) (h_bound : βn, F n β€α΅[ΞΌ] bound)
(h_fin : β«β» a, bound a βΞΌ < β)
(h_lim : βα΅ a βΞΌ, tendsto (Ξ» n, F n a) at_top (π (f a))) :
tendsto (Ξ»n, β«β» a, F n a βΞΌ) at_top (π (β«β» a, f a βΞΌ)) :=
tendsto_of_le_liminf_of_limsup_le
(calc β«β» a, f a βΞΌ = β«β» a, liminf at_top (Ξ» (n : β), F n a) βΞΌ :
lintegral_congr_ae $ h_lim.mono $ assume a h, h.liminf_eq.symm
... β€ liminf at_top (Ξ» n, β«β» a, F n a βΞΌ) : lintegral_liminf_le hF_meas)
(calc limsup at_top (Ξ» (n : β), β«β» a, F n a βΞΌ) β€ β«β» a, limsup at_top (Ξ»n, F n a) βΞΌ :
limsup_lintegral_le hF_meas h_bound h_fin
... = β«β» a, f a βΞΌ : lintegral_congr_ae $ h_lim.mono $ Ξ» a h, h.limsup_eq)
/-- Dominated convergence theorem for nonnegative functions which are just almost everywhere
measurable. -/
lemma tendsto_lintegral_of_dominated_convergence'
{F : β β Ξ± β ββ₯0β} {f : Ξ± β ββ₯0β} (bound : Ξ± β ββ₯0β)
(hF_meas : βn, ae_measurable (F n) ΞΌ) (h_bound : βn, F n β€α΅[ΞΌ] bound)
(h_fin : β«β» a, bound a βΞΌ < β)
(h_lim : βα΅ a βΞΌ, tendsto (Ξ» n, F n a) at_top (π (f a))) :
tendsto (Ξ»n, β«β» a, F n a βΞΌ) at_top (π (β«β» a, f a βΞΌ)) :=
begin
have : β n, β«β» a, F n a βΞΌ = β«β» a, (hF_meas n).mk (F n) a βΞΌ :=
Ξ» n, lintegral_congr_ae (hF_meas n).ae_eq_mk,
simp_rw this,
apply tendsto_lintegral_of_dominated_convergence bound (Ξ» n, (hF_meas n).measurable_mk) _ h_fin,
{ have : β n, βα΅ a βΞΌ, (hF_meas n).mk (F n) a = F n a :=
Ξ» n, (hF_meas n).ae_eq_mk.symm,
have : βα΅ a βΞΌ, β n, (hF_meas n).mk (F n) a = F n a := ae_all_iff.mpr this,
filter_upwards [this, h_lim],
assume a H H',
simp_rw H,
exact H' },
{ assume n,
filter_upwards [h_bound n, (hF_meas n).ae_eq_mk],
assume a H H',
rwa H' at H }
end
/-- Dominated convergence theorem for filters with a countable basis -/
lemma tendsto_lintegral_filter_of_dominated_convergence {ΞΉ} {l : filter ΞΉ}
{F : ΞΉ β Ξ± β ββ₯0β} {f : Ξ± β ββ₯0β} (bound : Ξ± β ββ₯0β)
(hl_cb : l.is_countably_generated)
(hF_meas : βαΆ n in l, measurable (F n))
(h_bound : βαΆ n in l, βα΅ a βΞΌ, F n a β€ bound a)
(h_fin : β«β» a, bound a βΞΌ < β)
(h_lim : βα΅ a βΞΌ, tendsto (Ξ» n, F n a) l (π (f a))) :
tendsto (Ξ»n, β«β» a, F n a βΞΌ) l (π $ β«β» a, f a βΞΌ) :=
begin
rw hl_cb.tendsto_iff_seq_tendsto,
{ intros x xl,
have hxl, { rw tendsto_at_top' at xl, exact xl },
have h := inter_mem_sets hF_meas h_bound,
replace h := hxl _ h,
rcases h with β¨k, hβ©,
rw β tendsto_add_at_top_iff_nat k,
refine tendsto_lintegral_of_dominated_convergence _ _ _ _ _,
{ exact bound },
{ intro, refine (h _ _).1, exact nat.le_add_left _ _ },
{ intro, refine (h _ _).2, exact nat.le_add_left _ _ },
{ assumption },
{ refine h_lim.mono (Ξ» a h_lim, _),
apply @tendsto.comp _ _ _ (Ξ»n, x (n + k)) (Ξ»n, F n a),
{ assumption },
rw tendsto_add_at_top_iff_nat,
assumption } },
end
section
open encodable
/-- Monotone convergence for a suprema over a directed family and indexed by an encodable type -/
theorem lintegral_supr_directed [encodable Ξ²] {f : Ξ² β Ξ± β ββ₯0β}
(hf : βb, measurable (f b)) (h_directed : directed (β€) f) :
β«β» a, β¨b, f b a βΞΌ = β¨b, β«β» a, f b a βΞΌ :=
begin
by_cases hΞ² : nonempty Ξ², swap,
{ simp [supr_of_empty hΞ²] },
resetI, inhabit Ξ²,
have : βa, (β¨ b, f b a) = (β¨ n, f (h_directed.sequence f n) a),
{ assume a,
refine le_antisymm (supr_le $ assume b, _) (supr_le $ assume n, le_supr (Ξ»n, f n a) _),
exact le_supr_of_le (encode b + 1) (h_directed.le_sequence b a) },
calc β«β» a, β¨ b, f b a βΞΌ = β«β» a, β¨ n, f (h_directed.sequence f n) a βΞΌ :
by simp only [this]
... = β¨ n, β«β» a, f (h_directed.sequence f n) a βΞΌ :
lintegral_supr (assume n, hf _) h_directed.sequence_mono
... = β¨ b, β«β» a, f b a βΞΌ :
begin
refine le_antisymm (supr_le $ assume n, _) (supr_le $ assume b, _),
{ exact le_supr (Ξ»b, β«β» a, f b a βΞΌ) _ },
{ exact le_supr_of_le (encode b + 1)
(lintegral_mono $ h_directed.le_sequence b) }
end
end
end
lemma lintegral_tsum [encodable Ξ²] {f : Ξ² β Ξ± β ββ₯0β} (hf : βi, measurable (f i)) :
β«β» a, β' i, f i a βΞΌ = β' i, β«β» a, f i a βΞΌ :=
begin
simp only [ennreal.tsum_eq_supr_sum],
rw [lintegral_supr_directed],
{ simp [lintegral_finset_sum _ (Ξ» i _, hf i)] },
{ assume b, exact finset.measurable_sum _ (Ξ» i _, hf i) },
{ assume s t,
use [s βͺ t],
split,
exact assume a, finset.sum_le_sum_of_subset (finset.subset_union_left _ _),
exact assume a, finset.sum_le_sum_of_subset (finset.subset_union_right _ _) }
end
open measure
lemma lintegral_Union [encodable Ξ²] {s : Ξ² β set Ξ±} (hm : β i, measurable_set (s i))
(hd : pairwise (disjoint on s)) (f : Ξ± β ββ₯0β) :
β«β» a in β i, s i, f a βΞΌ = β' i, β«β» a in s i, f a βΞΌ :=
by simp only [measure.restrict_Union hd hm, lintegral_sum_measure]
lemma lintegral_Union_le [encodable Ξ²] (s : Ξ² β set Ξ±) (f : Ξ± β ββ₯0β) :
β«β» a in β i, s i, f a βΞΌ β€ β' i, β«β» a in s i, f a βΞΌ :=
begin
rw [β lintegral_sum_measure],
exact lintegral_mono' restrict_Union_le (le_refl _)
end
lemma lintegral_map [measurable_space Ξ²] {f : Ξ² β ββ₯0β} {g : Ξ± β Ξ²}
(hf : measurable f) (hg : measurable g) : β«β» a, f a β(map g ΞΌ) = β«β» a, f (g a) βΞΌ :=
begin
simp only [lintegral_eq_supr_eapprox_lintegral, hf, hf.comp hg],
{ congr, funext n, symmetry,
apply simple_func.lintegral_map,
{ assume a, exact congr_fun (simple_func.eapprox_comp hf hg) a },
{ assume s hs, exact map_apply hg hs } },
end
lemma lintegral_map' [measurable_space Ξ²] {f : Ξ² β ββ₯0β} {g : Ξ± β Ξ²}
(hf : ae_measurable f (measure.map g ΞΌ)) (hg : measurable g) :
β«β» a, f a β(measure.map g ΞΌ) = β«β» a, f (g a) βΞΌ :=
calc β«β» a, f a β(measure.map g ΞΌ) = β«β» a, hf.mk f a β(measure.map g ΞΌ) :
lintegral_congr_ae hf.ae_eq_mk
... = β«β» a, hf.mk f (g a) βΞΌ : lintegral_map hf.measurable_mk hg
... = β«β» a, f (g a) βΞΌ : lintegral_congr_ae (ae_eq_comp hg hf.ae_eq_mk.symm)
lemma lintegral_comp [measurable_space Ξ²] {f : Ξ² β ββ₯0β} {g : Ξ± β Ξ²}
(hf : measurable f) (hg : measurable g) : lintegral ΞΌ (f β g) = β«β» a, f a β(map g ΞΌ) :=
(lintegral_map hf hg).symm
lemma set_lintegral_map [measurable_space Ξ²] {f : Ξ² β ββ₯0β} {g : Ξ± β Ξ²}
{s : set Ξ²} (hs : measurable_set s) (hf : measurable f) (hg : measurable g) :
β«β» y in s, f y β(map g ΞΌ) = β«β» x in g β»ΒΉ' s, f (g x) βΞΌ :=
by rw [restrict_map hg hs, lintegral_map hf hg]
/-- The `lintegral` transforms appropriately under a measurable equivalence `g : Ξ± βα΅ Ξ²`.
(Compare `lintegral_map`, which applies to a wider class of functions `g : Ξ± β Ξ²`, but requires
measurability of the function being integrated.) -/
lemma lintegral_map_equiv [measurable_space Ξ²] (f : Ξ² β ββ₯0β) (g : Ξ± βα΅ Ξ²) :
β«β» a, f a β(map g ΞΌ) = β«β» a, f (g a) βΞΌ :=
begin
refine le_antisymm _ _,
{ refine supr_le_supr2 _,
intros fβ,
use fβ.comp g g.measurable,
refine supr_le_supr2 _,
intros hfβ,
use Ξ» x, hfβ (g x),
exact (lintegral_map_equiv fβ g).symm.le },
{ refine supr_le_supr2 _,
intros fβ,
use fβ.comp g.symm g.symm.measurable,
refine supr_le_supr2 _,
intros hfβ,
have : (Ξ» a, (fβ.comp (g.symm) g.symm.measurable) a) β€ Ξ» (a : Ξ²), f a,
{ convert Ξ» x, hfβ (g.symm x),
funext,
simp [congr_arg f (congr_fun g.self_comp_symm a)] },
use this,
convert (lintegral_map_equiv (fβ.comp g.symm g.symm.measurable) g).le,
apply simple_func.ext,
intros a,
convert congr_arg fβ (congr_fun g.symm_comp_self a).symm using 1 }
end
lemma lintegral_dirac' (a : Ξ±) {f : Ξ± β ββ₯0β} (hf : measurable f) :
β«β» a, f a β(dirac a) = f a :=
by simp [lintegral_congr_ae (ae_eq_dirac' hf)]
lemma lintegral_dirac [measurable_singleton_class Ξ±] (a : Ξ±) (f : Ξ± β ββ₯0β) :
β«β» a, f a β(dirac a) = f a :=
by simp [lintegral_congr_ae (ae_eq_dirac f)]
lemma lintegral_count' {f : Ξ± β ββ₯0β} (hf : measurable f) :
β«β» a, f a βcount = β' a, f a :=
begin
rw [count, lintegral_sum_measure],
congr,
exact funext (Ξ» a, lintegral_dirac' a hf),
end
lemma lintegral_count [measurable_singleton_class Ξ±] (f : Ξ± β ββ₯0β) :
β«β» a, f a βcount = β' a, f a :=
begin
rw [count, lintegral_sum_measure],
congr,
exact funext (Ξ» a, lintegral_dirac a f),
end
lemma ae_lt_top {f : Ξ± β ββ₯0β} (hf : measurable f) (h2f : β«β» x, f x βΞΌ < β) :
βα΅ x βΞΌ, f x < β :=
begin
simp_rw [ae_iff, ennreal.not_lt_top], by_contra h, rw [β not_le] at h2f, apply h2f,
have : (f β»ΒΉ' {β}).indicator β€ β€ f,
{ intro x, by_cases hx : x β f β»ΒΉ' {β}; [simpa [hx], simp [hx]] },
convert lintegral_mono this,
rw [lintegral_indicator _ (hf (measurable_set_singleton β))], simp [ennreal.top_mul, preimage, h]
end
lemma ae_lt_top' {f : Ξ± β ββ₯0β} (hf : ae_measurable f ΞΌ) (h2f : β«β» x, f x βΞΌ < β) :
βα΅ x βΞΌ, f x < β :=
begin
have h2f_meas : β«β» x, hf.mk f x βΞΌ < β, by rwa βlintegral_congr_ae hf.ae_eq_mk,
exact (ae_lt_top hf.measurable_mk h2f_meas).mp (hf.ae_eq_mk.mono (Ξ» x hx h, by rwa hx)),
end
/-- Given a measure `ΞΌ : measure Ξ±` and a function `f : Ξ± β ββ₯0β`, `ΞΌ.with_density f` is the
measure such that for a measurable set `s` we have `ΞΌ.with_density f s = β«β» a in s, f a βΞΌ`. -/
def measure.with_density (ΞΌ : measure Ξ±) (f : Ξ± β ββ₯0β) : measure Ξ± :=
measure.of_measurable (Ξ»s hs, β«β» a in s, f a βΞΌ) (by simp) (Ξ» s hs hd, lintegral_Union hs hd _)
@[simp] lemma with_density_apply (f : Ξ± β ββ₯0β) {s : set Ξ±} (hs : measurable_set s) :
ΞΌ.with_density f s = β«β» a in s, f a βΞΌ :=
measure.of_measurable_apply s hs
end lintegral
end measure_theory
open measure_theory measure_theory.simple_func
/-- To prove something for an arbitrary measurable function into `ββ₯0β`, it suffices to show
that the property holds for (multiples of) characteristic functions and is closed under addition
and supremum of increasing sequences of functions.
It is possible to make the hypotheses in the induction steps a bit stronger, and such conditions
can be added once we need them (for example in `h_add` it is only necessary to consider the sum of
a simple function with a multiple of a characteristic function and that the intersection
of their images is a subset of `{0}`. -/
@[elab_as_eliminator]
theorem measurable.ennreal_induction {Ξ±} [measurable_space Ξ±] {P : (Ξ± β ββ₯0β) β Prop}
(h_ind : β (c : ββ₯0β) β¦sβ¦, measurable_set s β P (indicator s (Ξ» _, c)))
(h_add : β β¦f g : Ξ± β ββ₯0ββ¦, disjoint (support f) (support g) β measurable f β measurable g β
P f β P g β P (f + g))
(h_supr : β β¦f : β β Ξ± β ββ₯0ββ¦ (hf : βn, measurable (f n)) (h_mono : monotone f)
(hP : β n, P (f n)), P (Ξ» x, β¨ n, f n x))
β¦f : Ξ± β ββ₯0ββ¦ (hf : measurable f) : P f :=
begin
convert h_supr (Ξ» n, (eapprox f n).measurable) (monotone_eapprox f) _,
{ ext1 x, rw [supr_eapprox_apply f hf] },
{ exact Ξ» n, simple_func.induction (Ξ» c s hs, h_ind c hs)
(Ξ» f g hfg hf hg, h_add hfg f.measurable g.measurable hf hg) (eapprox f n) }
end
namespace measure_theory
/-- This is Exercise 1.2.1 from [tao2010]. It allows you to express integration of a measurable
function with respect to `(ΞΌ.with_density f)` as an integral with respect to `ΞΌ`, called the base
measure. `ΞΌ` is often the Lebesgue measure, and in this circumstance `f` is the probability density
function, and `(ΞΌ.with_density f)` represents any continuous random variable as a
probability measure, such as the uniform distribution between 0 and 1, the Gaussian distribution,
the exponential distribution, the Beta distribution, or the Cauchy distribution (see Section 2.4
of [wasserman2004]). Thus, this method shows how to one can calculate expectations, variances,
and other moments as a function of the probability density function.
-/
lemma lintegral_with_density_eq_lintegral_mul {Ξ±} [measurable_space Ξ±] (ΞΌ : measure Ξ±)
{f : Ξ± β ββ₯0β} (h_mf : measurable f) : β {g : Ξ± β ββ₯0β}, measurable g β
β«β» a, g a β(ΞΌ.with_density f) = β«β» a, (f * g) a βΞΌ :=
begin
apply measurable.ennreal_induction,
{ intros c s h_ms,
simp [*, mul_comm _ c, β indicator_mul_right], },
{ intros g h h_univ h_mea_g h_mea_h h_ind_g h_ind_h,
simp [mul_add, *, measurable.mul] },
{ intros g h_mea_g h_mono_g h_ind,
have : monotone (Ξ» n a, f a * g n a) := Ξ» m n hmn x, ennreal.mul_le_mul le_rfl (h_mono_g hmn x),
simp [lintegral_supr, ennreal.mul_supr, h_mf.mul (h_mea_g _), *] }
end
end measure_theory
|
7b6110b9fc3d396f2d9294e670b276d1fef433ed | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/topology/partition_of_unity.lean | a826173c07c43637d122baef599c8573894a0351 | [
"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 | 23,036 | lean | /-
Copyright (c) 2021 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury Kudryashov
-/
import algebra.big_operators.finprod
import set_theory.ordinal.basic
import topology.continuous_function.algebra
import topology.paracompact
import topology.shrinking_lemma
import topology.urysohns_lemma
/-!
# Continuous partition of unity
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
In this file we define `partition_of_unity (ΞΉ X : Type*) [topological_space X] (s : set X := univ)`
to be a continuous partition of unity on `s` indexed by `ΞΉ`. More precisely, `f : partition_of_unity
ΞΉ X s` is a collection of continuous functions `f i : C(X, β)`, `i : ΞΉ`, such that
* the supports of `f i` form a locally finite family of sets;
* each `f i` is nonnegative;
* `βαΆ i, f i x = 1` for all `x β s`;
* `βαΆ i, f i x β€ 1` for all `x : X`.
In the case `s = univ` the last assumption follows from the previous one but it is convenient to
have this assumption in the case `s β univ`.
We also define a bump function covering,
`bump_covering (ΞΉ X : Type*) [topological_space X] (s : set X := univ)`, to be a collection of
functions `f i : C(X, β)`, `i : ΞΉ`, such that
* the supports of `f i` form a locally finite family of sets;
* each `f i` is nonnegative;
* for each `x β s` there exists `i : ΞΉ` such that `f i y = 1` in a neighborhood of `x`.
The term is motivated by the smooth case.
If `f` is a bump function covering indexed by a linearly ordered type, then
`g i x = f i x * βαΆ j < i, (1 - f j x)` is a partition of unity, see
`bump_covering.to_partition_of_unity`. Note that only finitely many terms `1 - f j x` are not equal
to one, so this product is well-defined.
Note that `g i x = βαΆ j β€ i, (1 - f j x) - βαΆ j < i, (1 - f j x)`, so most terms in the sum
`βαΆ i, g i x` cancel, and we get `βαΆ i, g i x = 1 - βαΆ i, (1 - f i x)`, and the latter product
equals zero because one of `f i x` is equal to one.
We say that a partition of unity or a bump function covering `f` is *subordinate* to a family of
sets `U i`, `i : ΞΉ`, if the closure of the support of each `f i` is included in `U i`. We use
Urysohn's Lemma to prove that a locally finite open covering of a normal topological space admits a
subordinate bump function covering (hence, a subordinate partition of unity), see
`bump_covering.exists_is_subordinate_of_locally_finite`. If `X` is a paracompact space, then any
open covering admits a locally finite refinement, hence it admits a subordinate bump function
covering and a subordinate partition of unity, see `bump_covering.exists_is_subordinate`.
We also provide two slightly more general versions of these lemmas,
`bump_covering.exists_is_subordinate_of_locally_finite_of_prop` and
`bump_covering.exists_is_subordinate_of_prop`, to be used later in the construction of a smooth
partition of unity.
## Implementation notes
Most (if not all) books only define a partition of unity of the whole space. However, quite a few
proofs only deal with `f i` such that `tsupport (f i)` meets a specific closed subset, and
it is easier to formalize these proofs if we don't have other functions right away.
We use `well_ordering_rel j i` instead of `j < i` in the definition of
`bump_covering.to_partition_of_unity` to avoid a `[linear_order ΞΉ]` assumption. While
`well_ordering_rel j i` is a well order, not only a strict linear order, we never use this property.
## Tags
partition of unity, bump function, Urysohn's lemma, normal space, paracompact space
-/
universes u v
open function set filter
open_locale big_operators topology classical
noncomputable theory
/-- A continuous partition of unity on a set `s : set X` is a collection of continuous functions
`f i` such that
* the supports of `f i` form a locally finite family of sets, i.e., for every point `x : X` there
exists a neighborhood `U β x` such that all but finitely many functions `f i` are zero on `U`;
* the functions `f i` are nonnegative;
* the sum `βαΆ i, f i x` is equal to one for every `x β s` and is less than or equal to one
otherwise.
If `X` is a normal paracompact space, then `partition_of_unity.exists_is_subordinate` guarantees
that for every open covering `U : set (set X)` of `s` there exists a partition of unity that is
subordinate to `U`.
-/
structure partition_of_unity (ΞΉ X : Type*) [topological_space X] (s : set X := univ) :=
(to_fun : ΞΉ β C(X, β))
(locally_finite' : locally_finite (Ξ» i, support (to_fun i)))
(nonneg' : 0 β€ to_fun)
(sum_eq_one' : β x β s, βαΆ i, to_fun i x = 1)
(sum_le_one' : β x, βαΆ i, to_fun i x β€ 1)
/-- A `bump_covering ΞΉ X s` is an indexed family of functions `f i`, `i : ΞΉ`, such that
* the supports of `f i` form a locally finite family of sets, i.e., for every point `x : X` there
exists a neighborhood `U β x` such that all but finitely many functions `f i` are zero on `U`;
* for all `i`, `x` we have `0 β€ f i x β€ 1`;
* each point `x β s` belongs to the interior of `{x | f i x = 1}` for some `i`.
One of the main use cases for a `bump_covering` is to define a `partition_of_unity`, see
`bump_covering.to_partition_of_unity`, but some proofs can directly use a `bump_covering` instead of
a `partition_of_unity`.
If `X` is a normal paracompact space, then `bump_covering.exists_is_subordinate` guarantees that for
every open covering `U : set (set X)` of `s` there exists a `bump_covering` of `s` that is
subordinate to `U`.
-/
structure bump_covering (ΞΉ X : Type*) [topological_space X] (s : set X := univ) :=
(to_fun : ΞΉ β C(X, β))
(locally_finite' : locally_finite (Ξ» i, support (to_fun i)))
(nonneg' : 0 β€ to_fun)
(le_one' : to_fun β€ 1)
(eventually_eq_one' : β x β s, β i, to_fun i =αΆ [π x] 1)
variables {ΞΉ : Type u} {X : Type v} [topological_space X]
namespace partition_of_unity
variables {E : Type*} [add_comm_monoid E] [smul_with_zero β E] [topological_space E]
[has_continuous_smul β E] {s : set X} (f : partition_of_unity ΞΉ X s)
instance : has_coe_to_fun (partition_of_unity ΞΉ X s) (Ξ» _, ΞΉ β C(X, β)) := β¨to_funβ©
protected lemma locally_finite : locally_finite (Ξ» i, support (f i)) := f.locally_finite'
lemma locally_finite_tsupport : locally_finite (Ξ» i, tsupport (f i)) := f.locally_finite.closure
lemma nonneg (i : ΞΉ) (x : X) : 0 β€ f i x := f.nonneg' i x
lemma sum_eq_one {x : X} (hx : x β s) : βαΆ i, f i x = 1 := f.sum_eq_one' x hx
/-- If `f` is a partition of unity on `s`, then for every `x β s` there exists an index `i` such
that `0 < f i x`. -/
lemma exists_pos {x : X} (hx : x β s) : β i, 0 < f i x :=
begin
have H := f.sum_eq_one hx,
contrapose! H,
simpa only [Ξ» i, (H i).antisymm (f.nonneg i x), finsum_zero] using zero_ne_one
end
lemma sum_le_one (x : X) : βαΆ i, f i x β€ 1 := f.sum_le_one' x
lemma sum_nonneg (x : X) : 0 β€ βαΆ i, f i x := finsum_nonneg $ Ξ» i, f.nonneg i x
lemma le_one (i : ΞΉ) (x : X) : f i x β€ 1 :=
(single_le_finsum i (f.locally_finite.point_finite x) (Ξ» j, f.nonneg j x)).trans (f.sum_le_one x)
/-- If `f` is a partition of unity on `s : set X` and `g : X β E` is continuous at every point of
the topological support of some `f i`, then `Ξ» x, f i x β’ g x` is continuous on the whole space. -/
lemma continuous_smul {g : X β E} {i : ΞΉ} (hg : β x β tsupport (f i), continuous_at g x) :
continuous (Ξ» x, f i x β’ g x) :=
continuous_of_tsupport $ Ξ» x hx, ((f i).continuous_at x).smul $
hg x $ tsupport_smul_subset_left _ _ hx
/-- If `f` is a partition of unity on a set `s : set X` and `g : ΞΉ β X β E` is a family of functions
such that each `g i` is continuous at every point of the topological support of `f i`, then the sum
`Ξ» x, βαΆ i, f i x β’ g i x` is continuous on the whole space. -/
lemma continuous_finsum_smul [has_continuous_add E] {g : ΞΉ β X β E}
(hg : β i (x β tsupport (f i)), continuous_at (g i) x) :
continuous (Ξ» x, βαΆ i, f i x β’ g i x) :=
continuous_finsum (Ξ» i, f.continuous_smul (hg i)) $
f.locally_finite.subset $ Ξ» i, support_smul_subset_left _ _
/-- A partition of unity `f i` is subordinate to a family of sets `U i` indexed by the same type if
for each `i` the closure of the support of `f i` is a subset of `U i`. -/
def is_subordinate (U : ΞΉ β set X) : Prop :=
β i, tsupport (f i) β U i
variables {f}
lemma exists_finset_nhd_support_subset {U : ΞΉ β set X}
(hso : f.is_subordinate U) (ho : β i, is_open (U i)) (x : X) :
β (is : finset ΞΉ) {n : set X} (hnβ : n β π x) (hnβ : n β β i β is, U i), β (z β n),
support (Ξ» i, f i z) β is :=
f.locally_finite.exists_finset_nhd_support_subset hso ho x
/-- If `f` is a partition of unity that is subordinate to a family of open sets `U i` and
`g : ΞΉ β X β E` is a family of functions such that each `g i` is continuous on `U i`, then the sum
`Ξ» x, βαΆ i, f i x β’ g i x` is a continuous function. -/
lemma is_subordinate.continuous_finsum_smul [has_continuous_add E] {U : ΞΉ β set X}
(ho : β i, is_open (U i)) (hf : f.is_subordinate U) {g : ΞΉ β X β E}
(hg : β i, continuous_on (g i) (U i)) :
continuous (Ξ» x, βαΆ i, f i x β’ g i x) :=
f.continuous_finsum_smul $ Ξ» i x hx, (hg i).continuous_at $ (ho i).mem_nhds $ hf i hx
end partition_of_unity
namespace bump_covering
variables {s : set X} (f : bump_covering ΞΉ X s)
instance : has_coe_to_fun (bump_covering ΞΉ X s) (Ξ» _, ΞΉ β C(X, β)) := β¨to_funβ©
protected lemma locally_finite : locally_finite (Ξ» i, support (f i)) := f.locally_finite'
lemma locally_finite_tsupport : locally_finite (Ξ» i, tsupport (f i)) := f.locally_finite.closure
protected lemma point_finite (x : X) : {i | f i x β 0}.finite := f.locally_finite.point_finite x
lemma nonneg (i : ΞΉ) (x : X) : 0 β€ f i x := f.nonneg' i x
lemma le_one (i : ΞΉ) (x : X) : f i x β€ 1 := f.le_one' i x
/-- A `bump_covering` that consists of a single function, uniformly equal to one, defined as an
example for `inhabited` instance. -/
protected def single (i : ΞΉ) (s : set X) : bump_covering ΞΉ X s :=
{ to_fun := pi.single i 1,
locally_finite' := Ξ» x,
begin
refine β¨univ, univ_mem, (finite_singleton i).subset _β©,
rintro j β¨x, hx, -β©,
contrapose! hx,
rw [mem_singleton_iff] at hx,
simp [hx]
end,
nonneg' := le_update_iff.2 β¨Ξ» x, zero_le_one, Ξ» _ _, le_rflβ©,
le_one' := update_le_iff.2 β¨le_rfl, Ξ» _ _ _, zero_le_oneβ©,
eventually_eq_one' := Ξ» x _, β¨i, by simpβ© }
@[simp] lemma coe_single (i : ΞΉ) (s : set X) : β(bump_covering.single i s) = pi.single i 1 := rfl
instance [inhabited ΞΉ] : inhabited (bump_covering ΞΉ X s) :=
β¨bump_covering.single default sβ©
/-- A collection of bump functions `f i` is subordinate to a family of sets `U i` indexed by the
same type if for each `i` the closure of the support of `f i` is a subset of `U i`. -/
def is_subordinate (f : bump_covering ΞΉ X s) (U : ΞΉ β set X) : Prop :=
β i, tsupport (f i) β U i
lemma is_subordinate.mono {f : bump_covering ΞΉ X s} {U V : ΞΉ β set X} (hU : f.is_subordinate U)
(hV : β i, U i β V i) :
f.is_subordinate V :=
Ξ» i, subset.trans (hU i) (hV i)
/-- If `X` is a normal topological space and `U i`, `i : ΞΉ`, is a locally finite open covering of a
closed set `s`, then there exists a `bump_covering ΞΉ X s` that is subordinate to `U`. If `X` is a
paracompact space, then the assumption `hf : locally_finite U` can be omitted, see
`bump_covering.exists_is_subordinate`. This version assumes that `p : (X β β) β Prop` is a predicate
that satisfies Urysohn's lemma, and provides a `bump_covering` such that each function of the
covering satisfies `p`. -/
lemma exists_is_subordinate_of_locally_finite_of_prop [normal_space X] (p : (X β β) β Prop)
(h01 : β s t, is_closed s β is_closed t β disjoint s t β
β f : C(X, β), p f β§ eq_on f 0 s β§ eq_on f 1 t β§ β x, f x β Icc (0 : β) 1)
(hs : is_closed s) (U : ΞΉ β set X) (ho : β i, is_open (U i)) (hf : locally_finite U)
(hU : s β β i, U i) :
β f : bump_covering ΞΉ X s, (β i, p (f i)) β§ f.is_subordinate U :=
begin
rcases exists_subset_Union_closure_subset hs ho (Ξ» x _, hf.point_finite x) hU
with β¨V, hsV, hVo, hVUβ©,
have hVU' : β i, V i β U i, from Ξ» i, subset.trans subset_closure (hVU i),
rcases exists_subset_Union_closure_subset hs hVo
(Ξ» x _, (hf.subset hVU').point_finite x) hsV with β¨W, hsW, hWo, hWVβ©,
choose f hfp hf0 hf1 hf01
using Ξ» i, h01 _ _ (is_closed_compl_iff.2 $ hVo i)
is_closed_closure (disjoint_right.2 $ Ξ» x hx, not_not.2 (hWV i hx)),
have hsupp : β i, support (f i) β V i,
from Ξ» i, support_subset_iff'.2 (hf0 i),
refine β¨β¨f, hf.subset (Ξ» i, subset.trans (hsupp i) (hVU' i)),
Ξ» i x, (hf01 i x).1, Ξ» i x, (hf01 i x).2, Ξ» x hx, _β©, hfp,
Ξ» i, subset.trans (closure_mono (hsupp i)) (hVU i)β©,
rcases mem_Union.1 (hsW hx) with β¨i, hiβ©,
exact β¨i, ((hf1 i).mono subset_closure).eventually_eq_of_mem ((hWo i).mem_nhds hi)β©
end
/-- If `X` is a normal topological space and `U i`, `i : ΞΉ`, is a locally finite open covering of a
closed set `s`, then there exists a `bump_covering ΞΉ X s` that is subordinate to `U`. If `X` is a
paracompact space, then the assumption `hf : locally_finite U` can be omitted, see
`bump_covering.exists_is_subordinate`. -/
lemma exists_is_subordinate_of_locally_finite [normal_space X] (hs : is_closed s)
(U : ΞΉ β set X) (ho : β i, is_open (U i)) (hf : locally_finite U)
(hU : s β β i, U i) :
β f : bump_covering ΞΉ X s, f.is_subordinate U :=
let β¨f, _, hfUβ© :=
exists_is_subordinate_of_locally_finite_of_prop (Ξ» _, true)
(Ξ» s t hs ht hd, (exists_continuous_zero_one_of_closed hs ht hd).imp $ Ξ» f hf, β¨trivial, hfβ©)
hs U ho hf hU
in β¨f, hfUβ©
/-- If `X` is a paracompact normal topological space and `U` is an open covering of a closed set
`s`, then there exists a `bump_covering ΞΉ X s` that is subordinate to `U`. This version assumes that
`p : (X β β) β Prop` is a predicate that satisfies Urysohn's lemma, and provides a
`bump_covering` such that each function of the covering satisfies `p`. -/
lemma exists_is_subordinate_of_prop [normal_space X] [paracompact_space X] (p : (X β β) β Prop)
(h01 : β s t, is_closed s β is_closed t β disjoint s t β
β f : C(X, β), p f β§ eq_on f 0 s β§ eq_on f 1 t β§ β x, f x β Icc (0 : β) 1)
(hs : is_closed s) (U : ΞΉ β set X) (ho : β i, is_open (U i)) (hU : s β β i, U i) :
β f : bump_covering ΞΉ X s, (β i, p (f i)) β§ f.is_subordinate U :=
begin
rcases precise_refinement_set hs _ ho hU with β¨V, hVo, hsV, hVf, hVUβ©,
rcases exists_is_subordinate_of_locally_finite_of_prop p h01 hs V hVo hVf hsV with β¨f, hfp, hfβ©,
exact β¨f, hfp, hf.mono hVUβ©
end
/-- If `X` is a paracompact normal topological space and `U` is an open covering of a closed set
`s`, then there exists a `bump_covering ΞΉ X s` that is subordinate to `U`. -/
lemma exists_is_subordinate [normal_space X] [paracompact_space X]
(hs : is_closed s) (U : ΞΉ β set X) (ho : β i, is_open (U i)) (hU : s β β i, U i) :
β f : bump_covering ΞΉ X s, f.is_subordinate U :=
begin
rcases precise_refinement_set hs _ ho hU with β¨V, hVo, hsV, hVf, hVUβ©,
rcases exists_is_subordinate_of_locally_finite hs V hVo hVf hsV with β¨f, hfβ©,
exact β¨f, hf.mono hVUβ©
end
/-- Index of a bump function such that `fs i =αΆ [π x] 1`. -/
def ind (x : X) (hx : x β s) : ΞΉ := (f.eventually_eq_one' x hx).some
lemma eventually_eq_one (x : X) (hx : x β s) : f (f.ind x hx) =αΆ [π x] 1 :=
(f.eventually_eq_one' x hx).some_spec
lemma ind_apply (x : X) (hx : x β s) : f (f.ind x hx) x = 1 :=
(f.eventually_eq_one x hx).eq_of_nhds
/-- Partition of unity defined by a `bump_covering`. We use this auxiliary definition to prove some
properties of the new family of functions before bundling it into a `partition_of_unity`. Do not use
this definition, use `bump_function.to_partition_of_unity` instead.
The partition of unity is given by the formula `g i x = f i x * βαΆ j < i, (1 - f j x)`. In other
words, `g i x = βαΆ j < i, (1 - f j x) - βαΆ j β€ i, (1 - f j x)`, so
`βαΆ i, g i x = 1 - βαΆ j, (1 - f j x)`. If `x β s`, then one of `f j x` equals one, hence the product
of `1 - f j x` vanishes, and `βαΆ i, g i x = 1`.
In order to avoid an assumption `linear_order ΞΉ`, we use `well_ordering_rel` instead of `(<)`. -/
def to_pou_fun (i : ΞΉ) (x : X) : β :=
f i x * βαΆ j (hj : well_ordering_rel j i), (1 - f j x)
lemma to_pou_fun_zero_of_zero {i : ΞΉ} {x : X} (h : f i x = 0) :
f.to_pou_fun i x = 0 :=
by rw [to_pou_fun, h, zero_mul]
lemma support_to_pou_fun_subset (i : ΞΉ) :
support (f.to_pou_fun i) β support (f i) :=
Ξ» x, mt $ f.to_pou_fun_zero_of_zero
lemma to_pou_fun_eq_mul_prod (i : ΞΉ) (x : X) (t : finset ΞΉ)
(ht : β j, well_ordering_rel j i β f j x β 0 β j β t) :
f.to_pou_fun i x = f i x * β j in t.filter (Ξ» j, well_ordering_rel j i), (1 - f j x) :=
begin
refine congr_arg _ (finprod_cond_eq_prod_of_cond_iff _ (Ξ» j hj, _)),
rw [ne.def, sub_eq_self] at hj,
rw [finset.mem_filter, iff.comm, and_iff_right_iff_imp],
exact flip (ht j) hj
end
lemma sum_to_pou_fun_eq (x : X) :
βαΆ i, f.to_pou_fun i x = 1 - βαΆ i, (1 - f i x) :=
begin
set s := (f.point_finite x).to_finset,
have hs : (s : set ΞΉ) = {i | f i x β 0} := finite.coe_to_finset _,
have A : support (Ξ» i, to_pou_fun f i x) β s,
{ rw hs,
exact Ξ» i hi, f.support_to_pou_fun_subset i hi },
have B : mul_support (Ξ» i, 1 - f i x) β s,
{ rw [hs, mul_support_one_sub], exact Ξ» i, id },
letI : linear_order ΞΉ := linear_order_of_STO well_ordering_rel,
rw [finsum_eq_sum_of_support_subset _ A, finprod_eq_prod_of_mul_support_subset _ B,
finset.prod_one_sub_ordered, sub_sub_cancel],
refine finset.sum_congr rfl (Ξ» i hi, _),
convert f.to_pou_fun_eq_mul_prod _ _ _ (Ξ» j hji hj, _),
rwa finite.mem_to_finset
end
lemma exists_finset_to_pou_fun_eventually_eq (i : ΞΉ) (x : X) :
β t : finset ΞΉ, f.to_pou_fun i =αΆ [π x]
f i * β j in t.filter (Ξ» j, well_ordering_rel j i), (1 - f j) :=
begin
rcases f.locally_finite x with β¨U, hU, hfβ©,
use hf.to_finset,
filter_upwards [hU] with y hyU,
simp only [pi.mul_apply, finset.prod_apply],
apply to_pou_fun_eq_mul_prod,
intros j hji hj,
exact hf.mem_to_finset.2 β¨y, β¨hj, hyUβ©β©
end
lemma continuous_to_pou_fun (i : ΞΉ) : continuous (f.to_pou_fun i) :=
begin
refine ((f i).continuous.mul $
continuous_finprod_cond (Ξ» j _, continuous_const.sub (f j).continuous) _),
simp only [mul_support_one_sub],
exact f.locally_finite
end
/-- The partition of unity defined by a `bump_covering`.
The partition of unity is given by the formula `g i x = f i x * βαΆ j < i, (1 - f j x)`. In other
words, `g i x = βαΆ j < i, (1 - f j x) - βαΆ j β€ i, (1 - f j x)`, so
`βαΆ i, g i x = 1 - βαΆ j, (1 - f j x)`. If `x β s`, then one of `f j x` equals one, hence the product
of `1 - f j x` vanishes, and `βαΆ i, g i x = 1`.
In order to avoid an assumption `linear_order ΞΉ`, we use `well_ordering_rel` instead of `(<)`. -/
def to_partition_of_unity : partition_of_unity ΞΉ X s :=
{ to_fun := Ξ» i, β¨f.to_pou_fun i, f.continuous_to_pou_fun iβ©,
locally_finite' := f.locally_finite.subset f.support_to_pou_fun_subset,
nonneg' := Ξ» i x, mul_nonneg (f.nonneg i x)
(finprod_cond_nonneg $ Ξ» j hj, sub_nonneg.2 $ f.le_one j x),
sum_eq_one' := Ξ» x hx,
begin
simp only [continuous_map.coe_mk, sum_to_pou_fun_eq, sub_eq_self],
apply finprod_eq_zero (Ξ» i, 1 - f i x) (f.ind x hx),
{ simp only [f.ind_apply x hx, sub_self] },
{ rw mul_support_one_sub, exact f.point_finite x }
end,
sum_le_one' := Ξ» x,
begin
simp only [continuous_map.coe_mk, sum_to_pou_fun_eq, sub_le_self_iff],
exact finprod_nonneg (Ξ» i, sub_nonneg.2 $ f.le_one i x)
end }
lemma to_partition_of_unity_apply (i : ΞΉ) (x : X) :
f.to_partition_of_unity i x = f i x * βαΆ j (hj : well_ordering_rel j i), (1 - f j x) :=
rfl
lemma to_partition_of_unity_eq_mul_prod (i : ΞΉ) (x : X) (t : finset ΞΉ)
(ht : β j, well_ordering_rel j i β f j x β 0 β j β t) :
f.to_partition_of_unity i x = f i x * β j in t.filter (Ξ» j, well_ordering_rel j i), (1 - f j x) :=
f.to_pou_fun_eq_mul_prod i x t ht
lemma exists_finset_to_partition_of_unity_eventually_eq (i : ΞΉ) (x : X) :
β t : finset ΞΉ, f.to_partition_of_unity i =αΆ [π x]
f i * β j in t.filter (Ξ» j, well_ordering_rel j i), (1 - f j) :=
f.exists_finset_to_pou_fun_eventually_eq i x
lemma to_partition_of_unity_zero_of_zero {i : ΞΉ} {x : X} (h : f i x = 0) :
f.to_partition_of_unity i x = 0 :=
f.to_pou_fun_zero_of_zero h
lemma support_to_partition_of_unity_subset (i : ΞΉ) :
support (f.to_partition_of_unity i) β support (f i) :=
f.support_to_pou_fun_subset i
lemma sum_to_partition_of_unity_eq (x : X) :
βαΆ i, f.to_partition_of_unity i x = 1 - βαΆ i, (1 - f i x) :=
f.sum_to_pou_fun_eq x
lemma is_subordinate.to_partition_of_unity {f : bump_covering ΞΉ X s} {U : ΞΉ β set X}
(h : f.is_subordinate U) :
f.to_partition_of_unity.is_subordinate U :=
Ξ» i, subset.trans (closure_mono $ f.support_to_partition_of_unity_subset i) (h i)
end bump_covering
namespace partition_of_unity
variables {s : set X}
instance [inhabited ΞΉ] : inhabited (partition_of_unity ΞΉ X s) :=
β¨bump_covering.to_partition_of_unity defaultβ©
/-- If `X` is a normal topological space and `U` is a locally finite open covering of a closed set
`s`, then there exists a `partition_of_unity ΞΉ X s` that is subordinate to `U`. If `X` is a
paracompact space, then the assumption `hf : locally_finite U` can be omitted, see
`bump_covering.exists_is_subordinate`. -/
lemma exists_is_subordinate_of_locally_finite [normal_space X] (hs : is_closed s)
(U : ΞΉ β set X) (ho : β i, is_open (U i)) (hf : locally_finite U)
(hU : s β β i, U i) :
β f : partition_of_unity ΞΉ X s, f.is_subordinate U :=
let β¨f, hfβ© := bump_covering.exists_is_subordinate_of_locally_finite hs U ho hf hU
in β¨f.to_partition_of_unity, hf.to_partition_of_unityβ©
/-- If `X` is a paracompact normal topological space and `U` is an open covering of a closed set
`s`, then there exists a `partition_of_unity ΞΉ X s` that is subordinate to `U`. -/
lemma exists_is_subordinate [normal_space X] [paracompact_space X] (hs : is_closed s)
(U : ΞΉ β set X) (ho : β i, is_open (U i)) (hU : s β β i, U i) :
β f : partition_of_unity ΞΉ X s, f.is_subordinate U :=
let β¨f, hfβ© := bump_covering.exists_is_subordinate hs U ho hU
in β¨f.to_partition_of_unity, hf.to_partition_of_unityβ©
end partition_of_unity
|
7e6a839fb469fa3da0c1fd115dd406eb3da4faf6 | 88fb7558b0636ec6b181f2a548ac11ad3919f8a5 | /tests/lean/1467.lean | bc9476e0d02e4374bef9b97e3d80f3112277a592 | [
"Apache-2.0"
] | permissive | moritayasuaki/lean | 9f666c323cb6fa1f31ac597d777914aed41e3b7a | ae96ebf6ee953088c235ff7ae0e8c95066ba8001 | refs/heads/master | 1,611,135,440,814 | 1,493,852,869,000 | 1,493,852,869,000 | 90,269,903 | 0 | 0 | null | 1,493,906,291,000 | 1,493,906,291,000 | null | UTF-8 | Lean | false | false | 856 | lean | constants f g h : β β β
axiom H_f_g : β n, f (g n) = n
example (m : β) : h m = h m :=
begin
definev n : β := g m,
assertv H : f n = m := begin dsimp, rw H_f_g end,
subst H, -- Error here
end
set_option pp.instantiate_mvars false
example (m : β) : h m = h m :=
begin
define n : β, -- add metavar
exact g m,
assertv H : f n = m := begin dsimp, rw H_f_g end,
subst H, -- Error here
end
example (m : β) : h m = h m :=
begin
definev n : β := g m,
assertv H : f n = m := begin dsimp, rw H_f_g end,
subst m, -- Error here
end
set_option pp.instantiate_mvars false
example (m : β) : h m = h m :=
begin
define n : β, -- add metavar
exact g m,
assertv H : f n = m := begin dsimp, rw H_f_g end,
subst m, -- Error here
end
example (m p: β) : h m = h m :=
begin
definev a : β := g p,
definev n : β := g a,
clear p -- Error here
end
|
a47d2a156f5e524f5449de2a23166f08f953c94b | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/algebra/star/star_alg_hom.lean | 40a10f489024005e408a964876e2927533df0255 | [
"Apache-2.0"
] | permissive | jcommelin/mathlib | d8456447c36c176e14d96d9e76f39841f69d2d9b | ee8279351a2e434c2852345c51b728d22af5a156 | refs/heads/master | 1,664,782,136,488 | 1,663,638,983,000 | 1,663,638,983,000 | 132,563,656 | 0 | 0 | Apache-2.0 | 1,663,599,929,000 | 1,525,760,539,000 | Lean | UTF-8 | Lean | false | false | 17,544 | lean | /-
Copyright (c) 2022 Jireh Loreaux. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jireh Loreaux
-/
import algebra.hom.non_unital_alg
import algebra.star.prod
/-!
# Morphisms of star algebras
This file defines morphisms between `R`-algebras (unital or non-unital) `A` and `B` where both
`A` and `B` are equipped with a `star` operation. These morphisms, namely `star_alg_hom` and
`non_unital_star_alg_hom` are direct extensions of their non-`star`red counterparts with a field
`map_star` which guarantees they preserve the star operation. We keep the type classes as generic
as possible, in keeping with the definition of `non_unital_alg_hom` in the non-unital case. In this
file, we only assume `has_star` unless we want to talk about the zero map as a
`non_unital_star_alg_hom`, in which case we need `star_add_monoid`. Note that the scalar ring `R`
is not required to have a star operation, nor do we need `star_ring` or `star_module` structures on
`A` and `B`.
As with `non_unital_alg_hom`, in the non-unital case the multiplications are not assumed to be
associative or unital, or even to be compatible with the scalar actions. In a typical application,
the operations will satisfy compatibility conditions making them into algebras (albeit possibly
non-associative and/or non-unital) but such conditions are not required here for the definitions.
The primary impetus for defining these types is that they constitute the morphisms in the categories
of unital Cβ-algebras (with `star_alg_hom`s) and of Cβ-algebras (with `non_unital_star_alg_hom`s).
TODO: add `star_alg_equiv`.
## Main definitions
* `non_unital_alg_hom`
* `star_alg_hom`
## Tags
non-unital, algebra, morphism, star
-/
set_option old_structure_cmd true
/-! ### Non-unital star algebra homomorphisms -/
/-- A *non-unital β-algebra homomorphism* is a non-unital algebra homomorphism between
non-unital `R`-algebras `A` and `B` equipped with a `star` operation, and this homomorphism is
also `star`-preserving. -/
structure non_unital_star_alg_hom (R A B : Type*) [monoid R]
[non_unital_non_assoc_semiring A] [distrib_mul_action R A] [has_star A]
[non_unital_non_assoc_semiring B] [distrib_mul_action R B] [has_star B]
extends A βββ[R] B :=
(map_star' : β a : A, to_fun (star a) = star (to_fun a))
infixr ` ββββ `:25 := non_unital_star_alg_hom _
notation A ` ββββ[`:25 R `] ` B := non_unital_star_alg_hom R A B
/-- Reinterpret a non-unital star algebra homomorphism as a non-unital algebra homomorphism
by forgetting the interaction with the star operation. -/
add_decl_doc non_unital_star_alg_hom.to_non_unital_alg_hom
/-- `non_unital_star_alg_hom_class F R A B` asserts `F` is a type of bundled non-unital β-algebra
homomorphisms from `A` to `B`. -/
class non_unital_star_alg_hom_class (F : Type*) (R : out_param Type*) (A : out_param Type*)
(B : out_param Type*) [monoid R] [has_star A] [has_star B]
[non_unital_non_assoc_semiring A] [non_unital_non_assoc_semiring B]
[distrib_mul_action R A] [distrib_mul_action R B]
extends non_unital_alg_hom_class F R A B, star_hom_class F A B
-- `R` becomes a metavariable but that's fine because it's an `out_param`
attribute [nolint dangerous_instance] non_unital_star_alg_hom_class.to_star_hom_class
namespace non_unital_star_alg_hom
section basic
variables {R A B C D : Type*} [monoid R]
variables [non_unital_non_assoc_semiring A] [distrib_mul_action R A] [has_star A]
variables [non_unital_non_assoc_semiring B] [distrib_mul_action R B] [has_star B]
variables [non_unital_non_assoc_semiring C] [distrib_mul_action R C] [has_star C]
variables [non_unital_non_assoc_semiring D] [distrib_mul_action R D] [has_star D]
instance : non_unital_star_alg_hom_class (A ββββ[R] B) R A B :=
{ coe := to_fun,
coe_injective' := by rintro β¨f, _β© β¨g, _β© β¨hβ©; congr,
map_smul := Ξ» f, f.map_smul',
map_add := Ξ» f, f.map_add',
map_zero := Ξ» f, f.map_zero',
map_mul := Ξ» f, f.map_mul',
map_star := Ξ» f, f.map_star' }
/-- Helper instance for when there's too many metavariables to apply `fun_like.has_coe_to_fun`
directly. -/
instance : has_coe_to_fun (A ββββ[R] B) (Ξ» _, A β B) := fun_like.has_coe_to_fun
initialize_simps_projections non_unital_star_alg_hom (to_fun β apply)
@[simp] lemma coe_to_non_unital_alg_hom {f : A ββββ[R] B} :
(f.to_non_unital_alg_hom : A β B) = f := rfl
@[ext] lemma ext {f g : A ββββ[R] B} (h : β x, f x = g x) : f = g := fun_like.ext _ _ h
/-- Copy of a `non_unital_star_alg_hom` with a new `to_fun` equal to the old one. Useful
to fix definitional equalities. -/
protected def copy (f : A ββββ[R] B) (f' : A β B) (h : f' = f) : A ββββ[R] B :=
{ to_fun := f',
map_smul' := h.symm βΈ map_smul f,
map_zero' := h.symm βΈ map_zero f,
map_add' := h.symm βΈ map_add f,
map_mul' := h.symm βΈ map_mul f,
map_star' := h.symm βΈ map_star f }
@[simp] lemma coe_mk (f : A β B) (hβ hβ hβ hβ hβ
) :
((β¨f, hβ, hβ, hβ, hβ, hβ
β© : A ββββ[R] B) : A β B) = f :=
rfl
@[simp] lemma mk_coe (f : A ββββ[R] B) (hβ hβ hβ hβ hβ
) :
(β¨f, hβ, hβ, hβ, hβ, hβ
β© : A ββββ[R] B) = f :=
by { ext, refl, }
section
variables (R A)
/-- The identity as a non-unital β-algebra homomorphism. -/
protected def id : A ββββ[R] A :=
{ map_star' := Ξ» x, rfl, .. (1 : A βββ[R] A) }
@[simp] lemma coe_id : β(non_unital_star_alg_hom.id R A) = id := rfl
end
/-- The composition of non-unital β-algebra homomorphisms, as a non-unital β-algebra
homomorphism. -/
def comp (f : B ββββ[R] C) (g : A ββββ[R] B) : A ββββ[R] C :=
{ map_star' := by simp only [map_star, non_unital_alg_hom.to_fun_eq_coe, eq_self_iff_true,
non_unital_alg_hom.coe_comp, coe_to_non_unital_alg_hom, function.comp_app, forall_const],
.. f.to_non_unital_alg_hom.comp g.to_non_unital_alg_hom }
@[simp] lemma coe_comp (f : B ββββ[R] C) (g : A ββββ[R] B) : β(comp f g) = f β g := rfl
@[simp] lemma comp_apply (f : B ββββ[R] C) (g : A ββββ[R] B) (a : A) : comp f g a = f (g a) := rfl
@[simp] lemma comp_assoc (f : C ββββ[R] D) (g : B ββββ[R] C) (h : A ββββ[R] B) :
(f.comp g).comp h = f.comp (g.comp h) := rfl
@[simp] lemma id_comp (f : A ββββ[R] B) : (non_unital_star_alg_hom.id _ _).comp f = f :=
ext $ Ξ» _, rfl
@[simp] lemma comp_id (f : A ββββ[R] B) : f.comp (non_unital_star_alg_hom.id _ _) = f :=
ext $ Ξ» _, rfl
instance : monoid (A ββββ[R] A) :=
{ mul := comp,
mul_assoc := comp_assoc,
one := non_unital_star_alg_hom.id R A,
one_mul := id_comp,
mul_one := comp_id, }
@[simp] lemma coe_one : ((1 : A ββββ[R] A) : A β A) = id := rfl
lemma one_apply (a : A) : (1 : A ββββ[R] A) a = a := rfl
end basic
section zero
-- the `zero` requires extra type class assumptions because we need `star_zero`
variables {R A B C D : Type*} [monoid R]
variables [non_unital_non_assoc_semiring A] [distrib_mul_action R A] [star_add_monoid A]
variables [non_unital_non_assoc_semiring B] [distrib_mul_action R B] [star_add_monoid B]
instance : has_zero (A ββββ[R] B) :=
β¨{ map_star' := by simp, .. (0 : non_unital_alg_hom R A B) }β©
instance : inhabited (A ββββ[R] B) := β¨0β©
instance : monoid_with_zero (A ββββ[R] A) :=
{ zero_mul := Ξ» f, ext $ Ξ» x, rfl,
mul_zero := Ξ» f, ext $ Ξ» x, map_zero f,
.. non_unital_star_alg_hom.monoid,
.. non_unital_star_alg_hom.has_zero }
@[simp] lemma coe_zero : ((0 : A ββββ[R] B) : A β B) = 0 := rfl
lemma zero_apply (a : A) : (0 : A ββββ[R] B) a = 0 := rfl
end zero
end non_unital_star_alg_hom
/-! ### Unital star algebra homomorphisms -/
section unital
/-- A *β-algebra homomorphism* is an algebra homomorphism between `R`-algebras `A` and `B`
equipped with a `star` operation, and this homomorphism is also `star`-preserving. -/
structure star_alg_hom (R A B: Type*) [comm_semiring R] [semiring A] [algebra R A] [has_star A]
[semiring B] [algebra R B] [has_star B] extends alg_hom R A B :=
(map_star' : β x : A, to_fun (star x) = star (to_fun x))
infixr ` βββ `:25 := star_alg_hom _
notation A ` βββ[`:25 R `] ` B := star_alg_hom R A B
/-- Reinterpret a unital star algebra homomorphism as a unital algebra homomorphism
by forgetting the interaction with the star operation. -/
add_decl_doc star_alg_hom.to_alg_hom
/-- `star_alg_hom_class F R A B` states that `F` is a type of β-algebra homomorphisms.
You should also extend this typeclass when you extend `star_alg_hom`. -/
class star_alg_hom_class (F : Type*) (R : out_param Type*) (A : out_param Type*)
(B : out_param Type*) [comm_semiring R] [semiring A] [algebra R A] [has_star A]
[semiring B] [algebra R B] [has_star B] extends alg_hom_class F R A B, star_hom_class F A B
-- `R` becomes a metavariable but that's fine because it's an `out_param`
attribute [nolint dangerous_instance] star_alg_hom_class.to_star_hom_class
@[priority 100] /- See note [lower instance priority] -/
instance star_alg_hom_class.to_non_unital_star_alg_hom_class
(F R A B : Type*) [comm_semiring R] [semiring A] [algebra R A] [has_star A]
[semiring B] [algebra R B] [has_star B] [star_alg_hom_class F R A B] :
non_unital_star_alg_hom_class F R A B :=
{ map_smul := map_smul,
.. star_alg_hom_class.to_alg_hom_class F R A B,
.. star_alg_hom_class.to_star_hom_class F R A B, }
namespace star_alg_hom
variables {F R A B C D : Type*} [comm_semiring R]
[semiring A] [algebra R A] [has_star A]
[semiring B] [algebra R B] [has_star B]
[semiring C] [algebra R C] [has_star C]
[semiring D] [algebra R D] [has_star D]
instance : star_alg_hom_class (A βββ[R] B) R A B :=
{ coe := Ξ» f, f.to_fun,
coe_injective' := Ξ» f g h,
begin
obtain β¨_, _, _, _, _, _, _β© := f;
obtain β¨_, _, _, _, _, _, _β© := g;
congr'
end,
map_mul := map_mul',
map_one := map_one',
map_add := map_add',
map_zero := map_zero',
commutes := commutes',
map_star := map_star' }
/-- Helper instance for when there's too many metavariables to apply `fun_like.has_coe_to_fun`
directly. -/
instance : has_coe_to_fun (A βββ[R] B) (Ξ» _, A β B) := fun_like.has_coe_to_fun
@[simp] lemma coe_to_alg_hom {f : A βββ[R] B} :
(f.to_alg_hom : A β B) = f := rfl
@[ext] lemma ext {f g : A βββ[R] B} (h : β x, f x = g x) : f = g := fun_like.ext _ _ h
/-- Copy of a `star_alg_hom` with a new `to_fun` equal to the old one. Useful
to fix definitional equalities. -/
protected def copy (f : A βββ[R] B) (f' : A β B) (h : f' = f) : A βββ[R] B :=
{ to_fun := f',
map_one' := h.symm βΈ map_one f ,
map_mul' := h.symm βΈ map_mul f,
map_zero' := h.symm βΈ map_zero f,
map_add' := h.symm βΈ map_add f,
commutes' := h.symm βΈ alg_hom_class.commutes f,
map_star' := h.symm βΈ map_star f }
@[simp] lemma coe_mk (f : A β B) (hβ hβ hβ hβ hβ
hβ) :
((β¨f, hβ, hβ, hβ, hβ, hβ
, hββ© : A βββ[R] B) : A β B) = f :=
rfl
@[simp] lemma mk_coe (f : A βββ[R] B) (hβ hβ hβ hβ hβ
hβ) :
(β¨f, hβ, hβ, hβ, hβ, hβ
, hββ© : A βββ[R] B) = f :=
by { ext, refl, }
section
variables (R A)
/-- The identity as a `star_alg_hom`. -/
protected def id : A βββ[R] A := { map_star' := Ξ» x, rfl, .. alg_hom.id _ _ }
@[simp] lemma coe_id : β(star_alg_hom.id R A) = id := rfl
end
instance : inhabited (A βββ[R] A) := β¨star_alg_hom.id R Aβ©
/-- The composition of β-algebra homomorphisms, as a β-algebra homomorphism. -/
def comp (f : B βββ[R] C) (g : A βββ[R] B) : A βββ[R] C :=
{ map_star' := by simp only [map_star, alg_hom.to_fun_eq_coe, alg_hom.coe_comp, coe_to_alg_hom,
function.comp_app, eq_self_iff_true, forall_const],
.. f.to_alg_hom.comp g.to_alg_hom }
@[simp] lemma coe_comp (f : B βββ[R] C) (g : A βββ[R] B) : β(comp f g) = f β g := rfl
@[simp] lemma comp_apply (f : B βββ[R] C) (g : A βββ[R] B) (a : A) : comp f g a = f (g a) := rfl
@[simp] lemma comp_assoc (f : C βββ[R] D) (g : B βββ[R] C) (h : A βββ[R] B) :
(f.comp g).comp h = f.comp (g.comp h) := rfl
@[simp] lemma id_comp (f : A βββ[R] B) : (star_alg_hom.id _ _).comp f = f := ext $ Ξ» _, rfl
@[simp] lemma comp_id (f : A βββ[R] B) : f.comp (star_alg_hom.id _ _) = f := ext $ Ξ» _, rfl
instance : monoid (A βββ[R] A) :=
{ mul := comp,
mul_assoc := comp_assoc,
one := star_alg_hom.id R A,
one_mul := id_comp,
mul_one := comp_id }
/-- A unital morphism of β-algebras is a `non_unital_star_alg_hom`. -/
def to_non_unital_star_alg_hom (f : A βββ[R] B) : A ββββ[R] B :=
{ map_smul' := map_smul f, .. f, }
@[simp] lemma coe_to_non_unital_star_alg_hom (f : A βββ[R] B) :
(f.to_non_unital_star_alg_hom : A β B) = f :=
rfl
end star_alg_hom
end unital
/-! ### Operations on the product type
Note that this is copied from [`algebra/hom/non_unital_alg`](non_unital_alg). -/
namespace non_unital_star_alg_hom
section prod
variables (R A B C : Type*) [monoid R]
[non_unital_non_assoc_semiring A] [distrib_mul_action R A] [has_star A]
[non_unital_non_assoc_semiring B] [distrib_mul_action R B] [has_star B]
[non_unital_non_assoc_semiring C] [distrib_mul_action R C] [has_star C]
/-- The first projection of a product is a non-unital β-algebra homomoprhism. -/
@[simps]
def fst : A Γ B ββββ[R] A :=
{ map_star' := Ξ» x, rfl, .. non_unital_alg_hom.fst R A B }
/-- The second projection of a product is a non-unital β-algebra homomorphism. -/
@[simps]
def snd : A Γ B ββββ[R] B :=
{ map_star' := Ξ» x, rfl, .. non_unital_alg_hom.snd R A B }
variables {R A B C}
/-- The `pi.prod` of two morphisms is a morphism. -/
@[simps] def prod (f : A ββββ[R] B) (g : A ββββ[R] C) : (A ββββ[R] B Γ C) :=
{ map_star' := Ξ» x, by simp [map_star, prod.star_def],
.. f.to_non_unital_alg_hom.prod g.to_non_unital_alg_hom }
lemma coe_prod (f : A ββββ[R] B) (g : A ββββ[R] C) : β(f.prod g) = pi.prod f g := rfl
@[simp] theorem fst_prod (f : A ββββ[R] B) (g : A ββββ[R] C) :
(fst R B C).comp (prod f g) = f := by ext; refl
@[simp] theorem snd_prod (f : A ββββ[R] B) (g : A ββββ[R] C) :
(snd R B C).comp (prod f g) = g := by ext; refl
@[simp] theorem prod_fst_snd : prod (fst R A B) (snd R A B) = 1 :=
fun_like.coe_injective pi.prod_fst_snd
/-- Taking the product of two maps with the same domain is equivalent to taking the product of
their codomains. -/
@[simps] def prod_equiv : ((A ββββ[R] B) Γ (A ββββ[R] C)) β (A ββββ[R] B Γ C) :=
{ to_fun := Ξ» f, f.1.prod f.2,
inv_fun := Ξ» f, ((fst _ _ _).comp f, (snd _ _ _).comp f),
left_inv := Ξ» f, by ext; refl,
right_inv := Ξ» f, by ext; refl }
end prod
section inl_inr
variables (R A B C : Type*) [monoid R]
[non_unital_non_assoc_semiring A] [distrib_mul_action R A] [star_add_monoid A]
[non_unital_non_assoc_semiring B] [distrib_mul_action R B] [star_add_monoid B]
[non_unital_non_assoc_semiring C] [distrib_mul_action R C] [star_add_monoid C]
/-- The left injection into a product is a non-unital algebra homomorphism. -/
def inl : A ββββ[R] A Γ B := prod 1 0
/-- The right injection into a product is a non-unital algebra homomorphism. -/
def inr : B ββββ[R] A Γ B := prod 0 1
variables {R A B}
@[simp] theorem coe_inl : (inl R A B : A β A Γ B) = Ξ» x, (x, 0) := rfl
theorem inl_apply (x : A) : inl R A B x = (x, 0) := rfl
@[simp] theorem coe_inr : (inr R A B : B β A Γ B) = prod.mk 0 := rfl
theorem inr_apply (x : B) : inr R A B x = (0, x) := rfl
end inl_inr
end non_unital_star_alg_hom
namespace star_alg_hom
variables (R A B C : Type*) [comm_semiring R]
[semiring A] [algebra R A] [has_star A]
[semiring B] [algebra R B] [has_star B]
[semiring C] [algebra R C] [has_star C]
/-- The first projection of a product is a β-algebra homomoprhism. -/
@[simps]
def fst : A Γ B βββ[R] A :=
{ map_star' := Ξ» x, rfl, .. alg_hom.fst R A B }
/-- The second projection of a product is a β-algebra homomorphism. -/
@[simps]
def snd : A Γ B βββ[R] B :=
{ map_star' := Ξ» x, rfl, .. alg_hom.snd R A B }
variables {R A B C}
/-- The `pi.prod` of two morphisms is a morphism. -/
@[simps] def prod (f : A βββ[R] B) (g : A βββ[R] C) : (A βββ[R] B Γ C) :=
{ map_star' := Ξ» x, by simp [prod.star_def, map_star],
.. f.to_alg_hom.prod g.to_alg_hom }
lemma coe_prod (f : A βββ[R] B) (g : A βββ[R] C) : β(f.prod g) = pi.prod f g := rfl
@[simp] theorem fst_prod (f : A βββ[R] B) (g : A βββ[R] C) :
(fst R B C).comp (prod f g) = f := by ext; refl
@[simp] theorem snd_prod (f : A βββ[R] B) (g : A βββ[R] C) :
(snd R B C).comp (prod f g) = g := by ext; refl
@[simp] theorem prod_fst_snd : prod (fst R A B) (snd R A B) = 1 :=
fun_like.coe_injective pi.prod_fst_snd
/-- Taking the product of two maps with the same domain is equivalent to taking the product of
their codomains. -/
@[simps] def prod_equiv : ((A βββ[R] B) Γ (A βββ[R] C)) β (A βββ[R] B Γ C) :=
{ to_fun := Ξ» f, f.1.prod f.2,
inv_fun := Ξ» f, ((fst _ _ _).comp f, (snd _ _ _).comp f),
left_inv := Ξ» f, by ext; refl,
right_inv := Ξ» f, by ext; refl }
end star_alg_hom
|
a1f2c6ded5581073b08720ea03678eb86b7885eb | 80cc5bf14c8ea85ff340d1d747a127dcadeb966f | /src/order/galois_connection.lean | 1cf73b9b814218da63cb17034f1d375be6a5fa8e | [
"Apache-2.0"
] | permissive | lacker/mathlib | f2439c743c4f8eb413ec589430c82d0f73b2d539 | ddf7563ac69d42cfa4a1bfe41db1fed521bd795f | refs/heads/master | 1,671,948,326,773 | 1,601,479,268,000 | 1,601,479,268,000 | 298,686,743 | 0 | 0 | Apache-2.0 | 1,601,070,794,000 | 1,601,070,794,000 | null | UTF-8 | Lean | false | false | 24,886 | 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 order.complete_lattice
import order.rel_iso
/-!
# Galois connections, insertions and coinsertions
Galois connections are order theoretic adjoints, i.e. a pair of functions `u` and `l`,
such that `βa b, l a β€ b β a β€ u b`.
## Main definitions
* `galois_connection`: A Galois connection is a pair of functions `l` and `u` satisfying
`l a β€ b β a β€ u b`. They are special cases of adjoint functors in category theory,
but do not depend on the category theory library in mathlib.
* `galois_insertion`: A Galois insertion is a Galois connection where `l β u = id`
* `galois_coinsertion`: A Galois coinsertion is a Galois connection where `u β l = id`
## Implementation details
Galois insertions can be used to lift order structures from one type to another.
For example if `Ξ±` is a complete lattice, and `l : Ξ± β Ξ²`, and `u : Ξ² β Ξ±` form
a Galois insertion, then `Ξ²` is also a complete lattice. `l` is the lower adjoint and
`u` is the upper adjoint.
An example of this is the Galois insertion is in group thery. If `G` is a topological space,
then there is a Galois insertion between the set of subsets of `G`, `set G`, and the
set of subgroups of `G`, `subgroup G`. The left adjoint is `subgroup.closure`,
taking the `subgroup` generated by a `set`, The right adjoint is the coercion from `subgroup G` to
`set G`, taking the underlying set of an subgroup.
Naively lifting a lattice structure along this Galois insertion would mean that the definition
of `inf` on subgroups would be `subgroup.closure (βS β© βT)`. This is an undesirable definition
because the intersection of subgroups is already a subgroup, so there is no need to take the
closure. For this reason a `choice` function is added as a field to the `galois_insertion`
structure. It has type `Ξ S : set G, β(closure S) β€ S β subgroup G`. When `β(closure S) β€ S`, then
`S` is already a subgroup, so this function can be defined using `subgroup.mk` and not `closure`.
This means the infimum of subgroups will be defined to be the intersection of sets, paired
with a proof that intersection of subgroups is a subgroup, rather than the closure of the
intersection.
-/
open function set
universes u v w x
variables {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} {ΞΉ : Sort x} {a aβ aβ : Ξ±} {b bβ bβ : Ξ²}
/-- A Galois connection is a pair of functions `l` and `u` satisfying
`l a β€ b β a β€ u b`. They are special cases of adjoint functors in category theory,
but do not depend on the category theory library in mathlib. -/
def galois_connection [preorder Ξ±] [preorder Ξ²] (l : Ξ± β Ξ²) (u : Ξ² β Ξ±) := βa b, l a β€ b β a β€ u b
/-- Makes a Galois connection from an order-preserving bijection. -/
theorem order_iso.to_galois_connection [preorder Ξ±] [preorder Ξ²] (oi : Ξ± βo Ξ²) :
galois_connection oi oi.symm :=
Ξ» b g, by rw [oi.map_rel_iff, rel_iso.apply_symm_apply]
namespace galois_connection
section
variables [preorder Ξ±] [preorder Ξ²] {l : Ξ± β Ξ²} {u : Ξ² β Ξ±} (gc : galois_connection l u)
lemma monotone_intro (hu : monotone u) (hl : monotone l)
(hul : β a, a β€ u (l a)) (hlu : β a, l (u a) β€ a) : galois_connection l u :=
assume a b, β¨assume h, le_trans (hul _) (hu h), assume h, le_trans (hl h) (hlu _)β©
include gc
lemma l_le {a : Ξ±} {b : Ξ²} : a β€ u b β l a β€ b :=
(gc _ _).mpr
lemma le_u {a : Ξ±} {b : Ξ²} : l a β€ b β a β€ u b :=
(gc _ _).mp
lemma le_u_l (a) : a β€ u (l a) :=
gc.le_u $ le_refl _
lemma l_u_le (a) : l (u a) β€ a :=
gc.l_le $ le_refl _
lemma monotone_u : monotone u :=
assume a b H, gc.le_u (le_trans (gc.l_u_le a) H)
lemma monotone_l : monotone l :=
assume a b H, gc.l_le (le_trans H (gc.le_u_l b))
lemma upper_bounds_l_image_subset {s : set Ξ±} : upper_bounds (l '' s) β u β»ΒΉ' upper_bounds s :=
assume b hb c, assume : c β s, gc.le_u (hb (mem_image_of_mem _ βΉc β sβΊ))
lemma lower_bounds_u_image_subset {s : set Ξ²} : lower_bounds (u '' s) β l β»ΒΉ' lower_bounds s :=
assume a ha c, assume : c β s, gc.l_le (ha (mem_image_of_mem _ βΉc β sβΊ))
lemma is_lub_l_image {s : set Ξ±} {a : Ξ±} (h : is_lub s a) : is_lub (l '' s) (l a) :=
β¨gc.monotone_l.mem_upper_bounds_image $ and.elim_left βΉis_lub s aβΊ,
assume b hb, gc.l_le $ and.elim_right βΉis_lub s aβΊ $ gc.upper_bounds_l_image_subset hbβ©
lemma is_glb_u_image {s : set Ξ²} {b : Ξ²} (h : is_glb s b) : is_glb (u '' s) (u b) :=
β¨gc.monotone_u.mem_lower_bounds_image $ and.elim_left βΉis_glb s bβΊ,
assume a ha, gc.le_u $ and.elim_right βΉis_glb s bβΊ $ gc.lower_bounds_u_image_subset haβ©
lemma is_glb_l {a : Ξ±} : is_glb { b | a β€ u b } (l a) :=
β¨assume b, gc.l_le, assume b h, h $ gc.le_u_l _β©
lemma is_lub_u {b : Ξ²} : is_lub { a | l a β€ b } (u b) :=
β¨assume b, gc.le_u, assume b h, h $ gc.l_u_le _β©
end
section partial_order
variables [partial_order Ξ±] [partial_order Ξ²] {l : Ξ± β Ξ²} {u : Ξ² β Ξ±} (gc : galois_connection l u)
include gc
lemma u_l_u_eq_u : u β l β u = u :=
funext (assume x, le_antisymm (gc.monotone_u (gc.l_u_le _)) (gc.le_u_l _))
lemma l_u_l_eq_l : l β u β l = l :=
funext (assume x, le_antisymm (gc.l_u_le _) (gc.monotone_l (gc.le_u_l _)))
lemma l_unique {l' : Ξ± β Ξ²} {u' : Ξ² β Ξ±} (gc' : galois_connection l' u')
(hu : β b, u b = u' b) {a : Ξ±} : l a = l' a :=
le_antisymm (gc.l_le $ (hu (l' a)).symm βΈ gc'.le_u_l _)
(gc'.l_le $ hu (l a) βΈ gc.le_u_l _)
lemma u_unique {l' : Ξ± β Ξ²} {u' : Ξ² β Ξ±} (gc' : galois_connection l' u')
(hl : β a, l a = l' a) {b : Ξ²} : u b = u' b :=
le_antisymm (gc'.le_u $ hl (u b) βΈ gc.l_u_le _)
(gc.le_u $ (hl (u' b)).symm βΈ gc'.l_u_le _)
end partial_order
section order_top
variables [order_top Ξ±] [order_top Ξ²] {l : Ξ± β Ξ²} {u : Ξ² β Ξ±} (gc : galois_connection l u)
include gc
lemma u_top : u β€ = β€ :=
(gc.is_glb_u_image is_glb_empty).unique $ by simp only [is_glb_empty, image_empty]
end order_top
section order_bot
variables [order_bot Ξ±] [order_bot Ξ²] {l : Ξ± β Ξ²} {u : Ξ² β Ξ±} (gc : galois_connection l u)
include gc
lemma l_bot : l β₯ = β₯ :=
(gc.is_lub_l_image is_lub_empty).unique $ by simp only [is_lub_empty, image_empty]
end order_bot
section semilattice_sup
variables [semilattice_sup Ξ±] [semilattice_sup Ξ²] {l : Ξ± β Ξ²} {u : Ξ² β Ξ±} (gc : galois_connection l u)
include gc
lemma l_sup : l (aβ β aβ) = l aβ β l aβ :=
(gc.is_lub_l_image is_lub_pair).unique $ by simp only [image_pair, is_lub_pair]
end semilattice_sup
section semilattice_inf
variables [semilattice_inf Ξ±] [semilattice_inf Ξ²] {l : Ξ± β Ξ²} {u : Ξ² β Ξ±} (gc : galois_connection l u)
include gc
lemma u_inf : u (bβ β bβ) = u bβ β u bβ :=
(gc.is_glb_u_image is_glb_pair).unique $ by simp only [image_pair, is_glb_pair]
end semilattice_inf
section complete_lattice
variables [complete_lattice Ξ±] [complete_lattice Ξ²] {l : Ξ± β Ξ²} {u : Ξ² β Ξ±} (gc : galois_connection l u)
include gc
lemma l_supr {f : ΞΉ β Ξ±} : l (supr f) = (β¨i, l (f i)) :=
eq.symm $ is_lub.supr_eq $ show is_lub (range (l β f)) (l (supr f)),
by rw [range_comp, β Sup_range]; exact gc.is_lub_l_image (is_lub_Sup _)
lemma u_infi {f : ΞΉ β Ξ²} : u (infi f) = (β¨
i, u (f i)) :=
eq.symm $ is_glb.infi_eq $ show is_glb (range (u β f)) (u (infi f)),
by rw [range_comp, β Inf_range]; exact gc.is_glb_u_image (is_glb_Inf _)
lemma l_Sup {s : set Ξ±} : l (Sup s) = (β¨aβs, l a) :=
by simp only [Sup_eq_supr, gc.l_supr]
lemma u_Inf {s : set Ξ²} : u (Inf s) = (β¨
aβs, u a) :=
by simp only [Inf_eq_infi, gc.u_infi]
end complete_lattice
/- Constructing Galois connections -/
section constructions
protected lemma id [pΞ± : preorder Ξ±] : @galois_connection Ξ± Ξ± pΞ± pΞ± id id :=
assume a b, iff.intro (Ξ»x, x) (Ξ»x, x)
protected lemma compose [preorder Ξ±] [preorder Ξ²] [preorder Ξ³]
(l1 : Ξ± β Ξ²) (u1 : Ξ² β Ξ±) (l2 : Ξ² β Ξ³) (u2 : Ξ³ β Ξ²)
(gc1 : galois_connection l1 u1) (gc2 : galois_connection l2 u2) :
galois_connection (l2 β l1) (u1 β u2) :=
by intros a b; rw [gc2, gc1]
protected lemma dual [pΞ± : preorder Ξ±] [pΞ² : preorder Ξ²]
{l : Ξ± β Ξ²} {u : Ξ² β Ξ±} (gc : galois_connection l u) :
@galois_connection (order_dual Ξ²) (order_dual Ξ±) _ _ u l :=
assume a b, (gc _ _).symm
protected lemma dfun {ΞΉ : Type u} {Ξ± : ΞΉ β Type v} {Ξ² : ΞΉ β Type w}
[βi, preorder (Ξ± i)] [βi, preorder (Ξ² i)]
(l : Ξ i, Ξ± i β Ξ² i) (u : Ξ i, Ξ² i β Ξ± i) (gc : βi, galois_connection (l i) (u i)) :
@galois_connection (Ξ i, Ξ± i) (Ξ i, Ξ² i) _ _ (Ξ»a i, l i (a i)) (Ξ»b i, u i (b i)) :=
assume a b, forall_congr $ assume i, gc i (a i) (b i)
end constructions
end galois_connection
namespace nat
lemma galois_connection_mul_div {k : β} (h : 0 < k) : galois_connection (Ξ»n, n * k) (Ξ»n, n / k) :=
assume x y, (le_div_iff_mul_le x y h).symm
end nat
/-- A Galois insertion is a Galois connection where `l β u = id`. It also contains a constructive
choice function, to give better definitional equalities when lifting order structures. Dual
to `galois_coinsertion` -/
@[nolint has_inhabited_instance]
structure galois_insertion {Ξ± Ξ² : Type*} [preorder Ξ±] [preorder Ξ²] (l : Ξ± β Ξ²) (u : Ξ² β Ξ±) :=
(choice : Ξ x:Ξ±, u (l x) β€ x β Ξ²)
(gc : galois_connection l u)
(le_l_u : βx, x β€ l (u x))
(choice_eq : βa h, choice a h = l a)
/-- A constructor for a Galois insertion with the trivial `choice` function. -/
def galois_insertion.monotone_intro {Ξ± Ξ² : Type*} [preorder Ξ±] [preorder Ξ²] {l : Ξ± β Ξ²} {u : Ξ² β Ξ±}
(hu : monotone u) (hl : monotone l) (hul : β a, a β€ u (l a)) (hlu : β b, l (u b) = b) :
galois_insertion l u :=
{ choice := Ξ» x _, l x,
gc := galois_connection.monotone_intro hu hl hul (Ξ» b, le_of_eq (hlu b)),
le_l_u := Ξ» b, le_of_eq $ (hlu b).symm,
choice_eq := Ξ» _ _, rfl }
/-- Makes a Galois insertion from an order-preserving bijection. -/
protected def rel_iso.to_galois_insertion [preorder Ξ±] [preorder Ξ²] (oi : Ξ± βo Ξ²) :
@galois_insertion Ξ± Ξ² _ _ (oi) (oi.symm) :=
{ choice := Ξ» b h, oi b,
gc := oi.to_galois_connection,
le_l_u := Ξ» g, le_of_eq (oi.right_inv g).symm,
choice_eq := Ξ» b h, rfl }
/-- Make a `galois_insertion l u` from a `galois_connection l u` such that `β b, b β€ l (u b)` -/
def galois_connection.to_galois_insertion {Ξ± Ξ² : Type*} [preorder Ξ±] [preorder Ξ²]
{l : Ξ± β Ξ²} {u : Ξ² β Ξ±} (gc : galois_connection l u) (h : β b, b β€ l (u b)) :
galois_insertion l u :=
{ choice := Ξ» x _, l x,
gc := gc,
le_l_u := h,
choice_eq := Ξ» _ _, rfl }
/-- Lift the bottom along a Galois connection -/
def galois_connection.lift_order_bot {Ξ± Ξ² : Type*} [order_bot Ξ±] [partial_order Ξ²]
{l : Ξ± β Ξ²} {u : Ξ² β Ξ±} (gc : galois_connection l u) :
order_bot Ξ² :=
{ bot := l β₯,
bot_le := assume b, gc.l_le $ bot_le,
.. βΉpartial_order Ξ²βΊ }
namespace galois_insertion
variables {l : Ξ± β Ξ²} {u : Ξ² β Ξ±}
lemma l_u_eq [preorder Ξ±] [partial_order Ξ²] (gi : galois_insertion l u) (b : Ξ²) :
l (u b) = b :=
le_antisymm (gi.gc.l_u_le _) (gi.le_l_u _)
lemma l_surjective [preorder Ξ±] [partial_order Ξ²] (gi : galois_insertion l u) :
surjective l :=
assume b, β¨u b, gi.l_u_eq bβ©
lemma u_injective [preorder Ξ±] [partial_order Ξ²] (gi : galois_insertion l u) :
injective u :=
assume a b h,
calc a = l (u a) : (gi.l_u_eq a).symm
... = l (u b) : congr_arg l h
... = b : gi.l_u_eq b
lemma l_sup_u [semilattice_sup Ξ±] [semilattice_sup Ξ²] (gi : galois_insertion l u) (a b : Ξ²) :
l (u a β u b) = a β b :=
calc l (u a β u b) = l (u a) β l (u b) : gi.gc.l_sup
... = a β b : by simp only [gi.l_u_eq]
lemma l_supr_u [complete_lattice Ξ±] [complete_lattice Ξ²] (gi : galois_insertion l u)
{ΞΉ : Sort x} (f : ΞΉ β Ξ²) :
l (β¨ i, u (f i)) = β¨ i, (f i) :=
calc l (β¨ (i : ΞΉ), u (f i)) = β¨ (i : ΞΉ), l (u (f i)) : gi.gc.l_supr
... = β¨ (i : ΞΉ), f i : congr_arg _ $ funext $ Ξ» i, gi.l_u_eq (f i)
lemma l_supr_of_ul_eq_self [complete_lattice Ξ±] [complete_lattice Ξ²] (gi : galois_insertion l u)
{ΞΉ : Sort x} (f : ΞΉ β Ξ±) (hf : β i, u (l (f i)) = f i) :
l (β¨ i, (f i)) = β¨ i, l (f i) :=
calc l (β¨ (i : ΞΉ), (f i)) = l β¨ (i : ΞΉ), (u (l (f i))) : by simp [hf]
... = β¨ (i : ΞΉ), l (f i) : gi.l_supr_u _
lemma l_inf_u [semilattice_inf Ξ±] [semilattice_inf Ξ²] (gi : galois_insertion l u) (a b : Ξ²) :
l (u a β u b) = a β b :=
calc l (u a β u b) = l (u (a β b)) : congr_arg l gi.gc.u_inf.symm
... = a β b : by simp only [gi.l_u_eq]
lemma l_infi_u [complete_lattice Ξ±] [complete_lattice Ξ²] (gi : galois_insertion l u)
{ΞΉ : Sort x} (f : ΞΉ β Ξ²) :
l (β¨
i, u (f i)) = β¨
i, (f i) :=
calc l (β¨
(i : ΞΉ), u (f i)) = l (u (β¨
(i : ΞΉ), (f i))) : congr_arg l gi.gc.u_infi.symm
... = β¨
(i : ΞΉ), f i : gi.l_u_eq _
lemma l_infi_of_ul_eq_self [complete_lattice Ξ±] [complete_lattice Ξ²] (gi : galois_insertion l u)
{ΞΉ : Sort x} (f : ΞΉ β Ξ±) (hf : β i, u (l (f i)) = f i) :
l (β¨
i, (f i)) = β¨
i, l (f i) :=
calc l (β¨
i, (f i)) = l β¨
(i : ΞΉ), (u (l (f i))) : by simp [hf]
... = β¨
i, l (f i) : gi.l_infi_u _
lemma u_le_u_iff [preorder Ξ±] [preorder Ξ²] (gi : galois_insertion l u) {a b} :
u a β€ u b β a β€ b :=
β¨Ξ» h, le_trans (gi.le_l_u _) (gi.gc.l_le h),
Ξ» h, gi.gc.monotone_u hβ©
lemma strict_mono_u [preorder Ξ±] [partial_order Ξ²] (gi : galois_insertion l u) : strict_mono u :=
strict_mono_of_le_iff_le $ Ξ» _ _, gi.u_le_u_iff.symm
section lift
variables [partial_order Ξ²]
/-- Lift the suprema along a Galois insertion -/
def lift_semilattice_sup [semilattice_sup Ξ±] (gi : galois_insertion l u) : semilattice_sup Ξ² :=
{ sup := Ξ»a b, l (u a β u b),
le_sup_left := assume a b, le_trans (gi.le_l_u a) $ gi.gc.monotone_l $ le_sup_left,
le_sup_right := assume a b, le_trans (gi.le_l_u b) $ gi.gc.monotone_l $ le_sup_right,
sup_le := assume a b c hac hbc, gi.gc.l_le $ sup_le (gi.gc.monotone_u hac) (gi.gc.monotone_u hbc),
.. βΉpartial_order Ξ²βΊ }
/-- Lift the infima along a Galois insertion -/
def lift_semilattice_inf [semilattice_inf Ξ±] (gi : galois_insertion l u) : semilattice_inf Ξ² :=
{ inf := Ξ»a b, gi.choice (u a β u b) $
(le_inf (gi.gc.monotone_u $ gi.gc.l_le $ inf_le_left) (gi.gc.monotone_u $ gi.gc.l_le $ inf_le_right)),
inf_le_left := by simp only [gi.choice_eq]; exact assume a b, gi.gc.l_le inf_le_left,
inf_le_right := by simp only [gi.choice_eq]; exact assume a b, gi.gc.l_le inf_le_right,
le_inf := by simp only [gi.choice_eq]; exact assume a b c hac hbc,
le_trans (gi.le_l_u a) $ gi.gc.monotone_l $ le_inf (gi.gc.monotone_u hac) (gi.gc.monotone_u hbc),
.. βΉpartial_order Ξ²βΊ }
/-- Lift the suprema and infima along a Galois insertion -/
def lift_lattice [lattice Ξ±] (gi : galois_insertion l u) : lattice Ξ² :=
{ .. gi.lift_semilattice_sup, .. gi.lift_semilattice_inf }
/-- Lift the top along a Galois insertion -/
def lift_order_top [order_top Ξ±] (gi : galois_insertion l u) : order_top Ξ² :=
{ top := gi.choice β€ $ le_top,
le_top := by simp only [gi.choice_eq]; exact assume b, le_trans (gi.le_l_u b) (gi.gc.monotone_l le_top),
.. βΉpartial_order Ξ²βΊ }
/-- Lift the top, bottom, suprema, and infima along a Galois insertion -/
def lift_bounded_lattice [bounded_lattice Ξ±] (gi : galois_insertion l u) : bounded_lattice Ξ² :=
{ .. gi.lift_lattice, .. gi.lift_order_top, .. gi.gc.lift_order_bot }
/-- Lift all suprema and infima along a Galois insertion -/
def lift_complete_lattice [complete_lattice Ξ±] (gi : galois_insertion l u) : complete_lattice Ξ² :=
{ Sup := Ξ»s, l (β¨ bβs, u b),
Sup_le := assume s a hs, gi.gc.l_le $ supr_le $ assume b, supr_le $ assume hb, gi.gc.monotone_u $ hs _ hb,
le_Sup := assume s a ha, le_trans (gi.le_l_u a) $ gi.gc.monotone_l $ le_supr_of_le a $ le_supr_of_le ha $ le_refl _,
Inf := Ξ»s, gi.choice (β¨
bβs, u b) $ le_infi $ assume b, le_infi $ assume hb,
gi.gc.monotone_u $ gi.gc.l_le $ infi_le_of_le b $ infi_le_of_le hb $ le_refl _,
Inf_le := by simp only [gi.choice_eq]; exact
assume s a ha, gi.gc.l_le $ infi_le_of_le a $ infi_le_of_le ha $ le_refl _,
le_Inf := by simp only [gi.choice_eq]; exact
assume s a hs, le_trans (gi.le_l_u a) $ gi.gc.monotone_l $ le_infi $ assume b,
show u a β€ β¨
(H : b β s), u b, from le_infi $ assume hb, gi.gc.monotone_u $ hs _ hb,
.. gi.lift_bounded_lattice }
end lift
end galois_insertion
/-- A Galois coinsertion is a Galois connection where `u β l = id`. It also contains a constructive
choice function, to give better definitional equalities when lifting order structures. Dual to
`galois_insertion` -/
@[nolint has_inhabited_instance]
structure galois_coinsertion {Ξ± Ξ² : Type*} [preorder Ξ±] [preorder Ξ²] (l : Ξ± β Ξ²) (u : Ξ² β Ξ±) :=
(choice : Ξ x:Ξ², x β€ l (u x) β Ξ±)
(gc : galois_connection l u)
(u_l_le : βx, u (l x) β€ x)
(choice_eq : βa h, choice a h = u a)
/-- Make a `galois_insertion u l` in the `order_dual`, from a `galois_coinsertion l u` -/
def galois_coinsertion.dual {Ξ± Ξ² : Type*} [preorder Ξ±] [preorder Ξ²] {l : Ξ± β Ξ²} {u : Ξ² β Ξ±} :
galois_coinsertion l u β @galois_insertion (order_dual Ξ²) (order_dual Ξ±) _ _ u l :=
Ξ» x, β¨x.1, x.2.dual, x.3, x.4β©
/-- Make a `galois_coinsertion u l` in the `order_dual`, from a `galois_insertion l u` -/
def galois_insertion.dual {Ξ± Ξ² : Type*} [preorder Ξ±] [preorder Ξ²] {l : Ξ± β Ξ²} {u : Ξ² β Ξ±} :
galois_insertion l u β @galois_coinsertion (order_dual Ξ²) (order_dual Ξ±) _ _ u l :=
Ξ» x, β¨x.1, x.2.dual, x.3, x.4β©
/-- Make a `galois_coinsertion l u` from a `galois_insertion l u` in the `order_dual` -/
def galois_coinsertion.of_dual {Ξ± Ξ² : Type*} [preorder Ξ±] [preorder Ξ²] {l : Ξ± β Ξ²} {u : Ξ² β Ξ±} :
@galois_insertion (order_dual Ξ²) (order_dual Ξ±) _ _ u l β galois_coinsertion l u :=
Ξ» x, β¨x.1, x.2.dual, x.3, x.4β©
/-- Make a `galois_insertion l u` from a `galois_coinsertion l u` in the `order_dual` -/
def galois_insertion.of_dual {Ξ± Ξ² : Type*} [preorder Ξ±] [preorder Ξ²] {l : Ξ± β Ξ²} {u : Ξ² β Ξ±} :
@galois_coinsertion (order_dual Ξ²) (order_dual Ξ±) _ _ u l β galois_insertion l u :=
Ξ» x, β¨x.1, x.2.dual, x.3, x.4β©
/-- Makes a Galois coinsertion from an order-preserving bijection. -/
protected def rel_iso.to_galois_coinsertion [preorder Ξ±] [preorder Ξ²] (oi : Ξ± βo Ξ²) :
@galois_coinsertion Ξ± Ξ² _ _ (oi) (oi.symm) :=
{ choice := Ξ» b h, oi.symm b,
gc := oi.to_galois_connection,
u_l_le := Ξ» g, le_of_eq (oi.left_inv g),
choice_eq := Ξ» b h, rfl }
/-- A constructor for a Galois coinsertion with the trivial `choice` function. -/
def galois_coinsertion.monotone_intro {Ξ± Ξ² : Type*} [preorder Ξ±] [preorder Ξ²] {l : Ξ± β Ξ²} {u : Ξ² β Ξ±}
(hu : monotone u) (hl : monotone l) (hlu : β b, l (u b) β€ b) (hul : β a, u (l a) = a) :
galois_coinsertion l u :=
galois_coinsertion.of_dual (galois_insertion.monotone_intro hl.order_dual hu.order_dual hlu hul)
/-- Make a `galois_coinsertion l u` from a `galois_connection l u` such that `β b, b β€ l (u b)` -/
def galois_connection.to_galois_coinsertion {Ξ± Ξ² : Type*} [preorder Ξ±] [preorder Ξ²]
{l : Ξ± β Ξ²} {u : Ξ² β Ξ±} (gc : galois_connection l u) (h : β a, u (l a) β€ a) :
galois_coinsertion l u :=
{ choice := Ξ» x _, u x,
gc := gc,
u_l_le := h,
choice_eq := Ξ» _ _, rfl }
/-- Lift the top along a Galois connection -/
def galois_connection.lift_order_top {Ξ± Ξ² : Type*} [partial_order Ξ±] [order_top Ξ²]
{l : Ξ± β Ξ²} {u : Ξ² β Ξ±} (gc : galois_connection l u) :
order_top Ξ± :=
{ top := u β€,
le_top := assume b, gc.le_u $ le_top,
.. βΉpartial_order Ξ±βΊ }
namespace galois_coinsertion
variables {l : Ξ± β Ξ²} {u : Ξ² β Ξ±}
lemma u_l_eq [partial_order Ξ±] [preorder Ξ²] (gi : galois_coinsertion l u) (a : Ξ±) :
u (l a) = a :=
gi.dual.l_u_eq a
lemma u_surjective [partial_order Ξ±] [preorder Ξ²] (gi : galois_coinsertion l u) :
surjective u :=
gi.dual.l_surjective
lemma l_injective [partial_order Ξ±] [preorder Ξ²] (gi : galois_coinsertion l u) :
injective l :=
gi.dual.u_injective
lemma u_inf_l [semilattice_inf Ξ±] [semilattice_inf Ξ²] (gi : galois_coinsertion l u) (a b : Ξ±) :
u (l a β l b) = a β b :=
gi.dual.l_sup_u a b
lemma u_infi_l [complete_lattice Ξ±] [complete_lattice Ξ²] (gi : galois_coinsertion l u)
{ΞΉ : Sort x} (f : ΞΉ β Ξ±) :
u (β¨
i, l (f i)) = β¨
i, (f i) :=
gi.dual.l_supr_u _
lemma u_infi_of_lu_eq_self [complete_lattice Ξ±] [complete_lattice Ξ²] (gi : galois_coinsertion l u)
{ΞΉ : Sort x} (f : ΞΉ β Ξ²) (hf : β i, l (u (f i)) = f i) :
u (β¨
i, (f i)) = β¨
i, u (f i) :=
gi.dual.l_supr_of_ul_eq_self _ hf
lemma u_sup_l [semilattice_sup Ξ±] [semilattice_sup Ξ²] (gi : galois_coinsertion l u) (a b : Ξ±) :
u (l a β l b) = a β b :=
gi.dual.l_inf_u _ _
lemma u_supr_l [complete_lattice Ξ±] [complete_lattice Ξ²] (gi : galois_coinsertion l u)
{ΞΉ : Sort x} (f : ΞΉ β Ξ±) :
u (β¨ i, l (f i)) = β¨ i, (f i) :=
gi.dual.l_infi_u _
lemma u_supr_of_lu_eq_self [complete_lattice Ξ±] [complete_lattice Ξ²] (gi : galois_coinsertion l u)
{ΞΉ : Sort x} (f : ΞΉ β Ξ²) (hf : β i, l (u (f i)) = f i) :
u (β¨ i, (f i)) = β¨ i, u (f i) :=
gi.dual.l_infi_of_ul_eq_self _ hf
lemma l_le_l_iff [preorder Ξ±] [preorder Ξ²] (gi : galois_coinsertion l u) {a b} :
l a β€ l b β a β€ b :=
gi.dual.u_le_u_iff
lemma strict_mono_l [partial_order Ξ±] [preorder Ξ²] (gi : galois_coinsertion l u) : strict_mono l :=
Ξ» a b h, gi.dual.strict_mono_u h
section lift
variables [partial_order Ξ±]
/-- Lift the infima along a Galois coinsertion -/
def lift_semilattice_inf [semilattice_inf Ξ²] (gi : galois_coinsertion l u) : semilattice_inf Ξ± :=
{ inf := Ξ»a b, u (l a β l b),
inf_le_left := assume a b, le_trans (gi.gc.monotone_u $ inf_le_left) (gi.u_l_le a),
inf_le_right := assume a b, le_trans (gi.gc.monotone_u $ inf_le_right) (gi.u_l_le b),
le_inf := assume a b c hac hbc, gi.gc.le_u $ le_inf (gi.gc.monotone_l hac) (gi.gc.monotone_l hbc),
.. βΉpartial_order Ξ±βΊ }
/-- Lift the suprema along a Galois coinsertion -/
def lift_semilattice_sup [semilattice_sup Ξ²] (gi : galois_coinsertion l u) : semilattice_sup Ξ± :=
{ sup := Ξ»a b, gi.choice (l a β l b) $
(sup_le (gi.gc.monotone_l $ gi.gc.le_u $ le_sup_left) (gi.gc.monotone_l $ gi.gc.le_u $ le_sup_right)),
le_sup_left := by simp only [gi.choice_eq]; exact assume a b, gi.gc.le_u le_sup_left,
le_sup_right := by simp only [gi.choice_eq]; exact assume a b, gi.gc.le_u le_sup_right,
sup_le := by simp only [gi.choice_eq]; exact assume a b c hac hbc,
le_trans (gi.gc.monotone_u $ sup_le (gi.gc.monotone_l hac) (gi.gc.monotone_l hbc)) (gi.u_l_le c),
.. βΉpartial_order Ξ±βΊ }
/-- Lift the suprema and infima along a Galois coinsertion -/
def lift_lattice [lattice Ξ²] (gi : galois_coinsertion l u) : lattice Ξ± :=
{ .. gi.lift_semilattice_sup, .. gi.lift_semilattice_inf }
/-- Lift the bot along a Galois coinsertion -/
def lift_order_bot [order_bot Ξ²] (gi : galois_coinsertion l u) : order_bot Ξ± :=
{ bot := gi.choice β₯ $ bot_le,
bot_le := by simp only [gi.choice_eq]; exact assume b, le_trans (gi.gc.monotone_u bot_le) (gi.u_l_le b),
.. βΉpartial_order Ξ±βΊ }
/-- Lift the top, bottom, suprema, and infima along a Galois coinsertion -/
def lift_bounded_lattice [bounded_lattice Ξ²] (gi : galois_coinsertion l u) : bounded_lattice Ξ± :=
{ .. gi.lift_lattice, .. gi.lift_order_bot, .. gi.gc.lift_order_top }
/-- Lift all suprema and infima along a Galois coinsertion -/
def lift_complete_lattice [complete_lattice Ξ²] (gi : galois_coinsertion l u) : complete_lattice Ξ± :=
{ Inf := Ξ»s, u (β¨
aβs, l a),
le_Inf := assume s a hs, gi.gc.le_u $ le_infi $ assume b, le_infi $ assume hb, gi.gc.monotone_l $ hs _ hb,
Inf_le := assume s a ha, le_trans (gi.gc.monotone_u $ infi_le_of_le a $
infi_le_of_le ha $ le_refl _) (gi.u_l_le a),
Sup := Ξ»s, gi.choice (β¨ aβs, l a) $ supr_le $ assume b, supr_le $ assume hb,
gi.gc.monotone_l $ gi.gc.le_u $ le_supr_of_le b $ le_supr_of_le hb $ le_refl _,
le_Sup := by simp only [gi.choice_eq]; exact
assume s a ha, gi.gc.le_u $ le_supr_of_le a $ le_supr_of_le ha $ le_refl _,
Sup_le := by simp only [gi.choice_eq]; exact
assume s a hs, le_trans (gi.gc.monotone_u $ supr_le $ assume b,
show (β¨ (hb : b β s), l b) β€ l a, from supr_le $ assume hb, gi.gc.monotone_l $ hs b hb)
(gi.u_l_le a),
.. gi.lift_bounded_lattice }
end lift
end galois_coinsertion
|
1b089ff01f2d0c4bb95e43110e1215806df47b7e | 63abd62053d479eae5abf4951554e1064a4c45b4 | /test/simps.lean | 353fe73675e008e2c10fd69ca8bc46e12bb72a25 | [
"Apache-2.0"
] | permissive | Lix0120/mathlib | 0020745240315ed0e517cbf32e738d8f9811dd80 | e14c37827456fc6707f31b4d1d16f1f3a3205e91 | refs/heads/master | 1,673,102,855,024 | 1,604,151,044,000 | 1,604,151,044,000 | 308,930,245 | 0 | 0 | Apache-2.0 | 1,604,164,710,000 | 1,604,163,547,000 | null | UTF-8 | Lean | false | false | 24,735 | lean | import tactic.simps
universe variables v u w
-- set_option trace.simps.verbose true
-- set_option trace.app_builder true
open function tactic expr
structure equiv (Ξ± : Sort*) (Ξ² : Sort*) :=
(to_fun : Ξ± β Ξ²)
(inv_fun : Ξ² β Ξ±)
(left_inv : left_inverse inv_fun to_fun)
(right_inv : right_inverse inv_fun to_fun)
local infix ` β `:25 := equiv
/- Since `prod` and `pprod` are a special case for `@[simps]`, we define a new structure to test
the basic functionality.-/
structure my_prod (Ξ± Ξ² : Type*) := (fst : Ξ±) (snd : Ξ²)
def myprod.map {Ξ± Ξ±' Ξ² Ξ²'} (f : Ξ± β Ξ±') (g : Ξ² β Ξ²') (x : my_prod Ξ± Ξ²) : my_prod Ξ±' Ξ²' :=
β¨f x.1, g x.2β©
namespace foo
@[simps] protected def rfl {Ξ±} : Ξ± β Ξ± :=
β¨id, Ξ» x, x, Ξ» x, rfl, Ξ» x, rflβ©
/- simps adds declarations -/
run_cmd do
e β get_env,
e.get `foo.rfl_to_fun,
e.get `foo.rfl_inv_fun,
success_if_fail (e.get `foo.rfl_left_inv),
success_if_fail (e.get `foo.rfl_right_inv)
example (n : β) : foo.rfl.to_fun n = n := by rw [foo.rfl_to_fun, id]
example (n : β) : foo.rfl.inv_fun n = n := by rw [foo.rfl_inv_fun]
/- the declarations are simp-lemmas -/
@[simps] def foo : β Γ β€ := (1, 2)
example : foo.1 = 1 := by simp
example : foo.2 = 2 := by simp
example : foo.1 = 1 := by { dsimp, refl } -- check that dsimp also unfolds
example : foo.2 = 2 := by { dsimp, refl }
example {Ξ±} (x : Ξ±) : foo.rfl.to_fun x = x := by simp
example {Ξ±} (x : Ξ±) : foo.rfl.inv_fun x = x := by simp
example {Ξ±} (x : Ξ±) : foo.rfl.to_fun = @id Ξ± := by { success_if_fail {simp}, refl }
/- check some failures -/
def bar1 : β := 1 -- type is not a structure
def bar2 : β Γ β€ := prod.map (Ξ» x, x + 2) (Ξ» y, y - 3) (3, 4) -- value is not a constructor
noncomputable def bar3 {Ξ±} : Ξ± β Ξ± :=
classical.choice β¨foo.rflβ©
run_cmd do
success_if_fail_with_msg (simps_tac `foo.bar1)
"Invalid `simps` attribute. Target is not a structure",
success_if_fail_with_msg (simps_tac `foo.bar2)
"Invalid `simps` attribute. The body is not a constructor application:
prod.map (Ξ» (x : β), x + 2) (Ξ» (y : β€), y - 3) (3, 4)
Possible solution: add option {rhs_md := semireducible}.
The option {simp_rhs := tt} might also be useful to simplify the right-hand side.",
success_if_fail_with_msg (simps_tac `foo.bar3)
"Invalid `simps` attribute. The body is not a constructor application:
classical.choice bar3._proof_1
Possible solution: add option {rhs_md := semireducible}.
The option {simp_rhs := tt} might also be useful to simplify the right-hand side.",
e β get_env,
let nm := `foo.bar1,
d β e.get nm,
let lhs : expr := const d.to_name (d.univ_params.map level.param),
simps_add_projections e nm "" d.type lhs d.value [] d.univ_params ff {} []
/- test `rhs_md` option -/
def rfl2 {Ξ±} : Ξ± β Ξ± := foo.rfl
run_cmd success_if_fail (simps_tac `foo.rfl2)
attribute [simps {rhs_md := semireducible}] foo.rfl2
/- test `fully_applied` option -/
@[simps {fully_applied := ff}] def rfl3 {Ξ±} : Ξ± β Ξ± := β¨id, Ξ» x, x, Ξ» x, rfl, Ξ» x, rflβ©
end foo
/- we reduce the type when applying [simps] -/
def my_equiv := equiv
@[simps] def baz : my_equiv β β := β¨id, Ξ» x, x, Ξ» x, rfl, Ξ» x, rflβ©
/- test name clashes -/
def name_clash_fst := 1
def name_clash_snd := 1
def name_clash_snd_2 := 1
@[simps] def name_clash := (2, 3)
run_cmd do
e β get_env,
e.get `name_clash_fst_2,
e.get `name_clash_snd_3
/- check projections for nested structures -/
namespace count_nested
@[simps {attrs := [`simp, `norm]}] def nested1 : my_prod β $ my_prod β€ β :=
β¨2, -1, 1β©
@[simps {attrs := []}] def nested2 : β Γ my_prod β β :=
β¨2, myprod.map nat.succ nat.pred β¨1, 2β©β©
end count_nested
run_cmd do
e β get_env,
e.get `count_nested.nested1_fst,
e.get `count_nested.nested1_snd_fst,
e.get `count_nested.nested1_snd_snd,
e.get `count_nested.nested2_fst,
e.get `count_nested.nested2_snd,
is_simp_lemma `count_nested.nested1_fst >>= Ξ» b, guard b, -- simp attribute is global
is_simp_lemma `count_nested.nested2_fst >>= Ξ» b, guard $ Β¬b, --lemmas_only doesn't add simp lemma
guard $ 7 = e.fold 0 -- there are no other lemmas generated
(Ξ» d n, n + if d.to_name.components.init.ilast = `count_nested then 1 else 0)
-- testing with arguments
@[simps] def bar {Ξ± : Type*} (n m : β) : β Γ β€ :=
β¨n - m, n + mβ©
structure equiv_plus_data (Ξ± Ξ²) extends Ξ± β Ξ² :=
(P : (Ξ± β Ξ²) β Prop)
(data : P to_fun)
structure automorphism_plus_data Ξ± extends Ξ± β Ξ± β Ξ± β Ξ± :=
(P : (Ξ± β Ξ± β Ξ± β Ξ±) β Prop)
(data : P to_fun)
(extra : bool β my_prod β β)
@[simps]
def refl_with_data {Ξ±} : equiv_plus_data Ξ± Ξ± :=
{ P := Ξ» f, f = id,
data := rfl,
..foo.rfl }
@[simps]
def refl_with_data' {Ξ±} : equiv_plus_data Ξ± Ξ± :=
{ P := Ξ» f, f = id,
data := rfl,
to_equiv := foo.rfl }
/- test whether eta expansions are reduced correctly -/
@[simps]
def test {Ξ±} : automorphism_plus_data Ξ± :=
{ P := Ξ» f, f = id,
data := rfl,
extra := Ξ» b, β¨(β¨3, 5β© : my_prod _ _).1, (β¨3, 5β© : my_prod _ _).2β©,
..foo.rfl }
/- test whether this is indeed rejected as a valid eta expansion -/
@[simps]
def test_sneaky {Ξ±} : automorphism_plus_data Ξ± :=
{ P := Ξ» f, f = id,
data := rfl,
extra := Ξ» b, β¨(3,5).1,(3,5).2β©,
..foo.rfl }
run_cmd do
e β get_env,
e.get `refl_with_data_to_equiv,
e.get `refl_with_data'_to_equiv,
e.get `test_extra,
e.get `test_sneaky_extra_fst,
success_if_fail (e.get `refl_with_data_to_equiv_to_fun),
success_if_fail (e.get `refl_with_data'_to_equiv_to_fun),
success_if_fail (e.get `test_extra_fst),
success_if_fail (e.get `test_sneaky_extra)
structure partially_applied_str :=
(data : β β my_prod β β)
/- if we have a partially applied constructor, we treat it as if it were eta-expanded -/
@[simps]
def partially_applied_term : partially_applied_str := β¨my_prod.mk 3β©
run_cmd do
e β get_env,
e.get `partially_applied_term_data_fst,
e.get `partially_applied_term_data_snd
structure very_partially_applied_str :=
(data : βΞ², β β Ξ² β my_prod β Ξ²)
/- if we have a partially applied constructor, we treat it as if it were eta-expanded -/
@[simps]
-- def very_partially_applied_term : very_partially_applied_str := β¨@my_prod.mk ββ©
def very_partially_applied_term : very_partially_applied_str := β¨Ξ» x y z, my_prod.mk y zβ©
run_cmd do
e β get_env,
e.get `very_partially_applied_term_data_fst,
e.get `very_partially_applied_term_data_snd
@[simps] def let1 : β Γ β€ :=
let n := 3 in β¨n + 4, 5β©
@[simps] def let2 : β Γ β€ :=
let n := 3, m := 4 in let k := 5 in β¨n + m, kβ©
@[simps] def let3 : β β β Γ β€ :=
Ξ» n, let m := 4, k := 5 in β¨n + m, kβ©
@[simps] def let4 : β β β Γ β€ :=
let m := 4, k := 5 in Ξ» n, β¨n + m, kβ©
run_cmd do
e β get_env,
e.get `let1_fst, e.get `let2_fst, e.get `let3_fst, e.get `let4_fst,
e.get `let1_snd, e.get `let2_snd, e.get `let3_snd, e.get `let4_snd
namespace specify
@[simps fst] def specify1 : β Γ β Γ β := (1, 2, 3)
@[simps snd] def specify2 : β Γ β Γ β := (1, 2, 3)
@[simps snd_fst] def specify3 : β Γ β Γ β := (1, 2, 3)
@[simps snd snd_snd snd_snd] def specify4 : β Γ β Γ β := (1, 2, 3) -- last argument is ignored
@[simps] def specify5 : β Γ β Γ β := (1, prod.map (Ξ» x, x) (Ξ» y, y) (2, 3))
end specify
run_cmd do
e β get_env,
e.get `specify.specify1_fst, e.get `specify.specify2_snd,
e.get `specify.specify3_snd_fst, e.get `specify.specify4_snd_snd, e.get `specify.specify4_snd,
e.get `specify.specify5_fst, e.get `specify.specify5_snd,
guard $ 12 = e.fold 0 -- there are no other lemmas generated
(Ξ» d n, n + if d.to_name.components.init.ilast = `specify then 1 else 0),
success_if_fail_with_msg (simps_tac `specify.specify1 {} ["fst_fst"])
"Invalid simp-lemma specify.specify1_fst_fst.
Projection fst doesn't exist, because target is not a structure.",
success_if_fail_with_msg (simps_tac `specify.specify1 {} ["foo_fst"])
"Invalid simp-lemma specify.specify1_foo_fst. Structure prod does not have projection foo.
The known projections are:
[fst, snd]
You can also see this information by running
`initialize_simps_projections prod`.
Note: the projection names used by @[simps] might not correspond to the projection names in the structure.",
success_if_fail_with_msg (simps_tac `specify.specify1 {} ["snd_bar"])
"Invalid simp-lemma specify.specify1_snd_bar. Structure prod does not have projection bar.
The known projections are:
[fst, snd]
You can also see this information by running
`initialize_simps_projections prod`.
Note: the projection names used by @[simps] might not correspond to the projection names in the structure.",
success_if_fail_with_msg (simps_tac `specify.specify5 {} ["snd_snd"])
"Invalid simp-lemma specify.specify5_snd_snd.
The given definition is not a constructor application:
prod.map (Ξ» (x : β), x) (Ξ» (y : β), y) (2, 3)
Possible solution: add option {rhs_md := semireducible}.
The option {simp_rhs := tt} might also be useful to simplify the right-hand side."
/- We also eta-reduce if we explicitly specify the projection. -/
attribute [simps extra] test
run_cmd do
e β get_env,
d1 β e.get `test_extra,
d2 β e.get `test_extra_2,
guard $ d1.type =β d2.type,
skip
/- check short_name option -/
@[simps {short_name := tt}] def short_name1 : my_prod β β Γ my_prod β β := β¨β¨1, 2β©, 3, 4β©
run_cmd do
e β get_env,
e.get `short_name1_fst, e.get `short_name1_fst_2,
e.get `short_name1_snd, e.get `short_name1_snd_2
/- check simp_rhs option -/
@[simps {simp_rhs := tt}] def equiv.trans {Ξ± Ξ² Ξ³} (f : Ξ± β Ξ²) (g : Ξ² β Ξ³) : Ξ± β Ξ³ :=
β¨g.to_fun β f.to_fun, f.inv_fun β g.inv_fun,
by { intro x, simp [equiv.left_inv _ _] }, by { intro x, simp [equiv.right_inv _ _] }β©
example {Ξ± Ξ² Ξ³ : Type} (f : Ξ± β Ξ²) (g : Ξ² β Ξ³) (x : Ξ±) :
(f.trans g).to_fun x = (f.trans g).to_fun x :=
begin
dsimp only [equiv.trans_to_fun],
guard_target g.to_fun (f.to_fun x) = g.to_fun (f.to_fun x),
refl,
end
local attribute [simp] nat.zero_add nat.one_mul nat.mul_one
@[simps {simp_rhs := tt}] def my_nat_equiv : β β β :=
β¨Ξ» n, 0 + n, Ξ» n, 1 * n * 1, by { intro n, simp }, by { intro n, simp }β©
run_cmd success_if_fail (has_attribute `_refl_lemma `my_nat_equiv_to_fun) >>
has_attribute `_refl_lemma `equiv.trans_to_fun
example (n : β) : my_nat_equiv.to_fun (my_nat_equiv.to_fun $ my_nat_equiv.inv_fun n) = n :=
by { success_if_fail { refl }, simp only [my_nat_equiv_to_fun, my_nat_equiv_inv_fun] }
@[simps {simp_rhs := tt}] def succeed_without_simplification_possible : β β β :=
β¨Ξ» n, n, Ξ» n, n, by { intro n, refl }, by { intro n, refl }β©
/- test that we don't recursively take projections of `prod` and `pprod` -/
@[simps] def pprod_equiv_prod : pprod β β β β Γ β :=
{ to_fun := Ξ» x, β¨x.1, x.2β©,
inv_fun := Ξ» x, β¨x.1, x.2β©,
left_inv := Ξ» β¨x, yβ©, rfl,
right_inv := Ξ» β¨x, yβ©, rfl }
run_cmd do
e β get_env,
e.get `pprod_equiv_prod_to_fun,
e.get `pprod_equiv_prod_inv_fun
attribute [simps to_fun_fst inv_fun_snd] pprod_equiv_prod
run_cmd do
e β get_env,
e.get `pprod_equiv_prod_to_fun_fst,
e.get `pprod_equiv_prod_inv_fun_snd
/- Tests with universe levels -/
class has_hom (obj : Type u) : Type (max u (v+1)) :=
(hom : obj β obj β Type v)
infixr ` βΆ `:10 := has_hom.hom -- type as \h
class category_struct (obj : Type u) extends has_hom.{v} obj : Type (max u (v+1)) :=
(id : Ξ X : obj, hom X X)
(comp : Ξ {X Y Z : obj}, (X βΆ Y) β (Y βΆ Z) β (X βΆ Z))
notation `π` := category_struct.id -- type as \b1
infixr ` β« `:80 := category_struct.comp -- type as \gg
@[simps] instance types : category_struct (Type u) :=
{ hom := Ξ» a b, (a β b),
id := Ξ» a, id,
comp := Ξ» _ _ _ f g, g β f }
example (X : Type u) : (X βΆ X) = (X β X) := by simp
example (X : Type u) : π X = (Ξ» x, x) := by { funext, simp }
example (X Y Z : Type u) (f : X βΆ Y) (g : Y βΆ Z) : f β« g = g β f := by { funext, simp }
namespace coercing
structure foo_str :=
(c : Type)
(x : c)
instance : has_coe_to_sort foo_str := β¨_, foo_str.cβ©
@[simps] def foo : foo_str := β¨β, 3β©
@[simps] def foo2 : foo_str := β¨β, 34β©
example : β₯foo = β := by simp only [foo_c]
example : foo.x = (3 : β) := by simp only [foo_x]
structure voo_str (n : β) :=
(c : Type)
(x : c)
instance has_coe_voo_str (n : β) : has_coe_to_sort (voo_str n) := β¨_, voo_str.cβ©
@[simps] def voo : voo_str 7 := β¨β, 3β©
@[simps] def voo2 : voo_str 4 := β¨β, 34β©
example : β₯voo = β := by simp only [voo_c]
example : voo.x = (3 : β) := by simp only [voo_x]
structure equiv2 (Ξ± : Sort*) (Ξ² : Sort*) :=
(to_fun : Ξ± β Ξ²)
(inv_fun : Ξ² β Ξ±)
(left_inv : left_inverse inv_fun to_fun)
(right_inv : right_inverse inv_fun to_fun)
instance {Ξ± Ξ²} : has_coe_to_fun $ equiv2 Ξ± Ξ² := β¨_, equiv2.to_funβ©
@[simps] protected def rfl2 {Ξ±} : equiv2 Ξ± Ξ± :=
β¨Ξ» x, x, Ξ» x, x, Ξ» x, rfl, Ξ» x, rflβ©
example {Ξ±} (x : Ξ±) : coercing.rfl2 x = x := by rw [coercing.rfl2_to_fun]
example {Ξ±} (x : Ξ±) : coercing.rfl2 x = x := by simp
example {Ξ±} (x : Ξ±) : coercing.rfl2.inv_fun x = x := by simp
@[simps] protected def equiv2.symm {Ξ± Ξ²} (f : equiv2 Ξ± Ξ²) : equiv2 Ξ² Ξ± :=
β¨f.inv_fun, f, f.right_inv, f.left_invβ©
@[simps] protected def equiv2.symm2 {Ξ± Ξ²} (f : equiv2 Ξ± Ξ²) : equiv2 Ξ² Ξ± :=
β¨f.inv_fun, f.to_fun, f.right_inv, f.left_invβ©
/- we can use the `md` attribute to not unfold the `has_coe_to_fun` attribute, so that `@[simps]`
doesn't recognize that the type of `βf` is still a function type. -/
@[simps {type_md := reducible}] protected def equiv2.symm3 {Ξ± Ξ²} (f : equiv2 Ξ± Ξ²) : equiv2 Ξ² Ξ± :=
β¨f.inv_fun, f, f.right_inv, f.left_invβ©
example {Ξ± Ξ²} (f : equiv2 Ξ± Ξ²) (y : Ξ²) : f.symm y = f.inv_fun y := by simp
example {Ξ± Ξ²} (f : equiv2 Ξ± Ξ²) (x : Ξ±) : f.symm.inv_fun x = f x := by simp
example {Ξ± Ξ²} (f : equiv2 Ξ± Ξ²) : f.symm.inv_fun = f := by { success_if_fail {simp}, refl }
example {Ξ± Ξ²} (f : equiv2 Ξ± Ξ²) : f.symm3.inv_fun = f := by simp
section
set_option old_structure_cmd true
class semigroup (G : Type u) extends has_mul G :=
(mul_assoc : β a b c : G, a * b * c = a * (b * c))
end
@[simps] instance {Ξ± Ξ²} [semigroup Ξ±] [semigroup Ξ²] : semigroup (Ξ± Γ Ξ²) :=
{ mul := Ξ» x y, (x.1 * y.1, x.2 * y.2),
mul_assoc := by { intros, simp only [semigroup.mul_assoc], refl } }
example {Ξ± Ξ²} [semigroup Ξ±] [semigroup Ξ²] (x y : Ξ± Γ Ξ²) : x * y = (x.1 * y.1, x.2 * y.2) :=
by simp
example {Ξ± Ξ²} [semigroup Ξ±] [semigroup Ξ²] (x y : Ξ± Γ Ξ²) : (x * y).1 = x.1 * y.1 := by simp
structure Semigroup :=
(G : Type*)
(op : G β G β G)
(infix * := op)
(op_assoc : β (x y z : G), (x * y) * z = x * (y * z))
namespace Group
instance : has_coe_to_sort Semigroup := β¨_, Semigroup.Gβ©
instance (G : Semigroup) : has_mul G := β¨G.opβ©
@[simps] def prod_Semigroup (G H : Semigroup) : Semigroup :=
{ G := G Γ H,
op := Ξ» x y, (x.1 * y.1, x.2 * y.2),
op_assoc := by { intros, dsimp [Group.has_mul], simp [Semigroup.op_assoc] }}
end Group
section
set_option old_structure_cmd true
class extending_stuff (G : Type u) extends has_mul G, has_zero G, has_neg G, has_subset G :=
(new_axiom : β x : G, x * - 0 β - x)
end
@[simps] def bar : extending_stuff β :=
{ mul := (*),
zero := 0,
neg := nat.succ,
subset := Ξ» x y, true,
new_axiom := Ξ» x, trivial }
section
local attribute [instance] bar
example (x : β) : x * - 0 β - x := by simp
end
class new_extending_stuff (G : Type u) extends has_mul G, has_zero G, has_neg G, has_subset G :=
(new_axiom : β x : G, x * - 0 β - x)
@[simps] def new_bar : new_extending_stuff β :=
{ mul := (*),
zero := 0,
neg := nat.succ,
subset := Ξ» x y, true,
new_axiom := Ξ» x, trivial }
section
local attribute [instance] new_bar
example (x : β) : x * - 0 β - x := by simp
end
end coercing
namespace manual_coercion
structure equiv (Ξ± : Sort*) (Ξ² : Sort*) :=
(to_fun : Ξ± β Ξ²)
(inv_fun : Ξ² β Ξ±)
local infix ` β `:25 := manual_coercion.equiv
variables {Ξ± Ξ² Ξ³ : Sort*}
instance : has_coe_to_fun $ Ξ± β Ξ² := β¨_, equiv.to_funβ©
def equiv.symm (e : Ξ± β Ξ²) : Ξ² β Ξ± := β¨e.inv_fun, e.to_funβ©
/-- See Note [custom simps projection] -/
def equiv.simps.inv_fun (e : Ξ± β Ξ²) : Ξ² β Ξ± := e.symm
/-- Composition of equivalences `eβ : Ξ± β Ξ²` and `eβ : Ξ² β Ξ³`. -/
@[simps {simp_rhs := tt}] protected def equiv.trans (eβ : Ξ± β Ξ²) (eβ : Ξ² β Ξ³) : Ξ± β Ξ³ :=
β¨eβ β eβ, eβ.symm β eβ.symmβ©
example (eβ : Ξ± β Ξ²) (eβ : Ξ² β Ξ³) (x : Ξ³) : (eβ.trans eβ).symm x = eβ.symm (eβ.symm x) :=
by simp only [equiv.trans_inv_fun]
end manual_coercion
namespace faulty_manual_coercion
structure equiv (Ξ± : Sort*) (Ξ² : Sort*) :=
(to_fun : Ξ± β Ξ²)
(inv_fun : Ξ² β Ξ±)
local infix ` β `:25 := faulty_manual_coercion.equiv
variables {Ξ± Ξ² Ξ³ : Sort*}
/-- See Note [custom simps projection] -/
noncomputable def equiv.simps.inv_fun (e : Ξ± β Ξ²) : Ξ² β Ξ± := classical.choice β¨e.inv_funβ©
run_cmd do e β get_env, success_if_fail_with_msg (simps_get_raw_projections e `faulty_manual_coercion.equiv)
"Invalid custom projection:
Ξ» {Ξ± : Sort u_1} {Ξ² : Sort u_2} (e : Ξ± β Ξ²), classical.choice _
Expression is not definitionally equal to equiv.inv_fun."
end faulty_manual_coercion
namespace manual_initialize
/- defining a manual coercion. -/
variables {Ξ± Ξ² Ξ³ : Sort*}
structure equiv (Ξ± : Sort*) (Ξ² : Sort*) :=
(to_fun : Ξ± β Ξ²)
(inv_fun : Ξ² β Ξ±)
local infix ` β `:25 := manual_initialize.equiv
instance : has_coe_to_fun $ Ξ± β Ξ² := β¨_, equiv.to_funβ©
def equiv.symm (e : Ξ± β Ξ²) : Ξ² β Ξ± := β¨e.inv_fun, e.to_funβ©
/-- See Note [custom simps projection] -/
-- test: intentionally using different unvierse levels for equiv.symm than for equiv
def equiv.simps.inv_fun (e : Ξ± β Ξ²) : Ξ² β Ξ± := e.symm
initialize_simps_projections equiv
run_cmd has_attribute `_simps_str `manual_initialize.equiv
/-- Composition of equivalences `eβ : Ξ± β Ξ²` and `eβ : Ξ² β Ξ³`. -/
@[simps {simp_rhs := tt}] protected def equiv.trans (eβ : Ξ± β Ξ²) (eβ : Ξ² β Ξ³) : Ξ± β Ξ³ :=
β¨eβ β eβ, eβ.symm β eβ.symmβ©
end manual_initialize
namespace faulty_universes
variables {Ξ± Ξ² Ξ³ : Sort*}
structure equiv (Ξ± : Sort u) (Ξ² : Sort v) :=
(to_fun : Ξ± β Ξ²)
(inv_fun : Ξ² β Ξ±)
local infix ` β `:25 := faulty_universes.equiv
instance : has_coe_to_fun $ Ξ± β Ξ² := β¨_, equiv.to_funβ©
def equiv.symm (e : Ξ± β Ξ²) : Ξ² β Ξ± := β¨e.inv_fun, e.to_funβ©
/-- See Note [custom simps projection] -/
-- test: intentionally using different names for the universe variables for equiv.symm than for
-- equiv
def equiv.simps.inv_fun {Ξ± : Type u} {Ξ² : Type v} (e : Ξ± β Ξ²) : Ξ² β Ξ± := e.symm
run_cmd do e β get_env,
success_if_fail_with_msg (simps_get_raw_projections e `faulty_universes.equiv)
"Invalid custom projection:
Ξ» {Ξ± : Type u} {Ξ² : Type v} (e : Ξ± β Ξ²), β(e.symm)
Expression has different type than equiv.inv_fun. Given type:
Ξ {Ξ± : Type u} {Ξ² : Type v} (e : Ξ± β Ξ²), has_coe_to_fun.F e.symm
Expected type:
Ξ {Ξ± : Sort u} {Ξ² : Sort v}, Ξ± β Ξ² β Ξ² β Ξ±"
end faulty_universes
namespace manual_universes
variables {Ξ± Ξ² Ξ³ : Sort*}
structure equiv (Ξ± : Sort u) (Ξ² : Sort v) :=
(to_fun : Ξ± β Ξ²)
(inv_fun : Ξ² β Ξ±)
local infix ` β `:25 := manual_universes.equiv
instance : has_coe_to_fun $ Ξ± β Ξ² := β¨_, equiv.to_funβ©
def equiv.symm (e : Ξ± β Ξ²) : Ξ² β Ξ± := β¨e.inv_fun, e.to_funβ©
/-- See Note [custom simps projection] -/
-- test: intentionally using different unvierse levels for equiv.symm than for equiv
def equiv.simps.inv_fun {Ξ± : Sort w} {Ξ² : Sort u} (e : Ξ± β Ξ²) : Ξ² β Ξ± := e.symm
-- check whether we can generate custom projections even if the universe names don't match
initialize_simps_projections equiv
end manual_universes
namespace manual_projection_names
structure equiv (Ξ± : Sort*) (Ξ² : Sort*) :=
(to_fun : Ξ± β Ξ²)
(inv_fun : Ξ² β Ξ±)
local infix ` β `:25 := manual_projection_names.equiv
variables {Ξ± Ξ² Ξ³ : Sort*}
instance : has_coe_to_fun $ Ξ± β Ξ² := β¨_, equiv.to_funβ©
def equiv.symm (e : Ξ± β Ξ²) : Ξ² β Ξ± := β¨e.inv_fun, e.to_funβ©
/-- See Note [custom simps projection] -/
def equiv.simps.inv_fun (e : Ξ± β Ξ²) : Ξ² β Ξ± := e.symm
initialize_simps_projections equiv (to_fun β apply, inv_fun β symm_apply)
run_cmd do
e β get_env,
data β simps_get_raw_projections e `manual_projection_names.equiv,
guard $ data.2.map prod.fst = [`apply, `symm_apply]
@[simps {simp_rhs := tt}] protected def equiv.trans (eβ : Ξ± β Ξ²) (eβ : Ξ² β Ξ³) : Ξ± β Ξ³ :=
β¨eβ β eβ, eβ.symm β eβ.symmβ©
example (eβ : Ξ± β Ξ²) (eβ : Ξ² β Ξ³) (x : Ξ±) : (eβ.trans eβ) x = eβ (eβ x) :=
by simp only [equiv.trans_apply]
example (eβ : Ξ± β Ξ²) (eβ : Ξ² β Ξ³) (x : Ξ³) : (eβ.trans eβ).symm x = eβ.symm (eβ.symm x) :=
by simp only [equiv.trans_symm_apply]
-- the new projection names are parsed correctly (the old projection names won't work anymore)
@[simps apply symm_apply] protected def equiv.trans2 (eβ : Ξ± β Ξ²) (eβ : Ξ² β Ξ³) : Ξ± β Ξ³ :=
β¨eβ β eβ, eβ.symm β eβ.symmβ©
-- initialize_simps_projections equiv
end manual_projection_names
-- test transparency setting
structure set_plus (Ξ± : Type) :=
(s : set Ξ±)
(x : Ξ±)
(h : x β s)
@[simps] def nat_set_plus : set_plus β := β¨set.univ, 1, trivialβ©
example : nat_set_plus.s = set.univ :=
begin
dsimp only [nat_set_plus_s],
guard_target @set.univ β = set.univ,
refl
end
@[simps {type_md := semireducible}] def nat_set_plus2 : set_plus β := β¨set.univ, 1, trivialβ©
example : nat_set_plus2.s = set.univ :=
begin
success_if_fail { dsimp only [nat_set_plus2_s] }, refl
end
@[simps {rhs_md := semireducible}] def nat_set_plus3 : set_plus β := nat_set_plus
example : nat_set_plus3.s = set.univ :=
begin
dsimp only [nat_set_plus3_s],
guard_target @set.univ β = set.univ,
refl
end
namespace nested_non_fully_applied
structure equiv (Ξ± : Sort*) (Ξ² : Sort*) :=
(to_fun : Ξ± β Ξ²)
(inv_fun : Ξ² β Ξ±)
local infix ` β `:25 := nested_non_fully_applied.equiv
variables {Ξ± Ξ² Ξ³ : Sort*}
@[simps] def equiv.symm (e : Ξ± β Ξ²) : Ξ² β Ξ± := β¨e.inv_fun, e.to_funβ©
@[simps {rhs_md := semireducible, fully_applied := ff}] def equiv.symm2 : (Ξ± β Ξ²) β (Ξ² β Ξ±) :=
β¨equiv.symm, equiv.symmβ©
example (e : Ξ± β Ξ²) : (equiv.symm2.inv_fun e).to_fun = e.inv_fun :=
begin
dsimp only [equiv.symm2_inv_fun_to_fun],
guard_target e.inv_fun = e.inv_fun,
refl
end
/- do not prematurely unfold `equiv.symm`, unless necessary -/
@[simps to_fun to_fun_to_fun {rhs_md := semireducible}] def equiv.symm3 : (Ξ± β Ξ²) β (Ξ² β Ξ±) :=
equiv.symm2
example (e : Ξ± β Ξ²) (y : Ξ²) : (equiv.symm3.to_fun e).to_fun y = e.inv_fun y β§
(equiv.symm3.to_fun e).to_fun y = e.inv_fun y :=
begin
split,
{ dsimp only [equiv.symm3_to_fun], guard_target e.symm.to_fun y = e.inv_fun y, refl },
{ dsimp only [equiv.symm3_to_fun_to_fun], guard_target e.inv_fun y = e.inv_fun y, refl }
end
end nested_non_fully_applied
/- fail if you add an attribute with a parameter. -/
run_cmd success_if_fail $ simps_tac `foo.rfl { attrs := [`higher_order] }
-- test that type classes which are props work
class prop_class (n : β) : Prop :=
(has_true : true)
instance has_prop_class (n : β) : prop_class n := β¨trivialβ©
structure needs_prop_class (n : β) [prop_class n] :=
(t : true)
@[simps] def test_prop_class : needs_prop_class 1 :=
{ t := trivial }
/- check that when the coercion is given in eta-expanded form, we can also find the coercion. -/
structure alg_hom (R A B : Type*) :=
(to_fun : A β B)
instance (R A B : Type*) : has_coe_to_fun (alg_hom R A B) := β¨_, Ξ» f, f.to_funβ©
@[simps] def my_alg_hom : alg_hom unit bool bool :=
{ to_fun := id }
example (x : bool) : my_alg_hom x = id x := by simp only [my_alg_hom_to_fun]
structure ring_hom (A B : Type*) :=
(to_fun : A β B)
instance (A B : Type*) : has_coe_to_fun (ring_hom A B) := β¨_, Ξ» f, f.to_funβ©
@[simps] def my_ring_hom : ring_hom bool bool :=
{ to_fun := id }
example (x : bool) : my_ring_hom x = id x := by simp only [my_ring_hom_to_fun]
|
7c12408200b55a6d254c1283b7a103b3eedd951a | bb31430994044506fa42fd667e2d556327e18dfe | /src/algebra/hom/units.lean | 71f0f7f067f89717fd28ab560a6e4f33d0ed65bf | [
"Apache-2.0"
] | permissive | sgouezel/mathlib | 0cb4e5335a2ba189fa7af96d83a377f83270e503 | 00638177efd1b2534fc5269363ebf42a7871df9a | refs/heads/master | 1,674,527,483,042 | 1,673,665,568,000 | 1,673,665,568,000 | 119,598,202 | 0 | 0 | null | 1,517,348,647,000 | 1,517,348,646,000 | null | UTF-8 | Lean | false | false | 13,632 | lean | /-
Copyright (c) 2018 Johan Commelin All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin, Chris Hughes, Kevin Buzzard
-/
import algebra.hom.group
import algebra.group.units
/-!
# Monoid homomorphisms and units
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
This file allows to lift monoid homomorphisms to group homomorphisms of their units subgroups. It
also contains unrelated results about `units` that depend on `monoid_hom`.
## Main declarations
* `units.map`: Turn an homomorphism from `Ξ±` to `Ξ²` monoids into an homomorphism from `Ξ±Λ£` to `Ξ²Λ£`.
* `monoid_hom.to_hom_units`: Turn an homomorphism from a group `Ξ±` to `Ξ²` into an homomorphism from
`Ξ±` to `Ξ²Λ£`.
## TODO
The results that don't mention homomorphisms should be proved (earlier?) in a different file and be
used to golf the basic `group` lemmas.
-/
open function
universes u v w
@[to_additive] lemma group.is_unit {G} [group G] (g : G) : is_unit g :=
β¨β¨g, gβ»ΒΉ, mul_inv_self g, inv_mul_self gβ©, rflβ©
section monoid_hom_class
/-- If two homomorphisms from a division monoid to a monoid are equal at a unit `x`, then they are
equal at `xβ»ΒΉ`. -/
@[to_additive "If two homomorphisms from a subtraction monoid to an additive monoid are equal at an
additive unit `x`, then they are equal at `-x`."]
lemma is_unit.eq_on_inv {F G N} [division_monoid G] [monoid N] [monoid_hom_class F G N] {x : G}
(hx : is_unit x) (f g : F) (h : f x = g x) : f xβ»ΒΉ = g xβ»ΒΉ :=
left_inv_eq_right_inv (map_mul_eq_one f hx.inv_mul_cancel) $
h.symm βΈ map_mul_eq_one g $ hx.mul_inv_cancel
/-- If two homomorphism from a group to a monoid are equal at `x`, then they are equal at `xβ»ΒΉ`. -/
@[to_additive "If two homomorphism from an additive group to an additive monoid are equal at `x`,
then they are equal at `-x`." ]
lemma eq_on_inv {F G M} [group G] [monoid M] [monoid_hom_class F G M] (f g : F) {x : G}
(h : f x = g x) : f xβ»ΒΉ = g xβ»ΒΉ :=
(group.is_unit x).eq_on_inv f g h
end monoid_hom_class
namespace units
variables {Ξ± : Type*} {M : Type u} {N : Type v} {P : Type w} [monoid M] [monoid N] [monoid P]
/-- The group homomorphism on units induced by a `monoid_hom`. -/
@[to_additive "The `add_group` homomorphism on `add_unit`s induced by an `add_monoid_hom`."]
def map (f : M β* N) : MΛ£ β* NΛ£ :=
monoid_hom.mk'
(Ξ» u, β¨f u.val, f u.inv,
by rw [β f.map_mul, u.val_inv, f.map_one],
by rw [β f.map_mul, u.inv_val, f.map_one]β©)
(Ξ» x y, ext (f.map_mul x y))
@[simp, to_additive] lemma coe_map (f : M β* N) (x : MΛ£) : β(map f x) = f x := rfl
@[simp, to_additive] lemma coe_map_inv (f : M β* N) (u : MΛ£) :
β(map f u)β»ΒΉ = f βuβ»ΒΉ :=
rfl
@[simp, to_additive]
lemma map_comp (f : M β* N) (g : N β* P) : map (g.comp f) = (map g).comp (map f) := rfl
variables (M)
@[simp, to_additive] lemma map_id : map (monoid_hom.id M) = monoid_hom.id MΛ£ :=
by ext; refl
/-- Coercion `MΛ£ β M` as a monoid homomorphism. -/
@[to_additive "Coercion `add_units M β M` as an add_monoid homomorphism."]
def coe_hom : MΛ£ β* M := β¨coe, coe_one, coe_mulβ©
variable {M}
@[simp, to_additive] lemma coe_hom_apply (x : MΛ£) : coe_hom M x = βx := rfl
@[simp, norm_cast, to_additive]
lemma coe_pow (u : MΛ£) (n : β) : ((u ^ n : MΛ£) : M) = u ^ n :=
(units.coe_hom M).map_pow u n
section division_monoid
variables [division_monoid Ξ±]
@[simp, norm_cast, to_additive] lemma coe_div : β uβ uβ : Ξ±Λ£, β(uβ / uβ) = (uβ / uβ : Ξ±) :=
(units.coe_hom Ξ±).map_div
@[simp, norm_cast, to_additive] lemma coe_zpow : β (u : Ξ±Λ£) (n : β€), ((u ^ n : Ξ±Λ£) : Ξ±) = u ^ n :=
(units.coe_hom Ξ±).map_zpow
@[field_simps] lemma _root_.divp_eq_div (a : Ξ±) (u : Ξ±Λ£) : a /β u = a / u :=
by rw [div_eq_mul_inv, divp, u.coe_inv]
@[simp, to_additive]
lemma _root_.map_units_inv {F : Type*} [monoid_hom_class F M Ξ±] (f : F) (u : units M) :
f βuβ»ΒΉ = (f u)β»ΒΉ :=
((f : M β* Ξ±).comp (units.coe_hom M)).map_inv u
end division_monoid
/-- If a map `g : M β NΛ£` agrees with a homomorphism `f : M β* N`, then
this map is a monoid homomorphism too. -/
@[to_additive "If a map `g : M β add_units N` agrees with a homomorphism `f : M β+ N`, then this map
is an add_monoid homomorphism too."]
def lift_right (f : M β* N) (g : M β NΛ£) (h : β x, β(g x) = f x) :
M β* NΛ£ :=
{ to_fun := g,
map_one' := units.ext $ (h 1).symm βΈ f.map_one,
map_mul' := Ξ» x y, units.ext $ by simp only [h, coe_mul, f.map_mul] }
@[simp, to_additive] lemma coe_lift_right {f : M β* N} {g : M β NΛ£}
(h : β x, β(g x) = f x) (x) : (lift_right f g h x : N) = f x := h x
@[simp, to_additive] lemma mul_lift_right_inv {f : M β* N} {g : M β NΛ£}
(h : β x, β(g x) = f x) (x) : f x * β(lift_right f g h x)β»ΒΉ = 1 :=
by rw [units.mul_inv_eq_iff_eq_mul, one_mul, coe_lift_right]
@[simp, to_additive] lemma lift_right_inv_mul {f : M β* N} {g : M β NΛ£}
(h : β x, β(g x) = f x) (x) : β(lift_right f g h x)β»ΒΉ * f x = 1 :=
by rw [units.inv_mul_eq_iff_eq_mul, mul_one, coe_lift_right]
end units
namespace monoid_hom
/-- If `f` is a homomorphism from a group `G` to a monoid `M`,
then its image lies in the units of `M`,
and `f.to_hom_units` is the corresponding monoid homomorphism from `G` to `MΛ£`. -/
@[to_additive "If `f` is a homomorphism from an additive group `G` to an additive monoid `M`,
then its image lies in the `add_units` of `M`,
and `f.to_hom_units` is the corresponding homomorphism from `G` to `add_units M`."]
def to_hom_units {G M : Type*} [group G] [monoid M] (f : G β* M) : G β* MΛ£ :=
units.lift_right f
(Ξ» g, β¨f g, f gβ»ΒΉ, map_mul_eq_one f (mul_inv_self _), map_mul_eq_one f (inv_mul_self _)β©)
(Ξ» g, rfl)
@[simp, to_additive]
lemma coe_to_hom_units {G M : Type*} [group G] [monoid M] (f : G β* M) (g : G) :
(f.to_hom_units g : M) = f g :=
rfl
end monoid_hom
namespace is_unit
variables {F G Ξ± M N : Type*}
section monoid
variables [monoid M] [monoid N]
@[to_additive] lemma map [monoid_hom_class F M N] (f : F) {x : M} (h : is_unit x) : is_unit (f x) :=
by rcases h with β¨y, rflβ©; exact (units.map (f : M β* N) y).is_unit
@[to_additive] lemma of_left_inverse [monoid_hom_class F M N] [monoid_hom_class G N M]
{f : F} {x : M} (g : G) (hfg : function.left_inverse g f) (h : is_unit (f x)) :
is_unit x :=
by simpa only [hfg x] using h.map g
@[to_additive] lemma _root_.is_unit_map_of_left_inverse
[monoid_hom_class F M N] [monoid_hom_class G N M]
{f : F} {x : M} (g : G) (hfg : function.left_inverse g f) :
is_unit (f x) β is_unit x :=
β¨of_left_inverse g hfg, map _β©
/-- If a homomorphism `f : M β* N` sends each element to an `is_unit`, then it can be lifted
to `f : M β* NΛ£`. See also `units.lift_right` for a computable version. -/
@[to_additive "If a homomorphism `f : M β+ N` sends each element to an `is_add_unit`, then it can be
lifted to `f : M β+ add_units N`. See also `add_units.lift_right` for a computable version."]
noncomputable def lift_right (f : M β* N) (hf : β x, is_unit (f x)) : M β* NΛ£ :=
units.lift_right f (Ξ» x, (hf x).unit) $ Ξ» x, rfl
@[to_additive] lemma coe_lift_right (f : M β* N) (hf : β x, is_unit (f x)) (x) :
(is_unit.lift_right f hf x : N) = f x := rfl
@[simp, to_additive] lemma mul_lift_right_inv (f : M β* N) (h : β x, is_unit (f x)) (x) :
f x * β(is_unit.lift_right f h x)β»ΒΉ = 1 :=
units.mul_lift_right_inv (Ξ» y, rfl) x
@[simp, to_additive] lemma lift_right_inv_mul (f : M β* N) (h : β x, is_unit (f x)) (x) :
β(is_unit.lift_right f h x)β»ΒΉ * f x = 1 :=
units.lift_right_inv_mul (Ξ» y, rfl) x
end monoid
section division_monoid
variables [division_monoid Ξ±] {a b c : Ξ±}
/-- The element of the group of units, corresponding to an element of a monoid which is a unit. As
opposed to `is_unit.unit`, the inverse is computable and comes from the inversion on `Ξ±`. This is
useful to transfer properties of inversion in `units Ξ±` to `Ξ±`. See also `to_units`. -/
@[to_additive "The element of the additive group of additive units, corresponding to an element of
an additive monoid which is an additive unit. As opposed to `is_add_unit.add_unit`, the negation is
computable and comes from the negation on `Ξ±`. This is useful to transfer properties of negation in
`add_units Ξ±` to `Ξ±`. See also `to_add_units`.", simps]
def unit' (h : is_unit a) : Ξ±Λ£ := β¨a, aβ»ΒΉ, h.mul_inv_cancel, h.inv_mul_cancelβ©
@[simp, to_additive] protected lemma mul_inv_cancel_left (h : is_unit a) : β b, a * (aβ»ΒΉ * b) = b :=
h.unit'.mul_inv_cancel_left
@[simp, to_additive] protected lemma inv_mul_cancel_left (h : is_unit a) : β b, aβ»ΒΉ * (a * b) = b :=
h.unit'.inv_mul_cancel_left
@[simp, to_additive] protected lemma mul_inv_cancel_right (h : is_unit b) (a : Ξ±) :
a * b * bβ»ΒΉ = a :=
h.unit'.mul_inv_cancel_right _
@[simp, to_additive] protected lemma inv_mul_cancel_right (h : is_unit b) (a : Ξ±) :
a * bβ»ΒΉ * b = a :=
h.unit'.inv_mul_cancel_right _
@[to_additive] protected lemma div_self (h : is_unit a) : a / a = 1 :=
by rw [div_eq_mul_inv, h.mul_inv_cancel]
@[to_additive] protected lemma eq_mul_inv_iff_mul_eq (h : is_unit c) : a = b * cβ»ΒΉ β a * c = b :=
h.unit'.eq_mul_inv_iff_mul_eq
@[to_additive] protected lemma eq_inv_mul_iff_mul_eq (h : is_unit b) : a = bβ»ΒΉ * c β b * a = c :=
h.unit'.eq_inv_mul_iff_mul_eq
@[to_additive] protected lemma inv_mul_eq_iff_eq_mul (h : is_unit a) : aβ»ΒΉ * b = c β b = a * c :=
h.unit'.inv_mul_eq_iff_eq_mul
@[to_additive] protected lemma mul_inv_eq_iff_eq_mul (h : is_unit b) : a * bβ»ΒΉ = c β a = c * b :=
h.unit'.mul_inv_eq_iff_eq_mul
@[to_additive] protected lemma mul_inv_eq_one (h : is_unit b) : a * bβ»ΒΉ = 1 β a = b :=
@units.mul_inv_eq_one _ _ h.unit' _
@[to_additive] protected lemma inv_mul_eq_one (h : is_unit a) : aβ»ΒΉ * b = 1 β a = b :=
@units.inv_mul_eq_one _ _ h.unit' _
@[to_additive] protected lemma mul_eq_one_iff_eq_inv (h : is_unit b) : a * b = 1 β a = bβ»ΒΉ :=
@units.mul_eq_one_iff_eq_inv _ _ h.unit' _
@[to_additive] protected lemma mul_eq_one_iff_inv_eq (h : is_unit a) : a * b = 1 β aβ»ΒΉ = b :=
@units.mul_eq_one_iff_inv_eq _ _ h.unit' _
@[simp, to_additive] protected lemma div_mul_cancel (h : is_unit b) (a : Ξ±) : a / b * b = a :=
by rw [div_eq_mul_inv, h.inv_mul_cancel_right]
@[simp, to_additive] protected lemma mul_div_cancel (h : is_unit b) (a : Ξ±) : a * b / b = a :=
by rw [div_eq_mul_inv, h.mul_inv_cancel_right]
@[to_additive] protected lemma mul_one_div_cancel (h : is_unit a) : a * (1 / a) = 1 := by simp [h]
@[to_additive] protected lemma one_div_mul_cancel (h : is_unit a) : (1 / a) * a = 1 := by simp [h]
@[to_additive] lemma inv : is_unit a β is_unit aβ»ΒΉ :=
by { rintro β¨u, rflβ©, rw βunits.coe_inv, exact units.is_unit _ }
@[to_additive] lemma div (ha : is_unit a) (hb : is_unit b) : is_unit (a / b) :=
by { rw div_eq_mul_inv, exact ha.mul hb.inv }
@[to_additive] protected lemma div_left_inj (h : is_unit c) : a / c = b / c β a = b :=
by { simp_rw div_eq_mul_inv, exact units.mul_left_inj h.inv.unit' }
@[to_additive] protected lemma div_eq_iff (h : is_unit b) : a / b = c β a = c * b :=
by rw [div_eq_mul_inv, h.mul_inv_eq_iff_eq_mul]
@[to_additive] protected lemma eq_div_iff (h : is_unit c) : a = b / c β a * c = b :=
by rw [div_eq_mul_inv, h.eq_mul_inv_iff_mul_eq]
@[to_additive] protected lemma div_eq_of_eq_mul (h : is_unit b) : a = c * b β a / b = c :=
h.div_eq_iff.2
@[to_additive] protected lemma eq_div_of_mul_eq (h : is_unit c) : a * c = b β a = b / c :=
h.eq_div_iff.2
@[to_additive] protected lemma div_eq_one_iff_eq (h : is_unit b) : a / b = 1 β a = b :=
β¨eq_of_div_eq_one, Ξ» hab, hab.symm βΈ h.div_selfβ©
@[to_additive] protected lemma div_mul_left (h : is_unit b) : b / (a * b) = 1 / a :=
by rw [div_eq_mul_inv, mul_inv_rev, h.mul_inv_cancel_left, one_div]
@[to_additive] protected lemma mul_div_mul_right (h : is_unit c) (a b : Ξ±) :
(a * c) / (b * c) = a / b :=
by simp only [div_eq_mul_inv, mul_inv_rev, mul_assoc, h.mul_inv_cancel_left]
@[to_additive] protected lemma mul_mul_div (a : Ξ±) (h : is_unit b) : a * b * (1 / b) = a :=
by simp [h]
end division_monoid
section division_comm_monoid
variables [division_comm_monoid Ξ±] {a b c d : Ξ±}
@[to_additive] protected lemma div_mul_right (h : is_unit a) (b : Ξ±) : a / (a * b) = 1 / b :=
by rw [mul_comm, h.div_mul_left]
@[to_additive] protected lemma mul_div_cancel_left (h : is_unit a) (b : Ξ±) : a * b / a = b :=
by rw [mul_comm, h.mul_div_cancel]
@[to_additive] protected lemma mul_div_cancel' (h : is_unit a) (b : Ξ±) : a * (b / a) = b :=
by rw [mul_comm, h.div_mul_cancel]
@[to_additive] protected lemma mul_div_mul_left (h : is_unit c) (a b : Ξ±) :
(c * a) / (c * b) = a / b :=
by rw [mul_comm c, mul_comm c, h.mul_div_mul_right]
@[to_additive] protected lemma mul_eq_mul_of_div_eq_div (hb : is_unit b) (hd : is_unit d) (a c : Ξ±)
(h : a / b = c / d) : a * d = c * b :=
by rw [βmul_one a, βhb.div_self, βmul_comm_div, h, div_mul_eq_mul_div, hd.div_mul_cancel]
@[to_additive] protected lemma div_eq_div_iff (hb : is_unit b) (hd : is_unit d) :
a / b = c / d β a * d = c * b :=
by rw [β(hb.mul hd).mul_left_inj, βmul_assoc, hb.div_mul_cancel, βmul_assoc, mul_right_comm,
hd.div_mul_cancel]
@[to_additive] protected lemma div_div_cancel (h : is_unit a) : a / (a / b) = b :=
by rw [div_div_eq_mul_div, h.mul_div_cancel_left]
end division_comm_monoid
end is_unit
|
9c5fc063ceed812c623bb14019a9ac07ea19ec48 | 6dc0c8ce7a76229dd81e73ed4474f15f88a9e294 | /stage0/src/Lean/Parser/Level.lean | 0528d9bf828754b939ca8e074baa17791e0596ff | [
"Apache-2.0"
] | permissive | williamdemeo/lean4 | 72161c58fe65c3ad955d6a3050bb7d37c04c0d54 | 6d00fcf1d6d873e195f9220c668ef9c58e9c4a35 | refs/heads/master | 1,678,305,356,877 | 1,614,708,995,000 | 1,614,708,995,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,003 | lean | /-
Copyright (c) 2019 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura, Sebastian Ullrich
-/
import Lean.Parser.Extra
namespace Lean
namespace Parser
builtin_initialize
registerBuiltinParserAttribute `builtinLevelParser `level
@[inline] def levelParser (rbp : Nat := 0) : Parser :=
categoryParser `level rbp
namespace Level
@[builtinLevelParser] def paren := parser! "(" >> levelParser >> ")"
@[builtinLevelParser] def max := parser! nonReservedSymbol "max" true >> many1 (ppSpace >> levelParser maxPrec)
@[builtinLevelParser] def imax := parser! nonReservedSymbol "imax" true >> many1 (ppSpace >> levelParser maxPrec)
@[builtinLevelParser] def hole := parser! "_"
@[builtinLevelParser] def num := checkPrec maxPrec >> numLit
@[builtinLevelParser] def ident := checkPrec maxPrec >> Parser.ident
@[builtinLevelParser] def addLit := tparser!:65 " + " >> numLit
end Level
end Parser
end Lean
|
c047fafc1e9f6925220345b142dceee0542c9e66 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/category_theory/monoidal/limits_auto.lean | e8be4878cce567fae6e1fc3e954e41edc7470ca7 | [] | 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,349 | lean | /-
Copyright (c) 2020 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.category_theory.monoidal.functorial
import Mathlib.category_theory.monoidal.functor_category
import Mathlib.category_theory.limits.limits
import Mathlib.PostPort
universes u v
namespace Mathlib
/-!
# `lim : (J β₯€ C) β₯€ C` is lax monoidal when `C` is a monoidal category.
When `C` is a monoidal category, the functorial association `F β¦ limit F` is lax monoidal,
i.e. there are morphisms
* `lim_lax.Ξ΅ : (π_ C) β limit (π_ (J β₯€ C))`
* `lim_lax.ΞΌ : limit F β limit G βΆ limit (F β G)`
satisfying the laws of a lax monoidal functor.
-/
namespace category_theory.limits
protected instance limit_functorial {J : Type v} [small_category J] {C : Type u} [category C]
[has_limits C] : functorial fun (F : J β₯€ C) => limit F :=
functorial.mk (functor.map lim)
@[simp] theorem limit_functorial_map {J : Type v} [small_category J] {C : Type u} [category C]
[has_limits C] {F : J β₯€ C} {G : J β₯€ C} (Ξ± : F βΆ G) :
map (fun (F : J β₯€ C) => limit F) Ξ± = functor.map lim Ξ± :=
rfl
protected instance limit_lax_monoidal {J : Type v} [small_category J] {C : Type u} [category C]
[has_limits C] [monoidal_category C] : lax_monoidal fun (F : J β₯€ C) => limit F :=
lax_monoidal.mk
(limit.lift (functor.obj (functor.const J) π_) (cone.mk π_ (nat_trans.mk fun (j : J) => π)))
fun (F G : J β₯€ C) =>
limit.lift (F β G)
(cone.mk (limit F β limit G) (nat_trans.mk fun (j : J) => limit.Ο F j β limit.Ο G j))
/-- The limit functor `F β¦ limit F` bundled as a lax monoidal functor. -/
def lim_lax {J : Type v} [small_category J] {C : Type u} [category C] [has_limits C]
[monoidal_category C] : lax_monoidal_functor (J β₯€ C) C :=
lax_monoidal_functor.of fun (F : J β₯€ C) => limit F
@[simp] theorem lim_lax_obj {J : Type v} [small_category J] {C : Type u} [category C] [has_limits C]
[monoidal_category C] (F : J β₯€ C) :
functor.obj (lax_monoidal_functor.to_functor lim_lax) F = limit F :=
rfl
theorem lim_lax_obj' {J : Type v} [small_category J] {C : Type u} [category C] [has_limits C]
[monoidal_category C] (F : J β₯€ C) :
functor.obj (lax_monoidal_functor.to_functor lim_lax) F = functor.obj lim F :=
rfl
@[simp] theorem lim_lax_map {J : Type v} [small_category J] {C : Type u} [category C] [has_limits C]
[monoidal_category C] {F : J β₯€ C} {G : J β₯€ C} (Ξ± : F βΆ G) :
functor.map (lax_monoidal_functor.to_functor lim_lax) Ξ± = functor.map lim Ξ± :=
rfl
@[simp] theorem lim_lax_Ξ΅ {J : Type v} [small_category J] {C : Type u} [category C] [has_limits C]
[monoidal_category C] :
lax_monoidal_functor.Ξ΅ lim_lax =
limit.lift (functor.obj (functor.const J) π_)
(cone.mk π_ (nat_trans.mk fun (j : J) => π)) :=
rfl
@[simp] theorem lim_lax_ΞΌ {J : Type v} [small_category J] {C : Type u} [category C] [has_limits C]
[monoidal_category C] (F : J β₯€ C) (G : J β₯€ C) :
lax_monoidal_functor.ΞΌ lim_lax F G =
limit.lift (F β G)
(cone.mk (limit F β limit G) (nat_trans.mk fun (j : J) => limit.Ο F j β limit.Ο G j)) :=
rfl
end Mathlib |
e31b1770a7aa1986dfeee67655800741aabc9e99 | 618003631150032a5676f229d13a079ac875ff77 | /src/algebra/iterate_hom.lean | d09735f139da3636d52dfcb7ac9497372403c03c | [
"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 | 3,479 | lean | /-
Copyright (c) 2020 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Yury Kudryashov
-/
import algebra.group_power
import logic.function.iterate
/-!
# Iterates of monoid and ring homomorphisms
Iterate of a monoid/ring homomorphism is a monoid/ring homomorphism but it has a wrong type, so Lean
can't apply lemmas like `monoid_hom.map_one` to `f^[n] 1`. Though it is possible to define
a monoid structure on the endomorphisms, quite often we do not want to convert from
`M β* M` to (not yet defined) `monoid.End M` and from `f^[n]` to `f^n` just to apply a simple lemma.
So, we restate standard `*_hom.map_*` lemmas under names `*_hom.iterate_map_*`.
-/
open function
variables {M : Type*} {N : Type*} {G : Type*} {H : Type*}
namespace monoid_hom
variables [monoid M] [monoid N] [group G] [group H]
@[simp, to_additive]
theorem iterate_map_one (f : M β* M) (n : β) : f^[n] 1 = 1 :=
iterate_fixed f.map_one n
@[simp, to_additive]
theorem iterate_map_mul (f : M β* M) (n : β) (x y) :
f^[n] (x * y) = (f^[n] x) * (f^[n] y) :=
semiconjβ.iterate f.map_mul n x y
@[simp, to_additive]
theorem iterate_map_inv (f : G β* G) (n : β) (x) :
f^[n] (xβ»ΒΉ) = (f^[n] x)β»ΒΉ :=
commute.iterate_left f.map_inv n x
theorem iterate_map_pow (f : M β* M) (a) (n m : β) : f^[n] (a^m) = (f^[n] a)^m :=
commute.iterate_left (Ξ» x, f.map_pow x m) n a
theorem iterate_map_gpow (f : G β* G) (a) (n : β) (m : β€) : f^[n] (a^m) = (f^[n] a)^m :=
commute.iterate_left (Ξ» x, f.map_gpow x m) n a
end monoid_hom
namespace add_monoid_hom
variables [add_monoid M] [add_monoid N] [add_group G] [add_group H]
@[simp]
theorem iterate_map_sub (f : G β+ G) (n : β) (x y) :
f^[n] (x - y) = (f^[n] x) - (f^[n] y) :=
semiconjβ.iterate f.map_sub n x y
theorem iterate_map_smul (f : M β+ M) (n m : β) (x : M) :
f^[n] (m β’β x) = m β’β (f^[n] x) :=
f.to_multiplicative.iterate_map_pow x n m
theorem iterate_map_gsmul (f : G β+ G) (n : β) (m : β€) (x : G) :
f^[n] (m β’β€ x) = m β’β€ (f^[n] x) :=
f.to_multiplicative.iterate_map_gpow x n m
end add_monoid_hom
namespace ring_hom
section semiring
variables {R : Type*} [semiring R] (f : R β+* R) (n : β) (x y : R)
lemma coe_pow : β n : β, β(f^n) = (f^[n])
| 0 := rfl
| (n+1) := by { simp only [function.iterate_succ, pow_succ', coe_mul, coe_pow n] }
theorem iterate_map_one : f^[n] 1 = 1 := f.to_monoid_hom.iterate_map_one n
theorem iterate_map_zero : f^[n] 0 = 0 := f.to_add_monoid_hom.iterate_map_zero n
theorem iterate_map_add : f^[n] (x + y) = (f^[n] x) + (f^[n] y) :=
f.to_add_monoid_hom.iterate_map_add n x y
theorem iterate_map_mul : f^[n] (x * y) = (f^[n] x) * (f^[n] y) :=
f.to_monoid_hom.iterate_map_mul n x y
theorem iterate_map_pow (a) (n m : β) : f^[n] (a^m) = (f^[n] a)^m :=
f.to_monoid_hom.iterate_map_pow a n m
theorem iterate_map_smul (n m : β) (x : R) :
f^[n] (m β’β x) = m β’β (f^[n] x) :=
f.to_add_monoid_hom.iterate_map_smul n m x
end semiring
variables {R : Type*} [ring R] (f : R β+* R) (n : β) (x y : R)
theorem iterate_map_sub : f^[n] (x - y) = (f^[n] x) - (f^[n] y) :=
f.to_add_monoid_hom.iterate_map_sub n x y
theorem iterate_map_neg : f^[n] (-x) = -(f^[n] x) :=
f.to_add_monoid_hom.iterate_map_neg n x
theorem iterate_map_gsmul (n : β) (m : β€) (x : R) :
f^[n] (m β’β€ x) = m β’β€ (f^[n] x) :=
f.to_add_monoid_hom.iterate_map_gsmul n m x
end ring_hom
|
237cd13cc7d18b12c500c0bb3576f2a6affd64a6 | a45212b1526d532e6e83c44ddca6a05795113ddc | /src/number_theory/pell.lean | eb2f2479dfea5b94d133ff91021352acd4f7edff | [
"Apache-2.0"
] | permissive | fpvandoorn/mathlib | b21ab4068db079cbb8590b58fda9cc4bc1f35df4 | b3433a51ea8bc07c4159c1073838fc0ee9b8f227 | refs/heads/master | 1,624,791,089,608 | 1,556,715,231,000 | 1,556,715,231,000 | 165,722,980 | 5 | 0 | Apache-2.0 | 1,552,657,455,000 | 1,547,494,646,000 | Lean | UTF-8 | Lean | false | false | 37,599 | lean | /-
Copyright (c) 2017 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import data.nat.modeq data.zsqrtd.basic
namespace pell
open nat
section
parameters {a : β} (a1 : a > 1)
include a1
private def d := a*a - 1
@[simp] theorem d_pos : 0 < d := nat.sub_pos_of_lt (mul_lt_mul a1 (le_of_lt a1) dec_trivial dec_trivial : 1*1<a*a)
/-- The Pell sequences, defined together in mutual recursion. -/
def pell : β β β Γ β :=
Ξ»n, nat.rec_on n (1, 0) (Ξ»n xy, (xy.1*a + d*xy.2, xy.1 + xy.2*a))
/-- The Pell `x` sequence. -/
def xn (n : β) : β := (pell n).1
/-- The Pell `y` sequence. -/
def yn (n : β) : β := (pell n).2
@[simp] theorem pell_val (n : β) : pell n = (xn n, yn n) :=
show pell n = ((pell n).1, (pell n).2), from match pell n with (a, b) := rfl end
@[simp] theorem xn_zero : xn 0 = 1 := rfl
@[simp] theorem yn_zero : yn 0 = 0 := rfl
@[simp] theorem xn_succ (n : β) : xn (n+1) = xn n * a + d * yn n := rfl
@[simp] theorem yn_succ (n : β) : yn (n+1) = xn n + yn n * a := rfl
@[simp] theorem xn_one : xn 1 = a := by simp
@[simp] theorem yn_one : yn 1 = 1 := by simp
def xz (n : β) : β€ := xn n
def yz (n : β) : β€ := yn n
def az : β€ := a
theorem asq_pos : 0 < a*a :=
le_trans (le_of_lt a1) (by have := @nat.mul_le_mul_left 1 a a (le_of_lt a1); rwa mul_one at this)
theorem dz_val : βd = az*az - 1 :=
have 1 β€ a*a, from asq_pos,
show β(a*a - 1) = _, by rw int.coe_nat_sub this; refl
@[simp] theorem xz_succ (n : β) : xz (n+1) = xz n * az + βd * yz n := rfl
@[simp] theorem yz_succ (n : β) : yz (n+1) = xz n + yz n * az := rfl
/-- The Pell sequence can also be viewed as an element of `β€βd` -/
def pell_zd (n : β) : β€βd := β¨xn n, yn nβ©
@[simp] theorem pell_zd_re (n : β) : (pell_zd n).re = xn n := rfl
@[simp] theorem pell_zd_im (n : β) : (pell_zd n).im = yn n := rfl
/-- The property of being a solution to the Pell equation, expressed
as a property of elements of `β€βd`. -/
def is_pell : β€βd β Prop | β¨x, yβ© := x*x - d*y*y = 1
theorem is_pell_nat {x y : β} : is_pell β¨x, yβ© β x*x - d*y*y = 1 :=
β¨Ξ»h, int.coe_nat_inj (by rw int.coe_nat_sub (int.le_of_coe_nat_le_coe_nat $ int.le.intro_sub h); exact h),
Ξ»h, show ((x*x : β) - (d*y*y:β) : β€) = 1, by rw [β int.coe_nat_sub $ le_of_lt $ nat.lt_of_sub_eq_succ h, h]; reflβ©
theorem is_pell_norm : Ξ {b : β€βd}, is_pell b β b * b.conj = 1
| β¨x, yβ© := by simp [zsqrtd.ext, is_pell, mul_comm]
theorem is_pell_mul {b c : β€βd} (hb : is_pell b) (hc : is_pell c) : is_pell (b * c) :=
is_pell_norm.2 (by simp [mul_comm, mul_left_comm,
zsqrtd.conj_mul, pell.is_pell_norm.1 hb, pell.is_pell_norm.1 hc])
theorem is_pell_conj : β {b : β€βd}, is_pell b β is_pell b.conj | β¨x, yβ© :=
by simp [is_pell, zsqrtd.conj]
@[simp] theorem pell_zd_succ (n : β) : pell_zd (n+1) = pell_zd n * β¨a, 1β© :=
by simp [zsqrtd.ext]
theorem is_pell_one : is_pell β¨a, 1β© :=
show az*az-d*1*1=1, by simp [dz_val]
theorem is_pell_pell_zd : β (n : β), is_pell (pell_zd n)
| 0 := rfl
| (n+1) := let o := is_pell_one in by simp; exact pell.is_pell_mul (is_pell_pell_zd n) o
@[simp] theorem pell_eqz (n : β) : xz n * xz n - d * yz n * yz n = 1 := is_pell_pell_zd n
@[simp] theorem pell_eq (n : β) : xn n * xn n - d * yn n * yn n = 1 :=
let pn := pell_eqz n in
have h : (β(xn n * xn n) : β€) - β(d * yn n * yn n) = 1,
by repeat {rw int.coe_nat_mul}; exact pn,
have hl : d * yn n * yn n β€ xn n * xn n, from
int.le_of_coe_nat_le_coe_nat $ int.le.intro $ add_eq_of_eq_sub' $ eq.symm h,
int.coe_nat_inj (by rw int.coe_nat_sub hl; exact h)
instance dnsq : zsqrtd.nonsquare d := β¨Ξ»n h,
have n*n + 1 = a*a, by rw β h; exact nat.succ_pred_eq_of_pos (asq_pos a1),
have na : n < a, from nat.mul_self_lt_mul_self_iff.2 (by rw β this; exact nat.lt_succ_self _),
have (n+1)*(n+1) β€ n*n + 1, by rw this; exact nat.mul_self_le_mul_self na,
have n+n β€ 0, from @nat.le_of_add_le_add_right (n*n + 1) _ _ (by simpa [mul_add, mul_comm, mul_left_comm]),
ne_of_gt d_pos $ by rw nat.eq_zero_of_le_zero (le_trans (nat.le_add_left _ _) this) at h; exact hβ©
theorem xn_ge_a_pow : β (n : β), a^n β€ xn n
| 0 := le_refl 1
| (n+1) := by simp [nat.pow_succ]; exact le_trans
(nat.mul_le_mul_right _ (xn_ge_a_pow n)) (nat.le_add_right _ _)
theorem n_lt_a_pow : β (n : β), n < a^n
| 0 := nat.le_refl 1
| (n+1) := begin have IH := n_lt_a_pow n,
have : a^n + a^n β€ a^n * a,
{ rw β mul_two, exact nat.mul_le_mul_left _ a1 },
simp [nat.pow_succ], refine lt_of_lt_of_le _ this,
exact add_lt_add_of_lt_of_le IH (lt_of_le_of_lt (nat.zero_le _) IH)
end
theorem n_lt_xn (n) : n < xn n :=
lt_of_lt_of_le (n_lt_a_pow n) (xn_ge_a_pow n)
theorem x_pos (n) : xn n > 0 :=
lt_of_le_of_lt (nat.zero_le n) (n_lt_xn n)
lemma eq_pell_lem : βn (b:β€βd), 1 β€ b β is_pell b β pell_zd n β₯ b β βn, b = pell_zd n
| 0 b := Ξ»h1 hp hl, β¨0, @zsqrtd.le_antisymm _ dnsq _ _ hl h1β©
| (n+1) b := Ξ»h1 hp h,
have a1p : (0:β€βd) β€ β¨a, 1β©, from trivial,
have am1p : (0:β€βd) β€ β¨a, -1β©, from show (_:nat) β€ _, by simp; exact nat.pred_le _,
have a1m : (β¨a, 1β© * β¨a, -1β© : β€βd) = 1, from is_pell_norm.1 is_pell_one,
if ha : b β₯ β¨βa, 1β© then
let β¨m, eβ© := eq_pell_lem n (b * β¨a, -1β©)
(by rw β a1m; exact mul_le_mul_of_nonneg_right ha am1p)
(is_pell_mul hp (is_pell_conj.1 is_pell_one))
(by have t := mul_le_mul_of_nonneg_right h am1p; rwa [pell_zd_succ, mul_assoc, a1m, mul_one] at t) in
β¨m+1, by rw [show b = b * β¨a, -1β© * β¨a, 1β©, by rw [mul_assoc, eq.trans (mul_comm _ _) a1m]; simp, pell_zd_succ, e]β©
else
suffices Β¬1 < b, from β¨0, show b = 1, from (or.resolve_left (lt_or_eq_of_le h1) this).symmβ©, Ξ»h1l,
by cases b with x y; exact
have bm : (_*β¨_,_β© :β€β(d a1)) = 1, from pell.is_pell_norm.1 hp,
have y0l : (0:β€β(d a1)) < β¨x - x, y - -yβ©, from sub_lt_sub h1l $ Ξ»(hn : (1:β€β(d a1)) β€ β¨x, -yβ©),
by have t := mul_le_mul_of_nonneg_left hn (le_trans zero_le_one h1); rw [bm, mul_one] at t; exact h1l t,
have yl2 : (β¨_, _β© : β€β_) < β¨_, _β©, from
show (β¨x, yβ© - β¨x, -yβ© : β€β(d a1)) < β¨a, 1β© - β¨a, -1β©, from
sub_lt_sub (by exact ha) $ Ξ»(hn : (β¨x, -yβ© : β€β(d a1)) β€ β¨a, -1β©),
by have t := mul_le_mul_of_nonneg_right (mul_le_mul_of_nonneg_left hn (le_trans zero_le_one h1)) a1p;
rw [bm, one_mul, mul_assoc, eq.trans (mul_comm _ _) a1m, mul_one] at t; exact ha t,
by simp at y0l; simp at yl2; exact
match y, y0l, (yl2 : (β¨_, _β© : β€β_) < β¨_, _β©) with
| 0, y0l, yl2 := y0l (le_refl 0)
| (y+1 : β), y0l, yl2 := yl2 (zsqrtd.le_of_le_le (le_refl 0)
(let t := int.coe_nat_le_coe_nat_of_le (nat.succ_pos y) in add_le_add t t))
| -[1+y], y0l, yl2 := y0l trivial
end
theorem eq_pell_zd (b : β€βd) (b1 : 1 β€ b) (hp : is_pell b) : βn, b = pell_zd n :=
let β¨n, hβ© := @zsqrtd.le_arch d b in
eq_pell_lem n b b1 hp $ zsqrtd.le_trans h $ by rw zsqrtd.coe_nat_val; exact
zsqrtd.le_of_le_le
(int.coe_nat_le_coe_nat_of_le $ le_of_lt $ n_lt_xn _ _) (int.coe_zero_le _)
theorem eq_pell {x y : β} (hp : x*x - d*y*y = 1) : βn, x = xn n β§ y = yn n :=
have (1:β€βd) β€ β¨x, yβ©, from match x, hp with
| 0, (hp : 0 - _ = 1) := by rw nat.zero_sub at hp; contradiction
| (x+1), hp := zsqrtd.le_of_le_le (int.coe_nat_le_coe_nat_of_le $ nat.succ_pos x) (int.coe_zero_le _)
end,
let β¨m, eβ© := eq_pell_zd β¨x, yβ© this (is_pell_nat.2 hp) in
β¨m, match x, y, e with ._, ._, rfl := β¨rfl, rflβ© endβ©
theorem pell_zd_add (m) : β n, pell_zd (m + n) = pell_zd m * pell_zd n
| 0 := (mul_one _).symm
| (n+1) := by rw[β add_assoc, pell_zd_succ, pell_zd_succ, pell_zd_add n, β mul_assoc]
theorem xn_add (m n) : xn (m + n) = xn m * xn n + d * yn m * yn n :=
by injection (pell_zd_add _ m n) with h _;
repeat {rw β int.coe_nat_add at h <|> rw β int.coe_nat_mul at h};
exact int.coe_nat_inj h
theorem yn_add (m n) : yn (m + n) = xn m * yn n + yn m * xn n :=
by injection (pell_zd_add _ m n) with _ h;
repeat {rw β int.coe_nat_add at h <|> rw β int.coe_nat_mul at h};
exact int.coe_nat_inj h
theorem pell_zd_sub {m n} (h : n β€ m) : pell_zd (m - n) = pell_zd m * (pell_zd n).conj :=
let t := pell_zd_add n (m - n) in
by rw [nat.add_sub_of_le h] at t;
rw [t, mul_comm (pell_zd _ n) _, mul_assoc, (is_pell_norm _).1 (is_pell_pell_zd _ _), mul_one]
theorem xz_sub {m n} (h : n β€ m) : xz (m - n) = xz m * xz n - d * yz m * yz n :=
by injection (pell_zd_sub _ h) with h _; repeat {rw β neg_mul_eq_mul_neg at h}; exact h
theorem yz_sub {m n} (h : n β€ m) : yz (m - n) = xz n * yz m - xz m * yz n :=
by injection (pell_zd_sub a1 h) with _ h; repeat {rw β neg_mul_eq_mul_neg at h}; rw [add_comm, mul_comm] at h; exact h
theorem xy_coprime (n) : (xn n).coprime (yn n) :=
nat.coprime_of_dvd' $ Ξ»k kx ky,
let p := pell_eq n in by rw β p; exact
nat.dvd_sub (le_of_lt $ nat.lt_of_sub_eq_succ p)
(dvd_mul_of_dvd_right kx _) (dvd_mul_of_dvd_right ky _)
theorem y_increasing {m} : Ξ {n}, m < n β yn m < yn n
| 0 h := absurd h $ nat.not_lt_zero _
| (n+1) h :=
have yn m β€ yn n, from or.elim (lt_or_eq_of_le $ nat.le_of_succ_le_succ h)
(Ξ»hl, le_of_lt $ y_increasing hl) (Ξ»e, by rw e),
by simp; refine lt_of_le_of_lt _ (nat.lt_add_of_pos_left $ x_pos a1 n);
rw β mul_one (yn a1 m);
exact mul_le_mul this (le_of_lt a1) (nat.zero_le _) (nat.zero_le _)
theorem x_increasing {m} : Ξ {n}, m < n β xn m < xn n
| 0 h := absurd h $ nat.not_lt_zero _
| (n+1) h :=
have xn m β€ xn n, from or.elim (lt_or_eq_of_le $ nat.le_of_succ_le_succ h)
(Ξ»hl, le_of_lt $ x_increasing hl) (Ξ»e, by rw e),
by simp; refine lt_of_lt_of_le (lt_of_le_of_lt this _) (nat.le_add_right _ _);
have t := nat.mul_lt_mul_of_pos_left a1 (x_pos a1 n); rwa mul_one at t
theorem yn_ge_n : Ξ n, n β€ yn n
| 0 := nat.zero_le _
| (n+1) := show n < yn (n+1), from lt_of_le_of_lt (yn_ge_n n) (y_increasing $ nat.lt_succ_self n)
theorem y_mul_dvd (n) : βk, yn n β£ yn (n * k)
| 0 := dvd_zero _
| (k+1) := by rw [nat.mul_succ, yn_add]; exact
dvd_add (dvd_mul_left _ _) (dvd_mul_of_dvd_left (y_mul_dvd k) _)
theorem y_dvd_iff (m n) : yn m β£ yn n β m β£ n :=
β¨Ξ»h, nat.dvd_of_mod_eq_zero $ (nat.eq_zero_or_pos _).resolve_right $ Ξ»hp,
have co : nat.coprime (yn m) (xn (m * (n / m))), from nat.coprime.symm $
(xy_coprime _).coprime_dvd_right (y_mul_dvd m (n / m)),
have m0 : m > 0, from m.eq_zero_or_pos.resolve_left $
Ξ»e, by rw [e, nat.mod_zero] at hp; rw [e] at h; exact
have 0 < yn a1 n, from y_increasing _ hp,
ne_of_lt (y_increasing a1 hp) (eq_zero_of_zero_dvd h).symm,
by rw [β nat.mod_add_div n m, yn_add] at h; exact
not_le_of_gt (y_increasing _ $ nat.mod_lt n m0)
(nat.le_of_dvd (y_increasing _ hp) $ co.dvd_of_dvd_mul_right $
(nat.dvd_add_iff_right $ dvd_mul_of_dvd_right (y_mul_dvd _ _ _) _).2 h),
Ξ»β¨k, eβ©, by rw e; apply y_mul_dvdβ©
theorem xy_modeq_yn (n) :
βk, xn (n * k) β‘ (xn n)^k [MOD (yn n)^2]
β§ yn (n * k) β‘ k * (xn n)^(k-1) * yn n [MOD (yn n)^3]
| 0 := by constructor; simp
| (k+1) :=
let β¨hx, hyβ© := xy_modeq_yn k in
have L : xn (n * k) * xn n + d * yn (n * k) * yn n β‘ xn n^k * xn n + 0 [MOD yn n^2], from
modeq.modeq_add (modeq.modeq_mul_right _ hx) $ modeq.modeq_zero_iff.2 $
by rw nat.pow_succ; exact
mul_dvd_mul_right (dvd_mul_of_dvd_right (modeq.modeq_zero_iff.1 $
(hy.modeq_of_dvd_of_modeq $ by simp [nat.pow_succ]).trans $ modeq.modeq_zero_iff.2 $
by simp [-mul_comm, -mul_assoc]) _) _,
have R : xn (n * k) * yn n + yn (n * k) * xn n β‘
xn n^k * yn n + k * xn n^k * yn n [MOD yn n^3], from
modeq.modeq_add (by rw nat.pow_succ; exact modeq.modeq_mul_right' _ hx) $
have k * xn n^(k - 1) * yn n * xn n = k * xn n^k * yn n,
by clear _let_match; cases k with k; simp [nat.pow_succ, mul_comm, mul_left_comm],
by rw β this; exact modeq.modeq_mul_right _ hy,
by rw [nat.add_sub_cancel, nat.mul_succ, xn_add, yn_add, nat.pow_succ (xn _ n),
nat.succ_mul, add_comm (k * xn _ n^k) (xn _ n^k), right_distrib];
exact β¨L, Rβ©
theorem ysq_dvd_yy (n) : yn n * yn n β£ yn (n * yn n) :=
modeq.modeq_zero_iff.1 $
((xy_modeq_yn n (yn n)).right.modeq_of_dvd_of_modeq $ by simp [nat.pow_succ]).trans
(modeq.modeq_zero_iff.2 $ by simp [mul_dvd_mul_left, mul_assoc])
theorem dvd_of_ysq_dvd {n t} (h : yn n * yn n β£ yn t) : yn n β£ t :=
have nt : n β£ t, from (y_dvd_iff n t).1 $ dvd_of_mul_left_dvd h,
n.eq_zero_or_pos.elim (Ξ»n0, by rw n0; rw n0 at nt; exact nt) $ Ξ»(n0l : n > 0),
let β¨k, keβ© := nt in
have yn n β£ k * (xn n)^(k-1), from
nat.dvd_of_mul_dvd_mul_right (y_increasing n0l) $ modeq.modeq_zero_iff.1 $
by have xm := (xy_modeq_yn a1 n k).right; rw β ke at xm; exact
(xm.modeq_of_dvd_of_modeq $ by simp [nat.pow_succ]).symm.trans
(modeq.modeq_zero_iff.2 h),
by rw ke; exact dvd_mul_of_dvd_right
(((xy_coprime _ _).pow_left _).symm.dvd_of_dvd_mul_right this) _
theorem pell_zd_succ_succ (n) : pell_zd (n + 2) + pell_zd n = (2 * a : β) * pell_zd (n + 1) :=
have (1:β€βd) + β¨a, 1β© * β¨a, 1β© = β¨a, 1β© * (2 * a),
by rw zsqrtd.coe_nat_val; change (β¨_,_β©:β€β(d a1))=β¨_,_β©;
rw dz_val; change az a1 with a; simp [mul_add, add_mul],
by simpa [mul_add, mul_comm, mul_left_comm] using congr_arg (* pell_zd a1 n) this
theorem xy_succ_succ (n) : xn (n + 2) + xn n = (2 * a) * xn (n + 1) β§
yn (n + 2) + yn n = (2 * a) * yn (n + 1) := begin
have := pell_zd_succ_succ a1 n, unfold pell_zd at this,
rw [β int.cast_coe_nat, zsqrtd.smul_val] at this,
injection this with hβ hβ,
split; apply int.coe_nat_inj; [simpa using hβ, simpa using hβ]
end
theorem xn_succ_succ (n) : xn (n + 2) + xn n = (2 * a) * xn (n + 1) := (xy_succ_succ n).1
theorem yn_succ_succ (n) : yn (n + 2) + yn n = (2 * a) * yn (n + 1) := (xy_succ_succ n).2
theorem xz_succ_succ (n) : xz (n + 2) = (2 * a : β) * xz (n + 1) - xz n :=
eq_sub_of_add_eq $ by delta xz; rw [β int.coe_nat_add, β int.coe_nat_mul, xn_succ_succ]
theorem yz_succ_succ (n) : yz (n + 2) = (2 * a : β) * yz (n + 1) - yz n :=
eq_sub_of_add_eq $ by delta yz; rw [β int.coe_nat_add, β int.coe_nat_mul, yn_succ_succ]
theorem yn_modeq_a_sub_one : β n, yn n β‘ n [MOD a-1]
| 0 := by simp
| 1 := by simp
| (n+2) := modeq.modeq_add_cancel_right (yn_modeq_a_sub_one n) $
have 2*(n+1) = n+2+n, by simp [two_mul],
by rw [yn_succ_succ, β this];
refine modeq.modeq_mul (modeq.modeq_mul_left 2 (_ : a β‘ 1 [MOD a-1])) (yn_modeq_a_sub_one (n+1));
exact (modeq.modeq_of_dvd $ by rw [int.coe_nat_sub $ le_of_lt a1]; apply dvd_refl).symm
theorem yn_modeq_two : β n, yn n β‘ n [MOD 2]
| 0 := by simp
| 1 := by simp
| (n+2) := modeq.modeq_add_cancel_right (yn_modeq_two n) $
have 2*(n+1) = n+2+n, by simp [two_mul],
by rw [yn_succ_succ, β this];
refine modeq.modeq_mul _ (yn_modeq_two (n+1));
exact modeq.trans
(modeq.modeq_zero_iff.2 $ by simp)
(modeq.modeq_zero_iff.2 $ by simp).symm
-- TODO(Mario): Hopefully a tactic will be able to dispense this lemma
lemma x_sub_y_dvd_pow_lem (y2 y1 y0 yn1 yn0 xn1 xn0 ay a2 : β€) :
(a2 * yn1 - yn0) * ay + y2 - (a2 * xn1 - xn0) =
y2 - a2 * y1 + y0 + a2 * (yn1 * ay + y1 - xn1) - (yn0 * ay + y0 - xn0) :=
calc (a2 * yn1 - yn0) * ay + y2 - (a2 * xn1 - xn0)
= a2 * yn1 * ay - yn0 * ay + y2 - (a2 * xn1 - xn0) : by rw [mul_sub_right_distrib]
... = y2 + a2 * (yn1 * ay) - a2 * xn1 - yn0 * ay + xn0 : by simp [mul_comm, mul_left_comm]
... = y2 + a2 * (yn1 * ay) - a2 * y1 + a2 * y1 - a2 * xn1 - yn0 * ay + y0 - y0 + xn0 : by rw [add_sub_cancel, sub_add_cancel]
... = y2 - a2 * y1 + y0 + a2 * (yn1 * ay + y1 - xn1) - (yn0 * ay + y0 - xn0) : by simp [mul_add]
theorem x_sub_y_dvd_pow (y : β) :
β n, (2*a*y - y*y - 1 : β€) β£ yz n * (a - y) + β(y^n) - xz n
| 0 := by simp [xz, yz, int.coe_nat_zero, int.coe_nat_one]
| 1 := by simp [xz, yz, int.coe_nat_zero, int.coe_nat_one]
| (n+2) :=
have (2*a*y - y*y - 1 : β€) β£ β(y^(n + 2)) - β(2 * a) * β(y^(n + 1)) + β(y^n), from
β¨-β(y^n), by simp [nat.pow_succ, mul_add, int.coe_nat_mul,
show ((2:β):β€) = 2, from rfl, mul_comm, mul_left_comm]β©,
by rw [xz_succ_succ, yz_succ_succ, x_sub_y_dvd_pow_lem a1 β(y^(n+2)) β(y^(n+1)) β(y^n)]; exact
dvd_sub (dvd_add this $ dvd_mul_of_dvd_right (x_sub_y_dvd_pow (n+1)) _) (x_sub_y_dvd_pow n)
theorem xn_modeq_x2n_add_lem (n j) : xn n β£ d * yn n * (yn n * xn j) + xn j :=
have h1 : d * yn n * (yn n * xn j) + xn j = (d * yn n * yn n + 1) * xn j,
by simp [add_mul, mul_assoc],
have h2 : d * yn n * yn n + 1 = xn n * xn n, by apply int.coe_nat_inj;
repeat {rw int.coe_nat_add <|> rw int.coe_nat_mul}; exact
add_eq_of_eq_sub' (eq.symm $ pell_eqz _ _),
by rw h2 at h1; rw [h1, mul_assoc]; exact dvd_mul_right _ _
theorem xn_modeq_x2n_add (n j) : xn (2 * n + j) + xn j β‘ 0 [MOD xn n] :=
by rw [two_mul, add_assoc, xn_add, add_assoc]; exact
show _ β‘ 0+0 [MOD xn a1 n], from modeq.modeq_add (modeq.modeq_zero_iff.2 $ dvd_mul_right (xn a1 n) (xn a1 (n + j))) $
by rw [yn_add, left_distrib, add_assoc]; exact
show _ β‘ 0+0 [MOD xn a1 n], from modeq.modeq_add (modeq.modeq_zero_iff.2 $ dvd_mul_of_dvd_right (dvd_mul_right _ _) _) $
modeq.modeq_zero_iff.2 $ xn_modeq_x2n_add_lem _ _ _
lemma xn_modeq_x2n_sub_lem {n j} (h : j β€ n) : xn (2 * n - j) + xn j β‘ 0 [MOD xn n] :=
have h1 : xz n β£ βd * yz n * yz (n - j) + xz j, by rw [yz_sub _ h, mul_sub_left_distrib, sub_add_eq_add_sub]; exact
dvd_sub
(by delta xz; delta yz;
repeat {rw β int.coe_nat_add <|> rw β int.coe_nat_mul}; rw mul_comm (xn a1 j) (yn a1 n);
exact int.coe_nat_dvd.2 (xn_modeq_x2n_add_lem _ _ _))
(dvd_mul_of_dvd_right (dvd_mul_right _ _) _),
by rw [two_mul, nat.add_sub_assoc h, xn_add, add_assoc]; exact
show _ β‘ 0+0 [MOD xn a1 n], from modeq.modeq_add (modeq.modeq_zero_iff.2 $ dvd_mul_right _ _) $
modeq.modeq_zero_iff.2 $ int.coe_nat_dvd.1 $ by simpa [xz, yz] using h1
theorem xn_modeq_x2n_sub {n j} (h : j β€ 2 * n) : xn (2 * n - j) + xn j β‘ 0 [MOD xn n] :=
(le_total j n).elim xn_modeq_x2n_sub_lem
(Ξ»jn, have 2 * n - j + j β€ n + j, by rw [nat.sub_add_cancel h, two_mul]; exact nat.add_le_add_left jn _,
let t := xn_modeq_x2n_sub_lem (nat.le_of_add_le_add_right this) in by rwa [nat.sub_sub_self h, add_comm] at t)
theorem xn_modeq_x4n_add (n j) : xn (4 * n + j) β‘ xn j [MOD xn n] :=
modeq.modeq_add_cancel_right (modeq.refl $ xn (2 * n + j)) $
by refine @modeq.trans _ _ 0 _ _ (by rw add_comm; exact (xn_modeq_x2n_add _ _ _).symm);
rw [show 4*n = 2*n + 2*n, from right_distrib 2 2 n, add_assoc]; apply xn_modeq_x2n_add
theorem xn_modeq_x4n_sub {n j} (h : j β€ 2 * n) : xn (4 * n - j) β‘ xn j [MOD xn n] :=
have h' : j β€ 2*n, from le_trans h (by rw nat.succ_mul; apply nat.le_add_left),
modeq.modeq_add_cancel_right (modeq.refl $ xn (2 * n - j)) $
by refine @modeq.trans _ _ 0 _ _ (by rw add_comm; exact (xn_modeq_x2n_sub _ h).symm);
rw [show 4*n = 2*n + 2*n, from right_distrib 2 2 n, nat.add_sub_assoc h']; apply xn_modeq_x2n_add
theorem eq_of_xn_modeq_lem1 {i n} (npos : n > 0) : Ξ {j}, i < j β j < n β xn i % xn n < xn j % xn n
| 0 ij _ := absurd ij (nat.not_lt_zero _)
| (j+1) ij jn :=
suffices xn j % xn n < xn (j + 1) % xn n, from
(lt_or_eq_of_le (nat.le_of_succ_le_succ ij)).elim
(Ξ»h, lt_trans (eq_of_xn_modeq_lem1 h (le_of_lt jn)) this)
(Ξ»h, by rw h; exact this),
by rw [nat.mod_eq_of_lt (x_increasing _ (nat.lt_of_succ_lt jn)), nat.mod_eq_of_lt (x_increasing _ jn)];
exact x_increasing _ (nat.lt_succ_self _)
theorem eq_of_xn_modeq_lem2 {n} (h : 2 * xn n = xn (n + 1)) : a = 2 β§ n = 0 :=
by rw [xn_succ, mul_comm] at h; exact
have n = 0, from n.eq_zero_or_pos.resolve_right $ Ξ»np,
ne_of_lt (lt_of_le_of_lt (nat.mul_le_mul_left _ a1)
(nat.lt_add_of_pos_right $ mul_pos (d_pos a1) (y_increasing a1 np))) h,
by cases this; simp at h; exact β¨h.symm, rflβ©
theorem eq_of_xn_modeq_lem3 {i n} (npos : n > 0) :
Ξ {j}, i < j β j β€ 2 * n β j β n β Β¬(a = 2 β§ n = 1 β§ i = 0 β§ j = 2) β xn i % xn n < xn j % xn n
| 0 ij _ _ _ := absurd ij (nat.not_lt_zero _)
| (j+1) ij j2n jnn ntriv :=
have lem2 : βk > n, k β€ 2*n β (β(xn k % xn n) : β€) = xn n - xn (2 * n - k), from Ξ»k kn k2n,
let k2nl := lt_of_add_lt_add_right $ show 2*n-k+k < n+k, by
{rw nat.sub_add_cancel, rw two_mul; exact (add_lt_add_left kn n), exact k2n } in
have xle : xn (2 * n - k) β€ xn n, from le_of_lt $ x_increasing k2nl,
suffices xn k % xn n = xn n - xn (2 * n - k), by rw [this, int.coe_nat_sub xle],
by {
rw β nat.mod_eq_of_lt (nat.sub_lt (x_pos a1 n) (x_pos a1 (2 * n - k))),
apply modeq.modeq_add_cancel_right (modeq.refl (xn a1 (2 * n - k))),
rw [nat.sub_add_cancel xle],
have t := xn_modeq_x2n_sub_lem a1 (le_of_lt k2nl),
rw nat.sub_sub_self k2n at t,
exact t.trans (modeq.modeq_zero_iff.2 $ dvd_refl _).symm },
(lt_trichotomy j n).elim
(Ξ» (jn : j < n), eq_of_xn_modeq_lem1 npos ij (lt_of_le_of_ne jn jnn)) $ Ξ»o, o.elim
(Ξ» (jn : j = n), by {
cases jn,
apply int.lt_of_coe_nat_lt_coe_nat,
rw [lem2 (n+1) (nat.lt_succ_self _) j2n,
show 2 * n - (n + 1) = n - 1, by rw[two_mul, β nat.sub_sub, nat.add_sub_cancel]],
refine lt_sub_left_of_add_lt (int.coe_nat_lt_coe_nat_of_lt _),
cases (lt_or_eq_of_le $ nat.le_of_succ_le_succ ij) with lin ein,
{ rw nat.mod_eq_of_lt (x_increasing _ lin),
have ll : xn a1 (n-1) + xn a1 (n-1) β€ xn a1 n,
{ rw [β two_mul, mul_comm, show xn a1 n = xn a1 (n-1+1), by rw [nat.sub_add_cancel npos], xn_succ],
exact le_trans (nat.mul_le_mul_left _ a1) (nat.le_add_right _ _) },
have npm : (n-1).succ = n := nat.succ_pred_eq_of_pos npos,
have il : i β€ n - 1 := by apply nat.le_of_succ_le_succ; rw npm; exact lin,
cases lt_or_eq_of_le il with ill ile,
{ exact lt_of_lt_of_le (nat.add_lt_add_left (x_increasing a1 ill) _) ll },
{ rw ile,
apply lt_of_le_of_ne ll,
rw β two_mul,
exact Ξ»e, ntriv $
let β¨a2, s1β© := @eq_of_xn_modeq_lem2 _ a1 (n-1) (by rw[nat.sub_add_cancel npos]; exact e) in
have n1 : n = 1, from le_antisymm (nat.le_of_sub_eq_zero s1) npos,
by rw [ile, a2, n1]; exact β¨rfl, rfl, rfl, rflβ© } },
{ rw [ein, nat.mod_self, add_zero],
exact x_increasing _ (nat.pred_lt $ ne_of_gt npos) } })
(Ξ» (jn : j > n),
have lem1 : j β n β xn j % xn n < xn (j + 1) % xn n β xn i % xn n < xn (j + 1) % xn n, from Ξ»jn s,
(lt_or_eq_of_le (nat.le_of_succ_le_succ ij)).elim
(Ξ»h, lt_trans (eq_of_xn_modeq_lem3 h (le_of_lt j2n) jn $ Ξ»β¨a1, n1, i0, j2β©,
by rw [n1, j2] at j2n; exact absurd j2n dec_trivial) s)
(Ξ»h, by rw h; exact s),
lem1 (ne_of_gt jn) $ int.lt_of_coe_nat_lt_coe_nat $ by {
rw [lem2 j jn (le_of_lt j2n), lem2 (j+1) (nat.le_succ_of_le jn) j2n],
refine sub_lt_sub_left (int.coe_nat_lt_coe_nat_of_lt $ x_increasing _ _) _,
rw [nat.sub_succ],
exact nat.pred_lt (ne_of_gt $ nat.sub_pos_of_lt j2n) })
theorem eq_of_xn_modeq_le {i j n} (npos : n > 0) (ij : i β€ j) (j2n : j β€ 2 * n) (h : xn i β‘ xn j [MOD xn n])
(ntriv : Β¬(a = 2 β§ n = 1 β§ i = 0 β§ j = 2)) : i = j :=
(lt_or_eq_of_le ij).resolve_left $ Ξ»ij',
if jn : j = n then by {
refine ne_of_gt _ h,
rw [jn, nat.mod_self],
have x0 : xn a1 0 % xn a1 n > 0 := by rw [nat.mod_eq_of_lt (x_increasing a1 npos)]; exact dec_trivial,
cases i with i, exact x0,
rw jn at ij',
exact lt_trans x0 (eq_of_xn_modeq_lem3 _ npos (nat.succ_pos _) (le_trans ij j2n) (ne_of_lt ij') $
Ξ»β¨a1, n1, _, i2β©, by rw [n1, i2] at ij'; exact absurd ij' dec_trivial)
} else ne_of_lt (eq_of_xn_modeq_lem3 npos ij' j2n jn ntriv) h
theorem eq_of_xn_modeq {i j n} (npos : n > 0) (i2n : i β€ 2 * n) (j2n : j β€ 2 * n) (h : xn i β‘ xn j [MOD xn n])
(ntriv : a = 2 β n = 1 β (i = 0 β j β 2) β§ (i = 2 β j β 0)) : i = j :=
(le_total i j).elim
(Ξ»ij, eq_of_xn_modeq_le npos ij j2n h $ Ξ»β¨a2, n1, i0, j2β©, (ntriv a2 n1).left i0 j2)
(Ξ»ij, (eq_of_xn_modeq_le npos ij i2n h.symm $ Ξ»β¨a2, n1, j0, i2β©, (ntriv a2 n1).right i2 j0).symm)
theorem eq_of_xn_modeq' {i j n} (ipos : i > 0) (hin : i β€ n) (j4n : j β€ 4 * n) (h : xn j β‘ xn i [MOD xn n]) :
j = i β¨ j + i = 4 * n :=
have i2n : i β€ 2*n, by apply le_trans hin; rw two_mul; apply nat.le_add_left,
have npos : n > 0, from lt_of_lt_of_le ipos hin,
(le_or_gt j (2 * n)).imp
(Ξ»j2n : j β€ 2*n, eq_of_xn_modeq npos j2n i2n h $
Ξ»a2 n1, β¨Ξ»j0 i2, by rw [n1, i2] at hin; exact absurd hin dec_trivial,
Ξ»j2 i0, ne_of_gt ipos i0β©)
(Ξ»j2n : j > 2*n, suffices i = 4*n - j, by rw [this, nat.add_sub_of_le j4n],
have j42n : 4*n - j β€ 2*n, from @nat.le_of_add_le_add_right j _ _ $
by rw [nat.sub_add_cancel j4n, show 4*n = 2*n + 2*n, from right_distrib 2 2 n];
exact nat.add_le_add_left (le_of_lt j2n) _,
eq_of_xn_modeq npos i2n j42n
(h.symm.trans $ let t := xn_modeq_x4n_sub j42n in by rwa [nat.sub_sub_self j4n] at t)
(Ξ»a2 n1, β¨Ξ»i0, absurd i0 (ne_of_gt ipos), Ξ»i2, by rw[n1, i2] at hin; exact absurd hin dec_trivialβ©))
theorem modeq_of_xn_modeq {i j n} (ipos : i > 0) (hin : i β€ n) (h : xn j β‘ xn i [MOD xn n]) :
j β‘ i [MOD 4 * n] β¨ j + i β‘ 0 [MOD 4 * n] :=
let j' := j % (4 * n) in
have n4 : 4 * n > 0, from mul_pos dec_trivial (lt_of_lt_of_le ipos hin),
have jl : j' < 4 * n, from nat.mod_lt _ n4,
have jj : j β‘ j' [MOD 4 * n], by delta modeq; rw nat.mod_eq_of_lt jl,
have βj q, xn (j + 4 * n * q) β‘ xn j [MOD xn n], begin
intros j q, induction q with q IH, { simp },
rw[nat.mul_succ, β add_assoc, add_comm],
exact modeq.trans (xn_modeq_x4n_add _ _ _) IH
end,
or.imp
(Ξ»(ji : j' = i), by rwa β ji)
(Ξ»(ji : j' + i = 4 * n), (modeq.modeq_add jj (modeq.refl _)).trans $
by rw ji; exact modeq.modeq_zero_iff.2 (dvd_refl _))
(eq_of_xn_modeq' ipos hin (le_of_lt jl) $
(modeq.symm (by rw β nat.mod_add_div j (4*n); exact this j' _)).trans h)
end
theorem xy_modeq_of_modeq {a b c} (a1 : a > 1) (b1 : b > 1) (h : a β‘ b [MOD c]) :
β n, xn a1 n β‘ xn b1 n [MOD c] β§ yn a1 n β‘ yn b1 n [MOD c]
| 0 := by constructor; refl
| 1 := by simp; exact β¨h, modeq.refl 1β©
| (n+2) := β¨
modeq.modeq_add_cancel_right (xy_modeq_of_modeq n).left $
by rw [xn_succ_succ a1, xn_succ_succ b1]; exact
modeq.modeq_mul (modeq.modeq_mul_left _ h) (xy_modeq_of_modeq (n+1)).left,
modeq.modeq_add_cancel_right (xy_modeq_of_modeq n).right $
by rw [yn_succ_succ a1, yn_succ_succ b1]; exact
modeq.modeq_mul (modeq.modeq_mul_left _ h) (xy_modeq_of_modeq (n+1)).rightβ©
theorem matiyasevic {a k x y} : (β a1 : a > 1, xn a1 k = x β§ yn a1 k = y) β
a > 1 β§ k β€ y β§
(x = 1 β§ y = 0 β¨
β (u v s t b : β),
x * x - (a * a - 1) * y * y = 1 β§
u * u - (a * a - 1) * v * v = 1 β§
s * s - (b * b - 1) * t * t = 1 β§
b > 1 β§ b β‘ 1 [MOD 4 * y] β§ b β‘ a [MOD u] β§
v > 0 β§ y * y β£ v β§
s β‘ x [MOD u] β§
t β‘ k [MOD 4 * y]) :=
β¨Ξ»β¨a1, hx, hyβ©, by rw [β hx, β hy];
refine β¨a1, (nat.eq_zero_or_pos k).elim
(Ξ»k0, by rw k0; exact β¨le_refl _, or.inl β¨rfl, rflβ©β©) (Ξ»kpos, _)β©; exact
let x := xn a1 k, y := yn a1 k,
m := 2 * (k * y),
u := xn a1 m, v := yn a1 m in
have ky : k β€ y, from yn_ge_n a1 k,
have yv : y * y β£ v, from dvd_trans (ysq_dvd_yy a1 k) $
(y_dvd_iff _ _ _).2 $ dvd_mul_left _ _,
have uco : nat.coprime u (4 * y), from
have 2 β£ v, from modeq.modeq_zero_iff.1 $ (yn_modeq_two _ _).trans $
modeq.modeq_zero_iff.2 (dvd_mul_right _ _),
have nat.coprime u 2, from
(xy_coprime a1 m).coprime_dvd_right this,
(this.mul_right this).mul_right $
(xy_coprime _ _).coprime_dvd_right (dvd_of_mul_left_dvd yv),
let β¨b, ba, bm1β© := modeq.chinese_remainder uco a 1 in
have m1 : 1 < m, from
have 0 < k * y, from mul_pos kpos (y_increasing a1 kpos),
nat.mul_le_mul_left 2 this,
have vp : v > 0, from y_increasing a1 (lt_trans zero_lt_one m1),
have b1 : b > 1, from
have u > xn a1 1, from x_increasing a1 m1,
have u > a, by simp at this; exact this,
lt_of_lt_of_le a1 $ by delta modeq at ba;
rw nat.mod_eq_of_lt this at ba; rw β ba; apply nat.mod_le,
let s := xn b1 k, t := yn b1 k in
have sx : s β‘ x [MOD u], from (xy_modeq_of_modeq b1 a1 ba k).left,
have tk : t β‘ k [MOD 4 * y], from
have 4 * y β£ b - 1, from int.coe_nat_dvd.1 $
by rw int.coe_nat_sub (le_of_lt b1);
exact modeq.dvd_of_modeq bm1.symm,
modeq.modeq_of_dvd_of_modeq this $ yn_modeq_a_sub_one _ _,
β¨ky, or.inr β¨u, v, s, t, b,
pell_eq _ _, pell_eq _ _, pell_eq _ _, b1, bm1, ba, vp, yv, sx, tkβ©β©,
Ξ»β¨a1, ky, oβ©, β¨a1, match o with
| or.inl β¨x1, y0β© := by rw y0 at ky; rw [nat.eq_zero_of_le_zero ky, x1, y0]; exact β¨rfl, rflβ©
| or.inr β¨u, v, s, t, b, xy, uv, st, b1, remβ© :=
match x, y, eq_pell a1 xy, u, v, eq_pell a1 uv, s, t, eq_pell b1 st, rem, ky with
| ._, ._, β¨i, rfl, rflβ©, ._, ._, β¨n, rfl, rflβ©, ._, ._, β¨j, rfl, rflβ©,
β¨(bm1 : b β‘ 1 [MOD 4 * yn a1 i]),
(ba : b β‘ a [MOD xn a1 n]),
(vp : yn a1 n > 0),
(yv : yn a1 i * yn a1 i β£ yn a1 n),
(sx : xn b1 j β‘ xn a1 i [MOD xn a1 n]),
(tk : yn b1 j β‘ k [MOD 4 * yn a1 i])β©,
(ky : k β€ yn a1 i) :=
(nat.eq_zero_or_pos i).elim
(Ξ»i0, by simp [i0] at ky; rw [i0, ky]; exact β¨rfl, rflβ©) $ Ξ»ipos,
suffices i = k, by rw this; exact β¨rfl, rflβ©,
by clear _x o rem xy uv st _match _match _fun_match; exact
have iln : i β€ n, from le_of_not_gt $ Ξ»hin,
not_lt_of_ge (nat.le_of_dvd vp (dvd_of_mul_left_dvd yv)) (y_increasing a1 hin),
have yd : 4 * yn a1 i β£ 4 * n, from mul_dvd_mul_left _ $ dvd_of_ysq_dvd a1 yv,
have jk : j β‘ k [MOD 4 * yn a1 i], from
have 4 * yn a1 i β£ b - 1, from int.coe_nat_dvd.1 $
by rw int.coe_nat_sub (le_of_lt b1); exact modeq.dvd_of_modeq bm1.symm,
(modeq.modeq_of_dvd_of_modeq this (yn_modeq_a_sub_one b1 _)).symm.trans tk,
have ki : k + i < 4 * yn a1 i, from
lt_of_le_of_lt (add_le_add ky (yn_ge_n a1 i)) $
by rw β two_mul; exact nat.mul_lt_mul_of_pos_right dec_trivial (y_increasing a1 ipos),
have ji : j β‘ i [MOD 4 * n], from
have xn a1 j β‘ xn a1 i [MOD xn a1 n], from (xy_modeq_of_modeq b1 a1 ba j).left.symm.trans sx,
(modeq_of_xn_modeq a1 ipos iln this).resolve_right $ Ξ» (ji : j + i β‘ 0 [MOD 4 * n]),
not_le_of_gt ki $ nat.le_of_dvd (lt_of_lt_of_le ipos $ nat.le_add_left _ _) $
modeq.modeq_zero_iff.1 $ (modeq.modeq_add jk.symm (modeq.refl i)).trans $
modeq.modeq_of_dvd_of_modeq yd ji,
by have : i % (4 * yn a1 i) = k % (4 * yn a1 i) :=
(modeq.modeq_of_dvd_of_modeq yd ji).symm.trans jk;
rwa [nat.mod_eq_of_lt (lt_of_le_of_lt (nat.le_add_left _ _) ki),
nat.mod_eq_of_lt (lt_of_le_of_lt (nat.le_add_right _ _) ki)] at this
end
endβ©β©
lemma eq_pow_of_pell_lem {a y k} (a1 : 1 < a) (ypos : y > 0) : k > 0 β a > y^k β
(β(y^k) : β€) < 2*a*y - y*y - 1 :=
have y < a β 2*a*y β₯ a + (y*y + 1), begin
intro ya, induction y with y IH, exact absurd ypos (lt_irrefl _),
cases nat.eq_zero_or_pos y with y0 ypos,
{ rw y0, simp [two_mul], apply add_le_add_left, exact a1 },
{ rw [nat.mul_succ, nat.mul_succ, nat.succ_mul y],
have : 2 * a β₯ y + nat.succ y,
{ change y + y < 2 * a, rw β two_mul,
exact mul_lt_mul_of_pos_left (nat.lt_of_succ_lt ya) dec_trivial },
have := add_le_add (IH ypos (nat.lt_of_succ_lt ya)) this,
simp at this, simp, exact this }
end, Ξ»k0 yak,
lt_of_lt_of_le (int.coe_nat_lt_coe_nat_of_lt yak) $
by rw sub_sub; apply le_sub_right_of_add_le;
apply int.coe_nat_le_coe_nat_of_le;
have y1 := nat.pow_le_pow_of_le_right ypos k0; simp at y1;
exact this (lt_of_le_of_lt y1 yak)
theorem eq_pow_of_pell {m n k} : (n^k = m β
k = 0 β§ m = 1 β¨ k > 0 β§
(n = 0 β§ m = 0 β¨ n > 0 β§
β (w a t z : β) (a1 : a > 1),
xn a1 k β‘ yn a1 k * (a - n) + m [MOD t] β§
2 * a * n = t + (n * n + 1) β§
m < t β§ n β€ w β§ k β€ w β§
a * a - ((w + 1) * (w + 1) - 1) * (w * z) * (w * z) = 1)) :=
β¨Ξ»e, by rw β e;
refine (nat.eq_zero_or_pos k).elim
(Ξ»k0, by rw k0; exact or.inl β¨rfl, rflβ©)
(Ξ»kpos, or.inr β¨kpos, _β©);
refine (nat.eq_zero_or_pos n).elim
(Ξ»n0, by rw [n0, nat.zero_pow kpos]; exact or.inl β¨rfl, rflβ©)
(Ξ»npos, or.inr β¨npos, _β©); exact
let w := _root_.max n k in
have nw : n β€ w, from le_max_left _ _,
have kw : k β€ w, from le_max_right _ _,
have wpos : w > 0, from lt_of_lt_of_le npos nw,
have w1 : w + 1 > 1, from nat.succ_lt_succ wpos,
let a := xn w1 w in
have a1 : a > 1, from x_increasing w1 wpos,
let x := xn a1 k, y := yn a1 k in
let β¨z, zeβ© := show w β£ yn w1 w, from modeq.modeq_zero_iff.1 $
modeq.trans (yn_modeq_a_sub_one w1 w) (modeq.modeq_zero_iff.2 $ dvd_refl _) in
have nt : (β(n^k) : β€) < 2 * a * n - n * n - 1, from
eq_pow_of_pell_lem a1 npos kpos $ calc
n^k β€ n^w : nat.pow_le_pow_of_le_right npos kw
... < (w + 1)^w : nat.pow_lt_pow_of_lt_left (nat.lt_succ_of_le nw) wpos
... β€ a : xn_ge_a_pow w1 w,
let β¨t, teβ© := int.eq_coe_of_zero_le $
le_trans (int.coe_zero_le _) $ le_of_lt nt in
have na : n β€ a, from le_trans nw $ le_of_lt $ n_lt_xn w1 w,
have tm : x β‘ y * (a - n) + n^k [MOD t], begin
apply modeq.modeq_of_dvd,
rw [int.coe_nat_add, int.coe_nat_mul, int.coe_nat_sub na, β te],
exact x_sub_y_dvd_pow a1 n k
end,
have ta : 2 * a * n = t + (n * n + 1), from int.coe_nat_inj $
by rw [int.coe_nat_add, β te, sub_sub];
repeat {rw int.coe_nat_add <|> rw int.coe_nat_mul};
rw [int.coe_nat_one, sub_add_cancel]; refl,
have mt : n^k < t, from int.lt_of_coe_nat_lt_coe_nat $
by rw β te; exact nt,
have zp : a * a - ((w + 1) * (w + 1) - 1) * (w * z) * (w * z) = 1,
by rw β ze; exact pell_eq w1 w,
β¨w, a, t, z, a1, tm, ta, mt, nw, kw, zpβ©,
Ξ»o, match o with
| or.inl β¨k0, m1β© := by rw [k0, m1]; refl
| or.inr β¨kpos, or.inl β¨n0, m0β©β© := by rw [n0, m0, nat.zero_pow kpos]
| or.inr β¨kpos, or.inr β¨npos, w, a, t, z,
(a1 : a > 1),
(tm : xn a1 k β‘ yn a1 k * (a - n) + m [MOD t]),
(ta : 2 * a * n = t + (n * n + 1)),
(mt : m < t),
(nw : n β€ w),
(kw : k β€ w),
(zp : a * a - ((w + 1) * (w + 1) - 1) * (w * z) * (w * z) = 1)β©β© :=
have wpos : w > 0, from lt_of_lt_of_le npos nw,
have w1 : w + 1 > 1, from nat.succ_lt_succ wpos,
let β¨j, xj, yjβ© := eq_pell w1 zp in
by clear _match o _let_match; exact
have jpos : j > 0, from (nat.eq_zero_or_pos j).resolve_left $ Ξ»j0,
have a1 : a = 1, by rw j0 at xj; exact xj,
have 2 * n = t + (n * n + 1), by rw a1 at ta; exact ta,
have n1 : n = 1, from
have n * n < n * 2, by rw [mul_comm n 2, this]; apply nat.le_add_left,
have n β€ 1, from nat.le_of_lt_succ $ lt_of_mul_lt_mul_left this (nat.zero_le _),
le_antisymm this npos,
by rw n1 at this;
rw β @nat.add_right_cancel 0 2 t this at mt;
exact nat.not_lt_zero _ mt,
have wj : w β€ j, from nat.le_of_dvd jpos $ modeq.modeq_zero_iff.1 $
(yn_modeq_a_sub_one w1 j).symm.trans $
modeq.modeq_zero_iff.2 β¨z, yj.symmβ©,
have nt : (β(n^k) : β€) < 2 * a * n - n * n - 1, from
eq_pow_of_pell_lem a1 npos kpos $ calc
n^k β€ n^j : nat.pow_le_pow_of_le_right npos (le_trans kw wj)
... < (w + 1)^j : nat.pow_lt_pow_of_lt_left (nat.lt_succ_of_le nw) jpos
... β€ xn w1 j : xn_ge_a_pow w1 j
... = a : xj.symm,
have na : n β€ a, by rw xj; exact
le_trans (le_trans nw wj) (le_of_lt $ n_lt_xn _ _),
have te : (t : β€) = 2 * βa * βn - βn * βn - 1, by
rw sub_sub; apply eq_sub_of_add_eq; apply (int.coe_nat_eq_coe_nat_iff _ _).2;
exact ta.symm,
have xn a1 k β‘ yn a1 k * (a - n) + n^k [MOD t],
by have := x_sub_y_dvd_pow a1 n k;
rw [β te, β int.coe_nat_sub na] at this; exact modeq.modeq_of_dvd this,
have n^k % t = m % t, from
modeq.modeq_add_cancel_left (modeq.refl _) (this.symm.trans tm),
by rw β te at nt;
rwa [nat.mod_eq_of_lt (int.lt_of_coe_nat_lt_coe_nat nt), nat.mod_eq_of_lt mt] at this
endβ©
end pell
|
3960bde033afcf7aa65279bd3e61cfb790dfc427 | e151e9053bfd6d71740066474fc500a087837323 | /src/hott/prop_trunc.lean | 4c99ec1e2c7354680de9aed466316eb66f68916a | [
"Apache-2.0"
] | permissive | daniel-carranza/hott3 | 15bac2d90589dbb952ef15e74b2837722491963d | 913811e8a1371d3a5751d7d32ff9dec8aa6815d9 | refs/heads/master | 1,610,091,349,670 | 1,596,222,336,000 | 1,596,222,336,000 | 241,957,822 | 0 | 0 | Apache-2.0 | 1,582,222,839,000 | 1,582,222,838,000 | null | UTF-8 | Lean | false | false | 1,980 | lean | /-
Copyright (c) 2015 Jakob von Raumer. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jakob von Raumer, Floris van Doorn
Proof of the @[hott] theorem that (is_trunc n A) is a mere proposition
We prove this here to avoid circular dependency of files
We want to use this in .equiv; .equiv is imported by .function and .function is imported by .trunc
-/
import hott.types.pi
universe u
namespace hott
hott_theory
open hott.sigma
namespace is_trunc
@[hott] def is_contr.sigma_char (A : Type u) :
(Ξ£ (center : A), Ξ (a : A), center = a) β (is_contr A) :=
begin
fapply equiv.MK,
{ intro S, exact (is_contr.mk S.1 S.2)},
{ intro H, cases H with H', cases H' with ce co, exact β¨ce, coβ©},
{ intro H, cases H with H', cases H' with ce co, exact idp},
{ intro S, cases S, apply idp}
end
@[hott] def is_trunc.pi_char (n : βββ) (A : Type u) :
(Ξ (x y : A), is_trunc n (x = y)) β (is_trunc (n .+1) A) :=
begin
fapply equiv.MK,
{ exact is_trunc_succ_intro},
{ introsI H x y, apply is_trunc_eq},
{ intro H, cases H, apply idp},
{ introI P, apply eq_of_homotopy, intro a, apply eq_of_homotopy, intro b,
change is_trunc.mk (to_internal n (a = b)) = P a b,
induction (P a b), apply idp},
end
@[hott] lemma is_prop_is_trunc (n : βββ) (A : Type u) : is_prop (is_trunc n A) :=
begin
induction n generalizing A,
{ apply is_trunc_equiv_closed,
{ apply is_contr.sigma_char },
apply is_prop.mk, intros,
fapply sigma_eq, apply x.2,
apply is_prop.elimo', apply pi.is_prop_pi_eq },
{ apply is_trunc_equiv_closed,
apply is_trunc.pi_char,
unfreezeI, apply_instance },
end
local attribute [instance] is_prop_is_trunc
@[hott, instance] def is_trunc_succ_is_trunc (n m : βββ) (A : Type u) :
is_trunc (n.+1) (is_trunc m A) :=
is_trunc_succ_of_is_prop (is_trunc m A) n
end is_trunc
end hott |
c1189e45399f3835bbf5cb5819b220d8a116c042 | 2eab05920d6eeb06665e1a6df77b3157354316ad | /src/data/multiset/basic.lean | 60f0aa4328b99b6c91e0f5d2c678e64478980571 | [
"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 | 105,383 | lean | /-
Copyright (c) 2015 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import data.list.perm
import data.list.prod_monoid
/-!
# Multisets
These are implemented as the quotient of a list by permutations.
## Notation
We define the global infix notation `::β` for `multiset.cons`.
-/
open list subtype nat
variables {Ξ± : Type*} {Ξ² : Type*} {Ξ³ : Type*}
/-- `multiset Ξ±` is the quotient of `list Ξ±` by list permutation. The result
is a type of finite sets with duplicates allowed. -/
def {u} multiset (Ξ± : Type u) : Type u :=
quotient (list.is_setoid Ξ±)
namespace multiset
instance : has_coe (list Ξ±) (multiset Ξ±) := β¨quot.mk _β©
@[simp] theorem quot_mk_to_coe (l : list Ξ±) : @eq (multiset Ξ±) β¦lβ§ l := rfl
@[simp] theorem quot_mk_to_coe' (l : list Ξ±) : @eq (multiset Ξ±) (quot.mk (β) l) l := rfl
@[simp] theorem quot_mk_to_coe'' (l : list Ξ±) : @eq (multiset Ξ±) (quot.mk setoid.r l) l := rfl
@[simp] theorem coe_eq_coe {lβ lβ : list Ξ±} : (lβ : multiset Ξ±) = lβ β lβ ~ lβ := quotient.eq
instance has_decidable_eq [decidable_eq Ξ±] : decidable_eq (multiset Ξ±)
| sβ sβ := quotient.rec_on_subsingletonβ sβ sβ $ Ξ» lβ lβ,
decidable_of_iff' _ quotient.eq
/-- defines a size for a multiset by referring to the size of the underlying list -/
protected def sizeof [has_sizeof Ξ±] (s : multiset Ξ±) : β :=
quot.lift_on s sizeof $ Ξ» lβ lβ, perm.sizeof_eq_sizeof
instance has_sizeof [has_sizeof Ξ±] : has_sizeof (multiset Ξ±) := β¨multiset.sizeofβ©
/-! ### Empty multiset -/
/-- `0 : multiset Ξ±` is the empty set -/
protected def zero : multiset Ξ± := @nil Ξ±
instance : has_zero (multiset Ξ±) := β¨multiset.zeroβ©
instance : has_emptyc (multiset Ξ±) := β¨0β©
instance inhabited_multiset : inhabited (multiset Ξ±) := β¨0β©
@[simp] theorem coe_nil_eq_zero : (@nil Ξ± : multiset Ξ±) = 0 := rfl
@[simp] theorem empty_eq_zero : (β
: multiset Ξ±) = 0 := rfl
theorem coe_eq_zero (l : list Ξ±) : (l : multiset Ξ±) = 0 β l = [] :=
iff.trans coe_eq_coe perm_nil
/-! ### `multiset.cons` -/
/-- `cons a s` is the multiset which contains `s` plus one more
instance of `a`. -/
def cons (a : Ξ±) (s : multiset Ξ±) : multiset Ξ± :=
quot.lift_on s (Ξ» l, (a :: l : multiset Ξ±))
(Ξ» lβ lβ p, quot.sound (p.cons a))
infixr ` ::β `:67 := multiset.cons
instance : has_insert Ξ± (multiset Ξ±) := β¨consβ©
@[simp] theorem insert_eq_cons (a : Ξ±) (s : multiset Ξ±) :
insert a s = a ::β s := rfl
@[simp] theorem cons_coe (a : Ξ±) (l : list Ξ±) :
(a ::β l : multiset Ξ±) = (a::l : list Ξ±) := rfl
theorem singleton_coe (a : Ξ±) : (a ::β 0 : multiset Ξ±) = ([a] : list Ξ±) := rfl
@[simp] theorem cons_inj_left {a b : Ξ±} (s : multiset Ξ±) :
a ::β s = b ::β s β a = b :=
β¨quot.induction_on s $ Ξ» l e,
have [a] ++ l ~ [b] ++ l, from quotient.exact e,
singleton_perm_singleton.1 $ (perm_append_right_iff _).1 this, congr_arg _β©
@[simp] theorem cons_inj_right (a : Ξ±) : β{s t : multiset Ξ±}, a ::β s = a ::β t β s = t :=
by rintros β¨lββ© β¨lββ©; simp
@[recursor 5] protected theorem induction {p : multiset Ξ± β Prop}
(hβ : p 0) (hβ : β β¦a : Ξ±β¦ {s : multiset Ξ±}, p s β p (a ::β s)) : βs, p s :=
by rintros β¨lβ©; induction l with _ _ ih; [exact hβ, exact hβ ih]
@[elab_as_eliminator] protected theorem induction_on {p : multiset Ξ± β Prop}
(s : multiset Ξ±) (hβ : p 0) (hβ : β β¦a : Ξ±β¦ {s : multiset Ξ±}, p s β p (a ::β s)) : p s :=
multiset.induction hβ hβ s
theorem cons_swap (a b : Ξ±) (s : multiset Ξ±) : a ::β b ::β s = b ::β a ::β s :=
quot.induction_on s $ Ξ» l, quotient.sound $ perm.swap _ _ _
section rec
variables {C : multiset Ξ± β Sort*}
/-- Dependent recursor on multisets.
TODO: should be @[recursor 6], but then the definition of `multiset.pi` fails with a stack
overflow in `whnf`.
-/
protected def rec
(C_0 : C 0)
(C_cons : Ξ a m, C m β C (a ::β m))
(C_cons_heq : β a a' m b, C_cons a (a' ::β m) (C_cons a' m b) ==
C_cons a' (a ::β m) (C_cons a m b))
(m : multiset Ξ±) : C m :=
quotient.hrec_on m (@list.rec Ξ± (Ξ»l, C β¦lβ§) C_0 (Ξ»a l b, C_cons a β¦lβ§ b)) $
assume l l' h,
h.rec_heq
(assume a l l' b b' hl, have β¦lβ§ = β¦l'β§, from quot.sound hl, by cc)
(assume a a' l, C_cons_heq a a' β¦lβ§)
/-- Companion to `multiset.rec` with more convenient argument order. -/
@[elab_as_eliminator]
protected def rec_on (m : multiset Ξ±)
(C_0 : C 0)
(C_cons : Ξ a m, C m β C (a ::β m))
(C_cons_heq : βa a' m b, C_cons a (a' ::β m) (C_cons a' m b) ==
C_cons a' (a ::β m) (C_cons a m b)) :
C m :=
multiset.rec C_0 C_cons C_cons_heq m
variables {C_0 : C 0} {C_cons : Ξ a m, C m β C (a ::β m)}
{C_cons_heq : βa a' m b, C_cons a (a' ::β m) (C_cons a' m b) ==
C_cons a' (a ::β m) (C_cons a m b)}
@[simp] lemma rec_on_0 : @multiset.rec_on Ξ± C (0:multiset Ξ±) C_0 C_cons C_cons_heq = C_0 :=
rfl
@[simp] lemma rec_on_cons (a : Ξ±) (m : multiset Ξ±) :
(a ::β m).rec_on C_0 C_cons C_cons_heq = C_cons a m (m.rec_on C_0 C_cons C_cons_heq) :=
quotient.induction_on m $ assume l, rfl
end rec
section mem
/-- `a β s` means that `a` has nonzero multiplicity in `s`. -/
def mem (a : Ξ±) (s : multiset Ξ±) : Prop :=
quot.lift_on s (Ξ» l, a β l) (Ξ» lβ lβ (e : lβ ~ lβ), propext $ e.mem_iff)
instance : has_mem Ξ± (multiset Ξ±) := β¨memβ©
@[simp] lemma mem_coe {a : Ξ±} {l : list Ξ±} : a β (l : multiset Ξ±) β a β l := iff.rfl
instance decidable_mem [decidable_eq Ξ±] (a : Ξ±) (s : multiset Ξ±) : decidable (a β s) :=
quot.rec_on_subsingleton s $ list.decidable_mem a
@[simp] theorem mem_cons {a b : Ξ±} {s : multiset Ξ±} : a β b ::β s β a = b β¨ a β s :=
quot.induction_on s $ Ξ» l, iff.rfl
lemma mem_cons_of_mem {a b : Ξ±} {s : multiset Ξ±} (h : a β s) : a β b ::β s :=
mem_cons.2 $ or.inr h
@[simp] theorem mem_cons_self (a : Ξ±) (s : multiset Ξ±) : a β a ::β s :=
mem_cons.2 (or.inl rfl)
theorem forall_mem_cons {p : Ξ± β Prop} {a : Ξ±} {s : multiset Ξ±} :
(β x β (a ::β s), p x) β p a β§ β x β s, p x :=
quotient.induction_on' s $ Ξ» L, list.forall_mem_cons
theorem exists_cons_of_mem {s : multiset Ξ±} {a : Ξ±} : a β s β β t, s = a ::β t :=
quot.induction_on s $ Ξ» l (h : a β l),
let β¨lβ, lβ, eβ© := mem_split h in
e.symm βΈ β¨(lβ++lβ : list Ξ±), quot.sound perm_middleβ©
@[simp] theorem not_mem_zero (a : Ξ±) : a β (0 : multiset Ξ±) := id
theorem eq_zero_of_forall_not_mem {s : multiset Ξ±} : (βx, x β s) β s = 0 :=
quot.induction_on s $ Ξ» l H, by rw eq_nil_iff_forall_not_mem.mpr H; refl
theorem eq_zero_iff_forall_not_mem {s : multiset Ξ±} : s = 0 β β a, a β s :=
β¨Ξ» h, h.symm βΈ Ξ» _, not_false, eq_zero_of_forall_not_memβ©
theorem exists_mem_of_ne_zero {s : multiset Ξ±} : s β 0 β β a : Ξ±, a β s :=
quot.induction_on s $ assume l hl,
match l, hl with
| [] := assume h, false.elim $ h rfl
| (a :: l) := assume _, β¨a, by simpβ©
end
@[simp] lemma zero_ne_cons {a : Ξ±} {m : multiset Ξ±} : 0 β a ::β m :=
assume h, have a β (0:multiset Ξ±), from h.symm βΈ mem_cons_self _ _, not_mem_zero _ this
@[simp] lemma cons_ne_zero {a : Ξ±} {m : multiset Ξ±} : a ::β m β 0 := zero_ne_cons.symm
lemma cons_eq_cons {a b : Ξ±} {as bs : multiset Ξ±} :
a ::β as = b ::β bs β ((a = b β§ as = bs) β¨ (a β b β§ βcs, as = b ::β cs β§ bs = a ::β cs)) :=
begin
haveI : decidable_eq Ξ± := classical.dec_eq Ξ±,
split,
{ assume eq,
by_cases a = b,
{ subst h, simp * at * },
{ have : a β b ::β bs, from eq βΈ mem_cons_self _ _,
have : a β bs, by simpa [h],
rcases exists_cons_of_mem this with β¨cs, hcsβ©,
simp [h, hcs],
have : a ::β as = b ::β a ::β cs, by simp [eq, hcs],
have : a ::β as = a ::β b ::β cs, by rwa [cons_swap],
simpa using this } },
{ assume h,
rcases h with β¨eqβ, eqββ© | β¨h, cs, eqβ, eqββ©,
{ simp * },
{ simp [*, cons_swap a b] } }
end
end mem
/-! ### `multiset.subset` -/
section subset
/-- `s β t` is the lift of the list subset relation. It means that any
element with nonzero multiplicity in `s` has nonzero multiplicity in `t`,
but it does not imply that the multiplicity of `a` in `s` is less or equal than in `t`;
see `s β€ t` for this relation. -/
protected def subset (s t : multiset Ξ±) : Prop := β β¦a : Ξ±β¦, a β s β a β t
instance : has_subset (multiset Ξ±) := β¨multiset.subsetβ©
@[simp] theorem coe_subset {lβ lβ : list Ξ±} : (lβ : multiset Ξ±) β lβ β lβ β lβ := iff.rfl
@[simp] theorem subset.refl (s : multiset Ξ±) : s β s := Ξ» a h, h
theorem subset.trans {s t u : multiset Ξ±} : s β t β t β u β s β u :=
Ξ» hβ hβ a m, hβ (hβ m)
theorem subset_iff {s t : multiset Ξ±} : s β t β (ββ¦xβ¦, x β s β x β t) := iff.rfl
theorem mem_of_subset {s t : multiset Ξ±} {a : Ξ±} (h : s β t) : a β s β a β t := @h _
@[simp] theorem zero_subset (s : multiset Ξ±) : 0 β s :=
Ξ» a, (not_mem_nil a).elim
@[simp] theorem cons_subset {a : Ξ±} {s t : multiset Ξ±} : (a ::β s) β t β a β t β§ s β t :=
by simp [subset_iff, or_imp_distrib, forall_and_distrib]
theorem eq_zero_of_subset_zero {s : multiset Ξ±} (h : s β 0) : s = 0 :=
eq_zero_of_forall_not_mem h
theorem subset_zero {s : multiset Ξ±} : s β 0 β s = 0 :=
β¨eq_zero_of_subset_zero, Ξ» xeq, xeq.symm βΈ subset.refl 0β©
lemma induction_on' {p : multiset Ξ± β Prop} (S : multiset Ξ±)
(hβ : p β
) (hβ : β {a s}, a β S β s β S β p s β p (insert a s)) : p S :=
@multiset.induction_on Ξ± (Ξ» T, T β S β p T) S (Ξ» _, hβ) (Ξ» a s hps hs,
let β¨hS, sSβ© := cons_subset.1 hs in hβ hS sS (hps sS)) (subset.refl S)
end subset
section to_list
/-- Produces a list of the elements in the multiset using choice. -/
@[reducible] noncomputable def to_list {Ξ± : Type*} (s : multiset Ξ±) :=
classical.some (quotient.exists_rep s)
@[simp] lemma to_list_zero {Ξ± : Type*} : (multiset.to_list 0 : list Ξ±) = [] :=
(multiset.coe_eq_zero _).1 (classical.some_spec (quotient.exists_rep multiset.zero))
@[simp, norm_cast]
lemma coe_to_list {Ξ± : Type*} (s : multiset Ξ±) : (s.to_list : multiset Ξ±) = s :=
classical.some_spec (quotient.exists_rep _)
@[simp]
lemma mem_to_list {Ξ± : Type*} (a : Ξ±) (s : multiset Ξ±) : a β s.to_list β a β s :=
by rw [βmultiset.mem_coe, multiset.coe_to_list]
end to_list
/-! ### Partial order on `multiset`s -/
/-- `s β€ t` means that `s` is a sublist of `t` (up to permutation).
Equivalently, `s β€ t` means that `count a s β€ count a t` for all `a`. -/
protected def le (s t : multiset Ξ±) : Prop :=
quotient.lift_onβ s t (<+~) $ Ξ» vβ vβ wβ wβ pβ pβ,
propext (pβ.subperm_left.trans pβ.subperm_right)
instance : partial_order (multiset Ξ±) :=
{ le := multiset.le,
le_refl := by rintros β¨lβ©; exact subperm.refl _,
le_trans := by rintros β¨lββ© β¨lββ© β¨lββ©; exact @subperm.trans _ _ _ _,
le_antisymm := by rintros β¨lββ© β¨lββ© hβ hβ; exact quot.sound (subperm.antisymm hβ hβ) }
theorem subset_of_le {s t : multiset Ξ±} : s β€ t β s β t :=
quotient.induction_onβ s t $ Ξ» lβ lβ, subperm.subset
theorem mem_of_le {s t : multiset Ξ±} {a : Ξ±} (h : s β€ t) : a β s β a β t :=
mem_of_subset (subset_of_le h)
@[simp] theorem coe_le {lβ lβ : list Ξ±} : (lβ : multiset Ξ±) β€ lβ β lβ <+~ lβ := iff.rfl
@[elab_as_eliminator] theorem le_induction_on {C : multiset Ξ± β multiset Ξ± β Prop}
{s t : multiset Ξ±} (h : s β€ t)
(H : β {lβ lβ : list Ξ±}, lβ <+ lβ β C lβ lβ) : C s t :=
quotient.induction_onβ s t (Ξ» lβ lβ β¨l, p, sβ©,
(show β¦lβ§ = β¦lββ§, from quot.sound p) βΈ H s) h
theorem zero_le (s : multiset Ξ±) : 0 β€ s :=
quot.induction_on s $ Ξ» l, (nil_sublist l).subperm
theorem le_zero {s : multiset Ξ±} : s β€ 0 β s = 0 :=
β¨Ξ» h, le_antisymm h (zero_le _), le_of_eqβ©
theorem lt_cons_self (s : multiset Ξ±) (a : Ξ±) : s < a ::β s :=
quot.induction_on s $ Ξ» l,
suffices l <+~ a :: l β§ (Β¬l ~ a :: l),
by simpa [lt_iff_le_and_ne],
β¨(sublist_cons _ _).subperm,
Ξ» p, ne_of_lt (lt_succ_self (length l)) p.length_eqβ©
theorem le_cons_self (s : multiset Ξ±) (a : Ξ±) : s β€ a ::β s :=
le_of_lt $ lt_cons_self _ _
theorem cons_le_cons_iff (a : Ξ±) {s t : multiset Ξ±} : a ::β s β€ a ::β t β s β€ t :=
quotient.induction_onβ s t $ Ξ» lβ lβ, subperm_cons a
theorem cons_le_cons (a : Ξ±) {s t : multiset Ξ±} : s β€ t β a ::β s β€ a ::β t :=
(cons_le_cons_iff a).2
theorem le_cons_of_not_mem {a : Ξ±} {s t : multiset Ξ±} (m : a β s) : s β€ a ::β t β s β€ t :=
begin
refine β¨_, Ξ» h, le_trans h $ le_cons_self _ _β©,
suffices : β {t'} (_ : s β€ t') (_ : a β t'), a ::β s β€ t',
{ exact Ξ» h, (cons_le_cons_iff a).1 (this h (mem_cons_self _ _)) },
introv h, revert m, refine le_induction_on h _,
introv s mβ mβ,
rcases mem_split mβ with β¨rβ, rβ, rflβ©,
exact perm_middle.subperm_left.2 ((subperm_cons _).2 $
((sublist_or_mem_of_sublist s).resolve_right mβ).subperm)
end
/-! ### Singleton -/
instance : has_singleton Ξ± (multiset Ξ±) := β¨Ξ» a, a ::β 0β©
instance : is_lawful_singleton Ξ± (multiset Ξ±) := β¨Ξ» a, rflβ©
theorem singleton_eq_cons (a : Ξ±) : singleton a = a ::β 0 := rfl
@[simp] theorem mem_singleton {a b : Ξ±} : b β ({a} : multiset Ξ±) β b = a :=
by simp only [singleton_eq_cons, mem_cons, iff_self, or_false, not_mem_zero]
theorem mem_singleton_self (a : Ξ±) : a β ({a} : multiset Ξ±) :=
by { rw singleton_eq_cons, exact mem_cons_self _ _ }
theorem singleton_inj {a b : Ξ±} : ({a} : multiset Ξ±) = {b} β a = b :=
by { simp_rw [singleton_eq_cons], exact cons_inj_left _ }
@[simp] theorem singleton_ne_zero (a : Ξ±) : ({a} : multiset Ξ±) β 0 :=
ne_of_gt (lt_cons_self _ _)
@[simp] theorem singleton_le {a : Ξ±} {s : multiset Ξ±} : {a} β€ s β a β s :=
β¨Ξ» h, mem_of_le h (mem_singleton_self _),
Ξ» h, let β¨t, eβ© := exists_cons_of_mem h in e.symm βΈ cons_le_cons _ (zero_le _)β©
/-! ### Additive monoid -/
/-- The sum of two multisets is the lift of the list append operation.
This adds the multiplicities of each element,
i.e. `count a (s + t) = count a s + count a t`. -/
protected def add (sβ sβ : multiset Ξ±) : multiset Ξ± :=
quotient.lift_onβ sβ sβ (Ξ» lβ lβ, ((lβ ++ lβ : list Ξ±) : multiset Ξ±)) $
Ξ» vβ vβ wβ wβ pβ pβ, quot.sound $ pβ.append pβ
instance : has_add (multiset Ξ±) := β¨multiset.addβ©
@[simp] theorem coe_add (s t : list Ξ±) : (s + t : multiset Ξ±) = (s ++ t : list Ξ±) := rfl
protected theorem add_comm (s t : multiset Ξ±) : s + t = t + s :=
quotient.induction_onβ s t $ Ξ» lβ lβ, quot.sound perm_append_comm
protected theorem zero_add (s : multiset Ξ±) : 0 + s = s :=
quot.induction_on s $ Ξ» l, rfl
theorem singleton_add (a : Ξ±) (s : multiset Ξ±) : {a} + s = a ::β s := rfl
protected theorem add_le_add_left (s) {t u : multiset Ξ±} : s + t β€ s + u β t β€ u :=
quotient.induction_onβ s t u $ Ξ» lβ lβ lβ, subperm_append_left _
protected theorem add_left_cancel (s) {t u : multiset Ξ±} (h : s + t = s + u) : t = u :=
le_antisymm ((multiset.add_le_add_left _).1 (le_of_eq h))
((multiset.add_le_add_left _).1 (le_of_eq h.symm))
instance : ordered_cancel_add_comm_monoid (multiset Ξ±) :=
{ zero := 0,
add := (+),
add_comm := multiset.add_comm,
add_assoc := Ξ» sβ sβ sβ, quotient.induction_onβ sβ sβ sβ $ Ξ» lβ lβ lβ,
congr_arg coe $ append_assoc lβ lβ lβ,
zero_add := multiset.zero_add,
add_zero := Ξ» s, by rw [multiset.add_comm, multiset.zero_add],
add_left_cancel := multiset.add_left_cancel,
add_le_add_left := Ξ» sβ sβ h sβ, (multiset.add_le_add_left _).2 h,
le_of_add_le_add_left := Ξ» sβ sβ sβ, (multiset.add_le_add_left _).1,
..@multiset.partial_order Ξ± }
theorem le_add_right (s t : multiset Ξ±) : s β€ s + t :=
by simpa using add_le_add_left (zero_le t) s
theorem le_add_left (s t : multiset Ξ±) : s β€ t + s :=
by simpa using add_le_add_right (zero_le t) s
theorem le_iff_exists_add {s t : multiset Ξ±} : s β€ t β β u, t = s + u :=
β¨Ξ» h, le_induction_on h $ Ξ» lβ lβ s,
let β¨l, pβ© := s.exists_perm_append in β¨l, quot.sound pβ©,
Ξ» β¨u, eβ©, e.symm βΈ le_add_right _ _β©
instance : canonically_ordered_add_monoid (multiset Ξ±) :=
{ le_iff_exists_add := @le_iff_exists_add _,
bot := 0,
bot_le := multiset.zero_le,
..multiset.ordered_cancel_add_comm_monoid }
@[simp] theorem cons_add (a : Ξ±) (s t : multiset Ξ±) : a ::β s + t = a ::β (s + t) :=
by rw [β singleton_add, β singleton_add, add_assoc]
@[simp] theorem add_cons (a : Ξ±) (s t : multiset Ξ±) : s + a ::β t = a ::β (s + t) :=
by rw [add_comm, cons_add, add_comm]
@[simp] theorem mem_add {a : Ξ±} {s t : multiset Ξ±} : a β s + t β a β s β¨ a β t :=
quotient.induction_onβ s t $ Ξ» lβ lβ, mem_append
lemma mem_of_mem_nsmul {a : Ξ±} {s : multiset Ξ±} {n : β} (h : a β n β’ s) : a β s :=
begin
induction n with n ih,
{ rw zero_nsmul at h,
exact absurd h (not_mem_zero _) },
{ rw [succ_nsmul, mem_add] at h,
exact h.elim id ih },
end
@[simp]
lemma mem_nsmul {a : Ξ±} {s : multiset Ξ±} {n : β} (h0 : n β 0) : a β n β’ s β a β s :=
begin
refine β¨mem_of_mem_nsmul, Ξ» h, _β©,
obtain β¨n, rflβ© := exists_eq_succ_of_ne_zero h0,
rw [succ_nsmul, mem_add],
exact or.inl h
end
lemma nsmul_cons {s : multiset Ξ±} (n : β) (a : Ξ±) : n β’ (a ::β s) = n β’ {a} + n β’ s :=
by rw [βsingleton_add, nsmul_add]
/-! ### Cardinality -/
/-- The cardinality of a multiset is the sum of the multiplicities
of all its elements, or simply the length of the underlying list. -/
def card : multiset Ξ± β+ β :=
{ to_fun := Ξ» s, quot.lift_on s length $ Ξ» lβ lβ, perm.length_eq,
map_zero' := rfl,
map_add' := Ξ» s t, quotient.induction_onβ s t length_append }
@[simp] theorem coe_card (l : list Ξ±) : card (l : multiset Ξ±) = length l := rfl
@[simp] theorem card_zero : @card Ξ± 0 = 0 := rfl
theorem card_add (s t : multiset Ξ±) : card (s + t) = card s + card t :=
card.map_add s t
lemma card_nsmul (s : multiset Ξ±) (n : β) :
(n β’ s).card = n * s.card :=
by rw [card.map_nsmul s n, nat.nsmul_eq_mul]
@[simp] theorem card_cons (a : Ξ±) (s : multiset Ξ±) : card (a ::β s) = card s + 1 :=
quot.induction_on s $ Ξ» l, rfl
@[simp] theorem card_singleton (a : Ξ±) : card ({a} : multiset Ξ±) = 1 :=
by simp only [singleton_eq_cons, card_zero, eq_self_iff_true, zero_add, card_cons]
theorem card_eq_one {s : multiset Ξ±} : card s = 1 β β a, s = {a} :=
β¨quot.induction_on s $ Ξ» l h,
(list.length_eq_one.1 h).imp $ Ξ» a, congr_arg coe,
Ξ» β¨a, eβ©, e.symm βΈ rflβ©
theorem card_le_of_le {s t : multiset Ξ±} (h : s β€ t) : card s β€ card t :=
le_induction_on h $ Ξ» lβ lβ, length_le_of_sublist
theorem eq_of_le_of_card_le {s t : multiset Ξ±} (h : s β€ t) : card t β€ card s β s = t :=
le_induction_on h $ Ξ» lβ lβ s hβ, congr_arg coe $ eq_of_sublist_of_length_le s hβ
theorem card_lt_of_lt {s t : multiset Ξ±} (h : s < t) : card s < card t :=
lt_of_not_ge $ Ξ» hβ, ne_of_lt h $ eq_of_le_of_card_le (le_of_lt h) hβ
theorem lt_iff_cons_le {s t : multiset Ξ±} : s < t β β a, a ::β s β€ t :=
β¨quotient.induction_onβ s t $ Ξ» lβ lβ h,
subperm.exists_of_length_lt (le_of_lt h) (card_lt_of_lt h),
Ξ» β¨a, hβ©, lt_of_lt_of_le (lt_cons_self _ _) hβ©
@[simp] theorem card_eq_zero {s : multiset Ξ±} : card s = 0 β s = 0 :=
β¨Ξ» h, (eq_of_le_of_card_le (zero_le _) (le_of_eq h)).symm, Ξ» e, by simp [e]β©
theorem card_pos {s : multiset Ξ±} : 0 < card s β s β 0 :=
pos_iff_ne_zero.trans $ not_congr card_eq_zero
theorem card_pos_iff_exists_mem {s : multiset Ξ±} : 0 < card s β β a, a β s :=
quot.induction_on s $ Ξ» l, length_pos_iff_exists_mem
/-- A strong induction principle for multisets:
If you construct a value for a particular multiset given values for all strictly smaller multisets,
you can construct a value for any multiset.
-/
@[elab_as_eliminator] def strong_induction_on {p : multiset Ξ± β Sort*} :
β (s : multiset Ξ±), (β s, (βt < s, p t) β p s) β p s
| s := Ξ» ih, ih s $ Ξ» t h,
have card t < card s, from card_lt_of_lt h,
strong_induction_on t ih
using_well_founded {rel_tac := Ξ» _ _, `[exact β¨_, measure_wf cardβ©]}
theorem strong_induction_eq {p : multiset Ξ± β Sort*}
(s : multiset Ξ±) (H) : @strong_induction_on _ p s H =
H s (Ξ» t h, @strong_induction_on _ p t H) :=
by rw [strong_induction_on]
@[elab_as_eliminator] lemma case_strong_induction_on {p : multiset Ξ± β Prop}
(s : multiset Ξ±) (hβ : p 0) (hβ : β a s, (βt β€ s, p t) β p (a ::β s)) : p s :=
multiset.strong_induction_on s $ assume s,
multiset.induction_on s (Ξ» _, hβ) $ Ξ» a s _ ih, hβ _ _ $
Ξ» t h, ih _ $ lt_of_le_of_lt h $ lt_cons_self _ _
/-- Suppose that, given that `p t` can be defined on all supersets of `s` of cardinality less than
`n`, one knows how to define `p s`. Then one can inductively define `p s` for all multisets `s` of
cardinality less than `n`, starting from multisets of card `n` and iterating. This
can be used either to define data, or to prove properties. -/
def strong_downward_induction {p : multiset Ξ± β Sort*} {n : β} (H : β tβ, (β {tβ : multiset Ξ±},
tβ.card β€ n β tβ < tβ β p tβ) β tβ.card β€ n β p tβ) :
β (s : multiset Ξ±), s.card β€ n β p s
| s := H s (Ξ» t ht h, have n - card t < n - card s,
from (tsub_lt_tsub_iff_left_of_le ht).2 (card_lt_of_lt h),
strong_downward_induction t ht)
using_well_founded {rel_tac := Ξ» _ _, `[exact β¨_, measure_wf (Ξ» (t : multiset Ξ±), n - t.card)β©]}
lemma strong_downward_induction_eq {p : multiset Ξ± β Sort*} {n : β} (H : β tβ, (β {tβ : multiset Ξ±},
tβ.card β€ n β tβ < tβ β p tβ) β tβ.card β€ n β p tβ) (s : multiset Ξ±) :
strong_downward_induction H s = H s (Ξ» t ht hst, strong_downward_induction H t ht) :=
by rw strong_downward_induction
/-- Analogue of `strong_downward_induction` with order of arguments swapped. -/
@[elab_as_eliminator] def strong_downward_induction_on {p : multiset Ξ± β Sort*} {n : β} :
β (s : multiset Ξ±), (β tβ, (β {tβ : multiset Ξ±}, tβ.card β€ n β tβ < tβ β p tβ) β tβ.card β€ n β
p tβ) β s.card β€ n β p s :=
Ξ» s H, strong_downward_induction H s
lemma strong_downward_induction_on_eq {p : multiset Ξ± β Sort*} (s : multiset Ξ±) {n : β} (H : β tβ,
(β {tβ : multiset Ξ±}, tβ.card β€ n β tβ < tβ β p tβ) β tβ.card β€ n β p tβ) :
s.strong_downward_induction_on H = H s (Ξ» t ht h, t.strong_downward_induction_on H ht) :=
by { dunfold strong_downward_induction_on, rw strong_downward_induction }
/-- Another way of expressing `strong_induction_on`: the `(<)` relation is well-founded. -/
lemma well_founded_lt : well_founded ((<) : multiset Ξ± β multiset Ξ± β Prop) :=
subrelation.wf (Ξ» _ _, multiset.card_lt_of_lt) (measure_wf multiset.card)
/-! ### `multiset.repeat` -/
/-- `repeat a n` is the multiset containing only `a` with multiplicity `n`. -/
def repeat (a : Ξ±) (n : β) : multiset Ξ± := repeat a n
@[simp] lemma repeat_zero (a : Ξ±) : repeat a 0 = 0 := rfl
@[simp] lemma repeat_succ (a : Ξ±) (n) : repeat a (n+1) = a ::β repeat a n := by simp [repeat]
@[simp] lemma repeat_one (a : Ξ±) : repeat a 1 = {a} :=
by simp only [repeat_succ, singleton_eq_cons, eq_self_iff_true, repeat_zero, cons_inj_right]
@[simp] lemma card_repeat : β (a : Ξ±) n, card (repeat a n) = n := length_repeat
theorem eq_of_mem_repeat {a b : Ξ±} {n} : b β repeat a n β b = a := eq_of_mem_repeat
theorem eq_repeat' {a : Ξ±} {s : multiset Ξ±} : s = repeat a s.card β β b β s, b = a :=
quot.induction_on s $ Ξ» l, iff.trans β¨Ξ» h,
(perm_repeat.1 $ (quotient.exact h)), congr_arg coeβ© eq_repeat'
theorem eq_repeat_of_mem {a : Ξ±} {s : multiset Ξ±} : (β b β s, b = a) β s = repeat a s.card :=
eq_repeat'.2
theorem eq_repeat {a : Ξ±} {n} {s : multiset Ξ±} : s = repeat a n β card s = n β§ β b β s, b = a :=
β¨Ξ» h, h.symm βΈ β¨card_repeat _ _, Ξ» b, eq_of_mem_repeatβ©,
Ξ» β¨e, alβ©, e βΈ eq_repeat_of_mem alβ©
theorem repeat_subset_singleton : β (a : Ξ±) n, repeat a n β {a} := repeat_subset_singleton
theorem repeat_le_coe {a : Ξ±} {n} {l : list Ξ±} : repeat a n β€ l β list.repeat a n <+ l :=
β¨Ξ» β¨l', p, sβ©, (perm_repeat.1 p) βΈ s, sublist.subpermβ©
theorem nsmul_singleton (a : Ξ±) (n) : n β’ ({a} : multiset Ξ±) = repeat a n :=
begin
refine eq_repeat.mpr β¨_, Ξ» b hb, mem_singleton.mp (mem_of_mem_nsmul hb)β©,
rw [card_nsmul, card_singleton, mul_one]
end
lemma nsmul_repeat {a : Ξ±} (n m : β) : n β’ (repeat a m) = repeat a (n * m) :=
begin
rw eq_repeat,
split,
{ rw [card_nsmul, card_repeat] },
{ exact Ξ» b hb, eq_of_mem_repeat (mem_of_mem_nsmul hb) },
end
/-! ### Erasing one copy of an element -/
section erase
variables [decidable_eq Ξ±] {s t : multiset Ξ±} {a b : Ξ±}
/-- `erase s a` is the multiset that subtracts 1 from the
multiplicity of `a`. -/
def erase (s : multiset Ξ±) (a : Ξ±) : multiset Ξ± :=
quot.lift_on s (Ξ» l, (l.erase a : multiset Ξ±))
(Ξ» lβ lβ p, quot.sound (p.erase a))
@[simp] theorem coe_erase (l : list Ξ±) (a : Ξ±) :
erase (l : multiset Ξ±) a = l.erase a := rfl
@[simp] theorem erase_zero (a : Ξ±) : (0 : multiset Ξ±).erase a = 0 := rfl
@[simp] theorem erase_cons_head (a : Ξ±) (s : multiset Ξ±) : (a ::β s).erase a = s :=
quot.induction_on s $ Ξ» l, congr_arg coe $ erase_cons_head a l
@[simp, priority 990]
theorem erase_cons_tail {a b : Ξ±} (s : multiset Ξ±) (h : b β a) :
(b ::β s).erase a = b ::β s.erase a :=
quot.induction_on s $ Ξ» l, congr_arg coe $ erase_cons_tail l h
@[simp, priority 980]
theorem erase_of_not_mem {a : Ξ±} {s : multiset Ξ±} : a β s β s.erase a = s :=
quot.induction_on s $ Ξ» l h, congr_arg coe $ erase_of_not_mem h
@[simp, priority 980]
theorem cons_erase {s : multiset Ξ±} {a : Ξ±} : a β s β a ::β s.erase a = s :=
quot.induction_on s $ Ξ» l h, quot.sound (perm_cons_erase h).symm
theorem le_cons_erase (s : multiset Ξ±) (a : Ξ±) : s β€ a ::β s.erase a :=
if h : a β s then le_of_eq (cons_erase h).symm
else by rw erase_of_not_mem h; apply le_cons_self
theorem erase_add_left_pos {a : Ξ±} {s : multiset Ξ±} (t) : a β s β (s + t).erase a = s.erase a + t :=
quotient.induction_onβ s t $ Ξ» lβ lβ h, congr_arg coe $ erase_append_left lβ h
theorem erase_add_right_pos {a : Ξ±} (s) {t : multiset Ξ±} (h : a β t) :
(s + t).erase a = s + t.erase a :=
by rw [add_comm, erase_add_left_pos s h, add_comm]
theorem erase_add_right_neg {a : Ξ±} {s : multiset Ξ±} (t) :
a β s β (s + t).erase a = s + t.erase a :=
quotient.induction_onβ s t $ Ξ» lβ lβ h, congr_arg coe $ erase_append_right lβ h
theorem erase_add_left_neg {a : Ξ±} (s) {t : multiset Ξ±} (h : a β t) :
(s + t).erase a = s.erase a + t :=
by rw [add_comm, erase_add_right_neg s h, add_comm]
theorem erase_le (a : Ξ±) (s : multiset Ξ±) : s.erase a β€ s :=
quot.induction_on s $ Ξ» l, (erase_sublist a l).subperm
@[simp] theorem erase_lt {a : Ξ±} {s : multiset Ξ±} : s.erase a < s β a β s :=
β¨Ξ» h, not_imp_comm.1 erase_of_not_mem (ne_of_lt h),
Ξ» h, by simpa [h] using lt_cons_self (s.erase a) aβ©
theorem erase_subset (a : Ξ±) (s : multiset Ξ±) : s.erase a β s :=
subset_of_le (erase_le a s)
theorem mem_erase_of_ne {a b : Ξ±} {s : multiset Ξ±} (ab : a β b) : a β s.erase b β a β s :=
quot.induction_on s $ Ξ» l, list.mem_erase_of_ne ab
theorem mem_of_mem_erase {a b : Ξ±} {s : multiset Ξ±} : a β s.erase b β a β s :=
mem_of_subset (erase_subset _ _)
theorem erase_comm (s : multiset Ξ±) (a b : Ξ±) : (s.erase a).erase b = (s.erase b).erase a :=
quot.induction_on s $ Ξ» l, congr_arg coe $ l.erase_comm a b
theorem erase_le_erase {s t : multiset Ξ±} (a : Ξ±) (h : s β€ t) : s.erase a β€ t.erase a :=
le_induction_on h $ Ξ» lβ lβ h, (h.erase _).subperm
theorem erase_le_iff_le_cons {s t : multiset Ξ±} {a : Ξ±} : s.erase a β€ t β s β€ a ::β t :=
β¨Ξ» h, le_trans (le_cons_erase _ _) (cons_le_cons _ h),
Ξ» h, if m : a β s
then by rw β cons_erase m at h; exact (cons_le_cons_iff _).1 h
else le_trans (erase_le _ _) ((le_cons_of_not_mem m).1 h)β©
@[simp] theorem card_erase_of_mem {a : Ξ±} {s : multiset Ξ±} :
a β s β card (s.erase a) = pred (card s) :=
quot.induction_on s $ Ξ» l, length_erase_of_mem
theorem card_erase_lt_of_mem {a : Ξ±} {s : multiset Ξ±} : a β s β card (s.erase a) < card s :=
Ξ» h, card_lt_of_lt (erase_lt.mpr h)
theorem card_erase_le {a : Ξ±} {s : multiset Ξ±} : card (s.erase a) β€ card s :=
card_le_of_le (erase_le a s)
theorem card_erase_eq_ite {a : Ξ±} {s : multiset Ξ±} :
card (s.erase a) = if a β s then pred (card s) else card s :=
begin
by_cases h : a β s,
{ rwa [card_erase_of_mem h, if_pos] },
{ rwa [erase_of_not_mem h, if_neg] }
end
end erase
@[simp] theorem coe_reverse (l : list Ξ±) : (reverse l : multiset Ξ±) = l :=
quot.sound $ reverse_perm _
/-! ### `multiset.map` -/
/-- `map f s` is the lift of the list `map` operation. The multiplicity
of `b` in `map f s` is the number of `a β s` (counting multiplicity)
such that `f a = b`. -/
def map (f : Ξ± β Ξ²) (s : multiset Ξ±) : multiset Ξ² :=
quot.lift_on s (Ξ» l : list Ξ±, (l.map f : multiset Ξ²))
(Ξ» lβ lβ p, quot.sound (p.map f))
theorem forall_mem_map_iff {f : Ξ± β Ξ²} {p : Ξ² β Prop} {s : multiset Ξ±} :
(β y β s.map f, p y) β (β x β s, p (f x)) :=
quotient.induction_on' s $ Ξ» L, list.forall_mem_map_iff
@[simp] theorem coe_map (f : Ξ± β Ξ²) (l : list Ξ±) : map f βl = l.map f := rfl
@[simp] theorem map_zero (f : Ξ± β Ξ²) : map f 0 = 0 := rfl
@[simp] theorem map_cons (f : Ξ± β Ξ²) (a s) : map f (a ::β s) = f a ::β map f s :=
quot.induction_on s $ Ξ» l, rfl
@[simp] theorem map_singleton (f : Ξ± β Ξ²) (a : Ξ±) : ({a} : multiset Ξ±).map f = {f a} := rfl
theorem map_repeat (f : Ξ± β Ξ²) (a : Ξ±) (k : β) : (repeat a k).map f = repeat (f a) k := by
{ induction k, simp, simpa }
@[simp] theorem map_add (f : Ξ± β Ξ²) (s t) : map f (s + t) = map f s + map f t :=
quotient.induction_onβ s t $ Ξ» lβ lβ, congr_arg coe $ map_append _ _ _
/-- If each element of `s : multiset Ξ±` can be lifted to `Ξ²`, then `s` can be lifted to
`multiset Ξ²`. -/
instance [can_lift Ξ± Ξ²] : can_lift (multiset Ξ±) (multiset Ξ²) :=
{ cond := Ξ» s, β x β s, can_lift.cond Ξ² x,
coe := map can_lift.coe,
prf := by { rintro β¨lβ© hl, lift l to list Ξ² using hl, exact β¨l, coe_map _ _β© } }
/-- `multiset.map` as an `add_monoid_hom`. -/
def map_add_monoid_hom (f : Ξ± β Ξ²) : multiset Ξ± β+ multiset Ξ² :=
{ to_fun := map f,
map_zero' := map_zero _,
map_add' := map_add _ }
@[simp] lemma coe_map_add_monoid_hom (f : Ξ± β Ξ²) :
(map_add_monoid_hom f : multiset Ξ± β multiset Ξ²) = map f := rfl
theorem map_nsmul (f : Ξ± β Ξ²) (n : β) (s) : map f (n β’ s) = n β’ (map f s) :=
(map_add_monoid_hom f).map_nsmul _ _
@[simp] theorem mem_map {f : Ξ± β Ξ²} {b : Ξ²} {s : multiset Ξ±} :
b β map f s β β a, a β s β§ f a = b :=
quot.induction_on s $ Ξ» l, mem_map
@[simp] theorem card_map (f : Ξ± β Ξ²) (s) : card (map f s) = card s :=
quot.induction_on s $ Ξ» l, length_map _ _
@[simp] theorem map_eq_zero {s : multiset Ξ±} {f : Ξ± β Ξ²} : s.map f = 0 β s = 0 :=
by rw [β multiset.card_eq_zero, multiset.card_map, multiset.card_eq_zero]
theorem mem_map_of_mem (f : Ξ± β Ξ²) {a : Ξ±} {s : multiset Ξ±} (h : a β s) : f a β map f s :=
mem_map.2 β¨_, h, rflβ©
lemma map_eq_singleton {f : Ξ± β Ξ²} {s : multiset Ξ±} {b : Ξ²} :
map f s = {b} β β a : Ξ±, s = {a} β§ f a = b :=
begin
split,
{ intro h,
obtain β¨a, haβ© : β a, s = {a},
{ rw [βcard_eq_one, βcard_map, h, card_singleton] },
refine β¨a, ha, _β©,
rw [βmem_singleton, βh, ha, map_singleton, mem_singleton] },
{ rintro β¨a, rfl, rflβ©,
simp }
end
theorem mem_map_of_injective {f : Ξ± β Ξ²} (H : function.injective f) {a : Ξ±} {s : multiset Ξ±} :
f a β map f s β a β s :=
quot.induction_on s $ Ξ» l, mem_map_of_injective H
@[simp] theorem map_map (g : Ξ² β Ξ³) (f : Ξ± β Ξ²) (s : multiset Ξ±) :
map g (map f s) = map (g β f) s :=
quot.induction_on s $ Ξ» l, congr_arg coe $ list.map_map _ _ _
theorem map_id (s : multiset Ξ±) : map id s = s :=
quot.induction_on s $ Ξ» l, congr_arg coe $ map_id _
@[simp] lemma map_id' (s : multiset Ξ±) : map (Ξ»x, x) s = s := map_id s
@[simp] theorem map_const (s : multiset Ξ±) (b : Ξ²) : map (function.const Ξ± b) s = repeat b s.card :=
quot.induction_on s $ Ξ» l, congr_arg coe $ map_const _ _
@[congr] theorem map_congr {f g : Ξ± β Ξ²} {s : multiset Ξ±} :
(β x β s, f x = g x) β map f s = map g s :=
quot.induction_on s $ Ξ» l H, congr_arg coe $ map_congr H
lemma map_hcongr {Ξ²' : Type*} {m : multiset Ξ±} {f : Ξ± β Ξ²} {f' : Ξ± β Ξ²'}
(h : Ξ² = Ξ²') (hf : βaβm, f a == f' a) : map f m == map f' m :=
begin subst h, simp at hf, simp [map_congr hf] end
theorem eq_of_mem_map_const {bβ bβ : Ξ²} {l : list Ξ±} (h : bβ β map (function.const Ξ± bβ) l) :
bβ = bβ :=
eq_of_mem_repeat $ by rwa map_const at h
@[simp] theorem map_le_map {f : Ξ± β Ξ²} {s t : multiset Ξ±} (h : s β€ t) : map f s β€ map f t :=
le_induction_on h $ Ξ» lβ lβ h, (h.map f).subperm
@[simp] theorem map_subset_map {f : Ξ± β Ξ²} {s t : multiset Ξ±} (H : s β t) : map f s β map f t :=
Ξ» b m, let β¨a, h, eβ© := mem_map.1 m in mem_map.2 β¨a, H h, eβ©
lemma map_erase [decidable_eq Ξ±] [decidable_eq Ξ²]
(f : Ξ± β Ξ²) (hf : function.injective f) (x : Ξ±) (s : multiset Ξ±) :
(s.erase x).map f = (s.map f).erase (f x) :=
begin
induction s using multiset.induction_on with y s ih,
{ simp },
by_cases hxy : y = x,
{ cases hxy, simp },
{ rw [s.erase_cons_tail hxy, map_cons, map_cons, (s.map f).erase_cons_tail (hf.ne hxy), ih] }
end
/-! ### `multiset.fold` -/
/-- `foldl f H b s` is the lift of the list operation `foldl f b l`,
which folds `f` over the multiset. It is well defined when `f` is right-commutative,
that is, `f (f b aβ) aβ = f (f b aβ) aβ`. -/
def foldl (f : Ξ² β Ξ± β Ξ²) (H : right_commutative f) (b : Ξ²) (s : multiset Ξ±) : Ξ² :=
quot.lift_on s (Ξ» l, foldl f b l)
(Ξ» lβ lβ p, p.foldl_eq H b)
@[simp] theorem foldl_zero (f : Ξ² β Ξ± β Ξ²) (H b) : foldl f H b 0 = b := rfl
@[simp] theorem foldl_cons (f : Ξ² β Ξ± β Ξ²) (H b a s) :
foldl f H b (a ::β s) = foldl f H (f b a) s :=
quot.induction_on s $ Ξ» l, rfl
@[simp] theorem foldl_add (f : Ξ² β Ξ± β Ξ²) (H b s t) :
foldl f H b (s + t) = foldl f H (foldl f H b s) t :=
quotient.induction_onβ s t $ Ξ» lβ lβ, foldl_append _ _ _ _
/-- `foldr f H b s` is the lift of the list operation `foldr f b l`,
which folds `f` over the multiset. It is well defined when `f` is left-commutative,
that is, `f aβ (f aβ b) = f aβ (f aβ b)`. -/
def foldr (f : Ξ± β Ξ² β Ξ²) (H : left_commutative f) (b : Ξ²) (s : multiset Ξ±) : Ξ² :=
quot.lift_on s (Ξ» l, foldr f b l)
(Ξ» lβ lβ p, p.foldr_eq H b)
@[simp] theorem foldr_zero (f : Ξ± β Ξ² β Ξ²) (H b) : foldr f H b 0 = b := rfl
@[simp] theorem foldr_cons (f : Ξ± β Ξ² β Ξ²) (H b a s) :
foldr f H b (a ::β s) = f a (foldr f H b s) :=
quot.induction_on s $ Ξ» l, rfl
@[simp] theorem foldr_singleton (f : Ξ± β Ξ² β Ξ²) (H b a) :
foldr f H b ({a} : multiset Ξ±) = f a b :=
rfl
@[simp] theorem foldr_add (f : Ξ± β Ξ² β Ξ²) (H b s t) :
foldr f H b (s + t) = foldr f H (foldr f H b t) s :=
quotient.induction_onβ s t $ Ξ» lβ lβ, foldr_append _ _ _ _
@[simp] theorem coe_foldr (f : Ξ± β Ξ² β Ξ²) (H : left_commutative f) (b : Ξ²) (l : list Ξ±) :
foldr f H b l = l.foldr f b := rfl
@[simp] theorem coe_foldl (f : Ξ² β Ξ± β Ξ²) (H : right_commutative f) (b : Ξ²) (l : list Ξ±) :
foldl f H b l = l.foldl f b := rfl
theorem coe_foldr_swap (f : Ξ± β Ξ² β Ξ²) (H : left_commutative f) (b : Ξ²) (l : list Ξ±) :
foldr f H b l = l.foldl (Ξ» x y, f y x) b :=
(congr_arg (foldr f H b) (coe_reverse l)).symm.trans $ foldr_reverse _ _ _
theorem foldr_swap (f : Ξ± β Ξ² β Ξ²) (H : left_commutative f) (b : Ξ²) (s : multiset Ξ±) :
foldr f H b s = foldl (Ξ» x y, f y x) (Ξ» x y z, (H _ _ _).symm) b s :=
quot.induction_on s $ Ξ» l, coe_foldr_swap _ _ _ _
theorem foldl_swap (f : Ξ² β Ξ± β Ξ²) (H : right_commutative f) (b : Ξ²) (s : multiset Ξ±) :
foldl f H b s = foldr (Ξ» x y, f y x) (Ξ» x y z, (H _ _ _).symm) b s :=
(foldr_swap _ _ _ _).symm
lemma foldr_induction' (f : Ξ± β Ξ² β Ξ²) (H : left_commutative f) (x : Ξ²) (q : Ξ± β Prop)
(p : Ξ² β Prop) (s : multiset Ξ±) (hpqf : β a b, q a β p b β p (f a b)) (px : p x)
(q_s : β a β s, q a) :
p (foldr f H x s) :=
begin
revert s,
refine multiset.induction (by simp [px]) _,
intros a s hs hsa,
rw foldr_cons,
have hps : β (x : Ξ±), x β s β q x, from Ξ» x hxs, hsa x (mem_cons_of_mem hxs),
exact hpqf a (foldr f H x s) (hsa a (mem_cons_self a s)) (hs hps),
end
lemma foldr_induction (f : Ξ± β Ξ± β Ξ±) (H : left_commutative f) (x : Ξ±) (p : Ξ± β Prop)
(s : multiset Ξ±) (p_f : β a b, p a β p b β p (f a b)) (px : p x) (p_s : β a β s, p a) :
p (foldr f H x s) :=
foldr_induction' f H x p p s p_f px p_s
lemma foldl_induction' (f : Ξ² β Ξ± β Ξ²) (H : right_commutative f) (x : Ξ²) (q : Ξ± β Prop)
(p : Ξ² β Prop) (s : multiset Ξ±) (hpqf : β a b, q a β p b β p (f b a)) (px : p x)
(q_s : β a β s, q a) :
p (foldl f H x s) :=
begin
rw foldl_swap,
exact foldr_induction' (Ξ» x y, f y x) (Ξ» x y z, (H _ _ _).symm) x q p s hpqf px q_s,
end
lemma foldl_induction (f : Ξ± β Ξ± β Ξ±) (H : right_commutative f) (x : Ξ±) (p : Ξ± β Prop)
(s : multiset Ξ±) (p_f : β a b, p a β p b β p (f b a)) (px : p x) (p_s : β a β s, p a) :
p (foldl f H x s) :=
foldl_induction' f H x p p s p_f px p_s
/-- Product of a multiset given a commutative monoid structure on `Ξ±`.
`prod {a, b, c} = a * b * c` -/
@[to_additive "Sum of a multiset given a commutative additive monoid structure on `Ξ±`.
`sum {a, b, c} = a + b + c`"]
def prod [comm_monoid Ξ±] : multiset Ξ± β Ξ± :=
foldr (*) (Ξ» x y z, by simp [mul_left_comm]) 1
@[to_additive]
theorem prod_eq_foldr [comm_monoid Ξ±] (s : multiset Ξ±) :
prod s = foldr (*) (Ξ» x y z, by simp [mul_left_comm]) 1 s := rfl
@[to_additive]
theorem prod_eq_foldl [comm_monoid Ξ±] (s : multiset Ξ±) :
prod s = foldl (*) (Ξ» x y z, by simp [mul_right_comm]) 1 s :=
(foldr_swap _ _ _ _).trans (by simp [mul_comm])
@[simp, norm_cast, to_additive]
theorem coe_prod [comm_monoid Ξ±] (l : list Ξ±) : prod βl = l.prod :=
prod_eq_foldl _
@[simp, to_additive] theorem prod_to_list [comm_monoid Ξ±] (s : multiset Ξ±) :
s.to_list.prod = s.prod :=
begin
conv_rhs { rw βcoe_to_list s, },
rw coe_prod,
end
@[simp, to_additive]
theorem prod_zero [comm_monoid Ξ±] : @prod Ξ± _ 0 = 1 := rfl
@[simp, to_additive]
theorem prod_cons [comm_monoid Ξ±] (a : Ξ±) (s) : prod (a ::β s) = a * prod s :=
foldr_cons _ _ _ _ _
@[simp, to_additive]
theorem prod_singleton [comm_monoid Ξ±] (a : Ξ±) : prod {a} = a :=
by simp only [mul_one, prod_cons, singleton_eq_cons, eq_self_iff_true, prod_zero]
@[simp, to_additive]
theorem prod_add [comm_monoid Ξ±] (s t : multiset Ξ±) : prod (s + t) = prod s * prod t :=
quotient.induction_onβ s t $ Ξ» lβ lβ, by simp
/-- `multiset.sum`, the sum of the elements of a multiset, promoted to a morphism of
`add_comm_monoid`s. -/
def sum_add_monoid_hom [add_comm_monoid Ξ±] : multiset Ξ± β+ Ξ± :=
{ to_fun := sum,
map_zero' := sum_zero,
map_add' := sum_add }
@[simp] lemma coe_sum_add_monoid_hom [add_comm_monoid Ξ±] :
(sum_add_monoid_hom : multiset Ξ± β Ξ±) = sum := rfl
lemma prod_nsmul {Ξ± : Type*} [comm_monoid Ξ±] (m : multiset Ξ±) :
β (n : β), (n β’ m).prod = m.prod ^ n
| 0 := by { rw [zero_nsmul, pow_zero], refl }
| (n + 1) :=
by rw [add_nsmul, one_nsmul, pow_add, pow_one, prod_add, prod_nsmul n]
@[simp, to_additive] theorem prod_repeat [comm_monoid Ξ±] (a : Ξ±) (n : β) :
prod (multiset.repeat a n) = a ^ n :=
by simp [repeat, list.prod_repeat]
@[to_additive]
lemma prod_map_one [comm_monoid Ξ³] {m : multiset Ξ±} :
prod (m.map (Ξ»a, (1 : Ξ³))) = (1 : Ξ³) :=
by simp
@[simp, to_additive]
lemma prod_map_mul [comm_monoid Ξ³] {m : multiset Ξ±} {f g : Ξ± β Ξ³} :
prod (m.map $ Ξ»a, f a * g a) = prod (m.map f) * prod (m.map g) :=
multiset.induction_on m (by simp) (assume a m ih, by simp [ih]; cc)
@[to_additive]
lemma prod_map_prod_map [comm_monoid Ξ³] (m : multiset Ξ±) (n : multiset Ξ²) {f : Ξ± β Ξ² β Ξ³} :
prod (m.map $ Ξ»a, prod $ n.map $ Ξ»b, f a b) = prod (n.map $ Ξ»b, prod $ m.map $ Ξ»a, f a b) :=
multiset.induction_on m (by simp) (assume a m ih, by simp [ih])
lemma sum_map_mul_left [semiring Ξ²] {b : Ξ²} {s : multiset Ξ±} {f : Ξ± β Ξ²} :
sum (s.map (Ξ»a, b * f a)) = b * sum (s.map f) :=
multiset.induction_on s (by simp) (assume a s ih, by simp [ih, mul_add])
lemma sum_map_mul_right [semiring Ξ²] {b : Ξ²} {s : multiset Ξ±} {f : Ξ± β Ξ²} :
sum (s.map (Ξ»a, f a * b)) = sum (s.map f) * b :=
multiset.induction_on s (by simp) (assume a s ih, by simp [ih, add_mul])
lemma prod_eq_zero {Mβ : Type*} [comm_monoid_with_zero Mβ] {s : multiset Mβ} (h : (0 : Mβ) β s) :
multiset.prod s = 0 :=
begin
rcases multiset.exists_cons_of_mem h with β¨s', hs'β©,
simp [hs', multiset.prod_cons]
end
lemma prod_eq_zero_iff {Mβ : Type*} [comm_monoid_with_zero Mβ] [no_zero_divisors Mβ] [nontrivial Mβ]
{s : multiset Mβ} :
multiset.prod s = 0 β (0 : Mβ) β s :=
by { rcases s with β¨lβ©, simp }
theorem prod_ne_zero {Mβ : Type*} [comm_monoid_with_zero Mβ] [no_zero_divisors Mβ] [nontrivial Mβ]
{m : multiset Mβ} (h : (0 : Mβ) β m) : m.prod β 0 :=
mt prod_eq_zero_iff.1 h
@[to_additive]
lemma prod_hom [comm_monoid Ξ±] [comm_monoid Ξ²] (s : multiset Ξ±) (f : Ξ± β* Ξ²) :
(s.map f).prod = f s.prod :=
quotient.induction_on s $ Ξ» l, by simp only [l.prod_hom f, quot_mk_to_coe, coe_map, coe_prod]
@[to_additive]
theorem prod_hom_rel [comm_monoid Ξ²] [comm_monoid Ξ³] (s : multiset Ξ±) {r : Ξ² β Ξ³ β Prop}
{f : Ξ± β Ξ²} {g : Ξ± β Ξ³} (hβ : r 1 1) (hβ : ββ¦a b cβ¦, r b c β r (f a * b) (g a * c)) :
r (s.map f).prod (s.map g).prod :=
quotient.induction_on s $ Ξ» l,
by simp only [l.prod_hom_rel hβ hβ, quot_mk_to_coe, coe_map, coe_prod]
@[simp] lemma coe_inv_monoid_hom {G : Type*} [comm_group G] :
(comm_group.inv_monoid_hom : G β G) = has_inv.inv := rfl
@[simp, to_additive]
lemma prod_map_inv {G : Type*} [comm_group G] (m : multiset G) :
(m.map has_inv.inv).prod = m.prodβ»ΒΉ :=
m.prod_hom comm_group.inv_monoid_hom
lemma dvd_prod [comm_monoid Ξ±] {a : Ξ±} {s : multiset Ξ±} : a β s β a β£ s.prod :=
quotient.induction_on s (Ξ» l a h, by simpa using list.dvd_prod h) a
lemma prod_dvd_prod [comm_monoid Ξ±] {s t : multiset Ξ±} (h : s β€ t) :
s.prod β£ t.prod :=
begin
rcases multiset.le_iff_exists_add.1 h with β¨z, rflβ©,
simp,
end
lemma prod_nonneg [ordered_comm_semiring Ξ±] {m : multiset Ξ±} (h : β a β m, (0 : Ξ±) β€ a) :
0 β€ m.prod :=
begin
revert h,
refine m.induction_on _ _,
{ rintro -, rw prod_zero, exact zero_le_one },
{ intros a s hs ih,
rw prod_cons,
apply mul_nonneg,
{ exact ih _ (mem_cons_self _ _) },
{ exact hs (Ξ» a ha, ih _ (mem_cons_of_mem ha)) } }
end
@[to_additive sum_nonneg]
lemma one_le_prod_of_one_le [ordered_comm_monoid Ξ±] {m : multiset Ξ±} :
(β x β m, (1 : Ξ±) β€ x) β 1 β€ m.prod :=
quotient.induction_on m $ Ξ» l hl, by simpa using list.one_le_prod_of_one_le hl
@[to_additive]
lemma single_le_prod [ordered_comm_monoid Ξ±] {m : multiset Ξ±} :
(β x β m, (1 : Ξ±) β€ x) β β x β m, x β€ m.prod :=
quotient.induction_on m $ Ξ» l hl x hx, by simpa using list.single_le_prod hl x hx
@[to_additive]
lemma prod_le_of_forall_le [ordered_comm_monoid Ξ±] (l : multiset Ξ±) (n : Ξ±) (h : β (x β l), x β€ n) :
l.prod β€ n ^ l.card :=
begin
induction l using quotient.induction_on,
simpa using list.prod_le_of_forall_le _ _ h
end
@[to_additive all_zero_of_le_zero_le_of_sum_eq_zero]
lemma all_one_of_le_one_le_of_prod_eq_one [ordered_comm_monoid Ξ±] {m : multiset Ξ±} :
(β x β m, (1 : Ξ±) β€ x) β m.prod = 1 β (β x β m, x = (1 : Ξ±)) :=
begin
apply quotient.induction_on m,
simp only [quot_mk_to_coe, coe_prod, mem_coe],
intros l hlβ hlβ x hx,
apply all_one_of_le_one_le_of_prod_eq_one hlβ hlβ _ hx,
end
lemma sum_eq_zero_iff [canonically_ordered_add_monoid Ξ±] {m : multiset Ξ±} :
m.sum = 0 β β x β m, x = (0 : Ξ±) :=
quotient.induction_on m $ Ξ» l, by simpa using list.sum_eq_zero_iff l
@[to_additive]
lemma prod_induction {M : Type*} [comm_monoid M] (p : M β Prop) (s : multiset M)
(p_mul : β a b, p a β p b β p (a * b)) (p_one : p 1) (p_s : β a β s, p a) :
p s.prod :=
begin
rw prod_eq_foldr,
exact foldr_induction (*) (Ξ» x y z, by simp [mul_left_comm]) 1 p s p_mul p_one p_s,
end
@[to_additive le_sum_of_subadditive_on_pred]
lemma le_prod_of_submultiplicative_on_pred [comm_monoid Ξ±] [ordered_comm_monoid Ξ²]
(f : Ξ± β Ξ²) (p : Ξ± β Prop) (h_one : f 1 = 1) (hp_one : p 1)
(h_mul : β a b, p a β p b β f (a * b) β€ f a * f b)
(hp_mul : β a b, p a β p b β p (a * b)) (s : multiset Ξ±) (hps : β a, a β s β p a) :
f s.prod β€ (s.map f).prod :=
begin
revert s,
refine multiset.induction _ _,
{ simp [le_of_eq h_one], },
intros a s hs hpsa,
have hps : β x, x β s β p x, from Ξ» x hx, hpsa x (mem_cons_of_mem hx),
have hp_prod : p s.prod, from prod_induction p s hp_mul hp_one hps,
rw [prod_cons, map_cons, prod_cons],
exact (h_mul a s.prod (hpsa a (mem_cons_self a s)) hp_prod).trans (mul_le_mul_left' (hs hps) _),
end
@[to_additive le_sum_of_subadditive]
lemma le_prod_of_submultiplicative [comm_monoid Ξ±] [ordered_comm_monoid Ξ²]
(f : Ξ± β Ξ²) (h_one : f 1 = 1) (h_mul : β a b, f (a * b) β€ f a * f b) (s : multiset Ξ±) :
f s.prod β€ (s.map f).prod :=
le_prod_of_submultiplicative_on_pred f (Ξ» i, true) h_one trivial (Ξ» x y _ _ , h_mul x y) (by simp)
s (by simp)
@[to_additive]
lemma prod_induction_nonempty {M : Type*} [comm_monoid M] (p : M β Prop)
(p_mul : β a b, p a β p b β p (a * b)) {s : multiset M} (hs_nonempty : s β β
)
(p_s : β a β s, p a) :
p s.prod :=
begin
revert s,
refine multiset.induction _ _,
{ intro h,
exfalso,
simpa using h, },
intros a s hs hsa hpsa,
rw prod_cons,
by_cases hs_empty : s = β
,
{ simp [hs_empty, hpsa a], },
have hps : β (x : M), x β s β p x, from Ξ» x hxs, hpsa x (mem_cons_of_mem hxs),
exact p_mul a s.prod (hpsa a (mem_cons_self a s)) (hs hs_empty hps),
end
@[to_additive le_sum_nonempty_of_subadditive_on_pred]
lemma le_prod_nonempty_of_submultiplicative_on_pred [comm_monoid Ξ±] [ordered_comm_monoid Ξ²]
(f : Ξ± β Ξ²) (p : Ξ± β Prop) (h_mul : β a b, p a β p b β f (a * b) β€ f a * f b)
(hp_mul : β a b, p a β p b β p (a * b)) (s : multiset Ξ±) (hs_nonempty : s β β
)
(hs : β a, a β s β p a) :
f s.prod β€ (s.map f).prod :=
begin
revert s,
refine multiset.induction _ _,
{ intro h,
exfalso,
exact h rfl, },
rintros a s hs hsa_nonempty hsa_prop,
rw [prod_cons, map_cons, prod_cons],
by_cases hs_empty : s = β
,
{ simp [hs_empty], },
have hsa_restrict : (β x, x β s β p x), from Ξ» x hx, hsa_prop x (mem_cons_of_mem hx),
have hp_sup : p s.prod,
from prod_induction_nonempty p hp_mul hs_empty hsa_restrict,
have hp_a : p a, from hsa_prop a (mem_cons_self a s),
exact (h_mul a _ hp_a hp_sup).trans (mul_le_mul_left' (hs hs_empty hsa_restrict) _),
end
@[to_additive le_sum_nonempty_of_subadditive]
lemma le_prod_nonempty_of_submultiplicative [comm_monoid Ξ±] [ordered_comm_monoid Ξ²]
(f : Ξ± β Ξ²) (h_mul : β a b, f (a * b) β€ f a * f b) (s : multiset Ξ±) (hs_nonempty : s β β
) :
f s.prod β€ (s.map f).prod :=
le_prod_nonempty_of_submultiplicative_on_pred f (Ξ» i, true) (by simp [h_mul]) (by simp) s
hs_nonempty (by simp)
theorem dvd_sum [comm_semiring Ξ±] {a : Ξ±} {s : multiset Ξ±} : (β x β s, a β£ x) β a β£ s.sum :=
multiset.induction_on s (Ξ» _, dvd_zero _)
(Ξ» x s ih h, by rw sum_cons; exact dvd_add
(h _ (mem_cons_self _ _)) (ih (Ξ» y hy, h _ (mem_cons.2 (or.inr hy)))))
@[simp] theorem sum_map_singleton (s : multiset Ξ±) : (s.map (Ξ» a, ({a} : multiset Ξ±))).sum = s :=
multiset.induction_on s (by simp) (by simp [singleton_eq_cons])
theorem abs_sum_le_sum_abs [linear_ordered_add_comm_group Ξ±] {s : multiset Ξ±} :
abs s.sum β€ (s.map abs).sum :=
le_sum_of_subadditive _ abs_zero abs_add s
/-! ### Join -/
/-- `join S`, where `S` is a multiset of multisets, is the lift of the list join
operation, that is, the union of all the sets.
join {{1, 2}, {1, 2}, {0, 1}} = {0, 1, 1, 1, 2, 2} -/
def join : multiset (multiset Ξ±) β multiset Ξ± := sum
theorem coe_join : β L : list (list Ξ±),
join (L.map (@coe _ (multiset Ξ±) _) : multiset (multiset Ξ±)) = L.join
| [] := rfl
| (l :: L) := congr_arg (Ξ» s : multiset Ξ±, βl + s) (coe_join L)
@[simp] theorem join_zero : @join Ξ± 0 = 0 := rfl
@[simp] theorem join_cons (s S) : @join Ξ± (s ::β S) = s + join S :=
sum_cons _ _
@[simp] theorem join_add (S T) : @join Ξ± (S + T) = join S + join T :=
sum_add _ _
@[simp] theorem singleton_join (a) : join ({a} : multiset (multiset Ξ±)) = a :=
sum_singleton _
@[simp] theorem mem_join {a S} : a β @join Ξ± S β β s β S, a β s :=
multiset.induction_on S (by simp) $
by simp [or_and_distrib_right, exists_or_distrib] {contextual := tt}
@[simp] theorem card_join (S) : card (@join Ξ± S) = sum (map card S) :=
multiset.induction_on S (by simp) (by simp)
/-! ### `multiset.bind` -/
/-- `bind s f` is the monad bind operation, defined as `join (map f s)`.
It is the union of `f a` as `a` ranges over `s`. -/
def bind (s : multiset Ξ±) (f : Ξ± β multiset Ξ²) : multiset Ξ² :=
join (map f s)
@[simp] theorem coe_bind (l : list Ξ±) (f : Ξ± β list Ξ²) :
@bind Ξ± Ξ² l (Ξ» a, f a) = l.bind f :=
by rw [list.bind, β coe_join, list.map_map]; refl
@[simp] theorem zero_bind (f : Ξ± β multiset Ξ²) : bind 0 f = 0 := rfl
@[simp] theorem cons_bind (a s) (f : Ξ± β multiset Ξ²) : bind (a ::β s) f = f a + bind s f :=
by simp [bind]
@[simp] theorem singleton_bind (a) (f : Ξ± β multiset Ξ²) : bind {a} f = f a :=
by simp [bind]
@[simp] theorem add_bind (s t) (f : Ξ± β multiset Ξ²) : bind (s + t) f = bind s f + bind t f :=
by simp [bind]
@[simp] theorem bind_zero (s : multiset Ξ±) : bind s (Ξ»a, 0 : Ξ± β multiset Ξ²) = 0 :=
by simp [bind, join, nsmul_zero]
@[simp] theorem bind_add (s : multiset Ξ±) (f g : Ξ± β multiset Ξ²) :
bind s (Ξ»a, f a + g a) = bind s f + bind s g :=
by simp [bind, join]
@[simp] theorem bind_cons (s : multiset Ξ±) (f : Ξ± β Ξ²) (g : Ξ± β multiset Ξ²) :
bind s (Ξ»a, f a ::β g a) = map f s + bind s g :=
multiset.induction_on s (by simp) (by simp [add_comm, add_left_comm] {contextual := tt})
@[simp] theorem bind_singleton (s : multiset Ξ±) (f : Ξ± β Ξ²) :
bind s (Ξ» x, ({f x} : multiset Ξ²)) = map f s :=
multiset.induction_on s (by rw [zero_bind, map_zero]) (by simp [singleton_add])
@[simp] theorem mem_bind {b s} {f : Ξ± β multiset Ξ²} : b β bind s f β β a β s, b β f a :=
by simp [bind]; simp [-exists_and_distrib_right, exists_and_distrib_right.symm];
rw exists_swap; simp [and_assoc]
@[simp] theorem card_bind (s) (f : Ξ± β multiset Ξ²) : card (bind s f) = sum (map (card β f) s) :=
by simp [bind]
lemma bind_congr {f g : Ξ± β multiset Ξ²} {m : multiset Ξ±} :
(βaβm, f a = g a) β bind m f = bind m g :=
by simp [bind] {contextual := tt}
lemma bind_hcongr {Ξ²' : Type*} {m : multiset Ξ±} {f : Ξ± β multiset Ξ²} {f' : Ξ± β multiset Ξ²'}
(h : Ξ² = Ξ²') (hf : βaβm, f a == f' a) : bind m f == bind m f' :=
begin subst h, simp at hf, simp [bind_congr hf] end
lemma map_bind (m : multiset Ξ±) (n : Ξ± β multiset Ξ²) (f : Ξ² β Ξ³) :
map f (bind m n) = bind m (Ξ»a, map f (n a)) :=
multiset.induction_on m (by simp) (by simp {contextual := tt})
lemma bind_map (m : multiset Ξ±) (n : Ξ² β multiset Ξ³) (f : Ξ± β Ξ²) :
bind (map f m) n = bind m (Ξ»a, n (f a)) :=
multiset.induction_on m (by simp) (by simp {contextual := tt})
lemma bind_assoc {s : multiset Ξ±} {f : Ξ± β multiset Ξ²} {g : Ξ² β multiset Ξ³} :
(s.bind f).bind g = s.bind (Ξ»a, (f a).bind g) :=
multiset.induction_on s (by simp) (by simp {contextual := tt})
lemma bind_bind (m : multiset Ξ±) (n : multiset Ξ²) {f : Ξ± β Ξ² β multiset Ξ³} :
(bind m $ Ξ»a, bind n $ Ξ»b, f a b) = (bind n $ Ξ»b, bind m $ Ξ»a, f a b) :=
multiset.induction_on m (by simp) (by simp {contextual := tt})
lemma bind_map_comm (m : multiset Ξ±) (n : multiset Ξ²) {f : Ξ± β Ξ² β Ξ³} :
(bind m $ Ξ»a, n.map $ Ξ»b, f a b) = (bind n $ Ξ»b, m.map $ Ξ»a, f a b) :=
multiset.induction_on m (by simp) (by simp {contextual := tt})
@[simp, to_additive]
lemma prod_bind [comm_monoid Ξ²] (s : multiset Ξ±) (t : Ξ± β multiset Ξ²) :
prod (bind s t) = prod (s.map $ Ξ»a, prod (t a)) :=
multiset.induction_on s (by simp) (assume a s ih, by simp [ih, cons_bind])
/-! ### Product of two `multiset`s -/
/-- The multiplicity of `(a, b)` in `product s t` is
the product of the multiplicity of `a` in `s` and `b` in `t`. -/
def product (s : multiset Ξ±) (t : multiset Ξ²) : multiset (Ξ± Γ Ξ²) :=
s.bind $ Ξ» a, t.map $ prod.mk a
@[simp] theorem coe_product (lβ : list Ξ±) (lβ : list Ξ²) :
@product Ξ± Ξ² lβ lβ = lβ.product lβ :=
by rw [product, list.product, β coe_bind]; simp
@[simp] theorem zero_product (t) : @product Ξ± Ξ² 0 t = 0 := rfl
@[simp] theorem cons_product (a : Ξ±) (s : multiset Ξ±) (t : multiset Ξ²) :
product (a ::β s) t = map (prod.mk a) t + product s t :=
by simp [product]
@[simp] theorem product_singleton (a : Ξ±) (b : Ξ²) :
product ({a} : multiset Ξ±) ({b} : multiset Ξ²) = {(a,b)} :=
by simp only [product, bind_singleton, map_singleton]
@[simp] theorem add_product (s t : multiset Ξ±) (u : multiset Ξ²) :
product (s + t) u = product s u + product t u :=
by simp [product]
@[simp] theorem product_add (s : multiset Ξ±) : β t u : multiset Ξ²,
product s (t + u) = product s t + product s u :=
multiset.induction_on s (Ξ» t u, rfl) $ Ξ» a s IH t u,
by rw [cons_product, IH]; simp; cc
@[simp] theorem mem_product {s t} : β {p : Ξ± Γ Ξ²}, p β @product Ξ± Ξ² s t β p.1 β s β§ p.2 β t
| (a, b) := by simp [product, and.left_comm]
@[simp] theorem card_product (s : multiset Ξ±) (t : multiset Ξ²) :
card (product s t) = card s * card t :=
by simp [product, repeat, (β), mul_comm]
/-! ### Sigma multiset -/
section
variable {Ο : Ξ± β Type*}
/-- `sigma s t` is the dependent version of `product`. It is the sum of
`(a, b)` as `a` ranges over `s` and `b` ranges over `t a`. -/
protected def sigma (s : multiset Ξ±) (t : Ξ a, multiset (Ο a)) : multiset (Ξ£ a, Ο a) :=
s.bind $ Ξ» a, (t a).map $ sigma.mk a
@[simp] theorem coe_sigma (lβ : list Ξ±) (lβ : Ξ a, list (Ο a)) :
@multiset.sigma Ξ± Ο lβ (Ξ» a, lβ a) = lβ.sigma lβ :=
by rw [multiset.sigma, list.sigma, β coe_bind]; simp
@[simp] theorem zero_sigma (t) : @multiset.sigma Ξ± Ο 0 t = 0 := rfl
@[simp] theorem cons_sigma (a : Ξ±) (s : multiset Ξ±) (t : Ξ a, multiset (Ο a)) :
(a ::β s).sigma t = map (sigma.mk a) (t a) + s.sigma t :=
by simp [multiset.sigma]
@[simp] theorem sigma_singleton (a : Ξ±) (b : Ξ± β Ξ²) :
({a} : multiset Ξ±).sigma (Ξ» a, ({b a} : multiset Ξ²)) = {β¨a, b aβ©} := rfl
@[simp] theorem add_sigma (s t : multiset Ξ±) (u : Ξ a, multiset (Ο a)) :
(s + t).sigma u = s.sigma u + t.sigma u :=
by simp [multiset.sigma]
@[simp] theorem sigma_add (s : multiset Ξ±) : β t u : Ξ a, multiset (Ο a),
s.sigma (Ξ» a, t a + u a) = s.sigma t + s.sigma u :=
multiset.induction_on s (Ξ» t u, rfl) $ Ξ» a s IH t u,
by rw [cons_sigma, IH]; simp; cc
@[simp] theorem mem_sigma {s t} : β {p : Ξ£ a, Ο a},
p β @multiset.sigma Ξ± Ο s t β p.1 β s β§ p.2 β t p.1
| β¨a, bβ© := by simp [multiset.sigma, and_assoc, and.left_comm]
@[simp] theorem card_sigma (s : multiset Ξ±) (t : Ξ a, multiset (Ο a)) :
card (s.sigma t) = sum (map (Ξ» a, card (t a)) s) :=
by simp [multiset.sigma, (β)]
end
/-! ### Map for partial functions -/
/-- Lift of the list `pmap` operation. Map a partial function `f` over a multiset
`s` whose elements are all in the domain of `f`. -/
def pmap {p : Ξ± β Prop} (f : Ξ a, p a β Ξ²) (s : multiset Ξ±) : (β a β s, p a) β multiset Ξ² :=
quot.rec_on s (Ξ» l H, β(pmap f l H)) $ Ξ» lβ lβ (pp : lβ ~ lβ),
funext $ Ξ» (Hβ : β a β lβ, p a),
have Hβ : β a β lβ, p a, from Ξ» a h, Hβ a (pp.subset h),
have β {sβ e H}, @eq.rec (multiset Ξ±) lβ
(Ξ» s, (β a β s, p a) β multiset Ξ²) (Ξ» _, β(pmap f lβ Hβ))
sβ e H = β(pmap f lβ Hβ), by intros sβ e _; subst e,
this.trans $ quot.sound $ pp.pmap f
@[simp] theorem coe_pmap {p : Ξ± β Prop} (f : Ξ a, p a β Ξ²)
(l : list Ξ±) (H : β a β l, p a) : pmap f l H = l.pmap f H := rfl
@[simp] lemma pmap_zero {p : Ξ± β Prop} (f : Ξ a, p a β Ξ²) (h : βaβ(0:multiset Ξ±), p a) :
pmap f 0 h = 0 := rfl
@[simp] lemma pmap_cons {p : Ξ± β Prop} (f : Ξ a, p a β Ξ²) (a : Ξ±) (m : multiset Ξ±) :
β(h : βbβa ::β m, p b), pmap f (a ::β m) h =
f a (h a (mem_cons_self a m)) ::β pmap f m (Ξ»a ha, h a $ mem_cons_of_mem ha) :=
quotient.induction_on m $ assume l h, rfl
/-- "Attach" a proof that `a β s` to each element `a` in `s` to produce
a multiset on `{x // x β s}`. -/
def attach (s : multiset Ξ±) : multiset {x // x β s} := pmap subtype.mk s (Ξ» a, id)
@[simp] theorem coe_attach (l : list Ξ±) :
@eq (multiset {x // x β l}) (@attach Ξ± l) l.attach := rfl
theorem sizeof_lt_sizeof_of_mem [has_sizeof Ξ±] {x : Ξ±} {s : multiset Ξ±} (hx : x β s) :
sizeof x < sizeof s := by
{ induction s with l a b, exact list.sizeof_lt_sizeof_of_mem hx, refl }
theorem pmap_eq_map (p : Ξ± β Prop) (f : Ξ± β Ξ²) (s : multiset Ξ±) :
β H, @pmap _ _ p (Ξ» a _, f a) s H = map f s :=
quot.induction_on s $ Ξ» l H, congr_arg coe $ pmap_eq_map p f l H
theorem pmap_congr {p q : Ξ± β Prop} {f : Ξ a, p a β Ξ²} {g : Ξ a, q a β Ξ²}
(s : multiset Ξ±) {Hβ Hβ} (h : β a hβ hβ, f a hβ = g a hβ) :
pmap f s Hβ = pmap g s Hβ :=
quot.induction_on s (Ξ» l Hβ Hβ, congr_arg coe $ pmap_congr l h) Hβ Hβ
theorem map_pmap {p : Ξ± β Prop} (g : Ξ² β Ξ³) (f : Ξ a, p a β Ξ²)
(s) : β H, map g (pmap f s H) = pmap (Ξ» a h, g (f a h)) s H :=
quot.induction_on s $ Ξ» l H, congr_arg coe $ map_pmap g f l H
theorem pmap_eq_map_attach {p : Ξ± β Prop} (f : Ξ a, p a β Ξ²)
(s) : β H, pmap f s H = s.attach.map (Ξ» x, f x.1 (H _ x.2)) :=
quot.induction_on s $ Ξ» l H, congr_arg coe $ pmap_eq_map_attach f l H
theorem attach_map_val (s : multiset Ξ±) : s.attach.map subtype.val = s :=
quot.induction_on s $ Ξ» l, congr_arg coe $ attach_map_val l
@[simp] theorem mem_attach (s : multiset Ξ±) : β x, x β s.attach :=
quot.induction_on s $ Ξ» l, mem_attach _
@[simp] theorem mem_pmap {p : Ξ± β Prop} {f : Ξ a, p a β Ξ²}
{s H b} : b β pmap f s H β β a (h : a β s), f a (H a h) = b :=
quot.induction_on s (Ξ» l H, mem_pmap) H
@[simp] theorem card_pmap {p : Ξ± β Prop} (f : Ξ a, p a β Ξ²)
(s H) : card (pmap f s H) = card s :=
quot.induction_on s (Ξ» l H, length_pmap) H
@[simp] theorem card_attach {m : multiset Ξ±} : card (attach m) = card m := card_pmap _ _ _
@[simp] lemma attach_zero : (0 : multiset Ξ±).attach = 0 := rfl
lemma attach_cons (a : Ξ±) (m : multiset Ξ±) :
(a ::β m).attach = β¨a, mem_cons_self a mβ© ::β (m.attach.map $ Ξ»p, β¨p.1, mem_cons_of_mem p.2β©) :=
quotient.induction_on m $ assume l, congr_arg coe $ congr_arg (list.cons _) $
by rw [list.map_pmap]; exact list.pmap_congr _ (assume a' hβ hβ, subtype.eq rfl)
section decidable_pi_exists
variables {m : multiset Ξ±}
/-- If `p` is a decidable predicate,
so is the predicate that all elements of a multiset satisfy `p`. -/
protected def decidable_forall_multiset {p : Ξ± β Prop} [hp : βa, decidable (p a)] :
decidable (βaβm, p a) :=
quotient.rec_on_subsingleton m (Ξ»l, decidable_of_iff (βaβl, p a) $ by simp)
instance decidable_dforall_multiset {p : Ξ aβm, Prop} [hp : βa (h : a β m), decidable (p a h)] :
decidable (βa (h : a β m), p a h) :=
decidable_of_decidable_of_iff
(@multiset.decidable_forall_multiset {a // a β m} m.attach (Ξ»a, p a.1 a.2) _)
(iff.intro (assume h a ha, h β¨a, haβ© (mem_attach _ _)) (assume h β¨a, haβ© _, h _ _))
/-- decidable equality for functions whose domain is bounded by multisets -/
instance decidable_eq_pi_multiset {Ξ² : Ξ± β Type*} [h : βa, decidable_eq (Ξ² a)] :
decidable_eq (Ξ aβm, Ξ² a) :=
assume f g, decidable_of_iff (βa (h : a β m), f a h = g a h) (by simp [function.funext_iff])
/-- If `p` is a decidable predicate,
so is the existence of an element in a multiset satisfying `p`. -/
def decidable_exists_multiset {p : Ξ± β Prop} [decidable_pred p] :
decidable (β x β m, p x) :=
quotient.rec_on_subsingleton m list.decidable_exists_mem
instance decidable_dexists_multiset {p : Ξ aβm, Prop} [hp : βa (h : a β m), decidable (p a h)] :
decidable (βa (h : a β m), p a h) :=
decidable_of_decidable_of_iff
(@multiset.decidable_exists_multiset {a // a β m} m.attach (Ξ»a, p a.1 a.2) _)
(iff.intro (Ξ» β¨β¨a, haββ©, _, haββ©, β¨a, haβ, haββ©)
(Ξ» β¨a, haβ, haββ©, β¨β¨a, haββ©, mem_attach _ _, haββ©))
end decidable_pi_exists
/-! ### Subtraction -/
section
variables [decidable_eq Ξ±] {s t u : multiset Ξ±} {a b : Ξ±}
/-- `s - t` is the multiset such that `count a (s - t) = count a s - count a t` for all `a`
(note that it is truncated subtraction, so it is `0` if `count a t β₯ count a s`). -/
protected def sub (s t : multiset Ξ±) : multiset Ξ± :=
quotient.lift_onβ s t (Ξ» lβ lβ, (lβ.diff lβ : multiset Ξ±)) $ Ξ» vβ vβ wβ wβ pβ pβ,
quot.sound $ pβ.diff pβ
instance : has_sub (multiset Ξ±) := β¨multiset.subβ©
@[simp] theorem coe_sub (s t : list Ξ±) : (s - t : multiset Ξ±) = (s.diff t : list Ξ±) := rfl
/-- This is a special case of `tsub_zero`, which should be used instead of this.
This is needed to prove `has_ordered_sub (multiset Ξ±)`. -/
protected theorem sub_zero (s : multiset Ξ±) : s - 0 = s :=
quot.induction_on s $ Ξ» l, rfl
@[simp] theorem sub_cons (a : Ξ±) (s t : multiset Ξ±) : s - a ::β t = s.erase a - t :=
quotient.induction_onβ s t $ Ξ» lβ lβ, congr_arg coe $ diff_cons _ _ _
/-- This is a special case of `tsub_le_iff_right`, which should be used instead of this.
This is needed to prove `has_ordered_sub (multiset Ξ±)`. -/
protected theorem sub_le_iff_le_add : s - t β€ u β s β€ u + t :=
by revert s; exact
multiset.induction_on t (by simp [multiset.sub_zero])
(Ξ» a t IH s, by simp [IH, erase_le_iff_le_cons])
instance : has_ordered_sub (multiset Ξ±) :=
β¨Ξ» n m k, multiset.sub_le_iff_le_addβ©
theorem sub_eq_fold_erase (s t : multiset Ξ±) : s - t = foldl erase erase_comm s t :=
quotient.induction_onβ s t $ Ξ» lβ lβ,
show β(lβ.diff lβ) = foldl erase erase_comm βlβ βlβ,
by { rw diff_eq_foldl lβ lβ, symmetry, exact foldl_hom _ _ _ _ _ (Ξ» x y, rfl) }
@[simp] theorem card_sub {s t : multiset Ξ±} (h : t β€ s) : card (s - t) = card s - card t :=
(tsub_eq_of_eq_add_rev $ by rw [add_comm, β card_add, tsub_add_cancel_of_le h]).symm
/-! ### Union -/
/-- `s βͺ t` is the lattice join operation with respect to the
multiset `β€`. The multiplicity of `a` in `s βͺ t` is the maximum
of the multiplicities in `s` and `t`. -/
def union (s t : multiset Ξ±) : multiset Ξ± := s - t + t
instance : has_union (multiset Ξ±) := β¨unionβ©
theorem union_def (s t : multiset Ξ±) : s βͺ t = s - t + t := rfl
theorem le_union_left (s t : multiset Ξ±) : s β€ s βͺ t := le_tsub_add
theorem le_union_right (s t : multiset Ξ±) : t β€ s βͺ t := le_add_left _ _
theorem eq_union_left : t β€ s β s βͺ t = s := tsub_add_cancel_of_le
theorem union_le_union_right (h : s β€ t) (u) : s βͺ u β€ t βͺ u :=
add_le_add_right (tsub_le_tsub_right h _) u
theorem union_le (hβ : s β€ u) (hβ : t β€ u) : s βͺ t β€ u :=
by rw β eq_union_left hβ; exact union_le_union_right hβ t
@[simp] theorem mem_union : a β s βͺ t β a β s β¨ a β t :=
β¨Ξ» h, (mem_add.1 h).imp_left (mem_of_le tsub_le_self),
or.rec (mem_of_le $ le_union_left _ _) (mem_of_le $ le_union_right _ _)β©
@[simp] theorem map_union [decidable_eq Ξ²] {f : Ξ± β Ξ²} (finj : function.injective f)
{s t : multiset Ξ±} :
map f (s βͺ t) = map f s βͺ map f t :=
quotient.induction_onβ s t $ Ξ» lβ lβ,
congr_arg coe (by rw [list.map_append f, list.map_diff finj])
/-! ### Intersection -/
/-- `s β© t` is the lattice meet operation with respect to the
multiset `β€`. The multiplicity of `a` in `s β© t` is the minimum
of the multiplicities in `s` and `t`. -/
def inter (s t : multiset Ξ±) : multiset Ξ± :=
quotient.lift_onβ s t (Ξ» lβ lβ, (lβ.bag_inter lβ : multiset Ξ±)) $ Ξ» vβ vβ wβ wβ pβ pβ,
quot.sound $ pβ.bag_inter pβ
instance : has_inter (multiset Ξ±) := β¨interβ©
@[simp] theorem inter_zero (s : multiset Ξ±) : s β© 0 = 0 :=
quot.induction_on s $ Ξ» l, congr_arg coe l.bag_inter_nil
@[simp] theorem zero_inter (s : multiset Ξ±) : 0 β© s = 0 :=
quot.induction_on s $ Ξ» l, congr_arg coe l.nil_bag_inter
@[simp] theorem cons_inter_of_pos {a} (s : multiset Ξ±) {t} :
a β t β (a ::β s) β© t = a ::β s β© t.erase a :=
quotient.induction_onβ s t $ Ξ» lβ lβ h,
congr_arg coe $ cons_bag_inter_of_pos _ h
@[simp] theorem cons_inter_of_neg {a} (s : multiset Ξ±) {t} :
a β t β (a ::β s) β© t = s β© t :=
quotient.induction_onβ s t $ Ξ» lβ lβ h,
congr_arg coe $ cons_bag_inter_of_neg _ h
theorem inter_le_left (s t : multiset Ξ±) : s β© t β€ s :=
quotient.induction_onβ s t $ Ξ» lβ lβ,
(bag_inter_sublist_left _ _).subperm
theorem inter_le_right (s : multiset Ξ±) : β t, s β© t β€ t :=
multiset.induction_on s (Ξ» t, (zero_inter t).symm βΈ zero_le _) $
Ξ» a s IH t, if h : a β t
then by simpa [h] using cons_le_cons a (IH (t.erase a))
else by simp [h, IH]
theorem le_inter (hβ : s β€ t) (hβ : s β€ u) : s β€ t β© u :=
begin
revert s u, refine multiset.induction_on t _ (Ξ» a t IH, _); intros,
{ simp [hβ] },
by_cases a β u,
{ rw [cons_inter_of_pos _ h, β erase_le_iff_le_cons],
exact IH (erase_le_iff_le_cons.2 hβ) (erase_le_erase _ hβ) },
{ rw cons_inter_of_neg _ h,
exact IH ((le_cons_of_not_mem $ mt (mem_of_le hβ) h).1 hβ) hβ }
end
@[simp] theorem mem_inter : a β s β© t β a β s β§ a β t :=
β¨Ξ» h, β¨mem_of_le (inter_le_left _ _) h, mem_of_le (inter_le_right _ _) hβ©,
Ξ» β¨hβ, hββ©, by rw [β cons_erase hβ, cons_inter_of_pos _ hβ]; apply mem_cons_selfβ©
instance : lattice (multiset Ξ±) :=
{ sup := (βͺ),
sup_le := @union_le _ _,
le_sup_left := le_union_left,
le_sup_right := le_union_right,
inf := (β©),
le_inf := @le_inter _ _,
inf_le_left := inter_le_left,
inf_le_right := inter_le_right,
..@multiset.partial_order Ξ± }
@[simp] theorem sup_eq_union (s t : multiset Ξ±) : s β t = s βͺ t := rfl
@[simp] theorem inf_eq_inter (s t : multiset Ξ±) : s β t = s β© t := rfl
@[simp] theorem le_inter_iff : s β€ t β© u β s β€ t β§ s β€ u := le_inf_iff
@[simp] theorem union_le_iff : s βͺ t β€ u β s β€ u β§ t β€ u := sup_le_iff
instance : semilattice_inf_bot (multiset Ξ±) :=
{ bot := 0, bot_le := zero_le, ..multiset.lattice }
theorem union_comm (s t : multiset Ξ±) : s βͺ t = t βͺ s := sup_comm
theorem inter_comm (s t : multiset Ξ±) : s β© t = t β© s := inf_comm
theorem eq_union_right (h : s β€ t) : s βͺ t = t :=
by rw [union_comm, eq_union_left h]
theorem union_le_union_left (h : s β€ t) (u) : u βͺ s β€ u βͺ t :=
sup_le_sup_left h _
theorem union_le_add (s t : multiset Ξ±) : s βͺ t β€ s + t :=
union_le (le_add_right _ _) (le_add_left _ _)
theorem union_add_distrib (s t u : multiset Ξ±) : (s βͺ t) + u = (s + u) βͺ (t + u) :=
by simpa [(βͺ), union, eq_comm, add_assoc] using show s + u - (t + u) = s - t,
by rw [add_comm t, tsub_add_eq_tsub_tsub, add_tsub_cancel_right]
theorem add_union_distrib (s t u : multiset Ξ±) : s + (t βͺ u) = (s + t) βͺ (s + u) :=
by rw [add_comm, union_add_distrib, add_comm s, add_comm s]
theorem cons_union_distrib (a : Ξ±) (s t : multiset Ξ±) : a ::β (s βͺ t) = (a ::β s) βͺ (a ::β t) :=
by simpa using add_union_distrib (a ::β 0) s t
theorem inter_add_distrib (s t u : multiset Ξ±) : (s β© t) + u = (s + u) β© (t + u) :=
begin
by_contra h,
cases lt_iff_cons_le.1 (lt_of_le_of_ne (le_inter
(add_le_add_right (inter_le_left s t) u)
(add_le_add_right (inter_le_right s t) u)) h) with a hl,
rw β cons_add at hl,
exact not_le_of_lt (lt_cons_self (s β© t) a) (le_inter
(le_of_add_le_add_right (le_trans hl (inter_le_left _ _)))
(le_of_add_le_add_right (le_trans hl (inter_le_right _ _))))
end
theorem add_inter_distrib (s t u : multiset Ξ±) : s + (t β© u) = (s + t) β© (s + u) :=
by rw [add_comm, inter_add_distrib, add_comm s, add_comm s]
theorem cons_inter_distrib (a : Ξ±) (s t : multiset Ξ±) : a ::β (s β© t) = (a ::β s) β© (a ::β t) :=
by simp
theorem union_add_inter (s t : multiset Ξ±) : s βͺ t + s β© t = s + t :=
begin
apply le_antisymm,
{ rw union_add_distrib,
refine union_le (add_le_add_left (inter_le_right _ _) _) _,
rw add_comm, exact add_le_add_right (inter_le_left _ _) _ },
{ rw [add_comm, add_inter_distrib],
refine le_inter (add_le_add_right (le_union_right _ _) _) _,
rw add_comm, exact add_le_add_right (le_union_left _ _) _ }
end
theorem sub_add_inter (s t : multiset Ξ±) : s - t + s β© t = s :=
begin
rw [inter_comm],
revert s, refine multiset.induction_on t (by simp) (Ξ» a t IH s, _),
by_cases a β s,
{ rw [cons_inter_of_pos _ h, sub_cons, add_cons, IH, cons_erase h] },
{ rw [cons_inter_of_neg _ h, sub_cons, erase_of_not_mem h, IH] }
end
theorem sub_inter (s t : multiset Ξ±) : s - (s β© t) = s - t :=
add_right_cancel $ by rw [sub_add_inter s t, tsub_add_cancel_of_le (inter_le_left s t)]
end
/-! ### `multiset.filter` -/
section
variables (p : Ξ± β Prop) [decidable_pred p]
/-- `filter p s` returns the elements in `s` (with the same multiplicities)
which satisfy `p`, and removes the rest. -/
def filter (s : multiset Ξ±) : multiset Ξ± :=
quot.lift_on s (Ξ» l, (filter p l : multiset Ξ±))
(Ξ» lβ lβ h, quot.sound $ h.filter p)
@[simp] theorem coe_filter (l : list Ξ±) : filter p (βl) = l.filter p := rfl
@[simp] theorem filter_zero : filter p 0 = 0 := rfl
lemma filter_congr {p q : Ξ± β Prop} [decidable_pred p] [decidable_pred q]
{s : multiset Ξ±} : (β x β s, p x β q x) β filter p s = filter q s :=
quot.induction_on s $ Ξ» l h, congr_arg coe $ filter_congr h
@[simp] theorem filter_add (s t : multiset Ξ±) : filter p (s + t) = filter p s + filter p t :=
quotient.induction_onβ s t $ Ξ» lβ lβ, congr_arg coe $ filter_append _ _
@[simp] theorem filter_le (s : multiset Ξ±) : filter p s β€ s :=
quot.induction_on s $ Ξ» l, (filter_sublist _).subperm
@[simp] theorem filter_subset (s : multiset Ξ±) : filter p s β s :=
subset_of_le $ filter_le _ _
theorem filter_le_filter {s t} (h : s β€ t) : filter p s β€ filter p t :=
le_induction_on h $ Ξ» lβ lβ h, (h.filter p).subperm
lemma monotone_filter_left :
monotone (filter p) :=
Ξ» s t, filter_le_filter p
lemma monotone_filter_right (s : multiset Ξ±) β¦p q : Ξ± β Propβ¦
[decidable_pred p] [decidable_pred q] (h : p β€ q) :
s.filter p β€ s.filter q :=
quotient.induction_on s (Ξ» l, (l.monotone_filter_right h).subperm)
variable {p}
@[simp] theorem filter_cons_of_pos {a : Ξ±} (s) : p a β filter p (a ::β s) = a ::β filter p s :=
quot.induction_on s $ Ξ» l h, congr_arg coe $ filter_cons_of_pos l h
@[simp] theorem filter_cons_of_neg {a : Ξ±} (s) : Β¬ p a β filter p (a ::β s) = filter p s :=
quot.induction_on s $ Ξ» l h, @congr_arg _ _ _ _ coe $ filter_cons_of_neg l h
@[simp] theorem mem_filter {a : Ξ±} {s} : a β filter p s β a β s β§ p a :=
quot.induction_on s $ Ξ» l, mem_filter
theorem of_mem_filter {a : Ξ±} {s} (h : a β filter p s) : p a :=
(mem_filter.1 h).2
theorem mem_of_mem_filter {a : Ξ±} {s} (h : a β filter p s) : a β s :=
(mem_filter.1 h).1
theorem mem_filter_of_mem {a : Ξ±} {l} (m : a β l) (h : p a) : a β filter p l :=
mem_filter.2 β¨m, hβ©
theorem filter_eq_self {s} : filter p s = s β β a β s, p a :=
quot.induction_on s $ Ξ» l, iff.trans β¨Ξ» h,
eq_of_sublist_of_length_eq (filter_sublist _) (@congr_arg _ _ _ _ card h),
congr_arg coeβ© filter_eq_self
theorem filter_eq_nil {s} : filter p s = 0 β β a β s, Β¬p a :=
quot.induction_on s $ Ξ» l, iff.trans β¨Ξ» h,
eq_nil_of_length_eq_zero (@congr_arg _ _ _ _ card h),
congr_arg coeβ© filter_eq_nil
theorem le_filter {s t} : s β€ filter p t β s β€ t β§ β a β s, p a :=
β¨Ξ» h, β¨le_trans h (filter_le _ _), Ξ» a m, of_mem_filter (mem_of_le h m)β©,
Ξ» β¨h, alβ©, filter_eq_self.2 al βΈ filter_le_filter p hβ©
theorem filter_cons {a : Ξ±} (s : multiset Ξ±) :
filter p (a ::β s) = (if p a then {a} else 0) + filter p s :=
begin
split_ifs with h,
{ rw [filter_cons_of_pos _ h, singleton_add] },
{ rw [filter_cons_of_neg _ h, zero_add] },
end
lemma filter_nsmul (s : multiset Ξ±) (n : β) :
filter p (n β’ s) = n β’ filter p s :=
begin
refine s.induction_on _ _,
{ simp only [filter_zero, nsmul_zero] },
{ intros a ha ih,
rw [nsmul_cons, filter_add, ih, filter_cons, nsmul_add],
congr,
split_ifs with hp;
{ simp only [filter_eq_self, nsmul_zero, filter_eq_nil],
intros b hb,
rwa (mem_singleton.mp (mem_of_mem_nsmul hb)) } }
end
variable (p)
@[simp] theorem filter_sub [decidable_eq Ξ±] (s t : multiset Ξ±) :
filter p (s - t) = filter p s - filter p t :=
begin
revert s, refine multiset.induction_on t (by simp) (Ξ» a t IH s, _),
rw [sub_cons, IH],
by_cases p a,
{ rw [filter_cons_of_pos _ h, sub_cons], congr,
by_cases m : a β s,
{ rw [β cons_inj_right a, β filter_cons_of_pos _ h,
cons_erase (mem_filter_of_mem m h), cons_erase m] },
{ rw [erase_of_not_mem m, erase_of_not_mem (mt mem_of_mem_filter m)] } },
{ rw [filter_cons_of_neg _ h],
by_cases m : a β s,
{ rw [(by rw filter_cons_of_neg _ h : filter p (erase s a) = filter p (a ::β erase s a)),
cons_erase m] },
{ rw [erase_of_not_mem m] } }
end
@[simp] theorem filter_union [decidable_eq Ξ±] (s t : multiset Ξ±) :
filter p (s βͺ t) = filter p s βͺ filter p t :=
by simp [(βͺ), union]
@[simp] theorem filter_inter [decidable_eq Ξ±] (s t : multiset Ξ±) :
filter p (s β© t) = filter p s β© filter p t :=
le_antisymm (le_inter
(filter_le_filter _ $ inter_le_left _ _)
(filter_le_filter _ $ inter_le_right _ _)) $ le_filter.2
β¨inf_le_inf (filter_le _ _) (filter_le _ _),
Ξ» a h, of_mem_filter (mem_of_le (inter_le_left _ _) h)β©
@[simp] theorem filter_filter (q) [decidable_pred q] (s : multiset Ξ±) :
filter p (filter q s) = filter (Ξ» a, p a β§ q a) s :=
quot.induction_on s $ Ξ» l, congr_arg coe $ filter_filter p q l
theorem filter_add_filter (q) [decidable_pred q] (s : multiset Ξ±) :
filter p s + filter q s = filter (Ξ» a, p a β¨ q a) s + filter (Ξ» a, p a β§ q a) s :=
multiset.induction_on s rfl $ Ξ» a s IH,
by by_cases p a; by_cases q a; simp *
theorem filter_add_not (s : multiset Ξ±) :
filter p s + filter (Ξ» a, Β¬ p a) s = s :=
by rw [filter_add_filter, filter_eq_self.2, filter_eq_nil.2]; simp [decidable.em]
theorem map_filter (f : Ξ² β Ξ±) (s : multiset Ξ²) :
filter p (map f s) = map f (filter (p β f) s) :=
quot.induction_on s (Ξ» l, by simp [map_filter])
/-! ### Simultaneously filter and map elements of a multiset -/
/-- `filter_map f s` is a combination filter/map operation on `s`.
The function `f : Ξ± β option Ξ²` is applied to each element of `s`;
if `f a` is `some b` then `b` is added to the result, otherwise
`a` is removed from the resulting multiset. -/
def filter_map (f : Ξ± β option Ξ²) (s : multiset Ξ±) : multiset Ξ² :=
quot.lift_on s (Ξ» l, (filter_map f l : multiset Ξ²))
(Ξ» lβ lβ h, quot.sound $ h.filter_map f)
@[simp] theorem coe_filter_map (f : Ξ± β option Ξ²) (l : list Ξ±) :
filter_map f l = l.filter_map f := rfl
@[simp] theorem filter_map_zero (f : Ξ± β option Ξ²) : filter_map f 0 = 0 := rfl
@[simp] theorem filter_map_cons_none {f : Ξ± β option Ξ²} (a : Ξ±) (s : multiset Ξ±) (h : f a = none) :
filter_map f (a ::β s) = filter_map f s :=
quot.induction_on s $ Ξ» l, @congr_arg _ _ _ _ coe $ filter_map_cons_none a l h
@[simp] theorem filter_map_cons_some (f : Ξ± β option Ξ²)
(a : Ξ±) (s : multiset Ξ±) {b : Ξ²} (h : f a = some b) :
filter_map f (a ::β s) = b ::β filter_map f s :=
quot.induction_on s $ Ξ» l, @congr_arg _ _ _ _ coe $ filter_map_cons_some f a l h
theorem filter_map_eq_map (f : Ξ± β Ξ²) : filter_map (some β f) = map f :=
funext $ Ξ» s, quot.induction_on s $ Ξ» l,
@congr_arg _ _ _ _ coe $ congr_fun (filter_map_eq_map f) l
theorem filter_map_eq_filter : filter_map (option.guard p) = filter p :=
funext $ Ξ» s, quot.induction_on s $ Ξ» l,
@congr_arg _ _ _ _ coe $ congr_fun (filter_map_eq_filter p) l
theorem filter_map_filter_map (f : Ξ± β option Ξ²) (g : Ξ² β option Ξ³) (s : multiset Ξ±) :
filter_map g (filter_map f s) = filter_map (Ξ» x, (f x).bind g) s :=
quot.induction_on s $ Ξ» l, congr_arg coe $ filter_map_filter_map f g l
theorem map_filter_map (f : Ξ± β option Ξ²) (g : Ξ² β Ξ³) (s : multiset Ξ±) :
map g (filter_map f s) = filter_map (Ξ» x, (f x).map g) s :=
quot.induction_on s $ Ξ» l, congr_arg coe $ map_filter_map f g l
theorem filter_map_map (f : Ξ± β Ξ²) (g : Ξ² β option Ξ³) (s : multiset Ξ±) :
filter_map g (map f s) = filter_map (g β f) s :=
quot.induction_on s $ Ξ» l, congr_arg coe $ filter_map_map f g l
theorem filter_filter_map (f : Ξ± β option Ξ²) (p : Ξ² β Prop) [decidable_pred p] (s : multiset Ξ±) :
filter p (filter_map f s) = filter_map (Ξ» x, (f x).filter p) s :=
quot.induction_on s $ Ξ» l, congr_arg coe $ filter_filter_map f p l
theorem filter_map_filter (f : Ξ± β option Ξ²) (s : multiset Ξ±) :
filter_map f (filter p s) = filter_map (Ξ» x, if p x then f x else none) s :=
quot.induction_on s $ Ξ» l, congr_arg coe $ filter_map_filter p f l
@[simp] theorem filter_map_some (s : multiset Ξ±) : filter_map some s = s :=
quot.induction_on s $ Ξ» l, congr_arg coe $ filter_map_some l
@[simp] theorem mem_filter_map (f : Ξ± β option Ξ²) (s : multiset Ξ±) {b : Ξ²} :
b β filter_map f s β β a, a β s β§ f a = some b :=
quot.induction_on s $ Ξ» l, mem_filter_map f l
theorem map_filter_map_of_inv (f : Ξ± β option Ξ²) (g : Ξ² β Ξ±)
(H : β x : Ξ±, (f x).map g = some x) (s : multiset Ξ±) :
map g (filter_map f s) = s :=
quot.induction_on s $ Ξ» l, congr_arg coe $ map_filter_map_of_inv f g H l
theorem filter_map_le_filter_map (f : Ξ± β option Ξ²) {s t : multiset Ξ±}
(h : s β€ t) : filter_map f s β€ filter_map f t :=
le_induction_on h $ Ξ» lβ lβ h, (h.filter_map _).subperm
/-! ### countp -/
/-- `countp p s` counts the number of elements of `s` (with multiplicity) that
satisfy `p`. -/
def countp (s : multiset Ξ±) : β :=
quot.lift_on s (countp p) (Ξ» lβ lβ, perm.countp_eq p)
@[simp] theorem coe_countp (l : list Ξ±) : countp p l = l.countp p := rfl
@[simp] theorem countp_zero : countp p 0 = 0 := rfl
variable {p}
@[simp] theorem countp_cons_of_pos {a : Ξ±} (s) : p a β countp p (a ::β s) = countp p s + 1 :=
quot.induction_on s $ countp_cons_of_pos p
@[simp] theorem countp_cons_of_neg {a : Ξ±} (s) : Β¬ p a β countp p (a ::β s) = countp p s :=
quot.induction_on s $ countp_cons_of_neg p
variable (p)
theorem countp_cons (b : Ξ±) (s) : countp p (b ::β s) = countp p s + (if p b then 1 else 0) :=
begin
split_ifs with h;
simp only [h, multiset.countp_cons_of_pos, add_zero, multiset.countp_cons_of_neg, not_false_iff],
end
theorem countp_eq_card_filter (s) : countp p s = card (filter p s) :=
quot.induction_on s $ Ξ» l, countp_eq_length_filter _ _
@[simp] theorem countp_add (s t) : countp p (s + t) = countp p s + countp p t :=
by simp [countp_eq_card_filter]
/-- `countp p`, the number of elements of a multiset satisfying `p`, promoted to an
`add_monoid_hom`. -/
def countp_add_monoid_hom : multiset Ξ± β+ β :=
{ to_fun := countp p,
map_zero' := countp_zero _,
map_add' := countp_add _ }
@[simp] lemma coe_countp_add_monoid_hom :
(countp_add_monoid_hom p : multiset Ξ± β β) = countp p := rfl
@[simp] theorem countp_sub [decidable_eq Ξ±] {s t : multiset Ξ±} (h : t β€ s) :
countp p (s - t) = countp p s - countp p t :=
by simp [countp_eq_card_filter, h, filter_le_filter]
theorem countp_le_of_le {s t} (h : s β€ t) : countp p s β€ countp p t :=
by simpa [countp_eq_card_filter] using card_le_of_le (filter_le_filter p h)
@[simp] theorem countp_filter (q) [decidable_pred q] (s : multiset Ξ±) :
countp p (filter q s) = countp (Ξ» a, p a β§ q a) s :=
by simp [countp_eq_card_filter]
theorem countp_map (f : Ξ± β Ξ²) (s : multiset Ξ±) (p : Ξ² β Prop) [decidable_pred p] :
countp p (map f s) = (s.filter (Ξ» a, p (f a))).card :=
begin
refine multiset.induction_on s _ (Ξ» a t IH, _),
{ rw [map_zero, countp_zero, filter_zero, card_zero] },
{ rw [map_cons, countp_cons, IH, filter_cons, card_add, apply_ite card, card_zero,
card_singleton, add_comm] },
end
variable {p}
theorem countp_pos {s} : 0 < countp p s β β a β s, p a :=
by simp [countp_eq_card_filter, card_pos_iff_exists_mem]
theorem countp_pos_of_mem {s a} (h : a β s) (pa : p a) : 0 < countp p s :=
countp_pos.2 β¨_, h, paβ©
end
/-! ### Multiplicity of an element -/
section
variable [decidable_eq Ξ±]
/-- `count a s` is the multiplicity of `a` in `s`. -/
def count (a : Ξ±) : multiset Ξ± β β := countp (eq a)
@[simp] theorem coe_count (a : Ξ±) (l : list Ξ±) : count a (βl) = l.count a := coe_countp _ _
@[simp] theorem count_zero (a : Ξ±) : count a 0 = 0 := rfl
@[simp] theorem count_cons_self (a : Ξ±) (s : multiset Ξ±) : count a (a ::β s) = succ (count a s) :=
countp_cons_of_pos _ rfl
@[simp, priority 990]
theorem count_cons_of_ne {a b : Ξ±} (h : a β b) (s : multiset Ξ±) : count a (b ::β s) = count a s :=
countp_cons_of_neg _ h
theorem count_le_of_le (a : Ξ±) {s t} : s β€ t β count a s β€ count a t :=
countp_le_of_le _
theorem count_le_count_cons (a b : Ξ±) (s : multiset Ξ±) : count a s β€ count a (b ::β s) :=
count_le_of_le _ (le_cons_self _ _)
theorem count_cons (a b : Ξ±) (s : multiset Ξ±) :
count a (b ::β s) = count a s + (if a = b then 1 else 0) :=
by by_cases h : a = b; simp [h]
theorem count_singleton_self (a : Ξ±) : count a ({a} : multiset Ξ±) = 1 :=
by simp only [count_cons_self, singleton_eq_cons, eq_self_iff_true, count_zero]
theorem count_singleton (a b : Ξ±) : count a ({b} : multiset Ξ±) = if a = b then 1 else 0 :=
by simp only [count_cons, singleton_eq_cons, count_zero, zero_add]
@[simp] theorem count_add (a : Ξ±) : β s t, count a (s + t) = count a s + count a t :=
countp_add _
/-- `count a`, the multiplicity of `a` in a multiset, promoted to an `add_monoid_hom`. -/
def count_add_monoid_hom (a : Ξ±) : multiset Ξ± β+ β := countp_add_monoid_hom (eq a)
@[simp] lemma coe_count_add_monoid_hom {a : Ξ±} :
(count_add_monoid_hom a : multiset Ξ± β β) = count a := rfl
@[simp] theorem count_nsmul (a : Ξ±) (n s) : count a (n β’ s) = n * count a s :=
by induction n; simp [*, succ_nsmul', succ_mul, zero_nsmul]
theorem count_pos {a : Ξ±} {s : multiset Ξ±} : 0 < count a s β a β s :=
by simp [count, countp_pos]
@[simp, priority 980]
theorem count_eq_zero_of_not_mem {a : Ξ±} {s : multiset Ξ±} (h : a β s) : count a s = 0 :=
by_contradiction $ Ξ» h', h $ count_pos.1 (nat.pos_of_ne_zero h')
@[simp] theorem count_eq_zero {a : Ξ±} {s : multiset Ξ±} : count a s = 0 β a β s :=
iff_not_comm.1 $ count_pos.symm.trans pos_iff_ne_zero
theorem count_ne_zero {a : Ξ±} {s : multiset Ξ±} : count a s β 0 β a β s :=
by simp [ne.def, count_eq_zero]
@[simp] theorem count_repeat_self (a : Ξ±) (n : β) : count a (repeat a n) = n :=
by simp [repeat]
theorem count_repeat (a b : Ξ±) (n : β) :
count a (repeat b n) = if (a = b) then n else 0 :=
begin
split_ifs with hβ,
{ rw [hβ, count_repeat_self] },
{ rw [count_eq_zero],
apply mt eq_of_mem_repeat hβ },
end
@[simp] theorem count_erase_self (a : Ξ±) (s : multiset Ξ±) :
count a (erase s a) = pred (count a s) :=
begin
by_cases a β s,
{ rw [(by rw cons_erase h : count a s = count a (a ::β erase s a)),
count_cons_self]; refl },
{ rw [erase_of_not_mem h, count_eq_zero.2 h]; refl }
end
@[simp, priority 980] theorem count_erase_of_ne {a b : Ξ±} (ab : a β b) (s : multiset Ξ±) :
count a (erase s b) = count a s :=
begin
by_cases b β s,
{ rw [β count_cons_of_ne ab, cons_erase h] },
{ rw [erase_of_not_mem h] }
end
@[simp] theorem count_sub (a : Ξ±) (s t : multiset Ξ±) : count a (s - t) = count a s - count a t :=
begin
revert s, refine multiset.induction_on t (by simp) (Ξ» b t IH s, _),
rw [sub_cons, IH],
by_cases ab : a = b,
{ subst b, rw [count_erase_self, count_cons_self, sub_succ, pred_sub] },
{ rw [count_erase_of_ne ab, count_cons_of_ne ab] }
end
@[simp] theorem count_union (a : Ξ±) (s t : multiset Ξ±) :
count a (s βͺ t) = max (count a s) (count a t) :=
by simp [(βͺ), union, tsub_add_eq_max, -add_comm]
@[simp] theorem count_inter (a : Ξ±) (s t : multiset Ξ±) :
count a (s β© t) = min (count a s) (count a t) :=
begin
apply @nat.add_left_cancel (count a (s - t)),
rw [β count_add, sub_add_inter, count_sub, tsub_add_min],
end
lemma count_sum {m : multiset Ξ²} {f : Ξ² β multiset Ξ±} {a : Ξ±} :
count a (map f m).sum = sum (m.map $ Ξ»b, count a $ f b) :=
multiset.induction_on m (by simp) ( by simp)
lemma count_bind {m : multiset Ξ²} {f : Ξ² β multiset Ξ±} {a : Ξ±} :
count a (bind m f) = sum (m.map $ Ξ»b, count a $ f b) := count_sum
theorem le_count_iff_repeat_le {a : Ξ±} {s : multiset Ξ±} {n : β} : n β€ count a s β repeat a n β€ s :=
quot.induction_on s $ Ξ» l, le_count_iff_repeat_sublist.trans repeat_le_coe.symm
@[simp] theorem count_filter_of_pos {p} [decidable_pred p]
{a} {s : multiset Ξ±} (h : p a) : count a (filter p s) = count a s :=
quot.induction_on s $ Ξ» l, count_filter h
@[simp] theorem count_filter_of_neg {p} [decidable_pred p]
{a} {s : multiset Ξ±} (h : Β¬ p a) : count a (filter p s) = 0 :=
multiset.count_eq_zero_of_not_mem (Ξ» t, h (of_mem_filter t))
theorem ext {s t : multiset Ξ±} : s = t β β a, count a s = count a t :=
quotient.induction_onβ s t $ Ξ» lβ lβ, quotient.eq.trans perm_iff_count
@[ext]
theorem ext' {s t : multiset Ξ±} : (β a, count a s = count a t) β s = t :=
ext.2
@[simp] theorem coe_inter (s t : list Ξ±) : (s β© t : multiset Ξ±) = (s.bag_inter t : list Ξ±) :=
by ext; simp
theorem le_iff_count {s t : multiset Ξ±} : s β€ t β β a, count a s β€ count a t :=
β¨Ξ» h a, count_le_of_le a h, Ξ» al,
by rw β (ext.2 (Ξ» a, by simp [max_eq_right (al a)]) : s βͺ t = t);
apply le_union_leftβ©
instance : distrib_lattice (multiset Ξ±) :=
{ le_sup_inf := Ξ» s t u, le_of_eq $ eq.symm $
ext.2 $ Ξ» a, by simp only [max_min_distrib_left,
multiset.count_inter, multiset.sup_eq_union, multiset.count_union, multiset.inf_eq_inter],
..multiset.lattice }
instance : semilattice_sup_bot (multiset Ξ±) :=
{ bot := 0,
bot_le := zero_le,
..multiset.lattice }
theorem repeat_inf (s : multiset Ξ±) (a : Ξ±) (n : β) :
(repeat a n) β s = repeat a (min (s.count a) n) :=
begin
ext x,
rw [inf_eq_inter, count_inter, count_repeat, count_repeat],
by_cases x = a,
simp only [min_comm, h, if_true, eq_self_iff_true],
simp only [h, if_false, zero_min],
end
/-- `multiset.map f` preserves `count` if `f` is injective on the set of elements contained in
the multiset -/
theorem count_map_eq_count [decidable_eq Ξ²] (f : Ξ± β Ξ²) (s : multiset Ξ±)
(hf : set.inj_on f {x : Ξ± | x β s}) (x β s) : (s.map f).count (f x) = s.count x :=
begin
suffices : (filter (Ξ» (a : Ξ±), f x = f a) s).count x = card (filter (Ξ» (a : Ξ±), f x = f a) s),
{ rw [count, countp_map, β this],
exact count_filter_of_pos rfl },
{ rw eq_repeat.2 β¨rfl, Ξ» b hb, eq_comm.1 ((hf H (mem_filter.1 hb).left) (mem_filter.1 hb).right)β©,
simp only [count_repeat, eq_self_iff_true, if_true, card_repeat]},
end
end
/-! ### Lift a relation to `multiset`s -/
section rel
/-- `rel r s t` -- lift the relation `r` between two elements to a relation between `s` and `t`,
s.t. there is a one-to-one mapping betweem elements in `s` and `t` following `r`. -/
@[mk_iff] inductive rel (r : Ξ± β Ξ² β Prop) : multiset Ξ± β multiset Ξ² β Prop
| zero : rel 0 0
| cons {a b as bs} : r a b β rel as bs β rel (a ::β as) (b ::β bs)
variables {Ξ΄ : Type*} {r : Ξ± β Ξ² β Prop} {p : Ξ³ β Ξ΄ β Prop}
private lemma rel_flip_aux {s t} (h : rel r s t) : rel (flip r) t s :=
rel.rec_on h rel.zero (assume _ _ _ _ hβ hβ ih, rel.cons hβ ih)
lemma rel_flip {s t} : rel (flip r) s t β rel r t s :=
β¨rel_flip_aux, rel_flip_auxβ©
lemma rel_refl_of_refl_on {m : multiset Ξ±} {r : Ξ± β Ξ± β Prop} :
(β x β m, r x x) β rel r m m :=
begin
apply m.induction_on,
{ intros, apply rel.zero },
{ intros a m ih h,
exact rel.cons (h _ (mem_cons_self _ _)) (ih (Ξ» _ ha, h _ (mem_cons_of_mem ha))) }
end
lemma rel_eq_refl {s : multiset Ξ±} : rel (=) s s :=
rel_refl_of_refl_on (Ξ» x hx, rfl)
lemma rel_eq {s t : multiset Ξ±} : rel (=) s t β s = t :=
begin
split,
{ assume h, induction h; simp * },
{ assume h, subst h, exact rel_eq_refl }
end
lemma rel.mono {r p : Ξ± β Ξ² β Prop} {s t} (hst : rel r s t) (h : β(a β s) (b β t), r a b β p a b) :
rel p s t :=
begin
induction hst,
case rel.zero { exact rel.zero },
case rel.cons : a b s t hab hst ih {
apply rel.cons (h a (mem_cons_self _ _) b (mem_cons_self _ _) hab),
exact ih (Ξ» a' ha' b' hb' h', h a' (mem_cons_of_mem ha') b' (mem_cons_of_mem hb') h') }
end
lemma rel.add {s t u v} (hst : rel r s t) (huv : rel r u v) : rel r (s + u) (t + v) :=
begin
induction hst,
case rel.zero { simpa using huv },
case rel.cons : a b s t hab hst ih { simpa using ih.cons hab }
end
lemma rel_flip_eq {s t : multiset Ξ±} : rel (Ξ»a b, b = a) s t β s = t :=
show rel (flip (=)) s t β s = t, by rw [rel_flip, rel_eq, eq_comm]
@[simp] lemma rel_zero_left {b : multiset Ξ²} : rel r 0 b β b = 0 :=
by rw [rel_iff]; simp
@[simp] lemma rel_zero_right {a : multiset Ξ±} : rel r a 0 β a = 0 :=
by rw [rel_iff]; simp
lemma rel_cons_left {a as bs} :
rel r (a ::β as) bs β (βb bs', r a b β§ rel r as bs' β§ bs = b ::β bs') :=
begin
split,
{ generalize hm : a ::β as = m,
assume h,
induction h generalizing as,
case rel.zero { simp at hm, contradiction },
case rel.cons : a' b as' bs ha'b h ih
{ rcases cons_eq_cons.1 hm with β¨eqβ, eqββ© | β¨h, cs, eqβ, eqββ©,
{ subst eqβ, subst eqβ, exact β¨b, bs, ha'b, h, rflβ© },
{ rcases ih eqβ.symm with β¨b', bs', hβ, hβ, eqβ©,
exact β¨b', b ::β bs', hβ, eqβ.symm βΈ rel.cons ha'b hβ, eq.symm βΈ cons_swap _ _ _β© } } },
{ exact assume β¨b, bs', hab, h, eqβ©, eq.symm βΈ rel.cons hab h }
end
lemma rel_cons_right {as b bs} :
rel r as (b ::β bs) β (βa as', r a b β§ rel r as' bs β§ as = a ::β as') :=
begin
rw [β rel_flip, rel_cons_left],
apply exists_congr, assume a,
apply exists_congr, assume as',
rw [rel_flip, flip]
end
lemma rel_add_left {asβ asβ} :
β{bs}, rel r (asβ + asβ) bs β (βbsβ bsβ, rel r asβ bsβ β§ rel r asβ bsβ β§ bs = bsβ + bsβ) :=
multiset.induction_on asβ (by simp)
begin
assume a s ih bs,
simp only [ih, cons_add, rel_cons_left],
split,
{ assume h,
rcases h with β¨b, bs', hab, h, rflβ©,
rcases h with β¨bsβ, bsβ, hβ, hβ, rflβ©,
exact β¨b ::β bsβ, bsβ, β¨b, bsβ, hab, hβ, rflβ©, hβ, by simpβ© },
{ assume h,
rcases h with β¨bsβ, bsβ, h, hβ, rflβ©,
rcases h with β¨b, bs, hab, hβ, rflβ©,
exact β¨b, bs + bsβ, hab, β¨bs, bsβ, hβ, hβ, rflβ©, by simpβ© }
end
lemma rel_add_right {as bsβ bsβ} :
rel r as (bsβ + bsβ) β (βasβ asβ, rel r asβ bsβ β§ rel r asβ bsβ β§ as = asβ + asβ) :=
by rw [β rel_flip, rel_add_left]; simp [rel_flip]
lemma rel_map_left {s : multiset Ξ³} {f : Ξ³ β Ξ±} :
β{t}, rel r (s.map f) t β rel (Ξ»a b, r (f a) b) s t :=
multiset.induction_on s (by simp) (by simp [rel_cons_left] {contextual := tt})
lemma rel_map_right {s : multiset Ξ±} {t : multiset Ξ³} {f : Ξ³ β Ξ²} :
rel r s (t.map f) β rel (Ξ»a b, r a (f b)) s t :=
by rw [β rel_flip, rel_map_left, β rel_flip]; refl
lemma rel_join {s t} (h : rel (rel r) s t) : rel r s.join t.join :=
begin
induction h,
case rel.zero { simp },
case rel.cons : a b s t hab hst ih { simpa using hab.add ih }
end
lemma rel_map {s : multiset Ξ±} {t : multiset Ξ²} {f : Ξ± β Ξ³} {g : Ξ² β Ξ΄} :
rel p (s.map f) (t.map g) β rel (Ξ»a b, p (f a) (g b)) s t :=
rel_map_left.trans rel_map_right
lemma rel_bind {p : Ξ³ β Ξ΄ β Prop} {s t} {f : Ξ± β multiset Ξ³} {g : Ξ² β multiset Ξ΄}
(h : (r β rel p) f g) (hst : rel r s t) :
rel p (s.bind f) (t.bind g) :=
by { apply rel_join, rw rel_map, exact hst.mono (Ξ» a ha b hb hr, h hr) }
lemma card_eq_card_of_rel {r : Ξ± β Ξ² β Prop} {s : multiset Ξ±} {t : multiset Ξ²} (h : rel r s t) :
card s = card t :=
by induction h; simp [*]
lemma exists_mem_of_rel_of_mem {r : Ξ± β Ξ² β Prop} {s : multiset Ξ±} {t : multiset Ξ²}
(h : rel r s t) :
β {a : Ξ±} (ha : a β s), β b β t, r a b :=
begin
induction h with x y s t hxy hst ih,
{ simp },
{ assume a ha,
cases mem_cons.1 ha with ha ha,
{ exact β¨y, mem_cons_self _ _, ha.symm βΈ hxyβ© },
{ rcases ih ha with β¨b, hbt, habβ©,
exact β¨b, mem_cons.2 (or.inr hbt), habβ© } }
end
lemma rel_of_forall {m1 m2 : multiset Ξ±} {r : Ξ± β Ξ± β Prop} (h : β a b, a β m1 β b β m2 β r a b)
(hc : card m1 = card m2) :
m1.rel r m2 :=
begin
revert m1,
apply m2.induction_on,
{ intros m h hc,
rw [rel_zero_right, β card_eq_zero, hc, card_zero] },
{ intros a t ih m h hc,
rw card_cons at hc,
obtain β¨b, hbβ© := card_pos_iff_exists_mem.1 (show 0 < card m, from hc.symm βΈ (nat.succ_pos _)),
obtain β¨m', rflβ© := exists_cons_of_mem hb,
refine rel_cons_right.mpr β¨b, m', h _ _ hb (mem_cons_self _ _), ih _ _, rflβ©,
{ exact Ξ» _ _ ha hb, h _ _ (mem_cons_of_mem ha) (mem_cons_of_mem hb) },
{ simpa using hc } }
end
lemma rel_repeat_left {m : multiset Ξ±} {a : Ξ±} {r : Ξ± β Ξ± β Prop} {n : β} :
(repeat a n).rel r m β m.card = n β§ β x, x β m β r a x :=
β¨Ξ» h, β¨(card_eq_card_of_rel h).symm.trans (card_repeat _ _), Ξ» x hx, begin
obtain β¨b, hb1, hb2β© := exists_mem_of_rel_of_mem (rel_flip.2 h) hx,
rwa eq_of_mem_repeat hb1 at hb2,
endβ©,
Ξ» h, rel_of_forall (Ξ» x y hx hy, (eq_of_mem_repeat hx).symm βΈ (h.2 _ hy))
(eq.trans (card_repeat _ _) h.1.symm)β©
lemma rel_repeat_right {m : multiset Ξ±} {a : Ξ±} {r : Ξ± β Ξ± β Prop} {n : β} :
m.rel r (repeat a n) β m.card = n β§ β x, x β m β r x a :=
by { rw [β rel_flip], exact rel_repeat_left }
lemma sum_le_sum_of_rel_le [ordered_add_comm_monoid Ξ±]
{m1 m2 : multiset Ξ±} (h : m1.rel (β€) m2) : m1.sum β€ m2.sum :=
begin
induction h with _ _ _ _ rh _ rt,
{ refl },
{ rw [sum_cons, sum_cons],
exact add_le_add rh rt }
end
end rel
section sum_inequalities
lemma le_sum_of_mem [canonically_ordered_add_monoid Ξ±] {m : multiset Ξ±} {a : Ξ±}
(h : a β m) : a β€ m.sum :=
begin
obtain β¨m', rflβ© := exists_cons_of_mem h,
rw [sum_cons],
exact _root_.le_add_right (le_refl a),
end
variables [ordered_add_comm_monoid Ξ±]
lemma sum_map_le_sum
{m : multiset Ξ±} (f : Ξ± β Ξ±) (h : β x, x β m β f x β€ x) : (m.map f).sum β€ m.sum :=
sum_le_sum_of_rel_le (rel_map_left.2 (rel_refl_of_refl_on h))
lemma sum_le_sum_map
{m : multiset Ξ±} (f : Ξ± β Ξ±) (h : β x, x β m β x β€ f x) : m.sum β€ (m.map f).sum :=
@sum_map_le_sum (order_dual Ξ±) _ _ f h
lemma card_nsmul_le_sum {b : Ξ±}
{m : multiset Ξ±} (h : β x, x β m β b β€ x) : (card m) β’ b β€ m.sum :=
begin
rw [βmultiset.sum_repeat, βmultiset.map_const],
exact sum_map_le_sum _ h,
end
lemma sum_le_card_nsmul {b : Ξ±}
{m : multiset Ξ±} (h : β x, x β m β x β€ b) : m.sum β€ (card m) β’ b :=
begin
rw [βmultiset.sum_repeat, βmultiset.map_const],
exact sum_le_sum_map _ h,
end
end sum_inequalities
section map
theorem map_eq_map {f : Ξ± β Ξ²} (hf : function.injective f) {s t : multiset Ξ±} :
s.map f = t.map f β s = t :=
by { rw [β rel_eq, β rel_eq, rel_map], simp only [hf.eq_iff] }
theorem map_injective {f : Ξ± β Ξ²} (hf : function.injective f) :
function.injective (multiset.map f) :=
assume x y, (map_eq_map hf).1
end map
section quot
theorem map_mk_eq_map_mk_of_rel {r : Ξ± β Ξ± β Prop} {s t : multiset Ξ±} (hst : s.rel r t) :
s.map (quot.mk r) = t.map (quot.mk r) :=
rel.rec_on hst rfl $ assume a b s t hab hst ih, by simp [ih, quot.sound hab]
theorem exists_multiset_eq_map_quot_mk {r : Ξ± β Ξ± β Prop} (s : multiset (quot r)) :
βt:multiset Ξ±, s = t.map (quot.mk r) :=
multiset.induction_on s β¨0, rflβ© $
assume a s β¨t, htβ©, quot.induction_on a $ assume a, ht.symm βΈ β¨a ::β t, (map_cons _ _ _).symmβ©
theorem induction_on_multiset_quot
{r : Ξ± β Ξ± β Prop} {p : multiset (quot r) β Prop} (s : multiset (quot r)) :
(βs:multiset Ξ±, p (s.map (quot.mk r))) β p s :=
match s, exists_multiset_eq_map_quot_mk s with _, β¨t, rflβ© := assume h, h _ end
end quot
/-! ### Disjoint multisets -/
/-- `disjoint s t` means that `s` and `t` have no elements in common. -/
def disjoint (s t : multiset Ξ±) : Prop := β β¦aβ¦, a β s β a β t β false
@[simp] theorem coe_disjoint (lβ lβ : list Ξ±) : @disjoint Ξ± lβ lβ β lβ.disjoint lβ := iff.rfl
theorem disjoint.symm {s t : multiset Ξ±} (d : disjoint s t) : disjoint t s
| a iβ iβ := d iβ iβ
theorem disjoint_comm {s t : multiset Ξ±} : disjoint s t β disjoint t s :=
β¨disjoint.symm, disjoint.symmβ©
theorem disjoint_left {s t : multiset Ξ±} : disjoint s t β β {a}, a β s β a β t := iff.rfl
theorem disjoint_right {s t : multiset Ξ±} : disjoint s t β β {a}, a β t β a β s :=
disjoint_comm
theorem disjoint_iff_ne {s t : multiset Ξ±} : disjoint s t β β a β s, β b β t, a β b :=
by simp [disjoint_left, imp_not_comm]
theorem disjoint_of_subset_left {s t u : multiset Ξ±} (h : s β u) (d : disjoint u t) : disjoint s t
| x mβ := d (h mβ)
theorem disjoint_of_subset_right {s t u : multiset Ξ±} (h : t β u) (d : disjoint s u) : disjoint s t
| x m mβ := d m (h mβ)
theorem disjoint_of_le_left {s t u : multiset Ξ±} (h : s β€ u) : disjoint u t β disjoint s t :=
disjoint_of_subset_left (subset_of_le h)
theorem disjoint_of_le_right {s t u : multiset Ξ±} (h : t β€ u) : disjoint s u β disjoint s t :=
disjoint_of_subset_right (subset_of_le h)
@[simp] theorem zero_disjoint (l : multiset Ξ±) : disjoint 0 l
| a := (not_mem_nil a).elim
@[simp, priority 1100]
theorem singleton_disjoint {l : multiset Ξ±} {a : Ξ±} : disjoint {a} l β a β l :=
by simp [disjoint]; refl
@[simp, priority 1100]
theorem disjoint_singleton {l : multiset Ξ±} {a : Ξ±} : disjoint l {a} β a β l :=
by rw [disjoint_comm, singleton_disjoint]
@[simp] theorem disjoint_add_left {s t u : multiset Ξ±} :
disjoint (s + t) u β disjoint s u β§ disjoint t u :=
by simp [disjoint, or_imp_distrib, forall_and_distrib]
@[simp] theorem disjoint_add_right {s t u : multiset Ξ±} :
disjoint s (t + u) β disjoint s t β§ disjoint s u :=
by rw [disjoint_comm, disjoint_add_left]; tauto
@[simp] theorem disjoint_cons_left {a : Ξ±} {s t : multiset Ξ±} :
disjoint (a ::β s) t β a β t β§ disjoint s t :=
(@disjoint_add_left _ {a} s t).trans $ by rw singleton_disjoint
@[simp] theorem disjoint_cons_right {a : Ξ±} {s t : multiset Ξ±} :
disjoint s (a ::β t) β a β s β§ disjoint s t :=
by rw [disjoint_comm, disjoint_cons_left]; tauto
theorem inter_eq_zero_iff_disjoint [decidable_eq Ξ±] {s t : multiset Ξ±} : s β© t = 0 β disjoint s t :=
by rw β subset_zero; simp [subset_iff, disjoint]
@[simp] theorem disjoint_union_left [decidable_eq Ξ±] {s t u : multiset Ξ±} :
disjoint (s βͺ t) u β disjoint s u β§ disjoint t u :=
by simp [disjoint, or_imp_distrib, forall_and_distrib]
@[simp] theorem disjoint_union_right [decidable_eq Ξ±] {s t u : multiset Ξ±} :
disjoint s (t βͺ u) β disjoint s t β§ disjoint s u :=
by simp [disjoint, or_imp_distrib, forall_and_distrib]
lemma add_eq_union_iff_disjoint [decidable_eq Ξ±] {s t : multiset Ξ±} :
s + t = s βͺ t β disjoint s t :=
by simp_rw [βinter_eq_zero_iff_disjoint, ext, count_add, count_union, count_inter, count_zero,
nat.min_eq_zero_iff, nat.add_eq_max_iff]
lemma disjoint_map_map {f : Ξ± β Ξ³} {g : Ξ² β Ξ³} {s : multiset Ξ±} {t : multiset Ξ²} :
disjoint (s.map f) (t.map g) β (βaβs, βbβt, f a β g b) :=
by { simp [disjoint, @eq_comm _ (f _) (g _)], refl }
/-- `pairwise r m` states that there exists a list of the elements s.t. `r` holds pairwise on this
list. -/
def pairwise (r : Ξ± β Ξ± β Prop) (m : multiset Ξ±) : Prop :=
βl:list Ξ±, m = l β§ l.pairwise r
lemma pairwise_coe_iff_pairwise {r : Ξ± β Ξ± β Prop} (hr : symmetric r) {l : list Ξ±} :
multiset.pairwise r l β l.pairwise r :=
iff.intro
(assume β¨l', eq, hβ©, ((quotient.exact eq).pairwise_iff hr).2 h)
(assume h, β¨l, rfl, hβ©)
end multiset
namespace multiset
section choose
variables (p : Ξ± β Prop) [decidable_pred p] (l : multiset Ξ±)
/-- Given a proof `hp` that there exists a unique `a β l` such that `p a`, `choose_x p l hp` returns
that `a` together with proofs of `a β l` and `p a`. -/
def choose_x : Ξ hp : (β! a, a β l β§ p a), { a // a β l β§ p a } :=
quotient.rec_on l (Ξ» l' ex_unique, list.choose_x p l' (exists_of_exists_unique ex_unique)) begin
intros,
funext hp,
suffices all_equal : β x y : { t // t β b β§ p t }, x = y,
{ apply all_equal },
{ rintros β¨x, pxβ© β¨y, pyβ©,
rcases hp with β¨z, β¨z_mem_l, pzβ©, z_uniqueβ©,
congr,
calc x = z : z_unique x px
... = y : (z_unique y py).symm }
end
/-- Given a proof `hp` that there exists a unique `a β l` such that `p a`, `choose p l hp` returns
that `a`. -/
def choose (hp : β! a, a β l β§ p a) : Ξ± := choose_x p l hp
lemma choose_spec (hp : β! a, a β l β§ p a) : choose p l hp β l β§ p (choose p l hp) :=
(choose_x p l hp).property
lemma choose_mem (hp : β! a, a β l β§ p a) : choose p l hp β l := (choose_spec _ _ _).1
lemma choose_property (hp : β! a, a β l β§ p a) : p (choose p l hp) := (choose_spec _ _ _).2
end choose
variable (Ξ±)
/-- The equivalence between lists and multisets of a subsingleton type. -/
def subsingleton_equiv [subsingleton Ξ±] : list Ξ± β multiset Ξ± :=
{ to_fun := coe,
inv_fun := quot.lift id $ Ξ» (a b : list Ξ±) (h : a ~ b),
list.ext_le h.length_eq $ Ξ» n hβ hβ, subsingleton.elim _ _,
left_inv := Ξ» l, rfl,
right_inv := Ξ» m, quot.induction_on m $ Ξ» l, rfl }
variable {Ξ±}
@[simp]
lemma coe_subsingleton_equiv [subsingleton Ξ±] :
(subsingleton_equiv Ξ± : list Ξ± β multiset Ξ±) = coe :=
rfl
end multiset
@[to_additive]
theorem monoid_hom.map_multiset_prod [comm_monoid Ξ±] [comm_monoid Ξ²] (f : Ξ± β* Ξ²) (s : multiset Ξ±) :
f s.prod = (s.map f).prod :=
(s.prod_hom f).symm
|
43bbf775aafabff40cfbe5001fac8d7ec0ecd07b | 7cef822f3b952965621309e88eadf618da0c8ae9 | /src/data/pfun.lean | 73e516f3831259ac8f8b3f149f40023f22985471 | [
"Apache-2.0"
] | permissive | rmitta/mathlib | 8d90aee30b4db2b013e01f62c33f297d7e64a43d | 883d974b608845bad30ae19e27e33c285200bf84 | refs/heads/master | 1,585,776,832,544 | 1,576,874,096,000 | 1,576,874,096,000 | 153,663,165 | 0 | 2 | Apache-2.0 | 1,544,806,490,000 | 1,539,884,365,000 | Lean | UTF-8 | Lean | false | false | 22,508 | lean | /-
Copyright (c) 2017 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Mario Carneiro, Jeremy Avigad
-/
import data.set.basic data.equiv.basic data.rel logic.relator
/-- `roption Ξ±` is the type of "partial values" of type `Ξ±`. It
is similar to `option Ξ±` except the domain condition can be an
arbitrary proposition, not necessarily decidable. -/
structure {u} roption (Ξ± : Type u) : Type u :=
(dom : Prop)
(get : dom β Ξ±)
namespace roption
variables {Ξ± : Type*} {Ξ² : Type*} {Ξ³ : Type*}
/-- Convert an `roption Ξ±` with a decidable domain to an option -/
def to_option (o : roption Ξ±) [decidable o.dom] : option Ξ± :=
if h : dom o then some (o.get h) else none
/-- `roption` extensionality -/
theorem ext' : β {o p : roption Ξ±}
(H1 : o.dom β p.dom)
(H2 : βhβ hβ, o.get hβ = p.get hβ), o = p
| β¨od, oβ© β¨pd, pβ© H1 H2 := have t : od = pd, from propext H1,
by cases t; rw [show o = p, from funext $ Ξ»p, H2 p p]
/-- `roption` eta expansion -/
@[simp] theorem eta : Ξ (o : roption Ξ±), (β¨o.dom, Ξ» h, o.get hβ© : roption Ξ±) = o
| β¨h, fβ© := rfl
/-- `a β o` means that `o` is defined and equal to `a` -/
protected def mem (a : Ξ±) (o : roption Ξ±) : Prop := β h, o.get h = a
instance : has_mem Ξ± (roption Ξ±) := β¨roption.memβ©
theorem mem_eq (a : Ξ±) (o : roption Ξ±) : (a β o) = (β h, o.get h = a) :=
rfl
theorem dom_iff_mem : β {o : roption Ξ±}, o.dom β βy, y β o
| β¨p, fβ© := β¨Ξ»h, β¨f h, h, rflβ©, Ξ»β¨_, h, rflβ©, hβ©
theorem get_mem {o : roption Ξ±} (h) : get o h β o := β¨_, rflβ©
/-- `roption` extensionality -/
theorem ext {o p : roption Ξ±} (H : β a, a β o β a β p) : o = p :=
ext' β¨Ξ» h, ((H _).1 β¨h, rflβ©).fst,
Ξ» h, ((H _).2 β¨h, rflβ©).fstβ© $
Ξ» a b, ((H _).2 β¨_, rflβ©).snd
/-- The `none` value in `roption` has a `false` domain and an empty function. -/
def none : roption Ξ± := β¨false, false.rec _β©
@[simp] theorem not_mem_none (a : Ξ±) : a β @none Ξ± := Ξ» h, h.fst
/-- The `some a` value in `roption` has a `true` domain and the
function returns `a`. -/
def some (a : Ξ±) : roption Ξ± := β¨true, Ξ»_, aβ©
theorem mem_unique : relator.left_unique ((β) : Ξ± β roption Ξ± β Prop)
| _ β¨p, fβ© _ β¨hβ, rflβ© β¨hβ, rflβ© := rfl
theorem get_eq_of_mem {o : roption Ξ±} {a} (h : a β o) (h') : get o h' = a :=
mem_unique β¨_, rflβ© h
@[simp] theorem get_some {a : Ξ±} (ha : (some a).dom) : get (some a) ha = a := rfl
theorem mem_some (a : Ξ±) : a β some a := β¨trivial, rflβ©
@[simp] theorem mem_some_iff {a b} : b β (some a : roption Ξ±) β b = a :=
β¨Ξ»β¨h, eβ©, e.symm, Ξ» e, β¨trivial, e.symmβ©β©
theorem eq_some_iff {a : Ξ±} {o : roption Ξ±} : o = some a β a β o :=
β¨Ξ» e, e.symm βΈ mem_some _,
Ξ» β¨h, eβ©, e βΈ ext' (iff_true_intro h) (Ξ» _ _, rfl)β©
theorem eq_none_iff {o : roption Ξ±} : o = none β β a, a β o :=
β¨Ξ» e, e.symm βΈ not_mem_none,
Ξ» h, ext (by simpa [not_mem_none])β©
theorem eq_none_iff' {o : roption Ξ±} : o = none β Β¬ o.dom :=
β¨Ξ» e, e.symm βΈ id, Ξ» h, eq_none_iff.2 (Ξ» a h', h h'.fst)β©
lemma some_ne_none (x : Ξ±) : some x β none :=
by { intro h, change none.dom, rw [β h], trivial }
lemma ne_none_iff {o : roption Ξ±} : o β none β βx, o = some x :=
begin
split,
{ rw [ne, eq_none_iff], intro h, push_neg at h, cases h with x hx, use x, rwa [eq_some_iff] },
{ rintro β¨x, rflβ©, apply some_ne_none }
end
@[simp] lemma some_inj {a b : Ξ±} : roption.some a = some b β a = b :=
function.injective.eq_iff (Ξ» a b h, congr_fun (eq_of_heq (roption.mk.inj h).2) trivial)
@[simp] lemma some_get {a : roption Ξ±} (ha : a.dom) :
roption.some (roption.get a ha) = a :=
eq.symm (eq_some_iff.2 β¨ha, rflβ©)
lemma get_eq_iff_eq_some {a : roption Ξ±} {ha : a.dom} {b : Ξ±} :
a.get ha = b β a = some b :=
β¨Ξ» h, by simp [h.symm], Ξ» h, by simp [h]β©
instance none_decidable : decidable (@none Ξ±).dom := decidable.false
instance some_decidable (a : Ξ±) : decidable (some a).dom := decidable.true
def get_or_else (a : roption Ξ±) [decidable a.dom] (d : Ξ±) :=
if ha : a.dom then a.get ha else d
@[simp] lemma get_or_else_none (d : Ξ±) : get_or_else none d = d :=
dif_neg id
@[simp] lemma get_or_else_some (a : Ξ±) (d : Ξ±) : get_or_else (some a) d = a :=
dif_pos trivial
@[simp] theorem mem_to_option {o : roption Ξ±} [decidable o.dom] {a : Ξ±} :
a β to_option o β a β o :=
begin
unfold to_option,
by_cases h : o.dom; simp [h],
{ exact β¨Ξ» h, β¨_, hβ©, Ξ» β¨_, hβ©, hβ© },
{ exact mt Exists.fst h }
end
/-- Convert an `option Ξ±` into an `roption Ξ±` -/
def of_option : option Ξ± β roption Ξ±
| option.none := none
| (option.some a) := some a
@[simp] theorem mem_of_option {a : Ξ±} : β {o : option Ξ±}, a β of_option o β a β o
| option.none := β¨Ξ» h, h.fst.elim, Ξ» h, option.no_confusion hβ©
| (option.some b) := β¨Ξ» h, congr_arg option.some h.snd,
Ξ» h, β¨trivial, option.some.inj hβ©β©
@[simp] theorem of_option_dom {Ξ±} : β (o : option Ξ±), (of_option o).dom β o.is_some
| option.none := by simp [of_option, none]
| (option.some a) := by simp [of_option]
theorem of_option_eq_get {Ξ±} (o : option Ξ±) : of_option o = β¨_, @option.get _ oβ© :=
roption.ext' (of_option_dom o) $ Ξ» hβ hβ, by cases o; [cases hβ, refl]
instance : has_coe (option Ξ±) (roption Ξ±) := β¨of_optionβ©
@[simp] theorem mem_coe {a : Ξ±} {o : option Ξ±} :
a β (o : roption Ξ±) β a β o := mem_of_option
@[simp] theorem coe_none : (@option.none Ξ± : roption Ξ±) = none := rfl
@[simp] theorem coe_some (a : Ξ±) : (option.some a : roption Ξ±) = some a := rfl
@[elab_as_eliminator] protected lemma induction_on {P : roption Ξ± β Prop}
(a : roption Ξ±) (hnone : P none) (hsome : β a : Ξ±, P (some a)) : P a :=
(classical.em a.dom).elim
(Ξ» h, roption.some_get h βΈ hsome _)
(Ξ» h, (eq_none_iff'.2 h).symm βΈ hnone)
instance of_option_decidable : β o : option Ξ±, decidable (of_option o).dom
| option.none := roption.none_decidable
| (option.some a) := roption.some_decidable a
@[simp] theorem to_of_option (o : option Ξ±) : to_option (of_option o) = o :=
by cases o; refl
@[simp] theorem of_to_option (o : roption Ξ±) [decidable o.dom] : of_option (to_option o) = o :=
ext $ Ξ» a, mem_of_option.trans mem_to_option
noncomputable def equiv_option : roption Ξ± β option Ξ± :=
by haveI := classical.dec; exact
β¨Ξ» o, to_option o, of_option, Ξ» o, of_to_option o,
Ξ» o, eq.trans (by dsimp; congr) (to_of_option o)β©
/-- `assert p f` is a bind-like operation which appends an additional condition
`p` to the domain and uses `f` to produce the value. -/
def assert (p : Prop) (f : p β roption Ξ±) : roption Ξ± :=
β¨βh : p, (f h).dom, Ξ»ha, (f ha.fst).get ha.sndβ©
/-- The bind operation has value `g (f.get)`, and is defined when all the
parts are defined. -/
protected def bind (f : roption Ξ±) (g : Ξ± β roption Ξ²) : roption Ξ² :=
assert (dom f) (Ξ»b, g (f.get b))
/-- The map operation for `roption` just maps the value and maintains the same domain. -/
def map (f : Ξ± β Ξ²) (o : roption Ξ±) : roption Ξ² :=
β¨o.dom, f β o.getβ©
theorem mem_map (f : Ξ± β Ξ²) {o : roption Ξ±} :
β {a}, a β o β f a β map f o
| _ β¨h, rflβ© := β¨_, rflβ©
@[simp] theorem mem_map_iff (f : Ξ± β Ξ²) {o : roption Ξ±} {b} :
b β map f o β β a β o, f a = b :=
β¨match b with _, β¨h, rflβ© := β¨_, β¨_, rflβ©, rflβ© end,
Ξ» β¨a, hβ, hββ©, hβ βΈ mem_map f hββ©
@[simp] theorem map_none (f : Ξ± β Ξ²) :
map f none = none := eq_none_iff.2 $ Ξ» a, by simp
@[simp] theorem map_some (f : Ξ± β Ξ²) (a : Ξ±) : map f (some a) = some (f a) :=
eq_some_iff.2 $ mem_map f $ mem_some _
theorem mem_assert {p : Prop} {f : p β roption Ξ±}
: β {a} (h : p), a β f h β a β assert p f
| _ _ β¨h, rflβ© := β¨β¨_, _β©, rflβ©
@[simp] theorem mem_assert_iff {p : Prop} {f : p β roption Ξ±} {a} :
a β assert p f β β h : p, a β f h :=
β¨match a with _, β¨h, rflβ© := β¨_, β¨_, rflβ©β© end,
Ξ» β¨a, hβ©, mem_assert _ hβ©
theorem mem_bind {f : roption Ξ±} {g : Ξ± β roption Ξ²} :
β {a b}, a β f β b β g a β b β f.bind g
| _ _ β¨h, rflβ© β¨hβ, rflβ© := β¨β¨_, _β©, rflβ©
@[simp] theorem mem_bind_iff {f : roption Ξ±} {g : Ξ± β roption Ξ²} {b} :
b β f.bind g β β a β f, b β g a :=
β¨match b with _, β¨β¨hβ, hββ©, rflβ© := β¨_, β¨_, rflβ©, β¨_, rflβ©β© end,
Ξ» β¨a, hβ, hββ©, mem_bind hβ hββ©
@[simp] theorem bind_none (f : Ξ± β roption Ξ²) :
none.bind f = none := eq_none_iff.2 $ Ξ» a, by simp
@[simp] theorem bind_some (a : Ξ±) (f : Ξ± β roption Ξ²) :
(some a).bind f = f a := ext $ by simp
theorem bind_some_eq_map (f : Ξ± β Ξ²) (x : roption Ξ±) :
x.bind (some β f) = map f x :=
ext $ by simp [eq_comm]
theorem bind_assoc {Ξ³} (f : roption Ξ±) (g : Ξ± β roption Ξ²) (k : Ξ² β roption Ξ³) :
(f.bind g).bind k = f.bind (Ξ» x, (g x).bind k) :=
ext $ Ξ» a, by simp; exact
β¨Ξ» β¨_, β¨_, hβ, hββ©, hββ©, β¨_, hβ, _, hβ, hββ©,
Ξ» β¨_, hβ, _, hβ, hββ©, β¨_, β¨_, hβ, hββ©, hββ©β©
@[simp] theorem bind_map {Ξ³} (f : Ξ± β Ξ²) (x) (g : Ξ² β roption Ξ³) :
(map f x).bind g = x.bind (Ξ» y, g (f y)) :=
by rw [β bind_some_eq_map, bind_assoc]; simp
@[simp] theorem map_bind {Ξ³} (f : Ξ± β roption Ξ²) (x : roption Ξ±) (g : Ξ² β Ξ³) :
map g (x.bind f) = x.bind (Ξ» y, map g (f y)) :=
by rw [β bind_some_eq_map, bind_assoc]; simp [bind_some_eq_map]
theorem map_map (g : Ξ² β Ξ³) (f : Ξ± β Ξ²) (o : roption Ξ±) :
map g (map f o) = map (g β f) o :=
by rw [β bind_some_eq_map, bind_map, bind_some_eq_map]
instance : monad roption :=
{ pure := @some,
map := @map,
bind := @roption.bind }
instance : is_lawful_monad roption :=
{ bind_pure_comp_eq_map := @bind_some_eq_map,
id_map := Ξ» Ξ² f, by cases f; refl,
pure_bind := @bind_some,
bind_assoc := @bind_assoc }
theorem map_id' {f : Ξ± β Ξ±} (H : β (x : Ξ±), f x = x) (o) : map f o = o :=
by rw [show f = id, from funext H]; exact id_map o
@[simp] theorem bind_some_right (x : roption Ξ±) : x.bind some = x :=
by rw [bind_some_eq_map]; simp [map_id']
@[simp] theorem ret_eq_some (a : Ξ±) : return a = some a := rfl
@[simp] theorem map_eq_map {Ξ± Ξ²} (f : Ξ± β Ξ²) (o : roption Ξ±) :
f <$> o = map f o := rfl
@[simp] theorem bind_eq_bind {Ξ± Ξ²} (f : roption Ξ±) (g : Ξ± β roption Ξ²) :
f >>= g = f.bind g := rfl
instance : monad_fail roption :=
{ fail := Ξ»_ _, none, ..roption.monad }
/- `restrict p o h` replaces the domain of `o` with `p`, and is well defined when
`p` implies `o` is defined. -/
def restrict (p : Prop) : β (o : roption Ξ±), (p β o.dom) β roption Ξ±
| β¨d, fβ© H := β¨p, Ξ»h, f (H h)β©
@[simp]
theorem mem_restrict (p : Prop) (o : roption Ξ±) (h : p β o.dom) (a : Ξ±) :
a β restrict p o h β p β§ a β o :=
begin
cases o, dsimp [restrict, mem_eq], split,
{ rintro β¨hβ, hββ©, exact β¨hβ, β¨_, hββ©β© },
rintro β¨hβ, hβ, hββ©, exact β¨hβ, hββ©
end
/-- `unwrap o` gets the value at `o`, ignoring the condition.
(This function is unsound.) -/
meta def unwrap (o : roption Ξ±) : Ξ± := o.get undefined
theorem assert_defined {p : Prop} {f : p β roption Ξ±} :
β (h : p), (f h).dom β (assert p f).dom := exists.intro
theorem bind_defined {f : roption Ξ±} {g : Ξ± β roption Ξ²} :
β (h : f.dom), (g (f.get h)).dom β (f.bind g).dom := assert_defined
@[simp] theorem bind_dom {f : roption Ξ±} {g : Ξ± β roption Ξ²} :
(f.bind g).dom β β h : f.dom, (g (f.get h)).dom := iff.rfl
end roption
/-- `pfun Ξ± Ξ²`, or `Ξ± β. Ξ²`, is the type of partial functions from
`Ξ±` to `Ξ²`. It is defined as `Ξ± β roption Ξ²`. -/
def pfun (Ξ± : Type*) (Ξ² : Type*) := Ξ± β roption Ξ²
infixr ` β. `:25 := pfun
namespace pfun
variables {Ξ± : Type*} {Ξ² : Type*} {Ξ³ : Type*}
/-- The domain of a partial function -/
def dom (f : Ξ± β. Ξ²) : set Ξ± := Ξ» a, (f a).dom
theorem mem_dom (f : Ξ± β. Ξ²) (x : Ξ±) : x β dom f β β y, y β f x :=
by simp [dom, set.mem_def, roption.dom_iff_mem]
theorem dom_eq (f : Ξ± β. Ξ²) : dom f = {x | β y, y β f x} :=
set.ext (mem_dom f)
/-- Evaluate a partial function -/
def fn (f : Ξ± β. Ξ²) (x) (h : dom f x) : Ξ² := (f x).get h
/-- Evaluate a partial function to return an `option` -/
def eval_opt (f : Ξ± β. Ξ²) [D : decidable_pred (dom f)] (x : Ξ±) : option Ξ² :=
@roption.to_option _ _ (D x)
/-- Partial function extensionality -/
theorem ext' {f g : Ξ± β. Ξ²}
(H1 : β a, a β dom f β a β dom g)
(H2 : β a p q, f.fn a p = g.fn a q) : f = g :=
funext $ Ξ» a, roption.ext' (H1 a) (H2 a)
theorem ext {f g : Ξ± β. Ξ²} (H : β a b, b β f a β b β g a) : f = g :=
funext $ Ξ» a, roption.ext (H a)
/-- Turn a partial function into a function out of a subtype -/
def as_subtype (f : Ξ± β. Ξ²) (s : {x // f.dom x}) : Ξ² := f.fn s.1 s.2
def equiv_subtype : (Ξ± β. Ξ²) β (Ξ£ p : Ξ± β Prop, subtype p β Ξ²) :=
β¨Ξ» f, β¨f.dom, as_subtype fβ©,
Ξ» β¨p, fβ© x, β¨p x, Ξ» h, f β¨x, hβ©β©,
Ξ» f, funext $ Ξ» a, roption.eta _,
Ξ» β¨p, fβ©, by dsimp; congr; funext a; cases a; reflβ©
theorem as_subtype_eq_of_mem {f : Ξ± β. Ξ²} {x : Ξ±} {y : Ξ²} (fxy : y β f x) (domx : x β f.dom) :
f.as_subtype β¨x, domxβ© = y :=
roption.mem_unique (roption.get_mem _) fxy
/-- Turn a total function into a partial function -/
protected def lift (f : Ξ± β Ξ²) : Ξ± β. Ξ² := Ξ» a, roption.some (f a)
instance : has_coe (Ξ± β Ξ²) (Ξ± β. Ξ²) := β¨pfun.liftβ©
@[simp] theorem lift_eq_coe (f : Ξ± β Ξ²) : pfun.lift f = f := rfl
@[simp] theorem coe_val (f : Ξ± β Ξ²) (a : Ξ±) :
(f : Ξ± β. Ξ²) a = roption.some (f a) := rfl
/-- The graph of a partial function is the set of pairs
`(x, f x)` where `x` is in the domain of `f`. -/
def graph (f : Ξ± β. Ξ²) : set (Ξ± Γ Ξ²) := {p | p.2 β f p.1}
def graph' (f : Ξ± β. Ξ²) : rel Ξ± Ξ² := Ξ» x y, y β f x
/-- The range of a partial function is the set of values
`f x` where `x` is in the domain of `f`. -/
def ran (f : Ξ± β. Ξ²) : set Ξ² := {b | βa, b β f a}
/-- Restrict a partial function to a smaller domain. -/
def restrict (f : Ξ± β. Ξ²) {p : set Ξ±} (H : p β f.dom) : Ξ± β. Ξ² :=
Ξ» x, roption.restrict (p x) (f x) (@H x)
@[simp]
theorem mem_restrict {f : Ξ± β. Ξ²} {s : set Ξ±} (h : s β f.dom) (a : Ξ±) (b : Ξ²) :
b β restrict f h a β a β s β§ b β f a :=
by { simp [restrict], reflexivity }
def res (f : Ξ± β Ξ²) (s : set Ξ±) : Ξ± β. Ξ² :=
restrict (pfun.lift f) (set.subset_univ s)
theorem mem_res (f : Ξ± β Ξ²) (s : set Ξ±) (a : Ξ±) (b : Ξ²) :
b β res f s a β (a β s β§ f a = b) :=
by { simp [res], split; {intro h, simp [h]} }
theorem res_univ (f : Ξ± β Ξ²) : pfun.res f set.univ = f :=
rfl
theorem dom_iff_graph (f : Ξ± β. Ξ²) (x : Ξ±) : x β f.dom β βy, (x, y) β f.graph :=
roption.dom_iff_mem
theorem lift_graph {f : Ξ± β Ξ²} {a b} : (a, b) β (f : Ξ± β. Ξ²).graph β f a = b :=
show (β (h : true), f a = b) β f a = b, by simp
/-- The monad `pure` function, the total constant `x` function -/
protected def pure (x : Ξ²) : Ξ± β. Ξ² := Ξ»_, roption.some x
/-- The monad `bind` function, pointwise `roption.bind` -/
def bind (f : Ξ± β. Ξ²) (g : Ξ² β Ξ± β. Ξ³) : Ξ± β. Ξ³ :=
Ξ»a, roption.bind (f a) (Ξ»b, g b a)
/-- The monad `map` function, pointwise `roption.map` -/
def map (f : Ξ² β Ξ³) (g : Ξ± β. Ξ²) : Ξ± β. Ξ³ :=
Ξ»a, roption.map f (g a)
instance : monad (pfun Ξ±) :=
{ pure := @pfun.pure _,
bind := @pfun.bind _,
map := @pfun.map _ }
instance : is_lawful_monad (pfun Ξ±) :=
{ bind_pure_comp_eq_map := Ξ» Ξ² Ξ³ f x, funext $ Ξ» a, roption.bind_some_eq_map _ _,
id_map := Ξ» Ξ² f, by funext a; dsimp [functor.map, pfun.map]; cases f a; refl,
pure_bind := Ξ» Ξ² Ξ³ x f, funext $ Ξ» a, roption.bind_some.{u_1 u_2} _ (f x),
bind_assoc := Ξ» Ξ² Ξ³ Ξ΄ f g k,
funext $ Ξ» a, roption.bind_assoc (f a) (Ξ» b, g b a) (Ξ» b, k b a) }
theorem pure_defined (p : set Ξ±) (x : Ξ²) : p β (@pfun.pure Ξ± _ x).dom := set.subset_univ p
theorem bind_defined {Ξ± Ξ² Ξ³} (p : set Ξ±) {f : Ξ± β. Ξ²} {g : Ξ² β Ξ± β. Ξ³}
(H1 : p β f.dom) (H2 : βx, p β (g x).dom) : p β (f >>= g).dom :=
Ξ»a ha, (β¨H1 ha, H2 _ haβ© : (f >>= g).dom a)
def fix (f : Ξ± β. Ξ² β Ξ±) : Ξ± β. Ξ² := Ξ» a,
roption.assert (acc (Ξ» x y, sum.inr x β f y) a) $ Ξ» h,
@well_founded.fix_F _ (Ξ» x y, sum.inr x β f y) _
(Ξ» a IH, roption.assert (f a).dom $ Ξ» hf,
by cases e : (f a).get hf with b a';
[exact roption.some b, exact IH _ β¨hf, eβ©])
a h
theorem dom_of_mem_fix {f : Ξ± β. Ξ² β Ξ±} {a : Ξ±} {b : Ξ²}
(h : b β fix f a) : (f a).dom :=
let β¨hβ, hββ© := roption.mem_assert_iff.1 h in
by rw well_founded.fix_F_eq at hβ; exact hβ.fst.fst
theorem mem_fix_iff {f : Ξ± β. Ξ² β Ξ±} {a : Ξ±} {b : Ξ²} :
b β fix f a β sum.inl b β f a β¨ β a', sum.inr a' β f a β§ b β fix f a' :=
β¨Ξ» h, let β¨hβ, hββ© := roption.mem_assert_iff.1 h in
begin
rw well_founded.fix_F_eq at hβ,
simp at hβ,
cases hβ with hβ hβ,
cases e : (f a).get hβ with b' a'; simp [e] at hβ,
{ subst b', refine or.inl β¨hβ, eβ© },
{ exact or.inr β¨a', β¨_, eβ©, roption.mem_assert _ hββ© }
end,
Ξ» h, begin
simp [fix],
rcases h with β¨hβ, hββ© | β¨a', h, hββ©,
{ refine β¨β¨_, Ξ» y h', _β©, _β©,
{ injection roption.mem_unique β¨hβ, hββ© h' },
{ rw well_founded.fix_F_eq, simp [hβ, hβ] } },
{ simp [fix] at hβ, cases hβ with hβ hβ,
refine β¨β¨_, Ξ» y h', _β©, _β©,
{ injection roption.mem_unique h h' with e,
exact e βΈ hβ },
{ cases h with hβ hβ,
rw well_founded.fix_F_eq, simp [hβ, hβ, hβ] } }
endβ©
@[elab_as_eliminator] def fix_induction
{f : Ξ± β. Ξ² β Ξ±} {b : Ξ²} {C : Ξ± β Sort*} {a : Ξ±} (h : b β fix f a)
(H : β a, b β fix f a β
(β a', b β fix f a' β sum.inr a' β f a β C a') β C a) : C a :=
begin
replace h := roption.mem_assert_iff.1 h,
have := h.snd, revert this,
induction h.fst with a ha IH, intro hβ,
refine H a (roption.mem_assert_iff.2 β¨β¨_, haβ©, hββ©)
(Ξ» a' ha' fa', _),
have := (roption.mem_assert_iff.1 ha').snd,
exact IH _ fa' β¨ha _ fa', thisβ© this
end
end pfun
namespace pfun
variables {Ξ± : Type*} {Ξ² : Type*} (f : Ξ± β. Ξ²)
def image (s : set Ξ±) : set Ξ² := rel.image f.graph' s
lemma image_def (s : set Ξ±) : image f s = {y | β x β s, y β f x} := rfl
lemma mem_image (y : Ξ²) (s : set Ξ±) : y β image f s β β x β s, y β f x :=
iff.rfl
lemma image_mono {s t : set Ξ±} (h : s β t) : f.image s β f.image t :=
rel.image_mono _ h
lemma image_inter (s t : set Ξ±) : f.image (s β© t) β f.image s β© f.image t :=
rel.image_inter _ s t
lemma image_union (s t : set Ξ±) : f.image (s βͺ t) = f.image s βͺ f.image t :=
rel.image_union _ s t
def preimage (s : set Ξ²) : set Ξ± := rel.preimage (Ξ» x y, y β f x) s
lemma preimage_def (s : set Ξ²) : preimage f s = {x | β y β s, y β f x} := rfl
lemma mem_preimage (s : set Ξ²) (x : Ξ±) : x β preimage f s β β y β s, y β f x :=
iff.rfl
lemma preimage_subset_dom (s : set Ξ²) : f.preimage s β f.dom :=
assume x β¨y, ys, fxyβ©, roption.dom_iff_mem.mpr β¨y, fxyβ©
lemma preimage_mono {s t : set Ξ²} (h : s β t) : f.preimage s β f.preimage t :=
rel.preimage_mono _ h
lemma preimage_inter (s t : set Ξ²) : f.preimage (s β© t) β f.preimage s β© f.preimage t :=
rel.preimage_inter _ s t
lemma preimage_union (s t : set Ξ²) : f.preimage (s βͺ t) = f.preimage s βͺ f.preimage t :=
rel.preimage_union _ s t
lemma preimage_univ : f.preimage set.univ = f.dom :=
by ext; simp [mem_preimage, mem_dom]
def core (s : set Ξ²) : set Ξ± := rel.core f.graph' s
lemma core_def (s : set Ξ²) : core f s = {x | β y, y β f x β y β s} := rfl
lemma mem_core (x : Ξ±) (s : set Ξ²) : x β core f s β (β y, y β f x β y β s) :=
iff.rfl
lemma compl_dom_subset_core (s : set Ξ²) : -f.dom β f.core s :=
assume x hx y fxy,
absurd ((mem_dom f x).mpr β¨y, fxyβ©) hx
lemma core_mono {s t : set Ξ²} (h : s β t) : f.core s β f.core t :=
rel.core_mono _ h
lemma core_inter (s t : set Ξ²) : f.core (s β© t) = f.core s β© f.core t :=
rel.core_inter _ s t
lemma mem_core_res (f : Ξ± β Ξ²) (s : set Ξ±) (t : set Ξ²) (x : Ξ±) :
x β core (res f s) t β (x β s β f x β t) :=
begin
simp [mem_core, mem_res], split,
{ intros h h', apply h _ h', reflexivity },
intros h y xs fxeq, rw βfxeq, exact h xs
end
section
open_locale classical
lemma core_res (f : Ξ± β Ξ²) (s : set Ξ±) (t : set Ξ²) : core (res f s) t = -s βͺ f β»ΒΉ' t :=
by { ext, rw mem_core_res, by_cases h : x β s; simp [h] }
end
lemma core_restrict (f : Ξ± β Ξ²) (s : set Ξ²) : core (f : Ξ± β. Ξ²) s = set.preimage f s :=
by ext x; simp [core_def]
lemma preimage_subset_core (f : Ξ± β. Ξ²) (s : set Ξ²) : f.preimage s β f.core s :=
assume x β¨y, ys, fxyβ© y' fxy',
have y = y', from roption.mem_unique fxy fxy',
this βΈ ys
lemma preimage_eq (f : Ξ± β. Ξ²) (s : set Ξ²) : f.preimage s = f.core s β© f.dom :=
set.eq_of_subset_of_subset
(set.subset_inter (preimage_subset_core f s) (preimage_subset_dom f s))
(assume x β¨xcore, xdomβ©,
let y := (f x).get xdom in
have ys : y β s, from xcore _ (roption.get_mem _),
show x β preimage f s, from β¨(f x).get xdom, ys, roption.get_mem _β©)
lemma core_eq (f : Ξ± β. Ξ²) (s : set Ξ²) : f.core s = f.preimage s βͺ -f.dom :=
by rw [preimage_eq, set.union_distrib_right, set.union_comm (dom f), set.compl_union_self,
set.inter_univ, set.union_eq_self_of_subset_right (compl_dom_subset_core f s)]
lemma preimage_as_subtype (f : Ξ± β. Ξ²) (s : set Ξ²) :
f.as_subtype β»ΒΉ' s = subtype.val β»ΒΉ' pfun.preimage f s :=
begin
ext x,
simp only [set.mem_preimage, set.mem_set_of_eq, pfun.as_subtype, pfun.mem_preimage],
show pfun.fn f (x.val) _ β s β β y β s, y β f (x.val),
exact iff.intro
(assume h, β¨_, h, roption.get_mem _β©)
(assume β¨y, ys, fxyβ©,
have f.fn x.val x.property β f x.val := roption.get_mem _,
roption.mem_unique fxy this βΈ ys)
end
end pfun
|
163d1c0294f6c6564ff5b67e5ef986fba7d71ace | d9ed0fce1c218297bcba93e046cb4e79c83c3af8 | /library/tools/super/resolution.lean | a12a1dd7e1fce72e8a4058bad1ab92505cbc713a | [
"Apache-2.0"
] | permissive | leodemoura/lean_clone | 005c63aa892a6492f2d4741ee3c2cb07a6be9d7f | cc077554b584d39bab55c360bc12a6fe7957afe6 | refs/heads/master | 1,610,506,475,484 | 1,482,348,354,000 | 1,482,348,543,000 | 77,091,586 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,298 | 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 .utils
open tactic monad
namespace super
variable gt : expr β expr β bool
variables (ac1 ac2 : derived_clause)
variables (c1 c2 : clause)
variables (i1 i2 : nat)
-- c1 : ... β Β¬a β ...
-- c2 : ... β a β ...
meta def try_resolve : tactic clause := do
qf1 β c1^.open_metan c1^.num_quants,
qf2 β c2^.open_metan c2^.num_quants,
-- FIXME: using default transparency unifies m*n with (x*y*z)*w here ???
unify_core transparency.reducible (qf1.1^.get_lit i1)^.formula (qf2.1^.get_lit i2)^.formula,
qf1i β qf1.1^.inst_mvars,
guard $ clause.is_maximal gt qf1i i1,
op1 β qf1.1^.open_constn i1,
op2 β qf2.1^.open_constn c2^.num_lits,
a_in_op2 β (op2.2^.nth i2)^.to_monad,
clause.meta_closure (qf1.2 ++ qf2.2) $
(op1.1^.inst (op2.1^.close_const a_in_op2)^.proof)^.close_constn (op1.2 ++ op2.2^.remove_nth i2)
meta def try_add_resolvent : prover unit := do
c' β β― try_resolve gt ac1^.c ac2^.c i1 i2,
inf_score 1 [ac1^.sc, ac2^.sc] >>= mk_derived c' >>= add_inferred
meta def maybe_add_resolvent : prover unit :=
try_add_resolvent gt ac1 ac2 i1 i2 <|> return ()
meta def resolution_left_inf : inference :=
take given, do active β get_active, sequence' $ do
given_i β given^.selected,
guard $ clause.literal.is_neg (given^.c^.get_lit given_i),
other β rb_map.values active,
guard $ Β¬given^.sc^.in_sos β¨ Β¬other^.sc^.in_sos,
other_i β other^.selected,
guard $ clause.literal.is_pos (other^.c^.get_lit other_i),
[maybe_add_resolvent gt other given other_i given_i]
meta def resolution_right_inf : inference :=
take given, do active β get_active, sequence' $ do
given_i β given^.selected,
guard $ clause.literal.is_pos (given^.c^.get_lit given_i),
other β rb_map.values active,
guard $ Β¬given^.sc^.in_sos β¨ Β¬other^.sc^.in_sos,
other_i β other^.selected,
guard $ clause.literal.is_neg (other^.c^.get_lit other_i),
[maybe_add_resolvent gt given other given_i other_i]
@[super.inf]
meta def resolution_inf : inf_decl := inf_decl.mk 40 $
take given, do gt β get_term_order, resolution_left_inf gt given >> resolution_right_inf gt given
end super
|
e97b4ac79e3713beb515653d9f0c8cde7d817e41 | 7cef822f3b952965621309e88eadf618da0c8ae9 | /src/data/polynomial.lean | 38d4b105924abda1ab4d3341dd9d0ca9a8414201 | [
"Apache-2.0"
] | permissive | rmitta/mathlib | 8d90aee30b4db2b013e01f62c33f297d7e64a43d | 883d974b608845bad30ae19e27e33c285200bf84 | refs/heads/master | 1,585,776,832,544 | 1,576,874,096,000 | 1,576,874,096,000 | 153,663,165 | 0 | 2 | Apache-2.0 | 1,544,806,490,000 | 1,539,884,365,000 | Lean | UTF-8 | Lean | false | false | 103,286 | 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, Jens Wagemaker
Theory of univariate polynomials, represented as `β ββ Ξ±`, where Ξ± is a commutative semiring.
-/
import data.finsupp algebra.gcd_domain ring_theory.euclidean_domain tactic.ring_exp ring_theory.multiplicity
noncomputable theory
local attribute [instance, priority 100] classical.prop_decidable
/-- `polynomial Ξ±` is the type of univariate polynomials over `Ξ±`.
Polynomials should be seen as (semi-)rings with the additional constructor `X`.
The embedding from Ξ± is called `C`. -/
def polynomial (Ξ± : Type*) [comm_semiring Ξ±] := β ββ Ξ±
open finsupp finset lattice
namespace polynomial
universes u v
variables {Ξ± : Type u} {Ξ² : Type v} {a b : Ξ±} {m n : β}
section comm_semiring
variables [comm_semiring Ξ±] {p q r : polynomial Ξ±}
instance : has_zero (polynomial Ξ±) := finsupp.has_zero
instance : has_one (polynomial Ξ±) := finsupp.has_one
instance : has_add (polynomial Ξ±) := finsupp.has_add
instance : has_mul (polynomial Ξ±) := finsupp.has_mul
instance : comm_semiring (polynomial Ξ±) := finsupp.comm_semiring
def coeff_coe_to_fun : has_coe_to_fun (polynomial Ξ±) :=
finsupp.has_coe_to_fun
local attribute [instance] finsupp.comm_semiring coeff_coe_to_fun
@[simp] lemma support_zero : (0 : polynomial Ξ±).support = β
:= rfl
/-- `C a` is the constant polynomial `a`. -/
def C (a : Ξ±) : polynomial Ξ± := single 0 a
/-- `X` is the polynomial variable (aka indeterminant). -/
def X : polynomial Ξ± := single 1 1
/-- coeff p n is the coefficient of X^n in p -/
def coeff (p : polynomial Ξ±) := p.to_fun
@[simp] lemma coeff_mk (s) (f) (h) : coeff (finsupp.mk s f h : polynomial Ξ±) = f := rfl
instance [has_repr Ξ±] : has_repr (polynomial Ξ±) :=
β¨Ξ» p, if p = 0 then "0"
else (p.support.sort (β€)).foldr
(Ξ» n a, a ++ (if a = "" then "" else " + ") ++
if n = 0
then "C (" ++ repr (coeff p n) ++ ")"
else if n = 1
then if (coeff p n) = 1 then "X" else "C (" ++ repr (coeff p n) ++ ") * X"
else if (coeff p n) = 1 then "X ^ " ++ repr n
else "C (" ++ repr (coeff p n) ++ ") * X ^ " ++ repr n) ""β©
theorem ext_iff {p q : polynomial Ξ±} : p = q β β n, coeff p n = coeff q n :=
β¨Ξ» h n, h βΈ rfl, finsupp.extβ©
@[ext] lemma ext {p q : polynomial Ξ±} : (β n, coeff p n = coeff q n) β p = q :=
(@ext_iff _ _ p q).2
/-- `degree p` is the degree of the polynomial `p`, i.e. the largest `X`-exponent in `p`.
`degree p = some n` when `p β 0` and `n` is the highest power of `X` that appears in `p`, otherwise
`degree 0 = β₯`. -/
def degree (p : polynomial Ξ±) : with_bot β := p.support.sup some
lemma degree_lt_wf : well_founded (Ξ»p q : polynomial Ξ±, degree p < degree q) :=
inv_image.wf degree (with_bot.well_founded_lt nat.lt_wf)
instance : has_well_founded (polynomial Ξ±) := β¨_, degree_lt_wfβ©
/-- `nat_degree p` forces `degree p` to β, by defining nat_degree 0 = 0. -/
def nat_degree (p : polynomial Ξ±) : β := (degree p).get_or_else 0
lemma single_eq_C_mul_X : β{n}, single n a = C a * X^n
| 0 := (mul_one _).symm
| (n+1) :=
calc single (n + 1) a = single n a * X : by rw [X, single_mul_single, mul_one]
... = (C a * X^n) * X : by rw [single_eq_C_mul_X]
... = C a * X^(n+1) : by simp only [pow_add, mul_assoc, pow_one]
lemma sum_C_mul_X_eq (p : polynomial Ξ±) : p.sum (Ξ»n a, C a * X^n) = p :=
eq.trans (sum_congr rfl $ assume n hn, single_eq_C_mul_X.symm) (finsupp.sum_single _)
@[elab_as_eliminator] protected lemma induction_on {M : polynomial Ξ± β Prop} (p : polynomial Ξ±)
(h_C : βa, M (C a))
(h_add : βp q, M p β M q β M (p + q))
(h_monomial : β(n : β) (a : Ξ±), M (C a * X^n) β M (C a * X^(n+1))) :
M p :=
have β{n:β} {a}, M (C a * X^n),
begin
assume n a,
induction n with n ih,
{ simp only [pow_zero, mul_one, h_C] },
{ exact h_monomial _ _ ih }
end,
finsupp.induction p
(suffices M (C 0), by simpa only [C, single_zero],
h_C 0)
(assume n a p _ _ hp, suffices M (C a * X^n + p), by rwa [single_eq_C_mul_X],
h_add _ _ this hp)
@[simp] lemma C_0 : C (0 : Ξ±) = 0 := single_zero
@[simp] lemma C_1 : C (1 : Ξ±) = 1 := rfl
@[simp] lemma C_mul : C (a * b) = C a * C b :=
(@single_mul_single _ _ _ _ 0 0 a b).symm
@[simp] lemma C_add : C (a + b) = C a + C b := finsupp.single_add
instance C.is_semiring_hom : is_semiring_hom (C : Ξ± β polynomial Ξ±) :=
β¨C_0, C_1, Ξ» _ _, C_add, Ξ» _ _, C_mulβ©
@[simp] lemma C_pow : C (a ^ n) = C a ^ n := is_semiring_hom.map_pow _ _ _
lemma nat_cast_eq_C (n : β) : (n : polynomial Ξ±) = C n :=
by refine (nat.eq_cast (Ξ» n, C (n : Ξ±)) _ _ _ n).symm; simp
section coeff
lemma apply_eq_coeff : p n = coeff p n := rfl
@[simp] lemma coeff_zero (n : β) : coeff (0 : polynomial Ξ±) n = 0 := rfl
lemma coeff_single : coeff (single n a) m = if n = m then a else 0 :=
by { dsimp [single], congr }
@[simp] lemma coeff_one_zero : coeff (1 : polynomial Ξ±) 0 = 1 :=
coeff_single
@[simp] lemma coeff_add (p q : polynomial Ξ±) (n : β) : coeff (p + q) n = coeff p n + coeff q n := rfl
instance coeff.is_add_monoid_hom {n : β} : is_add_monoid_hom (Ξ» p : polynomial Ξ±, p.coeff n) :=
{ map_add := Ξ» p q, coeff_add p q n,
map_zero := coeff_zero _ }
lemma coeff_C : coeff (C a) n = ite (n = 0) a 0 :=
by simp [coeff, eq_comm, C, single]; congr
@[simp] lemma coeff_C_zero : coeff (C a) 0 = a := coeff_single
@[simp] lemma coeff_X_one : coeff (X : polynomial Ξ±) 1 = 1 := coeff_single
@[simp] lemma coeff_X_zero : coeff (X : polynomial Ξ±) 0 = 0 := coeff_single
lemma coeff_X : coeff (X : polynomial Ξ±) n = if 1 = n then 1 else 0 := coeff_single
@[simp] lemma coeff_C_mul_X (x : Ξ±) (k n : β) :
coeff (C x * X^k : polynomial Ξ±) n = if n = k then x else 0 :=
by rw [β single_eq_C_mul_X]; simp [single, eq_comm, coeff]; congr
lemma coeff_sum [comm_semiring Ξ²] (n : β) (f : β β Ξ± β polynomial Ξ²) :
coeff (p.sum f) n = p.sum (Ξ» a b, coeff (f a b) n) := finsupp.sum_apply
@[simp] lemma coeff_C_mul (p : polynomial Ξ±) : coeff (C a * p) n = a * coeff p n :=
begin
conv in (a * _) { rw [β @sum_single _ _ _ p, coeff_sum] },
rw [mul_def, C, sum_single_index],
{ simp [coeff_single, finsupp.mul_sum, coeff_sum],
apply sum_congr rfl,
assume i hi, by_cases i = n; simp [h] },
simp
end
@[simp] lemma coeff_one (n : β) : coeff (1 : polynomial Ξ±) n = if 0 = n then 1 else 0 :=
coeff_single
@[simp] lemma coeff_X_pow (k n : β) :
coeff (X^k : polynomial Ξ±) n = if n = k then 1 else 0 :=
by simpa only [C_1, one_mul] using coeff_C_mul_X (1:Ξ±) k n
lemma coeff_mul (p q : polynomial Ξ±) (n : β) :
coeff (p * q) n = (nat.antidiagonal n).sum (Ξ» x, coeff p x.1 * coeff q x.2) :=
have hite : β a : β Γ β, ite (a.1 + a.2 = n) (coeff p (a.fst) * coeff q (a.snd)) 0 β 0
β a.1 + a.2 = n, from Ξ» a ha, by_contradiction
(Ξ» h, absurd (eq.refl (0 : Ξ±)) (by rwa if_neg h at ha)),
calc coeff (p * q) n = sum (p.support) (Ξ» a, sum (q.support)
(Ξ» b, ite (a + b = n) (coeff p a * coeff q b) 0)) :
by simp only [finsupp.mul_def, coeff_sum, coeff_single]; refl
... = (p.support.product q.support).sum
(Ξ» v : β Γ β, ite (v.1 + v.2 = n) (coeff p v.1 * coeff q v.2) 0) :
by rw sum_product
... = (nat.antidiagonal n).sum (Ξ» x, coeff p x.1 * coeff q x.2) :
begin
refine sum_bij_ne_zero (Ξ» x _ _, x)
(Ξ» x _ hx, nat.mem_antidiagonal.2 (hite x hx)) (Ξ» _ _ _ _ _ _ h, h)
(Ξ» x hβ hβ, β¨x, _, _, rflβ©) _,
{ rw [mem_product, mem_support_iff, mem_support_iff],
exact β¨ne_zero_of_mul_ne_zero_right hβ, ne_zero_of_mul_ne_zero_left hββ© },
{ rw nat.mem_antidiagonal at hβ, rwa [if_pos hβ] },
{ intros x h hx, rw [if_pos (hite x hx)] }
end
theorem coeff_mul_X_pow (p : polynomial Ξ±) (n d : β) :
coeff (p * polynomial.X ^ n) (d + n) = coeff p d :=
begin
rw [coeff_mul, sum_eq_single (d,n), coeff_X_pow, if_pos rfl, mul_one],
{ rintros β¨i,jβ© h1 h2, rw [coeff_X_pow, if_neg, mul_zero], rintro rfl, apply h2,
rw [nat.mem_antidiagonal, add_right_cancel_iff] at h1, subst h1 },
{ exact Ξ» h1, (h1 (nat.mem_antidiagonal.2 rfl)).elim }
end
theorem coeff_mul_X (p : polynomial Ξ±) (n : β) :
coeff (p * X) (n + 1) = coeff p n :=
by simpa only [pow_one] using coeff_mul_X_pow p 1 n
theorem mul_X_pow_eq_zero {p : polynomial Ξ±} {n : β}
(H : p * X ^ n = 0) : p = 0 :=
ext $ Ξ» k, (coeff_mul_X_pow p n k).symm.trans $ ext_iff.1 H (k+n)
end coeff
lemma C_inj : C a = C b β a = b :=
β¨Ξ» h, coeff_C_zero.symm.trans (h.symm βΈ coeff_C_zero), congr_arg Cβ©
section evalβ
variables [semiring Ξ²]
variables (f : Ξ± β Ξ²) (x : Ξ²)
open is_semiring_hom
/-- Evaluate a polynomial `p` given a ring hom `f` from the scalar ring
to the target and a value `x` for the variable in the target -/
def evalβ (p : polynomial Ξ±) : Ξ² :=
p.sum (Ξ» e a, f a * x ^ e)
variables [is_semiring_hom f]
@[simp] lemma evalβ_C : (C a).evalβ f x = f a :=
(sum_single_index $ by rw [map_zero f, zero_mul]).trans $ by rw [pow_zero, mul_one]
@[simp] lemma evalβ_X : X.evalβ f x = x :=
(sum_single_index $ by rw [map_zero f, zero_mul]).trans $ by rw [map_one f, one_mul, pow_one]
@[simp] lemma evalβ_zero : (0 : polynomial Ξ±).evalβ f x = 0 :=
finsupp.sum_zero_index
@[simp] lemma evalβ_add : (p + q).evalβ f x = p.evalβ f x + q.evalβ f x :=
finsupp.sum_add_index
(Ξ» _, by rw [map_zero f, zero_mul])
(Ξ» _ _ _, by rw [map_add f, add_mul])
@[simp] lemma evalβ_one : (1 : polynomial Ξ±).evalβ f x = 1 :=
by rw [β C_1, evalβ_C, map_one f]
instance evalβ.is_add_monoid_hom : is_add_monoid_hom (evalβ f x) :=
{ map_zero := evalβ_zero _ _, map_add := Ξ» _ _, evalβ_add _ _ }
end evalβ
section evalβ
variables [comm_semiring Ξ²]
variables (f : Ξ± β Ξ²) [is_semiring_hom f] (x : Ξ²)
open is_semiring_hom
@[simp] lemma evalβ_mul : (p * q).evalβ f x = p.evalβ f x * q.evalβ f x :=
begin
dunfold evalβ,
rw [mul_def, finsupp.sum_mul _ p], simp only [finsupp.mul_sum _ q], rw [sum_sum_index],
{ apply sum_congr rfl, assume i hi, dsimp only, rw [sum_sum_index],
{ apply sum_congr rfl, assume j hj, dsimp only,
rw [sum_single_index, map_mul f, pow_add],
{ simp only [mul_assoc, mul_left_comm] },
{ rw [map_zero f, zero_mul] } },
{ intro, rw [map_zero f, zero_mul] },
{ intros, rw [map_add f, add_mul] } },
{ intro, rw [map_zero f, zero_mul] },
{ intros, rw [map_add f, add_mul] }
end
instance evalβ.is_semiring_hom : is_semiring_hom (evalβ f x) :=
β¨evalβ_zero _ _, evalβ_one _ _, Ξ» _ _, evalβ_add _ _, Ξ» _ _, evalβ_mul _ _β©
lemma evalβ_pow (n : β) : (p ^ n).evalβ f x = p.evalβ f x ^ n := map_pow _ _ _
lemma evalβ_sum (p : polynomial Ξ±) (g : β β Ξ± β polynomial Ξ±) (x : Ξ²) :
(p.sum g).evalβ f x = p.sum (Ξ» n a, (g n a).evalβ f x) :=
finsupp.sum_sum_index (by simp [is_add_monoid_hom.map_zero f])
(by intros; simp [right_distrib, is_add_monoid_hom.map_add f])
end evalβ
section eval
variable {x : Ξ±}
/-- `eval x p` is the evaluation of the polynomial `p` at `x` -/
def eval : Ξ± β polynomial Ξ± β Ξ± := evalβ id
@[simp] lemma eval_C : (C a).eval x = a := evalβ_C _ _
@[simp] lemma eval_X : X.eval x = x := evalβ_X _ _
@[simp] lemma eval_zero : (0 : polynomial Ξ±).eval x = 0 := evalβ_zero _ _
@[simp] lemma eval_add : (p + q).eval x = p.eval x + q.eval x := evalβ_add _ _
@[simp] lemma eval_one : (1 : polynomial Ξ±).eval x = 1 := evalβ_one _ _
@[simp] lemma eval_mul : (p * q).eval x = p.eval x * q.eval x := evalβ_mul _ _
instance eval.is_semiring_hom : is_semiring_hom (eval x) := evalβ.is_semiring_hom _ _
@[simp] lemma eval_pow (n : β) : (p ^ n).eval x = p.eval x ^ n := evalβ_pow _ _ _
lemma eval_sum (p : polynomial Ξ±) (f : β β Ξ± β polynomial Ξ±) (x : Ξ±) :
(p.sum f).eval x = p.sum (Ξ» n a, (f n a).eval x) :=
evalβ_sum _ _ _ _
lemma evalβ_hom [comm_semiring Ξ²] (f : Ξ± β Ξ²) [is_semiring_hom f] (x : Ξ±) :
p.evalβ f (f x) = f (p.eval x) :=
polynomial.induction_on p
(by simp)
(by simp [is_semiring_hom.map_add f] {contextual := tt})
(by simp [is_semiring_hom.map_mul f, eval_pow,
is_semiring_hom.map_pow f, pow_succ', (mul_assoc _ _ _).symm] {contextual := tt})
/-- `is_root p x` implies `x` is a root of `p`. The evaluation of `p` at `x` is zero -/
def is_root (p : polynomial Ξ±) (a : Ξ±) : Prop := p.eval a = 0
instance [decidable_eq Ξ±] : decidable (is_root p a) := by unfold is_root; apply_instance
@[simp] lemma is_root.def : is_root p a β p.eval a = 0 := iff.rfl
lemma root_mul_left_of_is_root (p : polynomial Ξ±) {q : polynomial Ξ±} :
is_root q a β is_root (p * q) a :=
Ξ» H, by rw [is_root, eval_mul, is_root.def.1 H, mul_zero]
lemma root_mul_right_of_is_root {p : polynomial Ξ±} (q : polynomial Ξ±) :
is_root p a β is_root (p * q) a :=
Ξ» H, by rw [is_root, eval_mul, is_root.def.1 H, zero_mul]
lemma coeff_zero_eq_eval_zero (p : polynomial Ξ±) :
coeff p 0 = p.eval 0 :=
calc coeff p 0 = coeff p 0 * 0 ^ 0 : by simp
... = p.eval 0 : eq.symm $
finset.sum_eq_single _ (Ξ» b _ hb, by simp [zero_pow (nat.pos_of_ne_zero hb)]) (by simp)
lemma zero_is_root_of_coeff_zero_eq_zero {p : polynomial Ξ±} (hp : p.coeff 0 = 0) :
is_root p 0 :=
by rwa coeff_zero_eq_eval_zero at hp
end eval
section comp
def comp (p q : polynomial Ξ±) : polynomial Ξ± := p.evalβ C q
lemma evalβ_comp [comm_semiring Ξ²] (f : Ξ± β Ξ²) [is_semiring_hom f] {x : Ξ²} :
(p.comp q).evalβ f x = p.evalβ f (q.evalβ f x) :=
show (p.sum (Ξ» e a, C a * q ^ e)).evalβ f x = p.evalβ f (evalβ f x q),
by simp only [evalβ_mul, evalβ_C, evalβ_pow, evalβ_sum]; refl
lemma eval_comp : (p.comp q).eval a = p.eval (q.eval a) := evalβ_comp _
@[simp] lemma comp_X : p.comp X = p :=
begin
refine ext (Ξ» n, _),
rw [comp, evalβ],
conv in (C _ * _) { rw β single_eq_C_mul_X },
rw finsupp.sum_single
end
@[simp] lemma X_comp : X.comp p = p := evalβ_X _ _
@[simp] lemma comp_C : p.comp (C a) = C (p.eval a) :=
begin
dsimp [comp, evalβ, eval, finsupp.sum],
rw [β sum_hom (@C Ξ± _)],
apply finset.sum_congr rfl; simp
end
@[simp] lemma C_comp : (C a).comp p = C a := evalβ_C _ _
@[simp] lemma comp_zero : p.comp (0 : polynomial Ξ±) = C (p.eval 0) :=
by rw [β C_0, comp_C]
@[simp] lemma zero_comp : comp (0 : polynomial Ξ±) p = 0 :=
by rw [β C_0, C_comp]
@[simp] lemma comp_one : p.comp 1 = C (p.eval 1) :=
by rw [β C_1, comp_C]
@[simp] lemma one_comp : comp (1 : polynomial Ξ±) p = 1 :=
by rw [β C_1, C_comp]
instance : is_semiring_hom (Ξ» q : polynomial Ξ±, q.comp p) :=
by unfold comp; apply_instance
@[simp] lemma add_comp : (p + q).comp r = p.comp r + q.comp r := evalβ_add _ _
@[simp] lemma mul_comp : (p * q).comp r = p.comp r * q.comp r := evalβ_mul _ _
end comp
/-- `leading_coeff p` gives the coefficient of the highest power of `X` in `p`-/
def leading_coeff (p : polynomial Ξ±) : Ξ± := coeff p (nat_degree p)
/-- a polynomial is `monic` if its leading coefficient is 1 -/
def monic (p : polynomial Ξ±) := leading_coeff p = (1 : Ξ±)
lemma monic.def : monic p β leading_coeff p = 1 := iff.rfl
instance monic.decidable [decidable_eq Ξ±] : decidable (monic p) :=
by unfold monic; apply_instance
@[simp] lemma monic.leading_coeff {p : polynomial Ξ±} (hp : p.monic) :
leading_coeff p = 1 := hp
@[simp] lemma degree_zero : degree (0 : polynomial Ξ±) = β₯ := rfl
@[simp] lemma nat_degree_zero : nat_degree (0 : polynomial Ξ±) = 0 := rfl
@[simp] lemma degree_C (ha : a β 0) : degree (C a) = (0 : with_bot β) :=
show sup (ite (a = 0) β
{0}) some = 0, by rw if_neg ha; refl
lemma degree_C_le : degree (C a) β€ (0 : with_bot β) :=
by by_cases h : a = 0; [rw [h, C_0], rw [degree_C h]]; [exact bot_le, exact le_refl _]
lemma degree_one_le : degree (1 : polynomial Ξ±) β€ (0 : with_bot β) :=
by rw [β C_1]; exact degree_C_le
lemma degree_eq_bot : degree p = β₯ β p = 0 :=
β¨Ξ» h, by rw [degree, β max_eq_sup_with_bot] at h;
exact support_eq_empty.1 (max_eq_none.1 h),
Ξ» h, h.symm βΈ rflβ©
lemma degree_eq_nat_degree (hp : p β 0) : degree p = (nat_degree p : with_bot β) :=
let β¨n, hnβ© :=
classical.not_forall.1 (mt option.eq_none_iff_forall_not_mem.2 (mt degree_eq_bot.1 hp)) in
have hn : degree p = some n := not_not.1 hn,
by rw [nat_degree, hn]; refl
lemma degree_eq_iff_nat_degree_eq {p : polynomial Ξ±} {n : β} (hp : p β 0) :
p.degree = n β p.nat_degree = n :=
by rw [degree_eq_nat_degree hp, with_bot.coe_eq_coe]
lemma degree_eq_iff_nat_degree_eq_of_pos {p : polynomial Ξ±} {n : β} (hn : n > 0) :
p.degree = n β p.nat_degree = n :=
begin
split,
{ intro H, rwa β degree_eq_iff_nat_degree_eq, rintro rfl,
rw degree_zero at H, exact option.no_confusion H },
{ intro H, rwa degree_eq_iff_nat_degree_eq, rintro rfl,
rw nat_degree_zero at H, rw H at hn, exact lt_irrefl _ hn }
end
lemma nat_degree_eq_of_degree_eq_some {p : polynomial Ξ±} {n : β}
(h : degree p = n) : nat_degree p = n :=
have hp0 : p β 0, from Ξ» hp0, by rw hp0 at h; exact option.no_confusion h,
option.some_inj.1 $ show (nat_degree p : with_bot β) = n,
by rwa [β degree_eq_nat_degree hp0]
@[simp] lemma degree_le_nat_degree : degree p β€ nat_degree p :=
begin
by_cases hp : p = 0, { rw hp, exact bot_le },
rw [degree_eq_nat_degree hp],
exact le_refl _
end
lemma nat_degree_eq_of_degree_eq [comm_semiring Ξ²] {q : polynomial Ξ²}
(h : degree p = degree q) : nat_degree p = nat_degree q :=
by unfold nat_degree; rw h
lemma le_degree_of_ne_zero (h : coeff p n β 0) : (n : with_bot β) β€ degree p :=
show @has_le.le (with_bot β) _ (some n : with_bot β) (p.support.sup some : with_bot β),
from finset.le_sup (finsupp.mem_support_iff.2 h)
lemma le_nat_degree_of_ne_zero (h : coeff p n β 0) : n β€ nat_degree p :=
begin
rw [β with_bot.coe_le_coe, β degree_eq_nat_degree],
exact le_degree_of_ne_zero h,
{ assume h, subst h, exact h rfl }
end
lemma degree_le_degree (h : coeff q (nat_degree p) β 0) : degree p β€ degree q :=
begin
by_cases hp : p = 0,
{ rw hp, exact bot_le },
{ rw degree_eq_nat_degree hp, exact le_degree_of_ne_zero h }
end
@[simp] lemma nat_degree_C (a : Ξ±) : nat_degree (C a) = 0 :=
begin
by_cases ha : a = 0,
{ have : C a = 0, { rw [ha, C_0] },
rw [nat_degree, degree_eq_bot.2 this],
refl },
{ rw [nat_degree, degree_C ha], refl }
end
@[simp] lemma nat_degree_one : nat_degree (1 : polynomial Ξ±) = 0 := nat_degree_C 1
@[simp] lemma nat_degree_nat_cast (n : β) : nat_degree (n : polynomial Ξ±) = 0 :=
by simp [nat_cast_eq_C]
@[simp] lemma degree_monomial (n : β) (ha : a β 0) : degree (C a * X ^ n) = n :=
by rw [β single_eq_C_mul_X, degree, support_single_ne_zero ha]; refl
lemma degree_monomial_le (n : β) (a : Ξ±) : degree (C a * X ^ n) β€ n :=
if h : a = 0 then by rw [h, C_0, zero_mul]; exact bot_le else le_of_eq (degree_monomial n h)
lemma coeff_eq_zero_of_degree_lt (h : degree p < n) : coeff p n = 0 :=
not_not.1 (mt le_degree_of_ne_zero (not_le_of_gt h))
lemma coeff_eq_zero_of_nat_degree_lt {p : polynomial Ξ±} {n : β} (h : p.nat_degree < n) :
p.coeff n = 0 :=
begin
apply coeff_eq_zero_of_degree_lt,
by_cases hp : p = 0,
{ subst hp, exact with_bot.bot_lt_coe n },
{ rwa [degree_eq_nat_degree hp, with_bot.coe_lt_coe] }
end
-- TODO find a home (this file)
@[simp] lemma finset_sum_coeff (s : finset Ξ²) (f : Ξ² β polynomial Ξ±) (n : β) :
coeff (s.sum f) n = s.sum (Ξ» b, coeff (f b) n) :=
(finset.sum_hom (Ξ» q : polynomial Ξ±, q.coeff n)).symm
-- We need the explicit `decidable` argument here because an exotic one shows up in a moment!
lemma ite_le_nat_degree_coeff (p : polynomial Ξ±) (n : β) (I : decidable (n < 1 + nat_degree p)) :
@ite (n < 1 + nat_degree p) I _ (coeff p n) 0 = coeff p n :=
begin
split_ifs,
{ refl },
{ exact (coeff_eq_zero_of_nat_degree_lt (not_le.1 (Ξ» w, h (nat.lt_one_add_iff.2 w)))).symm, }
end
lemma as_sum (p : polynomial Ξ±) :
p = (range (p.nat_degree + 1)).sum (Ξ» i, C (p.coeff i) * X^i) :=
begin
ext n,
simp only [add_comm, coeff_X_pow, coeff_C_mul, finset.mem_range,
finset.sum_mul_boole, finset_sum_coeff, ite_le_nat_degree_coeff],
end
lemma monic.as_sum {p : polynomial Ξ±} (hp : p.monic) :
p = X^(p.nat_degree) + ((finset.range p.nat_degree).sum $ Ξ» i, C (p.coeff i) * X^i) :=
begin
conv_lhs { rw [p.as_sum, finset.sum_range_succ] },
suffices : C (p.coeff p.nat_degree) = 1,
{ rw [this, one_mul] },
exact congr_arg C hp
end
section map
variables [comm_semiring Ξ²]
variables (f : Ξ± β Ξ²)
/-- `map f p` maps a polynomial `p` across a ring hom `f` -/
def map : polynomial Ξ± β polynomial Ξ² := evalβ (C β f) X
variables [is_semiring_hom f]
@[simp] lemma map_C : (C a).map f = C (f a) := evalβ_C _ _
@[simp] lemma map_X : X.map f = X := evalβ_X _ _
@[simp] lemma map_zero : (0 : polynomial Ξ±).map f = 0 := evalβ_zero _ _
@[simp] lemma map_add : (p + q).map f = p.map f + q.map f := evalβ_add _ _
@[simp] lemma map_one : (1 : polynomial Ξ±).map f = 1 := evalβ_one _ _
@[simp] lemma map_mul : (p * q).map f = p.map f * q.map f := evalβ_mul _ _
instance map.is_semiring_hom : is_semiring_hom (map f) := evalβ.is_semiring_hom _ _
@[simp] lemma map_pow (n : β) : (p ^ n).map f = p.map f ^ n := evalβ_pow _ _ _
lemma coeff_map (n : β) : coeff (p.map f) n = f (coeff p n) :=
begin
rw [map, evalβ, coeff_sum],
conv_rhs { rw [β sum_C_mul_X_eq p, coeff_sum, finsupp.sum,
β finset.sum_hom f], },
refine finset.sum_congr rfl (Ξ» x hx, _),
simp [function.comp, coeff_C_mul_X, is_semiring_hom.map_mul f],
split_ifs; simp [is_semiring_hom.map_zero f],
end
lemma map_map {Ξ³ : Type*} [comm_semiring Ξ³] (g : Ξ² β Ξ³) [is_semiring_hom g]
(p : polynomial Ξ±) : (p.map f).map g = p.map (Ξ» x, g (f x)) :=
ext (by simp [coeff_map])
lemma evalβ_map {Ξ³ : Type*} [comm_semiring Ξ³] (g : Ξ² β Ξ³) [is_semiring_hom g] (x : Ξ³) :
(p.map f).evalβ g x = p.evalβ (Ξ» y, g (f y)) x :=
polynomial.induction_on p
(by simp)
(by simp [is_semiring_hom.map_add f] {contextual := tt})
(by simp [is_semiring_hom.map_mul f,
is_semiring_hom.map_pow f, pow_succ', (mul_assoc _ _ _).symm] {contextual := tt})
lemma eval_map (x : Ξ²) : (p.map f).eval x = p.evalβ f x := evalβ_map _ _ _
@[simp] lemma map_id : p.map id = p := by simp [id, polynomial.ext_iff, coeff_map]
lemma mem_map_range {p : polynomial Ξ²} :
p β set.range (map f) β β n, p.coeff n β (set.range f) :=
begin
split,
{ rintro β¨p, rflβ© n, rw coeff_map, exact set.mem_range_self _ },
{ intro h, rw p.as_sum,
apply is_add_submonoid.finset_sum_mem,
intros i hi,
rcases h i with β¨c, hcβ©,
use [C c * X^i],
rw [map_mul, map_C, hc, map_pow, map_X] }
end
end map
section
variables {Ξ³ : Type*} [comm_semiring Ξ²] [comm_semiring Ξ³]
variables (f : Ξ± β Ξ²) (g : Ξ² β Ξ³) [is_semiring_hom f] [is_semiring_hom g] (p)
lemma hom_evalβ (x : Ξ²) : g (p.evalβ f x) = p.evalβ (g β f) (g x) :=
begin
apply polynomial.induction_on p; clear p,
{ intros a, rw [evalβ_C, evalβ_C] },
{ intros p q hp hq, simp only [hp, hq, evalβ_add, is_semiring_hom.map_add g] },
{ intros n a ih,
replace ih := congr_arg (Ξ» y, y * g x) ih,
simpa [pow_succ', is_semiring_hom.map_mul g, (mul_assoc _ _ _).symm,
evalβ_C, evalβ_mul, evalβ_X] using ih }
end
end
lemma coeff_nat_degree_eq_zero_of_degree_lt (h : degree p < degree q) : coeff p (nat_degree q) = 0 :=
coeff_eq_zero_of_degree_lt (lt_of_lt_of_le h degree_le_nat_degree)
lemma ne_zero_of_degree_gt {n : with_bot β} (h : n < degree p) : p β 0 :=
mt degree_eq_bot.2 (ne.symm (ne_of_lt (lt_of_le_of_lt bot_le h)))
lemma eq_C_of_degree_le_zero (h : degree p β€ 0) : p = C (coeff p 0) :=
begin
refine ext (Ξ» n, _),
cases n,
{ simp },
{ have : degree p < β(nat.succ n) := lt_of_le_of_lt h (with_bot.some_lt_some.2 (nat.succ_pos _)),
rw [coeff_C, if_neg (nat.succ_ne_zero _), coeff_eq_zero_of_degree_lt this] }
end
lemma eq_C_of_degree_eq_zero (h : degree p = 0) : p = C (coeff p 0) :=
eq_C_of_degree_le_zero (h βΈ le_refl _)
lemma degree_le_zero_iff : degree p β€ 0 β p = C (coeff p 0) :=
β¨eq_C_of_degree_le_zero, Ξ» h, h.symm βΈ degree_C_leβ©
lemma degree_add_le (p q : polynomial Ξ±) : degree (p + q) β€ max (degree p) (degree q) :=
calc degree (p + q) = ((p + q).support).sup some : rfl
... β€ (p.support βͺ q.support).sup some : by convert sup_mono support_add
... = p.support.sup some β q.support.sup some : by convert sup_union
... = _ : with_bot.sup_eq_max _ _
@[simp] lemma leading_coeff_zero : leading_coeff (0 : polynomial Ξ±) = 0 := rfl
@[simp] lemma leading_coeff_eq_zero : leading_coeff p = 0 β p = 0 :=
β¨Ξ» h, by_contradiction $ Ξ» hp, mt mem_support_iff.1
(not_not.2 h) (mem_of_max (degree_eq_nat_degree hp)),
Ξ» h, h.symm βΈ leading_coeff_zeroβ©
lemma leading_coeff_eq_zero_iff_deg_eq_bot : leading_coeff p = 0 β degree p = β₯ :=
by rw [leading_coeff_eq_zero, degree_eq_bot]
lemma degree_add_eq_of_degree_lt (h : degree p < degree q) : degree (p + q) = degree q :=
le_antisymm (max_eq_right_of_lt h βΈ degree_add_le _ _) $ degree_le_degree $
begin
rw [coeff_add, coeff_nat_degree_eq_zero_of_degree_lt h, zero_add],
exact mt leading_coeff_eq_zero.1 (ne_zero_of_degree_gt h)
end
lemma degree_add_C (hp : 0 < degree p) : degree (p + C a) = degree p :=
add_comm (C a) p βΈ degree_add_eq_of_degree_lt $ lt_of_le_of_lt degree_C_le hp
lemma degree_add_eq_of_leading_coeff_add_ne_zero (h : leading_coeff p + leading_coeff q β 0) :
degree (p + q) = max p.degree q.degree :=
le_antisymm (degree_add_le _ _) $
match lt_trichotomy (degree p) (degree q) with
| or.inl hlt :=
by rw [degree_add_eq_of_degree_lt hlt, max_eq_right_of_lt hlt]; exact le_refl _
| or.inr (or.inl heq) :=
le_of_not_gt $
assume hlt : max (degree p) (degree q) > degree (p + q),
h $ show leading_coeff p + leading_coeff q = 0,
begin
rw [heq, max_self] at hlt,
rw [leading_coeff, leading_coeff, nat_degree_eq_of_degree_eq heq, β coeff_add],
exact coeff_nat_degree_eq_zero_of_degree_lt hlt
end
| or.inr (or.inr hlt) :=
by rw [add_comm, degree_add_eq_of_degree_lt hlt, max_eq_left_of_lt hlt]; exact le_refl _
end
lemma degree_erase_le (p : polynomial Ξ±) (n : β) : degree (p.erase n) β€ degree p :=
by convert sup_mono (erase_subset _ _)
lemma degree_erase_lt (hp : p β 0) : degree (p.erase (nat_degree p)) < degree p :=
lt_of_le_of_ne (degree_erase_le _ _) $
(degree_eq_nat_degree hp).symm βΈ (by convert Ξ» h, not_mem_erase _ _ (mem_of_max h))
lemma degree_sum_le (s : finset Ξ²) (f : Ξ² β polynomial Ξ±) :
degree (s.sum f) β€ s.sup (Ξ» b, degree (f b)) :=
finset.induction_on s (by simp only [sum_empty, sup_empty, degree_zero, le_refl]) $
assume a s has ih,
calc degree (sum (insert a s) f) β€ max (degree (f a)) (degree (s.sum f)) :
by rw sum_insert has; exact degree_add_le _ _
... β€ _ : by rw [sup_insert, with_bot.sup_eq_max]; exact max_le_max (le_refl _) ih
lemma degree_mul_le (p q : polynomial Ξ±) : degree (p * q) β€ degree p + degree q :=
calc degree (p * q) β€ (p.support).sup (Ξ»i, degree (sum q (Ξ»j a, C (coeff p i * a) * X ^ (i + j)))) :
by simp only [single_eq_C_mul_X.symm]; exact degree_sum_le _ _
... β€ p.support.sup (Ξ»i, q.support.sup (Ξ»j, degree (C (coeff p i * coeff q j) * X ^ (i + j)))) :
finset.sup_mono_fun (assume i hi, degree_sum_le _ _)
... β€ degree p + degree q :
begin
refine finset.sup_le (Ξ» a ha, finset.sup_le (Ξ» b hb, le_trans (degree_monomial_le _ _) _)),
rw [with_bot.coe_add],
rw mem_support_iff at ha hb,
exact add_le_add' (le_degree_of_ne_zero ha) (le_degree_of_ne_zero hb)
end
lemma degree_pow_le (p : polynomial Ξ±) : β n, degree (p ^ n) β€ add_monoid.smul n (degree p)
| 0 := by rw [pow_zero, add_monoid.zero_smul]; exact degree_one_le
| (n+1) := calc degree (p ^ (n + 1)) β€ degree p + degree (p ^ n) :
by rw pow_succ; exact degree_mul_le _ _
... β€ _ : by rw succ_smul; exact add_le_add' (le_refl _) (degree_pow_le _)
@[simp] lemma leading_coeff_monomial (a : Ξ±) (n : β) : leading_coeff (C a * X ^ n) = a :=
begin
by_cases ha : a = 0,
{ simp only [ha, C_0, zero_mul, leading_coeff_zero] },
{ rw [leading_coeff, nat_degree, degree_monomial _ ha, β single_eq_C_mul_X],
exact @finsupp.single_eq_same _ _ _ n a }
end
@[simp] lemma leading_coeff_C (a : Ξ±) : leading_coeff (C a) = a :=
suffices leading_coeff (C a * X^0) = a, by rwa [pow_zero, mul_one] at this,
leading_coeff_monomial a 0
@[simp] lemma leading_coeff_X : leading_coeff (X : polynomial Ξ±) = 1 :=
suffices leading_coeff (C (1:Ξ±) * X^1) = 1, by rwa [C_1, pow_one, one_mul] at this,
leading_coeff_monomial 1 1
@[simp] lemma monic_X : monic (X : polynomial Ξ±) := leading_coeff_X
@[simp] lemma leading_coeff_one : leading_coeff (1 : polynomial Ξ±) = 1 :=
suffices leading_coeff (C (1:Ξ±) * X^0) = 1, by rwa [C_1, pow_zero, mul_one] at this,
leading_coeff_monomial 1 0
@[simp] lemma monic_one : monic (1 : polynomial Ξ±) := leading_coeff_C _
lemma monic.ne_zero_of_zero_ne_one (h : (0:Ξ±) β 1) {p : polynomial Ξ±} (hp : p.monic) :
p β 0 :=
by { contrapose! h, rwa [h] at hp }
lemma monic.ne_zero {Ξ± : Type*} [nonzero_comm_ring Ξ±] {p : polynomial Ξ±} (hp : p.monic) :
p β 0 :=
hp.ne_zero_of_zero_ne_one $ zero_ne_one
lemma leading_coeff_add_of_degree_lt (h : degree p < degree q) :
leading_coeff (p + q) = leading_coeff q :=
have coeff p (nat_degree q) = 0, from coeff_nat_degree_eq_zero_of_degree_lt h,
by simp only [leading_coeff, nat_degree_eq_of_degree_eq (degree_add_eq_of_degree_lt h),
this, coeff_add, zero_add]
lemma leading_coeff_add_of_degree_eq (h : degree p = degree q)
(hlc : leading_coeff p + leading_coeff q β 0) :
leading_coeff (p + q) = leading_coeff p + leading_coeff q :=
have nat_degree (p + q) = nat_degree p,
by apply nat_degree_eq_of_degree_eq; rw [degree_add_eq_of_leading_coeff_add_ne_zero hlc, h, max_self],
by simp only [leading_coeff, this, nat_degree_eq_of_degree_eq h, coeff_add]
@[simp] lemma coeff_mul_degree_add_degree (p q : polynomial Ξ±) :
coeff (p * q) (nat_degree p + nat_degree q) = leading_coeff p * leading_coeff q :=
calc coeff (p * q) (nat_degree p + nat_degree q) =
(nat.antidiagonal (nat_degree p + nat_degree q)).sum
(Ξ» x, coeff p x.1 * coeff q x.2) : coeff_mul _ _ _
... = coeff p (nat_degree p) * coeff q (nat_degree q) :
begin
refine finset.sum_eq_single (nat_degree p, nat_degree q) _ _,
{ rintro β¨i,jβ© hβ hβ, rw nat.mem_antidiagonal at hβ,
by_cases H : nat_degree p < i,
{ rw [coeff_eq_zero_of_degree_lt
(lt_of_le_of_lt degree_le_nat_degree (with_bot.coe_lt_coe.2 H)), zero_mul] },
{ rw not_lt_iff_eq_or_lt at H, cases H,
{ subst H, rw add_left_cancel_iff at hβ, dsimp at hβ, subst hβ, exfalso, exact hβ rfl },
{ suffices : nat_degree q < j,
{ rw [coeff_eq_zero_of_degree_lt
(lt_of_le_of_lt degree_le_nat_degree (with_bot.coe_lt_coe.2 this)), mul_zero] },
{ by_contra H', rw not_lt at H',
exact ne_of_lt (nat.lt_of_lt_of_le
(nat.add_lt_add_right H j) (nat.add_le_add_left H' _)) hβ } } } },
{ intro H, exfalso, apply H, rw nat.mem_antidiagonal }
end
lemma degree_mul_eq' (h : leading_coeff p * leading_coeff q β 0) :
degree (p * q) = degree p + degree q :=
have hp : p β 0 := by refine mt _ h; exact Ξ» hp, by rw [hp, leading_coeff_zero, zero_mul],
have hq : q β 0 := by refine mt _ h; exact Ξ» hq, by rw [hq, leading_coeff_zero, mul_zero],
le_antisymm (degree_mul_le _ _)
begin
rw [degree_eq_nat_degree hp, degree_eq_nat_degree hq],
refine le_degree_of_ne_zero _,
rwa coeff_mul_degree_add_degree
end
lemma nat_degree_mul_eq' (h : leading_coeff p * leading_coeff q β 0) :
nat_degree (p * q) = nat_degree p + nat_degree q :=
have hp : p β 0 := mt leading_coeff_eq_zero.2 (Ξ» hβ, h $ by rw [hβ, zero_mul]),
have hq : q β 0 := mt leading_coeff_eq_zero.2 (Ξ» hβ, h $ by rw [hβ, mul_zero]),
have hpq : p * q β 0 := Ξ» hpq, by rw [β coeff_mul_degree_add_degree, hpq, coeff_zero] at h;
exact h rfl,
option.some_inj.1 (show (nat_degree (p * q) : with_bot β) = nat_degree p + nat_degree q,
by rw [β degree_eq_nat_degree hpq, degree_mul_eq' h, degree_eq_nat_degree hp, degree_eq_nat_degree hq])
lemma leading_coeff_mul' (h : leading_coeff p * leading_coeff q β 0) :
leading_coeff (p * q) = leading_coeff p * leading_coeff q :=
begin
unfold leading_coeff,
rw [nat_degree_mul_eq' h, coeff_mul_degree_add_degree],
refl
end
lemma leading_coeff_pow' : leading_coeff p ^ n β 0 β
leading_coeff (p ^ n) = leading_coeff p ^ n :=
nat.rec_on n (by simp) $
Ξ» n ih h,
have hβ : leading_coeff p ^ n β 0 :=
Ξ» hβ, h $ by rw [pow_succ, hβ, mul_zero],
have hβ : leading_coeff p * leading_coeff (p ^ n) β 0 :=
by rwa [pow_succ, β ih hβ] at h,
by rw [pow_succ, pow_succ, leading_coeff_mul' hβ, ih hβ]
lemma degree_pow_eq' : β {n}, leading_coeff p ^ n β 0 β
degree (p ^ n) = add_monoid.smul n (degree p)
| 0 := Ξ» h, by rw [pow_zero, β C_1] at *;
rw [degree_C h, add_monoid.zero_smul]
| (n+1) := Ξ» h,
have hβ : leading_coeff p ^ n β 0 := Ξ» hβ, h $
by rw [pow_succ, hβ, mul_zero],
have hβ : leading_coeff p * leading_coeff (p ^ n) β 0 :=
by rwa [pow_succ, β leading_coeff_pow' hβ] at h,
by rw [pow_succ, degree_mul_eq' hβ, succ_smul, degree_pow_eq' hβ]
lemma nat_degree_pow_eq' {n : β} (h : leading_coeff p ^ n β 0) :
nat_degree (p ^ n) = n * nat_degree p :=
if hp0 : p = 0 then
if hn0 : n = 0 then by simp *
else by rw [hp0, zero_pow (nat.pos_of_ne_zero hn0)]; simp
else
have hpn : p ^ n β 0, from Ξ» hpn0, have h1 : _ := h,
by rw [β leading_coeff_pow' h1, hpn0, leading_coeff_zero] at h;
exact h rfl,
option.some_inj.1 $ show (nat_degree (p ^ n) : with_bot β) = (n * nat_degree p : β),
by rw [β degree_eq_nat_degree hpn, degree_pow_eq' h, degree_eq_nat_degree hp0,
β with_bot.coe_smul]; simp
@[simp] lemma leading_coeff_X_pow : β n : β, leading_coeff ((X : polynomial Ξ±) ^ n) = 1
| 0 := by simp
| (n+1) :=
if h10 : (1 : Ξ±) = 0
then by rw [pow_succ, β one_mul X, β C_1, h10]; simp
else
have h : leading_coeff (X : polynomial Ξ±) * leading_coeff (X ^ n) β 0,
by rw [leading_coeff_X, leading_coeff_X_pow n, one_mul];
exact h10,
by rw [pow_succ, leading_coeff_mul' h, leading_coeff_X, leading_coeff_X_pow, one_mul]
lemma nat_degree_comp_le : nat_degree (p.comp q) β€ nat_degree p * nat_degree q :=
if h0 : p.comp q = 0 then by rw [h0, nat_degree_zero]; exact nat.zero_le _
else with_bot.coe_le_coe.1 $
calc β(nat_degree (p.comp q)) = degree (p.comp q) : (degree_eq_nat_degree h0).symm
... β€ _ : degree_sum_le _ _
... β€ _ : sup_le (Ξ» n hn,
calc degree (C (coeff p n) * q ^ n)
β€ degree (C (coeff p n)) + degree (q ^ n) : degree_mul_le _ _
... β€ nat_degree (C (coeff p n)) + add_monoid.smul n (degree q) :
add_le_add' degree_le_nat_degree (degree_pow_le _ _)
... β€ nat_degree (C (coeff p n)) + add_monoid.smul n (nat_degree q) :
add_le_add_left' (add_monoid.smul_le_smul_of_le_right
(@degree_le_nat_degree _ _ q) n)
... = (n * nat_degree q : β) :
by rw [nat_degree_C, with_bot.coe_zero, zero_add, β with_bot.coe_smul,
add_monoid.smul_eq_mul]; simp
... β€ (nat_degree p * nat_degree q : β) : with_bot.coe_le_coe.2 $
mul_le_mul_of_nonneg_right
(le_nat_degree_of_ne_zero (finsupp.mem_support_iff.1 hn))
(nat.zero_le _))
lemma degree_map_le [comm_semiring Ξ²] (f : Ξ± β Ξ²) [is_semiring_hom f] :
degree (p.map f) β€ degree p :=
if h : p.map f = 0 then by simp [h]
else begin
rw [degree_eq_nat_degree h],
refine le_degree_of_ne_zero (mt (congr_arg f) _),
rw [β coeff_map f, is_semiring_hom.map_zero f],
exact mt leading_coeff_eq_zero.1 h
end
lemma subsingleton_of_monic_zero (h : monic (0 : polynomial Ξ±)) :
(β p q : polynomial Ξ±, p = q) β§ (β a b : Ξ±, a = b) :=
by rw [monic.def, leading_coeff_zero] at h;
exact β¨Ξ» p q, by rw [β mul_one p, β mul_one q, β C_1, β h, C_0, mul_zero, mul_zero],
Ξ» a b, by rw [β mul_one a, β mul_one b, β h, mul_zero, mul_zero]β©
lemma degree_map_eq_of_leading_coeff_ne_zero [comm_semiring Ξ²] (f : Ξ± β Ξ²)
[is_semiring_hom f] (hf : f (leading_coeff p) β 0) : degree (p.map f) = degree p :=
le_antisymm (degree_map_le f) $
have hp0 : p β 0, from Ξ» hp0, by simpa [hp0, is_semiring_hom.map_zero f] using hf,
begin
rw [degree_eq_nat_degree hp0],
refine le_degree_of_ne_zero _,
rw [coeff_map], exact hf
end
lemma monic_map [comm_semiring Ξ²] (f : Ξ± β Ξ²)
[is_semiring_hom f] (hp : monic p) : monic (p.map f) :=
if h : (0 : Ξ²) = 1 then
by haveI := subsingleton_of_zero_eq_one Ξ² h;
exact subsingleton.elim _ _
else
have f (leading_coeff p) β 0,
by rwa [show _ = _, from hp, is_semiring_hom.map_one f, ne.def, eq_comm],
by erw [monic, leading_coeff, nat_degree_eq_of_degree_eq
(degree_map_eq_of_leading_coeff_ne_zero f this), coeff_map,
β leading_coeff, show _ = _, from hp, is_semiring_hom.map_one f]
lemma zero_le_degree_iff {p : polynomial Ξ±} : 0 β€ degree p β p β 0 :=
by rw [ne.def, β degree_eq_bot];
cases degree p; exact dec_trivial
@[simp] lemma coeff_mul_X_zero (p : polynomial Ξ±) : coeff (p * X) 0 = 0 :=
by rw [coeff_mul, nat.antidiagonal_zero];
simp only [polynomial.coeff_X_zero, finset.insert_empty_eq_singleton, finset.sum_singleton, mul_zero]
end comm_semiring
instance subsingleton [subsingleton Ξ±] [comm_semiring Ξ±] : subsingleton (polynomial Ξ±) :=
β¨Ξ» _ _, ext (Ξ» _, subsingleton.elim _ _)β©
section comm_semiring
variables [comm_semiring Ξ±] {p q r : polynomial Ξ±}
lemma ne_zero_of_monic_of_zero_ne_one (hp : monic p) (h : (0 : Ξ±) β 1) :
p β 0 := mt (congr_arg leading_coeff) $ by rw [monic.def.1 hp, leading_coeff_zero]; cc
lemma eq_X_add_C_of_degree_le_one (h : degree p β€ 1) :
p = C (p.coeff 1) * X + C (p.coeff 0) :=
ext (Ξ» n, nat.cases_on n (by simp)
(Ξ» n, nat.cases_on n (by simp [coeff_C])
(Ξ» m, have degree p < m.succ.succ, from lt_of_le_of_lt h dec_trivial,
by simp [coeff_eq_zero_of_degree_lt this, coeff_C, nat.succ_ne_zero, coeff_X,
nat.succ_inj', @eq_comm β 0])))
lemma eq_X_add_C_of_degree_eq_one (h : degree p = 1) :
p = C (p.leading_coeff) * X + C (p.coeff 0) :=
(eq_X_add_C_of_degree_le_one (show degree p β€ 1, from h βΈ le_refl _)).trans
(by simp [leading_coeff, nat_degree_eq_of_degree_eq_some h])
theorem degree_C_mul_X_pow_le (r : Ξ±) (n : β) : degree (C r * X^n) β€ n :=
begin
rw [β single_eq_C_mul_X],
refine finset.sup_le (Ξ» b hb, _),
rw list.eq_of_mem_singleton (finsupp.support_single_subset hb),
exact le_refl _
end
theorem degree_X_pow_le (n : β) : degree (X^n : polynomial Ξ±) β€ n :=
by simpa only [C_1, one_mul] using degree_C_mul_X_pow_le (1:Ξ±) n
theorem degree_X_le : degree (X : polynomial Ξ±) β€ 1 :=
by simpa only [C_1, one_mul, pow_one] using degree_C_mul_X_pow_le (1:Ξ±) 1
section injective
open function
variables [comm_semiring Ξ²] {f : Ξ± β Ξ²} [is_semiring_hom f] (hf : function.injective f)
include hf
lemma degree_map_eq_of_injective (p : polynomial Ξ±) : degree (p.map f) = degree p :=
if h : p = 0 then by simp [h]
else degree_map_eq_of_leading_coeff_ne_zero _
(by rw [β is_semiring_hom.map_zero f]; exact mt hf.eq_iff.1
(mt leading_coeff_eq_zero.1 h))
lemma degree_map' (p : polynomial Ξ±) :
degree (p.map f) = degree p :=
p.degree_map_eq_of_injective hf
lemma nat_degree_map' (p : polynomial Ξ±) :
nat_degree (p.map f) = nat_degree p :=
nat_degree_eq_of_degree_eq (degree_map' hf p)
lemma map_injective (p : polynomial Ξ±) :
injective (map f : polynomial Ξ± β polynomial Ξ²) :=
Ξ» p q h, ext $ Ξ» m, hf $
begin
rw ext_iff at h,
specialize h m,
rw [coeff_map f, coeff_map f] at h,
exact h
end
lemma leading_coeff_of_injective (p : polynomial Ξ±) :
leading_coeff (p.map f) = f (leading_coeff p) :=
begin
delta leading_coeff,
rw [coeff_map f, nat_degree_map' hf p]
end
lemma monic_of_injective {p : polynomial Ξ±} (hp : (p.map f).monic) : p.monic :=
begin
apply hf,
rw [β leading_coeff_of_injective hf, hp.leading_coeff, is_semiring_hom.map_one f]
end
end injective
theorem monic_of_degree_le (n : β) (H1 : degree p β€ n) (H2 : coeff p n = 1) : monic p :=
decidable.by_cases
(assume H : degree p < n, @subsingleton.elim _ (subsingleton_of_zero_eq_one Ξ± $
H2 βΈ (coeff_eq_zero_of_degree_lt H).symm) _ _)
(assume H : Β¬degree p < n, by rwa [monic, leading_coeff, nat_degree, (lt_or_eq_of_le H1).resolve_left H])
theorem monic_X_pow_add {n : β} (H : degree p β€ n) : monic (X ^ (n+1) + p) :=
have H1 : degree p < n+1, from lt_of_le_of_lt H (with_bot.coe_lt_coe.2 (nat.lt_succ_self n)),
monic_of_degree_le (n+1)
(le_trans (degree_add_le _ _) (max_le (degree_X_pow_le _) (le_of_lt H1)))
(by rw [coeff_add, coeff_X_pow, if_pos rfl, coeff_eq_zero_of_degree_lt H1, add_zero])
theorem monic_X_add_C (x : Ξ±) : monic (X + C x) :=
pow_one (X : polynomial Ξ±) βΈ monic_X_pow_add degree_C_le
theorem degree_le_iff_coeff_zero (f : polynomial Ξ±) (n : with_bot β) :
degree f β€ n β β m : β, n < m β coeff f m = 0 :=
β¨Ξ» (H : finset.sup (f.support) some β€ n) m (Hm : n < (m : with_bot β)), decidable.of_not_not $ Ξ» H4,
have H1 : m β f.support,
from Ξ» H2, not_lt_of_ge ((finset.sup_le_iff.1 H) m H2 : ((m : with_bot β) β€ n)) Hm,
H1 $ (finsupp.mem_support_to_fun f m).2 H4,
Ξ» H, finset.sup_le $ Ξ» b Hb, decidable.of_not_not $ Ξ» Hn,
(finsupp.mem_support_to_fun f b).1 Hb $ H b $ lt_of_not_ge Hnβ©
theorem nat_degree_le_of_degree_le {p : polynomial Ξ±} {n : β}
(H : degree p β€ n) : nat_degree p β€ n :=
show option.get_or_else (degree p) 0 β€ n, from match degree p, H with
| none, H := zero_le _
| (some d), H := with_bot.coe_le_coe.1 H
end
theorem leading_coeff_mul_X_pow {p : polynomial Ξ±} {n : β} :
leading_coeff (p * X ^ n) = leading_coeff p :=
decidable.by_cases
(assume H : leading_coeff p = 0, by rw [H, leading_coeff_eq_zero.1 H, zero_mul, leading_coeff_zero])
(assume H : leading_coeff p β 0,
by rw [leading_coeff_mul', leading_coeff_X_pow, mul_one];
rwa [leading_coeff_X_pow, mul_one])
lemma monic_mul (hp : monic p) (hq : monic q) : monic (p * q) :=
if h0 : (0 : Ξ±) = 1 then by haveI := subsingleton_of_zero_eq_one _ h0;
exact subsingleton.elim _ _
else
have leading_coeff p * leading_coeff q β 0, by simp [monic.def.1 hp, monic.def.1 hq, ne.symm h0],
by rw [monic.def, leading_coeff_mul' this, monic.def.1 hp, monic.def.1 hq, one_mul]
lemma monic_pow (hp : monic p) : β (n : β), monic (p ^ n)
| 0 := monic_one
| (n+1) := monic_mul hp (monic_pow n)
lemma multiplicity_finite_of_degree_pos_of_monic (hp : (0 : with_bot β) < degree p)
(hmp : monic p) (hq : q β 0) : multiplicity.finite p q :=
have zn0 : (0 : Ξ±) β 1, from Ξ» h, by haveI := subsingleton_of_zero_eq_one _ h;
exact hq (subsingleton.elim _ _),
β¨nat_degree q, Ξ» β¨r, hrβ©,
have hp0 : p β 0, from Ξ» hp0, by simp [hp0] at hp; contradiction,
have hr0 : r β 0, from Ξ» hr0, by simp * at *,
have hpn1 : leading_coeff p ^ (nat_degree q + 1) = 1,
by simp [show _ = _, from hmp],
have hpn0' : leading_coeff p ^ (nat_degree q + 1) β 0,
from hpn1.symm βΈ zn0.symm,
have hpnr0 : leading_coeff (p ^ (nat_degree q + 1)) * leading_coeff r β 0,
by simp only [leading_coeff_pow' hpn0', leading_coeff_eq_zero, hpn1,
one_pow, one_mul, ne.def, hr0]; simp,
have hpn0 : p ^ (nat_degree q + 1) β 0,
from mt leading_coeff_eq_zero.2 $
by rw [leading_coeff_pow' hpn0', show _ = _, from hmp, one_pow]; exact zn0.symm,
have hnp : 0 < nat_degree p,
by rw [β with_bot.coe_lt_coe, β degree_eq_nat_degree hp0];
exact hp,
begin
have := congr_arg nat_degree hr,
rw [nat_degree_mul_eq' hpnr0, nat_degree_pow_eq' hpn0', add_mul, add_assoc] at this,
exact ne_of_lt (lt_add_of_le_of_pos (le_mul_of_one_le_right' (nat.zero_le _) hnp)
(add_pos_of_pos_of_nonneg (by rwa one_mul) (nat.zero_le _))) this
endβ©
end comm_semiring
section nonzero_comm_semiring
variables [nonzero_comm_semiring Ξ±] {p q : polynomial Ξ±}
instance : nonzero_comm_semiring (polynomial Ξ±) :=
{ zero_ne_one := Ξ» (h : (0 : polynomial Ξ±) = 1),
@zero_ne_one Ξ± _ $
calc (0 : Ξ±) = eval 0 0 : eval_zero.symm
... = eval 0 1 : congr_arg _ h
... = 1 : eval_C,
..polynomial.comm_semiring }
@[simp] lemma degree_one : degree (1 : polynomial Ξ±) = (0 : with_bot β) :=
degree_C (show (1 : Ξ±) β 0, from zero_ne_one.symm)
@[simp] lemma degree_X : degree (X : polynomial Ξ±) = 1 :=
begin
unfold X degree single finsupp.support,
rw if_neg (zero_ne_one).symm,
refl
end
lemma X_ne_zero : (X : polynomial Ξ±) β 0 :=
mt (congr_arg (Ξ» p, coeff p 1)) (by simp)
@[simp] lemma degree_X_pow : β (n : β), degree ((X : polynomial Ξ±) ^ n) = n
| 0 := by simp only [pow_zero, degree_one]; refl
| (n+1) :=
have h : leading_coeff (X : polynomial Ξ±) * leading_coeff (X ^ n) β 0,
by rw [leading_coeff_X, leading_coeff_X_pow n, one_mul];
exact zero_ne_one.symm,
by rw [pow_succ, degree_mul_eq' h, degree_X, degree_X_pow, add_comm]; refl
@[simp] lemma not_monic_zero : Β¬monic (0 : polynomial Ξ±) :=
by simpa only [monic, leading_coeff_zero] using zero_ne_one
lemma ne_zero_of_monic (h : monic p) : p β 0 :=
Ξ» hβ, @not_monic_zero Ξ± _ (hβ βΈ h)
end nonzero_comm_semiring
section comm_semiring
variables [comm_semiring Ξ±] {p q : polynomial Ξ±}
/-- `dix_X p` return a polynomial `q` such that `q * X + C (p.coeff 0) = p`.
It can be used in a semiring where the usual division algorithm is not possible -/
def div_X (p : polynomial Ξ±) : polynomial Ξ± :=
{ to_fun := Ξ» n, p.coeff (n + 1),
support := β¨(p.support.filter (> 0)).1.map (Ξ» n, n - 1),
multiset.nodup_map_on begin
simp only [finset.mem_def.symm, finset.mem_erase, finset.mem_filter],
assume x hx y hy hxy,
rwa [β @add_right_cancel_iff _ _ 1, nat.sub_add_cancel hx.2,
nat.sub_add_cancel hy.2] at hxy
end
(p.support.filter (> 0)).2β©,
mem_support_to_fun := Ξ» n,
suffices (β (a : β), (Β¬coeff p a = 0 β§ a > 0) β§ a - 1 = n) β
Β¬coeff p (n + 1) = 0,
by simpa [finset.mem_def.symm, apply_eq_coeff],
β¨Ξ» β¨a, haβ©, by rw [β ha.2, nat.sub_add_cancel ha.1.2]; exact ha.1.1,
Ξ» h, β¨n + 1, β¨h, nat.succ_pos _β©, nat.succ_sub_one _β©β© }
lemma div_X_mul_X_add (p : polynomial Ξ±) : div_X p * X + C (p.coeff 0) = p :=
ext $ Ξ» n,
nat.cases_on n
(by simp)
(by simp [coeff_C, nat.succ_ne_zero, coeff_mul_X, div_X])
@[simp] lemma div_X_C (a : Ξ±) : div_X (C a) = 0 :=
ext $ Ξ» n, by cases n; simp [div_X, coeff_C]; simp [coeff]
lemma div_X_eq_zero_iff : div_X p = 0 β p = C (p.coeff 0) :=
β¨Ξ» h, by simpa [eq_comm, h] using div_X_mul_X_add p,
Ξ» h, by rw [h, div_X_C]β©
lemma div_X_add : div_X (p + q) = div_X p + div_X q :=
ext $ by simp [div_X]
def nonzero_comm_semiring.of_polynomial_ne (h : p β q) : nonzero_comm_semiring Ξ± :=
{ zero_ne_one := Ξ» h01 : 0 = 1, h $
by rw [β mul_one p, β mul_one q, β C_1, β h01, C_0, mul_zero, mul_zero],
..show comm_semiring Ξ±, by apply_instance }
lemma degree_lt_degree_mul_X (hp : p β 0) : p.degree < (p * X).degree :=
by letI := nonzero_comm_semiring.of_polynomial_ne hp; exact
have leading_coeff p * leading_coeff X β 0, by simpa,
by erw [degree_mul_eq' this, degree_eq_nat_degree hp,
degree_X, β with_bot.coe_one, β with_bot.coe_add, with_bot.coe_lt_coe];
exact nat.lt_succ_self _
lemma degree_div_X_lt (hp0 : p β 0) : (div_X p).degree < p.degree :=
by letI := nonzero_comm_semiring.of_polynomial_ne hp0; exact
calc (div_X p).degree < (div_X p * X + C (p.coeff 0)).degree :
if h : degree p β€ 0
then begin
have h' : C (p.coeff 0) β 0, by rwa [β eq_C_of_degree_le_zero h],
rw [eq_C_of_degree_le_zero h, div_X_C, degree_zero, zero_mul, zero_add],
exact lt_of_le_of_ne lattice.bot_le (ne.symm (mt degree_eq_bot.1 $
by simp [h'])),
end
else
have hXp0 : div_X p β 0,
by simpa [div_X_eq_zero_iff, -not_le, degree_le_zero_iff] using h,
have leading_coeff (div_X p) * leading_coeff X β 0, by simpa,
have degree (C (p.coeff 0)) < degree (div_X p * X),
from calc degree (C (p.coeff 0)) β€ 0 : degree_C_le
... < 1 : dec_trivial
... = degree (X : polynomial Ξ±) : degree_X.symm
... β€ degree (div_X p * X) :
by rw [β zero_add (degree X), degree_mul_eq' this];
exact add_le_add'
(by rw [zero_le_degree_iff, ne.def, div_X_eq_zero_iff];
exact Ξ» h0, h (h0.symm βΈ degree_C_le))
(le_refl _),
by rw [add_comm, degree_add_eq_of_degree_lt this];
exact degree_lt_degree_mul_X hXp0
... = p.degree : by rw div_X_mul_X_add
@[elab_as_eliminator] noncomputable def rec_on_horner
{M : polynomial Ξ± β Sort*} : Ξ (p : polynomial Ξ±),
M 0 β
(Ξ p a, coeff p 0 = 0 β a β 0 β M p β M (p + C a)) β
(Ξ p, p β 0 β M p β M (p * X)) β
M p
| p := Ξ» M0 MC MX,
if hp : p = 0 then eq.rec_on hp.symm M0
else
have wf : degree (div_X p) < degree p,
from degree_div_X_lt hp,
by rw [β div_X_mul_X_add p] at *;
exact
if hcp0 : coeff p 0 = 0
then by rw [hcp0, C_0, add_zero];
exact MX _ (Ξ» h : div_X p = 0, by simpa [h, hcp0] using hp)
(rec_on_horner _ M0 MC MX)
else MC _ _ (coeff_mul_X_zero _) hcp0 (if hpX0 : div_X p = 0
then show M (div_X p * X), by rw [hpX0, zero_mul]; exact M0
else MX (div_X p) hpX0 (rec_on_horner _ M0 MC MX))
using_well_founded {dec_tac := tactic.assumption}
@[elab_as_eliminator] lemma degree_pos_induction_on
{P : polynomial Ξ± β Prop} (p : polynomial Ξ±) (h0 : 0 < degree p)
(hC : β {a}, a β 0 β P (C a * X))
(hX : β {p}, 0 < degree p β P p β P (p * X))
(hadd : β {p} {a}, 0 < degree p β P p β P (p + C a)) : P p :=
rec_on_horner p
(Ξ» h, by rw degree_zero at h; exact absurd h dec_trivial)
(Ξ» p a _ _ ih h0,
have 0 < degree p,
from lt_of_not_ge (Ξ» h, (not_lt_of_ge degree_C_le) $
by rwa [eq_C_of_degree_le_zero h, β C_add] at h0),
hadd this (ih this))
(Ξ» p _ ih h0',
if h0 : 0 < degree p
then hX h0 (ih h0)
else by rw [eq_C_of_degree_le_zero (le_of_not_gt h0)] at *;
exact hC (Ξ» h : coeff p 0 = 0,
by simpa [h, not_lt.2 (@lattice.bot_le ( β) _ _)] using h0'))
h0
end comm_semiring
section comm_ring
variables [comm_ring Ξ±] {p q : polynomial Ξ±}
instance : comm_ring (polynomial Ξ±) := finsupp.comm_ring
instance : has_scalar Ξ± (polynomial Ξ±) := finsupp.has_scalar
-- TODO if this becomes a semimodule then the below lemma could be proved for semimodules
instance : module Ξ± (polynomial Ξ±) := finsupp.module β Ξ±
-- TODO -- this is OK for semimodules
@[simp] lemma coeff_smul (p : polynomial Ξ±) (r : Ξ±) (n : β) :
coeff (r β’ p) n = r * coeff p n := finsupp.smul_apply
-- TODO -- this is OK for semimodules
lemma C_mul' (a : Ξ±) (f : polynomial Ξ±) : C a * f = a β’ f :=
ext $ Ξ» n, coeff_C_mul f
variable (Ξ±)
def lcoeff (n : β) : polynomial Ξ± ββ Ξ± :=
{ to_fun := Ξ» f, coeff f n,
add := Ξ» f g, coeff_add f g n,
smul := Ξ» r p, coeff_smul p r n }
variable {Ξ±}
@[simp] lemma lcoeff_apply (n : β) (f : polynomial Ξ±) : lcoeff Ξ± n f = coeff f n := rfl
instance C.is_ring_hom : is_ring_hom (@C Ξ± _) := by apply is_ring_hom.of_semiring
lemma int_cast_eq_C (n : β€) : (n : polynomial Ξ±) = C n :=
congr_fun (int.eq_cast' _).symm n
@[simp] lemma C_neg : C (-a) = -C a := is_ring_hom.map_neg C
@[simp] lemma C_sub : C (a - b) = C a - C b := is_ring_hom.map_sub C
instance evalβ.is_ring_hom {Ξ²} [comm_ring Ξ²]
(f : Ξ± β Ξ²) [is_ring_hom f] {x : Ξ²} : is_ring_hom (evalβ f x) :=
by apply is_ring_hom.of_semiring
instance eval.is_ring_hom {x : Ξ±} : is_ring_hom (eval x) := evalβ.is_ring_hom _
instance map.is_ring_hom {Ξ²} [comm_ring Ξ²]
(f : Ξ± β Ξ²) [is_ring_hom f] : is_ring_hom (map f) :=
evalβ.is_ring_hom (C β f)
@[simp] lemma map_sub {Ξ²} [comm_ring Ξ²]
(f : Ξ± β Ξ²) [is_ring_hom f] : (p - q).map f = p.map f - q.map f := is_ring_hom.map_sub _
@[simp] lemma map_neg {Ξ²} [comm_ring Ξ²]
(f : Ξ± β Ξ²) [is_ring_hom f] : (-p).map f = -(p.map f) := is_ring_hom.map_neg _
@[simp] lemma degree_neg (p : polynomial Ξ±) : degree (-p) = degree p :=
by unfold degree; rw support_neg
@[simp] lemma nat_degree_neg (p : polynomial Ξ±) : nat_degree (-p) = nat_degree p :=
by simp [nat_degree]
@[simp] lemma nat_degree_int_cast (n : β€) : nat_degree (n : polynomial Ξ±) = 0 :=
by simp [int_cast_eq_C]
@[simp] lemma coeff_neg (p : polynomial Ξ±) (n : β) : coeff (-p) n = -coeff p n := rfl
@[simp] lemma coeff_sub (p q : polynomial Ξ±) (n : β) : coeff (p - q) n = coeff p n - coeff q n := rfl
@[simp] lemma evalβ_neg {Ξ²} [comm_ring Ξ²] (f : Ξ± β Ξ²) [is_ring_hom f] {x : Ξ²} :
(-p).evalβ f x = -p.evalβ f x :=
is_ring_hom.map_neg _
@[simp] lemma evalβ_sub {Ξ²} [comm_ring Ξ²] (f : Ξ± β Ξ²) [is_ring_hom f] {x : Ξ²} :
(p - q).evalβ f x = p.evalβ f x - q.evalβ f x :=
is_ring_hom.map_sub _
@[simp] lemma eval_neg (p : polynomial Ξ±) (x : Ξ±) : (-p).eval x = -p.eval x :=
is_ring_hom.map_neg _
@[simp] lemma eval_sub (p q : polynomial Ξ±) (x : Ξ±) : (p - q).eval x = p.eval x - q.eval x :=
is_ring_hom.map_sub _
lemma degree_sub_lt (hd : degree p = degree q)
(hp0 : p β 0) (hlc : leading_coeff p = leading_coeff q) :
degree (p - q) < degree p :=
have hp : single (nat_degree p) (leading_coeff p) + p.erase (nat_degree p) = p :=
finsupp.single_add_erase,
have hq : single (nat_degree q) (leading_coeff q) + q.erase (nat_degree q) = q :=
finsupp.single_add_erase,
have hd' : nat_degree p = nat_degree q := by unfold nat_degree; rw hd,
have hq0 : q β 0 := mt degree_eq_bot.2 (hd βΈ mt degree_eq_bot.1 hp0),
calc degree (p - q) = degree (erase (nat_degree q) p + -erase (nat_degree q) q) :
by conv {to_lhs, rw [β hp, β hq, hlc, hd', add_sub_add_left_eq_sub, sub_eq_add_neg]}
... β€ max (degree (erase (nat_degree q) p)) (degree (erase (nat_degree q) q))
: degree_neg (erase (nat_degree q) q) βΈ degree_add_le _ _
... < degree p : max_lt_iff.2 β¨hd' βΈ degree_erase_lt hp0, hd.symm βΈ degree_erase_lt hq0β©
lemma ne_zero_of_ne_zero_of_monic (hp : p β 0) (hq : monic q) : q β 0
| h := begin
rw [h, monic.def, leading_coeff_zero] at hq,
rw [β mul_one p, β C_1, β hq, C_0, mul_zero] at hp,
exact hp rfl
end
lemma div_wf_lemma (h : degree q β€ degree p β§ p β 0) (hq : monic q) :
degree (p - C (leading_coeff p) * X ^ (nat_degree p - nat_degree q) * q) < degree p :=
have hp : leading_coeff p β 0 := mt leading_coeff_eq_zero.1 h.2,
have hpq : leading_coeff (C (leading_coeff p) * X ^ (nat_degree p - nat_degree q)) *
leading_coeff q β 0,
by rwa [leading_coeff_monomial, monic.def.1 hq, mul_one],
if h0 : p - C (leading_coeff p) * X ^ (nat_degree p - nat_degree q) * q = 0
then h0.symm βΈ (lt_of_not_ge $ mt le_bot_iff.1 (mt degree_eq_bot.1 h.2))
else
have hq0 : q β 0 := ne_zero_of_ne_zero_of_monic h.2 hq,
have hlt : nat_degree q β€ nat_degree p := with_bot.coe_le_coe.1
(by rw [β degree_eq_nat_degree h.2, β degree_eq_nat_degree hq0];
exact h.1),
degree_sub_lt
(by rw [degree_mul_eq' hpq, degree_monomial _ hp, degree_eq_nat_degree h.2,
degree_eq_nat_degree hq0, β with_bot.coe_add, nat.sub_add_cancel hlt])
h.2
(by rw [leading_coeff_mul' hpq, leading_coeff_monomial, monic.def.1 hq, mul_one])
noncomputable def div_mod_by_monic_aux : Ξ (p : polynomial Ξ±) {q : polynomial Ξ±},
monic q β polynomial Ξ± Γ polynomial Ξ±
| p := Ξ» q hq, if h : degree q β€ degree p β§ p β 0 then
let z := C (leading_coeff p) * X^(nat_degree p - nat_degree q) in
have wf : _ := div_wf_lemma h hq,
let dm := div_mod_by_monic_aux (p - z * q) hq in
β¨z + dm.1, dm.2β©
else β¨0, pβ©
using_well_founded {dec_tac := tactic.assumption}
/-- `div_by_monic` gives the quotient of `p` by a monic polynomial `q`. -/
def div_by_monic (p q : polynomial Ξ±) : polynomial Ξ± :=
if hq : monic q then (div_mod_by_monic_aux p hq).1 else 0
/-- `mod_by_monic` gives the remainder of `p` by a monic polynomial `q`. -/
def mod_by_monic (p q : polynomial Ξ±) : polynomial Ξ± :=
if hq : monic q then (div_mod_by_monic_aux p hq).2 else p
infixl ` /β ` : 70 := div_by_monic
infixl ` %β ` : 70 := mod_by_monic
lemma degree_mod_by_monic_lt : β (p : polynomial Ξ±) {q : polynomial Ξ±} (hq : monic q)
(hq0 : q β 0), degree (p %β q) < degree q
| p := Ξ» q hq hq0,
if h : degree q β€ degree p β§ p β 0 then
have wf : _ := div_wf_lemma β¨h.1, h.2β© hq,
have degree ((p - C (leading_coeff p) * X ^ (nat_degree p - nat_degree q) * q) %β q) < degree q :=
degree_mod_by_monic_lt (p - C (leading_coeff p) * X ^ (nat_degree p - nat_degree q) * q)
hq hq0,
begin
unfold mod_by_monic at this β’,
unfold div_mod_by_monic_aux,
rw dif_pos hq at this β’,
rw if_pos h,
exact this
end
else
or.cases_on (not_and_distrib.1 h) begin
unfold mod_by_monic div_mod_by_monic_aux,
rw [dif_pos hq, if_neg h],
exact lt_of_not_ge,
end
begin
assume hp,
unfold mod_by_monic div_mod_by_monic_aux,
rw [dif_pos hq, if_neg h, not_not.1 hp],
exact lt_of_le_of_ne bot_le
(ne.symm (mt degree_eq_bot.1 hq0)),
end
using_well_founded {dec_tac := tactic.assumption}
lemma mod_by_monic_eq_sub_mul_div : β (p : polynomial Ξ±) {q : polynomial Ξ±} (hq : monic q),
p %β q = p - q * (p /β q)
| p := Ξ» q hq,
if h : degree q β€ degree p β§ p β 0 then
have wf : _ := div_wf_lemma h hq,
have ih : _ := mod_by_monic_eq_sub_mul_div
(p - C (leading_coeff p) * X ^ (nat_degree p - nat_degree q) * q) hq,
begin
unfold mod_by_monic div_by_monic div_mod_by_monic_aux,
rw [dif_pos hq, if_pos h],
rw [mod_by_monic, dif_pos hq] at ih,
refine ih.trans _,
unfold div_by_monic,
rw [dif_pos hq, dif_pos hq, if_pos h, mul_add, sub_add_eq_sub_sub, mul_comm]
end
else
begin
unfold mod_by_monic div_by_monic div_mod_by_monic_aux,
rw [dif_pos hq, if_neg h, dif_pos hq, if_neg h, mul_zero, sub_zero]
end
using_well_founded {dec_tac := tactic.assumption}
lemma mod_by_monic_add_div (p : polynomial Ξ±) {q : polynomial Ξ±} (hq : monic q) :
p %β q + q * (p /β q) = p := eq_sub_iff_add_eq.1 (mod_by_monic_eq_sub_mul_div p hq)
@[simp] lemma zero_mod_by_monic (p : polynomial Ξ±) : 0 %β p = 0 :=
begin
unfold mod_by_monic div_mod_by_monic_aux,
by_cases hp : monic p,
{ rw [dif_pos hp, if_neg (mt and.right (not_not_intro rfl))] },
{ rw [dif_neg hp] }
end
@[simp] lemma zero_div_by_monic (p : polynomial Ξ±) : 0 /β p = 0 :=
begin
unfold div_by_monic div_mod_by_monic_aux,
by_cases hp : monic p,
{ rw [dif_pos hp, if_neg (mt and.right (not_not_intro rfl))] },
{ rw [dif_neg hp] }
end
@[simp] lemma mod_by_monic_zero (p : polynomial Ξ±) : p %β 0 = p :=
if h : monic (0 : polynomial Ξ±) then (subsingleton_of_monic_zero h).1 _ _ else
by unfold mod_by_monic div_mod_by_monic_aux; rw dif_neg h
@[simp] lemma div_by_monic_zero (p : polynomial Ξ±) : p /β 0 = 0 :=
if h : monic (0 : polynomial Ξ±) then (subsingleton_of_monic_zero h).1 _ _ else
by unfold div_by_monic div_mod_by_monic_aux; rw dif_neg h
lemma div_by_monic_eq_of_not_monic (p : polynomial Ξ±) (hq : Β¬monic q) : p /β q = 0 := dif_neg hq
lemma mod_by_monic_eq_of_not_monic (p : polynomial Ξ±) (hq : Β¬monic q) : p %β q = p := dif_neg hq
lemma mod_by_monic_eq_self_iff (hq : monic q) (hq0 : q β 0) : p %β q = p β degree p < degree q :=
β¨Ξ» h, h βΈ degree_mod_by_monic_lt _ hq hq0,
Ξ» h, have Β¬ degree q β€ degree p := not_le_of_gt h,
by unfold mod_by_monic div_mod_by_monic_aux; rw [dif_pos hq, if_neg (mt and.left this)]β©
lemma div_by_monic_eq_zero_iff (hq : monic q) (hq0 : q β 0) : p /β q = 0 β degree p < degree q :=
β¨Ξ» h, by have := mod_by_monic_add_div p hq;
rwa [h, mul_zero, add_zero, mod_by_monic_eq_self_iff hq hq0] at this,
Ξ» h, have Β¬ degree q β€ degree p := not_le_of_gt h,
by unfold div_by_monic div_mod_by_monic_aux; rw [dif_pos hq, if_neg (mt and.left this)]β©
lemma degree_add_div_by_monic (hq : monic q) (h : degree q β€ degree p) :
degree q + degree (p /β q) = degree p :=
if hq0 : q = 0 then
have β (p : polynomial Ξ±), p = 0,
from Ξ» p, (@subsingleton_of_monic_zero Ξ± _ (hq0 βΈ hq)).1 _ _,
by rw [this (p /β q), this p, this q]; refl
else
have hdiv0 : p /β q β 0 := by rwa [(β ), div_by_monic_eq_zero_iff hq hq0, not_lt],
have hlc : leading_coeff q * leading_coeff (p /β q) β 0 :=
by rwa [monic.def.1 hq, one_mul, (β ), leading_coeff_eq_zero],
have hmod : degree (p %β q) < degree (q * (p /β q)) :=
calc degree (p %β q) < degree q : degree_mod_by_monic_lt _ hq hq0
... β€ _ : by rw [degree_mul_eq' hlc, degree_eq_nat_degree hq0,
degree_eq_nat_degree hdiv0, β with_bot.coe_add, with_bot.coe_le_coe];
exact nat.le_add_right _ _,
calc degree q + degree (p /β q) = degree (q * (p /β q)) : eq.symm (degree_mul_eq' hlc)
... = degree (p %β q + q * (p /β q)) : (degree_add_eq_of_degree_lt hmod).symm
... = _ : congr_arg _ (mod_by_monic_add_div _ hq)
lemma degree_div_by_monic_le (p q : polynomial Ξ±) : degree (p /β q) β€ degree p :=
if hp0 : p = 0 then by simp only [hp0, zero_div_by_monic, le_refl]
else if hq : monic q then
have hq0 : q β 0 := ne_zero_of_ne_zero_of_monic hp0 hq,
if h : degree q β€ degree p
then by rw [β degree_add_div_by_monic hq h, degree_eq_nat_degree hq0,
degree_eq_nat_degree (mt (div_by_monic_eq_zero_iff hq hq0).1 (not_lt.2 h))];
exact with_bot.coe_le_coe.2 (nat.le_add_left _ _)
else
by unfold div_by_monic div_mod_by_monic_aux;
simp only [dif_pos hq, h, false_and, if_false, degree_zero, bot_le]
else (div_by_monic_eq_of_not_monic p hq).symm βΈ bot_le
lemma degree_div_by_monic_lt (p : polynomial Ξ±) {q : polynomial Ξ±} (hq : monic q)
(hp0 : p β 0) (h0q : 0 < degree q) : degree (p /β q) < degree p :=
have hq0 : q β 0 := ne_zero_of_ne_zero_of_monic hp0 hq,
if hpq : degree p < degree q
then begin
rw [(div_by_monic_eq_zero_iff hq hq0).2 hpq, degree_eq_nat_degree hp0],
exact with_bot.bot_lt_some _
end
else begin
rw [β degree_add_div_by_monic hq (not_lt.1 hpq), degree_eq_nat_degree hq0,
degree_eq_nat_degree (mt (div_by_monic_eq_zero_iff hq hq0).1 hpq)],
exact with_bot.coe_lt_coe.2 (nat.lt_add_of_pos_left
(with_bot.coe_lt_coe.1 $ (degree_eq_nat_degree hq0) βΈ h0q))
end
lemma div_mod_by_monic_unique {f g} (q r : polynomial Ξ±) (hg : monic g)
(h : r + g * q = f β§ degree r < degree g) : f /β g = q β§ f %β g = r :=
if hg0 : g = 0 then by split; exact (subsingleton_of_monic_zero
(hg0 βΈ hg : monic (0 : polynomial Ξ±))).1 _ _
else
have hβ : r - f %β g = -g * (q - f /β g),
from eq_of_sub_eq_zero
(by rw [β sub_eq_zero_of_eq (h.1.trans (mod_by_monic_add_div f hg).symm)];
simp [mul_add, mul_comm]),
have hβ : degree (r - f %β g) = degree (g * (q - f /β g)),
by simp [hβ],
have hβ : degree (r - f %β g) < degree g,
from calc degree (r - f %β g) β€ max (degree r) (degree (-(f %β g))) :
degree_add_le _ _
... < degree g : max_lt_iff.2 β¨h.2, by rw degree_neg; exact degree_mod_by_monic_lt _ hg hg0β©,
have hβ
: q - (f /β g) = 0,
from by_contradiction
(Ξ» hqf, not_le_of_gt hβ $
calc degree g β€ degree g + degree (q - f /β g) :
by erw [degree_eq_nat_degree hg0, degree_eq_nat_degree hqf,
with_bot.coe_le_coe];
exact nat.le_add_right _ _
... = degree (r - f %β g) :
by rw [hβ, degree_mul_eq']; simpa [monic.def.1 hg]),
β¨eq.symm $ eq_of_sub_eq_zero hβ
,
eq.symm $ eq_of_sub_eq_zero $ by simpa [hβ
] using hββ©
lemma map_mod_div_by_monic [comm_ring Ξ²] (f : Ξ± β Ξ²) [is_semiring_hom f] (hq : monic q) :
(p /β q).map f = p.map f /β q.map f β§ (p %β q).map f = p.map f %β q.map f :=
if h01 : (0 : Ξ²) = 1 then by haveI := subsingleton_of_zero_eq_one Ξ² h01;
exact β¨subsingleton.elim _ _, subsingleton.elim _ _β©
else
have h01Ξ± : (0 : Ξ±) β 1, from mt (congr_arg f)
(by rwa [is_semiring_hom.map_one f, is_semiring_hom.map_zero f]),
have map f p /β map f q = map f (p /β q) β§ map f p %β map f q = map f (p %β q),
from (div_mod_by_monic_unique ((p /β q).map f) _ (monic_map f hq)
β¨eq.symm $ by rw [β map_mul, β map_add, mod_by_monic_add_div _ hq],
calc _ β€ degree (p %β q) : degree_map_le _
... < degree q : degree_mod_by_monic_lt _ hq
$ (ne_zero_of_monic_of_zero_ne_one hq h01Ξ±)
... = _ : eq.symm $ degree_map_eq_of_leading_coeff_ne_zero _
(by rw [monic.def.1 hq, is_semiring_hom.map_one f]; exact ne.symm h01)β©),
β¨this.1.symm, this.2.symmβ©
lemma map_div_by_monic [comm_ring Ξ²] (f : Ξ± β Ξ²) [is_semiring_hom f] (hq : monic q) :
(p /β q).map f = p.map f /β q.map f :=
(map_mod_div_by_monic f hq).1
lemma map_mod_by_monic [comm_ring Ξ²] (f : Ξ± β Ξ²) [is_semiring_hom f] (hq : monic q) :
(p %β q).map f = p.map f %β q.map f :=
(map_mod_div_by_monic f hq).2
lemma dvd_iff_mod_by_monic_eq_zero (hq : monic q) : p %β q = 0 β q β£ p :=
β¨Ξ» h, by rw [β mod_by_monic_add_div p hq, h, zero_add];
exact dvd_mul_right _ _,
Ξ» h, if hq0 : q = 0 then by rw hq0 at hq;
exact (subsingleton_of_monic_zero hq).1 _ _
else
let β¨r, hrβ© := exists_eq_mul_right_of_dvd h in
by_contradiction (Ξ» hpq0,
have hmod : p %β q = q * (r - p /β q) :=
by rw [mod_by_monic_eq_sub_mul_div _ hq, mul_sub, β hr],
have degree (q * (r - p /β q)) < degree q :=
hmod βΈ degree_mod_by_monic_lt _ hq hq0,
have hrpq0 : leading_coeff (r - p /β q) β 0 :=
Ξ» h, hpq0 $ leading_coeff_eq_zero.1
(by rw [hmod, leading_coeff_eq_zero.1 h, mul_zero, leading_coeff_zero]),
have hlc : leading_coeff q * leading_coeff (r - p /β q) β 0 :=
by rwa [monic.def.1 hq, one_mul],
by rw [degree_mul_eq' hlc, degree_eq_nat_degree hq0,
degree_eq_nat_degree (mt leading_coeff_eq_zero.2 hrpq0)] at this;
exact not_lt_of_ge (nat.le_add_right _ _) (with_bot.some_lt_some.1 this))β©
@[simp] lemma mod_by_monic_one (p : polynomial Ξ±) : p %β 1 = 0 :=
(dvd_iff_mod_by_monic_eq_zero monic_one).2 (one_dvd _)
@[simp] lemma div_by_monic_one (p : polynomial Ξ±) : p /β 1 = p :=
by conv_rhs { rw [β mod_by_monic_add_div p monic_one] }; simp
lemma degree_pos_of_root (hp : p β 0) (h : is_root p a) : 0 < degree p :=
lt_of_not_ge $ Ξ» hlt, begin
have := eq_C_of_degree_le_zero hlt,
rw [is_root, this, eval_C] at h,
exact hp (finsupp.ext (Ξ» n, show coeff p n = 0, from
nat.cases_on n h (Ξ» _, coeff_eq_zero_of_degree_lt (lt_of_le_of_lt hlt
(with_bot.coe_lt_coe.2 (nat.succ_pos _)))))),
end
theorem monic_X_sub_C (x : Ξ±) : monic (X - C x) :=
by simpa only [C_neg] using monic_X_add_C (-x)
theorem monic_X_pow_sub {n : β} (H : degree p β€ n) : monic (X ^ (n+1) - p) :=
monic_X_pow_add ((degree_neg p).symm βΈ H)
theorem degree_mod_by_monic_le (p : polynomial Ξ±) {q : polynomial Ξ±}
(hq : monic q) : degree (p %β q) β€ degree q :=
decidable.by_cases
(assume H : q = 0, by rw [monic, H, leading_coeff_zero] at hq;
have : (0:polynomial Ξ±) = 1 := (by rw [β C_0, β C_1, hq]);
rw [eq_zero_of_zero_eq_one _ this (p %β q), eq_zero_of_zero_eq_one _ this q]; exact le_refl _)
(assume H : q β 0, le_of_lt $ degree_mod_by_monic_lt _ hq H)
lemma root_X_sub_C : is_root (X - C a) b β a = b :=
by rw [is_root.def, eval_sub, eval_X, eval_C, sub_eq_zero_iff_eq, eq_comm]
def nonzero_comm_ring.of_polynomial_ne (h : p β q) : nonzero_comm_ring Ξ± :=
{ zero := 0,
one := 1,
zero_ne_one := Ξ» h01, h $
by rw [β one_mul p, β one_mul q, β C_1, β h01, C_0, zero_mul, zero_mul],
..show comm_ring Ξ±, by apply_instance }
end comm_ring
section nonzero_comm_ring
variables [nonzero_comm_ring Ξ±] {p q : polynomial Ξ±}
instance : nonzero_comm_ring (polynomial Ξ±) :=
{ ..polynomial.nonzero_comm_semiring,
..polynomial.comm_ring }
@[simp] lemma degree_X_sub_C (a : Ξ±) : degree (X - C a) = 1 :=
begin
rw [sub_eq_add_neg, add_comm, β @degree_X Ξ±],
by_cases ha : a = 0,
{ simp only [ha, C_0, neg_zero, zero_add] },
exact degree_add_eq_of_degree_lt (by rw [degree_X, degree_neg, degree_C ha]; exact dec_trivial)
end
lemma degree_X_pow_sub_C {n : β} (hn : 0 < n) (a : Ξ±) :
degree ((X : polynomial Ξ±) ^ n - C a) = n :=
have degree (-C a) < degree ((X : polynomial Ξ±) ^ n),
from calc degree (-C a) β€ 0 : by rw degree_neg; exact degree_C_le
... < degree ((X : polynomial Ξ±) ^ n) : by rwa [degree_X_pow];
exact with_bot.coe_lt_coe.2 hn,
by rw [sub_eq_add_neg, add_comm, degree_add_eq_of_degree_lt this, degree_X_pow]
lemma X_pow_sub_C_ne_zero {n : β} (hn : 0 < n) (a : Ξ±) :
(X : polynomial Ξ±) ^ n - C a β 0 :=
mt degree_eq_bot.2 (show degree ((X : polynomial Ξ±) ^ n - C a) β β₯,
by rw degree_X_pow_sub_C hn; exact dec_trivial)
end nonzero_comm_ring
section comm_ring
variables [comm_ring Ξ±] {p q : polynomial Ξ±}
@[simp] lemma mod_by_monic_X_sub_C_eq_C_eval (p : polynomial Ξ±) (a : Ξ±) : p %β (X - C a) = C (p.eval a) :=
if h0 : (0 : Ξ±) = 1 then by letI := subsingleton_of_zero_eq_one Ξ± h0; exact subsingleton.elim _ _
else
by letI : nonzero_comm_ring Ξ± := nonzero_comm_ring.of_ne h0; exact
have h : (p %β (X - C a)).eval a = p.eval a :=
by rw [mod_by_monic_eq_sub_mul_div _ (monic_X_sub_C a), eval_sub, eval_mul,
eval_sub, eval_X, eval_C, sub_self, zero_mul, sub_zero],
have degree (p %β (X - C a)) < 1 :=
degree_X_sub_C a βΈ degree_mod_by_monic_lt p (monic_X_sub_C a) ((degree_X_sub_C a).symm βΈ
ne_zero_of_monic (monic_X_sub_C _)),
have degree (p %β (X - C a)) β€ 0 :=
begin
cases (degree (p %β (X - C a))),
{ exact bot_le },
{ exact with_bot.some_le_some.2 (nat.le_of_lt_succ (with_bot.some_lt_some.1 this)) }
end,
begin
rw [eq_C_of_degree_le_zero this, eval_C] at h,
rw [eq_C_of_degree_le_zero this, h]
end
lemma mul_div_by_monic_eq_iff_is_root : (X - C a) * (p /β (X - C a)) = p β is_root p a :=
β¨Ξ» h, by rw [β h, is_root.def, eval_mul, eval_sub, eval_X, eval_C, sub_self, zero_mul],
Ξ» h : p.eval a = 0,
by conv {to_rhs, rw β mod_by_monic_add_div p (monic_X_sub_C a)};
rw [mod_by_monic_X_sub_C_eq_C_eval, h, C_0, zero_add]β©
lemma dvd_iff_is_root : (X - C a) β£ p β is_root p a :=
β¨Ξ» h, by rwa [β dvd_iff_mod_by_monic_eq_zero (monic_X_sub_C _),
mod_by_monic_X_sub_C_eq_C_eval, β C_0, C_inj] at h,
Ξ» h, β¨(p /β (X - C a)), by rw mul_div_by_monic_eq_iff_is_root.2 hβ©β©
lemma mod_by_monic_X (p : polynomial Ξ±) : p %β X = C (p.eval 0) :=
by rw [β mod_by_monic_X_sub_C_eq_C_eval, C_0, sub_zero]
section multiplicity
def decidable_dvd_monic (p : polynomial Ξ±) (hq : monic q) : decidable (q β£ p) :=
decidable_of_iff (p %β q = 0) (dvd_iff_mod_by_monic_eq_zero hq)
open_locale classical
lemma multiplicity_X_sub_C_finite (a : Ξ±) (h0 : p β 0) :
multiplicity.finite (X - C a) p :=
multiplicity_finite_of_degree_pos_of_monic
(have (0 : Ξ±) β 1, from (Ξ» h, by haveI := subsingleton_of_zero_eq_one _ h;
exact h0 (subsingleton.elim _ _)),
by letI : nonzero_comm_ring Ξ± := { zero_ne_one := this, ..show comm_ring Ξ±, by apply_instance };
rw degree_X_sub_C; exact dec_trivial)
(monic_X_sub_C _) h0
def root_multiplicity (a : Ξ±) (p : polynomial Ξ±) : β :=
if h0 : p = 0 then 0
else let I : decidable_pred (Ξ» n : β, Β¬(X - C a) ^ (n + 1) β£ p) :=
Ξ» n, @not.decidable _ (decidable_dvd_monic p (monic_pow (monic_X_sub_C a) (n + 1))) in
by exactI nat.find (multiplicity_X_sub_C_finite a h0)
lemma root_multiplicity_eq_multiplicity (p : polynomial Ξ±) (a : Ξ±) :
root_multiplicity a p = if h0 : p = 0 then 0 else
(multiplicity (X - C a) p).get (multiplicity_X_sub_C_finite a h0) :=
by simp [multiplicity, root_multiplicity, roption.dom];
congr; funext; congr
lemma pow_root_multiplicity_dvd (p : polynomial Ξ±) (a : Ξ±) :
(X - C a) ^ root_multiplicity a p β£ p :=
if h : p = 0 then by simp [h]
else by rw [root_multiplicity_eq_multiplicity, dif_neg h];
exact multiplicity.pow_multiplicity_dvd _
lemma div_by_monic_mul_pow_root_multiplicity_eq
(p : polynomial Ξ±) (a : Ξ±) :
p /β ((X - C a) ^ root_multiplicity a p) *
(X - C a) ^ root_multiplicity a p = p :=
have monic ((X - C a) ^ root_multiplicity a p),
from monic_pow (monic_X_sub_C _) _,
by conv_rhs { rw [β mod_by_monic_add_div p this,
(dvd_iff_mod_by_monic_eq_zero this).2 (pow_root_multiplicity_dvd _ _)] };
simp [mul_comm]
lemma eval_div_by_monic_pow_root_multiplicity_ne_zero
{p : polynomial Ξ±} (a : Ξ±) (hp : p β 0) :
(p /β ((X - C a) ^ root_multiplicity a p)).eval a β 0 :=
begin
letI : nonzero_comm_ring Ξ± := nonzero_comm_ring.of_polynomial_ne hp,
rw [ne.def, β is_root.def, β dvd_iff_is_root],
rintros β¨q, hqβ©,
have := div_by_monic_mul_pow_root_multiplicity_eq p a,
rw [mul_comm, hq, β mul_assoc, β pow_succ',
root_multiplicity_eq_multiplicity, dif_neg hp] at this,
exact multiplicity.is_greatest'
(multiplicity_finite_of_degree_pos_of_monic
(show (0 : with_bot β) < degree (X - C a),
by rw degree_X_sub_C; exact dec_trivial) _ hp)
(nat.lt_succ_self _) (dvd_of_mul_right_eq _ this)
end
end multiplicity
end comm_ring
section integral_domain
variables [integral_domain Ξ±] {p q : polynomial Ξ±}
@[simp] lemma degree_mul_eq : degree (p * q) = degree p + degree q :=
if hp0 : p = 0 then by simp only [hp0, degree_zero, zero_mul, with_bot.bot_add]
else if hq0 : q = 0 then by simp only [hq0, degree_zero, mul_zero, with_bot.add_bot]
else degree_mul_eq' $ mul_ne_zero (mt leading_coeff_eq_zero.1 hp0)
(mt leading_coeff_eq_zero.1 hq0)
@[simp] lemma degree_pow_eq (p : polynomial Ξ±) (n : β) :
degree (p ^ n) = add_monoid.smul n (degree p) :=
by induction n; [simp only [pow_zero, degree_one, add_monoid.zero_smul],
simp only [*, pow_succ, succ_smul, degree_mul_eq]]
@[simp] lemma leading_coeff_mul (p q : polynomial Ξ±) : leading_coeff (p * q) =
leading_coeff p * leading_coeff q :=
begin
by_cases hp : p = 0,
{ simp only [hp, zero_mul, leading_coeff_zero] },
{ by_cases hq : q = 0,
{ simp only [hq, mul_zero, leading_coeff_zero] },
{ rw [leading_coeff_mul'],
exact mul_ne_zero (mt leading_coeff_eq_zero.1 hp) (mt leading_coeff_eq_zero.1 hq) } }
end
@[simp] lemma leading_coeff_pow (p : polynomial Ξ±) (n : β) :
leading_coeff (p ^ n) = leading_coeff p ^ n :=
by induction n; [simp only [pow_zero, leading_coeff_one],
simp only [*, pow_succ, leading_coeff_mul]]
instance : integral_domain (polynomial Ξ±) :=
{ eq_zero_or_eq_zero_of_mul_eq_zero := Ξ» a b h, begin
have : leading_coeff 0 = leading_coeff a * leading_coeff b := h βΈ leading_coeff_mul a b,
rw [leading_coeff_zero, eq_comm] at this,
erw [β leading_coeff_eq_zero, β leading_coeff_eq_zero],
exact eq_zero_or_eq_zero_of_mul_eq_zero this
end,
..polynomial.nonzero_comm_ring }
lemma nat_degree_mul_eq (hp : p β 0) (hq : q β 0) : nat_degree (p * q) =
nat_degree p + nat_degree q :=
by rw [β with_bot.coe_eq_coe, β degree_eq_nat_degree (mul_ne_zero hp hq),
with_bot.coe_add, β degree_eq_nat_degree hp,
β degree_eq_nat_degree hq, degree_mul_eq]
@[simp] lemma nat_degree_pow_eq (p : polynomial Ξ±) (n : β) :
nat_degree (p ^ n) = n * nat_degree p :=
if hp0 : p = 0
then if hn0 : n = 0 then by simp [hp0, hn0]
else by rw [hp0, zero_pow (nat.pos_of_ne_zero hn0)]; simp
else nat_degree_pow_eq'
(by rw [β leading_coeff_pow, ne.def, leading_coeff_eq_zero]; exact pow_ne_zero _ hp0)
lemma root_or_root_of_root_mul (h : is_root (p * q) a) : is_root p a β¨ is_root q a :=
by rw [is_root, eval_mul] at h;
exact eq_zero_or_eq_zero_of_mul_eq_zero h
lemma degree_le_mul_left (p : polynomial Ξ±) (hq : q β 0) : degree p β€ degree (p * q) :=
if hp : p = 0 then by simp only [hp, zero_mul, le_refl]
else by rw [degree_mul_eq, degree_eq_nat_degree hp,
degree_eq_nat_degree hq];
exact with_bot.coe_le_coe.2 (nat.le_add_right _ _)
lemma exists_finset_roots : β {p : polynomial Ξ±} (hp : p β 0),
β s : finset Ξ±, (s.card : with_bot β) β€ degree p β§ β x, x β s β is_root p x
| p := Ξ» hp, by haveI := classical.prop_decidable (β x, is_root p x); exact
if h : β x, is_root p x
then
let β¨x, hxβ© := h in
have hpd : 0 < degree p := degree_pos_of_root hp hx,
have hd0 : p /β (X - C x) β 0 :=
Ξ» h, by rw [β mul_div_by_monic_eq_iff_is_root.2 hx, h, mul_zero] at hp; exact hp rfl,
have wf : degree (p /β _) < degree p :=
degree_div_by_monic_lt _ (monic_X_sub_C x) hp
((degree_X_sub_C x).symm βΈ dec_trivial),
let β¨t, htd, htrβ© := @exists_finset_roots (p /β (X - C x)) hd0 in
have hdeg : degree (X - C x) β€ degree p := begin
rw [degree_X_sub_C, degree_eq_nat_degree hp],
rw degree_eq_nat_degree hp at hpd,
exact with_bot.coe_le_coe.2 (with_bot.coe_lt_coe.1 hpd)
end,
have hdiv0 : p /β (X - C x) β 0 := mt (div_by_monic_eq_zero_iff (monic_X_sub_C x)
(ne_zero_of_monic (monic_X_sub_C x))).1 $ not_lt.2 hdeg,
β¨insert x t, calc (card (insert x t) : with_bot β) β€ card t + 1 :
with_bot.coe_le_coe.2 $ finset.card_insert_le _ _
... β€ degree p :
by rw [β degree_add_div_by_monic (monic_X_sub_C x) hdeg,
degree_X_sub_C, add_comm];
exact add_le_add' (le_refl (1 : with_bot β)) htd,
begin
assume y,
rw [mem_insert, htr, eq_comm, β root_X_sub_C],
conv {to_rhs, rw β mul_div_by_monic_eq_iff_is_root.2 hx},
exact β¨Ξ» h, or.cases_on h (root_mul_right_of_is_root _) (root_mul_left_of_is_root _),
root_or_root_of_root_mulβ©
endβ©
else
β¨β
, (degree_eq_nat_degree hp).symm βΈ with_bot.coe_le_coe.2 (nat.zero_le _),
by simpa only [not_mem_empty, false_iff, not_exists] using hβ©
using_well_founded {dec_tac := tactic.assumption}
/-- `roots p` noncomputably gives a finset containing all the roots of `p` -/
noncomputable def roots (p : polynomial Ξ±) : finset Ξ± :=
if h : p = 0 then β
else classical.some (exists_finset_roots h)
lemma card_roots (hp0 : p β 0) : ((roots p).card : with_bot β) β€ degree p :=
begin
unfold roots,
rw dif_neg hp0,
exact (classical.some_spec (exists_finset_roots hp0)).1
end
lemma card_roots' {p : polynomial Ξ±} (hp0 : p β 0) : p.roots.card β€ nat_degree p :=
with_bot.coe_le_coe.1 (le_trans (card_roots hp0) (le_of_eq $ degree_eq_nat_degree hp0))
lemma card_roots_sub_C {p : polynomial Ξ±} {a : Ξ±} (hp0 : 0 < degree p) :
((p - C a).roots.card : with_bot β) β€ degree p :=
calc ((p - C a).roots.card : with_bot β) β€ degree (p - C a) :
card_roots $ mt sub_eq_zero.1 $ Ξ» h, not_le_of_gt hp0 $ h.symm βΈ degree_C_le
... = degree p : by rw [sub_eq_add_neg, β C_neg]; exact degree_add_C hp0
lemma card_roots_sub_C' {p : polynomial Ξ±} {a : Ξ±} (hp0 : 0 < degree p) :
(p - C a).roots.card β€ nat_degree p :=
with_bot.coe_le_coe.1 (le_trans (card_roots_sub_C hp0) (le_of_eq $ degree_eq_nat_degree
(Ξ» h, by simp [*, lt_irrefl] at *)))
@[simp] lemma mem_roots (hp : p β 0) : a β p.roots β is_root p a :=
by unfold roots; rw dif_neg hp; exact (classical.some_spec (exists_finset_roots hp)).2 _
@[simp] lemma mem_roots_sub_C {p : polynomial Ξ±} {a x : Ξ±} (hp0 : 0 < degree p) :
x β (p - C a).roots β p.eval x = a :=
(mem_roots (show p - C a β 0, from mt sub_eq_zero.1 $ Ξ» h,
not_le_of_gt hp0 $ h.symm βΈ degree_C_le)).trans
(by rw [is_root.def, eval_sub, eval_C, sub_eq_zero])
lemma card_roots_X_pow_sub_C {n : β} (hn : 0 < n) (a : Ξ±) :
(roots ((X : polynomial Ξ±) ^ n - C a)).card β€ n :=
with_bot.coe_le_coe.1 $
calc ((roots ((X : polynomial Ξ±) ^ n - C a)).card : with_bot β)
β€ degree ((X : polynomial Ξ±) ^ n - C a) : card_roots (X_pow_sub_C_ne_zero hn a)
... = n : degree_X_pow_sub_C hn a
/-- `nth_roots n a` noncomputably returns the solutions to `x ^ n = a`-/
def nth_roots {Ξ± : Type*} [integral_domain Ξ±] (n : β) (a : Ξ±) : finset Ξ± :=
roots ((X : polynomial Ξ±) ^ n - C a)
@[simp] lemma mem_nth_roots {Ξ± : Type*} [integral_domain Ξ±] {n : β} (hn : 0 < n) {a x : Ξ±} :
x β nth_roots n a β x ^ n = a :=
by rw [nth_roots, mem_roots (X_pow_sub_C_ne_zero hn a),
is_root.def, eval_sub, eval_C, eval_pow, eval_X, sub_eq_zero_iff_eq]
lemma card_nth_roots {Ξ± : Type*} [integral_domain Ξ±] (n : β) (a : Ξ±) :
(nth_roots n a).card β€ n :=
if hn : n = 0
then if h : (X : polynomial Ξ±) ^ n - C a = 0
then by simp only [nat.zero_le, nth_roots, roots, h, dif_pos rfl, card_empty]
else with_bot.coe_le_coe.1 (le_trans (card_roots h)
(by rw [hn, pow_zero, β C_1, β @is_ring_hom.map_sub _ _ _ _ (@C Ξ± _)];
exact degree_C_le))
else by rw [β with_bot.coe_le_coe, β degree_X_pow_sub_C (nat.pos_of_ne_zero hn) a];
exact card_roots (X_pow_sub_C_ne_zero (nat.pos_of_ne_zero hn) a)
lemma coeff_comp_degree_mul_degree (hqd0 : nat_degree q β 0) :
coeff (p.comp q) (nat_degree p * nat_degree q) =
leading_coeff p * leading_coeff q ^ nat_degree p :=
if hp0 : p = 0 then by simp [hp0] else
calc coeff (p.comp q) (nat_degree p * nat_degree q)
= p.sum (Ξ» n a, coeff (C a * q ^ n) (nat_degree p * nat_degree q)) :
by rw [comp, evalβ, coeff_sum]
... = coeff (C (leading_coeff p) * q ^ nat_degree p) (nat_degree p * nat_degree q) :
finset.sum_eq_single _
begin
assume b hbs hbp,
have hq0 : q β 0, from Ξ» hq0, hqd0 (by rw [hq0, nat_degree_zero]),
have : coeff p b β 0, rwa [β apply_eq_coeff, β finsupp.mem_support_iff],
dsimp [apply_eq_coeff],
refine coeff_eq_zero_of_degree_lt _,
rw [degree_mul_eq, degree_C this, degree_pow_eq, zero_add, degree_eq_nat_degree hq0,
β with_bot.coe_smul, add_monoid.smul_eq_mul, with_bot.coe_lt_coe, nat.cast_id],
exact (mul_lt_mul_right (nat.pos_of_ne_zero hqd0)).2
(lt_of_le_of_ne (with_bot.coe_le_coe.1 (by rw β degree_eq_nat_degree hp0; exact le_sup hbs)) hbp)
end
(by rw [finsupp.mem_support_iff, apply_eq_coeff, β leading_coeff, ne.def, leading_coeff_eq_zero,
classical.not_not]; simp {contextual := tt})
... = _ :
have coeff (q ^ nat_degree p) (nat_degree p * nat_degree q) = leading_coeff (q ^ nat_degree p),
by rw [leading_coeff, nat_degree_pow_eq],
by rw [coeff_C_mul, this, leading_coeff_pow]
lemma nat_degree_comp : nat_degree (p.comp q) = nat_degree p * nat_degree q :=
le_antisymm nat_degree_comp_le
(if hp0 : p = 0 then by rw [hp0, zero_comp, nat_degree_zero, zero_mul]
else if hqd0 : nat_degree q = 0
then have degree q β€ 0, by rw [β with_bot.coe_zero, β hqd0]; exact degree_le_nat_degree,
by rw [eq_C_of_degree_le_zero this]; simp
else le_nat_degree_of_ne_zero $
have hq0 : q β 0, from Ξ» hq0, hqd0 $ by rw [hq0, nat_degree_zero],
calc coeff (p.comp q) (nat_degree p * nat_degree q)
= leading_coeff p * leading_coeff q ^ nat_degree p :
coeff_comp_degree_mul_degree hqd0
... β 0 : mul_ne_zero (mt leading_coeff_eq_zero.1 hp0)
(pow_ne_zero _ (mt leading_coeff_eq_zero.1 hq0)))
lemma leading_coeff_comp (hq : nat_degree q β 0) : leading_coeff (p.comp q) =
leading_coeff p * leading_coeff q ^ nat_degree p :=
by rw [β coeff_comp_degree_mul_degree hq, β nat_degree_comp]; refl
lemma degree_eq_zero_of_is_unit (h : is_unit p) : degree p = 0 :=
let β¨q, hqβ© := is_unit_iff_dvd_one.1 h in
have hp0 : p β 0, from Ξ» hp0, by simpa [hp0] using hq,
have hq0 : q β 0, from Ξ» hp0, by simpa [hp0] using hq,
have nat_degree (1 : polynomial Ξ±) = nat_degree (p * q),
from congr_arg _ hq,
by rw [nat_degree_one, nat_degree_mul_eq hp0 hq0, eq_comm,
add_eq_zero_iff, β with_bot.coe_eq_coe,
β degree_eq_nat_degree hp0] at this;
exact this.1
@[simp] lemma degree_coe_units (u : units (polynomial Ξ±)) :
degree (u : polynomial Ξ±) = 0 :=
degree_eq_zero_of_is_unit β¨u, rflβ©
@[simp] lemma nat_degree_coe_units (u : units (polynomial Ξ±)) :
nat_degree (u : polynomial Ξ±) = 0 :=
nat_degree_eq_of_degree_eq_some (degree_coe_units u)
lemma coeff_coe_units_zero_ne_zero (u : units (polynomial Ξ±)) :
coeff (u : polynomial Ξ±) 0 β 0 :=
begin
conv in (0) {rw [β nat_degree_coe_units u]},
rw [β leading_coeff, ne.def, leading_coeff_eq_zero],
exact units.coe_ne_zero _
end
lemma degree_eq_degree_of_associated (h : associated p q) : degree p = degree q :=
let β¨u, huβ© := h in by simp [hu.symm]
lemma degree_eq_one_of_irreducible_of_root (hi : irreducible p) {x : Ξ±} (hx : is_root p x) :
degree p = 1 :=
let β¨g, hgβ© := dvd_iff_is_root.2 hx in
have is_unit (X - C x) β¨ is_unit g, from hi.2 _ _ hg,
this.elim
(Ξ» h, have hβ : degree (X - C x) = 1, from degree_X_sub_C x,
have hβ : degree (X - C x) = 0, from degree_eq_zero_of_is_unit h,
by rw hβ at hβ; exact absurd hβ dec_trivial)
(Ξ» hgu, by rw [hg, degree_mul_eq, degree_X_sub_C, degree_eq_zero_of_is_unit hgu, add_zero])
end integral_domain
section field
variables [discrete_field Ξ±] {p q : polynomial Ξ±}
instance : vector_space Ξ± (polynomial Ξ±) := finsupp.vector_space _ _
lemma is_unit_iff_degree_eq_zero : is_unit p β degree p = 0 :=
β¨degree_eq_zero_of_is_unit,
Ξ» h, have degree p β€ 0, by simp [*, le_refl],
have hc : coeff p 0 β 0, from Ξ» hc,
by rw [eq_C_of_degree_le_zero this, hc] at h;
simpa using h,
is_unit_iff_dvd_one.2 β¨C (coeff p 0)β»ΒΉ, begin
conv in p { rw eq_C_of_degree_le_zero this },
rw [β C_mul, _root_.mul_inv_cancel hc, C_1]
endβ©β©
lemma degree_pos_of_ne_zero_of_nonunit (hp0 : p β 0) (hp : Β¬is_unit p) :
0 < degree p :=
lt_of_not_ge (Ξ» h, by rw [eq_C_of_degree_le_zero h] at hp0 hp;
exact (hp $ is_unit.map' C $
is_unit.mk0 (coeff p 0) (mt C_inj.2 (by simpa using hp0))))
lemma irreducible_of_degree_eq_one (hp1 : degree p = 1) : irreducible p :=
β¨mt is_unit_iff_dvd_one.1 (Ξ» β¨q, hqβ©,
absurd (congr_arg degree hq) (Ξ» h,
have degree q = 0, by rw [degree_one, degree_mul_eq, hp1, eq_comm,
nat.with_bot.add_eq_zero_iff] at h; exact h.2,
by simp [degree_mul_eq, this, degree_one, hp1] at h;
exact absurd h dec_trivial)),
Ξ» q r hpqr, begin
have := congr_arg degree hpqr,
rw [hp1, degree_mul_eq, eq_comm, nat.with_bot.add_eq_one_iff] at this,
rw [is_unit_iff_degree_eq_zero, is_unit_iff_degree_eq_zero]; tautology
endβ©
lemma monic_mul_leading_coeff_inv (h : p β 0) :
monic (p * C (leading_coeff p)β»ΒΉ) :=
by rw [monic, leading_coeff_mul, leading_coeff_C,
mul_inv_cancel (show leading_coeff p β 0, from mt leading_coeff_eq_zero.1 h)]
lemma degree_mul_leading_coeff_inv (p : polynomial Ξ±) (h : q β 0) :
degree (p * C (leading_coeff q)β»ΒΉ) = degree p :=
have hβ : (leading_coeff q)β»ΒΉ β 0 :=
inv_ne_zero (mt leading_coeff_eq_zero.1 h),
by rw [degree_mul_eq, degree_C hβ, add_zero]
def div (p q : polynomial Ξ±) :=
C (leading_coeff q)β»ΒΉ * (p /β (q * C (leading_coeff q)β»ΒΉ))
def mod (p q : polynomial Ξ±) :=
p %β (q * C (leading_coeff q)β»ΒΉ)
private lemma quotient_mul_add_remainder_eq_aux (p q : polynomial Ξ±) :
q * div p q + mod p q = p :=
if h : q = 0 then by simp only [h, zero_mul, mod, mod_by_monic_zero, zero_add]
else begin
conv {to_rhs, rw β mod_by_monic_add_div p (monic_mul_leading_coeff_inv h)},
rw [div, mod, add_comm, mul_assoc]
end
private lemma remainder_lt_aux (p : polynomial Ξ±) (hq : q β 0) :
degree (mod p q) < degree q :=
by rw β degree_mul_leading_coeff_inv q hq; exact
degree_mod_by_monic_lt p (monic_mul_leading_coeff_inv hq)
(mul_ne_zero hq (mt leading_coeff_eq_zero.2 (by rw leading_coeff_C;
exact inv_ne_zero (mt leading_coeff_eq_zero.1 hq))))
instance : has_div (polynomial Ξ±) := β¨divβ©
instance : has_mod (polynomial Ξ±) := β¨modβ©
lemma div_def : p / q = C (leading_coeff q)β»ΒΉ * (p /β (q * C (leading_coeff q)β»ΒΉ)) := rfl
lemma mod_def : p % q = p %β (q * C (leading_coeff q)β»ΒΉ) := rfl
lemma mod_by_monic_eq_mod (p : polynomial Ξ±) (hq : monic q) : p %β q = p % q :=
show p %β q = p %β (q * C (leading_coeff q)β»ΒΉ), by simp only [monic.def.1 hq, inv_one, mul_one, C_1]
lemma div_by_monic_eq_div (p : polynomial Ξ±) (hq : monic q) : p /β q = p / q :=
show p /β q = C (leading_coeff q)β»ΒΉ * (p /β (q * C (leading_coeff q)β»ΒΉ)),
by simp only [monic.def.1 hq, inv_one, C_1, one_mul, mul_one]
lemma mod_X_sub_C_eq_C_eval (p : polynomial Ξ±) (a : Ξ±) : p % (X - C a) = C (p.eval a) :=
mod_by_monic_eq_mod p (monic_X_sub_C a) βΈ mod_by_monic_X_sub_C_eq_C_eval _ _
lemma mul_div_eq_iff_is_root : (X - C a) * (p / (X - C a)) = p β is_root p a :=
div_by_monic_eq_div p (monic_X_sub_C a) βΈ mul_div_by_monic_eq_iff_is_root
instance : euclidean_domain (polynomial Ξ±) :=
{ quotient := (/),
quotient_zero := by simp [div_def],
remainder := (%),
r := _,
r_well_founded := degree_lt_wf,
quotient_mul_add_remainder_eq := quotient_mul_add_remainder_eq_aux,
remainder_lt := Ξ» p q hq, remainder_lt_aux _ hq,
mul_left_not_lt := Ξ» p q hq, not_lt_of_ge (degree_le_mul_left _ hq) }
lemma mod_eq_self_iff (hq0 : q β 0) : p % q = p β degree p < degree q :=
β¨Ξ» h, h βΈ euclidean_domain.mod_lt _ hq0,
Ξ» h, have Β¬degree (q * C (leading_coeff q)β»ΒΉ) β€ degree p :=
not_le_of_gt $ by rwa degree_mul_leading_coeff_inv q hq0,
begin
rw [mod_def, mod_by_monic, dif_pos (monic_mul_leading_coeff_inv hq0)],
unfold div_mod_by_monic_aux,
simp only [this, false_and, if_false]
endβ©
lemma div_eq_zero_iff (hq0 : q β 0) : p / q = 0 β degree p < degree q :=
β¨Ξ» h, by have := euclidean_domain.div_add_mod p q;
rwa [h, mul_zero, zero_add, mod_eq_self_iff hq0] at this,
Ξ» h, have hlt : degree p < degree (q * C (leading_coeff q)β»ΒΉ),
by rwa degree_mul_leading_coeff_inv q hq0,
have hm : monic (q * C (leading_coeff q)β»ΒΉ) := monic_mul_leading_coeff_inv hq0,
by rw [div_def, (div_by_monic_eq_zero_iff hm (ne_zero_of_monic hm)).2 hlt, mul_zero]β©
lemma degree_add_div (hq0 : q β 0) (hpq : degree q β€ degree p) :
degree q + degree (p / q) = degree p :=
have degree (p % q) < degree (q * (p / q)) :=
calc degree (p % q) < degree q : euclidean_domain.mod_lt _ hq0
... β€ _ : degree_le_mul_left _ (mt (div_eq_zero_iff hq0).1 (not_lt_of_ge hpq)),
by conv {to_rhs, rw [β euclidean_domain.div_add_mod p q, add_comm,
degree_add_eq_of_degree_lt this, degree_mul_eq]}
lemma degree_div_le (p q : polynomial Ξ±) : degree (p / q) β€ degree p :=
if hq : q = 0 then by simp [hq]
else by rw [div_def, mul_comm, degree_mul_leading_coeff_inv _ hq];
exact degree_div_by_monic_le _ _
lemma degree_div_lt (hp : p β 0) (hq : 0 < degree q) : degree (p / q) < degree p :=
have hq0 : q β 0, from Ξ» hq0, by simpa [hq0] using hq,
by rw [div_def, mul_comm, degree_mul_leading_coeff_inv _ hq0];
exact degree_div_by_monic_lt _ (monic_mul_leading_coeff_inv hq0) hp
(by rw degree_mul_leading_coeff_inv _ hq0; exact hq)
@[simp] lemma degree_map [discrete_field Ξ²] (p : polynomial Ξ±) (f : Ξ± β Ξ²) [is_ring_hom f] :
degree (p.map f) = degree p :=
p.degree_map_eq_of_injective (is_ring_hom.injective f)
@[simp] lemma nat_degree_map [discrete_field Ξ²] (f : Ξ± β Ξ²) [is_ring_hom f] :
nat_degree (p.map f) = nat_degree p :=
nat_degree_eq_of_degree_eq (degree_map _ f)
@[simp] lemma leading_coeff_map [discrete_field Ξ²] (f : Ξ± β Ξ²) [is_ring_hom f] :
leading_coeff (p.map f) = f (leading_coeff p) :=
by simp [leading_coeff, coeff_map f]
lemma map_div [discrete_field Ξ²] (f : Ξ± β Ξ²) [is_ring_hom f] :
(p / q).map f = p.map f / q.map f :=
if hq0 : q = 0 then by simp [hq0]
else
by rw [div_def, div_def, map_mul, map_div_by_monic f (monic_mul_leading_coeff_inv hq0)];
simp [is_ring_hom.map_inv f, leading_coeff, coeff_map f]
lemma map_mod [discrete_field Ξ²] (f : Ξ± β Ξ²) [is_ring_hom f] :
(p % q).map f = p.map f % q.map f :=
if hq0 : q = 0 then by simp [hq0]
else by rw [mod_def, mod_def, leading_coeff_map f, β is_ring_hom.map_inv f, β map_C f,
β map_mul f, map_mod_by_monic f (monic_mul_leading_coeff_inv hq0)]
@[simp] lemma map_eq_zero [discrete_field Ξ²] (f : Ξ± β Ξ²) [is_ring_hom f] :
p.map f = 0 β p = 0 :=
by simp [polynomial.ext_iff, is_ring_hom.map_eq_zero f, coeff_map]
lemma exists_root_of_degree_eq_one (h : degree p = 1) : β x, is_root p x :=
β¨-(p.coeff 0 / p.coeff 1),
have p.coeff 1 β 0,
by rw β nat_degree_eq_of_degree_eq_some h;
exact mt leading_coeff_eq_zero.1 (Ξ» h0, by simpa [h0] using h),
by conv in p { rw [eq_X_add_C_of_degree_le_one (show degree p β€ 1, by rw h; exact le_refl _)] };
simp [is_root, mul_div_cancel' _ this]β©
lemma coeff_inv_units (u : units (polynomial Ξ±)) (n : β) :
((βu : polynomial Ξ±).coeff n)β»ΒΉ = ((βuβ»ΒΉ : polynomial Ξ±).coeff n) :=
begin
rw [eq_C_of_degree_eq_zero (degree_coe_units u), eq_C_of_degree_eq_zero (degree_coe_units uβ»ΒΉ),
coeff_C, coeff_C, inv_eq_one_div],
split_ifs,
{ rw [div_eq_iff_mul_eq (coeff_coe_units_zero_ne_zero u), coeff_zero_eq_eval_zero,
coeff_zero_eq_eval_zero, β eval_mul, β units.coe_mul, inv_mul_self];
simp },
{ simp }
end
instance : normalization_domain (polynomial Ξ±) :=
{ norm_unit := Ξ» p, if hp0 : p = 0 then 1
else β¨C p.leading_coeffβ»ΒΉ, C p.leading_coeff,
by rw [β C_mul, inv_mul_cancel, C_1];
exact mt leading_coeff_eq_zero.1 hp0,
by rw [β C_mul, mul_inv_cancel, C_1];
exact mt leading_coeff_eq_zero.1 hp0,β©,
norm_unit_zero := dif_pos rfl,
norm_unit_mul := Ξ» p q hp0 hq0, begin
rw [dif_neg hp0, dif_neg hq0, dif_neg (mul_ne_zero hp0 hq0)],
apply units.ext,
show C (leading_coeff (p * q))β»ΒΉ = C (leading_coeff p)β»ΒΉ * C (leading_coeff q)β»ΒΉ,
rw [leading_coeff_mul, mul_inv', C_mul, mul_comm]
end,
norm_unit_coe_units := Ξ» u,
have hu : degree βuβ»ΒΉ = 0, from degree_eq_zero_of_is_unit β¨uβ»ΒΉ, rflβ©,
begin
apply units.ext,
rw [dif_neg (units.coe_ne_zero u)],
conv_rhs {rw eq_C_of_degree_eq_zero hu},
refine C_inj.2 _,
rw [β nat_degree_eq_of_degree_eq_some hu, leading_coeff,
coeff_inv_units],
simp
end,
..polynomial.integral_domain }
lemma monic_normalize (hp0 : p β 0) : monic (normalize p) :=
show leading_coeff (p * β(dite _ _ _)) = 1,
by rw dif_neg hp0; exact monic_mul_leading_coeff_inv hp0
lemma coe_norm_unit (hp : p β 0) : (norm_unit p : polynomial Ξ±) = C p.leading_coeffβ»ΒΉ :=
show β(dite _ _ _) = C p.leading_coeffβ»ΒΉ, by rw dif_neg hp; refl
end field
section derivative
variables [comm_semiring Ξ±]
/-- `derivative p` formal derivative of the polynomial `p` -/
def derivative (p : polynomial Ξ±) : polynomial Ξ± := p.sum (Ξ»n a, C (a * n) * X^(n - 1))
lemma coeff_derivative (p : polynomial Ξ±) (n : β) : coeff (derivative p) n = coeff p (n + 1) * (n + 1) :=
begin
rw [derivative],
simp only [coeff_X_pow, coeff_sum, coeff_C_mul],
rw [finsupp.sum, finset.sum_eq_single (n + 1), apply_eq_coeff],
{ rw [if_pos (nat.add_sub_cancel _ _).symm, mul_one, nat.cast_add, nat.cast_one] },
{ assume b, cases b,
{ intros, rw [nat.cast_zero, mul_zero, zero_mul] },
{ intros _ H, rw [nat.succ_sub_one b, if_neg (mt (congr_arg nat.succ) H.symm), mul_zero] } },
{ intro H, rw [not_mem_support_iff.1 H, zero_mul, zero_mul] }
end
@[simp] lemma derivative_zero : derivative (0 : polynomial Ξ±) = 0 :=
finsupp.sum_zero_index
lemma derivative_monomial (a : Ξ±) (n : β) : derivative (C a * X ^ n) = C (a * n) * X^(n - 1) :=
by rw [β single_eq_C_mul_X, β single_eq_C_mul_X, derivative, sum_single_index, single_eq_C_mul_X];
simp only [zero_mul, C_0]; refl
@[simp] lemma derivative_C {a : Ξ±} : derivative (C a) = 0 :=
suffices derivative (C a * X^0) = C (a * 0:Ξ±) * X ^ 0,
by simpa only [mul_one, zero_mul, C_0, mul_zero, pow_zero],
derivative_monomial a 0
@[simp] lemma derivative_X : derivative (X : polynomial Ξ±) = 1 :=
suffices derivative (C (1:Ξ±) * X^1) = C (1 * (1:β)) * X ^ 0,
by simpa only [mul_one, one_mul, C_1, pow_one, nat.cast_one, pow_zero],
derivative_monomial 1 1
@[simp] lemma derivative_one : derivative (1 : polynomial Ξ±) = 0 :=
derivative_C
@[simp] lemma derivative_add {f g : polynomial Ξ±} :
derivative (f + g) = derivative f + derivative g :=
by refine finsupp.sum_add_index _ _; intros;
simp only [add_mul, zero_mul, C_0, C_add, C_mul]
instance : is_add_monoid_hom (derivative : polynomial Ξ± β polynomial Ξ±) :=
{ map_add := Ξ» _ _, derivative_add, map_zero := derivative_zero }
@[simp] lemma derivative_sum {s : finset Ξ²} {f : Ξ² β polynomial Ξ±} :
derivative (s.sum f) = s.sum (Ξ»b, derivative (f b)) :=
(finset.sum_hom derivative).symm
@[simp] lemma derivative_mul {f g : polynomial Ξ±} :
derivative (f * g) = derivative f * g + f * derivative g :=
calc derivative (f * g) = f.sum (Ξ»n a, g.sum (Ξ»m b, C ((a * b) * (n + m : β)) * X^((n + m) - 1))) :
begin
transitivity, exact derivative_sum,
transitivity, { apply finset.sum_congr rfl, assume x hx, exact derivative_sum },
apply finset.sum_congr rfl, assume n hn, apply finset.sum_congr rfl, assume m hm,
transitivity,
{ apply congr_arg, exact single_eq_C_mul_X },
exact derivative_monomial _ _
end
... = f.sum (Ξ»n a, g.sum (Ξ»m b,
(C (a * n) * X^(n - 1)) * (C b * X^m) + (C a * X^n) * (C (b * m) * X^(m - 1)))) :
sum_congr rfl $ assume n hn, sum_congr rfl $ assume m hm,
by simp only [nat.cast_add, mul_add, add_mul, C_add, C_mul];
cases n; simp only [nat.succ_sub_succ, pow_zero];
cases m; simp only [nat.cast_zero, C_0, nat.succ_sub_succ, zero_mul, mul_zero,
nat.sub_zero, pow_zero, pow_add, one_mul, pow_succ, mul_comm, mul_left_comm]
... = derivative f * g + f * derivative g :
begin
conv { to_rhs, congr,
{ rw [β sum_C_mul_X_eq g] },
{ rw [β sum_C_mul_X_eq f] } },
unfold derivative finsupp.sum,
simp only [sum_add_distrib, finset.mul_sum, finset.sum_mul]
end
lemma derivative_eval (p : polynomial Ξ±) (x : Ξ±) : p.derivative.eval x = p.sum (Ξ» n a, (a * n)*x^(n-1)) :=
by simp [derivative, eval_sum, eval_pow]
end derivative
section domain
variables [integral_domain Ξ±]
lemma mem_support_derivative [char_zero Ξ±] (p : polynomial Ξ±) (n : β) :
n β (derivative p).support β n + 1 β p.support :=
suffices (Β¬(coeff p (n + 1) = 0 β¨ ((n + 1:β) : Ξ±) = 0)) β coeff p (n + 1) β 0,
by simpa only [coeff_derivative, apply_eq_coeff, mem_support_iff, ne.def, mul_eq_zero],
by rw [nat.cast_eq_zero]; simp only [nat.succ_ne_zero, or_false]
@[simp] lemma degree_derivative_eq [char_zero Ξ±] (p : polynomial Ξ±) (hp : 0 < nat_degree p) :
degree (derivative p) = (nat_degree p - 1 : β) :=
le_antisymm
(le_trans (degree_sum_le _ _) $ sup_le $ assume n hn,
have n β€ nat_degree p, begin
rw [β with_bot.coe_le_coe, β degree_eq_nat_degree],
{ refine le_degree_of_ne_zero _, simpa only [mem_support_iff] using hn },
{ assume h, simpa only [h, support_zero] using hn }
end,
le_trans (degree_monomial_le _ _) $ with_bot.coe_le_coe.2 $ nat.sub_le_sub_right this _)
begin
refine le_sup _,
rw [mem_support_derivative, nat.sub_add_cancel, mem_support_iff],
{ show Β¬ leading_coeff p = 0,
rw [leading_coeff_eq_zero],
assume h, rw [h, nat_degree_zero] at hp,
exact lt_irrefl 0 (lt_of_le_of_lt (zero_le _) hp), },
exact hp
end
end domain
section identities
/- @TODO: pow_add_expansion and pow_sub_pow_factor are not specific to polynomials.
These belong somewhere else. But not in group_power because they depend on tactic.ring
Maybe use data.nat.choose to prove it.
-/
def pow_add_expansion {Ξ± : Type*} [comm_semiring Ξ±] (x y : Ξ±) : β (n : β),
{k // (x + y)^n = x^n + n*x^(n-1)*y + k * y^2}
| 0 := β¨0, by simpβ©
| 1 := β¨0, by simpβ©
| (n+2) :=
begin
cases pow_add_expansion (n+1) with z hz,
existsi x*z + (n+1)*x^n+z*y,
calc (x + y) ^ (n + 2) = (x + y) * (x + y) ^ (n + 1) : by ring_exp
... = (x + y) * (x ^ (n + 1) + β(n + 1) * x ^ (n + 1 - 1) * y + z * y ^ 2) : by rw hz
... = x ^ (n + 2) + β(n + 2) * x ^ (n + 1) * y + (x*z + (n+1)*x^n+z*y) * y ^ 2 :
by { push_cast, ring_exp! }
end
variables [comm_ring Ξ±]
private def poly_binom_aux1 (x y : Ξ±) (e : β) (a : Ξ±) :
{k : Ξ± // a * (x + y)^e = a * (x^e + e*x^(e-1)*y + k*y^2)} :=
begin
existsi (pow_add_expansion x y e).val,
congr,
apply (pow_add_expansion _ _ _).property
end
private lemma poly_binom_aux2 (f : polynomial Ξ±) (x y : Ξ±) :
f.eval (x + y) = f.sum (Ξ» e a, a * (x^e + e*x^(e-1)*y + (poly_binom_aux1 x y e a).val*y^2)) :=
begin
unfold eval evalβ, congr, ext,
apply (poly_binom_aux1 x y _ _).property
end
private lemma poly_binom_aux3 (f : polynomial Ξ±) (x y : Ξ±) : f.eval (x + y) =
f.sum (Ξ» e a, a * x^e) +
f.sum (Ξ» e a, (a * e * x^(e-1)) * y) +
f.sum (Ξ» e a, (a *(poly_binom_aux1 x y e a).val)*y^2) :=
by rw poly_binom_aux2; simp [left_distrib, finsupp.sum_add, mul_assoc]
def binom_expansion (f : polynomial Ξ±) (x y : Ξ±) :
{k : Ξ± // f.eval (x + y) = f.eval x + (f.derivative.eval x) * y + k * y^2} :=
begin
existsi f.sum (Ξ» e a, a *((poly_binom_aux1 x y e a).val)),
rw poly_binom_aux3,
congr,
{ rw derivative_eval, symmetry,
apply finsupp.sum_mul },
{ symmetry, apply finsupp.sum_mul }
end
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),
calc x ^ (k + 2) - y ^ (k + 2)
= x * (x ^ (k + 1) - y ^ (k + 1)) + (x * y ^ (k + 1) - y ^ (k + 2)) : by ring_exp
... = x * (z * (x - y)) + (x * y ^ (k + 1) - y ^ (k + 2)) : by rw hz
... = (z * x + y ^ (k + 1)) * (x - y) : by ring_exp
end
def eval_sub_factor (f : polynomial Ξ±) (x y : Ξ±) :
{z : Ξ± // f.eval x - f.eval y = z*(x - y)} :=
begin
existsi f.sum (Ξ» a b, b * (pow_sub_pow_factor x y).val),
unfold eval evalβ,
rw [βfinsupp.sum_sub],
have : finsupp.sum f (Ξ» (a : β) (b : Ξ±), b * (pow_sub_pow_factor x y).val) * (x - y) =
finsupp.sum f (Ξ» (a : β) (b : Ξ±), b * (pow_sub_pow_factor x y).val * (x - y)),
{ apply finsupp.sum_mul },
rw this,
congr, ext e a,
rw [mul_assoc, β(pow_sub_pow_factor x y).property],
simp [left_distrib]
end
end identities
end polynomial
|
979c75c196557384bc815a826685e7bee9e416f9 | b7f22e51856f4989b970961f794f1c435f9b8f78 | /tests/lean/run/blast_trace.lean | 0ffbbe06e179766ecf1abd31c3dae3e195f27d3d | [
"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 | 120 | lean | set_option trace.blast true
set_option trace.blast.action false
example (a b : Prop) : a β§ b β b β§ a :=
by blast
|
ad84745238268fe803477b525b4005f0a83db8a3 | d796eac6dc113f68ec6fc0579c13e8eae2bdef6c | /Resources/Code/Category+Theory-Github-Topic/monoidal-categories-reboot/src/monoidal_categories_reboot/tensor_product.lean | 326cb974171057311bdae95c430e496593c3462e | [
"Apache-2.0"
] | permissive | paddlelaw/functional-discipline-content-cats | 5a7e13e8adedd96b94f66b0b3cbf1847bc86d7f6 | d93b7aa849b343c384cec40c73ee84a9300004e8 | refs/heads/master | 1,675,541,859,508 | 1,607,251,766,000 | 1,607,251,766,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,322 | lean | -- Copyright (c) 2018 Michael Jendrusch. All rights reserved.
import category_theory.category
import category_theory.functor
import category_theory.products
import category_theory.natural_isomorphism
open category_theory
universes v u
open category_theory.category
open category_theory.functor
open category_theory.prod
open category_theory.functor.category.nat_trans
namespace category_theory.monoidal
@[reducible] def tensor_obj_type
(C : Sort u) [category.{v} C] :=
C β C β C
@[reducible] def tensor_hom_type
{C : Sort u} [category.{v} C] (tensor_obj : tensor_obj_type C) : Sort (imax u u u u v) :=
Ξ {Xβ Yβ Xβ Yβ : C}, has_hom.hom Xβ Yβ β has_hom.hom Xβ Yβ β has_hom.hom (tensor_obj Xβ Xβ) (tensor_obj Yβ Yβ)
local attribute [tidy] tactic.assumption
def assoc_obj
{C : Sort u} [category.{v} C] (tensor_obj : tensor_obj_type C) : Sort (max u v 1) :=
Ξ X Y Z : C, (tensor_obj (tensor_obj X Y) Z) β
(tensor_obj X (tensor_obj Y Z))
def assoc_natural
{C : Sort u} [category.{v} C]
(tensor_obj : tensor_obj_type C)
(tensor_hom : tensor_hom_type tensor_obj)
(assoc : assoc_obj tensor_obj) : Prop :=
β {Xβ Xβ Xβ Yβ Yβ Yβ : C} (fβ : Xβ βΆ Yβ) (fβ : Xβ βΆ Yβ) (fβ : Xβ βΆ Yβ),
(tensor_hom (tensor_hom fβ fβ) fβ) β« (assoc Yβ Yβ Yβ).hom = (assoc Xβ Xβ Xβ).hom β« (tensor_hom fβ (tensor_hom fβ fβ))
def left_unitor_obj
{C : Sort u} [category.{v} C]
(tensor_obj : tensor_obj_type C)
(tensor_unit : C) : Sort (max v 1 u) :=
Ξ X : C, (tensor_obj tensor_unit X) β
X
def left_unitor_natural
{C : Sort u} [category.{v} C]
(tensor_obj : tensor_obj_type C)
(tensor_hom : tensor_hom_type tensor_obj)
(tensor_unit : C)
(left_unitor : left_unitor_obj tensor_obj tensor_unit) : Prop :=
β {X Y : C} (f : X βΆ Y),
(tensor_hom (π tensor_unit) f) β« (left_unitor Y).hom = (left_unitor X).hom β« f
def right_unitor_obj
{C : Sort u} [category.{v} C]
(tensor_obj : tensor_obj_type C)
(tensor_unit : C) : Sort (max v 1 u) :=
Ξ (X : C), (tensor_obj X tensor_unit) β
X
def right_unitor_natural
{C : Sort u} [category.{v} C]
(tensor_obj : tensor_obj_type C)
(tensor_hom : tensor_hom_type tensor_obj)
(tensor_unit : C)
(right_unitor : right_unitor_obj tensor_obj tensor_unit) : Prop :=
β {X Y : C} (f : X βΆ Y),
(tensor_hom f (π tensor_unit)) β« (right_unitor Y).hom = (right_unitor X).hom β« f
@[reducible] def pentagon
{C : Sort u} [category.{v} C]
{tensor_obj : tensor_obj_type C}
(tensor_hom : tensor_hom_type tensor_obj)
(assoc : assoc_obj tensor_obj) : Prop :=
β W X Y Z : C,
(tensor_hom (assoc W X Y).hom (π Z)) β« (assoc W (tensor_obj X Y) Z).hom β« (tensor_hom (π W) (assoc X Y Z).hom)
= (assoc (tensor_obj W X) Y Z).hom β« (assoc W X (tensor_obj Y Z)).hom
@[reducible] def triangle
{C : Sort u} [category.{v} C]
{tensor_obj : tensor_obj_type C} {tensor_unit : C}
(tensor_hom : tensor_hom_type tensor_obj)
(left_unitor : left_unitor_obj tensor_obj tensor_unit)
(right_unitor : right_unitor_obj tensor_obj tensor_unit)
(assoc : assoc_obj tensor_obj) : Prop :=
β X Y : C,
(assoc X tensor_unit Y).hom β« (tensor_hom (π X) (left_unitor Y).hom)
= tensor_hom (right_unitor X).hom (π Y)
end category_theory.monoidal
|
725b197bb14652dc138423eb7154f960e1d8d295 | d29d82a0af640c937e499f6be79fc552eae0aa13 | /src/group_theory/subgroup.lean | 7f8b05fec3b83bdc265ccb46fbfd46711b5402cc | [
"Apache-2.0"
] | permissive | AbdulMajeedkhurasani/mathlib | 835f8a5c5cf3075b250b3737172043ab4fa1edf6 | 79bc7323b164aebd000524ebafd198eb0e17f956 | refs/heads/master | 1,688,003,895,660 | 1,627,788,521,000 | 1,627,788,521,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 84,747 | lean | /-
Copyright (c) 2020 Kexing Ying. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kexing Ying
-/
import group_theory.submonoid
import algebra.group.conj
import algebra.pointwise
import order.atoms
/-!
# Subgroups
This file defines multiplicative and additive subgroups as an extension of submonoids, in a bundled
form (unbundled subgroups are in `deprecated/subgroups.lean`).
We prove subgroups of a group form a complete lattice, and results about images and preimages of
subgroups under group homomorphisms. The bundled subgroups use bundled monoid homomorphisms.
There are also theorems about the subgroups generated by an element or a subset of a group,
defined both inductively and as the infimum of the set of subgroups containing a given
element/subset.
Special thanks goes to Amelia Livingston and Yury Kudryashov for their help and inspiration.
## Main definitions
Notation used here:
- `G N` are `group`s
- `A` is an `add_group`
- `H K` are `subgroup`s of `G` or `add_subgroup`s of `A`
- `x` is an element of type `G` or type `A`
- `f g : N β* G` are group homomorphisms
- `s k` are sets of elements of type `G`
Definitions in the file:
* `subgroup G` : the type of subgroups of a group `G`
* `add_subgroup A` : the type of subgroups of an additive group `A`
* `complete_lattice (subgroup G)` : the subgroups of `G` form a complete lattice
* `subgroup.closure k` : the minimal subgroup that includes the set `k`
* `subgroup.subtype` : the natural group homomorphism from a subgroup of group `G` to `G`
* `subgroup.gi` : `closure` forms a Galois insertion with the coercion to set
* `subgroup.comap H f` : the preimage of a subgroup `H` along the group homomorphism `f` is also a
subgroup
* `subgroup.map f H` : the image of a subgroup `H` along the group homomorphism `f` is also a
subgroup
* `subgroup.prod H K` : the product of subgroups `H`, `K` of groups `G`, `N` respectively, `H Γ K`
is a subgroup of `G Γ N`
* `monoid_hom.range f` : the range of the group homomorphism `f` is a subgroup
* `monoid_hom.ker f` : the kernel of a group homomorphism `f` is the subgroup of elements `x : G`
such that `f x = 1`
* `monoid_hom.eq_locus f g` : given group homomorphisms `f`, `g`, the elements of `G` such that
`f x = g x` form a subgroup of `G`
* `is_simple_group G` : a class indicating that a group has exactly two normal subgroups
## Implementation notes
Subgroup inclusion is denoted `β€` rather than `β`, although `β` is defined as
membership of a subgroup's underlying set.
## Tags
subgroup, subgroups
-/
open_locale big_operators
variables {G : Type*} [group G]
variables {A : Type*} [add_group A]
set_option old_structure_cmd true
/-- A subgroup of a group `G` is a subset containing 1, closed under multiplication
and closed under multiplicative inverse. -/
structure subgroup (G : Type*) [group G] extends submonoid G :=
(inv_mem' {x} : x β carrier β xβ»ΒΉ β carrier)
/-- An additive subgroup of an additive group `G` is a subset containing 0, closed
under addition and additive inverse. -/
structure add_subgroup (G : Type*) [add_group G] extends add_submonoid G:=
(neg_mem' {x} : x β carrier β -x β carrier)
attribute [to_additive] subgroup
attribute [to_additive add_subgroup.to_add_submonoid] subgroup.to_submonoid
/-- Reinterpret a `subgroup` as a `submonoid`. -/
add_decl_doc subgroup.to_submonoid
/-- Reinterpret an `add_subgroup` as an `add_submonoid`. -/
add_decl_doc add_subgroup.to_add_submonoid
namespace subgroup
@[to_additive]
instance : set_like (subgroup G) G :=
β¨subgroup.carrier, Ξ» p q h, by cases p; cases q; congr'β©
@[simp, to_additive]
lemma mem_carrier {s : subgroup G} {x : G} : x β s.carrier β x β s := iff.rfl
/-- See Note [custom simps projection] -/
@[to_additive "See Note [custom simps projection]"]
def simps.coe (S : subgroup G) : set G := S
initialize_simps_projections subgroup (carrier β coe)
initialize_simps_projections add_subgroup (carrier β coe)
@[simp, to_additive]
lemma coe_to_submonoid (K : subgroup G) : (K.to_submonoid : set G) = K := rfl
@[simp, to_additive]
lemma mem_to_submonoid (K : subgroup G) (x : G) : x β K.to_submonoid β x β K := iff.rfl
@[to_additive]
instance (K : subgroup G) [d : decidable_pred (β K)] [fintype G] : fintype K :=
show fintype {g : G // g β K}, from infer_instance
@[to_additive]
theorem to_submonoid_injective :
function.injective (to_submonoid : subgroup G β submonoid G) :=
Ξ» p q h, set_like.ext'_iff.2 (show _, from set_like.ext'_iff.1 h)
@[simp, to_additive]
theorem to_submonoid_eq {p q : subgroup G} : p.to_submonoid = q.to_submonoid β p = q :=
to_submonoid_injective.eq_iff
@[mono, to_additive] lemma to_submonoid_strict_mono :
strict_mono (to_submonoid : subgroup G β submonoid G) := Ξ» _ _, id
@[mono, to_additive]
lemma to_submonoid_mono : monotone (to_submonoid : subgroup G β submonoid G) :=
to_submonoid_strict_mono.monotone
@[simp, to_additive]
lemma to_submonoid_le {p q : subgroup G} : p.to_submonoid β€ q.to_submonoid β p β€ q :=
iff.rfl
end subgroup
/-!
### Conversion to/from `additive`/`multiplicative`
-/
section mul_add
/-- Supgroups of a group `G` are isomorphic to additive subgroups of `additive G`. -/
@[simps]
def subgroup.to_add_subgroup : subgroup G βo add_subgroup (additive G) :=
{ to_fun := Ξ» S,
{ neg_mem' := S.inv_mem',
..S.to_submonoid.to_add_submonoid },
inv_fun := Ξ» S,
{ inv_mem' := S.neg_mem',
..S.to_add_submonoid.to_submonoid' },
left_inv := Ξ» x, by cases x; refl,
right_inv := Ξ» x, by cases x; refl,
map_rel_iff' := Ξ» a b, iff.rfl, }
/-- Additive subgroup of an additive group `additive G` are isomorphic to subgroup of `G`. -/
abbreviation add_subgroup.to_subgroup' : add_subgroup (additive G) βo subgroup G :=
subgroup.to_add_subgroup.symm
/-- Additive supgroups of an additive group `A` are isomorphic to subgroups of `multiplicative A`.
-/
@[simps]
def add_subgroup.to_subgroup : add_subgroup A βo subgroup (multiplicative A) :=
{ to_fun := Ξ» S,
{ inv_mem' := S.neg_mem',
..S.to_add_submonoid.to_submonoid },
inv_fun := Ξ» S,
{ neg_mem' := S.inv_mem',
..S.to_submonoid.to_add_submonoid' },
left_inv := Ξ» x, by cases x; refl,
right_inv := Ξ» x, by cases x; refl,
map_rel_iff' := Ξ» a b, iff.rfl, }
/-- Subgroups of an additive group `multiplicative A` are isomorphic to additive subgroups of `A`.
-/
abbreviation subgroup.to_add_subgroup' : subgroup (multiplicative A) βo add_subgroup A :=
add_subgroup.to_subgroup.symm
end mul_add
namespace subgroup
variables (H K : subgroup G)
/-- Copy of a subgroup with a new `carrier` equal to the old one. Useful to fix definitional
equalities.-/
@[to_additive "Copy of an additive subgroup with a new `carrier` equal to the old one.
Useful to fix definitional equalities"]
protected def copy (K : subgroup G) (s : set G) (hs : s = K) : subgroup G :=
{ carrier := s,
one_mem' := hs.symm βΈ K.one_mem',
mul_mem' := hs.symm βΈ K.mul_mem',
inv_mem' := hs.symm βΈ K.inv_mem' }
/-- Two subgroups are equal if they have the same elements. -/
@[ext, to_additive "Two `add_subgroup`s are equal if they have the same elements."]
theorem ext {H K : subgroup G} (h : β x, x β H β x β K) : H = K := set_like.ext h
attribute [ext] add_subgroup.ext
/-- A subgroup contains the group's 1. -/
@[to_additive "An `add_subgroup` contains the group's 0."]
theorem one_mem : (1 : G) β H := H.one_mem'
/-- A subgroup is closed under multiplication. -/
@[to_additive "An `add_subgroup` is closed under addition."]
theorem mul_mem {x y : G} : x β H β y β H β x * y β H := Ξ» hx hy, H.mul_mem' hx hy
/-- A subgroup is closed under inverse. -/
@[to_additive "An `add_subgroup` is closed under inverse."]
theorem inv_mem {x : G} : x β H β xβ»ΒΉ β H := Ξ» hx, H.inv_mem' hx
/-- A subgroup is closed under division. -/
@[to_additive "An `add_subgroup` is closed under subtraction."]
theorem div_mem {x y : G} (hx : x β H) (hy : y β H) : x / y β H :=
by simpa only [div_eq_mul_inv] using H.mul_mem' hx (H.inv_mem' hy)
@[simp, to_additive] theorem inv_mem_iff {x : G} : xβ»ΒΉ β H β x β H :=
β¨Ξ» h, inv_inv x βΈ H.inv_mem h, H.inv_memβ©
@[to_additive] lemma div_mem_comm_iff {a b : G} : a / b β H β b / a β H :=
by rw [β H.inv_mem_iff, div_eq_mul_inv, div_eq_mul_inv, mul_inv_rev, inv_inv]
@[simp, to_additive]
theorem inv_coe_set : (H : set G)β»ΒΉ = H :=
by { ext, simp, }
@[simp, to_additive]
lemma exists_inv_mem_iff_exists_mem (K : subgroup G) {P : G β Prop} :
(β (x : G), x β K β§ P xβ»ΒΉ) β β x β K, P x :=
by split; { rintros β¨x, x_in, hxβ©, exact β¨xβ»ΒΉ, inv_mem K x_in, by simp [hx]β© }
@[to_additive]
lemma mul_mem_cancel_right {x y : G} (h : x β H) : y * x β H β y β H :=
β¨Ξ» hba, by simpa using H.mul_mem hba (H.inv_mem h), Ξ» hb, H.mul_mem hb hβ©
@[to_additive]
lemma mul_mem_cancel_left {x y : G} (h : x β H) : x * y β H β y β H :=
β¨Ξ» hab, by simpa using H.mul_mem (H.inv_mem h) hab, H.mul_mem hβ©
/-- Product of a list of elements in a subgroup is in the subgroup. -/
@[to_additive "Sum of a list of elements in an `add_subgroup` is in the `add_subgroup`."]
lemma list_prod_mem {l : list G} : (β x β l, x β K) β l.prod β K :=
K.to_submonoid.list_prod_mem
/-- Product of a multiset of elements in a subgroup of a `comm_group` is in the subgroup. -/
@[to_additive "Sum of a multiset of elements in an `add_subgroup` of an `add_comm_group`
is in the `add_subgroup`."]
lemma multiset_prod_mem {G} [comm_group G] (K : subgroup G) (g : multiset G) :
(β a β g, a β K) β g.prod β K := K.to_submonoid.multiset_prod_mem g
/-- Product of elements of a subgroup of a `comm_group` indexed by a `finset` is in the
subgroup. -/
@[to_additive "Sum of elements in an `add_subgroup` of an `add_comm_group` indexed by a `finset`
is in the `add_subgroup`."]
lemma prod_mem {G : Type*} [comm_group G] (K : subgroup G)
{ΞΉ : Type*} {t : finset ΞΉ} {f : ΞΉ β G} (h : β c β t, f c β K) :
β c in t, f c β K :=
K.to_submonoid.prod_mem h
lemma pow_mem {x : G} (hx : x β K) : β n : β, x ^ n β K := K.to_submonoid.pow_mem hx
lemma gpow_mem {x : G} (hx : x β K) : β n : β€, x ^ n β K
| (n : β) := by { rw [gpow_coe_nat], exact pow_mem _ hx n }
| -[1+ n] := by { rw [gpow_neg_succ_of_nat], exact K.inv_mem (K.pow_mem hx n.succ) }
/-- Construct a subgroup from a nonempty set that is closed under division. -/
@[to_additive "Construct a subgroup from a nonempty set that is closed under subtraction"]
def of_div (s : set G) (hsn : s.nonempty) (hs : β x y β s, x * yβ»ΒΉ β s) : subgroup G :=
have one_mem : (1 : G) β s, from let β¨x, hxβ© := hsn in by simpa using hs x x hx hx,
have inv_mem : β x, x β s β xβ»ΒΉ β s, from Ξ» x hx, by simpa using hs 1 x one_mem hx,
{ carrier := s,
one_mem' := one_mem,
inv_mem' := inv_mem,
mul_mem' := Ξ» x y hx hy, by simpa using hs x yβ»ΒΉ hx (inv_mem y hy) }
/-- A subgroup of a group inherits a multiplication. -/
@[to_additive "An `add_subgroup` of an `add_group` inherits an addition."]
instance has_mul : has_mul H := H.to_submonoid.has_mul
/-- A subgroup of a group inherits a 1. -/
@[to_additive "An `add_subgroup` of an `add_group` inherits a zero."]
instance has_one : has_one H := H.to_submonoid.has_one
/-- A subgroup of a group inherits an inverse. -/
@[to_additive "A `add_subgroup` of a `add_group` inherits an inverse."]
instance has_inv : has_inv H := β¨Ξ» a, β¨aβ»ΒΉ, H.inv_mem a.2β©β©
/-- A subgroup of a group inherits a division -/
@[to_additive "An `add_subgroup` of an `add_group` inherits a subtraction."]
instance has_div : has_div H := β¨Ξ» a b, β¨a / b, H.div_mem a.2 b.2β©β©
@[simp, norm_cast, to_additive] lemma coe_mul (x y : H) : (β(x * y) : G) = βx * βy := rfl
@[simp, norm_cast, to_additive] lemma coe_one : ((1 : H) : G) = 1 := rfl
@[simp, norm_cast, to_additive] lemma coe_inv (x : H) : β(xβ»ΒΉ : H) = (xβ»ΒΉ : G) := rfl
@[simp, norm_cast, to_additive] lemma coe_div (x y : H) : (β(x / y) : G) = βx / βy := rfl
@[simp, norm_cast, to_additive] lemma coe_mk (x : G) (hx : x β H) : ((β¨x, hxβ© : H) : G) = x := rfl
attribute [norm_cast] add_subgroup.coe_add add_subgroup.coe_zero
add_subgroup.coe_neg add_subgroup.coe_mk
/-- A subgroup of a group inherits a group structure. -/
@[to_additive "An `add_subgroup` of an `add_group` inherits an `add_group` structure."]
instance to_group {G : Type*} [group G] (H : subgroup G) : group H :=
subtype.coe_injective.group _ rfl (Ξ» _ _, rfl) (Ξ» _, rfl) (Ξ» _ _, rfl)
/-- A subgroup of a `comm_group` is a `comm_group`. -/
@[to_additive "An `add_subgroup` of an `add_comm_group` is an `add_comm_group`."]
instance to_comm_group {G : Type*} [comm_group G] (H : subgroup G) : comm_group H :=
subtype.coe_injective.comm_group _ rfl (Ξ» _ _, rfl) (Ξ» _, rfl) (Ξ» _ _, rfl)
/-- A subgroup of an `ordered_comm_group` is an `ordered_comm_group`. -/
@[to_additive "An `add_subgroup` of an `add_ordered_comm_group` is an `add_ordered_comm_group`."]
instance to_ordered_comm_group {G : Type*} [ordered_comm_group G] (H : subgroup G) :
ordered_comm_group H :=
subtype.coe_injective.ordered_comm_group _ rfl (Ξ» _ _, rfl) (Ξ» _, rfl) (Ξ» _ _, rfl)
/-- A subgroup of a `linear_ordered_comm_group` is a `linear_ordered_comm_group`. -/
@[to_additive "An `add_subgroup` of a `linear_ordered_add_comm_group` is a
`linear_ordered_add_comm_group`."]
instance to_linear_ordered_comm_group {G : Type*} [linear_ordered_comm_group G]
(H : subgroup G) : linear_ordered_comm_group H :=
subtype.coe_injective.linear_ordered_comm_group _ rfl (Ξ» _ _, rfl) (Ξ» _, rfl) (Ξ» _ _, rfl)
/-- The natural group hom from a subgroup of group `G` to `G`. -/
@[to_additive "The natural group hom from an `add_subgroup` of `add_group` `G` to `G`."]
def subtype : H β* G := β¨coe, rfl, Ξ» _ _, rflβ©
@[simp, to_additive] theorem coe_subtype : βH.subtype = coe := rfl
@[simp, norm_cast] lemma coe_pow (x : H) (n : β) : ((x ^ n : H) : G) = x ^ n :=
coe_subtype H βΈ monoid_hom.map_pow _ _ _
@[simp, norm_cast] lemma coe_gpow (x : H) (n : β€) : ((x ^ n : H) : G) = x ^ n :=
coe_subtype H βΈ monoid_hom.map_gpow _ _ _
/-- The inclusion homomorphism from a subgroup `H` contained in `K` to `K`. -/
@[to_additive "The inclusion homomorphism from a additive subgroup `H` contained in `K` to `K`."]
def inclusion {H K : subgroup G} (h : H β€ K) : H β* K :=
monoid_hom.mk' (Ξ» x, β¨x, h x.propβ©) (Ξ» β¨a, haβ© β¨b, hbβ©, rfl)
@[simp, to_additive]
lemma coe_inclusion {H K : subgroup G} {h : H β€ K} (a : H) : (inclusion h a : G) = a :=
by { cases a, simp only [inclusion, coe_mk, monoid_hom.mk'_apply] }
@[simp, to_additive]
lemma subtype_comp_inclusion {H K : subgroup G} (hH : H β€ K) :
K.subtype.comp (inclusion hH) = H.subtype :=
by { ext, simp }
/-- The subgroup `G` of the group `G`. -/
@[to_additive "The `add_subgroup G` of the `add_group G`."]
instance : has_top (subgroup G) :=
β¨{ inv_mem' := Ξ» _ _, set.mem_univ _ , .. (β€ : submonoid G) }β©
/-- The trivial subgroup `{1}` of an group `G`. -/
@[to_additive "The trivial `add_subgroup` `{0}` of an `add_group` `G`."]
instance : has_bot (subgroup G) :=
β¨{ inv_mem' := Ξ» _, by simp *, .. (β₯ : submonoid G) }β©
@[to_additive]
instance : inhabited (subgroup G) := β¨β₯β©
@[simp, to_additive] lemma mem_bot {x : G} : x β (β₯ : subgroup G) β x = 1 := iff.rfl
@[simp, to_additive] lemma mem_top (x : G) : x β (β€ : subgroup G) := set.mem_univ x
@[simp, to_additive] lemma coe_top : ((β€ : subgroup G) : set G) = set.univ := rfl
@[simp, to_additive] lemma coe_bot : ((β₯ : subgroup G) : set G) = {1} := rfl
@[to_additive] lemma eq_bot_iff_forall : H = β₯ β β x β H, x = (1 : G) :=
begin
rw set_like.ext'_iff,
simp only [coe_bot, set.eq_singleton_iff_unique_mem, set_like.mem_coe, H.one_mem, true_and],
end
@[to_additive] lemma eq_bot_of_subsingleton [subsingleton H] : H = β₯ :=
begin
rw subgroup.eq_bot_iff_forall,
intros y hy,
rw [β subgroup.coe_mk H y hy, subsingleton.elim (β¨y, hyβ© : H) 1, subgroup.coe_one],
end
@[to_additive] instance fintype_bot : fintype (β₯ : subgroup G) := β¨{1},
by {rintro β¨x, β¨hxβ©β©, exact finset.mem_singleton_self _}β©
@[simp] lemma _root_.add_subgroup.card_bot : fintype.card (β₯ : add_subgroup A) = 1 :=
fintype.card_eq_one_iff.2
β¨β¨(0 : A), set.mem_singleton 0β©, Ξ» β¨y, hyβ©, subtype.eq $ add_subgroup.mem_bot.1 hyβ©
-- `@[to_additive]` doesn't work, because it converts the `1 : β` to `0`.
@[simp] lemma card_bot : fintype.card (β₯ : subgroup G) = 1 :=
fintype.card_eq_one_iff.2
β¨β¨(1 : G), set.mem_singleton 1β©, Ξ» β¨y, hyβ©, subtype.eq $ subgroup.mem_bot.1 hyβ©
@[to_additive] lemma eq_top_of_card_eq [fintype H] [fintype G]
(h : fintype.card H = fintype.card G) : H = β€ :=
begin
haveI : fintype (H : set G) := βΉfintype HβΊ,
rw [set_like.ext'_iff, coe_top, β finset.coe_univ, β (H : set G).coe_to_finset, finset.coe_inj,
β finset.card_eq_iff_eq_univ, β h, set.to_finset_card],
congr
end
@[to_additive] lemma eq_top_of_le_card [fintype H] [fintype G]
(h : fintype.card G β€ fintype.card H) : H = β€ :=
eq_top_of_card_eq H (le_antisymm (fintype.card_le_of_injective coe subtype.coe_injective) h)
@[to_additive] lemma eq_bot_of_card_le [fintype H] (h : fintype.card H β€ 1) : H = β₯ :=
let _ := fintype.card_le_one_iff_subsingleton.mp h in by exactI eq_bot_of_subsingleton H
@[to_additive] lemma eq_bot_of_card_eq [fintype H] (h : fintype.card H = 1) : H = β₯ :=
H.eq_bot_of_card_le (le_of_eq h)
@[to_additive] lemma nontrivial_iff_exists_ne_one (H : subgroup G) :
nontrivial H β β x β H, x β (1:G) :=
subtype.nontrivial_iff_exists_ne (Ξ» x, x β H) (1 : H)
/-- A subgroup is either the trivial subgroup or nontrivial. -/
@[to_additive] lemma bot_or_nontrivial (H : subgroup G) : H = β₯ β¨ nontrivial H :=
begin
classical,
by_cases h : β x β H, x = (1 : G),
{ left,
exact H.eq_bot_iff_forall.mpr h },
{ right,
push_neg at h,
simpa [nontrivial_iff_exists_ne_one] using h },
end
/-- A subgroup is either the trivial subgroup or contains a nonzero element. -/
@[to_additive] lemma bot_or_exists_ne_one (H : subgroup G) : H = β₯ β¨ β x β H, x β (1:G) :=
begin
convert H.bot_or_nontrivial,
rw nontrivial_iff_exists_ne_one
end
/-- The inf of two subgroups is their intersection. -/
@[to_additive "The inf of two `add_subgroups`s is their intersection."]
instance : has_inf (subgroup G) :=
β¨Ξ» Hβ Hβ,
{ inv_mem' := Ξ» _ β¨hx, hx'β©, β¨Hβ.inv_mem hx, Hβ.inv_mem hx'β©,
.. Hβ.to_submonoid β Hβ.to_submonoid }β©
@[simp, to_additive]
lemma coe_inf (p p' : subgroup G) : ((p β p' : subgroup G) : set G) = p β© p' := rfl
@[simp, to_additive]
lemma mem_inf {p p' : subgroup G} {x : G} : x β p β p' β x β p β§ x β p' := iff.rfl
@[to_additive]
instance : has_Inf (subgroup G) :=
β¨Ξ» s,
{ inv_mem' := Ξ» x hx, set.mem_bInter $ Ξ» i h, i.inv_mem (by apply set.mem_bInter_iff.1 hx i h),
.. (β¨
S β s, subgroup.to_submonoid S).copy (β S β s, βS) (by simp) }β©
@[simp, to_additive]
lemma coe_Inf (H : set (subgroup G)) : ((Inf H : subgroup G) : set G) = β s β H, βs := rfl
attribute [norm_cast] coe_Inf add_subgroup.coe_Inf
@[simp, to_additive]
lemma mem_Inf {S : set (subgroup G)} {x : G} : x β Inf S β β p β S, x β p := set.mem_bInter_iff
@[to_additive]
lemma mem_infi {ΞΉ : Sort*} {S : ΞΉ β subgroup G} {x : G} : (x β β¨
i, S i) β β i, x β S i :=
by simp only [infi, mem_Inf, set.forall_range_iff]
@[simp, to_additive]
lemma coe_infi {ΞΉ : Sort*} {S : ΞΉ β subgroup G} : (β(β¨
i, S i) : set G) = β i, S i :=
by simp only [infi, coe_Inf, set.bInter_range]
attribute [norm_cast] coe_infi add_subgroup.coe_infi
/-- Subgroups of a group form a complete lattice. -/
@[to_additive "The `add_subgroup`s of an `add_group` form a complete lattice."]
instance : complete_lattice (subgroup G) :=
{ bot := (β₯),
bot_le := Ξ» S x hx, (mem_bot.1 hx).symm βΈ S.one_mem,
top := (β€),
le_top := Ξ» S x hx, mem_top x,
inf := (β),
le_inf := Ξ» a b c ha hb x hx, β¨ha hx, hb hxβ©,
inf_le_left := Ξ» a b x, and.left,
inf_le_right := Ξ» a b x, and.right,
.. complete_lattice_of_Inf (subgroup G) $ Ξ» s, is_glb.of_image
(Ξ» H K, show (H : set G) β€ K β H β€ K, from set_like.coe_subset_coe) is_glb_binfi }
@[to_additive]
lemma mem_sup_left {S T : subgroup G} : β {x : G}, x β S β x β S β T :=
show S β€ S β T, from le_sup_left
@[to_additive]
lemma mem_sup_right {S T : subgroup G} : β {x : G}, x β T β x β S β T :=
show T β€ S β T, from le_sup_right
@[to_additive]
lemma mem_supr_of_mem {ΞΉ : Type*} {S : ΞΉ β subgroup G} (i : ΞΉ) :
β {x : G}, x β S i β x β supr S :=
show S i β€ supr S, from le_supr _ _
@[to_additive]
lemma mem_Sup_of_mem {S : set (subgroup G)} {s : subgroup G}
(hs : s β S) : β {x : G}, x β s β x β Sup S :=
show s β€ Sup S, from le_Sup hs
@[to_additive]
lemma subsingleton_iff : subsingleton G β subsingleton (subgroup G) :=
β¨ Ξ» h, by exactI β¨Ξ» x y, subgroup.ext $ Ξ» i, subsingleton.elim 1 i βΈ by simp [subgroup.one_mem]β©,
Ξ» h, by exactI β¨Ξ» x y,
have β i : G, i = 1 := Ξ» i, mem_bot.mp $ subsingleton.elim (β€ : subgroup G) β₯ βΈ mem_top i,
(this x).trans (this y).symmβ©β©
@[to_additive]
lemma nontrivial_iff : nontrivial G β nontrivial (subgroup G) :=
not_iff_not.mp (
(not_nontrivial_iff_subsingleton.trans subsingleton_iff).trans
not_nontrivial_iff_subsingleton.symm)
@[to_additive]
instance [subsingleton G] : unique (subgroup G) :=
β¨β¨β₯β©, Ξ» a, @subsingleton.elim _ (subsingleton_iff.mp βΉ_βΊ) a _β©
@[to_additive]
instance [nontrivial G] : nontrivial (subgroup G) := nontrivial_iff.mp βΉ_βΊ
@[to_additive] lemma eq_top_iff' : H = β€ β β x : G, x β H :=
eq_top_iff.trans β¨Ξ» h m, h $ mem_top m, Ξ» h m _, h mβ©
/-- The `subgroup` generated by a set. -/
@[to_additive "The `add_subgroup` generated by a set"]
def closure (k : set G) : subgroup G := Inf {K | k β K}
variable {k : set G}
@[to_additive]
lemma mem_closure {x : G} : x β closure k β β K : subgroup G, k β K β x β K :=
mem_Inf
/-- The subgroup generated by a set includes the set. -/
@[simp, to_additive "The `add_subgroup` generated by a set includes the set."]
lemma subset_closure : k β closure k := Ξ» x hx, mem_closure.2 $ Ξ» K hK, hK hx
open set
/-- A subgroup `K` includes `closure k` if and only if it includes `k`. -/
@[simp, to_additive "An additive subgroup `K` includes `closure k` if and only if it includes `k`"]
lemma closure_le : closure k β€ K β k β K :=
β¨subset.trans subset_closure, Ξ» h, Inf_le hβ©
@[to_additive]
lemma closure_eq_of_le (hβ : k β K) (hβ : K β€ closure k) : closure k = K :=
le_antisymm ((closure_le $ K).2 hβ) hβ
/-- An induction principle for closure membership. If `p` holds for `1` and all elements of `k`, and
is preserved under multiplication and inverse, then `p` holds for all elements of the closure
of `k`. -/
@[to_additive "An induction principle for additive closure membership. If `p` holds for `0` and all
elements of `k`, and is preserved under addition and inverses, then `p` holds for all elements
of the additive closure of `k`."]
lemma closure_induction {p : G β Prop} {x} (h : x β closure k)
(Hk : β x β k, p x) (H1 : p 1)
(Hmul : β x y, p x β p y β p (x * y))
(Hinv : β x, p x β p xβ»ΒΉ) : p x :=
(@closure_le _ _ β¨p, H1, Hmul, Hinvβ© _).2 Hk h
attribute [elab_as_eliminator] subgroup.closure_induction add_subgroup.closure_induction
/-- An induction principle on elements of the subtype `subgroup.closure`.
If `p` holds for `1` and all elements of `k`, and is preserved under multiplication and inverse,
then `p` holds for all elements `x : closure k`.
The difference with `subgroup.closure_induction` is that this acts on the subtype.
-/
@[to_additive "An induction principle on elements of the subtype `add_subgroup.closure`.
If `p` holds for `0` and all elements of `k`, and is preserved under addition and negation,
then `p` holds for all elements `x : closure k`.
The difference with `add_subgroup.closure_induction` is that this acts on the subtype."]
lemma closure_induction' (k : set G) {p : closure k β Prop}
(Hk : β x (h : x β k), p β¨x, subset_closure hβ©)
(H1 : p 1)
(Hmul : β x y, p x β p y β p (x * y))
(Hinv : β x, p x β p xβ»ΒΉ)
(x : closure k) :
p x :=
subtype.rec_on x $ Ξ» x hx, begin
refine exists.elim _ (Ξ» (hx : x β closure k) (hc : p β¨x, hxβ©), hc),
exact closure_induction hx
(Ξ» x hx, β¨subset_closure hx, Hk x hxβ©)
β¨one_mem _, H1β©
(Ξ» x y hx hy, exists.elim hx $ Ξ» hx' hx, exists.elim hy $ Ξ» hy' hy,
β¨mul_mem _ hx' hy', Hmul _ _ hx hyβ©)
(Ξ» x hx, exists.elim hx $ Ξ» hx' hx, β¨inv_mem _ hx', Hinv _ hxβ©),
end
attribute [elab_as_eliminator] subgroup.closure_induction' add_subgroup.closure_induction'
variable (G)
/-- `closure` forms a Galois insertion with the coercion to set. -/
@[to_additive "`closure` forms a Galois insertion with the coercion to set."]
protected def gi : galois_insertion (@closure G _) coe :=
{ choice := Ξ» s _, closure s,
gc := Ξ» s t, @closure_le _ _ t s,
le_l_u := Ξ» s, subset_closure,
choice_eq := Ξ» s h, rfl }
variable {G}
/-- Subgroup closure of a set is monotone in its argument: if `h β k`,
then `closure h β€ closure k`. -/
@[to_additive "Additive subgroup closure of a set is monotone in its argument: if `h β k`,
then `closure h β€ closure k`"]
lemma closure_mono β¦h k : set Gβ¦ (h' : h β k) : closure h β€ closure k :=
(subgroup.gi G).gc.monotone_l h'
/-- Closure of a subgroup `K` equals `K`. -/
@[simp, to_additive "Additive closure of an additive subgroup `K` equals `K`"]
lemma closure_eq : closure (K : set G) = K := (subgroup.gi G).l_u_eq K
@[simp, to_additive] lemma closure_empty : closure (β
: set G) = β₯ :=
(subgroup.gi G).gc.l_bot
@[simp, to_additive] lemma closure_univ : closure (univ : set G) = β€ :=
@coe_top G _ βΈ closure_eq β€
@[to_additive]
lemma closure_union (s t : set G) : closure (s βͺ t) = closure s β closure t :=
(subgroup.gi G).gc.l_sup
@[to_additive]
lemma closure_Union {ΞΉ} (s : ΞΉ β set G) : closure (β i, s i) = β¨ i, closure (s i) :=
(subgroup.gi G).gc.l_supr
@[to_additive]
lemma closure_eq_bot_iff (G : Type*) [group G] (S : set G) :
closure S = β₯ β S β {1} :=
by { rw [β le_bot_iff], exact closure_le _}
/-- The subgroup generated by an element of a group equals the set of integer number powers of
the element. -/
lemma mem_closure_singleton {x y : G} : y β closure ({x} : set G) β β n : β€, x ^ n = y :=
begin
refine β¨Ξ» hy, closure_induction hy _ _ _ _,
Ξ» β¨n, hnβ©, hn βΈ gpow_mem _ (subset_closure $ mem_singleton x) nβ©,
{ intros y hy,
rw [eq_of_mem_singleton hy],
exact β¨1, gpow_one xβ© },
{ exact β¨0, gpow_zero xβ© },
{ rintros _ _ β¨n, rflβ© β¨m, rflβ©,
exact β¨n + m, gpow_add x n mβ© },
rintros _ β¨n, rflβ©,
exact β¨-n, gpow_neg x nβ©
end
lemma closure_singleton_one : closure ({1} : set G) = β₯ :=
by simp [eq_bot_iff_forall, mem_closure_singleton]
@[simp, to_additive] lemma inv_subset_closure (S : set G) : Sβ»ΒΉ β closure S :=
begin
intros s hs,
rw [set_like.mem_coe, βsubgroup.inv_mem_iff],
exact subset_closure (mem_inv.mp hs),
end
@[simp, to_additive] lemma closure_inv (S : set G) : closure Sβ»ΒΉ = closure S :=
begin
refine le_antisymm ((subgroup.closure_le _).2 _) ((subgroup.closure_le _).2 _),
{ exact inv_subset_closure S },
{ simpa only [set.inv_inv] using inv_subset_closure Sβ»ΒΉ },
end
@[to_additive]
lemma closure_to_submonoid (S : set G) :
(closure S).to_submonoid = submonoid.closure (S βͺ Sβ»ΒΉ) :=
begin
refine le_antisymm _ (submonoid.closure_le.2 _),
{ intros x hx,
refine closure_induction hx (Ξ» x hx, submonoid.closure_mono (subset_union_left S Sβ»ΒΉ)
(submonoid.subset_closure hx)) (submonoid.one_mem _) (Ξ» x y hx hy, submonoid.mul_mem _ hx hy)
(Ξ» x hx, _),
rwa [βsubmonoid.mem_closure_inv, set.union_inv, set.inv_inv, set.union_comm] },
{ simp only [true_and, coe_to_submonoid, union_subset_iff, subset_closure, inv_subset_closure] }
end
/-- An induction principle for closure membership. If `p` holds for `1` and all elements of
`k` and their inverse, and is preserved under multiplication, then `p` holds for all elements of
the closure of `k`. -/
@[to_additive "An induction principle for additive closure membership. If `p` holds for `0` and all
elements of `k` and their negation, and is preserved under addition, then `p` holds for all
elements of the additive closure of `k`."]
lemma closure_induction'' {p : G β Prop} {x} (h : x β closure k)
(Hk : β x β k, p x) (Hk_inv : β x β k, p xβ»ΒΉ) (H1 : p 1)
(Hmul : β x y, p x β p y β p (x * y)) : p x :=
begin
rw [β mem_to_submonoid, closure_to_submonoid k] at h,
refine submonoid.closure_induction h (Ξ» x hx, _) H1 (Ξ» x y hx hy, Hmul x y hx hy),
{ rw [mem_union, mem_inv] at hx,
cases hx with mem invmem,
{ exact Hk x mem },
{ rw [β inv_inv x],
exact Hk_inv _ invmem } },
end
@[to_additive]
lemma mem_supr_of_directed {ΞΉ} [hΞΉ : nonempty ΞΉ] {K : ΞΉ β subgroup G} (hK : directed (β€) K)
{x : G} :
x β (supr K : subgroup G) β β i, x β K i :=
begin
refine β¨_, Ξ» β¨i, hiβ©, (set_like.le_def.1 $ le_supr K i) hiβ©,
suffices : x β closure (β i, (K i : set G)) β β i, x β K i,
by simpa only [closure_Union, closure_eq (K _)] using this,
refine (Ξ» hx, closure_induction hx (Ξ» _, mem_Union.1) _ _ _),
{ exact hΞΉ.elim (Ξ» i, β¨i, (K i).one_memβ©) },
{ rintros x y β¨i, hiβ© β¨j, hjβ©,
rcases hK i j with β¨k, hki, hkjβ©,
exact β¨k, (K k).mul_mem (hki hi) (hkj hj)β© },
rintros _ β¨i, hiβ©, exact β¨i, inv_mem (K i) hiβ©
end
@[to_additive]
lemma coe_supr_of_directed {ΞΉ} [nonempty ΞΉ] {S : ΞΉ β subgroup G} (hS : directed (β€) S) :
((β¨ i, S i : subgroup G) : set G) = β i, β(S i) :=
set.ext $ Ξ» x, by simp [mem_supr_of_directed hS]
@[to_additive]
lemma mem_Sup_of_directed_on {K : set (subgroup G)} (Kne : K.nonempty)
(hK : directed_on (β€) K) {x : G} :
x β Sup K β β s β K, x β s :=
begin
haveI : nonempty K := Kne.to_subtype,
simp only [Sup_eq_supr', mem_supr_of_directed hK.directed_coe, set_coe.exists, subtype.coe_mk]
end
variables {N : Type*} [group N] {P : Type*} [group P]
/-- The preimage of a subgroup along a monoid homomorphism is a subgroup. -/
@[to_additive "The preimage of an `add_subgroup` along an `add_monoid` homomorphism
is an `add_subgroup`."]
def comap {N : Type*} [group N] (f : G β* N)
(H : subgroup N) : subgroup G :=
{ carrier := (f β»ΒΉ' H),
inv_mem' := Ξ» a ha,
show f aβ»ΒΉ β H, by rw f.map_inv; exact H.inv_mem ha,
.. H.to_submonoid.comap f }
@[simp, to_additive]
lemma coe_comap (K : subgroup N) (f : G β* N) : (K.comap f : set G) = f β»ΒΉ' K := rfl
@[simp, to_additive]
lemma mem_comap {K : subgroup N} {f : G β* N} {x : G} : x β K.comap f β f x β K := iff.rfl
@[to_additive]
lemma comap_mono {f : G β* N} {K K' : subgroup N} : K β€ K' β comap f K β€ comap f K' :=
preimage_mono
@[to_additive]
lemma comap_comap (K : subgroup P) (g : N β* P) (f : G β* N) :
(K.comap g).comap f = K.comap (g.comp f) :=
rfl
/-- The image of a subgroup along a monoid homomorphism is a subgroup. -/
@[to_additive "The image of an `add_subgroup` along an `add_monoid` homomorphism
is an `add_subgroup`."]
def map (f : G β* N) (H : subgroup G) : subgroup N :=
{ carrier := (f '' H),
inv_mem' := by { rintros _ β¨x, hx, rflβ©, exact β¨xβ»ΒΉ, H.inv_mem hx, f.map_inv xβ© },
.. H.to_submonoid.map f }
@[simp, to_additive]
lemma coe_map (f : G β* N) (K : subgroup G) :
(K.map f : set N) = f '' K := rfl
@[simp, to_additive]
lemma mem_map {f : G β* N} {K : subgroup G} {y : N} :
y β K.map f β β x β K, f x = y :=
mem_image_iff_bex
@[to_additive]
lemma mem_map_of_mem (f : G β* N) {K : subgroup G} {x : G} (hx : x β K) : f x β K.map f :=
mem_image_of_mem f hx
@[to_additive]
lemma apply_coe_mem_map (f : G β* N) (K : subgroup G) (x : K) : f x β K.map f :=
mem_map_of_mem f x.prop
@[to_additive]
lemma map_mono {f : G β* N} {K K' : subgroup G} : K β€ K' β map f K β€ map f K' :=
image_subset _
@[to_additive]
lemma map_map (g : N β* P) (f : G β* N) : (K.map f).map g = K.map (g.comp f) :=
set_like.coe_injective $ image_image _ _ _
@[to_additive]
lemma mem_map_equiv {f : G β* N} {K : subgroup G} {x : N} :
x β K.map f.to_monoid_hom β f.symm x β K :=
@set.mem_image_equiv _ _ βK f.to_equiv x
@[to_additive]
lemma map_equiv_eq_comap_symm (f : G β* N) (K : subgroup G) :
K.map f.to_monoid_hom = K.comap f.symm.to_monoid_hom :=
set_like.coe_injective (f.to_equiv.image_eq_preimage K)
@[to_additive]
lemma comap_equiv_eq_map_symm (f : N β* G) (K : subgroup G) :
K.comap f.to_monoid_hom = K.map f.symm.to_monoid_hom :=
(map_equiv_eq_comap_symm f.symm K).symm
@[to_additive]
lemma map_le_iff_le_comap {f : G β* N} {K : subgroup G} {H : subgroup N} :
K.map f β€ H β K β€ H.comap f :=
image_subset_iff
@[to_additive]
lemma gc_map_comap (f : G β* N) : galois_connection (map f) (comap f) :=
Ξ» _ _, map_le_iff_le_comap
@[to_additive]
lemma map_sup (H K : subgroup G) (f : G β* N) : (H β K).map f = H.map f β K.map f :=
(gc_map_comap f).l_sup
@[to_additive]
lemma map_supr {ΞΉ : Sort*} (f : G β* N) (s : ΞΉ β subgroup G) :
(supr s).map f = β¨ i, (s i).map f :=
(gc_map_comap f).l_supr
@[to_additive] lemma comap_sup_comap_le
(H K : subgroup N) (f : G β* N) : comap f H β comap f K β€ comap f (H β K) :=
monotone.le_map_sup (Ξ» _ _, comap_mono) H K
@[to_additive] lemma supr_comap_le {ΞΉ : Sort*} (f : G β* N) (s : ΞΉ β subgroup N) :
(β¨ i, (s i).comap f) β€ (supr s).comap f :=
monotone.le_map_supr (Ξ» _ _, comap_mono)
@[to_additive]
lemma comap_inf (H K : subgroup N) (f : G β* N) : (H β K).comap f = H.comap f β K.comap f :=
(gc_map_comap f).u_inf
@[to_additive]
lemma comap_infi {ΞΉ : Sort*} (f : G β* N) (s : ΞΉ β subgroup N) :
(infi s).comap f = β¨
i, (s i).comap f :=
(gc_map_comap f).u_infi
@[to_additive] lemma map_inf_le (H K : subgroup G) (f : G β* N) :
map f (H β K) β€ map f H β map f K :=
le_inf (map_mono inf_le_left) (map_mono inf_le_right)
@[to_additive] lemma map_inf_eq (H K : subgroup G) (f : G β* N) (hf : function.injective f) :
map f (H β K) = map f H β map f K :=
begin
rw β set_like.coe_set_eq,
simp [set.image_inter hf],
end
@[simp, to_additive] lemma map_bot (f : G β* N) : (β₯ : subgroup G).map f = β₯ :=
(gc_map_comap f).l_bot
@[simp, to_additive] lemma comap_top (f : G β* N) : (β€ : subgroup N).comap f = β€ :=
(gc_map_comap f).u_top
@[simp, to_additive]
lemma comap_subtype_inf_left {H K : subgroup G} : comap H.subtype (H β K) = comap H.subtype K :=
ext $ Ξ» x, and_iff_right_of_imp (Ξ» _, x.prop)
@[simp, to_additive]
lemma comap_subtype_inf_right {H K : subgroup G} : comap K.subtype (H β K) = comap K.subtype H :=
ext $ Ξ» x, and_iff_left_of_imp (Ξ» _, x.prop)
/-- For any subgroups `H` and `K`, view `H β K` as a subgroup of `K`. -/
@[to_additive "For any subgroups `H` and `K`, view `H β K` as a subgroup of `K`."]
def subgroup_of (H K : subgroup G) : subgroup K := H.comap K.subtype
@[to_additive] lemma coe_subgroup_of (H K : subgroup G) :
(H.subgroup_of K : set K) = K.subtype β»ΒΉ' H := rfl
@[to_additive] lemma mem_subgroup_of {H K : subgroup G} {h : K} :
h β H.subgroup_of K β (h : G) β H :=
iff.rfl
@[to_additive] lemma subgroup_of_map_subtype (H K : subgroup G) :
(H.subgroup_of K).map K.subtype = H β K := set_like.ext'
begin
convert set.image_preimage_eq_inter_range,
simp only [subtype.range_coe_subtype, coe_subtype, coe_inf],
refl,
end
/-- Given `subgroup`s `H`, `K` of groups `G`, `N` respectively, `H Γ K` as a subgroup of `G Γ N`. -/
@[to_additive prod "Given `add_subgroup`s `H`, `K` of `add_group`s `A`, `B` respectively, `H Γ K`
as an `add_subgroup` of `A Γ B`."]
def prod (H : subgroup G) (K : subgroup N) : subgroup (G Γ N) :=
{ inv_mem' := Ξ» _ hx, β¨H.inv_mem' hx.1, K.inv_mem' hx.2β©,
.. submonoid.prod H.to_submonoid K.to_submonoid}
@[to_additive coe_prod]
lemma coe_prod (H : subgroup G) (K : subgroup N) :
(H.prod K : set (G Γ N)) = (H : set G).prod (K : set N) := rfl
@[to_additive mem_prod]
lemma mem_prod {H : subgroup G} {K : subgroup N} {p : G Γ N} :
p β H.prod K β p.1 β H β§ p.2 β K := iff.rfl
@[to_additive prod_mono]
lemma prod_mono : ((β€) β (β€) β (β€)) (@prod G _ N _) (@prod G _ N _) :=
Ξ» s s' hs t t' ht, set.prod_mono hs ht
@[to_additive prod_mono_right]
lemma prod_mono_right (K : subgroup G) : monotone (Ξ» t : subgroup N, K.prod t) :=
prod_mono (le_refl K)
@[to_additive prod_mono_left]
lemma prod_mono_left (H : subgroup N) : monotone (Ξ» K : subgroup G, K.prod H) :=
Ξ» sβ sβ hs, prod_mono hs (le_refl H)
@[to_additive prod_top]
lemma prod_top (K : subgroup G) :
K.prod (β€ : subgroup N) = K.comap (monoid_hom.fst G N) :=
ext $ Ξ» x, by simp [mem_prod, monoid_hom.coe_fst]
@[to_additive top_prod]
lemma top_prod (H : subgroup N) :
(β€ : subgroup G).prod H = H.comap (monoid_hom.snd G N) :=
ext $ Ξ» x, by simp [mem_prod, monoid_hom.coe_snd]
@[simp, to_additive top_prod_top]
lemma top_prod_top : (β€ : subgroup G).prod (β€ : subgroup N) = β€ :=
(top_prod _).trans $ comap_top _
@[to_additive] lemma bot_prod_bot : (β₯ : subgroup G).prod (β₯ : subgroup N) = β₯ :=
set_like.coe_injective $ by simp [coe_prod, prod.one_eq_mk]
/-- Product of subgroups is isomorphic to their product as groups. -/
@[to_additive prod_equiv "Product of additive subgroups is isomorphic to their product
as additive groups"]
def prod_equiv (H : subgroup G) (K : subgroup N) : H.prod K β* H Γ K :=
{ map_mul' := Ξ» x y, rfl, .. equiv.set.prod βH βK }
/-- A subgroup is normal if whenever `n β H`, then `g * n * gβ»ΒΉ β H` for every `g : G` -/
structure normal : Prop :=
(conj_mem : β n, n β H β β g : G, g * n * gβ»ΒΉ β H)
attribute [class] normal
end subgroup
namespace add_subgroup
/-- An add_subgroup is normal if whenever `n β H`, then `g + n - g β H` for every `g : G` -/
structure normal (H : add_subgroup A) : Prop :=
(conj_mem [] : β n, n β H β β g : A, g + n + -g β H)
attribute [to_additive add_subgroup.normal] subgroup.normal
attribute [class] normal
end add_subgroup
namespace subgroup
variables {H K : subgroup G}
@[priority 100, to_additive]
instance normal_of_comm {G : Type*} [comm_group G] (H : subgroup G) : H.normal :=
β¨by simp [mul_comm, mul_left_comm]β©
namespace normal
variable (nH : H.normal)
@[to_additive] lemma mem_comm {a b : G} (h : a * b β H) : b * a β H :=
have aβ»ΒΉ * (a * b) * aβ»ΒΉβ»ΒΉ β H, from nH.conj_mem (a * b) h aβ»ΒΉ, by simpa
@[to_additive] lemma mem_comm_iff {a b : G} : a * b β H β b * a β H :=
β¨nH.mem_comm, nH.mem_commβ©
end normal
@[priority 100, to_additive]
instance bot_normal : normal (β₯ : subgroup G) := β¨by simpβ©
@[priority 100, to_additive]
instance top_normal : normal (β€ : subgroup G) := β¨Ξ» _ _, mem_topβ©
variable (G)
/-- The center of a group `G` is the set of elements that commute with everything in `G` -/
@[to_additive "The center of a group `G` is the set of elements that commute with everything in
`G`"]
def center : subgroup G :=
{ carrier := {z | β g, g * z = z * g},
one_mem' := by simp,
mul_mem' := Ξ» a b (ha : β g, g * a = a * g) (hb : β g, g * b = b * g) g,
by assoc_rw [ha, hb g],
inv_mem' := Ξ» a (ha : β g, g * a = a * g) g,
by rw [β inv_inj, mul_inv_rev, inv_inv, β ha, mul_inv_rev, inv_inv] }
variable {G}
@[to_additive] lemma mem_center_iff {z : G} : z β center G β β g, g * z = z * g := iff.rfl
@[priority 100, to_additive]
instance center_normal : (center G).normal :=
β¨begin
assume n hn g h,
assoc_rw [hn (h * g), hn g],
simp
endβ©
variables {G} (H)
/-- The `normalizer` of `H` is the largest subgroup of `G` inside which `H` is normal. -/
@[to_additive "The `normalizer` of `H` is the largest subgroup of `G` inside which `H` is normal."]
def normalizer : subgroup G :=
{ carrier := {g : G | β n, n β H β g * n * gβ»ΒΉ β H},
one_mem' := by simp,
mul_mem' := Ξ» a b (ha : β n, n β H β a * n * aβ»ΒΉ β H) (hb : β n, n β H β b * n * bβ»ΒΉ β H) n,
by { rw [hb, ha], simp [mul_assoc] },
inv_mem' := Ξ» a (ha : β n, n β H β a * n * aβ»ΒΉ β H) n,
by { rw [ha (aβ»ΒΉ * n * aβ»ΒΉβ»ΒΉ)], simp [mul_assoc] } }
-- variant for sets.
-- TODO should this replace `normalizer`?
/-- The `set_normalizer` of `S` is the subgroup of `G` whose elements satisfy `g*S*gβ»ΒΉ=S` -/
@[to_additive "The `set_normalizer` of `S` is the subgroup of `G` whose elements satisfy
`g+S-g=S`."]
def set_normalizer (S : set G) : subgroup G :=
{ carrier := {g : G | β n, n β S β g * n * gβ»ΒΉ β S},
one_mem' := by simp,
mul_mem' := Ξ» a b (ha : β n, n β S β a * n * aβ»ΒΉ β S) (hb : β n, n β S β b * n * bβ»ΒΉ β S) n,
by { rw [hb, ha], simp [mul_assoc] },
inv_mem' := Ξ» a (ha : β n, n β S β a * n * aβ»ΒΉ β S) n,
by { rw [ha (aβ»ΒΉ * n * aβ»ΒΉβ»ΒΉ)], simp [mul_assoc] } }
lemma mem_normalizer_fintype {S : set G} [fintype S] {x : G}
(h : β n, n β S β x * n * xβ»ΒΉ β S) : x β subgroup.set_normalizer S :=
by haveI := classical.prop_decidable;
haveI := set.fintype_image S (Ξ» n, x * n * xβ»ΒΉ); exact
Ξ» n, β¨h n, Ξ» hβ,
have heq : (Ξ» n, x * n * xβ»ΒΉ) '' S = S := set.eq_of_subset_of_card_le
(Ξ» n β¨y, hyβ©, hy.2 βΈ h y hy.1) (by rw set.card_image_of_injective S conj_injective),
have x * n * xβ»ΒΉ β (Ξ» n, x * n * xβ»ΒΉ) '' S := heq.symm βΈ hβ,
let β¨y, hyβ© := this in conj_injective hy.2 βΈ hy.1β©
variable {H}
@[to_additive] lemma mem_normalizer_iff {g : G} :
g β normalizer H β β n, n β H β g * n * gβ»ΒΉ β H := iff.rfl
@[to_additive] lemma le_normalizer : H β€ normalizer H :=
Ξ» x xH n, by rw [H.mul_mem_cancel_right (H.inv_mem xH), H.mul_mem_cancel_left xH]
@[priority 100, to_additive]
instance normal_in_normalizer : (H.comap H.normalizer.subtype).normal :=
β¨Ξ» x xH g, by simpa using (g.2 x).1 xHβ©
open_locale classical
@[to_additive]
lemma le_normalizer_of_normal [hK : (H.comap K.subtype).normal] (HK : H β€ K) : K β€ H.normalizer :=
Ξ» x hx y, β¨Ξ» yH, hK.conj_mem β¨y, HK yHβ© yH β¨x, hxβ©,
Ξ» yH, by simpa [mem_comap, mul_assoc] using
hK.conj_mem β¨x * y * xβ»ΒΉ, HK yHβ© yH β¨xβ»ΒΉ, K.inv_mem hxβ©β©
variable (H)
/-- Commutivity of a subgroup -/
structure is_commutative : Prop :=
(is_comm : _root_.is_commutative H (*))
attribute [class] is_commutative
/-- Commutivity of an additive subgroup -/
structure _root_.add_subgroup.is_commutative (H : add_subgroup A) : Prop :=
(is_comm : _root_.is_commutative H (+))
attribute [to_additive add_subgroup.is_commutative] subgroup.is_commutative
attribute [class] add_subgroup.is_commutative
/-- A commutative subgroup is commutative -/
@[to_additive] instance is_commutative.comm_group [h : H.is_commutative] : comm_group H :=
{ mul_comm := h.is_comm.comm, .. H.to_group }
instance center.is_commutative : (center G).is_commutative :=
β¨β¨Ξ» a b, subtype.ext (b.2 a)β©β©
end subgroup
namespace group
variables {s : set G}
/-- Given a set `s`, `conjugates_of_set s` is the set of all conjugates of
the elements of `s`. -/
def conjugates_of_set (s : set G) : set G := β a β s, conjugates_of a
lemma mem_conjugates_of_set_iff {x : G} : x β conjugates_of_set s β β a β s, is_conj a x :=
set.mem_bUnion_iff
theorem subset_conjugates_of_set : s β conjugates_of_set s :=
Ξ» (x : G) (h : x β s), mem_conjugates_of_set_iff.2 β¨x, h, is_conj_refl _β©
theorem conjugates_of_set_mono {s t : set G} (h : s β t) :
conjugates_of_set s β conjugates_of_set t :=
set.bUnion_subset_bUnion_left h
lemma conjugates_subset_normal {N : subgroup G} [tn : N.normal] {a : G} (h : a β N) :
conjugates_of a β N :=
by { rintros a hc, obtain β¨c, rflβ© := is_conj_iff.1 hc, exact tn.conj_mem a h c }
theorem conjugates_of_set_subset {s : set G} {N : subgroup G} [N.normal] (h : s β N) :
conjugates_of_set s β N :=
set.bUnion_subset (Ξ» x H, conjugates_subset_normal (h H))
/-- The set of conjugates of `s` is closed under conjugation. -/
lemma conj_mem_conjugates_of_set {x c : G} :
x β conjugates_of_set s β (c * x * cβ»ΒΉ β conjugates_of_set s) :=
Ξ» H,
begin
rcases (mem_conjugates_of_set_iff.1 H) with β¨a,hβ,hββ©,
exact mem_conjugates_of_set_iff.2 β¨a, hβ, is_conj_trans hβ (is_conj_iff.2 β¨c,rflβ©)β©,
end
end group
namespace subgroup
open group
variable {s : set G}
/-- The normal closure of a set `s` is the subgroup closure of all the conjugates of
elements of `s`. It is the smallest normal subgroup containing `s`. -/
def normal_closure (s : set G) : subgroup G := closure (conjugates_of_set s)
theorem conjugates_of_set_subset_normal_closure : conjugates_of_set s β normal_closure s :=
subset_closure
theorem subset_normal_closure : s β normal_closure s :=
set.subset.trans subset_conjugates_of_set conjugates_of_set_subset_normal_closure
theorem le_normal_closure {H : subgroup G} : H β€ normal_closure βH :=
Ξ» _ h, subset_normal_closure h
/-- The normal closure of `s` is a normal subgroup. -/
instance normal_closure_normal : (normal_closure s).normal :=
β¨Ξ» n h g,
begin
refine subgroup.closure_induction h (Ξ» x hx, _) _ (Ξ» x y ihx ihy, _) (Ξ» x ihx, _),
{ exact (conjugates_of_set_subset_normal_closure (conj_mem_conjugates_of_set hx)) },
{ simpa using (normal_closure s).one_mem },
{ rw β conj_mul,
exact mul_mem _ ihx ihy },
{ rw β conj_inv,
exact inv_mem _ ihx }
endβ©
/-- The normal closure of `s` is the smallest normal subgroup containing `s`. -/
theorem normal_closure_le_normal {N : subgroup G} [N.normal]
(h : s β N) : normal_closure s β€ N :=
begin
assume a w,
refine closure_induction w (Ξ» x hx, _) _ (Ξ» x y ihx ihy, _) (Ξ» x ihx, _),
{ exact (conjugates_of_set_subset h hx) },
{ exact subgroup.one_mem _ },
{ exact subgroup.mul_mem _ ihx ihy },
{ exact subgroup.inv_mem _ ihx }
end
lemma normal_closure_subset_iff {N : subgroup G} [N.normal] : s β N β normal_closure s β€ N :=
β¨normal_closure_le_normal, set.subset.trans (subset_normal_closure)β©
theorem normal_closure_mono {s t : set G} (h : s β t) : normal_closure s β€ normal_closure t :=
normal_closure_le_normal (set.subset.trans h subset_normal_closure)
theorem normal_closure_eq_infi : normal_closure s =
β¨
(N : subgroup G) [normal N] (hs : s β N), N :=
le_antisymm
(le_infi (Ξ» N, le_infi (Ξ» hN, by exactI le_infi (normal_closure_le_normal))))
(infi_le_of_le (normal_closure s) (infi_le_of_le (by apply_instance)
(infi_le_of_le subset_normal_closure (le_refl _))))
@[simp] theorem normal_closure_eq_self (H : subgroup G) [H.normal] : normal_closure βH = H :=
le_antisymm (normal_closure_le_normal rfl.subset) (le_normal_closure)
@[simp] theorem normal_closure_idempotent : normal_closure β(normal_closure s) = normal_closure s :=
normal_closure_eq_self _
theorem closure_le_normal_closure {s : set G} : closure s β€ normal_closure s :=
by simp only [subset_normal_closure, closure_le]
@[simp] theorem normal_closure_closure_eq_normal_closure {s : set G} :
normal_closure β(closure s) = normal_closure s :=
le_antisymm (normal_closure_le_normal closure_le_normal_closure)
(normal_closure_mono subset_closure)
end subgroup
namespace add_subgroup
open set
lemma gsmul_mem (H : add_subgroup A) {x : A} (hx : x β H) :
β n : β€, n β’ x β H
| (n : β) := by { rw [gsmul_coe_nat], exact add_submonoid.nsmul_mem H.to_add_submonoid hx n }
| -[1+ n] := begin
rw gsmul_neg_succ_of_nat,
apply H.neg_mem',
exact add_submonoid.nsmul_mem H.to_add_submonoid hx n.succ
end
/-- The `add_subgroup` generated by an element of an `add_group` equals the set of
natural number multiples of the element. -/
lemma mem_closure_singleton {x y : A} :
y β closure ({x} : set A) β β n : β€, n β’ x = y :=
begin
refine β¨Ξ» hy, closure_induction hy _ _ _ _,
Ξ» β¨n, hnβ©, hn βΈ gsmul_mem _ (subset_closure $ mem_singleton x) nβ©,
{ intros y hy,
rw [eq_of_mem_singleton hy],
exact β¨1, one_gsmul xβ© },
{ exact β¨0, zero_gsmul xβ© },
{ rintros _ _ β¨n, rflβ© β¨m, rflβ©,
exact β¨n + m, add_gsmul x n mβ© },
{ rintros _ β¨n, rflβ©,
refine β¨-n, neg_gsmul x nβ© }
end
lemma closure_singleton_zero : closure ({0} : set A) = β₯ :=
by simp [eq_bot_iff_forall, mem_closure_singleton]
variable (H : add_subgroup A)
@[simp] lemma coe_smul (x : H) (n : β) : ((n β’ x : H) : A) = n β’ x :=
coe_subtype H βΈ add_monoid_hom.map_nsmul _ _ _
@[simp] lemma coe_gsmul (x : H) (n : β€) : ((n β’ x : H) : A) = n β’ x :=
coe_subtype H βΈ add_monoid_hom.map_gsmul _ _ _
attribute [to_additive add_subgroup.coe_smul] subgroup.coe_pow
attribute [to_additive add_subgroup.coe_gsmul] subgroup.coe_gpow
end add_subgroup
namespace monoid_hom
variables {N : Type*} {P : Type*} [group N] [group P] (K : subgroup G)
open subgroup
/-- The range of a monoid homomorphism from a group is a subgroup. -/
@[to_additive "The range of an `add_monoid_hom` from an `add_group` is an `add_subgroup`."]
def range (f : G β* N) : subgroup N :=
subgroup.copy ((β€ : subgroup G).map f) (set.range f) (by simp [set.ext_iff])
@[to_additive]
instance decidable_mem_range (f : G β* N) [fintype G] [decidable_eq N] :
decidable_pred (β f.range) :=
Ξ» x, fintype.decidable_exists_fintype
@[simp, to_additive] lemma coe_range (f : G β* N) :
(f.range : set N) = set.range f := rfl
@[simp, to_additive] lemma mem_range {f : G β* N} {y : N} :
y β f.range β β x, f x = y :=
iff.rfl
@[to_additive] lemma range_eq_map (f : G β* N) : f.range = (β€ : subgroup G).map f :=
by ext; simp
/-- The canonical surjective group homomorphism `G β* f(G)` induced by a group
homomorphism `G β* N`. -/
@[to_additive "The canonical surjective `add_group` homomorphism `G β+ f(G)` induced by a group
homomorphism `G β+ N`."]
def range_restrict (f : G β* N) : G β* f.range :=
monoid_hom.mk' (Ξ» g, β¨f g, β¨g, rflβ©β©) $ Ξ» a b, by {ext, exact f.map_mul' _ _}
@[simp, to_additive]
lemma coe_range_restrict (f : G β* N) (g : G) : (f.range_restrict g : N) = f g := rfl
@[to_additive]
lemma map_range (g : N β* P) (f : G β* N) : f.range.map g = (g.comp f).range :=
by rw [range_eq_map, range_eq_map]; exact (β€ : subgroup G).map_map g f
@[to_additive]
lemma range_top_iff_surjective {N} [group N] {f : G β* N} :
f.range = (β€ : subgroup N) β function.surjective f :=
set_like.ext'_iff.trans $ iff.trans (by rw [coe_range, coe_top]) set.range_iff_surjective
/-- The range of a surjective monoid homomorphism is the whole of the codomain. -/
@[to_additive "The range of a surjective `add_monoid` homomorphism is the whole of the codomain."]
lemma range_top_of_surjective {N} [group N] (f : G β* N) (hf : function.surjective f) :
f.range = (β€ : subgroup N) :=
range_top_iff_surjective.2 hf
@[simp, to_additive] lemma _root_.subgroup.subtype_range (H : subgroup G) : H.subtype.range = H :=
by { rw [range_eq_map, β set_like.coe_set_eq, coe_map, subgroup.coe_subtype], ext, simp }
/-- Restriction of a group hom to a subgroup of the domain. -/
@[to_additive "Restriction of an `add_group` hom to an `add_subgroup` of the domain."]
def restrict (f : G β* N) (H : subgroup G) : H β* N :=
f.comp H.subtype
@[simp, to_additive]
lemma restrict_apply {H : subgroup G} (f : G β* N) (x : H) :
f.restrict H x = f (x : G) := rfl
/-- Restriction of a group hom to a subgroup of the codomain. -/
@[to_additive "Restriction of an `add_group` hom to an `add_subgroup` of the codomain."]
def cod_restrict (f : G β* N) (S : subgroup N) (h : β x, f x β S) : G β* S :=
{ to_fun := Ξ» n, β¨f n, h nβ©,
map_one' := subtype.eq f.map_one,
map_mul' := Ξ» x y, subtype.eq (f.map_mul x y) }
@[simp, to_additive]
lemma cod_restrict_apply {G : Type*} [group G] {N : Type*} [group N] (f : G β* N)
(S : subgroup N) (h : β (x : G), f x β S) {x : G} :
f.cod_restrict S h x = β¨f x, h xβ© := rfl
@[to_additive] lemma subgroup_of_range_eq_of_le {Gβ Gβ : Type*} [group Gβ] [group Gβ]
{K : subgroup Gβ} (f : Gβ β* Gβ) (h : f.range β€ K) :
f.range.subgroup_of K = (f.cod_restrict K (Ξ» x, h β¨x, rflβ©)).range :=
begin
ext k,
refine exists_congr _,
simp [subtype.ext_iff],
end
/-- Computable alternative to `monoid_hom.of_injective`. -/
def of_left_inverse {f : G β* N} {g : N β* G} (h : function.left_inverse g f) : G β* f.range :=
{ to_fun := f.range_restrict,
inv_fun := g β f.range.subtype,
left_inv := h,
right_inv := by
{ rintros β¨x, y, rflβ©,
apply subtype.ext,
rw [coe_range_restrict, function.comp_apply, subgroup.coe_subtype, subtype.coe_mk, h] },
.. f.range_restrict }
@[simp] lemma of_left_inverse_apply {f : G β* N} {g : N β* G}
(h : function.left_inverse g f) (x : G) :
β(of_left_inverse h x) = f x := rfl
@[simp] lemma of_left_inverse_symm_apply {f : G β* N} {g : N β* G}
(h : function.left_inverse g f) (x : f.range) :
(of_left_inverse h).symm x = g x := rfl
/-- The range of an injective group homomorphism is isomorphic to its domain. -/
noncomputable def of_injective {f : G β* N} (hf : function.injective f) : G β* f.range :=
(mul_equiv.of_bijective (f.cod_restrict f.range (Ξ» x, β¨x, rflβ©))
β¨Ξ» x y h, hf (subtype.ext_iff.mp h), by { rintros β¨x, y, rflβ©, exact β¨y, rflβ© }β©)
lemma of_injective_apply {f : G β* N} (hf : function.injective f) {x : G} :
β(of_injective hf x) = f x := rfl
/-- The multiplicative kernel of a monoid homomorphism is the subgroup of elements `x : G` such that
`f x = 1` -/
@[to_additive "The additive kernel of an `add_monoid` homomorphism is the `add_subgroup` of elements
such that `f x = 0`"]
def ker (f : G β* N) := (β₯ : subgroup N).comap f
@[to_additive]
lemma mem_ker (f : G β* N) {x : G} : x β f.ker β f x = 1 := iff.rfl
@[to_additive]
lemma coe_ker (f : G β* N) : (f.ker : set G) = (f : G β N) β»ΒΉ' {1} := rfl
@[to_additive]
lemma eq_iff (f : G β* N) {x y : G} : f x = f y β yβ»ΒΉ * x β f.ker :=
by rw [f.mem_ker, f.map_mul, f.map_inv, inv_mul_eq_one, eq_comm]
@[to_additive]
instance decidable_mem_ker [decidable_eq N] (f : G β* N) :
decidable_pred (β f.ker) :=
Ξ» x, decidable_of_iff (f x = 1) f.mem_ker
@[to_additive]
lemma comap_ker (g : N β* P) (f : G β* N) : g.ker.comap f = (g.comp f).ker := rfl
@[simp, to_additive] lemma comap_bot (f : G β* N) :
(β₯ : subgroup N).comap f = f.ker := rfl
@[to_additive] lemma range_restrict_ker (f : G β* N) : ker (range_restrict f) = ker f :=
begin
ext,
change (β¨f x, _β© : range f) = β¨1, _β© β f x = 1,
simp only [],
end
@[simp, to_additive]
lemma ker_one : (1 : G β* N).ker = β€ :=
by { ext, simp [mem_ker] }
@[to_additive] lemma ker_eq_bot_iff (f : G β* N) : f.ker = β₯ β function.injective f :=
begin
split,
{ intros h x y hxy,
rwa [βmul_inv_eq_one, βmap_inv, βmap_mul, βmem_ker, h, mem_bot, mul_inv_eq_one] at hxy },
{ exact Ξ» h, le_bot_iff.mp (Ξ» x hx, h (hx.trans f.map_one.symm)) },
end
@[to_additive]
lemma prod_map_comap_prod {G' : Type*} {N' : Type*} [group G'] [group N']
(f : G β* N) (g : G' β* N') (S : subgroup N) (S' : subgroup N') :
(S.prod S').comap (prod_map f g) = (S.comap f).prod (S'.comap g) :=
set_like.coe_injective $ set.preimage_prod_map_prod f g _ _
@[to_additive]
lemma ker_prod_map {G' : Type*} {N' : Type*} [group G'] [group N'] (f : G β* N) (g : G' β* N') :
(prod_map f g).ker = f.ker.prod g.ker :=
begin
dsimp only [ker],
rw [βprod_map_comap_prod, bot_prod_bot],
end
/-- The subgroup of elements `x : G` such that `f x = g x` -/
@[to_additive "The additive subgroup of elements `x : G` such that `f x = g x`"]
def eq_locus (f g : G β* N) : subgroup G :=
{ inv_mem' := Ξ» x (hx : f x = g x), show f xβ»ΒΉ = g xβ»ΒΉ, by rw [f.map_inv, g.map_inv, hx],
.. eq_mlocus f g}
/-- If two monoid homomorphisms are equal on a set, then they are equal on its subgroup closure. -/
@[to_additive]
lemma eq_on_closure {f g : G β* N} {s : set G} (h : set.eq_on f g s) :
set.eq_on f g (closure s) :=
show closure s β€ f.eq_locus g, from (closure_le _).2 h
@[to_additive]
lemma eq_of_eq_on_top {f g : G β* N} (h : set.eq_on f g (β€ : subgroup G)) :
f = g :=
ext $ Ξ» x, h trivial
@[to_additive]
lemma eq_of_eq_on_dense {s : set G} (hs : closure s = β€) {f g : G β* N} (h : s.eq_on f g) :
f = g :=
eq_of_eq_on_top $ hs βΈ eq_on_closure h
@[to_additive]
lemma gclosure_preimage_le (f : G β* N) (s : set N) :
closure (f β»ΒΉ' s) β€ (closure s).comap f :=
(closure_le _).2 $ Ξ» x hx, by rw [set_like.mem_coe, mem_comap]; exact subset_closure hx
/-- The image under a monoid homomorphism of the subgroup generated by a set equals the subgroup
generated by the image of the set. -/
@[to_additive "The image under an `add_monoid` hom of the `add_subgroup` generated by a set equals
the `add_subgroup` generated by the image of the set."]
lemma map_closure (f : G β* N) (s : set G) :
(closure s).map f = closure (f '' s) :=
le_antisymm
(map_le_iff_le_comap.2 $ le_trans (closure_mono $ set.subset_preimage_image f s)
(gclosure_preimage_le _ _))
((closure_le _).2 $ set.image_subset _ subset_closure)
-- this instance can't go just after the definition of `mrange` because `fintype` is
-- not imported at that stage
/-- The range of a finite monoid under a monoid homomorphism is finite.
Note: this instance can form a diamond with `subtype.fintype` in the
presence of `fintype N`. -/
@[to_additive "The range of a finite additive monoid under an additive monoid homomorphism is
finite.
Note: this instance can form a diamond with `subtype.fintype` or `subgroup.fintype` in the
presence of `fintype N`."]
instance fintype_mrange {M N : Type*} [monoid M] [monoid N] [fintype M] [decidable_eq N]
(f : M β* N) : fintype (mrange f) :=
set.fintype_range f
/-- The range of a finite group under a group homomorphism is finite.
Note: this instance can form a diamond with `subtype.fintype` or `subgroup.fintype` in the
presence of `fintype N`. -/
@[to_additive "The range of a finite additive group under an additive group homomorphism is finite.
Note: this instance can form a diamond with `subtype.fintype` or `subgroup.fintype` in the
presence of `fintype N`."]
instance fintype_range [fintype G] [decidable_eq N] (f : G β* N) : fintype (range f) :=
set.fintype_range f
end monoid_hom
namespace subgroup
variables {N : Type*} [group N] (H : subgroup G)
@[to_additive] lemma map_eq_bot_iff {f : G β* N} : H.map f = β₯ β H β€ f.ker :=
begin
rw eq_bot_iff,
split,
{ exact Ξ» h x hx, h β¨x, hx, rflβ© },
{ intros h x hx,
obtain β¨y, hy, rflβ© := hx,
exact h hy },
end
@[to_additive]
lemma map_eq_bot_iff_of_injective {f : G β* N} (hf : function.injective f) : H.map f = β₯ β H = β₯ :=
by rw [map_eq_bot_iff, f.ker_eq_bot_iff.mpr hf, le_bot_iff]
end subgroup
namespace subgroup
open monoid_hom
variables {N : Type*} [group N] (f : G β* N)
@[to_additive]
lemma map_le_range (H : subgroup G) : map f H β€ f.range :=
(range_eq_map f).symm βΈ map_mono le_top
@[to_additive]
lemma ker_le_comap (H : subgroup N) : f.ker β€ comap f H :=
comap_mono bot_le
@[to_additive]
lemma map_comap_le (H : subgroup N) : map f (comap f H) β€ H :=
(gc_map_comap f).l_u_le _
@[to_additive]
lemma le_comap_map (H : subgroup G) : H β€ comap f (map f H) :=
(gc_map_comap f).le_u_l _
@[to_additive]
lemma map_comap_eq (H : subgroup N) :
map f (comap f H) = f.range β H :=
set_like.ext' begin
convert set.image_preimage_eq_inter_range,
simp [set.inter_comm],
end
@[to_additive]
lemma comap_map_eq (H : subgroup G) : comap f (map f H) = H β f.ker :=
begin
refine le_antisymm _ (sup_le (le_comap_map _ _) (ker_le_comap _ _)),
intros x hx, simp only [exists_prop, mem_map, mem_comap] at hx,
rcases hx with β¨y, hy, hy'β©,
have : yβ»ΒΉ * x β f.ker, { rw mem_ker, simp [hy'] },
convert mul_mem _ (mem_sup_left hy) (mem_sup_right this),
simp,
end
@[to_additive]
lemma map_comap_eq_self {f : G β* N} {H : subgroup N} (h : H β€ f.range) :
map f (comap f H) = H :=
by rwa [map_comap_eq, inf_eq_right]
@[to_additive]
lemma map_comap_eq_self_of_surjective {f : G β* N} (h : function.surjective f) (H : subgroup N) :
map f (comap f H) = H :=
map_comap_eq_self ((range_top_of_surjective _ h).symm βΈ le_top)
@[to_additive]
lemma comap_injective {f : G β* N} (h : function.surjective f) : function.injective (comap f) :=
Ξ» K L hKL, by { apply_fun map f at hKL, simpa [map_comap_eq_self_of_surjective h] using hKL }
@[to_additive]
lemma comap_map_eq_self {f : G β* N} {H : subgroup G} (h : f.ker β€ H) :
comap f (map f H) = H :=
by rwa [comap_map_eq, sup_eq_left]
@[to_additive]
lemma comap_map_eq_self_of_injective {f : G β* N} (h : function.injective f) (H : subgroup G) :
comap f (map f H) = H :=
comap_map_eq_self (((ker_eq_bot_iff _).mpr h).symm βΈ bot_le)
@[to_additive]
lemma map_injective {f : G β* N} (h : function.injective f) : function.injective (map f) :=
Ξ» K L hKL, by { apply_fun comap f at hKL, simpa [comap_map_eq_self_of_injective h] using hKL }
@[to_additive]
lemma map_eq_comap_of_inverse {f : G β* N} {g : N β* G} (hl : function.left_inverse g f)
(hr : function.right_inverse g f) (H : subgroup G) : map f H = comap g H :=
set_like.ext' $ by rw [coe_map, coe_comap, set.image_eq_preimage_of_inverse hl hr]
/-- Given `f(A) = f(B)`, `ker f β€ A`, and `ker f β€ B`, deduce that `A = B` -/
@[to_additive] lemma map_injective_of_ker_le
{H K : subgroup G} (hH : f.ker β€ H) (hK : f.ker β€ K) (hf : map f H = map f K) :
H = K :=
begin
apply_fun comap f at hf,
rwa [comap_map_eq, comap_map_eq, sup_of_le_left hH, sup_of_le_left hK] at hf,
end
@[to_additive] lemma comap_sup_eq
(H K : subgroup N) (hf : function.surjective f):
comap f H β comap f K = comap f (H β K) :=
begin
have : map f (comap f H β comap f K) = map f (comap f (H β K)),
{ simp [subgroup.map_comap_eq, map_sup, f.range_top_of_surjective hf], },
refine map_injective_of_ker_le f _ _ this,
{ calc f.ker β€ comap f H : ker_le_comap f _
... β€ comap f H β comap f K : le_sup_left, },
exact ker_le_comap _ _,
end
/-- A subgroup is isomorphic to its image under an injective function -/
@[to_additive "An additive subgroup is isomorphic to its image under an injective function"]
noncomputable def equiv_map_of_injective (H : subgroup G)
(f : G β* N) (hf : function.injective f) : H β* H.map f :=
{ map_mul' := Ξ» _ _, subtype.ext (f.map_mul _ _), ..equiv.set.image f H hf }
@[simp, to_additive] lemma coe_equiv_map_of_injective_apply (H : subgroup G)
(f : G β* N) (hf : function.injective f) (h : H) :
(equiv_map_of_injective H f hf h : N) = f h := rfl
end subgroup
namespace monoid_hom
variables {Gβ Gβ Gβ : Type*} [group Gβ] [group Gβ] [group Gβ]
variables (f : Gβ β* Gβ) (f_inv : Gβ β Gβ)
/-- Auxiliary definition used to define `lift_of_right_inverse` -/
@[to_additive "Auxiliary definition used to define `lift_of_right_inverse`"]
def lift_of_right_inverse_aux
(hf : function.right_inverse f_inv f) (g : Gβ β* Gβ) (hg : f.ker β€ g.ker) :
Gβ β* Gβ :=
{ to_fun := Ξ» b, g (f_inv b),
map_one' := hg (hf 1),
map_mul' :=
begin
intros x y,
rw [β g.map_mul, β mul_inv_eq_one, β g.map_inv, β g.map_mul, β g.mem_ker],
apply hg,
rw [f.mem_ker, f.map_mul, f.map_inv, mul_inv_eq_one, f.map_mul],
simp only [hf _],
end }
@[simp, to_additive]
lemma lift_of_right_inverse_aux_comp_apply
(hf : function.right_inverse f_inv f) (g : Gβ β* Gβ) (hg : f.ker β€ g.ker) (x : Gβ) :
(f.lift_of_right_inverse_aux f_inv hf g hg) (f x) = g x :=
begin
dsimp [lift_of_right_inverse_aux],
rw [β mul_inv_eq_one, β g.map_inv, β g.map_mul, β g.mem_ker],
apply hg,
rw [f.mem_ker, f.map_mul, f.map_inv, mul_inv_eq_one],
simp only [hf _],
end
/-- `lift_of_right_inverse f hf g hg` is the unique group homomorphism `Ο`
* such that `Ο.comp f = g` (`monoid_hom.lift_of_right_inverse_comp`),
* where `f : Gβ β+* Gβ` has a right_inverse `f_inv` (`hf`),
* and `g : Gβ β+* Gβ` satisfies `hg : f.ker β€ g.ker`.
See `monoid_hom.eq_lift_of_right_inverse` for the uniqueness lemma.
```
Gβ.
| \
f | \ g
| \
v \β
Gβ----> Gβ
β!Ο
```
-/
@[to_additive "`lift_of_right_inverse f f_inv hf g hg` is the unique additive group homomorphism `Ο`
* such that `Ο.comp f = g` (`add_monoid_hom.lift_of_right_inverse_comp`),
* where `f : Gβ β+ Gβ` has a right_inverse `f_inv` (`hf`),
* and `g : Gβ β+ Gβ` satisfies `hg : f.ker β€ g.ker`.
See `add_monoid_hom.eq_lift_of_right_inverse` for the uniqueness lemma.
```
Gβ.
| \\
f | \\ g
| \\
v \\β
Gβ----> Gβ
β!Ο
```"]
def lift_of_right_inverse
(hf : function.right_inverse f_inv f) : {g : Gβ β* Gβ // f.ker β€ g.ker} β (Gβ β* Gβ) :=
{ to_fun := Ξ» g, f.lift_of_right_inverse_aux f_inv hf g.1 g.2,
inv_fun := Ξ» Ο, β¨Ο.comp f, Ξ» x hx, (mem_ker _).mpr $ by simp [(mem_ker _).mp hx]β©,
left_inv := Ξ» g, by {
ext,
simp only [comp_apply, lift_of_right_inverse_aux_comp_apply, subtype.coe_mk,
subtype.val_eq_coe], },
right_inv := Ξ» Ο, by {
ext b,
simp [lift_of_right_inverse_aux, hf b], } }
/-- A non-computable version of `monoid_hom.lift_of_right_inverse` for when no computable right
inverse is available, that uses `function.surj_inv`. -/
@[simp, to_additive "A non-computable version of `add_monoid_hom.lift_of_right_inverse` for when no
computable right inverse is available."]
noncomputable abbreviation lift_of_surjective
(hf : function.surjective f) : {g : Gβ β* Gβ // f.ker β€ g.ker} β (Gβ β* Gβ) :=
f.lift_of_right_inverse (function.surj_inv hf) (function.right_inverse_surj_inv hf)
@[simp, to_additive]
lemma lift_of_right_inverse_comp_apply
(hf : function.right_inverse f_inv f) (g : {g : Gβ β* Gβ // f.ker β€ g.ker}) (x : Gβ) :
(f.lift_of_right_inverse f_inv hf g) (f x) = g x :=
f.lift_of_right_inverse_aux_comp_apply f_inv hf g.1 g.2 x
@[simp, to_additive]
lemma lift_of_right_inverse_comp (hf : function.right_inverse f_inv f)
(g : {g : Gβ β* Gβ // f.ker β€ g.ker}) :
(f.lift_of_right_inverse f_inv hf g).comp f = g :=
monoid_hom.ext $ f.lift_of_right_inverse_comp_apply f_inv hf g
@[to_additive]
lemma eq_lift_of_right_inverse (hf : function.right_inverse f_inv f) (g : Gβ β* Gβ)
(hg : f.ker β€ g.ker) (h : Gβ β* Gβ) (hh : h.comp f = g) :
h = (f.lift_of_right_inverse f_inv hf β¨g, hgβ©) :=
begin
simp_rw βhh,
exact ((f.lift_of_right_inverse f_inv hf).apply_symm_apply _).symm,
end
end monoid_hom
variables {N : Type*} [group N]
-- Here `H.normal` is an explicit argument so we can use dot notation with `comap`.
@[to_additive]
lemma subgroup.normal.comap {H : subgroup N} (hH : H.normal) (f : G β* N) :
(H.comap f).normal :=
β¨Ξ» _, by simp [subgroup.mem_comap, hH.conj_mem] {contextual := tt}β©
@[priority 100, to_additive]
instance subgroup.normal_comap {H : subgroup N}
[nH : H.normal] (f : G β* N) : (H.comap f).normal := nH.comap _
@[priority 100, to_additive]
instance monoid_hom.normal_ker (f : G β* N) : f.ker.normal :=
by rw [monoid_hom.ker]; apply_instance
@[priority 100, to_additive]
instance subgroup.normal_inf (H N : subgroup G) [hN : N.normal] :
((H β N).comap H.subtype).normal :=
β¨Ξ» x hx g, begin
simp only [subgroup.mem_inf, coe_subtype, subgroup.mem_comap] at hx,
simp only [subgroup.coe_mul, subgroup.mem_inf, coe_subtype, subgroup.coe_inv, subgroup.mem_comap],
exact β¨H.mul_mem (H.mul_mem g.2 hx.1) (H.inv_mem g.2), hN.1 x hx.2 gβ©,
endβ©
namespace subgroup
/-- The subgroup generated by an element. -/
def gpowers (g : G) : subgroup G :=
subgroup.copy (gpowers_hom G g).range (set.range ((^) g : β€ β G)) rfl
@[simp] lemma mem_gpowers (g : G) : g β gpowers g := β¨1, gpow_one _β©
lemma gpowers_eq_closure (g : G) : gpowers g = closure {g} :=
by { ext, exact mem_closure_singleton.symm }
@[simp] lemma range_gpowers_hom (g : G) : (gpowers_hom G g).range = gpowers g := rfl
lemma gpowers_subset {a : G} {K : subgroup G} (h : a β K) : gpowers a β€ K :=
Ξ» x hx, match x, hx with _, β¨i, rflβ© := K.gpow_mem h i end
lemma mem_gpowers_iff {g h : G} :
h β gpowers g β β (k : β€), g ^ k = h :=
iff.rfl
end subgroup
namespace add_subgroup
/-- The subgroup generated by an element. -/
def gmultiples (a : A) : add_subgroup A :=
add_subgroup.copy (gmultiples_hom A a).range (set.range ((β’ a) : β€ β A)) rfl
@[simp] lemma mem_gmultiples (a : A) : a β gmultiples a := β¨1, one_gsmul _β©
lemma gmultiples_eq_closure (a : A) : gmultiples a = closure {a} :=
by { ext, exact mem_closure_singleton.symm }
@[simp] lemma range_gmultiples_hom (a : A) : (gmultiples_hom A a).range = gmultiples a := rfl
lemma gmultiples_subset {a : A} {B : add_subgroup A} (h : a β B) : gmultiples a β€ B :=
@subgroup.gpowers_subset (multiplicative A) _ _ (B.to_subgroup) h
lemma mem_gmultiples_iff {a b : A} :
b β add_subgroup.gmultiples a β β (k : β€), k β’ a = b :=
iff.rfl
attribute [to_additive add_subgroup.gmultiples] subgroup.gpowers
attribute [to_additive add_subgroup.mem_gmultiples] subgroup.mem_gpowers
attribute [to_additive add_subgroup.gmultiples_eq_closure] subgroup.gpowers_eq_closure
attribute [to_additive add_subgroup.range_gmultiples_hom] subgroup.range_gpowers_hom
attribute [to_additive add_subgroup.gmultiples_subset] subgroup.gpowers_subset
attribute [to_additive add_subgroup.mem_gmultiples_iff] subgroup.mem_gpowers_iff
end add_subgroup
lemma int.mem_gmultiples_iff {a b : β€} :
b β add_subgroup.gmultiples a β a β£ b :=
exists_congr (Ξ» k, by rw [mul_comm, eq_comm, β smul_eq_mul])
lemma of_mul_image_gpowers_eq_gmultiples_of_mul { x : G } :
additive.of_mul '' ((subgroup.gpowers x) : set G) = add_subgroup.gmultiples (additive.of_mul x) :=
begin
ext y,
split,
{ rintro β¨z, β¨m, hmβ©, hz2β©,
use m,
simp only,
rwa [β of_mul_gpow, hm] },
{ rintros β¨n, hnβ©,
refine β¨x ^ n, β¨n, rflβ©, _β©,
rwa of_mul_gpow }
end
lemma of_add_image_gmultiples_eq_gpowers_of_add {x : A} :
multiplicative.of_add '' ((add_subgroup.gmultiples x) : set A) =
subgroup.gpowers (multiplicative.of_add x) :=
begin
symmetry,
rw equiv.eq_image_iff_symm_image_eq,
exact of_mul_image_gpowers_eq_gmultiples_of_mul,
end
namespace mul_equiv
variables {H K : subgroup G}
/-- Makes the identity isomorphism from a proof two subgroups of a multiplicative
group are equal. -/
@[to_additive "Makes the identity additive isomorphism from a proof
two subgroups of an additive group are equal."]
def subgroup_congr (h : H = K) : H β* K :=
{ map_mul' := Ξ» _ _, rfl, ..equiv.set_congr $ congr_arg _ h }
end mul_equiv
-- TODO : β₯(β€ : subgroup H) β* H ?
namespace subgroup
variables {C : Type*} [comm_group C] {s t : subgroup C} {x : C}
@[to_additive]
lemma mem_sup : x β s β t β β (y β s) (z β t), y * z = x :=
β¨Ξ» h, begin
rw [β closure_eq s, β closure_eq t, β closure_union] at h,
apply closure_induction h,
{ rintro y (h | h),
{ exact β¨y, h, 1, t.one_mem, by simpβ© },
{ exact β¨1, s.one_mem, y, h, by simpβ© } },
{ exact β¨1, s.one_mem, 1, β¨t.one_mem, mul_one 1β©β© },
{ rintro _ _ β¨yβ, hyβ, zβ, hzβ, rflβ© β¨yβ, hyβ, zβ, hzβ, rflβ©,
exact β¨_, mul_mem _ hyβ hyβ, _, mul_mem _ hzβ hzβ, by simp [mul_assoc]; ccβ© },
{ rintro _ β¨y, hy, z, hz, rflβ©,
exact β¨_, inv_mem _ hy, _, inv_mem _ hz, mul_comm z y βΈ (mul_inv_rev z y).symmβ© }
end,
by rintro β¨y, hy, z, hz, rflβ©; exact mul_mem _
((le_sup_left : s β€ s β t) hy)
((le_sup_right : t β€ s β t) hz)β©
@[to_additive]
lemma mem_sup' : x β s β t β β (y : s) (z : t), (y:C) * z = x :=
mem_sup.trans $ by simp only [set_like.exists, coe_mk]
@[to_additive]
instance : is_modular_lattice (subgroup C) :=
β¨Ξ» x y z xz a ha, begin
rw [mem_inf, mem_sup] at ha,
rcases ha with β¨β¨b, hb, c, hc, rflβ©, hazβ©,
rw mem_sup,
refine β¨b, hb, c, mem_inf.2 β¨hc, _β©, rflβ©,
rw β inv_mul_cancel_left b c,
apply z.mul_mem (z.inv_mem (xz hb)) haz,
endβ©
end subgroup
section
variables (G) (A)
/-- A `group` is simple when it has exactly two normal `subgroup`s. -/
class is_simple_group extends nontrivial G : Prop :=
(eq_bot_or_eq_top_of_normal : β H : subgroup G, H.normal β H = β₯ β¨ H = β€)
/-- An `add_group` is simple when it has exactly two normal `add_subgroup`s. -/
class is_simple_add_group extends nontrivial A : Prop :=
(eq_bot_or_eq_top_of_normal : β H : add_subgroup A, H.normal β H = β₯ β¨ H = β€)
attribute [to_additive] is_simple_group
variables {G} {A}
@[to_additive]
lemma subgroup.normal.eq_bot_or_eq_top [is_simple_group G] {H : subgroup G} (Hn : H.normal) :
H = β₯ β¨ H = β€ :=
is_simple_group.eq_bot_or_eq_top_of_normal H Hn
namespace is_simple_group
@[to_additive]
instance {C : Type*} [comm_group C] [is_simple_group C] :
is_simple_lattice (subgroup C) :=
β¨Ξ» H, H.normal_of_comm.eq_bot_or_eq_topβ©
open subgroup
@[to_additive]
lemma is_simple_group_of_surjective {H : Type*} [group H] [is_simple_group G]
[nontrivial H] (f : G β* H) (hf : function.surjective f) :
is_simple_group H :=
β¨nontrivial.exists_pair_ne, Ξ» H iH, begin
refine ((iH.comap f).eq_bot_or_eq_top).imp (Ξ» h, _) (Ξ» h, _),
{ rw [βmap_bot f, βh, map_comap_eq_self_of_surjective hf] },
{ rw [βcomap_top f] at h, exact comap_injective hf h }
endβ©
end is_simple_group
end
namespace subgroup
section pointwise
@[to_additive]
lemma closure_mul_le (S T : set G) : 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 : subgroup G) : 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)
@[to_additive]
private def mul_normal_aux (H N : subgroup G) [hN : N.normal] : subgroup G :=
{ carrier := (H : set G) * N,
one_mem' := β¨1, 1, H.one_mem, N.one_mem, by rw mul_oneβ©,
mul_mem' := Ξ» a b β¨h, n, hh, hn, haβ© β¨h', n', hh', hn', hbβ©,
β¨h * h', h'β»ΒΉ * n * h' * n',
H.mul_mem hh hh', N.mul_mem (by simpa using hN.conj_mem _ hn h'β»ΒΉ) hn',
by simp [β ha, β hb, mul_assoc]β©,
inv_mem' := Ξ» x β¨h, n, hh, hn, hxβ©,
β¨hβ»ΒΉ, h * nβ»ΒΉ * hβ»ΒΉ, H.inv_mem hh, hN.conj_mem _ (N.inv_mem hn) h,
by rw [mul_assoc h, inv_mul_cancel_left, β hx, mul_inv_rev]β© }
/-- The carrier of `H β N` is just `βH * βN` (pointwise set product) when `N` is normal. -/
@[to_additive "The carrier of `H β N` is just `βH + βN` (pointwise set addition)
when `N` is normal."]
lemma mul_normal (H N : subgroup G) [N.normal] : (β(H β N) : set G) = H * N :=
set.subset.antisymm
(show H β N β€ mul_normal_aux H N,
by { rw sup_eq_closure, apply Inf_le _, dsimp, refl })
((sup_eq_closure H N).symm βΈ subset_closure)
@[to_additive]
private def normal_mul_aux (N H : subgroup G) [hN : N.normal] : subgroup G :=
{ carrier := (N : set G) * H,
one_mem' := β¨1, 1, N.one_mem, H.one_mem, by rw mul_oneβ©,
mul_mem' := Ξ» a b β¨n, h, hn, hh, haβ© β¨n', h', hn', hh', hbβ©,
β¨n * (h * n' * hβ»ΒΉ), h * h',
N.mul_mem hn (hN.conj_mem _ hn' _), H.mul_mem hh hh',
by simp [β ha, β hb, mul_assoc]β©,
inv_mem' := Ξ» x β¨n, h, hn, hh, hxβ©,
β¨hβ»ΒΉ * nβ»ΒΉ * h, hβ»ΒΉ,
by simpa using hN.conj_mem _ (N.inv_mem hn) hβ»ΒΉ, H.inv_mem hh,
by rw [mul_inv_cancel_right, β mul_inv_rev, hx]β© }
/-- The carrier of `N β H` is just `βN * βH` (pointwise set product) when `N` is normal. -/
@[to_additive "The carrier of `N β H` is just `βN + βH` (pointwise set addition)
when `N` is normal."]
lemma normal_mul (N H : subgroup G) [N.normal] : (β(N β H) : set G) = N * H :=
set.subset.antisymm
(show N β H β€ normal_mul_aux N H,
by { rw sup_eq_closure, apply Inf_le _, dsimp, refl })
((sup_eq_closure N H).symm βΈ subset_closure)
@[to_additive] lemma mul_inf_assoc (A B C : subgroup G) (h : A β€ C) :
(A : set G) * β(B β C) = (A * B) β C :=
begin
ext,
simp only [coe_inf, set.inf_eq_inter, set.mem_mul, set.mem_inter_iff],
split,
{ rintros β¨y, z, hy, β¨hzB, hzCβ©, rflβ©,
refine β¨_, mul_mem C (h hy) hzCβ©,
exact β¨y, z, hy, hzB, rflβ© },
rintros β¨β¨y, z, hy, hz, rflβ©, hyzβ©,
refine β¨y, z, hy, β¨hz, _β©, rflβ©,
suffices : yβ»ΒΉ * (y * z) β C, { simpa },
exact mul_mem C (inv_mem C (h hy)) hyz
end
@[to_additive] lemma inf_mul_assoc (A B C : subgroup G) (h : C β€ A) :
((A β B : subgroup G) : set G) * C = A β (B * C) :=
begin
ext,
simp only [coe_inf, set.inf_eq_inter, set.mem_mul, set.mem_inter_iff],
split,
{ rintros β¨y, z, β¨hyA, hyBβ©, hz, rflβ©,
refine β¨mul_mem A hyA (h hz), _β©,
exact β¨y, z, hyB, hz, rflβ© },
rintros β¨hyz, y, z, hy, hz, rflβ©,
refine β¨y, z, β¨_, hyβ©, hz, rflβ©,
suffices : (y * z) * zβ»ΒΉ β A, { simpa },
exact mul_mem A hyz (inv_mem A (h hz))
end
end pointwise
section subgroup_normal
@[to_additive] lemma normal_subgroup_of_iff {H K : subgroup G} (hHK : H β€ K) :
(H.subgroup_of K).normal β β h k, h β H β k β K β k * h * kβ»ΒΉ β H :=
β¨Ξ» hN h k hH hK, hN.conj_mem β¨h, hHK hHβ© hH β¨k, hKβ©,
Ξ» hN, { conj_mem := Ξ» h hm k, (hN h.1 k.1 hm k.2) }β©
@[to_additive] instance prod_subgroup_of_prod_normal
{Hβ Kβ : subgroup G} {Hβ Kβ : subgroup N}
[hβ : (Hβ.subgroup_of Kβ).normal] [hβ : (Hβ.subgroup_of Kβ).normal] :
((Hβ.prod Hβ).subgroup_of (Kβ.prod Kβ)).normal :=
{ conj_mem := Ξ» n hgHK g,
β¨hβ.conj_mem β¨(n : G Γ N).fst, (mem_prod.mp n.2).1β©
hgHK.1 β¨(g : G Γ N).fst, (mem_prod.mp g.2).1β©,
hβ.conj_mem β¨(n : G Γ N).snd, (mem_prod.mp n.2).2β©
hgHK.2 β¨(g : G Γ N).snd, (mem_prod.mp g.2).2β©β© }
@[to_additive] instance prod_normal
(H : subgroup G) (K : subgroup N) [hH : H.normal] [hK : K.normal] :
(H.prod K).normal :=
{ conj_mem := Ξ» n hg g,
β¨hH.conj_mem n.fst (subgroup.mem_prod.mp hg).1 g.fst,
hK.conj_mem n.snd (subgroup.mem_prod.mp hg).2 g.sndβ© }
@[to_additive] lemma inf_subgroup_of_inf_normal_of_right
(A B' B : subgroup G) (hB : B' β€ B) [hN : (B'.subgroup_of B).normal] :
((A β B').subgroup_of (A β B)).normal :=
{ conj_mem := Ξ» n hn g,
β¨mul_mem A (mul_mem A (mem_inf.1 g.2).1 (mem_inf.1 n.2).1) (inv_mem A (mem_inf.1 g.2).1),
(normal_subgroup_of_iff hB).mp hN n g hn.2 (mem_inf.mp g.2).2β© }
@[to_additive] lemma inf_subgroup_of_inf_normal_of_left
{A' A : subgroup G} (B : subgroup G) (hA : A' β€ A) [hN : (A'.subgroup_of A).normal] :
((A' β B).subgroup_of (A β B)).normal :=
{ conj_mem := Ξ» n hn g,
β¨(normal_subgroup_of_iff hA).mp hN n g hn.1 (mem_inf.mp g.2).1,
mul_mem B (mul_mem B (mem_inf.1 g.2).2 (mem_inf.1 n.2).2) (inv_mem B (mem_inf.1 g.2).2)β© }
instance sup_normal (H K : subgroup G) [hH : H.normal] [hK : K.normal] : (H β K).normal :=
{ conj_mem := Ξ» n hmem g,
begin
change n β β(H β K) at hmem,
change g * n * gβ»ΒΉ β β(H β K),
rw [normal_mul, set.mem_mul] at *,
rcases hmem with β¨h, k, hh, hk, rflβ©,
refine β¨g * h * gβ»ΒΉ, g * k * gβ»ΒΉ, hH.conj_mem h hh g, hK.conj_mem k hk g, _β©,
simp
end }
@[to_additive] instance normal_inf_normal (H K : subgroup G) [hH : H.normal] [hK : K.normal] :
(H β K).normal :=
{ conj_mem := Ξ» n hmem g,
by { rw mem_inf at *, exact β¨hH.conj_mem n hmem.1 g, hK.conj_mem n hmem.2 gβ© } }
@[to_additive] lemma subgroup_of_sup (A A' B : subgroup G) (hA : A β€ B) (hA' : A' β€ B) :
(A β A').subgroup_of B = A.subgroup_of B β A'.subgroup_of B :=
begin
refine map_injective_of_ker_le B.subtype
(ker_le_comap _ _) (le_trans (ker_le_comap B.subtype _) le_sup_left) _,
{ simp only [subgroup_of, map_comap_eq, map_sup, subtype_range],
rw [inf_of_le_right (sup_le hA hA'), inf_of_le_right hA', inf_of_le_right hA] },
end
@[to_additive] lemma subgroup_normal.mem_comm {H K : subgroup G}
(hK : H β€ K) [hN : (H.subgroup_of K).normal] {a b : G} (hb : b β K) (h : a * b β H) :
b * a β H :=
begin
have := (normal_subgroup_of_iff hK).mp hN (a * b) b h hb,
rwa [mul_assoc, mul_assoc, mul_right_inv, mul_one] at this,
end
end subgroup_normal
end subgroup
namespace is_conj
open subgroup
lemma normal_closure_eq_top_of {N : subgroup G} [hn : N.normal]
{g g' : G} {hg : g β N} {hg' : g' β N} (hc : is_conj g g')
(ht : normal_closure ({β¨g, hgβ©} : set N) = β€) :
normal_closure ({β¨g', hg'β©} : set N) = β€ :=
begin
obtain β¨c, rflβ© := is_conj_iff.1 hc,
have h : β x : N, (mul_aut.conj c) x β N,
{ rintro β¨x, hxβ©,
exact hn.conj_mem _ hx c },
have hs : function.surjective (((mul_aut.conj c).to_monoid_hom.restrict N).cod_restrict _ h),
{ rintro β¨x, hxβ©,
refine β¨β¨cβ»ΒΉ * x * c, _β©, _β©,
{ have h := hn.conj_mem _ hx cβ»ΒΉ,
rwa [inv_inv] at h },
simp only [monoid_hom.cod_restrict_apply, mul_equiv.coe_to_monoid_hom, mul_aut.conj_apply,
coe_mk, monoid_hom.restrict_apply, subtype.mk_eq_mk, β mul_assoc, mul_inv_self, one_mul],
rw [mul_assoc, mul_inv_self, mul_one] },
have ht' := map_mono (eq_top_iff.1 ht),
rw [β monoid_hom.range_eq_map, monoid_hom.range_top_of_surjective _ hs] at ht',
refine eq_top_iff.2 (le_trans ht' (map_le_iff_le_comap.2 (normal_closure_le_normal _))),
rw [set.singleton_subset_iff, set_like.mem_coe],
simp only [monoid_hom.cod_restrict_apply, mul_equiv.coe_to_monoid_hom, mul_aut.conj_apply, coe_mk,
monoid_hom.restrict_apply, mem_comap],
exact subset_normal_closure (set.mem_singleton _),
end
end is_conj
/-! ### Actions by `subgroup`s
These are just copies of the definitions about `submonoid` starting from `submonoid.mul_action`.
-/
section actions
namespace subgroup
variables {Ξ± Ξ² : Type*}
/-- The action by a subgroup is the action by the underlying group. -/
@[to_additive /-"The additive action by an add_subgroup is the action by the underlying
add_group. "-/]
instance [mul_action G Ξ±] (S : subgroup G) : mul_action S Ξ± :=
S.to_submonoid.mul_action
@[to_additive]
lemma smul_def [mul_action G Ξ±] {S : subgroup G} (g : S) (m : Ξ±) : g β’ m = (g : G) β’ m := rfl
@[to_additive]
instance smul_comm_class_left
[mul_action G Ξ²] [has_scalar Ξ± Ξ²] [smul_comm_class G Ξ± Ξ²] (S : subgroup G) :
smul_comm_class S Ξ± Ξ² :=
S.to_submonoid.smul_comm_class_left
@[to_additive]
instance smul_comm_class_right
[has_scalar Ξ± Ξ²] [mul_action G Ξ²] [smul_comm_class Ξ± G Ξ²] (S : subgroup G) :
smul_comm_class Ξ± S Ξ² :=
S.to_submonoid.smul_comm_class_right
/-- Note that this provides `is_scalar_tower S G G` which is needed by `smul_mul_assoc`. -/
instance
[has_scalar Ξ± Ξ²] [mul_action G Ξ±] [mul_action G Ξ²] [is_scalar_tower G Ξ± Ξ²] (S : subgroup G) :
is_scalar_tower S Ξ± Ξ² :=
S.to_submonoid.is_scalar_tower
/-- The action by a subgroup is the action by the underlying group. -/
instance [add_monoid Ξ±] [distrib_mul_action G Ξ±] (S : subgroup G) : distrib_mul_action S Ξ± :=
S.to_submonoid.distrib_mul_action
end subgroup
end actions
|
40d3757445e0521998125183d64acd809aadcab3 | 12dabd587ce2621d9a4eff9f16e354d02e206c8e | /world03/level07.lean | 6c50d1e68f9055cb7d11ed97917fc4109421417e | [] | no_license | abdelq/natural-number-game | a1b5b8f1d52625a7addcefc97c966d3f06a48263 | bbddadc6d2e78ece2e9acd40fa7702ecc2db75c2 | refs/heads/master | 1,668,606,478,691 | 1,594,175,058,000 | 1,594,175,058,000 | 278,673,209 | 0 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 241 | lean | lemma add_mul (a b t : mynat) : (a + b) * t = a * t + b * t :=
begin
induction t with h hd,
repeat {rw mul_zero},
refl,
repeat {rw mul_succ},
rw hd,
rw add_right_comm,
rw add_assoc,
rw add_assoc,
rw add_comm (b * h),
rw add_assoc,
refl,
end
|
f7fe836e8ecb255fcc7a467c07d1331bcfbae22f | 471bedbd023d35c9d078c2f936dd577ace7f5813 | /library/init/data/nat/basic.lean | 0ebd580bebfedac7b1ab86e2971b52f9c9b7460c | [
"Apache-2.0"
] | permissive | lambdaxymox/lean | e06f0fa503666df827edd9867d7f49ca017aae64 | fc13c8c72a15dab71a2c2b31410c2cadc3526bd7 | refs/heads/master | 1,666,785,407,985 | 1,666,153,673,000 | 1,666,153,673,000 | 310,165,986 | 0 | 0 | Apache-2.0 | 1,604,542,096,000 | 1,604,542,095,000 | null | UTF-8 | Lean | false | false | 5,985 | lean | /-
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn, Leonardo de Moura
-/
prelude
import init.logic
notation `β` := nat
namespace nat
inductive less_than_or_equal (a : β) : β β Prop
| refl : less_than_or_equal a
| step : Ξ {b}, less_than_or_equal b β less_than_or_equal (succ b)
instance : has_le β :=
β¨nat.less_than_or_equalβ©
@[reducible] protected def le (n m : β) := nat.less_than_or_equal n m
@[reducible] protected def lt (n m : β) := nat.less_than_or_equal (succ n) m
instance : has_lt β :=
β¨nat.ltβ©
def pred : β β β
| 0 := 0
| (a+1) := a
protected def sub : β β β β β
| a 0 := a
| a (b+1) := pred (sub a b)
protected def mul : nat β nat β nat
| a 0 := 0
| a (b+1) := (mul a b) + a
instance : has_sub β :=
β¨nat.subβ©
instance : has_mul β :=
β¨nat.mulβ©
-- defeq to the instance provided by comm_semiring
instance : has_dvd β :=
has_dvd.mk (Ξ» a b, β c, b = a * c)
instance : decidable_eq β
| zero zero := is_true rfl
| (succ x) zero := is_false (Ξ» h, nat.no_confusion h)
| zero (succ y) := is_false (Ξ» h, nat.no_confusion h)
| (succ x) (succ y) :=
match decidable_eq x y with
| is_true xeqy := is_true (xeqy βΈ eq.refl (succ x))
| is_false xney := is_false (Ξ» h, nat.no_confusion h (Ξ» xeqy, absurd xeqy xney))
end
def {u} repeat {Ξ± : Type u} (f : β β Ξ± β Ξ±) : β β Ξ± β Ξ±
| 0 a := a
| (succ n) a := f n (repeat n a)
instance : inhabited β :=
β¨nat.zeroβ©
@[simp] lemma nat_zero_eq_zero : nat.zero = 0 :=
rfl
/- properties of inequality -/
@[refl] protected lemma le_refl (a : β) : a β€ a :=
less_than_or_equal.refl
lemma le_succ (n : β) : n β€ succ n :=
less_than_or_equal.step (nat.le_refl n)
lemma succ_le_succ {n m : β} : n β€ m β succ n β€ succ m :=
Ξ» h, less_than_or_equal.rec (nat.le_refl (succ n)) (Ξ» a b, less_than_or_equal.step) h
protected lemma zero_le : β (n : β), 0 β€ n
| 0 := nat.le_refl 0
| (n+1) := less_than_or_equal.step (zero_le n)
lemma zero_lt_succ (n : β) : 0 < succ n :=
succ_le_succ n.zero_le
lemma succ_pos (n : β) : 0 < succ n := zero_lt_succ n
lemma not_succ_le_zero : β (n : β), succ n β€ 0 β false
.
protected lemma not_lt_zero (a : β) : Β¬ a < 0 := not_succ_le_zero a
lemma pred_le_pred {n m : β} : n β€ m β pred n β€ pred m :=
Ξ» h, less_than_or_equal.rec_on h
(nat.le_refl (pred n))
(Ξ» n, nat.rec (Ξ» a b, b) (Ξ» a b c, less_than_or_equal.step) n)
lemma le_of_succ_le_succ {n m : β} : succ n β€ succ m β n β€ m :=
pred_le_pred
instance decidable_le : β a b : β, decidable (a β€ b)
| 0 b := is_true b.zero_le
| (a+1) 0 := is_false (not_succ_le_zero a)
| (a+1) (b+1) :=
match decidable_le a b with
| is_true h := is_true (succ_le_succ h)
| is_false h := is_false (Ξ» a, h (le_of_succ_le_succ a))
end
instance decidable_lt : β a b : β, decidable (a < b) :=
Ξ» a b, nat.decidable_le (succ a) b
protected lemma eq_or_lt_of_le {a b : β} (h : a β€ b) : a = b β¨ a < b :=
less_than_or_equal.cases_on h (or.inl rfl) (Ξ» n h, or.inr (succ_le_succ h))
lemma lt_succ_of_le {a b : β} : a β€ b β a < succ b :=
succ_le_succ
@[simp] lemma succ_sub_succ_eq_sub (a b : β) : succ a - succ b = a - b :=
nat.rec_on b
(show succ a - succ zero = a - zero, from (eq.refl (succ a - succ zero)))
(Ξ» b, congr_arg pred)
lemma not_succ_le_self : β n : β, Β¬succ n β€ n :=
Ξ» n, nat.rec (not_succ_le_zero 0) (Ξ» a b c, b (le_of_succ_le_succ c)) n
protected lemma lt_irrefl (n : β) : Β¬n < n :=
not_succ_le_self n
protected lemma le_trans {n m k : β} (h1 : n β€ m) : m β€ k β n β€ k :=
less_than_or_equal.rec h1 (Ξ» p h2, less_than_or_equal.step)
lemma pred_le : β (n : β), pred n β€ n
| 0 := less_than_or_equal.refl
| (succ a) := less_than_or_equal.step less_than_or_equal.refl
lemma pred_lt : β {n : β}, n β 0 β pred n < n
| 0 h := absurd rfl h
| (succ a) h := lt_succ_of_le less_than_or_equal.refl
protected lemma sub_le (a b : β) : a - b β€ a :=
nat.rec_on b (nat.le_refl (a - 0)) (Ξ» bβ, nat.le_trans (pred_le (a - bβ)))
protected lemma sub_lt : β {a b : β}, 0 < a β 0 < b β a - b < a
| 0 b h1 h2 := absurd h1 (nat.lt_irrefl 0)
| (a+1) 0 h1 h2 := absurd h2 (nat.lt_irrefl 0)
| (a+1) (b+1) h1 h2 :=
eq.symm (succ_sub_succ_eq_sub a b) βΈ
show a - b < succ a, from
lt_succ_of_le (a.sub_le b)
protected lemma lt_of_lt_of_le {n m k : β} : n < m β m β€ k β n < k :=
nat.le_trans
/- Basic nat.add lemmas -/
protected lemma zero_add : β n : β, 0 + n = n
| 0 := rfl
| (n+1) := congr_arg succ (zero_add n)
lemma succ_add : β n m : β, (succ n) + m = succ (n + m)
| n 0 := rfl
| n (m+1) := congr_arg succ (succ_add n m)
lemma add_succ (n m : β) : n + succ m = succ (n + m) :=
rfl
protected lemma add_zero (n : β) : n + 0 = n :=
rfl
lemma add_one (n : β) : n + 1 = succ n :=
rfl
lemma succ_eq_add_one (n : β) : succ n = n + 1 :=
rfl
/- Basic lemmas for comparing numerals -/
protected lemma bit0_succ_eq (n : β) : bit0 (succ n) = succ (succ (bit0 n)) :=
show succ (succ n + n) = succ (succ (n + n)), from
congr_arg succ (succ_add n n)
protected lemma zero_lt_bit0 : β {n : nat}, n β 0 β 0 < bit0 n
| 0 h := absurd rfl h
| (succ n) h :=
calc 0 < succ (succ (bit0 n)) : zero_lt_succ _
... = bit0 (succ n) : (nat.bit0_succ_eq n).symm
protected lemma zero_lt_bit1 (n : nat) : 0 < bit1 n :=
zero_lt_succ _
protected lemma bit0_ne_zero : β {n : β}, n β 0 β bit0 n β 0
| 0 h := absurd rfl h
| (n+1) h :=
suffices (n+1) + (n+1) β 0, from this,
suffices succ ((n+1) + n) β 0, from this,
Ξ» h, nat.no_confusion h
protected lemma bit1_ne_zero (n : β) : bit1 n β 0 :=
show succ (n + n) β 0, from
Ξ» h, nat.no_confusion h
end nat
|
328d7df4ae72bb4a448aad1b1b03db52a5aa4440 | 43390109ab88557e6090f3245c47479c123ee500 | /src/finite_dimensional_vector_spaces/eigen.lean | f107fc8abaa14795e422a2b6889dd6bfa6eb0c42 | [
"Apache-2.0"
] | permissive | Ja1941/xena-UROP-2018 | 41f0956519f94d56b8bf6834a8d39473f4923200 | b111fb87f343cf79eca3b886f99ee15c1dd9884b | refs/heads/master | 1,662,355,955,139 | 1,590,577,325,000 | 1,590,577,325,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 13,545 | lean | /-
Copyright (c) 2018 Keji Neri, Blair Shi. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Blair Shi
-- This file aims to prove some theorems about eigenvalue and eigenvectors
* `is_eigenvalue` `is_eigenvector`: constructed eigenvalues and eigenvectors
in terms of linear map way
* `is_eigenvalue_M` `is_eigenvector_M`: constructed eigenvalues and eigenvectors
in terms of matrix way
-- Proved two ways are equivalent
* `is_diagonal_mat` : checked whether the given matrix is diagonal
-- currently working on Cayley-Hamilton Theorem
- Proved e β’ V = (e β’ I) * V, with e : F, V : vector F
-/
import .linear_map .ring_n_is_module_of_vector
import xenalib.Ellen_Arlt_matrix_rings
import xenalib.Keji_further_matrix_things
namespace M_module
instance mo {R : Type} {n : β} [ring R] : module R (matrix R n 1) :=
{
smul_add := @smul_add' _ _ 1 _,
add_smul := @add_smul' _ _ 1 _,
mul_smul := @mul_smul' _ _ 1 _,
one_smul := @one_smul' _ _ 1 _,
}
end M_module
def mat_to_finite_free_module {F : Type} {n : β} [ring F] (M : matrix F n 1) :
finite_free_module F n := Ξ» I, M I 0
def finite_free_module_to_vec {F : Type} {n : β} (fn : finite_free_module F n) :
vector F n := vector.of_fn fn
instance add_hommm {F : Type} [ring F] {n : nat}
: is_add_group_hom (@mat_to_finite_free_module F n _) :=
{
add :=
begin
intros a b,
unfold mat_to_finite_free_module,refl,
end
}
def module_hom_mat_to_finite_free_module {F : Type} [ring F] {n : nat}
: is_linear_map (@mat_to_finite_free_module F n _) :=
{
smul :=
begin
intro c,
intro x,
unfold mat_to_finite_free_module,
funext,
refl,
end,
.. add_hommm
}
def mat_to_finite_free_module_is_linear {F : Type} [ring F] {n : nat}:
linear_map (matrix F n 1) (finite_free_module F n) :=
β¨mat_to_finite_free_module, module_hom_mat_to_finite_free_moduleβ©
def smul' {F : Type} {n : β} [ring F] (M : matrix F n n) (fn : finite_free_module F n) :
finite_free_module F n := @mat_to_finite_free_module_is_linear F _ n (map_matrix.mat_mul_vec M (finite_free_module_to_vec fn))
theorem vector.nth_map {Ξ± Ξ² : Type*} {n : β} (v : vector Ξ± n) (f : Ξ± β Ξ²) (m : fin n) :
(v.map f).nth m = f (v.nth m) :=
begin
cases v with l hl,
unfold vector.nth vector.map,
rw list.nth_le_map,
end
open M_module
open map_matrix
lemma smul_eq_smul_two {F : Type} {n : β} [ring F] (fn : finite_free_module F n) :
β a : F, (smul a fn) =
mat_to_finite_free_module_is_linear.val
(@smul_M F _ _ _ a (vec_to_mat((finite_free_module_to_vec fn)))) :=
begin
intro a,
unfold mat_to_finite_free_module_is_linear,
simp,
unfold mat_to_finite_free_module,
unfold smul,
funext,
unfold smul_M,
unfold vec_to_mat,
unfold finite_free_module_to_vec,
simp,
end
def is_eigenvalue_M {F : Type*} {n : β} [ring F] (M : matrix F n n ) (a : F) :=
β v : (finite_free_module F n), (v β (0 : finite_free_module F n)) β§ (smul' M v = smul a v)
def is_eigenvector_M {F : Type*} {n : β} [ring F] (M : matrix F n n) (fn : finite_free_module F n) :=
β a : F, smul' M fn = smul a fn
def is_eigenvalue {F : Type*} {n : β} [field F] [vector_space F (finite_free_module F n)]
(T : linear_map (finite_free_module F n) (finite_free_module F n)) (a : F) :=
β v : (finite_free_module F n) , (v β (0 : (finite_free_module F n))) β§ (T v = smul a v)
def is_eigenvector {F : Type*} {n : β} [field F] [vector_space F (finite_free_module F n)]
(T : linear_map (finite_free_module F n) (finite_free_module F n)) (v : finite_free_module F n) (h : v β (0 : finite_free_module F n)) :=
β a : F, T v = smul a v
namespace eigen
open function
-- def smul {R : Type} {n : nat} [ring R] (s : R) (rn : finite_free_module R n) :
-- finite_free_module R n := Ξ» I, s * (rn I)
theorem finite_free_module_eq_vec {F : Type} [ring F] {n : nat} (fn : finite_free_module F n) :
β K, fn K = vector.nth (@finite_free_module_to_vec _ _ fn) K :=
begin
intro,
unfold finite_free_module_to_vec,
simp,
end
def finite_free_module_to_row_mat {F : Type} {n : β} [ring F] (fn : finite_free_module F n) :
matrix F 1 n := Ξ» I, Ξ» J, fn J
def finite_free_module_to_col_mat {F : Type} {n : β} [ring F] (fn : finite_free_module F n) :
matrix F n 1 := Ξ» I, Ξ» J, fn I
theorem finite_free_module_eq_col_mat {F : Type} [ring F] {n : nat} (fn : finite_free_module F n) :
β K, fn K = (finite_free_module_to_col_mat fn) K 0 :=
begin
intro,
unfold finite_free_module_to_col_mat,
end
theorem finite_free_module_eq_row_mat {F : Type} [ring F] {n : nat} (fn : finite_free_module F n) :
β K, fn K = (finite_free_module_to_row_mat fn) 0 K :=
begin
intro,
unfold finite_free_module_to_row_mat,
end
-- A : matrix F n n
-- The eigenvalues of TA are just those of A. (TA : Fn β Fn,TA(v) = Av)
open vector
instance {F : Type} {n : β} [field F]: vector_space F (finite_free_module F n) := {}
open map_matrix
#check transpose_of_product
theorem eigen_map_equiv_one {F : Type} {n : β} [field F] (M : matrix F n n) :
β eva : F, is_eigenvalue_M M eva β
@is_eigenvalue F n _ _ (@matrix_to_linear_map _ _ _ _ (Mα΅)) eva :=
begin
intro,
split,
intro hβ,
unfold is_eigenvalue,
cases hβ with v,
have hβ: v β 0 β§ (matrix_to_linear_map (Mα΅)) v = smul eva v,
cases hβ_h with hβ_hl hβ_hr,
split,
exact hβ_hl,
unfold smul' at hβ_hr,
unfold_coes at hβ_hr,
unfold mat_to_finite_free_module_is_linear at hβ_hr,
simp at hβ_hr,
unfold mat_to_finite_free_module at hβ_hr,
unfold map_matrix.mat_mul_vec at hβ_hr,
unfold matrix.mul at hβ_hr,
unfold map_matrix.vec_to_mat at hβ_hr,
show (matrix_to_linear_map (Mα΅)).1 v = smul eva v,
unfold matrix_to_linear_map,
simp,
unfold matrix_to_map,
rw βhβ_hr,
funext,
simp only [(finite_free_module_eq_vec _ _).symm],
conv in (v _ * (Mα΅) _ i)
begin
rw [finite_free_module_eq_row_mat v _],
end,
conv in (M i _ * v _ )
begin
rw [finite_free_module_eq_col_mat v _],
end,
show (matrix.mul F (finite_free_module_to_row_mat v) (Mα΅)) 0 i = _,
show _ = matrix.mul F M (finite_free_module_to_col_mat v) i 0,
show _ =(matrix.mul F M (finite_free_module_to_col_mat v))α΅ 0 i,
rw [transpose_of_product],
have hβ : (finite_free_module_to_row_mat v) = finite_free_module_to_col_mat vα΅,
unfold finite_free_module_to_row_mat,
unfold finite_free_module_to_col_mat,
funext,
rw [transpose],
rw [hβ],
existsi v,
exact hβ,
intro hβ,
unfold is_eigenvalue at hβ,
unfold is_eigenvalue_M,
cases hβ with v,
have hβ : v β 0 β§ smul' M v = smul eva v,
split,
cases hβ_h with hβ_hl hβ_hr,
exact hβ_hl,
unfold smul',
cases hβ_h with hβ_hl hβ_hr,
unfold_coes at hβ_hr,
unfold matrix_to_linear_map at hβ_hr,
simp at hβ_hr,
unfold matrix_to_map at hβ_hr,
unfold_coes,
unfold mat_to_finite_free_module_is_linear,
simp,
unfold mat_to_finite_free_module,
unfold mat_mul_vec,
unfold vec_to_mat,
unfold matrix.mul,
unfold finite_free_module_to_vec,
rw [β hβ_hr],
funext,
simp,
conv in (v _ * (Mα΅) _ I)
begin
rw [finite_free_module_eq_row_mat v _],
end,
conv in (M I _ * v _ )
begin
rw [finite_free_module_eq_col_mat v _],
end,
rw [eq_comm],
show (matrix.mul F (finite_free_module_to_row_mat v) (Mα΅)) 0 I = _,
show _ = matrix.mul F M (finite_free_module_to_col_mat v) I 0,
show _ =(matrix.mul F M (finite_free_module_to_col_mat v))α΅ 0 I,
rw [transpose_of_product],
have hβ : (finite_free_module_to_row_mat v) = finite_free_module_to_col_mat vα΅,
unfold finite_free_module_to_row_mat,
unfold finite_free_module_to_col_mat,
funext,
rw [transpose],
rw [hβ],
existsi v,
exact hβ,
end
end eigen
namespace Cayley_Hamilton
open eigen
theorem L0 {F : Type} [comm_ring F] {n : β} (v : finite_free_module F n):
β eva : F, smul_M eva (vec_to_mat (finite_free_module_to_vec v)) =
matrix.mul F (smul_M eva (matrix.identity_matrix F)) (vec_to_mat (finite_free_module_to_vec v)) :=
begin
intro eva,
unfold smul_M,
unfold matrix.mul,
funext,
unfold matrix.identity_matrix,
unfold vec_to_mat,
unfold finite_free_module_to_vec,
simp,
conv in ( _ * _ * _)
begin
rw [mul_assoc],
end,
conv
begin
to_rhs,
rw [β finset.mul_sum],
end,
congr,
rw [β @finset.sum_single _ _ _ _ (Ξ» (x : fin n), ite (I = x) 1 0 * v x) I _],
simp,
intro J,
intro h,
funext,
simp,
split_ifs,
simp,
apply false.elim,
contradiction,
simp,
end
lemma sub_eq_sub {R : Type} [ring R] {a b : β} (A B : matrix R a b) :
A - B = matrix.sub R A B :=
begin
refl,
end
@[simp] lemma val_eq_coe {Ξ± Ξ² Ξ³ : Type*} [ring Ξ±] [module Ξ± Ξ²] [module Ξ± Ξ³]
(f : linear_map Ξ² Ξ³) : f.val = f := rfl
theorem L1 {R : Type} [comm_ring R] {n : β} (M : matrix R n n) (v : finite_free_module R n):
β eva : R, (smul' M v = smul eva v) β
smul' (matrix.sub R M (smul_M eva (@matrix.identity_matrix R _ n))) v = (0 : finite_free_module R n) :=
begin
intro eva,
split,
intro hβ,
rw [β sub_eq_zero] at hβ,
unfold smul' at hβ,
unfold smul',
simp only [smul_eq_smul_two] at hβ,
unfold mat_mul_vec,
unfold mat_mul_vec at hβ,
rw val_eq_coe at hβ,
conv at hβ
begin
to_lhs,
rw β @linear_map.map_sub _ _ _ _ _ _ (mat_to_finite_free_module_is_linear) _ _,
end,
have hβ : matrix.sub R (matrix.mul R M (vec_to_mat (finite_free_module_to_vec v))) (smul_M eva (vec_to_mat (finite_free_module_to_vec v)))
= matrix.mul R (matrix.sub R M (smul_M eva (matrix.identity_matrix R))) (vec_to_mat (finite_free_module_to_vec v)),
rw [β matrix.mul_sub_mul],
congr,
rw [L0],
conv at hβ in (matrix.mul R M _ - smul_M eva _)
begin
rw [sub_eq_sub],
rw [hβ],
end,
exact hβ,
intro hβ,
rw [β sub_eq_zero],
unfold smul' at hβ,
unfold_coes at hβ,
unfold mat_mul_vec at hβ,
conv at hβ in ((matrix.mul R (matrix.sub R _ _) _))
begin
rw [β matrix.mul_sub_mul],
end,
unfold mat_to_finite_free_module_is_linear at hβ,
simp at hβ,
have hβ : mat_to_finite_free_module
(matrix.sub R (matrix.mul R M (vec_to_mat (finite_free_module_to_vec v)))
(matrix.mul R (smul_M eva (matrix.identity_matrix R)) (vec_to_mat (finite_free_module_to_vec v)))) =
smul' M v - smul eva v,
unfold smul',
unfold smul,
unfold mat_to_finite_free_module,
unfold_coes,
unfold mat_to_finite_free_module_is_linear,
simp,
unfold mat_to_finite_free_module,
unfold matrix.sub,
unfold mat_mul_vec,
simp,
funext,
congr,
unfold matrix.mul,
unfold vec_to_mat,
unfold finite_free_module_to_vec,
simp,
unfold smul_M,
conv in (_ * _ * _)
begin
rw [mul_assoc],
end,
rw [β finset.mul_sum],
congr,
rw [eq_comm],
unfold matrix.identity_matrix,
rw [β @finset.sum_single _ _ _ _ (Ξ» (x : fin n), ite (I = x) 1 0 * v x) I _],
simp,
intro J,
intro h,
funext,
simp,
split_ifs,
simp,
apply false.elim,
contradiction,
simp,
rw [hβ] at hβ,
exact hβ,
end
-- noncomputable def det_grl {R : Type} [comm_ring R] : Ξ {n : β} (M : matrix R (n + 1) (n + 1))
-- (a : fin (n + 1)), R
-- | 0 M a := M 0 0
-- | (n + 1) M a :=
-- finset.sum finset.univ (Ξ» (K : fin (n + 1)), M a K *
-- (((-1) ^ (a.val + K.val)) * (Ξ» b : fin n, @det_grl n (minor M a.val K.val) b)))
-- theorem det_grl_eq_det {R : Type} [comm_ring R] :
-- β n : β, β M : matrix R (n + 1) (n + 1), β a : fin (n + 1),
-- det_grl M a = det M :=
-- begin
-- intros n M a,
-- unfold det,
-- unfold det_grl,
-- end
theorem L3 {R : Type} [comm_ring R] {n : β} :
β a : R,
smul_M a (@matrix.identity_matrix _ _ n) = Ξ» I J, if I = J then a else 0 :=
begin
intro a,
unfold smul_M,
funext,
unfold matrix.identity_matrix,
split_ifs,
rw [mul_one],
rw [mul_zero],
end
#check equiv.perm
theorem L2 {R : Type} [comm_ring R] {n : β} (M : matrix R (n + 1) (n + 1)) :
smul_M (det M) (@matrix.identity_matrix _ _ (n + 1)) = matrix.mul R M (adj M) :=
begin
rw [L3],
funext,
unfold matrix.mul,
split_ifs,
unfold adj,
unfold transpose,
unfold cofactor,
unfold_coes,
unfold det,
unfold minor,
funext,
sorry
end
theorem Cayley_Hamilton {R : Type} [comm_ring R] {n : β} (M : matrix R n n) :
β eva : R, @det n R _ (matrix.sub R M (smul_M eva (@matrix.identity_matrix R _ n))) = 0
β is_eigenvalue_M M eva :=
begin
intro,
split,
intro hβ,
show is_eigenvalue_M M eva,
sorry,
intro hβ,
unfold is_eigenvalue_M at hβ,
cases hβ with v,
cases hβ_h with hβ_hl hβ_hl,
rw [L1] at hβ_hl,
sorry
end
end Cayley_Hamilton
namespace diagonal
def is_diagonal_mat {R : Type} [ring R] {n: β} (M : matrix R n n) : Prop :=
β I J, (I β J) β (M I J = 0)
-- Proposition 7.5. Suppose V is a f.d.v.s, and B = {v1, ..., vn} is a basis.
-- Suppose T : V β V is a linear map. Then,
-- [T]B is diagonalβv1,...,vn are eigenvectors of T
-- theorem diagonal_eigen_basis {R : Type} [ring R] {a b : β} (M : )
-- Definition 7.6. If V is a f.d.v.s, and T : V β V is a linear map, say that T is diagonalisable if
-- there a basis of V consisting of eigenvectors of V .
-- Corollary 7.7. Suppose V is a f.d.v.s. with a basis B = {v1, ..., vn}. Suppose T : V β V is a
-- linear map. Then the following are equivalent: i T is diagonalisable
-- ii There is a basis C of V with [T]C is diagonal
-- iii There is an invertible nΓn matrix P with Pβ1[T]BP a diagonal matrix
end diagonal |
f5c62f252734eac79a36068e4645b07afcaa9fbd | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/tactic/omega/nat/preterm_auto.lean | d946a9c83be81cd72023cbad6debf44fb4c5f170 | [] | no_license | AurelienSaue/Mathlib4_auto | f538cfd0980f65a6361eadea39e6fc639e9dae14 | 590df64109b08190abe22358fabc3eae000943f2 | refs/heads/master | 1,683,906,849,776 | 1,622,564,669,000 | 1,622,564,669,000 | 371,723,747 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,640 | lean | /-
Copyright (c) 2019 Seul Baek. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Seul Baek
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.tactic.omega.term
import Mathlib.PostPort
universes l
namespace Mathlib
/-
Linear natural number arithmetic terms in pre-normalized form.
-/
namespace omega
namespace nat
/-- The shadow syntax for arithmetic terms. All constants are reified to `cst`
(e.g., `5` is reified to `cst 5`) and all other atomic terms are reified to
`exp` (e.g., `5 * (list.length l)` is reified to `exp 5 \`(list.length l)`).
`exp` accepts a coefficient of type `nat` as its first argument because
multiplication by constant is allowed by the omega test. -/
/-- Similar to `exprterm`, except that all exprs are now replaced with
de Brujin indices of type `nat`. This is akin to generalizing over
the terms represented by the said exprs. -/
inductive preterm where
| cst : β β preterm
| var : β β β β preterm
| add : preterm β preterm β preterm
| sub : preterm β preterm β preterm
namespace preterm
/-- Helper tactic for proof by induction over preterms -/
/-- Preterm evaluation -/
def val (v : β β β) : preterm β β := sorry
@[simp] theorem val_const {v : β β β} {m : β} : val v (cst m) = m := rfl
@[simp] theorem val_var {v : β β β} {m : β} {n : β} : val v (var m n) = m * v n := sorry
@[simp] theorem val_add {v : β β β} {t : preterm} {s : preterm} :
val v (add t s) = val v t + val v s :=
rfl
@[simp] theorem val_sub {v : β β β} {t : preterm} {s : preterm} :
val v (sub t s) = val v t - val v s :=
rfl
/-- Fresh de Brujin index not used by any variable in argument -/
def fresh_index : preterm β β := sorry
/-- If variable assignments `v` and `w` agree on all variables that occur
in term `t`, the value of `t` under `v` and `w` are identical. -/
theorem val_constant (v : β β β) (w : β β β) (t : preterm) :
(β (x : β), x < fresh_index t β v x = w x) β val v t = val w t :=
sorry
def repr : preterm β string := sorry
@[simp] def add_one (t : preterm) : preterm := add t (cst 1)
/-- Preterm is free of subtractions -/
def sub_free : preterm β Prop := sorry
end preterm
/-- Return a term (which is in canonical form by definition)
that is equivalent to the input preterm -/
@[simp] def canonize : preterm β term := sorry
@[simp] theorem val_canonize {v : β β β} {t : preterm} :
preterm.sub_free t β term.val (fun (x : β) => β(v x)) (canonize t) = β(preterm.val v t) :=
sorry
end Mathlib |
069dc5dd4471ebd4a20f0702e8eae08e8232c99e | 12dabd587ce2621d9a4eff9f16e354d02e206c8e | /world08/level07.lean | 7ce6c43e9023460d4c83cadb3d06efba83e5b69a | [] | no_license | abdelq/natural-number-game | a1b5b8f1d52625a7addcefc97c966d3f06a48263 | bbddadc6d2e78ece2e9acd40fa7702ecc2db75c2 | refs/heads/master | 1,668,606,478,691 | 1,594,175,058,000 | 1,594,175,058,000 | 278,673,209 | 0 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 138 | lean | theorem add_right_cancel_iff (t a b : mynat) : a + t = b + t β a = b :=
begin
split,
exact add_right_cancel _ _ _,
intro h,
rwa h,
end
|
5c8c32e58fb15521e9fecd27e339015764376583 | fe84e287c662151bb313504482b218a503b972f3 | /src/data/list_extra.lean | fa5a8e2363853c4113cff5d96bbd2b10e9f757a9 | [] | no_license | NeilStrickland/lean_lib | 91e163f514b829c42fe75636407138b5c75cba83 | 6a9563de93748ace509d9db4302db6cd77d8f92c | refs/heads/master | 1,653,408,198,261 | 1,652,996,419,000 | 1,652,996,419,000 | 181,006,067 | 4 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 3,655 | lean | import data.list.basic data.list.big_operators logic.embedding
import tactic.squeeze
namespace list
universes u v w x
variables {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} {Ξ΄ : Type x}
open list
def all_prop {Ξ± : Type*} (p : Ξ± β Prop) : β (l : list Ξ±), Prop
| nil := true
| (a :: l) := (p a) β§ (all_prop l)
lemma all_prop_iff {Ξ± : Type*} {p : Ξ± β Prop} : β {l : list Ξ±},
all_prop p l β β a, a β l β p a
| nil := by {rw[all_prop,true_iff],intros a ha,cases ha,}
| (cons m l) :=
by {rw[all_prop],split,
{rintro β¨hm,hlβ© a ha,rcases ha,exact ha.symm βΈ hm,
exact (all_prop_iff.mp hl) a ha},
{intro h,split,
exact h m (mem_cons_self m l),
apply all_prop_iff.mpr,intros a ha,
exact h a (mem_cons_of_mem m ha),
}
}
def cons_embedding (a : Ξ±) : list Ξ± βͺ list Ξ± := β¨cons a,cons_injectiveβ©
def concat_embedding (a : Ξ±) : list Ξ± βͺ list Ξ± :=
β¨Ξ» l, l ++ [a],Ξ» lβ lβ e, append_right_cancel eβ©
def rtake (n : β) (l : list Ξ±) := l.drop (l.length - n)
def rdrop (n : β) (l : list Ξ±) := l.take (l.length - n)
lemma eq_singleton : β (l : list Ξ±) (h : l.length = 1),
l = [l.nth_le 0 (by {rw[h],exact nat.lt_succ_self 0})]
| [] h := by {cases h}
| [a] h := rfl
| (_ :: _ :: _) h := by {cases h}
lemma nth_le_congr (l : list Ξ±) {nβ nβ : β} (hβ : nβ < l.length) (e : nβ = nβ) :
l.nth_le nβ hβ = l.nth_le nβ (e βΈ hβ) := by {cases e,refl}
lemma nth_le_append' : β (lβ : list Ξ±) {lβ : list Ξ±} {n : β} (hn : n < lβ.length),
(lβ ++ lβ).nth_le (lβ.length + n) ((list.length_append lβ lβ).symm βΈ (nat.add_lt_add_left hn lβ.length)) = lβ.nth_le n hn
| list.nil lβ n hn := by {congr,dsimp[length],rw[zero_add]}
| (a :: lβ) lβ n hn := begin
let h := nth_le_append' lβ hn,
dsimp[append],rw[h.symm],
have : lβ.length + 1 + n = (lβ.length + n) + 1 := by {rw[add_assoc,add_comm 1,β add_assoc],},
rw[nth_le_congr _ _ this,nth_le],refl,
end
#check list.nth_le_take
lemma nth_le_take_old : β {n m : β} {l : list Ξ±} (hn : n < m) (hm : m β€ l.length),
(l.take m).nth_le n (by {rw[length_take,min_eq_left hm], exact hn}) =
l.nth_le n (lt_of_lt_of_le hn hm)
| n 0 l hn hm := by {cases hn}
| n (m + 1) [] hn hm := by {cases hm}
| 0 (m + 1) (a :: l) hn hm := rfl
| (n + 1) (m + 1) (a :: l) hn hm :=
nth_le_take_old (nat.lt_of_succ_lt_succ hn) (nat.le_of_succ_le_succ hm)
#check list.nth_le_drop
lemma nth_le_drop_old : β {n m : β} {l : list Ξ±} (h : m + n < l.length),
(l.drop m).nth_le n (by {rw[length_drop],rw[add_comm] at h,exact lt_tsub_iff_right.mpr h}) =
l.nth_le (m + n) h
| n 0 l h := (nth_le_congr l h (zero_add n)).symm
| n (m + 1) [] h := by {cases h}
| n (m + 1) (a :: l) h := begin
dsimp[drop],
have : m + 1 + n = (m + n) + 1 := by {rw[add_assoc,add_comm 1,β add_assoc],},
rw[this] at h,
rw[nth_le_congr _ _ this],dsimp[nth_le],apply nth_le_drop_old,
end
lemma drop_eq_last {n : β} {l : list Ξ±} (h : l.length = n + 1) :
(l.drop n) = [l.nth_le n (h.symm βΈ n.lt_succ_self)] :=
begin
let t := l.drop n,
let a := l.nth_le n (h.symm βΈ n.lt_succ_self),
change t = [a],
let a' := t.nth_le 0 _,
have : a = a' := begin
dsimp[a,a',last],
let h := @list.nth_le_drop_old Ξ± 0 n l (by {rw[h,add_zero],exact n.lt_succ_self}),
rw[list.nth_le_congr _ _ (add_zero n)] at h,
exact h.symm,
end,
rw[this],
have t_len : t.length = 1 := by { rw[list.length_drop,h,nat.add_sub_cancel_left]},
exact eq_singleton t t_len,
end
#check list.sum_singleton
lemma sum_singleton_old [add_monoid Ξ±] (a : Ξ±) : [a].sum = a :=
by {rw[list.sum_cons,sum_nil,add_zero]}
end list |
443f265b100fdfb6ba105ff5a66d036fdb3c8bc4 | e38d5e91d30731bef617cc9b6de7f79c34cdce9a | /src/core/connection/join.lean | b6ee75d7ffaf921169a8ea9e1abe6bac9180c63c | [
"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 | 6,851 | lean | /-
Copyright (c) 2019 Bruno Bentzen. All rights reserved.
Released under the Apache License 2.0 (see "License");
Author: Bruno Bentzen
-/
import .meet
open interval
-- the join connection
def join.horn {A : Type} (kan : has_hcom2 A)
(p : I β A) : horn2 A :=
let u := horn1.mk (Ξ» _, p i0) (Ξ» _, p i0) p rfl rfl in
horn2.mk (Ξ» _ _, p i0)
(meet kan p) (Ξ» i _, p i)
(meet kan p) (Ξ» i _, p i)
((kan.eq0j (meet.horn kan p)).symm) rfl
((kan.eq0j (meet.horn kan p)).symm) rfl rfl
begin transitivity, apply kan.eq1i (meet.horn kan p), apply (kan.has1.t1 u) end
begin symmetry, transitivity, apply kan.eq1i (meet.horn kan p), apply (kan.has1.t1 u) end
rfl
def join.filler {A : Type} (kan : has_hcom2 A)
(p : I β A) : I β I β I β A :=
kan.hcom2 (join.horn kan p)
def join {A : Type} (kan : has_hcom2 A)
(p : I β A) : I β I β A :=
(join.filler kan p) i1
--notation p `[` j `β¨` i `]` kan := join kan p j i
namespace join
lemma face0i {A : Type} (kan : has_hcom2 A)
(p : I β A) : join kan p i0 = p :=
begin
transitivity, apply kan.eq0j (join.horn kan p),
transitivity, apply kan.eq1j (meet.horn kan p),
apply (kan.has1.t1 (horn1.mk (Ξ» _, p i0) (Ξ» _, p i0) p rfl rfl))
end
lemma face1i {A : Type} (kan : has_hcom2 A)
(p : I β A) : join kan p i1 = Ξ» _, p i1 :=
kan.eq1j (join.horn kan p)
lemma face1j {A : Type} (kan : has_hcom2 A)
(p : I β A) : (Ξ» i, join kan p i i1) = Ξ» _, p i1 :=
kan.eq1i (join.horn kan p)
lemma face0j {A : Type} (kan : has_hcom2 A)
(p : I β A) : (Ξ» i, join kan p i i0) = p :=
begin
transitivity, apply kan.eq0i, -- (join.horn kan p)
transitivity, apply kan.eq1j, -- (meet.horn kan p)
apply (kan.has1.t1) -- (horn1.mk (Ξ» _, p i0) (Ξ» _, p i0) p rfl rfl)
end
end join
/- p --> i
p i0 -----------------> p i1 j |
| || v
| ||
| ||
p | join p j i || Ξ» _, p i1
| ||
| ||
v ||
p i1 ================== p i1
Ξ» _, p i1
-/
/-def test2 {A : Type} {a b : A} (kan : has_hcom2 A) (p : path A a b) :
join kan (Ξ» (i : I), p @@ i) i0 i0 = eq.mp rfl (p@@i0) :=
begin
rw join.face0i, refl --apply path.app0
end
def test3 {A : Type} {a b : A} (kan : has_hcom2 A) (p : path A a b) :
join kan (Ξ» (i : I), p @@ i) i0 i1 = eq.mp rfl (p@@i1) :=
begin
rw join.face0i, refl
end
def test31 {A : Type} {a b : A} (kan : has_hcom2 A) (p : path A a b) :
(Ξ» i, p @@ i) i1 = eq.mp rfl (p @@ i1) :=
begin
refl
end
variables {A : Type} {a b : A} (kan : has_hcom2 A) (p : path A a b)
#check join kan (Ξ» i, p@@i) i0
#check (Ξ» i, p@@i)
#check @pathdp.abs
#check pathdp.abs (Ξ» i, p@@i) rfl rfl
#check pathdp.abs (join kan (Ξ» (i : I), p @@ i) i0) rfl rfl
def joinsquare22 {A : Type} {a b : A} (kan : has_hcom2 A) (p : path A a b) :
pathdp.abs (join kan (Ξ» i, p@@i) i0) rfl rfl (test2 kan p) (test3 kan p) =
eq.mp (path.tyeq p) p :=
begin
transitivity, apply pathdp.abseq, apply join.face0i, --rw pathdp.eta,
--have h : pathdp.abs (Ξ» (i : I), p @@ i) rfl rfl _ _ =
-- pathdp.abs (Ξ» (i : I), p @@ i) rfl rfl (pathdp.app0 p) (pathdp.app1 p) :=
-- pathdp.abs_irrel,
apply pathdp.abs_irrel',
--apply eq.mp, apply pathdp.abs_irrel,
transitivity, exact pathdp.eta p rfl rfl (pathdp.app0 p) (pathdp.app1 p),
end
def joinsquare {A : Type} {a b : A} (kan : has_hcom2 A) (p : path A a b) :
pathdp.abs (join kan (Ξ» i, p@@i) i0) rfl rfl (test2 kan p) (test3 kan p) =
eq.mp (path.tyeq p) p :=
begin
--induction ((join.face0i kan (Ξ» i, p@@i)).symm),
transitivity, apply pathdp.eta.eq,
end
def tyeq_refl {A : I β Type} {A0 A1 : Type} {a : A i0} {b : A i1} (p : Ξ i, A i)
{ha : A i0 = A i0} {hb : A i1 = A i1}
(p0 : p i0 = eq.mp ha a) (p1 : p i1 = eq.mp hb b) :
(pathd.tyeq (pathdp.abs p ha hb p0 p1)) = refl :=
sorry
def joinsquare'' {A : Type} {a b : A} (p : path A a b) :
pathdp.abs (pathdp.app p) rfl rfl rfl rfl = eq.mp (pathd.tyeq p) p :=
begin
induction p, --induction p_ha,
--induction (path.tyeq p),--induction p,
--induction ((join.face0i kan (Ξ» i, p@@i)).symm),
end
def joinsquare' {A : Type} {a b : A} (p : path A a b) (q : I β A)
(h0 : q i0 = eq.mp rfl (p @@ i0)) (h1 : q i1 = eq.mp rfl (p @@ i1)) :
pathdp.abs q rfl rfl h0 h1 =
eq.mp (path.tyeq p) p :=
begin
--induction ((join.face0i kan (Ξ» i, p@@i)).symm),
end
--calc
-- pathdp.abs (join kan (Ξ» i, p@@i) i0) rfl rfl (test2 kan p) (test3 kan p) =
-- pathdp.abs (Ξ» i, p @@ i) rfl rfl rfl rfl : eq.rec _ (join.face0i kan (Ξ» i, p@@i)).symm
-- ... = eq.mp (path.tyeq p) p : sorry
--eq.rec _ (join.face0i kan (Ξ» i, p@@i)).symm
-- eq.rec (eq.rec _ (pathdp.eta p (pathdp.app0 p) (pathdp.app1 p))) (join.face0i kan (Ξ» i, p@@i)).symm
-- (eq.rec _ (pathdp.eta (app p) (test2 kan p) (test3 kan p)))
--(eq.rec rfl (pathd.app0 p).symm) (eq.rec rfl (pathd.app1 p).symm)
def join0 {A : Type} {a b : A} (p : path A a b) : Type :=
pathdp (Ξ» j, pathd (Ξ» _, A) (p @@ j) b) p (path.refl b)
--set_option pp.implicit true
lemma pathjoin {A : Type} {a b : A} (kan : has_hcom2 A) (p : path A a b) :
pathdp (Ξ» j, pathd (Ξ» _, A) (p @@ j) b) p (path.refl b) :=
begin
fapply pathdp.abs,
intro j, fapply pathdp.abs,
intro i, apply join kan,
apply (Ξ» i, p @@ i), apply j, apply i,
repeat {refl},
apply (calc
join kan (Ξ» i, p@@i) j i0 = (Ξ» i, join kan (Ξ» i, p@@i) i i0) j : rfl
... = p@@j : by rw join.face0j kan ),
apply (calc
join kan (Ξ» i, p@@i) j i1 = (Ξ» i, join kan (Ξ» i, p@@i) i i1) j : rfl
... = p@@i1 : by rw join.face1j kan
... = b : by apply path.app1 ),
--rw pathd.app0, refl,
--rw pathd.app1, refl,
exact (eq.rec rfl (pathd.app0 p).symm),
exact (eq.rec rfl (pathd.app1 p).symm),
--simp, simp, transitivity, --rw join.face0i, -- (join.face0i kan (app p)),
simp, simp,
--rw join.face01 kan,
--exact (eq.rec (Ξ» x, pathdp.abs x _ _ _ _ = eq.rec p (eq.rec rfl (pathd.app0 p).symm)) (join.face0i kan (app p)) )
--rw join.face01 kan (@app (Ξ» (_x : I), A) ((Ξ» (_x : I), A) i0) ((Ξ» (_x : I), A) i1) a b _ _ p),
-- (@join A kan (@app (Ξ» (_x : I), A) ((Ξ» (_x : I), A) i0) ((Ξ» (_x : I), A) i1) a b _ _ p)
--apply pathdp.eta,
-- join kan (Ξ» (i : I), p@@i) j i0 = p@@j
end
-/
|
3ba4f47ddbadc760547428c3792263cb39d3b54f | 90edd5cdcf93124fe15627f7304069fdce3442dd | /stage0/src/Lean/Aesop/Tree.lean | 393f578e35a53421951fe88ea5bf97ffad5e5ed1 | [
"Apache-2.0"
] | permissive | JLimperg/lean4-aesop | 8a9d9cd3ee484a8e67fda2dd9822d76708098712 | 5c4b9a3e05c32f69a4357c3047c274f4b94f9c71 | refs/heads/master | 1,689,415,944,104 | 1,627,383,284,000 | 1,627,383,284,000 | 377,536,770 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 15,781 | lean | /-
Copyright (c) 2021 Jannis Limperg. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jannis Limperg, Asta Halkjær From
-/
import Lean.Aesop.MutAltTree
import Lean.Aesop.Rule
import Lean.Aesop.Util
import Lean.Aesop.Tracing
open Lean
open Lean.Meta
@[inlineIfReduce]
private def Bool.toYesNo : Bool β Format
| true => "yes"
| false => "no "
namespace Lean.Aesop
/-! ## Node IDs -/
structure GoalId where
toNat : Nat
deriving Inhabited, DecidableEq
namespace GoalId
protected def zero : GoalId :=
β¨0β©
protected def one : GoalId :=
β¨1β©
protected def succ : GoalId β GoalId
| β¨nβ© => β¨n + 1β©
def dummy : GoalId :=
β¨1000000000000000β©
instance : LT GoalId where
lt n m := n.toNat < m.toNat
instance : DecidableRel (Ξ± := GoalId) (Β· < Β·) :=
Ξ» n m => inferInstanceAs (Decidable (n.toNat < m.toNat))
instance : ToFormat GoalId where
format n := format n.toNat
end GoalId
/-! ## Rule Application IDs -/
structure RappId where
toNat : Nat
deriving Inhabited, DecidableEq
namespace RappId
protected def zero : RappId :=
β¨0β©
protected def succ : RappId β RappId
| β¨nβ© => β¨n + 1β©
protected def one : RappId :=
β¨1β©
def dummy : RappId :=
β¨1000000000000000β©
instance : LT RappId where
lt n m := n.toNat < m.toNat
instance : DecidableRel (Ξ± := RappId) (Β· < Β·) :=
Ξ» n m => inferInstanceAs $ Decidable (n.toNat < m.toNat)
instance : ToFormat RappId where
format n := format n.toNat
end RappId
/-! ## Goal Nodes and Rule Applications -/
structure GoalData : Type where
id : GoalId
goal : MVarId
successProbability : Percent
normalizationProof : Option Expr
failedRapps : List RegularRule
unsafeQueue : Option (List UnsafeRule)
proven? : Bool
unprovable? : Bool
irrelevant? : Bool
deriving Inhabited
namespace GoalData
def normal? (g : GoalData) : Bool :=
g.normalizationProof.isSome
protected structure MessageInfo where
showGoal : Bool
showUnsafeQueue : Bool
showFailedRapps : Bool
deriving Inhabited
open GoalData (MessageInfo)
variable [Monad m] [MonadOptions m]
protected def getMessageInfo (traceCtx : TraceContext) : m MessageInfo := do
return {
showGoal := (β TraceOption.showGoals.get traceCtx)
showUnsafeQueue := (β TraceOption.showUnsafeQueues.get traceCtx)
showFailedRapps := (β TraceOption.showFailedRapps.get traceCtx)
}
open MessageData in
protected def toMessageData' (minfo : MessageInfo) (g : GoalData) : MessageData :=
let unsafeQueueLength :=
match g.unsafeQueue with
| none => f!"<not selected>"
| some q => format q.length
m!"Goal {g.id} [{g.successProbability.toHumanString}]" ++ nodeFiltering #[
m!"Unsafe rules in queue: {unsafeQueueLength}, failed: {g.failedRapps.length}",
join
[ m!"normal: {g.normal?.toYesNo} | ",
m!"proven: {g.proven?.toYesNo} | ",
m!"unprovable: {g.unprovable?.toYesNo} | ",
m!"irrelevant: {g.irrelevant?.toYesNo}" ],
if Β¬ minfo.showGoal then none else
m!"Goal:{indentD $ ofGoal g.goal}",
if Β¬ minfo.showUnsafeQueue && g.unsafeQueue.isSome then none else
m!"Unsafe queue:{indentDUnlines $ g.unsafeQueue.get!.map toMessageData}",
if Β¬ minfo.showFailedRapps then none else
m!"Failed rule applications:{indentDUnlines $ g.failedRapps.map toMessageData}" ]
protected def toMessageData (traceCtx : TraceContext) (g : GoalData) :
m MessageData :=
return g.toMessageData' (β GoalData.getMessageInfo traceCtx)
protected def mkInitial (id : GoalId) (goal : MVarId) (successProbability : Percent) :
GoalData where
id := id
goal := goal
successProbability := successProbability
normalizationProof := none
failedRapps := []
unsafeQueue := none
proven? := false
unprovable? := false
irrelevant? := false
end GoalData
structure RappData : Type where
id : RappId
appliedRule : RegularRule
successProbability : Percent
proof : Expr
proven? : Bool
unprovable? : Bool
irrelevant? : Bool
deriving Inhabited
namespace RappData
protected structure MessageInfo where
showProof : Bool
open RappData (MessageInfo)
variable [Monad m] [MonadOptions m]
protected def getMessageInfo (traceCtx : TraceContext) : m MessageInfo :=
return {
showProof := (β TraceOption.showProofs.get traceCtx)
}
open MessageData in
protected def toMessageData' (minfo : MessageInfo) (r : RappData) : MessageData :=
m!"Rule application {r.id} [{r.successProbability}]" ++ nodeFiltering #[
toMessageData r.appliedRule,
join
[ m!"proven: {r.proven?.toYesNo} | ",
m!"unprovable: {r.unprovable?.toYesNo} | ",
m!"irrelevant: {r.irrelevant?.toYesNo}" ],
if Β¬ minfo.showProof then none else
m!"Proof:{indentD r.proof}" ]
protected def toMessageData (traceCtx : TraceContext) (r : RappData) :
m MessageData :=
return r.toMessageData' (β RappData.getMessageInfo traceCtx)
protected def mkInitial (id : RappId) (appliedRule : RegularRule)
(successProbability : Percent) (proof : Expr) : RappData where
id := id
appliedRule := appliedRule
successProbability := successProbability
proof := proof
proven? := false
unprovable? := false
irrelevant? := false
end RappData
abbrev Goal := MutAltTree IO.RealWorld GoalData RappData
abbrev GoalRef := IO.Ref Goal
abbrev Rapp := MutAltTree IO.RealWorld RappData GoalData
abbrev RappRef := IO.Ref Rapp
variable [Monad m] [MonadLiftT (ST IO.RealWorld) m]
/-! ## Functions on Goals -/
namespace Goal
/-! ### Constructors -/
@[inline]
protected def mk (parent : Option RappRef) (rapps : Array RappRef)
(data : GoalData) : Goal :=
MutAltTree.mk data parent rapps
/-! ### Getters -/
@[inline]
def rapps (g : Goal) : Array RappRef :=
g.children
@[inline]
def id (g : Goal) : GoalId :=
g.payload.id
@[inline]
def goal (g : Goal) : MVarId :=
g.payload.goal
@[inline]
def successProbability (g : Goal) : Percent :=
g.payload.successProbability
@[inline]
def normalizationProof (g : Goal) : Option Expr :=
g.payload.normalizationProof
@[inline]
def failedRapps (g : Goal) : List RegularRule :=
g.payload.failedRapps
@[inline]
def unsafeQueue (g : Goal) : Option (List UnsafeRule) :=
g.payload.unsafeQueue
@[inline]
def proven? (g : Goal) : Bool :=
g.payload.proven?
@[inline]
def unprovable? (g : Goal) : Bool :=
g.payload.unprovable?
@[inline]
def irrelevant? (g : Goal) : Bool :=
g.payload.irrelevant?
/-! ### Setters -/
@[inline]
def setId (id : GoalId) (g : Goal) : Goal :=
g.modifyPayload Ξ» d => { d with id := id }
@[inline]
def setGoal (goal : MVarId) (g : Goal) : Goal :=
g.modifyPayload Ξ» d => { d with goal := goal }
@[inline]
def setSuccessProbability (successProbability : Percent) (g : Goal) : Goal :=
g.modifyPayload Ξ» d => { d with successProbability := successProbability }
@[inline]
def setNormalizationProof (normalizationProof : Expr) (g : Goal) : Goal :=
g.modifyPayload Ξ» d => { d with normalizationProof := normalizationProof }
@[inline]
def setFailedRapps (failedRapps : List RegularRule) (g : Goal) : Goal :=
g.modifyPayload Ξ» d => { d with failedRapps := failedRapps }
@[inline]
def setUnsafeQueue (unsafeQueue : Option (List UnsafeRule)) (g : Goal) : Goal :=
g.modifyPayload Ξ» d => { d with unsafeQueue := unsafeQueue }
@[inline]
def setProven? (proven? : Bool) (g : Goal) : Goal :=
g.modifyPayload Ξ» d => { d with proven? := proven? }
@[inline]
def setUnprovable? (unprovable? : Bool) (g : Goal) : Goal :=
g.modifyPayload Ξ» d => { d with unprovable? := unprovable? }
@[inline]
def setIrrelevant? (irrelevant? : Bool) (g : Goal) : Goal :=
g.modifyPayload Ξ» d => { d with irrelevant? := irrelevant? }
/-! ### Miscellaneous -/
@[inline]
def normal? (g : Goal) : Bool :=
g.payload.normal?
def hasNoUnexpandedUnsafeRule (g : Goal) : Bool :=
match g.unsafeQueue with
| none => false
| some q => q.isEmpty
end Goal
/-! ## Functions on Rule Applications -/
namespace Rapp
/-! ### Constructors -/
@[inline]
protected def mk (parent : Option GoalRef) (subgoals : Array GoalRef)
(data : RappData) : Rapp :=
MutAltTree.mk data parent subgoals
/-! ### Getters -/
@[inline]
def subgoals (r : Rapp) : Array GoalRef :=
r.children
@[inline]
def id (r : Rapp) : RappId :=
r.payload.id
@[inline]
def appliedRule (r : Rapp) : RegularRule :=
r.payload.appliedRule
@[inline]
def successProbability (r : Rapp) : Percent :=
r.payload.successProbability
@[inline]
def proof (r : Rapp) : Expr :=
r.payload.proof
@[inline]
def proven? (r : Rapp) : Bool :=
r.payload.proven?
@[inline]
def unprovable? (r : Rapp) : Bool :=
r.payload.unprovable?
@[inline]
def irrelevant? (r : Rapp) : Bool :=
r.payload.irrelevant?
-- Setters
@[inline]
def setId (id : RappId) (r : Rapp) : Rapp :=
r.modifyPayload Ξ» r => { r with id := id }
@[inline]
def setAppliedRule (appliedRule : RegularRule) (r : Rapp) : Rapp :=
r.modifyPayload Ξ» r => { r with appliedRule := appliedRule }
@[inline]
def setSuccessProbability (successProbability : Percent) (r : Rapp) : Rapp :=
r.modifyPayload Ξ» r => { r with successProbability := successProbability }
@[inline]
def setProof (proof : Expr) (r : Rapp) : Rapp :=
r.modifyPayload Ξ» r => { r with proof := proof }
@[inline]
def setProven? (proven? : Bool) (r : Rapp) : Rapp :=
r.modifyPayload Ξ» r => { r with proven? := proven? }
@[inline]
def setUnprovable? (unprovable? : Bool) (r : Rapp) : Rapp :=
r.modifyPayload Ξ» r => { r with unprovable? := unprovable? }
@[inline]
def setIrrelevant? (irrelevant? : Bool) (r : Rapp) : Rapp :=
r.modifyPayload Ξ» r => { r with irrelevant? := irrelevant? }
/-! ### Miscellaneous -/
def subgoalsProven? (r : Rapp) : m Bool :=
r.subgoals.allM Ξ» subgoal => return (β subgoal.get).proven?
end Rapp
/-! ## Formatting -/
mutual
private partial def formatTreeGoal (goalMInfo : GoalData.MessageInfo)
(rappMInfo : RappData.MessageInfo) (goal : Goal) : m MessageData := do
let goalMsg := goal.payload.toMessageData' goalMInfo
let childrenMsgs β goal.rapps.mapM Ξ» c => do
formatTreeRapp goalMInfo rappMInfo (β c.get)
return goalMsg ++ indentD (MessageData.node childrenMsgs)
private partial def formatTreeRapp (goalMInfo : GoalData.MessageInfo)
(rappMInfo : RappData.MessageInfo) (rapp : Rapp) : m MessageData := do
let rappMsg := rapp.payload.toMessageData' rappMInfo
let childrenMsgs β rapp.subgoals.mapM Ξ» c => do
formatTreeGoal goalMInfo rappMInfo (β c.get)
return rappMsg ++ indentD (MessageData.node childrenMsgs)
end
namespace Goal
def toMessageData {m} [Monad m] [MonadOptions m] (traceCtx : TraceContext)
(g : Goal) : m MessageData := do
g.payload.toMessageData traceCtx
@[inline]
def treeToMessageData' :
GoalData.MessageInfo β RappData.MessageInfo β Goal β m MessageData :=
formatTreeGoal
def treeToMessageData [MonadOptions m] (traceCtx : TraceContext) (g : Goal) :
m MessageData := do
treeToMessageData' (β GoalData.getMessageInfo traceCtx)
(β RappData.getMessageInfo traceCtx) g
end Goal
namespace Rapp
def toMessageData {m} [Monad m] [MonadOptions m] (traceCtx : TraceContext)
(r : Rapp) : m MessageData := do
r.payload.toMessageData traceCtx
@[inline]
def treeToMessageData' :
GoalData.MessageInfo β RappData.MessageInfo β Rapp β m MessageData :=
formatTreeRapp
def treeToMessageData [MonadOptions m] (traceCtx : TraceContext) (r : Rapp) :
m MessageData := do
treeToMessageData' (β GoalData.getMessageInfo traceCtx)
(β RappData.getMessageInfo traceCtx) r
end Rapp
/-! ## Miscellaneous Functions on Goals -/
namespace Goal
def mayHaveUnexpandedRapp (g : Goal) : m Bool := do pure $
Β¬ g.hasNoUnexpandedUnsafeRule β§
Β¬ (β g.rapps.anyM Ξ» r => return (β r.get : Rapp).appliedRule.isSafe)
def hasProvableRapp (g : Goal) : m Bool :=
g.rapps.anyM Ξ» r => return Β¬ (β r.get).unprovable?
end Goal
/-! ## Proof Extraction -/
namespace GoalRef
/- May only be called *once*. The given goal must be proven. -/
partial def linkProofs (gref : GoalRef) : MetaM Unit := do
let g β gref.get
let (some r) β g.rapps.findSomeM? Ξ» r => do
let r β r.get
return if r.proven? then some r else none
| throwError "aesop/linkProofs: internal error: node {g.id} not proven"
r.subgoals.forM linkProofs
let goalMVar := g.goal
checkNotAssigned `aesop goalMVar
-- TODO check for type-correct assignment?
-- let goalType β getMVarType goalMVar
-- let (true) β isDefEq goalType r.proof | throwError
-- "aesop/linkProofs: internal error: proof of rule application {r.id} did not unify with the goal of its parent node {g.id}"
assignExprMVar g.goal r.proof
/- Only call this after `linkProofs` has been run. -/
def extractProof (gref : GoalRef) : MetaM Expr := do
let g β gref.get
match g.normalizationProof with
| none => instantiateMVars $ mkMVar g.goal
| some prf => instantiateMVars prf
end GoalRef
/-! ## Propagating Provability/Unprovability/Irrelevance -/
@[inline]
def Internal.setIrrelevant : Sum GoalRef RappRef β m Unit :=
MutAltTree.visitDown'
(Ξ» gref => do
let g : Goal β gref.get
if g.irrelevant?
then return false -- Subtree should already be marked as irrelevant.
else do
gref.set $ g.setIrrelevant? true
return true)
(Ξ» rref => do
let r : Rapp β rref.get
if r.irrelevant?
then return false
else do
rref.set $ r.setIrrelevant? true
return true)
def GoalRef.setIrrelevant : GoalRef β m Unit :=
Internal.setIrrelevant β Sum.inl
def RappRef.setIrrelevant : RappRef β m Unit :=
Internal.setIrrelevant β Sum.inr
@[inline]
def Internal.setProven : Sum GoalRef RappRef β m Unit :=
MutAltTree.visitUp'
-- Goals are unconditionally marked as proven.
(Ξ» gref => do
gref.modify Ξ» (g : Goal) => g.setProven? true
return true)
-- Rapps are marked as proven only if they are in fact proven, i.e. if all
-- their subgoals are (marked as) proven. In this case, we also need to
-- mark siblings of the rapp (and their descendants) as irrelevant.
(Ξ» rref => do
let r : Rapp β rref.get
if Β¬ (β r.subgoalsProven?)
then return false
else do
rref.set $ r.setProven? true
let siblings β MutAltTree.siblings rref
siblings.forM RappRef.setIrrelevant
return true)
def GoalRef.setProven : GoalRef β m Unit :=
Internal.setProven β Sum.inl
def RappRef.setProven : RappRef β m Unit :=
Internal.setProven β Sum.inr
@[inline]
def Internal.setUnprovable : Sum GoalRef RappRef β m Unit :=
MutAltTree.visitUp'
-- Goals are marked as unprovable only if they are in fact unprovable, i.e.
-- if all their rule applications are unprovable and they do not have
-- unexpanded rule applications. In this case, we also need to mark
-- siblings of the goal (and their descendants) as irrelevant.
(Ξ» gref => do
let g : Goal β gref.get
if (β g.mayHaveUnexpandedRapp <||> g.hasProvableRapp)
then return false
else do
gref.set $ g.setUnprovable? true
let siblings β MutAltTree.siblings gref
siblings.forM GoalRef.setIrrelevant
return true)
-- Rapps are unconditionally marked as unprovable.
(Ξ» rref => do
rref.modify Ξ» (r : Rapp) => r.setUnprovable? true
return true)
def GoalRef.setUnprovable : GoalRef β m Unit :=
Internal.setUnprovable β Sum.inl
def RappRef.setUnprovable : RappRef β m Unit :=
Internal.setUnprovable β Sum.inr
end Lean.Aesop
|
2692df9103eb831f4cb0d4c679003428c7492cbf | 4727251e0cd73359b15b664c3170e5d754078599 | /src/number_theory/zsqrtd/basic.lean | 23fc0249b1bfd54d73e81119bcab6e9c7f419e16 | [
"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 | 33,185 | lean | /-
Copyright (c) 2017 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import algebra.associated
import ring_theory.int.basic
import tactic.ring
/-! # β€[βd]
The ring of integers adjoined with a square root of `d : β€`.
After defining the norm, we show that it is a linearly ordered commutative ring,
as well as an integral domain.
We provide the universal property, that ring homomorphisms `β€βd β+* R` correspond
to choices of square roots of `d` in `R`.
-/
/-- The ring of integers adjoined with a square root of `d`.
These have the form `a + b βd` where `a b : β€`. The components
are called `re` and `im` by analogy to the negative `d` case. -/
structure zsqrtd (d : β€) :=
(re : β€)
(im : β€)
prefix `β€β`:100 := zsqrtd
namespace zsqrtd
section
parameters {d : β€}
instance : decidable_eq β€βd :=
by tactic.mk_dec_eq_instance
theorem ext : β {z w : β€βd}, z = w β z.re = w.re β§ z.im = w.im
| β¨x, yβ© β¨x', y'β© := β¨Ξ» h, by injection h; split; assumption,
Ξ» β¨hβ, hββ©, by congr; assumptionβ©
/-- Convert an integer to a `β€βd` -/
def of_int (n : β€) : β€βd := β¨n, 0β©
theorem of_int_re (n : β€) : (of_int n).re = n := rfl
theorem of_int_im (n : β€) : (of_int n).im = 0 := rfl
/-- The zero of the ring -/
instance : has_zero β€βd := β¨of_int 0β©
@[simp] theorem zero_re : (0 : β€βd).re = 0 := rfl
@[simp] theorem zero_im : (0 : β€βd).im = 0 := rfl
instance : inhabited β€βd := β¨0β©
/-- The one of the ring -/
instance : has_one β€βd := β¨of_int 1β©
@[simp] theorem one_re : (1 : β€βd).re = 1 := rfl
@[simp] theorem one_im : (1 : β€βd).im = 0 := rfl
/-- The representative of `βd` in the ring -/
def sqrtd : β€βd := β¨0, 1β©
@[simp] theorem sqrtd_re : (sqrtd : β€βd).re = 0 := rfl
@[simp] theorem sqrtd_im : (sqrtd : β€βd).im = 1 := rfl
/-- Addition of elements of `β€βd` -/
instance : has_add β€βd := β¨Ξ» z w, β¨z.1 + w.1, z.2 + w.2β©β©
@[simp] lemma add_def (x y x' y' : β€) : (β¨x, yβ© + β¨x', y'β© : β€βd) = β¨x + x', y + y'β© := rfl
@[simp] lemma add_re (z w : β€βd) : (z + w).re = z.re + w.re := rfl
@[simp] lemma add_im (z w : β€βd) : (z + w).im = z.im + w.im := rfl
@[simp] lemma bit0_re (z) : (bit0 z : β€βd).re = bit0 z.re := rfl
@[simp] lemma bit0_im (z) : (bit0 z : β€βd).im = bit0 z.im := rfl
@[simp] theorem bit1_re (z) : (bit1 z : β€βd).re = bit1 z.re := rfl
@[simp] theorem bit1_im (z) : (bit1 z : β€βd).im = bit0 z.im := by simp [bit1]
/-- Negation in `β€βd` -/
instance : has_neg β€βd := β¨Ξ» z, β¨-z.1, -z.2β©β©
@[simp] lemma neg_re (z : β€βd) : (-z).re = -z.re := rfl
@[simp] lemma neg_im (z : β€βd) : (-z).im = -z.im := rfl
/-- Multiplication in `β€βd` -/
instance : has_mul β€βd := β¨Ξ» z w, β¨z.1 * w.1 + d * z.2 * w.2, z.1 * w.2 + z.2 * w.1β©β©
@[simp] lemma mul_re (z w : β€βd) : (z * w).re = z.re * w.re + d * z.im * w.im := rfl
@[simp] lemma mul_im (z w : β€βd) : (z * w).im = z.re * w.im + z.im * w.re := rfl
instance : comm_ring β€βd :=
by refine_struct
{ add := (+),
zero := (0 : β€βd),
neg := has_neg.neg,
mul := (*),
sub := Ξ» a b, a + -b,
one := 1,
npow := @npow_rec (β€βd) β¨1β© β¨(*)β©,
nsmul := @nsmul_rec (β€βd) β¨0β© β¨(+)β©,
zsmul := @zsmul_rec (β€βd) β¨0β© β¨(+)β© β¨has_neg.negβ© };
intros; try { refl }; simp [ext, add_mul, mul_add, add_comm, add_left_comm, mul_comm, mul_left_comm]
instance : add_comm_monoid β€βd := by apply_instance
instance : add_monoid β€βd := by apply_instance
instance : monoid β€βd := by apply_instance
instance : comm_monoid β€βd := by apply_instance
instance : comm_semigroup β€βd := by apply_instance
instance : semigroup β€βd := by apply_instance
instance : add_comm_semigroup β€βd := by apply_instance
instance : add_semigroup β€βd := by apply_instance
instance : comm_semiring β€βd := by apply_instance
instance : semiring β€βd := by apply_instance
instance : ring β€βd := by apply_instance
instance : distrib β€βd := by apply_instance
/-- Conjugation in `β€βd`. The conjugate of `a + b βd` is `a - b βd`. -/
def conj (z : β€βd) : β€βd := β¨z.1, -z.2β©
@[simp] lemma conj_re (z : β€βd) : (conj z).re = z.re := rfl
@[simp] lemma conj_im (z : β€βd) : (conj z).im = -z.im := rfl
/-- `conj` as an `add_monoid_hom`. -/
def conj_hom : β€βd β+ β€βd :=
{ to_fun := conj,
map_add' := Ξ» β¨a, aiβ© β¨b, biβ©, ext.mpr β¨rfl, neg_add _ _β©,
map_zero' := ext.mpr β¨rfl, neg_zeroβ© }
@[simp] lemma conj_zero : conj (0 : β€βd) = 0 :=
conj_hom.map_zero
@[simp] lemma conj_one : conj (1 : β€βd) = 1 :=
by simp only [zsqrtd.ext, zsqrtd.conj_re, zsqrtd.conj_im, zsqrtd.one_im, neg_zero, eq_self_iff_true,
and_self]
@[simp] lemma conj_neg (x : β€βd) : (-x).conj = -x.conj := rfl
@[simp] lemma conj_add (x y : β€βd) : (x + y).conj = x.conj + y.conj :=
conj_hom.map_add x y
@[simp] lemma conj_sub (x y : β€βd) : (x - y).conj = x.conj - y.conj :=
conj_hom.map_sub x y
@[simp] lemma conj_conj {d : β€} (x : β€βd) : x.conj.conj = x :=
by simp only [ext, true_and, conj_re, eq_self_iff_true, neg_neg, conj_im]
instance : nontrivial β€βd :=
β¨β¨0, 1, dec_trivialβ©β©
@[simp] theorem coe_nat_re (n : β) : (n : β€βd).re = n :=
by induction n; simp *
@[simp] theorem coe_nat_im (n : β) : (n : β€βd).im = 0 :=
by induction n; simp *
theorem coe_nat_val (n : β) : (n : β€βd) = β¨n, 0β© :=
by simp [ext]
@[simp] theorem coe_int_re (n : β€) : (n : β€βd).re = n :=
by cases n; simp [*, int.of_nat_eq_coe, int.neg_succ_of_nat_eq]
@[simp] theorem coe_int_im (n : β€) : (n : β€βd).im = 0 :=
by cases n; simp *
theorem coe_int_val (n : β€) : (n : β€βd) = β¨n, 0β© :=
by simp [ext]
instance : char_zero β€βd :=
{ cast_injective := Ξ» m n, by simp [ext] }
@[simp] theorem of_int_eq_coe (n : β€) : (of_int n : β€βd) = n :=
by simp [ext, of_int_re, of_int_im]
@[simp] theorem smul_val (n x y : β€) : (n : β€βd) * β¨x, yβ© = β¨n * x, n * yβ© :=
by simp [ext]
theorem smul_re (a : β€) (b : β€βd) : (βa * b).re = a * b.re := by simp
theorem smul_im (a : β€) (b : β€βd) : (βa * b).im = a * b.im := by simp
@[simp] theorem muld_val (x y : β€) : sqrtd * β¨x, yβ© = β¨d * y, xβ© :=
by simp [ext]
@[simp] theorem dmuld : sqrtd * sqrtd = d :=
by simp [ext]
@[simp] theorem smuld_val (n x y : β€) : sqrtd * (n : β€βd) * β¨x, yβ© = β¨d * n * y, n * xβ© :=
by simp [ext]
theorem decompose {x y : β€} : (β¨x, yβ© : β€βd) = x + sqrtd * y :=
by simp [ext]
theorem mul_conj {x y : β€} : (β¨x, yβ© * conj β¨x, yβ© : β€βd) = x * x - d * y * y :=
by simp [ext, sub_eq_add_neg, mul_comm]
theorem conj_mul {a b : β€βd} : conj (a * b) = conj a * conj b :=
by { simp [ext], ring }
protected lemma coe_int_add (m n : β€) : (β(m + n) : β€βd) = βm + βn :=
(int.cast_ring_hom _).map_add _ _
protected lemma coe_int_sub (m n : β€) : (β(m - n) : β€βd) = βm - βn :=
(int.cast_ring_hom _).map_sub _ _
protected lemma coe_int_mul (m n : β€) : (β(m * n) : β€βd) = βm * βn :=
(int.cast_ring_hom _).map_mul _ _
protected lemma coe_int_inj {m n : β€} (h : (βm : β€βd) = βn) : m = n :=
by simpa using congr_arg re h
lemma coe_int_dvd_iff (z : β€) (a : β€βd) : βz β£ a β z β£ a.re β§ z β£ a.im :=
begin
split,
{ rintro β¨x, rflβ©,
simp only [add_zero, coe_int_re, zero_mul, mul_im, dvd_mul_right, and_self, mul_re, mul_zero,
coe_int_im] },
{ rintro β¨β¨r, hrβ©, β¨i, hiβ©β©,
use β¨r, iβ©,
rw [smul_val, ext],
exact β¨hr, hiβ© },
end
@[simp, norm_cast]
lemma coe_int_dvd_coe_int (a b : β€) : (a : β€βd) β£ b β a β£ b :=
begin
rw coe_int_dvd_iff,
split,
{ rintro β¨hre, -β©,
rwa [coe_int_re] at hre },
{ rw [coe_int_re, coe_int_im],
exact Ξ» hc, β¨hc, dvd_zero aβ© },
end
protected lemma eq_of_smul_eq_smul_left {a : β€} {b c : β€βd}
(ha : a β 0) (h : βa * b = a * c) : b = c :=
begin
rw ext at h β’,
apply and.imp _ _ h;
{ simp only [smul_re, smul_im],
exact int.eq_of_mul_eq_mul_left ha },
end
section gcd
lemma gcd_eq_zero_iff (a : β€βd) : int.gcd a.re a.im = 0 β a = 0 :=
by simp only [int.gcd_eq_zero_iff, ext, eq_self_iff_true, zero_im, zero_re]
lemma gcd_pos_iff (a : β€βd) : 0 < int.gcd a.re a.im β a β 0 :=
pos_iff_ne_zero.trans $ not_congr a.gcd_eq_zero_iff
lemma coprime_of_dvd_coprime {a b : β€βd} (hcoprime : is_coprime a.re a.im) (hdvd : b β£ a) :
is_coprime b.re b.im :=
begin
apply is_coprime_of_dvd,
{ rintro β¨hre, himβ©,
obtain rfl : b = 0,
{ simp only [ext, hre, eq_self_iff_true, zero_im, him, and_self, zero_re] },
rw zero_dvd_iff at hdvd,
simpa only [hdvd, zero_im, zero_re, not_coprime_zero_zero] using hcoprime },
{ intros z hz hznezero hzdvdu hzdvdv,
apply hz,
obtain β¨ha, hbβ© : z β£ a.re β§ z β£ a.im,
{ rw βcoe_int_dvd_iff,
apply dvd_trans _ hdvd,
rw coe_int_dvd_iff,
exact β¨hzdvdu, hzdvdvβ© },
exact hcoprime.is_unit_of_dvd' ha hb },
end
lemma exists_coprime_of_gcd_pos {a : β€βd} (hgcd : 0 < int.gcd a.re a.im) :
β b : β€βd, a = ((int.gcd a.re a.im : β€) : β€βd) * b β§ is_coprime b.re b.im :=
begin
obtain β¨re, im, H1, Hre, Himβ© := int.exists_gcd_one hgcd,
rw [mul_comm] at Hre Him,
refine β¨β¨re, imβ©, _, _β©,
{ rw [smul_val, ext, βHre, βHim], split; refl },
{ rw [βint.gcd_eq_one_iff_coprime, H1] }
end
end gcd
/-- Read `sq_le a c b d` as `a βc β€ b βd` -/
def sq_le (a c b d : β) : Prop := c*a*a β€ d*b*b
theorem sq_le_of_le {c d x y z w : β} (xz : z β€ x) (yw : y β€ w) (xy : sq_le x c y d) :
sq_le z c w d :=
le_trans (mul_le_mul (nat.mul_le_mul_left _ xz) xz (nat.zero_le _) (nat.zero_le _)) $
le_trans xy (mul_le_mul (nat.mul_le_mul_left _ yw) yw (nat.zero_le _) (nat.zero_le _))
theorem sq_le_add_mixed {c d x y z w : β} (xy : sq_le x c y d) (zw : sq_le z c w d) :
c * (x * z) β€ d * (y * w) :=
nat.mul_self_le_mul_self_iff.2 $
by simpa [mul_comm, mul_left_comm] using
mul_le_mul xy zw (nat.zero_le _) (nat.zero_le _)
theorem sq_le_add {c d x y z w : β} (xy : sq_le x c y d) (zw : sq_le z c w d) :
sq_le (x + z) c (y + w) d :=
begin
have xz := sq_le_add_mixed xy zw,
simp [sq_le, mul_assoc] at xy zw,
simp [sq_le, mul_add, mul_comm, mul_left_comm, add_le_add, *]
end
theorem sq_le_cancel {c d x y z w : β} (zw : sq_le y d x c) (h : sq_le (x + z) c (y + w) d) :
sq_le z c w d :=
begin
apply le_of_not_gt,
intro l,
refine not_le_of_gt _ h,
simp [sq_le, mul_add, mul_comm, mul_left_comm, add_assoc],
have hm := sq_le_add_mixed zw (le_of_lt l),
simp [sq_le, mul_assoc] at l zw,
exact lt_of_le_of_lt (add_le_add_right zw _)
(add_lt_add_left (add_lt_add_of_le_of_lt hm (add_lt_add_of_le_of_lt hm l)) _)
end
theorem sq_le_smul {c d x y : β} (n : β) (xy : sq_le x c y d) : sq_le (n * x) c (n * y) d :=
by simpa [sq_le, mul_left_comm, mul_assoc] using
nat.mul_le_mul_left (n * n) xy
theorem sq_le_mul {d x y z w : β} :
(sq_le x 1 y d β sq_le z 1 w d β sq_le (x * w + y * z) d (x * z + d * y * w) 1) β§
(sq_le x 1 y d β sq_le w d z 1 β sq_le (x * z + d * y * w) 1 (x * w + y * z) d) β§
(sq_le y d x 1 β sq_le z 1 w d β sq_le (x * z + d * y * w) 1 (x * w + y * z) d) β§
(sq_le y d x 1 β sq_le w d z 1 β sq_le (x * w + y * z) d (x * z + d * y * w) 1) :=
by refine β¨_, _, _, _β©;
{ intros xy zw,
have := int.mul_nonneg (sub_nonneg_of_le (int.coe_nat_le_coe_nat_of_le xy))
(sub_nonneg_of_le (int.coe_nat_le_coe_nat_of_le zw)),
refine int.le_of_coe_nat_le_coe_nat (le_of_sub_nonneg _),
convert this,
simp only [one_mul, int.coe_nat_add, int.coe_nat_mul],
ring }
/-- "Generalized" `nonneg`. `nonnegg c d x y` means `a βc + b βd β₯ 0`;
we are interested in the case `c = 1` but this is more symmetric -/
def nonnegg (c d : β) : β€ β β€ β Prop
| (a : β) (b : β) := true
| (a : β) -[1+ b] := sq_le (b+1) c a d
| -[1+ a] (b : β) := sq_le (a+1) d b c
| -[1+ a] -[1+ b] := false
theorem nonnegg_comm {c d : β} {x y : β€} : nonnegg c d x y = nonnegg d c y x :=
by induction x; induction y; refl
theorem nonnegg_neg_pos {c d} : Ξ {a b : β}, nonnegg c d (-a) b β sq_le a d b c
| 0 b := β¨by simp [sq_le, nat.zero_le], Ξ»a, trivialβ©
| (a+1) b := by rw β int.neg_succ_of_nat_coe; refl
theorem nonnegg_pos_neg {c d} {a b : β} : nonnegg c d a (-b) β sq_le b c a d :=
by rw nonnegg_comm; exact nonnegg_neg_pos
theorem nonnegg_cases_right {c d} {a : β} :
Ξ {b : β€}, (Ξ x : β, b = -x β sq_le x c a d) β nonnegg c d a b
| (b:nat) h := trivial
| -[1+ b] h := h (b+1) rfl
theorem nonnegg_cases_left {c d} {b : β} {a : β€} (h : Ξ x : β, a = -x β sq_le x d b c) :
nonnegg c d a b :=
cast nonnegg_comm (nonnegg_cases_right h)
section norm
/-- The norm of an element of `β€[βd]`. -/
def norm (n : β€βd) : β€ := n.re * n.re - d * n.im * n.im
lemma norm_def (n : β€βd) : n.norm = n.re * n.re - d * n.im * n.im := rfl
@[simp] lemma norm_zero : norm 0 = 0 := by simp [norm]
@[simp] lemma norm_one : norm 1 = 1 := by simp [norm]
@[simp] lemma norm_int_cast (n : β€) : norm n = n * n := by simp [norm]
@[simp] lemma norm_nat_cast (n : β) : norm n = n * n := norm_int_cast n
@[simp] lemma norm_mul (n m : β€βd) : norm (n * m) = norm n * norm m :=
by { simp only [norm, mul_im, mul_re], ring }
/-- `norm` as a `monoid_hom`. -/
def norm_monoid_hom : β€βd β* β€ :=
{ to_fun := norm,
map_mul' := norm_mul,
map_one' := norm_one }
lemma norm_eq_mul_conj (n : β€βd) : (norm n : β€βd) = n * n.conj :=
by cases n; simp [norm, conj, zsqrtd.ext, mul_comm, sub_eq_add_neg]
@[simp] lemma norm_neg (x : β€βd) : (-x).norm = x.norm :=
coe_int_inj $ by simp only [norm_eq_mul_conj, conj_neg, neg_mul,
mul_neg, neg_neg]
@[simp] lemma norm_conj (x : β€βd) : x.conj.norm = x.norm :=
coe_int_inj $ by simp only [norm_eq_mul_conj, conj_conj, mul_comm]
lemma norm_nonneg (hd : d β€ 0) (n : β€βd) : 0 β€ n.norm :=
add_nonneg (mul_self_nonneg _)
(by rw [mul_assoc, neg_mul_eq_neg_mul];
exact (mul_nonneg (neg_nonneg.2 hd) (mul_self_nonneg _)))
lemma norm_eq_one_iff {x : β€βd} : x.norm.nat_abs = 1 β is_unit x :=
β¨Ξ» h, is_unit_iff_dvd_one.2 $
(le_total 0 (norm x)).cases_on
(Ξ» hx, show x β£ 1, from β¨x.conj,
by rwa [β int.coe_nat_inj', int.nat_abs_of_nonneg hx,
β @int.cast_inj (β€βd) _ _, norm_eq_mul_conj, eq_comm] at hβ©)
(Ξ» hx, show x β£ 1, from β¨- x.conj,
by rwa [β int.coe_nat_inj', int.of_nat_nat_abs_of_nonpos hx,
β @int.cast_inj (β€βd) _ _, int.cast_neg, norm_eq_mul_conj, neg_mul_eq_mul_neg,
eq_comm] at hβ©),
Ξ» h, let β¨y, hyβ© := is_unit_iff_dvd_one.1 h in begin
have := congr_arg (int.nat_abs β norm) hy,
rw [function.comp_app, function.comp_app, norm_mul, int.nat_abs_mul,
norm_one, int.nat_abs_one, eq_comm, nat.mul_eq_one_iff] at this,
exact this.1
endβ©
lemma is_unit_iff_norm_is_unit {d : β€} (z : β€βd) : is_unit z β is_unit z.norm :=
by rw [int.is_unit_iff_nat_abs_eq, norm_eq_one_iff]
lemma norm_eq_one_iff' {d : β€} (hd : d β€ 0) (z : β€βd) : z.norm = 1 β is_unit z :=
by rw [βnorm_eq_one_iff, βint.coe_nat_inj', int.nat_abs_of_nonneg (norm_nonneg hd z),
int.coe_nat_one]
lemma norm_eq_zero_iff {d : β€} (hd : d < 0) (z : β€βd) : z.norm = 0 β z = 0 :=
begin
split,
{ intro h,
rw [ext, zero_re, zero_im],
rw [norm_def, sub_eq_add_neg, mul_assoc] at h,
have left := mul_self_nonneg z.re,
have right := neg_nonneg.mpr (mul_nonpos_of_nonpos_of_nonneg hd.le (mul_self_nonneg z.im)),
obtain β¨ha, hbβ© := (add_eq_zero_iff' left right).mp h,
split; apply eq_zero_of_mul_self_eq_zero,
{ exact ha },
{ rw [neg_eq_zero, mul_eq_zero] at hb,
exact hb.resolve_left hd.ne } },
{ rintro rfl, exact norm_zero }
end
lemma norm_eq_of_associated {d : β€} (hd : d β€ 0) {x y : β€βd} (h : associated x y) :
x.norm = y.norm :=
begin
obtain β¨u, rflβ© := h,
rw [norm_mul, (norm_eq_one_iff' hd _).mpr u.is_unit, mul_one],
end
end norm
end
section
parameter {d : β}
/-- Nonnegativity of an element of `β€βd`. -/
def nonneg : β€βd β Prop | β¨a, bβ© := nonnegg d 1 a b
instance : has_le β€βd := β¨Ξ» a b, nonneg (b - a)β©
instance : has_lt β€βd := β¨Ξ» a b, Β¬ b β€ aβ©
instance decidable_nonnegg (c d a b) : decidable (nonnegg c d a b) :=
by cases a; cases b; repeat {rw int.of_nat_eq_coe}; unfold nonnegg sq_le; apply_instance
instance decidable_nonneg : Ξ (a : β€βd), decidable (nonneg a)
| β¨a, bβ© := zsqrtd.decidable_nonnegg _ _ _ _
instance decidable_le : @decidable_rel (β€βd) (β€) := Ξ» _ _, decidable_nonneg _
theorem nonneg_cases : Ξ {a : β€βd}, nonneg a β β x y : β, a = β¨x, yβ© β¨ a = β¨x, -yβ© β¨ a = β¨-x, yβ©
| β¨(x : β), (y : β)β© h := β¨x, y, or.inl rflβ©
| β¨(x : β), -[1+ y]β© h := β¨x, y+1, or.inr $ or.inl rflβ©
| β¨-[1+ x], (y : β)β© h := β¨x+1, y, or.inr $ or.inr rflβ©
| β¨-[1+ x], -[1+ y]β© h := false.elim h
lemma nonneg_add_lem {x y z w : β} (xy : nonneg β¨x, -yβ©) (zw : nonneg β¨-z, wβ©) :
nonneg (β¨x, -yβ© + β¨-z, wβ©) :=
have nonneg β¨int.sub_nat_nat x z, int.sub_nat_nat w yβ©, from int.sub_nat_nat_elim x z
(Ξ»m n i, sq_le y d m 1 β sq_le n 1 w d β nonneg β¨i, int.sub_nat_nat w yβ©)
(Ξ»j k, int.sub_nat_nat_elim w y
(Ξ»m n i, sq_le n d (k + j) 1 β sq_le k 1 m d β nonneg β¨int.of_nat j, iβ©)
(Ξ»m n xy zw, trivial)
(Ξ»m n xy zw, sq_le_cancel zw xy))
(Ξ»j k, int.sub_nat_nat_elim w y
(Ξ»m n i, sq_le n d k 1 β sq_le (k + j + 1) 1 m d β nonneg β¨-[1+ j], iβ©)
(Ξ»m n xy zw, sq_le_cancel xy zw)
(Ξ»m n xy zw, let t := nat.le_trans zw (sq_le_of_le (nat.le_add_right n (m+1)) le_rfl xy) in
have k + j + 1 β€ k, from nat.mul_self_le_mul_self_iff.2 (by repeat{rw one_mul at t}; exact t),
absurd this (not_le_of_gt $ nat.succ_le_succ $ nat.le_add_right _ _))) (nonnegg_pos_neg.1 xy)
(nonnegg_neg_pos.1 zw),
show nonneg β¨_, _β©, by rw [neg_add_eq_sub];
rwa [int.sub_nat_nat_eq_coe,int.sub_nat_nat_eq_coe] at this
lemma nonneg.add {a b : β€βd} (ha : nonneg a) (hb : nonneg b) : nonneg (a + b) :=
begin
rcases nonneg_cases ha with β¨x, y, rfl|rfl|rflβ©;
rcases nonneg_cases hb with β¨z, w, rfl|rfl|rflβ©,
{ trivial },
{ refine nonnegg_cases_right (Ξ»i h, sq_le_of_le _ _ (nonnegg_pos_neg.1 hb)),
{ exact int.coe_nat_le.1 (le_of_neg_le_neg (@int.le.intro _ _ y (by simp [add_comm, *]))) },
{ apply nat.le_add_left } },
{ refine nonnegg_cases_left (Ξ»i h, sq_le_of_le _ _ (nonnegg_neg_pos.1 hb)),
{ exact int.coe_nat_le.1 (le_of_neg_le_neg (@int.le.intro _ _ x (by simp [add_comm, *]))) },
{ apply nat.le_add_left } },
{ refine nonnegg_cases_right (Ξ»i h, sq_le_of_le _ _ (nonnegg_pos_neg.1 ha)),
{ exact int.coe_nat_le.1 (le_of_neg_le_neg (@int.le.intro _ _ w (by simp *))) },
{ apply nat.le_add_right } },
{ simpa [add_comm] using
nonnegg_pos_neg.2 (sq_le_add (nonnegg_pos_neg.1 ha) (nonnegg_pos_neg.1 hb)) },
{ exact nonneg_add_lem ha hb },
{ refine nonnegg_cases_left (Ξ»i h, sq_le_of_le _ _ (nonnegg_neg_pos.1 ha)),
{ exact int.coe_nat_le.1 (le_of_neg_le_neg (int.le.intro h)) },
{ apply nat.le_add_right } },
{ dsimp, rw [add_comm, add_comm βy], exact nonneg_add_lem hb ha },
{ simpa [add_comm] using
nonnegg_neg_pos.2 (sq_le_add (nonnegg_neg_pos.1 ha) (nonnegg_neg_pos.1 hb)) },
end
theorem nonneg_iff_zero_le {a : β€βd} : nonneg a β 0 β€ a := show _ β nonneg _, by simp
theorem le_of_le_le {x y z w : β€} (xz : x β€ z) (yw : y β€ w) : (β¨x, yβ© : β€βd) β€ β¨z, wβ© :=
show nonneg β¨z - x, w - yβ©, from
match z - x, w - y, int.le.dest_sub xz, int.le.dest_sub yw with ._, ._, β¨a, rflβ©, β¨b, rflβ© :=
trivial end
protected theorem nonneg_total : Ξ (a : β€βd), nonneg a β¨ nonneg (-a)
| β¨(x : β), (y : β)β© := or.inl trivial
| β¨-[1+ x], -[1+ y]β© := or.inr trivial
| β¨0, -[1+ y]β© := or.inr trivial
| β¨-[1+ x], 0β© := or.inr trivial
| β¨(x+1:β), -[1+ y]β© := nat.le_total
| β¨-[1+ x], (y+1:β)β© := nat.le_total
protected theorem le_total (a b : β€βd) : a β€ b β¨ b β€ a :=
begin
have t := (b - a).nonneg_total,
rwa neg_sub at t,
end
instance : preorder β€βd :=
{ le := (β€),
le_refl := Ξ» a, show nonneg (a - a), by simp only [sub_self],
le_trans := Ξ» a b c hab hbc, by simpa [sub_add_sub_cancel'] using hab.add hbc,
lt := (<),
lt_iff_le_not_le := Ξ» a b,
(and_iff_right_of_imp (zsqrtd.le_total _ _).resolve_left).symm }
theorem le_arch (a : β€βd) : βn : β, a β€ n :=
let β¨x, y, (h : a β€ β¨x, yβ©)β© := show βx y : β, nonneg (β¨x, yβ© + -a), from match -a with
| β¨int.of_nat x, int.of_nat yβ© := β¨0, 0, trivialβ©
| β¨int.of_nat x, -[1+ y]β© := β¨0, y+1, by simp [int.neg_succ_of_nat_coe, add_assoc]β©
| β¨-[1+ x], int.of_nat yβ© := β¨x+1, 0, by simp [int.neg_succ_of_nat_coe, add_assoc]β©
| β¨-[1+ x], -[1+ y]β© := β¨x+1, y+1, by simp [int.neg_succ_of_nat_coe, add_assoc]β©
end in begin
refine β¨x + d*y, h.trans _β©,
rw [β int.cast_coe_nat, β of_int_eq_coe],
change nonneg β¨(βx + d*y) - βx, 0-βyβ©,
cases y with y,
{ simp },
have h : βy, sq_le y d (d * y) 1 := Ξ» y,
by simpa [sq_le, mul_comm, mul_left_comm] using
nat.mul_le_mul_right (y * y) (nat.le_mul_self d),
rw [show (x:β€) + d * nat.succ y - x = d * nat.succ y, by simp],
exact h (y+1)
end
protected theorem add_le_add_left (a b : β€βd) (ab : a β€ b) (c : β€βd) : c + a β€ c + b :=
show nonneg _, by rw add_sub_add_left_eq_sub; exact ab
protected theorem le_of_add_le_add_left (a b c : β€βd) (h : c + a β€ c + b) : a β€ b :=
by simpa using zsqrtd.add_le_add_left _ _ h (-c)
protected theorem add_lt_add_left (a b : β€βd) (h : a < b) (c) : c + a < c + b :=
Ξ» h', h (zsqrtd.le_of_add_le_add_left _ _ _ h')
theorem nonneg_smul {a : β€βd} {n : β} (ha : nonneg a) : nonneg (n * a) :=
by rw β int.cast_coe_nat; exact match a, nonneg_cases ha, ha with
| ._, β¨x, y, or.inl rflβ©, ha := by rw smul_val; trivial
| ._, β¨x, y, or.inr $ or.inl rflβ©, ha := by rw smul_val; simpa using
nonnegg_pos_neg.2 (sq_le_smul n $ nonnegg_pos_neg.1 ha)
| ._, β¨x, y, or.inr $ or.inr rflβ©, ha := by rw smul_val; simpa using
nonnegg_neg_pos.2 (sq_le_smul n $ nonnegg_neg_pos.1 ha)
end
theorem nonneg_muld {a : β€βd} (ha : nonneg a) : nonneg (sqrtd * a) :=
by refine match a, nonneg_cases ha, ha with
| ._, β¨x, y, or.inl rflβ©, ha := trivial
| ._, β¨x, y, or.inr $ or.inl rflβ©, ha := by simp; apply nonnegg_neg_pos.2;
simpa [sq_le, mul_comm, mul_left_comm] using
nat.mul_le_mul_left d (nonnegg_pos_neg.1 ha)
| ._, β¨x, y, or.inr $ or.inr rflβ©, ha := by simp; apply nonnegg_pos_neg.2;
simpa [sq_le, mul_comm, mul_left_comm] using
nat.mul_le_mul_left d (nonnegg_neg_pos.1 ha)
end
theorem nonneg_mul_lem {x y : β} {a : β€βd} (ha : nonneg a) : nonneg (β¨x, yβ© * a) :=
have (β¨x, yβ© * a : β€βd) = x * a + sqrtd * (y * a), by rw [decompose, right_distrib, mul_assoc];
refl,
by rw this; exact (nonneg_smul ha).add (nonneg_muld $ nonneg_smul ha)
theorem nonneg_mul {a b : β€βd} (ha : nonneg a) (hb : nonneg b) : nonneg (a * b) :=
match a, b, nonneg_cases ha, nonneg_cases hb, ha, hb with
| ._, ._, β¨x, y, or.inl rflβ©, β¨z, w, or.inl rflβ©, ha, hb := trivial
| ._, ._, β¨x, y, or.inl rflβ©, β¨z, w, or.inr $ or.inr rflβ©, ha, hb := nonneg_mul_lem hb
| ._, ._, β¨x, y, or.inl rflβ©, β¨z, w, or.inr $ or.inl rflβ©, ha, hb := nonneg_mul_lem hb
| ._, ._, β¨x, y, or.inr $ or.inr rflβ©, β¨z, w, or.inl rflβ©, ha, hb :=
by rw mul_comm; exact nonneg_mul_lem ha
| ._, ._, β¨x, y, or.inr $ or.inl rflβ©, β¨z, w, or.inl rflβ©, ha, hb :=
by rw mul_comm; exact nonneg_mul_lem ha
| ._, ._, β¨x, y, or.inr $ or.inr rflβ©, β¨z, w, or.inr $ or.inr rflβ©, ha, hb :=
by rw [calc (β¨-x, yβ© * β¨-z, wβ© : β€βd) = β¨_, _β© : rfl
... = β¨x * z + d * y * w, -(x * w + y * z)β© : by simp [add_comm]]; exact
nonnegg_pos_neg.2 (sq_le_mul.left (nonnegg_neg_pos.1 ha) (nonnegg_neg_pos.1 hb))
| ._, ._, β¨x, y, or.inr $ or.inr rflβ©, β¨z, w, or.inr $ or.inl rflβ©, ha, hb :=
by rw [calc (β¨-x, yβ© * β¨z, -wβ© : β€βd) = β¨_, _β© : rfl
... = β¨-(x * z + d * y * w), x * w + y * zβ© : by simp [add_comm]]; exact
nonnegg_neg_pos.2 (sq_le_mul.right.left (nonnegg_neg_pos.1 ha) (nonnegg_pos_neg.1 hb))
| ._, ._, β¨x, y, or.inr $ or.inl rflβ©, β¨z, w, or.inr $ or.inr rflβ©, ha, hb :=
by rw [calc (β¨x, -yβ© * β¨-z, wβ© : β€βd) = β¨_, _β© : rfl
... = β¨-(x * z + d * y * w), x * w + y * zβ© : by simp [add_comm]]; exact
nonnegg_neg_pos.2 (sq_le_mul.right.right.left (nonnegg_pos_neg.1 ha) (nonnegg_neg_pos.1 hb))
| ._, ._, β¨x, y, or.inr $ or.inl rflβ©, β¨z, w, or.inr $ or.inl rflβ©, ha, hb :=
by rw [calc (β¨x, -yβ© * β¨z, -wβ© : β€βd) = β¨_, _β© : rfl
... = β¨x * z + d * y * w, -(x * w + y * z)β© : by simp [add_comm]]; exact
nonnegg_pos_neg.2 (sq_le_mul.right.right.right (nonnegg_pos_neg.1 ha) (nonnegg_pos_neg.1 hb))
end
protected theorem mul_nonneg (a b : β€βd) : 0 β€ a β 0 β€ b β 0 β€ a * b :=
by repeat {rw β nonneg_iff_zero_le}; exact nonneg_mul
theorem not_sq_le_succ (c d y) (h : 0 < c) : Β¬sq_le (y + 1) c 0 d :=
not_le_of_gt $ mul_pos (mul_pos h $ nat.succ_pos _) $ nat.succ_pos _
/-- A nonsquare is a natural number that is not equal to the square of an
integer. This is implemented as a typeclass because it's a necessary condition
for much of the Pell equation theory. -/
class nonsquare (x : β) : Prop := (ns [] : βn : β, x β n*n)
parameter [dnsq : nonsquare d]
include dnsq
theorem d_pos : 0 < d := lt_of_le_of_ne (nat.zero_le _) $ ne.symm $ (nonsquare.ns d 0)
theorem divides_sq_eq_zero {x y} (h : x * x = d * y * y) : x = 0 β§ y = 0 :=
let g := x.gcd y in or.elim g.eq_zero_or_pos
(Ξ»H, β¨nat.eq_zero_of_gcd_eq_zero_left H, nat.eq_zero_of_gcd_eq_zero_right Hβ©)
(Ξ»gpos, false.elim $
let β¨m, n, co, (hx : x = m * g), (hy : y = n * g)β© := nat.exists_coprime gpos in
begin
rw [hx, hy] at h,
have : m * m = d * (n * n) := nat.eq_of_mul_eq_mul_left (mul_pos gpos gpos)
(by simpa [mul_comm, mul_left_comm] using h),
have co2 := let co1 := co.mul_right co in co1.mul co1,
exact nonsquare.ns d m (nat.dvd_antisymm (by rw this; apply dvd_mul_right) $
co2.dvd_of_dvd_mul_right $ by simp [this])
end)
theorem divides_sq_eq_zero_z {x y : β€} (h : x * x = d * y * y) : x = 0 β§ y = 0 :=
by rw [mul_assoc, β int.nat_abs_mul_self, β int.nat_abs_mul_self, β int.coe_nat_mul, β mul_assoc]
at h;
exact let β¨h1, h2β© := divides_sq_eq_zero (int.coe_nat_inj h) in
β¨int.eq_zero_of_nat_abs_eq_zero h1, int.eq_zero_of_nat_abs_eq_zero h2β©
theorem not_divides_sq (x y) : (x + 1) * (x + 1) β d * (y + 1) * (y + 1) :=
Ξ»e, by have t := (divides_sq_eq_zero e).left; contradiction
theorem nonneg_antisymm : Ξ {a : β€βd}, nonneg a β nonneg (-a) β a = 0
| β¨0, 0β© xy yx := rfl
| β¨-[1+ x], -[1+ y]β© xy yx := false.elim xy
| β¨(x+1:nat), (y+1:nat)β© xy yx := false.elim yx
| β¨-[1+ x], 0β© xy yx := absurd xy (not_sq_le_succ _ _ _ dec_trivial)
| β¨(x+1:nat), 0β© xy yx := absurd yx (not_sq_le_succ _ _ _ dec_trivial)
| β¨0, -[1+ y]β© xy yx := absurd xy (not_sq_le_succ _ _ _ d_pos)
| β¨0, (y+1:nat)β© _ yx := absurd yx (not_sq_le_succ _ _ _ d_pos)
| β¨(x+1:nat), -[1+ y]β© (xy : sq_le _ _ _ _) (yx : sq_le _ _ _ _) :=
let t := le_antisymm yx xy in by rw[one_mul] at t; exact absurd t (not_divides_sq _ _)
| β¨-[1+ x], (y+1:nat)β© (xy : sq_le _ _ _ _) (yx : sq_le _ _ _ _) :=
let t := le_antisymm xy yx in by rw[one_mul] at t; exact absurd t (not_divides_sq _ _)
theorem le_antisymm {a b : β€βd} (ab : a β€ b) (ba : b β€ a) : a = b :=
eq_of_sub_eq_zero $ nonneg_antisymm ba (by rw neg_sub; exact ab)
instance : linear_order β€βd :=
{ le_antisymm := @zsqrtd.le_antisymm,
le_total := zsqrtd.le_total,
decidable_le := zsqrtd.decidable_le,
..zsqrtd.preorder }
protected theorem eq_zero_or_eq_zero_of_mul_eq_zero : Ξ {a b : β€βd}, a * b = 0 β a = 0 β¨ b = 0
| β¨x, yβ© β¨z, wβ© h := by injection h with h1 h2; exact
have h1 : x*z = -(d*y*w), from eq_neg_of_add_eq_zero_left h1,
have h2 : x*w = -(y*z), from eq_neg_of_add_eq_zero_left h2,
have fin : x*x = d*y*y β (β¨x, yβ©:β€βd) = 0, from
Ξ»e, match x, y, divides_sq_eq_zero_z e with ._, ._, β¨rfl, rflβ© := rfl end,
if z0 : z = 0 then if w0 : w = 0 then
or.inr (match z, w, z0, w0 with ._, ._, rfl, rfl := rfl end)
else
or.inl $ fin $ mul_right_cancelβ w0 $ calc
x * x * w = -y * (x * z) : by simp [h2, mul_assoc, mul_left_comm]
... = d * y * y * w : by simp [h1, mul_assoc, mul_left_comm]
else
or.inl $ fin $ mul_right_cancelβ z0 $ calc
x * x * z = d * -y * (x * w) : by simp [h1, mul_assoc, mul_left_comm]
... = d * y * y * z : by simp [h2, mul_assoc, mul_left_comm]
instance : is_domain β€βd :=
{ eq_zero_or_eq_zero_of_mul_eq_zero := @zsqrtd.eq_zero_or_eq_zero_of_mul_eq_zero,
.. zsqrtd.comm_ring, .. zsqrtd.nontrivial }
protected theorem mul_pos (a b : β€βd) (a0 : 0 < a) (b0 : 0 < b) : 0 < a * b := Ξ»ab,
or.elim (eq_zero_or_eq_zero_of_mul_eq_zero
(le_antisymm ab (mul_nonneg _ _ (le_of_lt a0) (le_of_lt b0))))
(Ξ»e, ne_of_gt a0 e)
(Ξ»e, ne_of_gt b0 e)
instance : linear_ordered_comm_ring β€βd :=
{ add_le_add_left := @zsqrtd.add_le_add_left,
mul_pos := @zsqrtd.mul_pos,
zero_le_one := dec_trivial,
.. zsqrtd.comm_ring, .. zsqrtd.linear_order, .. zsqrtd.nontrivial }
instance : linear_ordered_ring β€βd := by apply_instance
instance : ordered_ring β€βd := by apply_instance
end
lemma norm_eq_zero {d : β€} (h_nonsquare : β n : β€, d β n*n) (a : β€βd) :
norm a = 0 β a = 0 :=
begin
refine β¨Ξ» ha, ext.mpr _, Ξ» h, by rw [h, norm_zero]β©,
delta norm at ha,
rw sub_eq_zero at ha,
by_cases h : 0 β€ d,
{ obtain β¨d', rflβ© := int.eq_coe_of_zero_le h,
haveI : nonsquare d' := β¨Ξ» n h, h_nonsquare n $ by exact_mod_cast hβ©,
exact divides_sq_eq_zero_z ha, },
{ push_neg at h,
suffices : a.re * a.re = 0,
{ rw eq_zero_of_mul_self_eq_zero this at ha β’,
simpa only [true_and, or_self_right, zero_re, zero_im, eq_self_iff_true,
zero_eq_mul, mul_zero, mul_eq_zero, h.ne, false_or, or_self] using ha },
apply _root_.le_antisymm _ (mul_self_nonneg _),
rw [ha, mul_assoc],
exact mul_nonpos_of_nonpos_of_nonneg h.le (mul_self_nonneg _) }
end
variables {R : Type}
@[ext] lemma hom_ext [ring R] {d : β€} (f g : β€βd β+* R) (h : f sqrtd = g sqrtd) : f = g :=
begin
ext β¨x_re, x_imβ©,
simp [decompose, h],
end
variables [comm_ring R]
/-- The unique `ring_hom` from `β€βd` to a ring `R`, constructed by replacing `βd` with the provided
root. Conversely, this associates to every mapping `β€βd β+* R` a value of `βd` in `R`. -/
@[simps]
def lift {d : β€} : {r : R // r * r = βd} β (β€βd β+* R) :=
{ to_fun := Ξ» r,
{ to_fun := Ξ» a, a.1 + a.2*(r : R),
map_zero' := by simp,
map_add' := Ξ» a b, by { simp, ring, },
map_one' := by simp,
map_mul' := Ξ» a b, by
{ have : (a.re + a.im * r : R) * (b.re + b.im * r) =
a.re * b.re + (a.re * b.im + a.im * b.re) * r + a.im * b.im * (r * r) := by ring,
simp [this, r.prop],
ring, } },
inv_fun := Ξ» f, β¨f sqrtd, by rw [βf.map_mul, dmuld, ring_hom.map_int_cast]β©,
left_inv := Ξ» r, by { ext, simp },
right_inv := Ξ» f, by { ext, simp } }
/-- `lift r` is injective if `d` is non-square, and R has characteristic zero (that is, the map from
`β€` into `R` is injective). -/
lemma lift_injective [char_zero R] {d : β€} (r : {r : R // r * r = βd}) (hd : β n : β€, d β n*n) :
function.injective (lift r) :=
(injective_iff_map_eq_zero (lift r)).mpr $ Ξ» a ha,
begin
have h_inj : function.injective (coe : β€ β R) := int.cast_injective,
suffices : lift r a.norm = 0,
{ simp only [coe_int_re, add_zero, lift_apply_apply, coe_int_im, int.cast_zero, zero_mul] at this,
rwa [β int.cast_zero, h_inj.eq_iff, norm_eq_zero hd] at this },
rw [norm_eq_mul_conj, ring_hom.map_mul, ha, zero_mul]
end
end zsqrtd
|
ec6fc8fa1f5b93fae2a796e2d3c386dca1463431 | bb31430994044506fa42fd667e2d556327e18dfe | /src/ring_theory/is_adjoin_root.lean | a9d74b7173734c8e96776d0e5fdfd3fb5ba0851b | [
"Apache-2.0"
] | permissive | sgouezel/mathlib | 0cb4e5335a2ba189fa7af96d83a377f83270e503 | 00638177efd1b2534fc5269363ebf42a7871df9a | refs/heads/master | 1,674,527,483,042 | 1,673,665,568,000 | 1,673,665,568,000 | 119,598,202 | 0 | 0 | null | 1,517,348,647,000 | 1,517,348,646,000 | null | UTF-8 | Lean | false | false | 26,087 | lean | /-
Copyright (c) 2022 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Anne Baanen
-/
import data.polynomial.algebra_map
import field_theory.minpoly.gcd_monoid
import ring_theory.power_basis
/-!
# A predicate on adjoining roots of polynomial
This file defines a predicate `is_adjoin_root S f`, which states that the ring `S` can be
constructed by adjoining a specified root of the polynomial `f : R[X]` to `R`.
This predicate is useful when the same ring can be generated by adjoining the root of different
polynomials, and you want to vary which polynomial you're considering.
The results in this file are intended to mirror those in `ring_theory.adjoin_root`,
in order to provide an easier way to translate results from one to the other.
## Motivation
`adjoin_root` presents one construction of a ring `R[Ξ±]`. However, it is possible to obtain
rings of this form in many ways, such as `number_field.ring_of_integers β(β-5)`,
or `algebra.adjoin R {Ξ±, Ξ±^2}`, or `intermediate_field.adjoin R {Ξ±, 2 - Ξ±}`,
or even if we want to view `β` as adjoining a root of `X^2 + 1` to `β`.
## Main definitions
The two main predicates in this file are:
* `is_adjoin_root S f`: `S` is generated by adjoining a specified root of `f : R[X]` to `R`
* `is_adjoin_root_monic S f`: `S` is generated by adjoining a root of the monic polynomial
`f : R[X]` to `R`
Using `is_adjoin_root` to map into `S`:
* `is_adjoin_root.map`: inclusion from `R[X]` to `S`
* `is_adjoin_root.root`: the specific root adjoined to `R` to give `S`
Using `is_adjoin_root` to map out of `S`:
* `is_adjoin_root.repr`: choose a non-unique representative in `R[X]`
* `is_adjoin_root.lift`, `is_adjoin_root.lift_hom`: lift a morphism `R β+* T` to `S β+* T`
* `is_adjoin_root_monic.mod_by_monic_hom`: a unique representative in `R[X]` if `f` is monic
## Main results
* `adjoin_root.is_adjoin_root` and `adjoin_root.is_adjoin_root_monic`:
`adjoin_root` satisfies the conditions on `is_adjoin_root`(`_monic`)
* `is_adjoin_root_monic.power_basis`: the `root` generates a power basis on `S` over `R`
* `is_adjoin_root.aequiv`: algebra isomorphism showing adjoining a root gives a unique ring
up to isomorphism
* `is_adjoin_root.of_equiv`: transfer `is_adjoin_root` across an algebra isomorphism
* `is_adjoin_root_eq.minpoly_eq`: the minimal polynomial of the adjoined root of `f` is equal to
`f`, if `f` is irreducible and monic, and `R` is a GCD domain
-/
open_locale polynomial
open polynomial
noncomputable theory
universes u v
section move_me
end move_me
/-- `is_adjoin_root S f` states that the ring `S` can be constructed by adjoining a specified root
of the polynomial `f : R[X]` to `R`.
Compare `power_basis R S`, which does not explicitly specify which polynomial we adjoin a root of
(in particular `f` does not need to be the minimal polynomial of the root we adjoin),
and `adjoin_root` which constructs a new type.
This is not a typeclass because the choice of root given `S` and `f` is not unique.
-/
@[nolint has_nonempty_instance] -- This class doesn't really make sense on a predicate
structure is_adjoin_root {R : Type u} (S : Type v) [comm_semiring R] [semiring S] [algebra R S]
(f : R[X]) : Type (max u v) :=
(map : R[X] β+* S)
(map_surjective : function.surjective map)
(ker_map : ring_hom.ker map = ideal.span {f})
(algebra_map_eq : algebra_map R S = map.comp polynomial.C)
/-- `is_adjoin_root_monic S f` states that the ring `S` can be constructed by adjoining a specified
root of the monic polynomial `f : R[X]` to `R`.
As long as `f` is monic, there is a well-defined representation of elements of `S` as polynomials
in `R[X]` of degree lower than `deg f` (see `mod_by_monic_hom` and `coeff`). In particular,
we have `is_adjoin_root_monic.power_basis`.
Bundling `monic` into this structure is very useful when working with explicit `f`s such as
`X^2 - C a * X - C b` since it saves you carrying around the proofs of monicity.
-/
@[nolint has_nonempty_instance] -- This class doesn't really make sense on a predicate
structure is_adjoin_root_monic {R : Type u} (S : Type v) [comm_semiring R] [semiring S]
[algebra R S] (f : R[X]) extends is_adjoin_root S f :=
(monic : monic f)
section ring
variables {R : Type u} {S : Type v} [comm_ring R] [ring S] {f : R[X]} [algebra R S]
namespace is_adjoin_root
/-- `(h : is_adjoin_root S f).root` is the root of `f` that can be adjoined to generate `S`. -/
def root (h : is_adjoin_root S f) : S := h.map X
lemma subsingleton (h : is_adjoin_root S f) [subsingleton R] : subsingleton S :=
h.map_surjective.subsingleton
lemma algebra_map_apply (h : is_adjoin_root S f) (x : R) :
algebra_map R S x = h.map (polynomial.C x) :=
by rw [h.algebra_map_eq, ring_hom.comp_apply]
@[simp] lemma mem_ker_map (h : is_adjoin_root S f) {p} : p β ring_hom.ker h.map β f β£ p :=
by rw [h.ker_map, ideal.mem_span_singleton]
lemma map_eq_zero_iff (h : is_adjoin_root S f) {p} : h.map p = 0 β f β£ p :=
by rw [β h.mem_ker_map, ring_hom.mem_ker]
@[simp] lemma map_X (h : is_adjoin_root S f) : h.map X = h.root := rfl
@[simp] lemma map_self (h : is_adjoin_root S f) : h.map f = 0 :=
h.map_eq_zero_iff.mpr dvd_rfl
@[simp] lemma aeval_eq (h : is_adjoin_root S f) (p : R[X]) : aeval h.root p = h.map p :=
polynomial.induction_on p (Ξ» x, by { rw [aeval_C, h.algebra_map_apply] })
(Ξ» p q ihp ihq, by rw [alg_hom.map_add, ring_hom.map_add, ihp, ihq])
(Ξ» n x ih, by { rw [alg_hom.map_mul, aeval_C, alg_hom.map_pow, aeval_X, ring_hom.map_mul,
β h.algebra_map_apply, ring_hom.map_pow, map_X] })
@[simp] lemma aeval_root (h : is_adjoin_root S f) : aeval h.root f = 0 :=
by rw [aeval_eq, map_self]
/-- Choose an arbitrary representative so that `h.map (h.repr x) = x`.
If `f` is monic, use `is_adjoin_root_monic.mod_by_monic_hom` for a unique choice of representative.
-/
def repr (h : is_adjoin_root S f) (x : S) : R[X] :=
(h.map_surjective x).some
lemma map_repr (h : is_adjoin_root S f) (x : S) : h.map (h.repr x) = x :=
(h.map_surjective x).some_spec
/-- `repr` preserves zero, up to multiples of `f` -/
lemma repr_zero_mem_span (h : is_adjoin_root S f) : h.repr 0 β ideal.span ({f} : set R[X]) :=
by rw [β h.ker_map, ring_hom.mem_ker, h.map_repr]
/-- `repr` preserves addition, up to multiples of `f` -/
lemma repr_add_sub_repr_add_repr_mem_span (h : is_adjoin_root S f) (x y : S) :
h.repr (x + y) - (h.repr x + h.repr y) β ideal.span ({f} : set R[X]) :=
by rw [β h.ker_map, ring_hom.mem_ker, map_sub, h.map_repr, map_add, h.map_repr, h.map_repr,
sub_self]
/-- Extensionality of the `is_adjoin_root` structure itself. See `is_adjoin_root_monic.ext_elem`
for extensionality of the ring elements. -/
lemma ext_map (h h' : is_adjoin_root S f) (eq : β x, h.map x = h'.map x) : h = h' :=
begin
cases h, cases h', congr,
exact ring_hom.ext eq
end
/-- Extensionality of the `is_adjoin_root` structure itself. See `is_adjoin_root_monic.ext_elem`
for extensionality of the ring elements. -/
@[ext]
lemma ext (h h' : is_adjoin_root S f) (eq : h.root = h'.root) : h = h' :=
h.ext_map h' (Ξ» x, by rw [β h.aeval_eq, β h'.aeval_eq, eq])
section lift
variables {T : Type*} [comm_ring T] {i : R β+* T} {x : T} (hx : f.evalβ i x = 0)
include hx
/-- Auxiliary lemma for `is_adjoin_root.lift` -/
lemma evalβ_repr_eq_evalβ_of_map_eq (h : is_adjoin_root S f) (z : S) (w : R[X])
(hzw : h.map w = z) :
(h.repr z).evalβ i x = w.evalβ i x :=
begin
rw [eq_comm, β sub_eq_zero, β h.map_repr z, β map_sub, h.map_eq_zero_iff] at hzw,
obtain β¨y, hyβ© := hzw,
rw [β sub_eq_zero, β evalβ_sub, hy, evalβ_mul, hx, zero_mul]
end
variables (i x) -- To match `adjoin_root.lift`
/-- Lift a ring homomorphism `R β+* T` to `S β+* T` by specifying a root `x` of `f` in `T`,
where `S` is given by adjoining a root of `f` to `R`. -/
def lift (h : is_adjoin_root S f) : S β+* T :=
{ to_fun := Ξ» z, (h.repr z).evalβ i x,
map_zero' := by rw [h.evalβ_repr_eq_evalβ_of_map_eq hx _ _ (map_zero _), evalβ_zero],
map_add' := Ξ» z w, begin
rw [h.evalβ_repr_eq_evalβ_of_map_eq hx _ (h.repr z + h.repr w), evalβ_add],
{ rw [map_add, map_repr, map_repr] }
end,
map_one' := by rw [h.evalβ_repr_eq_evalβ_of_map_eq hx _ _ (map_one _), evalβ_one],
map_mul' := Ξ» z w, begin
rw [h.evalβ_repr_eq_evalβ_of_map_eq hx _ (h.repr z * h.repr w), evalβ_mul],
{ rw [map_mul, map_repr, map_repr] }
end }
variables {i x}
@[simp] lemma lift_map (h : is_adjoin_root S f) (z : R[X]) :
h.lift i x hx (h.map z) = z.evalβ i x :=
by rw [lift, ring_hom.coe_mk, h.evalβ_repr_eq_evalβ_of_map_eq hx _ _ rfl]
@[simp] lemma lift_root (h : is_adjoin_root S f) :
h.lift i x hx h.root = x :=
by rw [β h.map_X, lift_map, evalβ_X]
@[simp] lemma lift_algebra_map (h : is_adjoin_root S f) (a : R) :
h.lift i x hx (algebra_map R S a) = i a :=
by rw [h.algebra_map_apply, lift_map, evalβ_C]
/-- Auxiliary lemma for `apply_eq_lift` -/
lemma apply_eq_lift (h : is_adjoin_root S f) (g : S β+* T)
(hmap : β a, g (algebra_map R S a) = i a) (hroot : g h.root = x) (a : S):
g a = h.lift i x hx a :=
begin
rw [β h.map_repr a, polynomial.as_sum_range_C_mul_X_pow (h.repr a)],
simp only [map_sum, map_mul, map_pow, h.map_X, hroot, β h.algebra_map_apply, hmap, lift_root,
lift_algebra_map]
end
/-- Unicity of `lift`: a map that agrees on `R` and `h.root` agrees with `lift` everywhere. -/
lemma eq_lift (h : is_adjoin_root S f) (g : S β+* T)
(hmap : β a, g (algebra_map R S a) = i a) (hroot : g h.root = x) :
g = h.lift i x hx :=
ring_hom.ext (h.apply_eq_lift hx g hmap hroot)
variables [algebra R T] (hx' : aeval x f = 0)
omit hx
variables (x) -- To match `adjoin_root.lift_hom`
/-- Lift the algebra map `R β T` to `S ββ[R] T` by specifying a root `x` of `f` in `T`,
where `S` is given by adjoining a root of `f` to `R`. -/
def lift_hom (h : is_adjoin_root S f) : S ββ[R] T :=
{ commutes' := Ξ» a, h.lift_algebra_map hx' a,
.. h.lift (algebra_map R T) x hx' }
variables {x}
@[simp] lemma coe_lift_hom (h : is_adjoin_root S f) :
(h.lift_hom x hx' : S β+* T) = h.lift (algebra_map R T) x hx' := rfl
lemma lift_algebra_map_apply (h : is_adjoin_root S f) (z : S) :
h.lift (algebra_map R T) x hx' z = h.lift_hom x hx' z := rfl
@[simp] lemma lift_hom_map (h : is_adjoin_root S f) (z : R[X]) :
h.lift_hom x hx' (h.map z) = aeval x z :=
by rw [β lift_algebra_map_apply, lift_map, aeval_def]
@[simp] lemma lift_hom_root (h : is_adjoin_root S f) :
h.lift_hom x hx' h.root = x :=
by rw [β lift_algebra_map_apply, lift_root]
/-- Unicity of `lift_hom`: a map that agrees on `h.root` agrees with `lift_hom` everywhere. -/
lemma eq_lift_hom (h : is_adjoin_root S f) (g : S ββ[R] T) (hroot : g h.root = x) :
g = h.lift_hom x hx' :=
alg_hom.ext (h.apply_eq_lift hx' g g.commutes hroot)
end lift
end is_adjoin_root
namespace adjoin_root
variables (f)
/-- `adjoin_root f` is indeed given by adjoining a root of `f`. -/
protected def is_adjoin_root : is_adjoin_root (adjoin_root f) f :=
{ map := adjoin_root.mk f,
map_surjective := ideal.quotient.mk_surjective,
ker_map := begin
ext,
rw [ring_hom.mem_ker, β @adjoin_root.mk_self _ _ f, adjoin_root.mk_eq_mk,
ideal.mem_span_singleton, β dvd_add_left (dvd_refl f), sub_add_cancel]
end,
algebra_map_eq := adjoin_root.algebra_map_eq f }
/-- `adjoin_root f` is indeed given by adjoining a root of `f`. If `f` is monic this is more
powerful than `adjoin_root.is_adjoin_root`. -/
protected def is_adjoin_root_monic (hf : monic f) :
is_adjoin_root_monic (adjoin_root f) f :=
{ monic := hf,
.. adjoin_root.is_adjoin_root f }
end adjoin_root
namespace is_adjoin_root_monic
open is_adjoin_root
lemma map_mod_by_monic (h : is_adjoin_root_monic S f) (g : R[X]) :
h.map (g %β f) = h.map g :=
begin
rw [β ring_hom.sub_mem_ker_iff, mem_ker_map, mod_by_monic_eq_sub_mul_div _ h.monic,
sub_right_comm, sub_self, zero_sub, dvd_neg],
exact β¨_, rflβ©
end
lemma mod_by_monic_repr_map (h : is_adjoin_root_monic S f) (g : R[X]) :
h.repr (h.map g) %β f = g %β f :=
mod_by_monic_eq_of_dvd_sub h.monic $ by rw [β h.mem_ker_map, ring_hom.sub_mem_ker_iff, map_repr]
/-- `is_adjoin_root.mod_by_monic_hom` sends the equivalence class of `f` mod `g` to `f %β g`. -/
def mod_by_monic_hom (h : is_adjoin_root_monic S f) : S ββ[R] R[X] :=
{ to_fun := Ξ» x, h.repr x %β f,
map_add' := Ξ» x y,
by conv_lhs { rw [β h.map_repr x, β h.map_repr y, β map_add, h.mod_by_monic_repr_map,
add_mod_by_monic] },
map_smul' := Ξ» c x,
by rw [ring_hom.id_apply, β h.map_repr x, algebra.smul_def, h.algebra_map_apply, β map_mul,
h.mod_by_monic_repr_map, β smul_eq_C_mul, smul_mod_by_monic, h.map_repr] }
@[simp] lemma mod_by_monic_hom_map (h : is_adjoin_root_monic S f) (g : R[X]) :
h.mod_by_monic_hom (h.map g) = g %β f :=
h.mod_by_monic_repr_map g
@[simp] lemma map_mod_by_monic_hom (h : is_adjoin_root_monic S f) (x : S) :
h.map (h.mod_by_monic_hom x) = x :=
by rw [mod_by_monic_hom, linear_map.coe_mk, map_mod_by_monic, map_repr]
@[simp] lemma mod_by_monic_hom_root_pow (h : is_adjoin_root_monic S f) {n : β}
(hdeg : n < nat_degree f) :
h.mod_by_monic_hom (h.root ^ n) = X ^ n :=
begin
nontriviality R,
rwa [β h.map_X, β map_pow, mod_by_monic_hom_map, mod_by_monic_eq_self_iff h.monic, degree_X_pow],
contrapose! hdeg,
simpa [nat_degree_le_iff_degree_le] using hdeg
end
@[simp] lemma mod_by_monic_hom_root (h : is_adjoin_root_monic S f) (hdeg : 1 < nat_degree f) :
h.mod_by_monic_hom h.root = X :=
by simpa using mod_by_monic_hom_root_pow h hdeg
/-- The basis on `S` generated by powers of `h.root`.
Auxiliary definition for `is_adjoin_root_monic.power_basis`. -/
def basis (h : is_adjoin_root_monic S f) : basis (fin (nat_degree f)) R S :=
basis.of_repr
{ to_fun := Ξ» x, (h.mod_by_monic_hom x).to_finsupp.comap_domain coe (fin.coe_injective.inj_on _),
inv_fun := Ξ» g, h.map (of_finsupp (g.map_domain coe)),
left_inv := Ξ» x, begin
casesI subsingleton_or_nontrivial R,
{ haveI := h.subsingleton,
exact subsingleton.elim _ _ },
simp only,
rw [finsupp.map_domain_comap_domain, polynomial.eta, h.map_mod_by_monic_hom x],
intros i hi,
refine set.mem_range.mpr β¨β¨i, _β©, rflβ©,
contrapose! hi,
simp only [polynomial.to_finsupp_apply, not_not, finsupp.mem_support_iff, ne.def,
mod_by_monic_hom, linear_map.coe_mk, finset.mem_coe],
by_cases hx : h.to_is_adjoin_root.repr x %β f = 0,
{ simp [hx] },
refine coeff_eq_zero_of_nat_degree_lt (lt_of_lt_of_le _ hi),
rw nat_degree_lt_nat_degree_iff hx,
{ exact degree_mod_by_monic_lt _ h.monic },
end,
right_inv := Ξ» g, begin
nontriviality R,
ext i,
simp only [h.mod_by_monic_hom_map, finsupp.comap_domain_apply, polynomial.to_finsupp_apply],
rw [(polynomial.mod_by_monic_eq_self_iff h.monic).mpr, polynomial.coeff,
finsupp.map_domain_apply fin.coe_injective],
rw [degree_eq_nat_degree h.monic.ne_zero, degree_lt_iff_coeff_zero],
intros m hm,
rw [polynomial.coeff, finsupp.map_domain_notin_range],
rw [set.mem_range, not_exists],
rintro i rfl,
exact i.prop.not_le hm
end,
map_add' := Ξ» x y,
by simp only [map_add, finsupp.comap_domain_add_of_injective fin.coe_injective, to_finsupp_add],
map_smul' := Ξ» c x,
by simp only [map_smul, finsupp.comap_domain_smul_of_injective fin.coe_injective,
ring_hom.id_apply, to_finsupp_smul] }
@[simp] lemma basis_apply (h : is_adjoin_root_monic S f) (i) : h.basis i = h.root ^ (i : β) :=
basis.apply_eq_iff.mpr $
show (h.mod_by_monic_hom (h.to_is_adjoin_root.root ^ (i : β))).to_finsupp
.comap_domain coe (fin.coe_injective.inj_on _) = finsupp.single _ _,
begin
ext j,
rw [finsupp.comap_domain_apply, mod_by_monic_hom_root_pow],
{ rw [X_pow_eq_monomial, to_finsupp_monomial, finsupp.single_apply_left fin.coe_injective] },
{ exact i.is_lt },
end
lemma deg_pos [nontrivial S] (h : is_adjoin_root_monic S f) : 0 < nat_degree f :=
begin
rcases h.basis.index_nonempty with β¨β¨i, hiβ©β©,
exact (nat.zero_le _).trans_lt hi
end
lemma deg_ne_zero [nontrivial S] (h : is_adjoin_root_monic S f) : nat_degree f β 0 :=
h.deg_pos.ne'
/-- If `f` is monic, the powers of `h.root` form a basis. -/
@[simps gen dim basis]
def power_basis (h : is_adjoin_root_monic S f) : power_basis R S :=
{ gen := h.root,
dim := nat_degree f,
basis := h.basis,
basis_eq_pow := h.basis_apply }
@[simp] lemma basis_repr (h : is_adjoin_root_monic S f) (x : S) (i : fin (nat_degree f)) :
h.basis.repr x i = (h.mod_by_monic_hom x).coeff (i : β) :=
begin
change (h.mod_by_monic_hom x).to_finsupp.comap_domain coe (fin.coe_injective.inj_on _) i = _,
rw [finsupp.comap_domain_apply, polynomial.to_finsupp_apply]
end
lemma basis_one (h : is_adjoin_root_monic S f) (hdeg : 1 < nat_degree f) :
h.basis β¨1, hdegβ© = h.root :=
by rw [h.basis_apply, fin.coe_mk, pow_one]
/-- `is_adjoin_root_monic.lift_polyβ` lifts a linear map on polynomials to a linear map on `S`. -/
@[simps]
def lift_polyβ {T : Type*} [add_comm_group T] [module R T] (h : is_adjoin_root_monic S f)
(g : R[X] ββ[R] T) : S ββ[R] T :=
g.comp h.mod_by_monic_hom
/-- `is_adjoin_root_monic.coeff h x i` is the `i`th coefficient of the representative of `x : S`.
-/
def coeff (h : is_adjoin_root_monic S f) : S ββ[R] (β β R) :=
h.lift_polyβ
{ to_fun := polynomial.coeff,
map_add' := Ξ» p q, funext (polynomial.coeff_add p q),
map_smul' := Ξ» c p, funext (polynomial.coeff_smul c p) }
lemma coeff_apply_lt (h : is_adjoin_root_monic S f) (z : S) (i : β) (hi : i < nat_degree f) :
h.coeff z i = h.basis.repr z β¨i, hiβ© :=
begin
simp only [coeff, linear_map.comp_apply, finsupp.lcoe_fun_apply, finsupp.lmap_domain_apply,
linear_equiv.coe_coe, lift_polyβ_apply, linear_map.coe_mk, h.basis_repr],
refl
end
lemma coeff_apply_coe (h : is_adjoin_root_monic S f) (z : S) (i : fin (nat_degree f)) :
h.coeff z i = h.basis.repr z i :=
h.coeff_apply_lt z i i.prop
lemma coeff_apply_le (h : is_adjoin_root_monic S f) (z : S) (i : β) (hi : nat_degree f β€ i) :
h.coeff z i = 0 :=
begin
simp only [coeff, linear_map.comp_apply, finsupp.lcoe_fun_apply, finsupp.lmap_domain_apply,
linear_equiv.coe_coe, lift_polyβ_apply, linear_map.coe_mk, h.basis_repr],
nontriviality R,
exact polynomial.coeff_eq_zero_of_degree_lt ((degree_mod_by_monic_lt _ h.monic).trans_le
(polynomial.degree_le_of_nat_degree_le hi)),
end
lemma coeff_apply (h : is_adjoin_root_monic S f) (z : S) (i : β) :
h.coeff z i = if hi : i < nat_degree f then h.basis.repr z β¨i, hiβ© else 0 :=
begin
split_ifs with hi,
{ exact h.coeff_apply_lt z i hi },
{ exact h.coeff_apply_le z i (le_of_not_lt hi) },
end
lemma coeff_root_pow (h : is_adjoin_root_monic S f) {n} (hn : n < nat_degree f) :
h.coeff (h.root ^ n) = pi.single n 1 :=
begin
ext i,
rw coeff_apply,
split_ifs with hi,
{ calc h.basis.repr (h.root ^ n) β¨i, _β©
= h.basis.repr (h.basis β¨n, hnβ©) β¨i, hiβ©
: by rw [h.basis_apply, fin.coe_mk]
... = pi.single ((β¨n, hnβ© : fin _) : β) (1 : (Ξ» _, R) _) β(β¨i, _β© : fin _) :
by rw [h.basis.repr_self, β finsupp.single_eq_pi_single,
finsupp.single_apply_left fin.coe_injective]
... = pi.single n 1 i : by rw [fin.coe_mk, fin.coe_mk] },
{ refine (pi.single_eq_of_ne _ (1 : (Ξ» _, R) _)).symm,
rintro rfl,
simpa [hi] using hn },
end
lemma coeff_one [nontrivial S] (h : is_adjoin_root_monic S f) :
h.coeff 1 = pi.single 0 1 :=
by rw [β h.coeff_root_pow h.deg_pos, pow_zero]
lemma coeff_root (h : is_adjoin_root_monic S f) (hdeg : 1 < (nat_degree f)) :
h.coeff h.root = pi.single 1 1 :=
by rw [β h.coeff_root_pow hdeg, pow_one]
lemma coeff_algebra_map [nontrivial S] (h : is_adjoin_root_monic S f) (x : R) :
h.coeff (algebra_map R S x) = pi.single 0 x :=
begin
ext i,
rw [algebra.algebra_map_eq_smul_one, map_smul, coeff_one, pi.smul_apply, smul_eq_mul],
refine (pi.apply_single (Ξ» _ y, x * y) _ 0 1 i).trans (by simp),
intros,
simp
end
lemma ext_elem (h : is_adjoin_root_monic S f) β¦x y : Sβ¦
(hxy : β i < (nat_degree f), h.coeff x i = h.coeff y i) : x = y :=
equiv_like.injective h.basis.equiv_fun $ funext $ Ξ» i,
show h.basis.equiv_fun x i = h.basis.equiv_fun y i,
by rw [basis.equiv_fun_apply, β h.coeff_apply_coe, basis.equiv_fun_apply, β h.coeff_apply_coe,
hxy i i.prop]
lemma ext_elem_iff (h : is_adjoin_root_monic S f) {x y : S} :
x = y β β i < (nat_degree f), h.coeff x i = h.coeff y i :=
β¨Ξ» hxy i hi, hxy βΈ rfl, Ξ» hxy, h.ext_elem hxyβ©
lemma coeff_injective (h : is_adjoin_root_monic S f) : function.injective h.coeff :=
Ξ» x y hxy, h.ext_elem (Ξ» i hi, hxy βΈ rfl)
lemma is_integral_root (h : is_adjoin_root_monic S f) : is_integral R h.root :=
β¨f, h.monic, h.aeval_rootβ©
end is_adjoin_root_monic
end ring
section comm_ring
variables {R : Type u} {S : Type v} [comm_ring R] [comm_ring S] [algebra R S] {f : R[X]}
namespace is_adjoin_root
section lift
@[simp] lemma lift_self_apply (h : is_adjoin_root S f) (x : S) :
h.lift (algebra_map R S) h.root h.aeval_root x = x :=
by rw [β h.map_repr x, lift_map, β aeval_def, h.aeval_eq]
lemma lift_self (h : is_adjoin_root S f) :
h.lift (algebra_map R S) h.root h.aeval_root = ring_hom.id S :=
ring_hom.ext (h.lift_self_apply)
end lift
section equiv
variables {T : Type*} [comm_ring T] [algebra R T]
/-- Adjoining a root gives a unique ring up to algebra isomorphism.
This is the converse of `is_adjoin_root.of_equiv`: this turns an `is_adjoin_root` into an
`alg_equiv`, and `is_adjoin_root.of_equiv` turns an `alg_equiv` into an `is_adjoin_root`.
-/
def aequiv (h : is_adjoin_root S f) (h' : is_adjoin_root T f) : S ββ[R] T :=
{ to_fun := h.lift_hom h'.root h'.aeval_root,
inv_fun := h'.lift_hom h.root h.aeval_root,
left_inv := Ξ» x, by rw [β h.map_repr x, lift_hom_map, aeval_eq, lift_hom_map, aeval_eq],
right_inv := Ξ» x, by rw [β h'.map_repr x, lift_hom_map, aeval_eq, lift_hom_map, aeval_eq],
.. h.lift_hom h'.root h'.aeval_root }
@[simp] lemma aequiv_map (h : is_adjoin_root S f) (h' : is_adjoin_root T f) (z : R[X]) :
h.aequiv h' (h.map z) = h'.map z :=
by rw [aequiv, alg_equiv.coe_mk, lift_hom_map, aeval_eq]
@[simp] lemma aequiv_root (h : is_adjoin_root S f) (h' : is_adjoin_root T f) :
h.aequiv h' h.root = h'.root :=
by rw [aequiv, alg_equiv.coe_mk, lift_hom_root]
@[simp] lemma aequiv_self (h : is_adjoin_root S f) : h.aequiv h = alg_equiv.refl :=
by { ext a, exact h.lift_self_apply a }
@[simp] lemma aequiv_symm (h : is_adjoin_root S f) (h' : is_adjoin_root T f) :
(h.aequiv h').symm = h'.aequiv h :=
by { ext, refl }
@[simp] lemma lift_aequiv {U : Type*} [comm_ring U]
(h : is_adjoin_root S f) (h' : is_adjoin_root T f) (i : R β+* U) (x hx z) :
(h'.lift i x hx (h.aequiv h' z)) = h.lift i x hx z :=
by rw [β h.map_repr z, aequiv_map, lift_map, lift_map]
@[simp] lemma lift_hom_aequiv {U : Type*} [comm_ring U] [algebra R U]
(h : is_adjoin_root S f) (h' : is_adjoin_root T f) (x : U) (hx z) :
(h'.lift_hom x hx (h.aequiv h' z)) = h.lift_hom x hx z :=
h.lift_aequiv h' _ _ hx _
@[simp] lemma aequiv_aequiv {U : Type*} [comm_ring U] [algebra R U]
(h : is_adjoin_root S f) (h' : is_adjoin_root T f) (h'' : is_adjoin_root U f) (x) :
(h'.aequiv h'') (h.aequiv h' x) = h.aequiv h'' x :=
h.lift_hom_aequiv _ _ h''.aeval_root _
@[simp] lemma aequiv_trans {U : Type*} [comm_ring U] [algebra R U]
(h : is_adjoin_root S f) (h' : is_adjoin_root T f) (h'' : is_adjoin_root U f) :
(h.aequiv h').trans (h'.aequiv h'') = h.aequiv h'' :=
by { ext z, exact h.aequiv_aequiv h' h'' z }
/-- Transfer `is_adjoin_root` across an algebra isomorphism.
This is the converse of `is_adjoin_root.aequiv`: this turns an `alg_equiv` into an `is_adjoin_root`,
and `is_adjoin_root.aequiv` turns an `is_adjoin_root` into an `alg_equiv`.
-/
@[simps map_apply]
def of_equiv (h : is_adjoin_root S f) (e : S ββ[R] T) : is_adjoin_root T f :=
{ map := ((e : S β+* T) : S β+* T).comp h.map,
map_surjective := e.surjective.comp h.map_surjective,
ker_map := by rw [β ring_hom.comap_ker, ring_hom.ker_coe_equiv, β ring_hom.ker_eq_comap_bot,
h.ker_map],
algebra_map_eq := by ext;
simp only [alg_equiv.commutes, ring_hom.comp_apply, alg_equiv.coe_ring_equiv,
ring_equiv.coe_to_ring_hom, β h.algebra_map_apply] }
@[simp] lemma of_equiv_root (h : is_adjoin_root S f) (e : S ββ[R] T) :
(h.of_equiv e).root = e h.root := rfl
@[simp] lemma aequiv_of_equiv {U : Type*} [comm_ring U] [algebra R U]
(h : is_adjoin_root S f) (h' : is_adjoin_root T f) (e : T ββ[R] U) :
h.aequiv (h'.of_equiv e) = (h.aequiv h').trans e :=
by ext a; rw [β h.map_repr a, aequiv_map, alg_equiv.trans_apply, aequiv_map, of_equiv_map_apply]
@[simp] lemma of_equiv_aequiv {U : Type*} [comm_ring U] [algebra R U]
(h : is_adjoin_root S f) (h' : is_adjoin_root U f) (e : S ββ[R] T) :
(h.of_equiv e).aequiv h' = e.symm.trans (h.aequiv h') :=
by ext a; rw [β (h.of_equiv e).map_repr a, aequiv_map, alg_equiv.trans_apply, of_equiv_map_apply,
e.symm_apply_apply, aequiv_map]
end equiv
end is_adjoin_root
namespace is_adjoin_root_monic
lemma minpoly_eq [is_domain R] [is_domain S] [no_zero_smul_divisors R S] [normalized_gcd_monoid R]
(h : is_adjoin_root_monic S f) (hirr : irreducible f) :
minpoly R h.root = f :=
let β¨q, hqβ© := minpoly.gcd_domain_dvd h.is_integral_root h.monic.ne_zero h.aeval_root in
symm $ eq_of_monic_of_associated h.monic (minpoly.monic h.is_integral_root) $
by convert (associated.mul_left (minpoly R h.root) $
associated_one_iff_is_unit.2 $ (hirr.is_unit_or_is_unit hq).resolve_left $
minpoly.not_is_unit R h.root);
rw mul_one
end is_adjoin_root_monic
end comm_ring
|
81e02554cc4a390a755ad01b51da3b53182071cc | 0d7f5899c0475f9e105a439896d9377f80c0d7c3 | /src/examples/monoid.lean | f8df1505848b1045ae86b7c2fdc6fe078cb7c00f | [] | no_license | adamtopaz/UnivAlg | 127038f320e68cdf3efcd0c084c9af02fdb8da3d | 2458d47a6e4fd0525e3a25b07cb7dd518ac173ef | refs/heads/master | 1,670,320,985,286 | 1,597,350,882,000 | 1,597,350,882,000 | 280,585,500 | 4 | 0 | null | 1,597,350,883,000 | 1,595,048,527,000 | Lean | UTF-8 | Lean | false | false | 926 | lean | import ..lang
import data.nat.basic
namespace langs
namespace monoid
inductive preL : β β Type
| one : preL 0
| mul : preL 2
--| const : preL 1
def L : lang := β¨preLβ©
open lang.term
inductive preR : Ξ n, L.gen n β L.gen n β Prop
| one_mul : preR 1 get $ proj (fin.cast $ show 0 + 1 = 1, by simp)
(compl (of preL.one)
(proj (fin.cast $ show 2 = 1 + 1, by simp) (of preL.mul)))
| mul_one : preR 1 get $ proj (fin.cast $ show 1 + 0 = 1, by simp)
(compr (of preL.one)
(proj (fin.cast $ show 2 = 1 + 1, by simp) (of preL.mul)))
| mul_assoc : preR 3
(proj (fin.cast $ show 2 + 1 = 3, by simp) $ compl
(of preL.mul)
(proj (fin.cast $ show 2 = 1 + 1, by simp) $ of preL.mul))
(proj (fin.cast $ show 1 + 2 = 3, by simp) $ compr
(of preL.mul)
(proj (fin.cast $ show 2 = 1 + 1, by simp) $ of preL.mul))
def R : rules L := β¨preRβ©
end monoid
end langs |
8ce24bf64d50c9220a743464f3d2043b5b221d2c | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/measure_theory/constructions/borel_space.lean | 283316dff24095e2bb9f1c77bc5c0b67e6bc0b6a | [
"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 | 85,920 | 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, Yury Kudryashov
-/
import measure_theory.function.ae_measurable_sequence
import analysis.complex.basic
import analysis.normed_space.finite_dimension
import measure_theory.group.arithmetic
import measure_theory.lattice
import measure_theory.measure.open_pos
import topology.algebra.ordered.liminf_limsup
import topology.continuous_function.basic
import topology.instances.ereal
import topology.G_delta
import topology.semicontinuous
import topology.order.lattice
/-!
# Borel (measurable) space
## Main definitions
* `borel Ξ±` : the least `Ο`-algebra that contains all open sets;
* `class borel_space` : a space with `topological_space` and `measurable_space` structures
such that `βΉmeasurable_space Ξ±βΊ = borel Ξ±`;
* `class opens_measurable_space` : a space with `topological_space` and `measurable_space`
structures such that all open sets are measurable; equivalently, `borel Ξ± β€ βΉmeasurable_space Ξ±βΊ`.
* `borel_space` instances on `empty`, `unit`, `bool`, `nat`, `int`, `rat`;
* `measurable` and `borel_space` instances on `β`, `ββ₯0`, `ββ₯0β`.
## Main statements
* `is_open.measurable_set`, `is_closed.measurable_set`: open and closed sets are measurable;
* `continuous.measurable` : a continuous function is measurable;
* `continuous.measurable2` : if `f : Ξ± β Ξ²` and `g : Ξ± β Ξ³` are measurable and `op : Ξ² Γ Ξ³ β Ξ΄`
is continuous, then `Ξ» x, op (f x, g y)` is measurable;
* `measurable.add` etc : dot notation for arithmetic operations on `measurable` predicates,
and similarly for `dist` and `edist`;
* `ae_measurable.add` : similar dot notation for almost everywhere measurable functions;
* `measurable.ennreal*` : special cases for arithmetic operations on `ββ₯0β`.
-/
noncomputable theory
open classical set filter measure_theory
open_locale classical big_operators topological_space nnreal ennreal interval
universes u v w x y
variables {Ξ± Ξ² Ξ³ Ξ³β Ξ΄ : Type*} {ΞΉ : Sort y} {s t u : set Ξ±}
open measurable_space topological_space
/-- `measurable_space` structure generated by `topological_space`. -/
def borel (Ξ± : Type u) [topological_space Ξ±] : measurable_space Ξ± :=
generate_from {s : set Ξ± | is_open s}
lemma borel_eq_top_of_discrete [topological_space Ξ±] [discrete_topology Ξ±] :
borel Ξ± = β€ :=
top_le_iff.1 $ Ξ» s hs, generate_measurable.basic s (is_open_discrete s)
lemma borel_eq_top_of_encodable [topological_space Ξ±] [t1_space Ξ±] [encodable Ξ±] :
borel Ξ± = β€ :=
begin
refine (top_le_iff.1 $ Ξ» s hs, bUnion_of_singleton s βΈ _),
apply measurable_set.bUnion s.countable_encodable,
intros x hx,
apply measurable_set.of_compl,
apply generate_measurable.basic,
exact is_closed_singleton.is_open_compl
end
lemma borel_eq_generate_from_of_subbasis {s : set (set Ξ±)}
[t : topological_space Ξ±] [second_countable_topology Ξ±] (hs : t = generate_from s) :
borel Ξ± = generate_from s :=
le_antisymm
(generate_from_le $ assume u (hu : t.is_open u),
begin
rw [hs] at hu,
induction hu,
case generate_open.basic : u hu
{ exact generate_measurable.basic u hu },
case generate_open.univ
{ exact @measurable_set.univ Ξ± (generate_from s) },
case generate_open.inter : sβ sβ _ _ hsβ hsβ
{ exact @measurable_set.inter Ξ± (generate_from s) _ _ hsβ hsβ },
case generate_open.sUnion : f hf ih
{ rcases is_open_sUnion_countable f (by rwa hs) with β¨v, hv, vf, vuβ©,
rw β vu,
exact @measurable_set.sUnion Ξ± (generate_from s) _ hv
(Ξ» x xv, ih _ (vf xv)) }
end)
(generate_from_le $ assume u hu, generate_measurable.basic _ $
show t.is_open u, by rw [hs]; exact generate_open.basic _ hu)
lemma topological_space.is_topological_basis.borel_eq_generate_from [topological_space Ξ±]
[second_countable_topology Ξ±] {s : set (set Ξ±)} (hs : is_topological_basis s) :
borel Ξ± = generate_from s :=
borel_eq_generate_from_of_subbasis hs.eq_generate_from
lemma is_pi_system_is_open [topological_space Ξ±] : is_pi_system (is_open : set Ξ± β Prop) :=
Ξ» s hs t ht hst, is_open.inter hs ht
lemma borel_eq_generate_from_is_closed [topological_space Ξ±] :
borel Ξ± = generate_from {s | is_closed s} :=
le_antisymm
(generate_from_le $ Ξ» t ht, @measurable_set.of_compl Ξ± _ (generate_from {s | is_closed s})
(generate_measurable.basic _ $ is_closed_compl_iff.2 ht))
(generate_from_le $ Ξ» t ht, @measurable_set.of_compl Ξ± _ (borel Ξ±)
(generate_measurable.basic _ $ is_open_compl_iff.2 ht))
section order_topology
variable (Ξ±)
variables [topological_space Ξ±] [second_countable_topology Ξ±] [linear_order Ξ±] [order_topology Ξ±]
lemma borel_eq_generate_from_Iio : borel Ξ± = generate_from (range Iio) :=
begin
refine le_antisymm _ (generate_from_le _),
{ rw borel_eq_generate_from_of_subbasis (@order_topology.topology_eq_generate_intervals Ξ± _ _ _),
letI : measurable_space Ξ± := measurable_space.generate_from (range Iio),
have H : β a : Ξ±, measurable_set (Iio a) := Ξ» a, generate_measurable.basic _ β¨_, rflβ©,
refine generate_from_le _, rintro _ β¨a, rfl | rflβ©; [skip, apply H],
by_cases h : β a', β b, a < b β a' β€ b,
{ rcases h with β¨a', ha'β©,
rw (_ : Ioi a = (Iio a')αΆ), { exact (H _).compl },
simp [set.ext_iff, ha'] },
{ rcases is_open_Union_countable
(Ξ» a' : {a' : Ξ± // a < a'}, {b | a'.1 < b})
(Ξ» a', is_open_lt' _) with β¨v, β¨hvβ©, vuβ©,
simp [set.ext_iff] at vu,
have : Ioi a = β x : v, (Iio x.1.1)αΆ,
{ simp [set.ext_iff],
refine Ξ» x, β¨Ξ» ax, _, Ξ» β¨a', β¨h, avβ©, axβ©, lt_of_lt_of_le h axβ©,
rcases (vu x).2 _ with β¨a', hβ, hββ©,
{ exact β¨a', hβ, le_of_lt hββ© },
refine not_imp_comm.1 (Ξ» h, _) h,
exact β¨x, Ξ» b, β¨Ξ» ab, le_of_not_lt (Ξ» h', h β¨b, ab, h'β©),
lt_of_lt_of_le axβ©β© },
rw this, resetI,
apply measurable_set.Union,
exact Ξ» _, (H _).compl } },
{ rw forall_range_iff,
intro a,
exact generate_measurable.basic _ is_open_Iio }
end
lemma borel_eq_generate_from_Ioi : borel Ξ± = generate_from (range Ioi) :=
@borel_eq_generate_from_Iio (order_dual Ξ±) _ (by apply_instance : second_countable_topology Ξ±) _ _
end order_topology
lemma borel_comap {f : Ξ± β Ξ²} {t : topological_space Ξ²} :
@borel Ξ± (t.induced f) = (@borel Ξ² t).comap f :=
comap_generate_from.symm
lemma continuous.borel_measurable [topological_space Ξ±] [topological_space Ξ²]
{f : Ξ± β Ξ²} (hf : continuous f) :
@measurable Ξ± Ξ² (borel Ξ±) (borel Ξ²) f :=
measurable.of_le_map $ generate_from_le $
Ξ» s hs, generate_measurable.basic (f β»ΒΉ' s) (hs.preimage hf)
/-- A space with `measurable_space` and `topological_space` structures such that
all open sets are measurable. -/
class opens_measurable_space (Ξ± : Type*) [topological_space Ξ±] [h : measurable_space Ξ±] : Prop :=
(borel_le : borel Ξ± β€ h)
/-- A space with `measurable_space` and `topological_space` structures such that
the `Ο`-algebra of measurable sets is exactly the `Ο`-algebra generated by open sets. -/
class borel_space (Ξ± : Type*) [topological_space Ξ±] [measurable_space Ξ±] : Prop :=
(measurable_eq : βΉmeasurable_space Ξ±βΊ = borel Ξ±)
@[priority 100]
instance order_dual.opens_measurable_space {Ξ± : Type*} [topological_space Ξ±] [measurable_space Ξ±]
[h : opens_measurable_space Ξ±] :
opens_measurable_space (order_dual Ξ±) :=
{ borel_le := h.borel_le }
@[priority 100]
instance order_dual.borel_space {Ξ± : Type*} [topological_space Ξ±] [measurable_space Ξ±]
[h : borel_space Ξ±] :
borel_space (order_dual Ξ±) :=
{ measurable_eq := h.measurable_eq }
/-- In a `borel_space` all open sets are measurable. -/
@[priority 100]
instance borel_space.opens_measurable {Ξ± : Type*} [topological_space Ξ±] [measurable_space Ξ±]
[borel_space Ξ±] : opens_measurable_space Ξ± :=
β¨ge_of_eq $ borel_space.measurable_eqβ©
instance subtype.borel_space {Ξ± : Type*} [topological_space Ξ±] [measurable_space Ξ±]
[hΞ± : borel_space Ξ±] (s : set Ξ±) :
borel_space s :=
β¨by { rw [hΞ±.1, subtype.measurable_space, β borel_comap], refl }β©
instance subtype.opens_measurable_space {Ξ± : Type*} [topological_space Ξ±] [measurable_space Ξ±]
[h : opens_measurable_space Ξ±] (s : set Ξ±) :
opens_measurable_space s :=
β¨by { rw [borel_comap], exact comap_mono h.1 }β©
theorem _root_.measurable_set.induction_on_open [topological_space Ξ±] [measurable_space Ξ±]
[borel_space Ξ±] {C : set Ξ± β Prop} (h_open : β U, is_open U β C U)
(h_compl : β t, measurable_set t β C t β C tαΆ)
(h_union : β f : β β set Ξ±, pairwise (disjoint on f) β
(β i, measurable_set (f i)) β (β i, C (f i)) β C (β i, f i)) :
β β¦tβ¦, measurable_set t β C t :=
measurable_space.induction_on_inter borel_space.measurable_eq is_pi_system_is_open
(h_open _ is_open_empty) h_open h_compl h_union
section
variables [topological_space Ξ±] [measurable_space Ξ±] [opens_measurable_space Ξ±]
[topological_space Ξ²] [measurable_space Ξ²] [opens_measurable_space Ξ²]
[topological_space Ξ³] [measurable_space Ξ³] [borel_space Ξ³]
[topological_space Ξ³β] [measurable_space Ξ³β] [borel_space Ξ³β]
[measurable_space Ξ΄]
lemma is_open.measurable_set (h : is_open s) : measurable_set s :=
opens_measurable_space.borel_le _ $ generate_measurable.basic _ h
@[measurability]
lemma measurable_set_interior : measurable_set (interior s) := is_open_interior.measurable_set
lemma is_GΞ΄.measurable_set (h : is_GΞ΄ s) : measurable_set s :=
begin
rcases h with β¨S, hSo, hSc, rflβ©,
exact measurable_set.sInter hSc (Ξ» t ht, (hSo t ht).measurable_set)
end
lemma measurable_set_of_continuous_at {Ξ²} [emetric_space Ξ²] (f : Ξ± β Ξ²) :
measurable_set {x | continuous_at f x} :=
(is_GΞ΄_set_of_continuous_at f).measurable_set
lemma is_closed.measurable_set (h : is_closed s) : measurable_set s :=
h.is_open_compl.measurable_set.of_compl
lemma is_compact.measurable_set [t2_space Ξ±] (h : is_compact s) : measurable_set s :=
h.is_closed.measurable_set
@[measurability]
lemma measurable_set_closure : measurable_set (closure s) :=
is_closed_closure.measurable_set
lemma measurable_of_is_open {f : Ξ΄ β Ξ³} (hf : β s, is_open s β measurable_set (f β»ΒΉ' s)) :
measurable f :=
by { rw [βΉborel_space Ξ³βΊ.measurable_eq], exact measurable_generate_from hf }
lemma measurable_of_is_closed {f : Ξ΄ β Ξ³} (hf : β s, is_closed s β measurable_set (f β»ΒΉ' s)) :
measurable f :=
begin
apply measurable_of_is_open, intros s hs,
rw [β measurable_set.compl_iff, β preimage_compl], apply hf, rw [is_closed_compl_iff], exact hs
end
lemma measurable_of_is_closed' {f : Ξ΄ β Ξ³}
(hf : β s, is_closed s β s.nonempty β s β univ β measurable_set (f β»ΒΉ' s)) : measurable f :=
begin
apply measurable_of_is_closed, intros s hs,
cases eq_empty_or_nonempty s with h1 h1, { simp [h1] },
by_cases h2 : s = univ, { simp [h2] },
exact hf s hs h1 h2
end
instance nhds_is_measurably_generated (a : Ξ±) : (π a).is_measurably_generated :=
begin
rw [nhds, infi_subtype'],
refine @filter.infi_is_measurably_generated _ _ _ _ (Ξ» i, _),
exact i.2.2.measurable_set.principal_is_measurably_generated
end
/-- If `s` is a measurable set, then `π[s] a` is a measurably generated filter for
each `a`. This cannot be an `instance` because it depends on a non-instance `hs : measurable_set s`.
-/
lemma measurable_set.nhds_within_is_measurably_generated {s : set Ξ±} (hs : measurable_set s)
(a : Ξ±) :
(π[s] a).is_measurably_generated :=
by haveI := hs.principal_is_measurably_generated; exact filter.inf_is_measurably_generated _ _
@[priority 100] -- see Note [lower instance priority]
instance opens_measurable_space.to_measurable_singleton_class [t1_space Ξ±] :
measurable_singleton_class Ξ± :=
β¨Ξ» x, is_closed_singleton.measurable_setβ©
instance pi.opens_measurable_space_encodable {ΞΉ : Type*} {Ο : ΞΉ β Type*} [encodable ΞΉ]
[t' : Ξ i, topological_space (Ο i)]
[Ξ i, measurable_space (Ο i)] [β i, second_countable_topology (Ο i)]
[β i, opens_measurable_space (Ο i)] :
opens_measurable_space (Ξ i, Ο i) :=
begin
constructor,
have : Pi.topological_space =
generate_from {t | β(s:Ξ a, set (Ο a)) (i : finset ΞΉ), (βaβi, s a β countable_basis (Ο a)) β§
t = pi βi s},
{ rw [funext (Ξ» a, @eq_generate_from_countable_basis (Ο a) _ _), pi_generate_from_eq] },
rw [borel_eq_generate_from_of_subbasis this],
apply generate_from_le,
rintros _ β¨s, i, hi, rflβ©,
refine measurable_set.pi i.countable_to_set (Ξ» a ha, is_open.measurable_set _),
rw [eq_generate_from_countable_basis (Ο a)],
exact generate_open.basic _ (hi a ha)
end
instance pi.opens_measurable_space_fintype {ΞΉ : Type*} {Ο : ΞΉ β Type*} [fintype ΞΉ]
[t' : Ξ i, topological_space (Ο i)]
[Ξ i, measurable_space (Ο i)] [β i, second_countable_topology (Ο i)]
[β i, opens_measurable_space (Ο i)] :
opens_measurable_space (Ξ i, Ο i) :=
by { letI := fintype.encodable ΞΉ, apply_instance }
instance prod.opens_measurable_space [second_countable_topology Ξ±] [second_countable_topology Ξ²] :
opens_measurable_space (Ξ± Γ Ξ²) :=
begin
constructor,
rw [((is_basis_countable_basis Ξ±).prod (is_basis_countable_basis Ξ²)).borel_eq_generate_from],
apply generate_from_le,
rintros _ β¨u, v, hu, hv, rflβ©,
exact (is_open_of_mem_countable_basis hu).measurable_set.prod
(is_open_of_mem_countable_basis hv).measurable_set
end
variables {Ξ±' : Type*} [topological_space Ξ±'] [measurable_space Ξ±']
lemma measure_interior_of_null_bdry {ΞΌ : measure Ξ±'} {s : set Ξ±'}
(h_nullbdry : ΞΌ (frontier s) = 0) : ΞΌ (interior s) = ΞΌ s :=
measure_eq_measure_smaller_of_between_null_diff
interior_subset subset_closure h_nullbdry
lemma measure_closure_of_null_bdry {ΞΌ : measure Ξ±'} {s : set Ξ±'}
(h_nullbdry : ΞΌ (frontier s) = 0) : ΞΌ (closure s) = ΞΌ s :=
(measure_eq_measure_larger_of_between_null_diff
interior_subset subset_closure h_nullbdry).symm
section preorder
variables [preorder Ξ±] [order_closed_topology Ξ±] {a b x : Ξ±}
@[simp, measurability]
lemma measurable_set_Ici : measurable_set (Ici a) := is_closed_Ici.measurable_set
@[simp, measurability]
lemma measurable_set_Iic : measurable_set (Iic a) := is_closed_Iic.measurable_set
@[simp, measurability]
lemma measurable_set_Icc : measurable_set (Icc a b) := is_closed_Icc.measurable_set
instance nhds_within_Ici_is_measurably_generated :
(π[Ici b] a).is_measurably_generated :=
measurable_set_Ici.nhds_within_is_measurably_generated _
instance nhds_within_Iic_is_measurably_generated :
(π[Iic b] a).is_measurably_generated :=
measurable_set_Iic.nhds_within_is_measurably_generated _
instance nhds_within_Icc_is_measurably_generated :
is_measurably_generated (π[Icc a b] x) :=
by { rw [β Ici_inter_Iic, nhds_within_inter], apply_instance }
instance at_top_is_measurably_generated : (filter.at_top : filter Ξ±).is_measurably_generated :=
@filter.infi_is_measurably_generated _ _ _ _ $
Ξ» a, (measurable_set_Ici : measurable_set (Ici a)).principal_is_measurably_generated
instance at_bot_is_measurably_generated : (filter.at_bot : filter Ξ±).is_measurably_generated :=
@filter.infi_is_measurably_generated _ _ _ _ $
Ξ» a, (measurable_set_Iic : measurable_set (Iic a)).principal_is_measurably_generated
end preorder
section partial_order
variables [partial_order Ξ±] [order_closed_topology Ξ±] [second_countable_topology Ξ±]
{a b : Ξ±}
@[measurability]
lemma measurable_set_le' : measurable_set {p : Ξ± Γ Ξ± | p.1 β€ p.2} :=
order_closed_topology.is_closed_le'.measurable_set
@[measurability]
lemma measurable_set_le {f g : Ξ΄ β Ξ±} (hf : measurable f) (hg : measurable g) :
measurable_set {a | f a β€ g a} :=
hf.prod_mk hg measurable_set_le'
end partial_order
section linear_order
variables [linear_order Ξ±] [order_closed_topology Ξ±] {a b x : Ξ±}
@[simp, measurability]
lemma measurable_set_Iio : measurable_set (Iio a) := is_open_Iio.measurable_set
@[simp, measurability]
lemma measurable_set_Ioi : measurable_set (Ioi a) := is_open_Ioi.measurable_set
@[simp, measurability]
lemma measurable_set_Ioo : measurable_set (Ioo a b) := is_open_Ioo.measurable_set
@[simp, measurability] lemma measurable_set_Ioc : measurable_set (Ioc a b) :=
measurable_set_Ioi.inter measurable_set_Iic
@[simp, measurability] lemma measurable_set_Ico : measurable_set (Ico a b) :=
measurable_set_Ici.inter measurable_set_Iio
instance nhds_within_Ioi_is_measurably_generated :
(π[Ioi b] a).is_measurably_generated :=
measurable_set_Ioi.nhds_within_is_measurably_generated _
instance nhds_within_Iio_is_measurably_generated :
(π[Iio b] a).is_measurably_generated :=
measurable_set_Iio.nhds_within_is_measurably_generated _
instance nhds_within_interval_is_measurably_generated :
is_measurably_generated (π[[a, b]] x) :=
nhds_within_Icc_is_measurably_generated
@[measurability]
lemma measurable_set_lt' [second_countable_topology Ξ±] : measurable_set {p : Ξ± Γ Ξ± | p.1 < p.2} :=
(is_open_lt continuous_fst continuous_snd).measurable_set
@[measurability]
lemma measurable_set_lt [second_countable_topology Ξ±] {f g : Ξ΄ β Ξ±} (hf : measurable f)
(hg : measurable g) : measurable_set {a | f a < g a} :=
hf.prod_mk hg measurable_set_lt'
lemma set.ord_connected.measurable_set (h : ord_connected s) : measurable_set s :=
begin
let u := β (x β s) (y β s), Ioo x y,
have huopen : is_open u := is_open_bUnion (Ξ» x hx, is_open_bUnion (Ξ» y hy, is_open_Ioo)),
have humeas : measurable_set u := huopen.measurable_set,
have hfinite : (s \ u).finite,
{ refine set.finite_of_forall_between_eq_endpoints (s \ u) (Ξ» x hx y hy z hz hxy hyz, _),
by_contra' h,
exact hy.2 (mem_Unionβ.mpr β¨x, hx.1,
mem_Unionβ.mpr β¨z, hz.1, lt_of_le_of_ne hxy h.1, lt_of_le_of_ne hyz h.2β©β©) },
have : u β s :=
Unionβ_subset (Ξ» x hx, Unionβ_subset (Ξ» y hy, Ioo_subset_Icc_self.trans (h.out hx hy))),
rw β union_diff_cancel this,
exact humeas.union hfinite.measurable_set
end
lemma is_preconnected.measurable_set
(h : is_preconnected s) : measurable_set s :=
h.ord_connected.measurable_set
lemma generate_from_Ico_mem_le_borel {Ξ± : Type*} [topological_space Ξ±] [linear_order Ξ±]
[order_closed_topology Ξ±] (s t : set Ξ±) :
measurable_space.generate_from {S | β (l β s) (u β t) (h : l < u), Ico l u = S} β€ borel Ξ± :=
begin
apply generate_from_le,
letI : measurable_space Ξ± := borel Ξ±, haveI : borel_space Ξ± := β¨rflβ©,
rintro _ β¨a, -, b, -, -, rflβ©,
exact measurable_set_Ico
end
lemma dense.borel_eq_generate_from_Ico_mem_aux {Ξ± : Type*} [topological_space Ξ±] [linear_order Ξ±]
[order_topology Ξ±] [second_countable_topology Ξ±] {s : set Ξ±} (hd : dense s)
(hbot : β x, is_bot x β x β s) (hIoo : β x y : Ξ±, x < y β Ioo x y = β
β y β s) :
borel Ξ± = generate_from {S : set Ξ± | β (l β s) (u β s) (h : l < u), Ico l u = S} :=
begin
set S : set (set Ξ±) := {S | β (l β s) (u β s) (h : l < u), Ico l u = S},
refine le_antisymm _ (generate_from_Ico_mem_le_borel _ _),
letI : measurable_space Ξ± := generate_from S,
rw borel_eq_generate_from_Iio,
refine generate_from_le (forall_range_iff.2 $ Ξ» a, _),
rcases hd.exists_countable_dense_subset_bot_top with β¨t, hts, hc, htd, htb, httβ©,
by_cases ha : β b < a, (Ioo b a).nonempty,
{ convert_to measurable_set (β (l β t) (u β t) (hlu : l < u) (hu : u β€ a), Ico l u),
{ ext y, simp only [mem_Union, mem_Iio, mem_Ico], split,
{ intro hy,
rcases htd.exists_le' (Ξ» b hb, htb _ hb (hbot b hb)) y with β¨l, hlt, hlyβ©,
rcases htd.exists_mem_open is_open_Ioo (ha y hy) with β¨u, hut, hyu, huaβ©,
exact β¨l, hlt, u, hut, hly.trans_lt hyu, hua.le, hly, hyuβ© },
{ rintro β¨l, -, u, -, -, hua, -, hyuβ©,
exact hyu.trans_le hua } },
{ refine measurable_set.bUnion hc (Ξ» a ha, measurable_set.bUnion hc $ Ξ» b hb, _),
refine measurable_set.Union_Prop (Ξ» hab, measurable_set.Union_Prop $ Ξ» hb', _),
exact generate_measurable.basic _ β¨a, hts ha, b, hts hb, hab, mem_singleton _β© } },
{ simp only [not_forall, not_nonempty_iff_eq_empty] at ha,
replace ha : a β s := hIoo ha.some a ha.some_spec.fst ha.some_spec.snd,
convert_to measurable_set (β (l β t) (hl : l < a), Ico l a),
{ symmetry,
simp only [β Ici_inter_Iio, β Union_inter, inter_eq_right_iff_subset, subset_def, mem_Union,
mem_Ici, mem_Iio],
intros x hx, rcases htd.exists_le' (Ξ» b hb, htb _ hb (hbot b hb)) x with β¨z, hzt, hzxβ©,
exact β¨z, hzt, hzx.trans_lt hx, hzxβ© },
{ refine measurable_set.bUnion hc (Ξ» x hx, measurable_set.Union_Prop $ Ξ» hlt, _),
exact generate_measurable.basic _ β¨x, hts hx, a, ha, hlt, mem_singleton _β© } }
end
lemma dense.borel_eq_generate_from_Ico_mem {Ξ± : Type*} [topological_space Ξ±] [linear_order Ξ±]
[order_topology Ξ±] [second_countable_topology Ξ±] [densely_ordered Ξ±] [no_min_order Ξ±]
{s : set Ξ±} (hd : dense s) :
borel Ξ± = generate_from {S : set Ξ± | β (l β s) (u β s) (h : l < u), Ico l u = S} :=
hd.borel_eq_generate_from_Ico_mem_aux (by simp) $
Ξ» x y hxy H, ((nonempty_Ioo.2 hxy).ne_empty H).elim
lemma borel_eq_generate_from_Ico (Ξ± : Type*) [topological_space Ξ±]
[second_countable_topology Ξ±] [linear_order Ξ±] [order_topology Ξ±] :
borel Ξ± = generate_from {S : set Ξ± | β l u (h : l < u), Ico l u = S} :=
by simpa only [exists_prop, mem_univ, true_and]
using (@dense_univ Ξ± _).borel_eq_generate_from_Ico_mem_aux (Ξ» _ _, mem_univ _)
(Ξ» _ _ _ _, mem_univ _)
lemma dense.borel_eq_generate_from_Ioc_mem_aux {Ξ± : Type*} [topological_space Ξ±] [linear_order Ξ±]
[order_topology Ξ±] [second_countable_topology Ξ±] {s : set Ξ±} (hd : dense s)
(hbot : β x, is_top x β x β s) (hIoo : β x y : Ξ±, x < y β Ioo x y = β
β x β s) :
borel Ξ± = generate_from {S : set Ξ± | β (l β s) (u β s) (h : l < u), Ioc l u = S} :=
begin
convert hd.order_dual.borel_eq_generate_from_Ico_mem_aux hbot (Ξ» x y hlt he, hIoo y x hlt _),
{ ext s,
split; rintro β¨l, hl, u, hu, hlt, rflβ©,
exacts [β¨u, hu, l, hl, hlt, dual_Icoβ©, β¨u, hu, l, hl, hlt, dual_Iocβ©] },
{ erw dual_Ioo,
exact he }
end
lemma dense.borel_eq_generate_from_Ioc_mem {Ξ± : Type*} [topological_space Ξ±] [linear_order Ξ±]
[order_topology Ξ±] [second_countable_topology Ξ±] [densely_ordered Ξ±] [no_max_order Ξ±]
{s : set Ξ±} (hd : dense s) :
borel Ξ± = generate_from {S : set Ξ± | β (l β s) (u β s) (h : l < u), Ioc l u = S} :=
hd.borel_eq_generate_from_Ioc_mem_aux (by simp) $
Ξ» x y hxy H, ((nonempty_Ioo.2 hxy).ne_empty H).elim
lemma borel_eq_generate_from_Ioc (Ξ± : Type*) [topological_space Ξ±]
[second_countable_topology Ξ±] [linear_order Ξ±] [order_topology Ξ±] :
borel Ξ± = generate_from {S : set Ξ± | β l u (h : l < u), Ioc l u = S} :=
by simpa only [exists_prop, mem_univ, true_and]
using (@dense_univ Ξ± _).borel_eq_generate_from_Ioc_mem_aux (Ξ» _ _, mem_univ _)
(Ξ» _ _ _ _, mem_univ _)
namespace measure_theory.measure
/-- Two finite measures on a Borel space are equal if they agree on all closed-open intervals. If
`Ξ±` is a conditionally complete linear order with no top element,
`measure_theory.measure..ext_of_Ico` is an extensionality lemma with weaker assumptions on `ΞΌ` and
`Ξ½`. -/
lemma ext_of_Ico_finite {Ξ± : Type*} [topological_space Ξ±] {m : measurable_space Ξ±}
[second_countable_topology Ξ±] [linear_order Ξ±] [order_topology Ξ±]
[borel_space Ξ±] (ΞΌ Ξ½ : measure Ξ±) [is_finite_measure ΞΌ] (hΞΌΞ½ : ΞΌ univ = Ξ½ univ)
(h : β β¦a bβ¦, a < b β ΞΌ (Ico a b) = Ξ½ (Ico a b)) : ΞΌ = Ξ½ :=
begin
refine ext_of_generate_finite _
(borel_space.measurable_eq.trans (borel_eq_generate_from_Ico Ξ±))
(is_pi_system_Ico (id : Ξ± β Ξ±) id) _ hΞΌΞ½,
{ rintro - β¨a, b, hlt, rflβ©,
exact h hlt }
end
/-- Two finite measures on a Borel space are equal if they agree on all open-closed intervals. If
`Ξ±` is a conditionally complete linear order with no top element,
`measure_theory.measure..ext_of_Ioc` is an extensionality lemma with weaker assumptions on `ΞΌ` and
`Ξ½`. -/
lemma ext_of_Ioc_finite {Ξ± : Type*} [topological_space Ξ±] {m : measurable_space Ξ±}
[second_countable_topology Ξ±] [linear_order Ξ±] [order_topology Ξ±]
[borel_space Ξ±] (ΞΌ Ξ½ : measure Ξ±) [is_finite_measure ΞΌ] (hΞΌΞ½ : ΞΌ univ = Ξ½ univ)
(h : β β¦a bβ¦, a < b β ΞΌ (Ioc a b) = Ξ½ (Ioc a b)) : ΞΌ = Ξ½ :=
begin
refine @ext_of_Ico_finite (order_dual Ξ±) _ _ _ _ _ βΉ_βΊ ΞΌ Ξ½ _ hΞΌΞ½ (Ξ» a b hab, _),
erw dual_Ico,
exact h hab
end
/-- Two measures which are finite on closed-open intervals are equal if the agree on all
closed-open intervals. -/
lemma ext_of_Ico' {Ξ± : Type*} [topological_space Ξ±] {m : measurable_space Ξ±}
[second_countable_topology Ξ±] [linear_order Ξ±] [order_topology Ξ±] [borel_space Ξ±]
[no_max_order Ξ±] (ΞΌ Ξ½ : measure Ξ±) (hΞΌ : β β¦a bβ¦, a < b β ΞΌ (Ico a b) β β)
(h : β β¦a bβ¦, a < b β ΞΌ (Ico a b) = Ξ½ (Ico a b)) : ΞΌ = Ξ½ :=
begin
rcases exists_countable_dense_bot_top Ξ± with β¨s, hsc, hsd, hsb, hstβ©,
have : countable (β (l β s) (u β s) (h : l < u), {Ico l u} : set (set Ξ±)),
from hsc.bUnion (Ξ» l hl, hsc.bUnion
(Ξ» u hu, countable_Union_Prop $ Ξ» _, countable_singleton _)),
simp only [β set_of_eq_eq_singleton, β set_of_exists] at this,
refine measure.ext_of_generate_from_of_cover_subset
(borel_space.measurable_eq.trans (borel_eq_generate_from_Ico Ξ±))
(is_pi_system_Ico id id) _ this _ _ _,
{ rintro _ β¨l, -, u, -, h, rflβ©, exact β¨l, u, h, rflβ© },
{ refine sUnion_eq_univ_iff.2 (Ξ» x, _),
rcases hsd.exists_le' hsb x with β¨l, hls, hlxβ©,
rcases hsd.exists_gt x with β¨u, hus, hxuβ©,
exact β¨_, β¨l, hls, u, hus, hlx.trans_lt hxu, rflβ©, hlx, hxuβ© },
{ rintro _ β¨l, -, u, -, hlt, rflβ©, exact hΞΌ hlt },
{ rintro _ β¨l, u, hlt, rflβ©, exact h hlt }
end
/-- Two measures which are finite on closed-open intervals are equal if the agree on all
open-closed intervals. -/
lemma ext_of_Ioc' {Ξ± : Type*} [topological_space Ξ±] {m : measurable_space Ξ±}
[second_countable_topology Ξ±] [linear_order Ξ±] [order_topology Ξ±] [borel_space Ξ±]
[no_min_order Ξ±] (ΞΌ Ξ½ : measure Ξ±) (hΞΌ : β β¦a bβ¦, a < b β ΞΌ (Ioc a b) β β)
(h : β β¦a bβ¦, a < b β ΞΌ (Ioc a b) = Ξ½ (Ioc a b)) : ΞΌ = Ξ½ :=
begin
refine @ext_of_Ico' (order_dual Ξ±) _ _ _ _ _ βΉ_βΊ _ ΞΌ Ξ½ _ _;
intros a b hab; erw dual_Ico,
exacts [hΞΌ hab, h hab]
end
/-- Two measures which are finite on closed-open intervals are equal if the agree on all
closed-open intervals. -/
lemma ext_of_Ico {Ξ± : Type*} [topological_space Ξ±] {m : measurable_space Ξ±}
[second_countable_topology Ξ±] [conditionally_complete_linear_order Ξ±] [order_topology Ξ±]
[borel_space Ξ±] [no_max_order Ξ±] (ΞΌ Ξ½ : measure Ξ±) [is_locally_finite_measure ΞΌ]
(h : β β¦a bβ¦, a < b β ΞΌ (Ico a b) = Ξ½ (Ico a b)) : ΞΌ = Ξ½ :=
ΞΌ.ext_of_Ico' Ξ½ (Ξ» a b hab, measure_Ico_lt_top.ne) h
/-- Two measures which are finite on closed-open intervals are equal if the agree on all
open-closed intervals. -/
lemma ext_of_Ioc {Ξ± : Type*} [topological_space Ξ±] {m : measurable_space Ξ±}
[second_countable_topology Ξ±] [conditionally_complete_linear_order Ξ±] [order_topology Ξ±]
[borel_space Ξ±] [no_min_order Ξ±] (ΞΌ Ξ½ : measure Ξ±) [is_locally_finite_measure ΞΌ]
(h : β β¦a bβ¦, a < b β ΞΌ (Ioc a b) = Ξ½ (Ioc a b)) : ΞΌ = Ξ½ :=
ΞΌ.ext_of_Ioc' Ξ½ (Ξ» a b hab, measure_Ioc_lt_top.ne) h
/-- Two finite measures on a Borel space are equal if they agree on all left-infinite right-closed
intervals. -/
lemma ext_of_Iic {Ξ± : Type*} [topological_space Ξ±] {m : measurable_space Ξ±}
[second_countable_topology Ξ±] [linear_order Ξ±] [order_topology Ξ±] [borel_space Ξ±]
(ΞΌ Ξ½ : measure Ξ±) [is_finite_measure ΞΌ] (h : β a, ΞΌ (Iic a) = Ξ½ (Iic a)) : ΞΌ = Ξ½ :=
begin
refine ext_of_Ioc_finite ΞΌ Ξ½ _ (Ξ» a b hlt, _),
{ rcases exists_countable_dense_bot_top Ξ± with β¨s, hsc, hsd, -, hstβ©,
have : directed_on (β€) s, from directed_on_iff_directed.2 (directed_of_sup $ Ξ» _ _, id),
simp only [β bsupr_measure_Iic hsc (hsd.exists_ge' hst) this, h] },
rw [β Iic_diff_Iic, measure_diff (Iic_subset_Iic.2 hlt.le) measurable_set_Iic,
measure_diff (Iic_subset_Iic.2 hlt.le) measurable_set_Iic, h a, h b],
{ rw β h a, exact (measure_lt_top ΞΌ _).ne },
{ exact (measure_lt_top ΞΌ _).ne }
end
/-- Two finite measures on a Borel space are equal if they agree on all left-closed right-infinite
intervals. -/
lemma ext_of_Ici {Ξ± : Type*} [topological_space Ξ±] {m : measurable_space Ξ±}
[second_countable_topology Ξ±] [linear_order Ξ±] [order_topology Ξ±] [borel_space Ξ±]
(ΞΌ Ξ½ : measure Ξ±) [is_finite_measure ΞΌ] (h : β a, ΞΌ (Ici a) = Ξ½ (Ici a)) : ΞΌ = Ξ½ :=
@ext_of_Iic (order_dual Ξ±) _ _ _ _ _ βΉ_βΊ _ _ _ h
end measure_theory.measure
end linear_order
section linear_order
variables [linear_order Ξ±] [order_closed_topology Ξ±]
@[measurability]
lemma measurable_set_interval {a b : Ξ±} : measurable_set (interval a b) :=
measurable_set_Icc
@[measurability]
lemma measurable_set_interval_oc {a b : Ξ±} : measurable_set (interval_oc a b) :=
measurable_set_Ioc
variables [second_countable_topology Ξ±]
@[measurability]
lemma measurable.max {f g : Ξ΄ β Ξ±} (hf : measurable f) (hg : measurable g) :
measurable (Ξ» a, max (f a) (g a)) :=
by simpa only [max_def] using hf.piecewise (measurable_set_le hg hf) hg
@[measurability]
lemma ae_measurable.max {f g : Ξ΄ β Ξ±} {ΞΌ : measure Ξ΄}
(hf : ae_measurable f ΞΌ) (hg : ae_measurable g ΞΌ) : ae_measurable (Ξ» a, max (f a) (g a)) ΞΌ :=
β¨Ξ» a, max (hf.mk f a) (hg.mk g a), hf.measurable_mk.max hg.measurable_mk,
eventually_eq.compβ hf.ae_eq_mk _ hg.ae_eq_mkβ©
@[measurability]
lemma measurable.min {f g : Ξ΄ β Ξ±} (hf : measurable f) (hg : measurable g) :
measurable (Ξ» a, min (f a) (g a)) :=
by simpa only [min_def] using hf.piecewise (measurable_set_le hf hg) hg
@[measurability]
lemma ae_measurable.min {f g : Ξ΄ β Ξ±} {ΞΌ : measure Ξ΄}
(hf : ae_measurable f ΞΌ) (hg : ae_measurable g ΞΌ) : ae_measurable (Ξ» a, min (f a) (g a)) ΞΌ :=
β¨Ξ» a, min (hf.mk f a) (hg.mk g a), hf.measurable_mk.min hg.measurable_mk,
eventually_eq.compβ hf.ae_eq_mk _ hg.ae_eq_mkβ©
end linear_order
/-- A continuous function from an `opens_measurable_space` to a `borel_space`
is measurable. -/
lemma continuous.measurable {f : Ξ± β Ξ³} (hf : continuous f) :
measurable f :=
hf.borel_measurable.mono opens_measurable_space.borel_le
(le_of_eq $ borel_space.measurable_eq)
/-- A continuous function from an `opens_measurable_space` to a `borel_space`
is ae-measurable. -/
lemma continuous.ae_measurable {f : Ξ± β Ξ³} (h : continuous f) (ΞΌ : measure Ξ±) : ae_measurable f ΞΌ :=
h.measurable.ae_measurable
lemma closed_embedding.measurable {f : Ξ± β Ξ³} (hf : closed_embedding f) :
measurable f :=
hf.continuous.measurable
lemma continuous.is_open_pos_measure_map {f : Ξ² β Ξ³} (hf : continuous f)
(hf_surj : function.surjective f) {ΞΌ : measure Ξ²} [ΞΌ.is_open_pos_measure] :
(measure.map f ΞΌ).is_open_pos_measure :=
begin
refine β¨Ξ» U hUo hUne, _β©,
rw [measure.map_apply hf.measurable hUo.measurable_set],
exact (hUo.preimage hf).measure_ne_zero ΞΌ (hf_surj.nonempty_preimage.mpr hUne)
end
@[priority 100, to_additive]
instance has_continuous_mul.has_measurable_mul [has_mul Ξ³] [has_continuous_mul Ξ³] :
has_measurable_mul Ξ³ :=
{ measurable_const_mul := Ξ» c, (continuous_const.mul continuous_id).measurable,
measurable_mul_const := Ξ» c, (continuous_id.mul continuous_const).measurable }
@[priority 100]
instance has_continuous_sub.has_measurable_sub [has_sub Ξ³] [has_continuous_sub Ξ³] :
has_measurable_sub Ξ³ :=
{ measurable_const_sub := Ξ» c, (continuous_const.sub continuous_id).measurable,
measurable_sub_const := Ξ» c, (continuous_id.sub continuous_const).measurable }
@[priority 100, to_additive]
instance topological_group.has_measurable_inv [group Ξ³] [topological_group Ξ³] :
has_measurable_inv Ξ³ :=
β¨continuous_inv.measurableβ©
@[priority 100]
instance has_continuous_smul.has_measurable_smul {M Ξ±} [topological_space M]
[topological_space Ξ±] [measurable_space M] [measurable_space Ξ±]
[opens_measurable_space M] [borel_space Ξ±] [has_scalar M Ξ±] [has_continuous_smul M Ξ±] :
has_measurable_smul M Ξ± :=
β¨Ξ» c, (continuous_const.smul continuous_id).measurable,
Ξ» y, (continuous_id.smul continuous_const).measurableβ©
section lattice
@[priority 100]
instance has_continuous_sup.has_measurable_sup [has_sup Ξ³] [has_continuous_sup Ξ³] :
has_measurable_sup Ξ³ :=
{ measurable_const_sup := Ξ» c, (continuous_const.sup continuous_id).measurable,
measurable_sup_const := Ξ» c, (continuous_id.sup continuous_const).measurable }
@[priority 100]
instance has_continuous_sup.has_measurable_supβ [second_countable_topology Ξ³] [has_sup Ξ³]
[has_continuous_sup Ξ³] :
has_measurable_supβ Ξ³ :=
β¨continuous_sup.measurableβ©
@[priority 100]
instance has_continuous_inf.has_measurable_inf [has_inf Ξ³] [has_continuous_inf Ξ³] :
has_measurable_inf Ξ³ :=
{ measurable_const_inf := Ξ» c, (continuous_const.inf continuous_id).measurable,
measurable_inf_const := Ξ» c, (continuous_id.inf continuous_const).measurable }
@[priority 100]
instance has_continuous_inf.has_measurable_infβ [second_countable_topology Ξ³] [has_inf Ξ³]
[has_continuous_inf Ξ³] :
has_measurable_infβ Ξ³ :=
β¨continuous_inf.measurableβ©
end lattice
section homeomorph
@[measurability] protected lemma homeomorph.measurable (h : Ξ± ββ Ξ³) : measurable h :=
h.continuous.measurable
/-- A homeomorphism between two Borel spaces is a measurable equivalence.-/
def homeomorph.to_measurable_equiv (h : Ξ³ ββ Ξ³β) : Ξ³ βα΅ Ξ³β :=
{ measurable_to_fun := h.measurable,
measurable_inv_fun := h.symm.measurable,
to_equiv := h.to_equiv }
@[simp]
lemma homeomorph.to_measurable_equiv_coe (h : Ξ³ ββ Ξ³β) : (h.to_measurable_equiv : Ξ³ β Ξ³β) = h :=
rfl
@[simp] lemma homeomorph.to_measurable_equiv_symm_coe (h : Ξ³ ββ Ξ³β) :
(h.to_measurable_equiv.symm : Ξ³β β Ξ³) = h.symm :=
rfl
end homeomorph
@[measurability] lemma continuous_map.measurable (f : C(Ξ±, Ξ³)) : measurable f :=
f.continuous.measurable
lemma measurable_of_continuous_on_compl_singleton [t1_space Ξ±] {f : Ξ± β Ξ³} (a : Ξ±)
(hf : continuous_on f {a}αΆ) :
measurable f :=
measurable_of_measurable_on_compl_singleton a
(continuous_on_iff_continuous_restrict.1 hf).measurable
lemma continuous.measurable2 [second_countable_topology Ξ±] [second_countable_topology Ξ²]
{f : Ξ΄ β Ξ±} {g : Ξ΄ β Ξ²} {c : Ξ± β Ξ² β Ξ³}
(h : continuous (Ξ» p : Ξ± Γ Ξ², c p.1 p.2)) (hf : measurable f) (hg : measurable g) :
measurable (Ξ» a, c (f a) (g a)) :=
h.measurable.comp (hf.prod_mk hg)
lemma continuous.ae_measurable2 [second_countable_topology Ξ±] [second_countable_topology Ξ²]
{f : Ξ΄ β Ξ±} {g : Ξ΄ β Ξ²} {c : Ξ± β Ξ² β Ξ³} {ΞΌ : measure Ξ΄}
(h : continuous (Ξ» p : Ξ± Γ Ξ², c p.1 p.2)) (hf : ae_measurable f ΞΌ) (hg : ae_measurable g ΞΌ) :
ae_measurable (Ξ» a, c (f a) (g a)) ΞΌ :=
h.measurable.comp_ae_measurable (hf.prod_mk hg)
@[priority 100]
instance has_continuous_invβ.has_measurable_inv [group_with_zero Ξ³] [t1_space Ξ³]
[has_continuous_invβ Ξ³] :
has_measurable_inv Ξ³ :=
β¨measurable_of_continuous_on_compl_singleton 0 continuous_on_invββ©
@[priority 100, to_additive]
instance has_continuous_mul.has_measurable_mulβ [second_countable_topology Ξ³] [has_mul Ξ³]
[has_continuous_mul Ξ³] : has_measurable_mulβ Ξ³ :=
β¨continuous_mul.measurableβ©
@[priority 100]
instance has_continuous_sub.has_measurable_subβ [second_countable_topology Ξ³] [has_sub Ξ³]
[has_continuous_sub Ξ³] : has_measurable_subβ Ξ³ :=
β¨continuous_sub.measurableβ©
@[priority 100]
instance has_continuous_smul.has_measurable_smulβ {M Ξ±} [topological_space M]
[second_countable_topology M] [measurable_space M] [opens_measurable_space M]
[topological_space Ξ±] [second_countable_topology Ξ±] [measurable_space Ξ±]
[borel_space Ξ±] [has_scalar M Ξ±] [has_continuous_smul M Ξ±] :
has_measurable_smulβ M Ξ± :=
β¨continuous_smul.measurableβ©
end
section borel_space
variables [topological_space Ξ±] [measurable_space Ξ±] [borel_space Ξ±]
[topological_space Ξ²] [measurable_space Ξ²] [borel_space Ξ²]
[topological_space Ξ³] [measurable_space Ξ³] [borel_space Ξ³]
[measurable_space Ξ΄]
lemma pi_le_borel_pi {ΞΉ : Type*} {Ο : ΞΉ β Type*} [Ξ i, topological_space (Ο i)]
[Ξ i, measurable_space (Ο i)] [β i, borel_space (Ο i)] :
measurable_space.pi β€ borel (Ξ i, Ο i) :=
begin
have : βΉΞ i, measurable_space (Ο i)βΊ = Ξ» i, borel (Ο i) :=
funext (Ξ» i, borel_space.measurable_eq),
rw [this],
exact supr_le (Ξ» i, comap_le_iff_le_map.2 $ (continuous_apply i).borel_measurable)
end
lemma prod_le_borel_prod : prod.measurable_space β€ borel (Ξ± Γ Ξ²) :=
begin
rw [βΉborel_space Ξ±βΊ.measurable_eq, βΉborel_space Ξ²βΊ.measurable_eq],
refine sup_le _ _,
{ exact comap_le_iff_le_map.mpr continuous_fst.borel_measurable },
{ exact comap_le_iff_le_map.mpr continuous_snd.borel_measurable }
end
instance pi.borel_space_fintype_encodable {ΞΉ : Type*} {Ο : ΞΉ β Type*} [encodable ΞΉ]
[t' : Ξ i, topological_space (Ο i)]
[Ξ i, measurable_space (Ο i)] [β i, second_countable_topology (Ο i)]
[β i, borel_space (Ο i)] :
borel_space (Ξ i, Ο i) :=
β¨le_antisymm pi_le_borel_pi opens_measurable_space.borel_leβ©
instance pi.borel_space_fintype {ΞΉ : Type*} {Ο : ΞΉ β Type*} [fintype ΞΉ]
[t' : Ξ i, topological_space (Ο i)]
[Ξ i, measurable_space (Ο i)] [β i, second_countable_topology (Ο i)]
[β i, borel_space (Ο i)] :
borel_space (Ξ i, Ο i) :=
β¨le_antisymm pi_le_borel_pi opens_measurable_space.borel_leβ©
instance prod.borel_space [second_countable_topology Ξ±] [second_countable_topology Ξ²] :
borel_space (Ξ± Γ Ξ²) :=
β¨le_antisymm prod_le_borel_prod opens_measurable_space.borel_leβ©
protected lemma embedding.measurable_embedding {f : Ξ± β Ξ²} (hβ : embedding f)
(hβ : measurable_set (range f)) : measurable_embedding f :=
show measurable_embedding (coe β (homeomorph.of_embedding f hβ).to_measurable_equiv),
from (measurable_embedding.subtype_coe hβ).comp (measurable_equiv.measurable_embedding _)
protected lemma closed_embedding.measurable_embedding {f : Ξ± β Ξ²} (h : closed_embedding f) :
measurable_embedding f :=
h.to_embedding.measurable_embedding h.closed_range.measurable_set
protected lemma open_embedding.measurable_embedding {f : Ξ± β Ξ²} (h : open_embedding f) :
measurable_embedding f :=
h.to_embedding.measurable_embedding h.open_range.measurable_set
section linear_order
variables [linear_order Ξ±] [order_topology Ξ±] [second_countable_topology Ξ±]
lemma measurable_of_Iio {f : Ξ΄ β Ξ±} (hf : β x, measurable_set (f β»ΒΉ' Iio x)) : measurable f :=
begin
convert measurable_generate_from _,
exact borel_space.measurable_eq.trans (borel_eq_generate_from_Iio _),
rintro _ β¨x, rflβ©, exact hf x
end
lemma upper_semicontinuous.measurable [topological_space Ξ΄] [opens_measurable_space Ξ΄]
{f : Ξ΄ β Ξ±} (hf : upper_semicontinuous f) : measurable f :=
measurable_of_Iio (Ξ» y, (hf.is_open_preimage y).measurable_set)
lemma measurable_of_Ioi {f : Ξ΄ β Ξ±} (hf : β x, measurable_set (f β»ΒΉ' Ioi x)) : measurable f :=
begin
convert measurable_generate_from _,
exact borel_space.measurable_eq.trans (borel_eq_generate_from_Ioi _),
rintro _ β¨x, rflβ©, exact hf x
end
lemma lower_semicontinuous.measurable [topological_space Ξ΄] [opens_measurable_space Ξ΄]
{f : Ξ΄ β Ξ±} (hf : lower_semicontinuous f) : measurable f :=
measurable_of_Ioi (Ξ» y, (hf.is_open_preimage y).measurable_set)
lemma measurable_of_Iic {f : Ξ΄ β Ξ±} (hf : β x, measurable_set (f β»ΒΉ' Iic x)) : measurable f :=
begin
apply measurable_of_Ioi,
simp_rw [β compl_Iic, preimage_compl, measurable_set.compl_iff],
assumption
end
lemma measurable_of_Ici {f : Ξ΄ β Ξ±} (hf : β x, measurable_set (f β»ΒΉ' Ici x)) : measurable f :=
begin
apply measurable_of_Iio,
simp_rw [β compl_Ici, preimage_compl, measurable_set.compl_iff],
assumption
end
lemma measurable.is_lub {ΞΉ} [encodable ΞΉ] {f : ΞΉ β Ξ΄ β Ξ±} {g : Ξ΄ β Ξ±} (hf : β i, measurable (f i))
(hg : β b, is_lub {a | β i, f i b = a} (g b)) :
measurable g :=
begin
change β b, is_lub (range $ Ξ» i, f i b) (g b) at hg,
rw [βΉborel_space Ξ±βΊ.measurable_eq, borel_eq_generate_from_Ioi Ξ±],
apply measurable_generate_from,
rintro _ β¨a, rflβ©,
simp_rw [set.preimage, mem_Ioi, lt_is_lub_iff (hg _), exists_range_iff, set_of_exists],
exact measurable_set.Union (Ξ» i, hf i (is_open_lt' _).measurable_set)
end
private lemma ae_measurable.is_lub_of_nonempty {ΞΉ} (hΞΉ : nonempty ΞΉ)
{ΞΌ : measure Ξ΄} [encodable ΞΉ] {f : ΞΉ β Ξ΄ β Ξ±} {g : Ξ΄ β Ξ±}
(hf : β i, ae_measurable (f i) ΞΌ) (hg : βα΅ b βΞΌ, is_lub {a | β i, f i b = a} (g b)) :
ae_measurable g ΞΌ :=
begin
let p : Ξ΄ β (ΞΉ β Ξ±) β Prop := Ξ» x f', is_lub {a | β i, f' i = a} (g x),
let g_seq := Ξ» x, ite (x β ae_seq_set hf p) (g x) (β¨g xβ© : nonempty Ξ±).some,
have hg_seq : β b, is_lub {a | β i, ae_seq hf p i b = a} (g_seq b),
{ intro b,
haveI hΞ± : nonempty Ξ± := nonempty.map g β¨bβ©,
simp only [ae_seq, g_seq],
split_ifs,
{ have h_set_eq : {a : Ξ± | β (i : ΞΉ), (hf i).mk (f i) b = a} = {a : Ξ± | β (i : ΞΉ), f i b = a},
{ ext x,
simp_rw [set.mem_set_of_eq, ae_seq.mk_eq_fun_of_mem_ae_seq_set hf h], },
rw h_set_eq,
exact ae_seq.fun_prop_of_mem_ae_seq_set hf h, },
{ have h_singleton : {a : Ξ± | β (i : ΞΉ), hΞ±.some = a} = {hΞ±.some},
{ ext1 x,
exact β¨Ξ» hx, hx.some_spec.symm, Ξ» hx, β¨hΞΉ.some, hx.symmβ©β©, },
rw h_singleton,
exact is_lub_singleton, }, },
refine β¨g_seq, measurable.is_lub (ae_seq.measurable hf p) hg_seq, _β©,
exact (ite_ae_eq_of_measure_compl_zero g (Ξ» x, (β¨g xβ© : nonempty Ξ±).some) (ae_seq_set hf p)
(ae_seq.measure_compl_ae_seq_set_eq_zero hf hg)).symm,
end
lemma ae_measurable.is_lub {ΞΉ} {ΞΌ : measure Ξ΄} [encodable ΞΉ] {f : ΞΉ β Ξ΄ β Ξ±} {g : Ξ΄ β Ξ±}
(hf : β i, ae_measurable (f i) ΞΌ) (hg : βα΅ b βΞΌ, is_lub {a | β i, f i b = a} (g b)) :
ae_measurable g ΞΌ :=
begin
by_cases hΞΌ : ΞΌ = 0, { rw hΞΌ, exact ae_measurable_zero_measure },
haveI : ΞΌ.ae.ne_bot, { simpa [ne_bot_iff] },
by_cases hΞΉ : nonempty ΞΉ, { exact ae_measurable.is_lub_of_nonempty hΞΉ hf hg, },
suffices : β x, g =α΅[ΞΌ] Ξ» y, g x,
by { exact β¨(Ξ» y, g this.some), measurable_const, this.some_specβ©, },
have h_empty : β x, {a : Ξ± | β (i : ΞΉ), f i x = a} = β
,
{ intro x,
ext1 y,
rw [set.mem_set_of_eq, set.mem_empty_eq, iff_false],
exact Ξ» hi, hΞΉ (nonempty_of_exists hi), },
simp_rw h_empty at hg,
exact β¨hg.exists.some, hg.mono (Ξ» y hy, is_lub.unique hy hg.exists.some_spec)β©,
end
lemma measurable.is_glb {ΞΉ} [encodable ΞΉ] {f : ΞΉ β Ξ΄ β Ξ±} {g : Ξ΄ β Ξ±} (hf : β i, measurable (f i))
(hg : β b, is_glb {a | β i, f i b = a} (g b)) :
measurable g :=
begin
change β b, is_glb (range $ Ξ» i, f i b) (g b) at hg,
rw [βΉborel_space Ξ±βΊ.measurable_eq, borel_eq_generate_from_Iio Ξ±],
apply measurable_generate_from,
rintro _ β¨a, rflβ©,
simp_rw [set.preimage, mem_Iio, is_glb_lt_iff (hg _), exists_range_iff, set_of_exists],
exact measurable_set.Union (Ξ» i, hf i (is_open_gt' _).measurable_set)
end
private lemma ae_measurable.is_glb_of_nonempty {ΞΉ} (hΞΉ : nonempty ΞΉ)
{ΞΌ : measure Ξ΄} [encodable ΞΉ] {f : ΞΉ β Ξ΄ β Ξ±} {g : Ξ΄ β Ξ±}
(hf : β i, ae_measurable (f i) ΞΌ) (hg : βα΅ b βΞΌ, is_glb {a | β i, f i b = a} (g b)) :
ae_measurable g ΞΌ :=
begin
let p : Ξ΄ β (ΞΉ β Ξ±) β Prop := Ξ» x f', is_glb {a | β i, f' i = a} (g x),
let g_seq := Ξ» x, ite (x β ae_seq_set hf p) (g x) (β¨g xβ© : nonempty Ξ±).some,
have hg_seq : β b, is_glb {a | β i, ae_seq hf p i b = a} (g_seq b),
{ intro b,
haveI hΞ± : nonempty Ξ± := nonempty.map g β¨bβ©,
simp only [ae_seq, g_seq],
split_ifs,
{ have h_set_eq : {a : Ξ± | β (i : ΞΉ), (hf i).mk (f i) b = a} = {a : Ξ± | β (i : ΞΉ), f i b = a},
{ ext x,
simp_rw [set.mem_set_of_eq, ae_seq.mk_eq_fun_of_mem_ae_seq_set hf h], },
rw h_set_eq,
exact ae_seq.fun_prop_of_mem_ae_seq_set hf h, },
{ have h_singleton : {a : Ξ± | β (i : ΞΉ), hΞ±.some = a} = {hΞ±.some},
{ ext1 x,
exact β¨Ξ» hx, hx.some_spec.symm, Ξ» hx, β¨hΞΉ.some, hx.symmβ©β©, },
rw h_singleton,
exact is_glb_singleton, }, },
refine β¨g_seq, measurable.is_glb (ae_seq.measurable hf p) hg_seq, _β©,
exact (ite_ae_eq_of_measure_compl_zero g (Ξ» x, (β¨g xβ© : nonempty Ξ±).some) (ae_seq_set hf p)
(ae_seq.measure_compl_ae_seq_set_eq_zero hf hg)).symm,
end
lemma ae_measurable.is_glb {ΞΉ} {ΞΌ : measure Ξ΄} [encodable ΞΉ] {f : ΞΉ β Ξ΄ β Ξ±} {g : Ξ΄ β Ξ±}
(hf : β i, ae_measurable (f i) ΞΌ) (hg : βα΅ b βΞΌ, is_glb {a | β i, f i b = a} (g b)) :
ae_measurable g ΞΌ :=
begin
by_cases hΞΌ : ΞΌ = 0, { rw hΞΌ, exact ae_measurable_zero_measure },
haveI : ΞΌ.ae.ne_bot, { simpa [ne_bot_iff] },
by_cases hΞΉ : nonempty ΞΉ, { exact ae_measurable.is_glb_of_nonempty hΞΉ hf hg, },
suffices : β x, g =α΅[ΞΌ] Ξ» y, g x,
by { exact β¨(Ξ» y, g this.some), measurable_const, this.some_specβ©, },
have h_empty : β x, {a : Ξ± | β (i : ΞΉ), f i x = a} = β
,
{ intro x,
ext1 y,
rw [set.mem_set_of_eq, set.mem_empty_eq, iff_false],
exact Ξ» hi, hΞΉ (nonempty_of_exists hi), },
simp_rw h_empty at hg,
exact β¨hg.exists.some, hg.mono (Ξ» y hy, is_glb.unique hy hg.exists.some_spec)β©,
end
protected lemma monotone.measurable [linear_order Ξ²] [order_closed_topology Ξ²] {f : Ξ² β Ξ±}
(hf : monotone f) : measurable f :=
suffices h : β x, ord_connected (f β»ΒΉ' Ioi x),
from measurable_of_Ioi (Ξ» x, (h x).measurable_set),
Ξ» x, ord_connected_def.mpr (Ξ» a ha b hb c hc, lt_of_lt_of_le ha (hf hc.1))
lemma ae_measurable_restrict_of_monotone_on [linear_order Ξ²] [order_closed_topology Ξ²]
{ΞΌ : measure Ξ²} {s : set Ξ²} (hs : measurable_set s) {f : Ξ² β Ξ±} (hf : monotone_on f s) :
ae_measurable f (ΞΌ.restrict s) :=
have this : monotone (f β coe : s β Ξ±), from Ξ» β¨x, hxβ© β¨y, hyβ© (hxy : x β€ y), hf hx hy hxy,
ae_measurable_restrict_of_measurable_subtype hs this.measurable
protected lemma antitone.measurable [linear_order Ξ²] [order_closed_topology Ξ²] {f : Ξ² β Ξ±}
(hf : antitone f) :
measurable f :=
@monotone.measurable (order_dual Ξ±) Ξ² _ _ βΉ_βΊ _ _ _ _ _ βΉ_βΊ _ _ _ hf
lemma ae_measurable_restrict_of_antitone_on [linear_order Ξ²] [order_closed_topology Ξ²]
{ΞΌ : measure Ξ²} {s : set Ξ²} (hs : measurable_set s) {f : Ξ² β Ξ±} (hf : antitone_on f s) :
ae_measurable f (ΞΌ.restrict s) :=
@ae_measurable_restrict_of_monotone_on (order_dual Ξ±) Ξ² _ _ βΉ_βΊ _ _ _ _ _ βΉ_βΊ _ _ _ _ hs _ hf
end linear_order
@[measurability]
lemma measurable.supr_Prop {Ξ±} [measurable_space Ξ±] [complete_lattice Ξ±]
(p : Prop) {f : Ξ΄ β Ξ±} (hf : measurable f) :
measurable (Ξ» b, β¨ h : p, f b) :=
classical.by_cases
(assume h : p, begin convert hf, funext, exact supr_pos h end)
(assume h : Β¬p, begin convert measurable_const, funext, exact supr_neg h end)
@[measurability]
lemma measurable.infi_Prop {Ξ±} [measurable_space Ξ±] [complete_lattice Ξ±]
(p : Prop) {f : Ξ΄ β Ξ±} (hf : measurable f) :
measurable (Ξ» b, β¨
h : p, f b) :=
classical.by_cases
(assume h : p, begin convert hf, funext, exact infi_pos h end )
(assume h : Β¬p, begin convert measurable_const, funext, exact infi_neg h end)
section complete_linear_order
variables [complete_linear_order Ξ±] [order_topology Ξ±] [second_countable_topology Ξ±]
@[measurability]
lemma measurable_supr {ΞΉ} [encodable ΞΉ] {f : ΞΉ β Ξ΄ β Ξ±} (hf : β i, measurable (f i)) :
measurable (Ξ» b, β¨ i, f i b) :=
measurable.is_lub hf $ Ξ» b, is_lub_supr
@[measurability]
lemma ae_measurable_supr {ΞΉ} {ΞΌ : measure Ξ΄} [encodable ΞΉ] {f : ΞΉ β Ξ΄ β Ξ±}
(hf : β i, ae_measurable (f i) ΞΌ) :
ae_measurable (Ξ» b, β¨ i, f i b) ΞΌ :=
ae_measurable.is_lub hf $ (ae_of_all ΞΌ (Ξ» b, is_lub_supr))
@[measurability]
lemma measurable_infi {ΞΉ} [encodable ΞΉ] {f : ΞΉ β Ξ΄ β Ξ±} (hf : β i, measurable (f i)) :
measurable (Ξ» b, β¨
i, f i b) :=
measurable.is_glb hf $ Ξ» b, is_glb_infi
@[measurability]
lemma ae_measurable_infi {ΞΉ} {ΞΌ : measure Ξ΄} [encodable ΞΉ] {f : ΞΉ β Ξ΄ β Ξ±}
(hf : β i, ae_measurable (f i) ΞΌ) :
ae_measurable (Ξ» b, β¨
i, f i b) ΞΌ :=
ae_measurable.is_glb hf $ (ae_of_all ΞΌ (Ξ» b, is_glb_infi))
lemma measurable_bsupr {ΞΉ} (s : set ΞΉ) {f : ΞΉ β Ξ΄ β Ξ±} (hs : countable s)
(hf : β i, measurable (f i)) : measurable (Ξ» b, β¨ i β s, f i b) :=
by { haveI : encodable s := hs.to_encodable, simp only [supr_subtype'],
exact measurable_supr (Ξ» i, hf i) }
lemma ae_measurable_bsupr {ΞΉ} {ΞΌ : measure Ξ΄} (s : set ΞΉ) {f : ΞΉ β Ξ΄ β Ξ±} (hs : countable s)
(hf : β i, ae_measurable (f i) ΞΌ) : ae_measurable (Ξ» b, β¨ i β s, f i b) ΞΌ :=
begin
haveI : encodable s := hs.to_encodable,
simp only [supr_subtype'],
exact ae_measurable_supr (Ξ» i, hf i),
end
lemma measurable_binfi {ΞΉ} (s : set ΞΉ) {f : ΞΉ β Ξ΄ β Ξ±} (hs : countable s)
(hf : β i, measurable (f i)) : measurable (Ξ» b, β¨
i β s, f i b) :=
by { haveI : encodable s := hs.to_encodable, simp only [infi_subtype'],
exact measurable_infi (Ξ» i, hf i) }
lemma ae_measurable_binfi {ΞΉ} {ΞΌ : measure Ξ΄} (s : set ΞΉ) {f : ΞΉ β Ξ΄ β Ξ±} (hs : countable s)
(hf : β i, ae_measurable (f i) ΞΌ) : ae_measurable (Ξ» b, β¨
i β s, f i b) ΞΌ :=
begin
haveI : encodable s := hs.to_encodable,
simp only [infi_subtype'],
exact ae_measurable_infi (Ξ» i, hf i),
end
/-- `liminf` over a general filter is measurable. See `measurable_liminf` for the version over `β`.
-/
lemma measurable_liminf' {ΞΉ ΞΉ'} {f : ΞΉ β Ξ΄ β Ξ±} {u : filter ΞΉ} (hf : β i, measurable (f i))
{p : ΞΉ' β Prop} {s : ΞΉ' β set ΞΉ} (hu : u.has_countable_basis p s) (hs : β i, (s i).countable) :
measurable (Ξ» x, liminf u (Ξ» i, f i x)) :=
begin
simp_rw [hu.to_has_basis.liminf_eq_supr_infi],
refine measurable_bsupr _ hu.countable _,
exact Ξ» i, measurable_binfi _ (hs i) hf
end
/-- `limsup` over a general filter is measurable. See `measurable_limsup` for the version over `β`.
-/
lemma measurable_limsup' {ΞΉ ΞΉ'} {f : ΞΉ β Ξ΄ β Ξ±} {u : filter ΞΉ} (hf : β i, measurable (f i))
{p : ΞΉ' β Prop} {s : ΞΉ' β set ΞΉ} (hu : u.has_countable_basis p s) (hs : β i, (s i).countable) :
measurable (Ξ» x, limsup u (Ξ» i, f i x)) :=
begin
simp_rw [hu.to_has_basis.limsup_eq_infi_supr],
refine measurable_binfi _ hu.countable _,
exact Ξ» i, measurable_bsupr _ (hs i) hf
end
/-- `liminf` over `β` is measurable. See `measurable_liminf'` for a version with a general filter.
-/
@[measurability]
lemma measurable_liminf {f : β β Ξ΄ β Ξ±} (hf : β i, measurable (f i)) :
measurable (Ξ» x, liminf at_top (Ξ» i, f i x)) :=
measurable_liminf' hf at_top_countable_basis (Ξ» i, countable_encodable _)
/-- `limsup` over `β` is measurable. See `measurable_limsup'` for a version with a general filter.
-/
@[measurability]
lemma measurable_limsup {f : β β Ξ΄ β Ξ±} (hf : β i, measurable (f i)) :
measurable (Ξ» x, limsup at_top (Ξ» i, f i x)) :=
measurable_limsup' hf at_top_countable_basis (Ξ» i, countable_encodable _)
end complete_linear_order
section conditionally_complete_linear_order
variables [conditionally_complete_linear_order Ξ±] [order_topology Ξ±] [second_countable_topology Ξ±]
lemma measurable_cSup {ΞΉ} {f : ΞΉ β Ξ΄ β Ξ±} {s : set ΞΉ} (hs : s.countable)
(hf : β i, measurable (f i)) (bdd : β x, bdd_above ((Ξ» i, f i x) '' s)) :
measurable (Ξ» x, Sup ((Ξ» i, f i x) '' s)) :=
begin
cases eq_empty_or_nonempty s with h2s h2s,
{ simp [h2s, measurable_const] },
{ apply measurable_of_Iic, intro y,
simp_rw [preimage, mem_Iic, cSup_le_iff (bdd _) (h2s.image _), ball_image_iff, set_of_forall],
exact measurable_set.bInter hs (Ξ» i hi, measurable_set_le (hf i) measurable_const) }
end
end conditionally_complete_linear_order
/-- Convert a `homeomorph` to a `measurable_equiv`. -/
def homemorph.to_measurable_equiv (h : Ξ± ββ Ξ²) : Ξ± βα΅ Ξ² :=
{ to_equiv := h.to_equiv,
measurable_to_fun := h.continuous_to_fun.measurable,
measurable_inv_fun := h.continuous_inv_fun.measurable }
protected lemma is_finite_measure_on_compacts.map
{Ξ± : Type*} {m0 : measurable_space Ξ±} [topological_space Ξ±] [opens_measurable_space Ξ±]
{Ξ² : Type*} [measurable_space Ξ²] [topological_space Ξ²] [borel_space Ξ²]
[t2_space Ξ²] (ΞΌ : measure Ξ±) [is_finite_measure_on_compacts ΞΌ] (f : Ξ± ββ Ξ²) :
is_finite_measure_on_compacts (measure.map f ΞΌ) :=
β¨begin
assume K hK,
rw [measure.map_apply f.measurable hK.measurable_set],
apply is_compact.measure_lt_top,
rwa f.compact_preimage
endβ©
end borel_space
instance empty.borel_space : borel_space empty := β¨borel_eq_top_of_discrete.symmβ©
instance unit.borel_space : borel_space unit := β¨borel_eq_top_of_discrete.symmβ©
instance bool.borel_space : borel_space bool := β¨borel_eq_top_of_discrete.symmβ©
instance nat.borel_space : borel_space β := β¨borel_eq_top_of_discrete.symmβ©
instance int.borel_space : borel_space β€ := β¨borel_eq_top_of_discrete.symmβ©
instance rat.borel_space : borel_space β := β¨borel_eq_top_of_encodable.symmβ©
@[priority 900]
instance is_R_or_C.measurable_space {π : Type*} [is_R_or_C π] : measurable_space π := borel π
@[priority 900]
instance is_R_or_C.borel_space {π : Type*} [is_R_or_C π] : borel_space π := β¨rflβ©
/- Instances on `real` and `complex` are special cases of `is_R_or_C` but without these instances,
Lean fails to prove `borel_space (ΞΉ β β)`, so we leave them here. -/
instance real.measurable_space : measurable_space β := borel β
instance real.borel_space : borel_space β := β¨rflβ©
instance nnreal.measurable_space : measurable_space ββ₯0 := subtype.measurable_space
instance nnreal.borel_space : borel_space ββ₯0 := subtype.borel_space _
instance ennreal.measurable_space : measurable_space ββ₯0β := borel ββ₯0β
instance ennreal.borel_space : borel_space ββ₯0β := β¨rflβ©
instance ereal.measurable_space : measurable_space ereal := borel ereal
instance ereal.borel_space : borel_space ereal := β¨rflβ©
instance complex.measurable_space : measurable_space β := borel β
instance complex.borel_space : borel_space β := β¨rflβ©
/-- One can cut out `ββ₯0β` into the sets `{0}`, `Ico (t^n) (t^(n+1))` for `n : β€` and `{β}`. This
gives a way to compute the measure of a set in terms of sets on which a given function `f` does not
fluctuate by more than `t`. -/
lemma measure_eq_measure_preimage_add_measure_tsum_Ico_zpow [measurable_space Ξ±] (ΞΌ : measure Ξ±)
{f : Ξ± β ββ₯0β} (hf : measurable f) {s : set Ξ±} (hs : measurable_set s) {t : ββ₯0} (ht : 1 < t) :
ΞΌ s = ΞΌ (s β© fβ»ΒΉ' {0}) + ΞΌ (s β© fβ»ΒΉ' {β}) + β' (n : β€), ΞΌ (s β© fβ»ΒΉ' (Ico (t^n) (t^(n+1)))) :=
begin
have A : ΞΌ s = ΞΌ (s β© fβ»ΒΉ' {0}) + ΞΌ (s β© fβ»ΒΉ' (Ioi 0)),
{ rw β measure_union,
{ congr' 1,
ext x,
have : 0 = f x β¨ 0 < f x := eq_or_lt_of_le bot_le,
rw eq_comm at this,
simp only [βand_or_distrib_left, this, mem_singleton_iff, mem_inter_eq, and_true,
mem_union_eq, mem_Ioi, mem_preimage], },
{ apply disjoint_left.2 (Ξ» x hx h'x, _),
have : 0 < f x := h'x.2,
exact lt_irrefl 0 (this.trans_le hx.2.le) },
{ exact hs.inter (hf measurable_set_Ioi) } },
have B : ΞΌ (s β© fβ»ΒΉ' (Ioi 0)) = ΞΌ (s β© fβ»ΒΉ' {β}) + ΞΌ (s β© fβ»ΒΉ' (Ioo 0 β)),
{ rw β measure_union,
{ rw β inter_union_distrib_left,
congr,
ext x,
simp only [mem_singleton_iff, mem_union_eq, mem_Ioo, mem_Ioi, mem_preimage],
have H : f x = β β¨ f x < β := eq_or_lt_of_le le_top,
cases H,
{ simp only [H, eq_self_iff_true, or_false, with_top.zero_lt_top, not_top_lt, and_false] },
{ simp only [H, H.ne, and_true, false_or] } },
{ apply disjoint_left.2 (Ξ» x hx h'x, _),
have : f x < β := h'x.2.2,
exact lt_irrefl _ (this.trans_le (le_of_eq hx.2.symm)) },
{ exact hs.inter (hf measurable_set_Ioo) } },
have C : ΞΌ (s β© fβ»ΒΉ' (Ioo 0 β)) = β' (n : β€), ΞΌ (s β© fβ»ΒΉ' (Ico (t^n) (t^(n+1)))),
{ rw [β measure_Union, ennreal.Ioo_zero_top_eq_Union_Ico_zpow (ennreal.one_lt_coe_iff.2 ht)
ennreal.coe_ne_top, preimage_Union, inter_Union],
{ assume i j,
simp only [function.on_fun],
wlog h : i β€ j := le_total i j using [i j, j i] tactic.skip,
{ assume hij,
replace hij : i + 1 β€ j := lt_of_le_of_ne h hij,
apply disjoint_left.2 (Ξ» x hx h'x, lt_irrefl (f x) _),
calc f x < t ^ (i + 1) : hx.2.2
... β€ t ^ j : ennreal.zpow_le_of_le (ennreal.one_le_coe_iff.2 ht.le) hij
... β€ f x : h'x.2.1 },
{ assume hij,
rw disjoint.comm,
exact this hij.symm } },
{ assume n,
exact hs.inter (hf measurable_set_Ico) } },
rw [A, B, C, add_assoc],
end
section metric_space
variables [metric_space Ξ±] [measurable_space Ξ±] [opens_measurable_space Ξ±]
variables [measurable_space Ξ²] {x : Ξ±} {Ξ΅ : β}
open metric
@[measurability]
lemma measurable_set_ball : measurable_set (metric.ball x Ξ΅) :=
metric.is_open_ball.measurable_set
@[measurability]
lemma measurable_set_closed_ball : measurable_set (metric.closed_ball x Ξ΅) :=
metric.is_closed_ball.measurable_set
@[measurability]
lemma measurable_inf_dist {s : set Ξ±} : measurable (Ξ» x, inf_dist x s) :=
(continuous_inf_dist_pt s).measurable
@[measurability]
lemma measurable.inf_dist {f : Ξ² β Ξ±} (hf : measurable f) {s : set Ξ±} :
measurable (Ξ» x, inf_dist (f x) s) :=
measurable_inf_dist.comp hf
@[measurability]
lemma measurable_inf_nndist {s : set Ξ±} : measurable (Ξ» x, inf_nndist x s) :=
(continuous_inf_nndist_pt s).measurable
@[measurability]
lemma measurable.inf_nndist {f : Ξ² β Ξ±} (hf : measurable f) {s : set Ξ±} :
measurable (Ξ» x, inf_nndist (f x) s) :=
measurable_inf_nndist.comp hf
section
variables [second_countable_topology Ξ±]
@[measurability]
lemma measurable_dist : measurable (Ξ» p : Ξ± Γ Ξ±, dist p.1 p.2) :=
continuous_dist.measurable
@[measurability]
lemma measurable.dist {f g : Ξ² β Ξ±} (hf : measurable f) (hg : measurable g) :
measurable (Ξ» b, dist (f b) (g b)) :=
(@continuous_dist Ξ± _).measurable2 hf hg
@[measurability]
lemma measurable_nndist : measurable (Ξ» p : Ξ± Γ Ξ±, nndist p.1 p.2) :=
continuous_nndist.measurable
@[measurability]
lemma measurable.nndist {f g : Ξ² β Ξ±} (hf : measurable f) (hg : measurable g) :
measurable (Ξ» b, nndist (f b) (g b)) :=
(@continuous_nndist Ξ± _).measurable2 hf hg
end
/-- If a set has a closed thickening with finite measure, then the measure of its `r`-closed
thickenings converges to the measure of its closure as `r` tends to `0`. -/
lemma tendsto_measure_cthickening {ΞΌ : measure Ξ±} {s : set Ξ±}
(hs : β R > 0, ΞΌ (cthickening R s) β β) :
tendsto (Ξ» r, ΞΌ (cthickening r s)) (π 0) (π (ΞΌ (closure s))) :=
begin
have A : tendsto (Ξ» r, ΞΌ (cthickening r s)) (π[Ioi 0] 0) (π (ΞΌ (closure s))),
{ rw closure_eq_Inter_cthickening,
exact tendsto_measure_bInter_gt (Ξ» r hr, is_closed_cthickening.measurable_set)
(Ξ» i j ipos ij, cthickening_mono ij _) hs },
have B : tendsto (Ξ» r, ΞΌ (cthickening r s)) (π[Iic 0] 0) (π (ΞΌ (closure s))),
{ apply tendsto.congr' _ tendsto_const_nhds,
filter_upwards [self_mem_nhds_within] with _ hr,
rw cthickening_of_nonpos hr, },
convert B.sup A,
exact (nhds_left_sup_nhds_right' 0).symm,
end
/-- If a closed set has a closed thickening with finite measure, then the measure of its `r`-closed
thickenings converges to its measure as `r` tends to `0`. -/
lemma tendsto_measure_cthickening_of_is_closed {ΞΌ : measure Ξ±} {s : set Ξ±}
(hs : β R > 0, ΞΌ (cthickening R s) β β) (h's : is_closed s) :
tendsto (Ξ» r, ΞΌ (cthickening r s)) (π 0) (π (ΞΌ s)) :=
begin
convert tendsto_measure_cthickening hs,
exact h's.closure_eq.symm
end
/-- Given a compact set in a proper space, the measure of its `r`-closed thickenings converges to
its measure as `r` tends to `0`. -/
lemma tendsto_measure_cthickening_of_is_compact [proper_space Ξ±] {ΞΌ : measure Ξ±}
[is_finite_measure_on_compacts ΞΌ] {s : set Ξ±} (hs : is_compact s) :
tendsto (Ξ» r, ΞΌ (cthickening r s)) (π 0) (π (ΞΌ s)) :=
tendsto_measure_cthickening_of_is_closed
β¨1, zero_lt_one, (bounded.measure_lt_top hs.bounded.cthickening).neβ© hs.is_closed
end metric_space
section emetric_space
variables [emetric_space Ξ±] [measurable_space Ξ±] [opens_measurable_space Ξ±]
variables [measurable_space Ξ²] {x : Ξ±} {Ξ΅ : ββ₯0β}
open emetric
@[measurability]
lemma measurable_set_eball : measurable_set (emetric.ball x Ξ΅) :=
emetric.is_open_ball.measurable_set
@[measurability]
lemma measurable_edist_right : measurable (edist x) :=
(continuous_const.edist continuous_id).measurable
@[measurability]
lemma measurable_edist_left : measurable (Ξ» y, edist y x) :=
(continuous_id.edist continuous_const).measurable
@[measurability]
lemma measurable_inf_edist {s : set Ξ±} : measurable (Ξ» x, inf_edist x s) :=
continuous_inf_edist.measurable
@[measurability]
lemma measurable.inf_edist {f : Ξ² β Ξ±} (hf : measurable f) {s : set Ξ±} :
measurable (Ξ» x, inf_edist (f x) s) :=
measurable_inf_edist.comp hf
variables [second_countable_topology Ξ±]
@[measurability]
lemma measurable_edist : measurable (Ξ» p : Ξ± Γ Ξ±, edist p.1 p.2) :=
continuous_edist.measurable
@[measurability]
lemma measurable.edist {f g : Ξ² β Ξ±} (hf : measurable f) (hg : measurable g) :
measurable (Ξ» b, edist (f b) (g b)) :=
(@continuous_edist Ξ± _).measurable2 hf hg
@[measurability]
lemma ae_measurable.edist {f g : Ξ² β Ξ±} {ΞΌ : measure Ξ²}
(hf : ae_measurable f ΞΌ) (hg : ae_measurable g ΞΌ) : ae_measurable (Ξ» a, edist (f a) (g a)) ΞΌ :=
(@continuous_edist Ξ± _).ae_measurable2 hf hg
end emetric_space
namespace real
open measurable_space measure_theory
lemma borel_eq_generate_from_Ioo_rat :
borel β = generate_from (β(a b : β) (h : a < b), {Ioo a b}) :=
is_topological_basis_Ioo_rat.borel_eq_generate_from
lemma is_pi_system_Ioo_rat : @is_pi_system β (β (a b : β) (h : a < b), {Ioo a b}) :=
begin
convert is_pi_system_Ioo (coe : β β β) (coe : β β β),
ext x,
simp [eq_comm]
end
/-- The intervals `(-(n + 1), (n + 1))` form a finite spanning sets in the set of open intervals
with rational endpoints for a locally finite measure `ΞΌ` on `β`. -/
def finite_spanning_sets_in_Ioo_rat (ΞΌ : measure β) [is_locally_finite_measure ΞΌ] :
ΞΌ.finite_spanning_sets_in (β (a b : β) (h : a < b), {Ioo a b}) :=
{ set := Ξ» n, Ioo (-(n + 1)) (n + 1),
set_mem := Ξ» n,
begin
simp only [mem_Union, mem_singleton_iff],
refine β¨-(n + 1), n + 1, _, by norm_castβ©,
exact (neg_nonpos.2 (@nat.cast_nonneg β _ (n + 1))).trans_lt n.cast_add_one_pos
end,
finite := Ξ» n, measure_Ioo_lt_top,
spanning := Union_eq_univ_iff.2 $ Ξ» x,
β¨β|x|ββ, neg_lt.1 ((neg_le_abs_self x).trans_lt (nat.lt_floor_add_one _)),
(le_abs_self x).trans_lt (nat.lt_floor_add_one _)β© }
lemma measure_ext_Ioo_rat {ΞΌ Ξ½ : measure β} [is_locally_finite_measure ΞΌ]
(h : β a b : β, ΞΌ (Ioo a b) = Ξ½ (Ioo a b)) : ΞΌ = Ξ½ :=
(finite_spanning_sets_in_Ioo_rat ΞΌ).ext borel_eq_generate_from_Ioo_rat is_pi_system_Ioo_rat $
by { simp only [mem_Union, mem_singleton_iff], rintro _ β¨a, b, -, rflβ©, apply h }
lemma borel_eq_generate_from_Iio_rat :
borel β = generate_from (β a : β, {Iio a}) :=
begin
let g : measurable_space β := generate_from (β a : β, {Iio a}),
refine le_antisymm _ _,
{ rw borel_eq_generate_from_Ioo_rat,
refine generate_from_le (Ξ» t, _),
simp only [mem_Union, mem_singleton_iff], rintro β¨a, b, h, rflβ©,
rw (set.ext (Ξ» x, _) : Ioo (a : β) b = (βc>a, (Iio c)αΆ) β© Iio b),
{ have hg : β q : β, g.measurable_set' (Iio q) :=
Ξ» q, generate_measurable.basic (Iio q) (by simp),
refine @measurable_set.inter _ g _ _ _ (hg _),
refine @measurable_set.bUnion _ _ g _ _ (countable_encodable _) (Ξ» c h, _),
exact @measurable_set.compl _ _ g (hg _) },
{ suffices : x < βb β (βa < x β β (i : β), a < i β§ βi β€ x), by simpa,
refine Ξ» _, β¨Ξ» h, _, Ξ» β¨i, hai, hixβ©, (rat.cast_lt.2 hai).trans_le hixβ©,
rcases exists_rat_btwn h with β¨c, ac, cxβ©,
exact β¨c, rat.cast_lt.1 ac, cx.leβ© } },
{ refine measurable_space.generate_from_le (Ξ» _, _),
simp only [mem_Union, mem_singleton_iff], rintro β¨r, rflβ©, exact measurable_set_Iio }
end
end real
variable [measurable_space Ξ±]
@[measurability]
lemma measurable_real_to_nnreal : measurable (real.to_nnreal) :=
nnreal.continuous_of_real.measurable
@[measurability]
lemma measurable.real_to_nnreal {f : Ξ± β β} (hf : measurable f) :
measurable (Ξ» x, real.to_nnreal (f x)) :=
measurable_real_to_nnreal.comp hf
@[measurability]
lemma ae_measurable.real_to_nnreal {f : Ξ± β β} {ΞΌ : measure Ξ±} (hf : ae_measurable f ΞΌ) :
ae_measurable (Ξ» x, real.to_nnreal (f x)) ΞΌ :=
measurable_real_to_nnreal.comp_ae_measurable hf
@[measurability]
lemma measurable_coe_nnreal_real : measurable (coe : ββ₯0 β β) :=
nnreal.continuous_coe.measurable
@[measurability]
lemma measurable.coe_nnreal_real {f : Ξ± β ββ₯0} (hf : measurable f) :
measurable (Ξ» x, (f x : β)) :=
measurable_coe_nnreal_real.comp hf
@[measurability]
lemma ae_measurable.coe_nnreal_real {f : Ξ± β ββ₯0} {ΞΌ : measure Ξ±} (hf : ae_measurable f ΞΌ) :
ae_measurable (Ξ» x, (f x : β)) ΞΌ :=
measurable_coe_nnreal_real.comp_ae_measurable hf
@[measurability]
lemma measurable_coe_nnreal_ennreal : measurable (coe : ββ₯0 β ββ₯0β) :=
ennreal.continuous_coe.measurable
@[measurability]
lemma measurable.coe_nnreal_ennreal {f : Ξ± β ββ₯0} (hf : measurable f) :
measurable (Ξ» x, (f x : ββ₯0β)) :=
ennreal.continuous_coe.measurable.comp hf
@[measurability]
lemma ae_measurable.coe_nnreal_ennreal {f : Ξ± β ββ₯0} {ΞΌ : measure Ξ±} (hf : ae_measurable f ΞΌ) :
ae_measurable (Ξ» x, (f x : ββ₯0β)) ΞΌ :=
ennreal.continuous_coe.measurable.comp_ae_measurable hf
@[measurability]
lemma measurable.ennreal_of_real {f : Ξ± β β} (hf : measurable f) :
measurable (Ξ» x, ennreal.of_real (f x)) :=
ennreal.continuous_of_real.measurable.comp hf
/-- The set of finite `ββ₯0β` numbers is `measurable_equiv` to `ββ₯0`. -/
def measurable_equiv.ennreal_equiv_nnreal : {r : ββ₯0β | r β β} βα΅ ββ₯0 :=
ennreal.ne_top_homeomorph_nnreal.to_measurable_equiv
namespace ennreal
lemma measurable_of_measurable_nnreal {f : ββ₯0β β Ξ±}
(h : measurable (Ξ» p : ββ₯0, f p)) : measurable f :=
measurable_of_measurable_on_compl_singleton β
(measurable_equiv.ennreal_equiv_nnreal.symm.measurable_comp_iff.1 h)
/-- `ββ₯0β` is `measurable_equiv` to `ββ₯0 β unit`. -/
def ennreal_equiv_sum : ββ₯0β βα΅ ββ₯0 β unit :=
{ measurable_to_fun := measurable_of_measurable_nnreal measurable_inl,
measurable_inv_fun := measurable_sum measurable_coe_nnreal_ennreal
(@measurable_const ββ₯0β unit _ _ β),
.. equiv.option_equiv_sum_punit ββ₯0 }
open function (uncurry)
lemma measurable_of_measurable_nnreal_prod [measurable_space Ξ²] [measurable_space Ξ³]
{f : ββ₯0β Γ Ξ² β Ξ³} (Hβ : measurable (Ξ» p : ββ₯0 Γ Ξ², f (p.1, p.2)))
(Hβ : measurable (Ξ» x, f (β, x))) :
measurable f :=
let e : ββ₯0β Γ Ξ² βα΅ ββ₯0 Γ Ξ² β unit Γ Ξ² :=
(ennreal_equiv_sum.prod_congr (measurable_equiv.refl Ξ²)).trans
(measurable_equiv.sum_prod_distrib _ _ _) in
e.symm.measurable_comp_iff.1 $ measurable_sum Hβ (Hβ.comp measurable_id.snd)
lemma measurable_of_measurable_nnreal_nnreal [measurable_space Ξ²]
{f : ββ₯0β Γ ββ₯0β β Ξ²} (hβ : measurable (Ξ» p : ββ₯0 Γ ββ₯0, f (p.1, p.2)))
(hβ : measurable (Ξ» r : ββ₯0, f (β, r))) (hβ : measurable (Ξ» r : ββ₯0, f (r, β))) :
measurable f :=
measurable_of_measurable_nnreal_prod
(measurable_swap_iff.1 $ measurable_of_measurable_nnreal_prod (hβ.comp measurable_swap) hβ)
(measurable_of_measurable_nnreal hβ)
@[measurability]
lemma measurable_of_real : measurable ennreal.of_real :=
ennreal.continuous_of_real.measurable
@[measurability]
lemma measurable_to_real : measurable ennreal.to_real :=
ennreal.measurable_of_measurable_nnreal measurable_coe_nnreal_real
@[measurability]
lemma measurable_to_nnreal : measurable ennreal.to_nnreal :=
ennreal.measurable_of_measurable_nnreal measurable_id
instance : has_measurable_mulβ ββ₯0β :=
begin
refine β¨measurable_of_measurable_nnreal_nnreal _ _ _β©,
{ simp only [β ennreal.coe_mul, measurable_mul.coe_nnreal_ennreal] },
{ simp only [ennreal.top_mul, ennreal.coe_eq_zero],
exact measurable_const.piecewise (measurable_set_singleton _) measurable_const },
{ simp only [ennreal.mul_top, ennreal.coe_eq_zero],
exact measurable_const.piecewise (measurable_set_singleton _) measurable_const }
end
instance : has_measurable_subβ ββ₯0β :=
β¨by apply measurable_of_measurable_nnreal_nnreal;
simp [β with_top.coe_sub, continuous_sub.measurable.coe_nnreal_ennreal]β©
instance : has_measurable_inv ββ₯0β := β¨ennreal.continuous_inv.measurableβ©
end ennreal
@[measurability]
lemma measurable.ennreal_to_nnreal {f : Ξ± β ββ₯0β} (hf : measurable f) :
measurable (Ξ» x, (f x).to_nnreal) :=
ennreal.measurable_to_nnreal.comp hf
@[measurability]
lemma ae_measurable.ennreal_to_nnreal {f : Ξ± β ββ₯0β} {ΞΌ : measure Ξ±} (hf : ae_measurable f ΞΌ) :
ae_measurable (Ξ» x, (f x).to_nnreal) ΞΌ :=
ennreal.measurable_to_nnreal.comp_ae_measurable hf
lemma measurable_coe_nnreal_ennreal_iff {f : Ξ± β ββ₯0} :
measurable (Ξ» x, (f x : ββ₯0β)) β measurable f :=
β¨Ξ» h, h.ennreal_to_nnreal, Ξ» h, h.coe_nnreal_ennrealβ©
@[measurability]
lemma measurable.ennreal_to_real {f : Ξ± β ββ₯0β} (hf : measurable f) :
measurable (Ξ» x, ennreal.to_real (f x)) :=
ennreal.measurable_to_real.comp hf
@[measurability]
lemma ae_measurable.ennreal_to_real {f : Ξ± β ββ₯0β} {ΞΌ : measure Ξ±} (hf : ae_measurable f ΞΌ) :
ae_measurable (Ξ» x, ennreal.to_real (f x)) ΞΌ :=
ennreal.measurable_to_real.comp_ae_measurable hf
/-- note: `ββ₯0β` can probably be generalized in a future version of this lemma. -/
@[measurability]
lemma measurable.ennreal_tsum {ΞΉ} [encodable ΞΉ] {f : ΞΉ β Ξ± β ββ₯0β} (h : β i, measurable (f i)) :
measurable (Ξ» x, β' i, f i x) :=
by { simp_rw [ennreal.tsum_eq_supr_sum], apply measurable_supr,
exact Ξ» s, s.measurable_sum (Ξ» i _, h i) }
@[measurability]
lemma measurable.ennreal_tsum' {ΞΉ} [encodable ΞΉ] {f : ΞΉ β Ξ± β ββ₯0β} (h : β i, measurable (f i)) :
measurable (β' i, f i) :=
begin
convert measurable.ennreal_tsum h,
ext1 x,
exact tsum_apply (pi.summable.2 (Ξ» _, ennreal.summable)),
end
@[measurability]
lemma measurable.nnreal_tsum {ΞΉ} [encodable ΞΉ] {f : ΞΉ β Ξ± β ββ₯0} (h : β i, measurable (f i)) :
measurable (Ξ» x, β' i, f i x) :=
begin
simp_rw [nnreal.tsum_eq_to_nnreal_tsum],
exact (measurable.ennreal_tsum (Ξ» i, (h i).coe_nnreal_ennreal)).ennreal_to_nnreal,
end
@[measurability]
lemma ae_measurable.ennreal_tsum {ΞΉ} [encodable ΞΉ] {f : ΞΉ β Ξ± β ββ₯0β} {ΞΌ : measure Ξ±}
(h : β i, ae_measurable (f i) ΞΌ) :
ae_measurable (Ξ» x, β' i, f i x) ΞΌ :=
by { simp_rw [ennreal.tsum_eq_supr_sum], apply ae_measurable_supr,
exact Ξ» s, finset.ae_measurable_sum s (Ξ» i _, h i) }
@[measurability]
lemma measurable_coe_real_ereal : measurable (coe : β β ereal) :=
continuous_coe_real_ereal.measurable
@[measurability]
lemma measurable.coe_real_ereal {f : Ξ± β β} (hf : measurable f) :
measurable (Ξ» x, (f x : ereal)) :=
measurable_coe_real_ereal.comp hf
@[measurability]
lemma ae_measurable.coe_real_ereal {f : Ξ± β β} {ΞΌ : measure Ξ±} (hf : ae_measurable f ΞΌ) :
ae_measurable (Ξ» x, (f x : ereal)) ΞΌ :=
measurable_coe_real_ereal.comp_ae_measurable hf
/-- The set of finite `ereal` numbers is `measurable_equiv` to `β`. -/
def measurable_equiv.ereal_equiv_real : ({β₯, β€} : set ereal).compl βα΅ β :=
ereal.ne_bot_top_homeomorph_real.to_measurable_equiv
lemma ereal.measurable_of_measurable_real {f : ereal β Ξ±}
(h : measurable (Ξ» p : β, f p)) : measurable f :=
measurable_of_measurable_on_compl_finite {β₯, β€} (by simp)
(measurable_equiv.ereal_equiv_real.symm.measurable_comp_iff.1 h)
@[measurability]
lemma measurable_ereal_to_real : measurable ereal.to_real :=
ereal.measurable_of_measurable_real (by simpa using measurable_id)
@[measurability]
lemma measurable.ereal_to_real {f : Ξ± β ereal} (hf : measurable f) :
measurable (Ξ» x, (f x).to_real) :=
measurable_ereal_to_real.comp hf
@[measurability]
lemma ae_measurable.ereal_to_real {f : Ξ± β ereal} {ΞΌ : measure Ξ±} (hf : ae_measurable f ΞΌ) :
ae_measurable (Ξ» x, (f x).to_real) ΞΌ :=
measurable_ereal_to_real.comp_ae_measurable hf
@[measurability]
lemma measurable_coe_ennreal_ereal : measurable (coe : ββ₯0β β ereal) :=
continuous_coe_ennreal_ereal.measurable
@[measurability]
lemma measurable.coe_ereal_ennreal {f : Ξ± β ββ₯0β} (hf : measurable f) :
measurable (Ξ» x, (f x : ereal)) :=
measurable_coe_ennreal_ereal.comp hf
@[measurability]
lemma ae_measurable.coe_ereal_ennreal {f : Ξ± β ββ₯0β} {ΞΌ : measure Ξ±} (hf : ae_measurable f ΞΌ) :
ae_measurable (Ξ» x, (f x : ereal)) ΞΌ :=
measurable_coe_ennreal_ereal.comp_ae_measurable hf
section normed_group
variables [normed_group Ξ±] [opens_measurable_space Ξ±] [measurable_space Ξ²]
@[measurability]
lemma measurable_norm : measurable (norm : Ξ± β β) :=
continuous_norm.measurable
@[measurability]
lemma measurable.norm {f : Ξ² β Ξ±} (hf : measurable f) : measurable (Ξ» a, norm (f a)) :=
measurable_norm.comp hf
@[measurability]
lemma ae_measurable.norm {f : Ξ² β Ξ±} {ΞΌ : measure Ξ²} (hf : ae_measurable f ΞΌ) :
ae_measurable (Ξ» a, norm (f a)) ΞΌ :=
measurable_norm.comp_ae_measurable hf
@[measurability]
lemma measurable_nnnorm : measurable (nnnorm : Ξ± β ββ₯0) :=
continuous_nnnorm.measurable
@[measurability]
lemma measurable.nnnorm {f : Ξ² β Ξ±} (hf : measurable f) : measurable (Ξ» a, nnnorm (f a)) :=
measurable_nnnorm.comp hf
@[measurability]
lemma ae_measurable.nnnorm {f : Ξ² β Ξ±} {ΞΌ : measure Ξ²} (hf : ae_measurable f ΞΌ) :
ae_measurable (Ξ» a, nnnorm (f a)) ΞΌ :=
measurable_nnnorm.comp_ae_measurable hf
@[measurability]
lemma measurable_ennnorm : measurable (Ξ» x : Ξ±, (nnnorm x : ββ₯0β)) :=
measurable_nnnorm.coe_nnreal_ennreal
@[measurability]
lemma measurable.ennnorm {f : Ξ² β Ξ±} (hf : measurable f) :
measurable (Ξ» a, (nnnorm (f a) : ββ₯0β)) :=
hf.nnnorm.coe_nnreal_ennreal
@[measurability]
lemma ae_measurable.ennnorm {f : Ξ² β Ξ±} {ΞΌ : measure Ξ²} (hf : ae_measurable f ΞΌ) :
ae_measurable (Ξ» a, (nnnorm (f a) : ββ₯0β)) ΞΌ :=
measurable_ennnorm.comp_ae_measurable hf
end normed_group
section limits
variables [measurable_space Ξ²] [metric_space Ξ²] [borel_space Ξ²]
open metric
/-- A limit (over a general filter) of measurable `ββ₯0` valued functions is measurable. -/
lemma measurable_of_tendsto_nnreal' {ΞΉ} {f : ΞΉ β Ξ± β ββ₯0} {g : Ξ± β ββ₯0} (u : filter ΞΉ)
[ne_bot u] [is_countably_generated u] (hf : β i, measurable (f i)) (lim : tendsto f u (π g)) :
measurable g :=
begin
rcases u.exists_seq_tendsto with β¨x, hxβ©,
rw [tendsto_pi_nhds] at lim, rw [β measurable_coe_nnreal_ennreal_iff],
have : β y, liminf at_top (Ξ» n, (f (x n) y : ββ₯0β)) = (g y : ββ₯0β) :=
Ξ» y, ((ennreal.continuous_coe.tendsto (g y)).comp $ (lim y).comp hx).liminf_eq,
simp only [β this],
show measurable (Ξ» y, liminf at_top (Ξ» n, (f (x n) y : ββ₯0β))),
exact measurable_liminf (Ξ» n, (hf (x n)).coe_nnreal_ennreal),
end
/-- A sequential limit of measurable `ββ₯0` valued functions is measurable. -/
lemma measurable_of_tendsto_nnreal {f : β β Ξ± β ββ₯0} {g : Ξ± β ββ₯0}
(hf : β i, measurable (f i)) (lim : tendsto f at_top (π g)) : measurable g :=
measurable_of_tendsto_nnreal' at_top hf lim
/-- A limit (over a general filter) of measurable functions valued in a metric space is measurable.
The assumption `hs` can be dropped using `filter.is_countably_generated.has_antitone_basis`, but we
don't need that case yet. -/
lemma measurable_of_tendsto_metric' {ΞΉ} {f : ΞΉ β Ξ± β Ξ²} {g : Ξ± β Ξ²}
(u : filter ΞΉ) [ne_bot u] [is_countably_generated u]
(hf : β i, measurable (f i)) (lim : tendsto f u (π g)) :
measurable g :=
begin
apply measurable_of_is_closed', intros s h1s h2s h3s,
have : measurable (Ξ» x, inf_nndist (g x) s),
{ suffices : tendsto (Ξ» i x, inf_nndist (f i x) s) u (π (Ξ» x, inf_nndist (g x) s)),
from measurable_of_tendsto_nnreal' u (Ξ» i, (hf i).inf_nndist) this,
rw [tendsto_pi_nhds] at lim β’, intro x,
exact ((continuous_inf_nndist_pt s).tendsto (g x)).comp (lim x) },
have h4s : g β»ΒΉ' s = (Ξ» x, inf_nndist (g x) s) β»ΒΉ' {0},
{ ext x, simp [h1s, β h1s.mem_iff_inf_dist_zero h2s, β nnreal.coe_eq_zero] },
rw [h4s], exact this (measurable_set_singleton 0),
end
/-- A sequential limit of measurable functions valued in a metric space is measurable. -/
lemma measurable_of_tendsto_metric {f : β β Ξ± β Ξ²} {g : Ξ± β Ξ²}
(hf : β i, measurable (f i)) (lim : tendsto f at_top (π g)) :
measurable g :=
measurable_of_tendsto_metric' at_top hf lim
lemma ae_measurable_of_tendsto_metric_ae {ΞΌ : measure Ξ±} {f : β β Ξ± β Ξ²} {g : Ξ± β Ξ²}
(hf : β n, ae_measurable (f n) ΞΌ)
(h_ae_tendsto : βα΅ x βΞΌ, tendsto (Ξ» n, f n x) at_top (π (g x))) :
ae_measurable g ΞΌ :=
begin
let p : Ξ± β (β β Ξ²) β Prop := Ξ» x f', filter.at_top.tendsto (Ξ» n, f' n) (π (g x)),
let hp : βα΅ x βΞΌ, p x (Ξ» n, f n x), from h_ae_tendsto,
let ae_seq_lim := Ξ» x, ite (x β ae_seq_set hf p) (g x) (β¨f 0 xβ© : nonempty Ξ²).some,
refine β¨ae_seq_lim, _, (ite_ae_eq_of_measure_compl_zero g (Ξ» x, (β¨f 0 xβ© : nonempty Ξ²).some)
(ae_seq_set hf p) (ae_seq.measure_compl_ae_seq_set_eq_zero hf hp)).symmβ©,
refine measurable_of_tendsto_metric (@ae_seq.measurable Ξ± Ξ² _ _ _ f ΞΌ hf p) _,
refine tendsto_pi_nhds.mpr (Ξ» x, _),
simp_rw [ae_seq, ae_seq_lim],
split_ifs with hx,
{ simp_rw ae_seq.mk_eq_fun_of_mem_ae_seq_set hf hx,
exact @ae_seq.fun_prop_of_mem_ae_seq_set Ξ± Ξ² _ _ _ _ _ _ hf x hx, },
{ exact tendsto_const_nhds, },
end
lemma ae_measurable_of_unif_approx {ΞΌ : measure Ξ±} {g : Ξ± β Ξ²}
(hf : β Ξ΅ > (0 : β), β (f : Ξ± β Ξ²), ae_measurable f ΞΌ β§ βα΅ x βΞΌ, dist (f x) (g x) β€ Ξ΅) :
ae_measurable g ΞΌ :=
begin
obtain β¨u, u_anti, u_pos, u_limβ© :
β (u : β β β), strict_anti u β§ (β (n : β), 0 < u n) β§ tendsto u at_top (π 0) :=
exists_seq_strict_anti_tendsto (0 : β),
choose f Hf using Ξ» (n : β), hf (u n) (u_pos n),
have : βα΅ x βΞΌ, tendsto (Ξ» n, f n x) at_top (π (g x)),
{ have : βα΅ x β ΞΌ, β n, dist (f n x) (g x) β€ u n := ae_all_iff.2 (Ξ» n, (Hf n).2),
filter_upwards [this],
assume x hx,
rw tendsto_iff_dist_tendsto_zero,
exact squeeze_zero (Ξ» n, dist_nonneg) hx u_lim },
exact ae_measurable_of_tendsto_metric_ae (Ξ» n, (Hf n).1) this,
end
lemma measurable_of_tendsto_metric_ae {ΞΌ : measure Ξ±} [ΞΌ.is_complete] {f : β β Ξ± β Ξ²} {g : Ξ± β Ξ²}
(hf : β n, measurable (f n))
(h_ae_tendsto : βα΅ x βΞΌ, filter.at_top.tendsto (Ξ» n, f n x) (π (g x))) :
measurable g :=
ae_measurable_iff_measurable.mp
(ae_measurable_of_tendsto_metric_ae (Ξ» i, (hf i).ae_measurable) h_ae_tendsto)
lemma measurable_limit_of_tendsto_metric_ae {ΞΌ : measure Ξ±} {f : β β Ξ± β Ξ²}
(hf : β n, ae_measurable (f n) ΞΌ)
(h_ae_tendsto : βα΅ x βΞΌ, β l : Ξ², filter.at_top.tendsto (Ξ» n, f n x) (π l)) :
β (f_lim : Ξ± β Ξ²) (hf_lim_meas : measurable f_lim),
βα΅ x βΞΌ, filter.at_top.tendsto (Ξ» n, f n x) (π (f_lim x)) :=
begin
let p : Ξ± β (β β Ξ²) β Prop := Ξ» x f', β l : Ξ², filter.at_top.tendsto (Ξ» n, f' n) (π l),
have hp_mem : β x, x β ae_seq_set hf p β p x (Ξ» n, f n x),
from Ξ» x hx, ae_seq.fun_prop_of_mem_ae_seq_set hf hx,
have hΞΌ_compl : ΞΌ (ae_seq_set hf p)αΆ = 0,
from ae_seq.measure_compl_ae_seq_set_eq_zero hf h_ae_tendsto,
let f_lim : Ξ± β Ξ² := Ξ» x, dite (x β ae_seq_set hf p) (Ξ» h, (hp_mem x h).some)
(Ξ» h, (β¨f 0 xβ© : nonempty Ξ²).some),
have hf_lim_conv : β x, x β ae_seq_set hf p β filter.at_top.tendsto (Ξ» n, f n x) (π (f_lim x)),
{ intros x hx_conv,
simp only [f_lim, hx_conv, dif_pos],
exact (hp_mem x hx_conv).some_spec, },
have hf_lim : β x, filter.at_top.tendsto (Ξ» n, ae_seq hf p n x) (π (f_lim x)),
{ intros x,
simp only [f_lim, ae_seq],
split_ifs,
{ rw funext (Ξ» n, ae_seq.mk_eq_fun_of_mem_ae_seq_set hf h n),
exact (hp_mem x h).some_spec, },
{ exact tendsto_const_nhds, }, },
have h_ae_tendsto_f_lim : βα΅ x βΞΌ, filter.at_top.tendsto (Ξ» n, f n x) (π (f_lim x)),
{ refine le_antisymm (le_of_eq (measure_mono_null _ hΞΌ_compl)) (zero_le _),
exact set.compl_subset_compl.mpr (Ξ» x hx, hf_lim_conv x hx), },
have h_f_lim_meas : measurable f_lim,
from measurable_of_tendsto_metric (ae_seq.measurable hf p)
(tendsto_pi_nhds.mpr (Ξ» x, hf_lim x)),
exact β¨f_lim, h_f_lim_meas, h_ae_tendsto_f_limβ©,
end
end limits
namespace continuous_linear_map
variables {π : Type*} [normed_field π]
variables {E : Type*} [normed_group E] [normed_space π E] [measurable_space E]
variables [opens_measurable_space E]
variables {F : Type*} [normed_group F] [normed_space π F] [measurable_space F] [borel_space F]
@[measurability]
protected lemma measurable (L : E βL[π] F) : measurable L :=
L.continuous.measurable
lemma measurable_comp (L : E βL[π] F) {Ο : Ξ± β E} (Ο_meas : measurable Ο) :
measurable (Ξ» (a : Ξ±), L (Ο a)) :=
L.measurable.comp Ο_meas
end continuous_linear_map
namespace continuous_linear_map
variables {π : Type*} [nondiscrete_normed_field π]
variables {E : Type*} [normed_group E] [normed_space π E]
{F : Type*} [normed_group F] [normed_space π F]
instance : measurable_space (E βL[π] F) := borel _
instance : borel_space (E βL[π] F) := β¨rflβ©
@[measurability]
lemma measurable_apply [measurable_space F] [borel_space F] (x : E) :
measurable (Ξ» f : E βL[π] F, f x) :=
(apply π F x).continuous.measurable
@[measurability]
lemma measurable_apply' [measurable_space E] [opens_measurable_space E]
[measurable_space F] [borel_space F] :
measurable (Ξ» (x : E) (f : E βL[π] F), f x) :=
measurable_pi_lambda _ $ Ξ» f, f.measurable
@[measurability]
lemma measurable_coe [measurable_space F] [borel_space F] :
measurable (Ξ» (f : E βL[π] F) (x : E), f x) :=
measurable_pi_lambda _ measurable_apply
end continuous_linear_map
section continuous_linear_map_nondiscrete_normed_field
variables {π : Type*} [nondiscrete_normed_field π]
variables {E : Type*} [normed_group E] [normed_space π E] [measurable_space E] [borel_space E]
variables {F : Type*} [normed_group F] [normed_space π F]
@[measurability]
lemma measurable.apply_continuous_linear_map {Ο : Ξ± β F βL[π] E} (hΟ : measurable Ο) (v : F) :
measurable (Ξ» a, Ο a v) :=
(continuous_linear_map.apply π E v).measurable.comp hΟ
@[measurability]
lemma ae_measurable.apply_continuous_linear_map {Ο : Ξ± β F βL[π] E} {ΞΌ : measure Ξ±}
(hΟ : ae_measurable Ο ΞΌ) (v : F) : ae_measurable (Ξ» a, Ο a v) ΞΌ :=
(continuous_linear_map.apply π E v).measurable.comp_ae_measurable hΟ
end continuous_linear_map_nondiscrete_normed_field
section normed_space
variables {π : Type*} [nondiscrete_normed_field π] [complete_space π] [measurable_space π]
variables [borel_space π]
variables {E : Type*} [normed_group E] [normed_space π E] [measurable_space E] [borel_space E]
lemma measurable_smul_const {f : Ξ± β π} {c : E} (hc : c β 0) :
measurable (Ξ» x, f x β’ c) β measurable f :=
(closed_embedding_smul_left hc).measurable_embedding.measurable_comp_iff
lemma ae_measurable_smul_const {f : Ξ± β π} {ΞΌ : measure Ξ±} {c : E} (hc : c β 0) :
ae_measurable (Ξ» x, f x β’ c) ΞΌ β ae_measurable f ΞΌ :=
(closed_embedding_smul_left hc).measurable_embedding.ae_measurable_comp_iff
end normed_space
|
4c1eb191a82b3758fe9d06d2a480a7ad078e5a76 | 75db7e3219bba2fbf41bf5b905f34fcb3c6ca3f2 | /library/data/nat/sqrt.lean | 18c5a3d057dfc81a64c73ca62609b2a686f68b49 | [
"Apache-2.0"
] | permissive | jroesch/lean | 30ef0860fa905d35b9ad6f76de1a4f65c9af6871 | 3de4ec1a6ce9a960feb2a48eeea8b53246fa34f2 | refs/heads/master | 1,586,090,835,348 | 1,455,142,203,000 | 1,455,142,277,000 | 51,536,958 | 1 | 0 | null | 1,455,215,811,000 | 1,455,215,811,000 | null | UTF-8 | Lean | false | false | 6,939 | lean | /-
Copyright (c) 2015 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
Very simple (sqrt n) function that returns s s.t.
s*s β€ n β€ s*s + s + s
-/
import data.nat.order data.nat.sub
namespace nat
open decidable
-- This is the simplest possible function that just performs a linear search
definition sqrt_aux : nat β nat β nat
| 0 n := 0
| (succ s) n := if (succ s)*(succ s) β€ n then succ s else sqrt_aux s n
theorem sqrt_aux_succ_of_pos {s n} : (succ s)*(succ s) β€ n β sqrt_aux (succ s) n = (succ s) :=
assume h, if_pos h
theorem sqrt_aux_succ_of_neg {s n} : Β¬ (succ s)*(succ s) β€ n β sqrt_aux (succ s) n = sqrt_aux s n :=
assume h, if_neg h
theorem sqrt_aux_of_le : β {s n : nat}, s * s β€ n β sqrt_aux s n = s
| 0 n h := rfl
| (succ s) n h := by rewrite [sqrt_aux_succ_of_pos h]
theorem sqrt_aux_le : β (s n), sqrt_aux s n β€ s
| 0 n := !zero_le
| (succ s) n := or.elim (em ((succ s)*(succ s) β€ n))
(Ξ» h, begin unfold sqrt_aux, rewrite [if_pos h] end)
(Ξ» h,
assert sqrt_aux s n β€ succ s, from le.step (sqrt_aux_le s n),
begin unfold sqrt_aux, rewrite [if_neg h], assumption end)
definition sqrt (n : nat) : nat :=
sqrt_aux n n
theorem sqrt_aux_lower : β {s n : nat}, s β€ n β sqrt_aux s n * sqrt_aux s n β€ n
| 0 n h := h
| (succ s) n h := by_cases
(Ξ» hβ : (succ s)*(succ s) β€ n, by rewrite [sqrt_aux_succ_of_pos hβ]; exact hβ)
(Ξ» hβ : Β¬ (succ s)*(succ s) β€ n,
assert aux : s β€ n, from le_of_succ_le h,
by rewrite [sqrt_aux_succ_of_neg hβ]; exact (sqrt_aux_lower aux))
theorem sqrt_lower (n : nat) : sqrt n * sqrt n β€ n :=
sqrt_aux_lower (le.refl n)
theorem sqrt_aux_upper : β {s n : nat}, n β€ s*s + s + s β n β€ sqrt_aux s n * sqrt_aux s n + sqrt_aux s n + sqrt_aux s n
| 0 n h := h
| (succ s) n h := by_cases
(Ξ» hβ : (succ s)*(succ s) β€ n,
by rewrite [sqrt_aux_succ_of_pos hβ]; exact h)
(Ξ» hβ : Β¬ (succ s)*(succ s) β€ n,
assert hβ : n < (succ s) * (succ s), from lt_of_not_ge hβ,
assert hβ : n β€ s * s + s + s, by rewrite [succ_mul_succ_eq at hβ]; exact le_of_lt_succ hβ,
by rewrite [sqrt_aux_succ_of_neg hβ]; exact (sqrt_aux_upper hβ))
theorem sqrt_upper (n : nat) : n β€ sqrt n * sqrt n + sqrt n + sqrt n :=
have aux : n β€ n*n + n + n, from le_add_of_le_right (le_add_of_le_left (le.refl n)),
sqrt_aux_upper aux
private theorem le_squared : β (n : nat), n β€ n*n
| 0 := !le.refl
| (succ n) :=
have auxβ : 1 β€ succ n, from succ_le_succ !zero_le,
assert auxβ : 1 * succ n β€ succ n * succ n, from nat.mul_le_mul auxβ !le.refl,
by rewrite [one_mul at auxβ]; exact auxβ
private theorem lt_squared : β {n : nat}, n > 1 β n < n * n
| 0 h := absurd h dec_trivial
| 1 h := absurd h dec_trivial
| (succ (succ n)) h :=
have 1 < succ (succ n), from dec_trivial,
assert succ (succ n) * 1 < succ (succ n) * succ (succ n), from mul_lt_mul_of_pos_left this dec_trivial,
by rewrite [mul_one at this]; exact this
theorem sqrt_le (n : nat) : sqrt n β€ n :=
calc sqrt n β€ sqrt n * sqrt n : le_squared
... β€ n : sqrt_lower
theorem eq_zero_of_sqrt_eq_zero {n : nat} : sqrt n = 0 β n = 0 :=
suppose sqrt n = 0,
assert n β€ sqrt n * sqrt n + sqrt n + sqrt n, from !sqrt_upper,
have n β€ 0, by rewrite [*`sqrt n = 0` at this]; exact this,
eq_zero_of_le_zero this
theorem le_three_of_sqrt_eq_one {n : nat} : sqrt n = 1 β n β€ 3 :=
suppose sqrt n = 1,
assert n β€ sqrt n * sqrt n + sqrt n + sqrt n, from !sqrt_upper,
show n β€ 3, by rewrite [*`sqrt n = 1` at this]; exact this
theorem sqrt_lt : β {n : nat}, n > 1 β sqrt n < n
| 0 h := absurd h dec_trivial
| 1 h := absurd h dec_trivial
| 2 h := dec_trivial
| 3 h := dec_trivial
| (n+4) h :=
have sqrt (n+4) > 1, from by_contradiction
(suppose Β¬ sqrt (n+4) > 1,
have sqrt (n+4) β€ 1, from le_of_not_gt this,
or.elim (eq_or_lt_of_le this)
(suppose sqrt (n+4) = 1,
have n+4 β€ 3, from le_three_of_sqrt_eq_one this,
absurd this dec_trivial)
(suppose sqrt (n+4) < 1,
have sqrt (n+4) = 0, from eq_zero_of_le_zero (le_of_lt_succ this),
have n + 4 = 0, from eq_zero_of_sqrt_eq_zero this,
absurd this dec_trivial)),
calc sqrt (n+4) < sqrt (n+4) * sqrt (n+4) : lt_squared this
... β€ n+4 : sqrt_lower
theorem sqrt_pos_of_pos {n : nat} : n > 0 β sqrt n > 0 :=
suppose n > 0,
have sqrt n β 0, from
suppose sqrt n = 0,
assert n = 0, from eq_zero_of_sqrt_eq_zero this,
by subst n; exact absurd `0 > 0` !lt.irrefl,
pos_of_ne_zero this
theorem sqrt_aux_offset_eq {n k : nat} (hβ : k β€ n + n) : β {s}, s β₯ n β sqrt_aux s (n*n + k) = n
| 0 hβ :=
assert neqz : n = 0, from eq_zero_of_le_zero hβ,
by rewrite neqz
| (succ s) hβ := by_cases
(Ξ» hl : (succ s)*(succ s) β€ n*n + k,
have lβ : n*n + k β€ n*n + n + n, from by rewrite [add.assoc]; exact (add_le_add_left hβ (n*n)),
assert lβ : n*n + k < n*n + n + n + 1, from lt_succ_of_le lβ,
have lβ : n*n + k < (succ n)*(succ n), by rewrite [-succ_mul_succ_eq at lβ]; exact lβ,
assert lβ : (succ s)*(succ s) < (succ n)*(succ n), from lt_of_le_of_lt hl lβ,
have ng : Β¬ succ s > (succ n), from
assume g : succ s > succ n,
have gβ : (succ s)*(succ s) > (succ n)*(succ n), from mul_lt_mul_of_le_of_le g g,
absurd (lt.trans gβ lβ) !lt.irrefl,
have sslesn : succ s β€ succ n, from le_of_not_gt ng,
have ssnesn : succ s β succ n, from
assume sseqsn : succ s = succ n,
by rewrite [sseqsn at lβ]; exact (absurd lβ !lt.irrefl),
have sslen : s < n, from lt_of_succ_lt_succ (lt_of_le_of_ne sslesn ssnesn),
assert sseqn : succ s = n, from le.antisymm sslen hβ,
by rewrite [sqrt_aux_succ_of_pos hl]; exact sseqn)
(Ξ» hg : Β¬ (succ s)*(succ s) β€ n*n + k,
or.elim (eq_or_lt_of_le hβ)
(Ξ» neqss : n = succ s,
have p : n*n β€ n*n + k, from !le_add_right,
have n : Β¬ n*n β€ n*n + k, by rewrite [-neqss at hg]; exact hg,
absurd p n)
(Ξ» sgen : succ s > n,
by rewrite [sqrt_aux_succ_of_neg hg]; exact (sqrt_aux_offset_eq (le_of_lt_succ sgen))))
theorem sqrt_offset_eq {n k : nat} : k β€ n + n β sqrt (n*n + k) = n :=
assume h,
have hβ : n β€ n*n + k, from le.trans !le_squared !le_add_right,
sqrt_aux_offset_eq h hβ
theorem sqrt_eq (n : nat) : sqrt (n*n) = n :=
sqrt_offset_eq !zero_le
theorem mul_square_cancel {a b : nat} : a*a = b*b β a = b :=
assume h,
assert aux : sqrt (a*a) = sqrt (b*b), by rewrite h,
by rewrite [*sqrt_eq at aux]; exact aux
end nat
|
fb276fb4ebfdbf6ccc641d5d8b79930802a1fd74 | 29cc89d6158dd3b90acbdbcab4d2c7eb9a7dbf0f | /Project/mucrl.lean | 2fe131155940967e3023c89a86792c46cf82a60d | [] | no_license | KjellZijlemaker/Logical_Verification_VU | ced0ba95316a30e3c94ba8eebd58ea004fa6f53b | 4578b93bf1615466996157bb333c84122b201d99 | refs/heads/master | 1,585,966,086,108 | 1,549,187,704,000 | 1,549,187,704,000 | 155,690,284 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 855 | lean | namespace test
inductive Set
| true : Set
| false : Set
inductive types
| onetype : types
| booltype : types
| nattype : types
| Dtype : types
| Frametype : types
| bool_Errtype : types
| Frame_Errtype : types
def type := types β Set
def one (t: type) := t types.onetype
def bool (t: type) := t types.booltype
def nat (t: type) := t types.nattype
def D (t: type) := t types.Dtype
def Frame (t: type) := t types.Frametype
def bool_Err (t: type) := t types.bool_Errtype
def Frame_Err (t: type) := t types.Frame_Errtype
notation `i` := one
notation `true` := bool
notation `false` := bool
notation `o` := nat
notation `S` := nat β nat
axiom t1 one (j: one) : j = i
axiom t2 bool : true = false
axiom t3 bool (b:bool) : b=true β¨ b=false
axiom nat_ind (P: nat β Prop) (n:nat)
end test
|
332e49ec293ebce8f9159a0d60f7ec9179138977 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/category_theory/category/Cat_auto.lean | e458d1e826659fb78d6fe15e9bbe2237f7aab80c | [] | no_license | AurelienSaue/Mathlib4_auto | f538cfd0980f65a6361eadea39e6fc639e9dae14 | 590df64109b08190abe22358fabc3eae000943f2 | refs/heads/master | 1,683,906,849,776 | 1,622,564,669,000 | 1,622,564,669,000 | 371,723,747 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,423 | 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.category_theory.concrete_category.default
import Mathlib.category_theory.discrete_category
import Mathlib.category_theory.eq_to_hom
import Mathlib.PostPort
universes v u u_1 u_2 u_3 u_4
namespace Mathlib
/-!
# Category of categories
This file contains the definition of the category `Cat` of all categories.
In this category objects are categories and
morphisms are functors between these categories.
## Implementation notes
Though `Cat` is not a concrete category, we use `bundled` to define
its carrier type.
-/
namespace category_theory
/-- Category of categories. -/
def Cat := bundled category
namespace Cat
protected instance inhabited : Inhabited Cat := { default := bundled.mk (Type u) }
protected instance has_coe_to_sort : has_coe_to_sort Cat := has_coe_to_sort.mk (Type u) bundled.Ξ±
protected instance str (C : Cat) : category β₯C := bundled.str C
/-- Construct a bundled `Cat` from the underlying type and the typeclass. -/
def of (C : Type u) [category C] : Cat := bundled.of C
/-- Category structure on `Cat` -/
protected instance category : large_category Cat := category.mk
/-- Functor that gets the set of objects of a category. It is not
called `forget`, because it is not a faithful functor. -/
def objects : Cat β₯€ Type u :=
functor.mk (fun (C : Cat) => β₯C) fun (C D : Cat) (F : C βΆ D) => functor.obj F
/-- Any isomorphism in `Cat` induces an equivalence of the underlying categories. -/
def equiv_of_iso {C : Cat} {D : Cat} (Ξ³ : C β
D) : β₯C β β₯D :=
equivalence.mk' (iso.hom Ξ³) (iso.inv Ξ³) (eq_to_iso sorry) (eq_to_iso (iso.inv_hom_id Ξ³))
end Cat
/--
Embedding `Type` into `Cat` as discrete categories.
This ought to be modelled as a 2-functor!
-/
@[simp] theorem Type_to_Cat_obj (X : Type u) : functor.obj Type_to_Cat X = Cat.of (discrete X) :=
Eq.refl (functor.obj Type_to_Cat X)
protected instance Type_to_Cat.faithful : faithful Type_to_Cat := faithful.mk
protected instance Type_to_Cat.full : full Type_to_Cat :=
full.mk
fun (X Y : Type (max (max (max u_1 u_2 u_3 u_4) u_1 u_2 u_3) (max u_1 u_2 u_3 u_4) u_1 u_2))
(F : functor.obj Type_to_Cat X βΆ functor.obj Type_to_Cat Y) => functor.obj F
end Mathlib |
34d604aa12f24bbda6b78b9fed0a9b68ec315b84 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/measure_theory/constructions/pi.lean | cf65cef2c62d15780420d42077a1155ea7db2543 | [
"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,404 | lean | /-
Copyright (c) 2020 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn
-/
import measure_theory.constructions.prod
import measure_theory.group.measure
/-!
# Product measures
In this file we define and prove properties about finite products of measures
(and at some point, countable products of measures).
## Main definition
* `measure_theory.measure.pi`: The product of finitely many Ο-finite measures.
Given `ΞΌ : Ξ i : ΞΉ, measure (Ξ± i)` for `[fintype ΞΉ]` it has type `measure (Ξ i : ΞΉ, Ξ± i)`.
To apply Fubini along some subset of the variables, use
`measure_theory.measure_preserving_pi_equiv_pi_subtype_prod` to reduce to the situation of a product
of two measures: this lemma states that the bijection
`measurable_equiv.pi_equiv_pi_subtype_prod Ξ± p` between `(Ξ i : ΞΉ, Ξ± i)` and
`(Ξ i : {i // p i}, Ξ± i) Γ (Ξ i : {i // Β¬ p i}, Ξ± i)` maps a product measure to a direct product of
product measures, to which one can apply the usual Fubini for direct product of measures.
## Implementation Notes
We define `measure_theory.outer_measure.pi`, the product of finitely many outer measures, as the
maximal outer measure `n` with the property that `n (pi univ s) β€ β i, m i (s i)`,
where `pi univ s` is the product of the sets `{s i | i : ΞΉ}`.
We then show that this induces a product of measures, called `measure_theory.measure.pi`.
For a collection of Ο-finite measures `ΞΌ` and a collection of measurable sets `s` we show that
`measure.pi ΞΌ (pi univ s) = β i, m i (s i)`. To do this, we follow the following steps:
* We know that there is some ordering on `ΞΉ`, given by an element of `[encodable ΞΉ]`.
* Using this, we have an equivalence `measurable_equiv.pi_measurable_equiv_tprod` between
`Ξ ΞΉ, Ξ± i` and an iterated product of `Ξ± i`, called `list.tprod Ξ± l` for some list `l`.
* On this iterated product we can easily define a product measure `measure_theory.measure.tprod`
by iterating `measure_theory.measure.prod`
* Using the previous two steps we construct `measure_theory.measure.pi'` on `Ξ ΞΉ, Ξ± i` for encodable
`ΞΉ`.
* We know that `measure_theory.measure.pi'` sends products of sets to products of measures, and
since `measure_theory.measure.pi` is the maximal such measure (or at least, it comes from an outer
measure which is the maximal such outer measure), we get the same rule for
`measure_theory.measure.pi`.
## Tags
finitary product measure
-/
noncomputable theory
open function set measure_theory.outer_measure filter measurable_space encodable
open_locale classical big_operators topological_space ennreal
universes u v
variables {ΞΉ ΞΉ' : Type*} {Ξ± : ΞΉ β Type*}
/-! We start with some measurability properties -/
/-- Boxes formed by Ο-systems form a Ο-system. -/
lemma is_pi_system.pi {C : Ξ i, set (set (Ξ± i))} (hC : β i, is_pi_system (C i)) :
is_pi_system (pi univ '' pi univ C) :=
begin
rintro _ β¨sβ, hsβ, rflβ© _ β¨sβ, hsβ, rflβ© hst,
rw [β pi_inter_distrib] at hst β’, rw [univ_pi_nonempty_iff] at hst,
exact mem_image_of_mem _ (Ξ» i _, hC i _ (hsβ i (mem_univ i)) _ (hsβ i (mem_univ i)) (hst i))
end
/-- Boxes form a Ο-system. -/
lemma is_pi_system_pi [Ξ i, measurable_space (Ξ± i)] :
is_pi_system (pi univ '' pi univ (Ξ» i, {s : set (Ξ± i) | measurable_set s})) :=
is_pi_system.pi (Ξ» i, is_pi_system_measurable_set)
variables [fintype ΞΉ] [fintype ΞΉ']
/-- Boxes of countably spanning sets are countably spanning. -/
lemma is_countably_spanning.pi {C : Ξ i, set (set (Ξ± i))}
(hC : β i, is_countably_spanning (C i)) :
is_countably_spanning (pi univ '' pi univ C) :=
begin
choose s h1s h2s using hC,
haveI := fintype.to_encodable ΞΉ,
let e : β β (ΞΉ β β) := Ξ» n, (decode (ΞΉ β β) n).iget,
refine β¨Ξ» n, pi univ (Ξ» i, s i (e n i)), Ξ» n, mem_image_of_mem _ (Ξ» i _, h1s i _), _β©,
simp_rw [(surjective_decode_iget (ΞΉ β β)).Union_comp (Ξ» x, pi univ (Ξ» i, s i (x i))),
Union_univ_pi s, h2s, pi_univ]
end
/-- The product of generated Ο-algebras is the one generated by boxes, if both generating sets
are countably spanning. -/
lemma generate_from_pi_eq {C : Ξ i, set (set (Ξ± i))}
(hC : β i, is_countably_spanning (C i)) :
@measurable_space.pi _ _ (Ξ» i, generate_from (C i)) = generate_from (pi univ '' pi univ C) :=
begin
haveI := fintype.to_encodable ΞΉ,
apply le_antisymm,
{ refine supr_le _, intro i, rw [comap_generate_from],
apply generate_from_le, rintro _ β¨s, hs, rflβ©, dsimp,
choose t h1t h2t using hC,
simp_rw [eval_preimage, β h2t],
rw [β @Union_const _ β _ s],
have : (pi univ (update (Ξ» (i' : ΞΉ), Union (t i')) i (β (i' : β), s))) =
(pi univ (Ξ» k, β j : β, @update ΞΉ (Ξ» i', set (Ξ± i')) _ (Ξ» i', t i' j) i s k)),
{ ext, simp_rw [mem_univ_pi], apply forall_congr, intro i',
by_cases (i' = i), { subst h, simp }, { rw [β ne.def] at h, simp [h] }},
rw [this, β Union_univ_pi],
apply measurable_set.Union,
intro n, apply measurable_set_generate_from,
apply mem_image_of_mem, intros j _, dsimp only,
by_cases h: j = i, subst h, rwa [update_same], rw [update_noteq h], apply h1t },
{ apply generate_from_le, rintro _ β¨s, hs, rflβ©,
rw [univ_pi_eq_Inter], apply measurable_set.Inter, intro i, apply measurable_pi_apply,
exact measurable_set_generate_from (hs i (mem_univ i)) }
end
/-- If `C` and `D` generate the Ο-algebras on `Ξ±` resp. `Ξ²`, then rectangles formed by `C` and `D`
generate the Ο-algebra on `Ξ± Γ Ξ²`. -/
lemma generate_from_eq_pi [h : Ξ i, measurable_space (Ξ± i)]
{C : Ξ i, set (set (Ξ± i))} (hC : β i, generate_from (C i) = h i)
(h2C : β i, is_countably_spanning (C i)) :
generate_from (pi univ '' pi univ C) = measurable_space.pi :=
by rw [β funext hC, generate_from_pi_eq h2C]
/-- The product Ο-algebra is generated from boxes, i.e. `s ΓΛ’ t` for sets `s : set Ξ±` and
`t : set Ξ²`. -/
lemma generate_from_pi [Ξ i, measurable_space (Ξ± i)] :
generate_from (pi univ '' pi univ (Ξ» i, { s : set (Ξ± i) | measurable_set s})) =
measurable_space.pi :=
generate_from_eq_pi (Ξ» i, generate_from_measurable_set) (Ξ» i, is_countably_spanning_measurable_set)
namespace measure_theory
variables {m : Ξ i, outer_measure (Ξ± i)}
/-- An upper bound for the measure in a finite product space.
It is defined to by taking the image of the set under all projections, and taking the product
of the measures of these images.
For measurable boxes it is equal to the correct measure. -/
@[simp] def pi_premeasure (m : Ξ i, outer_measure (Ξ± i)) (s : set (Ξ i, Ξ± i)) : ββ₯0β :=
β i, m i (eval i '' s)
lemma pi_premeasure_pi {s : Ξ i, set (Ξ± i)} (hs : (pi univ s).nonempty) :
pi_premeasure m (pi univ s) = β i, m i (s i) :=
by simp [hs]
lemma pi_premeasure_pi' {s : Ξ i, set (Ξ± i)} :
pi_premeasure m (pi univ s) = β i, m i (s i) :=
begin
casesI is_empty_or_nonempty ΞΉ,
{ simp, },
cases (pi univ s).eq_empty_or_nonempty with h h,
{ rcases univ_pi_eq_empty_iff.mp h with β¨i, hiβ©,
have : β i, m i (s i) = 0 := β¨i, by simp [hi]β©,
simpa [h, finset.card_univ, zero_pow (fintype.card_pos_iff.mpr βΉ_βΊ),
@eq_comm _ (0 : ββ₯0β), finset.prod_eq_zero_iff] },
{ simp [h] }
end
lemma pi_premeasure_pi_mono {s t : set (Ξ i, Ξ± i)} (h : s β t) :
pi_premeasure m s β€ pi_premeasure m t :=
finset.prod_le_prod' (Ξ» i _, (m i).mono' (image_subset _ h))
lemma pi_premeasure_pi_eval {s : set (Ξ i, Ξ± i)} :
pi_premeasure m (pi univ (Ξ» i, eval i '' s)) = pi_premeasure m s :=
by simp [pi_premeasure_pi']
namespace outer_measure
/-- `outer_measure.pi m` is the finite product of the outer measures `{m i | i : ΞΉ}`.
It is defined to be the maximal outer measure `n` with the property that
`n (pi univ s) β€ β i, m i (s i)`, where `pi univ s` is the product of the sets
`{s i | i : ΞΉ}`. -/
protected def pi (m : Ξ i, outer_measure (Ξ± i)) : outer_measure (Ξ i, Ξ± i) :=
bounded_by (pi_premeasure m)
lemma pi_pi_le (m : Ξ i, outer_measure (Ξ± i)) (s : Ξ i, set (Ξ± i)) :
outer_measure.pi m (pi univ s) β€ β i, m i (s i) :=
by { cases (pi univ s).eq_empty_or_nonempty with h h, simp [h],
exact (bounded_by_le _).trans_eq (pi_premeasure_pi h) }
lemma le_pi {m : Ξ i, outer_measure (Ξ± i)} {n : outer_measure (Ξ i, Ξ± i)} :
n β€ outer_measure.pi m β β (s : Ξ i, set (Ξ± i)), (pi univ s).nonempty β
n (pi univ s) β€ β i, m i (s i) :=
begin
rw [outer_measure.pi, le_bounded_by'], split,
{ intros h s hs, refine (h _ hs).trans_eq (pi_premeasure_pi hs) },
{ intros h s hs, refine le_trans (n.mono $ subset_pi_eval_image univ s) (h _ _),
simp [univ_pi_nonempty_iff, hs] }
end
end outer_measure
namespace measure
variables [Ξ i, measurable_space (Ξ± i)] (ΞΌ : Ξ i, measure (Ξ± i))
section tprod
open list
variables {Ξ΄ : Type*} {Ο : Ξ΄ β Type*} [β x, measurable_space (Ο x)]
/-- A product of measures in `tprod Ξ± l`. -/
-- for some reason the equation compiler doesn't like this definition
protected def tprod (l : list Ξ΄) (ΞΌ : Ξ i, measure (Ο i)) : measure (tprod Ο l) :=
by { induction l with i l ih, exact dirac punit.star, exact (ΞΌ i).prod ih }
@[simp] lemma tprod_nil (ΞΌ : Ξ i, measure (Ο i)) : measure.tprod [] ΞΌ = dirac punit.star := rfl
@[simp] lemma tprod_cons (i : Ξ΄) (l : list Ξ΄) (ΞΌ : Ξ i, measure (Ο i)) :
measure.tprod (i :: l) ΞΌ = (ΞΌ i).prod (measure.tprod l ΞΌ) := rfl
instance sigma_finite_tprod (l : list Ξ΄) (ΞΌ : Ξ i, measure (Ο i)) [β i, sigma_finite (ΞΌ i)] :
sigma_finite (measure.tprod l ΞΌ) :=
begin
induction l with i l ih,
{ rw [tprod_nil], apply_instance },
{ rw [tprod_cons], resetI, apply_instance }
end
lemma tprod_tprod (l : list Ξ΄) (ΞΌ : Ξ i, measure (Ο i)) [β i, sigma_finite (ΞΌ i)]
(s : Ξ i, set (Ο i)) :
measure.tprod l ΞΌ (set.tprod l s) = (l.map (Ξ» i, (ΞΌ i) (s i))).prod :=
begin
induction l with i l ih, { simp },
rw [tprod_cons, set.tprod, prod_prod, map_cons, prod_cons, ih]
end
end tprod
section encodable
open list measurable_equiv
variables [encodable ΞΉ]
/-- The product measure on an encodable finite type, defined by mapping `measure.tprod` along the
equivalence `measurable_equiv.pi_measurable_equiv_tprod`.
The definition `measure_theory.measure.pi` should be used instead of this one. -/
def pi' : measure (Ξ i, Ξ± i) :=
measure.map (tprod.elim' mem_sorted_univ) (measure.tprod (sorted_univ ΞΉ) ΞΌ)
lemma pi'_pi [β i, sigma_finite (ΞΌ i)] (s : Ξ i, set (Ξ± i)) : pi' ΞΌ (pi univ s) = β i, ΞΌ i (s i) :=
by rw [pi', β measurable_equiv.pi_measurable_equiv_tprod_symm_apply, measurable_equiv.map_apply,
measurable_equiv.pi_measurable_equiv_tprod_symm_apply, elim_preimage_pi, tprod_tprod _ ΞΌ,
β list.prod_to_finset, sorted_univ_to_finset]; exact sorted_univ_nodup ΞΉ
end encodable
lemma pi_caratheodory :
measurable_space.pi β€ (outer_measure.pi (Ξ» i, (ΞΌ i).to_outer_measure)).caratheodory :=
begin
refine supr_le _,
intros i s hs,
rw [measurable_space.comap] at hs,
rcases hs with β¨s, hs, rflβ©,
apply bounded_by_caratheodory,
intro t,
simp_rw [pi_premeasure],
refine finset.prod_add_prod_le' (finset.mem_univ i) _ _ _,
{ simp [image_inter_preimage, image_diff_preimage, measure_inter_add_diff _ hs, le_refl] },
{ rintro j - hj, apply mono', apply image_subset, apply inter_subset_left },
{ rintro j - hj, apply mono', apply image_subset, apply diff_subset }
end
/-- `measure.pi ΞΌ` is the finite product of the measures `{ΞΌ i | i : ΞΉ}`.
It is defined to be measure corresponding to `measure_theory.outer_measure.pi`. -/
@[irreducible] protected def pi : measure (Ξ i, Ξ± i) :=
to_measure (outer_measure.pi (Ξ» i, (ΞΌ i).to_outer_measure)) (pi_caratheodory ΞΌ)
lemma pi_pi_aux [β i, sigma_finite (ΞΌ i)] (s : Ξ i, set (Ξ± i)) (hs : β i, measurable_set (s i)) :
measure.pi ΞΌ (pi univ s) = β i, ΞΌ i (s i) :=
begin
refine le_antisymm _ _,
{ rw [measure.pi, to_measure_apply _ _ (measurable_set.pi_fintype (Ξ» i _, hs i))],
apply outer_measure.pi_pi_le },
{ haveI : encodable ΞΉ := fintype.to_encodable ΞΉ,
rw [β pi'_pi ΞΌ s],
simp_rw [β pi'_pi ΞΌ s, measure.pi,
to_measure_apply _ _ (measurable_set.pi_fintype (Ξ» i _, hs i)), β to_outer_measure_apply],
suffices : (pi' ΞΌ).to_outer_measure β€ outer_measure.pi (Ξ» i, (ΞΌ i).to_outer_measure),
{ exact this _ },
clear hs s,
rw [outer_measure.le_pi],
intros s hs,
simp_rw [to_outer_measure_apply],
exact (pi'_pi ΞΌ s).le }
end
variable {ΞΌ}
/-- `measure.pi ΞΌ` has finite spanning sets in rectangles of finite spanning sets. -/
def finite_spanning_sets_in.pi {C : Ξ i, set (set (Ξ± i))}
(hΞΌ : β i, (ΞΌ i).finite_spanning_sets_in (C i)) :
(measure.pi ΞΌ).finite_spanning_sets_in (pi univ '' pi univ C) :=
begin
haveI := Ξ» i, (hΞΌ i).sigma_finite,
haveI := fintype.to_encodable ΞΉ,
refine β¨Ξ» n, pi univ (Ξ» i, (hΞΌ i).set ((decode (ΞΉ β β) n).iget i)), Ξ» n, _, Ξ» n, _, _β©;
-- TODO (kmill) If this let comes before the refine, while the noncomputability checker
-- correctly sees this definition is computable, the Lean VM fails to see the binding is
-- computationally irrelevant. The `noncomputable theory` doesn't help because all it does
-- is insert `noncomputable` for you when necessary.
let e : β β (ΞΉ β β) := Ξ» n, (decode (ΞΉ β β) n).iget,
{ refine mem_image_of_mem _ (Ξ» i _, (hΞΌ i).set_mem _) },
{ calc measure.pi ΞΌ (pi univ (Ξ» i, (hΞΌ i).set (e n i)))
β€ measure.pi ΞΌ (pi univ (Ξ» i, to_measurable (ΞΌ i) ((hΞΌ i).set (e n i)))) :
measure_mono (pi_mono $ Ξ» i hi, subset_to_measurable _ _)
... = β i, ΞΌ i (to_measurable (ΞΌ i) ((hΞΌ i).set (e n i))) :
pi_pi_aux ΞΌ _ (Ξ» i, measurable_set_to_measurable _ _)
... = β i, ΞΌ i ((hΞΌ i).set (e n i)) :
by simp only [measure_to_measurable]
... < β : ennreal.prod_lt_top (Ξ» i hi, ((hΞΌ i).finite _).ne) },
{ simp_rw [(surjective_decode_iget (ΞΉ β β)).Union_comp (Ξ» x, pi univ (Ξ» i, (hΞΌ i).set (x i))),
Union_univ_pi (Ξ» i, (hΞΌ i).set), (hΞΌ _).spanning, set.pi_univ] }
end
/-- A measure on a finite product space equals the product measure if they are equal on rectangles
with as sides sets that generate the corresponding Ο-algebras. -/
lemma pi_eq_generate_from {C : Ξ i, set (set (Ξ± i))}
(hC : β i, generate_from (C i) = _inst_3 i)
(h2C : β i, is_pi_system (C i))
(h3C : β i, (ΞΌ i).finite_spanning_sets_in (C i))
{ΞΌΞ½ : measure (Ξ i, Ξ± i)}
(hβ : β s : Ξ i, set (Ξ± i), (β i, s i β C i) β ΞΌΞ½ (pi univ s) = β i, ΞΌ i (s i)) :
measure.pi ΞΌ = ΞΌΞ½ :=
begin
have h4C : β i (s : set (Ξ± i)), s β C i β measurable_set s,
{ intros i s hs, rw [β hC], exact measurable_set_generate_from hs },
refine (finite_spanning_sets_in.pi h3C).ext
(generate_from_eq_pi hC (Ξ» i, (h3C i).is_countably_spanning)).symm
(is_pi_system.pi h2C) _,
rintro _ β¨s, hs, rflβ©,
rw [mem_univ_pi] at hs,
haveI := Ξ» i, (h3C i).sigma_finite,
simp_rw [hβ s hs, pi_pi_aux ΞΌ s (Ξ» i, h4C i _ (hs i))]
end
variables [β i, sigma_finite (ΞΌ i)]
/-- A measure on a finite product space equals the product measure if they are equal on
rectangles. -/
lemma pi_eq {ΞΌ' : measure (Ξ i, Ξ± i)}
(h : β s : Ξ i, set (Ξ± i), (β i, measurable_set (s i)) β ΞΌ' (pi univ s) = β i, ΞΌ i (s i)) :
measure.pi ΞΌ = ΞΌ' :=
pi_eq_generate_from (Ξ» i, generate_from_measurable_set)
(Ξ» i, is_pi_system_measurable_set)
(Ξ» i, (ΞΌ i).to_finite_spanning_sets_in) h
variables (ΞΌ)
lemma pi'_eq_pi [encodable ΞΉ] : pi' ΞΌ = measure.pi ΞΌ :=
eq.symm $ pi_eq $ Ξ» s hs, pi'_pi ΞΌ s
@[simp] lemma pi_pi (s : Ξ i, set (Ξ± i)) : measure.pi ΞΌ (pi univ s) = β i, ΞΌ i (s i) :=
begin
haveI : encodable ΞΉ := fintype.to_encodable ΞΉ,
rw [β pi'_eq_pi, pi'_pi]
end
lemma pi_univ : measure.pi ΞΌ univ = β i, ΞΌ i univ := by rw [β pi_univ, pi_pi ΞΌ]
lemma pi_ball [β i, metric_space (Ξ± i)] (x : Ξ i, Ξ± i) {r : β}
(hr : 0 < r) :
measure.pi ΞΌ (metric.ball x r) = β i, ΞΌ i (metric.ball (x i) r) :=
by rw [ball_pi _ hr, pi_pi]
lemma pi_closed_ball [β i, metric_space (Ξ± i)] (x : Ξ i, Ξ± i) {r : β}
(hr : 0 β€ r) :
measure.pi ΞΌ (metric.closed_ball x r) = β i, ΞΌ i (metric.closed_ball (x i) r) :=
by rw [closed_ball_pi _ hr, pi_pi]
instance pi.sigma_finite : sigma_finite (measure.pi ΞΌ) :=
(finite_spanning_sets_in.pi (Ξ» i, (ΞΌ i).to_finite_spanning_sets_in)).sigma_finite
lemma pi_of_empty {Ξ± : Type*} [is_empty Ξ±] {Ξ² : Ξ± β Type*} {m : Ξ a, measurable_space (Ξ² a)}
(ΞΌ : Ξ a : Ξ±, measure (Ξ² a)) (x : Ξ a, Ξ² a := is_empty_elim) :
measure.pi ΞΌ = dirac x :=
begin
haveI : β a, sigma_finite (ΞΌ a) := is_empty_elim,
refine pi_eq (Ξ» s hs, _),
rw [fintype.prod_empty, dirac_apply_of_mem],
exact is_empty_elim
end
lemma pi_eval_preimage_null {i : ΞΉ} {s : set (Ξ± i)} (hs : ΞΌ i s = 0) :
measure.pi ΞΌ (eval i β»ΒΉ' s) = 0 :=
begin
/- WLOG, `s` is measurable -/
rcases exists_measurable_superset_of_null hs with β¨t, hst, htm, hΞΌtβ©,
suffices : measure.pi ΞΌ (eval i β»ΒΉ' t) = 0,
from measure_mono_null (preimage_mono hst) this,
clear_dependent s,
/- Now rewrite it as `set.pi`, and apply `pi_pi` -/
rw [β univ_pi_update_univ, pi_pi],
apply finset.prod_eq_zero (finset.mem_univ i),
simp [hΞΌt]
end
lemma pi_hyperplane (i : ΞΉ) [has_no_atoms (ΞΌ i)] (x : Ξ± i) :
measure.pi ΞΌ {f : Ξ i, Ξ± i | f i = x} = 0 :=
show measure.pi ΞΌ (eval i β»ΒΉ' {x}) = 0,
from pi_eval_preimage_null _ (measure_singleton x)
lemma ae_eval_ne (i : ΞΉ) [has_no_atoms (ΞΌ i)] (x : Ξ± i) :
βα΅ y : Ξ i, Ξ± i βmeasure.pi ΞΌ, y i β x :=
compl_mem_ae_iff.2 (pi_hyperplane ΞΌ i x)
variable {ΞΌ}
lemma tendsto_eval_ae_ae {i : ΞΉ} : tendsto (eval i) (measure.pi ΞΌ).ae (ΞΌ i).ae :=
Ξ» s hs, pi_eval_preimage_null ΞΌ hs
lemma ae_pi_le_pi : (measure.pi ΞΌ).ae β€ filter.pi (Ξ» i, (ΞΌ i).ae) :=
le_infi $ Ξ» i, tendsto_eval_ae_ae.le_comap
lemma ae_eq_pi {Ξ² : ΞΉ β Type*} {f f' : Ξ i, Ξ± i β Ξ² i} (h : β i, f i =α΅[ΞΌ i] f' i) :
(Ξ» (x : Ξ i, Ξ± i) i, f i (x i)) =α΅[measure.pi ΞΌ] (Ξ» x i, f' i (x i)) :=
(eventually_all.2 (Ξ» i, tendsto_eval_ae_ae.eventually (h i))).mono $ Ξ» x hx, funext hx
lemma ae_le_pi {Ξ² : ΞΉ β Type*} [Ξ i, preorder (Ξ² i)] {f f' : Ξ i, Ξ± i β Ξ² i}
(h : β i, f i β€α΅[ΞΌ i] f' i) :
(Ξ» (x : Ξ i, Ξ± i) i, f i (x i)) β€α΅[measure.pi ΞΌ] (Ξ» x i, f' i (x i)) :=
(eventually_all.2 (Ξ» i, tendsto_eval_ae_ae.eventually (h i))).mono $ Ξ» x hx, hx
lemma ae_le_set_pi {I : set ΞΉ} {s t : Ξ i, set (Ξ± i)} (h : β i β I, s i β€α΅[ΞΌ i] t i) :
(set.pi I s) β€α΅[measure.pi ΞΌ] (set.pi I t) :=
((eventually_all_finite (finite.of_fintype I)).2
(Ξ» i hi, tendsto_eval_ae_ae.eventually (h i hi))).mono $
Ξ» x hst hx i hi, hst i hi $ hx i hi
lemma ae_eq_set_pi {I : set ΞΉ} {s t : Ξ i, set (Ξ± i)} (h : β i β I, s i =α΅[ΞΌ i] t i) :
(set.pi I s) =α΅[measure.pi ΞΌ] (set.pi I t) :=
(ae_le_set_pi (Ξ» i hi, (h i hi).le)).antisymm (ae_le_set_pi (Ξ» i hi, (h i hi).symm.le))
section intervals
variables {ΞΌ} [Ξ i, partial_order (Ξ± i)] [β i, has_no_atoms (ΞΌ i)]
lemma pi_Iio_ae_eq_pi_Iic {s : set ΞΉ} {f : Ξ i, Ξ± i} :
pi s (Ξ» i, Iio (f i)) =α΅[measure.pi ΞΌ] pi s (Ξ» i, Iic (f i)) :=
ae_eq_set_pi $ Ξ» i hi, Iio_ae_eq_Iic
lemma pi_Ioi_ae_eq_pi_Ici {s : set ΞΉ} {f : Ξ i, Ξ± i} :
pi s (Ξ» i, Ioi (f i)) =α΅[measure.pi ΞΌ] pi s (Ξ» i, Ici (f i)) :=
ae_eq_set_pi $ Ξ» i hi, Ioi_ae_eq_Ici
lemma univ_pi_Iio_ae_eq_Iic {f : Ξ i, Ξ± i} :
pi univ (Ξ» i, Iio (f i)) =α΅[measure.pi ΞΌ] Iic f :=
by { rw β pi_univ_Iic, exact pi_Iio_ae_eq_pi_Iic }
lemma univ_pi_Ioi_ae_eq_Ici {f : Ξ i, Ξ± i} :
pi univ (Ξ» i, Ioi (f i)) =α΅[measure.pi ΞΌ] Ici f :=
by { rw β pi_univ_Ici, exact pi_Ioi_ae_eq_pi_Ici }
lemma pi_Ioo_ae_eq_pi_Icc {s : set ΞΉ} {f g : Ξ i, Ξ± i} :
pi s (Ξ» i, Ioo (f i) (g i)) =α΅[measure.pi ΞΌ] pi s (Ξ» i, Icc (f i) (g i)) :=
ae_eq_set_pi $ Ξ» i hi, Ioo_ae_eq_Icc
lemma pi_Ioo_ae_eq_pi_Ioc {s : set ΞΉ} {f g : Ξ i, Ξ± i} :
pi s (Ξ» i, Ioo (f i) (g i)) =α΅[measure.pi ΞΌ] pi s (Ξ» i, Ioc (f i) (g i)) :=
ae_eq_set_pi $ Ξ» i hi, Ioo_ae_eq_Ioc
lemma univ_pi_Ioo_ae_eq_Icc {f g : Ξ i, Ξ± i} :
pi univ (Ξ» i, Ioo (f i) (g i)) =α΅[measure.pi ΞΌ] Icc f g :=
by { rw β pi_univ_Icc, exact pi_Ioo_ae_eq_pi_Icc }
lemma pi_Ioc_ae_eq_pi_Icc {s : set ΞΉ} {f g : Ξ i, Ξ± i} :
pi s (Ξ» i, Ioc (f i) (g i)) =α΅[measure.pi ΞΌ] pi s (Ξ» i, Icc (f i) (g i)) :=
ae_eq_set_pi $ Ξ» i hi, Ioc_ae_eq_Icc
lemma univ_pi_Ioc_ae_eq_Icc {f g : Ξ i, Ξ± i} :
pi univ (Ξ» i, Ioc (f i) (g i)) =α΅[measure.pi ΞΌ] Icc f g :=
by { rw β pi_univ_Icc, exact pi_Ioc_ae_eq_pi_Icc }
lemma pi_Ico_ae_eq_pi_Icc {s : set ΞΉ} {f g : Ξ i, Ξ± i} :
pi s (Ξ» i, Ico (f i) (g i)) =α΅[measure.pi ΞΌ] pi s (Ξ» i, Icc (f i) (g i)) :=
ae_eq_set_pi $ Ξ» i hi, Ico_ae_eq_Icc
lemma univ_pi_Ico_ae_eq_Icc {f g : Ξ i, Ξ± i} :
pi univ (Ξ» i, Ico (f i) (g i)) =α΅[measure.pi ΞΌ] Icc f g :=
by { rw β pi_univ_Icc, exact pi_Ico_ae_eq_pi_Icc }
end intervals
/-- If one of the measures `ΞΌ i` has no atoms, them `measure.pi Β΅`
has no atoms. The instance below assumes that all `ΞΌ i` have no atoms. -/
lemma pi_has_no_atoms (i : ΞΉ) [has_no_atoms (ΞΌ i)] :
has_no_atoms (measure.pi ΞΌ) :=
β¨Ξ» x, flip measure_mono_null (pi_hyperplane ΞΌ i (x i)) (singleton_subset_iff.2 rfl)β©
instance [h : nonempty ΞΉ] [β i, has_no_atoms (ΞΌ i)] : has_no_atoms (measure.pi ΞΌ) :=
h.elim $ Ξ» i, pi_has_no_atoms i
instance [Ξ i, topological_space (Ξ± i)] [β i, is_locally_finite_measure (ΞΌ i)] :
is_locally_finite_measure (measure.pi ΞΌ) :=
begin
refine β¨Ξ» x, _β©,
choose s hxs ho hΞΌ using Ξ» i, (ΞΌ i).exists_is_open_measure_lt_top (x i),
refine β¨pi univ s, set_pi_mem_nhds finite_univ (Ξ» i hi, is_open.mem_nhds (ho i) (hxs i)), _β©,
rw [pi_pi],
exact ennreal.prod_lt_top (Ξ» i _, (hΞΌ i).ne)
end
variable (ΞΌ)
@[to_additive] instance pi.is_mul_left_invariant [β i, group (Ξ± i)] [β i, has_measurable_mul (Ξ± i)]
[β i, is_mul_left_invariant (ΞΌ i)] : is_mul_left_invariant (measure.pi ΞΌ) :=
begin
refine β¨Ξ» x, (measure.pi_eq (Ξ» s hs, _)).symmβ©,
have h : has_mul.mul x β»ΒΉ' (pi univ s) = set.pi univ (Ξ» i, (Ξ» y, x i * y) β»ΒΉ' s i),
{ ext, simp },
simp_rw [measure.map_apply (measurable_const_mul x) (measurable_set.univ_pi_fintype hs), h,
pi_pi, measure_preimage_mul]
end
@[to_additive] instance pi.is_inv_invariant [β i, group (Ξ± i)] [β i, has_measurable_inv (Ξ± i)]
[β i, is_inv_invariant (ΞΌ i)] : is_inv_invariant (measure.pi ΞΌ) :=
begin
refine β¨(measure.pi_eq (Ξ» s hs, _)).symmβ©,
have A : has_inv.inv β»ΒΉ' (pi univ s) = set.pi univ (Ξ» i, has_inv.inv β»ΒΉ' s i),
{ ext, simp },
simp_rw [measure.inv, measure.map_apply measurable_inv (measurable_set.univ_pi_fintype hs), A,
pi_pi, measure_preimage_inv]
end
end measure
instance measure_space.pi [Ξ i, measure_space (Ξ± i)] : measure_space (Ξ i, Ξ± i) :=
β¨measure.pi (Ξ» i, volume)β©
lemma volume_pi [Ξ i, measure_space (Ξ± i)] :
(volume : measure (Ξ i, Ξ± i)) = measure.pi (Ξ» i, volume) :=
rfl
lemma volume_pi_pi [Ξ i, measure_space (Ξ± i)] [β i, sigma_finite (volume : measure (Ξ± i))]
(s : Ξ i, set (Ξ± i)) :
volume (pi univ s) = β i, volume (s i) :=
measure.pi_pi (Ξ» i, volume) s
lemma volume_pi_ball [Ξ i, measure_space (Ξ± i)] [β i, sigma_finite (volume : measure (Ξ± i))]
[β i, metric_space (Ξ± i)] (x : Ξ i, Ξ± i) {r : β} (hr : 0 < r) :
volume (metric.ball x r) = β i, volume (metric.ball (x i) r) :=
measure.pi_ball _ _ hr
lemma volume_pi_closed_ball [Ξ i, measure_space (Ξ± i)] [β i, sigma_finite (volume : measure (Ξ± i))]
[β i, metric_space (Ξ± i)] (x : Ξ i, Ξ± i) {r : β} (hr : 0 β€ r) :
volume (metric.closed_ball x r) = β i, volume (metric.closed_ball (x i) r) :=
measure.pi_closed_ball _ _ hr
open measure
/-- We intentionally restrict this only to the nondependent function space, since type-class
inference cannot find an instance for `ΞΉ β β` when this is stated for dependent function spaces. -/
@[to_additive "We intentionally restrict this only to the nondependent function space, since
type-class inference cannot find an instance for `ΞΉ β β` when this is stated for dependent function
spaces."]
instance pi.is_mul_left_invariant_volume {Ξ±} [group Ξ±] [measure_space Ξ±]
[sigma_finite (volume : measure Ξ±)]
[has_measurable_mul Ξ±] [is_mul_left_invariant (volume : measure Ξ±)] :
is_mul_left_invariant (volume : measure (ΞΉ β Ξ±)) :=
pi.is_mul_left_invariant _
/-- We intentionally restrict this only to the nondependent function space, since type-class
inference cannot find an instance for `ΞΉ β β` when this is stated for dependent function spaces. -/
@[to_additive "We intentionally restrict this only to the nondependent function space, since
type-class inference cannot find an instance for `ΞΉ β β` when this is stated for dependent function
spaces."]
instance pi.is_inv_invariant_volume {Ξ±} [group Ξ±] [measure_space Ξ±]
[sigma_finite (volume : measure Ξ±)]
[has_measurable_inv Ξ±] [is_inv_invariant (volume : measure Ξ±)] :
is_inv_invariant (volume : measure (ΞΉ β Ξ±)) :=
pi.is_inv_invariant _
/-!
### Measure preserving equivalences
In this section we prove that some measurable equivalences (e.g., between `fin 1 β Ξ±` and `Ξ±` or
between `fin 2 β Ξ±` and `Ξ± Γ Ξ±`) preserve measure or volume. These lemmas can be used to prove that
measures of corresponding sets (images or preimages) have equal measures and functions `f β e` and
`f` have equal integrals, see lemmas in the `measure_theory.measure_preserving` prefix.
-/
section measure_preserving
lemma measure_preserving_pi_equiv_pi_subtype_prod {ΞΉ : Type u} {Ξ± : ΞΉ β Type v} [fintype ΞΉ]
{m : Ξ i, measurable_space (Ξ± i)} (ΞΌ : Ξ i, measure (Ξ± i)) [β i, sigma_finite (ΞΌ i)]
(p : ΞΉ β Prop) [decidable_pred p] :
measure_preserving (measurable_equiv.pi_equiv_pi_subtype_prod Ξ± p) (measure.pi ΞΌ)
((measure.pi $ Ξ» i : subtype p, ΞΌ i).prod (measure.pi $ Ξ» i, ΞΌ i)) :=
begin
set e := (measurable_equiv.pi_equiv_pi_subtype_prod Ξ± p).symm,
refine measure_preserving.symm e _,
refine β¨e.measurable, (pi_eq $ Ξ» s hs, _).symmβ©,
have : e β»ΒΉ' (pi univ s) =
(pi univ (Ξ» i : {i // p i}, s i)) ΓΛ’ (pi univ (Ξ» i : {i // Β¬p i}, s i)),
from equiv.preimage_pi_equiv_pi_subtype_prod_symm_pi p s,
rw [e.map_apply, this, prod_prod, pi_pi, pi_pi],
exact fintype.prod_subtype_mul_prod_subtype p (Ξ» i, ΞΌ i (s i))
end
lemma volume_preserving_pi_equiv_pi_subtype_prod {ΞΉ : Type*} (Ξ± : ΞΉ β Type*) [fintype ΞΉ]
[Ξ i, measure_space (Ξ± i)] [β i, sigma_finite (volume : measure (Ξ± i))]
(p : ΞΉ β Prop) [decidable_pred p] :
measure_preserving (measurable_equiv.pi_equiv_pi_subtype_prod Ξ± p) :=
measure_preserving_pi_equiv_pi_subtype_prod (Ξ» i, volume) p
lemma measure_preserving_pi_fin_succ_above_equiv {n : β} {Ξ± : fin (n + 1) β Type u}
{m : Ξ i, measurable_space (Ξ± i)} (ΞΌ : Ξ i, measure (Ξ± i)) [β i, sigma_finite (ΞΌ i)]
(i : fin (n + 1)) :
measure_preserving (measurable_equiv.pi_fin_succ_above_equiv Ξ± i) (measure.pi ΞΌ)
((ΞΌ i).prod $ measure.pi $ Ξ» j, ΞΌ (i.succ_above j)) :=
begin
set e := (measurable_equiv.pi_fin_succ_above_equiv Ξ± i).symm,
refine measure_preserving.symm e _,
refine β¨e.measurable, (pi_eq $ Ξ» s hs, _).symmβ©,
rw [e.map_apply, i.prod_univ_succ_above _, β pi_pi, β prod_prod],
congr' 1 with β¨x, fβ©,
simp [i.forall_iff_succ_above]
end
lemma volume_preserving_pi_fin_succ_above_equiv {n : β} (Ξ± : fin (n + 1) β Type u)
[Ξ i, measure_space (Ξ± i)] [β i, sigma_finite (volume : measure (Ξ± i))] (i : fin (n + 1)) :
measure_preserving (measurable_equiv.pi_fin_succ_above_equiv Ξ± i) :=
measure_preserving_pi_fin_succ_above_equiv (Ξ» _, volume) i
lemma measure_preserving_fun_unique {Ξ² : Type u} {m : measurable_space Ξ²} (ΞΌ : measure Ξ²)
(Ξ± : Type v) [unique Ξ±] :
measure_preserving (measurable_equiv.fun_unique Ξ± Ξ²) (measure.pi (Ξ» a : Ξ±, ΞΌ)) ΞΌ :=
begin
set e := measurable_equiv.fun_unique Ξ± Ξ²,
have : pi_premeasure (Ξ» _ : Ξ±, ΞΌ.to_outer_measure) = measure.map e.symm ΞΌ,
{ ext1 s,
rw [pi_premeasure, fintype.prod_unique, to_outer_measure_apply, e.symm.map_apply],
congr' 1, exact e.to_equiv.image_eq_preimage s },
simp only [measure.pi, outer_measure.pi, this, bounded_by_measure, to_outer_measure_to_measure],
exact (e.symm.measurable.measure_preserving _).symm e.symm
end
lemma volume_preserving_fun_unique (Ξ± : Type u) (Ξ² : Type v) [unique Ξ±] [measure_space Ξ²] :
measure_preserving (measurable_equiv.fun_unique Ξ± Ξ²) volume volume :=
measure_preserving_fun_unique volume Ξ±
lemma measure_preserving_pi_fin_two {Ξ± : fin 2 β Type u} {m : Ξ i, measurable_space (Ξ± i)}
(ΞΌ : Ξ i, measure (Ξ± i)) [β i, sigma_finite (ΞΌ i)] :
measure_preserving (measurable_equiv.pi_fin_two Ξ±) (measure.pi ΞΌ) ((ΞΌ 0).prod (ΞΌ 1)) :=
begin
refine β¨measurable_equiv.measurable _, (measure.prod_eq $ Ξ» s t hs ht, _).symmβ©,
rw [measurable_equiv.map_apply, measurable_equiv.pi_fin_two_apply, fin.preimage_apply_01_prod,
measure.pi_pi, fin.prod_univ_two],
refl
end
lemma volume_preserving_pi_fin_two (Ξ± : fin 2 β Type u) [Ξ i, measure_space (Ξ± i)]
[β i, sigma_finite (volume : measure (Ξ± i))] :
measure_preserving (measurable_equiv.pi_fin_two Ξ±) volume volume :=
measure_preserving_pi_fin_two _
lemma measure_preserving_fin_two_arrow_vec {Ξ± : Type u} {m : measurable_space Ξ±}
(ΞΌ Ξ½ : measure Ξ±) [sigma_finite ΞΌ] [sigma_finite Ξ½] :
measure_preserving measurable_equiv.fin_two_arrow (measure.pi ![ΞΌ, Ξ½]) (ΞΌ.prod Ξ½) :=
begin
haveI : β i, sigma_finite (![ΞΌ, Ξ½] i) := fin.forall_fin_two.2 β¨βΉ_βΊ, βΉ_βΊβ©,
exact measure_preserving_pi_fin_two _
end
lemma measure_preserving_fin_two_arrow {Ξ± : Type u} {m : measurable_space Ξ±}
(ΞΌ : measure Ξ±) [sigma_finite ΞΌ] :
measure_preserving measurable_equiv.fin_two_arrow (measure.pi (Ξ» _, ΞΌ)) (ΞΌ.prod ΞΌ) :=
by simpa only [matrix.vec_single_eq_const, matrix.vec_cons_const]
using measure_preserving_fin_two_arrow_vec ΞΌ ΞΌ
lemma volume_preserving_fin_two_arrow (Ξ± : Type u) [measure_space Ξ±]
[sigma_finite (volume : measure Ξ±)] :
measure_preserving (@measurable_equiv.fin_two_arrow Ξ± _) volume volume :=
measure_preserving_fin_two_arrow volume
lemma measure_preserving_pi_empty {ΞΉ : Type u} {Ξ± : ΞΉ β Type v} [is_empty ΞΉ]
{m : Ξ i, measurable_space (Ξ± i)} (ΞΌ : Ξ i, measure (Ξ± i)) :
measure_preserving (measurable_equiv.of_unique_of_unique (Ξ i, Ξ± i) unit)
(measure.pi ΞΌ) (measure.dirac ()) :=
begin
set e := (measurable_equiv.of_unique_of_unique (Ξ i, Ξ± i) unit),
refine β¨e.measurable, _β©,
rw [measure.pi_of_empty, measure.map_dirac e.measurable], refl
end
lemma volume_preserving_pi_empty {ΞΉ : Type u} (Ξ± : ΞΉ β Type v) [is_empty ΞΉ]
[Ξ i, measure_space (Ξ± i)] :
measure_preserving (measurable_equiv.of_unique_of_unique (Ξ i, Ξ± i) unit) volume volume :=
measure_preserving_pi_empty (Ξ» _, volume)
end measure_preserving
end measure_theory
|
16d17805395ccbdcca6ff16b35187248c73cbbd3 | c3f2fcd060adfa2ca29f924839d2d925e8f2c685 | /tests/lean/run/univ1.lean | 66f4d45923f6f1c5796ce8fcdba1a9653b727dcd | [
"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 | 644 | lean | import logic
namespace S1
axiom I : Type
definition F (X : Type) : Type := (X β Prop) β Prop
axiom unfold.{l} : I.{l} β F I.{l}
axiom foldd.{l} : F I.{l} β I.{l}
axiom iso1 : βx, foldd (unfold x) = x
end S1
namespace S2
universe u
axiom I : Type.{u}
definition F (X : Type) : Type := (X β Prop) β Prop
axiom unfold : I β F I
axiom foldd : F I β I
axiom iso1 : βx, foldd (unfold x) = x
end S2
namespace S3
context
hypothesis I : Type
definition F (X : Type) : Type := (X β Prop) β Prop
hypothesis unfold : I β F I
hypothesis foldd : F I β I
hypothesis iso1 : βx, foldd (unfold x) = x
end
end S3
|
f0a930507e31e221ffa8c7fc3ead9c8ea8bf001d | 4d2583807a5ac6caaffd3d7a5f646d61ca85d532 | /src/measure_theory/measure/giry_monad.lean | be269394c1034e749a87fbb7c269b7bf529981c9 | [
"Apache-2.0"
] | permissive | AntoineChambert-Loir/mathlib | 64aabb896129885f12296a799818061bc90da1ff | 07be904260ab6e36a5769680b6012f03a4727134 | refs/heads/master | 1,693,187,631,771 | 1,636,719,886,000 | 1,636,719,886,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 8,548 | 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
-/
import measure_theory.integral.lebesgue
/-!
# The Giry monad
Let X be a measurable space. The collection of all measures on X again
forms a measurable space. This construction forms a monad on
measurable spaces and measurable functions, called the Giry monad.
Note that most sources use the term "Giry monad" for the restriction
to *probability* measures. Here we include all measures on X.
See also `measure_theory/category/Meas.lean`, containing an upgrade of the type-level
monad to an honest monad of the functor `Measure : Meas β₯€ Meas`.
## References
* <https://ncatlab.org/nlab/show/Giry+monad>
## Tags
giry monad
-/
noncomputable theory
open_locale classical big_operators ennreal
open classical set filter
variables {Ξ± Ξ² Ξ³ Ξ΄ Ξ΅ : Type*}
namespace measure_theory
namespace measure
variables [measurable_space Ξ±] [measurable_space Ξ²]
/-- Measurability structure on `measure`: Measures are measurable w.r.t. all projections -/
instance : measurable_space (measure Ξ±) :=
β¨ (s : set Ξ±) (hs : measurable_set s), (borel ββ₯0β).comap (λμ, ΞΌ s)
lemma measurable_coe {s : set α} (hs : measurable_set s) : measurable (λμ : measure α, μ s) :=
measurable.of_comap_le $ le_supr_of_le s $ le_supr_of_le hs $ le_refl _
lemma measurable_of_measurable_coe (f : Ξ² β measure Ξ±)
(h : β(s : set Ξ±) (hs : measurable_set s), measurable (Ξ»b, f b s)) :
measurable f :=
measurable.of_le_map $ bsupr_le $ assume s hs, measurable_space.comap_le_iff_le_map.2 $
by rw [measurable_space.map_comp]; exact h s hs
lemma measurable_measure {ΞΌ : Ξ± β measure Ξ²} :
measurable ΞΌ β β(s : set Ξ²) (hs : measurable_set s), measurable (Ξ»b, ΞΌ b s) :=
β¨Ξ» hΞΌ s hs, (measurable_coe hs).comp hΞΌ, measurable_of_measurable_coe ΞΌβ©
lemma measurable_map (f : Ξ± β Ξ²) (hf : measurable f) :
measurable (λμ : measure α, map f μ) :=
measurable_of_measurable_coe _ $ assume s hs,
suffices measurable (Ξ» (ΞΌ : measure Ξ±), ΞΌ (f β»ΒΉ' s)),
by simpa [map_apply, hs, hf],
measurable_coe (hf hs)
lemma measurable_dirac :
measurable (measure.dirac : Ξ± β measure Ξ±) :=
measurable_of_measurable_coe _ $ assume s hs,
begin
simp only [dirac_apply', hs],
exact measurable_one.indicator hs
end
lemma measurable_lintegral {f : Ξ± β ββ₯0β} (hf : measurable f) :
measurable (λμ : measure Ξ±, β«β» x, f x βΞΌ) :=
begin
simp only [lintegral_eq_supr_eapprox_lintegral, hf, simple_func.lintegral],
refine measurable_supr (Ξ» n, finset.measurable_sum _ (Ξ» i _, _)),
refine measurable.const_mul _ _,
exact measurable_coe ((simple_func.eapprox f n).measurable_set_preimage _)
end
/-- Monadic join on `measure` in the category of measurable spaces and measurable
functions. -/
def join (m : measure (measure Ξ±)) : measure Ξ± :=
measure.of_measurable
(Ξ»s hs, β«β» ΞΌ, ΞΌ s βm)
(by simp)
begin
assume f hf h,
simp [measure_Union h hf],
apply lintegral_tsum,
assume i, exact measurable_coe (hf i)
end
@[simp] lemma join_apply {m : measure (measure Ξ±)} :
β{s : set Ξ±}, measurable_set s β join m s = β«β» ΞΌ, ΞΌ s βm :=
measure.of_measurable_apply
@[simp] lemma join_zero : (0 : measure (measure Ξ±)).join = 0 :=
by { ext1 s hs, simp [hs] }
lemma measurable_join : measurable (join : measure (measure Ξ±) β measure Ξ±) :=
measurable_of_measurable_coe _ $ assume s hs,
by simp only [join_apply hs]; exact measurable_lintegral (measurable_coe hs)
lemma lintegral_join {m : measure (measure Ξ±)} {f : Ξ± β ββ₯0β} (hf : measurable f) :
β«β» x, f x β(join m) = β«β» ΞΌ, β«β» x, f x βΞΌ βm :=
begin
rw [lintegral_eq_supr_eapprox_lintegral hf],
have : βn x,
join m (β(simple_func.eapprox (Ξ» (a : Ξ±), f a) n) β»ΒΉ' {x}) =
β«β» ΞΌ, ΞΌ ((β(simple_func.eapprox (Ξ» (a : Ξ±), f a) n) β»ΒΉ' {x})) βm :=
assume n x, join_apply (simple_func.measurable_set_preimage _ _),
simp only [simple_func.lintegral, this],
transitivity,
have : β(s : β β finset ββ₯0β) (f : β β ββ₯0β β measure Ξ± β ββ₯0β)
(hf : βn r, measurable (f n r)) (hm : monotone (Ξ»n ΞΌ, β r in s n, r * f n r ΞΌ)),
(β¨n:β, β r in s n, r * β«β» ΞΌ, f n r ΞΌ βm) =
β«β» ΞΌ, β¨n:β, β r in s n, r * f n r ΞΌ βm,
{ assume s f hf hm,
symmetry,
transitivity,
apply lintegral_supr,
{ assume n,
exact finset.measurable_sum _ (assume r _, (hf _ _).const_mul _) },
{ exact hm },
congr, funext n,
transitivity,
apply lintegral_finset_sum,
{ assume r _, exact (hf _ _).const_mul _ },
congr, funext r,
apply lintegral_const_mul,
exact hf _ _ },
specialize this (Ξ»n, simple_func.range (simple_func.eapprox f n)),
specialize this
(Ξ»n r ΞΌ, ΞΌ (β(simple_func.eapprox (Ξ» (a : Ξ±), f a) n) β»ΒΉ' {r})),
refine this _ _; clear this,
{ assume n r,
apply measurable_coe,
exact simple_func.measurable_set_preimage _ _ },
{ change monotone (Ξ»n ΞΌ, (simple_func.eapprox f n).lintegral ΞΌ),
assume n m h ΞΌ,
refine simple_func.lintegral_mono _ (le_refl _),
apply simple_func.monotone_eapprox,
assumption },
congr, funext ΞΌ,
symmetry,
apply lintegral_eq_supr_eapprox_lintegral,
exact hf
end
/-- Monadic bind on `measure`, only works in the category of measurable spaces and measurable
functions. When the function `f` is not measurable the result is not well defined. -/
def bind (m : measure Ξ±) (f : Ξ± β measure Ξ²) : measure Ξ² := join (map f m)
@[simp] lemma bind_zero_left (f : Ξ± β measure Ξ²) : bind 0 f = 0 :=
by simp [bind]
@[simp] lemma bind_zero_right (m : measure Ξ±) :
bind m (0 : Ξ± β measure Ξ²) = 0 :=
begin
ext1 s hs,
simp only [bind, hs, join_apply, coe_zero, pi.zero_apply],
rw [lintegral_map (measurable_coe hs) measurable_zero],
simp
end
@[simp] lemma bind_zero_right' (m : measure Ξ±) :
bind m (Ξ» _, 0 : Ξ± β measure Ξ²) = 0 :=
bind_zero_right m
@[simp] lemma bind_apply {m : measure Ξ±} {f : Ξ± β measure Ξ²} {s : set Ξ²}
(hs : measurable_set s) (hf : measurable f) :
bind m f s = β«β» a, f a s βm :=
by rw [bind, join_apply hs, lintegral_map (measurable_coe hs) hf]
lemma measurable_bind' {g : Ξ± β measure Ξ²} (hg : measurable g) : measurable (Ξ»m, bind m g) :=
measurable_join.comp (measurable_map _ hg)
lemma lintegral_bind {m : measure Ξ±} {ΞΌ : Ξ± β measure Ξ²} {f : Ξ² β ββ₯0β}
(hΞΌ : measurable ΞΌ) (hf : measurable f) :
β«β» x, f x β (bind m ΞΌ) = β«β» a, β«β» x, f x β(ΞΌ a) βm:=
(lintegral_join hf).trans (lintegral_map (measurable_lintegral hf) hΞΌ)
lemma bind_bind {Ξ³} [measurable_space Ξ³] {m : measure Ξ±} {f : Ξ± β measure Ξ²} {g : Ξ² β measure Ξ³}
(hf : measurable f) (hg : measurable g) :
bind (bind m f) g = bind m (Ξ»a, bind (f a) g) :=
measure.ext $ assume s hs,
begin
rw [bind_apply hs hg, bind_apply hs ((measurable_bind' hg).comp hf), lintegral_bind hf],
{ congr, funext a,
exact (bind_apply hs hg).symm },
exact (measurable_coe hs).comp hg
end
lemma bind_dirac {f : Ξ± β measure Ξ²} (hf : measurable f) (a : Ξ±) : bind (dirac a) f = f a :=
measure.ext $ Ξ» s hs, by rw [bind_apply hs hf, lintegral_dirac' a ((measurable_coe hs).comp hf)]
lemma dirac_bind {m : measure Ξ±} : bind m dirac = m :=
measure.ext $ assume s hs,
by simp [bind_apply hs measurable_dirac, dirac_apply' _ hs, lintegral_indicator 1 hs]
lemma join_eq_bind (ΞΌ : measure (measure Ξ±)) : join ΞΌ = bind ΞΌ id :=
by rw [bind, map_id]
lemma join_map_map {f : Ξ± β Ξ²} (hf : measurable f) (ΞΌ : measure (measure Ξ±)) :
join (map (map f) ΞΌ) = map f (join ΞΌ) :=
measure.ext $ assume s hs,
begin
rw [join_apply hs, map_apply hf hs, join_apply,
lintegral_map (measurable_coe hs) (measurable_map f hf)],
{ congr, funext Ξ½, exact map_apply hf hs },
exact hf hs
end
lemma join_map_join (ΞΌ : measure (measure (measure Ξ±))) :
join (map join ΞΌ) = join (join ΞΌ) :=
begin
show bind ΞΌ join = join (join ΞΌ),
rw [join_eq_bind, join_eq_bind, bind_bind measurable_id measurable_id],
apply congr_arg (bind ΞΌ),
funext Ξ½,
exact join_eq_bind Ξ½
end
lemma join_map_dirac (ΞΌ : measure Ξ±) : join (map dirac ΞΌ) = ΞΌ :=
dirac_bind
lemma join_dirac (ΞΌ : measure Ξ±) : join (dirac ΞΌ) = ΞΌ :=
eq.trans (join_eq_bind (dirac ΞΌ)) (bind_dirac measurable_id _)
end measure
end measure_theory
|
097d13d9717d99dacc1eddb4eedc89d3ad15b5b2 | 00d2363f9655e2a7618f6b94dda7e2c4e5cf8d19 | /src/parsing_example.lean | 14e20939730d02f16d307f7e1f7b6e6f460a180d | [
"Apache-2.0"
] | permissive | devjuice1/lean_proof_recording | 927e276e2ab8fb1288f51d9146dcfbf0d6444a87 | bf7c527315deccd35363fa7ca89d97d7b9cb6ac1 | refs/heads/master | 1,692,914,925,585 | 1,633,018,872,000 | 1,633,018,872,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,518 | lean | import init.meta.lean.parser
--set_option pp.colors false
namespace parser
open lean
meta def get_options : parser options :=
Ξ» s, interaction_monad.result.success s.options s
meta def myparser : parser unit := do
p <- parser.cur_pos,
o <- get_options,
tactic.trace "Inside parser",
tactic.trace o,
tactic.trace p,
return ()
@[reducible] protected meta def my_itactic : parser (tactic unit) := parser.val (do
pos <- parser.cur_pos,
tactic.trace pos,
a <- parser.itactic_reflected,
pos <- parser.cur_pos,
tactic.trace pos,
return a)
meta def pr_parser {Ξ± : Type} (p : parser Ξ±) [lean.parser.reflectable p] : parser Ξ± := do
pos <- parser.cur_pos,
tactic.trace pos,
a <- p,
pos <- parser.cur_pos,
tactic.trace pos,
return a
end parser
meta def tactic.interactive.my_try (t : interactive.parse parser.my_itactic) : tactic unit := tactic.try t
meta def tactic.interactive.my_apply (q : interactive.parse $ parser.pr_parser $ interactive.types.texpr) : tactic unit :=
tactic.interactive.concat_tags (do h β tactic.i_to_expr_for_apply q, tactic.apply h)
set_option pp.all true
#check interactive.parse $ interactive.types.texpr
#check interactive.parse $ parser.pr_parser $ interactive.types.texpr
#check interactive.parse $ parser.my_itactic
#check interactive.parse $ lean.parser.itactic
-- #check interactive.parse $ parser.pr_parser $ lean.parser.itactic
#check @lean.parser.reflectable.cast (tactic.{0} unit) lean.parser.itactic_reflected
example : true := begin
my_try { my_apply true.intro },
end |
b26a778349cb8d08a95981e95bb0f6a03abdf7e7 | 7cef822f3b952965621309e88eadf618da0c8ae9 | /src/ring_theory/polynomial.lean | b486c6ed84ccb54713c67b95f69d1e6a196bf477 | [
"Apache-2.0"
] | permissive | rmitta/mathlib | 8d90aee30b4db2b013e01f62c33f297d7e64a43d | 883d974b608845bad30ae19e27e33c285200bf84 | refs/heads/master | 1,585,776,832,544 | 1,576,874,096,000 | 1,576,874,096,000 | 153,663,165 | 0 | 2 | Apache-2.0 | 1,544,806,490,000 | 1,539,884,365,000 | Lean | UTF-8 | Lean | false | false | 14,166 | lean | /-
Copyright (c) 2019 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau
Ring-theoretic supplement of data.polynomial.
Main result: Hilbert basis theorem, that if a ring is noetherian then so is its polynomial ring.
-/
import data.polynomial data.mv_polynomial
import ring_theory.subring
import ring_theory.ideals ring_theory.noetherian
noncomputable theory
local attribute [instance, priority 100] classical.prop_decidable
universes u v w
namespace polynomial
variables (R : Type u) [comm_ring R] [decidable_eq R]
/-- The `R`-submodule of `R[X]` consisting of polynomials of degree β€ `n`. -/
def degree_le (n : with_bot β) : submodule R (polynomial R) :=
β¨
k : β, β¨
h : βk > n, (lcoeff R k).ker
variable {R}
theorem mem_degree_le {n : with_bot β} {f : polynomial R} :
f β degree_le R n β degree f β€ n :=
by simp only [degree_le, submodule.mem_infi, degree_le_iff_coeff_zero, linear_map.mem_ker]; refl
theorem degree_le_mono {m n : with_bot β} (H : m β€ n) :
degree_le R m β€ degree_le R n :=
Ξ» f hf, mem_degree_le.2 (le_trans (mem_degree_le.1 hf) H)
theorem degree_le_eq_span_X_pow {n : β} :
degree_le R n = submodule.span R β((finset.range (n+1)).image (Ξ» n, X^n) : finset (polynomial R)) :=
begin
apply le_antisymm,
{ intros p hp, replace hp := mem_degree_le.1 hp,
rw [β finsupp.sum_single p, finsupp.sum, submodule.mem_coe],
refine submodule.sum_mem _ (Ξ» k hk, _),
have := with_bot.coe_le_coe.1 (finset.sup_le_iff.1 hp k hk),
rw [single_eq_C_mul_X, C_mul'],
refine submodule.smul_mem _ _ (submodule.subset_span $ finset.mem_coe.2 $
finset.mem_image.2 β¨_, finset.mem_range.2 (nat.lt_succ_of_le this), rflβ©) },
rw [submodule.span_le, finset.coe_image, set.image_subset_iff],
intros k hk, apply mem_degree_le.2,
apply le_trans (degree_X_pow_le _) (with_bot.coe_le_coe.2 $ nat.le_of_lt_succ $ finset.mem_range.1 hk)
end
/-- Given a polynomial, return the polynomial whose coefficients are in
the ring closure of the original coefficients. -/
def restriction (p : polynomial R) : polynomial (ring.closure (βp.frange : set R)) :=
β¨p.support, Ξ» i, β¨p.to_fun i,
if H : p.to_fun i = 0 then H.symm βΈ is_add_submonoid.zero_mem _
else ring.subset_closure $ finsupp.mem_frange.2 β¨H, i, rflβ©β©,
Ξ» i, finsupp.mem_support_iff.trans (not_iff_not_of_iff β¨Ξ» H, subtype.eq H, subtype.mk.injβ©)β©
@[simp] theorem coeff_restriction {p : polynomial R} {n : β} : β(coeff (restriction p) n) = coeff p n := rfl
@[simp] theorem coeff_restriction' {p : polynomial R} {n : β} : (coeff (restriction p) n).1 = coeff p n := rfl
@[simp] theorem degree_restriction {p : polynomial R} : (restriction p).degree = p.degree := rfl
@[simp] theorem nat_degree_restriction {p : polynomial R} : (restriction p).nat_degree = p.nat_degree := rfl
@[simp] theorem monic_restriction {p : polynomial R} : monic (restriction p) β monic p :=
β¨Ξ» H, congr_arg subtype.val H, Ξ» H, subtype.eq Hβ©
@[simp] theorem restriction_zero : restriction (0 : polynomial R) = 0 := rfl
@[simp] theorem restriction_one : restriction (1 : polynomial R) = 1 :=
ext $ Ξ» i, subtype.eq $ by rw [coeff_restriction', coeff_one, coeff_one]; split_ifs; refl
variables {S : Type v} [comm_ring S] {f : R β S} {x : S}
theorem evalβ_restriction {p : polynomial R} :
evalβ f x p = evalβ (f β subtype.val) x p.restriction :=
rfl
section to_subring
variables (p : polynomial R) (T : set R) [is_subring T]
/-- Given a polynomial `p` and a subring `T` that contains the coefficients of `p`,
return the corresponding polynomial whose coefficients are in `T. -/
def to_subring (hp : βp.frange β T) : polynomial T :=
β¨p.support, Ξ» i, β¨p.to_fun i,
if H : p.to_fun i = 0 then H.symm βΈ is_add_submonoid.zero_mem _
else hp $ finsupp.mem_frange.2 β¨H, i, rflβ©β©,
Ξ» i, finsupp.mem_support_iff.trans (not_iff_not_of_iff β¨Ξ» H, subtype.eq H, subtype.mk.injβ©)β©
variables (hp : βp.frange β T)
include hp
@[simp] theorem coeff_to_subring {n : β} : β(coeff (to_subring p T hp) n) = coeff p n := rfl
@[simp] theorem coeff_to_subring' {n : β} : (coeff (to_subring p T hp) n).1 = coeff p n := rfl
@[simp] theorem degree_to_subring : (to_subring p T hp).degree = p.degree := rfl
@[simp] theorem nat_degree_to_subring : (to_subring p T hp).nat_degree = p.nat_degree := rfl
@[simp] theorem monic_to_subring : monic (to_subring p T hp) β monic p :=
β¨Ξ» H, congr_arg subtype.val H, Ξ» H, subtype.eq Hβ©
omit hp
@[simp] theorem to_subring_zero : to_subring (0 : polynomial R) T (set.empty_subset _) = 0 := rfl
@[simp] theorem to_subring_one : to_subring (1 : polynomial R) T
(set.subset.trans (finset.coe_subset.2 finsupp.frange_single)
(set.singleton_subset_iff.2 (is_submonoid.one_mem _))) = 1 :=
ext $ Ξ» i, subtype.eq $ by rw [coeff_to_subring', coeff_one, coeff_one]; split_ifs; refl
end to_subring
variables (T : set R) [is_subring T]
/-- Given a polynomial whose coefficients are in some subring, return
the corresponding polynomial whose coefificents are in the ambient ring. -/
def of_subring (p : polynomial T) : polynomial R :=
β¨p.support, subtype.val β p.to_fun,
Ξ» n, finsupp.mem_support_iff.trans (not_iff_not_of_iff
β¨Ξ» h, congr_arg subtype.val h, Ξ» h, subtype.eq hβ©)β©
@[simp] theorem frange_of_subring {p : polynomial T} :
β(p.of_subring T).frange β T :=
Ξ» y H, let β¨hy, x, hxβ© := finsupp.mem_frange.1 H in hx βΈ (p.to_fun x).2
end polynomial
variables {R : Type u} [comm_ring R] [decidable_eq R]
namespace ideal
open polynomial
/-- Transport an ideal of `R[X]` to an `R`-submodule of `R[X]`. -/
def of_polynomial (I : ideal (polynomial R)) : submodule R (polynomial R) :=
{ carrier := I.carrier,
zero := I.zero_mem,
add := Ξ» _ _, I.add_mem,
smul := Ξ» c x H, by rw [β C_mul']; exact submodule.smul_mem _ _ H }
variables {I : ideal (polynomial R)}
theorem mem_of_polynomial (x) : x β I.of_polynomial β x β I := iff.rfl
variables (I)
/-- Given an ideal `I` of `R[X]`, make the `R`-submodule of `I`
consisting of polynomials of degree β€ `n`. -/
def degree_le (n : with_bot β) : submodule R (polynomial R) :=
degree_le R n β I.of_polynomial
/-- Given an ideal `I` of `R[X]`, make the ideal in `R` of
leading coefficients of polynomials in `I` with degree β€ `n`. -/
def leading_coeff_nth (n : β) : ideal R :=
(I.degree_le n).map $ lcoeff R n
theorem mem_leading_coeff_nth (n : β) (x) :
x β I.leading_coeff_nth n β β p β I, degree p β€ n β§ leading_coeff p = x :=
begin
simp only [leading_coeff_nth, degree_le, submodule.mem_map, lcoeff_apply, submodule.mem_inf, mem_degree_le],
split,
{ rintro β¨p, β¨hpdeg, hpIβ©, rflβ©,
cases lt_or_eq_of_le hpdeg with hpdeg hpdeg,
{ refine β¨0, I.zero_mem, lattice.bot_le, _β©,
rw [leading_coeff_zero, eq_comm],
exact coeff_eq_zero_of_degree_lt hpdeg },
{ refine β¨p, hpI, le_of_eq hpdeg, _β©,
rw [leading_coeff, nat_degree, hpdeg], refl } },
{ rintro β¨p, hpI, hpdeg, rflβ©,
have : nat_degree p + (n - nat_degree p) = n,
{ exact nat.add_sub_cancel' (nat_degree_le_of_degree_le hpdeg) },
refine β¨p * X ^ (n - nat_degree p), β¨_, I.mul_mem_right hpIβ©, _β©,
{ apply le_trans (degree_mul_le _ _) _,
apply le_trans (add_le_add' (degree_le_nat_degree) (degree_X_pow_le _)) _,
rw [β with_bot.coe_add, this],
exact le_refl _ },
{ rw [leading_coeff, β coeff_mul_X_pow p (n - nat_degree p), this] } }
end
theorem mem_leading_coeff_nth_zero (x) :
x β I.leading_coeff_nth 0 β C x β I :=
(mem_leading_coeff_nth _ _ _).trans
β¨Ξ» β¨p, hpI, hpdeg, hpxβ©, by rwa [β hpx, leading_coeff,
nat.eq_zero_of_le_zero (nat_degree_le_of_degree_le hpdeg),
β eq_C_of_degree_le_zero hpdeg],
Ξ» hx, β¨C x, hx, degree_C_le, leading_coeff_C xβ©β©
theorem leading_coeff_nth_mono {m n : β} (H : m β€ n) :
I.leading_coeff_nth m β€ I.leading_coeff_nth n :=
begin
intros r hr,
simp only [submodule.mem_coe, mem_leading_coeff_nth] at hr β’,
rcases hr with β¨p, hpI, hpdeg, rflβ©,
refine β¨p * X ^ (n - m), I.mul_mem_right hpI, _, leading_coeff_mul_X_powβ©,
refine le_trans (degree_mul_le _ _) _,
refine le_trans (add_le_add' hpdeg (degree_X_pow_le _)) _,
rw [β with_bot.coe_add, nat.add_sub_cancel' H],
exact le_refl _
end
/-- Given an ideal `I` in `R[X]`, make the ideal in `R` of the
leading coefficients in `I`. -/
def leading_coeff : ideal R :=
β¨ n : β, I.leading_coeff_nth n
theorem mem_leading_coeff (x) :
x β I.leading_coeff β β p β I, polynomial.leading_coeff p = x :=
begin
rw [leading_coeff, submodule.mem_supr_of_directed],
simp only [mem_leading_coeff_nth],
{ split, { rintro β¨i, p, hpI, hpdeg, rflβ©, exact β¨p, hpI, rflβ© },
rintro β¨p, hpI, rflβ©, exact β¨nat_degree p, p, hpI, degree_le_nat_degree, rflβ© },
{ exact β¨0β© },
intros i j, exact β¨i + j, I.leading_coeff_nth_mono (nat.le_add_right _ _),
I.leading_coeff_nth_mono (nat.le_add_left _ _)β©
end
theorem is_fg_degree_le [is_noetherian_ring R] (n : β) :
submodule.fg (I.degree_le n) :=
is_noetherian_submodule_left.1 (is_noetherian_of_fg_of_noetherian _
β¨_, degree_le_eq_span_X_pow.symmβ©) _
end ideal
/-- Hilbert basis theorem. -/
theorem is_noetherian_ring_polynomial [is_noetherian_ring R] : is_noetherian_ring (polynomial R) :=
β¨assume I : ideal (polynomial R),
let L := I.leading_coeff in
let M := well_founded.min (is_noetherian_iff_well_founded.1 (by apply_instance))
(set.range I.leading_coeff_nth) β¨_, β¨0, rflβ©β© in
have hm : M β set.range I.leading_coeff_nth := well_founded.min_mem _ _ _,
let β¨N, HNβ© := hm, β¨s, hsβ© := I.is_fg_degree_le N in
have hm2 : β k, I.leading_coeff_nth k β€ M := Ξ» k, or.cases_on (le_or_lt k N)
(Ξ» h, HN βΈ I.leading_coeff_nth_mono h)
(Ξ» h x hx, classical.by_contradiction $ Ξ» hxm,
have Β¬M < I.leading_coeff_nth k, by refine well_founded.not_lt_min
(well_founded_submodule_gt _ _) _ _ _; exact β¨k, rflβ©,
this β¨HN βΈ I.leading_coeff_nth_mono (le_of_lt h), Ξ» H, hxm (H hx)β©),
have hs2 : β {x}, x β I.degree_le N β x β ideal.span (βs : set (polynomial R)),
from hs βΈ Ξ» x hx, submodule.span_induction hx (Ξ» _ hx, ideal.subset_span hx) (ideal.zero_mem _)
(Ξ» _ _, ideal.add_mem _) (Ξ» c f hf, f.C_mul' c βΈ ideal.mul_mem_left _ hf),
β¨s, le_antisymm (ideal.span_le.2 $ Ξ» x hx, have x β I.degree_le N, from hs βΈ submodule.subset_span hx, this.2) $ begin
change I β€ ideal.span βs,
intros p hp, generalize hn : p.nat_degree = k,
induction k using nat.strong_induction_on with k ih generalizing p,
cases le_or_lt k N,
{ subst k, refine hs2 β¨polynomial.mem_degree_le.2
(le_trans polynomial.degree_le_nat_degree $ with_bot.coe_le_coe.2 h), hpβ© },
{ have hp0 : p β 0,
{ rintro rfl, cases hn, exact nat.not_lt_zero _ h },
have : (0 : R) β 1,
{ intro h, apply hp0, ext i, refine (mul_one _).symm.trans _,
rw [β h, mul_zero], refl },
letI : nonzero_comm_ring R := { zero_ne_one := this,
..(infer_instance : comm_ring R) },
have : p.leading_coeff β I.leading_coeff_nth N,
{ rw HN, exact hm2 k ((I.mem_leading_coeff_nth _ _).2
β¨_, hp, hn βΈ polynomial.degree_le_nat_degree, rflβ©) },
rw I.mem_leading_coeff_nth at this,
rcases this with β¨q, hq, hdq, hlqpβ©,
have hq0 : q β 0,
{ intro H, rw [β polynomial.leading_coeff_eq_zero] at H,
rw [hlqp, polynomial.leading_coeff_eq_zero] at H, exact hp0 H },
have h1 : p.degree = (q * polynomial.X ^ (k - q.nat_degree)).degree,
{ rw [polynomial.degree_mul_eq', polynomial.degree_X_pow],
rw [polynomial.degree_eq_nat_degree hp0, polynomial.degree_eq_nat_degree hq0],
rw [β with_bot.coe_add, nat.add_sub_cancel', hn],
{ refine le_trans (polynomial.nat_degree_le_of_degree_le hdq) (le_of_lt h) },
rw [polynomial.leading_coeff_X_pow, mul_one],
exact mt polynomial.leading_coeff_eq_zero.1 hq0 },
have h2 : p.leading_coeff = (q * polynomial.X ^ (k - q.nat_degree)).leading_coeff,
{ rw [β hlqp, polynomial.leading_coeff_mul_X_pow] },
have := polynomial.degree_sub_lt h1 hp0 h2,
rw [polynomial.degree_eq_nat_degree hp0] at this,
rw β sub_add_cancel p (q * polynomial.X ^ (k - q.nat_degree)),
refine (ideal.span βs).add_mem _ ((ideal.span βs).mul_mem_right _),
{ by_cases hpq : p - q * polynomial.X ^ (k - q.nat_degree) = 0,
{ rw hpq, exact ideal.zero_mem _ },
refine ih _ _ (I.sub_mem hp (I.mul_mem_right hq)) rfl,
rwa [polynomial.degree_eq_nat_degree hpq, with_bot.coe_lt_coe, hn] at this },
exact hs2 β¨polynomial.mem_degree_le.2 hdq, hqβ© }
endβ©β©
theorem is_noetherian_ring_mv_polynomial_fin {n : β} [is_noetherian_ring R] :
is_noetherian_ring (mv_polynomial (fin n) R) :=
begin
induction n with n ih,
{ exact is_noetherian_ring_of_ring_equiv R
((mv_polynomial.pempty_ring_equiv R).symm.trans $ mv_polynomial.ring_equiv_of_equiv _
β¨pempty.elim, fin.elim0, Ξ» x, pempty.elim x, Ξ» x, fin.elim0 xβ©) },
exact @is_noetherian_ring_of_ring_equiv (polynomial (mv_polynomial (fin n) R)) _
(mv_polynomial (fin (n+1)) R) _
((mv_polynomial.option_equiv_left _ _).symm.trans (mv_polynomial.ring_equiv_of_equiv _
β¨Ξ» x, option.rec_on x 0 fin.succ, Ξ» x, fin.cases none some x,
by rintro β¨none | xβ©; [refl, exact fin.cases_succ _],
Ξ» x, fin.cases rfl (Ξ» i, show (option.rec_on (fin.cases none some (fin.succ i) : option (fin n))
0 fin.succ : fin n.succ) = _, by rw fin.cases_succ) xβ©))
(@@is_noetherian_ring_polynomial _ _ ih)
end
theorem is_noetherian_ring_mv_polynomial_of_fintype {Ο : Type v} [fintype Ο] [decidable_eq Ο]
[is_noetherian_ring R] : is_noetherian_ring (mv_polynomial Ο R) :=
trunc.induction_on (fintype.equiv_fin Ο) $ Ξ» e,
@is_noetherian_ring_of_ring_equiv (mv_polynomial (fin (fintype.card Ο)) R) _ _ _
(mv_polynomial.ring_equiv_of_equiv _ e.symm) is_noetherian_ring_mv_polynomial_fin
|
295974f8d636d6dffb57e9575521552a114b5eb8 | e00ea76a720126cf9f6d732ad6216b5b824d20a7 | /src/data/equiv/functor.lean | adbf42f84919396ce8e6cc2d00e52cab4940b589 | [
"Apache-2.0"
] | permissive | vaibhavkarve/mathlib | a574aaf68c0a431a47fa82ce0637f0f769826bfe | 17f8340912468f49bdc30acdb9a9fa02eeb0473a | refs/heads/master | 1,621,263,802,637 | 1,585,399,588,000 | 1,585,399,588,000 | 250,833,447 | 0 | 0 | Apache-2.0 | 1,585,410,341,000 | 1,585,410,341,000 | null | UTF-8 | Lean | false | false | 2,256 | lean | /-
Copyright (c) 2019 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin, Simon Hudon, Scott Morrison
-/
import data.equiv.basic
import category.bifunctor
/-!
# Functor and bifunctors can be applied to `equiv`s.
We define
```lean
def functor.map_equiv (f : Type u β Type v) [functor f] [is_lawful_functor f] :
Ξ± β Ξ² β f Ξ± β f Ξ²
```
and
```lean
def bifunctor.map_equiv (F : Type u β Type v β Type w) [bifunctor F] [is_lawful_bifunctor F] :
Ξ± β Ξ² β Ξ±' β Ξ²' β F Ξ± Ξ±' β F Ξ² Ξ²'
```
-/
universes u v w
variables {Ξ± Ξ² : Type u}
open equiv
namespace functor
variables (f : Type u β Type v) [functor f] [is_lawful_functor f]
/-- Apply a functor to an `equiv`. -/
def map_equiv (h : Ξ± β Ξ²) : f Ξ± β f Ξ² :=
{ to_fun := map h,
inv_fun := map h.symm,
left_inv := Ξ» x,
by { rw map_map, convert is_lawful_functor.id_map x, ext a, apply symm_apply_apply },
right_inv := Ξ» x,
by { rw map_map, convert is_lawful_functor.id_map x, ext a, apply apply_symm_apply } }
@[simp]
lemma map_equiv_apply (h : Ξ± β Ξ²) (x : f Ξ±) :
(map_equiv f h : f Ξ± β f Ξ²) x = map h x := rfl
@[simp]
lemma map_equiv_symm_apply (h : Ξ± β Ξ²) (y : f Ξ²) :
(map_equiv f h : f Ξ± β f Ξ²).symm y = map h.symm y := rfl
end functor
namespace bifunctor
variables {Ξ±' Ξ²' : Type v} (F : Type u β Type v β Type w) [bifunctor F] [is_lawful_bifunctor F]
/-- Apply a bifunctor to a pair of `equiv`s. -/
def map_equiv (h : Ξ± β Ξ²) (h' : Ξ±' β Ξ²') : F Ξ± Ξ±' β F Ξ² Ξ²' :=
{ to_fun := bimap h h',
inv_fun := bimap h.symm h'.symm,
left_inv := Ξ» x,
by { rw bimap_bimap, convert is_lawful_bifunctor.id_bimap x; { ext a, apply symm_apply_apply } },
right_inv := Ξ» x,
by { rw bimap_bimap, convert is_lawful_bifunctor.id_bimap x; { ext a, apply apply_symm_apply } } }
@[simp]
lemma map_equiv_apply (h : Ξ± β Ξ²) (h' : Ξ±' β Ξ²') (x : F Ξ± Ξ±') :
(map_equiv F h h' : F Ξ± Ξ±' β F Ξ² Ξ²') x = bimap h h' x := rfl
@[simp]
lemma map_equiv_symm_apply (h : Ξ± β Ξ²) (h' : Ξ±' β Ξ²') (y : F Ξ² Ξ²') :
(map_equiv F h h' : F Ξ± Ξ±' β F Ξ² Ξ²').symm y = bimap h.symm h'.symm y := rfl
end bifunctor
|
77d9ef8800dfad59a33baa65c4772d2552137360 | 2eab05920d6eeb06665e1a6df77b3157354316ad | /src/analysis/convex/basic.lean | c68fac1eead759b10854210e579617f6b74d7c7f | [
"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 | 39,557 | lean | /-
Copyright (c) 2019 Alexander Bentkamp. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Alexander Bentkamp, Yury Kudriashov, YaΓ«l Dillies
-/
import algebra.order.module
import linear_algebra.affine_space.affine_subspace
/-!
# Convex sets and functions in vector spaces
In a π-vector space, we define the following objects and properties.
* `segment π x y`: Closed segment joining `x` and `y`.
* `open_segment π x y`: Open segment joining `x` and `y`.
* `convex π s`: A set `s` is convex if for any two points `x y β s` it includes `segment π x y`.
* `std_simplex π ΞΉ`: The standard simplex in `ΞΉ β π` (currently requires `fintype ΞΉ`). It is the
intersection of the positive quadrant with the hyperplane `s.sum = 1`.
We also provide various equivalent versions of the definitions above, prove that some specific sets
are convex.
## Notations
We provide the following notation:
* `[x -[π] y] = segment π x y` in locale `convex`
## TODO
Generalize all this file to affine spaces.
Should we rename `segment` and `open_segment` to `convex.Icc` and `convex.Ioo`? Should we also
define `clopen_segment`/`convex.Ico`/`convex.Ioc`?
-/
variables {π E F Ξ² : Type*}
open linear_map set
open_locale big_operators classical pointwise
/-! ### Segment -/
section ordered_semiring
variables [ordered_semiring π] [add_comm_monoid E]
section has_scalar
variables (π) [has_scalar π E]
/-- Segments in a vector space. -/
def segment (x y : E) : set E :=
{z : E | β (a b : π) (ha : 0 β€ a) (hb : 0 β€ b) (hab : a + b = 1), a β’ x + b β’ y = z}
/-- Open segment in a vector space. Note that `open_segment π x x = {x}` instead of being `β
` when
the base semiring has some element between `0` and `1`. -/
def open_segment (x y : E) : set E :=
{z : E | β (a b : π) (ha : 0 < a) (hb : 0 < b) (hab : a + b = 1), a β’ x + b β’ y = z}
localized "notation `[` x ` -[` π `] ` y `]` := segment π x y" in convex
lemma segment_symm (x y : E) : [x -[π] y] = [y -[π] x] :=
set.ext $ Ξ» z,
β¨Ξ» β¨a, b, ha, hb, hab, Hβ©, β¨b, a, hb, ha, (add_comm _ _).trans hab, (add_comm _ _).trans Hβ©,
Ξ» β¨a, b, ha, hb, hab, Hβ©, β¨b, a, hb, ha, (add_comm _ _).trans hab, (add_comm _ _).trans Hβ©β©
lemma open_segment_symm (x y : E) :
open_segment π x y = open_segment π y x :=
set.ext $ Ξ» z,
β¨Ξ» β¨a, b, ha, hb, hab, Hβ©, β¨b, a, hb, ha, (add_comm _ _).trans hab, (add_comm _ _).trans Hβ©,
Ξ» β¨a, b, ha, hb, hab, Hβ©, β¨b, a, hb, ha, (add_comm _ _).trans hab, (add_comm _ _).trans Hβ©β©
lemma open_segment_subset_segment (x y : E) :
open_segment π x y β [x -[π] y] :=
Ξ» z β¨a, b, ha, hb, hab, hzβ©, β¨a, b, ha.le, hb.le, hab, hzβ©
end has_scalar
open_locale convex
section mul_action_with_zero
variables (π) [mul_action_with_zero π E]
lemma left_mem_segment (x y : E) : x β [x -[π] y] :=
β¨1, 0, zero_le_one, le_refl 0, add_zero 1, by rw [zero_smul, one_smul, add_zero]β©
lemma right_mem_segment (x y : E) : y β [x -[π] y] :=
segment_symm π y x βΈ left_mem_segment π y x
end mul_action_with_zero
section module
variables (π) [module π E]
lemma segment_same (x : E) : [x -[π] x] = {x} :=
set.ext $ Ξ» z, β¨Ξ» β¨a, b, ha, hb, hab, hzβ©,
by simpa only [(add_smul _ _ _).symm, mem_singleton_iff, hab, one_smul, eq_comm] using hz,
Ξ» h, mem_singleton_iff.1 h βΈ left_mem_segment π z zβ©
lemma mem_open_segment_of_ne_left_right {x y z : E} (hx : x β z) (hy : y β z)
(hz : z β [x -[π] y]) :
z β open_segment π x y :=
begin
obtain β¨a, b, ha, hb, hab, hzβ© := hz,
by_cases ha' : a = 0,
{ rw [ha', zero_add] at hab,
rw [ha', hab, zero_smul, one_smul, zero_add] at hz,
exact (hy hz).elim },
by_cases hb' : b = 0,
{ rw [hb', add_zero] at hab,
rw [hb', hab, zero_smul, one_smul, add_zero] at hz,
exact (hx hz).elim },
exact β¨a, b, ha.lt_of_ne (ne.symm ha'), hb.lt_of_ne (ne.symm hb'), hab, hzβ©,
end
variables {π}
lemma open_segment_subset_iff_segment_subset {x y : E} {s : set E} (hx : x β s) (hy : y β s) :
open_segment π x y β s β [x -[π] y] β s :=
begin
refine β¨Ξ» h z hz, _, (open_segment_subset_segment π x y).transβ©,
obtain rfl | hxz := eq_or_ne x z,
{ exact hx },
obtain rfl | hyz := eq_or_ne y z,
{ exact hy },
exact h (mem_open_segment_of_ne_left_right π hxz hyz hz),
end
lemma convex.combo_self {a b : π} (h : a + b = 1) (x : E) : a β’ x + b β’ x = x :=
by rw [βadd_smul, h, one_smul]
end module
end ordered_semiring
open_locale convex
section ordered_ring
variables [ordered_ring π]
section add_comm_group
variables (π) [add_comm_group E] [add_comm_group F] [module π E] [module π F]
section densely_ordered
variables [nontrivial π] [densely_ordered π]
@[simp] lemma open_segment_same (x : E) :
open_segment π x x = {x} :=
set.ext $ Ξ» z, β¨Ξ» β¨a, b, ha, hb, hab, hzβ©,
by simpa only [β add_smul, mem_singleton_iff, hab, one_smul, eq_comm] using hz,
Ξ» (h : z = x), begin
obtain β¨a, haβ, haββ© := densely_ordered.dense (0 : π) 1 zero_lt_one,
refine β¨a, 1 - a, haβ, sub_pos_of_lt haβ, add_sub_cancel'_right _ _, _β©,
rw [βadd_smul, add_sub_cancel'_right, one_smul, h],
endβ©
end densely_ordered
lemma segment_eq_image (x y : E) : [x -[π] y] = (Ξ» ΞΈ : π, (1 - ΞΈ) β’ x + ΞΈ β’ y) '' Icc (0 : π) 1 :=
set.ext $ Ξ» z,
β¨Ξ» β¨a, b, ha, hb, hab, hzβ©,
β¨b, β¨hb, hab βΈ le_add_of_nonneg_left haβ©, hab βΈ hz βΈ by simp only [add_sub_cancel]β©,
Ξ» β¨ΞΈ, β¨hΞΈβ, hΞΈββ©, hzβ©, β¨1-ΞΈ, ΞΈ, sub_nonneg.2 hΞΈβ, hΞΈβ, sub_add_cancel _ _, hzβ©β©
lemma open_segment_eq_image (x y : E) :
open_segment π x y = (Ξ» (ΞΈ : π), (1 - ΞΈ) β’ x + ΞΈ β’ y) '' Ioo (0 : π) 1 :=
set.ext $ Ξ» z,
β¨Ξ» β¨a, b, ha, hb, hab, hzβ©,
β¨b, β¨hb, hab βΈ lt_add_of_pos_left _ haβ©, hab βΈ hz βΈ by simp only [add_sub_cancel]β©,
Ξ» β¨ΞΈ, β¨hΞΈβ, hΞΈββ©, hzβ©, β¨1 - ΞΈ, ΞΈ, sub_pos.2 hΞΈβ, hΞΈβ, sub_add_cancel _ _, hzβ©β©
lemma segment_eq_imageβ (x y : E) :
[x -[π] y] = (Ξ» p : π Γ π, p.1 β’ x + p.2 β’ y) '' {p | 0 β€ p.1 β§ 0 β€ p.2 β§ p.1 + p.2 = 1} :=
by simp only [segment, image, prod.exists, mem_set_of_eq, exists_prop, and_assoc]
lemma open_segment_eq_imageβ (x y : E) :
open_segment π x y =
(Ξ» p : π Γ π, p.1 β’ x + p.2 β’ y) '' {p | 0 < p.1 β§ 0 < p.2 β§ p.1 + p.2 = 1} :=
by simp only [open_segment, image, prod.exists, mem_set_of_eq, exists_prop, and_assoc]
lemma segment_eq_image' (x y : E) :
[x -[π] y] = (Ξ» (ΞΈ : π), x + ΞΈ β’ (y - x)) '' Icc (0 : π) 1 :=
by { convert segment_eq_image π x y, ext ΞΈ, simp only [smul_sub, sub_smul, one_smul], abel }
lemma open_segment_eq_image' (x y : E) :
open_segment π x y = (Ξ» (ΞΈ : π), x + ΞΈ β’ (y - x)) '' Ioo (0 : π) 1 :=
by { convert open_segment_eq_image π x y, ext ΞΈ, simp only [smul_sub, sub_smul, one_smul], abel }
lemma segment_image (f : E ββ[π] F) (a b : E) : f '' [a -[π] b] = [f a -[π] f b] :=
set.ext (Ξ» x, by simp_rw [segment_eq_image, mem_image, exists_exists_and_eq_and, map_add, map_smul])
@[simp] lemma open_segment_image (f : E ββ[π] F) (a b : E) :
f '' open_segment π a b = open_segment π (f a) (f b) :=
set.ext (Ξ» x, by simp_rw [open_segment_eq_image, mem_image, exists_exists_and_eq_and, map_add,
map_smul])
lemma mem_segment_translate (a : E) {x b c} : a + x β [a + b -[π] a + c] β x β [b -[π] c] :=
begin
rw [segment_eq_image', segment_eq_image'],
refine exists_congr (Ξ» ΞΈ, and_congr iff.rfl _),
simp only [add_sub_add_left_eq_sub, add_assoc, add_right_inj],
end
@[simp] lemma mem_open_segment_translate (a : E) {x b c : E} :
a + x β open_segment π (a + b) (a + c) β x β open_segment π b c :=
begin
rw [open_segment_eq_image', open_segment_eq_image'],
refine exists_congr (Ξ» ΞΈ, and_congr iff.rfl _),
simp only [add_sub_add_left_eq_sub, add_assoc, add_right_inj],
end
lemma segment_translate_preimage (a b c : E) : (Ξ» x, a + x) β»ΒΉ' [a + b -[π] a + c] = [b -[π] c] :=
set.ext $ Ξ» x, mem_segment_translate π a
lemma open_segment_translate_preimage (a b c : E) :
(Ξ» x, a + x) β»ΒΉ' open_segment π (a + b) (a + c) = open_segment π b c :=
set.ext $ Ξ» x, mem_open_segment_translate π a
lemma segment_translate_image (a b c : E) : (Ξ» x, a + x) '' [b -[π] c] = [a + b -[π] a + c] :=
segment_translate_preimage π a b c βΈ image_preimage_eq _ $ add_left_surjective a
lemma open_segment_translate_image (a b c : E) :
(Ξ» x, a + x) '' open_segment π b c = open_segment π (a + b) (a + c) :=
open_segment_translate_preimage π a b c βΈ image_preimage_eq _ $ add_left_surjective a
end add_comm_group
end ordered_ring
section linear_ordered_field
variables [linear_ordered_field π]
section add_comm_group
variables [add_comm_group E] [add_comm_group F] [module π E] [module π F]
@[simp] lemma left_mem_open_segment_iff [no_zero_smul_divisors π E] {x y : E} :
x β open_segment π x y β x = y :=
begin
split,
{ rintro β¨a, b, ha, hb, hab, hxβ©,
refine smul_right_injective _ hb.ne' ((add_right_inj (a β’ x)).1 _),
rw [hx, βadd_smul, hab, one_smul] },
{ rintro rfl,
rw open_segment_same,
exact mem_singleton _ }
end
@[simp] lemma right_mem_open_segment_iff {x y : E} :
y β open_segment π x y β x = y :=
by rw [open_segment_symm, left_mem_open_segment_iff, eq_comm]
end add_comm_group
end linear_ordered_field
/-!
#### Segments in an ordered space
Relates `segment`, `open_segment` and `set.Icc`, `set.Ico`, `set.Ioc`, `set.Ioo`
-/
section ordered_semiring
variables [ordered_semiring π]
section ordered_add_comm_monoid
variables [ordered_add_comm_monoid E] [module π E] [ordered_smul π E]
lemma segment_subset_Icc {x y : E} (h : x β€ y) : [x -[π] y] β Icc x y :=
begin
rintro z β¨a, b, ha, hb, hab, rflβ©,
split,
calc
x = a β’ x + b β’ x :(convex.combo_self hab _).symm
... β€ a β’ x + b β’ y : add_le_add_left (smul_le_smul_of_nonneg h hb) _,
calc
a β’ x + b β’ y
β€ a β’ y + b β’ y : add_le_add_right (smul_le_smul_of_nonneg h ha) _
... = y : convex.combo_self hab _,
end
end ordered_add_comm_monoid
section ordered_cancel_add_comm_monoid
variables [ordered_cancel_add_comm_monoid E] [module π E] [ordered_smul π E]
lemma open_segment_subset_Ioo {x y : E} (h : x < y) : open_segment π x y β Ioo x y :=
begin
rintro z β¨a, b, ha, hb, hab, rflβ©,
split,
calc
x = a β’ x + b β’ x : (convex.combo_self hab _).symm
... < a β’ x + b β’ y : add_lt_add_left (smul_lt_smul_of_pos h hb) _,
calc
a β’ x + b β’ y
< a β’ y + b β’ y : add_lt_add_right (smul_lt_smul_of_pos h ha) _
... = y : convex.combo_self hab _,
end
end ordered_cancel_add_comm_monoid
section linear_ordered_add_comm_monoid
variables [linear_ordered_add_comm_monoid E] [module π E] [ordered_smul π E] {π}
lemma segment_subset_interval (x y : E) : [x -[π] y] β interval x y :=
begin
cases le_total x y,
{ rw interval_of_le h,
exact segment_subset_Icc h },
{ rw [interval_of_ge h, segment_symm],
exact segment_subset_Icc h }
end
lemma convex.min_le_combo (x y : E) {a b : π} (ha : 0 β€ a) (hb : 0 β€ b) (hab : a + b = 1) :
min x y β€ a β’ x + b β’ y :=
(segment_subset_interval x y β¨_, _, ha, hb, hab, rflβ©).1
lemma convex.combo_le_max (x y : E) {a b : π} (ha : 0 β€ a) (hb : 0 β€ b) (hab : a + b = 1) :
a β’ x + b β’ y β€ max x y :=
(segment_subset_interval x y β¨_, _, ha, hb, hab, rflβ©).2
end linear_ordered_add_comm_monoid
end ordered_semiring
section linear_ordered_field
variables [linear_ordered_field π]
lemma Icc_subset_segment {x y : π} : Icc x y β [x -[π] y] :=
begin
rintro z β¨hxz, hyzβ©,
obtain rfl | h := (hxz.trans hyz).eq_or_lt,
{ rw segment_same,
exact hyz.antisymm hxz },
rw βsub_nonneg at hxz hyz,
rw βsub_pos at h,
refine β¨(y - z) / (y - x), (z - x) / (y - x), div_nonneg hyz h.le, div_nonneg hxz h.le, _, _β©,
{ rw [βadd_div, sub_add_sub_cancel, div_self h.ne'] },
{ rw [smul_eq_mul, smul_eq_mul, βmul_div_right_comm, βmul_div_right_comm, βadd_div,
div_eq_iff h.ne', add_comm, sub_mul, sub_mul, mul_comm x, sub_add_sub_cancel, mul_sub] }
end
@[simp] lemma segment_eq_Icc {x y : π} (h : x β€ y) : [x -[π] y] = Icc x y :=
(segment_subset_Icc h).antisymm Icc_subset_segment
lemma Ioo_subset_open_segment {x y : π} : Ioo x y β open_segment π x y :=
Ξ» z hz, mem_open_segment_of_ne_left_right _ hz.1.ne hz.2.ne'
(Icc_subset_segment $ Ioo_subset_Icc_self hz)
@[simp] lemma open_segment_eq_Ioo {x y : π} (h : x < y) : open_segment π x y = Ioo x y :=
(open_segment_subset_Ioo h).antisymm Ioo_subset_open_segment
lemma segment_eq_Icc' (x y : π) : [x -[π] y] = Icc (min x y) (max x y) :=
begin
cases le_total x y,
{ rw [segment_eq_Icc h, max_eq_right h, min_eq_left h] },
{ rw [segment_symm, segment_eq_Icc h, max_eq_left h, min_eq_right h] }
end
lemma open_segment_eq_Ioo' {x y : π} (hxy : x β y) :
open_segment π x y = Ioo (min x y) (max x y) :=
begin
cases hxy.lt_or_lt,
{ rw [open_segment_eq_Ioo h, max_eq_right h.le, min_eq_left h.le] },
{ rw [open_segment_symm, open_segment_eq_Ioo h, max_eq_left h.le, min_eq_right h.le] }
end
lemma segment_eq_interval (x y : π) : [x -[π] y] = interval x y :=
segment_eq_Icc' _ _
/-- A point is in an `Icc` iff it can be expressed as a convex combination of the endpoints. -/
lemma convex.mem_Icc {x y : π} (h : x β€ y) {z : π} :
z β Icc x y β β (a b : π), 0 β€ a β§ 0 β€ b β§ a + b = 1 β§ a * x + b * y = z :=
begin
rw βsegment_eq_Icc h,
simp_rw [βexists_prop],
refl,
end
/-- A point is in an `Ioo` iff it can be expressed as a strict convex combination of the endpoints.
-/
lemma convex.mem_Ioo {x y : π} (h : x < y) {z : π} :
z β Ioo x y β β (a b : π), 0 < a β§ 0 < b β§ a + b = 1 β§ a * x + b * y = z :=
begin
rw βopen_segment_eq_Ioo h,
simp_rw [βexists_prop],
refl,
end
/-- A point is in an `Ioc` iff it can be expressed as a semistrict convex combination of the
endpoints. -/
lemma convex.mem_Ioc {x y : π} (h : x < y) {z : π} :
z β Ioc x y β β (a b : π), 0 β€ a β§ 0 < b β§ a + b = 1 β§ a * x + b * y = z :=
begin
split,
{ rintro hz,
obtain β¨a, b, ha, hb, hab, rflβ© := (convex.mem_Icc h.le).1 (Ioc_subset_Icc_self hz),
obtain rfl | hb' := hb.eq_or_lt,
{ rw add_zero at hab,
rw [hab, one_mul, zero_mul, add_zero] at hz,
exact (hz.1.ne rfl).elim },
{ exact β¨a, b, ha, hb', hab, rflβ© } },
{ rintro β¨a, b, ha, hb, hab, rflβ©,
obtain rfl | ha' := ha.eq_or_lt,
{ rw zero_add at hab,
rwa [hab, one_mul, zero_mul, zero_add, right_mem_Ioc] },
{ exact Ioo_subset_Ioc_self ((convex.mem_Ioo h).2 β¨a, b, ha', hb, hab, rflβ©) } }
end
/-- A point is in an `Ico` iff it can be expressed as a semistrict convex combination of the
endpoints. -/
lemma convex.mem_Ico {x y : π} (h : x < y) {z : π} :
z β Ico x y β β (a b : π), 0 < a β§ 0 β€ b β§ a + b = 1 β§ a * x + b * y = z :=
begin
split,
{ rintro hz,
obtain β¨a, b, ha, hb, hab, rflβ© := (convex.mem_Icc h.le).1 (Ico_subset_Icc_self hz),
obtain rfl | ha' := ha.eq_or_lt,
{ rw zero_add at hab,
rw [hab, one_mul, zero_mul, zero_add] at hz,
exact (hz.2.ne rfl).elim },
{ exact β¨a, b, ha', hb, hab, rflβ© } },
{ rintro β¨a, b, ha, hb, hab, rflβ©,
obtain rfl | hb' := hb.eq_or_lt,
{ rw add_zero at hab,
rwa [hab, one_mul, zero_mul, add_zero, left_mem_Ico] },
{ exact Ioo_subset_Ico_self ((convex.mem_Ioo h).2 β¨a, b, ha, hb', hab, rflβ©) } }
end
end linear_ordered_field
/-! ### Convexity of sets -/
section ordered_semiring
variables [ordered_semiring π]
section add_comm_monoid
variables [add_comm_monoid E] [add_comm_monoid F]
section has_scalar
variables (π) [has_scalar π E] [has_scalar π F] (s : set E)
/-- Convexity of sets. -/
def convex : Prop :=
β β¦x y : Eβ¦, x β s β y β s β β β¦a b : πβ¦, 0 β€ a β 0 β€ b β a + b = 1 β
a β’ x + b β’ y β s
variables {π s}
lemma convex_iff_segment_subset :
convex π s β β β¦x yβ¦, x β s β y β s β [x -[π] y] β s :=
begin
split,
{ rintro h x y hx hy z β¨a, b, ha, hb, hab, rflβ©,
exact h hx hy ha hb hab },
{ rintro h x y hx hy a b ha hb hab,
exact h hx hy β¨a, b, ha, hb, hab, rflβ© }
end
lemma convex.segment_subset (h : convex π s) {x y : E} (hx : x β s) (hy : y β s) :
[x -[π] y] β s :=
convex_iff_segment_subset.1 h hx hy
lemma convex.open_segment_subset (h : convex π s) {x y : E} (hx : x β s) (hy : y β s) :
open_segment π x y β s :=
(open_segment_subset_segment π x y).trans (h.segment_subset hx hy)
/-- Alternative definition of set convexity, in terms of pointwise set operations. -/
lemma convex_iff_pointwise_add_subset :
convex π s β β β¦a b : πβ¦, 0 β€ a β 0 β€ b β a + b = 1 β a β’ s + b β’ s β s :=
iff.intro
begin
rintro hA a b ha hb hab w β¨au, bv, β¨u, hu, rflβ©, β¨v, hv, rflβ©, rflβ©,
exact hA hu hv ha hb hab
end
(Ξ» h x y hx hy a b ha hb hab,
(h ha hb hab) (set.add_mem_add β¨_, hx, rflβ© β¨_, hy, rflβ©))
lemma convex_empty : convex π (β
: set E) := by finish
lemma convex_univ : convex π (set.univ : set E) := Ξ» _ _ _ _ _ _ _ _ _, trivial
lemma convex.inter {t : set E} (hs : convex π s) (ht : convex π t) : convex π (s β© t) :=
Ξ» x y (hx : x β s β© t) (hy : y β s β© t) a b (ha : 0 β€ a) (hb : 0 β€ b) (hab : a + b = 1),
β¨hs hx.left hy.left ha hb hab, ht hx.right hy.right ha hb habβ©
lemma convex_sInter {S : set (set E)} (h : β s β S, convex π s) : convex π (ββ S) :=
assume x y hx hy a b ha hb hab s hs,
h s hs (hx s hs) (hy s hs) ha hb hab
lemma convex_Inter {ΞΉ : Sort*} {s : ΞΉ β set E} (h : β i : ΞΉ, convex π (s i)) :
convex π (β i, s i) :=
(sInter_range s) βΈ convex_sInter $ forall_range_iff.2 h
lemma convex.prod {s : set E} {t : set F} (hs : convex π s) (ht : convex π t) :
convex π (s.prod t) :=
begin
intros x y hx hy a b ha hb hab,
apply mem_prod.2,
exact β¨hs (mem_prod.1 hx).1 (mem_prod.1 hy).1 ha hb hab,
ht (mem_prod.1 hx).2 (mem_prod.1 hy).2 ha hb habβ©
end
lemma directed.convex_Union {ΞΉ : Sort*} {s : ΞΉ β set E} (hdir : directed (β) s)
(hc : β β¦i : ΞΉβ¦, convex π (s i)) :
convex π (β i, s i) :=
begin
rintro x y hx hy a b ha hb hab,
rw mem_Union at β’ hx hy,
obtain β¨i, hxβ© := hx,
obtain β¨j, hyβ© := hy,
obtain β¨k, hik, hjkβ© := hdir i j,
exact β¨k, hc (hik hx) (hjk hy) ha hb habβ©,
end
lemma directed_on.convex_sUnion {c : set (set E)} (hdir : directed_on (β) c)
(hc : β β¦A : set Eβ¦, A β c β convex π A) :
convex π (ββc) :=
begin
rw sUnion_eq_Union,
exact (directed_on_iff_directed.1 hdir).convex_Union (Ξ» A, hc A.2),
end
end has_scalar
section module
variables [module π E] [module π F] {s : set E}
lemma convex_iff_forall_pos :
convex π s β β β¦x yβ¦, x β s β y β s β β β¦a b : πβ¦, 0 < a β 0 < b β a + b = 1
β a β’ x + b β’ y β s :=
begin
refine β¨Ξ» h x y hx hy a b ha hb hab, h hx hy ha.le hb.le hab, _β©,
intros h x y hx hy a b ha hb hab,
cases ha.eq_or_lt with ha ha,
{ subst a, rw [zero_add] at hab, simp [hab, hy] },
cases hb.eq_or_lt with hb hb,
{ subst b, rw [add_zero] at hab, simp [hab, hx] },
exact h hx hy ha hb hab
end
lemma convex_iff_pairwise_on_pos :
convex π s β s.pairwise_on (Ξ» x y, β β¦a b : πβ¦, 0 < a β 0 < b β a + b = 1 β a β’ x + b β’ y β s) :=
begin
refine β¨Ξ» h x hx y hy _ a b ha hb hab, h hx hy ha.le hb.le hab, _β©,
intros h x y hx hy a b ha hb hab,
obtain rfl | ha' := ha.eq_or_lt,
{ rw [zero_add] at hab, rwa [hab, zero_smul, one_smul, zero_add] },
obtain rfl | hb' := hb.eq_or_lt,
{ rw [add_zero] at hab, rwa [hab, zero_smul, one_smul, add_zero] },
obtain rfl | hxy := eq_or_ne x y,
{ rwa convex.combo_self hab },
exact h _ hx _ hy hxy ha' hb' hab,
end
lemma convex_iff_open_segment_subset :
convex π s β β β¦x yβ¦, x β s β y β s β open_segment π x y β s :=
begin
rw convex_iff_segment_subset,
exact forallβ_congr (Ξ» x y, forallβ_congr $ Ξ» hx hy,
(open_segment_subset_iff_segment_subset hx hy).symm),
end
lemma convex_singleton (c : E) : convex π ({c} : set E) :=
begin
intros x y hx hy a b ha hb hab,
rw [set.eq_of_mem_singleton hx, set.eq_of_mem_singleton hy, βadd_smul, hab, one_smul],
exact mem_singleton c
end
lemma convex.linear_image (hs : convex π s) (f : E ββ[π] F) : convex π (s.image f) :=
begin
intros x y hx hy a b ha hb hab,
obtain β¨x', hx', rflβ© := mem_image_iff_bex.1 hx,
obtain β¨y', hy', rflβ© := mem_image_iff_bex.1 hy,
exact β¨a β’ x' + b β’ y', hs hx' hy' ha hb hab, by rw [f.map_add, f.map_smul, f.map_smul]β©,
end
lemma convex.is_linear_image (hs : convex π s) {f : E β F} (hf : is_linear_map π f) :
convex π (f '' s) :=
hs.linear_image $ hf.mk' f
lemma convex.linear_preimage {s : set F} (hs : convex π s) (f : E ββ[π] F) :
convex π (s.preimage f) :=
begin
intros x y hx hy a b ha hb hab,
rw [mem_preimage, f.map_add, f.map_smul, f.map_smul],
exact hs hx hy ha hb hab,
end
lemma convex.is_linear_preimage {s : set F} (hs : convex π s) {f : E β F} (hf : is_linear_map π f) :
convex π (preimage f s) :=
hs.linear_preimage $ hf.mk' f
lemma convex.add {t : set E} (hs : convex π s) (ht : convex π t) : convex π (s + t) :=
by { rw β add_image_prod, exact (hs.prod ht).is_linear_image is_linear_map.is_linear_map_add }
lemma convex.translate (hs : convex π s) (z : E) : convex π ((Ξ» x, z + x) '' s) :=
begin
intros x y hx hy a b ha hb hab,
obtain β¨x', hx', rflβ© := mem_image_iff_bex.1 hx,
obtain β¨y', hy', rflβ© := mem_image_iff_bex.1 hy,
refine β¨a β’ x' + b β’ y', hs hx' hy' ha hb hab, _β©,
rw [smul_add, smul_add, add_add_add_comm, βadd_smul, hab, one_smul],
end
/-- The translation of a convex set is also convex. -/
lemma convex.translate_preimage_right (hs : convex π s) (z : E) : convex π ((Ξ» x, z + x) β»ΒΉ' s) :=
begin
intros x y hx hy a b ha hb hab,
have h := hs hx hy ha hb hab,
rwa [smul_add, smul_add, add_add_add_comm, βadd_smul, hab, one_smul] at h,
end
/-- The translation of a convex set is also convex. -/
lemma convex.translate_preimage_left (hs : convex π s) (z : E) : convex π ((Ξ» x, x + z) β»ΒΉ' s) :=
by simpa only [add_comm] using hs.translate_preimage_right z
section ordered_add_comm_monoid
variables [ordered_add_comm_monoid Ξ²] [module π Ξ²] [ordered_smul π Ξ²]
lemma convex_Iic (r : Ξ²) : convex π (Iic r) :=
Ξ» x y hx hy a b ha hb hab,
calc
a β’ x + b β’ y
β€ a β’ r + b β’ r
: add_le_add (smul_le_smul_of_nonneg hx ha) (smul_le_smul_of_nonneg hy hb)
... = r : convex.combo_self hab _
lemma convex_Ici (r : Ξ²) : convex π (Ici r) :=
@convex_Iic π (order_dual Ξ²) _ _ _ _ r
lemma convex_Icc (r s : Ξ²) : convex π (Icc r s) :=
Ici_inter_Iic.subst ((convex_Ici r).inter $ convex_Iic s)
lemma convex_halfspace_le {f : E β Ξ²} (h : is_linear_map π f) (r : Ξ²) :
convex π {w | f w β€ r} :=
(convex_Iic r).is_linear_preimage h
lemma convex_halfspace_ge {f : E β Ξ²} (h : is_linear_map π f) (r : Ξ²) :
convex π {w | r β€ f w} :=
(convex_Ici r).is_linear_preimage h
lemma convex_hyperplane {f : E β Ξ²} (h : is_linear_map π f) (r : Ξ²) :
convex π {w | f w = r} :=
begin
simp_rw le_antisymm_iff,
exact (convex_halfspace_le h r).inter (convex_halfspace_ge h r),
end
end ordered_add_comm_monoid
section ordered_cancel_add_comm_monoid
variables [ordered_cancel_add_comm_monoid Ξ²] [module π Ξ²] [ordered_smul π Ξ²]
lemma convex_Iio (r : Ξ²) : convex π (Iio r) :=
begin
intros x y hx hy a b ha hb hab,
obtain rfl | ha' := ha.eq_or_lt,
{ rw zero_add at hab,
rwa [zero_smul, zero_add, hab, one_smul] },
rw mem_Iio at hx hy,
calc
a β’ x + b β’ y
< a β’ r + b β’ r
: add_lt_add_of_lt_of_le (smul_lt_smul_of_pos hx ha') (smul_le_smul_of_nonneg hy.le hb)
... = r : convex.combo_self hab _
end
lemma convex_Ioi (r : Ξ²) : convex π (Ioi r) :=
@convex_Iio π (order_dual Ξ²) _ _ _ _ r
lemma convex_Ioo (r s : Ξ²) : convex π (Ioo r s) :=
Ioi_inter_Iio.subst ((convex_Ioi r).inter $ convex_Iio s)
lemma convex_Ico (r s : Ξ²) : convex π (Ico r s) :=
Ici_inter_Iio.subst ((convex_Ici r).inter $ convex_Iio s)
lemma convex_Ioc (r s : Ξ²) : convex π (Ioc r s) :=
Ioi_inter_Iic.subst ((convex_Ioi r).inter $ convex_Iic s)
lemma convex_halfspace_lt {f : E β Ξ²} (h : is_linear_map π f) (r : Ξ²) :
convex π {w | f w < r} :=
(convex_Iio r).is_linear_preimage h
lemma convex_halfspace_gt {f : E β Ξ²} (h : is_linear_map π f) (r : Ξ²) :
convex π {w | r < f w} :=
(convex_Ioi r).is_linear_preimage h
end ordered_cancel_add_comm_monoid
section linear_ordered_add_comm_monoid
variables [linear_ordered_add_comm_monoid Ξ²] [module π Ξ²] [ordered_smul π Ξ²]
lemma convex_interval (r s : Ξ²) : convex π (interval r s) :=
convex_Icc _ _
end linear_ordered_add_comm_monoid
end module
end add_comm_monoid
section linear_ordered_add_comm_monoid
variables [linear_ordered_add_comm_monoid E] [ordered_add_comm_monoid Ξ²] [module π E]
[ordered_smul π E] {s : set E} {f : E β Ξ²}
lemma monotone_on.convex_le (hf : monotone_on f s) (hs : convex π s) (r : Ξ²) :
convex π {x β s | f x β€ r} :=
Ξ» x y hx hy a b ha hb hab, β¨hs hx.1 hy.1 ha hb hab,
(hf (hs hx.1 hy.1 ha hb hab) (max_rec' s hx.1 hy.1) (convex.combo_le_max x y ha hb hab)).trans
(max_rec' _ hx.2 hy.2)β©
lemma monotone_on.convex_lt (hf : monotone_on f s) (hs : convex π s) (r : Ξ²) :
convex π {x β s | f x < r} :=
Ξ» x y hx hy a b ha hb hab, β¨hs hx.1 hy.1 ha hb hab,
(hf (hs hx.1 hy.1 ha hb hab) (max_rec' s hx.1 hy.1) (convex.combo_le_max x y ha hb hab)).trans_lt
(max_rec' _ hx.2 hy.2)β©
lemma monotone_on.convex_ge (hf : monotone_on f s) (hs : convex π s) (r : Ξ²) :
convex π {x β s | r β€ f x} :=
@monotone_on.convex_le π (order_dual E) (order_dual Ξ²) _ _ _ _ _ _ _ hf.dual hs r
lemma monotone_on.convex_gt (hf : monotone_on f s) (hs : convex π s) (r : Ξ²) :
convex π {x β s | r < f x} :=
@monotone_on.convex_lt π (order_dual E) (order_dual Ξ²) _ _ _ _ _ _ _ hf.dual hs r
lemma antitone_on.convex_le (hf : antitone_on f s) (hs : convex π s) (r : Ξ²) :
convex π {x β s | f x β€ r} :=
@monotone_on.convex_ge π E (order_dual Ξ²) _ _ _ _ _ _ _ hf hs r
lemma antitone_on.convex_lt (hf : antitone_on f s) (hs : convex π s) (r : Ξ²) :
convex π {x β s | f x < r} :=
@monotone_on.convex_gt π E (order_dual Ξ²) _ _ _ _ _ _ _ hf hs r
lemma antitone_on.convex_ge (hf : antitone_on f s) (hs : convex π s) (r : Ξ²) :
convex π {x β s | r β€ f x} :=
@monotone_on.convex_le π E (order_dual Ξ²) _ _ _ _ _ _ _ hf hs r
lemma antitone_on.convex_gt (hf : antitone_on f s) (hs : convex π s) (r : Ξ²) :
convex π {x β s | r < f x} :=
@monotone_on.convex_lt π E (order_dual Ξ²) _ _ _ _ _ _ _ hf hs r
lemma monotone.convex_le (hf : monotone f) (r : Ξ²) :
convex π {x | f x β€ r} :=
set.sep_univ.subst ((hf.monotone_on univ).convex_le convex_univ r)
lemma monotone.convex_lt (hf : monotone f) (r : Ξ²) :
convex π {x | f x β€ r} :=
set.sep_univ.subst ((hf.monotone_on univ).convex_le convex_univ r)
lemma monotone.convex_ge (hf : monotone f ) (r : Ξ²) :
convex π {x | r β€ f x} :=
set.sep_univ.subst ((hf.monotone_on univ).convex_ge convex_univ r)
lemma monotone.convex_gt (hf : monotone f) (r : Ξ²) :
convex π {x | f x β€ r} :=
set.sep_univ.subst ((hf.monotone_on univ).convex_le convex_univ r)
lemma antitone.convex_le (hf : antitone f) (r : Ξ²) :
convex π {x | f x β€ r} :=
set.sep_univ.subst ((hf.antitone_on univ).convex_le convex_univ r)
lemma antitone.convex_lt (hf : antitone f) (r : Ξ²) :
convex π {x | f x < r} :=
set.sep_univ.subst ((hf.antitone_on univ).convex_lt convex_univ r)
lemma antitone.convex_ge (hf : antitone f) (r : Ξ²) :
convex π {x | r β€ f x} :=
set.sep_univ.subst ((hf.antitone_on univ).convex_ge convex_univ r)
lemma antitone.convex_gt (hf : antitone f) (r : Ξ²) :
convex π {x | r < f x} :=
set.sep_univ.subst ((hf.antitone_on univ).convex_gt convex_univ r)
end linear_ordered_add_comm_monoid
section add_comm_group
variables [add_comm_group E] [module π E] {s t : set E}
lemma convex.combo_eq_vadd {a b : π} {x y : E} (h : a + b = 1) :
a β’ x + b β’ y = b β’ (y - x) + x :=
calc
a β’ x + b β’ y = (b β’ y - b β’ x) + (a β’ x + b β’ x) : by abel
... = b β’ (y - x) + x : by rw [smul_sub, convex.combo_self h]
lemma convex.sub (hs : convex π s) (ht : convex π t) :
convex π ((Ξ» x : E Γ E, x.1 - x.2) '' (s.prod t)) :=
(hs.prod ht).is_linear_image is_linear_map.is_linear_map_sub
lemma convex_segment (x y : E) : convex π [x -[π] y] :=
begin
rintro p q β¨ap, bp, hap, hbp, habp, rflβ© β¨aq, bq, haq, hbq, habq, rflβ© a b ha hb hab,
refine β¨a * ap + b * aq, a * bp + b * bq,
add_nonneg (mul_nonneg ha hap) (mul_nonneg hb haq),
add_nonneg (mul_nonneg ha hbp) (mul_nonneg hb hbq), _, _β©,
{ rw [add_add_add_comm, βmul_add, βmul_add, habp, habq, mul_one, mul_one, hab] },
{ simp_rw [add_smul, mul_smul, smul_add],
exact add_add_add_comm _ _ _ _ }
end
lemma convex_open_segment (a b : E) : convex π (open_segment π a b) :=
begin
rw convex_iff_open_segment_subset,
rintro p q β¨ap, bp, hap, hbp, habp, rflβ© β¨aq, bq, haq, hbq, habq, rflβ© z β¨a, b, ha, hb, hab, rflβ©,
refine β¨a * ap + b * aq, a * bp + b * bq,
add_pos (mul_pos ha hap) (mul_pos hb haq),
add_pos (mul_pos ha hbp) (mul_pos hb hbq), _, _β©,
{ rw [add_add_add_comm, βmul_add, βmul_add, habp, habq, mul_one, mul_one, hab] },
{ simp_rw [add_smul, mul_smul, smul_add],
exact add_add_add_comm _ _ _ _ }
end
end add_comm_group
end ordered_semiring
section ordered_comm_semiring
variables [ordered_comm_semiring π]
section add_comm_monoid
variables [add_comm_monoid E] [add_comm_monoid F] [module π E] [module π F] {s : set E}
lemma convex.smul (hs : convex π s) (c : π) : convex π (c β’ s) :=
hs.linear_image (linear_map.lsmul _ _ c)
lemma convex.smul_preimage (hs : convex π s) (c : π) : convex π ((Ξ» z, c β’ z) β»ΒΉ' s) :=
hs.linear_preimage (linear_map.lsmul _ _ c)
lemma convex.affinity (hs : convex π s) (z : E) (c : π) : convex π ((Ξ» x, z + c β’ x) '' s) :=
begin
have h := (hs.smul c).translate z,
rwa [βimage_smul, image_image] at h,
end
end add_comm_monoid
end ordered_comm_semiring
section ordered_ring
variables [ordered_ring π]
section add_comm_group
variables [add_comm_group E] [add_comm_group F] [module π E] [module π F] {s : set E}
lemma convex.add_smul_mem (hs : convex π s) {x y : E} (hx : x β s) (hy : x + y β s)
{t : π} (ht : t β Icc (0 : π) 1) : x + t β’ y β s :=
begin
have h : x + t β’ y = (1 - t) β’ x + t β’ (x + y),
{ rw [smul_add, βadd_assoc, βadd_smul, sub_add_cancel, one_smul] },
rw h,
exact hs hx hy (sub_nonneg_of_le ht.2) ht.1 (sub_add_cancel _ _),
end
lemma convex.smul_mem_of_zero_mem (hs : convex π s) {x : E} (zero_mem : (0 : E) β s) (hx : x β s)
{t : π} (ht : t β Icc (0 : π) 1) : t β’ x β s :=
by simpa using hs.add_smul_mem zero_mem (by simpa using hx) ht
lemma convex.add_smul_sub_mem (h : convex π s) {x y : E} (hx : x β s) (hy : y β s)
{t : π} (ht : t β Icc (0 : π) 1) : x + t β’ (y - x) β s :=
begin
apply h.segment_subset hx hy,
rw segment_eq_image',
exact mem_image_of_mem _ ht,
end
/-- Affine subspaces are convex. -/
lemma affine_subspace.convex (Q : affine_subspace π E) : convex π (Q : set E) :=
begin
intros x y hx hy a b ha hb hab,
rw [eq_sub_of_add_eq hab, β affine_map.line_map_apply_module],
exact affine_map.line_map_mem b hx hy,
end
/--
Applying an affine map to an affine combination of two points yields
an affine combination of the images.
-/
lemma convex.combo_affine_apply {a b : π} {x y : E} {f : E βα΅[π] F} (h : a + b = 1) :
f (a β’ x + b β’ y) = a β’ f x + b β’ f y :=
begin
simp only [convex.combo_eq_vadd h, β vsub_eq_sub],
exact f.apply_line_map _ _ _,
end
/-- The preimage of a convex set under an affine map is convex. -/
lemma convex.affine_preimage (f : E βα΅[π] F) {s : set F} (hs : convex π s) :
convex π (f β»ΒΉ' s) :=
begin
intros x y xs ys a b ha hb hab,
rw [mem_preimage, convex.combo_affine_apply hab],
exact hs xs ys ha hb hab,
end
/-- The image of a convex set under an affine map is convex. -/
lemma convex.affine_image (f : E βα΅[π] F) {s : set E} (hs : convex π s) :
convex π (f '' s) :=
begin
rintro x y β¨x', β¨hx', hx'fβ©β© β¨y', β¨hy', hy'fβ©β© a b ha hb hab,
refine β¨a β’ x' + b β’ y', β¨hs hx' hy' ha hb hab, _β©β©,
rw [convex.combo_affine_apply hab, hx'f, hy'f]
end
lemma convex.neg (hs : convex π s) : convex π ((Ξ» z, -z) '' s) :=
hs.is_linear_image is_linear_map.is_linear_map_neg
lemma convex.neg_preimage (hs : convex π s) : convex π ((Ξ» z, -z) β»ΒΉ' s) :=
hs.is_linear_preimage is_linear_map.is_linear_map_neg
end add_comm_group
end ordered_ring
section linear_ordered_field
variables [linear_ordered_field π]
section add_comm_group
variables [add_comm_group E] [add_comm_group F] [module π E] [module π F] {s : set E}
/-- Alternative definition of set convexity, using division. -/
lemma convex_iff_div :
convex π s β β β¦x y : Eβ¦, x β s β y β s β β β¦a b : πβ¦,
0 β€ a β 0 β€ b β 0 < a + b β (a/(a+b)) β’ x + (b/(a+b)) β’ y β s :=
β¨Ξ» h x y hx hy a b ha hb hab, begin
apply h hx hy,
{ have ha', from mul_le_mul_of_nonneg_left ha (inv_pos.2 hab).le,
rwa [mul_zero, βdiv_eq_inv_mul] at ha' },
{ have hb', from mul_le_mul_of_nonneg_left hb (inv_pos.2 hab).le,
rwa [mul_zero, βdiv_eq_inv_mul] at hb' },
{ rw βadd_div,
exact div_self hab.ne' }
end, Ξ» h x y hx hy a b ha hb hab,
begin
have h', from h hx hy ha hb,
rw [hab, div_one, div_one] at h',
exact h' zero_lt_one
endβ©
lemma convex.mem_smul_of_zero_mem (h : convex π s) {x : E} (zero_mem : (0 : E) β s)
(hx : x β s) {t : π} (ht : 1 β€ t) :
x β t β’ s :=
begin
rw mem_smul_set_iff_inv_smul_memβ (zero_lt_one.trans_le ht).ne',
exact h.smul_mem_of_zero_mem zero_mem hx β¨inv_nonneg.2 (zero_le_one.trans ht), inv_le_one htβ©,
end
lemma convex.add_smul (h_conv : convex π s) {p q : π} (hp : 0 β€ p) (hq : 0 β€ q) :
(p + q) β’ s = p β’ s + q β’ s :=
begin
obtain rfl | hs := s.eq_empty_or_nonempty,
{ simp_rw [smul_set_empty, add_empty] },
obtain rfl | hp' := hp.eq_or_lt,
{ rw [zero_add, zero_smul_set hs, zero_add] },
obtain rfl | hq' := hq.eq_or_lt,
{ rw [add_zero, zero_smul_set hs, add_zero] },
ext,
split,
{ rintro β¨v, hv, rflβ©,
exact β¨p β’ v, q β’ v, smul_mem_smul_set hv, smul_mem_smul_set hv, (add_smul _ _ _).symmβ© },
{ rintro β¨vβ, vβ, β¨vββ, hββ, rflβ©, β¨vββ, hββ, rflβ©, rflβ©,
have hpq := add_pos hp' hq',
exact mem_smul_set.2 β¨_, h_conv hββ hββ (div_pos hp' hpq).le (div_pos hq' hpq).le
(by rw [βdiv_self hpq.ne', add_div] : p / (p + q) + q / (p + q) = 1),
by simp only [β mul_smul, smul_add, mul_div_cancel' _ hpq.ne']β© }
end
end add_comm_group
end linear_ordered_field
/-!
#### Convex sets in an ordered space
Relates `convex` and `ord_connected`.
-/
section
lemma set.ord_connected.convex_of_chain [ordered_semiring π] [ordered_add_comm_monoid E]
[module π E] [ordered_smul π E] {s : set E} (hs : s.ord_connected) (h : zorn.chain (β€) s) :
convex π s :=
begin
intros x y hx hy a b ha hb hab,
obtain hxy | hyx := h.total_of_refl hx hy,
{ refine hs.out hx hy (mem_Icc.2 β¨_, _β©),
calc
x = a β’ x + b β’ x : (convex.combo_self hab _).symm
... β€ a β’ x + b β’ y : add_le_add_left (smul_le_smul_of_nonneg hxy hb) _,
calc
a β’ x + b β’ y
β€ a β’ y + b β’ y : add_le_add_right (smul_le_smul_of_nonneg hxy ha) _
... = y : convex.combo_self hab _ },
{ refine hs.out hy hx (mem_Icc.2 β¨_, _β©),
calc
y = a β’ y + b β’ y : (convex.combo_self hab _).symm
... β€ a β’ x + b β’ y : add_le_add_right (smul_le_smul_of_nonneg hyx ha) _,
calc
a β’ x + b β’ y
β€ a β’ x + b β’ x : add_le_add_left (smul_le_smul_of_nonneg hyx hb) _
... = x : convex.combo_self hab _ }
end
lemma set.ord_connected.convex [ordered_semiring π] [linear_ordered_add_comm_monoid E] [module π E]
[ordered_smul π E] {s : set E} (hs : s.ord_connected) :
convex π s :=
hs.convex_of_chain (zorn.chain_of_trichotomous s)
lemma convex_iff_ord_connected [linear_ordered_field π] {s : set π} :
convex π s β s.ord_connected :=
begin
simp_rw [convex_iff_segment_subset, segment_eq_interval, ord_connected_iff_interval_subset],
exact forall_congr (Ξ» x, forall_swap)
end
alias convex_iff_ord_connected β convex.ord_connected _
end
/-! #### Convexity of submodules/subspaces -/
section submodule
open submodule
lemma submodule.convex [ordered_semiring π] [add_comm_monoid E] [module π E] (K : submodule π E) :
convex π (βK : set E) :=
by { repeat {intro}, refine add_mem _ (smul_mem _ _ _) (smul_mem _ _ _); assumption }
lemma subspace.convex [linear_ordered_field π] [add_comm_group E] [module π E] (K : subspace π E) :
convex π (βK : set E) :=
K.convex
end submodule
/-! ### Simplex -/
section simplex
variables (π) (ΞΉ : Type*) [ordered_semiring π] [fintype ΞΉ]
/-- The standard simplex in the space of functions `ΞΉ β π` is the set of vectors with non-negative
coordinates with total sum `1`. This is the free object in the category of convex spaces. -/
def std_simplex : set (ΞΉ β π) :=
{f | (β x, 0 β€ f x) β§ β x, f x = 1}
lemma std_simplex_eq_inter :
std_simplex π ΞΉ = (β x, {f | 0 β€ f x}) β© {f | β x, f x = 1} :=
by { ext f, simp only [std_simplex, set.mem_inter_eq, set.mem_Inter, set.mem_set_of_eq] }
lemma convex_std_simplex : convex π (std_simplex π ΞΉ) :=
begin
refine Ξ» f g hf hg a b ha hb hab, β¨Ξ» x, _, _β©,
{ apply_rules [add_nonneg, mul_nonneg, hf.1, hg.1] },
{ erw [finset.sum_add_distrib, β finset.smul_sum, β finset.smul_sum, hf.2, hg.2,
smul_eq_mul, smul_eq_mul, mul_one, mul_one],
exact hab }
end
variable {ΞΉ}
lemma ite_eq_mem_std_simplex (i : ΞΉ) : (Ξ» j, ite (i = j) (1:π) 0) β std_simplex π ΞΉ :=
β¨Ξ» j, by simp only; split_ifs; norm_num, by rw [finset.sum_ite_eq, if_pos (finset.mem_univ _)]β©
end simplex
|
d1bc322e6e0ae6ee483816d820377481fad03511 | 77c5b91fae1b966ddd1db969ba37b6f0e4901e88 | /src/analysis/asymptotics/asymptotics.lean | 152dba0a3ee156dc89f94b994118a3c6c2cdd3e6 | [
"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 | 61,421 | lean | /-
Copyright (c) 2019 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad, Yury Kudryashov
-/
import analysis.normed_space.basic
import topology.local_homeomorph
/-!
# Asymptotics
We introduce these relations:
* `is_O_with c f g l` : "f is big O of g along l with constant c";
* `is_O f g l` : "f is big O of g along l";
* `is_o f g l` : "f is little o of g along l".
Here `l` is any filter on the domain of `f` and `g`, which are assumed to be the same. The codomains
of `f` and `g` do not need to be the same; all that is needed that there is a norm associated with
these types, and it is the norm that is compared asymptotically.
The relation `is_O_with c` is introduced to factor out common algebraic arguments in the proofs of
similar properties of `is_O` and `is_o`. Usually proofs outside of this file should use `is_O`
instead.
Often the ranges of `f` and `g` will be the real numbers, in which case the norm is the absolute
value. In general, we have
`is_O f g l β is_O (Ξ» x, β₯f xβ₯) (Ξ» x, β₯g xβ₯) l`,
and similarly for `is_o`. But our setup allows us to use the notions e.g. with functions
to the integers, rationals, complex numbers, or any normed vector space without mentioning the
norm explicitly.
If `f` and `g` are functions to a normed field like the reals or complex numbers and `g` is always
nonzero, we have
`is_o f g l β tendsto (Ξ» x, f x / (g x)) l (π 0)`.
In fact, the right-to-left direction holds without the hypothesis on `g`, and in the other direction
it suffices to assume that `f` is zero wherever `g` is. (This generalization is useful in defining
the FrΓ©chet derivative.)
-/
open filter set
open_locale topological_space big_operators classical filter nnreal
namespace asymptotics
variables {Ξ± : Type*} {Ξ² : Type*} {E : Type*} {F : Type*} {G : Type*}
{E' : Type*} {F' : Type*} {G' : Type*} {R : Type*} {R' : Type*} {π : Type*} {π' : Type*}
variables [has_norm E] [has_norm F] [has_norm G] [normed_group E'] [normed_group F']
[normed_group G'] [normed_ring R] [normed_ring R'] [normed_field π] [normed_field π']
{c c' : β} {f : Ξ± β E} {g : Ξ± β F} {k : Ξ± β G} {f' : Ξ± β E'} {g' : Ξ± β F'} {k' : Ξ± β G'}
{l l' : filter Ξ±}
section defs
/-! ### Definitions -/
/-- This version of the Landau notation `is_O_with C f g l` where `f` and `g` are two functions on
a type `Ξ±` and `l` is a filter on `Ξ±`, means that eventually for `l`, `β₯fβ₯` is bounded by `C * β₯gβ₯`.
In other words, `β₯fβ₯ / β₯gβ₯` is eventually bounded by `C`, modulo division by zero issues that are
avoided by this definition. Probably you want to use `is_O` instead of this relation. -/
@[irreducible]
def is_O_with (c : β) (f : Ξ± β E) (g : Ξ± β F) (l : filter Ξ±) : Prop :=
βαΆ x in l, β₯ f x β₯ β€ c * β₯ g x β₯
/-- Definition of `is_O_with`. We record it in a lemma as we will set `is_O_with` to be irreducible
at the end of this file. -/
lemma is_O_with_iff {c : β} {f : Ξ± β E} {g : Ξ± β F} {l : filter Ξ±} :
is_O_with c f g l β βαΆ x in l, β₯ f x β₯ β€ c * β₯ g x β₯ := by rw is_O_with
alias is_O_with_iff β asymptotics.is_O_with.bound asymptotics.is_O_with.of_bound
/-- The Landau notation `is_O f g l` where `f` and `g` are two functions on a type `Ξ±` and `l` is
a filter on `Ξ±`, means that eventually for `l`, `β₯fβ₯` is bounded by a constant multiple of `β₯gβ₯`.
In other words, `β₯fβ₯ / β₯gβ₯` is eventually bounded, modulo division by zero issues that are avoided
by this definition. -/
@[irreducible]
def is_O (f : Ξ± β E) (g : Ξ± β F) (l : filter Ξ±) : Prop := β c : β, is_O_with c f g l
/-- Definition of `is_O` in terms of `is_O_with`. We record it in a lemma as we will set
`is_O` to be irreducible at the end of this file. -/
lemma is_O_iff_is_O_with {f : Ξ± β E} {g : Ξ± β F} {l : filter Ξ±} :
is_O f g l β β c : β, is_O_with c f g l := by rw is_O
/-- Definition of `is_O` in terms of filters. We record it in a lemma as we will set
`is_O` to be irreducible at the end of this file. -/
lemma is_O_iff {f : Ξ± β E} {g : Ξ± β F} {l : filter Ξ±} :
is_O f g l β β c : β, βαΆ x in l, β₯ f x β₯ β€ c * β₯ g x β₯ := by simp [is_O, is_O_with]
lemma is_O.of_bound (c : β) {f : Ξ± β E} {g : Ξ± β F} {l : filter Ξ±}
(h : βαΆ x in l, β₯ f x β₯ β€ c * β₯ g x β₯) : is_O f g l := is_O_iff.2 β¨c, hβ©
lemma is_O.bound {f : Ξ± β E} {g : Ξ± β F} {l : filter Ξ±} :
is_O f g l β β c : β, βαΆ x in l, β₯ f x β₯ β€ c * β₯ g x β₯ := is_O_iff.1
/-- The Landau notation `is_o f g l` where `f` and `g` are two functions on a type `Ξ±` and `l` is
a filter on `Ξ±`, means that eventually for `l`, `β₯fβ₯` is bounded by an arbitrarily small constant
multiple of `β₯gβ₯`. In other words, `β₯fβ₯ / β₯gβ₯` tends to `0` along `l`, modulo division by zero
issues that are avoided by this definition. -/
@[irreducible]
def is_o (f : Ξ± β E) (g : Ξ± β F) (l : filter Ξ±) : Prop := β β¦c : ββ¦, 0 < c β is_O_with c f g l
/-- Definition of `is_o` in terms of `is_O_with`. We record it in a lemma as we will set
`is_o` to be irreducible at the end of this file. -/
lemma is_o_iff_forall_is_O_with {f : Ξ± β E} {g : Ξ± β F} {l : filter Ξ±} :
is_o f g l β β β¦c : ββ¦, 0 < c β is_O_with c f g l := by rw is_o
alias is_o_iff_forall_is_O_with β asymptotics.is_o.forall_is_O_with asymptotics.is_o.of_is_O_with
/-- Definition of `is_o` in terms of filters. We record it in a lemma as we will set
`is_o` to be irreducible at the end of this file. -/
lemma is_o_iff {f : Ξ± β E} {g : Ξ± β F} {l : filter Ξ±} :
is_o f g l β β β¦c : ββ¦, 0 < c β βαΆ x in l, β₯ f x β₯ β€ c * β₯ g x β₯ :=
by simp only [is_o, is_O_with]
alias is_o_iff β asymptotics.is_o.bound asymptotics.is_o.of_bound
lemma is_o.def {f : Ξ± β E} {g : Ξ± β F} {l : filter Ξ±} (h : is_o f g l) {c : β} (hc : 0 < c) :
βαΆ x in l, β₯ f x β₯ β€ c * β₯ g x β₯ :=
is_o_iff.1 h hc
lemma is_o.def' {f : Ξ± β E} {g : Ξ± β F} {l : filter Ξ±} (h : is_o f g l) {c : β} (hc : 0 < c) :
is_O_with c f g l :=
is_O_with_iff.2 $ is_o_iff.1 h hc
end defs
/-! ### Conversions -/
theorem is_O_with.is_O (h : is_O_with c f g l) : is_O f g l := by rw is_O; exact β¨c, hβ©
theorem is_o.is_O_with (hgf : is_o f g l) : is_O_with 1 f g l := hgf.def' zero_lt_one
theorem is_o.is_O (hgf : is_o f g l) : is_O f g l := hgf.is_O_with.is_O
lemma is_O.is_O_with {f : Ξ± β E} {g : Ξ± β F} {l : filter Ξ±} :
is_O f g l β β c : β, is_O_with c f g l := is_O_iff_is_O_with.1
theorem is_O_with.weaken (h : is_O_with c f g' l) (hc : c β€ c') : is_O_with c' f g' l :=
is_O_with.of_bound $ mem_of_superset h.bound $ Ξ» x hx,
calc β₯f xβ₯ β€ c * β₯g' xβ₯ : hx
... β€ _ : mul_le_mul_of_nonneg_right hc (norm_nonneg _)
theorem is_O_with.exists_pos (h : is_O_with c f g' l) :
β c' (H : 0 < c'), is_O_with c' f g' l :=
β¨max c 1, lt_of_lt_of_le zero_lt_one (le_max_right c 1), h.weaken $ le_max_left c 1β©
theorem is_O.exists_pos (h : is_O f g' l) : β c (H : 0 < c), is_O_with c f g' l :=
let β¨c, hcβ© := h.is_O_with in hc.exists_pos
theorem is_O_with.exists_nonneg (h : is_O_with c f g' l) :
β c' (H : 0 β€ c'), is_O_with c' f g' l :=
let β¨c, cpos, hcβ© := h.exists_pos in β¨c, le_of_lt cpos, hcβ©
theorem is_O.exists_nonneg (h : is_O f g' l) :
β c (H : 0 β€ c), is_O_with c f g' l :=
let β¨c, hcβ© := h.is_O_with in hc.exists_nonneg
/-- `f = O(g)` if and only if `is_O_with c f g` for all sufficiently large `c`. -/
lemma is_O_iff_eventually_is_O_with : is_O f g' l β βαΆ c in at_top, is_O_with c f g' l :=
is_O_iff_is_O_with.trans
β¨Ξ» β¨c, hcβ©, mem_at_top_sets.2 β¨c, Ξ» c' hc', hc.weaken hc'β©, Ξ» h, h.existsβ©
/-- `f = O(g)` if and only if `βαΆ x in l, β₯f xβ₯ β€ c * β₯g xβ₯` for all sufficiently large `c`. -/
lemma is_O_iff_eventually : is_O f g' l β βαΆ c in at_top, βαΆ x in l, β₯f xβ₯ β€ c * β₯g' xβ₯ :=
is_O_iff_eventually_is_O_with.trans $ by simp only [is_O_with]
/-! ### Subsingleton -/
@[nontriviality] lemma is_o_of_subsingleton [subsingleton E'] : is_o f' g' l :=
is_o.of_bound $ Ξ» c hc, by simp [subsingleton.elim (f' _) 0, mul_nonneg hc.le]
@[nontriviality] lemma is_O_of_subsingleton [subsingleton E'] : is_O f' g' l :=
is_o_of_subsingleton.is_O
/-! ### Congruence -/
theorem is_O_with_congr {cβ cβ} {fβ fβ : Ξ± β E} {gβ gβ : Ξ± β F} {l : filter Ξ±}
(hc : cβ = cβ) (hf : fβ =αΆ [l] fβ) (hg : gβ =αΆ [l] gβ) :
is_O_with cβ fβ gβ l β is_O_with cβ fβ gβ l :=
begin
unfold is_O_with,
subst cβ,
apply filter.eventually_congr,
filter_upwards [hf, hg],
assume x eβ eβ,
rw [eβ, eβ]
end
theorem is_O_with.congr' {cβ cβ} {fβ fβ : Ξ± β E} {gβ gβ : Ξ± β F} {l : filter Ξ±}
(hc : cβ = cβ) (hf : fβ =αΆ [l] fβ) (hg : gβ =αΆ [l] gβ) :
is_O_with cβ fβ gβ l β is_O_with cβ fβ gβ l :=
(is_O_with_congr hc hf hg).mp
theorem is_O_with.congr {cβ cβ} {fβ fβ : Ξ± β E} {gβ gβ : Ξ± β F} {l : filter Ξ±}
(hc : cβ = cβ) (hf : β x, fβ x = fβ x) (hg : β x, gβ x = gβ x) :
is_O_with cβ fβ gβ l β is_O_with cβ fβ gβ l :=
Ξ» h, h.congr' hc (univ_mem' hf) (univ_mem' hg)
theorem is_O_with.congr_left {fβ fβ : Ξ± β E} {l : filter Ξ±} (hf : β x, fβ x = fβ x) :
is_O_with c fβ g l β is_O_with c fβ g l :=
is_O_with.congr rfl hf (Ξ» _, rfl)
theorem is_O_with.congr_right {gβ gβ : Ξ± β F} {l : filter Ξ±} (hg : β x, gβ x = gβ x) :
is_O_with c f gβ l β is_O_with c f gβ l :=
is_O_with.congr rfl (Ξ» _, rfl) hg
theorem is_O_with.congr_const {cβ cβ} {l : filter Ξ±} (hc : cβ = cβ) :
is_O_with cβ f g l β is_O_with cβ f g l :=
is_O_with.congr hc (Ξ» _, rfl) (Ξ» _, rfl)
theorem is_O_congr {fβ fβ : Ξ± β E} {gβ gβ : Ξ± β F} {l : filter Ξ±}
(hf : fβ =αΆ [l] fβ) (hg : gβ =αΆ [l] gβ) :
is_O fβ gβ l β is_O fβ gβ l :=
by { unfold is_O, exact exists_congr (Ξ» c, is_O_with_congr rfl hf hg) }
theorem is_O.congr' {fβ fβ : Ξ± β E} {gβ gβ : Ξ± β F} {l : filter Ξ±}
(hf : fβ =αΆ [l] fβ) (hg : gβ =αΆ [l] gβ) :
is_O fβ gβ l β is_O fβ gβ l :=
(is_O_congr hf hg).mp
theorem is_O.congr {fβ fβ : Ξ± β E} {gβ gβ : Ξ± β F} {l : filter Ξ±}
(hf : β x, fβ x = fβ x) (hg : β x, gβ x = gβ x) :
is_O fβ gβ l β is_O fβ gβ l :=
Ξ» h, h.congr' (univ_mem' hf) (univ_mem' hg)
theorem is_O.congr_left {fβ fβ : Ξ± β E} {l : filter Ξ±} (hf : β x, fβ x = fβ x) :
is_O fβ g l β is_O fβ g l :=
is_O.congr hf (Ξ» _, rfl)
theorem is_O.congr_right {gβ gβ : Ξ± β E} {l : filter Ξ±} (hg : β x, gβ x = gβ x) :
is_O f gβ l β is_O f gβ l :=
is_O.congr (Ξ» _, rfl) hg
theorem is_o_congr {fβ fβ : Ξ± β E} {gβ gβ : Ξ± β F} {l : filter Ξ±}
(hf : fβ =αΆ [l] fβ) (hg : gβ =αΆ [l] gβ) :
is_o fβ gβ l β is_o fβ gβ l :=
by { unfold is_o, exact ball_congr (Ξ» c hc, is_O_with_congr (eq.refl c) hf hg) }
theorem is_o.congr' {fβ fβ : Ξ± β E} {gβ gβ : Ξ± β F} {l : filter Ξ±}
(hf : fβ =αΆ [l] fβ) (hg : gβ =αΆ [l] gβ) :
is_o fβ gβ l β is_o fβ gβ l :=
(is_o_congr hf hg).mp
theorem is_o.congr {fβ fβ : Ξ± β E} {gβ gβ : Ξ± β F} {l : filter Ξ±}
(hf : β x, fβ x = fβ x) (hg : β x, gβ x = gβ x) :
is_o fβ gβ l β is_o fβ gβ l :=
Ξ» h, h.congr' (univ_mem' hf) (univ_mem' hg)
theorem is_o.congr_left {fβ fβ : Ξ± β E} {l : filter Ξ±} (hf : β x, fβ x = fβ x) :
is_o fβ g l β is_o fβ g l :=
is_o.congr hf (Ξ» _, rfl)
theorem is_o.congr_right {gβ gβ : Ξ± β E} {l : filter Ξ±} (hg : β x, gβ x = gβ x) :
is_o f gβ l β is_o f gβ l :=
is_o.congr (Ξ» _, rfl) hg
/-! ### Filter operations and transitivity -/
theorem is_O_with.comp_tendsto (hcfg : is_O_with c f g l)
{k : Ξ² β Ξ±} {l' : filter Ξ²} (hk : tendsto k l' l):
is_O_with c (f β k) (g β k) l' :=
is_O_with.of_bound $ hk hcfg.bound
theorem is_O.comp_tendsto (hfg : is_O f g l) {k : Ξ² β Ξ±} {l' : filter Ξ²} (hk : tendsto k l' l) :
is_O (f β k) (g β k) l' :=
is_O_iff_is_O_with.2 $ hfg.is_O_with.imp (Ξ» c h, h.comp_tendsto hk)
theorem is_o.comp_tendsto (hfg : is_o f g l) {k : Ξ² β Ξ±} {l' : filter Ξ²} (hk : tendsto k l' l) :
is_o (f β k) (g β k) l' :=
is_o.of_is_O_with $ Ξ» c cpos, (hfg.forall_is_O_with cpos).comp_tendsto hk
@[simp] theorem is_O_with_map {k : Ξ² β Ξ±} {l : filter Ξ²} :
is_O_with c f g (map k l) β is_O_with c (f β k) (g β k) l :=
by { unfold is_O_with, exact mem_map }
@[simp] theorem is_O_map {k : Ξ² β Ξ±} {l : filter Ξ²} :
is_O f g (map k l) β is_O (f β k) (g β k) l :=
by simp only [is_O, is_O_with_map]
@[simp] theorem is_o_map {k : Ξ² β Ξ±} {l : filter Ξ²} :
is_o f g (map k l) β is_o (f β k) (g β k) l :=
by simp only [is_o, is_O_with_map]
theorem is_O_with.mono (h : is_O_with c f g l') (hl : l β€ l') : is_O_with c f g l :=
is_O_with.of_bound $ hl h.bound
theorem is_O.mono (h : is_O f g l') (hl : l β€ l') : is_O f g l :=
is_O_iff_is_O_with.2 $ h.is_O_with.imp (Ξ» c h, h.mono hl)
theorem is_o.mono (h : is_o f g l') (hl : l β€ l') : is_o f g l :=
is_o.of_is_O_with $ Ξ» c cpos, (h.forall_is_O_with cpos).mono hl
theorem is_O_with.trans (hfg : is_O_with c f g l) (hgk : is_O_with c' g k l) (hc : 0 β€ c) :
is_O_with (c * c') f k l :=
begin
unfold is_O_with at *,
filter_upwards [hfg, hgk],
assume x hx hx',
calc β₯f xβ₯ β€ c * β₯g xβ₯ : hx
... β€ c * (c' * β₯k xβ₯) : mul_le_mul_of_nonneg_left hx' hc
... = c * c' * β₯k xβ₯ : (mul_assoc _ _ _).symm
end
theorem is_O.trans (hfg : is_O f g' l) (hgk : is_O g' k l) : is_O f k l :=
let β¨c, cnonneg, hcβ© := hfg.exists_nonneg, β¨c', hc'β© := hgk.is_O_with in
(hc.trans hc' cnonneg).is_O
theorem is_o.trans_is_O_with (hfg : is_o f g l) (hgk : is_O_with c g k l) (hc : 0 < c) :
is_o f k l :=
begin
unfold is_o at *,
intros c' c'pos,
have : 0 < c' / c, from div_pos c'pos hc,
exact ((hfg this).trans hgk (le_of_lt this)).congr_const (div_mul_cancel _ (ne_of_gt hc))
end
theorem is_o.trans_is_O (hfg : is_o f g l) (hgk : is_O g k' l) : is_o f k' l :=
let β¨c, cpos, hcβ© := hgk.exists_pos in hfg.trans_is_O_with hc cpos
theorem is_O_with.trans_is_o (hfg : is_O_with c f g l) (hgk : is_o g k l) (hc : 0 < c) :
is_o f k l :=
begin
unfold is_o at *,
intros c' c'pos,
have : 0 < c' / c, from div_pos c'pos hc,
exact (hfg.trans (hgk this) (le_of_lt hc)).congr_const (mul_div_cancel' _ (ne_of_gt hc))
end
theorem is_O.trans_is_o (hfg : is_O f g' l) (hgk : is_o g' k l) : is_o f k l :=
let β¨c, cpos, hcβ© := hfg.exists_pos in hc.trans_is_o hgk cpos
theorem is_o.trans (hfg : is_o f g l) (hgk : is_o g k' l) : is_o f k' l :=
hfg.trans_is_O hgk.is_O
theorem is_o.trans' (hfg : is_o f g' l) (hgk : is_o g' k l) : is_o f k l :=
hfg.is_O.trans_is_o hgk
section
variable (l)
theorem is_O_with_of_le' (hfg : β x, β₯f xβ₯ β€ c * β₯g xβ₯) : is_O_with c f g l :=
is_O_with.of_bound $ univ_mem' hfg
theorem is_O_with_of_le (hfg : β x, β₯f xβ₯ β€ β₯g xβ₯) : is_O_with 1 f g l :=
is_O_with_of_le' l $ Ξ» x, by { rw one_mul, exact hfg x }
theorem is_O_of_le' (hfg : β x, β₯f xβ₯ β€ c * β₯g xβ₯) : is_O f g l :=
(is_O_with_of_le' l hfg).is_O
theorem is_O_of_le (hfg : β x, β₯f xβ₯ β€ β₯g xβ₯) : is_O f g l :=
(is_O_with_of_le l hfg).is_O
end
theorem is_O_with_refl (f : Ξ± β E) (l : filter Ξ±) : is_O_with 1 f f l :=
is_O_with_of_le l $ Ξ» _, le_refl _
theorem is_O_refl (f : Ξ± β E) (l : filter Ξ±) : is_O f f l := (is_O_with_refl f l).is_O
theorem is_O_with.trans_le (hfg : is_O_with c f g l) (hgk : β x, β₯g xβ₯ β€ β₯k xβ₯) (hc : 0 β€ c) :
is_O_with c f k l :=
(hfg.trans (is_O_with_of_le l hgk) hc).congr_const $ mul_one c
theorem is_O.trans_le (hfg : is_O f g' l) (hgk : β x, β₯g' xβ₯ β€ β₯k xβ₯) :
is_O f k l :=
hfg.trans (is_O_of_le l hgk)
theorem is_o.trans_le (hfg : is_o f g l) (hgk : β x, β₯g xβ₯ β€ β₯k xβ₯) :
is_o f k l :=
hfg.trans_is_O_with (is_O_with_of_le _ hgk) zero_lt_one
section bot
variables (c f g)
@[simp] theorem is_O_with_bot : is_O_with c f g β₯ := is_O_with.of_bound $ trivial
@[simp] theorem is_O_bot : is_O f g β₯ := (is_O_with_bot 1 f g).is_O
@[simp] theorem is_o_bot : is_o f g β₯ := is_o.of_is_O_with $ Ξ» c _, is_O_with_bot c f g
end bot
theorem is_O_with.join (h : is_O_with c f g l) (h' : is_O_with c f g l') :
is_O_with c f g (l β l') :=
is_O_with.of_bound $ mem_sup.2 β¨h.bound, h'.boundβ©
theorem is_O_with.join' (h : is_O_with c f g' l) (h' : is_O_with c' f g' l') :
is_O_with (max c c') f g' (l β l') :=
is_O_with.of_bound $
mem_sup.2 β¨(h.weaken $ le_max_left c c').bound, (h'.weaken $ le_max_right c c').boundβ©
theorem is_O.join (h : is_O f g' l) (h' : is_O f g' l') : is_O f g' (l β l') :=
let β¨c, hcβ© := h.is_O_with, β¨c', hc'β© := h'.is_O_with in (hc.join' hc').is_O
theorem is_o.join (h : is_o f g l) (h' : is_o f g l') :
is_o f g (l β l') :=
is_o.of_is_O_with $
Ξ» c cpos, (h.forall_is_O_with cpos).join (h'.forall_is_O_with cpos)
/-! ### Simplification : norm -/
@[simp] theorem is_O_with_norm_right : is_O_with c f (Ξ» x, β₯g' xβ₯) l β is_O_with c f g' l :=
by simp only [is_O_with, norm_norm]
alias is_O_with_norm_right β asymptotics.is_O_with.of_norm_right asymptotics.is_O_with.norm_right
@[simp] theorem is_O_norm_right : is_O f (Ξ» x, β₯g' xβ₯) l β is_O f g' l :=
by { unfold is_O, exact exists_congr (Ξ» _, is_O_with_norm_right) }
alias is_O_norm_right β asymptotics.is_O.of_norm_right asymptotics.is_O.norm_right
@[simp] theorem is_o_norm_right : is_o f (Ξ» x, β₯g' xβ₯) l β is_o f g' l :=
by { unfold is_o, exact forall_congr (Ξ» _, forall_congr $ Ξ» _, is_O_with_norm_right) }
alias is_o_norm_right β asymptotics.is_o.of_norm_right asymptotics.is_o.norm_right
@[simp] theorem is_O_with_norm_left : is_O_with c (Ξ» x, β₯f' xβ₯) g l β is_O_with c f' g l :=
by simp only [is_O_with, norm_norm]
alias is_O_with_norm_left β asymptotics.is_O_with.of_norm_left asymptotics.is_O_with.norm_left
@[simp] theorem is_O_norm_left : is_O (Ξ» x, β₯f' xβ₯) g l β is_O f' g l :=
by { unfold is_O, exact exists_congr (Ξ» _, is_O_with_norm_left) }
alias is_O_norm_left β asymptotics.is_O.of_norm_left asymptotics.is_O.norm_left
@[simp] theorem is_o_norm_left : is_o (Ξ» x, β₯f' xβ₯) g l β is_o f' g l :=
by { unfold is_o, exact forall_congr (Ξ» _, forall_congr $ Ξ» _, is_O_with_norm_left) }
alias is_o_norm_left β asymptotics.is_o.of_norm_left asymptotics.is_o.norm_left
theorem is_O_with_norm_norm :
is_O_with c (Ξ» x, β₯f' xβ₯) (Ξ» x, β₯g' xβ₯) l β is_O_with c f' g' l :=
is_O_with_norm_left.trans is_O_with_norm_right
alias is_O_with_norm_norm β asymptotics.is_O_with.of_norm_norm asymptotics.is_O_with.norm_norm
theorem is_O_norm_norm :
is_O (Ξ» x, β₯f' xβ₯) (Ξ» x, β₯g' xβ₯) l β is_O f' g' l :=
is_O_norm_left.trans is_O_norm_right
alias is_O_norm_norm β asymptotics.is_O.of_norm_norm asymptotics.is_O.norm_norm
theorem is_o_norm_norm :
is_o (Ξ» x, β₯f' xβ₯) (Ξ» x, β₯g' xβ₯) l β is_o f' g' l :=
is_o_norm_left.trans is_o_norm_right
alias is_o_norm_norm β asymptotics.is_o.of_norm_norm asymptotics.is_o.norm_norm
/-! ### Simplification: negate -/
@[simp] theorem is_O_with_neg_right : is_O_with c f (Ξ» x, -(g' x)) l β is_O_with c f g' l :=
by simp only [is_O_with, norm_neg]
alias is_O_with_neg_right β asymptotics.is_O_with.of_neg_right asymptotics.is_O_with.neg_right
@[simp] theorem is_O_neg_right : is_O f (Ξ» x, -(g' x)) l β is_O f g' l :=
by { unfold is_O, exact exists_congr (Ξ» _, is_O_with_neg_right) }
alias is_O_neg_right β asymptotics.is_O.of_neg_right asymptotics.is_O.neg_right
@[simp] theorem is_o_neg_right : is_o f (Ξ» x, -(g' x)) l β is_o f g' l :=
by { unfold is_o, exact forall_congr (Ξ» _, (forall_congr (Ξ» _, is_O_with_neg_right))) }
alias is_o_neg_right β asymptotics.is_o.of_neg_right asymptotics.is_o.neg_right
@[simp] theorem is_O_with_neg_left : is_O_with c (Ξ» x, -(f' x)) g l β is_O_with c f' g l :=
by simp only [is_O_with, norm_neg]
alias is_O_with_neg_left β asymptotics.is_O_with.of_neg_left asymptotics.is_O_with.neg_left
@[simp] theorem is_O_neg_left : is_O (Ξ» x, -(f' x)) g l β is_O f' g l :=
by { unfold is_O, exact exists_congr (Ξ» _, is_O_with_neg_left) }
alias is_O_neg_left β asymptotics.is_O.of_neg_left asymptotics.is_O.neg_left
@[simp] theorem is_o_neg_left : is_o (Ξ» x, -(f' x)) g l β is_o f' g l :=
by { unfold is_o, exact forall_congr (Ξ» _, (forall_congr (Ξ» _, is_O_with_neg_left))) }
alias is_o_neg_left β asymptotics.is_o.of_neg_right asymptotics.is_o.neg_left
/-! ### Product of functions (right) -/
lemma is_O_with_fst_prod : is_O_with 1 f' (Ξ» x, (f' x, g' x)) l :=
is_O_with_of_le l $ Ξ» x, le_max_left _ _
lemma is_O_with_snd_prod : is_O_with 1 g' (Ξ» x, (f' x, g' x)) l :=
is_O_with_of_le l $ Ξ» x, le_max_right _ _
lemma is_O_fst_prod : is_O f' (Ξ» x, (f' x, g' x)) l := is_O_with_fst_prod.is_O
lemma is_O_snd_prod : is_O g' (Ξ» x, (f' x, g' x)) l := is_O_with_snd_prod.is_O
lemma is_O_fst_prod' {f' : Ξ± β E' Γ F'} : is_O (Ξ» x, (f' x).1) f' l :=
by simpa [is_O, is_O_with] using is_O_fst_prod
lemma is_O_snd_prod' {f' : Ξ± β E' Γ F'} : is_O (Ξ» x, (f' x).2) f' l :=
by simpa [is_O, is_O_with] using is_O_snd_prod
section
variables (f' k')
lemma is_O_with.prod_rightl (h : is_O_with c f g' l) (hc : 0 β€ c) :
is_O_with c f (Ξ» x, (g' x, k' x)) l :=
(h.trans is_O_with_fst_prod hc).congr_const (mul_one c)
lemma is_O.prod_rightl (h : is_O f g' l) : is_O f (Ξ»x, (g' x, k' x)) l :=
let β¨c, cnonneg, hcβ© := h.exists_nonneg in (hc.prod_rightl k' cnonneg).is_O
lemma is_o.prod_rightl (h : is_o f g' l) : is_o f (Ξ» x, (g' x, k' x)) l :=
is_o.of_is_O_with $
Ξ» c cpos, (h.forall_is_O_with cpos).prod_rightl k' (le_of_lt cpos)
lemma is_O_with.prod_rightr (h : is_O_with c f g' l) (hc : 0 β€ c) :
is_O_with c f (Ξ» x, (f' x, g' x)) l :=
(h.trans is_O_with_snd_prod hc).congr_const (mul_one c)
lemma is_O.prod_rightr (h : is_O f g' l) : is_O f (Ξ»x, (f' x, g' x)) l :=
let β¨c, cnonneg, hcβ© := h.exists_nonneg in (hc.prod_rightr f' cnonneg).is_O
lemma is_o.prod_rightr (h : is_o f g' l) : is_o f (Ξ»x, (f' x, g' x)) l :=
is_o.of_is_O_with $
Ξ» c cpos, (h.forall_is_O_with cpos).prod_rightr f' (le_of_lt cpos)
end
lemma is_O_with.prod_left_same (hf : is_O_with c f' k' l) (hg : is_O_with c g' k' l) :
is_O_with c (Ξ» x, (f' x, g' x)) k' l :=
by rw is_O_with_iff at *; filter_upwards [hf, hg] Ξ» x, max_le
lemma is_O_with.prod_left (hf : is_O_with c f' k' l) (hg : is_O_with c' g' k' l) :
is_O_with (max c c') (Ξ» x, (f' x, g' x)) k' l :=
(hf.weaken $ le_max_left c c').prod_left_same (hg.weaken $ le_max_right c c')
lemma is_O_with.prod_left_fst (h : is_O_with c (Ξ» x, (f' x, g' x)) k' l) :
is_O_with c f' k' l :=
(is_O_with_fst_prod.trans h zero_le_one).congr_const $ one_mul c
lemma is_O_with.prod_left_snd (h : is_O_with c (Ξ» x, (f' x, g' x)) k' l) :
is_O_with c g' k' l :=
(is_O_with_snd_prod.trans h zero_le_one).congr_const $ one_mul c
lemma is_O_with_prod_left :
is_O_with c (Ξ» x, (f' x, g' x)) k' l β is_O_with c f' k' l β§ is_O_with c g' k' l :=
β¨Ξ» h, β¨h.prod_left_fst, h.prod_left_sndβ©, Ξ» h, h.1.prod_left_same h.2β©
lemma is_O.prod_left (hf : is_O f' k' l) (hg : is_O g' k' l) : is_O (Ξ» x, (f' x, g' x)) k' l :=
let β¨c, hfβ© := hf.is_O_with, β¨c', hgβ© := hg.is_O_with in (hf.prod_left hg).is_O
lemma is_O.prod_left_fst (h : is_O (Ξ» x, (f' x, g' x)) k' l) : is_O f' k' l :=
is_O_fst_prod.trans h
lemma is_O.prod_left_snd (h : is_O (Ξ» x, (f' x, g' x)) k' l) : is_O g' k' l :=
is_O_snd_prod.trans h
@[simp] lemma is_O_prod_left :
is_O (Ξ» x, (f' x, g' x)) k' l β is_O f' k' l β§ is_O g' k' l :=
β¨Ξ» h, β¨h.prod_left_fst, h.prod_left_sndβ©, Ξ» h, h.1.prod_left h.2β©
lemma is_o.prod_left (hf : is_o f' k' l) (hg : is_o g' k' l) : is_o (Ξ» x, (f' x, g' x)) k' l :=
is_o.of_is_O_with $
Ξ» c hc, (hf.forall_is_O_with hc).prod_left_same (hg.forall_is_O_with hc)
lemma is_o.prod_left_fst (h : is_o (Ξ» x, (f' x, g' x)) k' l) : is_o f' k' l :=
is_O_fst_prod.trans_is_o h
lemma is_o.prod_left_snd (h : is_o (Ξ» x, (f' x, g' x)) k' l) : is_o g' k' l :=
is_O_snd_prod.trans_is_o h
@[simp] lemma is_o_prod_left :
is_o (Ξ» x, (f' x, g' x)) k' l β is_o f' k' l β§ is_o g' k' l :=
β¨Ξ» h, β¨h.prod_left_fst, h.prod_left_sndβ©, Ξ» h, h.1.prod_left h.2β©
lemma is_O_with.eq_zero_imp (h : is_O_with c f' g' l) : βαΆ x in l, g' x = 0 β f' x = 0 :=
eventually.mono h.bound $ Ξ» x hx hg, norm_le_zero_iff.1 $ by simpa [hg] using hx
lemma is_O.eq_zero_imp (h : is_O f' g' l) : βαΆ x in l, g' x = 0 β f' x = 0 :=
let β¨C, hCβ© := h.is_O_with in hC.eq_zero_imp
/-! ### Addition and subtraction -/
section add_sub
variables {cβ cβ : β} {fβ fβ : Ξ± β E'}
theorem is_O_with.add (hβ : is_O_with cβ fβ g l) (hβ : is_O_with cβ fβ g l) :
is_O_with (cβ + cβ) (Ξ» x, fβ x + fβ x) g l :=
by rw is_O_with at *; filter_upwards [hβ, hβ] Ξ» x hxβ hxβ,
calc β₯fβ x + fβ xβ₯ β€ cβ * β₯g xβ₯ + cβ * β₯g xβ₯ : norm_add_le_of_le hxβ hxβ
... = (cβ + cβ) * β₯g xβ₯ : (add_mul _ _ _).symm
theorem is_O.add (hβ : is_O fβ g l) (hβ : is_O fβ g l) : is_O (Ξ» x, fβ x + fβ x) g l :=
let β¨cβ, hcββ© := hβ.is_O_with, β¨cβ, hcββ© := hβ.is_O_with in (hcβ.add hcβ).is_O
theorem is_o.add (hβ : is_o fβ g l) (hβ : is_o fβ g l) : is_o (Ξ» x, fβ x + fβ x) g l :=
is_o.of_is_O_with $ Ξ» c cpos, ((hβ.forall_is_O_with $ half_pos cpos).add
(hβ.forall_is_O_with $ half_pos cpos)).congr_const (add_halves c)
theorem is_o.add_add {gβ gβ : Ξ± β F'} (hβ : is_o fβ gβ l) (hβ : is_o fβ gβ l) :
is_o (Ξ» x, fβ x + fβ x) (Ξ» x, β₯gβ xβ₯ + β₯gβ xβ₯) l :=
by refine (hβ.trans_le $ Ξ» x, _).add (hβ.trans_le _);
simp [real.norm_eq_abs, abs_of_nonneg, add_nonneg]
theorem is_O.add_is_o (hβ : is_O fβ g l) (hβ : is_o fβ g l) : is_O (Ξ» x, fβ x + fβ x) g l :=
hβ.add hβ.is_O
theorem is_o.add_is_O (hβ : is_o fβ g l) (hβ : is_O fβ g l) : is_O (Ξ» x, fβ x + fβ x) g l :=
hβ.is_O.add hβ
theorem is_O_with.add_is_o (hβ : is_O_with cβ fβ g l) (hβ : is_o fβ g l) (hc : cβ < cβ) :
is_O_with cβ (Ξ»x, fβ x + fβ x) g l :=
(hβ.add (hβ.forall_is_O_with (sub_pos.2 hc))).congr_const (add_sub_cancel'_right _ _)
theorem is_o.add_is_O_with (hβ : is_o fβ g l) (hβ : is_O_with cβ fβ g l) (hc : cβ < cβ) :
is_O_with cβ (Ξ»x, fβ x + fβ x) g l :=
(hβ.add_is_o hβ hc).congr_left $ Ξ» _, add_comm _ _
theorem is_O_with.sub (hβ : is_O_with cβ fβ g l) (hβ : is_O_with cβ fβ g l) :
is_O_with (cβ + cβ) (Ξ» x, fβ x - fβ x) g l :=
by simpa only [sub_eq_add_neg] using hβ.add hβ.neg_left
theorem is_O_with.sub_is_o (hβ : is_O_with cβ fβ g l) (hβ : is_o fβ g l) (hc : cβ < cβ) :
is_O_with cβ (Ξ» x, fβ x - fβ x) g l :=
by simpa only [sub_eq_add_neg] using hβ.add_is_o hβ.neg_left hc
theorem is_O.sub (hβ : is_O fβ g l) (hβ : is_O fβ g l) : is_O (Ξ» x, fβ x - fβ x) g l :=
by simpa only [sub_eq_add_neg] using hβ.add hβ.neg_left
theorem is_o.sub (hβ : is_o fβ g l) (hβ : is_o fβ g l) : is_o (Ξ» x, fβ x - fβ x) g l :=
by simpa only [sub_eq_add_neg] using hβ.add hβ.neg_left
end add_sub
/-! ### Lemmas about `is_O (fβ - fβ) g l` / `is_o (fβ - fβ) g l` treated as a binary relation -/
section is_oO_as_rel
variables {fβ fβ fβ : Ξ± β E'}
theorem is_O_with.symm (h : is_O_with c (Ξ» x, fβ x - fβ x) g l) :
is_O_with c (Ξ» x, fβ x - fβ x) g l :=
h.neg_left.congr_left $ Ξ» x, neg_sub _ _
theorem is_O_with_comm :
is_O_with c (Ξ» x, fβ x - fβ x) g l β is_O_with c (Ξ» x, fβ x - fβ x) g l :=
β¨is_O_with.symm, is_O_with.symmβ©
theorem is_O.symm (h : is_O (Ξ» x, fβ x - fβ x) g l) : is_O (Ξ» x, fβ x - fβ x) g l :=
h.neg_left.congr_left $ Ξ» x, neg_sub _ _
theorem is_O_comm : is_O (Ξ» x, fβ x - fβ x) g l β is_O (Ξ» x, fβ x - fβ x) g l :=
β¨is_O.symm, is_O.symmβ©
theorem is_o.symm (h : is_o (Ξ» x, fβ x - fβ x) g l) : is_o (Ξ» x, fβ x - fβ x) g l :=
by simpa only [neg_sub] using h.neg_left
theorem is_o_comm : is_o (Ξ» x, fβ x - fβ x) g l β is_o (Ξ» x, fβ x - fβ x) g l :=
β¨is_o.symm, is_o.symmβ©
theorem is_O_with.triangle (hβ : is_O_with c (Ξ» x, fβ x - fβ x) g l)
(hβ : is_O_with c' (Ξ» x, fβ x - fβ x) g l) :
is_O_with (c + c') (Ξ» x, fβ x - fβ x) g l :=
(hβ.add hβ).congr_left $ Ξ» x, sub_add_sub_cancel _ _ _
theorem is_O.triangle (hβ : is_O (Ξ» x, fβ x - fβ x) g l) (hβ : is_O (Ξ» x, fβ x - fβ x) g l) :
is_O (Ξ» x, fβ x - fβ x) g l :=
(hβ.add hβ).congr_left $ Ξ» x, sub_add_sub_cancel _ _ _
theorem is_o.triangle (hβ : is_o (Ξ» x, fβ x - fβ x) g l) (hβ : is_o (Ξ» x, fβ x - fβ x) g l) :
is_o (Ξ» x, fβ x - fβ x) g l :=
(hβ.add hβ).congr_left $ Ξ» x, sub_add_sub_cancel _ _ _
theorem is_O.congr_of_sub (h : is_O (Ξ» x, fβ x - fβ x) g l) :
is_O fβ g l β is_O fβ g l :=
β¨Ξ» h', (h'.sub h).congr_left (Ξ» x, sub_sub_cancel _ _),
Ξ» h', (h.add h').congr_left (Ξ» x, sub_add_cancel _ _)β©
theorem is_o.congr_of_sub (h : is_o (Ξ» x, fβ x - fβ x) g l) :
is_o fβ g l β is_o fβ g l :=
β¨Ξ» h', (h'.sub h).congr_left (Ξ» x, sub_sub_cancel _ _),
Ξ» h', (h.add h').congr_left (Ξ» x, sub_add_cancel _ _)β©
end is_oO_as_rel
/-! ### Zero, one, and other constants -/
section zero_const
variables (g g' l)
theorem is_o_zero : is_o (Ξ» x, (0 : E')) g' l :=
is_o.of_bound $ Ξ» c hc, univ_mem' $ Ξ» x,
by simpa using mul_nonneg (le_of_lt hc) (norm_nonneg $ g' x)
theorem is_O_with_zero (hc : 0 β€ c) : is_O_with c (Ξ» x, (0 : E')) g' l :=
is_O_with.of_bound $ univ_mem' $ Ξ» x, by simpa using mul_nonneg hc (norm_nonneg $ g' x)
theorem is_O_with_zero' : is_O_with 0 (Ξ» x, (0 : E')) g l :=
is_O_with.of_bound $ univ_mem' $ Ξ» x, by simp
theorem is_O_zero : is_O (Ξ» x, (0 : E')) g l :=
is_O_iff_is_O_with.2 β¨0, is_O_with_zero' _ _β©
theorem is_O_refl_left : is_O (Ξ» x, f' x - f' x) g' l :=
(is_O_zero g' l).congr_left $ Ξ» x, (sub_self _).symm
theorem is_o_refl_left : is_o (Ξ» x, f' x - f' x) g' l :=
(is_o_zero g' l).congr_left $ Ξ» x, (sub_self _).symm
variables {g g' l}
@[simp] theorem is_O_with_zero_right_iff :
is_O_with c f' (Ξ» x, (0 : F')) l β βαΆ x in l, f' x = 0 :=
by simp only [is_O_with, exists_prop, true_and, norm_zero, mul_zero, norm_le_zero_iff]
@[simp] theorem is_O_zero_right_iff : is_O f' (Ξ» x, (0 : F')) l β βαΆ x in l, f' x = 0 :=
β¨Ξ» h, let β¨c, hcβ© := h.is_O_with in is_O_with_zero_right_iff.1 hc,
Ξ» h, (is_O_with_zero_right_iff.2 h : is_O_with 1 _ _ _).is_Oβ©
@[simp] theorem is_o_zero_right_iff :
is_o f' (Ξ» x, (0 : F')) l β βαΆ x in l, f' x = 0 :=
β¨Ξ» h, is_O_zero_right_iff.1 h.is_O,
Ξ» h, is_o.of_is_O_with $ Ξ» c hc, is_O_with_zero_right_iff.2 hβ©
theorem is_O_with_const_const (c : E) {c' : F'} (hc' : c' β 0) (l : filter Ξ±) :
is_O_with (β₯cβ₯ / β₯c'β₯) (Ξ» x : Ξ±, c) (Ξ» x, c') l :=
begin
unfold is_O_with,
apply univ_mem',
intro x,
rw [mem_set_of_eq, div_mul_cancel],
rwa [ne.def, norm_eq_zero]
end
theorem is_O_const_const (c : E) {c' : F'} (hc' : c' β 0) (l : filter Ξ±) :
is_O (Ξ» x : Ξ±, c) (Ξ» x, c') l :=
(is_O_with_const_const c hc' l).is_O
end zero_const
@[simp] lemma is_O_with_top : is_O_with c f g β€ β β x, β₯f xβ₯ β€ c * β₯g xβ₯ := by rw is_O_with; refl
@[simp] lemma is_O_top : is_O f g β€ β β C, β x, β₯f xβ₯ β€ C * β₯g xβ₯ := by rw is_O_iff; refl
@[simp] lemma is_o_top : is_o f' g' β€ β β x, f' x = 0 :=
begin
refine β¨_, Ξ» h, (is_o_zero g' β€).congr (Ξ» x, (h x).symm) (Ξ» x, rfl)β©,
simp only [is_o_iff, eventually_top],
refine Ξ» h x, norm_le_zero_iff.1 _,
have : tendsto (Ξ» c : β, c * β₯g' xβ₯) (π[Ioi 0] 0) (π 0) :=
((continuous_id.mul continuous_const).tendsto' _ _ (zero_mul _)).mono_left inf_le_left,
exact le_of_tendsto_of_tendsto tendsto_const_nhds this
(eventually_nhds_within_iff.2 $ eventually_of_forall $ Ξ» c hc, h hc x)
end
@[simp] lemma is_O_with_principal {s : set Ξ±} :
is_O_with c f g (π s) β β x β s, β₯f xβ₯ β€ c * β₯g xβ₯ :=
by rw is_O_with; refl
lemma is_O_principal {s : set Ξ±} :
is_O f g (π s) β β c, β x β s, β₯f xβ₯ β€ c * β₯g xβ₯ :=
by rw is_O_iff; refl
theorem is_O_with_const_one (c : E) (l : filter Ξ±) : is_O_with β₯cβ₯ (Ξ» x : Ξ±, c) (Ξ» x, (1 : π)) l :=
begin
refine (is_O_with_const_const c _ l).congr_const _,
{ rw [norm_one, div_one] },
{ exact one_ne_zero }
end
theorem is_O_const_one (c : E) (l : filter Ξ±) : is_O (Ξ» x : Ξ±, c) (Ξ» x, (1 : π)) l :=
(is_O_with_const_one c l).is_O
section
variable (π)
theorem is_o_const_iff_is_o_one {c : F'} (hc : c β 0) :
is_o f (Ξ» x, c) l β is_o f (Ξ» x, (1:π)) l :=
β¨Ξ» h, h.trans_is_O $ is_O_const_one c l, Ξ» h, h.trans_is_O $ is_O_const_const _ hc _β©
end
theorem is_o_const_iff {c : F'} (hc : c β 0) :
is_o f' (Ξ» x, c) l β tendsto f' l (π 0) :=
(is_o_const_iff_is_o_one β hc).trans
begin
clear hc c,
simp only [is_o, is_O_with, norm_one, mul_one, metric.nhds_basis_closed_ball.tendsto_right_iff,
metric.mem_closed_ball, dist_zero_right]
end
theorem is_o_const_const_iff [ne_bot l] {d : E'} {c : F'} (hc : c β 0) :
is_o (Ξ» x, d) (Ξ» x, c) l β d = 0 :=
begin
rw is_o_const_iff hc,
refine β¨Ξ» h, tendsto_nhds_unique tendsto_const_nhds h, _β©,
rintros rfl,
exact tendsto_const_nhds,
end
lemma is_o_id_const {c : F'} (hc : c β 0) :
is_o (Ξ» (x : E'), x) (Ξ» x, c) (π 0) :=
(is_o_const_iff hc).mpr (continuous_id.tendsto 0)
theorem is_O_const_of_tendsto {y : E'} (h : tendsto f' l (π y)) {c : F'} (hc : c β 0) :
is_O f' (Ξ» x, c) l :=
begin
refine is_O.trans _ (is_O_const_const (β₯yβ₯ + 1) hc l),
refine is_O.of_bound 1 _,
simp only [is_O_with, one_mul],
have : tendsto (Ξ»x, β₯f' xβ₯) l (π β₯yβ₯), from (continuous_norm.tendsto _).comp h,
have Iy : β₯yβ₯ < β₯β₯yβ₯ + 1β₯, from lt_of_lt_of_le (lt_add_one _) (le_abs_self _),
exact this (ge_mem_nhds Iy)
end
section
variable (π)
theorem is_o_one_iff : is_o f' (Ξ» x, (1 : π)) l β tendsto f' l (π 0) :=
is_o_const_iff one_ne_zero
theorem is_O_one_of_tendsto {y : E'} (h : tendsto f' l (π y)) :
is_O f' (Ξ» x, (1:π)) l :=
is_O_const_of_tendsto h one_ne_zero
theorem is_O.trans_tendsto_nhds (hfg : is_O f g' l) {y : F'} (hg : tendsto g' l (π y)) :
is_O f (Ξ» x, (1:π)) l :=
hfg.trans $ is_O_one_of_tendsto π hg
end
theorem is_O.trans_tendsto (hfg : is_O f' g' l) (hg : tendsto g' l (π 0)) :
tendsto f' l (π 0) :=
(is_o_one_iff β).1 $ hfg.trans_is_o $ (is_o_one_iff β).2 hg
theorem is_o.trans_tendsto (hfg : is_o f' g' l) (hg : tendsto g' l (π 0)) :
tendsto f' l (π 0) :=
hfg.is_O.trans_tendsto hg
/-! ### Multiplication by a constant -/
theorem is_O_with_const_mul_self (c : R) (f : Ξ± β R) (l : filter Ξ±) :
is_O_with β₯cβ₯ (Ξ» x, c * f x) f l :=
is_O_with_of_le' _ $ Ξ» x, norm_mul_le _ _
theorem is_O_const_mul_self (c : R) (f : Ξ± β R) (l : filter Ξ±) :
is_O (Ξ» x, c * f x) f l :=
(is_O_with_const_mul_self c f l).is_O
theorem is_O_with.const_mul_left {f : Ξ± β R} (h : is_O_with c f g l) (c' : R) :
is_O_with (β₯c'β₯ * c) (Ξ» x, c' * f x) g l :=
(is_O_with_const_mul_self c' f l).trans h (norm_nonneg c')
theorem is_O.const_mul_left {f : Ξ± β R} (h : is_O f g l) (c' : R) :
is_O (Ξ» x, c' * f x) g l :=
let β¨c, hcβ© := h.is_O_with in (hc.const_mul_left c').is_O
theorem is_O_with_self_const_mul' (u : units R) (f : Ξ± β R) (l : filter Ξ±) :
is_O_with β₯(βuβ»ΒΉ:R)β₯ f (Ξ» x, βu * f x) l :=
(is_O_with_const_mul_self βuβ»ΒΉ _ l).congr_left $ Ξ» x, u.inv_mul_cancel_left (f x)
theorem is_O_with_self_const_mul (c : π) (hc : c β 0) (f : Ξ± β π) (l : filter Ξ±) :
is_O_with β₯cβ₯β»ΒΉ f (Ξ» x, c * f x) l :=
(is_O_with_self_const_mul' (units.mk0 c hc) f l).congr_const $
normed_field.norm_inv c
theorem is_O_self_const_mul' {c : R} (hc : is_unit c) (f : Ξ± β R) (l : filter Ξ±) :
is_O f (Ξ» x, c * f x) l :=
let β¨u, huβ© := hc in hu βΈ (is_O_with_self_const_mul' u f l).is_O
theorem is_O_self_const_mul (c : π) (hc : c β 0) (f : Ξ± β π) (l : filter Ξ±) :
is_O f (Ξ» x, c * f x) l :=
is_O_self_const_mul' (is_unit.mk0 c hc) f l
theorem is_O_const_mul_left_iff' {f : Ξ± β R} {c : R} (hc : is_unit c) :
is_O (Ξ» x, c * f x) g l β is_O f g l :=
β¨(is_O_self_const_mul' hc f l).trans, Ξ» h, h.const_mul_left cβ©
theorem is_O_const_mul_left_iff {f : Ξ± β π} {c : π} (hc : c β 0) :
is_O (Ξ» x, c * f x) g l β is_O f g l :=
is_O_const_mul_left_iff' $ is_unit.mk0 c hc
theorem is_o.const_mul_left {f : Ξ± β R} (h : is_o f g l) (c : R) :
is_o (Ξ» x, c * f x) g l :=
(is_O_const_mul_self c f l).trans_is_o h
theorem is_o_const_mul_left_iff' {f : Ξ± β R} {c : R} (hc : is_unit c) :
is_o (Ξ» x, c * f x) g l β is_o f g l :=
β¨(is_O_self_const_mul' hc f l).trans_is_o, Ξ» h, h.const_mul_left cβ©
theorem is_o_const_mul_left_iff {f : Ξ± β π} {c : π} (hc : c β 0) :
is_o (Ξ» x, c * f x) g l β is_o f g l :=
is_o_const_mul_left_iff' $ is_unit.mk0 c hc
theorem is_O_with.of_const_mul_right {g : Ξ± β R} {c : R} (hc' : 0 β€ c')
(h : is_O_with c' f (Ξ» x, c * g x) l) :
is_O_with (c' * β₯cβ₯) f g l :=
h.trans (is_O_with_const_mul_self c g l) hc'
theorem is_O.of_const_mul_right {g : Ξ± β R} {c : R}
(h : is_O f (Ξ» x, c * g x) l) :
is_O f g l :=
let β¨c, cnonneg, hcβ© := h.exists_nonneg in (hc.of_const_mul_right cnonneg).is_O
theorem is_O_with.const_mul_right' {g : Ξ± β R} {u : units R} {c' : β} (hc' : 0 β€ c')
(h : is_O_with c' f g l) :
is_O_with (c' * β₯(βuβ»ΒΉ:R)β₯) f (Ξ» x, βu * g x) l :=
h.trans (is_O_with_self_const_mul' _ _ _) hc'
theorem is_O_with.const_mul_right {g : Ξ± β π} {c : π} (hc : c β 0)
{c' : β} (hc' : 0 β€ c') (h : is_O_with c' f g l) :
is_O_with (c' * β₯cβ₯β»ΒΉ) f (Ξ» x, c * g x) l :=
h.trans (is_O_with_self_const_mul c hc g l) hc'
theorem is_O.const_mul_right' {g : Ξ± β R} {c : R} (hc : is_unit c) (h : is_O f g l) :
is_O f (Ξ» x, c * g x) l :=
h.trans (is_O_self_const_mul' hc g l)
theorem is_O.const_mul_right {g : Ξ± β π} {c : π} (hc : c β 0) (h : is_O f g l) :
is_O f (Ξ» x, c * g x) l :=
h.const_mul_right' $ is_unit.mk0 c hc
theorem is_O_const_mul_right_iff' {g : Ξ± β R} {c : R} (hc : is_unit c) :
is_O f (Ξ» x, c * g x) l β is_O f g l :=
β¨Ξ» h, h.of_const_mul_right, Ξ» h, h.const_mul_right' hcβ©
theorem is_O_const_mul_right_iff {g : Ξ± β π} {c : π} (hc : c β 0) :
is_O f (Ξ» x, c * g x) l β is_O f g l :=
is_O_const_mul_right_iff' $ is_unit.mk0 c hc
theorem is_o.of_const_mul_right {g : Ξ± β R} {c : R} (h : is_o f (Ξ» x, c * g x) l) :
is_o f g l :=
h.trans_is_O (is_O_const_mul_self c g l)
theorem is_o.const_mul_right' {g : Ξ± β R} {c : R} (hc : is_unit c) (h : is_o f g l) :
is_o f (Ξ» x, c * g x) l :=
h.trans_is_O (is_O_self_const_mul' hc g l)
theorem is_o.const_mul_right {g : Ξ± β π} {c : π} (hc : c β 0) (h : is_o f g l) :
is_o f (Ξ» x, c * g x) l :=
h.const_mul_right' $ is_unit.mk0 c hc
theorem is_o_const_mul_right_iff' {g : Ξ± β R} {c : R} (hc : is_unit c) :
is_o f (Ξ» x, c * g x) l β is_o f g l :=
β¨Ξ» h, h.of_const_mul_right, Ξ» h, h.const_mul_right' hcβ©
theorem is_o_const_mul_right_iff {g : Ξ± β π} {c : π} (hc : c β 0) :
is_o f (Ξ» x, c * g x) l β is_o f g l :=
is_o_const_mul_right_iff' $ is_unit.mk0 c hc
/-! ### Multiplication -/
theorem is_O_with.mul {fβ fβ : Ξ± β R} {gβ gβ : Ξ± β π} {cβ cβ : β}
(hβ : is_O_with cβ fβ gβ l) (hβ : is_O_with cβ fβ gβ l) :
is_O_with (cβ * cβ) (Ξ» x, fβ x * fβ x) (Ξ» x, gβ x * gβ x) l :=
begin
unfold is_O_with at *,
filter_upwards [hβ, hβ],
intros x hxβ hxβ,
apply le_trans (norm_mul_le _ _),
convert mul_le_mul hxβ hxβ (norm_nonneg _) (le_trans (norm_nonneg _) hxβ) using 1,
rw normed_field.norm_mul,
ac_refl
end
theorem is_O.mul {fβ fβ : Ξ± β R} {gβ gβ : Ξ± β π}
(hβ : is_O fβ gβ l) (hβ : is_O fβ gβ l) :
is_O (Ξ» x, fβ x * fβ x) (Ξ» x, gβ x * gβ x) l :=
let β¨c, hcβ© := hβ.is_O_with, β¨c', hc'β© := hβ.is_O_with in (hc.mul hc').is_O
theorem is_O.mul_is_o {fβ fβ : Ξ± β R} {gβ gβ : Ξ± β π}
(hβ : is_O fβ gβ l) (hβ : is_o fβ gβ l) :
is_o (Ξ» x, fβ x * fβ x) (Ξ» x, gβ x * gβ x) l :=
begin
unfold is_o at *,
intros c cpos,
rcases hβ.exists_pos with β¨c', c'pos, hc'β©,
exact (hc'.mul (hβ (div_pos cpos c'pos))).congr_const (mul_div_cancel' _ (ne_of_gt c'pos))
end
theorem is_o.mul_is_O {fβ fβ : Ξ± β R} {gβ gβ : Ξ± β π}
(hβ : is_o fβ gβ l) (hβ : is_O fβ gβ l) :
is_o (Ξ» x, fβ x * fβ x) (Ξ» x, gβ x * gβ x) l :=
begin
unfold is_o at *,
intros c cpos,
rcases hβ.exists_pos with β¨c', c'pos, hc'β©,
exact ((hβ (div_pos cpos c'pos)).mul hc').congr_const (div_mul_cancel _ (ne_of_gt c'pos))
end
theorem is_o.mul {fβ fβ : Ξ± β R} {gβ gβ : Ξ± β π} (hβ : is_o fβ gβ l) (hβ : is_o fβ gβ l) :
is_o (Ξ» x, fβ x * fβ x) (Ξ» x, gβ x * gβ x) l :=
hβ.mul_is_O hβ.is_O
theorem is_O_with.pow' {f : Ξ± β R} {g : Ξ± β π} (h : is_O_with c f g l) :
β n : β, is_O_with (nat.cases_on n β₯(1 : R)β₯ (Ξ» n, c ^ (n + 1))) (Ξ» x, f x ^ n) (Ξ» x, g x ^ n) l
| 0 := by simpa using is_O_with_const_const (1 : R) (@one_ne_zero π _ _) l
| 1 := by simpa
| (n + 2) := by simpa [pow_succ] using h.mul (is_O_with.pow' (n + 1))
theorem is_O_with.pow [norm_one_class R] {f : Ξ± β R} {g : Ξ± β π} (h : is_O_with c f g l) :
β n : β, is_O_with (c ^ n) (Ξ» x, f x ^ n) (Ξ» x, g x ^ n) l
| 0 := by simpa using h.pow' 0
| (n + 1) := h.pow' (n + 1)
theorem is_O.pow {f : Ξ± β R} {g : Ξ± β π} (h : is_O f g l) (n : β) :
is_O (Ξ» x, f x ^ n) (Ξ» x, g x ^ n) l :=
let β¨C, hCβ© := h.is_O_with in is_O_iff_is_O_with.2 β¨_, hC.pow' nβ©
theorem is_o.pow {f : Ξ± β R} {g : Ξ± β π} (h : is_o f g l) {n : β} (hn : 0 < n) :
is_o (Ξ» x, f x ^ n) (Ξ» x, g x ^ n) l :=
begin
cases n, exact hn.false.elim, clear hn,
induction n with n ihn, { simpa only [pow_one] },
convert h.mul ihn; simp [pow_succ]
end
/-! ### Scalar multiplication -/
section smul_const
variables [normed_space π E']
theorem is_O_with.const_smul_left (h : is_O_with c f' g l) (c' : π) :
is_O_with (β₯c'β₯ * c) (Ξ» x, c' β’ f' x) g l :=
by refine ((h.norm_left.const_mul_left (β₯c'β₯)).congr _ _ (Ξ» _, rfl)).of_norm_left;
intros; simp only [norm_norm, norm_smul]
theorem is_O_const_smul_left_iff {c : π} (hc : c β 0) :
is_O (Ξ» x, c β’ f' x) g l β is_O f' g l :=
begin
have cne0 : β₯cβ₯ β 0, from mt norm_eq_zero.mp hc,
rw [βis_O_norm_left], simp only [norm_smul],
rw [is_O_const_mul_left_iff cne0, is_O_norm_left],
end
theorem is_o_const_smul_left (h : is_o f' g l) (c : π) :
is_o (Ξ» x, c β’ f' x) g l :=
begin
refine ((h.norm_left.const_mul_left (β₯cβ₯)).congr_left _).of_norm_left,
exact Ξ» x, (norm_smul _ _).symm
end
theorem is_o_const_smul_left_iff {c : π} (hc : c β 0) :
is_o (Ξ» x, c β’ f' x) g l β is_o f' g l :=
begin
have cne0 : β₯cβ₯ β 0, from mt norm_eq_zero.mp hc,
rw [βis_o_norm_left], simp only [norm_smul],
rw [is_o_const_mul_left_iff cne0, is_o_norm_left]
end
theorem is_O_const_smul_right {c : π} (hc : c β 0) :
is_O f (Ξ» x, c β’ f' x) l β is_O f f' l :=
begin
have cne0 : β₯cβ₯ β 0, from mt norm_eq_zero.mp hc,
rw [βis_O_norm_right], simp only [norm_smul],
rw [is_O_const_mul_right_iff cne0, is_O_norm_right]
end
theorem is_o_const_smul_right {c : π} (hc : c β 0) :
is_o f (Ξ» x, c β’ f' x) l β is_o f f' l :=
begin
have cne0 : β₯cβ₯ β 0, from mt norm_eq_zero.mp hc,
rw [βis_o_norm_right], simp only [norm_smul],
rw [is_o_const_mul_right_iff cne0, is_o_norm_right]
end
end smul_const
section smul
variables [normed_space π E'] [normed_space π F']
theorem is_O_with.smul {kβ kβ : Ξ± β π} (hβ : is_O_with c kβ kβ l) (hβ : is_O_with c' f' g' l) :
is_O_with (c * c') (Ξ» x, kβ x β’ f' x) (Ξ» x, kβ x β’ g' x) l :=
by refine ((hβ.norm_norm.mul hβ.norm_norm).congr rfl _ _).of_norm_norm;
by intros; simp only [norm_smul]
theorem is_O.smul {kβ kβ : Ξ± β π} (hβ : is_O kβ kβ l) (hβ : is_O f' g' l) :
is_O (Ξ» x, kβ x β’ f' x) (Ξ» x, kβ x β’ g' x) l :=
by refine ((hβ.norm_norm.mul hβ.norm_norm).congr _ _).of_norm_norm;
by intros; simp only [norm_smul]
theorem is_O.smul_is_o {kβ kβ : Ξ± β π} (hβ : is_O kβ kβ l) (hβ : is_o f' g' l) :
is_o (Ξ» x, kβ x β’ f' x) (Ξ» x, kβ x β’ g' x) l :=
by refine ((hβ.norm_norm.mul_is_o hβ.norm_norm).congr _ _).of_norm_norm;
by intros; simp only [norm_smul]
theorem is_o.smul_is_O {kβ kβ : Ξ± β π} (hβ : is_o kβ kβ l) (hβ : is_O f' g' l) :
is_o (Ξ» x, kβ x β’ f' x) (Ξ» x, kβ x β’ g' x) l :=
by refine ((hβ.norm_norm.mul_is_O hβ.norm_norm).congr _ _).of_norm_norm;
by intros; simp only [norm_smul]
theorem is_o.smul {kβ kβ : Ξ± β π} (hβ : is_o kβ kβ l) (hβ : is_o f' g' l) :
is_o (Ξ» x, kβ x β’ f' x) (Ξ» x, kβ x β’ g' x) l :=
by refine ((hβ.norm_norm.mul hβ.norm_norm).congr _ _).of_norm_norm;
by intros; simp only [norm_smul]
end smul
/-! ### Sum -/
section sum
variables {ΞΉ : Type*} {A : ΞΉ β Ξ± β E'} {C : ΞΉ β β} {s : finset ΞΉ}
theorem is_O_with.sum (h : β i β s, is_O_with (C i) (A i) g l) :
is_O_with (β i in s, C i) (Ξ» x, β i in s, A i x) g l :=
begin
induction s using finset.induction_on with i s is IH,
{ simp only [is_O_with_zero', finset.sum_empty, forall_true_iff] },
{ simp only [is, finset.sum_insert, not_false_iff],
exact (h _ (finset.mem_insert_self i s)).add (IH (Ξ» j hj, h _ (finset.mem_insert_of_mem hj))) }
end
theorem is_O.sum (h : β i β s, is_O (A i) g l) :
is_O (Ξ» x, β i in s, A i x) g l :=
begin
induction s using finset.induction_on with i s is IH,
{ simp only [is_O_zero, finset.sum_empty, forall_true_iff] },
{ simp only [is, finset.sum_insert, not_false_iff],
exact (h _ (finset.mem_insert_self i s)).add (IH (Ξ» j hj, h _ (finset.mem_insert_of_mem hj))) }
end
theorem is_o.sum (h : β i β s, is_o (A i) g' l) :
is_o (Ξ» x, β i in s, A i x) g' l :=
begin
induction s using finset.induction_on with i s is IH,
{ simp only [is_o_zero, finset.sum_empty, forall_true_iff] },
{ simp only [is, finset.sum_insert, not_false_iff],
exact (h _ (finset.mem_insert_self i s)).add (IH (Ξ» j hj, h _ (finset.mem_insert_of_mem hj))) }
end
end sum
/-! ### Relation between `f = o(g)` and `f / g β 0` -/
theorem is_o.tendsto_0 {f g : Ξ± β π} {l : filter Ξ±} (h : is_o f g l) :
tendsto (Ξ» x, f x / (g x)) l (π 0) :=
have eqβ : is_o (Ξ» x, f x / g x) (Ξ» x, g x / g x) l,
by simpa only [div_eq_mul_inv] using h.mul_is_O (is_O_refl _ _),
have eqβ : is_O (Ξ» x, g x / g x) (Ξ» x, (1 : π)) l,
from is_O_of_le _ (Ξ» x, by by_cases h : β₯g xβ₯ = 0; simp [h, zero_le_one]),
(is_o_one_iff π).mp (eqβ.trans_is_O eqβ)
theorem is_o_iff_tendsto' {f g : Ξ± β π} {l : filter Ξ±}
(hgf : βαΆ x in l, g x = 0 β f x = 0) :
is_o f g l β tendsto (Ξ» x, f x / (g x)) l (π 0) :=
iff.intro is_o.tendsto_0 $ Ξ» h,
(((is_o_one_iff _).mpr h).mul_is_O (is_O_refl g l)).congr'
(hgf.mono $ Ξ» x, div_mul_cancel_of_imp) (eventually_of_forall $ Ξ» x, one_mul _)
theorem is_o_iff_tendsto {f g : Ξ± β π} {l : filter Ξ±}
(hgf : β x, g x = 0 β f x = 0) :
is_o f g l β tendsto (Ξ» x, f x / (g x)) l (π 0) :=
β¨Ξ» h, h.tendsto_0, (is_o_iff_tendsto' (eventually_of_forall hgf)).2β©
alias is_o_iff_tendsto' β _ asymptotics.is_o_of_tendsto'
alias is_o_iff_tendsto β _ asymptotics.is_o_of_tendsto
/-!
### Eventually (u / v) * v = u
If `u` and `v` are linked by an `is_O_with` relation, then we
eventually have `(u / v) * v = u`, even if `v` vanishes.
-/
section eventually_mul_div_cancel
variables {u v : Ξ± β π}
lemma is_O_with.eventually_mul_div_cancel (h : is_O_with c u v l) :
(u / v) * v =αΆ [l] u :=
eventually.mono h.bound (Ξ» y hy, div_mul_cancel_of_imp $ Ξ» hv, by simpa [hv] using hy)
/-- If `u = O(v)` along `l`, then `(u / v) * v = u` eventually at `l`. -/
lemma is_O.eventually_mul_div_cancel (h : is_O u v l) : (u / v) * v =αΆ [l] u :=
let β¨c, hcβ© := h.is_O_with in hc.eventually_mul_div_cancel
/-- If `u = o(v)` along `l`, then `(u / v) * v = u` eventually at `l`. -/
lemma is_o.eventually_mul_div_cancel (h : is_o u v l) : (u / v) * v =αΆ [l] u :=
(h.forall_is_O_with zero_lt_one).eventually_mul_div_cancel
end eventually_mul_div_cancel
/-! ### Equivalent definitions of the form `β Ο, u =αΆ [l] Ο * v` in a `normed_field`. -/
section exists_mul_eq
variables {u v : Ξ± β π}
/-- If `β₯Οβ₯` is eventually bounded by `c`, and `u =αΆ [l] Ο * v`, then we have `is_O_with c u v l`.
This does not require any assumptions on `c`, which is why we keep this version along with
`is_O_with_iff_exists_eq_mul`. -/
lemma is_O_with_of_eq_mul (Ο : Ξ± β π) (hΟ : βαΆ x in l, β₯Ο xβ₯ β€ c) (h : u =αΆ [l] Ο * v) :
is_O_with c u v l :=
begin
unfold is_O_with,
refine h.symm.rw (Ξ» x a, β₯aβ₯ β€ c * β₯v xβ₯) (hΟ.mono $ Ξ» x hx, _),
simp only [normed_field.norm_mul, pi.mul_apply],
exact mul_le_mul_of_nonneg_right hx (norm_nonneg _)
end
lemma is_O_with_iff_exists_eq_mul (hc : 0 β€ c) :
is_O_with c u v l β β (Ο : Ξ± β π) (hΟ : βαΆ x in l, β₯Ο xβ₯ β€ c), u =αΆ [l] Ο * v :=
begin
split,
{ intro h,
use (Ξ» x, u x / v x),
refine β¨eventually.mono h.bound (Ξ» y hy, _), h.eventually_mul_div_cancel.symmβ©,
simpa using div_le_of_nonneg_of_le_mul (norm_nonneg _) hc hy },
{ rintros β¨Ο, hΟ, hβ©,
exact is_O_with_of_eq_mul Ο hΟ h }
end
lemma is_O_with.exists_eq_mul (h : is_O_with c u v l) (hc : 0 β€ c) :
β (Ο : Ξ± β π) (hΟ : βαΆ x in l, β₯Ο xβ₯ β€ c), u =αΆ [l] Ο * v :=
(is_O_with_iff_exists_eq_mul hc).mp h
lemma is_O_iff_exists_eq_mul :
is_O u v l β β (Ο : Ξ± β π) (hΟ : l.is_bounded_under (β€) (norm β Ο)), u =αΆ [l] Ο * v :=
begin
split,
{ rintros h,
rcases h.exists_nonneg with β¨c, hnnc, hcβ©,
rcases hc.exists_eq_mul hnnc with β¨Ο, hΟ, huvΟβ©,
exact β¨Ο, β¨c, hΟβ©, huvΟβ© },
{ rintros β¨Ο, β¨c, hΟβ©, huvΟβ©,
exact is_O_iff_is_O_with.2 β¨c, is_O_with_of_eq_mul Ο hΟ huvΟβ© }
end
alias is_O_iff_exists_eq_mul β asymptotics.is_O.exists_eq_mul _
lemma is_o_iff_exists_eq_mul :
is_o u v l β β (Ο : Ξ± β π) (hΟ : tendsto Ο l (π 0)), u =αΆ [l] Ο * v :=
begin
split,
{ exact Ξ» h, β¨Ξ» x, u x / v x, h.tendsto_0, h.eventually_mul_div_cancel.symmβ© },
{ unfold is_o, rintros β¨Ο, hΟ, huvΟβ© c hpos,
rw normed_group.tendsto_nhds_zero at hΟ,
exact is_O_with_of_eq_mul _ ((hΟ c hpos).mono $ Ξ» x, le_of_lt) huvΟ }
end
alias is_o_iff_exists_eq_mul β asymptotics.is_o.exists_eq_mul _
end exists_mul_eq
/-! ### Miscellanous lemmas -/
theorem div_is_bounded_under_of_is_O {Ξ± : Type*} {l : filter Ξ±}
{f g : Ξ± β π} (h : is_O f g l) :
is_bounded_under (β€) l (Ξ» x, β₯f x / g xβ₯) :=
begin
obtain β¨c, hcβ© := is_O_iff.mp h,
refine β¨max c 0, eventually_map.2 (filter.mem_of_superset hc (Ξ» x hx, _))β©,
simp only [mem_set_of_eq, normed_field.norm_div] at β’ hx,
by_cases hgx : g x = 0,
{ rw [hgx, norm_zero, div_zero, le_max_iff],
exact or.inr le_rfl },
{ exact le_max_iff.2 (or.inl ((div_le_iff (norm_pos_iff.2 hgx)).2 hx)) }
end
theorem is_O_iff_div_is_bounded_under {Ξ± : Type*} {l : filter Ξ±}
{f g : Ξ± β π} (hgf : βαΆ x in l, g x = 0 β f x = 0) :
is_O f g l β is_bounded_under (β€) l (Ξ» x, β₯f x / g xβ₯) :=
begin
refine β¨div_is_bounded_under_of_is_O, Ξ» h, _β©,
obtain β¨c, hcβ© := h,
rw filter.eventually_iff at hgf hc,
simp only [mem_set_of_eq, mem_map, normed_field.norm_div] at hc,
refine is_O_iff.2 β¨c, filter.eventually_of_mem (inter_mem hgf hc) (Ξ» x hx, _)β©,
by_cases hgx : g x = 0,
{ simp [hx.1 hgx, hgx] },
{ refine (div_le_iff (norm_pos_iff.2 hgx)).mp hx.2 },
end
theorem is_O_of_div_tendsto_nhds {Ξ± : Type*} {l : filter Ξ±}
{f g : Ξ± β π} (hgf : βαΆ x in l, g x = 0 β f x = 0)
(c : π) (H : filter.tendsto (f / g) l (π c)) :
is_O f g l :=
(is_O_iff_div_is_bounded_under hgf).2 $ is_bounded_under_of_tendsto H
lemma is_o.tendsto_zero_of_tendsto {Ξ± E π : Type*} [normed_group E] [normed_field π] {u : Ξ± β E}
{v : Ξ± β π} {l : filter Ξ±} {y : π} (huv : is_o u v l) (hv : tendsto v l (π y)) :
tendsto u l (π 0) :=
begin
suffices h : is_o u (Ξ» x, (1 : π)) l,
{ rwa is_o_one_iff at h },
exact huv.trans_is_O (is_O_one_of_tendsto π hv),
end
theorem is_o_pow_pow {m n : β} (h : m < n) :
is_o (Ξ»(x : π), x^n) (Ξ»x, x^m) (π 0) :=
begin
let p := n - m,
have nmp : n = m + p := (nat.add_sub_cancel' (le_of_lt h)).symm,
have : (Ξ»(x : π), x^m) = (Ξ»x, x^m * 1), by simp only [mul_one],
simp only [this, pow_add, nmp],
refine is_O.mul_is_o (is_O_refl _ _) ((is_o_one_iff _).2 _),
convert (continuous_pow p).tendsto (0 : π),
exact (zero_pow (nat.sub_pos_of_lt h)).symm
end
theorem is_o_norm_pow_norm_pow {m n : β} (h : m < n) :
is_o (Ξ»(x : E'), β₯xβ₯^n) (Ξ»x, β₯xβ₯^m) (π (0 : E')) :=
(is_o_pow_pow h).comp_tendsto tendsto_norm_zero
theorem is_o_pow_id {n : β} (h : 1 < n) :
is_o (Ξ»(x : π), x^n) (Ξ»x, x) (π 0) :=
by { convert is_o_pow_pow h, simp only [pow_one] }
theorem is_o_norm_pow_id {n : β} (h : 1 < n) :
is_o (Ξ»(x : E'), β₯xβ₯^n) (Ξ»x, x) (π 0) :=
by simpa only [pow_one, is_o_norm_right] using @is_o_norm_pow_norm_pow E' _ _ _ h
theorem is_O_with.right_le_sub_of_lt_1 {fβ fβ : Ξ± β E'} (h : is_O_with c fβ fβ l) (hc : c < 1) :
is_O_with (1 / (1 - c)) fβ (Ξ»x, fβ x - fβ x) l :=
is_O_with.of_bound $ mem_of_superset h.bound $ Ξ» x hx,
begin
simp only [mem_set_of_eq] at hx β’,
rw [mul_comm, one_div, β div_eq_mul_inv, le_div_iff, mul_sub, mul_one, mul_comm],
{ exact le_trans (sub_le_sub_left hx _) (norm_sub_norm_le _ _) },
{ exact sub_pos.2 hc }
end
theorem is_O_with.right_le_add_of_lt_1 {fβ fβ : Ξ± β E'} (h : is_O_with c fβ fβ l) (hc : c < 1) :
is_O_with (1 / (1 - c)) fβ (Ξ»x, fβ x + fβ x) l :=
(h.neg_right.right_le_sub_of_lt_1 hc).neg_right.of_neg_left.congr rfl (Ξ» x, rfl)
(Ξ» x, by rw [neg_sub, sub_neg_eq_add])
theorem is_o.right_is_O_sub {fβ fβ : Ξ± β E'} (h : is_o fβ fβ l) :
is_O fβ (Ξ»x, fβ x - fβ x) l :=
((h.def' one_half_pos).right_le_sub_of_lt_1 one_half_lt_one).is_O
theorem is_o.right_is_O_add {fβ fβ : Ξ± β E'} (h : is_o fβ fβ l) :
is_O fβ (Ξ»x, fβ x + fβ x) l :=
((h.def' one_half_pos).right_le_add_of_lt_1 one_half_lt_one).is_O
/-- If `f x = O(g x)` along `cofinite`, then there exists a positive constant `C` such that
`β₯f xβ₯ β€ C * β₯g xβ₯` whenever `g x β 0`. -/
theorem bound_of_is_O_cofinite (h : is_O f g' cofinite) :
β C > 0, β β¦xβ¦, g' x β 0 β β₯f xβ₯ β€ C * β₯g' xβ₯ :=
begin
rcases h.exists_pos with β¨C, Cβ, hCβ©,
rw [is_O_with, eventually_cofinite] at hC,
rcases (hC.to_finset.image (Ξ» x, β₯f xβ₯ / β₯g' xβ₯)).exists_le with β¨C', hC'β©,
have : β x, C * β₯g' xβ₯ < β₯f xβ₯ β β₯f xβ₯ / β₯g' xβ₯ β€ C', by simpa using hC',
refine β¨max C C', lt_max_iff.2 (or.inl Cβ), Ξ» x hβ, _β©,
rw [max_mul_of_nonneg _ _ (norm_nonneg _), le_max_iff, or_iff_not_imp_left, not_le],
exact Ξ» hx, (div_le_iff (norm_pos_iff.2 hβ)).1 (this _ hx)
end
theorem is_O_cofinite_iff (h : β x, g' x = 0 β f' x = 0) :
is_O f' g' cofinite β β C, β x, β₯f' xβ₯ β€ C * β₯g' xβ₯ :=
β¨Ξ» h', let β¨C, Cβ, hCβ© := bound_of_is_O_cofinite h' in
β¨C, Ξ» x, if hx : g' x = 0 then by simp [h _ hx, hx] else hC hxβ©,
Ξ» h, (is_O_top.2 h).mono le_topβ©
theorem bound_of_is_O_nat_at_top {f : β β E} {g' : β β E'} (h : is_O f g' at_top) :
β C > 0, β β¦xβ¦, g' x β 0 β β₯f xβ₯ β€ C * β₯g' xβ₯ :=
bound_of_is_O_cofinite $ by rwa nat.cofinite_eq_at_top
theorem is_O_nat_at_top_iff {f : β β E'} {g : β β F'} (h : β x, g x = 0 β f x = 0) :
is_O f g at_top β β C, β x, β₯f xβ₯ β€ C * β₯g xβ₯ :=
by rw [β nat.cofinite_eq_at_top, is_O_cofinite_iff h]
theorem is_O_one_nat_at_top_iff {f : β β E'} :
is_O f (Ξ» n, 1 : β β β) at_top β β C, β n, β₯f nβ₯ β€ C :=
iff.trans (is_O_nat_at_top_iff (Ξ» n h, (one_ne_zero h).elim)) $
by simp only [norm_one, mul_one]
theorem is_O_with_pi {ΞΉ : Type*} [fintype ΞΉ] {E' : ΞΉ β Type*} [Ξ i, normed_group (E' i)]
{f : Ξ± β Ξ i, E' i} {C : β} (hC : 0 β€ C) :
is_O_with C f g' l β β i, is_O_with C (Ξ» x, f x i) g' l :=
have β x, 0 β€ C * β₯g' xβ₯, from Ξ» x, mul_nonneg hC (norm_nonneg _),
by simp only [is_O_with_iff, pi_norm_le_iff (this _), eventually_all]
@[simp] theorem is_O_pi {ΞΉ : Type*} [fintype ΞΉ] {E' : ΞΉ β Type*} [Ξ i, normed_group (E' i)]
{f : Ξ± β Ξ i, E' i} :
is_O f g' l β β i, is_O (Ξ» x, f x i) g' l :=
begin
simp only [is_O_iff_eventually_is_O_with, β eventually_all],
exact eventually_congr (eventually_at_top.2 β¨0, Ξ» c, is_O_with_piβ©)
end
@[simp] theorem is_o_pi {ΞΉ : Type*} [fintype ΞΉ] {E' : ΞΉ β Type*} [Ξ i, normed_group (E' i)]
{f : Ξ± β Ξ i, E' i} :
is_o f g' l β β i, is_o (Ξ» x, f x i) g' l :=
begin
simp only [is_o, is_O_with_pi, le_of_lt] { contextual := tt },
exact β¨Ξ» h i c hc, h hc i, Ξ» h c hc i, h i hcβ©
end
end asymptotics
open asymptotics
lemma summable_of_is_O {ΞΉ E} [normed_group E] [complete_space E] {f : ΞΉ β E} {g : ΞΉ β β}
(hg : summable g) (h : is_O f g cofinite) : summable f :=
let β¨C, hCβ© := h.is_O_with in
summable_of_norm_bounded_eventually (Ξ» x, C * β₯g xβ₯) (hg.abs.mul_left _) hC.bound
lemma summable_of_is_O_nat {E} [normed_group E] [complete_space E] {f : β β E} {g : β β β}
(hg : summable g) (h : is_O f g at_top) : summable f :=
summable_of_is_O hg $ nat.cofinite_eq_at_top.symm βΈ h
namespace local_homeomorph
variables {Ξ± : Type*} {Ξ² : Type*} [topological_space Ξ±] [topological_space Ξ²]
variables {E : Type*} [has_norm E] {F : Type*} [has_norm F]
/-- Transfer `is_O_with` over a `local_homeomorph`. -/
lemma is_O_with_congr (e : local_homeomorph Ξ± Ξ²) {b : Ξ²} (hb : b β e.target)
{f : Ξ² β E} {g : Ξ² β F} {C : β} :
is_O_with C f g (π b) β is_O_with C (f β e) (g β e) (π (e.symm b)) :=
β¨Ξ» h, h.comp_tendsto $
by { convert e.continuous_at (e.map_target hb), exact (e.right_inv hb).symm },
Ξ» h, (h.comp_tendsto (e.continuous_at_symm hb)).congr' rfl
((e.eventually_right_inverse hb).mono $ Ξ» x hx, congr_arg f hx)
((e.eventually_right_inverse hb).mono $ Ξ» x hx, congr_arg g hx)β©
/-- Transfer `is_O` over a `local_homeomorph`. -/
lemma is_O_congr (e : local_homeomorph Ξ± Ξ²) {b : Ξ²} (hb : b β e.target) {f : Ξ² β E} {g : Ξ² β F} :
is_O f g (π b) β is_O (f β e) (g β e) (π (e.symm b)) :=
by { unfold is_O, exact exists_congr (Ξ» C, e.is_O_with_congr hb) }
/-- Transfer `is_o` over a `local_homeomorph`. -/
lemma is_o_congr (e : local_homeomorph Ξ± Ξ²) {b : Ξ²} (hb : b β e.target) {f : Ξ² β E} {g : Ξ² β F} :
is_o f g (π b) β is_o (f β e) (g β e) (π (e.symm b)) :=
by { unfold is_o, exact (forall_congr $ Ξ» c, forall_congr $ Ξ» hc, e.is_O_with_congr hb) }
end local_homeomorph
namespace homeomorph
variables {Ξ± : Type*} {Ξ² : Type*} [topological_space Ξ±] [topological_space Ξ²]
variables {E : Type*} [has_norm E] {F : Type*} [has_norm F]
open asymptotics
/-- Transfer `is_O_with` over a `homeomorph`. -/
lemma is_O_with_congr (e : Ξ± ββ Ξ²) {b : Ξ²} {f : Ξ² β E} {g : Ξ² β F} {C : β} :
is_O_with C f g (π b) β is_O_with C (f β e) (g β e) (π (e.symm b)) :=
e.to_local_homeomorph.is_O_with_congr trivial
/-- Transfer `is_O` over a `homeomorph`. -/
lemma is_O_congr (e : Ξ± ββ Ξ²) {b : Ξ²} {f : Ξ² β E} {g : Ξ² β F} :
is_O f g (π b) β is_O (f β e) (g β e) (π (e.symm b)) :=
by { unfold is_O, exact exists_congr (Ξ» C, e.is_O_with_congr) }
/-- Transfer `is_o` over a `homeomorph`. -/
lemma is_o_congr (e : Ξ± ββ Ξ²) {b : Ξ²} {f : Ξ² β E} {g : Ξ² β F} :
is_o f g (π b) β is_o (f β e) (g β e) (π (e.symm b)) :=
by { unfold is_o, exact forall_congr (Ξ» c, forall_congr (Ξ» hc, e.is_O_with_congr)) }
end homeomorph
|
28704b2bb4d652e4bf9b491eb018cbc9f6b30b8d | d29d82a0af640c937e499f6be79fc552eae0aa13 | /src/data/int/basic.lean | 09f5ad8f75c838bed2e6034f1a4ddf65ba6cb5c1 | [
"Apache-2.0"
] | permissive | AbdulMajeedkhurasani/mathlib | 835f8a5c5cf3075b250b3737172043ab4fa1edf6 | 79bc7323b164aebd000524ebafd198eb0e17f956 | refs/heads/master | 1,688,003,895,660 | 1,627,788,521,000 | 1,627,788,521,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 57,658 | lean | /-
Copyright (c) 2016 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad
The integers, with addition, multiplication, and subtraction.
-/
import data.nat.pow
import algebra.order_functions
open nat
namespace int
instance : inhabited β€ := β¨int.zeroβ©
instance : nontrivial β€ :=
β¨β¨0, 1, int.zero_ne_oneβ©β©
instance : comm_ring int :=
{ add := int.add,
add_assoc := int.add_assoc,
zero := int.zero,
zero_add := int.zero_add,
add_zero := int.add_zero,
neg := int.neg,
add_left_neg := int.add_left_neg,
add_comm := int.add_comm,
mul := int.mul,
mul_assoc := int.mul_assoc,
one := int.one,
one_mul := int.one_mul,
mul_one := int.mul_one,
sub := int.sub,
left_distrib := int.distrib_left,
right_distrib := int.distrib_right,
mul_comm := int.mul_comm,
gsmul := (*),
gsmul_zero' := int.zero_mul,
gsmul_succ' := Ξ» n x, by rw [succ_eq_one_add, of_nat_add, int.distrib_right, of_nat_one,
int.one_mul],
gsmul_neg' := Ξ» n x, neg_mul_eq_neg_mul_symm (n.succ : β€) x }
/-! ### Extra instances to short-circuit type class resolution -/
-- instance : has_sub int := by apply_instance -- This is in core
instance : add_comm_monoid int := by apply_instance
instance : add_monoid int := by apply_instance
instance : monoid int := by apply_instance
instance : comm_monoid int := by apply_instance
instance : comm_semigroup int := by apply_instance
instance : semigroup int := by apply_instance
instance : add_comm_semigroup int := by apply_instance
instance : add_semigroup int := by apply_instance
instance : comm_semiring int := by apply_instance
instance : semiring int := by apply_instance
instance : ring int := by apply_instance
instance : distrib int := by apply_instance
instance : linear_ordered_comm_ring int :=
{ add_le_add_left := @int.add_le_add_left,
mul_pos := @int.mul_pos,
zero_le_one := le_of_lt int.zero_lt_one,
.. int.comm_ring, .. int.linear_order, .. int.nontrivial }
instance : linear_ordered_add_comm_group int :=
by apply_instance
@[simp] lemma add_neg_one (i : β€) : i + -1 = i - 1 := rfl
theorem abs_eq_nat_abs : β a : β€, abs a = nat_abs a
| (n : β) := abs_of_nonneg $ coe_zero_le _
| -[1+ n] := abs_of_nonpos $ le_of_lt $ neg_succ_lt_zero _
theorem nat_abs_abs (a : β€) : nat_abs (abs a) = nat_abs a :=
by rw [abs_eq_nat_abs]; refl
theorem sign_mul_abs (a : β€) : sign a * abs a = a :=
by rw [abs_eq_nat_abs, sign_mul_nat_abs]
@[simp] lemma default_eq_zero : default β€ = 0 := rfl
meta instance : has_to_format β€ := β¨Ξ» z, to_string zβ©
meta instance : has_reflect β€ := by tactic.mk_has_reflect_instance
attribute [simp] int.coe_nat_add int.coe_nat_mul int.coe_nat_zero int.coe_nat_one int.coe_nat_succ
attribute [simp] int.of_nat_eq_coe int.bodd
@[simp] theorem add_def {a b : β€} : int.add a b = a + b := rfl
@[simp] theorem mul_def {a b : β€} : int.mul a b = a * b := rfl
@[simp] lemma neg_succ_not_nonneg (n : β) : 0 β€ -[1+ n] β false :=
by { simp only [not_le, iff_false], exact int.neg_succ_lt_zero n, }
@[simp] lemma neg_succ_not_pos (n : β) : 0 < -[1+ n] β false :=
by simp only [not_lt, iff_false]
@[simp] lemma neg_succ_sub_one (n : β) : -[1+ n] - 1 = -[1+ (n+1)] := rfl
@[simp] theorem coe_nat_mul_neg_succ (m n : β) : (m : β€) * -[1+ n] = -(m * succ n) := rfl
@[simp] theorem neg_succ_mul_coe_nat (m n : β) : -[1+ m] * n = -(succ m * n) := rfl
@[simp] theorem neg_succ_mul_neg_succ (m n : β) : -[1+ m] * -[1+ n] = succ m * succ n := rfl
@[simp, norm_cast]
theorem coe_nat_le {m n : β} : (βm : β€) β€ βn β m β€ n := coe_nat_le_coe_nat_iff m n
@[simp, norm_cast]
theorem coe_nat_lt {m n : β} : (βm : β€) < βn β m < n := coe_nat_lt_coe_nat_iff m n
@[simp, norm_cast]
theorem coe_nat_inj' {m n : β} : (βm : β€) = βn β m = n := int.coe_nat_eq_coe_nat_iff m n
@[simp] theorem coe_nat_pos {n : β} : (0 : β€) < n β 0 < n :=
by rw [β int.coe_nat_zero, coe_nat_lt]
@[simp] theorem coe_nat_eq_zero {n : β} : (n : β€) = 0 β n = 0 :=
by rw [β int.coe_nat_zero, coe_nat_inj']
theorem coe_nat_ne_zero {n : β} : (n : β€) β 0 β n β 0 :=
not_congr coe_nat_eq_zero
@[simp] lemma coe_nat_nonneg (n : β) : 0 β€ (n : β€) := coe_nat_le.2 (nat.zero_le _)
lemma coe_nat_ne_zero_iff_pos {n : β} : (n : β€) β 0 β 0 < n :=
β¨Ξ» h, nat.pos_of_ne_zero (coe_nat_ne_zero.1 h),
Ξ» h, (ne_of_lt (coe_nat_lt.2 h)).symmβ©
lemma coe_nat_succ_pos (n : β) : 0 < (n.succ : β€) := int.coe_nat_pos.2 (succ_pos n)
@[simp, norm_cast] theorem coe_nat_abs (n : β) : abs (n : β€) = n :=
abs_of_nonneg (coe_nat_nonneg n)
/-! ### succ and pred -/
/-- Immediate successor of an integer: `succ n = n + 1` -/
def succ (a : β€) := a + 1
/-- Immediate predecessor of an integer: `pred n = n - 1` -/
def pred (a : β€) := a - 1
theorem nat_succ_eq_int_succ (n : β) : (nat.succ n : β€) = int.succ n := rfl
theorem pred_succ (a : β€) : pred (succ a) = a := add_sub_cancel _ _
theorem succ_pred (a : β€) : succ (pred a) = a := sub_add_cancel _ _
theorem neg_succ (a : β€) : -succ a = pred (-a) := neg_add _ _
theorem succ_neg_succ (a : β€) : succ (-succ a) = -a :=
by rw [neg_succ, succ_pred]
theorem neg_pred (a : β€) : -pred a = succ (-a) :=
by rw [eq_neg_of_eq_neg (neg_succ (-a)).symm, neg_neg]
theorem pred_neg_pred (a : β€) : pred (-pred a) = -a :=
by rw [neg_pred, pred_succ]
theorem pred_nat_succ (n : β) : pred (nat.succ n) = n := pred_succ n
theorem neg_nat_succ (n : β) : -(nat.succ n : β€) = pred (-n) := neg_succ n
theorem succ_neg_nat_succ (n : β) : succ (-nat.succ n) = -n := succ_neg_succ n
theorem lt_succ_self (a : β€) : a < succ a :=
lt_add_of_pos_right _ zero_lt_one
theorem pred_self_lt (a : β€) : pred a < a :=
sub_lt_self _ zero_lt_one
theorem add_one_le_iff {a b : β€} : a + 1 β€ b β a < b := iff.rfl
theorem lt_add_one_iff {a b : β€} : a < b + 1 β a β€ b :=
add_le_add_iff_right _
@[simp] lemma succ_coe_nat_pos (n : β) : 0 < (n : β€) + 1 :=
lt_add_one_iff.mpr (by simp)
@[norm_cast] lemma coe_pred_of_pos {n : β} (h : 0 < n) : ((n - 1 : β) : β€) = (n : β€) - 1 :=
by { cases n, cases h, simp, }
lemma le_add_one {a b : β€} (h : a β€ b) : a β€ b + 1 :=
le_of_lt (int.lt_add_one_iff.mpr h)
theorem sub_one_lt_iff {a b : β€} : a - 1 < b β a β€ b :=
sub_lt_iff_lt_add.trans lt_add_one_iff
theorem le_sub_one_iff {a b : β€} : a β€ b - 1 β a < b :=
le_sub_iff_add_le
@[simp] lemma eq_zero_iff_abs_lt_one {a : β€} : abs a < 1 β a = 0 :=
β¨Ξ» a0, let β¨hn, hpβ© := abs_lt.mp a0 in (le_of_lt_add_one (by exact hp)).antisymm hn,
Ξ» a0, (abs_eq_zero.mpr a0).le.trans_lt zero_lt_oneβ©
@[elab_as_eliminator] protected lemma induction_on {p : β€ β Prop}
(i : β€) (hz : p 0) (hp : β i : β, p i β p (i + 1)) (hn : β i : β, p (-i) β p (-i - 1)) : p i :=
begin
induction i,
{ induction i,
{ exact hz },
{ exact hp _ i_ih } },
{ have : β n:β, p (- n),
{ intro n, induction n,
{ simp [hz] },
{ convert hn _ n_ih using 1, simp [sub_eq_neg_add] } },
exact this (i + 1) }
end
/-- Inductively define a function on `β€` by defining it at `b`, for the `succ` of a number greater
than `b`, and the `pred` of a number less than `b`. -/
protected def induction_on' {C : β€ β Sort*} (z : β€) (b : β€) :
C b β (β k, b β€ k β C k β C (k + 1)) β (β k β€ b, C k β C (k - 1)) β C z :=
Ξ» H0 Hs Hp,
begin
rw βsub_add_cancel z b,
induction (z - b) with n n,
{ induction n with n ih, { rwa [of_nat_zero, zero_add] },
rw [of_nat_succ, add_assoc, add_comm 1 b, βadd_assoc],
exact Hs _ (le_add_of_nonneg_left (of_nat_nonneg _)) ih },
{ induction n with n ih,
{ rw [neg_succ_of_nat_eq, βof_nat_eq_coe, of_nat_zero, zero_add, neg_add_eq_sub],
exact Hp _ (le_refl _) H0 },
{ rw [neg_succ_of_nat_coe', nat.succ_eq_add_one, βneg_succ_of_nat_coe, sub_add_eq_add_sub],
exact Hp _ (le_of_lt (add_lt_of_neg_of_le (neg_succ_lt_zero _) (le_refl _))) ih } }
end
/-! ### nat abs -/
attribute [simp] nat_abs nat_abs_of_nat nat_abs_zero nat_abs_one
theorem nat_abs_add_le (a b : β€) : nat_abs (a + b) β€ nat_abs a + nat_abs b :=
begin
have : β (a b : β), nat_abs (sub_nat_nat a (nat.succ b)) β€ nat.succ (a + b),
{ refine (Ξ» a b : β, sub_nat_nat_elim a b.succ
(Ξ» m n i, n = b.succ β nat_abs i β€ (m + b).succ) _ _ rfl);
intros i n e,
{ subst e, rw [add_comm _ i, add_assoc],
exact nat.le_add_right i (b.succ + b).succ },
{ apply succ_le_succ,
rw [β succ.inj e, β add_assoc, add_comm],
apply nat.le_add_right } },
cases a; cases b with b b; simp [nat_abs, nat.succ_add];
try {refl}; [skip, rw add_comm a b]; apply this
end
lemma nat_abs_sub_le (a b : β€) : nat_abs (a - b) β€ nat_abs a + nat_abs b :=
by { rw [sub_eq_add_neg, β int.nat_abs_neg b], apply nat_abs_add_le }
theorem nat_abs_neg_of_nat (n : β) : nat_abs (neg_of_nat n) = n :=
by cases n; refl
theorem nat_abs_mul (a b : β€) : nat_abs (a * b) = (nat_abs a) * (nat_abs b) :=
by cases a; cases b;
simp only [β int.mul_def, int.mul, nat_abs_neg_of_nat, eq_self_iff_true, int.nat_abs]
lemma nat_abs_mul_nat_abs_eq {a b : β€} {c : β} (h : a * b = (c : β€)) :
a.nat_abs * b.nat_abs = c :=
by rw [β nat_abs_mul, h, nat_abs_of_nat]
@[simp] lemma nat_abs_mul_self' (a : β€) : (nat_abs a * nat_abs a : β€) = a * a :=
by rw [β int.coe_nat_mul, nat_abs_mul_self]
theorem neg_succ_of_nat_eq' (m : β) : -[1+ m] = -m - 1 :=
by simp [neg_succ_of_nat_eq, sub_eq_neg_add]
lemma nat_abs_ne_zero_of_ne_zero {z : β€} (hz : z β 0) : z.nat_abs β 0 :=
Ξ» h, hz $ int.eq_zero_of_nat_abs_eq_zero h
@[simp] lemma nat_abs_eq_zero {a : β€} : a.nat_abs = 0 β a = 0 :=
β¨int.eq_zero_of_nat_abs_eq_zero, Ξ» h, h.symm βΈ rflβ©
lemma nat_abs_ne_zero {a : β€} : a.nat_abs β 0 β a β 0 := not_congr int.nat_abs_eq_zero
lemma nat_abs_lt_nat_abs_of_nonneg_of_lt {a b : β€} (wβ : 0 β€ a) (wβ : a < b) :
a.nat_abs < b.nat_abs :=
begin
lift b to β using le_trans wβ (le_of_lt wβ),
lift a to β using wβ,
simpa using wβ,
end
lemma nat_abs_eq_nat_abs_iff {a b : β€} : a.nat_abs = b.nat_abs β a = b β¨ a = -b :=
begin
split; intro h,
{ cases int.nat_abs_eq a with hβ hβ; cases int.nat_abs_eq b with hβ hβ;
rw [hβ, hβ]; simp [h], },
{ cases h; rw h, rw int.nat_abs_neg, },
end
lemma nat_abs_eq_iff {a : β€} {n : β} : a.nat_abs = n β a = n β¨ a = -n :=
by rw [βint.nat_abs_eq_nat_abs_iff, int.nat_abs_of_nat]
lemma nat_abs_eq_iff_mul_self_eq {a b : β€} : a.nat_abs = b.nat_abs β a * a = b * b :=
begin
rw [β abs_eq_iff_mul_self_eq, abs_eq_nat_abs, abs_eq_nat_abs],
exact int.coe_nat_inj'.symm
end
lemma nat_abs_lt_iff_mul_self_lt {a b : β€} : a.nat_abs < b.nat_abs β a * a < b * b :=
begin
rw [β abs_lt_iff_mul_self_lt, abs_eq_nat_abs, abs_eq_nat_abs],
exact int.coe_nat_lt.symm
end
lemma nat_abs_le_iff_mul_self_le {a b : β€} : a.nat_abs β€ b.nat_abs β a * a β€ b * b :=
begin
rw [β abs_le_iff_mul_self_le, abs_eq_nat_abs, abs_eq_nat_abs],
exact int.coe_nat_le.symm
end
lemma nat_abs_eq_iff_sq_eq {a b : β€} : a.nat_abs = b.nat_abs β a ^ 2 = b ^ 2 :=
by { rw [sq, sq], exact nat_abs_eq_iff_mul_self_eq }
lemma nat_abs_lt_iff_sq_lt {a b : β€} : a.nat_abs < b.nat_abs β a ^ 2 < b ^ 2 :=
by { rw [sq, sq], exact nat_abs_lt_iff_mul_self_lt }
lemma nat_abs_le_iff_sq_le {a b : β€} : a.nat_abs β€ b.nat_abs β a ^ 2 β€ b ^ 2 :=
by { rw [sq, sq], exact nat_abs_le_iff_mul_self_le }
/-! ### `/` -/
@[simp] theorem of_nat_div (m n : β) : of_nat (m / n) = (of_nat m) / (of_nat n) := rfl
@[simp, norm_cast] theorem coe_nat_div (m n : β) : ((m / n : β) : β€) = m / n := rfl
theorem neg_succ_of_nat_div (m : β) {b : β€} (H : 0 < b) :
-[1+m] / b = -(m / b + 1) :=
match b, eq_succ_of_zero_lt H with ._, β¨n, rflβ© := rfl end
-- Will be generalized to Euclidean domains.
local attribute [simp]
protected theorem zero_div : β (b : β€), 0 / b = 0
| 0 := show of_nat _ = _, by simp
| (n+1:β) := show of_nat _ = _, by simp
| -[1+ n] := show -of_nat _ = _, by simp
local attribute [simp] -- Will be generalized to Euclidean domains.
protected theorem div_zero : β (a : β€), a / 0 = 0
| 0 := show of_nat _ = _, by simp
| (n+1:β) := show of_nat _ = _, by simp
| -[1+ n] := rfl
@[simp] protected theorem div_neg : β (a b : β€), a / -b = -(a / b)
| (m : β) 0 := show of_nat (m / 0) = -(m / 0 : β), by rw nat.div_zero; refl
| (m : β) (n+1:β) := rfl
| 0 -[1+ n] := by simp
| (m+1:β) -[1+ n] := (neg_neg _).symm
| -[1+ m] 0 := rfl
| -[1+ m] (n+1:β) := rfl
| -[1+ m] -[1+ n] := rfl
theorem div_of_neg_of_pos {a b : β€} (Ha : a < 0) (Hb : 0 < b) : a / b = -((-a - 1) / b + 1) :=
match a, b, eq_neg_succ_of_lt_zero Ha, eq_succ_of_zero_lt Hb with
| ._, ._, β¨m, rflβ©, β¨n, rflβ© :=
by change (- -[1+ m] : β€) with (m+1 : β€); rw add_sub_cancel; refl
end
protected theorem div_nonneg {a b : β€} (Ha : 0 β€ a) (Hb : 0 β€ b) : 0 β€ a / b :=
match a, b, eq_coe_of_zero_le Ha, eq_coe_of_zero_le Hb with
| ._, ._, β¨m, rflβ©, β¨n, rflβ© := coe_zero_le _
end
protected theorem div_nonpos {a b : β€} (Ha : 0 β€ a) (Hb : b β€ 0) : a / b β€ 0 :=
nonpos_of_neg_nonneg $ by rw [β int.div_neg]; exact int.div_nonneg Ha (neg_nonneg_of_nonpos Hb)
theorem div_neg' {a b : β€} (Ha : a < 0) (Hb : 0 < b) : a / b < 0 :=
match a, b, eq_neg_succ_of_lt_zero Ha, eq_succ_of_zero_lt Hb with
| ._, ._, β¨m, rflβ©, β¨n, rflβ© := neg_succ_lt_zero _
end
@[simp] protected theorem div_one : β (a : β€), a / 1 = a
| 0 := show of_nat _ = _, by simp
| (n+1:β) := congr_arg of_nat (nat.div_one _)
| -[1+ n] := congr_arg neg_succ_of_nat (nat.div_one _)
theorem div_eq_zero_of_lt {a b : β€} (H1 : 0 β€ a) (H2 : a < b) : a / b = 0 :=
match a, b, eq_coe_of_zero_le H1, eq_succ_of_zero_lt (lt_of_le_of_lt H1 H2), H2 with
| ._, ._, β¨m, rflβ©, β¨n, rflβ©, H2 :=
congr_arg of_nat $ nat.div_eq_of_lt $ lt_of_coe_nat_lt_coe_nat H2
end
theorem div_eq_zero_of_lt_abs {a b : β€} (H1 : 0 β€ a) (H2 : a < abs b) : a / b = 0 :=
match b, abs b, abs_eq_nat_abs b, H2 with
| (n : β), ._, rfl, H2 := div_eq_zero_of_lt H1 H2
| -[1+ n], ._, rfl, H2 := neg_injective $ by rw [β int.div_neg]; exact div_eq_zero_of_lt H1 H2
end
protected theorem add_mul_div_right (a b : β€) {c : β€} (H : c β 0) :
(a + b * c) / c = a / c + b :=
have β {k n : β} {a : β€}, (a + n * k.succ) / k.succ = a / k.succ + n, from
Ξ» k n a, match a with
| (m : β) := congr_arg of_nat $ nat.add_mul_div_right _ _ k.succ_pos
| -[1+ m] := show ((n * k.succ:β) - m.succ : β€) / k.succ =
n - (m / k.succ + 1 : β), begin
cases lt_or_ge m (n*k.succ) with h h,
{ rw [β int.coe_nat_sub h,
β int.coe_nat_sub ((nat.div_lt_iff_lt_mul _ _ k.succ_pos).2 h)],
apply congr_arg of_nat,
rw [mul_comm, nat.mul_sub_div], rwa mul_comm },
{ change (β(n * nat.succ k) - (m + 1) : β€) / β(nat.succ k) =
βn - ((m / nat.succ k : β) + 1),
rw [β sub_sub, β sub_sub, β neg_sub (m:β€), β neg_sub _ (n:β€),
β int.coe_nat_sub h,
β int.coe_nat_sub ((nat.le_div_iff_mul_le _ _ k.succ_pos).2 h),
β neg_succ_of_nat_coe', β neg_succ_of_nat_coe'],
{ apply congr_arg neg_succ_of_nat,
rw [mul_comm, nat.sub_mul_div], rwa mul_comm } }
end
end,
have β {a b c : β€}, 0 < c β (a + b * c) / c = a / c + b, from
Ξ» a b c H, match c, eq_succ_of_zero_lt H, b with
| ._, β¨k, rflβ©, (n : β) := this
| ._, β¨k, rflβ©, -[1+ n] :=
show (a - n.succ * k.succ) / k.succ = (a / k.succ) - n.succ, from
eq_sub_of_add_eq $ by rw [β this, sub_add_cancel]
end,
match lt_trichotomy c 0 with
| or.inl hlt := neg_inj.1 $ by rw [β int.div_neg, neg_add, β int.div_neg, β neg_mul_neg];
apply this (neg_pos_of_neg hlt)
| or.inr (or.inl heq) := absurd heq H
| or.inr (or.inr hgt) := this hgt
end
protected theorem add_mul_div_left (a : β€) {b : β€} (c : β€) (H : b β 0) :
(a + b * c) / b = a / b + c :=
by rw [mul_comm, int.add_mul_div_right _ _ H]
protected theorem add_div_of_dvd_right {a b c : β€} (H : c β£ b) :
(a + b) / c = a / c + b / c :=
begin
by_cases h1 : c = 0,
{ simp [h1] },
cases H with k hk,
rw hk,
change c β 0 at h1,
rw [mul_comm c k, int.add_mul_div_right _ _ h1, βzero_add (k * c), int.add_mul_div_right _ _ h1,
int.zero_div, zero_add]
end
protected theorem add_div_of_dvd_left {a b c : β€} (H : c β£ a) :
(a + b) / c = a / c + b / c :=
by rw [add_comm, int.add_div_of_dvd_right H, add_comm]
@[simp] protected theorem mul_div_cancel (a : β€) {b : β€} (H : b β 0) : a * b / b = a :=
by have := int.add_mul_div_right 0 a H;
rwa [zero_add, int.zero_div, zero_add] at this
@[simp] protected theorem mul_div_cancel_left {a : β€} (b : β€) (H : a β 0) : a * b / a = b :=
by rw [mul_comm, int.mul_div_cancel _ H]
@[simp] protected theorem div_self {a : β€} (H : a β 0) : a / a = 1 :=
by have := int.mul_div_cancel 1 H; rwa one_mul at this
/-! ### mod -/
theorem of_nat_mod (m n : nat) : (m % n : β€) = of_nat (m % n) := rfl
@[simp, norm_cast] theorem coe_nat_mod (m n : β) : (β(m % n) : β€) = βm % βn := rfl
theorem neg_succ_of_nat_mod (m : β) {b : β€} (bpos : 0 < b) :
-[1+m] % b = b - 1 - m % b :=
by rw [sub_sub, add_comm]; exact
match b, eq_succ_of_zero_lt bpos with ._, β¨n, rflβ© := rfl end
@[simp] theorem mod_neg : β (a b : β€), a % -b = a % b
| (m : β) n := @congr_arg β β€ _ _ (Ξ» i, β(m % i)) (nat_abs_neg _)
| -[1+ m] n := @congr_arg β β€ _ _ (Ξ» i, sub_nat_nat i (nat.succ (m % i))) (nat_abs_neg _)
@[simp] theorem mod_abs (a b : β€) : a % (abs b) = a % b :=
abs_by_cases (Ξ» i, a % i = a % b) rfl (mod_neg _ _)
local attribute [simp] -- Will be generalized to Euclidean domains.
theorem zero_mod (b : β€) : 0 % b = 0 := rfl
local attribute [simp] -- Will be generalized to Euclidean domains.
theorem mod_zero : β (a : β€), a % 0 = a
| (m : β) := congr_arg of_nat $ nat.mod_zero _
| -[1+ m] := congr_arg neg_succ_of_nat $ nat.mod_zero _
local attribute [simp] -- Will be generalized to Euclidean domains.
theorem mod_one : β (a : β€), a % 1 = 0
| (m : β) := congr_arg of_nat $ nat.mod_one _
| -[1+ m] := show (1 - (m % 1).succ : β€) = 0, by rw nat.mod_one; refl
theorem mod_eq_of_lt {a b : β€} (H1 : 0 β€ a) (H2 : a < b) : a % b = a :=
match a, b, eq_coe_of_zero_le H1, eq_coe_of_zero_le (le_trans H1 (le_of_lt H2)), H2 with
| ._, ._, β¨m, rflβ©, β¨n, rflβ©, H2 :=
congr_arg of_nat $ nat.mod_eq_of_lt (lt_of_coe_nat_lt_coe_nat H2)
end
theorem mod_nonneg : β (a : β€) {b : β€}, b β 0 β 0 β€ a % b
| (m : β) n H := coe_zero_le _
| -[1+ m] n H :=
sub_nonneg_of_le $ coe_nat_le_coe_nat_of_le $ nat.mod_lt _ (nat_abs_pos_of_ne_zero H)
theorem mod_lt_of_pos (a : β€) {b : β€} (H : 0 < b) : a % b < b :=
match a, b, eq_succ_of_zero_lt H with
| (m : β), ._, β¨n, rflβ© := coe_nat_lt_coe_nat_of_lt (nat.mod_lt _ (nat.succ_pos _))
| -[1+ m], ._, β¨n, rflβ© := sub_lt_self _ (coe_nat_lt_coe_nat_of_lt $ nat.succ_pos _)
end
theorem mod_lt (a : β€) {b : β€} (H : b β 0) : a % b < abs b :=
by rw [β mod_abs]; exact mod_lt_of_pos _ (abs_pos.2 H)
theorem mod_add_div_aux (m n : β) : (n - (m % n + 1) - (n * (m / n) + n) : β€) = -[1+ m] :=
begin
rw [β sub_sub, neg_succ_of_nat_coe, sub_sub (n:β€)],
apply eq_neg_of_eq_neg,
rw [neg_sub, sub_sub_self, add_right_comm],
exact @congr_arg β β€ _ _ (Ξ»i, (i + 1 : β€)) (nat.mod_add_div _ _).symm
end
theorem mod_add_div : β (a b : β€), a % b + b * (a / b) = a
| (m : β) 0 := congr_arg of_nat (nat.mod_add_div _ _)
| (m : β) (n+1:β) := congr_arg of_nat (nat.mod_add_div _ _)
| 0 -[1+ n] := by simp
| (m+1:β) -[1+ n] := show (_ + -(n+1) * -((m + 1) / (n + 1) : β) : β€) = _,
by rw [neg_mul_neg]; exact congr_arg of_nat (nat.mod_add_div _ _)
| -[1+ m] 0 := by rw [mod_zero, int.div_zero]; refl
| -[1+ m] (n+1:β) := mod_add_div_aux m n.succ
| -[1+ m] -[1+ n] := mod_add_div_aux m n.succ
theorem div_add_mod (a b : β€) : b * (a / b) + a % b = a :=
(add_comm _ _).trans (mod_add_div _ _)
lemma mod_add_div' (m k : β€) : m % k + (m / k) * k = m :=
by { rw mul_comm, exact mod_add_div _ _ }
lemma div_add_mod' (m k : β€) : (m / k) * k + m % k = m :=
by { rw mul_comm, exact div_add_mod _ _ }
theorem mod_def (a b : β€) : a % b = a - b * (a / b) :=
eq_sub_of_add_eq (mod_add_div _ _)
@[simp] theorem add_mul_mod_self {a b c : β€} : (a + b * c) % c = a % c :=
if cz : c = 0 then by rw [cz, mul_zero, add_zero] else
by rw [mod_def, mod_def, int.add_mul_div_right _ _ cz,
mul_add, mul_comm, add_sub_add_right_eq_sub]
@[simp] theorem add_mul_mod_self_left (a b c : β€) : (a + b * c) % b = a % b :=
by rw [mul_comm, add_mul_mod_self]
@[simp] theorem add_mod_self {a b : β€} : (a + b) % b = a % b :=
by have := add_mul_mod_self_left a b 1; rwa mul_one at this
@[simp] theorem add_mod_self_left {a b : β€} : (a + b) % a = b % a :=
by rw [add_comm, add_mod_self]
@[simp] theorem mod_add_mod (m n k : β€) : (m % n + k) % n = (m + k) % n :=
by have := (add_mul_mod_self_left (m % n + k) n (m / n)).symm;
rwa [add_right_comm, mod_add_div] at this
@[simp] theorem add_mod_mod (m n k : β€) : (m + n % k) % k = (m + n) % k :=
by rw [add_comm, mod_add_mod, add_comm]
lemma add_mod (a b n : β€) : (a + b) % n = ((a % n) + (b % n)) % n :=
by rw [add_mod_mod, mod_add_mod]
theorem add_mod_eq_add_mod_right {m n k : β€} (i : β€) (H : m % n = k % n) :
(m + i) % n = (k + i) % n :=
by rw [β mod_add_mod, β mod_add_mod k, H]
theorem add_mod_eq_add_mod_left {m n k : β€} (i : β€) (H : m % n = k % n) :
(i + m) % n = (i + k) % n :=
by rw [add_comm, add_mod_eq_add_mod_right _ H, add_comm]
theorem mod_add_cancel_right {m n k : β€} (i) : (m + i) % n = (k + i) % n β
m % n = k % n :=
β¨Ξ» H, by have := add_mod_eq_add_mod_right (-i) H;
rwa [add_neg_cancel_right, add_neg_cancel_right] at this,
add_mod_eq_add_mod_right _β©
theorem mod_add_cancel_left {m n k i : β€} :
(i + m) % n = (i + k) % n β m % n = k % n :=
by rw [add_comm, add_comm i, mod_add_cancel_right]
theorem mod_sub_cancel_right {m n k : β€} (i) : (m - i) % n = (k - i) % n β
m % n = k % n :=
mod_add_cancel_right _
theorem mod_eq_mod_iff_mod_sub_eq_zero {m n k : β€} : m % n = k % n β (m - k) % n = 0 :=
(mod_sub_cancel_right k).symm.trans $ by simp
@[simp] theorem mul_mod_left (a b : β€) : (a * b) % b = 0 :=
by rw [β zero_add (a * b), add_mul_mod_self, zero_mod]
@[simp] theorem mul_mod_right (a b : β€) : (a * b) % a = 0 :=
by rw [mul_comm, mul_mod_left]
lemma mul_mod (a b n : β€) : (a * b) % n = ((a % n) * (b % n)) % n :=
begin
conv_lhs {
rw [βmod_add_div a n, βmod_add_div' b n, right_distrib, left_distrib, left_distrib,
mul_assoc, mul_assoc, βleft_distrib n _ _, add_mul_mod_self_left, β mul_assoc,
add_mul_mod_self] }
end
@[simp] lemma neg_mod_two (i : β€) : (-i) % 2 = i % 2 :=
begin
apply int.mod_eq_mod_iff_mod_sub_eq_zero.mpr,
convert int.mul_mod_right 2 (-i),
simp only [two_mul, sub_eq_add_neg]
end
local attribute [simp] -- Will be generalized to Euclidean domains.
theorem mod_self {a : β€} : a % a = 0 :=
by have := mul_mod_left 1 a; rwa one_mul at this
@[simp] theorem mod_mod_of_dvd (n : β€) {m k : β€} (h : m β£ k) : n % k % m = n % m :=
begin
conv { to_rhs, rw βmod_add_div n k },
rcases h with β¨t, rflβ©, rw [mul_assoc, add_mul_mod_self_left]
end
@[simp] theorem mod_mod (a b : β€) : a % b % b = a % b :=
by conv {to_rhs, rw [β mod_add_div a b, add_mul_mod_self_left]}
lemma sub_mod (a b n : β€) : (a - b) % n = ((a % n) - (b % n)) % n :=
begin
apply (mod_add_cancel_right b).mp,
rw [sub_add_cancel, β add_mod_mod, sub_add_cancel, mod_mod]
end
/-! ### properties of `/` and `%` -/
@[simp] theorem mul_div_mul_of_pos {a : β€} (b c : β€) (H : 0 < a) : a * b / (a * c) = b / c :=
suffices β (m k : β) (b : β€), (m.succ * b / (m.succ * k) : β€) = b / k, from
match a, eq_succ_of_zero_lt H, c, eq_coe_or_neg c with
| ._, β¨m, rflβ©, ._, β¨k, or.inl rflβ© := this _ _ _
| ._, β¨m, rflβ©, ._, β¨k, or.inr rflβ© :=
by rw [β neg_mul_eq_mul_neg, int.div_neg, int.div_neg];
apply congr_arg has_neg.neg; apply this
end,
Ξ» m k b, match b, k with
| (n : β), k := congr_arg of_nat (nat.mul_div_mul _ _ m.succ_pos)
| -[1+ n], 0 := by rw [int.coe_nat_zero, mul_zero, int.div_zero, int.div_zero]
| -[1+ n], k+1 := congr_arg neg_succ_of_nat $
show (m.succ * n + m) / (m.succ * k.succ) = n / k.succ, begin
apply nat.div_eq_of_lt_le,
{ refine le_trans _ (nat.le_add_right _ _),
rw [β nat.mul_div_mul _ _ m.succ_pos],
apply nat.div_mul_le_self },
{ change m.succ * n.succ β€ _,
rw [mul_left_comm],
apply nat.mul_le_mul_left,
apply (nat.div_lt_iff_lt_mul _ _ k.succ_pos).1,
apply nat.lt_succ_self }
end
end
@[simp] theorem mul_div_mul_of_pos_left (a : β€) {b : β€} (H : 0 < b) (c : β€) :
a * b / (c * b) = a / c :=
by rw [mul_comm, mul_comm c, mul_div_mul_of_pos _ _ H]
@[simp] theorem mul_mod_mul_of_pos {a : β€} (H : 0 < a) (b c : β€) : a * b % (a * c) = a * (b % c) :=
by rw [mod_def, mod_def, mul_div_mul_of_pos _ _ H, mul_sub_left_distrib, mul_assoc]
theorem lt_div_add_one_mul_self (a : β€) {b : β€} (H : 0 < b) : a < (a / b + 1) * b :=
by { rw [add_mul, one_mul, mul_comm, β sub_lt_iff_lt_add', β mod_def],
exact mod_lt_of_pos _ H }
theorem abs_div_le_abs : β (a b : β€), abs (a / b) β€ abs a :=
suffices β (a : β€) (n : β), abs (a / n) β€ abs a, from
Ξ» a b, match b, eq_coe_or_neg b with
| ._, β¨n, or.inl rflβ© := this _ _
| ._, β¨n, or.inr rflβ© := by rw [int.div_neg, abs_neg]; apply this
end,
Ξ» a n, by rw [abs_eq_nat_abs, abs_eq_nat_abs]; exact
coe_nat_le_coe_nat_of_le (match a, n with
| (m : β), n := nat.div_le_self _ _
| -[1+ m], 0 := nat.zero_le _
| -[1+ m], n+1 := nat.succ_le_succ (nat.div_le_self _ _)
end)
theorem div_le_self {a : β€} (b : β€) (Ha : 0 β€ a) : a / b β€ a :=
by have := le_trans (le_abs_self _) (abs_div_le_abs a b);
rwa [abs_of_nonneg Ha] at this
theorem mul_div_cancel_of_mod_eq_zero {a b : β€} (H : a % b = 0) : b * (a / b) = a :=
by have := mod_add_div a b; rwa [H, zero_add] at this
theorem div_mul_cancel_of_mod_eq_zero {a b : β€} (H : a % b = 0) : a / b * b = a :=
by rw [mul_comm, mul_div_cancel_of_mod_eq_zero H]
lemma mod_two_eq_zero_or_one (n : β€) : n % 2 = 0 β¨ n % 2 = 1 :=
have h : n % 2 < 2 := abs_of_nonneg (show 0 β€ (2 : β€), from dec_trivial) βΈ int.mod_lt _ dec_trivial,
have hβ : 0 β€ n % 2 := int.mod_nonneg _ dec_trivial,
match (n % 2), h, hβ with
| (0 : β) := Ξ» _ _, or.inl rfl
| (1 : β) := Ξ» _ _, or.inr rfl
| (k + 2 : β) := Ξ» h _, absurd h dec_trivial
| -[1+ a] := Ξ» _ hβ, absurd hβ dec_trivial
end
/-! ### dvd -/
@[norm_cast] theorem coe_nat_dvd {m n : β} : (βm : β€) β£ βn β m β£ n :=
β¨Ξ» β¨a, aeβ©, m.eq_zero_or_pos.elim
(Ξ»m0, by simp [m0] at ae; simp [ae, m0])
(Ξ»m0l, by {
cases eq_coe_of_zero_le (@nonneg_of_mul_nonneg_left β€ _ m a
(by simp [ae.symm]) (by simpa using m0l)) with k e,
subst a, exact β¨k, int.coe_nat_inj aeβ© }),
Ξ» β¨k, eβ©, dvd.intro k $ by rw [e, int.coe_nat_mul]β©
theorem coe_nat_dvd_left {n : β} {z : β€} : (βn : β€) β£ z β n β£ z.nat_abs :=
by rcases nat_abs_eq z with eq | eq; rw eq; simp [coe_nat_dvd]
theorem coe_nat_dvd_right {n : β} {z : β€} : z β£ (βn : β€) β z.nat_abs β£ n :=
by rcases nat_abs_eq z with eq | eq; rw eq; simp [coe_nat_dvd]
theorem dvd_antisymm {a b : β€} (H1 : 0 β€ a) (H2 : 0 β€ b) : a β£ b β b β£ a β a = b :=
begin
rw [β abs_of_nonneg H1, β abs_of_nonneg H2, abs_eq_nat_abs, abs_eq_nat_abs],
rw [coe_nat_dvd, coe_nat_dvd, coe_nat_inj'],
apply nat.dvd_antisymm
end
theorem dvd_of_mod_eq_zero {a b : β€} (H : b % a = 0) : a β£ b :=
β¨b / a, (mul_div_cancel_of_mod_eq_zero H).symmβ©
theorem mod_eq_zero_of_dvd : β {a b : β€}, a β£ b β b % a = 0
| a ._ β¨c, rflβ© := mul_mod_right _ _
theorem dvd_iff_mod_eq_zero (a b : β€) : a β£ b β b % a = 0 :=
β¨mod_eq_zero_of_dvd, dvd_of_mod_eq_zeroβ©
/-- If `a % b = c` then `b` divides `a - c`. -/
lemma dvd_sub_of_mod_eq {a b c : β€} (h : a % b = c) : b β£ a - c :=
begin
have hx : a % b % b = c % b, { rw h },
rw [mod_mod, βmod_sub_cancel_right c, sub_self, zero_mod] at hx,
exact dvd_of_mod_eq_zero hx
end
theorem nat_abs_dvd {a b : β€} : (a.nat_abs : β€) β£ b β a β£ b :=
(nat_abs_eq a).elim (Ξ» e, by rw β e) (Ξ» e, by rw [β neg_dvd, β e])
theorem dvd_nat_abs {a b : β€} : a β£ b.nat_abs β a β£ b :=
(nat_abs_eq b).elim (Ξ» e, by rw β e) (Ξ» e, by rw [β dvd_neg, β e])
instance decidable_dvd : @decidable_rel β€ (β£) :=
assume a n, decidable_of_decidable_of_iff (by apply_instance) (dvd_iff_mod_eq_zero _ _).symm
protected theorem div_mul_cancel {a b : β€} (H : b β£ a) : a / b * b = a :=
div_mul_cancel_of_mod_eq_zero (mod_eq_zero_of_dvd H)
protected theorem mul_div_cancel' {a b : β€} (H : a β£ b) : a * (b / a) = b :=
by rw [mul_comm, int.div_mul_cancel H]
protected theorem mul_div_assoc (a : β€) : β {b c : β€}, c β£ b β (a * b) / c = a * (b / c)
| ._ c β¨d, rflβ© := if cz : c = 0 then by simp [cz] else
by rw [mul_left_comm, int.mul_div_cancel_left _ cz, int.mul_div_cancel_left _ cz]
protected theorem mul_div_assoc' (b : β€) {a c : β€} (h : c β£ a) : a * b / c = a / c * b :=
by rw [mul_comm, int.mul_div_assoc _ h, mul_comm]
theorem div_dvd_div : β {a b c : β€} (H1 : a β£ b) (H2 : b β£ c), b / a β£ c / a
| a ._ ._ β¨b, rflβ© β¨c, rflβ© := if az : a = 0 then by simp [az] else
by rw [int.mul_div_cancel_left _ az, mul_assoc, int.mul_div_cancel_left _ az];
apply dvd_mul_right
protected theorem eq_mul_of_div_eq_right {a b c : β€} (H1 : b β£ a) (H2 : a / b = c) :
a = b * c :=
by rw [β H2, int.mul_div_cancel' H1]
protected theorem div_eq_of_eq_mul_right {a b c : β€} (H1 : b β 0) (H2 : a = b * c) :
a / b = c :=
by rw [H2, int.mul_div_cancel_left _ H1]
protected theorem eq_div_of_mul_eq_right {a b c : β€} (H1 : a β 0) (H2 : a * b = c) :
b = c / a :=
eq.symm $ int.div_eq_of_eq_mul_right H1 H2.symm
protected theorem div_eq_iff_eq_mul_right {a b c : β€} (H : b β 0) (H' : b β£ a) :
a / b = c β a = b * c :=
β¨int.eq_mul_of_div_eq_right H', int.div_eq_of_eq_mul_right Hβ©
protected theorem div_eq_iff_eq_mul_left {a b c : β€} (H : b β 0) (H' : b β£ a) :
a / b = c β a = c * b :=
by rw mul_comm; exact int.div_eq_iff_eq_mul_right H H'
protected theorem eq_mul_of_div_eq_left {a b c : β€} (H1 : b β£ a) (H2 : a / b = c) :
a = c * b :=
by rw [mul_comm, int.eq_mul_of_div_eq_right H1 H2]
protected theorem div_eq_of_eq_mul_left {a b c : β€} (H1 : b β 0) (H2 : a = c * b) :
a / b = c :=
int.div_eq_of_eq_mul_right H1 (by rw [mul_comm, H2])
protected lemma eq_zero_of_div_eq_zero {d n : β€} (h : d β£ n) (H : n / d = 0) : n = 0 :=
by rw [β int.mul_div_cancel' h, H, mul_zero]
theorem neg_div_of_dvd : β {a b : β€} (H : b β£ a), -a / b = -(a / b)
| ._ b β¨c, rflβ© := if bz : b = 0 then by simp [bz] else
by rw [neg_mul_eq_mul_neg, int.mul_div_cancel_left _ bz, int.mul_div_cancel_left _ bz]
lemma sub_div_of_dvd (a : β€) {b c : β€} (hcb : c β£ b) : (a - b) / c = a / c - b / c :=
begin
rw [sub_eq_add_neg, sub_eq_add_neg, int.add_div_of_dvd_right ((dvd_neg c b).mpr hcb)],
congr,
exact neg_div_of_dvd hcb,
end
lemma sub_div_of_dvd_sub {a b c : β€} (hcab : c β£ (a - b)) : (a - b) / c = a / c - b / c :=
by rw [eq_sub_iff_add_eq, β int.add_div_of_dvd_left hcab, sub_add_cancel]
theorem div_sign : β a b, a / sign b = a * sign b
| a (n+1:β) := by unfold sign; simp
| a 0 := by simp [sign]
| a -[1+ n] := by simp [sign]
@[simp] theorem sign_mul : β a b, sign (a * b) = sign a * sign b
| a 0 := by simp
| 0 b := by simp
| (m+1:β) (n+1:β) := rfl
| (m+1:β) -[1+ n] := rfl
| -[1+ m] (n+1:β) := rfl
| -[1+ m] -[1+ n] := rfl
protected theorem sign_eq_div_abs (a : β€) : sign a = a / (abs a) :=
if az : a = 0 then by simp [az] else
(int.div_eq_of_eq_mul_left (mt abs_eq_zero.1 az)
(sign_mul_abs _).symm).symm
theorem mul_sign : β (i : β€), i * sign i = nat_abs i
| (n+1:β) := mul_one _
| 0 := mul_zero _
| -[1+ n] := mul_neg_one _
@[simp]
theorem sign_pow_bit1 (k : β) : β n : β€, n.sign ^ (bit1 k) = n.sign
| (n+1:β) := one_pow (bit1 k)
| 0 := zero_pow (nat.zero_lt_bit1 k)
| -[1+ n] := (neg_pow_bit1 1 k).trans (congr_arg (Ξ» x, -x) (one_pow (bit1 k)))
theorem le_of_dvd {a b : β€} (bpos : 0 < b) (H : a β£ b) : a β€ b :=
match a, b, eq_succ_of_zero_lt bpos, H with
| (m : β), ._, β¨n, rflβ©, H := coe_nat_le_coe_nat_of_le $
nat.le_of_dvd n.succ_pos $ coe_nat_dvd.1 H
| -[1+ m], ._, β¨n, rflβ©, _ :=
le_trans (le_of_lt $ neg_succ_lt_zero _) (coe_zero_le _)
end
theorem eq_one_of_dvd_one {a : β€} (H : 0 β€ a) (H' : a β£ 1) : a = 1 :=
match a, eq_coe_of_zero_le H, H' with
| ._, β¨n, rflβ©, H' := congr_arg coe $
nat.eq_one_of_dvd_one $ coe_nat_dvd.1 H'
end
theorem eq_one_of_mul_eq_one_right {a b : β€} (H : 0 β€ a) (H' : a * b = 1) : a = 1 :=
eq_one_of_dvd_one H β¨b, H'.symmβ©
theorem eq_one_of_mul_eq_one_left {a b : β€} (H : 0 β€ b) (H' : a * b = 1) : b = 1 :=
eq_one_of_mul_eq_one_right H (by rw [mul_comm, H'])
lemma of_nat_dvd_of_dvd_nat_abs {a : β} : β {z : β€} (haz : a β£ z.nat_abs), βa β£ z
| (int.of_nat _) haz := int.coe_nat_dvd.2 haz
| -[1+k] haz :=
begin
change βa β£ -(k+1 : β€),
apply dvd_neg_of_dvd,
apply int.coe_nat_dvd.2,
exact haz
end
lemma dvd_nat_abs_of_of_nat_dvd {a : β} : β {z : β€} (haz : βa β£ z), a β£ z.nat_abs
| (int.of_nat _) haz := int.coe_nat_dvd.1 (int.dvd_nat_abs.2 haz)
| -[1+k] haz :=
have haz' : (βa:β€) β£ (β(k+1):β€), from dvd_of_dvd_neg haz,
int.coe_nat_dvd.1 haz'
lemma pow_dvd_of_le_of_pow_dvd {p m n : β} {k : β€} (hmn : m β€ n) (hdiv : β(p ^ n) β£ k) :
β(p ^ m) β£ k :=
begin
induction k,
{ apply int.coe_nat_dvd.2,
apply pow_dvd_of_le_of_pow_dvd hmn,
apply int.coe_nat_dvd.1 hdiv },
change -[1+k] with -(β(k+1) : β€),
apply dvd_neg_of_dvd,
apply int.coe_nat_dvd.2,
apply pow_dvd_of_le_of_pow_dvd hmn,
apply int.coe_nat_dvd.1,
apply dvd_of_dvd_neg,
exact hdiv,
end
lemma dvd_of_pow_dvd {p k : β} {m : β€} (hk : 1 β€ k) (hpk : β(p^k) β£ m) : βp β£ m :=
by rw βpow_one p; exact pow_dvd_of_le_of_pow_dvd hk hpk
/-- If `n > 0` then `m` is not divisible by `n` iff it is between `n * k` and `n * (k + 1)`
for some `k`. -/
lemma exists_lt_and_lt_iff_not_dvd (m : β€) {n : β€} (hn : 0 < n) :
(β k, n * k < m β§ m < n * (k + 1)) β Β¬ n β£ m :=
begin
split,
{ rintro β¨k, h1k, h2kβ© β¨l, rflβ©, rw [mul_lt_mul_left hn] at h1k h2k,
rw [lt_add_one_iff, β not_lt] at h2k, exact h2k h1k },
{ intro h, rw [dvd_iff_mod_eq_zero, β ne.def] at h,
have := (mod_nonneg m hn.ne.symm).lt_of_ne h.symm,
simp only [β mod_add_div m n] {single_pass := tt},
refine β¨m / n, lt_add_of_pos_left _ this, _β©,
rw [add_comm _ (1 : β€), left_distrib, mul_one], exact add_lt_add_right (mod_lt_of_pos _ hn) _ }
end
/-! ### `/` and ordering -/
protected theorem div_mul_le (a : β€) {b : β€} (H : b β 0) : a / b * b β€ a :=
le_of_sub_nonneg $ by rw [mul_comm, β mod_def]; apply mod_nonneg _ H
protected theorem div_le_of_le_mul {a b c : β€} (H : 0 < c) (H' : a β€ b * c) : a / c β€ b :=
le_of_mul_le_mul_right (le_trans (int.div_mul_le _ (ne_of_gt H)) H') H
protected theorem mul_lt_of_lt_div {a b c : β€} (H : 0 < c) (H3 : a < b / c) : a * c < b :=
lt_of_not_ge $ mt (int.div_le_of_le_mul H) (not_le_of_gt H3)
protected theorem mul_le_of_le_div {a b c : β€} (H1 : 0 < c) (H2 : a β€ b / c) : a * c β€ b :=
le_trans (decidable.mul_le_mul_of_nonneg_right H2 (le_of_lt H1)) (int.div_mul_le _ (ne_of_gt H1))
protected theorem le_div_of_mul_le {a b c : β€} (H1 : 0 < c) (H2 : a * c β€ b) : a β€ b / c :=
le_of_lt_add_one $ lt_of_mul_lt_mul_right
(lt_of_le_of_lt H2 (lt_div_add_one_mul_self _ H1)) (le_of_lt H1)
protected theorem le_div_iff_mul_le {a b c : β€} (H : 0 < c) : a β€ b / c β a * c β€ b :=
β¨int.mul_le_of_le_div H, int.le_div_of_mul_le Hβ©
protected theorem div_le_div {a b c : β€} (H : 0 < c) (H' : a β€ b) : a / c β€ b / c :=
int.le_div_of_mul_le H (le_trans (int.div_mul_le _ (ne_of_gt H)) H')
protected theorem div_lt_of_lt_mul {a b c : β€} (H : 0 < c) (H' : a < b * c) : a / c < b :=
lt_of_not_ge $ mt (int.mul_le_of_le_div H) (not_le_of_gt H')
protected theorem lt_mul_of_div_lt {a b c : β€} (H1 : 0 < c) (H2 : a / c < b) : a < b * c :=
lt_of_not_ge $ mt (int.le_div_of_mul_le H1) (not_le_of_gt H2)
protected theorem div_lt_iff_lt_mul {a b c : β€} (H : 0 < c) : a / c < b β a < b * c :=
β¨int.lt_mul_of_div_lt H, int.div_lt_of_lt_mul Hβ©
protected theorem le_mul_of_div_le {a b c : β€} (H1 : 0 β€ b) (H2 : b β£ a) (H3 : a / b β€ c) :
a β€ c * b :=
by rw [β int.div_mul_cancel H2]; exact decidable.mul_le_mul_of_nonneg_right H3 H1
protected theorem lt_div_of_mul_lt {a b c : β€} (H1 : 0 β€ b) (H2 : b β£ c) (H3 : a * b < c) :
a < c / b :=
lt_of_not_ge $ mt (int.le_mul_of_div_le H1 H2) (not_le_of_gt H3)
protected theorem lt_div_iff_mul_lt {a b : β€} (c : β€) (H : 0 < c) (H' : c β£ b) :
a < b / c β a * c < b :=
β¨int.mul_lt_of_lt_div H, int.lt_div_of_mul_lt (le_of_lt H) H'β©
theorem div_pos_of_pos_of_dvd {a b : β€} (H1 : 0 < a) (H2 : 0 β€ b) (H3 : b β£ a) : 0 < a / b :=
int.lt_div_of_mul_lt H2 H3 (by rwa zero_mul)
theorem div_eq_div_of_mul_eq_mul {a b c d : β€} (H2 : d β£ c) (H3 : b β 0)
(H4 : d β 0) (H5 : a * d = b * c) :
a / b = c / d :=
int.div_eq_of_eq_mul_right H3 $
by rw [β int.mul_div_assoc _ H2]; exact
(int.div_eq_of_eq_mul_left H4 H5.symm).symm
theorem eq_mul_div_of_mul_eq_mul_of_dvd_left {a b c d : β€} (hb : b β 0) (hbc : b β£ c)
(h : b * a = c * d) :
a = c / b * d :=
begin
cases hbc with k hk,
subst hk,
rw [int.mul_div_cancel_left _ hb],
rw mul_assoc at h,
apply mul_left_cancel' hb h
end
/-- If an integer with larger absolute value divides an integer, it is
zero. -/
lemma eq_zero_of_dvd_of_nat_abs_lt_nat_abs {a b : β€} (w : a β£ b) (h : nat_abs b < nat_abs a) :
b = 0 :=
begin
rw [βnat_abs_dvd, βdvd_nat_abs, coe_nat_dvd] at w,
rw βnat_abs_eq_zero,
exact eq_zero_of_dvd_of_lt w h
end
lemma eq_zero_of_dvd_of_nonneg_of_lt {a b : β€} (wβ : 0 β€ a) (wβ : a < b) (h : b β£ a) : a = 0 :=
eq_zero_of_dvd_of_nat_abs_lt_nat_abs h (nat_abs_lt_nat_abs_of_nonneg_of_lt wβ wβ)
/-- If two integers are congruent to a sufficiently large modulus,
they are equal. -/
lemma eq_of_mod_eq_of_nat_abs_sub_lt_nat_abs {a b c : β€} (h1 : a % b = c)
(h2 : nat_abs (a - c) < nat_abs b) :
a = c :=
eq_of_sub_eq_zero (eq_zero_of_dvd_of_nat_abs_lt_nat_abs (dvd_sub_of_mod_eq h1) h2)
theorem of_nat_add_neg_succ_of_nat_of_lt {m n : β} (h : m < n.succ) :
of_nat m + -[1+n] = -[1+ n - m] :=
begin
change sub_nat_nat _ _ = _,
have h' : n.succ - m = (n - m).succ,
apply succ_sub,
apply le_of_lt_succ h,
simp [*, sub_nat_nat]
end
theorem of_nat_add_neg_succ_of_nat_of_ge {m n : β}
(h : n.succ β€ m) : of_nat m + -[1+n] = of_nat (m - n.succ) :=
begin
change sub_nat_nat _ _ = _,
have h' : n.succ - m = 0,
apply sub_eq_zero_of_le h,
simp [*, sub_nat_nat]
end
@[simp] theorem neg_add_neg (m n : β) : -[1+m] + -[1+n] = -[1+nat.succ(m+n)] := rfl
/-! ### to_nat -/
theorem to_nat_eq_max : β (a : β€), (to_nat a : β€) = max a 0
| (n : β) := (max_eq_left (coe_zero_le n)).symm
| -[1+ n] := (max_eq_right (le_of_lt (neg_succ_lt_zero n))).symm
@[simp] lemma to_nat_zero : (0 : β€).to_nat = 0 := rfl
@[simp] lemma to_nat_one : (1 : β€).to_nat = 1 := rfl
@[simp] theorem to_nat_of_nonneg {a : β€} (h : 0 β€ a) : (to_nat a : β€) = a :=
by rw [to_nat_eq_max, max_eq_left h]
@[simp] lemma to_nat_sub_of_le {a b : β€} (h : b β€ a) : (to_nat (a - b) : β€) = a - b :=
int.to_nat_of_nonneg (sub_nonneg_of_le h)
@[simp] theorem to_nat_coe_nat (n : β) : to_nat βn = n := rfl
@[simp] lemma to_nat_coe_nat_add_one {n : β} : ((n : β€) + 1).to_nat = n + 1 := rfl
theorem le_to_nat (a : β€) : a β€ to_nat a :=
by rw [to_nat_eq_max]; apply le_max_left
@[simp] theorem to_nat_le {a : β€} {n : β} : to_nat a β€ n β a β€ n :=
by rw [(coe_nat_le_coe_nat_iff _ _).symm, to_nat_eq_max, max_le_iff];
exact and_iff_left (coe_zero_le _)
@[simp] theorem lt_to_nat {n : β} {a : β€} : n < to_nat a β (n : β€) < a :=
le_iff_le_iff_lt_iff_lt.1 to_nat_le
theorem to_nat_le_to_nat {a b : β€} (h : a β€ b) : to_nat a β€ to_nat b :=
by rw to_nat_le; exact le_trans h (le_to_nat b)
theorem to_nat_lt_to_nat {a b : β€} (hb : 0 < b) : to_nat a < to_nat b β a < b :=
β¨Ξ» h, begin cases a, exact lt_to_nat.1 h, exact lt_trans (neg_succ_of_nat_lt_zero a) hb, end,
Ξ» h, begin rw lt_to_nat, cases a, exact h, exact hb endβ©
theorem lt_of_to_nat_lt {a b : β€} (h : to_nat a < to_nat b) : a < b :=
(to_nat_lt_to_nat $ lt_to_nat.1 $ lt_of_le_of_lt (nat.zero_le _) h).1 h
lemma to_nat_add {a b : β€} (ha : 0 β€ a) (hb : 0 β€ b) :
(a + b).to_nat = a.to_nat + b.to_nat :=
begin
lift a to β using ha,
lift b to β using hb,
norm_cast,
end
lemma to_nat_add_nat {a : β€} (ha : 0 β€ a) (n : β) : (a + n).to_nat = a.to_nat + n :=
begin
lift a to β using ha,
norm_cast,
end
@[simp]
lemma pred_to_nat : β (i : β€), (i - 1).to_nat = i.to_nat - 1
| (0:β) := rfl
| (n+1:β) := by simp
| -[1+ n] := rfl
@[simp]
lemma to_nat_pred_coe_of_pos {i : β€} (h : 0 < i) : ((i.to_nat - 1 : β) : β€) = i - 1 :=
by simp [h, le_of_lt h] with push_cast
@[simp] lemma to_nat_sub_to_nat_neg : β (n : β€), βn.to_nat - β((-n).to_nat) = n
| (0 : β) := rfl
| (n+1 : β) := show β(n+1) - (0:β€) = n+1, from sub_zero _
| -[1+ n] := show 0 - (n+1 : β€) = _, from zero_sub _
@[simp] lemma to_nat_add_to_nat_neg_eq_nat_abs : β (n : β€), (n.to_nat) + ((-n).to_nat) = n.nat_abs
| (0 : β) := rfl
| (n+1 : β) := show (n+1) + 0 = n+1, from add_zero _
| -[1+ n] := show 0 + (n+1) = n+1, from zero_add _
/-- If `n : β`, then `int.to_nat' n = some n`, if `n : β€` is negative, then `int.to_nat' n = none`.
-/
def to_nat' : β€ β option β
| (n : β) := some n
| -[1+ n] := none
theorem mem_to_nat' : β (a : β€) (n : β), n β to_nat' a β a = n
| (m : β) n := option.some_inj.trans coe_nat_inj'.symm
| -[1+ m] n := by split; intro h; cases h
lemma to_nat_of_nonpos : β {z : β€}, z β€ 0 β z.to_nat = 0
| (0 : β) := Ξ» _, rfl
| (n + 1 : β) := Ξ» h, (h.not_lt (by { exact_mod_cast nat.succ_pos n })).elim
| (-[1+ n]) := Ξ» _, rfl
/-! ### units -/
@[simp] theorem units_nat_abs (u : units β€) : nat_abs u = 1 :=
units.ext_iff.1 $ nat.units_eq_one β¨nat_abs u, nat_abs βuβ»ΒΉ,
by rw [β nat_abs_mul, units.mul_inv]; refl,
by rw [β nat_abs_mul, units.inv_mul]; reflβ©
theorem units_eq_one_or (u : units β€) : u = 1 β¨ u = -1 :=
by simpa only [units.ext_iff, units_nat_abs] using nat_abs_eq u
lemma is_unit_eq_one_or {a : β€} : is_unit a β a = 1 β¨ a = -1
| β¨x, hxβ© := hx βΈ (units_eq_one_or _).imp (congr_arg coe) (congr_arg coe)
lemma is_unit_iff {a : β€} : is_unit a β a = 1 β¨ a = -1 :=
begin
refine β¨Ξ» h, is_unit_eq_one_or h, Ξ» h, _β©,
rcases h with rfl | rfl,
{ exact is_unit_one },
{ exact is_unit_one.neg }
end
theorem is_unit_iff_nat_abs_eq {n : β€} : is_unit n β n.nat_abs = 1 :=
by simp [nat_abs_eq_iff, is_unit_iff]
lemma units_inv_eq_self (u : units β€) : uβ»ΒΉ = u :=
(units_eq_one_or u).elim (Ξ» h, h.symm βΈ rfl) (Ξ» h, h.symm βΈ rfl)
@[simp] lemma units_mul_self (u : units β€) : u * u = 1 :=
(units_eq_one_or u).elim (Ξ» h, h.symm βΈ rfl) (Ξ» h, h.symm βΈ rfl)
-- `units.coe_mul` is a "wrong turn" for the simplifier, this undoes it and simplifies further
@[simp] lemma units_coe_mul_self (u : units β€) : (u * u : β€) = 1 :=
by rw [βunits.coe_mul, units_mul_self, units.coe_one]
@[simp] lemma neg_one_pow_ne_zero {n : β} : (-1 : β€)^n β 0 :=
pow_ne_zero _ (abs_pos.mp trivial)
/-! ### bitwise ops -/
@[simp] lemma bodd_zero : bodd 0 = ff := rfl
@[simp] lemma bodd_one : bodd 1 = tt := rfl
lemma bodd_two : bodd 2 = ff := rfl
@[simp, norm_cast] lemma bodd_coe (n : β) : int.bodd n = nat.bodd n := rfl
@[simp] lemma bodd_sub_nat_nat (m n : β) : bodd (sub_nat_nat m n) = bxor m.bodd n.bodd :=
by apply sub_nat_nat_elim m n (Ξ» m n i, bodd i = bxor m.bodd n.bodd); intros;
simp; cases i.bodd; simp
@[simp] lemma bodd_neg_of_nat (n : β) : bodd (neg_of_nat n) = n.bodd :=
by cases n; simp; refl
@[simp] lemma bodd_neg (n : β€) : bodd (-n) = bodd n :=
by cases n; simp [has_neg.neg, int.coe_nat_eq, int.neg, bodd, -of_nat_eq_coe]
@[simp] lemma bodd_add (m n : β€) : bodd (m + n) = bxor (bodd m) (bodd n) :=
by cases m with m m; cases n with n n; unfold has_add.add;
simp [int.add, -of_nat_eq_coe, bool.bxor_comm]
@[simp] lemma bodd_mul (m n : β€) : bodd (m * n) = bodd m && bodd n :=
by cases m with m m; cases n with n n;
simp [β int.mul_def, int.mul, -of_nat_eq_coe, bool.bxor_comm]
theorem bodd_add_div2 : β n, cond (bodd n) 1 0 + 2 * div2 n = n
| (n : β) :=
by rw [show (cond (bodd n) 1 0 : β€) = (cond (bodd n) 1 0 : β),
by cases bodd n; refl]; exact congr_arg of_nat n.bodd_add_div2
| -[1+ n] := begin
refine eq.trans _ (congr_arg neg_succ_of_nat n.bodd_add_div2),
dsimp [bodd], cases nat.bodd n; dsimp [cond, bnot, div2, int.mul],
{ change -[1+ 2 * nat.div2 n] = _, rw zero_add },
{ rw [zero_add, add_comm], refl }
end
theorem div2_val : β n, div2 n = n / 2
| (n : β) := congr_arg of_nat n.div2_val
| -[1+ n] := congr_arg neg_succ_of_nat n.div2_val
lemma bit0_val (n : β€) : bit0 n = 2 * n := (two_mul _).symm
lemma bit1_val (n : β€) : bit1 n = 2 * n + 1 := congr_arg (+(1:β€)) (bit0_val _)
lemma bit_val (b n) : bit b n = 2 * n + cond b 1 0 :=
by { cases b, apply (bit0_val n).trans (add_zero _).symm, apply bit1_val }
lemma bit_decomp (n : β€) : bit (bodd n) (div2 n) = n :=
(bit_val _ _).trans $ (add_comm _ _).trans $ bodd_add_div2 _
/-- Defines a function from `β€` conditionally, if it is defined for odd and even integers separately
using `bit`. -/
def {u} bit_cases_on {C : β€ β Sort u} (n) (h : β b n, C (bit b n)) : C n :=
by rw [β bit_decomp n]; apply h
@[simp] lemma bit_zero : bit ff 0 = 0 := rfl
@[simp] lemma bit_coe_nat (b) (n : β) : bit b n = nat.bit b n :=
by rw [bit_val, nat.bit_val]; cases b; refl
@[simp] lemma bit_neg_succ (b) (n : β) : bit b -[1+ n] = -[1+ nat.bit (bnot b) n] :=
by rw [bit_val, nat.bit_val]; cases b; refl
@[simp] lemma bodd_bit (b n) : bodd (bit b n) = b :=
by rw bit_val; simp; cases b; cases bodd n; refl
@[simp] lemma bodd_bit0 (n : β€) : bodd (bit0 n) = ff := bodd_bit ff n
@[simp] lemma bodd_bit1 (n : β€) : bodd (bit1 n) = tt := bodd_bit tt n
@[simp] lemma div2_bit (b n) : div2 (bit b n) = n :=
begin
rw [bit_val, div2_val, add_comm, int.add_mul_div_left, (_ : (_/2:β€) = 0), zero_add],
cases b,
{ simp },
{ show of_nat _ = _, rw nat.div_eq_zero; simp },
{ cc }
end
lemma bit0_ne_bit1 (m n : β€) : bit0 m β bit1 n :=
mt (congr_arg bodd) $ by simp
lemma bit1_ne_bit0 (m n : β€) : bit1 m β bit0 n :=
(bit0_ne_bit1 _ _).symm
lemma bit1_ne_zero (m : β€) : bit1 m β 0 :=
by simpa only [bit0_zero] using bit1_ne_bit0 m 0
@[simp] lemma test_bit_zero (b) : β n, test_bit (bit b n) 0 = b
| (n : β) := by rw [bit_coe_nat]; apply nat.test_bit_zero
| -[1+ n] := by rw [bit_neg_succ]; dsimp [test_bit]; rw [nat.test_bit_zero];
clear test_bit_zero; cases b; refl
@[simp] lemma test_bit_succ (m b) : β n, test_bit (bit b n) (nat.succ m) = test_bit n m
| (n : β) := by rw [bit_coe_nat]; apply nat.test_bit_succ
| -[1+ n] := by rw [bit_neg_succ]; dsimp [test_bit]; rw [nat.test_bit_succ]
private meta def bitwise_tac : tactic unit := `[
funext m,
funext n,
cases m with m m; cases n with n n; try {refl},
all_goals {
apply congr_arg of_nat <|> apply congr_arg neg_succ_of_nat,
try {dsimp [nat.land, nat.ldiff, nat.lor]},
try {rw [
show nat.bitwise (Ξ» a b, a && bnot b) n m =
nat.bitwise (Ξ» a b, b && bnot a) m n, from
congr_fun (congr_fun (@nat.bitwise_swap (Ξ» a b, b && bnot a) rfl) n) m]},
apply congr_arg (Ξ» f, nat.bitwise f m n),
funext a,
funext b,
cases a; cases b; refl
},
all_goals {unfold nat.land nat.ldiff nat.lor}
]
theorem bitwise_or : bitwise bor = lor := by bitwise_tac
theorem bitwise_and : bitwise band = land := by bitwise_tac
theorem bitwise_diff : bitwise (Ξ» a b, a && bnot b) = ldiff := by bitwise_tac
theorem bitwise_xor : bitwise bxor = lxor := by bitwise_tac
@[simp] lemma bitwise_bit (f : bool β bool β bool) (a m b n) :
bitwise f (bit a m) (bit b n) = bit (f a b) (bitwise f m n) :=
begin
cases m with m m; cases n with n n;
repeat { rw [β int.coe_nat_eq] <|> rw bit_coe_nat <|> rw bit_neg_succ };
unfold bitwise nat_bitwise bnot;
[ induction h : f ff ff,
induction h : f ff tt,
induction h : f tt ff,
induction h : f tt tt ],
all_goals {
unfold cond, rw nat.bitwise_bit,
repeat { rw bit_coe_nat <|> rw bit_neg_succ <|> rw bnot_bnot } },
all_goals { unfold bnot {fail_if_unchanged := ff}; rw h; refl }
end
@[simp] lemma lor_bit (a m b n) : lor (bit a m) (bit b n) = bit (a || b) (lor m n) :=
by rw [β bitwise_or, bitwise_bit]
@[simp] lemma land_bit (a m b n) : land (bit a m) (bit b n) = bit (a && b) (land m n) :=
by rw [β bitwise_and, bitwise_bit]
@[simp] lemma ldiff_bit (a m b n) : ldiff (bit a m) (bit b n) = bit (a && bnot b) (ldiff m n) :=
by rw [β bitwise_diff, bitwise_bit]
@[simp] lemma lxor_bit (a m b n) : lxor (bit a m) (bit b n) = bit (bxor a b) (lxor m n) :=
by rw [β bitwise_xor, bitwise_bit]
@[simp] lemma lnot_bit (b) : β n, lnot (bit b n) = bit (bnot b) (lnot n)
| (n : β) := by simp [lnot]
| -[1+ n] := by simp [lnot]
@[simp] lemma test_bit_bitwise (f : bool β bool β bool) (m n k) :
test_bit (bitwise f m n) k = f (test_bit m k) (test_bit n k) :=
begin
induction k with k IH generalizing m n;
apply bit_cases_on m; intros a m';
apply bit_cases_on n; intros b n';
rw bitwise_bit,
{ simp [test_bit_zero] },
{ simp [test_bit_succ, IH] }
end
@[simp] lemma test_bit_lor (m n k) : test_bit (lor m n) k = test_bit m k || test_bit n k :=
by rw [β bitwise_or, test_bit_bitwise]
@[simp] lemma test_bit_land (m n k) : test_bit (land m n) k = test_bit m k && test_bit n k :=
by rw [β bitwise_and, test_bit_bitwise]
@[simp]
lemma test_bit_ldiff (m n k) : test_bit (ldiff m n) k = test_bit m k && bnot (test_bit n k) :=
by rw [β bitwise_diff, test_bit_bitwise]
@[simp] lemma test_bit_lxor (m n k) : test_bit (lxor m n) k = bxor (test_bit m k) (test_bit n k) :=
by rw [β bitwise_xor, test_bit_bitwise]
@[simp] lemma test_bit_lnot : β n k, test_bit (lnot n) k = bnot (test_bit n k)
| (n : β) k := by simp [lnot, test_bit]
| -[1+ n] k := by simp [lnot, test_bit]
lemma shiftl_add : β (m : β€) (n : β) (k : β€), shiftl m (n + k) = shiftl (shiftl m n) k
| (m : β) n (k:β) := congr_arg of_nat (nat.shiftl_add _ _ _)
| -[1+ m] n (k:β) := congr_arg neg_succ_of_nat (nat.shiftl'_add _ _ _ _)
| (m : β) n -[1+k] := sub_nat_nat_elim n k.succ
(Ξ» n k i, shiftl βm i = nat.shiftr (nat.shiftl m n) k)
(Ξ» i n, congr_arg coe $
by rw [β nat.shiftl_sub, nat.add_sub_cancel_left]; apply nat.le_add_right)
(Ξ» i n, congr_arg coe $
by rw [add_assoc, nat.shiftr_add, β nat.shiftl_sub, nat.sub_self]; refl)
| -[1+ m] n -[1+k] := sub_nat_nat_elim n k.succ
(Ξ» n k i, shiftl -[1+ m] i = -[1+ nat.shiftr (nat.shiftl' tt m n) k])
(Ξ» i n, congr_arg neg_succ_of_nat $
by rw [β nat.shiftl'_sub, nat.add_sub_cancel_left]; apply nat.le_add_right)
(Ξ» i n, congr_arg neg_succ_of_nat $
by rw [add_assoc, nat.shiftr_add, β nat.shiftl'_sub, nat.sub_self]; refl)
lemma shiftl_sub (m : β€) (n : β) (k : β€) : shiftl m (n - k) = shiftr (shiftl m n) k :=
shiftl_add _ _ _
@[simp] lemma shiftl_neg (m n : β€) : shiftl m (-n) = shiftr m n := rfl
@[simp] lemma shiftr_neg (m n : β€) : shiftr m (-n) = shiftl m n := by rw [β shiftl_neg, neg_neg]
@[simp] lemma shiftl_coe_nat (m n : β) : shiftl m n = nat.shiftl m n := rfl
@[simp] lemma shiftr_coe_nat (m n : β) : shiftr m n = nat.shiftr m n := by cases n; refl
@[simp] lemma shiftl_neg_succ (m n : β) : shiftl -[1+ m] n = -[1+ nat.shiftl' tt m n] := rfl
@[simp]
lemma shiftr_neg_succ (m n : β) : shiftr -[1+ m] n = -[1+ nat.shiftr m n] := by cases n; refl
lemma shiftr_add : β (m : β€) (n k : β), shiftr m (n + k) = shiftr (shiftr m n) k
| (m : β) n k := by rw [shiftr_coe_nat, shiftr_coe_nat,
β int.coe_nat_add, shiftr_coe_nat, nat.shiftr_add]
| -[1+ m] n k := by rw [shiftr_neg_succ, shiftr_neg_succ,
β int.coe_nat_add, shiftr_neg_succ, nat.shiftr_add]
lemma shiftl_eq_mul_pow : β (m : β€) (n : β), shiftl m n = m * β(2 ^ n)
| (m : β) n := congr_arg coe (nat.shiftl_eq_mul_pow _ _)
| -[1+ m] n := @congr_arg β β€ _ _ (Ξ»i, -i) (nat.shiftl'_tt_eq_mul_pow _ _)
lemma shiftr_eq_div_pow : β (m : β€) (n : β), shiftr m n = m / β(2 ^ n)
| (m : β) n := by rw shiftr_coe_nat; exact congr_arg coe (nat.shiftr_eq_div_pow _ _)
| -[1+ m] n := begin
rw [shiftr_neg_succ, neg_succ_of_nat_div, nat.shiftr_eq_div_pow], refl,
exact coe_nat_lt_coe_nat_of_lt (pow_pos dec_trivial _)
end
lemma one_shiftl (n : β) : shiftl 1 n = (2 ^ n : β) :=
congr_arg coe (nat.one_shiftl _)
@[simp] lemma zero_shiftl : β n : β€, shiftl 0 n = 0
| (n : β) := congr_arg coe (nat.zero_shiftl _)
| -[1+ n] := congr_arg coe (nat.zero_shiftr _)
@[simp] lemma zero_shiftr (n) : shiftr 0 n = 0 := zero_shiftl _
/-! ### Least upper bound property for integers -/
/-- A computable version of `exists_least_of_bdd`: given a decidable predicate on the
integers, with an explicit lower bound and a proof that it is somewhere true, return
the least value for which the predicate is true. -/
def least_of_bdd {P : β€ β Prop} [decidable_pred P]
(b : β€) (Hb : β z : β€, P z β b β€ z) (Hinh : β z : β€, P z) :
{lb : β€ // P lb β§ (β z : β€, P z β lb β€ z)} :=
have EX : β n : β, P (b + n), from
let β¨elt, Heltβ© := Hinh in
match elt, le.dest (Hb _ Helt), Helt with
| ._, β¨n, rflβ©, Hn := β¨n, Hnβ©
end,
β¨b + (nat.find EX : β€), nat.find_spec EX, Ξ» z h,
match z, le.dest (Hb _ h), h with
| ._, β¨n, rflβ©, h := add_le_add_left
(int.coe_nat_le.2 $ nat.find_min' _ h) _
endβ©
theorem exists_least_of_bdd {P : β€ β Prop}
(Hbdd : β b : β€, β z : β€, P z β b β€ z) (Hinh : β z : β€, P z) :
β lb : β€, P lb β§ (β z : β€, P z β lb β€ z) :=
by classical; exact let β¨b, Hbβ© := Hbdd, β¨lb, Hβ© := least_of_bdd b Hb Hinh in β¨lb, Hβ©
lemma coe_least_of_bdd_eq {P : β€ β Prop} [decidable_pred P]
{b b' : β€} (Hb : β z : β€, P z β b β€ z) (Hb' : β z : β€, P z β b' β€ z) (Hinh : β z : β€, P z) :
(least_of_bdd b Hb Hinh : β€) = least_of_bdd b' Hb' Hinh :=
begin
rcases least_of_bdd b Hb Hinh with β¨n, hn, h2nβ©,
rcases least_of_bdd b' Hb' Hinh with β¨n', hn', h2n'β©,
exact le_antisymm (h2n _ hn') (h2n' _ hn),
end
/-- A computable version of `exists_greatest_of_bdd`: given a decidable predicate on the
integers, with an explicit upper bound and a proof that it is somewhere true, return
the greatest value for which the predicate is true. -/
def greatest_of_bdd {P : β€ β Prop} [decidable_pred P]
(b : β€) (Hb : β z : β€, P z β z β€ b) (Hinh : β z : β€, P z) :
{ub : β€ // P ub β§ (β z : β€, P z β z β€ ub)} :=
have Hbdd' : β (z : β€), P (-z) β -b β€ z, from Ξ» z h, neg_le.1 (Hb _ h),
have Hinh' : β z : β€, P (-z), from
let β¨elt, Heltβ© := Hinh in β¨-elt, by rw [neg_neg]; exact Heltβ©,
let β¨lb, Plb, alβ© := least_of_bdd (-b) Hbdd' Hinh' in
β¨-lb, Plb, Ξ» z h, le_neg.1 $ al _ $ by rwa neg_negβ©
theorem exists_greatest_of_bdd {P : β€ β Prop}
(Hbdd : β b : β€, β z : β€, P z β z β€ b) (Hinh : β z : β€, P z) :
β ub : β€, P ub β§ (β z : β€, P z β z β€ ub) :=
by classical; exact let β¨b, Hbβ© := Hbdd, β¨lb, Hβ© := greatest_of_bdd b Hb Hinh in β¨lb, Hβ©
lemma coe_greatest_of_bdd_eq {P : β€ β Prop} [decidable_pred P]
{b b' : β€} (Hb : β z : β€, P z β z β€ b) (Hb' : β z : β€, P z β z β€ b') (Hinh : β z : β€, P z) :
(greatest_of_bdd b Hb Hinh : β€) = greatest_of_bdd b' Hb' Hinh :=
begin
rcases greatest_of_bdd b Hb Hinh with β¨n, hn, h2nβ©,
rcases greatest_of_bdd b' Hb' Hinh with β¨n', hn', h2n'β©,
exact le_antisymm (h2n' _ hn) (h2n _ hn'),
end
end int
attribute [irreducible] int.nonneg
|
16c11e5b939905de29c3beafced59dfc48f5f3bb | b7f22e51856f4989b970961f794f1c435f9b8f78 | /library/theories/finite_group_theory/cyclic.lean | 5adf832a7860248a3b9cb00403c71667bda4504d | [
"Apache-2.0"
] | permissive | soonhokong/lean | cb8aa01055ffe2af0fb99a16b4cda8463b882cd1 | 38607e3eb57f57f77c0ac114ad169e9e4262e24f | refs/heads/master | 1,611,187,284,081 | 1,450,766,737,000 | 1,476,122,547,000 | 11,513,992 | 2 | 0 | null | 1,401,763,102,000 | 1,374,182,235,000 | C++ | UTF-8 | Lean | false | false | 15,826 | lean | /-
Copyright (c) 2015 Haitao Zhang. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author : Haitao Zhang
-/
import data algebra.group algebra.group_power .finsubg .hom .perm
open function finset
open eq.ops
namespace group_theory
section cyclic
open nat fin list
local attribute madd [reducible]
variable {A : Type}
variable [ambG : group A]
include ambG
lemma pow_mod {a : A} {n m : nat} : a ^ m = 1 β a ^ n = a ^ (n % m) :=
assume Pid,
have a ^ (n / m * m) = 1, from calc
a ^ (n / m * m) = a ^ (m * (n / m)) : by rewrite (mul.comm (n / m) m)
... = (a ^ m) ^ (n / m) : by rewrite pow_mul
... = 1 ^ (n / m) : by rewrite Pid
... = 1 : one_pow (n / m),
calc a ^ n = a ^ (n / m * m + n % m) : by rewrite -(eq_div_mul_add_mod n m)
... = a ^ (n / m * m) * a ^ (n % m) : by rewrite pow_add
... = 1 * a ^ (n % m) : by rewrite this
... = a ^ (n % m) : by rewrite one_mul
lemma pow_sub_eq_one_of_pow_eq {a : A} {i j : nat} :
a^i = a^j β a^(i - j) = 1 :=
assume Pe, or.elim (lt_or_ge i j)
(assume Piltj, begin rewrite [sub_eq_zero_of_le (nat.le_of_lt Piltj)] end)
(assume Pigej, begin rewrite [pow_sub a Pigej, Pe, mul.right_inv] end)
lemma pow_dist_eq_one_of_pow_eq {a : A} {i j : nat} :
a^i = a^j β a^(dist i j) = 1 :=
assume Pe, or.elim (lt_or_ge i j)
(suppose i < j, by rewrite [dist_eq_sub_of_lt this]; exact pow_sub_eq_one_of_pow_eq (eq.symm Pe))
(suppose i β₯ j, by rewrite [dist_eq_sub_of_ge this]; exact pow_sub_eq_one_of_pow_eq Pe)
lemma pow_madd {a : A} {n : nat} {i j : fin (succ n)} :
a^(succ n) = 1 β a^(val (i + j)) = a^i * a^j :=
assume Pe, calc
a^(val (i + j)) = a^((i + j) % (succ n)) : rfl
... = a^(val i + val j) : by rewrite [-pow_mod Pe]
... = a^i * a^j : by rewrite pow_add
lemma mk_pow_mod {a : A} {n m : nat} : a ^ (succ m) = 1 β a ^ n = a ^ (mk_mod m n) :=
assume Pe, pow_mod Pe
variable [finA : fintype A]
include finA
open fintype
variable [deceqA : decidable_eq A]
include deceqA
lemma exists_pow_eq_one (a : A) : β n, n < card A β§ a ^ (succ n) = 1 :=
let f := (Ξ» i : fin (succ (card A)), a ^ i) in
have Pninj : Β¬(injective f), from assume Pinj,
absurd (card_le_of_inj _ _ (exists.intro f Pinj))
(begin rewrite [card_fin], apply not_succ_le_self end),
obtain iβ Pβ, from exists_not_of_not_forall Pninj,
obtain iβ Pβ, from exists_not_of_not_forall Pβ,
obtain Pfe Pne, from and_not_of_not_implies Pβ,
have Pvne : val iβ β val iβ, from assume Pveq, absurd (eq_of_veq Pveq) Pne,
exists.intro (pred (dist iβ iβ)) (begin
rewrite [succ_pred_of_pos (dist_pos_of_ne Pvne)], apply and.intro,
apply lt_of_succ_lt_succ,
rewrite [succ_pred_of_pos (dist_pos_of_ne Pvne)],
apply nat.lt_of_le_of_lt dist_le_max (max_lt iβ iβ),
apply pow_dist_eq_one_of_pow_eq Pfe
end)
-- Another possibility is to generate a list of powers and use find to get the first
-- unity.
-- The bound on bex is arbitrary as long as it is large enough (at least card A). Making
-- it larger simplifies some proofs, such as a β cyc a.
definition cyc (a : A) : finset A := {x β univ | bex (succ (card A)) (Ξ» n, a ^ n = x)}
definition order (a : A) := card (cyc a)
definition pow_fin (a : A) (n : nat) (i : fin (order a)) := a ^ (i + n)
definition cyc_pow_fin (a : A) (n : nat) : finset A := image (pow_fin a n) univ
lemma order_le_group_order {a : A} : order a β€ card A :=
card_le_card_of_subset !subset_univ
lemma cyc_has_one (a : A) : 1 β cyc a :=
begin
apply mem_sep_of_mem !mem_univ,
existsi 0, apply and.intro,
apply zero_lt_succ,
apply pow_zero
end
lemma order_pos (a : A) : 0 < order a :=
length_pos_of_mem (cyc_has_one a)
lemma cyc_mul_closed (a : A) : finset_mul_closed_on (cyc a) :=
take g h, assume Pgin Phin,
obtain n Plt Pe, from exists_pow_eq_one a,
obtain i Pilt Pig, from of_mem_sep Pgin,
obtain j Pjlt Pjh, from of_mem_sep Phin,
begin
rewrite [-Pig, -Pjh, -pow_add, pow_mod Pe],
apply mem_sep_of_mem !mem_univ,
existsi ((i + j) % (succ n)), apply and.intro,
apply nat.lt_trans (mod_lt (i+j) !zero_lt_succ) (succ_lt_succ Plt),
apply rfl
end
lemma cyc_has_inv (a : A) : finset_has_inv (cyc a) :=
take g, assume Pgin,
obtain n Plt Pe, from exists_pow_eq_one a,
obtain i Pilt Pig, from of_mem_sep Pgin,
let ni := -(mk_mod n i) in
have Pinv : g*a^ni = 1, by
rewrite [-Pig, mk_pow_mod Pe, -(pow_madd Pe), add.right_inv],
begin
rewrite [inv_eq_of_mul_eq_one Pinv],
apply mem_sep_of_mem !mem_univ,
existsi ni, apply and.intro,
apply nat.lt_trans (is_lt ni) (succ_lt_succ Plt),
apply rfl
end
lemma self_mem_cyc (a : A) : a β cyc a :=
mem_sep_of_mem !mem_univ
(exists.intro (1 : nat) (and.intro (succ_lt_succ card_pos) !pow_one))
lemma mem_cyc (a : A) : β {n : nat}, a^n β cyc a
| 0 := cyc_has_one a
| (succ n) :=
begin rewrite pow_succ', apply cyc_mul_closed a, exact mem_cyc, apply self_mem_cyc end
lemma order_le {a : A} {n : nat} : a^(succ n) = 1 β order a β€ succ n :=
assume Pe, let s := image (pow_nat a) (upto (succ n)) in
have Psub: cyc a β s, from subset_of_forall
(take g, assume Pgin, obtain i Pilt Pig, from of_mem_sep Pgin, begin
rewrite [-Pig, pow_mod Pe],
apply mem_image,
apply mem_upto_of_lt (mod_lt i !zero_lt_succ),
exact rfl end),
#nat calc order a β€ card s : card_le_card_of_subset Psub
... β€ card (upto (succ n)) : !card_image_le
... = succ n : card_upto (succ n)
lemma pow_ne_of_lt_order {a : A} {n : nat} : succ n < order a β a^(succ n) β 1 :=
assume Plt, not_imp_not_of_imp order_le (not_le_of_gt Plt)
lemma eq_zero_of_pow_eq_one {a : A} : β {n : nat}, a^n = 1 β n < order a β n = 0
| 0 := assume Pe Plt, rfl
| (succ n) := assume Pe Plt, absurd Pe (pow_ne_of_lt_order Plt)
lemma pow_fin_inj (a : A) (n : nat) : injective (pow_fin a n) :=
take i j : fin (order a),
suppose a^(i + n) = a^(j + n),
have a^(dist i j) = 1, begin apply !dist_add_add_right βΈ (pow_dist_eq_one_of_pow_eq this) end,
have dist i j = 0, from
eq_zero_of_pow_eq_one this (nat.lt_of_le_of_lt dist_le_max (max_lt i j)),
eq_of_veq (eq_of_dist_eq_zero this)
lemma cyc_eq_cyc (a : A) (n : nat) : cyc_pow_fin a n = cyc a :=
have Psub : cyc_pow_fin a n β cyc a, from subset_of_forall
(take g, assume Pgin,
obtain i Pin Pig, from exists_of_mem_image Pgin, by rewrite [-Pig]; apply mem_cyc),
eq_of_card_eq_of_subset (begin apply eq.trans,
apply card_image_eq_of_inj_on,
rewrite [to_set_univ, -set.injective_iff_inj_on_univ], exact pow_fin_inj a n,
rewrite [card_fin] end) Psub
lemma pow_order (a : A) : a^(order a) = 1 :=
obtain i Pin Pone, from exists_of_mem_image (eq.symm (cyc_eq_cyc a 1) βΈ cyc_has_one a),
or.elim (eq_or_lt_of_le (succ_le_of_lt (is_lt i)))
(assume P, P βΈ Pone) (assume P, absurd Pone (pow_ne_of_lt_order P))
lemma eq_one_of_order_eq_one {a : A} : order a = 1 β a = 1 :=
assume Porder,
calc a = a^1 : by rewrite (pow_one a)
... = a^(order a) : by rewrite Porder
... = 1 : by rewrite pow_order
lemma order_of_min_pow {a : A} {n : nat}
(Pone : a^(succ n) = 1) (Pmin : β i, i < n β a^(succ i) β 1) : order a = succ n :=
or.elim (eq_or_lt_of_le (order_le Pone)) (Ξ» P, P)
(Ξ» P : order a < succ n, begin
have Pn : a^(order a) β 1,
begin
rewrite [-(succ_pred_of_pos (order_pos a))],
apply Pmin, apply nat.lt_of_succ_lt_succ,
rewrite [succ_pred_of_pos !order_pos], assumption
end,
exact absurd (pow_order a) Pn end)
lemma order_dvd_of_pow_eq_one {a : A} {n : nat} (Pone : a^n = 1) : order a β£ n :=
have Pe : a^(n % order a) = 1, from
begin
revert Pone,
rewrite [eq_div_mul_add_mod n (order a) at {1}, pow_add, mul.comm _ (order a), pow_mul, pow_order, one_pow, one_mul],
intros, assumption
end,
dvd_of_mod_eq_zero (eq_zero_of_pow_eq_one Pe (mod_lt n !order_pos))
definition cyc_is_finsubg [instance] (a : A) : is_finsubg (cyc a) :=
is_finsubg.mk (cyc_has_one a) (cyc_mul_closed a) (cyc_has_inv a)
lemma order_dvd_group_order (a : A) : order a β£ card A :=
dvd.intro (eq.symm (!mul.comm βΈ lagrange_theorem (subset_univ (cyc a))))
definition pow_fin' (a : A) (i : fin (succ (pred (order a)))) := pow_nat a i
local attribute group_of_add_group [instance]
lemma pow_fin_hom (a : A) : homomorphic (pow_fin' a) :=
take i j : fin (succ (pred (order a))),
begin
rewrite [βpow_fin'],
apply pow_madd,
rewrite [succ_pred_of_pos !order_pos],
exact pow_order a
end
definition pow_fin_is_iso (a : A) : is_iso_class (pow_fin' a) :=
is_iso_class.mk (pow_fin_hom a)
(have H : injective (Ξ» (i : fin (order a)), a ^ (val i + 0)), from pow_fin_inj a 0,
begin rewrite [βpow_fin', succ_pred_of_pos !order_pos]; exact H end)
end cyclic
section rot
open nat list
open fin fintype list
section
local attribute group_of_add_group [instance]
lemma pow_eq_mul {n : nat} {i : fin (succ n)} : β {k : nat}, i^k = mk_mod n (i*k)
| 0 := by rewrite [pow_zero]
| (succ k) := begin
have Psucc : i^(succ k) = madd (i^k) i, by apply pow_succ',
rewrite [Psucc, pow_eq_mul],
apply eq_of_veq,
rewrite [mul_succ, val_madd, βmk_mod, mod_add_mod]
end
end
definition rotl : β {n : nat} m : nat, fin n β fin n
| 0 := take m i, elim0 i
| (succ n) := take m, madd (mk_mod n (n*m))
definition rotr : β {n : nat} m : nat, fin n β fin n
| 0 := take m i, elim0 i
| (succ n) := take m, madd (-(mk_mod n (n*m)))
lemma rotl_succ' {n m : nat} : rotl m = madd (mk_mod n (n*m)) := rfl
lemma rotl_zero : β {n : nat}, @rotl n 0 = id
| 0 := funext take i, elim0 i
| (nat.succ n) := funext take i, begin rewrite [βrotl, mul_zero, mk_mod_zero_eq, zero_madd] end
lemma rotl_id : β {n : nat}, @rotl n n = id
| 0 := funext take i, elim0 i
| (nat.succ n) :=
have P : mk_mod n (n * succ n) = mk_mod n 0,
from eq_of_veq (by rewrite [βmk_mod, mul_mod_left]),
begin rewrite [rotl_succ', P], apply rotl_zero end
lemma rotl_to_zero {n i : nat} : rotl i (mk_mod n i) = 0 :=
eq_of_veq begin rewrite [βrotl, val_madd], esimp [mk_mod], rewrite [ mod_add_mod, add_mod_mod, -succ_mul, mul_mod_right] end
lemma rotl_compose : β {n : nat} {j k : nat}, (@rotl n j) β (rotl k) = rotl (j + k)
| 0 := take j k, funext take i, elim0 i
| (succ n) := take j k, funext take i, eq.symm begin
rewrite [*rotl_succ', left_distrib, -(@madd_mk_mod n (n*j)), madd_assoc],
end
lemma rotr_rotl : β {n : nat} (m : nat) {i : fin n}, rotr m (rotl m i) = i
| 0 := take m i, elim0 i
| (nat.succ n) := take m i, calc (-(mk_mod n (n*m))) + ((mk_mod n (n*m)) + i) = i : by rewrite neg_add_cancel_left
lemma rotl_rotr : β {n : nat} (m : nat), (@rotl n m) β (rotr m) = id
| 0 := take m, funext take i, elim0 i
| (nat.succ n) := take m, funext take i, calc (mk_mod n (n*m)) + (-(mk_mod n (n*m)) + i) = i : add_neg_cancel_left
lemma rotl_succ {n : nat} : (rotl 1) β (@succ n) = lift_succ :=
funext (take i, eq_of_veq (begin rewrite [βcomp, βrotl, βmadd, mul_one n, βmk_mod, mod_add_mod, βlift_succ, val_succ, -succ_add_eq_succ_add, add_mod_self_left, mod_eq_of_lt (lt.trans (is_lt i) !lt_succ_self), -val_lift] end))
definition list.rotl {A : Type} : β l : list A, list A
| [] := []
| (a::l) := l++[a]
lemma rotl_cons {A : Type} {a : A} {l} : list.rotl (a::l) = l++[a] := rfl
lemma rotl_map {A B : Type} {f : A β B} : β {l : list A}, list.rotl (map f l) = map f (list.rotl l)
| [] := rfl
| (a::l) := begin rewrite [map_cons, *rotl_cons, map_append] end
lemma rotl_eq_rotl : β {n : nat}, map (rotl 1) (upto n) = list.rotl (upto n)
| 0 := rfl
| (succ n) := begin
rewrite [upto_step at {1}, fin.upto_succ, rotl_cons, map_append],
congruence,
rewrite [map_map], congruence, exact rotl_succ,
rewrite [map_singleton], congruence, rewrite [βrotl, mul_one n, βmk_mod, βmaxi, βmadd],
congruence, rewrite [ mod_add_mod, val_zero, add_zero, mod_eq_of_lt !lt_succ_self ]
end
definition seq [reducible] (A : Type) (n : nat) := fin n β A
variable {A : Type}
definition rotl_fun {n : nat} (m : nat) (f : seq A n) : seq A n := f β (rotl m)
definition rotr_fun {n : nat} (m : nat) (f : seq A n) : seq A n := f β (rotr m)
lemma rotl_seq_zero {n : nat} : rotl_fun 0 = @id (seq A n) :=
funext take f, begin rewrite [βrotl_fun, rotl_zero] end
lemma rotl_seq_ne_id : β {n : nat}, (β a b : A, a β b) β β i, i < n β rotl_fun (succ i) β (@id (seq A (succ n)))
| 0 := assume Pex, take i, assume Piltn, absurd Piltn !not_lt_zero
| (nat.succ n) := assume Pex, obtain a b Pne, from Pex, take i, assume Pilt,
let f := (Ξ» j : fin (succ (succ n)), if j = 0 then a else b),
fi := mk_mod (succ n) (succ i) in
have Pfne : rotl_fun (succ i) f fi β f fi,
from begin rewrite [βrotl_fun, rotl_to_zero, mk_mod_of_lt (succ_lt_succ Pilt), if_pos rfl, if_neg mk_succ_ne_zero], assumption end,
have P : rotl_fun (succ i) f β f, from
assume Peq, absurd (congr_fun Peq fi) Pfne,
assume Peq, absurd (congr_fun Peq f) P
lemma rotr_rotl_fun {n : nat} (m : nat) (f : seq A n) : rotr_fun m (rotl_fun m f) = f :=
calc f β (rotl m) β (rotr m) = f β ((rotl m) β (rotr m)) : by rewrite -comp.assoc
... = f β id : by rewrite (rotl_rotr m)
lemma rotl_fun_inj {n : nat} {m : nat} : @injective (seq A n) (seq A n) (rotl_fun m) :=
injective_of_has_left_inverse (exists.intro (rotr_fun m) (rotr_rotl_fun m))
lemma seq_rotl_eq_list_rotl {n : nat} (f : seq A n) :
fun_to_list (rotl_fun 1 f) = list.rotl (fun_to_list f) :=
begin
rewrite [βfun_to_list, βrotl_fun, -map_map, rotl_map],
congruence, exact rotl_eq_rotl
end
end rot
section rotg
open nat fin fintype
definition rotl_perm [reducible] (A : Type) [finA : fintype A] [deceqA : decidable_eq A] (n : nat) (m : nat) : perm (seq A n) :=
perm.mk (rotl_fun m) rotl_fun_inj
variable {A : Type}
variable [finA : fintype A]
variable [deceqA : decidable_eq A]
variable {n : nat}
include finA deceqA
lemma rotl_perm_mul {i j : nat} : (rotl_perm A n i) * (rotl_perm A n j) = rotl_perm A n (j+i) :=
eq_of_feq (funext take f, calc
f β (rotl j) β (rotl i) = f β ((rotl j) β (rotl i)) : by rewrite -comp.assoc
... = f β (rotl (j+i)) : by rewrite rotl_compose)
lemma rotl_perm_pow_eq : β {i : nat}, (rotl_perm A n 1) ^ i = rotl_perm A n i
| 0 := begin rewrite [pow_zero, βrotl_perm, perm_one, -eq_iff_feq], esimp, rewrite rotl_seq_zero end
| (succ i) := begin rewrite [pow_succ', rotl_perm_pow_eq, rotl_perm_mul, one_add] end
lemma rotl_perm_pow_eq_one : (rotl_perm A n 1) ^ n = 1 :=
eq.trans rotl_perm_pow_eq (eq_of_feq begin esimp [rotl_perm], rewrite [βrotl_fun, rotl_id] end)
lemma rotl_perm_mod {i : nat} : rotl_perm A n i = rotl_perm A n (i % n) :=
calc rotl_perm A n i = (rotl_perm A n 1) ^ i : by rewrite rotl_perm_pow_eq
... = (rotl_perm A n 1) ^ (i % n) : by rewrite (pow_mod rotl_perm_pow_eq_one)
... = rotl_perm A n (i % n) : by rewrite rotl_perm_pow_eq
-- needs A to have at least two elements!
lemma rotl_perm_pow_ne_one (Pex : β a b : A, a β b) : β i, i < n β (rotl_perm A (succ n) 1)^(succ i) β 1 :=
take i, assume Piltn, begin
intro P, revert P, rewrite [rotl_perm_pow_eq, -eq_iff_feq, perm_one, *perm.f_mk],
intro P, exact absurd P (rotl_seq_ne_id Pex i Piltn)
end
lemma rotl_perm_order (Pex : β a b : A, a β b) : order (rotl_perm A (succ n) 1) = (succ n) :=
order_of_min_pow rotl_perm_pow_eq_one (rotl_perm_pow_ne_one Pex)
end rotg
end group_theory
|
64ff51ba611ba148cac43093d4eb7ed824af677b | 94e33a31faa76775069b071adea97e86e218a8ee | /src/algebra/algebraic_card.lean | bac85cd699ce034b56a1d78820838c89d34a594f | [
"Apache-2.0"
] | permissive | urkud/mathlib | eab80095e1b9f1513bfb7f25b4fa82fa4fd02989 | 6379d39e6b5b279df9715f8011369a301b634e41 | refs/heads/master | 1,658,425,342,662 | 1,658,078,703,000 | 1,658,078,703,000 | 186,910,338 | 0 | 0 | Apache-2.0 | 1,568,512,083,000 | 1,557,958,709,000 | Lean | UTF-8 | Lean | false | false | 3,716 | lean | /-
Copyright (c) 2022 Violeta HernΓ‘ndez Palacios. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Violeta HernΓ‘ndez Palacios
-/
import data.polynomial.cardinal
import ring_theory.algebraic
/-!
### Cardinality of algebraic numbers
In this file, we prove variants of the following result: the cardinality of algebraic numbers under
an R-algebra is at most `# polynomial R * β΅β`.
Although this can be used to prove that real or complex transcendental numbers exist, a more direct
proof is given by `liouville.is_transcendental`.
-/
universes u v
open cardinal polynomial
open_locale cardinal
namespace algebraic
theorem aleph_0_le_cardinal_mk_of_char_zero (R A : Type*) [comm_ring R] [is_domain R]
[ring A] [algebra R A] [char_zero A] : β΅β β€ #{x : A // is_algebraic R x} :=
@mk_le_of_injective (ulift β) {x : A | is_algebraic R x} (Ξ» n, β¨_, is_algebraic_nat n.downβ©)
(Ξ» m n hmn, by simpa using hmn)
section lift
variables (R : Type u) (A : Type v) [comm_ring R] [comm_ring A] [is_domain A] [algebra R A]
[no_zero_smul_divisors R A]
theorem cardinal_mk_lift_le_mul :
cardinal.lift.{u v} (#{x : A // is_algebraic R x}) β€ cardinal.lift.{v u} (#(polynomial R)) * β΅β :=
begin
rw [βmk_ulift, βmk_ulift],
let g : ulift.{u} {x : A | is_algebraic R x} β ulift.{v} (polynomial R) :=
Ξ» x, ulift.up (classical.some x.1.2),
apply cardinal.mk_le_mk_mul_of_mk_preimage_le g (Ξ» f, _),
suffices : fintype (g β»ΒΉ' {f}),
{ exact @mk_le_aleph_0 _ (@fintype.to_encodable _ this) },
by_cases hf : f.1 = 0,
{ convert set.fintype_empty,
apply set.eq_empty_iff_forall_not_mem.2 (Ξ» x hx, _),
simp only [set.mem_preimage, set.mem_singleton_iff] at hx,
apply_fun ulift.down at hx,
rw hf at hx,
exact (classical.some_spec x.1.2).1 hx },
let h : g β»ΒΉ' {f} β f.down.root_set A := Ξ» x, β¨x.1.1.1, (mem_root_set_iff hf x.1.1.1).2 begin
have key' : g x = f := x.2,
simp_rw β key',
exact (classical.some_spec x.1.1.2).2
endβ©,
apply fintype.of_injective h (Ξ» _ _ H, _),
simp only [subtype.val_eq_coe, subtype.mk_eq_mk] at H,
exact subtype.ext (ulift.down_injective (subtype.ext H))
end
theorem cardinal_mk_lift_le_max :
cardinal.lift.{u v} (#{x : A // is_algebraic R x}) β€ max (cardinal.lift.{v u} (#R)) β΅β :=
(cardinal_mk_lift_le_mul R A).trans $
(mul_le_mul_right' (lift_le.2 cardinal_mk_le_max) _).trans $ by simp [le_total]
theorem cardinal_mk_lift_le_of_infinite [infinite R] :
cardinal.lift.{u v} (#{x : A // is_algebraic R x}) β€ cardinal.lift.{v u} (#R) :=
(cardinal_mk_lift_le_max R A).trans $ by simp
variable [encodable R]
@[simp] theorem countable_of_encodable : set.countable {x : A | is_algebraic R x} :=
begin
rw [βmk_set_le_aleph_0, βlift_le],
apply (cardinal_mk_lift_le_max R A).trans,
simp
end
@[simp] theorem cardinal_mk_of_encodable_of_char_zero [char_zero A] [is_domain R] :
#{x : A // is_algebraic R x} = β΅β :=
le_antisymm (by simp) (aleph_0_le_cardinal_mk_of_char_zero R A)
end lift
section non_lift
variables (R A : Type u) [comm_ring R] [comm_ring A] [is_domain A] [algebra R A]
[no_zero_smul_divisors R A]
theorem cardinal_mk_le_mul : #{x : A // is_algebraic R x} β€ #(polynomial R) * β΅β :=
by { rw [βlift_id (#_), βlift_id (#(polynomial R))], exact cardinal_mk_lift_le_mul R A }
theorem cardinal_mk_le_max : #{x : A // is_algebraic R x} β€ max (#R) β΅β :=
by { rw [βlift_id (#_), βlift_id (#R)], exact cardinal_mk_lift_le_max R A }
theorem cardinal_mk_le_of_infinite [infinite R] : #{x : A // is_algebraic R x} β€ #R :=
(cardinal_mk_le_max R A).trans $ by simp
end non_lift
end algebraic
|
37f1077227581e3857744c248b279afb0b1136e6 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/analysis/calculus/fderiv_measurable.lean | 5ea4d5a2c2a2fef67a38d87ac555b4917630ea0f | [] | 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 | 11,517 | lean | /-
Copyright (c) 2020 SΓ©bastien GouΓ«zel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: SΓ©bastien GouΓ«zel, Yury Kudryashov
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.analysis.calculus.deriv
import Mathlib.measure_theory.borel_space
import Mathlib.PostPort
universes u_1 u_2 u_3
namespace Mathlib
/-!
# Derivative is measurable
In this file we prove that the derivative of any function with complete codomain is a measurable
function. Namely, we prove:
* `is_measurable_set_of_differentiable_at`: the set `{x | differentiable_at π f x}` is measurable;
* `measurable_fderiv`: the function `fderiv π f` is measurable;
* `measurable_fderiv_apply_const`: for a fixed vector `y`, the function `Ξ» x, fderiv π f x y`
is measurable;
* `measurable_deriv`: the function `deriv f` is measurable (for `f : π β F`).
## Implementation
We give a proof that avoids second-countability issues, by expressing the differentiability set
as a function of open sets in the following way. Define `A (L, r, Ξ΅)` to be the set of points
where, on a ball of radius roughly `r` around `x`, the function is uniformly approximated by the
linear map `L`, up to `Ξ΅ r`. It is an open set.
Let also `B (L, r, s, Ξ΅) = A (L, r, Ξ΅) β© A (L, s, Ξ΅)`: we require that at two possibly different
scales `r` and `s`, the function is well approximated by the linear map `L`. It is also open.
We claim that the differentiability set of `f` is exactly
`D = β Ξ΅ > 0, β Ξ΄ > 0, β r, s < Ξ΄, β L, B (L, r, s, Ξ΅)`.
In other words, for any `Ξ΅ > 0`, we require that there is a size `Ξ΄` such that, for any two scales
below this size, the function is well approximated by a linear map, common to the two scales.
The set `β L, B (L, r, s, Ξ΅)` is open, as a union of open sets. Converting the intersections and
unions to countable ones (using real numbers of the form `2 ^ (-n)`), it follows that the
differentiability set is measurable.
To prove the claim, there are two inclusions. One is trivial: if the function is differentiable
at `x`, then `x` belongs to `D` (just take `L` to be the derivative, and use that the
differentiability exactly says that the map is well approximated by `L`). This is proved in
`mem_A_of_differentiable` and `differentiable_set_subset_D`.
For the other direction, the difficulty is that `L` in the union may depend on `Ξ΅, r, s`. The key
point is that, in fact, it doesn't depend too much on them. First, if `x` belongs both to
`A (L, r, Ξ΅)` and `A (L', r, Ξ΅)`, then `L` and `L'` have to be close on a shell, and thus
`β₯L - L'β₯` is bounded by `Ξ΅` (see `norm_sub_le_of_mem_A`). Assume now `x β D`. If one has two maps
`L` and `L'` such that `x` belongs to `A (L, r, Ξ΅)` and to `A (L', r', Ξ΅')`, one deduces that `L` is
close to `L'` by arguing as follows. Consider another scale `s` smaller than `r` and `r'`. Take a
linear map `Lβ` that approximates `f` around `x` both at scales `r` and `s` w.r.t. `Ξ΅` (it exists as
`x` belongs to `D`). Take also `Lβ` that approximates `f` around `x` both at scales `r'` and `s`
w.r.t. `Ξ΅'`. Then `Lβ` is close to `L` (as they are close on a shell of radius `r`), and `Lβ` is
close to `Lβ` (as they are close on a shell of radius `s`), and `L'` is close to `Lβ` (as they are
close on a shell of radius `r'`). It follows that `L` is close to `L'`, as we claimed.
It follows that the different approximating linear maps that show up form a Cauchy sequence when
`Ξ΅` tends to `0`. When the target space is complete, this sequence converges, to a limit `f'`.
With the same kind of arguments, one checks that `f` is differentiable with derivative `f'`.
To show that the derivative itself is measurable, add in the definition of `B` and `D` a set
`K` of continuous linear maps to which `L` should belong. Then, when `K` is complete, the set `D K`
is exactly the set of points where `f` is differentiable with a derivative in `K`.
## Tags
derivative, measurable function, Borel Ο-algebra
-/
namespace continuous_linear_map
protected instance measurable_space {π : Type u_1} {E : Type u_2} {F : Type u_3} [nondiscrete_normed_field π] [normed_group E] [normed_space π E] [normed_group F] [normed_space π F] : measurable_space (continuous_linear_map π E F) :=
borel (continuous_linear_map π E F)
protected instance borel_space {π : Type u_1} {E : Type u_2} {F : Type u_3} [nondiscrete_normed_field π] [normed_group E] [normed_space π E] [normed_group F] [normed_space π F] : borel_space (continuous_linear_map π E F) :=
borel_space.mk rfl
theorem measurable_apply {π : Type u_1} {E : Type u_2} {F : Type u_3} [nondiscrete_normed_field π] [normed_group E] [normed_space π E] [normed_group F] [normed_space π F] [measurable_space F] [borel_space F] (x : E) : measurable fun (f : continuous_linear_map π E F) => coe_fn f x :=
continuous.measurable (continuous_linear_map.continuous (apply π F x))
theorem measurable_apply' {π : Type u_1} {E : Type u_2} {F : Type u_3} [nondiscrete_normed_field π] [normed_group E] [normed_space π E] [normed_group F] [normed_space π F] [measurable_space E] [opens_measurable_space E] [measurable_space F] [borel_space F] : measurable fun (x : E) (f : continuous_linear_map π E F) => coe_fn f x :=
measurable_pi_lambda (fun (x : E) (f : continuous_linear_map π E F) => coe_fn f x)
fun (f : continuous_linear_map π E F) => continuous_linear_map.measurable f
theorem measurable_applyβ {π : Type u_1} {E : Type u_2} {F : Type u_3} [nondiscrete_normed_field π] [normed_group E] [normed_space π E] [normed_group F] [normed_space π F] [measurable_space E] [opens_measurable_space E] [topological_space.second_countable_topology E] [topological_space.second_countable_topology (continuous_linear_map π E F)] [measurable_space F] [borel_space F] : measurable fun (p : continuous_linear_map π E F Γ E) => coe_fn (prod.fst p) (prod.snd p) :=
continuous.measurable (is_bounded_bilinear_map.continuous is_bounded_bilinear_map_apply)
theorem measurable_coe {π : Type u_1} {E : Type u_2} {F : Type u_3} [nondiscrete_normed_field π] [normed_group E] [normed_space π E] [normed_group F] [normed_space π F] [measurable_space F] [borel_space F] : measurable fun (f : continuous_linear_map π E F) (x : E) => coe_fn f x :=
measurable_pi_lambda (fun (f : continuous_linear_map π E F) (x : E) => coe_fn f x) measurable_apply
end continuous_linear_map
namespace fderiv_measurable_aux
/-- The set `A f L r Ξ΅` is the set of points `x` around which the function `f` is well approximated
at scale `r` by the linear map `L`, up to an error `Ξ΅`. We tweak the definition to make sure that
this is an open set.-/
def A {π : Type u_1} [nondiscrete_normed_field π] {E : Type u_2} [normed_group E] [normed_space π E] {F : Type u_3} [normed_group F] [normed_space π F] (f : E β F) (L : continuous_linear_map π E F) (r : β) (Ξ΅ : β) : set E :=
set_of
fun (x : E) =>
β (r' : β),
β (H : r' β set.Ioc (r / bit0 1) r),
β (y z : E), y β metric.ball x r' β z β metric.ball x r' β norm (f z - f y - coe_fn L (z - y)) β€ Ξ΅ * r
/-- The set `B f K r s Ξ΅` is the set of points `x` around which there exists a continuous linear map
`L` belonging to `K` (a given set of continuous linear maps) that approximates well the
function `f` (up to an error `Ξ΅`), simultaneously at scales `r` and `s`. -/
def B {π : Type u_1} [nondiscrete_normed_field π] {E : Type u_2} [normed_group E] [normed_space π E] {F : Type u_3} [normed_group F] [normed_space π F] (f : E β F) (K : set (continuous_linear_map π E F)) (r : β) (s : β) (Ξ΅ : β) : set E :=
set.Union fun (L : continuous_linear_map π E F) => set.Union fun (H : L β K) => A f L r Ξ΅ β© A f L s Ξ΅
/-- The set `D f K` is a complicated set constructed using countable intersections and unions. Its
main use is that, when `K` is complete, it is exactly the set of points where `f` is differentiable,
with a derivative in `K`. -/
def D {π : Type u_1} [nondiscrete_normed_field π] {E : Type u_2} [normed_group E] [normed_space π E] {F : Type u_3} [normed_group F] [normed_space π F] (f : E β F) (K : set (continuous_linear_map π E F)) : set E :=
set.Inter
fun (e : β) =>
set.Union
fun (n : β) =>
set.Inter
fun (p : β) =>
set.Inter
fun (H : p β₯ n) =>
set.Inter
fun (q : β) =>
set.Inter fun (H : q β₯ n) => B f K ((1 / bit0 1) ^ p) ((1 / bit0 1) ^ q) ((1 / bit0 1) ^ e)
theorem is_open_A {π : Type u_1} [nondiscrete_normed_field π] {E : Type u_2} [normed_group E] [normed_space π E] {F : Type u_3} [normed_group F] [normed_space π F] {f : E β F} (L : continuous_linear_map π E F) (r : β) (Ξ΅ : β) : is_open (A f L r Ξ΅) := sorry
theorem is_open_B {π : Type u_1} [nondiscrete_normed_field π] {E : Type u_2} [normed_group E] [normed_space π E] {F : Type u_3} [normed_group F] [normed_space π F] {f : E β F} {K : set (continuous_linear_map π E F)} {r : β} {s : β} {Ξ΅ : β} : is_open (B f K r s Ξ΅) := sorry
theorem A_mono {π : Type u_1} [nondiscrete_normed_field π] {E : Type u_2} [normed_group E] [normed_space π E] {F : Type u_3} [normed_group F] [normed_space π F] {f : E β F} (L : continuous_linear_map π E F) (r : β) {Ξ΅ : β} {Ξ΄ : β} (h : Ξ΅ β€ Ξ΄) : A f L r Ξ΅ β A f L r Ξ΄ := sorry
theorem le_of_mem_A {π : Type u_1} [nondiscrete_normed_field π] {E : Type u_2} [normed_group E] [normed_space π E] {F : Type u_3} [normed_group F] [normed_space π F] {f : E β F} {r : β} {Ξ΅ : β} {L : continuous_linear_map π E F} {x : E} (hx : x β A f L r Ξ΅) {y : E} {z : E} (hy : y β metric.closed_ball x (r / bit0 1)) (hz : z β metric.closed_ball x (r / bit0 1)) : norm (f z - f y - coe_fn L (z - y)) β€ Ξ΅ * r := sorry
theorem mem_A_of_differentiable {π : Type u_1} [nondiscrete_normed_field π] {E : Type u_2} [normed_group E] [normed_space π E] {F : Type u_3} [normed_group F] [normed_space π F] {f : E β F} {Ξ΅ : β} (hΞ΅ : 0 < Ξ΅) {x : E} (hx : differentiable_at π f x) : β (R : β), β (H : R > 0), β (r : β), r β set.Ioo 0 R β x β A f (fderiv π f x) r Ξ΅ := sorry
theorem norm_sub_le_of_mem_A {π : Type u_1} [nondiscrete_normed_field π] {E : Type u_2} [normed_group E] [normed_space π E] {F : Type u_3} [normed_group F] [normed_space π F] {f : E β F} {c : π} (hc : 1 < norm c) {r : β} {Ξ΅ : β} (hΞ΅ : 0 < Ξ΅) (hr : 0 < r) {x : E} {Lβ : continuous_linear_map π E F} {Lβ : continuous_linear_map π E F} (hβ : x β A f Lβ r Ξ΅) (hβ : x β A f Lβ r Ξ΅) : norm (Lβ - Lβ) β€ bit0 (bit0 1) * norm c * Ξ΅ := sorry
/-- Easy inclusion: a differentiability point with derivative in `K` belongs to `D f K`. -/
theorem differentiable_set_subset_D {π : Type u_1} [nondiscrete_normed_field π] {E : Type u_2} [normed_group E] [normed_space π E] {F : Type u_3} [normed_group F] [normed_space π F] {f : E β F} (K : set (continuous_linear_map π E F)) : (set_of fun (x : E) => differentiable_at π f x β§ fderiv π f x β K) β D f K := sorry
/-- Harder inclusion: at a point in `D f K`, the function `f` has a derivative, in `K`. -/
|
b9cc6d8939cff2f936199d1d2f3895fc6aa7f485 | 92c6b42948d74fe325c2d88530f1d36da388b2f7 | /src/cvc4/sig/sat.lean | dbeba9db72000e3ca416651fc862f8f1c431bf88 | [
"MIT"
] | permissive | riaqn/smtlean | 8ad65055b6c1600cd03b9e345059a3b24419b6d5 | c11768cfb43cd634340b552f5039cba094701a87 | refs/heads/master | 1,584,569,627,940 | 1,535,314,713,000 | 1,535,314,713,000 | 135,333,334 | 0 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 213 | lean | namespace sig
def bool := bool
def tt := bool.tt
def ff := bool.ff
def satlem (Ξ± Ξ² : Prop) (x : Ξ±) (y : Ξ± β Ξ²) : Ξ² := y x
def satlem_simplify (Ξ± Ξ² : Prop) (x : Ξ±) (y : Ξ± β Ξ²) : Ξ² := y x
end sig
|
cff3ae880f5fb4c5cd68b59693d99fef705ba431 | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/algebra/hierarchy_design.lean | 6f4e04c9b95ea3cd426ec414331d5f1083cd100d | [
"Apache-2.0"
] | permissive | robertylewis/mathlib | 3d16e3e6daf5ddde182473e03a1b601d2810952c | 1d13f5b932f5e40a8308e3840f96fc882fae01f0 | refs/heads/master | 1,651,379,945,369 | 1,644,276,960,000 | 1,644,276,960,000 | 98,875,504 | 0 | 0 | Apache-2.0 | 1,644,253,514,000 | 1,501,495,700,000 | Lean | UTF-8 | Lean | false | false | 9,132 | lean | /-
Copyright (c) 2021 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Eric Weiser
-/
import tactic.doc_commands
/-!
# Documentation of the algebraic hierarchy
A library note giving advice on modifying the algebraic hierarchy.
(It is not intended as a "tour".)
TODO: Add sections about interactions with topological typeclasses, and order typeclasses.
-/
/--
# The algebraic hierarchy
In any theorem proving environment,
there are difficult decisions surrounding the design of the "algebraic hierarchy".
There is a danger of exponential explosion in the number of gadgets,
especially once interactions between algebraic and order/topological/etc structures are considered.
In mathlib, we try to avoid this by only introducing new algebraic typeclasses either
1. when there is "real mathematics" to be done with them, or
2. when there is a meaninful gain in simplicity by factoring out a common substructure.
(As examples, at this point we don't have `loop`, or `unital_magma`,
but we do have `lie_submodule` and `topological_field`!
We also have `group_with_zero`, as an exemplar of point 2.)
Generally in mathlib we use the extension mechanism (so `comm_ring` extends `ring`)
rather than mixins (e.g. with separate `ring` and `comm_mul` classes),
in part because of the potential blow-up in term sizes described at
https://www.ralfj.de/blog/2019/05/15/typeclasses-exponential-blowup.html
However there is tension here, as it results in considerable duplication in the API,
particularly in the interaction with order structures.
This library note is not intended as a design document
justifying and explaining the history of mathlib's algebraic hierarchy!
Instead it is intended as a developer's guide, for contributors wanting to extend
(either new leaves, or new intermediate classes) the algebraic hierarchy as it exists.
(Ideally we would have both a tour guide to the existing hierarchy,
and an account of the design choices.
See https://arxiv.org/abs/1910.09336 for an overview of mathlib as a whole,
with some attention to the algebraic hierarchy and
https://leanprover-community.github.io/mathlib-overview.html
for a summary of what is in mathlib today.)
## Instances
When adding a new typeclass `Z` to the algebraic hierarchy
one should attempt to add the following constructions and results,
when applicable:
* Instances transferred elementwise to products, like `prod.monoid`.
See `algebra.group.prod` for more examples.
```
instance prod.Z [Z M] [Z N] : Z (M Γ N) := ...
```
* Instances transferred elementwise to pi types, like `pi.monoid`.
See `algebra.group.pi` for more examples.
```
instance pi.Z [β i, Z $ f i] : Z (Ξ i : I, f i) := ...
```
* Instances transferred to `mul_opposite M`, like `mul_opposite.monoid`.
See `algebra.opposites` for more examples.
```
instance mul_opposite.Z [Z M] : Z (mul_opposite M) := ...
```
* Instances transferred to `ulift M`, like `ulift.monoid`.
See `algebra.group.ulift` for more examples.
```
instance ulift.Z [Z M] : Z (ulift M) := ...
```
* Definitions for transferring the proof fields of instances along
injective or surjective functions that agree on the data fields,
like `function.injective.monoid` and `function.surjective.monoid`.
We make these definitions `@[reducible]`, see note [reducible non-instances].
See `algebra.group.inj_surj` for more examples.
```
@[reducible]
def function.injective.Z [Z Mβ] (f : Mβ β Mβ) (hf : injective f)
(one : f 1 = 1) (mul : β x y, f (x * y) = f x * f y) : Z Mβ := ...
@[reducible]
def function.surjective.Z [Z Mβ] (f : Mβ β Mβ) (hf : surjective f)
(one : f 1 = 1) (mul : β x y, f (x * y) = f x * f y) : Z Mβ := ...
```
* Instances transferred elementwise to `finsupp`s, like `finsupp.semigroup`.
See `data.finsupp.pointwise` for more examples.
```
instance finsupp.Z [Z Ξ²] : Z (Ξ± ββ Ξ²) := ...
```
* Instances transferred elementwise to `set`s, like `set.monoid`.
See `algebra.pointwise` for more examples.
```
instance set.Z [Z Ξ±] : Z (set Ξ±) := ...
```
* Definitions for transferring the entire structure across an equivalence, like `equiv.monoid`.
See `data.equiv.transfer_instance` for more examples. See also the `transport` tactic.
```
def equiv.Z (e : Ξ± β Ξ²) [Z Ξ²] : Z Ξ± := ...
/- When there is a new notion of `Z`-equiv: -/
def equiv.Z_equiv (e : Ξ± β Ξ²) [Z Ξ²] : by { letI := equiv.Z e, exact Ξ± βZ Ξ² } := ...
```
## Subobjects
When a new typeclass `Z` adds new data fields,
you should also create a new `sub_Z` `structure` with a `carrier` field.
This can be a lot of work; for now try to closely follow the existing examples
(e.g. `submonoid`, `subring`, `subalgebra`).
We would very much like to provide some automation here, but a prerequisite will be making
all the existing APIs more uniform.
If `Z` extends `Y`, then `sub_Z` should usually extend `sub_Y`.
When `Z` adds only new proof fields to an existing structure `Y`,
you should provide instances transferring
`Z Ξ±` to `Z (sub_Y Ξ±)`, like `submonoid.to_comm_monoid`.
Typically this is done using the `function.injective.Z` definition mentioned above.
```
instance sub_Y.to_Z [Z Ξ±] : Z (sub_Y Ξ±) :=
coe_injective.Z coe ...
```
## Morphisms and equivalences
## Category theory
For many algebraic structures, particularly ones used in representation theory, algebraic geometry,
etc., we also define "bundled" versions, which carry `category` instances.
These bundled versions are usually named in camel case,
so for example we have `AddCommGroup` as a bundled `add_comm_group`,
and `TopCommRing` (which bundles together `comm_ring`, `topological_space`, and `topological_ring`).
These bundled versions have many appealing features:
* a uniform notation for morphisms `X βΆ Y`
* a uniform notation (and definition) for isomorphisms `X β
Y`
* a uniform API for subobjects, via the partial order `subobject X`
* interoperability with unbundled structures, via coercions to `Type`
(so if `G : AddCommGroup`, you can treat `G` as a type,
and it automatically has an `add_comm_group` instance)
and lifting maps `AddCommGroup.of G`, when `G` is a type with an `add_comm_group` instance.
If, for example you do the work of proving that a typeclass `Z` has a good notion of tensor product,
you are strongly encouraged to provide the corresponding `monoidal_category` instance
on a bundled version.
This ensures that the API for tensor products is complete, and enables use of general machinery.
Similarly if you prove universal properties, or adjunctions, you are encouraged to state these
using categorical language!
One disadvantage of the bundled approach is that we can only speak of morphisms between
objects living in the same type-theoretic universe.
In practice this is rarely a problem.
# Making a pull request
With so many moving parts, how do you actually go about changing the algebraic hierarchy?
We're still evolving how to handle this, but the current suggestion is:
* If you're adding a new "leaf" class, the requirements are lower,
and an initial PR can just add whatever is immediately needed.
* A new "intermediate" class, especially low down in the hierarchy,
needs to be careful about leaving gaps.
In a perfect world, there would be a group of simultaneous PRs that basically cover everything!
(Or at least an expectation that PRs may not be merged immediately while waiting on other
PRs that fill out the API.)
However "perfect is the enemy of good", and it would also be completely reasonable
to add a TODO list in the main module doc-string for the new class,
briefly listing the parts of the API which still need to be provided.
Hopefully this document makes it easy to assemble this list.
Another alternative to a TODO list in the doc-strings is adding github issues.
-/
library_note "the algebraic hierarchy"
/--
Some definitions that define objects of a class cannot be instances, because they have an
explicit argument that does not occur in the conclusion. An example is `preorder.lift` that has a
function `f : Ξ± β Ξ²` as an explicit argument to lift a preorder on `Ξ²` to a preorder on `Ξ±`.
If these definitions are used to define instances of this class *and* this class is an argument to
some other type-class so that type-class inference will have to unfold these instances to check
for definitional equality, then these definitions should be marked `@[reducible]`.
For example, `preorder.lift` is used to define `units.preorder` and `partial_order.lift` is used
to define `units.partial_order`. In some cases it is important that type-class inference can
recognize that `units.preorder` and `units.partial_order` give rise to the same `has_le` instance.
For example, you might have another class that takes `[has_le Ξ±]` as an argument, and this argument
sometimes comes from `units.preorder` and sometimes from `units.partial_order`.
Therefore, `preorder.lift` and `partial_order.lift` are marked `@[reducible]`.
-/
library_note "reducible non-instances"
|
b55503e1e30a717b1c4821b0278a5da888d52cb3 | 4d2583807a5ac6caaffd3d7a5f646d61ca85d532 | /src/data/equiv/transfer_instance.lean | 0c857f0699152295ac8d9d6258080d59fe039c80 | [
"Apache-2.0"
] | permissive | AntoineChambert-Loir/mathlib | 64aabb896129885f12296a799818061bc90da1ff | 07be904260ab6e36a5769680b6012f03a4727134 | refs/heads/master | 1,693,187,631,771 | 1,636,719,886,000 | 1,636,719,886,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 13,113 | lean | /-
Copyright (c) 2018 Johannes HΓΆlzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes HΓΆlzl
-/
import data.equiv.basic
import algebra.field
import algebra.module
import algebra.algebra.basic
import algebra.group.type_tags
import ring_theory.ideal.local_ring
/-!
# Transfer algebraic structures across `equiv`s
In this file we prove theorems of the following form: if `Ξ²` has a
group structure and `Ξ± β Ξ²` then `Ξ±` has a group structure, and
similarly for monoids, semigroups, rings, integral domains, fields and
so on.
Note that most of these constructions can also be obtained using the `transport` tactic.
## Tags
equiv, group, ring, field, module, algebra
-/
universes u v
variables {Ξ± : Type u} {Ξ² : Type v}
namespace equiv
section instances
variables (e : Ξ± β Ξ²)
/-- Transfer `has_one` across an `equiv` -/
@[to_additive "Transfer `has_zero` across an `equiv`"]
protected def has_one [has_one Ξ²] : has_one Ξ± := β¨e.symm 1β©
@[to_additive]
lemma one_def [has_one Ξ²] : @has_one.one _ (equiv.has_one e) = e.symm 1 := rfl
/-- Transfer `has_mul` across an `equiv` -/
@[to_additive "Transfer `has_add` across an `equiv`"]
protected def has_mul [has_mul Ξ²] : has_mul Ξ± := β¨Ξ» x y, e.symm (e x * e y)β©
@[to_additive]
lemma mul_def [has_mul Ξ²] (x y : Ξ±) :
@has_mul.mul _ (equiv.has_mul e) x y = e.symm (e x * e y) := rfl
/-- Transfer `has_div` across an `equiv` -/
@[to_additive "Transfer `has_sub` across an `equiv`"]
protected def has_div [has_div Ξ²] : has_div Ξ± := β¨Ξ» x y, e.symm (e x / e y)β©
@[to_additive]
lemma div_def [has_div Ξ²] (x y : Ξ±) :
@has_div.div _ (equiv.has_div e) x y = e.symm (e x / e y) := rfl
/-- Transfer `has_inv` across an `equiv` -/
@[to_additive "Transfer `has_neg` across an `equiv`"]
protected def has_inv [has_inv Ξ²] : has_inv Ξ± := β¨Ξ» x, e.symm (e x)β»ΒΉβ©
@[to_additive]
lemma inv_def [has_inv Ξ²] (x : Ξ±) : @has_inv.inv _ (equiv.has_inv e) x = e.symm (e x)β»ΒΉ := rfl
/-- Transfer `has_scalar` across an `equiv` -/
protected def has_scalar {R : Type*} [has_scalar R Ξ²] : has_scalar R Ξ± :=
β¨Ξ» r x, e.symm (r β’ (e x))β©
lemma smul_def {R : Type*} [has_scalar R Ξ²] (r : R) (x : Ξ±) :
@has_scalar.smul _ _ (equiv.has_scalar e) r x = e.symm (r β’ (e x)) := rfl
/--
An equivalence `e : Ξ± β Ξ²` gives a multiplicative equivalence `Ξ± β* Ξ²`
where the multiplicative structure on `Ξ±` is
the one obtained by transporting a multiplicative structure on `Ξ²` back along `e`.
-/
@[to_additive
"An equivalence `e : Ξ± β Ξ²` gives a additive equivalence `Ξ± β+ Ξ²`
where the additive structure on `Ξ±` is
the one obtained by transporting an additive structure on `Ξ²` back along `e`."]
def mul_equiv (e : Ξ± β Ξ²) [has_mul Ξ²] :
by { letI := equiv.has_mul e, exact Ξ± β* Ξ² } :=
begin
introsI,
exact
{ map_mul' := Ξ» x y, by { apply e.symm.injective, simp, refl, },
..e }
end
@[simp, to_additive] lemma mul_equiv_apply (e : Ξ± β Ξ²) [has_mul Ξ²] (a : Ξ±) :
(mul_equiv e) a = e a := rfl
@[to_additive] lemma mul_equiv_symm_apply (e : Ξ± β Ξ²) [has_mul Ξ²] (b : Ξ²) :
by { letI := equiv.has_mul e, exact (mul_equiv e).symm b = e.symm b } :=
begin
intros, refl,
end
/--
An equivalence `e : Ξ± β Ξ²` gives a ring equivalence `Ξ± β+* Ξ²`
where the ring structure on `Ξ±` is
the one obtained by transporting a ring structure on `Ξ²` back along `e`.
-/
def ring_equiv (e : Ξ± β Ξ²) [has_add Ξ²] [has_mul Ξ²] :
by { letI := equiv.has_add e, letI := equiv.has_mul e, exact Ξ± β+* Ξ² } :=
begin
introsI,
exact
{ map_add' := Ξ» x y, by { apply e.symm.injective, simp, refl, },
map_mul' := Ξ» x y, by { apply e.symm.injective, simp, refl, },
..e }
end
@[simp] lemma ring_equiv_apply (e : Ξ± β Ξ²) [has_add Ξ²] [has_mul Ξ²] (a : Ξ±) :
(ring_equiv e) a = e a := rfl
lemma ring_equiv_symm_apply (e : Ξ± β Ξ²) [has_add Ξ²] [has_mul Ξ²] (b : Ξ²) :
by { letI := equiv.has_add e, letI := equiv.has_mul e, exact (ring_equiv e).symm b = e.symm b } :=
begin
intros, refl,
end
/-- Transfer `semigroup` across an `equiv` -/
@[to_additive "Transfer `add_semigroup` across an `equiv`"]
protected def semigroup [semigroup Ξ²] : semigroup Ξ± :=
let mul := e.has_mul in
by resetI; apply e.injective.semigroup _; intros; exact e.apply_symm_apply _
/-- Transfer `semigroup_with_zero` across an `equiv` -/
protected def semigroup_with_zero [semigroup_with_zero Ξ²] : semigroup_with_zero Ξ± :=
let mul := e.has_mul, zero := e.has_zero in
by resetI; apply e.injective.semigroup_with_zero _; intros; exact e.apply_symm_apply _
/-- Transfer `comm_semigroup` across an `equiv` -/
@[to_additive "Transfer `add_comm_semigroup` across an `equiv`"]
protected def comm_semigroup [comm_semigroup Ξ²] : comm_semigroup Ξ± :=
let mul := e.has_mul in
by resetI; apply e.injective.comm_semigroup _; intros; exact e.apply_symm_apply _
/-- Transfer `mul_zero_class` across an `equiv` -/
protected def mul_zero_class [mul_zero_class Ξ²] : mul_zero_class Ξ± :=
let zero := e.has_zero, mul := e.has_mul in
by resetI; apply e.injective.mul_zero_class _; intros; exact e.apply_symm_apply _
/-- Transfer `mul_one_class` across an `equiv` -/
@[to_additive "Transfer `add_zero_class` across an `equiv`"]
protected def mul_one_class [mul_one_class Ξ²] : mul_one_class Ξ± :=
let one := e.has_one, mul := e.has_mul in
by resetI; apply e.injective.mul_one_class _; intros; exact e.apply_symm_apply _
/-- Transfer `mul_zero_one_class` across an `equiv` -/
protected def mul_zero_one_class [mul_zero_one_class Ξ²] : mul_zero_one_class Ξ± :=
let zero := e.has_zero, one := e.has_one,mul := e.has_mul in
by resetI; apply e.injective.mul_zero_one_class _; intros; exact e.apply_symm_apply _
/-- Transfer `monoid` across an `equiv` -/
@[to_additive "Transfer `add_monoid` across an `equiv`"]
protected def monoid [monoid Ξ²] : monoid Ξ± :=
let one := e.has_one, mul := e.has_mul in
by resetI; apply e.injective.monoid _; intros; exact e.apply_symm_apply _
/-- Transfer `comm_monoid` across an `equiv` -/
@[to_additive "Transfer `add_comm_monoid` across an `equiv`"]
protected def comm_monoid [comm_monoid Ξ²] : comm_monoid Ξ± :=
let one := e.has_one, mul := e.has_mul in
by resetI; apply e.injective.comm_monoid _; intros; exact e.apply_symm_apply _
/-- Transfer `group` across an `equiv` -/
@[to_additive "Transfer `add_group` across an `equiv`"]
protected def group [group Ξ²] : group Ξ± :=
let one := e.has_one, mul := e.has_mul, inv := e.has_inv, div := e.has_div in
by resetI; apply e.injective.group _; intros; exact e.apply_symm_apply _
/-- Transfer `comm_group` across an `equiv` -/
@[to_additive "Transfer `add_comm_group` across an `equiv`"]
protected def comm_group [comm_group Ξ²] : comm_group Ξ± :=
let one := e.has_one, mul := e.has_mul, inv := e.has_inv, div := e.has_div in
by resetI; apply e.injective.comm_group _; intros; exact e.apply_symm_apply _
/-- Transfer `non_unital_non_assoc_semiring` across an `equiv` -/
protected def non_unital_non_assoc_semiring [non_unital_non_assoc_semiring Ξ²] :
non_unital_non_assoc_semiring Ξ± :=
let zero := e.has_zero, add := e.has_add, mul := e.has_mul in
by resetI; apply e.injective.non_unital_non_assoc_semiring _; intros; exact e.apply_symm_apply _
/-- Transfer `non_unital_semiring` across an `equiv` -/
protected def non_unital_semiring [non_unital_semiring Ξ²] : non_unital_semiring Ξ± :=
let zero := e.has_zero, add := e.has_add, mul := e.has_mul in
by resetI; apply e.injective.non_unital_semiring _; intros; exact e.apply_symm_apply _
/-- Transfer `non_assoc_semiring` across an `equiv` -/
protected def non_assoc_semiring [non_assoc_semiring Ξ²] : non_assoc_semiring Ξ± :=
let zero := e.has_zero, add := e.has_add, one := e.has_one, mul := e.has_mul in
by resetI; apply e.injective.non_assoc_semiring _; intros; exact e.apply_symm_apply _
/-- Transfer `semiring` across an `equiv` -/
protected def semiring [semiring Ξ²] : semiring Ξ± :=
let zero := e.has_zero, add := e.has_add, one := e.has_one, mul := e.has_mul in
by resetI; apply e.injective.semiring _; intros; exact e.apply_symm_apply _
/-- Transfer `comm_semiring` across an `equiv` -/
protected def comm_semiring [comm_semiring Ξ²] : comm_semiring Ξ± :=
let zero := e.has_zero, add := e.has_add, one := e.has_one, mul := e.has_mul in
by resetI; apply e.injective.comm_semiring _; intros; exact e.apply_symm_apply _
/-- Transfer `ring` across an `equiv` -/
protected def ring [ring Ξ²] : ring Ξ± :=
let zero := e.has_zero, add := e.has_add, one := e.has_one, mul := e.has_mul, neg := e.has_neg,
sub := e.has_sub in
by resetI; apply e.injective.ring _; intros; exact e.apply_symm_apply _
/-- Transfer `comm_ring` across an `equiv` -/
protected def comm_ring [comm_ring Ξ²] : comm_ring Ξ± :=
let zero := e.has_zero, add := e.has_add, one := e.has_one, mul := e.has_mul, neg := e.has_neg,
sub := e.has_sub in
by resetI; apply e.injective.comm_ring _; intros; exact e.apply_symm_apply _
/-- Transfer `nonzero` across an `equiv` -/
protected theorem nontrivial [nontrivial Ξ²] : nontrivial Ξ± :=
e.surjective.nontrivial
/-- Transfer `is_domain` across an `equiv` -/
protected theorem is_domain [ring Ξ±] [ring Ξ²] [is_domain Ξ²] (e : Ξ± β+* Ξ²) : is_domain Ξ± :=
function.injective.is_domain e.to_ring_hom e.injective
/-- Transfer `division_ring` across an `equiv` -/
protected def division_ring [division_ring Ξ²] : division_ring Ξ± :=
let zero := e.has_zero, add := e.has_add, one := e.has_one, mul := e.has_mul, neg := e.has_neg,
sub := e.has_sub, inv := e.has_inv, div := e.has_div in
by resetI; apply e.injective.division_ring _; intros; exact e.apply_symm_apply _
/-- Transfer `field` across an `equiv` -/
protected def field [field Ξ²] : field Ξ± :=
let zero := e.has_zero, add := e.has_add, one := e.has_one, mul := e.has_mul, neg := e.has_neg,
sub := e.has_sub, inv := e.has_inv, div := e.has_div in
by resetI; apply e.injective.field _; intros; exact e.apply_symm_apply _
section R
variables (R : Type*)
include R
section
variables [monoid R]
/-- Transfer `mul_action` across an `equiv` -/
protected def mul_action (e : Ξ± β Ξ²) [mul_action R Ξ²] : mul_action R Ξ± :=
{ one_smul := by simp [smul_def],
mul_smul := by simp [smul_def, mul_smul],
..equiv.has_scalar e }
/-- Transfer `distrib_mul_action` across an `equiv` -/
protected def distrib_mul_action (e : Ξ± β Ξ²) [add_comm_monoid Ξ²] :
begin
letI := equiv.add_comm_monoid e,
exact Ξ [distrib_mul_action R Ξ²], distrib_mul_action R Ξ±
end :=
begin
intros,
letI := equiv.add_comm_monoid e,
exact (
{ smul_zero := by simp [zero_def, smul_def],
smul_add := by simp [add_def, smul_def, smul_add],
..equiv.mul_action R e } : distrib_mul_action R Ξ±)
end
end
section
variables [semiring R]
/-- Transfer `module` across an `equiv` -/
protected def module (e : Ξ± β Ξ²) [add_comm_monoid Ξ²] :
begin
letI := equiv.add_comm_monoid e,
exact Ξ [module R Ξ²], module R Ξ±
end :=
begin
introsI,
exact (
{ zero_smul := by simp [zero_def, smul_def],
add_smul := by simp [add_def, smul_def, add_smul],
..equiv.distrib_mul_action R e } : module R Ξ±)
end
/--
An equivalence `e : Ξ± β Ξ²` gives a linear equivalence `Ξ± ββ[R] Ξ²`
where the `R`-module structure on `Ξ±` is
the one obtained by transporting an `R`-module structure on `Ξ²` back along `e`.
-/
def linear_equiv (e : Ξ± β Ξ²) [add_comm_monoid Ξ²] [module R Ξ²] :
begin
letI := equiv.add_comm_monoid e,
letI := equiv.module R e,
exact Ξ± ββ[R] Ξ²
end :=
begin
introsI,
exact
{ map_smul' := Ξ» r x, by { apply e.symm.injective, simp, refl, },
..equiv.add_equiv e }
end
end
section
variables [comm_semiring R]
/-- Transfer `algebra` across an `equiv` -/
protected def algebra (e : Ξ± β Ξ²) [semiring Ξ²] :
begin
letI := equiv.semiring e,
exact Ξ [algebra R Ξ²], algebra R Ξ±
end :=
begin
introsI,
fapply ring_hom.to_algebra',
{ exact ((ring_equiv e).symm : Ξ² β+* Ξ±).comp (algebra_map R Ξ²), },
{ intros r x,
simp only [function.comp_app, ring_hom.coe_comp],
have p := ring_equiv_symm_apply e,
dsimp at p,
erw p, clear p,
apply (ring_equiv e).injective,
simp only [(ring_equiv e).map_mul],
simp [algebra.commutes], }
end
/--
An equivalence `e : Ξ± β Ξ²` gives an algebra equivalence `Ξ± ββ[R] Ξ²`
where the `R`-algebra structure on `Ξ±` is
the one obtained by transporting an `R`-algebra structure on `Ξ²` back along `e`.
-/
def alg_equiv (e : Ξ± β Ξ²) [semiring Ξ²] [algebra R Ξ²] :
begin
letI := equiv.semiring e,
letI := equiv.algebra R e,
exact Ξ± ββ[R] Ξ²
end :=
begin
introsI,
exact
{ commutes' := Ξ» r, by { apply e.symm.injective, simp, refl, },
..equiv.ring_equiv e }
end
end
end R
end instances
end equiv
namespace ring_equiv
protected lemma local_ring {A B : Type*} [comm_ring A] [local_ring A] [comm_ring B] (e : A β+* B) :
local_ring B :=
begin
haveI := e.symm.to_equiv.nontrivial,
refine @local_of_surjective A B _ _ _ _ e e.to_equiv.surjective,
end
end ring_equiv
|
07e526b6760794714d5baf3ba6f63a66cab23418 | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/combinatorics/set_family/lym.lean | 7734086928244cce66a49e6aae0a903a69fcc983 | [
"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 | 9,305 | lean | /-
Copyright (c) 2022 Bhavik Mehta, YaΓ«l Dillies. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Bhavik Mehta, Alena Gusakov, YaΓ«l Dillies
-/
import algebra.big_operators.ring
import combinatorics.double_counting
import combinatorics.set_family.shadow
import data.rat.order
import tactic.linarith
/-!
# Lubell-Yamamoto-Meshalkin inequality and Sperner's theorem
This file proves the local LYM and LYM inequalities as well as Sperner's theorem.
## Main declarations
* `finset.card_div_choose_le_card_shadow_div_choose`: Local Lubell-Yamamoto-Meshalkin inequality.
The shadow of a set `π` in a layer takes a greater proportion of its layer than `π` does.
* `finset.sum_card_slice_div_choose_le_one`: Lubell-Yamamoto-Meshalkin inequality. The sum of
densities of `π` in each layer is at most `1` for any antichain `π`.
* `is_antichain.sperner`: Sperner's theorem. The size of any antichain in `finset Ξ±` is at most the
size of the maximal layer of `finset Ξ±`. It is a corollary of `sum_card_slice_div_choose_le_one`.
## TODO
Prove upward local LYM.
Provide equality cases. Local LYM gives that the equality case of LYM and Sperner is precisely when
`π` is a middle layer.
`falling` could be useful more generally in grade orders.
## References
* http://b-mehta.github.io/maths-notes/iii/mich/combinatorics.pdf
* http://discretemath.imp.fu-berlin.de/DMII-2015-16/kruskal.pdf
## Tags
shadow, lym, slice, sperner, antichain
-/
open finset nat
open_locale big_operators finset_family
variables {π Ξ± : Type*} [linear_ordered_field π]
namespace finset
/-! ### Local LYM inequality -/
section local_lym
variables [decidable_eq Ξ±] [fintype Ξ±] {π : finset (finset Ξ±)} {r : β}
/-- The downward **local LYM inequality**, with cancelled denominators. `π` takes up less of `Ξ±^(r)`
(the finsets of card `r`) than `βπ` takes up of `Ξ±^(r - 1)`. -/
lemma card_mul_le_card_shadow_mul (hπ : (π : set (finset Ξ±)).sized r) :
π.card * r β€ (βπ).card * (fintype.card Ξ± - r + 1) :=
begin
refine card_mul_le_card_mul' (β) (Ξ» s hs, _) (Ξ» s hs, _),
{ rw [βhπ hs, βcard_image_of_inj_on s.erase_inj_on],
refine card_le_of_subset _,
simp_rw [image_subset_iff, mem_bipartite_below],
exact Ξ» a ha, β¨erase_mem_shadow hs ha, erase_subset _ _β© },
refine le_trans _ tsub_tsub_le_tsub_add,
rw [βhπ.shadow hs, βcard_compl, βcard_image_of_inj_on (insert_inj_on' _)],
refine card_le_of_subset (Ξ» t ht, _),
apply_instance,
rw mem_bipartite_above at ht,
have : β
β π,
{ rw [βmem_coe, hπ.empty_mem_iff, coe_eq_singleton],
rintro rfl,
rwa shadow_singleton_empty at hs },
obtain β¨a, ha, rflβ© :=
exists_eq_insert_iff.2 β¨ht.2, by rw [(sized_shadow_iff this).1 hπ.shadow ht.1, hπ.shadow hs]β©,
exact mem_image_of_mem _ (mem_compl.2 ha),
end
/-- The downward **local LYM inequality**. `π` takes up less of `Ξ±^(r)` (the finsets of card `r`)
than `βπ` takes up of `Ξ±^(r - 1)`. -/
lemma card_div_choose_le_card_shadow_div_choose (hr : r β 0) (hπ : (π : set (finset Ξ±)).sized r) :
(π.card : π) / (fintype.card Ξ±).choose r β€ (βπ).card / (fintype.card Ξ±).choose (r - 1) :=
begin
obtain hr' | hr' := lt_or_le (fintype.card Ξ±) r,
{ rw [choose_eq_zero_of_lt hr', cast_zero, div_zero],
exact div_nonneg (cast_nonneg _) (cast_nonneg _) },
replace hπ := card_mul_le_card_shadow_mul hπ,
rw div_le_div_iff; norm_cast,
{ cases r,
{ exact (hr rfl).elim },
rw nat.succ_eq_add_one at *,
rw [tsub_add_eq_add_tsub hr', add_tsub_add_eq_tsub_right] at hπ,
apply le_of_mul_le_mul_right _ (pos_iff_ne_zero.2 hr),
convert nat.mul_le_mul_right ((fintype.card Ξ±).choose r) hπ using 1,
{ simp [mul_assoc, nat.choose_succ_right_eq],
exact or.inl (mul_comm _ _) },
{ simp only [mul_assoc, choose_succ_right_eq, mul_eq_mul_left_iff],
exact or.inl (mul_comm _ _) } },
{ exact nat.choose_pos hr' },
{ exact nat.choose_pos (r.pred_le.trans hr') }
end
end local_lym
/-! ### LYM inequality -/
section lym
section falling
variables [decidable_eq Ξ±] (k : β) (π : finset (finset Ξ±))
/-- `falling k π` is all the finsets of cardinality `k` which are a subset of something in `π`. -/
def falling : finset (finset Ξ±) := π.sup $ powerset_len k
variables {π k} {s : finset Ξ±}
lemma mem_falling : s β falling k π β (β t β π, s β t) β§ s.card = k :=
by simp_rw [falling, mem_sup, mem_powerset_len, exists_and_distrib_right]
variables (π k)
lemma sized_falling : (falling k π : set (finset Ξ±)).sized k := Ξ» s hs, (mem_falling.1 hs).2
lemma slice_subset_falling : π # k β falling k π :=
Ξ» s hs, mem_falling.2 $ (mem_slice.1 hs).imp_left $ Ξ» h, β¨s, h, subset.refl _β©
lemma falling_zero_subset : falling 0 π β {β
} :=
subset_singleton_iff'.2 $ Ξ» t ht, card_eq_zero.1 $ sized_falling _ _ ht
lemma slice_union_shadow_falling_succ : π # k βͺ β (falling (k + 1) π) = falling k π :=
begin
ext s,
simp_rw [mem_union, mem_slice, mem_shadow_iff, exists_prop, mem_falling],
split,
{ rintro (h | β¨s, β¨β¨t, ht, hstβ©, hsβ©, a, ha, rflβ©),
{ exact β¨β¨s, h.1, subset.refl _β©, h.2β© },
refine β¨β¨t, ht, (erase_subset _ _).trans hstβ©, _β©,
rw [card_erase_of_mem ha, hs],
refl },
{ rintro β¨β¨t, ht, hstβ©, hsβ©,
by_cases s β π,
{ exact or.inl β¨h, hsβ© },
obtain β¨a, ha, hstβ© := ssubset_iff.1
(ssubset_of_subset_of_ne hst (ht.ne_of_not_mem h).symm),
refine or.inr β¨insert a s, β¨β¨t, ht, hstβ©, _β©, a, mem_insert_self _ _, erase_insert haβ©,
rw [card_insert_of_not_mem ha, hs] }
end
variables {π k}
/-- The shadow of `falling m π` is disjoint from the `n`-sized elements of `π`, thanks to the
antichain property. -/
lemma _root_.is_antichain.disjoint_slice_shadow_falling {m n : β}
(hπ : is_antichain (β) (π : set (finset Ξ±))) :
disjoint (π # m) (β (falling n π)) :=
disjoint_right.2 $ Ξ» s hβ hβ,
begin
simp_rw [mem_shadow_iff, exists_prop, mem_falling] at hβ,
obtain β¨s, β¨β¨t, ht, hstβ©, hsβ©, a, ha, rflβ© := hβ,
refine hπ (slice_subset hβ) ht _ ((erase_subset _ _).trans hst),
rintro rfl,
exact not_mem_erase _ _ (hst ha),
end
/-- A bound on any top part of the sum in LYM in terms of the size of `falling k π`. -/
lemma le_card_falling_div_choose [fintype Ξ±] (hk : k β€ fintype.card Ξ±)
(hπ : is_antichain (β) (π : set (finset Ξ±))) :
β r in range (k + 1),
((π # (fintype.card Ξ± - r)).card : π) / (fintype.card Ξ±).choose (fintype.card Ξ± - r)
β€ (falling (fintype.card Ξ± - k) π).card / (fintype.card Ξ±).choose (fintype.card Ξ± - k) :=
begin
induction k with k ih,
{ simp only [tsub_zero, cast_one, cast_le, sum_singleton, div_one, choose_self, range_one],
exact card_le_of_subset (slice_subset_falling _ _) },
rw succ_eq_add_one at *,
rw [sum_range_succ, βslice_union_shadow_falling_succ,
card_disjoint_union hπ.disjoint_slice_shadow_falling, cast_add, _root_.add_div, add_comm],
rw [βtsub_tsub, tsub_add_cancel_of_le (le_tsub_of_add_le_left hk)],
exact add_le_add_left ((ih $ le_of_succ_le hk).trans $ card_div_choose_le_card_shadow_div_choose
(tsub_pos_iff_lt.2 $ nat.succ_le_iff.1 hk).ne' $ sized_falling _ _) _,
end
end falling
variables {π : finset (finset Ξ±)} {s : finset Ξ±} {k : β}
/-- The **Lubell-Yamamoto-Meshalkin inequality**. If `π` is an antichain, then the sum of the
proportion of elements it takes from each layer is less than `1`. -/
lemma sum_card_slice_div_choose_le_one [fintype Ξ±] (hπ : is_antichain (β) (π : set (finset Ξ±))) :
β r in range (fintype.card Ξ± + 1), ((π # r).card : π) / (fintype.card Ξ±).choose r β€ 1 :=
begin
classical,
rw βsum_flip,
refine (le_card_falling_div_choose le_rfl hπ).trans _,
rw div_le_iff; norm_cast,
{ simpa only [nat.sub_self, one_mul, nat.choose_zero_right, falling]
using (sized_falling 0 π).card_le },
{ rw [tsub_self, choose_zero_right],
exact zero_lt_one }
end
end lym
/-! ### Sperner's theorem -/
/-- **Sperner's theorem**. The size of an antichain in `finset Ξ±` is bounded by the size of the
maximal layer in `finset Ξ±`. This precisely means that `finset Ξ±` is a Sperner order. -/
lemma _root_.is_antichain.sperner [fintype Ξ±] {π : finset (finset Ξ±)}
(hπ : is_antichain (β) (π : set (finset Ξ±))) :
π.card β€ (fintype.card Ξ±).choose (fintype.card Ξ± / 2) :=
begin
classical,
suffices : β r in Iic (fintype.card Ξ±),
((π # r).card : β) / (fintype.card Ξ±).choose (fintype.card Ξ± / 2) β€ 1,
{ rwa [βsum_div, βnat.cast_sum, div_le_one, cast_le, sum_card_slice] at this,
norm_cast,
exact choose_pos (nat.div_le_self _ _) },
rw [Iic_eq_Icc, βIco_succ_right, bot_eq_zero, Ico_zero_eq_range],
refine (sum_le_sum $ Ξ» r hr, _).trans (sum_card_slice_div_choose_le_one hπ),
rw mem_range at hr,
refine div_le_div_of_le_left _ _ _; norm_cast,
{ exact nat.zero_le _ },
{ exact choose_pos (lt_succ_iff.1 hr) },
{ exact choose_le_middle _ _ }
end
end finset
|
e76855b40319fc7b434a862561cf8aa954669e05 | 94637389e03c919023691dcd05bd4411b1034aa5 | /src/inClassNotes/propositions/propositions_as_types_the_idea.lean | 4260f391a7a6affde213b3ade617c115eada45f7 | [] | no_license | kevinsullivan/complogic-s21 | 7c4eef2105abad899e46502270d9829d913e8afc | 99039501b770248c8ceb39890be5dfe129dc1082 | refs/heads/master | 1,682,985,669,944 | 1,621,126,241,000 | 1,621,126,241,000 | 335,706,272 | 0 | 38 | null | 1,618,325,669,000 | 1,612,374,118,000 | Lean | UTF-8 | Lean | false | false | 2,867 | lean | /-
Propositions as types: the very idea.
-/
/-
Idea #1. Use types to represent
logical propositions and values of
these types to represent "evidence"
or "proofs" of truth. A type that
is inhabited we then judge to be
logically true, and one that is
uninhabited we judge to be false.
-/
/-
Idea #2. When we use values of a
type only to represent evidence
of logical truth, then it doesn't
really matter *which* value of a
type we pick as evidence. To the
first order, we don't care which
proof of a theorem a mathematician
produces, only that there is at
least one. And indeed, any proof
will do to justify a judgment that
a proposition is true. So when we
represent propositions as types and
proofs as values, in some sense we
want every proof of a given type to
be equivalent to any other. We want
every value of a "logical" type to
be equivalent to any other. Making
this equivalence of proof terms is
one of the main purposes of the last
of Lean's type universes, which we
now introduce: Prop.
-/
inductive its_raining : Prop -- not quite right
| i_see_rain_falling : its_raining
| i_hear_rain_on_roof : its_raining
inductive streets_wet : Prop
| i_see_wet_streets : streets_wet
| i_feel_wet_streets : streets_wet
open its_raining streets_wet
def proof_1'' : its_raining := i_see_rain_falling
def proof_2'' : its_raining := i_hear_rain_on_roof
lemma proof_1' : its_raining := i_see_rain_falling
lemma proof_2' : its_raining := i_hear_rain_on_roof
theorem proof_1 : its_raining := i_see_rain_falling
theorem proof_2 : its_raining := i_hear_rain_on_roof
/-
We just don't care which value of a
propositional type is used. Values
build by different constructors in
Type n, are always NOT equal. This
is the idea that "constructors are
injective and disjoint."
-/
/-
Prop Sort 0
Type 0 Sort 1
Type 1 Sort 2
Type 2 Sort 3
... ...
-/
-- Change Type to Prop above!
/-
Just as we can represent propositions
as types, we can represent *predicates*
as type families indexed by arguments,
i.e., as "propositions with parameters."
-/
inductive day : Type
| su | mo | tu | we | th | fr | sa
open day
/-
A type family indexed by day representing
the predicate β¨ is_always_rainy d β©, where
d is a parameter of type day. Providing a
value for d yields a specific proposition
in the family of propositions we've defined,
namely one claims that a specific day, d, is
always rainy.
-/
inductive is_always_rainy : day β Sort 0
| mo_rainy : β (d :day), (d = mo) β is_always_rainy d
open is_always_rainy
#check is_always_rainy
#check is_always_rainy su
#check is_always_rainy mo
#check is_always_rainy tu
lemma bad_tuesdays : is_always_rainy tu := mo_rainy _ _ -- stuck
lemma bad_fridays : is_always_rainy fr := mo_rainy _ _ -- stuck
lemma bad_mondays : is_always_rainy mo := mo_rainy mo (eq.refl mo)
|
4c6eab007431f36850f9d753823ef9685329421e | de4548698671d50981659ecc9f4910de15969d3d | /Metamath/Verify.lean | ba124670a9e2d55a18a7c64175435e9410a688b5 | [] | no_license | digama0/mm-lean4 | 7ad17c81853816c6cd4bb97b8abe4bea0fd35ff6 | 6a427edecb851cec04818848a755c0145a5f2e98 | refs/heads/master | 1,688,934,520,262 | 1,687,937,043,000 | 1,687,937,043,000 | 365,257,017 | 15 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 26,335 | lean | import Lean.Data.HashMap
import Lean.Data.HashSet
import Std.Data.Nat.Lemmas
theorem Fin.val_eq_of_lt : a < n.succ β (@Fin.ofNat n a).val = a := Nat.mod_eq_of_lt
theorem UInt32.val_eq_of_lt : a < UInt32.size β (UInt32.ofNat a).val = a := Fin.val_eq_of_lt
theorem toChar_aux (n : Nat) (h : n < UInt8.size) : Nat.isValidChar (UInt32.ofNat n).1 := by
rw [UInt32.val_eq_of_lt]
exact Or.inl (Nat.lt_trans h (by decide))
exact Nat.lt_trans h (by decide)
def UInt8.toChar (n : UInt8) : Char := β¨n.toUInt32, toChar_aux n.1 n.1.2β©
def Char.toUInt8 (n : Char) : UInt8 := n.1.toUInt8
theorem Char.utf8Size_pos (c : Char) : 0 < c.utf8Size :=
let foo {c} {t e : UInt32} : [Decidable c] β 0 < t β 0 < e β 0 < ite c t e
| Decidable.isTrue _, h, _ => h
| Decidable.isFalse _, _, h => h
foo (by decide) $ foo (by decide) $ foo (by decide) (by decide)
theorem String.csize_pos : (c : Char) β 0 < String.csize c := Char.utf8Size_pos
namespace UInt8
def isUpper (c : UInt8) : Bool :=
c β₯ 65 && c β€ 90
def isLower (c : UInt8) : Bool :=
c β₯ 97 && c β€ 122
def isAlpha (c : UInt8) : Bool :=
c.isUpper || c.isLower
def isDigit (c : UInt8) : Bool :=
c β₯ 48 && c β€ 57
def isAlphanum (c : UInt8) : Bool :=
c.isAlpha || c.isDigit
end UInt8
structure ByteSliceT where
arr : ByteArray
off : Nat
namespace ByteSliceT
@[inline] def size (self : ByteSliceT) : Nat := self.arr.size - self.off
instance : GetElem ByteSliceT Nat UInt8 fun _ _ => True where
getElem self idx _ := self.arr.get! (self.off + idx)
end ByteSliceT
def ByteArray.toSliceT (arr : ByteArray) : ByteSliceT := β¨arr, 0β©
structure ByteSlice where
arr : ByteArray
off : Nat
len : Nat
namespace ByteSlice
def toArray : ByteSlice β ByteArray
| β¨arr, off, lenβ© => arr.extract off len
instance : GetElem ByteSlice Nat UInt8 fun _ _ => True where
getElem self idx _ := self.arr.get! (self.off + idx)
def forIn.loop [Monad m] (f : UInt8 β Ξ² β m (ForInStep Ξ²))
(arr : ByteArray) (off stop : Nat) (i : Nat) (b : Ξ²) : m Ξ² := do
if i < stop then
match β f (arr.get! i) b with
| ForInStep.done b => pure b
| ForInStep.yield b => loop f arr off stop (i+1) b
else pure b
termination_by _ => stop - i
instance : ForIn m ByteSlice UInt8 :=
β¨fun β¨arr, off, lenβ© b f => forIn.loop f arr off (off + len) off bβ©
end ByteSlice
def ByteSliceT.toSlice : ByteSliceT β ByteSlice
| β¨arr, offβ© => β¨arr, off, arr.size - offβ©
def ByteArray.toSlice (arr : ByteArray) : ByteSlice := β¨arr, 0, arr.sizeβ©
def ByteSlice.eqArray (bs : ByteSlice) (arr : ByteArray) : Bool :=
let rec loop (arrβ : ByteArray) (i j : Nat) : Bool :=
if j < arr.size then
arrβ.get! i == arr.get! j && loop arrβ (i+1) (j+1)
else true
bs.len == arr.size && loop bs.arr bs.off 0
termination_by loop => arr.size - j
def String.toAscii (s : String) : ByteArray :=
let rec loop (out : ByteArray) (p : Pos) : ByteArray :=
if h : s.atEnd p then out else
let c := s.get p
have := Nat.sub_lt_sub_left (Nat.gt_of_not_le (mt decide_eq_true h)) (lt_next s _)
loop (out.push c.toUInt8) (s.next p)
loop ByteArray.empty 0
termination_by loop => s.endPos.1 - p.1
def ByteSlice.toString (bs : ByteSlice) : String := Id.run do
let mut s := ""
for c in bs do s := s.push c.toChar
s
instance : ToString ByteSlice where
toString bs := Id.run do
let mut s := ""
for c in bs do s := s.push c.toChar
s
namespace Metamath
namespace Verify
open IO.FS (Handle)
open Lean (HashMap HashSet)
def isLabelChar (c : UInt8) : Bool :=
c.isAlphanum || c == '-'.toUInt8 || c == '_'.toUInt8 || c == '.'.toUInt8
def isWhitespace (c : UInt8) : Bool :=
c == ' '.toUInt8 || c == '\n'.toUInt8 || c == '\r'.toUInt8 || c == '\t'.toUInt8
def isPrintable (c : UInt8) : Bool := c >= 32 && c <= 126
def isMathChar (c : UInt8) : Bool := c β '$'.toUInt8 && isPrintable c
def toLabel (bs : ByteSlice) : Bool Γ String := Id.run do
let mut ok := true
let mut s := ""
for c in bs do
s := s.push c.toChar
unless isLabelChar c do ok := false
(ok, s)
def toMath (bs : ByteSlice) : Bool Γ String := Id.run do
let mut ok := true
let mut s := ""
for c in bs do
s := s.push c.toChar
unless isPrintable c do ok := false
(ok, s)
structure Pos := (line col : Nat)
instance : ToString Pos := β¨fun β¨l, cβ© => s!"{l}:{c}"β©
def DJ := String Γ String
instance : BEq DJ := instBEqProd
structure Frame where
dj : Array DJ
hyps : Array String
deriving Inhabited
def Frame.size : Frame β Nat Γ Nat
| β¨dj, hypsβ© => (dj.size, hyps.size)
def Frame.shrink : Frame β Nat Γ Nat β Frame
| β¨dj, hypsβ©, (x, y) => β¨dj.shrink x, hyps.shrink yβ©
instance : ToString Frame := β¨fun fr => toString fr.hypsβ©
inductive Sym
| const (c : String)
| var (v : String)
deriving Inhabited
def Sym.isVar : Sym β Bool
| const _ => false
| var _ => true
def Sym.value : Sym β String
| const c => c
| var v => v
instance : BEq Sym := β¨fun a b => a.value == b.valueβ©
abbrev Formula := Array Sym
instance : ToString Formula where
toString f := Id.run do
let s := f[0]!.value
f.foldl (init := s) (start := 1) fun (s:String) v =>
s ++ " " ++ v.value
def Formula.subst (Ο : HashMap String Formula) (f : Formula) : Except String Formula := do
let mut f' := #[]
for c in f do
match c with
| Sym.const _ => f' := f'.push c
| Sym.var v =>
match Ο.find? v with
| none => throw s!"variable {v} not found"
| some e => f' := e.foldl Array.push f' 1
pure f'
def Formula.foldlVars (self : Formula) (init : Ξ±) (f : Ξ± β String β Ξ±) : Ξ± :=
self.foldl (init := init) (start := 1) fun a v =>
match v with
| Sym.var v => f a v
| _ => a
inductive Object
| const : String β Object
| var : String β Object
| hyp : Bool β Formula β String β Object
| assert : Formula β Frame β String β Object
inductive ProofTokenParser
| start
| preload
| normal
| compressed (chr : Nat)
inductive HeapEl
| fmla (f : Formula)
| assert (f : Formula) (fr : Frame)
instance : ToString HeapEl where
toString
| HeapEl.fmla f => toString f
| HeapEl.assert f fr => s!"{fr} |- {f}"
structure ProofState where
pos : Pos
label : String
fmla : Formula
frame : Frame
heap : Array HeapEl
stack : Array Formula
ptp : ProofTokenParser
instance : ToString ProofState where
toString p := Id.run do
let mut s := s!"at {p.pos}: {p.label}\n"
let mut i := 0
for el in p.heap do
s := s ++ s!"heap {i} := {el}\n"
i := i + 1
s := s ++ "\n"
for el in p.stack do
s := s ++ s!"{el}\n"
s
namespace ProofState
def push (pr : ProofState) (f : Formula) : ProofState :=
{ pr with stack := pr.stack.push f }
def pushHeap (pr : ProofState) (el : HeapEl) : ProofState :=
{ pr with heap := pr.heap.push el }
def save (pr : ProofState) : Except String ProofState :=
if pr.stack.isEmpty then
throw "can't save empty stack"
else
let f := pr.stack.back
pure <| pr.pushHeap (HeapEl.fmla f)
end ProofState
inductive Error
| error (pos : Pos) (msg : String)
| ax (pos : Pos) (l : String) (f : Formula) (fr : Frame)
| thm (pos : Pos) (l : String) (f : Formula) (fr : Frame)
structure Interrupt :=
e : Error
idx : Nat
structure DB where
frame : Frame
scopes : Array (Nat Γ Nat)
objects : HashMap String Object
interrupt : Bool
error? : Option Interrupt
deriving Inhabited
namespace DB
@[inline] def error (s : DB) : Bool := s.error?.isSome
def mkError (s : DB) (pos : Pos) (msg : String) : DB :=
{ s with error? := some β¨Error.error pos msg, defaultβ© }
def pushScope (s : DB) : DB :=
{ s with scopes := s.scopes.push s.frame.size }
def popScope (pos : Pos) (db : DB) : DB :=
if db.scopes.isEmpty then
db.mkError pos "can't pop global scope"
else
{ db with frame := db.frame.shrink db.scopes.back, scopes := db.scopes.pop }
def find? (db : DB) (l : String) : Option Object := db.objects.find? l
def isConst (db : DB) (tk : String) : Bool :=
if let some (Object.const _) := db.find? tk then true else false
def isVar (db : DB) (tk : String) : Bool :=
if let some (Object.var _) := db.find? tk then true else false
def isSym (db : DB) (tk : String) : Bool :=
match db.find? tk with
| some (Object.const _) => true
| some (Object.var _) => true
| _ => false
@[inline] def withFrame (f : Frame β Frame) (db : DB) : DB :=
{ db with frame := f db.frame }
@[inline] def withDJ (f : Array DJ β Array DJ) (db : DB) : DB :=
db.withFrame fun β¨dj, hypsβ© => β¨f dj, hypsβ©
@[inline] def withHyps (f : Array String β Array String) (db : DB) : DB :=
db.withFrame fun β¨dj, hypsβ© => β¨dj, f hypsβ©
def insert (db : DB) (pos : Pos) (l : String) (obj : String β Object) : DB :=
if let some o := db.find? l then
let ok : Bool := match o with
| Object.var _ => if let Object.var _ := obj l then true else false
| _ => false
if ok then db else db.mkError pos s!"duplicate symbol/assert {l}"
else
{ db with objects := db.objects.insert l (obj l) }
def insertHyp (db : DB) (pos : Pos) (l : String) (ess : Bool) (f : Formula) : DB :=
let db := db.insert pos l (Object.hyp ess f)
db.withHyps fun hyps => hyps.push l
def trimFrame (db : DB) (fmla : Formula) (fr := db.frame) : Bool Γ Frame := Id.run do
let collectVars (fmla : Formula) vars :=
fmla.foldlVars vars HashSet.insert
let mut vars : HashSet String := collectVars fmla HashSet.empty
for l in fr.hyps do
if let some (Object.hyp true f _) := db.find? l then
vars := collectVars f vars
let mut dj := #[]
for v in fr.dj do
if vars.contains v.1 && vars.contains v.2 then
dj := dj.push v
let mut hyps := #[]
let mut inHyps := false
let mut ok := true
for l in fr.hyps do
let ess β
if let some (Object.hyp false f _) := db.find? l then
if inHyps then ok := false
vars.contains f[1]!.value
else
inHyps := true
true
if ess then hyps := hyps.push l
(ok, β¨dj, hypsβ©)
def trimFrame' (db : DB) (fmla : Formula) : Except String Frame :=
let (ok, fr) := db.trimFrame fmla
if ok then pure fr
else throw s!"out of order hypotheses in frame"
def insertAxiom (db : DB) (pos : Pos) (l : String) (fmla : Formula) : DB :=
match db.trimFrame' fmla with
| Except.ok fr =>
if db.interrupt then { db with error? := some β¨Error.ax pos l fmla fr, defaultβ© }
else db.insert pos l (Object.assert fmla fr)
| Except.error msg => db.mkError pos msg
def mkProofState (db : DB) (pos : Pos) (l : String) (fmla : Formula) (fr : Frame) :
ProofState := Id.run do
let mut heap := #[]
for l in fr.hyps do
if let some (Object.hyp _ f _) := db.find? l then
heap := heap.push (HeapEl.fmla f)
β¨pos, l, fmla, fr, heap, #[], ProofTokenParser.startβ©
def preload (db : DB) (pr : ProofState) (l : String) : Except String ProofState :=
match db.find? l with
| some (Object.hyp true _ _) => throw "$e found in paren list"
| some (Object.hyp _ f _) => return pr.pushHeap (HeapEl.fmla f)
| some (Object.assert f fr _) => return pr.pushHeap (HeapEl.assert f fr)
| _ => throw s!"statement {l} not found"
@[inline] def checkHypF (db : DB) (hyps : Array String) (stack : Array Formula)
(off : {off // off + hyps.size = stack.size})
(IH : HashMap String Formula β Except String (HashMap String Formula))
(i : Nat) (h : i < hyps.size)
(subst : HashMap String Formula) : Except String (HashMap String Formula) := do
let val := stack.get β¨off.1 + i,
let thm {a b n} : i < a β n + a = b β n + i < b
| h, rfl => Nat.add_lt_add_left h _
thm h off.2β©
if let some (Object.hyp ess f _) := db.find? (hyps.get β¨i, hβ©) then
if f[0]! == val[0]! then
if ess then
if (β f.subst subst) == val then
IH subst
else throw "type error in substitution"
else
IH (subst.insert f[1]!.value val)
else throw s!"bad typecode in substitution {hyps[i]}: {f} / {val}"
else unreachable!
variable (db : DB) (hyps : Array String) (stack : Array Formula)
(off : {off // off + hyps.size = stack.size}) in
def checkHyp (i : Nat) (subst : HashMap String Formula) :
Except String (HashMap String Formula) := do
if h : i < hyps.size then
let val := stack.get β¨off.1 + i,
let thm {a b n} : i < a β n + a = b β n + i < b
| h, rfl => Nat.add_lt_add_left h _
thm h off.2β©
if let some (Object.hyp ess f _) := db.find? (hyps.get β¨i, hβ©) then
if f[0]! == val[0]! then
if ess then
if (β f.subst subst) == val then
checkHyp (i+1) subst
else throw "type error in substitution"
else
checkHyp (i+1) (subst.insert f[1]!.value val)
else throw s!"bad typecode in substitution {hyps[i]}: {f} / {val}"
else unreachable!
else pure subst
termination_by _ => hyps.size - i
def stepAssert (db : DB) (pr : ProofState) (f : Formula) : Frame β Except String ProofState
| β¨dj, hypsβ© => do
if h : hyps.size β€ pr.stack.size then
let off : {off // off + hyps.size = pr.stack.size} :=
β¨pr.stack.size - hyps.size, Nat.sub_add_cancel hβ©
let subst β checkHyp db hyps pr.stack off 0 HashMap.empty
let disj s1 s2 := s1 != s2 &&
db.frame.dj.contains (if s1 < s2 then (s1, s2) else (s2, s1))
for (v1, v2) in dj do
let e1 := subst.find! v1
let e2 := subst.find! v2
let disjoint :=
e1.foldlVars (init := true) fun b s1 =>
e2.foldlVars b fun b s2 => b && disj s1 s2
if !disjoint then throw "disjoint variable violation"
let concl β f.subst subst
pure { pr with stack := (pr.stack.shrink off).push concl }
else throw "stack underflow"
def stepNormal (db : DB) (pr : ProofState) (l : String) : Except String ProofState :=
match db.find? l with
| some (Object.hyp _ f _) => return pr.push f
| some (Object.assert f fr _) => db.stepAssert pr f fr
| _ => throw s!"statement {l} not found"
def stepProof (db : DB) (pr : ProofState) (i : Nat) : Except String ProofState :=
match pr.heap.get? i with
| none => throw "proof backref index out of range"
| some (HeapEl.fmla f) => return pr.push f
| some (HeapEl.assert f fr) => db.stepAssert pr f fr
end DB
inductive CharParser
| ws : CharParser
| token : Nat β ByteSliceT β CharParser
deriving Inhabited
inductive TokensKind
| float
| ess
| ax
| thm
open TokensKind in
instance : ToString TokensKind where
toString
| float => "float"
| ess => "ess"
| ax => "ax"
| thm => "thm"
def TokensKind.delim
| thm => "$=".toAscii
| _ => "$.".toAscii
structure TokensParser where
k : TokensKind
pos : Pos
label : String
instance : ToString TokensParser where
toString | β¨k, pos, labelβ© => s!"at {pos}: {k} {label}"
inductive TokenParser
| start : TokenParser
| comment : TokenParser β TokenParser
| const : TokenParser
| var : TokenParser
| djvars : Array String β TokenParser
| math : Array Sym β TokensParser β TokenParser
| label : Pos β String β TokenParser
| proof : ProofState β TokenParser
deriving Inhabited
def TokenParser.toString : TokenParser β String
| start => "start"
| comment p => "comment " ++ toString p
| const => "const"
| var => "var"
| djvars s => s!"djvars {s}"
| math s p => s!"math {s} {p}"
| label pos l => s!"at {pos}: ? {l}"
| proof p => ToString.toString p
instance : ToString TokenParser := β¨TokenParser.toStringβ©
structure ParserState where
db : DB
tokp : TokenParser
charp : CharParser
line : Nat
linepos : Nat
deriving Inhabited
namespace ParserState
@[inline] def withDB (f : DB β DB) (s : ParserState) : ParserState :=
{ s with db := f s.db }
def mkPos (s : ParserState) (pos : Nat) : Pos := β¨s.line, pos - s.lineposβ©
def mkError (s : ParserState) (pos : Pos) (msg : String) : ParserState :=
s.withDB fun db => db.mkError pos msg
def mkErrorAt (s : ParserState) (pos : Pos) (l msg : String) : ParserState :=
s.mkError pos s!"at {l}: {msg}"
def withAt (l : String) (f : Unit β ParserState) : ParserState :=
let s := f ()
if let some β¨Error.error pos msg, iβ© := s.db.error? then
s.withDB fun db => { db with error? := some β¨Error.error pos s!"at {l}: {msg}", iβ© }
else s
def label (s : ParserState) (pos : Pos) (tk : ByteSlice) : ParserState :=
let (ok, tk) := toLabel tk
if ok then { s with tokp := TokenParser.label pos tk }
else s.mkError pos s!"invalid label '{tk}'"
def withMath (s : ParserState) (pos : Pos) (tk : ByteSlice)
(f : ParserState β String β ParserState) : ParserState :=
let (ok, tk) := toMath tk
if !ok then s.mkError pos s!"invalid math string '{tk}'" else
f s tk
def sym (s : ParserState) (pos : Pos) (tk : ByteSlice) (f : String β Object) : ParserState :=
if tk.eqArray "$.".toAscii then
{ s with tokp := TokenParser.start }
else s.withMath pos tk fun s tk =>
s.withDB fun db => db.insert pos tk f
def resumeAxiom (s : ParserState)
(pos : Pos) (l : String) (fmla : Formula) (fr : Frame) : ParserState :=
s.withDB fun db => db.insert pos l (Object.assert fmla fr)
def resumeThm (s : ParserState)
(pos : Pos) (l : String) (fmla : Formula) (fr : Frame) : ParserState :=
let pr := s.db.mkProofState pos l fmla fr
{ s with tokp := TokenParser.proof pr }
def feedTokens (s : ParserState) (arr : Array Sym) : TokensParser β ParserState
| β¨k, pos, lβ© => withAt l fun _ => Id.run do
unless arr.size > 0 && !arr[0]!.isVar do
return s.mkError pos "first symbol is not a constant"
match k with
| TokensKind.float =>
unless arr.size == 2 && arr[1]!.isVar do
return s.mkError pos "expected a constant and a variable"
let s := s.withDB fun db => db.insertHyp pos l false arr
pure { s with tokp := TokenParser.start }
| TokensKind.ess =>
let s := s.withDB fun db => db.insertHyp pos l true arr
pure { s with tokp := TokenParser.start }
| TokensKind.ax =>
let s := s.withDB fun db => db.insertAxiom pos l arr
pure { s with tokp := TokenParser.start }
| TokensKind.thm =>
match s.db.trimFrame' arr with
| Except.ok fr =>
if s.db.interrupt then
s.withDB fun db => { db with error? := some β¨Error.thm pos l arr fr, defaultβ© }
else s.resumeThm pos l arr fr
| Except.error msg => s.mkError pos msg
def feedProof (s : ParserState) (tk : ByteSlice) (pr : ProofState) : ParserState :=
withAt pr.label fun _ =>
match go pr with
| Except.ok pr => { s with tokp := TokenParser.proof pr }
| Except.error msg => s.mkError pr.pos msg
where
goNormal (pr : ProofState) :=
let (ok, tk) := toLabel tk
if ok then s.db.stepNormal pr tk
else throw s!"invalid label '{tk}'"
go (pr : ProofState) : Except String ProofState := do
match pr.ptp with
| ProofTokenParser.start =>
if tk.eqArray "(".toAscii then
pure { pr with ptp := ProofTokenParser.preload }
else goNormal { pr with ptp := ProofTokenParser.normal }
| ProofTokenParser.preload =>
if tk.eqArray ")".toAscii then
pure { pr with ptp := ProofTokenParser.compressed 0 }
else
let (ok, tk) := toLabel tk
if ok then s.db.preload pr tk
else throw s!"invalid label '{tk}'"
| ProofTokenParser.normal => goNormal pr
| ProofTokenParser.compressed chr =>
let mut pr := pr
let mut chr := chr
for c in tk do
if 'A'.toUInt8 β€ c && c β€ 'Z'.toUInt8 then
if c β€ 'T'.toUInt8 then
let n := 20 * chr + (c - 'A'.toUInt8).1
pr β s.db.stepProof pr n
chr := 0
else if c < 'Z'.toUInt8 then
chr := 5 * chr + (c - 'T'.toUInt8).1
else
pr β pr.save
chr := 0
else if c = '?'.toUInt8 then
throw "proof contains '?'"
else
throw "proof parse error"
pure { pr with ptp := ProofTokenParser.compressed chr }
def finishProof (s : ParserState) : ProofState β ParserState
| β¨pos, l, fmla, fr, _, stack, ptpβ© => withAt l fun _ => Id.run do
let s := { s with tokp := TokenParser.start }
match ptp with
| ProofTokenParser.compressed 0 => ()
| ProofTokenParser.normal => ()
| _ => return s.mkError pos "proof parse error"
unless stack.size == 1 do
return s.mkError pos "more than one element on stack"
unless stack[0]! == fmla do
return s.mkError pos "theorem does not prove what it claims"
s.withDB fun db => db.insert pos l (Object.assert fmla fr)
def feedToken (s : ParserState) (pos : Nat) (tk : ByteSlice) : ParserState :=
let pos := s.mkPos pos
match s.tokp with
| TokenParser.comment p =>
if tk.eqArray "$)".toAscii then { s with tokp := p } else s
| p =>
if tk.eqArray "$(".toAscii then { s with tokp := p.comment } else
match p with
| TokenParser.comment _ => unreachable!
| TokenParser.start =>
if tk.len == 2 && tk[0] == '$'.toUInt8 then
match tk[1].toChar with
| '{' => s.withDB DB.pushScope
| '}' => s.withDB (DB.popScope pos)
| 'c' => { s with tokp := TokenParser.const }
| 'v' => { s with tokp := TokenParser.var }
| 'd' => { s with tokp := TokenParser.djvars #[] }
| _ => s.label pos tk
else s.label pos tk
| TokenParser.const => s.sym pos tk Object.const
| TokenParser.var => s.sym pos tk Object.var
| TokenParser.djvars arr =>
if tk.eqArray "$.".toAscii then { s with tokp := TokenParser.start } else
s.withMath pos tk fun s tk => Id.run do
unless s.db.isVar tk do return s.mkError pos s!"{tk} is not a variable"
let mut s := s
for tk1 in arr do
if tk1 == tk then
return s.mkError pos s!"duplicate disjoint variable {tk}"
let p := if tk1 < tk then (tk1, tk) else (tk, tk1)
s := s.withDB fun db => db.withDJ fun dj => dj.push p
{ s with tokp := TokenParser.djvars (arr.push tk) }
| TokenParser.math arr p =>
if tk.eqArray p.k.delim then
s.feedTokens arr p
else
s.withMath pos tk fun s tk => Id.run do
let tk β match s.db.find? tk with
| some (Object.const _) => Sym.const tk
| some (Object.var _) => Sym.var tk
| _ => return s.mkError pos s!"{tk} is not a constant or variable"
{ s with tokp := TokenParser.math (arr.push tk) p }
| TokenParser.label pos lab =>
if tk.len == 2 && tk[0] == '$'.toUInt8 then
let go (s : ParserState) (k : TokensKind) :=
{ s with tokp := TokenParser.math #[] β¨k, pos, labβ© }
match tk[1].toChar with
| 'f' => go s TokensKind.float
| 'e' => go s TokensKind.ess
| 'a' => go s TokensKind.ax
| 'p' => go s TokensKind.thm
| _ => s.mkError pos s!"unknown statement type {(toLabel tk).2}"
else s.mkError pos s!"unknown statement type {(toLabel tk).2}"
| TokenParser.proof pr =>
let s := { s with tokp := default }
if tk.eqArray "$.".toAscii then s.finishProof pr
else s.feedProof tk pr
inductive OldToken
| this (off : Nat)
| old (base off : Nat) (arr : ByteArray)
inductive FeedState
| ws : FeedState
| token : OldToken β FeedState
def updateLine (s : ParserState) (i : Nat) (c : UInt8) : ParserState :=
if c == '\n'.toUInt8 then { s with line := s.line + 1, linepos := i + 1 } else s
def feed (base : Nat) (arr : ByteArray)
(i : Nat) (rs : FeedState) (s : ParserState) : ParserState :=
if h : i < arr.size then
let c := arr.get β¨i, hβ©
if isWhitespace c then
match rs with
| FeedState.ws =>
let s := s.updateLine (base + i) c
feed base arr (i+1) FeedState.ws s
| FeedState.token ot =>
let s := match ot with
| OldToken.this off => s.feedToken (base + off) β¨arr, off, i - offβ©
| OldToken.old base off arr' => s.feedToken (base + off)
β¨arr.copySlice 0 arr' arr'.size i false, off, arr'.size - off + iβ©
let s : ParserState := s.updateLine (base + i) c
if let some β¨e, _β© := s.db.error? then
{ s with db := { s.db with error? := some β¨e, i+1β© } }
else feed base arr (i+1) FeedState.ws s
else
let rs := if let FeedState.ws := rs then FeedState.token (OldToken.this i) else rs
feed base arr (i+1) rs s
else
{ s with charp :=
match rs with
| FeedState.ws => CharParser.ws
| FeedState.token ot =>
match ot with
| OldToken.this off => CharParser.token base β¨arr, offβ©
| OldToken.old base off arr' => CharParser.token base β¨arr' ++ arr, offβ© }
termination_by _ => arr.size - i
def feedAll (s : ParserState) (base : Nat) (arr : ByteArray) : ParserState :=
match s.charp with
| CharParser.ws => s.feed base arr 0 FeedState.ws
| CharParser.token base' β¨arr', offβ© =>
let s := { s with charp := default }
s.feed base arr 0 (FeedState.token (OldToken.old base' off arr'))
def done (s : ParserState) (base : Nat) : DB := Id.run do
let mut s := s
if let CharParser.token pos tk := s.charp then
s := s.feedToken pos tk.toSlice
let base := s.mkPos base
let { db := db, tokp := tokp, ..} := s
match tokp with
| TokenParser.start => db
| TokenParser.comment _ => db.mkError base "unclosed comment"
| TokenParser.const => db.mkError base "unclosed $c"
| TokenParser.var => db.mkError base "unclosed $v"
| TokenParser.djvars _ => db.mkError base "unclosed $d"
| TokenParser.math _ p => match p.k with
| TokensKind.float => db.mkError base "unclosed $f"
| TokensKind.ess => db.mkError base "unclosed $e"
| TokensKind.ax => db.mkError base "unclosed $a"
| TokensKind.thm => db.mkError base "unclosed $p"
| TokenParser.label pos _ => db.mkError pos "not a command"
| TokenParser.proof _ => db.mkError base "unclosed $p proof"
end ParserState
partial def check (fname : String) : IO DB := do
let h β Handle.mk fname IO.FS.Mode.read
let rec loop (s : ParserState) (base : Nat) : IO DB := do
let buf β h.read 1024
if buf.isEmpty then
return s.done base
else
let s := s.feedAll base buf
if s.db.error?.isSome then return s.db
else loop s (base + buf.size)
loop default 0
|
1af2ef631c97d32af4dcee11cd88f361dcf881d5 | cf39355caa609c0f33405126beee2739aa3cb77e | /tests/lean/run/1657.lean | 7da8dddd02d52ea994fa8ba909bd25bd435239d8 | [
"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 | 556 | lean | namespace X1
inductive wrap (A : Type) : Type
| mk : A β wrap
inductive type : Type
| mk : list type β type
| refinement : type β (bool β type) β type
end X1
namespace X2
inductive type : Type
| mk : (bool β type) β list type β β β type
| refinement : list type β type β β β (β (b : bool), b = tt β type) β (bool β type) β type
end X2
namespace X3
mutual inductive type, term
with type : Type
| fn : list type β type
| refinement : type β (string β type) β type
with term : Type
| apply : term
end X3
|
12f3fe95c0d1af78016947a406acd5a7d3a819e9 | 07c6143268cfb72beccd1cc35735d424ebcb187b | /src/set_theory/cardinal.lean | 6cce49fdea09adc801a8ad3ebd38313bb81d3bec | [
"Apache-2.0"
] | permissive | khoek/mathlib | bc49a842910af13a3c372748310e86467d1dc766 | aa55f8b50354b3e11ba64792dcb06cccb2d8ee28 | refs/heads/master | 1,588,232,063,837 | 1,587,304,803,000 | 1,587,304,803,000 | 176,688,517 | 0 | 0 | Apache-2.0 | 1,553,070,585,000 | 1,553,070,585,000 | null | UTF-8 | Lean | false | false | 45,216 | 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, Mario Carneiro
-/
import data.set.countable data.quot logic.function set_theory.schroeder_bernstein
import data.fintype.card
/-!
# Cardinal Numbers
We define cardinal numbers as a quotient of types under the equivalence relation of equinumerity.
We define the order on cardinal numbers, define omega, and do basic cardinal arithmetic:
addition, multiplication, power, cardinal successor, minimum, supremum,
infinitary sums and products
## Implementation notes
* There is a type of cardinal numbers in every universe level: `cardinal.{u} : Type (u + 1)`
is the quotient of types in `Type u`.
There is a lift operation lifting cardinal numbers to a higher level.
* Cardinal arithmetic specifically for infinite cardinals (like `ΞΊ * ΞΊ = ΞΊ`) is in the file
`set_theory/ordinal.lean`, because concepts from that file are used in the proof.
## References
* <https://en.wikipedia.org/wiki/Cardinal_number>
## Tags
cardinal number, cardinal arithmetic, cardinal exponentiation, omega
-/
open function set
open_locale classical
universes u v w x
variables {Ξ± Ξ² : Type u}
/-- The equivalence relation on types given by equivalence (bijective correspondence) of types.
Quotienting by this equivalence relation gives the cardinal numbers.
-/
instance cardinal.is_equivalent : setoid (Type u) :=
{ r := λα Ξ², nonempty (Ξ± β Ξ²),
iseqv := β¨Ξ»Ξ±,
β¨equiv.refl Ξ±β©,
λα Ξ² β¨eβ©, β¨e.symmβ©,
λα Ξ² Ξ³ β¨eββ© β¨eββ©, β¨eβ.trans eββ©β© }
/-- `cardinal.{u}` is the type of cardinal numbers in `Type u`,
defined as the quotient of `Type u` by existence of an equivalence
(a bijection with explicit inverse). -/
def cardinal : Type (u + 1) := quotient cardinal.is_equivalent
namespace cardinal
/-- The cardinal number of a type -/
def mk : Type u β cardinal := quotient.mk
localized "notation `#` := cardinal.mk" in cardinal
protected lemma eq : mk Ξ± = mk Ξ² β nonempty (Ξ± β Ξ²) := quotient.eq
@[simp] theorem mk_def (Ξ± : Type u) : @eq cardinal β¦Ξ±β§ (mk Ξ±) := rfl
@[simp] theorem mk_out (c : cardinal) : mk (c.out) = c := quotient.out_eq _
/-- We define the order on cardinal numbers by `mk Ξ± β€ mk Ξ²` if and only if
there exists an embedding (injective function) from Ξ± to Ξ². -/
instance : has_le cardinal.{u} :=
β¨Ξ»qβ qβ, quotient.lift_onβ qβ qβ (λα Ξ², nonempty $ Ξ± βͺ Ξ²) $
assume Ξ± Ξ² Ξ³ Ξ΄ β¨eββ© β¨eββ©,
propext β¨assume β¨eβ©, β¨e.congr eβ eββ©, assume β¨eβ©, β¨e.congr eβ.symm eβ.symmβ©β©β©
theorem mk_le_of_injective {Ξ± Ξ² : Type u} {f : Ξ± β Ξ²} (hf : injective f) : mk Ξ± β€ mk Ξ² :=
β¨β¨f, hfβ©β©
theorem mk_le_of_surjective {Ξ± Ξ² : Type u} {f : Ξ± β Ξ²} (hf : surjective f) : mk Ξ² β€ mk Ξ± :=
β¨embedding.of_surjective hfβ©
theorem le_mk_iff_exists_set {c : cardinal} {Ξ± : Type u} :
c β€ mk Ξ± β β p : set Ξ±, mk p = c :=
β¨quotient.induction_on c $ Ξ» Ξ² β¨β¨f, hfβ©β©,
β¨set.range f, eq.symm $ quot.sound β¨equiv.set.range f hfβ©β©,
Ξ» β¨p, eβ©, e βΈ β¨β¨subtype.val, Ξ» a b, subtype.eqβ©β©β©
theorem out_embedding {c c' : cardinal} : c β€ c' β nonempty (c.out βͺ c'.out) :=
by { transitivity _, rw [βquotient.out_eq c, βquotient.out_eq c'], refl }
instance : linear_order cardinal.{u} :=
{ le := (β€),
le_refl := by rintros β¨Ξ±β©; exact β¨embedding.refl _β©,
le_trans := by rintros β¨Ξ±β© β¨Ξ²β© β¨Ξ³β© β¨eββ© β¨eββ©; exact β¨eβ.trans eββ©,
le_antisymm := by rintros β¨Ξ±β© β¨Ξ²β© β¨eββ© β¨eββ©; exact quotient.sound (eβ.antisymm eβ),
le_total := by rintros β¨Ξ±β© β¨Ξ²β©; exact embedding.total }
noncomputable instance : decidable_linear_order cardinal.{u} := classical.DLO _
noncomputable instance : distrib_lattice cardinal.{u} := by apply_instance -- short-circuit type class inference
instance : has_zero cardinal.{u} := β¨β¦pemptyβ§β©
instance : inhabited cardinal.{u} := β¨0β©
theorem ne_zero_iff_nonempty {Ξ± : Type u} : mk Ξ± β 0 β nonempty Ξ± :=
not_iff_comm.1
β¨Ξ» h, quotient.sound β¨(equiv.empty_of_not_nonempty h).trans equiv.empty_equiv_pemptyβ©,
Ξ» e, let β¨hβ© := quotient.exact e in Ξ» β¨aβ©, (h a).elimβ©
instance : has_one cardinal.{u} := β¨β¦punitβ§β©
instance : zero_ne_one_class cardinal.{u} :=
{ zero := 0, one := 1, zero_ne_one :=
ne.symm $ ne_zero_iff_nonempty.2 β¨punit.starβ© }
theorem le_one_iff_subsingleton {Ξ± : Type u} : mk Ξ± β€ 1 β subsingleton Ξ± :=
β¨Ξ» β¨fβ©, β¨Ξ» a b, f.inj (subsingleton.elim _ _)β©,
Ξ» β¨hβ©, β¨β¨Ξ» a, punit.star, Ξ» a b _, h _ _β©β©β©
instance : has_add cardinal.{u} :=
β¨Ξ»qβ qβ, quotient.lift_onβ qβ qβ (λα Ξ², mk (Ξ± β Ξ²)) $ assume Ξ± Ξ² Ξ³ Ξ΄ β¨eββ© β¨eββ©,
quotient.sound β¨equiv.sum_congr eβ eββ©β©
@[simp] theorem add_def (Ξ± Ξ²) : mk Ξ± + mk Ξ² = mk (Ξ± β Ξ²) := rfl
instance : has_mul cardinal.{u} :=
β¨Ξ»qβ qβ, quotient.lift_onβ qβ qβ (λα Ξ², mk (Ξ± Γ Ξ²)) $ assume Ξ± Ξ² Ξ³ Ξ΄ β¨eββ© β¨eββ©,
quotient.sound β¨equiv.prod_congr eβ eββ©β©
@[simp] theorem mul_def (Ξ± Ξ² : Type u) : mk Ξ± * mk Ξ² = mk (Ξ± Γ Ξ²) := rfl
private theorem add_comm (a b : cardinal.{u}) : a + b = b + a :=
quotient.induction_onβ a b $ assume Ξ± Ξ², quotient.sound β¨equiv.sum_comm Ξ± Ξ²β©
private theorem mul_comm (a b : cardinal.{u}) : a * b = b * a :=
quotient.induction_onβ a b $ assume Ξ± Ξ², quotient.sound β¨equiv.prod_comm Ξ± Ξ²β©
private theorem zero_add (a : cardinal.{u}) : 0 + a = a :=
quotient.induction_on a $ assume Ξ±, quotient.sound β¨equiv.pempty_sum Ξ±β©
private theorem zero_mul (a : cardinal.{u}) : 0 * a = 0 :=
quotient.induction_on a $ assume Ξ±, quotient.sound β¨equiv.pempty_prod Ξ±β©
private theorem one_mul (a : cardinal.{u}) : 1 * a = a :=
quotient.induction_on a $ assume Ξ±, quotient.sound β¨equiv.punit_prod Ξ±β©
private theorem left_distrib (a b c : cardinal.{u}) : a * (b + c) = a * b + a * c :=
quotient.induction_onβ a b c $ assume Ξ± Ξ² Ξ³, quotient.sound β¨equiv.prod_sum_distrib Ξ± Ξ² Ξ³β©
instance : comm_semiring cardinal.{u} :=
{ zero := 0,
one := 1,
add := (+),
mul := (*),
zero_add := zero_add,
add_zero := assume a, by rw [add_comm a 0, zero_add a],
add_assoc := Ξ»a b c, quotient.induction_onβ a b c $ assume Ξ± Ξ² Ξ³,
quotient.sound β¨equiv.sum_assoc Ξ± Ξ² Ξ³β©,
add_comm := add_comm,
zero_mul := zero_mul,
mul_zero := assume a, by rw [mul_comm a 0, zero_mul a],
one_mul := one_mul,
mul_one := assume a, by rw [mul_comm a 1, one_mul a],
mul_assoc := Ξ»a b c, quotient.induction_onβ a b c $ assume Ξ± Ξ² Ξ³,
quotient.sound β¨equiv.prod_assoc Ξ± Ξ² Ξ³β©,
mul_comm := mul_comm,
left_distrib := left_distrib,
right_distrib := assume a b c,
by rw [mul_comm (a + b) c, left_distrib c a b, mul_comm c a, mul_comm c b] }
/-- The cardinal exponential. `mk Ξ± ^ mk Ξ²` is the cardinal of `Ξ² β Ξ±`. -/
protected def power (a b : cardinal.{u}) : cardinal.{u} :=
quotient.lift_onβ a b (λα Ξ², mk (Ξ² β Ξ±)) $ assume Ξ±β Ξ±β Ξ²β Ξ²β β¨eββ© β¨eββ©,
quotient.sound β¨equiv.arrow_congr eβ eββ©
instance : has_pow cardinal cardinal := β¨cardinal.powerβ©
local infixr ^ := @has_pow.pow cardinal cardinal cardinal.has_pow
@[simp] theorem power_def (Ξ± Ξ²) : mk Ξ± ^ mk Ξ² = mk (Ξ² β Ξ±) := rfl
@[simp] theorem power_zero {a : cardinal} : a ^ 0 = 1 :=
quotient.induction_on a $ assume Ξ±, quotient.sound
β¨equiv.pempty_arrow_equiv_punit Ξ±β©
@[simp] theorem power_one {a : cardinal} : a ^ 1 = a :=
quotient.induction_on a $ assume Ξ±, quotient.sound
β¨equiv.punit_arrow_equiv Ξ±β©
@[simp] theorem one_power {a : cardinal} : 1 ^ a = 1 :=
quotient.induction_on a $ assume Ξ±, quotient.sound
β¨equiv.arrow_punit_equiv_punit Ξ±β©
@[simp] theorem prop_eq_two : mk (ulift Prop) = 2 :=
quot.sound β¨equiv.ulift.trans $ equiv.Prop_equiv_bool.trans equiv.bool_equiv_punit_sum_punitβ©
@[simp] theorem zero_power {a : cardinal} : a β 0 β 0 ^ a = 0 :=
quotient.induction_on a $ assume Ξ± heq,
nonempty.rec_on (ne_zero_iff_nonempty.1 heq) $ assume a,
quotient.sound β¨equiv.equiv_pempty $ assume f, pempty.rec (Ξ» _, false) (f a)β©
theorem power_ne_zero {a : cardinal} (b) : a β 0 β a ^ b β 0 :=
quotient.induction_onβ a b $ Ξ» Ξ± Ξ² h,
let β¨aβ© := ne_zero_iff_nonempty.1 h in
ne_zero_iff_nonempty.2 β¨Ξ» _, aβ©
theorem mul_power {a b c : cardinal} : (a * b) ^ c = a ^ c * b ^ c :=
quotient.induction_onβ a b c $ assume Ξ± Ξ² Ξ³,
quotient.sound β¨equiv.arrow_prod_equiv_prod_arrow Ξ± Ξ² Ξ³β©
theorem power_add {a b c : cardinal} : a ^ (b + c) = a ^ b * a ^ c :=
quotient.induction_onβ a b c $ assume Ξ± Ξ² Ξ³,
quotient.sound β¨equiv.sum_arrow_equiv_prod_arrow Ξ² Ξ³ Ξ±β©
theorem power_mul {a b c : cardinal} : (a ^ b) ^ c = a ^ (b * c) :=
by rw [_root_.mul_comm b c];
from (quotient.induction_onβ a b c $ assume Ξ± Ξ² Ξ³,
quotient.sound β¨equiv.arrow_arrow_equiv_prod_arrow Ξ³ Ξ² Ξ±β©)
@[simp] lemma pow_cast_right (ΞΊ : cardinal.{u}) :
β n : β, (ΞΊ ^ (βn : cardinal.{u})) = @has_pow.pow _ _ monoid.has_pow ΞΊ n
| 0 := by simp
| (_+1) := by rw [nat.cast_succ, power_add, power_one, _root_.mul_comm, pow_succ, pow_cast_right]
section order_properties
open sum
theorem zero_le : β(a : cardinal), 0 β€ a :=
by rintro β¨Ξ±β©; exact β¨embedding.of_not_nonempty $ Ξ» β¨aβ©, a.elimβ©
theorem le_zero (a : cardinal) : a β€ 0 β a = 0 :=
by simp [le_antisymm_iff, zero_le]
theorem pos_iff_ne_zero {o : cardinal} : 0 < o β o β 0 :=
by simp [lt_iff_le_and_ne, eq_comm, zero_le]
theorem zero_lt_one : (0 : cardinal) < 1 :=
lt_of_le_of_ne (zero_le _) zero_ne_one
lemma zero_power_le (c : cardinal.{u}) : (0 : cardinal.{u}) ^ c β€ 1 :=
by { by_cases h : c = 0, rw [h, power_zero], rw [zero_power h], apply zero_le }
theorem add_le_add : β{a b c d : cardinal}, a β€ b β c β€ d β a + c β€ b + d :=
by rintros β¨Ξ±β© β¨Ξ²β© β¨Ξ³β© β¨Ξ΄β© β¨eββ© β¨eββ©; exact β¨embedding.sum_congr eβ eββ©
theorem add_le_add_left (a) {b c : cardinal} : b β€ c β a + b β€ a + c :=
add_le_add (le_refl _)
theorem add_le_add_right {a b : cardinal} (c) (h : a β€ b) : a + c β€ b + c :=
add_le_add h (le_refl _)
theorem le_add_right (a b : cardinal) : a β€ a + b :=
by simpa using add_le_add_left a (zero_le b)
theorem le_add_left (a b : cardinal) : a β€ b + a :=
by simpa using add_le_add_right a (zero_le b)
theorem mul_le_mul : β{a b c d : cardinal}, a β€ b β c β€ d β a * c β€ b * d :=
by rintros β¨Ξ±β© β¨Ξ²β© β¨Ξ³β© β¨Ξ΄β© β¨eββ© β¨eββ©; exact β¨embedding.prod_congr eβ eββ©
theorem mul_le_mul_left (a) {b c : cardinal} : b β€ c β a * b β€ a * c :=
mul_le_mul (le_refl _)
theorem mul_le_mul_right {a b : cardinal} (c) (h : a β€ b) : a * c β€ b * c :=
mul_le_mul h (le_refl _)
theorem power_le_power_left : β{a b c : cardinal}, a β 0 β b β€ c β a ^ b β€ a ^ c :=
by rintros β¨Ξ±β© β¨Ξ²β© β¨Ξ³β© hΞ± β¨eβ©; exact
let β¨aβ© := ne_zero_iff_nonempty.1 hΞ± in
β¨@embedding.arrow_congr_right _ _ _ β¨aβ© eβ©
theorem power_le_max_power_one {a b c : cardinal} (h : b β€ c) : a ^ b β€ max (a ^ c) 1 :=
begin
by_cases ha : a = 0,
simp [ha, zero_power_le],
exact le_trans (power_le_power_left ha h) (le_max_left _ _)
end
theorem power_le_power_right {a b c : cardinal} : a β€ b β a ^ c β€ b ^ c :=
quotient.induction_onβ a b c $ assume Ξ± Ξ² Ξ³ β¨eβ©, β¨embedding.arrow_congr_left eβ©
theorem le_iff_exists_add {a b : cardinal} : a β€ b β β c, b = a + c :=
β¨quotient.induction_onβ a b $ Ξ» Ξ± Ξ² β¨β¨f, hfβ©β©,
have (Ξ± β β₯-range f) β Ξ², from
(equiv.sum_congr (equiv.set.range f hf) (equiv.refl _)).trans $
(equiv.set.sum_compl (range f)),
β¨β¦(-range f : set Ξ²)β§, quotient.sound β¨this.symmβ©β©,
Ξ» β¨c, eβ©, add_zero a βΈ e.symm βΈ add_le_add_left _ (zero_le _)β©
end order_properties
instance : order_bot cardinal.{u} :=
{ bot := 0, bot_le := zero_le, ..cardinal.linear_order }
instance : canonically_ordered_add_monoid cardinal.{u} :=
{ add_le_add_left := Ξ» a b h c, add_le_add_left _ h,
lt_of_add_lt_add_left := Ξ» a b c, lt_imp_lt_of_le_imp_le (add_le_add_left _),
le_iff_exists_add := @le_iff_exists_add,
..cardinal.order_bot,
..cardinal.comm_semiring, ..cardinal.linear_order }
theorem cantor : β(a : cardinal.{u}), a < 2 ^ a :=
by rw β prop_eq_two; rintros β¨aβ©; exact β¨
β¨β¨Ξ» a b, β¨a = bβ©, Ξ» a b h, cast (ulift.up.inj (@congr_fun _ _ _ _ h b)).symm rflβ©β©,
Ξ» β¨β¨f, hfβ©β©, cantor_injective (Ξ» s, f (Ξ» a, β¨s aβ©)) $
Ξ» s t h, by funext a; injection congr_fun (hf h) aβ©
instance : no_top_order cardinal.{u} :=
{ no_top := Ξ» a, β¨_, cantor aβ©, ..cardinal.linear_order }
/-- The minimum cardinal in a family of cardinals (the existence
of which is provided by `injective_min`). -/
noncomputable def min {ΞΉ} (I : nonempty ΞΉ) (f : ΞΉ β cardinal) : cardinal :=
f $ classical.some $
@embedding.injective_min _ (Ξ» i, (f i).out) I
theorem min_eq {ΞΉ} (I) (f : ΞΉ β cardinal) : β i, min I f = f i :=
β¨_, rflβ©
theorem min_le {ΞΉ I} (f : ΞΉ β cardinal) (i) : min I f β€ f i :=
by rw [β mk_out (min I f), β mk_out (f i)]; exact
let β¨gβ© := classical.some_spec
(@embedding.injective_min _ (Ξ» i, (f i).out) I) in
β¨g iβ©
theorem le_min {ΞΉ I} {f : ΞΉ β cardinal} {a} : a β€ min I f β β i, a β€ f i :=
β¨Ξ» h i, le_trans h (min_le _ _),
Ξ» h, let β¨i, eβ© := min_eq I f in e.symm βΈ h iβ©
protected theorem wf : @well_founded cardinal.{u} (<) :=
β¨Ξ» a, classical.by_contradiction $ Ξ» h,
let ΞΉ := {c :cardinal // Β¬ acc (<) c},
f : ΞΉ β cardinal := subtype.val,
β¨β¨c, hcβ©, hiβ© := @min_eq ΞΉ β¨β¨_, hβ©β© f in
hc (acc.intro _ (Ξ» j β¨_, h'β©,
classical.by_contradiction $ Ξ» hj, h' $
by have := min_le f β¨j, hjβ©; rwa hi at this))β©
instance has_wf : @has_well_founded cardinal.{u} := β¨(<), cardinal.wfβ©
instance wo : @is_well_order cardinal.{u} (<) := β¨cardinal.wfβ©
/-- The successor cardinal - the smallest cardinal greater than
`c`. This is not the same as `c + 1` except in the case of finite `c`. -/
noncomputable def succ (c : cardinal) : cardinal :=
@min {c' // c < c'} β¨β¨_, cantor _β©β© subtype.val
theorem lt_succ_self (c : cardinal) : c < succ c :=
by cases min_eq _ _ with s e; rw [succ, e]; exact s.2
theorem succ_le {a b : cardinal} : succ a β€ b β a < b :=
β¨lt_of_lt_of_le (lt_succ_self _), Ξ» h,
by exact min_le _ (subtype.mk b h)β©
theorem lt_succ {a b : cardinal} : a < succ b β a β€ b :=
by rw [β not_le, succ_le, not_lt]
theorem add_one_le_succ (c : cardinal) : c + 1 β€ succ c :=
begin
refine quot.induction_on c (Ξ» Ξ±, _) (lt_succ_self c),
refine quot.induction_on (succ (quot.mk setoid.r Ξ±)) (Ξ» Ξ² h, _),
cases h.left with f,
have : Β¬ surjective f := Ξ» hn,
ne_of_lt h (quotient.sound β¨equiv.of_bijective β¨f.inj, hnβ©β©),
cases classical.not_forall.1 this with b nex,
refine β¨β¨sum.rec (by exact f) _, _β©β©,
{ exact Ξ» _, b },
{ intros a b h, rcases a with a|β¨β¨β¨β©β©β©; rcases b with b|β¨β¨β¨β©β©β©,
{ rw f.inj h },
{ exact nex.elim β¨_, hβ© },
{ exact nex.elim β¨_, h.symmβ© },
{ refl } }
end
lemma succ_ne_zero (c : cardinal) : succ c β 0 :=
by { rw [βpos_iff_ne_zero, lt_succ], apply zero_le }
/-- The indexed sum of cardinals is the cardinality of the
indexed disjoint union, i.e. sigma type. -/
def sum {ΞΉ} (f : ΞΉ β cardinal) : cardinal := mk Ξ£ i, (f i).out
theorem le_sum {ΞΉ} (f : ΞΉ β cardinal) (i) : f i β€ sum f :=
by rw β quotient.out_eq (f i); exact
β¨β¨Ξ» a, β¨i, aβ©, Ξ» a b h, eq_of_heq $ by injection hβ©β©
@[simp] theorem sum_mk {ΞΉ} (f : ΞΉ β Type*) : sum (Ξ» i, mk (f i)) = mk (Ξ£ i, f i) :=
quot.sound β¨equiv.sigma_congr_right $ Ξ» i,
classical.choice $ quotient.exact $ quot.out_eq $ mk (f i)β©
theorem sum_const (ΞΉ : Type u) (a : cardinal.{u}) : sum (Ξ» _:ΞΉ, a) = mk ΞΉ * a :=
quotient.induction_on a $ Ξ» Ξ±, by simp; exact
quotient.sound β¨equiv.sigma_equiv_prod _ _β©
theorem sum_le_sum {ΞΉ} (f g : ΞΉ β cardinal) (H : β i, f i β€ g i) : sum f β€ sum g :=
β¨embedding.sigma_congr_right $ Ξ» i, classical.choice $
by have := H i; rwa [β quot.out_eq (f i), β quot.out_eq (g i)] at thisβ©
/-- The indexed supremum of cardinals is the smallest cardinal above
everything in the family. -/
noncomputable def sup {ΞΉ} (f : ΞΉ β cardinal) : cardinal :=
@min {c // β i, f i β€ c} β¨β¨sum f, le_sum fβ©β© (Ξ» a, a.1)
theorem le_sup {ΞΉ} (f : ΞΉ β cardinal) (i) : f i β€ sup f :=
by dsimp [sup]; cases min_eq _ _ with c hc; rw hc; exact c.2 i
theorem sup_le {ΞΉ} {f : ΞΉ β cardinal} {a} : sup f β€ a β β i, f i β€ a :=
β¨Ξ» h i, le_trans (le_sup _ _) h,
Ξ» h, by dsimp [sup]; change a with (β¨a, hβ©:subtype _).1; apply min_leβ©
theorem sup_le_sup {ΞΉ} (f g : ΞΉ β cardinal) (H : β i, f i β€ g i) : sup f β€ sup g :=
sup_le.2 $ Ξ» i, le_trans (H i) (le_sup _ _)
theorem sup_le_sum {ΞΉ} (f : ΞΉ β cardinal) : sup f β€ sum f :=
sup_le.2 $ le_sum _
theorem sum_le_sup {ΞΉ : Type u} (f : ΞΉ β cardinal.{u}) : sum f β€ mk ΞΉ * sup.{u u} f :=
by rw β sum_const; exact sum_le_sum _ _ (le_sup _)
theorem sup_eq_zero {ΞΉ} {f : ΞΉ β cardinal} (h : ΞΉ β false) : sup f = 0 :=
by { rw [βle_zero, sup_le], intro x, exfalso, exact h x }
/-- The indexed product of cardinals is the cardinality of the Pi type
(dependent product). -/
def prod {ΞΉ : Type u} (f : ΞΉ β cardinal) : cardinal := mk (Ξ i, (f i).out)
@[simp] theorem prod_mk {ΞΉ} (f : ΞΉ β Type*) : prod (Ξ» i, mk (f i)) = mk (Ξ i, f i) :=
quot.sound β¨equiv.Pi_congr_right $ Ξ» i,
classical.choice $ quotient.exact $ mk_out $ mk (f i)β©
theorem prod_const (ΞΉ : Type u) (a : cardinal.{u}) : prod (Ξ» _:ΞΉ, a) = a ^ mk ΞΉ :=
quotient.induction_on a $ by simp
theorem prod_le_prod {ΞΉ} (f g : ΞΉ β cardinal) (H : β i, f i β€ g i) : prod f β€ prod g :=
β¨embedding.Pi_congr_right $ Ξ» i, classical.choice $
by have := H i; rwa [β mk_out (f i), β mk_out (g i)] at thisβ©
theorem prod_ne_zero {ΞΉ} (f : ΞΉ β cardinal) : prod f β 0 β β i, f i β 0 :=
begin
conv in (f _) {rw β mk_out (f i)},
simp [prod, ne_zero_iff_nonempty, -mk_out, -ne.def],
exact β¨Ξ» β¨Fβ© i, β¨F iβ©, Ξ» h, β¨Ξ» i, classical.choice (h i)β©β©,
end
theorem prod_eq_zero {ΞΉ} (f : ΞΉ β cardinal) : prod f = 0 β β i, f i = 0 :=
not_iff_not.1 $ by simpa using prod_ne_zero f
/-- The universe lift operation on cardinals. You can specify the universes explicitly with
`lift.{u v} : cardinal.{u} β cardinal.{max u v}` -/
def lift (c : cardinal.{u}) : cardinal.{max u v} :=
quotient.lift_on c (Ξ» Ξ±, β¦ulift Ξ±β§) $ Ξ» Ξ± Ξ² β¨eβ©,
quotient.sound β¨equiv.ulift.trans $ e.trans equiv.ulift.symmβ©
theorem lift_mk (Ξ±) : lift.{u v} (mk Ξ±) = mk (ulift.{v u} Ξ±) := rfl
theorem lift_umax : lift.{u (max u v)} = lift.{u v} :=
funext $ Ξ» a, quot.induction_on a $ Ξ» Ξ±,
quotient.sound β¨equiv.ulift.trans equiv.ulift.symmβ©
theorem lift_id' (a : cardinal) : lift a = a :=
quot.induction_on a $ Ξ» Ξ±, quot.sound β¨equiv.uliftβ©
@[simp] theorem lift_id : β a, lift.{u u} a = a := lift_id'.{u u}
@[simp] theorem lift_lift (a : cardinal) : lift.{(max u v) w} (lift.{u v} a) = lift.{u (max v w)} a :=
quot.induction_on a $ Ξ» Ξ±,
quotient.sound β¨equiv.ulift.trans $ equiv.ulift.trans equiv.ulift.symmβ©
theorem lift_mk_le {Ξ± : Type u} {Ξ² : Type v} :
lift.{u (max v w)} (mk Ξ±) β€ lift.{v (max u w)} (mk Ξ²) β nonempty (Ξ± βͺ Ξ²) :=
β¨Ξ» β¨fβ©, β¨embedding.congr equiv.ulift equiv.ulift fβ©,
Ξ» β¨fβ©, β¨embedding.congr equiv.ulift.symm equiv.ulift.symm fβ©β©
theorem lift_mk_eq {Ξ± : Type u} {Ξ² : Type v} :
lift.{u (max v w)} (mk Ξ±) = lift.{v (max u w)} (mk Ξ²) β nonempty (Ξ± β Ξ²) :=
quotient.eq.trans
β¨Ξ» β¨fβ©, β¨equiv.ulift.symm.trans $ f.trans equiv.uliftβ©,
Ξ» β¨fβ©, β¨equiv.ulift.trans $ f.trans equiv.ulift.symmβ©β©
@[simp] theorem lift_le {a b : cardinal} : lift a β€ lift b β a β€ b :=
quotient.induction_onβ a b $ Ξ» Ξ± Ξ²,
by rw β lift_umax; exact lift_mk_le
@[simp] theorem lift_inj {a b : cardinal} : lift a = lift b β a = b :=
by simp [le_antisymm_iff]
@[simp] theorem lift_lt {a b : cardinal} : lift a < lift b β a < b :=
by simp [lt_iff_le_not_le, -not_le]
@[simp] theorem lift_zero : lift 0 = 0 :=
quotient.sound β¨equiv.ulift.trans equiv.pempty_equiv_pemptyβ©
@[simp] theorem lift_one : lift 1 = 1 :=
quotient.sound β¨equiv.ulift.trans equiv.punit_equiv_punitβ©
@[simp] theorem lift_add (a b) : lift (a + b) = lift a + lift b :=
quotient.induction_onβ a b $ Ξ» Ξ± Ξ²,
quotient.sound β¨equiv.ulift.trans (equiv.sum_congr equiv.ulift equiv.ulift).symmβ©
@[simp] theorem lift_mul (a b) : lift (a * b) = lift a * lift b :=
quotient.induction_onβ a b $ Ξ» Ξ± Ξ²,
quotient.sound β¨equiv.ulift.trans (equiv.prod_congr equiv.ulift equiv.ulift).symmβ©
@[simp] theorem lift_power (a b) : lift (a ^ b) = lift a ^ lift b :=
quotient.induction_onβ a b $ Ξ» Ξ± Ξ²,
quotient.sound β¨equiv.ulift.trans (equiv.arrow_congr equiv.ulift equiv.ulift).symmβ©
@[simp] theorem lift_two_power (a) : lift (2 ^ a) = 2 ^ lift a :=
by simp [bit0]
@[simp] theorem lift_min {ΞΉ I} (f : ΞΉ β cardinal) : lift (min I f) = min I (lift β f) :=
le_antisymm (le_min.2 $ Ξ» a, lift_le.2 $ min_le _ a) $
let β¨i, eβ© := min_eq I (lift β f) in
by rw e; exact lift_le.2 (le_min.2 $ Ξ» j, lift_le.1 $
by have := min_le (lift β f) j; rwa e at this)
theorem lift_down {a : cardinal.{u}} {b : cardinal.{max u v}} :
b β€ lift a β β a', lift a' = b :=
quotient.induction_onβ a b $ Ξ» Ξ± Ξ²,
by dsimp; rw [β lift_id (mk Ξ²), β lift_umax, β lift_umax.{u v}, lift_mk_le]; exact
Ξ» β¨fβ©, β¨mk (set.range f), eq.symm $ lift_mk_eq.2
β¨embedding.equiv_of_surjective
(embedding.cod_restrict _ f set.mem_range_self)
$ Ξ» β¨a, β¨b, eβ©β©, β¨b, subtype.eq eβ©β©β©
theorem le_lift_iff {a : cardinal.{u}} {b : cardinal.{max u v}} :
b β€ lift a β β a', lift a' = b β§ a' β€ a :=
β¨Ξ» h, let β¨a', eβ© := lift_down h in β¨a', e, lift_le.1 $ e.symm βΈ hβ©,
Ξ» β¨a', e, hβ©, e βΈ lift_le.2 hβ©
theorem lt_lift_iff {a : cardinal.{u}} {b : cardinal.{max u v}} :
b < lift a β β a', lift a' = b β§ a' < a :=
β¨Ξ» h, let β¨a', eβ© := lift_down (le_of_lt h) in
β¨a', e, lift_lt.1 $ e.symm βΈ hβ©,
Ξ» β¨a', e, hβ©, e βΈ lift_lt.2 hβ©
@[simp] theorem lift_succ (a) : lift (succ a) = succ (lift a) :=
le_antisymm
(le_of_not_gt $ Ξ» h, begin
rcases lt_lift_iff.1 h with β¨b, e, hβ©,
rw [lt_succ, β lift_le, e] at h,
exact not_lt_of_le h (lt_succ_self _)
end)
(succ_le.2 $ lift_lt.2 $ lt_succ_self _)
@[simp] theorem lift_max {a : cardinal.{u}} {b : cardinal.{v}} :
lift.{u (max v w)} a = lift.{v (max u w)} b β lift.{u v} a = lift.{v u} b :=
calc lift.{u (max v w)} a = lift.{v (max u w)} b
β lift.{(max u v) w} (lift.{u v} a)
= lift.{(max u v) w} (lift.{v u} b) : by simp
... β lift.{u v} a = lift.{v u} b : lift_inj
theorem mk_prod {Ξ± : Type u} {Ξ² : Type v} :
mk (Ξ± Γ Ξ²) = lift.{u v} (mk Ξ±) * lift.{v u} (mk Ξ²) :=
quotient.sound β¨equiv.prod_congr (equiv.ulift).symm (equiv.ulift).symmβ©
theorem sum_const_eq_lift_mul (ΞΉ : Type u) (a : cardinal.{v}) :
sum (Ξ» _:ΞΉ, a) = lift.{u v} (mk ΞΉ) * lift.{v u} a :=
begin
apply quotient.induction_on a,
intro Ξ±,
simp only [cardinal.mk_def, cardinal.sum_mk, cardinal.lift_id],
convert mk_prod using 1,
exact quotient.sound β¨equiv.sigma_equiv_prod ΞΉ Ξ±β©,
end
/-- `Ο` is the smallest infinite cardinal, also known as β΅β. -/
def omega : cardinal.{u} := lift (mk β)
lemma mk_nat : mk nat = omega := (lift_id _).symm
theorem omega_ne_zero : omega β 0 :=
ne_zero_iff_nonempty.2 β¨β¨0β©β©
theorem omega_pos : 0 < omega :=
pos_iff_ne_zero.2 omega_ne_zero
@[simp] theorem lift_omega : lift omega = omega := lift_lift _
/- properties about the cast from nat -/
@[simp] theorem mk_fin : β (n : β), mk (fin n) = n
| 0 := quotient.sound β¨(equiv.pempty_of_not_nonempty $ Ξ» β¨hβ©, h.elim0)β©
| (n+1) := by rw [nat.cast_succ, β mk_fin]; exact
quotient.sound (fintype.card_eq.1 $ by simp)
@[simp] theorem lift_nat_cast (n : β) : lift n = n :=
by induction n; simp *
lemma lift_eq_nat_iff {a : cardinal.{u}} {n : β} : lift.{u v} a = n β a = n :=
by rw [β lift_nat_cast.{u v} n, lift_inj]
lemma nat_eq_lift_eq_iff {n : β} {a : cardinal.{u}} :
(n : cardinal) = lift.{u v} a β (n : cardinal) = a :=
by rw [β lift_nat_cast.{u v} n, lift_inj]
theorem lift_mk_fin (n : β) : lift (mk (fin n)) = n := by simp
theorem fintype_card (Ξ± : Type u) [fintype Ξ±] : mk Ξ± = fintype.card Ξ± :=
by rw [β lift_mk_fin.{u}, β lift_id (mk Ξ±), lift_mk_eq.{u 0 u}];
exact fintype.card_eq.1 (by simp)
theorem card_le_of_finset {Ξ±} (s : finset Ξ±) :
(s.card : cardinal) β€ cardinal.mk Ξ± :=
begin
rw (_ : (s.card : cardinal) = cardinal.mk (βs : set Ξ±)),
{ exact β¨function.embedding.subtype _β© },
rw [cardinal.fintype_card, fintype.card_coe]
end
@[simp, norm_cast] theorem nat_cast_pow {m n : β} : (β(pow m n) : cardinal) = m ^ n :=
by induction n; simp [nat.pow_succ, -_root_.add_comm, power_add, *]
@[simp, norm_cast] theorem nat_cast_le {m n : β} : (m : cardinal) β€ n β m β€ n :=
by rw [β lift_mk_fin, β lift_mk_fin, lift_le]; exact
β¨Ξ» β¨β¨f, hfβ©β©, begin
have : _ = fintype.card _ := finset.card_image_of_injective finset.univ hf,
simp at this,
rw [β fintype.card_fin n, β this],
exact finset.card_le_of_subset (finset.subset_univ _)
end,
Ξ» h, β¨β¨Ξ» i, β¨i.1, lt_of_lt_of_le i.2 hβ©, Ξ» a b h,
have _, from fin.veq_of_eq h, fin.eq_of_veq thisβ©β©β©
@[simp, norm_cast] theorem nat_cast_lt {m n : β} : (m : cardinal) < n β m < n :=
by simp [lt_iff_le_not_le, -not_le]
@[simp, norm_cast] theorem nat_cast_inj {m n : β} : (m : cardinal) = n β m = n :=
by simp [le_antisymm_iff]
@[simp, norm_cast, priority 900] theorem nat_succ (n : β) : (n.succ : cardinal) = succ n :=
le_antisymm (add_one_le_succ _) (succ_le.2 $ nat_cast_lt.2 $ nat.lt_succ_self _)
@[simp] theorem succ_zero : succ 0 = 1 :=
by norm_cast
theorem cantor' (a) {b : cardinal} (hb : 1 < b) : a < b ^ a :=
by rw [β succ_le, (by norm_cast : succ 1 = 2)] at hb;
exact lt_of_lt_of_le (cantor _) (power_le_power_right hb)
theorem one_le_iff_pos {c : cardinal} : 1 β€ c β 0 < c :=
by rw [β succ_zero, succ_le]
theorem one_le_iff_ne_zero {c : cardinal} : 1 β€ c β c β 0 :=
by rw [one_le_iff_pos, pos_iff_ne_zero]
theorem nat_lt_omega (n : β) : (n : cardinal.{u}) < omega :=
succ_le.1 $ by rw [β nat_succ, β lift_mk_fin, omega, lift_mk_le.{0 0 u}]; exact
β¨β¨fin.val, Ξ» a b, fin.eq_of_veqβ©β©
theorem one_lt_omega : 1 < omega :=
by simpa using nat_lt_omega 1
theorem lt_omega {c : cardinal.{u}} : c < omega β β n : β, c = n :=
β¨Ξ» h, begin
rcases lt_lift_iff.1 h with β¨c, rfl, h'β©,
rcases le_mk_iff_exists_set.1 h'.1 with β¨S, rflβ©,
suffices : finite S,
{ cases this, resetI,
existsi fintype.card S,
rw [β lift_nat_cast.{0 u}, lift_inj, fintype_card S] },
by_contra nf,
have P : β (n : β) (IH : β i<n, S), β a : S, Β¬ β y h, IH y h = a :=
Ξ» n IH,
let g : {i | i < n} β S := Ξ» β¨i, hβ©, IH i h in
classical.not_forall.1 (Ξ» h, nf
β¨fintype.of_surjective g (Ξ» a, subtype.exists.2 (h a))β©),
let F : β β S := nat.lt_wf.fix (Ξ» n IH, classical.some (P n IH)),
refine not_le_of_lt h' β¨β¨F, _β©β©,
suffices : β (n : β) (m < n), F m β F n,
{ refine Ξ» m n, not_imp_not.1 (Ξ» ne, _),
rcases lt_trichotomy m n with h|h|h,
{ exact this n m h },
{ contradiction },
{ exact (this m n h).symm } },
intros n m h,
have := classical.some_spec (P n (Ξ» y _, F y)),
rw [β show F n = classical.some (P n (Ξ» y _, F y)),
from nat.lt_wf.fix_eq (Ξ» n IH, classical.some (P n IH)) n] at this,
exact Ξ» e, this β¨m, h, eβ©,
end, Ξ» β¨n, eβ©, e.symm βΈ nat_lt_omega _β©
theorem omega_le {c : cardinal.{u}} : omega β€ c β β n : β, (n:cardinal) β€ c :=
β¨Ξ» h n, le_trans (le_of_lt (nat_lt_omega _)) h,
Ξ» h, le_of_not_lt $ Ξ» hn, begin
rcases lt_omega.1 hn with β¨n, rflβ©,
exact not_le_of_lt (nat.lt_succ_self _) (nat_cast_le.1 (h (n+1)))
endβ©
theorem lt_omega_iff_fintype {Ξ± : Type u} : mk Ξ± < omega β nonempty (fintype Ξ±) :=
lt_omega.trans β¨Ξ» β¨n, eβ©, begin
rw [β lift_mk_fin n] at e,
cases quotient.exact e with f,
exact β¨fintype.of_equiv _ f.symmβ©
end, Ξ» β¨_β©, by exactI β¨_, fintype_card _β©β©
theorem lt_omega_iff_finite {Ξ±} {S : set Ξ±} : mk S < omega β finite S :=
lt_omega_iff_fintype
instance can_lift_cardinal_nat : can_lift cardinal β :=
β¨ coe, Ξ» x, x < omega, Ξ» x hx, let β¨n, hnβ© := lt_omega.mp hx in β¨n, hn.symmβ©β©
theorem add_lt_omega {a b : cardinal} (ha : a < omega) (hb : b < omega) : a + b < omega :=
match a, b, lt_omega.1 ha, lt_omega.1 hb with
| _, _, β¨m, rflβ©, β¨n, rflβ© := by rw [β nat.cast_add]; apply nat_lt_omega
end
lemma add_lt_omega_iff {a b : cardinal} : a + b < omega β a < omega β§ b < omega :=
β¨Ξ» h, β¨lt_of_le_of_lt (le_add_right _ _) h, lt_of_le_of_lt (le_add_left _ _) hβ©,
Ξ»β¨h1, h2β©, add_lt_omega h1 h2β©
theorem mul_lt_omega {a b : cardinal} (ha : a < omega) (hb : b < omega) : a * b < omega :=
match a, b, lt_omega.1 ha, lt_omega.1 hb with
| _, _, β¨m, rflβ©, β¨n, rflβ© := by rw [β nat.cast_mul]; apply nat_lt_omega
end
lemma mul_lt_omega_iff {a b : cardinal} : a * b < omega β a = 0 β¨ b = 0 β¨ a < omega β§ b < omega :=
begin
split,
{ intro h, by_cases ha : a = 0, { left, exact ha },
right, by_cases hb : b = 0, { left, exact hb },
right, rw [β ne, β one_le_iff_ne_zero] at ha hb, split,
{ rw [β mul_one a], refine lt_of_le_of_lt (mul_le_mul (le_refl a) hb) h },
{ rw [β _root_.one_mul b], refine lt_of_le_of_lt (mul_le_mul ha (le_refl b)) h }},
rintro (rfl|rfl|β¨ha,hbβ©); simp only [*, mul_lt_omega, omega_pos, _root_.zero_mul, mul_zero]
end
lemma mul_lt_omega_iff_of_ne_zero {a b : cardinal} (ha : a β 0) (hb : b β 0) :
a * b < omega β a < omega β§ b < omega :=
by simp [mul_lt_omega_iff, ha, hb]
theorem power_lt_omega {a b : cardinal} (ha : a < omega) (hb : b < omega) : a ^ b < omega :=
match a, b, lt_omega.1 ha, lt_omega.1 hb with
| _, _, β¨m, rflβ©, β¨n, rflβ© := by rw [β nat_cast_pow]; apply nat_lt_omega
end
lemma eq_one_iff_subsingleton_and_nonempty {Ξ± : Type*} :
mk Ξ± = 1 β (subsingleton Ξ± β§ nonempty Ξ±) :=
calc mk Ξ± = 1 β mk Ξ± β€ 1 β§ Β¬mk Ξ± < 1 : eq_iff_le_not_lt
... β subsingleton Ξ± β§ nonempty Ξ± :
begin
apply and_congr le_one_iff_subsingleton,
push_neg,
rw [one_le_iff_ne_zero, ne_zero_iff_nonempty]
end
theorem infinite_iff {Ξ± : Type u} : infinite Ξ± β omega β€ mk Ξ± :=
by rw [βnot_lt, lt_omega_iff_fintype, not_nonempty_fintype]
lemma countable_iff (s : set Ξ±) : countable s β mk s β€ omega :=
begin
rw [countable_iff_exists_injective], split,
rintro β¨f, hfβ©, exact β¨embedding.trans β¨f, hfβ© equiv.ulift.symm.to_embeddingβ©,
rintro β¨f'β©, cases embedding.trans f' equiv.ulift.to_embedding with f hf, exact β¨f, hfβ©
end
lemma denumerable_iff {Ξ± : Type u} : nonempty (denumerable Ξ±) β mk Ξ± = omega :=
β¨Ξ»β¨hβ©, quotient.sound $ by exactI β¨ (denumerable.eqv Ξ±).trans equiv.ulift.symm β©,
Ξ» h, by { cases quotient.exact h with f, exact β¨denumerable.mk' $ f.trans equiv.uliftβ© }β©
lemma mk_int : mk β€ = omega :=
denumerable_iff.mp β¨by apply_instanceβ©
lemma mk_pnat : mk β+ = omega :=
denumerable_iff.mp β¨by apply_instanceβ©
lemma two_le_iff : (2 : cardinal) β€ mk Ξ± β βx y : Ξ±, x β y :=
begin
split,
{ rintro β¨fβ©, refine β¨f $ sum.inl β¨β©, f $ sum.inr β¨β©, _β©, intro h, cases f.2 h },
{ rintro β¨x, y, hβ©, by_contra h',
rw [not_le, βnat.cast_two, nat_succ, lt_succ, nat.cast_one, le_one_iff_subsingleton] at h',
apply h, exactI subsingleton.elim _ _ }
end
lemma two_le_iff' (x : Ξ±) : (2 : cardinal) β€ mk Ξ± β βy : Ξ±, x β y :=
begin
rw [two_le_iff],
split,
{ rintro β¨y, z, hβ©, refine classical.by_cases (Ξ»(h' : x = y), _) (Ξ» h', β¨y, h'β©),
rw [βh'] at h, exact β¨z, hβ© },
{ rintro β¨y, hβ©, exact β¨x, y, hβ© }
end
/-- KΓΆnig's theorem -/
theorem sum_lt_prod {ΞΉ} (f g : ΞΉ β cardinal) (H : β i, f i < g i) : sum f < prod g :=
lt_of_not_ge $ Ξ» β¨Fβ©, begin
have : inhabited (Ξ (i : ΞΉ), (g i).out),
{ refine β¨Ξ» i, classical.choice $ ne_zero_iff_nonempty.1 _β©,
rw mk_out,
exact ne_of_gt (lt_of_le_of_lt (zero_le _) (H i)) }, resetI,
let G := inv_fun F,
have sG : surjective G := inv_fun_surjective F.2,
choose C hc using show β i, β b, β a, G β¨i, aβ© i β b,
{ assume i,
simp only [- not_exists, not_exists.symm, classical.not_forall.symm],
refine Ξ» h, not_le_of_lt (H i) _,
rw [β mk_out (f i), β mk_out (g i)],
exact β¨embedding.of_surjective hβ© },
exact (let β¨β¨i, aβ©, hβ© := sG C in hc i a (congr_fun h _))
end
@[simp] theorem mk_empty : mk empty = 0 :=
fintype_card empty
@[simp] theorem mk_pempty : mk pempty = 0 :=
fintype_card pempty
@[simp] theorem mk_plift_of_false {p : Prop} (h : Β¬ p) : mk (plift p) = 0 :=
quotient.sound β¨equiv.plift.trans $ equiv.equiv_pempty hβ©
theorem mk_unit : mk unit = 1 :=
(fintype_card unit).trans nat.cast_one
@[simp] theorem mk_punit : mk punit = 1 :=
(fintype_card punit).trans nat.cast_one
@[simp] theorem mk_singleton {Ξ± : Type u} (x : Ξ±) : mk ({x} : set Ξ±) = 1 :=
quotient.sound β¨equiv.set.singleton xβ©
@[simp] theorem mk_plift_of_true {p : Prop} (h : p) : mk (plift p) = 1 :=
quotient.sound β¨equiv.plift.trans $ equiv.prop_equiv_punit hβ©
@[simp] theorem mk_bool : mk bool = 2 :=
quotient.sound β¨equiv.bool_equiv_punit_sum_punitβ©
@[simp] theorem mk_Prop : mk Prop = 2 :=
(quotient.sound β¨equiv.Prop_equiv_boolβ© : mk Prop = mk bool).trans mk_bool
@[simp] theorem mk_option {Ξ± : Type u} : mk (option Ξ±) = mk Ξ± + 1 :=
quotient.sound β¨equiv.option_equiv_sum_punit Ξ±β©
theorem mk_list_eq_sum_pow (Ξ± : Type u) : mk (list Ξ±) = sum (Ξ» n : β, (mk Ξ±)^(n:cardinal.{u})) :=
calc mk (list Ξ±)
= mk (Ξ£ n, vector Ξ± n) : quotient.sound β¨(equiv.sigma_preimage_equiv list.length).symmβ©
... = mk (Ξ£ n, fin n β Ξ±) : quotient.sound β¨equiv.sigma_congr_right $ Ξ» n,
β¨vector.nth, vector.of_fn, vector.of_fn_nth, Ξ» f, funext $ vector.nth_of_fn fβ©β©
... = mk (Ξ£ n : β, ulift.{u} (fin n) β Ξ±) : quotient.sound β¨equiv.sigma_congr_right $ Ξ» n,
equiv.arrow_congr equiv.ulift.symm (equiv.refl Ξ±)β©
... = sum (Ξ» n : β, (mk Ξ±)^(n:cardinal.{u})) : by simp only [(lift_mk_fin _).symm, lift_mk, power_def, sum_mk]
theorem mk_quot_le {Ξ± : Type u} {r : Ξ± β Ξ± β Prop} : mk (quot r) β€ mk Ξ± :=
mk_le_of_surjective quot.exists_rep
theorem mk_quotient_le {Ξ± : Type u} {s : setoid Ξ±} : mk (quotient s) β€ mk Ξ± :=
mk_quot_le
theorem mk_subtype_le {Ξ± : Type u} (p : Ξ± β Prop) : mk (subtype p) β€ mk Ξ± :=
β¨embedding.subtype pβ©
theorem mk_subtype_le_of_subset {Ξ± : Type u} {p q : Ξ± β Prop} (h : β β¦xβ¦, p x β q x) :
mk (subtype p) β€ mk (subtype q) :=
β¨embedding.subtype_map (embedding.refl Ξ±) hβ©
@[simp] theorem mk_emptyc (Ξ± : Type u) : mk (β
: set Ξ±) = 0 :=
quotient.sound β¨equiv.set.pempty Ξ±β©
theorem mk_univ {Ξ± : Type u} : mk (@univ Ξ±) = mk Ξ± :=
quotient.sound β¨equiv.set.univ Ξ±β©
theorem mk_image_le {Ξ± Ξ² : Type u} {f : Ξ± β Ξ²} {s : set Ξ±} : mk (f '' s) β€ mk s :=
mk_le_of_surjective surjective_onto_image
theorem mk_image_le_lift {Ξ± : Type u} {Ξ² : Type v} {f : Ξ± β Ξ²} {s : set Ξ±} :
lift.{v u} (mk (f '' s)) β€ lift.{u v} (mk s) :=
lift_mk_le.{v u 0}.mpr β¨embedding.of_surjective surjective_onto_imageβ©
theorem mk_range_le {Ξ± Ξ² : Type u} {f : Ξ± β Ξ²} : mk (range f) β€ mk Ξ± :=
mk_le_of_surjective surjective_onto_range
lemma mk_range_eq (f : Ξ± β Ξ²) (h : injective f) : mk (range f) = mk Ξ± :=
quotient.sound β¨(equiv.set.range f h).symmβ©
lemma mk_range_eq_of_inj {Ξ± : Type u} {Ξ² : Type v} {f : Ξ± β Ξ²} (hf : injective f) :
lift.{v u} (mk (range f)) = lift.{u v} (mk Ξ±) :=
begin
have := (@lift_mk_eq.{v u max u v} (range f) Ξ±).2 β¨(equiv.set.range f hf).symmβ©,
simp only [lift_umax.{u v}, lift_umax.{v u}] at this,
exact this
end
lemma mk_range_eq_lift {Ξ± : Type u} {Ξ² : Type v} {f : Ξ± β Ξ²} (hf : injective f) :
lift.{v (max u w)} (# (range f)) = lift.{u (max v w)} (# Ξ±) :=
lift_mk_eq.mpr β¨(equiv.set.range f hf).symmβ©
theorem mk_image_eq {Ξ± Ξ² : Type u} {f : Ξ± β Ξ²} {s : set Ξ±} (hf : injective f) :
mk (f '' s) = mk s :=
quotient.sound β¨(equiv.set.image f s hf).symmβ©
theorem mk_Union_le_sum_mk {Ξ± ΞΉ : Type u} {f : ΞΉ β set Ξ±} : mk (β i, f i) β€ sum (Ξ» i, mk (f i)) :=
calc mk (β i, f i) β€ mk (Ξ£ i, f i) : mk_le_of_surjective (set.surjective_sigma_to_Union f)
... = sum (Ξ» i, mk (f i)) : (sum_mk _).symm
theorem mk_Union_eq_sum_mk {Ξ± ΞΉ : Type u} {f : ΞΉ β set Ξ±} (h : βi j, i β j β disjoint (f i) (f j)) :
mk (β i, f i) = sum (Ξ» i, mk (f i)) :=
calc mk (β i, f i) = mk (Ξ£i, f i) : quot.sound β¨set.Union_eq_sigma_of_disjoint hβ©
... = sum (Ξ»i, mk (f i)) : (sum_mk _).symm
lemma mk_Union_le {Ξ± ΞΉ : Type u} (f : ΞΉ β set Ξ±) :
mk (β i, f i) β€ mk ΞΉ * cardinal.sup.{u u} (Ξ» i, mk (f i)) :=
le_trans mk_Union_le_sum_mk (sum_le_sup _)
lemma mk_sUnion_le {Ξ± : Type u} (A : set (set Ξ±)) :
mk (ββ A) β€ mk A * cardinal.sup.{u u} (Ξ» s : A, mk s) :=
by { rw [sUnion_eq_Union], apply mk_Union_le }
lemma mk_bUnion_le {ΞΉ Ξ± : Type u} (A : ΞΉ β set Ξ±) (s : set ΞΉ) :
mk (β(x β s), A x) β€ mk s * cardinal.sup.{u u} (Ξ» x : s, mk (A x.1)) :=
by { rw [bUnion_eq_Union], apply mk_Union_le }
@[simp] lemma finset_card {Ξ± : Type u} {s : finset Ξ±} : β(finset.card s) = mk (βs : set Ξ±) :=
by rw [fintype_card, nat_cast_inj, fintype.card_coe]
lemma finset_card_lt_omega (s : finset Ξ±) : mk (βs : set Ξ±) < omega :=
by { rw [lt_omega_iff_fintype], exact β¨finset.subtype.fintype sβ© }
theorem mk_union_add_mk_inter {Ξ± : Type u} {S T : set Ξ±} : mk (S βͺ T : set Ξ±) + mk (S β© T : set Ξ±) = mk S + mk T :=
quot.sound β¨equiv.set.union_sum_inter S Tβ©
theorem mk_union_of_disjoint {Ξ± : Type u} {S T : set Ξ±} (H : disjoint S T) : mk (S βͺ T : set Ξ±) = mk S + mk T :=
quot.sound β¨equiv.set.union (disjoint_iff.1 H)β©
lemma mk_sum_compl {Ξ±} (s : set Ξ±) : #s + #(-s : set Ξ±) = #Ξ± :=
quotient.sound β¨equiv.set.sum_compl sβ©
lemma mk_le_mk_of_subset {Ξ±} {s t : set Ξ±} (h : s β t) : mk s β€ mk t :=
β¨ set.embedding_of_subset h β©
lemma mk_subtype_mono {p q : Ξ± β Prop} (h : βx, p x β q x) : mk {x // p x} β€ mk {x // q x} :=
β¨embedding_of_subset hβ©
lemma mk_set_le (s : set Ξ±) : mk s β€ mk Ξ± :=
mk_subtype_le s
lemma mk_image_eq_lift {Ξ± : Type u} {Ξ² : Type v} (f : Ξ± β Ξ²) (s : set Ξ±) (h : injective f) :
lift.{v u} (mk (f '' s)) = lift.{u v} (mk s) :=
lift_mk_eq.{v u 0}.mpr β¨(equiv.set.image f s h).symmβ©
lemma mk_image_eq_of_inj_on_lift {Ξ± : Type u} {Ξ² : Type v} (f : Ξ± β Ξ²) (s : set Ξ±)
(h : inj_on f s) : lift.{v u} (mk (f '' s)) = lift.{u v} (mk s) :=
lift_mk_eq.{v u 0}.mpr β¨(equiv.set.image_of_inj_on f s h).symmβ©
lemma mk_image_eq_of_inj_on {Ξ± Ξ² : Type u} (f : Ξ± β Ξ²) (s : set Ξ±) (h : inj_on f s) :
mk (f '' s) = mk s :=
quotient.sound β¨(equiv.set.image_of_inj_on f s h).symmβ©
lemma mk_subtype_of_equiv {Ξ± Ξ² : Type u} (p : Ξ± β Prop) (e : Ξ± β Ξ²) :
mk {a : Ξ± // p a} = mk {b : Ξ² // p (e.symm b)} :=
quotient.sound β¨equiv.subtype_equiv_of_subtype' eβ©
lemma mk_sep (s : set Ξ±) (t : Ξ± β Prop) : mk ({ x β s | t x } : set Ξ±) = mk { x : s | t x.1 } :=
quotient.sound β¨equiv.set.sep s tβ©
lemma mk_preimage_of_injective_lift {Ξ± : Type u} {Ξ² : Type v} (f : Ξ± β Ξ²) (s : set Ξ²)
(h : injective f) : lift.{u v} (mk (f β»ΒΉ' s)) β€ lift.{v u} (mk s) :=
begin
rw lift_mk_le.{u v 0}, use subtype.coind (Ξ» x, f x.1) (Ξ» x, x.2),
apply subtype.coind_injective, exact injective_comp h subtype.val_injective
end
lemma mk_preimage_of_subset_range_lift {Ξ± : Type u} {Ξ² : Type v} (f : Ξ± β Ξ²) (s : set Ξ²)
(h : s β range f) : lift.{v u} (mk s) β€ lift.{u v} (mk (f β»ΒΉ' s)) :=
begin
rw lift_mk_le.{v u 0},
refine β¨β¨_, _β©β©,
{ rintro β¨y, hyβ©, rcases classical.subtype_of_exists (h hy) with β¨x, rflβ©, exact β¨x, hyβ© },
rintro β¨y, hyβ© β¨y', hy'β©, dsimp,
rcases classical.subtype_of_exists (h hy) with β¨x, rflβ©,
rcases classical.subtype_of_exists (h hy') with β¨x', rflβ©,
simp, intro hxx', rw hxx'
end
lemma mk_preimage_of_injective_of_subset_range_lift {Ξ² : Type v} (f : Ξ± β Ξ²) (s : set Ξ²)
(h : injective f) (h2 : s β range f) : lift.{u v} (mk (f β»ΒΉ' s)) = lift.{v u} (mk s) :=
le_antisymm (mk_preimage_of_injective_lift f s h) (mk_preimage_of_subset_range_lift f s h2)
lemma mk_preimage_of_injective (f : Ξ± β Ξ²) (s : set Ξ²) (h : injective f) :
mk (f β»ΒΉ' s) β€ mk s :=
by { convert mk_preimage_of_injective_lift.{u u} f s h using 1; rw [lift_id] }
lemma mk_preimage_of_subset_range (f : Ξ± β Ξ²) (s : set Ξ²)
(h : s β range f) : mk s β€ mk (f β»ΒΉ' s) :=
by { convert mk_preimage_of_subset_range_lift.{u u} f s h using 1; rw [lift_id] }
lemma mk_preimage_of_injective_of_subset_range (f : Ξ± β Ξ²) (s : set Ξ²)
(h : injective f) (h2 : s β range f) : mk (f β»ΒΉ' s) = mk s :=
by { convert mk_preimage_of_injective_of_subset_range_lift.{u u} f s h h2 using 1; rw [lift_id] }
lemma mk_subset_ge_of_subset_image_lift {Ξ± : Type u} {Ξ² : Type v} (f : Ξ± β Ξ²) {s : set Ξ±}
{t : set Ξ²} (h : t β f '' s) :
lift.{v u} (mk t) β€ lift.{u v} (mk ({ x β s | f x β t } : set Ξ±)) :=
by { rw [image_eq_range] at h, convert mk_preimage_of_subset_range_lift _ _ h using 1, rw [mk_sep], refl }
lemma mk_subset_ge_of_subset_image (f : Ξ± β Ξ²) {s : set Ξ±} {t : set Ξ²} (h : t β f '' s) :
mk t β€ mk ({ x β s | f x β t } : set Ξ±) :=
by { rw [image_eq_range] at h, convert mk_preimage_of_subset_range _ _ h using 1, rw [mk_sep], refl }
theorem le_mk_iff_exists_subset {c : cardinal} {Ξ± : Type u} {s : set Ξ±} :
c β€ mk s β β p : set Ξ±, p β s β§ mk p = c :=
begin
rw [le_mk_iff_exists_set, βsubtype.exists_set_subtype],
apply exists_congr, intro t, rw [mk_image_eq], apply subtype.val_injective
end
/-- The function Ξ±^{<Ξ²}, defined to be sup_{Ξ³ < Ξ²} Ξ±^Ξ³.
We index over {s : set Ξ².out // mk s < Ξ² } instead of {Ξ³ // Ξ³ < Ξ²}, because the latter lives in a
higher universe -/
noncomputable def powerlt (Ξ± Ξ² : cardinal.{u}) : cardinal.{u} :=
sup.{u u} (Ξ»(s : {s : set Ξ².out // mk s < Ξ²}), Ξ± ^ mk.{u} s)
infix ` ^< `:80 := powerlt
theorem powerlt_aux {c c' : cardinal} (h : c < c') :
β(s : {s : set c'.out // mk s < c'}), mk s = c :=
begin
cases out_embedding.mp (le_of_lt h) with f,
have : mk β₯(range βf) = c, { rwa [mk_range_eq, mk, quotient.out_eq c], exact f.2 },
exact β¨β¨range f, by convert hβ©, thisβ©
end
lemma le_powerlt {cβ cβ cβ : cardinal} (h : cβ < cβ) : cβ ^ cβ β€ cβ ^< cβ :=
by { rcases powerlt_aux h with β¨s, rflβ©, apply le_sup _ s }
lemma powerlt_le {cβ cβ cβ : cardinal} : cβ ^< cβ β€ cβ β β(cβ < cβ), cβ ^ cβ β€ cβ :=
begin
rw [powerlt, sup_le],
split,
{ intros h cβ hcβ, rcases powerlt_aux hcβ with β¨s, rflβ©, exact h s },
intros h s, exact h _ s.2
end
lemma powerlt_le_powerlt_left {a b c : cardinal} (h : b β€ c) : a ^< b β€ a ^< c :=
by { rw [powerlt, sup_le], rintro β¨s, hsβ©, apply le_powerlt, exact lt_of_lt_of_le hs h }
lemma powerlt_succ {cβ cβ : cardinal} (h : cβ β 0) : cβ ^< cβ.succ = cβ ^ cβ :=
begin
apply le_antisymm,
{ rw powerlt_le, intros cβ h2, apply power_le_power_left h, rwa [βlt_succ] },
{ apply le_powerlt, apply lt_succ_self }
end
lemma powerlt_max {cβ cβ cβ : cardinal} : cβ ^< max cβ cβ = max (cβ ^< cβ) (cβ ^< cβ) :=
by { cases le_total cβ cβ; simp only [max_eq_left, max_eq_right, h, powerlt_le_powerlt_left] }
lemma zero_powerlt {a : cardinal} (h : a β 0) : 0 ^< a = 1 :=
begin
apply le_antisymm,
{ rw [powerlt_le], intros c hc, apply zero_power_le },
convert le_powerlt (pos_iff_ne_zero.2 h), rw [power_zero]
end
lemma powerlt_zero {a : cardinal} : a ^< 0 = 0 :=
by { apply sup_eq_zero, rintro β¨x, hxβ©, rw [βnot_le] at hx, apply hx, apply zero_le }
end cardinal
|
83031c73cb07040778bd1340b24dcd42226fd050 | bb31430994044506fa42fd667e2d556327e18dfe | /src/ring_theory/polynomial/cyclotomic/basic.lean | 7ec13bdc53eedfbccd2f67e4cdfff5b18caab9f7 | [
"Apache-2.0"
] | permissive | sgouezel/mathlib | 0cb4e5335a2ba189fa7af96d83a377f83270e503 | 00638177efd1b2534fc5269363ebf42a7871df9a | refs/heads/master | 1,674,527,483,042 | 1,673,665,568,000 | 1,673,665,568,000 | 119,598,202 | 0 | 0 | null | 1,517,348,647,000 | 1,517,348,646,000 | null | UTF-8 | Lean | false | false | 47,237 | lean | /-
Copyright (c) 2020 Riccardo Brasca. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Riccardo Brasca
-/
import algebra.ne_zero
import algebra.polynomial.big_operators
import analysis.complex.roots_of_unity
import data.polynomial.lifts
import data.polynomial.splits
import data.zmod.algebra
import field_theory.ratfunc
import field_theory.separable
import number_theory.arithmetic_function
import ring_theory.roots_of_unity
/-!
# Cyclotomic polynomials.
For `n : β` and an integral domain `R`, we define a modified version of the `n`-th cyclotomic
polynomial with coefficients in `R`, denoted `cyclotomic' n R`, as `β (X - ΞΌ)`, where `ΞΌ` varies
over the primitive `n`th roots of unity. If there is a primitive `n`th root of unity in `R` then
this the standard definition. We then define the standard cyclotomic polynomial `cyclotomic n R`
with coefficients in any ring `R`.
## Main definition
* `cyclotomic n R` : the `n`-th cyclotomic polynomial with coefficients in `R`.
## Main results
* `int_coeff_of_cycl` : If there is a primitive `n`-th root of unity in `K`, then `cyclotomic' n K`
comes from a polynomial with integer coefficients.
* `deg_of_cyclotomic` : The degree of `cyclotomic n` is `totient n`.
* `prod_cyclotomic_eq_X_pow_sub_one` : `X ^ n - 1 = β (cyclotomic i)`, where `i` divides `n`.
* `cyclotomic_eq_prod_X_pow_sub_one_pow_moebius` : The MΓΆbius inversion formula for
`cyclotomic n R` over an abstract fraction field for `R[X]`.
* `cyclotomic.irreducible` : `cyclotomic n β€` is irreducible.
## Implementation details
Our definition of `cyclotomic' n R` makes sense in any integral domain `R`, but the interesting
results hold if there is a primitive `n`-th root of unity in `R`. In particular, our definition is
not the standard one unless there is a primitive `n`th root of unity in `R`. For example,
`cyclotomic' 3 β€ = 1`, since there are no primitive cube roots of unity in `β€`. The main example is
`R = β`, we decided to work in general since the difficulties are essentially the same.
To get the standard cyclotomic polynomials, we use `int_coeff_of_cycl`, with `R = β`, to get a
polynomial with integer coefficients and then we map it to `R[X]`, for any ring `R`.
To prove `cyclotomic.irreducible`, the irreducibility of `cyclotomic n β€`, we show in
`cyclotomic_eq_minpoly` that `cyclotomic n β€` is the minimal polynomial of any `n`-th primitive root
of unity `ΞΌ : K`, where `K` is a field of characteristic `0`.
-/
open_locale classical big_operators polynomial
noncomputable theory
universe u
namespace polynomial
section cyclotomic'
section is_domain
variables {R : Type*} [comm_ring R] [is_domain R]
/-- The modified `n`-th cyclotomic polynomial with coefficients in `R`, it is the usual cyclotomic
polynomial if there is a primitive `n`-th root of unity in `R`. -/
def cyclotomic' (n : β) (R : Type*) [comm_ring R] [is_domain R] : R[X] :=
β ΞΌ in primitive_roots n R, (X - C ΞΌ)
/-- The zeroth modified cyclotomic polyomial is `1`. -/
@[simp] lemma cyclotomic'_zero
(R : Type*) [comm_ring R] [is_domain R] : cyclotomic' 0 R = 1 :=
by simp only [cyclotomic', finset.prod_empty, primitive_roots_zero]
/-- The first modified cyclotomic polyomial is `X - 1`. -/
@[simp] lemma cyclotomic'_one
(R : Type*) [comm_ring R] [is_domain R] : cyclotomic' 1 R = X - 1 :=
begin
simp only [cyclotomic', finset.prod_singleton, ring_hom.map_one,
is_primitive_root.primitive_roots_one]
end
/-- The second modified cyclotomic polyomial is `X + 1` if the characteristic of `R` is not `2`. -/
@[simp] lemma cyclotomic'_two
(R : Type*) [comm_ring R] [is_domain R] (p : β) [char_p R p] (hp : p β 2) :
cyclotomic' 2 R = X + 1 :=
begin
rw [cyclotomic'],
have prim_root_two : primitive_roots 2 R = {(-1 : R)},
{ simp only [finset.eq_singleton_iff_unique_mem, mem_primitive_roots two_pos],
exact β¨is_primitive_root.neg_one p hp, Ξ» x, is_primitive_root.eq_neg_one_of_two_rightβ© },
simp only [prim_root_two, finset.prod_singleton, ring_hom.map_neg, ring_hom.map_one,
sub_neg_eq_add]
end
/-- `cyclotomic' n R` is monic. -/
lemma cyclotomic'.monic
(n : β) (R : Type*) [comm_ring R] [is_domain R] : (cyclotomic' n R).monic :=
monic_prod_of_monic _ _ $ Ξ» z hz, monic_X_sub_C _
/-- `cyclotomic' n R` is different from `0`. -/
lemma cyclotomic'_ne_zero
(n : β) (R : Type*) [comm_ring R] [is_domain R] : cyclotomic' n R β 0 :=
(cyclotomic'.monic n R).ne_zero
/-- The natural degree of `cyclotomic' n R` is `totient n` if there is a primitive root of
unity in `R`. -/
lemma nat_degree_cyclotomic' {ΞΆ : R} {n : β} (h : is_primitive_root ΞΆ n) :
(cyclotomic' n R).nat_degree = nat.totient n :=
begin
rw [cyclotomic'],
rw nat_degree_prod (primitive_roots n R) (Ξ» (z : R), (X - C z)),
simp only [is_primitive_root.card_primitive_roots h, mul_one,
nat_degree_X_sub_C,
nat.cast_id, finset.sum_const, nsmul_eq_mul],
intros z hz,
exact X_sub_C_ne_zero z
end
/-- The degree of `cyclotomic' n R` is `totient n` if there is a primitive root of unity in `R`. -/
lemma degree_cyclotomic' {ΞΆ : R} {n : β} (h : is_primitive_root ΞΆ n) :
(cyclotomic' n R).degree = nat.totient n :=
by simp only [degree_eq_nat_degree (cyclotomic'_ne_zero n R), nat_degree_cyclotomic' h]
/-- The roots of `cyclotomic' n R` are the primitive `n`-th roots of unity. -/
lemma roots_of_cyclotomic (n : β) (R : Type*) [comm_ring R] [is_domain R] :
(cyclotomic' n R).roots = (primitive_roots n R).val :=
by { rw cyclotomic', exact roots_prod_X_sub_C (primitive_roots n R) }
/-- If there is a primitive `n`th root of unity in `K`, then `X ^ n - 1 = β (X - ΞΌ)`, where `ΞΌ`
varies over the `n`-th roots of unity. -/
lemma X_pow_sub_one_eq_prod {ΞΆ : R} {n : β} (hpos : 0 < n) (h : is_primitive_root ΞΆ n) :
X ^ n - 1 = β ΞΆ in nth_roots_finset n R, (X - C ΞΆ) :=
begin
rw [nth_roots_finset, β multiset.to_finset_eq (is_primitive_root.nth_roots_nodup h)],
simp only [finset.prod_mk, ring_hom.map_one],
rw [nth_roots],
have hmonic : (X ^ n - C (1 : R)).monic := monic_X_pow_sub_C (1 : R) (ne_of_lt hpos).symm,
symmetry,
apply prod_multiset_X_sub_C_of_monic_of_roots_card_eq hmonic,
rw [@nat_degree_X_pow_sub_C R _ _ n 1, β nth_roots],
exact is_primitive_root.card_nth_roots h
end
end is_domain
section field
variables {K : Type*} [field K]
/-- `cyclotomic' n K` splits. -/
lemma cyclotomic'_splits (n : β) : splits (ring_hom.id K) (cyclotomic' n K) :=
begin
apply splits_prod (ring_hom.id K),
intros z hz,
simp only [splits_X_sub_C (ring_hom.id K)]
end
/-- If there is a primitive `n`-th root of unity in `K`, then `X ^ n - 1`splits. -/
lemma X_pow_sub_one_splits {ΞΆ : K} {n : β} (h : is_primitive_root ΞΆ n) :
splits (ring_hom.id K) (X ^ n - C (1 : K)) :=
by rw [splits_iff_card_roots, β nth_roots, is_primitive_root.card_nth_roots h,
nat_degree_X_pow_sub_C]
/-- If there is a primitive `n`-th root of unity in `K`, then
`β i in nat.divisors n, cyclotomic' i K = X ^ n - 1`. -/
lemma prod_cyclotomic'_eq_X_pow_sub_one {K : Type*} [comm_ring K] [is_domain K] {ΞΆ : K} {n : β}
(hpos : 0 < n) (h : is_primitive_root ΞΆ n) : β i in nat.divisors n, cyclotomic' i K = X ^ n - 1 :=
have hd : (n.divisors : set β).pairwise_disjoint (Ξ» k, primitive_roots k K),
from Ξ» x hx y hy hne, is_primitive_root.disjoint hne,
by simp only [X_pow_sub_one_eq_prod hpos h, cyclotomic', β finset.prod_bUnion hd,
h.nth_roots_one_eq_bUnion_primitive_roots]
/-- If there is a primitive `n`-th root of unity in `K`, then
`cyclotomic' n K = (X ^ k - 1) /β (β i in nat.proper_divisors k, cyclotomic' i K)`. -/
lemma cyclotomic'_eq_X_pow_sub_one_div {K : Type*} [comm_ring K] [is_domain K] {ΞΆ : K} {n : β}
(hpos : 0 < n) (h : is_primitive_root ΞΆ n) :
cyclotomic' n K = (X ^ n - 1) /β (β i in nat.proper_divisors n, cyclotomic' i K) :=
begin
rw [βprod_cyclotomic'_eq_X_pow_sub_one hpos h,
nat.divisors_eq_proper_divisors_insert_self_of_pos hpos,
finset.prod_insert nat.proper_divisors.not_self_mem],
have prod_monic : (β i in nat.proper_divisors n, cyclotomic' i K).monic,
{ apply monic_prod_of_monic,
intros i hi,
exact cyclotomic'.monic i K },
rw (div_mod_by_monic_unique (cyclotomic' n K) 0 prod_monic _).1,
simp only [degree_zero, zero_add],
refine β¨by rw mul_comm, _β©,
rw [bot_lt_iff_ne_bot],
intro h,
exact monic.ne_zero prod_monic (degree_eq_bot.1 h)
end
/-- If there is a primitive `n`-th root of unity in `K`, then `cyclotomic' n K` comes from a
monic polynomial with integer coefficients. -/
lemma int_coeff_of_cyclotomic' {K : Type*} [comm_ring K] [is_domain K] {ΞΆ : K} {n : β}
(h : is_primitive_root ΞΆ n) :
(β (P : β€[X]), map (int.cast_ring_hom K) P = cyclotomic' n K β§
P.degree = (cyclotomic' n K).degree β§ P.monic) :=
begin
refine lifts_and_degree_eq_and_monic _ (cyclotomic'.monic n K),
induction n using nat.strong_induction_on with k ihk generalizing ΞΆ h,
rcases k.eq_zero_or_pos with rfl|hpos,
{ use 1,
simp only [cyclotomic'_zero, coe_map_ring_hom, polynomial.map_one] },
let B : K[X] := β i in nat.proper_divisors k, cyclotomic' i K,
have Bmo : B.monic,
{ apply monic_prod_of_monic,
intros i hi,
exact (cyclotomic'.monic i K) },
have Bint : B β lifts (int.cast_ring_hom K),
{ refine subsemiring.prod_mem (lifts (int.cast_ring_hom K)) _,
intros x hx,
have xsmall := (nat.mem_proper_divisors.1 hx).2,
obtain β¨d, hdβ© := (nat.mem_proper_divisors.1 hx).1,
rw [mul_comm] at hd,
exact ihk x xsmall (h.pow hpos hd) },
replace Bint := lifts_and_degree_eq_and_monic Bint Bmo,
obtain β¨Bβ, hBβ, hBβdeg, hBβmoβ© := Bint,
let Qβ : β€[X] := (X ^ k - 1) /β Bβ,
have huniq : 0 + B * cyclotomic' k K = X ^ k - 1 β§ (0 : K[X]).degree < B.degree,
{ split,
{ rw [zero_add, mul_comm, β(prod_cyclotomic'_eq_X_pow_sub_one hpos h),
nat.divisors_eq_proper_divisors_insert_self_of_pos hpos],
simp only [true_and, finset.prod_insert, not_lt, nat.mem_proper_divisors, dvd_refl] },
rw [degree_zero, bot_lt_iff_ne_bot],
intro habs,
exact (monic.ne_zero Bmo) (degree_eq_bot.1 habs) },
replace huniq := div_mod_by_monic_unique (cyclotomic' k K) (0 : K[X]) Bmo huniq,
simp only [lifts, ring_hom.mem_srange],
use Qβ,
rw [coe_map_ring_hom, (map_div_by_monic (int.cast_ring_hom K) hBβmo), hBβ, β huniq.1],
simp
end
/-- If `K` is of characteristic `0` and there is a primitive `n`-th root of unity in `K`,
then `cyclotomic n K` comes from a unique polynomial with integer coefficients. -/
lemma unique_int_coeff_of_cycl {K : Type*} [comm_ring K] [is_domain K] [char_zero K] {ΞΆ : K}
{n : β+} (h : is_primitive_root ΞΆ n) :
(β! (P : β€[X]), map (int.cast_ring_hom K) P = cyclotomic' n K) :=
begin
obtain β¨P, hPβ© := int_coeff_of_cyclotomic' h,
refine β¨P, hP.1, Ξ» Q hQ, _β©,
apply (map_injective (int.cast_ring_hom K) int.cast_injective),
rw [hP.1, hQ]
end
end field
end cyclotomic'
section cyclotomic
/-- The `n`-th cyclotomic polynomial with coefficients in `R`. -/
def cyclotomic (n : β) (R : Type*) [ring R] : R[X] :=
if h : n = 0 then 1 else
map (int.cast_ring_hom R) ((int_coeff_of_cyclotomic' (complex.is_primitive_root_exp n h)).some)
lemma int_cyclotomic_rw {n : β} (h : n β 0) :
cyclotomic n β€ = (int_coeff_of_cyclotomic' (complex.is_primitive_root_exp n h)).some :=
begin
simp only [cyclotomic, h, dif_neg, not_false_iff],
ext i,
simp only [coeff_map, int.cast_id, eq_int_cast]
end
/-- `cyclotomic n R` comes from `cyclotomic n β€`. -/
lemma map_cyclotomic_int (n : β) (R : Type*) [ring R] :
map (int.cast_ring_hom R) (cyclotomic n β€) = cyclotomic n R :=
begin
by_cases hzero : n = 0,
{ simp only [hzero, cyclotomic, dif_pos, polynomial.map_one] },
simp only [cyclotomic, int_cyclotomic_rw, hzero, ne.def, dif_neg, not_false_iff]
end
lemma int_cyclotomic_spec (n : β) : map (int.cast_ring_hom β) (cyclotomic n β€) = cyclotomic' n β β§
(cyclotomic n β€).degree = (cyclotomic' n β).degree β§ (cyclotomic n β€).monic :=
begin
by_cases hzero : n = 0,
{ simp only [hzero, cyclotomic, degree_one, monic_one, cyclotomic'_zero, dif_pos,
eq_self_iff_true, polynomial.map_one, and_self] },
rw int_cyclotomic_rw hzero,
exact (int_coeff_of_cyclotomic' (complex.is_primitive_root_exp n hzero)).some_spec
end
lemma int_cyclotomic_unique {n : β} {P : β€[X]} (h : map (int.cast_ring_hom β) P =
cyclotomic' n β) : P = cyclotomic n β€ :=
begin
apply map_injective (int.cast_ring_hom β) int.cast_injective,
rw [h, (int_cyclotomic_spec n).1]
end
/-- The definition of `cyclotomic n R` commutes with any ring homomorphism. -/
@[simp] lemma map_cyclotomic (n : β) {R S : Type*} [ring R] [ring S] (f : R β+* S) :
map f (cyclotomic n R) = cyclotomic n S :=
begin
rw [βmap_cyclotomic_int n R, βmap_cyclotomic_int n S, map_map],
congr
end
lemma cyclotomic.eval_apply {R S : Type*} (q : R) (n : β) [ring R] [ring S] (f : R β+* S) :
eval (f q) (cyclotomic n S) = f (eval q (cyclotomic n R)) :=
by rw [β map_cyclotomic n f, eval_map, evalβ_at_apply]
/-- The zeroth cyclotomic polyomial is `1`. -/
@[simp] lemma cyclotomic_zero (R : Type*) [ring R] : cyclotomic 0 R = 1 :=
by simp only [cyclotomic, dif_pos]
/-- The first cyclotomic polyomial is `X - 1`. -/
@[simp] lemma cyclotomic_one (R : Type*) [ring R] : cyclotomic 1 R = X - 1 :=
begin
have hspec : map (int.cast_ring_hom β) (X - 1) = cyclotomic' 1 β,
{ simp only [cyclotomic'_one, pnat.one_coe, map_X, polynomial.map_one, polynomial.map_sub] },
symmetry,
rw [βmap_cyclotomic_int, β(int_cyclotomic_unique hspec)],
simp only [map_X, polynomial.map_one, polynomial.map_sub]
end
/-- `cyclotomic n` is monic. -/
lemma cyclotomic.monic (n : β) (R : Type*) [ring R] : (cyclotomic n R).monic :=
begin
rw βmap_cyclotomic_int,
exact (int_cyclotomic_spec n).2.2.map _,
end
/-- `cyclotomic n` is primitive. -/
lemma cyclotomic.is_primitive (n : β) (R : Type*) [comm_ring R] : (cyclotomic n R).is_primitive :=
(cyclotomic.monic n R).is_primitive
/-- `cyclotomic n R` is different from `0`. -/
lemma cyclotomic_ne_zero (n : β) (R : Type*) [ring R] [nontrivial R] : cyclotomic n R β 0 :=
(cyclotomic.monic n R).ne_zero
/-- The degree of `cyclotomic n` is `totient n`. -/
lemma degree_cyclotomic (n : β) (R : Type*) [ring R] [nontrivial R] :
(cyclotomic n R).degree = nat.totient n :=
begin
rw βmap_cyclotomic_int,
rw degree_map_eq_of_leading_coeff_ne_zero (int.cast_ring_hom R) _,
{ cases n with k,
{ simp only [cyclotomic, degree_one, dif_pos, nat.totient_zero, with_top.coe_zero]},
rw [βdegree_cyclotomic' (complex.is_primitive_root_exp k.succ (nat.succ_ne_zero k))],
exact (int_cyclotomic_spec k.succ).2.1 },
simp only [(int_cyclotomic_spec n).right.right, eq_int_cast, monic.leading_coeff,
int.cast_one, ne.def, not_false_iff, one_ne_zero]
end
/-- The natural degree of `cyclotomic n` is `totient n`. -/
lemma nat_degree_cyclotomic (n : β) (R : Type*) [ring R] [nontrivial R] :
(cyclotomic n R).nat_degree = nat.totient n :=
by rw [nat_degree, degree_cyclotomic, with_bot.unbot'_coe]
/-- The degree of `cyclotomic n R` is positive. -/
lemma degree_cyclotomic_pos (n : β) (R : Type*) (hpos : 0 < n) [ring R] [nontrivial R] :
0 < (cyclotomic n R).degree := by
{ rw degree_cyclotomic n R, exact_mod_cast (nat.totient_pos hpos) }
open finset
/-- `β i in nat.divisors n, cyclotomic i R = X ^ n - 1`. -/
lemma prod_cyclotomic_eq_X_pow_sub_one {n : β} (hpos : 0 < n) (R : Type*) [comm_ring R] :
β i in nat.divisors n, cyclotomic i R = X ^ n - 1 :=
begin
have integer : β i in nat.divisors n, cyclotomic i β€ = X ^ n - 1,
{ apply map_injective (int.cast_ring_hom β) int.cast_injective,
simp only [polynomial.map_prod, int_cyclotomic_spec, polynomial.map_pow, map_X,
polynomial.map_one, polynomial.map_sub],
exact prod_cyclotomic'_eq_X_pow_sub_one hpos (complex.is_primitive_root_exp n hpos.ne') },
simpa only [polynomial.map_prod, map_cyclotomic_int, polynomial.map_sub, polynomial.map_one,
polynomial.map_pow, polynomial.map_X] using congr_arg (map (int.cast_ring_hom R)) integer
end
lemma cyclotomic.dvd_X_pow_sub_one (n : β) (R : Type*) [ring R] :
(cyclotomic n R) β£ X ^ n - 1 :=
begin
suffices : cyclotomic n β€ β£ X ^ n - 1,
{ simpa only [map_cyclotomic_int, polynomial.map_sub, polynomial.map_one, polynomial.map_pow,
polynomial.map_X] using map_dvd (int.cast_ring_hom R) this },
rcases n.eq_zero_or_pos with rfl | hn,
{ simp },
rw [β prod_cyclotomic_eq_X_pow_sub_one hn],
exact finset.dvd_prod_of_mem _ (n.mem_divisors_self hn.ne')
end
lemma prod_cyclotomic_eq_geom_sum {n : β} (h : 0 < n) (R) [comm_ring R] :
β i in n.divisors.erase 1, cyclotomic i R = β i in finset.range n, X ^ i :=
suffices β i in n.divisors.erase 1, cyclotomic i β€ = β i in finset.range n, X ^ i,
by simpa only [polynomial.map_prod, map_cyclotomic_int, polynomial.map_sum, polynomial.map_pow,
polynomial.map_X] using congr_arg (map (int.cast_ring_hom R)) this,
by rw [β mul_left_inj' (cyclotomic_ne_zero 1 β€), prod_erase_mul _ _ (nat.one_mem_divisors.2 h.ne'),
cyclotomic_one, geom_sum_mul, prod_cyclotomic_eq_X_pow_sub_one h]
/-- If `p` is prime, then `cyclotomic p R = β i in range p, X ^ i`. -/
lemma cyclotomic_prime (R : Type*) [ring R] (p : β) [hp : fact p.prime] :
cyclotomic p R = β i in finset.range p, X ^ i :=
begin
suffices : cyclotomic p β€ = β i in range p, X ^ i,
{ simpa only [map_cyclotomic_int, polynomial.map_sum, polynomial.map_pow, polynomial.map_X]
using congr_arg (map (int.cast_ring_hom R)) this },
rw [β prod_cyclotomic_eq_geom_sum hp.out.pos, hp.out.divisors,
erase_insert (mem_singleton.not.2 hp.out.ne_one.symm), prod_singleton]
end
lemma cyclotomic_prime_mul_X_sub_one (R : Type*) [ring R] (p : β) [hn : fact (nat.prime p)] :
(cyclotomic p R) * (X - 1) = X ^ p - 1 :=
by rw [cyclotomic_prime, geom_sum_mul]
@[simp] lemma cyclotomic_two (R : Type*) [ring R] : cyclotomic 2 R = X + 1 :=
by simp [cyclotomic_prime]
@[simp] lemma cyclotomic_three (R : Type*) [ring R] : cyclotomic 3 R = X ^ 2 + X + 1 :=
by simp [cyclotomic_prime, sum_range_succ']
lemma cyclotomic_dvd_geom_sum_of_dvd (R) [ring R] {d n : β} (hdn : d β£ n)
(hd : d β 1) : cyclotomic d R β£ β i in finset.range n, X ^ i :=
begin
suffices : cyclotomic d β€ β£ β i in finset.range n, X ^ i,
{ simpa only [map_cyclotomic_int, polynomial.map_sum, polynomial.map_pow, polynomial.map_X]
using map_dvd (int.cast_ring_hom R) this },
rcases n.eq_zero_or_pos with rfl | hn,
{ simp },
rw βprod_cyclotomic_eq_geom_sum hn,
apply finset.dvd_prod_of_mem,
simp [hd, hdn, hn.ne']
end
lemma X_pow_sub_one_mul_prod_cyclotomic_eq_X_pow_sub_one_of_dvd (R) [comm_ring R] {d n : β}
(h : d β n.proper_divisors) :
(X ^ d - 1) * β x in n.divisors \ d.divisors, cyclotomic x R = X ^ n - 1 :=
begin
obtain β¨hd, hdnβ© := nat.mem_proper_divisors.mp h,
have h0n : 0 < n := pos_of_gt hdn,
have h0d : 0 < d := nat.pos_of_dvd_of_pos hd h0n,
rw [βprod_cyclotomic_eq_X_pow_sub_one h0d, βprod_cyclotomic_eq_X_pow_sub_one h0n,
mul_comm, finset.prod_sdiff (nat.divisors_subset_of_dvd h0n.ne' hd)]
end
lemma X_pow_sub_one_mul_cyclotomic_dvd_X_pow_sub_one_of_dvd (R) [comm_ring R] {d n : β}
(h : d β n.proper_divisors) : (X ^ d - 1) * cyclotomic n R β£ X ^ n - 1 :=
begin
have hdn := (nat.mem_proper_divisors.mp h).2,
use β x in n.proper_divisors \ d.divisors, cyclotomic x R,
symmetry,
convert X_pow_sub_one_mul_prod_cyclotomic_eq_X_pow_sub_one_of_dvd R h using 1,
rw mul_assoc,
congr' 1,
rw [nat.divisors_eq_proper_divisors_insert_self_of_pos $ pos_of_gt hdn,
finset.insert_sdiff_of_not_mem, finset.prod_insert],
{ exact finset.not_mem_sdiff_of_not_mem_left nat.proper_divisors.not_self_mem },
{ exact Ξ» hk, hdn.not_le $ nat.divisor_le hk }
end
lemma _root_.is_root_of_unity_iff {n : β} (h : 0 < n) (R : Type*) [comm_ring R] [is_domain R]
{ΞΆ : R} : ΞΆ ^ n = 1 β β i β n.divisors, (cyclotomic i R).is_root ΞΆ :=
by rw [βmem_nth_roots h, nth_roots, mem_roots $ X_pow_sub_C_ne_zero h _,
C_1, βprod_cyclotomic_eq_X_pow_sub_one h, is_root_prod]; apply_instance
lemma is_root_of_unity_of_root_cyclotomic {n : β} {R} [comm_ring R] {ΞΆ : R} {i : β}
(hi : i β n.divisors) (h : (cyclotomic i R).is_root ΞΆ) : ΞΆ ^ n = 1 :=
begin
rcases n.eq_zero_or_pos with rfl | hn,
{ exact pow_zero _ },
have := congr_arg (eval ΞΆ) (prod_cyclotomic_eq_X_pow_sub_one hn R).symm,
rw [eval_sub, eval_pow, eval_X, eval_one] at this,
convert eq_add_of_sub_eq' this,
convert (add_zero _).symm,
apply eval_eq_zero_of_dvd_of_eval_eq_zero _ h,
exact finset.dvd_prod_of_mem _ hi
end
section arithmetic_function
open nat.arithmetic_function
open_locale arithmetic_function
/-- `cyclotomic n R` can be expressed as a product in a fraction field of `R[X]`
using MΓΆbius inversion. -/
lemma cyclotomic_eq_prod_X_pow_sub_one_pow_moebius {n : β} (R : Type*) [comm_ring R] [is_domain R] :
algebra_map _ (ratfunc R) (cyclotomic n R) =
β i in n.divisors_antidiagonal, (algebra_map R[X] _ (X ^ i.snd - 1)) ^ ΞΌ i.fst :=
begin
rcases n.eq_zero_or_pos with rfl | hpos,
{ simp },
have h : β (n : β), 0 < n β
β i in nat.divisors n, algebra_map _ (ratfunc R) (cyclotomic i R) = algebra_map _ _ (X ^ n - 1),
{ intros n hn,
rw [β prod_cyclotomic_eq_X_pow_sub_one hn R, ring_hom.map_prod] },
rw (prod_eq_iff_prod_pow_moebius_eq_of_nonzero (Ξ» n hn, _) (Ξ» n hn, _)).1 h n hpos;
rw [ne.def, is_fraction_ring.to_map_eq_zero_iff],
{ apply cyclotomic_ne_zero },
{ apply monic.ne_zero,
apply monic_X_pow_sub_C _ (ne_of_gt hn) }
end
end arithmetic_function
/-- We have
`cyclotomic n R = (X ^ k - 1) /β (β i in nat.proper_divisors k, cyclotomic i K)`. -/
lemma cyclotomic_eq_X_pow_sub_one_div {R : Type*} [comm_ring R] {n : β}
(hpos: 0 < n) : cyclotomic n R = (X ^ n - 1) /β (β i in nat.proper_divisors n, cyclotomic i R) :=
begin
nontriviality R,
rw [βprod_cyclotomic_eq_X_pow_sub_one hpos,
nat.divisors_eq_proper_divisors_insert_self_of_pos hpos,
finset.prod_insert nat.proper_divisors.not_self_mem],
have prod_monic : (β i in nat.proper_divisors n, cyclotomic i R).monic,
{ apply monic_prod_of_monic,
intros i hi,
exact cyclotomic.monic i R },
rw (div_mod_by_monic_unique (cyclotomic n R) 0 prod_monic _).1,
simp only [degree_zero, zero_add],
split,
{ rw mul_comm },
rw [bot_lt_iff_ne_bot],
intro h,
exact monic.ne_zero prod_monic (degree_eq_bot.1 h)
end
/-- If `m` is a proper divisor of `n`, then `X ^ m - 1` divides
`β i in nat.proper_divisors n, cyclotomic i R`. -/
lemma X_pow_sub_one_dvd_prod_cyclotomic (R : Type*) [comm_ring R] {n m : β} (hpos : 0 < n)
(hm : m β£ n) (hdiff : m β n) : X ^ m - 1 β£ β i in nat.proper_divisors n, cyclotomic i R :=
begin
replace hm := nat.mem_proper_divisors.2 β¨hm, lt_of_le_of_ne (nat.divisor_le (nat.mem_divisors.2
β¨hm, hpos.ne'β©)) hdiffβ©,
rw [β finset.sdiff_union_of_subset (nat.divisors_subset_proper_divisors (ne_of_lt hpos).symm
(nat.mem_proper_divisors.1 hm).1 (ne_of_lt (nat.mem_proper_divisors.1 hm).2)),
finset.prod_union finset.sdiff_disjoint,
prod_cyclotomic_eq_X_pow_sub_one (nat.pos_of_mem_proper_divisors hm)],
exact β¨(β (x : β) in n.proper_divisors \ m.divisors, cyclotomic x R), by rw mul_commβ©
end
/-- If there is a primitive `n`-th root of unity in `K`, then
`cyclotomic n K = β ΞΌ in primitive_roots n R, (X - C ΞΌ)`. In particular,
`cyclotomic n K = cyclotomic' n K` -/
lemma cyclotomic_eq_prod_X_sub_primitive_roots {K : Type*} [comm_ring K] [is_domain K] {ΞΆ : K}
{n : β} (hz : is_primitive_root ΞΆ n) :
cyclotomic n K = β ΞΌ in primitive_roots n K, (X - C ΞΌ) :=
begin
rw βcyclotomic',
induction n using nat.strong_induction_on with k hk generalizing ΞΆ hz,
obtain hzero | hpos := k.eq_zero_or_pos,
{ simp only [hzero, cyclotomic'_zero, cyclotomic_zero] },
have h : β i β k.proper_divisors, cyclotomic i K = cyclotomic' i K,
{ intros i hi,
obtain β¨d, hdβ© := (nat.mem_proper_divisors.1 hi).1,
rw mul_comm at hd,
exact hk i (nat.mem_proper_divisors.1 hi).2 (is_primitive_root.pow hpos hz hd) },
rw [@cyclotomic_eq_X_pow_sub_one_div _ _ _ hpos,
cyclotomic'_eq_X_pow_sub_one_div hpos hz, finset.prod_congr (refl k.proper_divisors) h]
end
section roots
variables {R : Type*} {n : β} [comm_ring R] [is_domain R]
/-- Any `n`-th primitive root of unity is a root of `cyclotomic n K`.-/
lemma _root_.is_primitive_root.is_root_cyclotomic (hpos : 0 < n) {ΞΌ : R}
(h : is_primitive_root ΞΌ n) : is_root (cyclotomic n R) ΞΌ :=
begin
rw [β mem_roots (cyclotomic_ne_zero n R),
cyclotomic_eq_prod_X_sub_primitive_roots h, roots_prod_X_sub_C, β finset.mem_def],
rwa [β mem_primitive_roots hpos] at h,
end
private lemma is_root_cyclotomic_iff' {n : β} {K : Type*} [field K] {ΞΌ : K} [ne_zero (n : K)] :
is_root (cyclotomic n K) ΞΌ β is_primitive_root ΞΌ n :=
begin
-- in this proof, `o` stands for `order_of ΞΌ`
have hnpos : 0 < n := (ne_zero.of_ne_zero_coe K).out.bot_lt,
refine β¨Ξ» hΞΌ, _, is_primitive_root.is_root_cyclotomic hnposβ©,
have hΞΌn : ΞΌ ^ n = 1,
{ rw is_root_of_unity_iff hnpos,
exact β¨n, n.mem_divisors_self hnpos.ne', hΞΌβ© },
by_contra hnΞΌ,
have ho : 0 < order_of ΞΌ,
{ apply order_of_pos',
rw is_of_fin_order_iff_pow_eq_one,
exact β¨n, hnpos, hΞΌnβ© },
have := pow_order_of_eq_one ΞΌ,
rw is_root_of_unity_iff ho at this,
obtain β¨i, hio, hiΞΌβ© := this,
replace hio := nat.dvd_of_mem_divisors hio,
rw is_primitive_root.not_iff at hnΞΌ,
rw βorder_of_dvd_iff_pow_eq_one at hΞΌn,
have key : i < n := (nat.le_of_dvd ho hio).trans_lt ((nat.le_of_dvd hnpos hΞΌn).lt_of_ne hnΞΌ),
have key' : i β£ n := hio.trans hΞΌn,
rw βpolynomial.dvd_iff_is_root at hΞΌ hiΞΌ,
have hni : {i, n} β n.divisors,
{ simpa [finset.insert_subset, key'] using hnpos.ne' },
obtain β¨k, hkβ© := hiΞΌ,
obtain β¨j, hjβ© := hΞΌ,
have := prod_cyclotomic_eq_X_pow_sub_one hnpos K,
rw [βfinset.prod_sdiff hni, finset.prod_pair key.ne, hk, hj] at this,
have hn := (X_pow_sub_one_separable_iff.mpr $ ne_zero.nat_cast_ne n K).squarefree,
rw [βthis, squarefree] at hn,
contrapose! hn,
refine β¨X - C ΞΌ, β¨(β x in n.divisors \ {i, n}, cyclotomic x K) * k * j, by ringβ©, _β©,
simp [polynomial.is_unit_iff_degree_eq_zero]
end
lemma is_root_cyclotomic_iff [ne_zero (n : R)] {ΞΌ : R} :
is_root (cyclotomic n R) ΞΌ β is_primitive_root ΞΌ n :=
begin
have hf : function.injective _ := is_fraction_ring.injective R (fraction_ring R),
haveI : ne_zero (n : fraction_ring R) := ne_zero.nat_of_injective hf,
rw [βis_root_map_iff hf, βis_primitive_root.map_iff_of_injective hf, map_cyclotomic,
βis_root_cyclotomic_iff']
end
lemma roots_cyclotomic_nodup [ne_zero (n : R)] : (cyclotomic n R).roots.nodup :=
begin
obtain h | β¨ΞΆ, hΞΆβ© := (cyclotomic n R).roots.empty_or_exists_mem,
{ exact h.symm βΈ multiset.nodup_zero },
rw [mem_roots $ cyclotomic_ne_zero n R, is_root_cyclotomic_iff] at hΞΆ,
refine multiset.nodup_of_le (roots.le_of_dvd (X_pow_sub_C_ne_zero
(ne_zero.pos_of_ne_zero_coe R) 1) $ cyclotomic.dvd_X_pow_sub_one n R) hΞΆ.nth_roots_nodup,
end
lemma cyclotomic.roots_to_finset_eq_primitive_roots [ne_zero (n : R)] :
(β¨(cyclotomic n R).roots, roots_cyclotomic_nodupβ© : finset _) = primitive_roots n R :=
by { ext, simp [cyclotomic_ne_zero n R, is_root_cyclotomic_iff,
mem_primitive_roots, ne_zero.pos_of_ne_zero_coe R] }
lemma cyclotomic.roots_eq_primitive_roots_val [ne_zero (n : R)] :
(cyclotomic n R).roots = (primitive_roots n R).val :=
by rw βcyclotomic.roots_to_finset_eq_primitive_roots
end roots
/-- If `R` is of characteristic zero, then `ΞΆ` is a root of `cyclotomic n R` if and only if it is a
primitive `n`-th root of unity. -/
lemma is_root_cyclotomic_iff_char_zero {n : β} {R : Type*} [comm_ring R] [is_domain R]
[char_zero R] {ΞΌ : R} (hn : 0 < n) :
(polynomial.cyclotomic n R).is_root ΞΌ β is_primitive_root ΞΌ n :=
by { letI := ne_zero.of_gt hn, exact is_root_cyclotomic_iff }
/-- Over a ring `R` of characteristic zero, `Ξ» n, cyclotomic n R` is injective. -/
lemma cyclotomic_injective {R : Type*} [comm_ring R] [char_zero R] :
function.injective (Ξ» n, cyclotomic n R) :=
begin
intros n m hnm,
simp only at hnm,
rcases eq_or_ne n 0 with rfl | hzero,
{ rw [cyclotomic_zero] at hnm,
replace hnm := congr_arg nat_degree hnm,
rw [nat_degree_one, nat_degree_cyclotomic] at hnm,
by_contra,
exact (nat.totient_pos (zero_lt_iff.2 (ne.symm h))).ne hnm },
{ haveI := ne_zero.mk hzero,
rw [β map_cyclotomic_int _ R, β map_cyclotomic_int _ R] at hnm,
replace hnm := map_injective (int.cast_ring_hom R) int.cast_injective hnm,
replace hnm := congr_arg (map (int.cast_ring_hom β)) hnm,
rw [map_cyclotomic_int, map_cyclotomic_int] at hnm,
have hprim := complex.is_primitive_root_exp _ hzero,
have hroot := is_root_cyclotomic_iff.2 hprim,
rw hnm at hroot,
haveI hmzero : ne_zero m := β¨Ξ» h, by simpa [h] using hrootβ©,
rw is_root_cyclotomic_iff at hroot,
replace hprim := hprim.eq_order_of,
rwa [β is_primitive_root.eq_order_of hroot] at hprim}
end
lemma eq_cyclotomic_iff {R : Type*} [comm_ring R] {n : β} (hpos: 0 < n)
(P : R[X]) :
P = cyclotomic n R β P * (β i in nat.proper_divisors n, polynomial.cyclotomic i R) = X ^ n - 1 :=
begin
nontriviality R,
refine β¨Ξ» hcycl, _, Ξ» hP, _β©,
{ rw [hcycl, β finset.prod_insert (@nat.proper_divisors.not_self_mem n),
β nat.divisors_eq_proper_divisors_insert_self_of_pos hpos],
exact prod_cyclotomic_eq_X_pow_sub_one hpos R },
{ have prod_monic : (β i in nat.proper_divisors n, cyclotomic i R).monic,
{ apply monic_prod_of_monic,
intros i hi,
exact cyclotomic.monic i R },
rw [@cyclotomic_eq_X_pow_sub_one_div R _ _ hpos,
(div_mod_by_monic_unique P 0 prod_monic _).1],
refine β¨by rwa [zero_add, mul_comm], _β©,
rw [degree_zero, bot_lt_iff_ne_bot],
intro h,
exact monic.ne_zero prod_monic (degree_eq_bot.1 h) },
end
/-- If `p ^ k` is a prime power, then
`cyclotomic (p ^ (n + 1)) R = β i in range p, (X ^ (p ^ n)) ^ i`. -/
lemma cyclotomic_prime_pow_eq_geom_sum {R : Type*} [comm_ring R] {p n : β} (hp : p.prime) :
cyclotomic (p ^ (n + 1)) R = β i in finset.range p, (X ^ (p ^ n)) ^ i :=
begin
have : β m, cyclotomic (p ^ (m + 1)) R = β i in finset.range p, (X ^ (p ^ m)) ^ i β
(β i in finset.range p, (X ^ (p ^ m)) ^ i) * β (x : β) in finset.range (m + 1),
cyclotomic (p ^ x) R = X ^ p ^ (m + 1) - 1,
{ intro m,
have := eq_cyclotomic_iff (pow_pos hp.pos (m + 1)) _,
rw eq_comm at this,
rw [this, nat.prod_proper_divisors_prime_pow hp], },
induction n with n_n n_ih,
{ haveI := fact.mk hp, simp [cyclotomic_prime], },
rw ((eq_cyclotomic_iff (pow_pos hp.pos (n_n.succ + 1)) _).mpr _).symm,
rw [nat.prod_proper_divisors_prime_pow hp, finset.prod_range_succ, n_ih],
rw this at n_ih,
rw [mul_comm _ (β i in _, _), n_ih, geom_sum_mul, sub_left_inj, β pow_mul, pow_add, pow_one],
end
lemma cyclotomic_prime_pow_mul_X_pow_sub_one (R : Type*) [comm_ring R] (p k : β)
[hn : fact (nat.prime p)] :
(cyclotomic (p ^ (k + 1)) R) * (X ^ (p ^ k) - 1) = X ^ (p ^ (k + 1)) - 1 :=
by rw [cyclotomic_prime_pow_eq_geom_sum hn.out, geom_sum_mul, β pow_mul, pow_succ, mul_comm]
/-- The constant term of `cyclotomic n R` is `1` if `2 β€ n`. -/
lemma cyclotomic_coeff_zero (R : Type*) [comm_ring R] {n : β} (hn : 2 β€ n) :
(cyclotomic n R).coeff 0 = 1 :=
begin
induction n using nat.strong_induction_on with n hi,
have hprod : (β i in nat.proper_divisors n, (polynomial.cyclotomic i R).coeff 0) = -1,
{ rw [βfinset.insert_erase (nat.one_mem_proper_divisors_iff_one_lt.2
(lt_of_lt_of_le one_lt_two hn)), finset.prod_insert (finset.not_mem_erase 1 _),
cyclotomic_one R],
have hleq : β j β n.proper_divisors.erase 1, 2 β€ j,
{ intros j hj,
apply nat.succ_le_of_lt,
exact (ne.le_iff_lt ((finset.mem_erase.1 hj).1).symm).mp
(nat.succ_le_of_lt (nat.pos_of_mem_proper_divisors (finset.mem_erase.1 hj).2)) },
have hcongr : β j β n.proper_divisors.erase 1, (cyclotomic j R).coeff 0 = 1,
{ intros j hj,
exact hi j (nat.mem_proper_divisors.1 (finset.mem_erase.1 hj).2).2 (hleq j hj) },
have hrw : β (x : β) in n.proper_divisors.erase 1, (cyclotomic x R).coeff 0 = 1,
{ rw finset.prod_congr (refl (n.proper_divisors.erase 1)) hcongr,
simp only [finset.prod_const_one] },
simp only [hrw, mul_one, zero_sub, coeff_one_zero, coeff_X_zero, coeff_sub] },
have heq : (X ^ n - 1).coeff 0 = -(cyclotomic n R).coeff 0,
{ rw [βprod_cyclotomic_eq_X_pow_sub_one (lt_of_lt_of_le zero_lt_two hn),
nat.divisors_eq_proper_divisors_insert_self_of_pos (lt_of_lt_of_le zero_lt_two hn),
finset.prod_insert nat.proper_divisors.not_self_mem, mul_coeff_zero, coeff_zero_prod, hprod,
mul_neg, mul_one] },
have hzero : (X ^ n - 1).coeff 0 = (-1 : R),
{ rw coeff_zero_eq_eval_zero _,
simp only [zero_pow (lt_of_lt_of_le zero_lt_two hn), eval_X, eval_one, zero_sub, eval_pow,
eval_sub] },
rw hzero at heq,
exact neg_inj.mp (eq.symm heq)
end
/-- If `(a : β)` is a root of `cyclotomic n (zmod p)`, where `p` is a prime, then `a` and `p` are
coprime. -/
lemma coprime_of_root_cyclotomic {n : β} (hpos : 0 < n) {p : β} [hprime : fact p.prime] {a : β}
(hroot : is_root (cyclotomic n (zmod p)) (nat.cast_ring_hom (zmod p) a)) :
a.coprime p :=
begin
apply nat.coprime.symm,
rw [hprime.1.coprime_iff_not_dvd],
intro h,
replace h := (zmod.nat_coe_zmod_eq_zero_iff_dvd a p).2 h,
rw [is_root.def, eq_nat_cast, h, β coeff_zero_eq_eval_zero] at hroot,
by_cases hone : n = 1,
{ simp only [hone, cyclotomic_one, zero_sub, coeff_one_zero, coeff_X_zero, neg_eq_zero,
one_ne_zero, coeff_sub] at hroot,
exact hroot },
rw [cyclotomic_coeff_zero (zmod p) (nat.succ_le_of_lt (lt_of_le_of_ne
(nat.succ_le_of_lt hpos) (ne.symm hone)))] at hroot,
exact one_ne_zero hroot
end
end cyclotomic
section order
/-- If `(a : β)` is a root of `cyclotomic n (zmod p)`, then the multiplicative order of `a` modulo
`p` divides `n`. -/
lemma order_of_root_cyclotomic_dvd {n : β} (hpos : 0 < n) {p : β} [fact p.prime]
{a : β} (hroot : is_root (cyclotomic n (zmod p)) (nat.cast_ring_hom (zmod p) a)) :
order_of (zmod.unit_of_coprime a (coprime_of_root_cyclotomic hpos hroot)) β£ n :=
begin
apply order_of_dvd_of_pow_eq_one,
suffices hpow : eval (nat.cast_ring_hom (zmod p) a) (X ^ n - 1 : (zmod p)[X]) = 0,
{ simp only [eval_X, eval_one, eval_pow, eval_sub, eq_nat_cast] at hpow,
apply units.coe_eq_one.1,
simp only [sub_eq_zero.mp hpow, zmod.coe_unit_of_coprime, units.coe_pow] },
rw [is_root.def] at hroot,
rw [β prod_cyclotomic_eq_X_pow_sub_one hpos (zmod p),
nat.divisors_eq_proper_divisors_insert_self_of_pos hpos,
finset.prod_insert nat.proper_divisors.not_self_mem, eval_mul, hroot, zero_mul]
end
end order
section minpoly
open is_primitive_root complex
/-- The minimal polynomial of a primitive `n`-th root of unity `ΞΌ` divides `cyclotomic n β€`. -/
lemma _root_.is_primitive_root.minpoly_dvd_cyclotomic {n : β} {K : Type*} [field K] {ΞΌ : K}
(h : is_primitive_root ΞΌ n) (hpos : 0 < n) [char_zero K] :
minpoly β€ ΞΌ β£ cyclotomic n β€ :=
begin
apply minpoly.gcd_domain_dvd (is_integral h hpos) (cyclotomic_ne_zero n β€),
simpa [aeval_def, evalβ_eq_eval_map, is_root.def] using is_root_cyclotomic hpos h
end
lemma _root_.is_primitive_root.minpoly_eq_cyclotomic_of_irreducible {K : Type*} [field K]
{R : Type*} [comm_ring R] [is_domain R] {ΞΌ : R} {n : β} [algebra K R] (hΞΌ : is_primitive_root ΞΌ n)
(h : irreducible $ cyclotomic n K) [ne_zero (n : K)] : cyclotomic n K = minpoly K ΞΌ :=
begin
haveI := ne_zero.of_no_zero_smul_divisors K R n,
refine minpoly.eq_of_irreducible_of_monic h _ (cyclotomic.monic n K),
rwa [aeval_def, evalβ_eq_eval_map, map_cyclotomic, βis_root.def, is_root_cyclotomic_iff]
end
/-- `cyclotomic n β€` is the minimal polynomial of a primitive `n`-th root of unity `ΞΌ`. -/
lemma cyclotomic_eq_minpoly {n : β} {K : Type*} [field K] {ΞΌ : K}
(h : is_primitive_root ΞΌ n) (hpos : 0 < n) [char_zero K] :
cyclotomic n β€ = minpoly β€ ΞΌ :=
begin
refine eq_of_monic_of_dvd_of_nat_degree_le (minpoly.monic (is_integral h hpos))
(cyclotomic.monic n β€) (h.minpoly_dvd_cyclotomic hpos) _,
simpa [nat_degree_cyclotomic n β€] using totient_le_degree_minpoly h
end
/-- `cyclotomic n β` is the minimal polynomial of a primitive `n`-th root of unity `ΞΌ`. -/
lemma cyclotomic_eq_minpoly_rat {n : β} {K : Type*} [field K] {ΞΌ : K}
(h : is_primitive_root ΞΌ n) (hpos : 0 < n) [char_zero K] :
cyclotomic n β = minpoly β ΞΌ :=
begin
rw [β map_cyclotomic_int, cyclotomic_eq_minpoly h hpos],
exact (minpoly.gcd_domain_eq_field_fractions' _ (is_integral h hpos)).symm
end
/-- `cyclotomic n β€` is irreducible. -/
lemma cyclotomic.irreducible {n : β} (hpos : 0 < n) : irreducible (cyclotomic n β€) :=
begin
rw [cyclotomic_eq_minpoly (is_primitive_root_exp n hpos.ne') hpos],
apply minpoly.irreducible,
exact (is_primitive_root_exp n hpos.ne').is_integral hpos,
end
/-- `cyclotomic n β` is irreducible. -/
lemma cyclotomic.irreducible_rat {n : β} (hpos : 0 < n) : irreducible (cyclotomic n β) :=
begin
rw [β map_cyclotomic_int],
exact (is_primitive.int.irreducible_iff_irreducible_map_cast (cyclotomic.is_primitive n β€)).1
(cyclotomic.irreducible hpos),
end
/-- If `n β m`, then `(cyclotomic n β)` and `(cyclotomic m β)` are coprime. -/
lemma cyclotomic.is_coprime_rat {n m : β} (h : n β m) :
is_coprime (cyclotomic n β) (cyclotomic m β) :=
begin
rcases n.eq_zero_or_pos with rfl | hnzero,
{ exact is_coprime_one_left },
rcases m.eq_zero_or_pos with rfl | hmzero,
{ exact is_coprime_one_right },
rw (irreducible.coprime_iff_not_dvd $ cyclotomic.irreducible_rat $ hnzero),
exact (Ξ» hdiv, h $ cyclotomic_injective $ eq_of_monic_of_associated (cyclotomic.monic n β)
(cyclotomic.monic m β) $ irreducible.associated_of_dvd (cyclotomic.irreducible_rat
hnzero) (cyclotomic.irreducible_rat hmzero) hdiv),
end
end minpoly
section expand
/-- If `p` is a prime such that `Β¬ p β£ n`, then
`expand R p (cyclotomic n R) = (cyclotomic (n * p) R) * (cyclotomic n R)`. -/
@[simp] lemma cyclotomic_expand_eq_cyclotomic_mul {p n : β} (hp : nat.prime p) (hdiv : Β¬p β£ n)
(R : Type*) [comm_ring R] :
expand R p (cyclotomic n R) = (cyclotomic (n * p) R) * (cyclotomic n R) :=
begin
rcases nat.eq_zero_or_pos n with rfl | hnpos,
{ simp },
haveI := ne_zero.of_pos hnpos,
suffices : expand β€ p (cyclotomic n β€) = (cyclotomic (n * p) β€) * (cyclotomic n β€),
{ rw [β map_cyclotomic_int, β map_expand, this, polynomial.map_mul, map_cyclotomic_int] },
refine eq_of_monic_of_dvd_of_nat_degree_le ((cyclotomic.monic _ _).mul
(cyclotomic.monic _ _)) ((cyclotomic.monic n β€).expand hp.pos) _ _,
{ refine (is_primitive.int.dvd_iff_map_cast_dvd_map_cast _ _ (is_primitive.mul
(cyclotomic.is_primitive (n * p) β€) (cyclotomic.is_primitive n β€))
((cyclotomic.monic n β€).expand hp.pos).is_primitive).2 _,
rw [polynomial.map_mul, map_cyclotomic_int, map_cyclotomic_int, map_expand, map_cyclotomic_int],
refine is_coprime.mul_dvd (cyclotomic.is_coprime_rat (Ξ» h, _)) _ _,
{ replace h : n * p = n * 1 := by simp [h],
exact nat.prime.ne_one hp (mul_left_cancelβ hnpos.ne' h) },
{ have hpos : 0 < n * p := mul_pos hnpos hp.pos,
have hprim := complex.is_primitive_root_exp _ hpos.ne',
rw [cyclotomic_eq_minpoly_rat hprim hpos],
refine @minpoly.dvd β β _ _ algebra_rat _ _ _,
rw [aeval_def, β eval_map, map_expand, map_cyclotomic, expand_eval, β is_root.def,
is_root_cyclotomic_iff],
convert is_primitive_root.pow_of_dvd hprim hp.ne_zero (dvd_mul_left p n),
rw [nat.mul_div_cancel _ (nat.prime.pos hp)] },
{ have hprim := complex.is_primitive_root_exp _ hnpos.ne.symm,
rw [cyclotomic_eq_minpoly_rat hprim hnpos],
refine @minpoly.dvd β β _ _ algebra_rat _ _ _,
rw [aeval_def, β eval_map, map_expand, expand_eval, β is_root.def,
β cyclotomic_eq_minpoly_rat hprim hnpos, map_cyclotomic, is_root_cyclotomic_iff],
exact is_primitive_root.pow_of_prime hprim hp hdiv,} },
{ rw [nat_degree_expand, nat_degree_cyclotomic, nat_degree_mul (cyclotomic_ne_zero _ β€)
(cyclotomic_ne_zero _ β€), nat_degree_cyclotomic, nat_degree_cyclotomic, mul_comm n,
nat.totient_mul ((nat.prime.coprime_iff_not_dvd hp).2 hdiv),
nat.totient_prime hp, mul_comm (p - 1), β nat.mul_succ, nat.sub_one,
nat.succ_pred_eq_of_pos hp.pos] }
end
/-- If `p` is a prime such that `p β£ n`, then
`expand R p (cyclotomic n R) = cyclotomic (p * n) R`. -/
@[simp] lemma cyclotomic_expand_eq_cyclotomic {p n : β} (hp : nat.prime p) (hdiv : p β£ n)
(R : Type*) [comm_ring R] : expand R p (cyclotomic n R) = cyclotomic (n * p) R :=
begin
rcases n.eq_zero_or_pos with rfl | hzero,
{ simp },
haveI := ne_zero.of_pos hzero,
suffices : expand β€ p (cyclotomic n β€) = cyclotomic (n * p) β€,
{ rw [β map_cyclotomic_int, β map_expand, this, map_cyclotomic_int] },
refine eq_of_monic_of_dvd_of_nat_degree_le (cyclotomic.monic _ _)
((cyclotomic.monic n β€).expand hp.pos) _ _,
{ have hpos := nat.mul_pos hzero hp.pos,
have hprim := complex.is_primitive_root_exp _ hpos.ne.symm,
rw [cyclotomic_eq_minpoly hprim hpos],
refine minpoly.gcd_domain_dvd (hprim.is_integral hpos)
((cyclotomic.monic n β€).expand hp.pos).ne_zero _,
rw [aeval_def, β eval_map, map_expand, map_cyclotomic, expand_eval,
β is_root.def, is_root_cyclotomic_iff],
{ convert is_primitive_root.pow_of_dvd hprim hp.ne_zero (dvd_mul_left p n),
rw [nat.mul_div_cancel _ hp.pos] } },
{ rw [nat_degree_expand, nat_degree_cyclotomic, nat_degree_cyclotomic, mul_comm n,
nat.totient_mul_of_prime_of_dvd hp hdiv, mul_comm] }
end
/-- If the `p ^ n`th cyclotomic polynomial is irreducible, so is the `p ^ m`th, for `m β€ n`. -/
lemma cyclotomic_irreducible_pow_of_irreducible_pow {p : β} (hp : nat.prime p)
{R} [comm_ring R] [is_domain R] {n m : β} (hmn : m β€ n)
(h : irreducible (cyclotomic (p ^ n) R)) : irreducible (cyclotomic (p ^ m) R) :=
begin
unfreezingI
{ rcases m.eq_zero_or_pos with rfl | hm,
{ simpa using irreducible_X_sub_C (1 : R) },
obtain β¨k, rflβ© := nat.exists_eq_add_of_le hmn,
induction k with k hk },
{ simpa using h },
have : m + k β 0 := (add_pos_of_pos_of_nonneg hm k.zero_le).ne',
rw [nat.add_succ, pow_succ', βcyclotomic_expand_eq_cyclotomic hp $ dvd_pow_self p this] at h,
exact hk (by linarith) (of_irreducible_expand hp.ne_zero h)
end
/-- If `irreducible (cyclotomic (p ^ n) R)` then `irreducible (cyclotomic p R).` -/
lemma cyclotomic_irreducible_of_irreducible_pow {p : β} (hp : nat.prime p) {R} [comm_ring R]
[is_domain R] {n : β} (hn : n β 0) (h : irreducible (cyclotomic (p ^ n) R)) :
irreducible (cyclotomic p R) :=
pow_one p βΈ cyclotomic_irreducible_pow_of_irreducible_pow hp hn.bot_lt h
end expand
section char_p
/-- If `R` is of characteristic `p` and `Β¬p β£ n`, then
`cyclotomic (n * p) R = (cyclotomic n R) ^ (p - 1)`. -/
lemma cyclotomic_mul_prime_eq_pow_of_not_dvd (R : Type*) {p n : β} [hp : fact (nat.prime p)]
[ring R] [char_p R p] (hn : Β¬p β£ n) : cyclotomic (n * p) R = (cyclotomic n R) ^ (p - 1) :=
begin
suffices : cyclotomic (n * p) (zmod p) = (cyclotomic n (zmod p)) ^ (p - 1),
{ rw [β map_cyclotomic _ (algebra_map (zmod p) R), β map_cyclotomic _ (algebra_map (zmod p) R),
this, polynomial.map_pow] },
apply mul_right_injectiveβ (cyclotomic_ne_zero n $ zmod p),
rw [βpow_succ, tsub_add_cancel_of_le hp.out.one_lt.le, mul_comm, β zmod.expand_card],
nth_rewrite 2 [β map_cyclotomic_int],
rw [β map_expand, cyclotomic_expand_eq_cyclotomic_mul hp.out hn, polynomial.map_mul,
map_cyclotomic, map_cyclotomic]
end
/-- If `R` is of characteristic `p` and `p β£ n`, then
`cyclotomic (n * p) R = (cyclotomic n R) ^ p`. -/
lemma cyclotomic_mul_prime_dvd_eq_pow (R : Type*) {p n : β} [hp : fact (nat.prime p)] [ring R]
[char_p R p] (hn : p β£ n) : cyclotomic (n * p) R = (cyclotomic n R) ^ p :=
begin
suffices : cyclotomic (n * p) (zmod p) = (cyclotomic n (zmod p)) ^ p,
{ rw [β map_cyclotomic _ (algebra_map (zmod p) R), β map_cyclotomic _ (algebra_map (zmod p) R),
this, polynomial.map_pow] },
rw [β zmod.expand_card, β map_cyclotomic_int n, β map_expand, cyclotomic_expand_eq_cyclotomic
hp.out hn, map_cyclotomic, mul_comm]
end
/-- If `R` is of characteristic `p` and `Β¬p β£ m`, then
`cyclotomic (p ^ k * m) R = (cyclotomic m R) ^ (p ^ k - p ^ (k - 1))`. -/
lemma cyclotomic_mul_prime_pow_eq (R : Type*) {p m : β} [fact (nat.prime p)]
[ring R] [char_p R p] (hm : Β¬p β£ m) :
β {k}, 0 < k β cyclotomic (p ^ k * m) R = (cyclotomic m R) ^ (p ^ k - p ^ (k - 1))
| 1 _ := by rw [pow_one, nat.sub_self, pow_zero, mul_comm,
cyclotomic_mul_prime_eq_pow_of_not_dvd R hm]
| (a + 2) _ :=
begin
have hdiv : p β£ p ^ a.succ * m := β¨p ^ a * m, by rw [β mul_assoc, pow_succ]β©,
rw [pow_succ, mul_assoc, mul_comm, cyclotomic_mul_prime_dvd_eq_pow R hdiv,
cyclotomic_mul_prime_pow_eq a.succ_pos, β pow_mul],
congr' 1,
simp only [tsub_zero, nat.succ_sub_succ_eq_sub],
rw [nat.mul_sub_right_distrib, mul_comm, pow_succ']
end
/-- If `R` is of characteristic `p` and `Β¬p β£ m`, then `ΞΆ` is a root of `cyclotomic (p ^ k * m) R`
if and only if it is a primitive `m`-th root of unity. -/
lemma is_root_cyclotomic_prime_pow_mul_iff_of_char_p {m k p : β} {R : Type*} [comm_ring R]
[is_domain R] [hp : fact (nat.prime p)] [hchar : char_p R p] {ΞΌ : R} [ne_zero (m : R)] :
(polynomial.cyclotomic (p ^ k * m) R).is_root ΞΌ β is_primitive_root ΞΌ m :=
begin
rcases k.eq_zero_or_pos with rfl | hk,
{ rw [pow_zero, one_mul, is_root_cyclotomic_iff] },
refine β¨Ξ» h, _, Ξ» h, _β©,
{ rw [is_root.def, cyclotomic_mul_prime_pow_eq R (ne_zero.not_char_dvd R p m) hk, eval_pow] at h,
replace h := pow_eq_zero h,
rwa [β is_root.def, is_root_cyclotomic_iff] at h },
{ rw [β is_root_cyclotomic_iff, is_root.def] at h,
rw [cyclotomic_mul_prime_pow_eq R (ne_zero.not_char_dvd R p m) hk,
is_root.def, eval_pow, h, zero_pow],
simp only [tsub_pos_iff_lt],
apply pow_strict_mono_right hp.out.one_lt (nat.pred_lt hk.ne') }
end
end char_p
end polynomial
|
00319f8e4aeba4b6208a69255eb56130c3f2dab8 | cfef816283af58a9ea4678b69595e86562fd4c6a | /src/scone/t2.lean | aecfc2a1037a6cb150a78688b639187080d2d67c | [] | no_license | rwbarton/scone | 6fc5bee6766c170d2edeefaf608ee32d665c7c77 | 6f3d35f7a3bed772475ff7954875b9ee7554aae3 | refs/heads/master | 1,672,462,177,915 | 1,603,245,162,000 | 1,603,245,162,000 | 305,762,158 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,394 | lean | import scone.edge
-- Demo file.
open scone
/- The type of "Hausdorffness", or any other property of a topological space.
Here, to stick close to the primitives, we just represent a topology as
a collection of (open) sets, and omit the axioms. -/
def A : Type 1 := Ξ (X : Type), ((X β Prop) β Prop) β Prop
def edge_A : edge_ty A :=
begin
unfold A, -- optional
-- β’ β’ Ξ (X : Type), ((X β Prop) β Prop) β Prop) type
refine pi_edge_ctx_ty _ type_edge_ty _,
-- X : Type β’ ((X β Prop) β Prop) β Prop) type
refine pi_edge_ctx_ty _ _ (gen_edge_ty prop_edge_ty),
refine pi_edge_ctx_ty _ _ (gen_edge_ty prop_edge_ty),
refine pi_edge_ctx_ty _ _ (gen_edge_ty prop_edge_ty),
-- X : Type β’ X type
-- For this we need Russell-style universes plus the variable rule.
refine univ_ctx_ty _ _,
-- X : Type β’ X : Type
refine last_var_ctx_tm _ _,
done
end
/- The property of being a Hausdorff space:
any two distinct points are separated by disjoint open sets. -/
def Tβ : A :=
Ξ» X Ο,
β (x y : X), (x = y β false) β
β (U V : X β Prop), Ο U β§ Ο V β§ U x β§ V y β§
β (z : X), U z β V z β false
-- TODO: By a similar process, prove
-- def edge_Tβ : edge_ctx_tm unit_edge_ctx edge_A (Ξ» _, Tβ) := sorry
variables {X Y : Type} (ΟX : (X β Prop) β Prop) (ΟY : (Y β Prop) β Prop)
variables (e : X β Y) (he : β (s : set X), ΟX s β ΟY (e.symm β»ΒΉ' s))
variables {F : A} (hF : edge_ctx_tm unit_edge_ctx edge_A.{0 0} (Ξ» _, F))
include he hF
lemma invariant : F X ΟX β F Y ΟY :=
begin
have := hF.e β¨rflβ©,
swap, exact (), swap, exact (), swap, exact (),
specialize this X Y (discrete_edge_ctx_ty _) (discrete_edge_ctx_ty _),
specialize this β¨e, Ξ» x, equiv.refl _, _β©,
swap,
{ rintros xβ xβ β¨β© β¨β©,
-- need to check that the function `e` "preserves edges",
-- which is automatic because `X` is discrete
change plift (_ =[rfl] _) β plift (_ =[rfl] _),
rw [deq_iff_eq, deq_iff_eq],
refine equiv.plift.trans ((equiv.of_iff _).trans equiv.plift.symm),
symmetry,
apply equiv.apply_eq_iff_eq },
specialize this ΟX ΟY,
-- next we need a proof that ΟX "preserves edges",
-- which is automatic because `X β Prop` is discrete,
-- and the same for ΟY
specialize this _, swap,
{ fsplit,
{ rintro (s : set X) hs,
exact β¨β© },
{ rintro (sβ : set X) (sβ : set X) hsβ hsβ h,
suffices : sβ = sβ,
{ constructor,
rw this },
ext x,
specialize h x x () () β¨deq.refl _β©,
exact h.down } },
specialize this _, swap,
{ fsplit,
{ rintro (s : set Y) hs,
exact β¨β© },
{ rintro (sβ : set Y) (sβ : set Y) hsβ hsβ h,
suffices : sβ = sβ,
{ constructor,
rw this },
ext y,
specialize h y y () () β¨deq.refl _β©,
exact h.down } },
-- now we need to use the compatibility of `e` and `ΟX`, `ΟY`
specialize this _, swap,
{ rintros (s : set X) (t : set Y) _ _ h,
suffices : t = e.symm β»ΒΉ' s,
{ change plift (ΟX s β ΟY t),
rw this,
exact β¨he sβ© },
ext y,
specialize h (e.symm y) y () (),
specialize h β¨_β©, swap,
{ conv { whnf },
conv_lhs { change e (e.symm y) },
rw e.apply_symm_apply },
exact h.down.symm },
exact this.down
end
|
7f633d96c3d2d92a142efd24aaa3b8b991125d7b | 55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5 | /src/category_theory/limits/shapes/equalizers.lean | 49c80aea41d771ffa21a7baf5bdd1182f97d57e1 | [
"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 | 28,307 | lean | /-
Copyright (c) 2018 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Markus Himmel
-/
import category_theory.epi_mono
import category_theory.limits.limits
/-!
# Equalizers and coequalizers
This file defines (co)equalizers as special cases of (co)limits.
An equalizer is the categorical generalization of the subobject {a β A | f(a) = g(a)} known
from abelian groups or modules. It is a limit cone over the diagram formed by `f` and `g`.
A coequalizer is the dual concept.
## Main definitions
* `walking_parallel_pair` is the indexing category used for (co)equalizer_diagrams
* `parallel_pair` is a functor from `walking_parallel_pair` to our category `C`.
* a `fork` is a cone over a parallel pair.
* there is really only one interesting morphism in a fork: the arrow from the vertex of the fork
to the domain of f and g. It is called `fork.ΞΉ`.
* an `equalizer` is now just a `limit (parallel_pair f g)`
Each of these has a dual.
## Main statements
* `equalizer.ΞΉ_mono` states that every equalizer map is a monomorphism
* `is_iso_limit_cone_parallel_pair_of_self` states that the identity on the domain of `f` is an equalizer
of `f` and `f`.
## Implementation notes
As with the other special shapes in the limits library, all the definitions here are given as
`abbreviation`s of the general statements for limits, so all the `simp` lemmas and theorems about
general limits can be used.
## References
* [F. Borceux, *Handbook of Categorical Algebra 1*][borceux-vol1]
-/
open category_theory
namespace category_theory.limits
local attribute [tidy] tactic.case_bash
universes v u
/-- The type of objects for the diagram indexing a (co)equalizer. -/
@[derive decidable_eq, derive inhabited] inductive walking_parallel_pair : Type v
| zero | one
open walking_parallel_pair
/-- The type family of morphisms for the diagram indexing a (co)equalizer. -/
@[derive decidable_eq] inductive walking_parallel_pair_hom :
walking_parallel_pair β walking_parallel_pair β Type v
| left : walking_parallel_pair_hom zero one
| right : walking_parallel_pair_hom zero one
| id : Ξ X : walking_parallel_pair.{v}, walking_parallel_pair_hom X X
/-- Satisfying the inhabited linter -/
instance : inhabited (walking_parallel_pair_hom zero one) :=
{ default := walking_parallel_pair_hom.left }
open walking_parallel_pair_hom
/-- Composition of morphisms in the indexing diagram for (co)equalizers. -/
def walking_parallel_pair_hom.comp :
Ξ (X Y Z : walking_parallel_pair)
(f : walking_parallel_pair_hom X Y) (g : walking_parallel_pair_hom Y Z),
walking_parallel_pair_hom X Z
| _ _ _ (id _) h := h
| _ _ _ left (id one) := left
| _ _ _ right (id one) := right
.
instance walking_parallel_pair_hom_category : small_category walking_parallel_pair :=
{ hom := walking_parallel_pair_hom,
id := walking_parallel_pair_hom.id,
comp := walking_parallel_pair_hom.comp }
@[simp]
lemma walking_parallel_pair_hom_id (X : walking_parallel_pair) :
walking_parallel_pair_hom.id X = π X :=
rfl
variables {C : Type u} [category.{v} C]
variables {X Y : C}
/-- `parallel_pair f g` is the diagram in `C` consisting of the two morphisms `f` and `g` with
common domain and codomain. -/
def parallel_pair (f g : X βΆ Y) : walking_parallel_pair β₯€ C :=
{ obj := Ξ» x, match x with
| zero := X
| one := Y
end,
map := Ξ» x y h, match x, y, h with
| _, _, (id _) := π _
| _, _, left := f
| _, _, right := g
end,
-- `tidy` can cope with this, but it's too slow:
map_comp' := begin rintros (β¨β©|β¨β©) (β¨β©|β¨β©) (β¨β©|β¨β©) β¨β©β¨β©; { unfold_aux, simp; refl }, end, }.
@[simp] lemma parallel_pair_obj_zero (f g : X βΆ Y) : (parallel_pair f g).obj zero = X := rfl
@[simp] lemma parallel_pair_obj_one (f g : X βΆ Y) : (parallel_pair f g).obj one = Y := rfl
@[simp] lemma parallel_pair_map_left (f g : X βΆ Y) : (parallel_pair f g).map left = f := rfl
@[simp] lemma parallel_pair_map_right (f g : X βΆ Y) : (parallel_pair f g).map right = g := rfl
@[simp] lemma parallel_pair_functor_obj
{F : walking_parallel_pair β₯€ C} (j : walking_parallel_pair) :
(parallel_pair (F.map left) (F.map right)).obj j = F.obj j :=
begin
cases j; refl
end
/-- Every functor indexing a (co)equalizer is naturally isomorphic (actually, equal) to a
`parallel_pair` -/
def diagram_iso_parallel_pair (F : walking_parallel_pair β₯€ C) :
F β
parallel_pair (F.map left) (F.map right) :=
nat_iso.of_components (Ξ» j, eq_to_iso $ by cases j; tidy) $ by tidy
/-- A fork on `f` and `g` is just a `cone (parallel_pair f g)`. -/
abbreviation fork (f g : X βΆ Y) := cone (parallel_pair f g)
/-- A cofork on `f` and `g` is just a `cocone (parallel_pair f g)`. -/
abbreviation cofork (f g : X βΆ Y) := cocone (parallel_pair f g)
variables {f g : X βΆ Y}
@[simp, reassoc] lemma fork.app_zero_left (s : fork f g) :
(s.Ο).app zero β« f = (s.Ο).app one :=
by rw [βs.w left, parallel_pair_map_left]
@[simp, reassoc] lemma fork.app_zero_right (s : fork f g) :
(s.Ο).app zero β« g = (s.Ο).app one :=
by rw [βs.w right, parallel_pair_map_right]
@[simp, reassoc] lemma cofork.left_app_one (s : cofork f g) :
f β« (s.ΞΉ).app one = (s.ΞΉ).app zero :=
by rw [βs.w left, parallel_pair_map_left]
@[simp, reassoc] lemma cofork.right_app_one (s : cofork f g) :
g β« (s.ΞΉ).app one = (s.ΞΉ).app zero :=
by rw [βs.w right, parallel_pair_map_right]
/-- A fork on `f g : X βΆ Y` is determined by the morphism `ΞΉ : P βΆ X` satisfying `ΞΉ β« f = ΞΉ β« g`.
-/
def fork.of_ΞΉ {P : C} (ΞΉ : P βΆ X) (w : ΞΉ β« f = ΞΉ β« g) : fork f g :=
{ X := P,
Ο :=
{ app := Ξ» X, begin cases X, exact ΞΉ, exact ΞΉ β« f, end,
naturality' := Ξ» X Y f,
begin
cases X; cases Y; cases f; dsimp; simp,
{ dsimp, simp, }, -- TODO If someone could decipher why these aren't done on the previous line, that would be great
{ exact w },
{ dsimp, simp, }, -- TODO idem
end } }
/-- A cofork on `f g : X βΆ Y` is determined by the morphism `Ο : Y βΆ P` satisfying
`f β« Ο = g β« Ο`. -/
def cofork.of_Ο {P : C} (Ο : Y βΆ P) (w : f β« Ο = g β« Ο) : cofork f g :=
{ X := P,
ΞΉ :=
{ app := Ξ» X, begin cases X, exact f β« Ο, exact Ο, end,
naturality' := Ξ» X Y f,
begin
cases X; cases Y; cases f; dsimp; simp,
{ dsimp, simp, }, -- TODO idem
{ exact w.symm },
{ dsimp, simp, }, -- TODO idem
end } }
@[simp] lemma fork.of_ΞΉ_app_zero {P : C} (ΞΉ : P βΆ X) (w : ΞΉ β« f = ΞΉ β« g) :
(fork.of_ΞΉ ΞΉ w).Ο.app zero = ΞΉ := rfl
@[simp] lemma fork.of_ΞΉ_app_one {P : C} (ΞΉ : P βΆ X) (w : ΞΉ β« f = ΞΉ β« g) :
(fork.of_ΞΉ ΞΉ w).Ο.app one = ΞΉ β« f := rfl
@[simp] lemma cofork.of_Ο_app_zero {P : C} (Ο : Y βΆ P) (w : f β« Ο = g β« Ο) :
(cofork.of_Ο Ο w).ΞΉ.app zero = f β« Ο := rfl
@[simp] lemma cofork.of_Ο_app_one {P : C} (Ο : Y βΆ P) (w : f β« Ο = g β« Ο) :
(cofork.of_Ο Ο w).ΞΉ.app one = Ο := rfl
/-- A fork `t` on the parallel pair `f g : X βΆ Y` consists of two morphisms `t.Ο.app zero : t.X βΆ X`
and `t.Ο.app one : t.X βΆ Y`. Of these, only the first one is interesting, and we give it the
shorter name `fork.ΞΉ t`. -/
abbreviation fork.ΞΉ (t : fork f g) := t.Ο.app zero
/-- A cofork `t` on the parallel_pair `f g : X βΆ Y` consists of two morphisms
`t.ΞΉ.app zero : X βΆ t.X` and `t.ΞΉ.app one : Y βΆ t.X`. Of these, only the second one is
interesting, and we give it the shorter name `cofork.Ο t`. -/
abbreviation cofork.Ο (t : cofork f g) := t.ΞΉ.app one
@[simp] lemma fork.ΞΉ_of_ΞΉ {P : C} (ΞΉ : P βΆ X) (w : ΞΉ β« f = ΞΉ β« g) :
fork.ΞΉ (fork.of_ΞΉ ΞΉ w) = ΞΉ := rfl
@[simp] lemma cofork.Ο_of_Ο {P : C} (Ο : Y βΆ P) (w : f β« Ο = g β« Ο) :
cofork.Ο (cofork.of_Ο Ο w) = Ο := rfl
lemma fork.ΞΉ_eq_app_zero (t : fork f g) : fork.ΞΉ t = t.Ο.app zero := rfl
lemma cofork.Ο_eq_app_one (t : cofork f g) : cofork.Ο t = t.ΞΉ.app one := rfl
lemma fork.condition (t : fork f g) : (fork.ΞΉ t) β« f = (fork.ΞΉ t) β« g :=
begin
erw [t.w left, β t.w right], refl
end
lemma cofork.condition (t : cofork f g) : f β« (cofork.Ο t) = g β« (cofork.Ο t) :=
begin
erw [t.w left, β t.w right], refl
end
/-- To check whether two maps are equalized by both maps of a fork, it suffices to check it for the
first map -/
lemma fork.equalizer_ext (s : fork f g) {W : C} {k l : W βΆ s.X}
(h : k β« fork.ΞΉ s = l β« fork.ΞΉ s) : β (j : walking_parallel_pair),
k β« s.Ο.app j = l β« s.Ο.app j
| zero := h
| one := by rw [βfork.app_zero_left, reassoc_of h]
/-- To check whether two maps are coequalized by both maps of a cofork, it suffices to check it for
the second map -/
lemma cofork.coequalizer_ext (s : cofork f g) {W : C} {k l : s.X βΆ W}
(h : cofork.Ο s β« k = cofork.Ο s β« l) : β (j : walking_parallel_pair),
s.ΞΉ.app j β« k = s.ΞΉ.app j β« l
| zero := by simp only [βcofork.left_app_one, category.assoc, h]
| one := h
lemma fork.is_limit.hom_ext {s : fork f g} (hs : is_limit s) {W : C} {k l : W βΆ s.X}
(h : k β« fork.ΞΉ s = l β« fork.ΞΉ s) : k = l :=
hs.hom_ext $ fork.equalizer_ext _ h
lemma cofork.is_colimit.hom_ext {s : cofork f g} (hs : is_colimit s) {W : C} {k l : s.X βΆ W}
(h : cofork.Ο s β« k = cofork.Ο s β« l) : k = l :=
hs.hom_ext $ cofork.coequalizer_ext _ h
/-- If `s` is a limit fork over `f` and `g`, then a morphism `k : W βΆ X` satisfying
`k β« f = k β« g` induces a morphism `l : W βΆ s.X` such that `l β« fork.ΞΉ s = k`. -/
def fork.is_limit.lift' {s : fork f g} (hs : is_limit s) {W : C} (k : W βΆ X) (h : k β« f = k β« g) :
{l : W βΆ s.X // l β« fork.ΞΉ s = k} :=
β¨hs.lift $ fork.of_ΞΉ _ h, hs.fac _ _β©
/-- If `s` is a colimit cofork over `f` and `g`, then a morphism `k : Y βΆ W` satisfying
`f β« k = g β« k` induces a morphism `l : s.X βΆ W` such that `cofork.Ο s β« l = k`. -/
def cofork.is_colimit.desc' {s : cofork f g} (hs : is_colimit s) {W : C} (k : Y βΆ W)
(h : f β« k = g β« k) : {l : s.X βΆ W // cofork.Ο s β« l = k} :=
β¨hs.desc $ cofork.of_Ο _ h, hs.fac _ _β©
/-- This is a slightly more convenient method to verify that a fork is a limit cone. It
only asks for a proof of facts that carry any mathematical content -/
def fork.is_limit.mk (t : fork f g)
(lift : Ξ (s : fork f g), s.X βΆ t.X)
(fac : β (s : fork f g), lift s β« fork.ΞΉ t = fork.ΞΉ s)
(uniq : β (s : fork f g) (m : s.X βΆ t.X)
(w : β j : walking_parallel_pair, m β« t.Ο.app j = s.Ο.app j), m = lift s) :
is_limit t :=
{ lift := lift,
fac' := Ξ» s j, walking_parallel_pair.cases_on j (fac s) $
by erw [βs.w left, βt.w left, βcategory.assoc, fac]; refl,
uniq' := uniq }
/-- This is another convenient method to verify that a fork is a limit cone. It
only asks for a proof of facts that carry any mathematical content, and allows access to the
same `s` for all parts. -/
def fork.is_limit.mk' {X Y : C} {f g : X βΆ Y} (t : fork f g)
(create : Ξ (s : fork f g), {l // l β« t.ΞΉ = s.ΞΉ β§ β {m}, m β« t.ΞΉ = s.ΞΉ β m = l}) :
is_limit t :=
fork.is_limit.mk t
(Ξ» s, (create s).1)
(Ξ» s, (create s).2.1)
(Ξ» s m w, (create s).2.2 (w zero))
/-- This is a slightly more convenient method to verify that a cofork is a colimit cocone. It
only asks for a proof of facts that carry any mathematical content -/
def cofork.is_colimit.mk (t : cofork f g)
(desc : Ξ (s : cofork f g), t.X βΆ s.X)
(fac : β (s : cofork f g), cofork.Ο t β« desc s = cofork.Ο s)
(uniq : β (s : cofork f g) (m : t.X βΆ s.X)
(w : β j : walking_parallel_pair, t.ΞΉ.app j β« m = s.ΞΉ.app j), m = desc s) :
is_colimit t :=
{ desc := desc,
fac' := Ξ» s j, walking_parallel_pair.cases_on j
(by erw [βs.w left, βt.w left, category.assoc, fac]; refl) (fac s),
uniq' := uniq }
/-- This is another convenient method to verify that a fork is a limit cone. It
only asks for a proof of facts that carry any mathematical content, and allows access to the
same `s` for all parts. -/
def cofork.is_colimit.mk' {X Y : C} {f g : X βΆ Y} (t : cofork f g)
(create : Ξ (s : cofork f g), {l : t.X βΆ s.X // t.Ο β« l = s.Ο β§ β {m}, t.Ο β« m = s.Ο β m = l}) :
is_colimit t :=
cofork.is_colimit.mk t
(Ξ» s, (create s).1)
(Ξ» s, (create s).2.1)
(Ξ» s m w, (create s).2.2 (w one))
/-- This is a helper construction that can be useful when verifying that a category has all
equalizers. Given `F : walking_parallel_pair β₯€ C`, which is really the same as
`parallel_pair (F.map left) (F.map right)`, and a fork on `F.map left` and `F.map right`,
we get a cone on `F`.
If you're thinking about using this, have a look at `has_equalizers_of_has_limit_parallel_pair`,
which you may find to be an easier way of achieving your goal. -/
def cone.of_fork
{F : walking_parallel_pair β₯€ C} (t : fork (F.map left) (F.map right)) : cone F :=
{ X := t.X,
Ο :=
{ app := Ξ» X, t.Ο.app X β« eq_to_hom (by tidy),
naturality' := Ξ» j j' g, by { cases j; cases j'; cases g; dsimp; simp } } }
/-- This is a helper construction that can be useful when verifying that a category has all
coequalizers. Given `F : walking_parallel_pair β₯€ C`, which is really the same as
`parallel_pair (F.map left) (F.map right)`, and a cofork on `F.map left` and `F.map right`,
we get a cocone on `F`.
If you're thinking about using this, have a look at
`has_coequalizers_of_has_colimit_parallel_pair`, which you may find to be an easier way of
achieving your goal. -/
def cocone.of_cofork
{F : walking_parallel_pair β₯€ C} (t : cofork (F.map left) (F.map right)) : cocone F :=
{ X := t.X,
ΞΉ :=
{ app := Ξ» X, eq_to_hom (by tidy) β« t.ΞΉ.app X,
naturality' := Ξ» j j' g, by { cases j; cases j'; cases g; dsimp; simp } } }
@[simp] lemma cone.of_fork_Ο
{F : walking_parallel_pair β₯€ C} (t : fork (F.map left) (F.map right)) (j) :
(cone.of_fork t).Ο.app j = t.Ο.app j β« eq_to_hom (by tidy) := rfl
@[simp] lemma cocone.of_cofork_ΞΉ
{F : walking_parallel_pair β₯€ C} (t : cofork (F.map left) (F.map right)) (j) :
(cocone.of_cofork t).ΞΉ.app j = eq_to_hom (by tidy) β« t.ΞΉ.app j := rfl
/-- Given `F : walking_parallel_pair β₯€ C`, which is really the same as
`parallel_pair (F.map left) (F.map right)` and a cone on `F`, we get a fork on
`F.map left` and `F.map right`. -/
def fork.of_cone
{F : walking_parallel_pair β₯€ C} (t : cone F) : fork (F.map left) (F.map right) :=
{ X := t.X,
Ο := { app := Ξ» X, t.Ο.app X β« eq_to_hom (by tidy) } }
/-- Given `F : walking_parallel_pair β₯€ C`, which is really the same as
`parallel_pair (F.map left) (F.map right)` and a cocone on `F`, we get a cofork on
`F.map left` and `F.map right`. -/
def cofork.of_cocone
{F : walking_parallel_pair β₯€ C} (t : cocone F) : cofork (F.map left) (F.map right) :=
{ X := t.X,
ΞΉ := { app := Ξ» X, eq_to_hom (by tidy) β« t.ΞΉ.app X } }
@[simp] lemma fork.of_cone_Ο {F : walking_parallel_pair β₯€ C} (t : cone F) (j) :
(fork.of_cone t).Ο.app j = t.Ο.app j β« eq_to_hom (by tidy) := rfl
@[simp] lemma cofork.of_cocone_ΞΉ {F : walking_parallel_pair β₯€ C} (t : cocone F) (j) :
(cofork.of_cocone t).ΞΉ.app j = eq_to_hom (by tidy) β« t.ΞΉ.app j := rfl
variables (f g)
section
variables [has_limit (parallel_pair f g)]
/-- If we have chosen an equalizer of `f` and `g`, we can access the corresponding object by
saying `equalizer f g`. -/
abbreviation equalizer := limit (parallel_pair f g)
/-- If we have chosen an equalizer of `f` and `g`, we can access the inclusion
`equalizer f g βΆ X` by saying `equalizer.ΞΉ f g`. -/
abbreviation equalizer.ΞΉ : equalizer f g βΆ X :=
limit.Ο (parallel_pair f g) zero
@[simp] lemma equalizer.ΞΉ.fork :
fork.ΞΉ (limit.cone (parallel_pair f g)) = equalizer.ΞΉ f g := rfl
@[simp] lemma equalizer.ΞΉ.eq_app_zero :
(limit.cone (parallel_pair f g)).Ο.app zero = equalizer.ΞΉ f g := rfl
@[reassoc] lemma equalizer.condition : equalizer.ΞΉ f g β« f = equalizer.ΞΉ f g β« g :=
fork.condition $ limit.cone $ parallel_pair f g
variables {f g}
/-- A morphism `k : W βΆ X` satisfying `k β« f = k β« g` factors through the equalizer of `f` and `g`
via `equalizer.lift : W βΆ equalizer f g`. -/
abbreviation equalizer.lift {W : C} (k : W βΆ X) (h : k β« f = k β« g) : W βΆ equalizer f g :=
limit.lift (parallel_pair f g) (fork.of_ΞΉ k h)
@[simp, reassoc]
lemma equalizer.lift_ΞΉ {W : C} (k : W βΆ X) (h : k β« f = k β« g) :
equalizer.lift k h β« equalizer.ΞΉ f g = k :=
limit.lift_Ο _ _
/-- A morphism `k : W βΆ X` satisfying `k β« f = k β« g` induces a morphism `l : W βΆ equalizer f g`
satisfying `l β« equalizer.ΞΉ f g = k`. -/
def equalizer.lift' {W : C} (k : W βΆ X) (h : k β« f = k β« g) :
{l : W βΆ equalizer f g // l β« equalizer.ΞΉ f g = k} :=
β¨equalizer.lift k h, equalizer.lift_ΞΉ _ _β©
/-- Two maps into an equalizer are equal if they are are equal when composed with the equalizer
map. -/
@[ext] lemma equalizer.hom_ext {W : C} {k l : W βΆ equalizer f g}
(h : k β« equalizer.ΞΉ f g = l β« equalizer.ΞΉ f g) : k = l :=
fork.is_limit.hom_ext (limit.is_limit _) h
/-- An equalizer morphism is a monomorphism -/
instance equalizer.ΞΉ_mono : mono (equalizer.ΞΉ f g) :=
{ right_cancellation := Ξ» Z h k w, equalizer.hom_ext w }
end
section
variables {f g}
/-- The equalizer morphism in any limit cone is a monomorphism. -/
lemma mono_of_is_limit_parallel_pair {c : cone (parallel_pair f g)} (i : is_limit c) :
mono (fork.ΞΉ c) :=
{ right_cancellation := Ξ» Z h k w, fork.is_limit.hom_ext i w }
end
section
variables {f g}
/-- The identity determines a cone on the equalizer diagram of `f` and `g` if `f = g`. -/
def id_fork (h : f = g) : fork f g :=
fork.of_ΞΉ (π X) $ h βΈ rfl
/-- The identity on `X` is an equalizer of `(f, g)`, if `f = g`. -/
def is_limit_id_fork (h : f = g) : is_limit (id_fork h) :=
fork.is_limit.mk _
(Ξ» s, fork.ΞΉ s)
(Ξ» s, category.comp_id _)
(Ξ» s m h, by { convert h zero, exact (category.comp_id _).symm })
/-- Every equalizer of `(f, g)`, where `f = g`, is an isomorphism. -/
def is_iso_limit_cone_parallel_pair_of_eq (hβ : f = g) {c : cone (parallel_pair f g)}
(h : is_limit c) : is_iso (c.Ο.app zero) :=
is_iso.of_iso $ is_limit.cone_point_unique_up_to_iso h $ is_limit_id_fork hβ
/-- The equalizer of `(f, g)`, where `f = g`, is an isomorphism. -/
def equalizer.ΞΉ_of_eq [has_limit (parallel_pair f g)] (h : f = g) : is_iso (equalizer.ΞΉ f g) :=
is_iso_limit_cone_parallel_pair_of_eq h $ limit.is_limit _
/-- Every equalizer of `(f, f)` is an isomorphism. -/
def is_iso_limit_cone_parallel_pair_of_self {c : cone (parallel_pair f f)} (h : is_limit c) :
is_iso (c.Ο.app zero) :=
is_iso_limit_cone_parallel_pair_of_eq rfl h
/-- An equalizer that is an epimorphism is an isomorphism. -/
def is_iso_limit_cone_parallel_pair_of_epi {c : cone (parallel_pair f g)}
(h : is_limit c) [epi (c.Ο.app zero)] : is_iso (c.Ο.app zero) :=
is_iso_limit_cone_parallel_pair_of_eq ((cancel_epi _).1 (fork.condition c)) h
end
/-- The equalizer inclusion for `(f, f)` is an isomorphism. -/
instance equalizer.ΞΉ_of_self [has_limit (parallel_pair f f)] : is_iso (equalizer.ΞΉ f f) :=
equalizer.ΞΉ_of_eq rfl
/-- The equalizer of a morphism with itself is isomorphic to the source. -/
def equalizer.iso_source_of_self [has_limit (parallel_pair f f)] : equalizer f f β
X :=
as_iso (equalizer.ΞΉ f f)
@[simp] lemma equalizer.iso_source_of_self_hom [has_limit (parallel_pair f f)] :
(equalizer.iso_source_of_self f).hom = equalizer.ΞΉ f f :=
rfl
@[simp] lemma equalizer.iso_source_of_self_inv [has_limit (parallel_pair f f)] :
(equalizer.iso_source_of_self f).inv = equalizer.lift (π X) (by simp) :=
rfl
section
variables [has_colimit (parallel_pair f g)]
/-- If we have chosen a coequalizer of `f` and `g`, we can access the corresponding object by
saying `coequalizer f g`. -/
abbreviation coequalizer := colimit (parallel_pair f g)
/-- If we have chosen a coequalizer of `f` and `g`, we can access the corresponding projection by
saying `coequalizer.Ο f g`. -/
abbreviation coequalizer.Ο : Y βΆ coequalizer f g :=
colimit.ΞΉ (parallel_pair f g) one
@[simp] lemma coequalizer.Ο.cofork :
cofork.Ο (colimit.cocone (parallel_pair f g)) = coequalizer.Ο f g := rfl
@[simp] lemma coequalizer.Ο.eq_app_one :
(colimit.cocone (parallel_pair f g)).ΞΉ.app one = coequalizer.Ο f g := rfl
@[reassoc] lemma coequalizer.condition : f β« coequalizer.Ο f g = g β« coequalizer.Ο f g :=
cofork.condition $ colimit.cocone $ parallel_pair f g
variables {f g}
/-- Any morphism `k : Y βΆ W` satisfying `f β« k = g β« k` factors through the coequalizer of `f`
and `g` via `coequalizer.desc : coequalizer f g βΆ W`. -/
abbreviation coequalizer.desc {W : C} (k : Y βΆ W) (h : f β« k = g β« k) : coequalizer f g βΆ W :=
colimit.desc (parallel_pair f g) (cofork.of_Ο k h)
@[simp, reassoc]
lemma coequalizer.Ο_desc {W : C} (k : Y βΆ W) (h : f β« k = g β« k) :
coequalizer.Ο f g β« coequalizer.desc k h = k :=
colimit.ΞΉ_desc _ _
/-- Any morphism `k : Y βΆ W` satisfying `f β« k = g β« k` induces a morphism
`l : coequalizer f g βΆ W` satisfying `coequalizer.Ο β« g = l`. -/
def coequalizer.desc' {W : C} (k : Y βΆ W) (h : f β« k = g β« k) :
{l : coequalizer f g βΆ W // coequalizer.Ο f g β« l = k} :=
β¨coequalizer.desc k h, coequalizer.Ο_desc _ _β©
/-- Two maps from a coequalizer are equal if they are equal when composed with the coequalizer
map -/
@[ext] lemma coequalizer.hom_ext {W : C} {k l : coequalizer f g βΆ W}
(h : coequalizer.Ο f g β« k = coequalizer.Ο f g β« l) : k = l :=
cofork.is_colimit.hom_ext (colimit.is_colimit _) h
/-- A coequalizer morphism is an epimorphism -/
instance coequalizer.Ο_epi : epi (coequalizer.Ο f g) :=
{ left_cancellation := Ξ» Z h k w, coequalizer.hom_ext w }
end
section
variables {f g}
/-- The coequalizer morphism in any colimit cocone is an epimorphism. -/
lemma epi_of_is_colimit_parallel_pair {c : cocone (parallel_pair f g)} (i : is_colimit c) :
epi (c.ΞΉ.app one) :=
{ left_cancellation := Ξ» Z h k w, cofork.is_colimit.hom_ext i w }
end
section
variables {f g}
/-- The identity determines a cocone on the coequalizer diagram of `f` and `g`, if `f = g`. -/
def id_cofork (h : f = g) : cofork f g :=
cofork.of_Ο (π Y) $ h βΈ rfl
/-- The identity on `Y` is a coequalizer of `(f, g)`, where `f = g`. -/
def is_colimit_id_cofork (h : f = g) : is_colimit (id_cofork h) :=
cofork.is_colimit.mk _
(Ξ» s, cofork.Ο s)
(Ξ» s, category.id_comp _)
(Ξ» s m h, by { convert h one, exact (category.id_comp _).symm })
/-- Every coequalizer of `(f, g)`, where `f = g`, is an isomorphism. -/
def is_iso_colimit_cocone_parallel_pair_of_eq (hβ : f = g) {c : cocone (parallel_pair f g)}
(h : is_colimit c) : is_iso (c.ΞΉ.app one) :=
is_iso.of_iso $ is_colimit.cocone_point_unique_up_to_iso (is_colimit_id_cofork hβ) h
/-- The coequalizer of `(f, g)`, where `f = g`, is an isomorphism. -/
def coequalizer.Ο_of_eq [has_colimit (parallel_pair f g)] (h : f = g) :
is_iso (coequalizer.Ο f g) :=
is_iso_colimit_cocone_parallel_pair_of_eq h $ colimit.is_colimit _
/-- Every coequalizer of `(f, f)` is an isomorphism. -/
def is_iso_colimit_cocone_parallel_pair_of_self {c : cocone (parallel_pair f f)}
(h : is_colimit c) : is_iso (c.ΞΉ.app one) :=
is_iso_colimit_cocone_parallel_pair_of_eq rfl h
/-- A coequalizer that is a monomorphism is an isomorphism. -/
def is_iso_limit_cocone_parallel_pair_of_epi {c : cocone (parallel_pair f g)}
(h : is_colimit c) [mono (c.ΞΉ.app one)] : is_iso (c.ΞΉ.app one) :=
is_iso_colimit_cocone_parallel_pair_of_eq ((cancel_mono _).1 (cofork.condition c)) h
end
/-- The coequalizer projection for `(f, f)` is an isomorphism. -/
instance coequalizer.Ο_of_self [has_colimit (parallel_pair f f)] : is_iso (coequalizer.Ο f f) :=
coequalizer.Ο_of_eq rfl
/-- The coequalizer of a morphism with itself is isomorphic to the target. -/
def coequalizer.iso_target_of_self [has_colimit (parallel_pair f f)] : coequalizer f f β
Y :=
(as_iso (coequalizer.Ο f f)).symm
@[simp] lemma coequalizer.iso_target_of_self_hom [has_colimit (parallel_pair f f)] :
(coequalizer.iso_target_of_self f).hom = coequalizer.desc (π Y) (by simp) :=
rfl
@[simp] lemma coequalizer.iso_target_of_self_inv [has_colimit (parallel_pair f f)] :
(coequalizer.iso_target_of_self f).inv = coequalizer.Ο f f :=
rfl
variables (C)
/-- `has_equalizers` represents a choice of equalizer for every pair of morphisms -/
class has_equalizers :=
(has_limits_of_shape : has_limits_of_shape walking_parallel_pair C)
/-- `has_coequalizers` represents a choice of coequalizer for every pair of morphisms -/
class has_coequalizers :=
(has_colimits_of_shape : has_colimits_of_shape walking_parallel_pair C)
attribute [instance] has_equalizers.has_limits_of_shape has_coequalizers.has_colimits_of_shape
/-- If `C` has all limits of diagrams `parallel_pair f g`, then it has all equalizers -/
def has_equalizers_of_has_limit_parallel_pair
[Ξ {X Y : C} {f g : X βΆ Y}, has_limit (parallel_pair f g)] : has_equalizers C :=
{ has_limits_of_shape := { has_limit := Ξ» F, has_limit_of_iso (diagram_iso_parallel_pair F).symm } }
/-- If `C` has all colimits of diagrams `parallel_pair f g`, then it has all coequalizers -/
def has_coequalizers_of_has_colimit_parallel_pair
[Ξ {X Y : C} {f g : X βΆ Y}, has_colimit (parallel_pair f g)] : has_coequalizers C :=
{ has_colimits_of_shape := { has_colimit := Ξ» F, has_colimit_of_iso (diagram_iso_parallel_pair F) } }
section
-- In this section we show that a split mono `f` equalizes `(retraction f β« f)` and `(π Y)`.
variables {C} [split_mono f]
/--
A split mono `f` equalizes `(retraction f β« f)` and `(π Y)`.
Here we build the cone, and show in `split_mono_equalizes` that it is a limit cone.
-/
def cone_of_split_mono : cone (parallel_pair (π Y) (retraction f β« f)) :=
fork.of_ΞΉ f (by tidy)
@[simp] lemma cone_of_split_mono_Ο_app_zero : (cone_of_split_mono f).Ο.app zero = f := rfl
@[simp] lemma cone_of_split_mono_Ο_app_one : (cone_of_split_mono f).Ο.app one = f β« π Y := rfl
/--
A split mono `f` equalizes `(retraction f β« f)` and `(π Y)`.
-/
def split_mono_equalizes {X Y : C} (f : X βΆ Y) [split_mono f] : is_limit (cone_of_split_mono f) :=
{ lift := Ξ» s, s.Ο.app zero β« retraction f,
fac' := Ξ» s,
begin
rintros (β¨β©|β¨β©),
{ rw [cone_of_split_mono_Ο_app_zero],
erw [category.assoc, β s.Ο.naturality right, s.Ο.naturality left, category.comp_id], },
{ erw [cone_of_split_mono_Ο_app_one, category.comp_id, category.assoc,
β s.Ο.naturality right, category.id_comp], }
end,
uniq' := Ξ» s m w, begin rw β(w zero), simp, end, }
end
section
-- In this section we show that a split epi `f` coequalizes `(f β« section_ f)` and `(π X)`.
variables {C} [split_epi f]
/--
A split epi `f` coequalizes `(f β« section_ f)` and `(π X)`.
Here we build the cocone, and show in `split_epi_coequalizes` that it is a colimit cocone.
-/
def cocone_of_split_epi : cocone (parallel_pair (π X) (f β« section_ f)) :=
cofork.of_Ο f (by tidy)
@[simp] lemma cocone_of_split_epi_ΞΉ_app_one : (cocone_of_split_epi f).ΞΉ.app one = f := rfl
@[simp] lemma cocone_of_split_epi_ΞΉ_app_zero : (cocone_of_split_epi f).ΞΉ.app zero = π X β« f := rfl
/--
A split epi `f` coequalizes `(f β« section_ f)` and `(π X)`.
-/
def split_epi_coequalizes {X Y : C} (f : X βΆ Y) [split_epi f] : is_colimit (cocone_of_split_epi f) :=
{ desc := Ξ» s, section_ f β« s.ΞΉ.app one,
fac' := Ξ» s,
begin
rintros (β¨β©|β¨β©),
{ erw [cocone_of_split_epi_ΞΉ_app_zero, category.assoc, category.id_comp, βcategory.assoc,
s.ΞΉ.naturality right, functor.const.obj_map, category.comp_id], },
{ erw [cocone_of_split_epi_ΞΉ_app_one, βcategory.assoc, s.ΞΉ.naturality right,
βs.ΞΉ.naturality left, category.id_comp] }
end,
uniq' := Ξ» s m w, begin rw β(w one), simp, end, }
end
end category_theory.limits
|
89f9e49362c1adb91cf940f559de62751de7f51b | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/data/list/basic.lean | bbbb9ac72c1bdfe023a0f81fa6efd7ff058295d4 | [
"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 | 161,587 | lean | /-
Copyright (c) 2014 Parikshit Khanna. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Parikshit Khanna, Jeremy Avigad, Leonardo de Moura, Floris van Doorn, Mario Carneiro
-/
import data.nat.order.basic
/-!
# Basic properties of lists
-/
open function nat (hiding one_pos)
assert_not_exists set.range
namespace list
universes u v w x
variables {ΞΉ : Type*} {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w} {Ξ΄ : Type x} {lβ lβ : list Ξ±}
attribute [inline] list.head
/-- There is only one list of an empty type -/
instance unique_of_is_empty [is_empty Ξ±] : unique (list Ξ±) :=
{ uniq := Ξ» l, match l with
| [] := rfl
| (a :: l) := is_empty_elim a
end,
..list.inhabited Ξ± }
instance : is_left_id (list Ξ±) has_append.append [] :=
β¨ nil_append β©
instance : is_right_id (list Ξ±) has_append.append [] :=
β¨ append_nil β©
instance : is_associative (list Ξ±) has_append.append :=
β¨ append_assoc β©
theorem cons_ne_nil (a : Ξ±) (l : list Ξ±) : a::l β [].
theorem cons_ne_self (a : Ξ±) (l : list Ξ±) : a::l β l :=
mt (congr_arg length) (nat.succ_ne_self _)
theorem head_eq_of_cons_eq {hβ hβ : Ξ±} {tβ tβ : list Ξ±} :
(hβ::tβ) = (hβ::tβ) β hβ = hβ :=
assume Peq, list.no_confusion Peq (assume Pheq Pteq, Pheq)
theorem tail_eq_of_cons_eq {hβ hβ : Ξ±} {tβ tβ : list Ξ±} :
(hβ::tβ) = (hβ::tβ) β tβ = tβ :=
assume Peq, list.no_confusion Peq (assume Pheq Pteq, Pteq)
@[simp] theorem cons_injective {a : Ξ±} : injective (cons a) :=
assume lβ lβ, assume Pe, tail_eq_of_cons_eq Pe
theorem cons_inj (a : Ξ±) {l l' : list Ξ±} : a::l = a::l' β l = l' :=
cons_injective.eq_iff
theorem cons_eq_cons {a b : Ξ±} {l l' : list Ξ±} : a::l = b::l' β a = b β§ l = l' :=
β¨list.cons.inj, Ξ» h, h.1 βΈ h.2 βΈ rflβ©
lemma singleton_injective : injective (Ξ» a : Ξ±, [a]) := Ξ» a b h, (cons_eq_cons.1 h).1
lemma singleton_inj {a b : Ξ±} : [a] = [b] β a = b := singleton_injective.eq_iff
theorem exists_cons_of_ne_nil {l : list Ξ±} (h : l β nil) : β b L, l = b :: L :=
by { induction l with c l', contradiction, use [c,l'], }
lemma set_of_mem_cons (l : list Ξ±) (a : Ξ±) : {x | x β a :: l} = insert a {x | x β l} := rfl
/-! ### mem -/
theorem mem_singleton_self (a : Ξ±) : a β [a] := mem_cons_self _ _
theorem eq_of_mem_singleton {a b : Ξ±} : a β [b] β a = b :=
assume : a β [b], or.elim (eq_or_mem_of_mem_cons this)
(assume : a = b, this)
(assume : a β [], absurd this (not_mem_nil a))
@[simp] theorem mem_singleton {a b : Ξ±} : a β [b] β a = b :=
β¨eq_of_mem_singleton, or.inlβ©
theorem mem_of_mem_cons_of_mem {a b : Ξ±} {l : list Ξ±} : a β b::l β b β l β a β l :=
assume ainbl binl, or.elim (eq_or_mem_of_mem_cons ainbl)
(assume : a = b, begin subst a, exact binl end)
(assume : a β l, this)
theorem _root_.decidable.list.eq_or_ne_mem_of_mem [decidable_eq Ξ±]
{a b : Ξ±} {l : list Ξ±} (h : a β b :: l) : a = b β¨ (a β b β§ a β l) :=
decidable.by_cases or.inl $ assume : a β b, h.elim or.inl $ assume h, or.inr β¨this, hβ©
theorem eq_or_ne_mem_of_mem {a b : Ξ±} {l : list Ξ±} : a β b :: l β a = b β¨ (a β b β§ a β l) :=
by classical; exact decidable.list.eq_or_ne_mem_of_mem
theorem not_mem_append {a : Ξ±} {s t : list Ξ±} (hβ : a β s) (hβ : a β t) : a β s ++ t :=
mt mem_append.1 $ not_or_distrib.2 β¨hβ, hββ©
theorem ne_nil_of_mem {a : Ξ±} {l : list Ξ±} (h : a β l) : l β [] :=
by intro e; rw e at h; cases h
theorem mem_split {a : Ξ±} {l : list Ξ±} (h : a β l) : β s t : list Ξ±, l = s ++ a :: t :=
begin
induction l with b l ih, {cases h}, rcases h with rfl | h,
{ exact β¨[], l, rflβ© },
{ rcases ih h with β¨s, t, rflβ©,
exact β¨b::s, t, rflβ© }
end
theorem mem_of_ne_of_mem {a y : Ξ±} {l : list Ξ±} (hβ : a β y) (hβ : a β y :: l) : a β l :=
or.elim (eq_or_mem_of_mem_cons hβ) (Ξ»e, absurd e hβ) (Ξ»r, r)
theorem ne_of_not_mem_cons {a b : Ξ±} {l : list Ξ±} : a β b::l β a β b :=
assume nin aeqb, absurd (or.inl aeqb) nin
theorem not_mem_of_not_mem_cons {a b : Ξ±} {l : list Ξ±} : a β b::l β a β l :=
assume nin nainl, absurd (or.inr nainl) nin
theorem not_mem_cons_of_ne_of_not_mem {a y : Ξ±} {l : list Ξ±} : a β y β a β l β a β y::l :=
assume p1 p2, not.intro (assume Pain, absurd (eq_or_mem_of_mem_cons Pain) (not_or p1 p2))
theorem ne_and_not_mem_of_not_mem_cons {a y : Ξ±} {l : list Ξ±} : a β y::l β a β y β§ a β l :=
assume p, and.intro (ne_of_not_mem_cons p) (not_mem_of_not_mem_cons p)
@[simp] theorem mem_map {f : Ξ± β Ξ²} {b : Ξ²} {l : list Ξ±} : b β map f l β β a, a β l β§ f a = b :=
begin
-- This proof uses no axioms, that's why it's longer that `induction`; simp [...]
induction l with a l ihl,
{ split, { rintro β¨_β© }, { rintro β¨a, β¨_β©, _β© } },
{ refine (or_congr eq_comm ihl).trans _,
split,
{ rintro (h|β¨c, hcl, hβ©),
exacts [β¨a, or.inl rfl, hβ©, β¨c, or.inr hcl, hβ©] },
{ rintro β¨c, (hc|hc), hβ©,
exacts [or.inl $ (congr_arg f hc.symm).trans h, or.inr β¨c, hc, hβ©] } }
end
alias mem_map β exists_of_mem_map _
theorem mem_map_of_mem (f : Ξ± β Ξ²) {a : Ξ±} {l : list Ξ±} (h : a β l) : f a β map f l :=
mem_map.2 β¨a, h, rflβ©
theorem mem_map_of_injective {f : Ξ± β Ξ²} (H : injective f) {a : Ξ±} {l : list Ξ±} :
f a β map f l β a β l :=
β¨Ξ» m, let β¨a', m', eβ© := exists_of_mem_map m in H e βΈ m', mem_map_of_mem _β©
@[simp] lemma _root_.function.involutive.exists_mem_and_apply_eq_iff {f : Ξ± β Ξ±}
(hf : function.involutive f) (x : Ξ±) (l : list Ξ±) :
(β (y : Ξ±), y β l β§ f y = x) β f x β l :=
β¨by { rintro β¨y, h, rflβ©, rwa hf y }, Ξ» h, β¨f x, h, hf _β©β©
theorem mem_map_of_involutive {f : Ξ± β Ξ±} (hf : involutive f) {a : Ξ±} {l : list Ξ±} :
a β map f l β f a β l :=
by rw [mem_map, hf.exists_mem_and_apply_eq_iff]
lemma forall_mem_map_iff {f : Ξ± β Ξ²} {l : list Ξ±} {P : Ξ² β Prop} :
(β i β l.map f, P i) β β j β l, P (f j) :=
begin
split,
{ assume H j hj,
exact H (f j) (mem_map_of_mem f hj) },
{ assume H i hi,
rcases mem_map.1 hi with β¨j, hj, jiβ©,
rw β ji,
exact H j hj }
end
@[simp] lemma map_eq_nil {f : Ξ± β Ξ²} {l : list Ξ±} : list.map f l = [] β l = [] :=
β¨by cases l; simp only [forall_prop_of_true, map, forall_prop_of_false, not_false_iff],
Ξ» h, h.symm βΈ rflβ©
@[simp] theorem mem_join {a : Ξ±} : β {L : list (list Ξ±)}, a β join L β β l, l β L β§ a β l
| [] := β¨false.elim, Ξ»β¨_, h, _β©, false.elim hβ©
| (c :: L) := by simp only [join, mem_append, @mem_join L, mem_cons_iff, or_and_distrib_right,
exists_or_distrib, exists_eq_left]
theorem exists_of_mem_join {a : Ξ±} {L : list (list Ξ±)} : a β join L β β l, l β L β§ a β l :=
mem_join.1
theorem mem_join_of_mem {a : Ξ±} {L : list (list Ξ±)} {l} (lL : l β L) (al : a β l) : a β join L :=
mem_join.2 β¨l, lL, alβ©
@[simp]
theorem mem_bind {b : Ξ²} {l : list Ξ±} {f : Ξ± β list Ξ²} : b β list.bind l f β β a β l, b β f a :=
iff.trans mem_join
β¨Ξ» β¨l', h1, h2β©, let β¨a, al, faβ© := exists_of_mem_map h1 in β¨a, al, fa.symm βΈ h2β©,
Ξ» β¨a, al, bfaβ©, β¨f a, mem_map_of_mem _ al, bfaβ©β©
theorem exists_of_mem_bind {b : Ξ²} {l : list Ξ±} {f : Ξ± β list Ξ²} :
b β list.bind l f β β a β l, b β f a :=
mem_bind.1
theorem mem_bind_of_mem {b : Ξ²} {l : list Ξ±} {f : Ξ± β list Ξ²} {a} (al : a β l) (h : b β f a) :
b β list.bind l f :=
mem_bind.2 β¨a, al, hβ©
lemma bind_map {g : Ξ± β list Ξ²} {f : Ξ² β Ξ³} :
β(l : list Ξ±), list.map f (l.bind g) = l.bind (Ξ»a, (g a).map f)
| [] := rfl
| (a::l) := by simp only [cons_bind, map_append, bind_map l]
lemma map_bind (g : Ξ² β list Ξ³) (f : Ξ± β Ξ²) :
β l : list Ξ±, (list.map f l).bind g = l.bind (Ξ» a, g (f a))
| [] := rfl
| (a::l) := by simp only [cons_bind, map_cons, map_bind l]
/-! ### length -/
theorem length_eq_zero {l : list Ξ±} : length l = 0 β l = [] :=
β¨eq_nil_of_length_eq_zero, Ξ» h, h.symm βΈ rflβ©
@[simp] lemma length_singleton (a : Ξ±) : length [a] = 1 := rfl
theorem length_pos_of_mem {a : Ξ±} : β {l : list Ξ±}, a β l β 0 < length l
| (b::l) _ := zero_lt_succ _
theorem exists_mem_of_length_pos : β {l : list Ξ±}, 0 < length l β β a, a β l
| (b::l) _ := β¨b, mem_cons_self _ _β©
theorem length_pos_iff_exists_mem {l : list Ξ±} : 0 < length l β β a, a β l :=
β¨exists_mem_of_length_pos, Ξ» β¨a, hβ©, length_pos_of_mem hβ©
theorem ne_nil_of_length_pos {l : list Ξ±} : 0 < length l β l β [] :=
Ξ» h1 h2, lt_irrefl 0 ((length_eq_zero.2 h2).subst h1)
theorem length_pos_of_ne_nil {l : list Ξ±} : l β [] β 0 < length l :=
Ξ» h, pos_iff_ne_zero.2 $ Ξ» h0, h $ length_eq_zero.1 h0
theorem length_pos_iff_ne_nil {l : list Ξ±} : 0 < length l β l β [] :=
β¨ne_nil_of_length_pos, length_pos_of_ne_nilβ©
lemma exists_mem_of_ne_nil (l : list Ξ±) (h : l β []) : β x, x β l :=
exists_mem_of_length_pos (length_pos_of_ne_nil h)
theorem length_eq_one {l : list Ξ±} : length l = 1 β β a, l = [a] :=
β¨match l with [a], _ := β¨a, rflβ© end, Ξ» β¨a, eβ©, e.symm βΈ rflβ©
lemma exists_of_length_succ {n} :
β l : list Ξ±, l.length = n + 1 β β h t, l = h :: t
| [] H := absurd H.symm $ succ_ne_zero n
| (h :: t) H := β¨h, t, rflβ©
@[simp] lemma length_injective_iff : injective (list.length : list Ξ± β β) β subsingleton Ξ± :=
begin
split,
{ intro h, refine β¨Ξ» x y, _β©, suffices : [x] = [y], { simpa using this }, apply h, refl },
{ intros hΞ± l1 l2 hl, induction l1 generalizing l2; cases l2,
{ refl }, { cases hl }, { cases hl },
congr, exactI subsingleton.elim _ _, apply l1_ih, simpa using hl }
end
@[simp] lemma length_injective [subsingleton Ξ±] : injective (length : list Ξ± β β) :=
length_injective_iff.mpr $ by apply_instance
lemma length_eq_two {l : list Ξ±} : l.length = 2 β β a b, l = [a, b] :=
β¨match l with [a, b], _ := β¨a, b, rflβ© end, Ξ» β¨a, b, eβ©, e.symm βΈ rflβ©
lemma length_eq_three {l : list Ξ±} : l.length = 3 β β a b c, l = [a, b, c] :=
β¨match l with [a, b, c], _ := β¨a, b, c, rflβ© end, Ξ» β¨a, b, c, eβ©, e.symm βΈ rflβ©
alias length_le_of_sublist β sublist.length_le
/-! ### set-theoretic notation of lists -/
lemma empty_eq : (β
: list Ξ±) = [] := by refl
lemma singleton_eq (x : Ξ±) : ({x} : list Ξ±) = [x] := rfl
lemma insert_neg [decidable_eq Ξ±] {x : Ξ±} {l : list Ξ±} (h : x β l) :
has_insert.insert x l = x :: l :=
if_neg h
lemma insert_pos [decidable_eq Ξ±] {x : Ξ±} {l : list Ξ±} (h : x β l) :
has_insert.insert x l = l :=
if_pos h
lemma doubleton_eq [decidable_eq Ξ±] {x y : Ξ±} (h : x β y) : ({x, y} : list Ξ±) = [x, y] :=
by { rw [insert_neg, singleton_eq], rwa [singleton_eq, mem_singleton] }
/-! ### bounded quantifiers over lists -/
theorem forall_mem_nil (p : Ξ± β Prop) : β x β @nil Ξ±, p x.
theorem forall_mem_cons : β {p : Ξ± β Prop} {a : Ξ±} {l : list Ξ±},
(β x β a :: l, p x) β p a β§ β x β l, p x :=
ball_cons
theorem forall_mem_of_forall_mem_cons {p : Ξ± β Prop} {a : Ξ±} {l : list Ξ±}
(h : β x β a :: l, p x) :
β x β l, p x :=
(forall_mem_cons.1 h).2
theorem forall_mem_singleton {p : Ξ± β Prop} {a : Ξ±} : (β x β [a], p x) β p a :=
by simp only [mem_singleton, forall_eq]
theorem forall_mem_append {p : Ξ± β Prop} {lβ lβ : list Ξ±} :
(β x β lβ ++ lβ, p x) β (β x β lβ, p x) β§ (β x β lβ, p x) :=
by simp only [mem_append, or_imp_distrib, forall_and_distrib]
theorem not_exists_mem_nil (p : Ξ± β Prop) : Β¬ β x β @nil Ξ±, p x.
theorem exists_mem_cons_of {p : Ξ± β Prop} {a : Ξ±} (l : list Ξ±) (h : p a) :
β x β a :: l, p x :=
bex.intro a (mem_cons_self _ _) h
theorem exists_mem_cons_of_exists {p : Ξ± β Prop} {a : Ξ±} {l : list Ξ±} (h : β x β l, p x) :
β x β a :: l, p x :=
bex.elim h (Ξ» x xl px, bex.intro x (mem_cons_of_mem _ xl) px)
theorem or_exists_of_exists_mem_cons {p : Ξ± β Prop} {a : Ξ±} {l : list Ξ±} (h : β x β a :: l, p x) :
p a β¨ β x β l, p x :=
bex.elim h (Ξ» x xal px,
or.elim (eq_or_mem_of_mem_cons xal)
(assume : x = a, begin rw βthis, left, exact px end)
(assume : x β l, or.inr (bex.intro x this px)))
theorem exists_mem_cons_iff (p : Ξ± β Prop) (a : Ξ±) (l : list Ξ±) :
(β x β a :: l, p x) β p a β¨ β x β l, p x :=
iff.intro or_exists_of_exists_mem_cons
(assume h, or.elim h (exists_mem_cons_of l) exists_mem_cons_of_exists)
/-! ### list subset -/
theorem subset_def {lβ lβ : list Ξ±} : lβ β lβ β β β¦a : Ξ±β¦, a β lβ β a β lβ := iff.rfl
theorem subset_append_of_subset_left (l lβ lβ : list Ξ±) : l β lβ β l β lβ++lβ :=
Ξ» s, subset.trans s $ subset_append_left _ _
theorem subset_append_of_subset_right (l lβ lβ : list Ξ±) : l β lβ β l β lβ++lβ :=
Ξ» s, subset.trans s $ subset_append_right _ _
@[simp] theorem cons_subset {a : Ξ±} {l m : list Ξ±} :
a::l β m β a β m β§ l β m :=
by simp only [subset_def, mem_cons_iff, or_imp_distrib, forall_and_distrib, forall_eq]
theorem cons_subset_of_subset_of_mem {a : Ξ±} {l m : list Ξ±}
(ainm : a β m) (lsubm : l β m) : a::l β m :=
cons_subset.2 β¨ainm, lsubmβ©
theorem append_subset_of_subset_of_subset {lβ lβ l : list Ξ±} (lβsubl : lβ β l) (lβsubl : lβ β l) :
lβ ++ lβ β l :=
Ξ» a h, (mem_append.1 h).elim (@lβsubl _) (@lβsubl _)
@[simp] theorem append_subset_iff {lβ lβ l : list Ξ±} :
lβ ++ lβ β l β lβ β l β§ lβ β l :=
begin
split,
{ intro h, simp only [subset_def] at *, split; intros; simp* },
{ rintro β¨h1, h2β©, apply append_subset_of_subset_of_subset h1 h2 }
end
theorem eq_nil_of_subset_nil : β {l : list Ξ±}, l β [] β l = []
| [] s := rfl
| (a::l) s := false.elim $ s $ mem_cons_self a l
theorem eq_nil_iff_forall_not_mem {l : list Ξ±} : l = [] β β a, a β l :=
show l = [] β l β [], from β¨Ξ» e, e βΈ subset.refl _, eq_nil_of_subset_nilβ©
theorem map_subset {lβ lβ : list Ξ±} (f : Ξ± β Ξ²) (H : lβ β lβ) : map f lβ β map f lβ :=
Ξ» x, by simp only [mem_map, not_and, exists_imp_distrib, and_imp]; exact Ξ» a h e, β¨a, H h, eβ©
theorem map_subset_iff {lβ lβ : list Ξ±} (f : Ξ± β Ξ²) (h : injective f) :
map f lβ β map f lβ β lβ β lβ :=
begin
refine β¨_, map_subset fβ©, intros h2 x hx,
rcases mem_map.1 (h2 (mem_map_of_mem f hx)) with β¨x', hx', hxx'β©,
cases h hxx', exact hx'
end
/-! ### append -/
lemma append_eq_has_append {Lβ Lβ : list Ξ±} : list.append Lβ Lβ = Lβ ++ Lβ := rfl
@[simp] lemma singleton_append {x : Ξ±} {l : list Ξ±} : [x] ++ l = x :: l := rfl
theorem append_ne_nil_of_ne_nil_left (s t : list Ξ±) : s β [] β s ++ t β [] :=
by induction s; intros; contradiction
theorem append_ne_nil_of_ne_nil_right (s t : list Ξ±) : t β [] β s ++ t β [] :=
by induction s; intros; contradiction
@[simp] lemma append_eq_nil {p q : list Ξ±} : (p ++ q) = [] β p = [] β§ q = [] :=
by cases p; simp only [nil_append, cons_append, eq_self_iff_true, true_and, false_and]
@[simp] lemma nil_eq_append_iff {a b : list Ξ±} : [] = a ++ b β a = [] β§ b = [] :=
by rw [eq_comm, append_eq_nil]
lemma append_eq_cons_iff {a b c : list Ξ±} {x : Ξ±} :
a ++ b = x :: c β (a = [] β§ b = x :: c) β¨ (βa', a = x :: a' β§ c = a' ++ b) :=
by cases a; simp only [and_assoc, @eq_comm _ c, nil_append, cons_append, eq_self_iff_true,
true_and, false_and, exists_false, false_or, or_false, exists_and_distrib_left, exists_eq_left']
lemma cons_eq_append_iff {a b c : list Ξ±} {x : Ξ±} :
(x :: c : list Ξ±) = a ++ b β (a = [] β§ b = x :: c) β¨ (βa', a = x :: a' β§ c = a' ++ b) :=
by rw [eq_comm, append_eq_cons_iff]
lemma append_eq_append_iff {a b c d : list Ξ±} :
a ++ b = c ++ d β (βa', c = a ++ a' β§ b = a' ++ d) β¨ (βc', a = c ++ c' β§ d = c' ++ b) :=
begin
induction a generalizing c,
case nil { rw nil_append, split,
{ rintro rfl, left, exact β¨_, rfl, rflβ© },
{ rintro (β¨a', rfl, rflβ© | β¨a', H, rflβ©), {refl}, {rw [β append_assoc, β H], refl} } },
case cons : a as ih
{ cases c,
{ simp only [cons_append, nil_append, false_and, exists_false, false_or, exists_eq_left'],
exact eq_comm },
{ simp only [cons_append, @eq_comm _ a, ih, and_assoc, and_or_distrib_left,
exists_and_distrib_left] } }
end
@[simp] theorem take_append_drop : β (n : β) (l : list Ξ±), take n l ++ drop n l = l
| 0 a := rfl
| (succ n) [] := rfl
| (succ n) (x :: xs) := congr_arg (cons x) $ take_append_drop n xs
-- TODO(Leo): cleanup proof after arith dec proc
theorem append_inj :
β {sβ sβ tβ tβ : list Ξ±}, sβ ++ tβ = sβ ++ tβ β length sβ = length sβ β sβ = sβ β§ tβ = tβ
| [] [] tβ tβ h hl := β¨rfl, hβ©
| (a::sβ) [] tβ tβ h hl := list.no_confusion $ eq_nil_of_length_eq_zero hl
| [] (b::sβ) tβ tβ h hl := list.no_confusion $ eq_nil_of_length_eq_zero hl.symm
| (a::sβ) (b::sβ) tβ tβ h hl := list.no_confusion h $ Ξ»ab hap,
let β¨e1, e2β© := @append_inj sβ sβ tβ tβ hap (succ.inj hl) in
by rw [ab, e1, e2]; exact β¨rfl, rflβ©
theorem append_inj_right {sβ sβ tβ tβ : list Ξ±} (h : sβ ++ tβ = sβ ++ tβ)
(hl : length sβ = length sβ) : tβ = tβ :=
(append_inj h hl).right
theorem append_inj_left {sβ sβ tβ tβ : list Ξ±} (h : sβ ++ tβ = sβ ++ tβ)
(hl : length sβ = length sβ) : sβ = sβ :=
(append_inj h hl).left
theorem append_inj' {sβ sβ tβ tβ : list Ξ±} (h : sβ ++ tβ = sβ ++ tβ) (hl : length tβ = length tβ) :
sβ = sβ β§ tβ = tβ :=
append_inj h $ @nat.add_right_cancel _ (length tβ) _ $
let hap := congr_arg length h in by simp only [length_append] at hap; rwa [β hl] at hap
theorem append_inj_right' {sβ sβ tβ tβ : list Ξ±} (h : sβ ++ tβ = sβ ++ tβ)
(hl : length tβ = length tβ) : tβ = tβ :=
(append_inj' h hl).right
theorem append_inj_left' {sβ sβ tβ tβ : list Ξ±} (h : sβ ++ tβ = sβ ++ tβ)
(hl : length tβ = length tβ) : sβ = sβ :=
(append_inj' h hl).left
theorem append_left_cancel {s tβ tβ : list Ξ±} (h : s ++ tβ = s ++ tβ) : tβ = tβ :=
append_inj_right h rfl
theorem append_right_cancel {sβ sβ t : list Ξ±} (h : sβ ++ t = sβ ++ t) : sβ = sβ :=
append_inj_left' h rfl
theorem append_right_injective (s : list Ξ±) : function.injective (Ξ» t, s ++ t) :=
Ξ» tβ tβ, append_left_cancel
theorem append_right_inj {tβ tβ : list Ξ±} (s) : s ++ tβ = s ++ tβ β tβ = tβ :=
(append_right_injective s).eq_iff
theorem append_left_injective (t : list Ξ±) : function.injective (Ξ» s, s ++ t) :=
Ξ» sβ sβ, append_right_cancel
theorem append_left_inj {sβ sβ : list Ξ±} (t) : sβ ++ t = sβ ++ t β sβ = sβ :=
(append_left_injective t).eq_iff
theorem map_eq_append_split {f : Ξ± β Ξ²} {l : list Ξ±} {sβ sβ : list Ξ²}
(h : map f l = sβ ++ sβ) : β lβ lβ, l = lβ ++ lβ β§ map f lβ = sβ β§ map f lβ = sβ :=
begin
have := h, rw [β take_append_drop (length sβ) l] at this β’,
rw map_append at this,
refine β¨_, _, rfl, append_inj this _β©,
rw [length_map, length_take, min_eq_left],
rw [β length_map f l, h, length_append],
apply nat.le_add_right
end
/-! ### repeat -/
@[simp] theorem repeat_succ (a : Ξ±) (n) : repeat a (n + 1) = a :: repeat a n := rfl
theorem mem_repeat {a b : Ξ±} : β {n}, b β repeat a n β n β 0 β§ b = a
| 0 := by simp
| (n + 1) := by simp [mem_repeat]
theorem eq_of_mem_repeat {a b : Ξ±} {n} (h : b β repeat a n) : b = a :=
(mem_repeat.1 h).2
theorem eq_repeat_of_mem {a : Ξ±} : β {l : list Ξ±}, (β b β l, b = a) β l = repeat a l.length
| [] H := rfl
| (b::l) H := by cases forall_mem_cons.1 H with Hβ Hβ;
unfold length repeat; congr; [exact Hβ, exact eq_repeat_of_mem Hβ]
theorem eq_repeat' {a : Ξ±} {l : list Ξ±} : l = repeat a l.length β β b β l, b = a :=
β¨Ξ» h, h.symm βΈ Ξ» b, eq_of_mem_repeat, eq_repeat_of_memβ©
theorem eq_repeat {a : Ξ±} {n} {l : list Ξ±} : l = repeat a n β length l = n β§ β b β l, b = a :=
β¨Ξ» h, h.symm βΈ β¨length_repeat _ _, Ξ» b, eq_of_mem_repeatβ©,
Ξ» β¨e, alβ©, e βΈ eq_repeat_of_mem alβ©
theorem repeat_add (a : Ξ±) (m n) : repeat a (m + n) = repeat a m ++ repeat a n :=
by induction m; simp only [*, zero_add, succ_add, repeat]; split; refl
theorem repeat_subset_singleton (a : Ξ±) (n) : repeat a n β [a] :=
Ξ» b h, mem_singleton.2 (eq_of_mem_repeat h)
lemma subset_singleton_iff {a : Ξ±} : β L : list Ξ±, L β [a] β β n, L = repeat a n
| [] := β¨Ξ» h, β¨0, by simpβ©, by simpβ©
| (h :: L) := begin
refine β¨Ξ» h, _, Ξ» β¨k, hkβ©, by simp [hk, repeat_subset_singleton]β©,
rw [cons_subset] at h,
obtain β¨n, rflβ© := (subset_singleton_iff L).mp h.2,
exact β¨n.succ, by simp [mem_singleton.mp h.1]β©
end
@[simp] theorem map_const (l : list Ξ±) (b : Ξ²) : map (function.const Ξ± b) l = repeat b l.length :=
by induction l; [refl, simp only [*, map]]; split; refl
theorem eq_of_mem_map_const {bβ bβ : Ξ²} {l : list Ξ±} (h : bβ β map (function.const Ξ± bβ) l) :
bβ = bβ :=
by rw map_const at h; exact eq_of_mem_repeat h
@[simp] theorem map_repeat (f : Ξ± β Ξ²) (a : Ξ±) (n) : map f (repeat a n) = repeat (f a) n :=
by induction n; [refl, simp only [*, repeat, map]]; split; refl
@[simp] theorem tail_repeat (a : Ξ±) (n) : tail (repeat a n) = repeat a n.pred :=
by cases n; refl
@[simp] theorem join_repeat_nil (n : β) : join (repeat [] n) = @nil Ξ± :=
by induction n; [refl, simp only [*, repeat, join, append_nil]]
lemma repeat_left_injective {n : β} (hn : n β 0) :
function.injective (Ξ» a : Ξ±, repeat a n) :=
Ξ» a b h, (eq_repeat.1 h).2 _ $ mem_repeat.2 β¨hn, rflβ©
lemma repeat_left_inj {a b : Ξ±} {n : β} (hn : n β 0) :
repeat a n = repeat b n β a = b :=
(repeat_left_injective hn).eq_iff
@[simp] lemma repeat_left_inj' {a b : Ξ±} :
β {n}, repeat a n = repeat b n β n = 0 β¨ a = b
| 0 := by simp
| (n + 1) := (repeat_left_inj n.succ_ne_zero).trans $ by simp only [n.succ_ne_zero, false_or]
lemma repeat_right_injective (a : Ξ±) : function.injective (repeat a) :=
function.left_inverse.injective (length_repeat a)
@[simp] lemma repeat_right_inj {a : Ξ±} {n m : β} :
repeat a n = repeat a m β n = m :=
(repeat_right_injective a).eq_iff
/-! ### pure -/
@[simp] theorem mem_pure {Ξ±} (x y : Ξ±) :
x β (pure y : list Ξ±) β x = y := by simp! [pure,list.ret]
/-! ### bind -/
@[simp] theorem bind_eq_bind {Ξ± Ξ²} (f : Ξ± β list Ξ²) (l : list Ξ±) :
l >>= f = l.bind f := rfl
-- TODO: duplicate of a lemma in core
theorem bind_append (f : Ξ± β list Ξ²) (lβ lβ : list Ξ±) :
(lβ ++ lβ).bind f = lβ.bind f ++ lβ.bind f :=
append_bind _ _ _
@[simp] theorem bind_singleton (f : Ξ± β list Ξ²) (x : Ξ±) : [x].bind f = f x :=
append_nil (f x)
@[simp] theorem bind_singleton' (l : list Ξ±) : l.bind (Ξ» x, [x]) = l := bind_pure l
theorem map_eq_bind {Ξ± Ξ²} (f : Ξ± β Ξ²) (l : list Ξ±) : map f l = l.bind (Ξ» x, [f x]) :=
by { transitivity, rw [β bind_singleton' l, bind_map], refl }
theorem bind_assoc {Ξ± Ξ²} (l : list Ξ±) (f : Ξ± β list Ξ²) (g : Ξ² β list Ξ³) :
(l.bind f).bind g = l.bind (Ξ» x, (f x).bind g) :=
by induction l; simp *
/-! ### concat -/
theorem concat_nil (a : Ξ±) : concat [] a = [a] := rfl
theorem concat_cons (a b : Ξ±) (l : list Ξ±) : concat (a :: l) b = a :: concat l b := rfl
@[simp] theorem concat_eq_append (a : Ξ±) (l : list Ξ±) : concat l a = l ++ [a] :=
by induction l; simp only [*, concat]; split; refl
theorem init_eq_of_concat_eq {a : Ξ±} {lβ lβ : list Ξ±} : concat lβ a = concat lβ a β lβ = lβ :=
begin
intro h,
rw [concat_eq_append, concat_eq_append] at h,
exact append_right_cancel h
end
theorem last_eq_of_concat_eq {a b : Ξ±} {l : list Ξ±} : concat l a = concat l b β a = b :=
begin
intro h,
rw [concat_eq_append, concat_eq_append] at h,
exact head_eq_of_cons_eq (append_left_cancel h)
end
theorem concat_ne_nil (a : Ξ±) (l : list Ξ±) : concat l a β [] :=
by simp
theorem concat_append (a : Ξ±) (lβ lβ : list Ξ±) : concat lβ a ++ lβ = lβ ++ a :: lβ :=
by simp
theorem length_concat (a : Ξ±) (l : list Ξ±) : length (concat l a) = succ (length l) :=
by simp only [concat_eq_append, length_append, length]
theorem append_concat (a : Ξ±) (lβ lβ : list Ξ±) : lβ ++ concat lβ a = concat (lβ ++ lβ) a :=
by simp
/-! ### reverse -/
@[simp] theorem reverse_nil : reverse (@nil Ξ±) = [] := rfl
local attribute [simp] reverse_core
@[simp] theorem reverse_cons (a : Ξ±) (l : list Ξ±) : reverse (a::l) = reverse l ++ [a] :=
have aux : β lβ lβ, reverse_core lβ lβ ++ [a] = reverse_core lβ (lβ ++ [a]),
by intro lβ; induction lβ; intros; [refl, simp only [*, reverse_core, cons_append]],
(aux l nil).symm
theorem reverse_core_eq (lβ lβ : list Ξ±) : reverse_core lβ lβ = reverse lβ ++ lβ :=
by induction lβ generalizing lβ; [refl, simp only [*, reverse_core, reverse_cons, append_assoc]];
refl
theorem reverse_cons' (a : Ξ±) (l : list Ξ±) : reverse (a::l) = concat (reverse l) a :=
by simp only [reverse_cons, concat_eq_append]
@[simp] theorem reverse_singleton (a : Ξ±) : reverse [a] = [a] := rfl
@[simp] theorem reverse_append (s t : list Ξ±) : reverse (s ++ t) = (reverse t) ++ (reverse s) :=
by induction s; [rw [nil_append, reverse_nil, append_nil],
simp only [*, cons_append, reverse_cons, append_assoc]]
theorem reverse_concat (l : list Ξ±) (a : Ξ±) : reverse (concat l a) = a :: reverse l :=
by rw [concat_eq_append, reverse_append, reverse_singleton, singleton_append]
@[simp] theorem reverse_reverse (l : list Ξ±) : reverse (reverse l) = l :=
by induction l; [refl, simp only [*, reverse_cons, reverse_append]]; refl
@[simp] theorem reverse_involutive : involutive (@reverse Ξ±) := reverse_reverse
@[simp] theorem reverse_injective : injective (@reverse Ξ±) := reverse_involutive.injective
theorem reverse_surjective : surjective (@reverse Ξ±) := reverse_involutive.surjective
theorem reverse_bijective : bijective (@reverse Ξ±) := reverse_involutive.bijective
@[simp] theorem reverse_inj {lβ lβ : list Ξ±} : reverse lβ = reverse lβ β lβ = lβ :=
reverse_injective.eq_iff
lemma reverse_eq_iff {l l' : list Ξ±} :
l.reverse = l' β l = l'.reverse :=
reverse_involutive.eq_iff
@[simp] theorem reverse_eq_nil {l : list Ξ±} : reverse l = [] β l = [] :=
@reverse_inj _ l []
theorem concat_eq_reverse_cons (a : Ξ±) (l : list Ξ±) : concat l a = reverse (a :: reverse l) :=
by simp only [concat_eq_append, reverse_cons, reverse_reverse]
@[simp] theorem length_reverse (l : list Ξ±) : length (reverse l) = length l :=
by induction l; [refl, simp only [*, reverse_cons, length_append, length]]
@[simp] theorem map_reverse (f : Ξ± β Ξ²) (l : list Ξ±) : map f (reverse l) = reverse (map f l) :=
by induction l; [refl, simp only [*, map, reverse_cons, map_append]]
theorem map_reverse_core (f : Ξ± β Ξ²) (lβ lβ : list Ξ±) :
map f (reverse_core lβ lβ) = reverse_core (map f lβ) (map f lβ) :=
by simp only [reverse_core_eq, map_append, map_reverse]
@[simp] theorem mem_reverse {a : Ξ±} {l : list Ξ±} : a β reverse l β a β l :=
by induction l; [refl, simp only [*, reverse_cons, mem_append, mem_singleton, mem_cons_iff,
not_mem_nil, false_or, or_false, or_comm]]
@[simp] theorem reverse_repeat (a : Ξ±) (n) : reverse (repeat a n) = repeat a n :=
eq_repeat.2 β¨by simp only [length_reverse, length_repeat],
Ξ» b h, eq_of_mem_repeat (mem_reverse.1 h)β©
/-! ### empty -/
attribute [simp] list.empty
lemma empty_iff_eq_nil {l : list Ξ±} : l.empty β l = [] :=
list.cases_on l (by simp) (by simp)
/-! ### init -/
@[simp] theorem length_init : β (l : list Ξ±), length (init l) = length l - 1
| [] := rfl
| [a] := rfl
| (a :: b :: l) :=
begin
rw init,
simp only [add_left_inj, length, succ_add_sub_one],
exact length_init (b :: l)
end
/-! ### last -/
@[simp] theorem last_cons {a : Ξ±} {l : list Ξ±} :
β (h : l β nil), last (a :: l) (cons_ne_nil a l) = last l h :=
by {induction l; intros, contradiction, reflexivity}
@[simp] theorem last_append_singleton {a : Ξ±} (l : list Ξ±) :
last (l ++ [a]) (append_ne_nil_of_ne_nil_right l _ (cons_ne_nil a _)) = a :=
by induction l;
[refl, simp only [cons_append, last_cons (Ξ» H, cons_ne_nil _ _ (append_eq_nil.1 H).2), *]]
theorem last_append (lβ lβ : list Ξ±) (h : lβ β []) :
last (lβ ++ lβ) (append_ne_nil_of_ne_nil_right lβ lβ h) = last lβ h :=
begin
induction lβ with _ _ ih,
{ simp },
{ simp only [cons_append], rw list.last_cons, exact ih },
end
theorem last_concat {a : Ξ±} (l : list Ξ±) : last (concat l a) (concat_ne_nil a l) = a :=
by simp only [concat_eq_append, last_append_singleton]
@[simp] theorem last_singleton (a : Ξ±) : last [a] (cons_ne_nil a []) = a := rfl
@[simp] theorem last_cons_cons (aβ aβ : Ξ±) (l : list Ξ±) :
last (aβ::aβ::l) (cons_ne_nil _ _) = last (aβ::l) (cons_ne_nil aβ l) := rfl
theorem init_append_last : β {l : list Ξ±} (h : l β []), init l ++ [last l h] = l
| [] h := absurd rfl h
| [a] h := rfl
| (a::b::l) h :=
begin
rw [init, cons_append, last_cons (cons_ne_nil _ _)],
congr,
exact init_append_last (cons_ne_nil b l)
end
theorem last_congr {lβ lβ : list Ξ±} (hβ : lβ β []) (hβ : lβ β []) (hβ : lβ = lβ) :
last lβ hβ = last lβ hβ :=
by subst lβ
theorem last_mem : β {l : list Ξ±} (h : l β []), last l h β l
| [] h := absurd rfl h
| [a] h := or.inl rfl
| (a::b::l) h := or.inr $ by { rw [last_cons_cons], exact last_mem (cons_ne_nil b l) }
lemma last_repeat_succ (a m : β) :
(repeat a m.succ).last (ne_nil_of_length_eq_succ
(show (repeat a m.succ).length = m.succ, by rw length_repeat)) = a :=
begin
induction m with k IH,
{ simp },
{ simpa only [repeat_succ, last] }
end
/-! ### last' -/
@[simp] theorem last'_is_none :
β {l : list Ξ±}, (last' l).is_none β l = []
| [] := by simp
| [a] := by simp
| (a::b::l) := by simp [@last'_is_none (b::l)]
@[simp] theorem last'_is_some : β {l : list Ξ±}, l.last'.is_some β l β []
| [] := by simp
| [a] := by simp
| (a::b::l) := by simp [@last'_is_some (b::l)]
theorem mem_last'_eq_last : β {l : list Ξ±} {x : Ξ±}, x β l.last' β β h, x = last l h
| [] x hx := false.elim $ by simpa using hx
| [a] x hx := have a = x, by simpa using hx, this βΈ β¨cons_ne_nil a [], rflβ©
| (a::b::l) x hx :=
begin
rw last' at hx,
rcases mem_last'_eq_last hx with β¨hβ, hββ©,
use cons_ne_nil _ _,
rwa [last_cons]
end
theorem last'_eq_last_of_ne_nil : β {l : list Ξ±} (h : l β []), l.last' = some (l.last h)
| [] h := (h rfl).elim
| [a] _ := by {unfold last, unfold last'}
| (a::b::l) _ := @last'_eq_last_of_ne_nil (b::l) (cons_ne_nil _ _)
theorem mem_last'_cons {x y : Ξ±} : β {l : list Ξ±} (h : x β l.last'), x β (y :: l).last'
| [] _ := by contradiction
| (a::l) h := h
theorem mem_of_mem_last' {l : list Ξ±} {a : Ξ±} (ha : a β l.last') : a β l :=
let β¨hβ, hββ© := mem_last'_eq_last ha in hβ.symm βΈ last_mem _
theorem init_append_last' : β {l : list Ξ±} (a β l.last'), init l ++ [a] = l
| [] a ha := (option.not_mem_none a ha).elim
| [a] _ rfl := rfl
| (a :: b :: l) c hc := by { rw [last'] at hc, rw [init, cons_append, init_append_last' _ hc] }
theorem ilast_eq_last' [inhabited Ξ±] : β l : list Ξ±, l.ilast = l.last'.iget
| [] := by simp [ilast, arbitrary]
| [a] := rfl
| [a, b] := rfl
| [a, b, c] := rfl
| (a :: b :: c :: l) := by simp [ilast, ilast_eq_last' (c :: l)]
@[simp] theorem last'_append_cons : β (lβ : list Ξ±) (a : Ξ±) (lβ : list Ξ±),
last' (lβ ++ a :: lβ) = last' (a :: lβ)
| [] a lβ := rfl
| [b] a lβ := rfl
| (b::c::lβ) a lβ := by rw [cons_append, cons_append, last', β cons_append, last'_append_cons]
@[simp] theorem last'_cons_cons (x y : Ξ±) (l : list Ξ±) :
last' (x :: y :: l) = last' (y :: l) := rfl
theorem last'_append_of_ne_nil (lβ : list Ξ±) : β {lβ : list Ξ±} (hlβ : lβ β []),
last' (lβ ++ lβ) = last' lβ
| [] hlβ := by contradiction
| (b::lβ) _ := last'_append_cons lβ b lβ
theorem last'_append {lβ lβ : list Ξ±} {x : Ξ±} (h : x β lβ.last') :
x β (lβ ++ lβ).last' :=
by { cases lβ, { contradiction, }, { rw list.last'_append_cons, exact h } }
/-! ### head(') and tail -/
theorem head_eq_head' [inhabited Ξ±] (l : list Ξ±) : head l = (head' l).iget :=
by cases l; refl
theorem surjective_head [inhabited Ξ±] : surjective (@head Ξ± _) := Ξ» x, β¨[x], rflβ©
theorem surjective_head' : surjective (@head' Ξ±) := option.forall.2 β¨β¨[], rflβ©, Ξ» x, β¨[x], rflβ©β©
theorem surjective_tail : surjective (@tail Ξ±)
| [] := β¨[], rflβ©
| (a :: l) := β¨a :: a :: l, rflβ©
lemma eq_cons_of_mem_head' {x : Ξ±} : β {l : list Ξ±}, x β l.head' β l = x::tail l
| [] h := (option.not_mem_none _ h).elim
| (a::l) h := by { simp only [head', option.mem_def] at h, exact h βΈ rfl }
theorem mem_of_mem_head' {x : Ξ±} {l : list Ξ±} (h : x β l.head') : x β l :=
(eq_cons_of_mem_head' h).symm βΈ mem_cons_self _ _
@[simp] theorem head_cons [inhabited Ξ±] (a : Ξ±) (l : list Ξ±) : head (a::l) = a := rfl
@[simp] theorem tail_nil : tail (@nil Ξ±) = [] := rfl
@[simp] theorem tail_cons (a : Ξ±) (l : list Ξ±) : tail (a::l) = l := rfl
@[simp] theorem head_append [inhabited Ξ±] (t : list Ξ±) {s : list Ξ±} (h : s β []) :
head (s ++ t) = head s :=
by {induction s, contradiction, refl}
theorem head'_append {s t : list Ξ±} {x : Ξ±} (h : x β s.head') :
x β (s ++ t).head' :=
by { cases s, contradiction, exact h }
theorem head'_append_of_ne_nil : β (lβ : list Ξ±) {lβ : list Ξ±} (hlβ : lβ β []),
head' (lβ ++ lβ) = head' lβ
| [] _ hlβ := by contradiction
| (x::lβ) _ _ := rfl
theorem tail_append_singleton_of_ne_nil {a : Ξ±} {l : list Ξ±} (h : l β nil) :
tail (l ++ [a]) = tail l ++ [a] :=
by { induction l, contradiction, rw [tail,cons_append,tail], }
theorem cons_head'_tail : β {l : list Ξ±} {a : Ξ±} (h : a β head' l), a :: tail l = l
| [] a h := by contradiction
| (b::l) a h := by { simp at h, simp [h] }
theorem head_mem_head' [inhabited Ξ±] : β {l : list Ξ±} (h : l β []), head l β head' l
| [] h := by contradiction
| (a::l) h := rfl
theorem cons_head_tail [inhabited Ξ±] {l : list Ξ±} (h : l β []) : (head l)::(tail l) = l :=
cons_head'_tail (head_mem_head' h)
lemma head_mem_self [inhabited Ξ±] {l : list Ξ±} (h : l β nil) : l.head β l :=
begin
have h' := mem_cons_self l.head l.tail,
rwa cons_head_tail h at h',
end
@[simp] theorem head'_map (f : Ξ± β Ξ²) (l) : head' (map f l) = (head' l).map f := by cases l; refl
lemma tail_append_of_ne_nil (l l' : list Ξ±) (h : l β []) :
(l ++ l').tail = l.tail ++ l' :=
begin
cases l,
{ contradiction },
{ simp }
end
@[simp]
lemma nth_le_tail (l : list Ξ±) (i) (h : i < l.tail.length)
(h' : i + 1 < l.length := by simpa [βlt_tsub_iff_right] using h) :
l.tail.nth_le i h = l.nth_le (i + 1) h' :=
begin
cases l,
{ cases h, },
{ simpa }
end
lemma nth_le_cons_aux {l : list Ξ±} {a : Ξ±} {n} (hn : n β 0) (h : n < (a :: l).length) :
n - 1 < l.length :=
begin
contrapose! h,
rw length_cons,
convert succ_le_succ h,
exact (nat.succ_pred_eq_of_pos hn.bot_lt).symm
end
lemma nth_le_cons {l : list Ξ±} {a : Ξ±} {n} (hl) :
(a :: l).nth_le n hl = if hn : n = 0 then a else l.nth_le (n - 1) (nth_le_cons_aux hn hl) :=
begin
split_ifs,
{ simp [nth_le, h] },
cases l,
{ rw [length_singleton, lt_succ_iff, nonpos_iff_eq_zero] at hl, contradiction },
cases n,
{ contradiction },
refl
end
@[simp] lemma modify_head_modify_head (l : list Ξ±) (f g : Ξ± β Ξ±) :
(l.modify_head f).modify_head g = l.modify_head (g β f) :=
by cases l; simp
/-! ### Induction from the right -/
/-- Induction principle from the right for lists: if a property holds for the empty list, and
for `l ++ [a]` if it holds for `l`, then it holds for all lists. The principle is given for
a `Sort`-valued predicate, i.e., it can also be used to construct data. -/
@[elab_as_eliminator] def reverse_rec_on {C : list Ξ± β Sort*}
(l : list Ξ±) (H0 : C [])
(H1 : β (l : list Ξ±) (a : Ξ±), C l β C (l ++ [a])) : C l :=
begin
rw β reverse_reverse l,
induction reverse l,
{ exact H0 },
{ rw reverse_cons, exact H1 _ _ ih }
end
/-- Bidirectional induction principle for lists: if a property holds for the empty list, the
singleton list, and `a :: (l ++ [b])` from `l`, then it holds for all lists. This can be used to
prove statements about palindromes. The principle is given for a `Sort`-valued predicate, i.e., it
can also be used to construct data. -/
def bidirectional_rec {C : list Ξ± β Sort*}
(H0 : C []) (H1 : β (a : Ξ±), C [a])
(Hn : β (a : Ξ±) (l : list Ξ±) (b : Ξ±), C l β C (a :: (l ++ [b]))) : β l, C l
| [] := H0
| [a] := H1 a
| (a :: b :: l) :=
let l' := init (b :: l), b' := last (b :: l) (cons_ne_nil _ _) in
have length l' < length (a :: b :: l), by { change _ < length l + 2, simp },
begin
rw βinit_append_last (cons_ne_nil b l),
have : C l', from bidirectional_rec l',
exact Hn a l' b' βΉC l'βΊ
end
using_well_founded { rel_tac := Ξ» _ _, `[exact β¨_, measure_wf list.lengthβ©] }
/-- Like `bidirectional_rec`, but with the list parameter placed first. -/
@[elab_as_eliminator] def bidirectional_rec_on {C : list Ξ± β Sort*}
(l : list Ξ±) (H0 : C []) (H1 : β (a : Ξ±), C [a])
(Hn : β (a : Ξ±) (l : list Ξ±) (b : Ξ±), C l β C (a :: (l ++ [b]))) : C l :=
bidirectional_rec H0 H1 Hn l
/-! ### sublists -/
@[simp] theorem nil_sublist : Ξ (l : list Ξ±), [] <+ l
| [] := sublist.slnil
| (a :: l) := sublist.cons _ _ a (nil_sublist l)
@[refl, simp] theorem sublist.refl : Ξ (l : list Ξ±), l <+ l
| [] := sublist.slnil
| (a :: l) := sublist.cons2 _ _ a (sublist.refl l)
@[trans] theorem sublist.trans {lβ lβ lβ : list Ξ±} (hβ : lβ <+ lβ) (hβ : lβ <+ lβ) : lβ <+ lβ :=
sublist.rec_on hβ (Ξ»_ s, s)
(Ξ»lβ lβ a hβ IH lβ hβ, sublist.cons _ _ _ (IH lβ hβ))
(Ξ»lβ lβ a hβ IH lβ hβ, @sublist.cases_on _ (Ξ»lβ lβ', lβ' = a :: lβ β lβ <+ a :: lβ) _ _ hβ
(Ξ»_, nil_sublist _)
(Ξ»lβ lβ' a' hβ' e, match a', lβ', e, hβ' with ._, ._, rfl, hβ :=
sublist.cons _ _ _ (IH _ hβ) end)
(Ξ»lβ lβ' a' hβ' e, match a', lβ', e, hβ' with ._, ._, rfl, hβ :=
sublist.cons2 _ _ _ (IH _ hβ) end) rfl)
lβ hβ
@[simp] theorem sublist_cons (a : Ξ±) (l : list Ξ±) : l <+ a::l :=
sublist.cons _ _ _ (sublist.refl l)
theorem sublist_of_cons_sublist {a : Ξ±} {lβ lβ : list Ξ±} : a::lβ <+ lβ β lβ <+ lβ :=
sublist.trans (sublist_cons a lβ)
theorem sublist.cons_cons {lβ lβ : list Ξ±} (a : Ξ±) (s : lβ <+ lβ) : a::lβ <+ a::lβ :=
sublist.cons2 _ _ _ s
@[simp] theorem sublist_append_left : Ξ (lβ lβ : list Ξ±), lβ <+ lβ++lβ
| [] lβ := nil_sublist _
| (a::lβ) lβ := (sublist_append_left lβ lβ).cons_cons _
@[simp] theorem sublist_append_right : Ξ (lβ lβ : list Ξ±), lβ <+ lβ++lβ
| [] lβ := sublist.refl _
| (a::lβ) lβ := sublist.cons _ _ _ (sublist_append_right lβ lβ)
theorem sublist_cons_of_sublist (a : Ξ±) {lβ lβ : list Ξ±} : lβ <+ lβ β lβ <+ a::lβ :=
sublist.cons _ _ _
theorem sublist_append_of_sublist_left {l lβ lβ : list Ξ±} (s : l <+ lβ) : l <+ lβ++lβ :=
s.trans $ sublist_append_left _ _
theorem sublist_append_of_sublist_right {l lβ lβ : list Ξ±} (s : l <+ lβ) : l <+ lβ++lβ :=
s.trans $ sublist_append_right _ _
theorem sublist_of_cons_sublist_cons {lβ lβ : list Ξ±} : β {a : Ξ±}, a::lβ <+ a::lβ β lβ <+ lβ
| ._ (sublist.cons ._ ._ a s) := sublist_of_cons_sublist s
| ._ (sublist.cons2 ._ ._ a s) := s
theorem cons_sublist_cons_iff {lβ lβ : list Ξ±} {a : Ξ±} : a::lβ <+ a::lβ β lβ <+ lβ :=
β¨sublist_of_cons_sublist_cons, sublist.cons_cons _β©
@[simp] theorem append_sublist_append_left {lβ lβ : list Ξ±} : β l, l++lβ <+ l++lβ β lβ <+ lβ
| [] := iff.rfl
| (a::l) := cons_sublist_cons_iff.trans (append_sublist_append_left l)
theorem sublist.append_right {lβ lβ : list Ξ±} (h : lβ <+ lβ) (l) : lβ++l <+ lβ++l :=
begin
induction h with _ _ a _ ih _ _ a _ ih,
{ refl },
{ apply sublist_cons_of_sublist a ih },
{ apply ih.cons_cons a }
end
theorem sublist_or_mem_of_sublist {l lβ lβ : list Ξ±} {a : Ξ±} (h : l <+ lβ ++ a::lβ) :
l <+ lβ ++ lβ β¨ a β l :=
begin
induction lβ with b lβ IH generalizing l,
{ cases h, { left, exact βΉl <+ lββΊ }, { right, apply mem_cons_self } },
{ cases h with _ _ _ h _ _ _ h,
{ exact or.imp_left (sublist_cons_of_sublist _) (IH h) },
{ exact (IH h).imp (sublist.cons_cons _) (mem_cons_of_mem _) } }
end
theorem sublist.reverse {lβ lβ : list Ξ±} (h : lβ <+ lβ) : lβ.reverse <+ lβ.reverse :=
begin
induction h with _ _ _ _ ih _ _ a _ ih, {refl},
{ rw reverse_cons, exact sublist_append_of_sublist_left ih },
{ rw [reverse_cons, reverse_cons], exact ih.append_right [a] }
end
@[simp] theorem reverse_sublist_iff {lβ lβ : list Ξ±} : lβ.reverse <+ lβ.reverse β lβ <+ lβ :=
β¨Ξ» h, lβ.reverse_reverse βΈ lβ.reverse_reverse βΈ h.reverse, sublist.reverseβ©
@[simp] theorem append_sublist_append_right {lβ lβ : list Ξ±} (l) : lβ++l <+ lβ++l β lβ <+ lβ :=
β¨Ξ» h, by simpa only [reverse_append, append_sublist_append_left, reverse_sublist_iff]
using h.reverse,
Ξ» h, h.append_right lβ©
theorem sublist.append {lβ lβ rβ rβ : list Ξ±}
(hl : lβ <+ lβ) (hr : rβ <+ rβ) : lβ ++ rβ <+ lβ ++ rβ :=
(hl.append_right _).trans ((append_sublist_append_left _).2 hr)
theorem sublist.subset : Ξ {lβ lβ : list Ξ±}, lβ <+ lβ β lβ β lβ
| ._ ._ sublist.slnil b h := h
| ._ ._ (sublist.cons lβ lβ a s) b h := mem_cons_of_mem _ (sublist.subset s h)
| ._ ._ (sublist.cons2 lβ lβ a s) b h :=
match eq_or_mem_of_mem_cons h with
| or.inl h := h βΈ mem_cons_self _ _
| or.inr h := mem_cons_of_mem _ (sublist.subset s h)
end
@[simp] theorem singleton_sublist {a : Ξ±} {l} : [a] <+ l β a β l :=
β¨Ξ» h, h.subset (mem_singleton_self _), Ξ» h,
let β¨s, t, eβ© := mem_split h in e.symm βΈ
((nil_sublist _).cons_cons _ ).trans (sublist_append_right _ _)β©
theorem eq_nil_of_sublist_nil {l : list Ξ±} (s : l <+ []) : l = [] :=
eq_nil_of_subset_nil $ s.subset
@[simp] theorem sublist_nil_iff_eq_nil {l : list Ξ±} : l <+ [] β l = [] :=
β¨eq_nil_of_sublist_nil, Ξ» H, H βΈ sublist.refl _β©
@[simp] theorem repeat_sublist_repeat (a : Ξ±) {m n} : repeat a m <+ repeat a n β m β€ n :=
β¨Ξ» h, by simpa only [length_repeat] using h.length_le,
Ξ» h, by induction h; [refl, simp only [*, repeat_succ, sublist.cons]] β©
lemma sublist_repeat_iff {l : list Ξ±} {a : Ξ±} {n : β} :
l <+ repeat a n β β (k β€ n), l = repeat a k :=
β¨Ξ» h, β¨l.length, h.length_le.trans (length_repeat _ _).le, eq_repeat.mpr
β¨rfl, Ξ» b hb, list.eq_of_mem_repeat (h.subset hb)β©β©,
by { rintro β¨k, h, rflβ©, exact (repeat_sublist_repeat _).mpr h }β©
theorem sublist.eq_of_length : β {lβ lβ : list Ξ±}, lβ <+ lβ β length lβ = length lβ β lβ = lβ
| ._ ._ sublist.slnil h := rfl
| ._ ._ (sublist.cons lβ lβ a s) h := by cases s.length_le.not_lt (by rw h; apply lt_succ_self)
| ._ ._ (sublist.cons2 lβ lβ a s) h :=
by rw [length, length] at h; injection h with h; rw s.eq_of_length h
theorem sublist.eq_of_length_le (s : lβ <+ lβ) (h : length lβ β€ length lβ) : lβ = lβ :=
s.eq_of_length $ s.length_le.antisymm h
lemma sublist.antisymm (sβ : lβ <+ lβ) (sβ : lβ <+ lβ) : lβ = lβ := sβ.eq_of_length_le sβ.length_le
instance decidable_sublist [decidable_eq Ξ±] : β (lβ lβ : list Ξ±), decidable (lβ <+ lβ)
| [] lβ := is_true $ nil_sublist _
| (a::lβ) [] := is_false $ Ξ»h, list.no_confusion $ eq_nil_of_sublist_nil h
| (a::lβ) (b::lβ) :=
if h : a = b then
decidable_of_decidable_of_iff (decidable_sublist lβ lβ) $
by rw [β h]; exact β¨sublist.cons_cons _, sublist_of_cons_sublist_consβ©
else decidable_of_decidable_of_iff (decidable_sublist (a::lβ) lβ)
β¨sublist_cons_of_sublist _, Ξ»s, match a, lβ, s, h with
| a, lβ, sublist.cons ._ ._ ._ s', h := s'
| ._, ._, sublist.cons2 t ._ ._ s', h := absurd rfl h
endβ©
/-! ### index_of -/
section index_of
variable [decidable_eq Ξ±]
@[simp] theorem index_of_nil (a : Ξ±) : index_of a [] = 0 := rfl
theorem index_of_cons (a b : Ξ±) (l : list Ξ±) :
index_of a (b::l) = if a = b then 0 else succ (index_of a l) := rfl
theorem index_of_cons_eq {a b : Ξ±} (l : list Ξ±) : a = b β index_of a (b::l) = 0 :=
assume e, if_pos e
@[simp] theorem index_of_cons_self (a : Ξ±) (l : list Ξ±) : index_of a (a::l) = 0 :=
index_of_cons_eq _ rfl
@[simp, priority 990]
theorem index_of_cons_ne {a b : Ξ±} (l : list Ξ±) : a β b β index_of a (b::l) = succ (index_of a l) :=
assume n, if_neg n
theorem index_of_eq_length {a : Ξ±} {l : list Ξ±} : index_of a l = length l β a β l :=
begin
induction l with b l ih,
{ exact iff_of_true rfl (not_mem_nil _) },
simp only [length, mem_cons_iff, index_of_cons], split_ifs,
{ exact iff_of_false (by rintro β¨β©) (Ξ» H, H $ or.inl h) },
{ simp only [h, false_or], rw β ih, exact succ_inj' }
end
@[simp, priority 980]
theorem index_of_of_not_mem {l : list Ξ±} {a : Ξ±} : a β l β index_of a l = length l :=
index_of_eq_length.2
theorem index_of_le_length {a : Ξ±} {l : list Ξ±} : index_of a l β€ length l :=
begin
induction l with b l ih, {refl},
simp only [length, index_of_cons],
by_cases h : a = b, {rw if_pos h, exact nat.zero_le _},
rw if_neg h, exact succ_le_succ ih
end
theorem index_of_lt_length {a} {l : list Ξ±} : index_of a l < length l β a β l :=
β¨Ξ»h, decidable.by_contradiction $ Ξ» al, ne_of_lt h $ index_of_eq_length.2 al,
Ξ»al, lt_of_le_of_ne index_of_le_length $ Ξ» h, index_of_eq_length.1 h alβ©
theorem index_of_append_of_mem {a : Ξ±} (h : a β lβ) :
index_of a (lβ ++ lβ) = index_of a lβ :=
begin
induction lβ with dβ tβ ih,
{ exfalso, exact not_mem_nil a h },
rw list.cons_append,
by_cases hh : a = dβ,
{ iterate 2 { rw index_of_cons_eq _ hh } },
rw [index_of_cons_ne _ hh, index_of_cons_ne _ hh, ih (mem_of_ne_of_mem hh h)],
end
theorem index_of_append_of_not_mem {a : Ξ±} (h : a β lβ) :
index_of a (lβ ++ lβ) = lβ.length + index_of a lβ :=
begin
induction lβ with dβ tβ ih,
{ rw [list.nil_append, list.length, zero_add] },
rw [list.cons_append, index_of_cons_ne _ (ne_of_not_mem_cons h),
list.length, ih (not_mem_of_not_mem_cons h), nat.succ_add],
end
end index_of
/-! ### nth element -/
theorem nth_le_of_mem : β {a} {l : list Ξ±}, a β l β β n h, nth_le l n h = a
| a (_ :: l) (or.inl rfl) := β¨0, succ_pos _, rflβ©
| a (b :: l) (or.inr m) :=
let β¨n, h, eβ© := nth_le_of_mem m in β¨n+1, succ_lt_succ h, eβ©
theorem nth_le_nth : β {l : list Ξ±} {n} h, nth l n = some (nth_le l n h)
| (a :: l) 0 h := rfl
| (a :: l) (n+1) h := @nth_le_nth l n _
theorem nth_len_le : β {l : list Ξ±} {n}, length l β€ n β nth l n = none
| [] n h := rfl
| (a :: l) (n+1) h := nth_len_le (le_of_succ_le_succ h)
@[simp] theorem nth_length (l : list Ξ±) : l.nth l.length = none := nth_len_le le_rfl
theorem nth_eq_some {l : list Ξ±} {n a} : nth l n = some a β β h, nth_le l n h = a :=
β¨Ξ» e,
have h : n < length l, from lt_of_not_ge $ Ξ» hn,
by rw nth_len_le hn at e; contradiction,
β¨h, by rw nth_le_nth h at e;
injection e with e; apply nth_le_memβ©,
Ξ» β¨h, eβ©, e βΈ nth_le_nth _β©
@[simp]
theorem nth_eq_none_iff : β {l : list Ξ±} {n}, nth l n = none β length l β€ n :=
begin
intros, split,
{ intro h, by_contradiction h',
have hβ : β h, l.nth_le n h = l.nth_le n (lt_of_not_ge h') := β¨lt_of_not_ge h', rflβ©,
rw [β nth_eq_some, h] at hβ, cases hβ },
{ solve_by_elim [nth_len_le] },
end
theorem nth_of_mem {a} {l : list Ξ±} (h : a β l) : β n, nth l n = some a :=
let β¨n, h, eβ© := nth_le_of_mem h in β¨n, by rw [nth_le_nth, e]β©
theorem nth_le_mem : β (l : list Ξ±) n h, nth_le l n h β l
| (a :: l) 0 h := mem_cons_self _ _
| (a :: l) (n+1) h := mem_cons_of_mem _ (nth_le_mem l _ _)
theorem nth_mem {l : list Ξ±} {n a} (e : nth l n = some a) : a β l :=
let β¨h, eβ© := nth_eq_some.1 e in e βΈ nth_le_mem _ _ _
theorem mem_iff_nth_le {a} {l : list Ξ±} : a β l β β n h, nth_le l n h = a :=
β¨nth_le_of_mem, Ξ» β¨n, h, eβ©, e βΈ nth_le_mem _ _ _β©
theorem mem_iff_nth {a} {l : list Ξ±} : a β l β β n, nth l n = some a :=
mem_iff_nth_le.trans $ exists_congr $ Ξ» n, nth_eq_some.symm
lemma nth_zero (l : list Ξ±) : l.nth 0 = l.head' := by cases l; refl
lemma nth_injective {Ξ± : Type u} {xs : list Ξ±} {i j : β}
(hβ : i < xs.length)
(hβ : nodup xs)
(hβ : xs.nth i = xs.nth j) : i = j :=
begin
induction xs with x xs generalizing i j,
{ cases hβ },
{ cases i; cases j,
case nat.zero nat.zero
{ refl },
case nat.succ nat.succ
{ congr, cases hβ,
apply xs_ih;
solve_by_elim [lt_of_succ_lt_succ] },
iterate 2
{ dsimp at hβ,
cases hβ with _ _ h h',
cases h x _ rfl,
rw mem_iff_nth,
exact β¨_, hβ.symmβ© <|>
exact β¨_, hββ© } },
end
@[simp] theorem nth_map (f : Ξ± β Ξ²) : β l n, nth (map f l) n = (nth l n).map f
| [] n := rfl
| (a :: l) 0 := rfl
| (a :: l) (n+1) := nth_map l n
theorem nth_le_map (f : Ξ± β Ξ²) {l n} (H1 H2) : nth_le (map f l) n H1 = f (nth_le l n H2) :=
option.some.inj $ by rw [β nth_le_nth, nth_map, nth_le_nth]; refl
/-- A version of `nth_le_map` that can be used for rewriting. -/
theorem nth_le_map_rev (f : Ξ± β Ξ²) {l n} (H) :
f (nth_le l n H) = nth_le (map f l) n ((length_map f l).symm βΈ H) :=
(nth_le_map f _ _).symm
@[simp] theorem nth_le_map' (f : Ξ± β Ξ²) {l n} (H) :
nth_le (map f l) n H = f (nth_le l n (length_map f l βΈ H)) :=
nth_le_map f _ _
/-- If one has `nth_le L i hi` in a formula and `h : L = L'`, one can not `rw h` in the formula as
`hi` gives `i < L.length` and not `i < L'.length`. The lemma `nth_le_of_eq` can be used to make
such a rewrite, with `rw (nth_le_of_eq h)`. -/
lemma nth_le_of_eq {L L' : list Ξ±} (h : L = L') {i : β} (hi : i < L.length) :
nth_le L i hi = nth_le L' i (h βΈ hi) :=
by { congr, exact h}
@[simp] lemma nth_le_singleton (a : Ξ±) {n : β} (hn : n < 1) :
nth_le [a] n hn = a :=
have hn0 : n = 0 := nat.eq_zero_of_le_zero (le_of_lt_succ hn),
by subst hn0; refl
lemma nth_le_zero [inhabited Ξ±] {L : list Ξ±} (h : 0 < L.length) :
L.nth_le 0 h = L.head :=
by { cases L, cases h, simp, }
lemma nth_le_append : β {lβ lβ : list Ξ±} {n : β} (hnβ) (hnβ),
(lβ ++ lβ).nth_le n hnβ = lβ.nth_le n hnβ
| [] _ n hnβ hnβ := (nat.not_lt_zero _ hnβ).elim
| (a::l) _ 0 hnβ hnβ := rfl
| (a::l) _ (n+1) hnβ hnβ := by simp only [nth_le, cons_append];
exact nth_le_append _ _
lemma nth_le_append_right_aux {lβ lβ : list Ξ±} {n : β}
(hβ : lβ.length β€ n) (hβ : n < (lβ ++ lβ).length) : n - lβ.length < lβ.length :=
begin
rw list.length_append at hβ,
apply lt_of_add_lt_add_right,
rwa [nat.sub_add_cancel hβ, nat.add_comm],
end
lemma nth_le_append_right : β {lβ lβ : list Ξ±} {n : β} (hβ : lβ.length β€ n) (hβ),
(lβ ++ lβ).nth_le n hβ = lβ.nth_le (n - lβ.length) (nth_le_append_right_aux hβ hβ)
| [] _ n hβ hβ := rfl
| (a :: l) _ (n+1) hβ hβ :=
begin
dsimp,
conv { to_rhs, congr, skip, rw [nat.add_sub_add_right], },
rw nth_le_append_right (nat.lt_succ_iff.mp hβ),
end
@[simp] lemma nth_le_repeat (a : Ξ±) {n m : β} (h : m < (list.repeat a n).length) :
(list.repeat a n).nth_le m h = a :=
eq_of_mem_repeat (nth_le_mem _ _ _)
lemma nth_append {lβ lβ : list Ξ±} {n : β} (hn : n < lβ.length) :
(lβ ++ lβ).nth n = lβ.nth n :=
have hn' : n < (lβ ++ lβ).length := lt_of_lt_of_le hn
(by rw length_append; exact nat.le_add_right _ _),
by rw [nth_le_nth hn, nth_le_nth hn', nth_le_append]
lemma nth_append_right {lβ lβ : list Ξ±} {n : β} (hn : lβ.length β€ n) :
(lβ ++ lβ).nth n = lβ.nth (n - lβ.length) :=
begin
by_cases hl : n < (lβ ++ lβ).length,
{ rw [nth_le_nth hl, nth_le_nth, nth_le_append_right hn] },
{ rw [nth_len_le (le_of_not_lt hl), nth_len_le],
rw [not_lt, length_append] at hl,
exact le_tsub_of_add_le_left hl }
end
lemma last_eq_nth_le : β (l : list Ξ±) (h : l β []),
last l h = l.nth_le (l.length - 1) (nat.sub_lt (length_pos_of_ne_nil h) one_pos)
| [] h := rfl
| [a] h := by rw [last_singleton, nth_le_singleton]
| (a :: b :: l) h := by { rw [last_cons, last_eq_nth_le (b :: l)],
refl, exact cons_ne_nil b l }
lemma nth_le_length_sub_one {l : list Ξ±} (h : l.length - 1 < l.length) :
l.nth_le (l.length - 1) h = l.last (by { rintro rfl, exact nat.lt_irrefl 0 h }) :=
(last_eq_nth_le l _).symm
@[simp] lemma nth_concat_length : β (l : list Ξ±) (a : Ξ±), (l ++ [a]).nth l.length = some a
| [] a := rfl
| (b::l) a := by rw [cons_append, length_cons, nth, nth_concat_length]
lemma nth_le_cons_length (x : Ξ±) (xs : list Ξ±) (n : β) (h : n = xs.length) :
(x :: xs).nth_le n (by simp [h]) = (x :: xs).last (cons_ne_nil x xs) :=
begin
rw last_eq_nth_le,
congr,
simp [h]
end
lemma take_one_drop_eq_of_lt_length {l : list Ξ±} {n : β} (h : n < l.length) :
(l.drop n).take 1 = [l.nth_le n h] :=
begin
induction l with x l ih generalizing n,
{ cases h },
{ by_cases hβ : l = [],
{ subst hβ, rw nth_le_singleton, simp [lt_succ_iff] at h, subst h, simp },
have hβ := h, rw [length_cons, nat.lt_succ_iff, le_iff_eq_or_lt] at hβ,
cases n, { simp }, rw [drop, nth_le], apply ih },
end
@[ext]
theorem ext : β {lβ lβ : list Ξ±}, (βn, nth lβ n = nth lβ n) β lβ = lβ
| [] [] h := rfl
| (a::lβ) [] h := by have h0 := h 0; contradiction
| [] (a'::lβ) h := by have h0 := h 0; contradiction
| (a::lβ) (a'::lβ) h := by have h0 : some a = some a' := h 0; injection h0 with aa;
simp only [aa, ext (Ξ»n, h (n+1))]; split; refl
theorem ext_le {lβ lβ : list Ξ±} (hl : length lβ = length lβ)
(h : βn hβ hβ, nth_le lβ n hβ = nth_le lβ n hβ) : lβ = lβ :=
ext $ Ξ»n, if hβ : n < length lβ
then by rw [nth_le_nth, nth_le_nth, h n hβ (by rwa [β hl])]
else let hβ := le_of_not_gt hβ in by { rw [nth_len_le hβ, nth_len_le], rwa [βhl], }
@[simp] theorem index_of_nth_le [decidable_eq Ξ±] {a : Ξ±} :
β {l : list Ξ±} h, nth_le l (index_of a l) h = a
| (b::l) h := by by_cases h' : a = b;
simp only [h', if_pos, if_false, index_of_cons, nth_le, @index_of_nth_le l]
@[simp] theorem index_of_nth [decidable_eq Ξ±] {a : Ξ±} {l : list Ξ±} (h : a β l) :
nth l (index_of a l) = some a :=
by rw [nth_le_nth, index_of_nth_le (index_of_lt_length.2 h)]
theorem nth_le_reverse_aux1 :
β (l r : list Ξ±) (i h1 h2), nth_le (reverse_core l r) (i + length l) h1 = nth_le r i h2
| [] r i := Ξ»h1 h2, rfl
| (a :: l) r i :=
by rw (show i + length (a :: l) = i + 1 + length l, from add_right_comm i (length l) 1);
exact Ξ»h1 h2, nth_le_reverse_aux1 l (a :: r) (i+1) h1 (succ_lt_succ h2)
lemma index_of_inj [decidable_eq Ξ±] {l : list Ξ±} {x y : Ξ±}
(hx : x β l) (hy : y β l) : index_of x l = index_of y l β x = y :=
β¨Ξ» h, have nth_le l (index_of x l) (index_of_lt_length.2 hx) =
nth_le l (index_of y l) (index_of_lt_length.2 hy),
by simp only [h],
by simpa only [index_of_nth_le],
Ξ» h, by subst hβ©
theorem nth_le_reverse_aux2 : β (l r : list Ξ±) (i : nat) (h1) (h2),
nth_le (reverse_core l r) (length l - 1 - i) h1 = nth_le l i h2
| [] r i h1 h2 := absurd h2 (nat.not_lt_zero _)
| (a :: l) r 0 h1 h2 := begin
have aux := nth_le_reverse_aux1 l (a :: r) 0,
rw zero_add at aux,
exact aux _ (zero_lt_succ _)
end
| (a :: l) r (i+1) h1 h2 := begin
have aux := nth_le_reverse_aux2 l (a :: r) i,
have heq := calc length (a :: l) - 1 - (i + 1)
= length l - (1 + i) : by rw add_comm; refl
... = length l - 1 - i : by rw β tsub_add_eq_tsub_tsub,
rw [β heq] at aux,
apply aux
end
@[simp] theorem nth_le_reverse (l : list Ξ±) (i : nat) (h1 h2) :
nth_le (reverse l) (length l - 1 - i) h1 = nth_le l i h2 :=
nth_le_reverse_aux2 _ _ _ _ _
lemma nth_le_reverse' (l : list Ξ±) (n : β) (hn : n < l.reverse.length) (hn') :
l.reverse.nth_le n hn = l.nth_le (l.length - 1 - n) hn' :=
begin
rw eq_comm,
convert nth_le_reverse l.reverse _ _ _ using 1,
{ simp },
{ simpa }
end
lemma eq_cons_of_length_one {l : list Ξ±} (h : l.length = 1) :
l = [l.nth_le 0 (h.symm βΈ zero_lt_one)] :=
begin
refine ext_le (by convert h) (Ξ» n hβ hβ, _),
simp only [nth_le_singleton],
congr,
exact eq_bot_iff.mpr (nat.lt_succ_iff.mp hβ)
end
lemma nth_le_eq_iff {l : list Ξ±} {n : β} {x : Ξ±} {h} : l.nth_le n h = x β l.nth n = some x :=
by { rw nth_eq_some, tauto }
lemma some_nth_le_eq {l : list Ξ±} {n : β} {h} : some (l.nth_le n h) = l.nth n :=
by { symmetry, rw nth_eq_some, tauto }
lemma modify_nth_tail_modify_nth_tail {f g : list Ξ± β list Ξ±} (m : β) :
βn (l:list Ξ±), (l.modify_nth_tail f n).modify_nth_tail g (m + n) =
l.modify_nth_tail (Ξ»l, (f l).modify_nth_tail g m) n
| 0 l := rfl
| (n+1) [] := rfl
| (n+1) (a::l) := congr_arg (list.cons a) (modify_nth_tail_modify_nth_tail n l)
lemma modify_nth_tail_modify_nth_tail_le
{f g : list Ξ± β list Ξ±} (m n : β) (l : list Ξ±) (h : n β€ m) :
(l.modify_nth_tail f n).modify_nth_tail g m =
l.modify_nth_tail (Ξ»l, (f l).modify_nth_tail g (m - n)) n :=
begin
rcases exists_add_of_le h with β¨m, rflβ©,
rw [add_tsub_cancel_left, add_comm, modify_nth_tail_modify_nth_tail]
end
lemma modify_nth_tail_modify_nth_tail_same {f g : list Ξ± β list Ξ±} (n : β) (l:list Ξ±) :
(l.modify_nth_tail f n).modify_nth_tail g n = l.modify_nth_tail (g β f) n :=
by rw [modify_nth_tail_modify_nth_tail_le n n l (le_refl n), tsub_self]; refl
lemma modify_nth_tail_id :
βn (l:list Ξ±), l.modify_nth_tail id n = l
| 0 l := rfl
| (n+1) [] := rfl
| (n+1) (a::l) := congr_arg (list.cons a) (modify_nth_tail_id n l)
theorem remove_nth_eq_nth_tail : β n (l : list Ξ±), remove_nth l n = modify_nth_tail tail n l
| 0 l := by cases l; refl
| (n+1) [] := rfl
| (n+1) (a::l) := congr_arg (cons _) (remove_nth_eq_nth_tail _ _)
theorem update_nth_eq_modify_nth (a : Ξ±) : β n (l : list Ξ±),
update_nth l n a = modify_nth (Ξ» _, a) n l
| 0 l := by cases l; refl
| (n+1) [] := rfl
| (n+1) (b::l) := congr_arg (cons _) (update_nth_eq_modify_nth _ _)
theorem modify_nth_eq_update_nth (f : Ξ± β Ξ±) : β n (l : list Ξ±),
modify_nth f n l = ((Ξ» a, update_nth l n (f a)) <$> nth l n).get_or_else l
| 0 l := by cases l; refl
| (n+1) [] := rfl
| (n+1) (b::l) := (congr_arg (cons b)
(modify_nth_eq_update_nth n l)).trans $ by cases nth l n; refl
theorem nth_modify_nth (f : Ξ± β Ξ±) : β n (l : list Ξ±) m,
nth (modify_nth f n l) m = (Ξ» a, if n = m then f a else a) <$> nth l m
| n l 0 := by cases l; cases n; refl
| n [] (m+1) := by cases n; refl
| 0 (a::l) (m+1) := by cases nth l m; refl
| (n+1) (a::l) (m+1) := (nth_modify_nth n l m).trans $
by cases nth l m with b; by_cases n = m;
simp only [h, if_pos, if_true, if_false, option.map_none, option.map_some, mt succ.inj,
not_false_iff]
theorem modify_nth_tail_length (f : list Ξ± β list Ξ±) (H : β l, length (f l) = length l) :
β n l, length (modify_nth_tail f n l) = length l
| 0 l := H _
| (n+1) [] := rfl
| (n+1) (a::l) := @congr_arg _ _ _ _ (+1) (modify_nth_tail_length _ _)
@[simp] theorem modify_nth_length (f : Ξ± β Ξ±) :
β n l, length (modify_nth f n l) = length l :=
modify_nth_tail_length _ (Ξ» l, by cases l; refl)
@[simp] theorem update_nth_length (l : list Ξ±) (n) (a : Ξ±) :
length (update_nth l n a) = length l :=
by simp only [update_nth_eq_modify_nth, modify_nth_length]
@[simp] theorem nth_modify_nth_eq (f : Ξ± β Ξ±) (n) (l : list Ξ±) :
nth (modify_nth f n l) n = f <$> nth l n :=
by simp only [nth_modify_nth, if_pos]
@[simp] theorem nth_modify_nth_ne (f : Ξ± β Ξ±) {m n} (l : list Ξ±) (h : m β n) :
nth (modify_nth f m l) n = nth l n :=
by simp only [nth_modify_nth, if_neg h, id_map']
theorem nth_update_nth_eq (a : Ξ±) (n) (l : list Ξ±) :
nth (update_nth l n a) n = (Ξ» _, a) <$> nth l n :=
by simp only [update_nth_eq_modify_nth, nth_modify_nth_eq]
theorem nth_update_nth_of_lt (a : Ξ±) {n} {l : list Ξ±} (h : n < length l) :
nth (update_nth l n a) n = some a :=
by rw [nth_update_nth_eq, nth_le_nth h]; refl
theorem nth_update_nth_ne (a : Ξ±) {m n} (l : list Ξ±) (h : m β n) :
nth (update_nth l m a) n = nth l n :=
by simp only [update_nth_eq_modify_nth, nth_modify_nth_ne _ _ h]
@[simp] lemma update_nth_nil (n : β) (a : Ξ±) : [].update_nth n a = [] := rfl
@[simp] lemma update_nth_succ (x : Ξ±) (xs : list Ξ±) (n : β) (a : Ξ±) :
(x :: xs).update_nth n.succ a = x :: xs.update_nth n a := rfl
lemma update_nth_comm (a b : Ξ±) : Ξ {n m : β} (l : list Ξ±) (h : n β m),
(l.update_nth n a).update_nth m b = (l.update_nth m b).update_nth n a
| _ _ [] _ := by simp
| 0 0 (x :: t) h := absurd rfl h
| (n + 1) 0 (x :: t) h := by simp [list.update_nth]
| 0 (m + 1) (x :: t) h := by simp [list.update_nth]
| (n + 1) (m + 1) (x :: t) h := by { simp only [update_nth, true_and, eq_self_iff_true],
exact update_nth_comm t (Ξ» h', h $ nat.succ_inj'.mpr h'), }
@[simp] lemma nth_le_update_nth_eq (l : list Ξ±) (i : β) (a : Ξ±)
(h : i < (l.update_nth i a).length) : (l.update_nth i a).nth_le i h = a :=
by rw [β option.some_inj, β nth_le_nth, nth_update_nth_eq, nth_le_nth]; simp * at *
@[simp] lemma nth_le_update_nth_of_ne {l : list Ξ±} {i j : β} (h : i β j) (a : Ξ±)
(hj : j < (l.update_nth i a).length) :
(l.update_nth i a).nth_le j hj = l.nth_le j (by simpa using hj) :=
by rw [β option.some_inj, β list.nth_le_nth, list.nth_update_nth_ne _ _ h, list.nth_le_nth]
lemma mem_or_eq_of_mem_update_nth : β {l : list Ξ±} {n : β} {a b : Ξ±}
(h : a β l.update_nth n b), a β l β¨ a = b
| [] n a b h := false.elim h
| (c::l) 0 a b h := ((mem_cons_iff _ _ _).1 h).elim
or.inr (or.inl β mem_cons_of_mem _)
| (c::l) (n+1) a b h := ((mem_cons_iff _ _ _).1 h).elim
(Ξ» h, h βΈ or.inl (mem_cons_self _ _))
(Ξ» h, (mem_or_eq_of_mem_update_nth h).elim
(or.inl β mem_cons_of_mem _) or.inr)
section insert_nth
variable {a : Ξ±}
@[simp] lemma insert_nth_zero (s : list Ξ±) (x : Ξ±) : insert_nth 0 x s = x :: s := rfl
@[simp] lemma insert_nth_succ_nil (n : β) (a : Ξ±) : insert_nth (n + 1) a [] = [] := rfl
@[simp] lemma insert_nth_succ_cons (s : list Ξ±) (hd x : Ξ±) (n : β) :
insert_nth (n + 1) x (hd :: s) = hd :: (insert_nth n x s) := rfl
lemma length_insert_nth : βn as, n β€ length as β length (insert_nth n a as) = length as + 1
| 0 as h := rfl
| (n+1) [] h := (nat.not_succ_le_zero _ h).elim
| (n+1) (a'::as) h := congr_arg nat.succ $ length_insert_nth n as (nat.le_of_succ_le_succ h)
lemma remove_nth_insert_nth (n:β) (l : list Ξ±) : (l.insert_nth n a).remove_nth n = l :=
by rw [remove_nth_eq_nth_tail, insert_nth, modify_nth_tail_modify_nth_tail_same];
from modify_nth_tail_id _ _
lemma insert_nth_remove_nth_of_ge : βn m as, n < length as β n β€ m β
insert_nth m a (as.remove_nth n) = (as.insert_nth (m + 1) a).remove_nth n
| 0 0 [] has _ := (lt_irrefl _ has).elim
| 0 0 (a::as) has hmn := by simp [remove_nth, insert_nth]
| 0 (m+1) (a::as) has hmn := rfl
| (n+1) (m+1) (a::as) has hmn :=
congr_arg (cons a) $
insert_nth_remove_nth_of_ge n m as (nat.lt_of_succ_lt_succ has) (nat.le_of_succ_le_succ hmn)
lemma insert_nth_remove_nth_of_le : βn m as, n < length as β m β€ n β
insert_nth m a (as.remove_nth n) = (as.insert_nth m a).remove_nth (n + 1)
| n 0 (a :: as) has hmn := rfl
| (n + 1) (m + 1) (a :: as) has hmn :=
congr_arg (cons a) $
insert_nth_remove_nth_of_le n m as (nat.lt_of_succ_lt_succ has) (nat.le_of_succ_le_succ hmn)
lemma insert_nth_comm (a b : Ξ±) :
β(i j : β) (l : list Ξ±) (h : i β€ j) (hj : j β€ length l),
(l.insert_nth i a).insert_nth (j + 1) b = (l.insert_nth j b).insert_nth i a
| 0 j l := by simp [insert_nth]
| (i + 1) 0 l := assume h, (nat.not_lt_zero _ h).elim
| (i + 1) (j+1) [] := by simp
| (i + 1) (j+1) (c::l) :=
assume hβ hβ,
by simp [insert_nth];
exact insert_nth_comm i j l (nat.le_of_succ_le_succ hβ) (nat.le_of_succ_le_succ hβ)
lemma mem_insert_nth {a b : Ξ±} : β {n : β} {l : list Ξ±} (hi : n β€ l.length),
a β l.insert_nth n b β a = b β¨ a β l
| 0 as h := iff.rfl
| (n+1) [] h := (nat.not_succ_le_zero _ h).elim
| (n+1) (a'::as) h := begin
dsimp [list.insert_nth],
erw [mem_insert_nth (nat.le_of_succ_le_succ h), β or.assoc, or_comm (a = a'), or.assoc]
end
lemma insert_nth_of_length_lt (l : list Ξ±) (x : Ξ±) (n : β) (h : l.length < n) :
insert_nth n x l = l :=
begin
induction l with hd tl IH generalizing n,
{ cases n,
{ simpa using h },
{ simp } },
{ cases n,
{ simpa using h },
{ simp only [nat.succ_lt_succ_iff, length] at h,
simpa using IH _ h } }
end
@[simp] lemma insert_nth_length_self (l : list Ξ±) (x : Ξ±) :
insert_nth l.length x l = l ++ [x] :=
begin
induction l with hd tl IH,
{ simp },
{ simpa using IH }
end
lemma length_le_length_insert_nth (l : list Ξ±) (x : Ξ±) (n : β) :
l.length β€ (insert_nth n x l).length :=
begin
cases le_or_lt n l.length with hn hn,
{ rw length_insert_nth _ _ hn,
exact (nat.lt_succ_self _).le },
{ rw insert_nth_of_length_lt _ _ _ hn }
end
lemma length_insert_nth_le_succ (l : list Ξ±) (x : Ξ±) (n : β) :
(insert_nth n x l).length β€ l.length + 1 :=
begin
cases le_or_lt n l.length with hn hn,
{ rw length_insert_nth _ _ hn },
{ rw insert_nth_of_length_lt _ _ _ hn,
exact (nat.lt_succ_self _).le }
end
lemma nth_le_insert_nth_of_lt (l : list Ξ±) (x : Ξ±) (n k : β) (hn : k < n)
(hk : k < l.length)
(hk' : k < (insert_nth n x l).length := hk.trans_le (length_le_length_insert_nth _ _ _)):
(insert_nth n x l).nth_le k hk' = l.nth_le k hk :=
begin
induction n with n IH generalizing k l,
{ simpa using hn },
{ cases l with hd tl,
{ simp },
{ cases k,
{ simp },
{ rw nat.succ_lt_succ_iff at hn,
simpa using IH _ _ hn _ } } }
end
@[simp] lemma nth_le_insert_nth_self (l : list Ξ±) (x : Ξ±) (n : β)
(hn : n β€ l.length) (hn' : n < (insert_nth n x l).length :=
by rwa [length_insert_nth _ _ hn, nat.lt_succ_iff]) :
(insert_nth n x l).nth_le n hn' = x :=
begin
induction l with hd tl IH generalizing n,
{ simp only [length, nonpos_iff_eq_zero] at hn,
simp [hn] },
{ cases n,
{ simp },
{ simp only [nat.succ_le_succ_iff, length] at hn,
simpa using IH _ hn } }
end
lemma nth_le_insert_nth_add_succ (l : list Ξ±) (x : Ξ±) (n k : β)
(hk' : n + k < l.length)
(hk : n + k + 1 < (insert_nth n x l).length :=
by rwa [length_insert_nth _ _ (le_self_add.trans hk'.le), nat.succ_lt_succ_iff]) :
(insert_nth n x l).nth_le (n + k + 1) hk = nth_le l (n + k) hk' :=
begin
induction l with hd tl IH generalizing n k,
{ simpa using hk' },
{ cases n,
{ simpa },
{ simpa [succ_add] using IH _ _ _ } }
end
lemma insert_nth_injective (n : β) (x : Ξ±) : function.injective (insert_nth n x) :=
begin
induction n with n IH,
{ have : insert_nth 0 x = cons x := funext (Ξ» _, rfl),
simp [this] },
{ rintros (_|β¨a, asβ©) (_|β¨b, bsβ©) h;
simpa [IH.eq_iff] using h <|> refl }
end
end insert_nth
/-! ### map -/
@[simp] lemma map_nil (f : Ξ± β Ξ²) : map f [] = [] := rfl
theorem map_eq_foldr (f : Ξ± β Ξ²) (l : list Ξ±) :
map f l = foldr (Ξ» a bs, f a :: bs) [] l :=
by induction l; simp *
lemma map_congr {f g : Ξ± β Ξ²} : β {l : list Ξ±}, (β x β l, f x = g x) β map f l = map g l
| [] _ := rfl
| (a::l) h := let β¨hβ, hββ© := forall_mem_cons.1 h in
by rw [map, map, hβ, map_congr hβ]
lemma map_eq_map_iff {f g : Ξ± β Ξ²} {l : list Ξ±} : map f l = map g l β (β x β l, f x = g x) :=
begin
refine β¨_, map_congrβ©, intros h x hx,
rw [mem_iff_nth_le] at hx, rcases hx with β¨n, hn, rflβ©,
rw [nth_le_map_rev f, nth_le_map_rev g], congr, exact h
end
theorem map_concat (f : Ξ± β Ξ²) (a : Ξ±) (l : list Ξ±) : map f (concat l a) = concat (map f l) (f a) :=
by induction l; [refl, simp only [*, concat_eq_append, cons_append, map, map_append]]; split; refl
@[simp] theorem map_id'' (l : list Ξ±) : map (Ξ» x, x) l = l :=
map_id _
theorem map_id' {f : Ξ± β Ξ±} (h : β x, f x = x) (l : list Ξ±) : map f l = l :=
by simp [show f = id, from funext h]
theorem eq_nil_of_map_eq_nil {f : Ξ± β Ξ²} {l : list Ξ±} (h : map f l = nil) : l = nil :=
eq_nil_of_length_eq_zero $ by rw [β length_map f l, h]; refl
@[simp] theorem map_join (f : Ξ± β Ξ²) (L : list (list Ξ±)) :
map f (join L) = join (map (map f) L) :=
by induction L; [refl, simp only [*, join, map, map_append]]
theorem bind_ret_eq_map (f : Ξ± β Ξ²) (l : list Ξ±) :
l.bind (list.ret β f) = map f l :=
by unfold list.bind; induction l; simp only [map, join, list.ret, cons_append, nil_append, *];
split; refl
lemma bind_congr {l : list Ξ±} {f g : Ξ± β list Ξ²} (h : β x β l, f x = g x) :
list.bind l f = list.bind l g :=
(congr_arg list.join $ map_congr h : _)
@[simp] theorem map_eq_map {Ξ± Ξ²} (f : Ξ± β Ξ²) (l : list Ξ±) : f <$> l = map f l := rfl
@[simp] theorem map_tail (f : Ξ± β Ξ²) (l) : map f (tail l) = tail (map f l) :=
by cases l; refl
@[simp] theorem map_injective_iff {f : Ξ± β Ξ²} : injective (map f) β injective f :=
begin
split; intros h x y hxy,
{ suffices : [x] = [y], { simpa using this }, apply h, simp [hxy] },
{ induction y generalizing x, simpa using hxy,
cases x, simpa using hxy, simp at hxy, simp [y_ih hxy.2, h hxy.1] }
end
/--
A single `list.map` of a composition of functions is equal to
composing a `list.map` with another `list.map`, fully applied.
This is the reverse direction of `list.map_map`.
-/
lemma comp_map (h : Ξ² β Ξ³) (g : Ξ± β Ξ²) (l : list Ξ±) :
map (h β g) l = map h (map g l) := (map_map _ _ _).symm
/--
Composing a `list.map` with another `list.map` is equal to
a single `list.map` of composed functions.
-/
@[simp] lemma map_comp_map (g : Ξ² β Ξ³) (f : Ξ± β Ξ²) :
map g β map f = map (g β f) :=
by { ext l, rw comp_map }
theorem map_filter_eq_foldr (f : Ξ± β Ξ²) (p : Ξ± β Prop) [decidable_pred p] (as : list Ξ±) :
map f (filter p as) = foldr (Ξ» a bs, if p a then f a :: bs else bs) [] as :=
by { induction as, { refl }, { simp! [*, apply_ite (map f)] } }
lemma last_map (f : Ξ± β Ξ²) {l : list Ξ±} (hl : l β []) :
(l.map f).last (mt eq_nil_of_map_eq_nil hl) = f (l.last hl) :=
begin
induction l with l_ih l_tl l_ih,
{ apply (hl rfl).elim },
{ cases l_tl,
{ simp },
{ simpa using l_ih } }
end
/-! ### mapβ -/
theorem nil_mapβ (f : Ξ± β Ξ² β Ξ³) (l : list Ξ²) : mapβ f [] l = [] :=
by cases l; refl
theorem mapβ_nil (f : Ξ± β Ξ² β Ξ³) (l : list Ξ±) : mapβ f l [] = [] :=
by cases l; refl
@[simp] theorem mapβ_flip (f : Ξ± β Ξ² β Ξ³) :
β as bs, mapβ (flip f) bs as = mapβ f as bs
| [] [] := rfl
| [] (b :: bs) := rfl
| (a :: as) [] := rfl
| (a :: as) (b :: bs) := by { simp! [mapβ_flip], refl }
/-! ### take, drop -/
@[simp] theorem take_zero (l : list Ξ±) : take 0 l = [] := rfl
@[simp] theorem take_nil : β n, take n [] = ([] : list Ξ±)
| 0 := rfl
| (n+1) := rfl
theorem take_cons (n) (a : Ξ±) (l : list Ξ±) : take (succ n) (a::l) = a :: take n l := rfl
@[simp] theorem take_length : β (l : list Ξ±), take (length l) l = l
| [] := rfl
| (a::l) := begin change a :: (take (length l) l) = a :: l, rw take_length end
theorem take_all_of_le : β {n} {l : list Ξ±}, length l β€ n β take n l = l
| 0 [] h := rfl
| 0 (a::l) h := absurd h (not_le_of_gt (zero_lt_succ _))
| (n+1) [] h := rfl
| (n+1) (a::l) h :=
begin
change a :: take n l = a :: l,
rw [take_all_of_le (le_of_succ_le_succ h)]
end
@[simp] theorem take_left : β lβ lβ : list Ξ±, take (length lβ) (lβ ++ lβ) = lβ
| [] lβ := rfl
| (a::lβ) lβ := congr_arg (cons a) (take_left lβ lβ)
theorem take_left' {lβ lβ : list Ξ±} {n} (h : length lβ = n) :
take n (lβ ++ lβ) = lβ :=
by rw β h; apply take_left
theorem take_take : β (n m) (l : list Ξ±), take n (take m l) = take (min n m) l
| n 0 l := by rw [min_zero, take_zero, take_nil]
| 0 m l := by rw [zero_min, take_zero, take_zero]
| (succ n) (succ m) nil := by simp only [take_nil]
| (succ n) (succ m) (a::l) := by simp only [take, min_succ_succ, take_take n m l]; split; refl
theorem take_repeat (a : Ξ±) : β (n m : β), take n (repeat a m) = repeat a (min n m)
| n 0 := by simp
| 0 m := by simp
| (succ n) (succ m) := by simp [min_succ_succ, take_repeat]
lemma map_take {Ξ± Ξ² : Type*} (f : Ξ± β Ξ²) :
β (L : list Ξ±) (i : β), (L.take i).map f = (L.map f).take i
| [] i := by simp
| L 0 := by simp
| (h :: t) (n+1) := by { dsimp, rw [map_take], }
/-- Taking the first `n` elements in `lβ ++ lβ` is the same as appending the first `n` elements
of `lβ` to the first `n - lβ.length` elements of `lβ`. -/
lemma take_append_eq_append_take {lβ lβ : list Ξ±} {n : β} :
take n (lβ ++ lβ) = take n lβ ++ take (n - lβ.length) lβ :=
begin
induction lβ generalizing n, { simp },
cases n, { simp }, simp *
end
lemma take_append_of_le_length {lβ lβ : list Ξ±} {n : β} (h : n β€ lβ.length) :
(lβ ++ lβ).take n = lβ.take n :=
by simp [take_append_eq_append_take, tsub_eq_zero_iff_le.mpr h]
/-- Taking the first `lβ.length + i` elements in `lβ ++ lβ` is the same as appending the first
`i` elements of `lβ` to `lβ`. -/
lemma take_append {lβ lβ : list Ξ±} (i : β) :
take (lβ.length + i) (lβ ++ lβ) = lβ ++ (take i lβ) :=
by simp [take_append_eq_append_take, take_all_of_le le_self_add]
/-- The `i`-th element of a list coincides with the `i`-th element of any of its prefixes of
length `> i`. Version designed to rewrite from the big list to the small list. -/
lemma nth_le_take (L : list Ξ±) {i j : β} (hi : i < L.length) (hj : i < j) :
nth_le L i hi = nth_le (L.take j) i (by { rw length_take, exact lt_min hj hi }) :=
by { rw nth_le_of_eq (take_append_drop j L).symm hi, exact nth_le_append _ _ }
/-- The `i`-th element of a list coincides with the `i`-th element of any of its prefixes of
length `> i`. Version designed to rewrite from the small list to the big list. -/
lemma nth_le_take' (L : list Ξ±) {i j : β} (hi : i < (L.take j).length) :
nth_le (L.take j) i hi = nth_le L i (lt_of_lt_of_le hi (by simp [le_refl])) :=
by { simp at hi, rw nth_le_take L _ hi.1 }
lemma nth_take {l : list Ξ±} {n m : β} (h : m < n) :
(l.take n).nth m = l.nth m :=
begin
induction n with n hn generalizing l m,
{ simp only [nat.nat_zero_eq_zero] at h,
exact absurd h (not_lt_of_le m.zero_le) },
{ cases l with hd tl,
{ simp only [take_nil] },
{ cases m,
{ simp only [nth, take] },
{ simpa only using hn (nat.lt_of_succ_lt_succ h) } } },
end
@[simp] lemma nth_take_of_succ {l : list Ξ±} {n : β} :
(l.take (n + 1)).nth n = l.nth n :=
nth_take (nat.lt_succ_self n)
lemma take_succ {l : list Ξ±} {n : β} :
l.take (n + 1) = l.take n ++ (l.nth n).to_list :=
begin
induction l with hd tl hl generalizing n,
{ simp only [option.to_list, nth, take_nil, append_nil]},
{ cases n,
{ simp only [option.to_list, nth, eq_self_iff_true, and_self, take, nil_append] },
{ simp only [hl, cons_append, nth, eq_self_iff_true, and_self, take] } }
end
@[simp] lemma take_eq_nil_iff {l : list Ξ±} {k : β} :
l.take k = [] β l = [] β¨ k = 0 :=
by { cases l; cases k; simp [nat.succ_ne_zero] }
lemma take_eq_take : β {l : list Ξ±} {m n : β},
l.take m = l.take n β min m l.length = min n l.length
| [] m n := by simp
| (x :: xs) 0 0 := by simp
| (x :: xs) (m + 1) 0 := by simp
| (x :: xs) 0 (n + 1) := by simp [@eq_comm β 0]
| (x :: xs) (m + 1) (n + 1) := by simp [nat.min_succ_succ, take_eq_take]
lemma take_add (l : list Ξ±) (m n : β) :
l.take (m + n) = l.take m ++ (l.drop m).take n :=
begin
convert_to
take (m + n) (take m l ++ drop m l) =
take m l ++ take n (drop m l),
{ rw take_append_drop },
rw [take_append_eq_append_take, take_all_of_le, append_right_inj], swap,
{ transitivity m,
{ apply length_take_le },
{ simp }},
simp only [take_eq_take, length_take, length_drop],
generalize : l.length = k, by_cases h : m β€ k,
{ simp [min_eq_left_iff.mpr h] },
{ push_neg at h, simp [nat.sub_eq_zero_of_le (le_of_lt h)] },
end
lemma init_eq_take (l : list Ξ±) : l.init = l.take l.length.pred :=
begin
cases l with x l,
{ simp [init] },
{ induction l with hd tl hl generalizing x,
{ simp [init], },
{ simp [init, hl] } }
end
lemma init_take {n : β} {l : list Ξ±} (h : n < l.length) :
(l.take n).init = l.take n.pred :=
by simp [init_eq_take, min_eq_left_of_lt h, take_take, pred_le]
@[simp] lemma init_cons_of_ne_nil {Ξ± : Type*} {x : Ξ±} :
β {l : list Ξ±} (h : l β []), (x :: l).init = x :: l.init
| [] h := false.elim (h rfl)
| (a :: l) _ := by simp [init]
@[simp] lemma init_append_of_ne_nil {Ξ± : Type*} {l : list Ξ±} :
β (l' : list Ξ±) (h : l β []), (l' ++ l).init = l' ++ l.init
| [] _ := by simp only [nil_append]
| (a :: l') h := by simp [append_ne_nil_of_ne_nil_right l' l h, init_append_of_ne_nil l' h]
@[simp] lemma drop_eq_nil_of_le {l : list Ξ±} {k : β} (h : l.length β€ k) :
l.drop k = [] :=
by simpa [βlength_eq_zero] using tsub_eq_zero_iff_le.mpr h
lemma drop_eq_nil_iff_le {l : list Ξ±} {k : β} :
l.drop k = [] β l.length β€ k :=
begin
refine β¨Ξ» h, _, drop_eq_nil_of_leβ©,
induction k with k hk generalizing l,
{ simp only [drop] at h,
simp [h] },
{ cases l,
{ simp },
{ simp only [drop] at h,
simpa [nat.succ_le_succ_iff] using hk h } }
end
lemma tail_drop (l : list Ξ±) (n : β) : (l.drop n).tail = l.drop (n + 1) :=
begin
induction l with hd tl hl generalizing n,
{ simp },
{ cases n,
{ simp },
{ simp [hl] } }
end
lemma cons_nth_le_drop_succ {l : list Ξ±} {n : β} (hn : n < l.length) :
l.nth_le n hn :: l.drop (n + 1) = l.drop n :=
begin
induction l with hd tl hl generalizing n,
{ exact absurd n.zero_le (not_le_of_lt (by simpa using hn)) },
{ cases n,
{ simp },
{ simp only [nat.succ_lt_succ_iff, list.length] at hn,
simpa [list.nth_le, list.drop] using hl hn } }
end
theorem drop_nil : β n, drop n [] = ([] : list Ξ±) :=
Ξ» _, drop_eq_nil_of_le (nat.zero_le _)
@[simp] theorem drop_one : β l : list Ξ±, drop 1 l = tail l
| [] := rfl
| (a :: l) := rfl
theorem drop_add : β m n (l : list Ξ±), drop (m + n) l = drop m (drop n l)
| m 0 l := rfl
| m (n+1) [] := (drop_nil _).symm
| m (n+1) (a::l) := drop_add m n _
@[simp] theorem drop_left : β lβ lβ : list Ξ±, drop (length lβ) (lβ ++ lβ) = lβ
| [] lβ := rfl
| (a::lβ) lβ := drop_left lβ lβ
theorem drop_left' {lβ lβ : list Ξ±} {n} (h : length lβ = n) :
drop n (lβ ++ lβ) = lβ :=
by rw β h; apply drop_left
theorem drop_eq_nth_le_cons : β {n} {l : list Ξ±} h,
drop n l = nth_le l n h :: drop (n+1) l
| 0 (a::l) h := rfl
| (n+1) (a::l) h := @drop_eq_nth_le_cons n _ _
@[simp] lemma drop_length (l : list Ξ±) : l.drop l.length = [] :=
calc l.drop l.length = (l ++ []).drop l.length : by simp
... = [] : drop_left _ _
lemma drop_length_cons {l : list Ξ±} (h : l β []) (a : Ξ±) :
(a :: l).drop l.length = [l.last h] :=
begin
induction l with y l ih generalizing a,
{ cases h rfl },
{ simp only [drop, length],
by_cases hβ : l = [], { simp [hβ] },
rw last_cons hβ, exact ih hβ y },
end
/-- Dropping the elements up to `n` in `lβ ++ lβ` is the same as dropping the elements up to `n`
in `lβ`, dropping the elements up to `n - lβ.length` in `lβ`, and appending them. -/
lemma drop_append_eq_append_drop {lβ lβ : list Ξ±} {n : β} :
drop n (lβ ++ lβ) = drop n lβ ++ drop (n - lβ.length) lβ :=
begin
induction lβ generalizing n, { simp },
cases n, { simp }, simp *
end
lemma drop_append_of_le_length {lβ lβ : list Ξ±} {n : β} (h : n β€ lβ.length) :
(lβ ++ lβ).drop n = lβ.drop n ++ lβ :=
by simp [drop_append_eq_append_drop, tsub_eq_zero_iff_le.mpr h]
/-- Dropping the elements up to `lβ.length + i` in `lβ + lβ` is the same as dropping the elements
up to `i` in `lβ`. -/
lemma drop_append {lβ lβ : list Ξ±} (i : β) :
drop (lβ.length + i) (lβ ++ lβ) = drop i lβ :=
by simp [drop_append_eq_append_drop, take_all_of_le le_self_add]
lemma drop_sizeof_le [has_sizeof Ξ±] (l : list Ξ±) : β (n : β), (l.drop n).sizeof β€ l.sizeof :=
begin
induction l with _ _ lih; intro n,
{ rw [drop_nil] },
{ induction n with n nih,
{ refl, },
{ exact trans (lih _) le_add_self } }
end
/-- The `i + j`-th element of a list coincides with the `j`-th element of the list obtained by
dropping the first `i` elements. Version designed to rewrite from the big list to the small list. -/
lemma nth_le_drop (L : list Ξ±) {i j : β} (h : i + j < L.length) :
nth_le L (i + j) h = nth_le (L.drop i) j
begin
have A : i < L.length := lt_of_le_of_lt (nat.le.intro rfl) h,
rw (take_append_drop i L).symm at h,
simpa only [le_of_lt A, min_eq_left, add_lt_add_iff_left, length_take, length_append] using h
end :=
begin
have A : length (take i L) = i, by simp [le_of_lt (lt_of_le_of_lt (nat.le.intro rfl) h)],
rw [nth_le_of_eq (take_append_drop i L).symm h, nth_le_append_right];
simp [A]
end
/-- The `i + j`-th element of a list coincides with the `j`-th element of the list obtained by
dropping the first `i` elements. Version designed to rewrite from the small list to the big list. -/
lemma nth_le_drop' (L : list Ξ±) {i j : β} (h : j < (L.drop i).length) :
nth_le (L.drop i) j h = nth_le L (i + j) (lt_tsub_iff_left.mp ((length_drop i L) βΈ h)) :=
by rw nth_le_drop
lemma nth_drop (L : list Ξ±) (i j : β) :
nth (L.drop i) j = nth L (i + j) :=
begin
ext,
simp only [nth_eq_some, nth_le_drop', option.mem_def],
split;
exact Ξ» β¨h, haβ©, β¨by simpa [lt_tsub_iff_left] using h, haβ©
end
@[simp] theorem drop_drop (n : β) : β (m) (l : list Ξ±), drop n (drop m l) = drop (n + m) l
| m [] := by simp
| 0 l := by simp
| (m+1) (a::l) :=
calc drop n (drop (m + 1) (a :: l)) = drop n (drop m l) : rfl
... = drop (n + m) l : drop_drop m l
... = drop (n + (m + 1)) (a :: l) : rfl
theorem drop_take : β (m : β) (n : β) (l : list Ξ±),
drop m (take (m + n) l) = take n (drop m l)
| 0 n _ := by simp
| (m+1) n nil := by simp
| (m+1) n (_::l) :=
have h: m + 1 + n = (m+n) + 1, by ac_refl,
by simpa [take_cons, h] using drop_take m n l
lemma map_drop {Ξ± Ξ² : Type*} (f : Ξ± β Ξ²) :
β (L : list Ξ±) (i : β), (L.drop i).map f = (L.map f).drop i
| [] i := by simp
| L 0 := by simp
| (h :: t) (n+1) := by { dsimp, rw [map_drop], }
theorem modify_nth_tail_eq_take_drop (f : list Ξ± β list Ξ±) (H : f [] = []) :
β n l, modify_nth_tail f n l = take n l ++ f (drop n l)
| 0 l := rfl
| (n+1) [] := H.symm
| (n+1) (b::l) := congr_arg (cons b) (modify_nth_tail_eq_take_drop n l)
theorem modify_nth_eq_take_drop (f : Ξ± β Ξ±) :
β n l, modify_nth f n l = take n l ++ modify_head f (drop n l) :=
modify_nth_tail_eq_take_drop _ rfl
theorem modify_nth_eq_take_cons_drop (f : Ξ± β Ξ±) {n l} (h) :
modify_nth f n l = take n l ++ f (nth_le l n h) :: drop (n+1) l :=
by rw [modify_nth_eq_take_drop, drop_eq_nth_le_cons h]; refl
theorem update_nth_eq_take_cons_drop (a : Ξ±) {n l} (h : n < length l) :
update_nth l n a = take n l ++ a :: drop (n+1) l :=
by rw [update_nth_eq_modify_nth, modify_nth_eq_take_cons_drop _ h]
lemma reverse_take {Ξ±} {xs : list Ξ±} (n : β)
(h : n β€ xs.length) :
xs.reverse.take n = (xs.drop (xs.length - n)).reverse :=
begin
induction xs generalizing n;
simp only [reverse_cons, drop, reverse_nil, zero_tsub, length, take_nil],
cases h.lt_or_eq_dec with h' h',
{ replace h' := le_of_succ_le_succ h',
rwa [take_append_of_le_length, xs_ih _ h'],
rw [show xs_tl.length + 1 - n = succ (xs_tl.length - n), from _, drop],
{ rwa [succ_eq_add_one, β tsub_add_eq_add_tsub] },
{ rwa length_reverse } },
{ subst h', rw [length, tsub_self, drop],
suffices : xs_tl.length + 1 = (xs_tl.reverse ++ [xs_hd]).length,
by rw [this, take_length, reverse_cons],
rw [length_append, length_reverse], refl }
end
@[simp] lemma update_nth_eq_nil (l : list Ξ±) (n : β) (a : Ξ±) : l.update_nth n a = [] β l = [] :=
by cases l; cases n; simp only [update_nth]
section take'
variable [inhabited Ξ±]
@[simp] theorem take'_length : β n l, length (@take' Ξ± _ n l) = n
| 0 l := rfl
| (n+1) l := congr_arg succ (take'_length _ _)
@[simp] theorem take'_nil : β n, take' n (@nil Ξ±) = repeat default n
| 0 := rfl
| (n+1) := congr_arg (cons _) (take'_nil _)
theorem take'_eq_take : β {n} {l : list Ξ±},
n β€ length l β take' n l = take n l
| 0 l h := rfl
| (n+1) (a::l) h := congr_arg (cons _) $
take'_eq_take $ le_of_succ_le_succ h
@[simp] theorem take'_left (lβ lβ : list Ξ±) : take' (length lβ) (lβ ++ lβ) = lβ :=
(take'_eq_take (by simp only [length_append, nat.le_add_right])).trans (take_left _ _)
theorem take'_left' {lβ lβ : list Ξ±} {n} (h : length lβ = n) :
take' n (lβ ++ lβ) = lβ :=
by rw β h; apply take'_left
end take'
/-! ### foldl, foldr -/
lemma foldl_ext (f g : Ξ± β Ξ² β Ξ±) (a : Ξ±)
{l : list Ξ²} (H : β a : Ξ±, β b β l, f a b = g a b) :
foldl f a l = foldl g a l :=
begin
induction l with hd tl ih generalizing a, {refl},
unfold foldl,
rw [ih (Ξ» a b bin, H a b $ mem_cons_of_mem _ bin), H a hd (mem_cons_self _ _)]
end
lemma foldr_ext (f g : Ξ± β Ξ² β Ξ²) (b : Ξ²)
{l : list Ξ±} (H : β a β l, β b : Ξ², f a b = g a b) :
foldr f b l = foldr g b l :=
begin
induction l with hd tl ih, {refl},
simp only [mem_cons_iff, or_imp_distrib, forall_and_distrib, forall_eq] at H,
simp only [foldr, ih H.2, H.1]
end
@[simp] theorem foldl_nil (f : Ξ± β Ξ² β Ξ±) (a : Ξ±) : foldl f a [] = a := rfl
@[simp] theorem foldl_cons (f : Ξ± β Ξ² β Ξ±) (a : Ξ±) (b : Ξ²) (l : list Ξ²) :
foldl f a (b::l) = foldl f (f a b) l := rfl
@[simp] theorem foldr_nil (f : Ξ± β Ξ² β Ξ²) (b : Ξ²) : foldr f b [] = b := rfl
@[simp] theorem foldr_cons (f : Ξ± β Ξ² β Ξ²) (b : Ξ²) (a : Ξ±) (l : list Ξ±) :
foldr f b (a::l) = f a (foldr f b l) := rfl
@[simp] theorem foldl_append (f : Ξ± β Ξ² β Ξ±) :
β (a : Ξ±) (lβ lβ : list Ξ²), foldl f a (lβ++lβ) = foldl f (foldl f a lβ) lβ
| a [] lβ := rfl
| a (b::lβ) lβ := by simp only [cons_append, foldl_cons, foldl_append (f a b) lβ lβ]
@[simp] theorem foldr_append (f : Ξ± β Ξ² β Ξ²) :
β (b : Ξ²) (lβ lβ : list Ξ±), foldr f b (lβ++lβ) = foldr f (foldr f b lβ) lβ
| b [] lβ := rfl
| b (a::lβ) lβ := by simp only [cons_append, foldr_cons, foldr_append b lβ lβ]
theorem foldl_fixed' {f : Ξ± β Ξ² β Ξ±} {a : Ξ±} (hf : β b, f a b = a) :
Ξ l : list Ξ², foldl f a l = a
| [] := rfl
| (b::l) := by rw [foldl_cons, hf b, foldl_fixed' l]
theorem foldr_fixed' {f : Ξ± β Ξ² β Ξ²} {b : Ξ²} (hf : β a, f a b = b) :
Ξ l : list Ξ±, foldr f b l = b
| [] := rfl
| (a::l) := by rw [foldr_cons, foldr_fixed' l, hf a]
@[simp] theorem foldl_fixed {a : Ξ±} : Ξ l : list Ξ², foldl (Ξ» a b, a) a l = a :=
foldl_fixed' (Ξ» _, rfl)
@[simp] theorem foldr_fixed {b : Ξ²} : Ξ l : list Ξ±, foldr (Ξ» a b, b) b l = b :=
foldr_fixed' (Ξ» _, rfl)
@[simp] theorem foldl_join (f : Ξ± β Ξ² β Ξ±) :
β (a : Ξ±) (L : list (list Ξ²)), foldl f a (join L) = foldl (foldl f) a L
| a [] := rfl
| a (l::L) := by simp only [join, foldl_append, foldl_cons, foldl_join (foldl f a l) L]
@[simp] theorem foldr_join (f : Ξ± β Ξ² β Ξ²) :
β (b : Ξ²) (L : list (list Ξ±)), foldr f b (join L) = foldr (Ξ» l b, foldr f b l) b L
| a [] := rfl
| a (l::L) := by simp only [join, foldr_append, foldr_join a L, foldr_cons]
theorem foldl_reverse (f : Ξ± β Ξ² β Ξ±) (a : Ξ±) (l : list Ξ²) :
foldl f a (reverse l) = foldr (Ξ»x y, f y x) a l :=
by induction l; [refl, simp only [*, reverse_cons, foldl_append, foldl_cons, foldl_nil, foldr]]
theorem foldr_reverse (f : Ξ± β Ξ² β Ξ²) (a : Ξ²) (l : list Ξ±) :
foldr f a (reverse l) = foldl (Ξ»x y, f y x) a l :=
let t := foldl_reverse (Ξ»x y, f y x) a (reverse l) in
by rw reverse_reverse l at t; rwa t
@[simp] theorem foldr_eta : β (l : list Ξ±), foldr cons [] l = l
| [] := rfl
| (x::l) := by simp only [foldr_cons, foldr_eta l]; split; refl
@[simp] theorem reverse_foldl {l : list Ξ±} : reverse (foldl (Ξ» t h, h :: t) [] l) = l :=
by rw βfoldr_reverse; simp
@[simp] theorem foldl_map (g : Ξ² β Ξ³) (f : Ξ± β Ξ³ β Ξ±) (a : Ξ±) (l : list Ξ²) :
foldl f a (map g l) = foldl (Ξ»x y, f x (g y)) a l :=
by revert a; induction l; intros; [refl, simp only [*, map, foldl]]
@[simp] theorem foldr_map (g : Ξ² β Ξ³) (f : Ξ³ β Ξ± β Ξ±) (a : Ξ±) (l : list Ξ²) :
foldr f a (map g l) = foldr (f β g) a l :=
by revert a; induction l; intros; [refl, simp only [*, map, foldr]]
theorem foldl_map' {Ξ± Ξ²: Type u} (g : Ξ± β Ξ²) (f : Ξ± β Ξ± β Ξ±) (f' : Ξ² β Ξ² β Ξ²)
(a : Ξ±) (l : list Ξ±) (h : β x y, f' (g x) (g y) = g (f x y)) :
list.foldl f' (g a) (l.map g) = g (list.foldl f a l) :=
begin
induction l generalizing a,
{ simp }, { simp [l_ih, h] }
end
theorem foldr_map' {Ξ± Ξ²: Type u} (g : Ξ± β Ξ²) (f : Ξ± β Ξ± β Ξ±) (f' : Ξ² β Ξ² β Ξ²)
(a : Ξ±) (l : list Ξ±) (h : β x y, f' (g x) (g y) = g (f x y)) :
list.foldr f' (g a) (l.map g) = g (list.foldr f a l) :=
begin
induction l generalizing a,
{ simp }, { simp [l_ih, h] }
end
theorem foldl_hom (l : list Ξ³) (f : Ξ± β Ξ²) (op : Ξ± β Ξ³ β Ξ±) (op' : Ξ² β Ξ³ β Ξ²) (a : Ξ±)
(h : βa x, f (op a x) = op' (f a) x) : foldl op' (f a) l = f (foldl op a l) :=
eq.symm $ by { revert a, induction l; intros; [refl, simp only [*, foldl]] }
theorem foldr_hom (l : list Ξ³) (f : Ξ± β Ξ²) (op : Ξ³ β Ξ± β Ξ±) (op' : Ξ³ β Ξ² β Ξ²) (a : Ξ±)
(h : βx a, f (op x a) = op' x (f a)) : foldr op' (f a) l = f (foldr op a l) :=
by { revert a, induction l; intros; [refl, simp only [*, foldr]] }
lemma foldl_homβ (l : list ΞΉ) (f : Ξ± β Ξ² β Ξ³) (opβ : Ξ± β ΞΉ β Ξ±) (opβ : Ξ² β ΞΉ β Ξ²) (opβ : Ξ³ β ΞΉ β Ξ³)
(a : Ξ±) (b : Ξ²) (h : β a b i, f (opβ a i) (opβ b i) = opβ (f a b) i) :
foldl opβ (f a b) l = f (foldl opβ a l) (foldl opβ b l) :=
eq.symm $ by { revert a b, induction l; intros; [refl, simp only [*, foldl]] }
lemma foldr_homβ (l : list ΞΉ) (f : Ξ± β Ξ² β Ξ³) (opβ : ΞΉ β Ξ± β Ξ±) (opβ : ΞΉ β Ξ² β Ξ²) (opβ : ΞΉ β Ξ³ β Ξ³)
(a : Ξ±) (b : Ξ²) (h : β a b i, f (opβ i a) (opβ i b) = opβ i (f a b)) :
foldr opβ (f a b) l = f (foldr opβ a l) (foldr opβ b l) :=
by { revert a, induction l; intros; [refl, simp only [*, foldr]] }
lemma injective_foldl_comp {Ξ± : Type*} {l : list (Ξ± β Ξ±)} {f : Ξ± β Ξ±}
(hl : β f β l, function.injective f) (hf : function.injective f):
function.injective (@list.foldl (Ξ± β Ξ±) (Ξ± β Ξ±) function.comp f l) :=
begin
induction l generalizing f,
{ exact hf },
{ apply l_ih (Ξ» _ h, hl _ (list.mem_cons_of_mem _ h)),
apply function.injective.comp hf,
apply hl _ (list.mem_cons_self _ _) }
end
/-- Induction principle for values produced by a `foldr`: if a property holds
for the seed element `b : Ξ²` and for all incremental `op : Ξ± β Ξ² β Ξ²`
performed on the elements `(a : Ξ±) β l`. The principle is given for
a `Sort`-valued predicate, i.e., it can also be used to construct data. -/
def foldr_rec_on {C : Ξ² β Sort*} (l : list Ξ±) (op : Ξ± β Ξ² β Ξ²) (b : Ξ²) (hb : C b)
(hl : β (b : Ξ²) (hb : C b) (a : Ξ±) (ha : a β l), C (op a b)) :
C (foldr op b l) :=
begin
induction l with hd tl IH,
{ exact hb },
{ refine hl _ _ hd (mem_cons_self hd tl),
refine IH _,
intros y hy x hx,
exact hl y hy x (mem_cons_of_mem hd hx) }
end
/-- Induction principle for values produced by a `foldl`: if a property holds
for the seed element `b : Ξ²` and for all incremental `op : Ξ² β Ξ± β Ξ²`
performed on the elements `(a : Ξ±) β l`. The principle is given for
a `Sort`-valued predicate, i.e., it can also be used to construct data. -/
def foldl_rec_on {C : Ξ² β Sort*} (l : list Ξ±) (op : Ξ² β Ξ± β Ξ²) (b : Ξ²) (hb : C b)
(hl : β (b : Ξ²) (hb : C b) (a : Ξ±) (ha : a β l), C (op b a)) :
C (foldl op b l) :=
begin
induction l with hd tl IH generalizing b,
{ exact hb },
{ refine IH _ _ _,
{ intros y hy x hx,
exact hl y hy x (mem_cons_of_mem hd hx) },
{ exact hl b hb hd (mem_cons_self hd tl) } }
end
@[simp] lemma foldr_rec_on_nil {C : Ξ² β Sort*} (op : Ξ± β Ξ² β Ξ²) (b) (hb : C b) (hl) :
foldr_rec_on [] op b hb hl = hb := rfl
@[simp] lemma foldr_rec_on_cons {C : Ξ² β Sort*} (x : Ξ±) (l : list Ξ±)
(op : Ξ± β Ξ² β Ξ²) (b) (hb : C b)
(hl : β (b : Ξ²) (hb : C b) (a : Ξ±) (ha : a β (x :: l)), C (op a b)) :
foldr_rec_on (x :: l) op b hb hl = hl _ (foldr_rec_on l op b hb
(Ξ» b hb a ha, hl b hb a (mem_cons_of_mem _ ha))) x (mem_cons_self _ _) := rfl
@[simp] lemma foldl_rec_on_nil {C : Ξ² β Sort*} (op : Ξ² β Ξ± β Ξ²) (b) (hb : C b) (hl) :
foldl_rec_on [] op b hb hl = hb := rfl
/- scanl -/
section scanl
variables {f : Ξ² β Ξ± β Ξ²} {b : Ξ²} {a : Ξ±} {l : list Ξ±}
lemma length_scanl :
β a l, length (scanl f a l) = l.length + 1
| a [] := rfl
| a (x :: l) := by erw [length_cons, length_cons, length_scanl]
@[simp] lemma scanl_nil (b : Ξ²) : scanl f b nil = [b] := rfl
@[simp] lemma scanl_cons :
scanl f b (a :: l) = [b] ++ scanl f (f b a) l :=
by simp only [scanl, eq_self_iff_true, singleton_append, and_self]
@[simp] lemma nth_zero_scanl : (scanl f b l).nth 0 = some b :=
begin
cases l,
{ simp only [nth, scanl_nil] },
{ simp only [nth, scanl_cons, singleton_append] }
end
@[simp] lemma nth_le_zero_scanl {h : 0 < (scanl f b l).length} :
(scanl f b l).nth_le 0 h = b :=
begin
cases l,
{ simp only [nth_le, scanl_nil] },
{ simp only [nth_le, scanl_cons, singleton_append] }
end
lemma nth_succ_scanl {i : β} :
(scanl f b l).nth (i + 1) = ((scanl f b l).nth i).bind (Ξ» x, (l.nth i).map (Ξ» y, f x y)) :=
begin
induction l with hd tl hl generalizing b i,
{ symmetry,
simp only [option.bind_eq_none', nth, forall_2_true_iff, not_false_iff, option.map_none',
scanl_nil, option.not_mem_none, forall_true_iff] },
{ simp only [nth, scanl_cons, singleton_append],
cases i,
{ simp only [option.map_some', nth_zero_scanl, nth, option.some_bind'] },
{ simp only [hl, nth] } }
end
lemma nth_le_succ_scanl {i : β} {h : i + 1 < (scanl f b l).length} :
(scanl f b l).nth_le (i + 1) h =
f ((scanl f b l).nth_le i (nat.lt_of_succ_lt h))
(l.nth_le i (nat.lt_of_succ_lt_succ (lt_of_lt_of_le h (le_of_eq (length_scanl b l))))) :=
begin
induction i with i hi generalizing b l,
{ cases l,
{ simp only [length, zero_add, scanl_nil] at h,
exact absurd h (lt_irrefl 1) },
{ simp only [scanl_cons, singleton_append, nth_le_zero_scanl, nth_le] } },
{ cases l,
{ simp only [length, add_lt_iff_neg_right, scanl_nil] at h,
exact absurd h (not_lt_of_lt nat.succ_pos') },
{ simp_rw scanl_cons,
rw nth_le_append_right _,
{ simpa only [hi, length, succ_add_sub_one] },
{ simp only [length, nat.zero_le, le_add_iff_nonneg_left] } } }
end
end scanl
/- scanr -/
@[simp] theorem scanr_nil (f : Ξ± β Ξ² β Ξ²) (b : Ξ²) : scanr f b [] = [b] := rfl
@[simp] theorem scanr_aux_cons (f : Ξ± β Ξ² β Ξ²) (b : Ξ²) : β (a : Ξ±) (l : list Ξ±),
scanr_aux f b (a::l) = (foldr f b (a::l), scanr f b l)
| a [] := rfl
| a (x::l) := let t := scanr_aux_cons x l in
by simp only [scanr, scanr_aux, t, foldr_cons]
@[simp] theorem scanr_cons (f : Ξ± β Ξ² β Ξ²) (b : Ξ²) (a : Ξ±) (l : list Ξ±) :
scanr f b (a::l) = foldr f b (a::l) :: scanr f b l :=
by simp only [scanr, scanr_aux_cons, foldr_cons]; split; refl
section foldl_eq_foldr
-- foldl and foldr coincide when f is commutative and associative
variables {f : Ξ± β Ξ± β Ξ±} (hcomm : commutative f) (hassoc : associative f)
include hassoc
theorem foldl1_eq_foldr1 : β a b l, foldl f a (l++[b]) = foldr f b (a::l)
| a b nil := rfl
| a b (c :: l) :=
by simp only [cons_append, foldl_cons, foldr_cons, foldl1_eq_foldr1 _ _ l]; rw hassoc
include hcomm
theorem foldl_eq_of_comm_of_assoc : β a b l, foldl f a (b::l) = f b (foldl f a l)
| a b nil := hcomm a b
| a b (c::l) := by simp only [foldl_cons];
rw [β foldl_eq_of_comm_of_assoc, right_comm _ hcomm hassoc]; refl
theorem foldl_eq_foldr : β a l, foldl f a l = foldr f a l
| a nil := rfl
| a (b :: l) :=
by simp only [foldr_cons, foldl_eq_of_comm_of_assoc hcomm hassoc]; rw (foldl_eq_foldr a l)
end foldl_eq_foldr
section foldl_eq_foldlr'
variables {f : Ξ± β Ξ² β Ξ±}
variables hf : β a b c, f (f a b) c = f (f a c) b
include hf
theorem foldl_eq_of_comm' : β a b l, foldl f a (b::l) = f (foldl f a l) b
| a b [] := rfl
| a b (c :: l) := by rw [foldl,foldl,foldl,β foldl_eq_of_comm',foldl,hf]
theorem foldl_eq_foldr' : β a l, foldl f a l = foldr (flip f) a l
| a [] := rfl
| a (b :: l) := by rw [foldl_eq_of_comm' hf,foldr,foldl_eq_foldr']; refl
end foldl_eq_foldlr'
section foldl_eq_foldlr'
variables {f : Ξ± β Ξ² β Ξ²}
variables hf : β a b c, f a (f b c) = f b (f a c)
include hf
theorem foldr_eq_of_comm' : β a b l, foldr f a (b::l) = foldr f (f b a) l
| a b [] := rfl
| a b (c :: l) := by rw [foldr,foldr,foldr,hf,β foldr_eq_of_comm']; refl
end foldl_eq_foldlr'
section
variables {op : Ξ± β Ξ± β Ξ±} [ha : is_associative Ξ± op] [hc : is_commutative Ξ± op]
local notation (name := op) a ` * ` b := op a b
local notation (name := foldl) l ` <*> ` a := foldl op a l
include ha
lemma foldl_assoc : β {l : list Ξ±} {aβ aβ}, l <*> (aβ * aβ) = aβ * (l <*> aβ)
| [] aβ aβ := rfl
| (a :: l) aβ aβ :=
calc a::l <*> (aβ * aβ) = l <*> (aβ * (aβ * a)) : by simp only [foldl_cons, ha.assoc]
... = aβ * (a::l <*> aβ) : by rw [foldl_assoc, foldl_cons]
lemma foldl_op_eq_op_foldr_assoc : β{l : list Ξ±} {aβ aβ}, (l <*> aβ) * aβ = aβ * l.foldr (*) aβ
| [] aβ aβ := rfl
| (a :: l) aβ aβ := by simp only [foldl_cons, foldr_cons, foldl_assoc, ha.assoc];
rw [foldl_op_eq_op_foldr_assoc]
include hc
lemma foldl_assoc_comm_cons {l : list Ξ±} {aβ aβ} : (aβ :: l) <*> aβ = aβ * (l <*> aβ) :=
by rw [foldl_cons, hc.comm, foldl_assoc]
end
/-! ### mfoldl, mfoldr, mmap -/
section mfoldl_mfoldr
variables {m : Type v β Type w} [monad m]
@[simp] theorem mfoldl_nil (f : Ξ² β Ξ± β m Ξ²) {b} : mfoldl f b [] = pure b := rfl
@[simp] theorem mfoldr_nil (f : Ξ± β Ξ² β m Ξ²) {b} : mfoldr f b [] = pure b := rfl
@[simp] theorem mfoldl_cons {f : Ξ² β Ξ± β m Ξ²} {b a l} :
mfoldl f b (a :: l) = f b a >>= Ξ» b', mfoldl f b' l := rfl
@[simp] theorem mfoldr_cons {f : Ξ± β Ξ² β m Ξ²} {b a l} :
mfoldr f b (a :: l) = mfoldr f b l >>= f a := rfl
theorem mfoldr_eq_foldr (f : Ξ± β Ξ² β m Ξ²) (b l) :
mfoldr f b l = foldr (Ξ» a mb, mb >>= f a) (pure b) l :=
by induction l; simp *
attribute [simp] mmap mmap'
variables [is_lawful_monad m]
theorem mfoldl_eq_foldl (f : Ξ² β Ξ± β m Ξ²) (b l) :
mfoldl f b l = foldl (Ξ» mb a, mb >>= Ξ» b, f b a) (pure b) l :=
begin
suffices h : β (mb : m Ξ²),
(mb >>= Ξ» b, mfoldl f b l) = foldl (Ξ» mb a, mb >>= Ξ» b, f b a) mb l,
by simp [βh (pure b)],
induction l; intro,
{ simp },
{ simp only [mfoldl, foldl, βl_ih] with functor_norm }
end
@[simp] theorem mfoldl_append {f : Ξ² β Ξ± β m Ξ²} : β {b lβ lβ},
mfoldl f b (lβ ++ lβ) = mfoldl f b lβ >>= Ξ» x, mfoldl f x lβ
| _ [] _ := by simp only [nil_append, mfoldl_nil, pure_bind]
| _ (_::_) _ := by simp only [cons_append, mfoldl_cons, mfoldl_append, is_lawful_monad.bind_assoc]
@[simp] theorem mfoldr_append {f : Ξ± β Ξ² β m Ξ²} : β {b lβ lβ},
mfoldr f b (lβ ++ lβ) = mfoldr f b lβ >>= Ξ» x, mfoldr f x lβ
| _ [] _ := by simp only [nil_append, mfoldr_nil, bind_pure]
| _ (_::_) _ := by simp only [mfoldr_cons, cons_append, mfoldr_append, is_lawful_monad.bind_assoc]
end mfoldl_mfoldr
/-! ### intersperse -/
@[simp] lemma intersperse_nil {Ξ± : Type u} (a : Ξ±) : intersperse a [] = [] := rfl
@[simp] lemma intersperse_singleton {Ξ± : Type u} (a b : Ξ±) : intersperse a [b] = [b] := rfl
@[simp] lemma intersperse_cons_cons {Ξ± : Type u} (a b c : Ξ±) (tl : list Ξ±) :
intersperse a (b :: c :: tl) = b :: a :: intersperse a (c :: tl) := rfl
/-! ### split_at and split_on -/
section split_at_on
variables (p : Ξ± β Prop) [decidable_pred p] (xs ys : list Ξ±)
(ls : list (list Ξ±)) (f : list Ξ± β list Ξ±)
@[simp] theorem split_at_eq_take_drop : β (n : β) (l : list Ξ±), split_at n l = (take n l, drop n l)
| 0 a := rfl
| (succ n) [] := rfl
| (succ n) (x :: xs) := by simp only [split_at, split_at_eq_take_drop n xs, take, drop]
@[simp] lemma split_on_nil {Ξ± : Type u} [decidable_eq Ξ±] (a : Ξ±) : [].split_on a = [[]] := rfl
@[simp] lemma split_on_p_nil : [].split_on_p p = [[]] := rfl
/-- An auxiliary definition for proving a specification lemma for `split_on_p`.
`split_on_p_aux' P xs ys` splits the list `ys ++ xs` at every element satisfying `P`,
where `ys` is an accumulating parameter for the initial segment of elements not satisfying `P`.
-/
def split_on_p_aux' {Ξ± : Type u} (P : Ξ± β Prop) [decidable_pred P] : list Ξ± β list Ξ± β list (list Ξ±)
| [] xs := [xs]
| (h :: t) xs :=
if P h then xs :: split_on_p_aux' t []
else split_on_p_aux' t (xs ++ [h])
lemma split_on_p_aux_eq : split_on_p_aux' p xs ys = split_on_p_aux p xs ((++) ys) :=
begin
induction xs with a t ih generalizing ys; simp! only [append_nil, eq_self_iff_true, and_self],
split_ifs; rw ih,
{ refine β¨rfl, rflβ© },
{ congr, ext, simp }
end
lemma split_on_p_aux_nil : split_on_p_aux p xs id = split_on_p_aux' p xs [] :=
by { rw split_on_p_aux_eq, refl }
/-- The original list `L` can be recovered by joining the lists produced by `split_on_p p L`,
interspersed with the elements `L.filter p`. -/
lemma split_on_p_spec (as : list Ξ±) :
join (zip_with (++) (split_on_p p as) ((as.filter p).map (Ξ» x, [x]) ++ [[]])) = as :=
begin
rw [split_on_p, split_on_p_aux_nil],
suffices : β xs,
join (zip_with (++) (split_on_p_aux' p as xs) ((as.filter p).map(Ξ» x, [x]) ++ [[]])) = xs ++ as,
{ rw this, refl },
induction as; intro; simp! only [split_on_p_aux', append_nil],
split_ifs; simp [zip_with, join, *],
end
lemma split_on_p_aux_ne_nil : split_on_p_aux p xs f β [] :=
begin
induction xs with _ _ ih generalizing f, { trivial, },
simp only [split_on_p_aux], split_ifs, { trivial, }, exact ih _,
end
lemma split_on_p_aux_spec : split_on_p_aux p xs f = (xs.split_on_p p).modify_head f :=
begin
simp only [split_on_p],
induction xs with hd tl ih generalizing f, { simp [split_on_p_aux], },
simp only [split_on_p_aux], split_ifs, { simp, },
rw [ih (Ξ» l, f (hd :: l)), ih (Ξ» l, id (hd :: l))],
simp,
end
lemma split_on_p_ne_nil : xs.split_on_p p β [] := split_on_p_aux_ne_nil _ _ id
@[simp] lemma split_on_p_cons (x : Ξ±) (xs : list Ξ±) :
(x :: xs).split_on_p p =
if p x then [] :: xs.split_on_p p else (xs.split_on_p p).modify_head (cons x) :=
by { simp only [split_on_p, split_on_p_aux], split_ifs, { simp }, rw split_on_p_aux_spec, refl, }
/-- If no element satisfies `p` in the list `xs`, then `xs.split_on_p p = [xs]` -/
lemma split_on_p_eq_single (h : β x β xs, Β¬p x) : xs.split_on_p p = [xs] :=
by { induction xs with hd tl ih, { refl, }, simp [h hd _, ih (Ξ» t ht, h t (or.inr ht))], }
/-- When a list of the form `[...xs, sep, ...as]` is split on `p`, the first element is `xs`,
assuming no element in `xs` satisfies `p` but `sep` does satisfy `p` -/
lemma split_on_p_first (h : β x β xs, Β¬p x) (sep : Ξ±) (hsep : p sep)
(as : list Ξ±) : (xs ++ sep :: as).split_on_p p = xs :: as.split_on_p p :=
by { induction xs with hd tl ih, { simp [hsep], }, simp [h hd _, ih (Ξ» t ht, h t (or.inr ht))], }
/-- `intercalate [x]` is the left inverse of `split_on x` -/
lemma intercalate_split_on (x : Ξ±) [decidable_eq Ξ±] : [x].intercalate (xs.split_on x) = xs :=
begin
simp only [intercalate, split_on],
induction xs with hd tl ih, { simp [join], }, simp only [split_on_p_cons],
cases h' : split_on_p (=x) tl with hd' tl', { exact (split_on_p_ne_nil _ tl h').elim, },
rw h' at ih, split_ifs, { subst h, simp [ih, join], },
cases tl'; simpa [join] using ih,
end
/-- `split_on x` is the left inverse of `intercalate [x]`, on the domain
consisting of each nonempty list of lists `ls` whose elements do not contain `x` -/
lemma split_on_intercalate [decidable_eq Ξ±] (x : Ξ±) (hx : β l β ls, x β l) (hls : ls β []) :
([x].intercalate ls).split_on x = ls :=
begin
simp only [intercalate],
induction ls with hd tl ih, { contradiction, },
cases tl,
{ suffices : hd.split_on x = [hd], { simpa [join], },
refine split_on_p_eq_single _ _ _, intros y hy H, rw H at hy,
refine hx hd _ hy, simp, },
{ simp only [intersperse_cons_cons, singleton_append, join],
specialize ih _ _, { intros l hl, apply hx l, simp at hl β’, tauto, }, { trivial, },
have := split_on_p_first (=x) hd _ x rfl _,
{ simp only [split_on] at β’ ih, rw this, rw ih, },
intros y hy H, rw H at hy, exact hx hd (or.inl rfl) hy, }
end
end split_at_on
/-! ### map for partial functions -/
/-- Partial map. If `f : Ξ a, p a β Ξ²` is a partial function defined on
`a : Ξ±` satisfying `p`, then `pmap f l h` is essentially the same as `map f l`
but is defined only when all members of `l` satisfy `p`, using the proof
to apply `f`. -/
@[simp] def pmap {p : Ξ± β Prop} (f : Ξ a, p a β Ξ²) : Ξ l : list Ξ±, (β a β l, p a) β list Ξ²
| [] H := []
| (a::l) H := f a (forall_mem_cons.1 H).1 :: pmap l (forall_mem_cons.1 H).2
/-- "Attach" the proof that the elements of `l` are in `l` to produce a new list
with the same elements but in the type `{x // x β l}`. -/
def attach (l : list Ξ±) : list {x // x β l} := pmap subtype.mk l (Ξ» a, id)
theorem sizeof_lt_sizeof_of_mem [has_sizeof Ξ±] {x : Ξ±} {l : list Ξ±} (hx : x β l) :
sizeof x < sizeof l :=
begin
induction l with h t ih; cases hx,
{ rw hx, exact lt_add_of_lt_of_nonneg (lt_one_add _) (nat.zero_le _) },
{ exact lt_add_of_pos_of_le (zero_lt_one_add _) (le_of_lt (ih hx)) }
end
@[simp] theorem pmap_eq_map (p : Ξ± β Prop) (f : Ξ± β Ξ²) (l : list Ξ±) (H) :
@pmap _ _ p (Ξ» a _, f a) l H = map f l :=
by induction l; [refl, simp only [*, pmap, map]]; split; refl
theorem pmap_congr {p q : Ξ± β Prop} {f : Ξ a, p a β Ξ²} {g : Ξ a, q a β Ξ²}
(l : list Ξ±) {Hβ Hβ} (h : β (a β l) hβ hβ, f a hβ = g a hβ) :
pmap f l Hβ = pmap g l Hβ :=
begin
induction l with _ _ ih,
{ refl, },
{ rw [pmap, pmap, h _ (mem_cons_self _ _), ih (Ξ» a ha, h a (mem_cons_of_mem _ ha))], },
end
theorem map_pmap {p : Ξ± β Prop} (g : Ξ² β Ξ³) (f : Ξ a, p a β Ξ²)
(l H) : map g (pmap f l H) = pmap (Ξ» a h, g (f a h)) l H :=
by induction l; [refl, simp only [*, pmap, map]]; split; refl
theorem pmap_map {p : Ξ² β Prop} (g : β b, p b β Ξ³) (f : Ξ± β Ξ²)
(l H) : pmap g (map f l) H = pmap (Ξ» a h, g (f a) h) l (Ξ» a h, H _ (mem_map_of_mem _ h)) :=
by induction l; [refl, simp only [*, pmap, map]]; split; refl
theorem pmap_eq_map_attach {p : Ξ± β Prop} (f : Ξ a, p a β Ξ²)
(l H) : pmap f l H = l.attach.map (Ξ» x, f x.1 (H _ x.2)) :=
by rw [attach, map_pmap]; exact pmap_congr l (Ξ» _ _ _ _, rfl)
@[simp] lemma attach_map_coe' (l : list Ξ±) (f : Ξ± β Ξ²) : l.attach.map (Ξ» i, f i) = l.map f :=
by rw [attach, map_pmap]; exact (pmap_eq_map _ _ _ _)
lemma attach_map_val' (l : list Ξ±) (f : Ξ± β Ξ²) : l.attach.map (Ξ» i, f i.val) = l.map f :=
attach_map_coe' _ _
@[simp] lemma attach_map_coe (l : list Ξ±) : l.attach.map (coe : _ β Ξ±) = l :=
(attach_map_coe' _ _).trans l.map_id
lemma attach_map_val (l : list Ξ±) : l.attach.map subtype.val = l := attach_map_coe _
@[simp] theorem mem_attach (l : list Ξ±) : β x, x β l.attach | β¨a, hβ© :=
by have := mem_map.1 (by rw [attach_map_val]; exact h);
{ rcases this with β¨β¨_, _β©, m, rflβ©, exact m }
@[simp] theorem mem_pmap {p : Ξ± β Prop} {f : Ξ a, p a β Ξ²}
{l H b} : b β pmap f l H β β a (h : a β l), f a (H a h) = b :=
by simp only [pmap_eq_map_attach, mem_map, mem_attach, true_and, subtype.exists]
@[simp] theorem length_pmap {p : Ξ± β Prop} {f : Ξ a, p a β Ξ²}
{l H} : length (pmap f l H) = length l :=
by induction l; [refl, simp only [*, pmap, length]]
@[simp] lemma length_attach (L : list Ξ±) : L.attach.length = L.length := length_pmap
@[simp] lemma pmap_eq_nil {p : Ξ± β Prop} {f : Ξ a, p a β Ξ²}
{l H} : pmap f l H = [] β l = [] :=
by rw [β length_eq_zero, length_pmap, length_eq_zero]
@[simp] lemma attach_eq_nil (l : list Ξ±) : l.attach = [] β l = [] := pmap_eq_nil
lemma last_pmap {Ξ± Ξ² : Type*} (p : Ξ± β Prop) (f : Ξ a, p a β Ξ²)
(l : list Ξ±) (hlβ : β a β l, p a) (hlβ : l β []) :
(l.pmap f hlβ).last (mt list.pmap_eq_nil.1 hlβ) = f (l.last hlβ) (hlβ _ (list.last_mem hlβ)) :=
begin
induction l with l_hd l_tl l_ih,
{ apply (hlβ rfl).elim },
{ cases l_tl,
{ simp },
{ apply l_ih } }
end
lemma nth_pmap {p : Ξ± β Prop} (f : Ξ a, p a β Ξ²) {l : list Ξ±} (h : β a β l, p a) (n : β) :
nth (pmap f l h) n = option.pmap f (nth l n) (Ξ» x H, h x (nth_mem H)) :=
begin
induction l with hd tl hl generalizing n,
{ simp },
{ cases n; simp [hl] }
end
lemma nth_le_pmap {p : Ξ± β Prop} (f : Ξ a, p a β Ξ²) {l : list Ξ±} (h : β a β l, p a) {n : β}
(hn : n < (pmap f l h).length) :
nth_le (pmap f l h) n hn = f (nth_le l n (@length_pmap _ _ p f l h βΈ hn))
(h _ (nth_le_mem l n (@length_pmap _ _ p f l h βΈ hn))) :=
begin
induction l with hd tl hl generalizing n,
{ simp only [length, pmap] at hn,
exact absurd hn (not_lt_of_le n.zero_le) },
{ cases n,
{ simp },
{ simpa [hl] } }
end
lemma pmap_append {p : ΞΉ β Prop} (f : Ξ (a : ΞΉ), p a β Ξ±) (lβ lβ : list ΞΉ)
(h : β a β lβ ++ lβ, p a) :
(lβ ++ lβ).pmap f h = lβ.pmap f (Ξ» a ha, h a (mem_append_left lβ ha)) ++
lβ.pmap f (Ξ» a ha, h a (mem_append_right lβ ha)) :=
begin
induction lβ with _ _ ih,
{ refl, },
{ dsimp only [pmap, cons_append],
rw ih, }
end
lemma pmap_append' {Ξ± Ξ² : Type*} {p : Ξ± β Prop} (f : Ξ (a : Ξ±), p a β Ξ²) (lβ lβ : list Ξ±)
(hβ : β a β lβ, p a) (hβ : β a β lβ, p a) :
(lβ ++ lβ).pmap f (Ξ» a ha, (list.mem_append.1 ha).elim (hβ a) (hβ a)) =
lβ.pmap f hβ ++ lβ.pmap f hβ :=
pmap_append f lβ lβ _
/-! ### find -/
section find
variables {p : Ξ± β Prop} [decidable_pred p] {l : list Ξ±} {a : Ξ±}
@[simp] theorem find_nil (p : Ξ± β Prop) [decidable_pred p] : find p [] = none :=
rfl
@[simp] theorem find_cons_of_pos (l) (h : p a) : find p (a::l) = some a :=
if_pos h
@[simp] theorem find_cons_of_neg (l) (h : Β¬ p a) : find p (a::l) = find p l :=
if_neg h
@[simp] theorem find_eq_none : find p l = none β β x β l, Β¬ p x :=
begin
induction l with a l IH,
{ exact iff_of_true rfl (forall_mem_nil _) },
rw forall_mem_cons, by_cases h : p a,
{ simp only [find_cons_of_pos _ h, h, not_true, false_and] },
{ rwa [find_cons_of_neg _ h, iff_true_intro h, true_and] }
end
theorem find_some (H : find p l = some a) : p a :=
begin
induction l with b l IH, {contradiction},
by_cases h : p b,
{ rw find_cons_of_pos _ h at H, cases H, exact h },
{ rw find_cons_of_neg _ h at H, exact IH H }
end
@[simp] theorem find_mem (H : find p l = some a) : a β l :=
begin
induction l with b l IH, {contradiction},
by_cases h : p b,
{ rw find_cons_of_pos _ h at H, cases H, apply mem_cons_self },
{ rw find_cons_of_neg _ h at H, exact mem_cons_of_mem _ (IH H) }
end
end find
/-! ### lookmap -/
section lookmap
variables (f : Ξ± β option Ξ±)
@[simp] theorem lookmap_nil : [].lookmap f = [] := rfl
@[simp] theorem lookmap_cons_none {a : Ξ±} (l : list Ξ±) (h : f a = none) :
(a :: l).lookmap f = a :: l.lookmap f :=
by simp [lookmap, h]
@[simp] theorem lookmap_cons_some {a b : Ξ±} (l : list Ξ±) (h : f a = some b) :
(a :: l).lookmap f = b :: l :=
by simp [lookmap, h]
theorem lookmap_some : β l : list Ξ±, l.lookmap some = l
| [] := rfl
| (a::l) := rfl
theorem lookmap_none : β l : list Ξ±, l.lookmap (Ξ» _, none) = l
| [] := rfl
| (a::l) := congr_arg (cons a) (lookmap_none l)
theorem lookmap_congr {f g : Ξ± β option Ξ±} :
β {l : list Ξ±}, (β a β l, f a = g a) β l.lookmap f = l.lookmap g
| [] H := rfl
| (a::l) H := begin
cases forall_mem_cons.1 H with Hβ Hβ,
cases h : g a with b,
{ simp [h, Hβ.trans h, lookmap_congr Hβ] },
{ simp [lookmap_cons_some _ _ h, lookmap_cons_some _ _ (Hβ.trans h)] }
end
theorem lookmap_of_forall_not {l : list Ξ±} (H : β a β l, f a = none) : l.lookmap f = l :=
(lookmap_congr H).trans (lookmap_none l)
theorem lookmap_map_eq (g : Ξ± β Ξ²) (h : β a (b β f a), g a = g b) :
β l : list Ξ±, map g (l.lookmap f) = map g l
| [] := rfl
| (a::l) := begin
cases h' : f a with b,
{ simp [h', lookmap_map_eq] },
{ simp [lookmap_cons_some _ _ h', h _ _ h'] }
end
theorem lookmap_id' (h : β a (b β f a), a = b) (l : list Ξ±) : l.lookmap f = l :=
by rw [β map_id (l.lookmap f), lookmap_map_eq, map_id]; exact h
theorem length_lookmap (l : list Ξ±) : length (l.lookmap f) = length l :=
by rw [β length_map, lookmap_map_eq _ (Ξ» _, ()), length_map]; simp
end lookmap
/-! ### filter_map -/
@[simp] theorem filter_map_nil (f : Ξ± β option Ξ²) : filter_map f [] = [] := rfl
@[simp] theorem filter_map_cons_none {f : Ξ± β option Ξ²} (a : Ξ±) (l : list Ξ±) (h : f a = none) :
filter_map f (a :: l) = filter_map f l :=
by simp only [filter_map, h]
@[simp] theorem filter_map_cons_some (f : Ξ± β option Ξ²)
(a : Ξ±) (l : list Ξ±) {b : Ξ²} (h : f a = some b) :
filter_map f (a :: l) = b :: filter_map f l :=
by simp only [filter_map, h]; split; refl
theorem filter_map_cons (f : Ξ± β option Ξ²) (a : Ξ±) (l : list Ξ±) :
filter_map f (a :: l) = option.cases_on (f a) (filter_map f l) (Ξ»b, b :: filter_map f l) :=
begin
generalize eq : f a = b,
cases b,
{ rw filter_map_cons_none _ _ eq },
{ rw filter_map_cons_some _ _ _ eq },
end
lemma filter_map_append {Ξ± Ξ² : Type*} (l l' : list Ξ±) (f : Ξ± β option Ξ²) :
filter_map f (l ++ l') = filter_map f l ++ filter_map f l' :=
begin
induction l with hd tl hl generalizing l',
{ simp },
{ rw [cons_append, filter_map, filter_map],
cases f hd;
simp only [filter_map, hl, cons_append, eq_self_iff_true, and_self] }
end
theorem filter_map_eq_map (f : Ξ± β Ξ²) : filter_map (some β f) = map f :=
begin
funext l,
induction l with a l IH, {refl},
simp only [filter_map_cons_some (some β f) _ _ rfl, IH, map_cons], split; refl
end
theorem filter_map_eq_filter (p : Ξ± β Prop) [decidable_pred p] :
filter_map (option.guard p) = filter p :=
begin
funext l,
induction l with a l IH, {refl},
by_cases pa : p a,
{ simp only [filter_map, option.guard, IH, if_pos pa, filter_cons_of_pos _ pa], split; refl },
{ simp only [filter_map, option.guard, IH, if_neg pa, filter_cons_of_neg _ pa] }
end
theorem filter_map_filter_map (f : Ξ± β option Ξ²) (g : Ξ² β option Ξ³) (l : list Ξ±) :
filter_map g (filter_map f l) = filter_map (Ξ» x, (f x).bind g) l :=
begin
induction l with a l IH, {refl},
cases h : f a with b,
{ rw [filter_map_cons_none _ _ h, filter_map_cons_none, IH],
simp only [h, option.none_bind'] },
rw filter_map_cons_some _ _ _ h,
cases h' : g b with c;
[ rw [filter_map_cons_none _ _ h', filter_map_cons_none, IH],
rw [filter_map_cons_some _ _ _ h', filter_map_cons_some, IH] ];
simp only [h, h', option.some_bind']
end
theorem map_filter_map (f : Ξ± β option Ξ²) (g : Ξ² β Ξ³) (l : list Ξ±) :
map g (filter_map f l) = filter_map (Ξ» x, (f x).map g) l :=
by rw [β filter_map_eq_map, filter_map_filter_map]; refl
theorem filter_map_map (f : Ξ± β Ξ²) (g : Ξ² β option Ξ³) (l : list Ξ±) :
filter_map g (map f l) = filter_map (g β f) l :=
by rw [β filter_map_eq_map, filter_map_filter_map]; refl
theorem filter_filter_map (f : Ξ± β option Ξ²) (p : Ξ² β Prop) [decidable_pred p] (l : list Ξ±) :
filter p (filter_map f l) = filter_map (Ξ» x, (f x).filter p) l :=
by rw [β filter_map_eq_filter, filter_map_filter_map]; refl
theorem filter_map_filter (p : Ξ± β Prop) [decidable_pred p] (f : Ξ± β option Ξ²) (l : list Ξ±) :
filter_map f (filter p l) = filter_map (Ξ» x, if p x then f x else none) l :=
begin
rw [β filter_map_eq_filter, filter_map_filter_map], congr,
funext x,
show (option.guard p x).bind f = ite (p x) (f x) none,
by_cases h : p x,
{ simp only [option.guard, if_pos h, option.some_bind'] },
{ simp only [option.guard, if_neg h, option.none_bind'] }
end
@[simp] theorem filter_map_some (l : list Ξ±) : filter_map some l = l :=
by rw filter_map_eq_map; apply map_id
theorem map_filter_map_some_eq_filter_map_is_some (f : Ξ± β option Ξ²) (l : list Ξ±) :
(l.filter_map f).map some = (l.map f).filter (Ξ» b, b.is_some) :=
begin
induction l with x xs ih,
{ simp },
{ cases h : f x; rw [list.filter_map_cons, h]; simp [h, ih] },
end
@[simp] theorem mem_filter_map (f : Ξ± β option Ξ²) (l : list Ξ±) {b : Ξ²} :
b β filter_map f l β β a, a β l β§ f a = some b :=
begin
induction l with a l IH,
{ split, { intro H, cases H }, { rintro β¨_, H, _β©, cases H } },
cases h : f a with b',
{ have : f a β some b, {rw h, intro, contradiction},
simp only [filter_map_cons_none _ _ h, IH, mem_cons_iff,
or_and_distrib_right, exists_or_distrib, exists_eq_left, this, false_or] },
{ have : f a = some b β b = b',
{ split; intro t, {rw t at h; injection h}, {exact t.symm βΈ h} },
simp only [filter_map_cons_some _ _ _ h, IH, mem_cons_iff,
or_and_distrib_right, exists_or_distrib, this, exists_eq_left] }
end
@[simp] theorem filter_map_join (f : Ξ± β option Ξ²) (L : list (list Ξ±)) :
filter_map f (join L) = join (map (filter_map f) L) :=
begin
induction L with hd tl ih,
{ refl },
{ rw [map, join, join, filter_map_append, ih] },
end
theorem map_filter_map_of_inv (f : Ξ± β option Ξ²) (g : Ξ² β Ξ±)
(H : β x : Ξ±, (f x).map g = some x) (l : list Ξ±) :
map g (filter_map f l) = l :=
by simp only [map_filter_map, H, filter_map_some]
theorem length_filter_le (p : Ξ± β Prop) [decidable_pred p] (l : list Ξ±) :
(l.filter p).length β€ l.length :=
(list.filter_sublist _).length_le
theorem length_filter_map_le (f : Ξ± β option Ξ²) (l : list Ξ±) :
(list.filter_map f l).length β€ l.length :=
begin
rw [β list.length_map some, list.map_filter_map_some_eq_filter_map_is_some, β list.length_map f],
apply list.length_filter_le,
end
theorem sublist.filter_map (f : Ξ± β option Ξ²) {lβ lβ : list Ξ±}
(s : lβ <+ lβ) : filter_map f lβ <+ filter_map f lβ :=
by induction s with lβ lβ a s IH lβ lβ a s IH;
simp only [filter_map]; cases f a with b;
simp only [filter_map, IH, sublist.cons, sublist.cons2]
theorem sublist.map (f : Ξ± β Ξ²) {lβ lβ : list Ξ±}
(s : lβ <+ lβ) : map f lβ <+ map f lβ :=
filter_map_eq_map f βΈ s.filter_map _
/-! ### reduce_option -/
@[simp] lemma reduce_option_cons_of_some (x : Ξ±) (l : list (option Ξ±)) :
reduce_option (some x :: l) = x :: l.reduce_option :=
by simp only [reduce_option, filter_map, id.def, eq_self_iff_true, and_self]
@[simp] lemma reduce_option_cons_of_none (l : list (option Ξ±)) :
reduce_option (none :: l) = l.reduce_option :=
by simp only [reduce_option, filter_map, id.def]
@[simp] lemma reduce_option_nil : @reduce_option Ξ± [] = [] := rfl
@[simp] lemma reduce_option_map {l : list (option Ξ±)} {f : Ξ± β Ξ²} :
reduce_option (map (option.map f) l) = map f (reduce_option l) :=
begin
induction l with hd tl hl,
{ simp only [reduce_option_nil, map_nil] },
{ cases hd;
simpa only [true_and, option.map_some', map, eq_self_iff_true,
reduce_option_cons_of_some] using hl },
end
lemma reduce_option_append (l l' : list (option Ξ±)) :
(l ++ l').reduce_option = l.reduce_option ++ l'.reduce_option :=
filter_map_append l l' id
lemma reduce_option_length_le (l : list (option Ξ±)) :
l.reduce_option.length β€ l.length :=
begin
induction l with hd tl hl,
{ simp only [reduce_option_nil, length] },
{ cases hd,
{ exact nat.le_succ_of_le hl },
{ simpa only [length, add_le_add_iff_right, reduce_option_cons_of_some] using hl} }
end
lemma reduce_option_length_eq_iff {l : list (option Ξ±)} :
l.reduce_option.length = l.length β β x β l, option.is_some x :=
begin
induction l with hd tl hl,
{ simp only [forall_const, reduce_option_nil, not_mem_nil,
forall_prop_of_false, eq_self_iff_true, length, not_false_iff] },
{ cases hd,
{ simp only [mem_cons_iff, forall_eq_or_imp, bool.coe_sort_ff, false_and,
reduce_option_cons_of_none, length, option.is_some_none, iff_false],
intro H,
have := reduce_option_length_le tl,
rw H at this,
exact absurd (nat.lt_succ_self _) (not_lt_of_le this) },
{ simp only [hl, true_and, mem_cons_iff, forall_eq_or_imp, add_left_inj,
bool.coe_sort_tt, length, option.is_some_some, reduce_option_cons_of_some] } }
end
lemma reduce_option_length_lt_iff {l : list (option Ξ±)} :
l.reduce_option.length < l.length β none β l :=
begin
rw [(reduce_option_length_le l).lt_iff_ne, ne, reduce_option_length_eq_iff],
induction l; simp *,
rw [eq_comm, β option.not_is_some_iff_eq_none, decidable.imp_iff_not_or]
end
lemma reduce_option_singleton (x : option Ξ±) :
[x].reduce_option = x.to_list :=
by cases x; refl
lemma reduce_option_concat (l : list (option Ξ±)) (x : option Ξ±) :
(l.concat x).reduce_option = l.reduce_option ++ x.to_list :=
begin
induction l with hd tl hl generalizing x,
{ cases x;
simp [option.to_list] },
{ simp only [concat_eq_append, reduce_option_append] at hl,
cases hd;
simp [hl, reduce_option_append] }
end
lemma reduce_option_concat_of_some (l : list (option Ξ±)) (x : Ξ±) :
(l.concat (some x)).reduce_option = l.reduce_option.concat x :=
by simp only [reduce_option_nil, concat_eq_append, reduce_option_append, reduce_option_cons_of_some]
lemma reduce_option_mem_iff {l : list (option Ξ±)} {x : Ξ±} :
x β l.reduce_option β (some x) β l :=
by simp only [reduce_option, id.def, mem_filter_map, exists_eq_right]
lemma reduce_option_nth_iff {l : list (option Ξ±)} {x : Ξ±} :
(β i, l.nth i = some (some x)) β β i, l.reduce_option.nth i = some x :=
by rw [βmem_iff_nth, βmem_iff_nth, reduce_option_mem_iff]
/-! ### filter -/
section filter
variables {p : Ξ± β Prop} [decidable_pred p]
lemma filter_singleton {a : Ξ±} : [a].filter p = if p a then [a] else [] := rfl
theorem filter_eq_foldr (p : Ξ± β Prop) [decidable_pred p] (l : list Ξ±) :
filter p l = foldr (Ξ» a out, if p a then a :: out else out) [] l :=
by induction l; simp [*, filter]
lemma filter_congr' {p q : Ξ± β Prop} [decidable_pred p] [decidable_pred q]
: β {l : list Ξ±}, (β x β l, p x β q x) β filter p l = filter q l
| [] _ := rfl
| (a::l) h := by rw forall_mem_cons at h; by_cases pa : p a;
[simp only [filter_cons_of_pos _ pa, filter_cons_of_pos _ (h.1.1 pa), filter_congr' h.2],
simp only [filter_cons_of_neg _ pa, filter_cons_of_neg _ (mt h.1.2 pa), filter_congr' h.2]];
split; refl
@[simp] theorem filter_subset (l : list Ξ±) : filter p l β l :=
(filter_sublist l).subset
theorem of_mem_filter {a : Ξ±} : β {l}, a β filter p l β p a
| (b::l) ain :=
if pb : p b then
have a β b :: filter p l, by simpa only [filter_cons_of_pos _ pb] using ain,
or.elim (eq_or_mem_of_mem_cons this)
(assume : a = b, begin rw [β this] at pb, exact pb end)
(assume : a β filter p l, of_mem_filter this)
else
begin simp only [filter_cons_of_neg _ pb] at ain, exact (of_mem_filter ain) end
theorem mem_of_mem_filter {a : Ξ±} {l} (h : a β filter p l) : a β l :=
filter_subset l h
theorem mem_filter_of_mem {a : Ξ±} : β {l}, a β l β p a β a β filter p l
| (_::l) (or.inl rfl) pa := by rw filter_cons_of_pos _ pa; apply mem_cons_self
| (b::l) (or.inr ain) pa := if pb : p b
then by rw [filter_cons_of_pos _ pb]; apply mem_cons_of_mem; apply mem_filter_of_mem ain pa
else by rw [filter_cons_of_neg _ pb]; apply mem_filter_of_mem ain pa
@[simp] theorem mem_filter {a : Ξ±} {l} : a β filter p l β a β l β§ p a :=
β¨Ξ» h, β¨mem_of_mem_filter h, of_mem_filter hβ©, Ξ» β¨hβ, hββ©, mem_filter_of_mem hβ hββ©
lemma monotone_filter_left (p : Ξ± β Prop) [decidable_pred p]
β¦l l' : list Ξ±β¦ (h : l β l') : filter p l β filter p l' :=
begin
intros x hx,
rw [mem_filter] at hx β’,
exact β¨h hx.left, hx.rightβ©
end
theorem filter_eq_self {l} : filter p l = l β β a β l, p a :=
begin
induction l with a l ih,
{ exact iff_of_true rfl (forall_mem_nil _) },
rw forall_mem_cons, by_cases p a,
{ rw [filter_cons_of_pos _ h, cons_inj, ih, and_iff_right h] },
{ refine iff_of_false (Ξ» hl, h $ of_mem_filter (_ : a β filter p (a :: l))) (mt and.left h),
rw hl,
exact mem_cons_self _ _ }
end
theorem filter_length_eq_length {l} : (filter p l).length = l.length β β a β l, p a :=
iff.trans β¨l.filter_sublist.eq_of_length, congr_arg list.lengthβ© filter_eq_self
theorem filter_eq_nil {l} : filter p l = [] β β a β l, Β¬p a :=
by simp only [eq_nil_iff_forall_not_mem, mem_filter, not_and]
variable (p)
theorem sublist.filter {lβ lβ} (s : lβ <+ lβ) : filter p lβ <+ filter p lβ :=
filter_map_eq_filter p βΈ s.filter_map _
lemma monotone_filter_right (l : list Ξ±) β¦p q : Ξ± β Propβ¦ [decidable_pred p] [decidable_pred q]
(h : p β€ q) : l.filter p <+ l.filter q :=
begin
induction l with hd tl IH,
{ refl },
{ by_cases hp : p hd,
{ rw [filter_cons_of_pos _ hp, filter_cons_of_pos _ (h _ hp)],
exact IH.cons_cons hd },
{ rw filter_cons_of_neg _ hp,
by_cases hq : q hd,
{ rw filter_cons_of_pos _ hq,
exact sublist_cons_of_sublist hd IH },
{ rw filter_cons_of_neg _ hq,
exact IH } } }
end
theorem map_filter (f : Ξ² β Ξ±) (l : list Ξ²) :
filter p (map f l) = map f (filter (p β f) l) :=
by rw [β filter_map_eq_map, filter_filter_map, filter_map_filter]; refl
@[simp] theorem filter_filter (q) [decidable_pred q] : β l,
filter p (filter q l) = filter (Ξ» a, p a β§ q a) l
| [] := rfl
| (a :: l) := by by_cases hp : p a; by_cases hq : q a; simp only [hp, hq, filter, if_true, if_false,
true_and, false_and, filter_filter l, eq_self_iff_true]
@[simp] lemma filter_true {h : decidable_pred (Ξ» a : Ξ±, true)} (l : list Ξ±) :
@filter Ξ± (Ξ» _, true) h l = l :=
by convert filter_eq_self.2 (Ξ» _ _, trivial)
@[simp] lemma filter_false {h : decidable_pred (Ξ» a : Ξ±, false)} (l : list Ξ±) :
@filter Ξ± (Ξ» _, false) h l = [] :=
by convert filter_eq_nil.2 (Ξ» _ _, id)
@[simp] theorem span_eq_take_drop : β (l : list Ξ±), span p l = (take_while p l, drop_while p l)
| [] := rfl
| (a::l) :=
if pa : p a then by simp only [span, if_pos pa, span_eq_take_drop l, take_while, drop_while]
else by simp only [span, take_while, drop_while, if_neg pa]
@[simp] theorem take_while_append_drop : β (l : list Ξ±), take_while p l ++ drop_while p l = l
| [] := rfl
| (a::l) := if pa : p a then by rw [take_while, drop_while, if_pos pa, if_pos pa, cons_append,
take_while_append_drop l]
else by rw [take_while, drop_while, if_neg pa, if_neg pa, nil_append]
lemma drop_while_nth_le_zero_not (l : list Ξ±) (hl : 0 < (l.drop_while p).length) :
Β¬ p ((l.drop_while p).nth_le 0 hl) :=
begin
induction l with hd tl IH,
{ cases hl },
{ simp only [drop_while],
split_ifs with hp,
{ exact IH _ },
{ simpa using hp } }
end
variables {p} {l : list Ξ±}
@[simp] lemma drop_while_eq_nil_iff : drop_while p l = [] β β x β l, p x :=
begin
induction l with x xs IH,
{ simp [drop_while] },
{ by_cases hp : p x;
simp [hp, drop_while, IH] }
end
@[simp] lemma take_while_eq_self_iff : take_while p l = l β β x β l, p x :=
begin
induction l with x xs IH,
{ simp [take_while] },
{ by_cases hp : p x;
simp [hp, take_while, IH] }
end
@[simp] lemma take_while_eq_nil_iff :
take_while p l = [] β β (hl : 0 < l.length), Β¬ p (l.nth_le 0 hl) :=
begin
induction l with x xs IH,
{ simp },
{ by_cases hp : p x;
simp [hp, take_while, IH] }
end
lemma mem_take_while_imp {x : Ξ±} (hx : x β take_while p l) : p x :=
begin
induction l with hd tl IH,
{ simpa [take_while] using hx },
{ simp only [take_while] at hx,
split_ifs at hx,
{ rw mem_cons_iff at hx,
rcases hx with rfl|hx,
{ exact h },
{ exact IH hx } },
{ simpa using hx } }
end
lemma take_while_take_while (p q : Ξ± β Prop) [decidable_pred p] [decidable_pred q] (l : list Ξ±) :
take_while p (take_while q l) = take_while (Ξ» a, p a β§ q a) l :=
begin
induction l with hd tl IH,
{ simp [take_while] },
{ by_cases hp : p hd;
by_cases hq : q hd;
simp [take_while, hp, hq, IH] }
end
lemma take_while_idem : take_while p (take_while p l) = take_while p l :=
by simp_rw [take_while_take_while, and_self]
end filter
/-! ### erasep -/
section erasep
variables {p : Ξ± β Prop} [decidable_pred p]
@[simp] theorem erasep_nil : [].erasep p = [] := rfl
theorem erasep_cons (a : Ξ±) (l : list Ξ±) :
(a :: l).erasep p = if p a then l else a :: l.erasep p := rfl
@[simp] theorem erasep_cons_of_pos {a : Ξ±} {l : list Ξ±} (h : p a) : (a :: l).erasep p = l :=
by simp [erasep_cons, h]
@[simp] theorem erasep_cons_of_neg {a : Ξ±} {l : list Ξ±} (h : Β¬ p a) :
(a::l).erasep p = a :: l.erasep p :=
by simp [erasep_cons, h]
theorem erasep_of_forall_not {l : list Ξ±}
(h : β a β l, Β¬ p a) : l.erasep p = l :=
by induction l with _ _ ih; [refl,
simp [h _ (or.inl rfl), ih (forall_mem_of_forall_mem_cons h)]]
theorem exists_of_erasep {l : list Ξ±} {a} (al : a β l) (pa : p a) :
β a lβ lβ, (β b β lβ, Β¬ p b) β§ p a β§ l = lβ ++ a :: lβ β§ l.erasep p = lβ ++ lβ :=
begin
induction l with b l IH, {cases al},
by_cases pb : p b,
{ exact β¨b, [], l, forall_mem_nil _, pb, by simp [pb]β© },
{ rcases al with rfl | al, {exact pb.elim pa},
rcases IH al with β¨c, lβ, lβ, hβ, hβ, hβ, hββ©,
exact β¨c, b::lβ, lβ, forall_mem_cons.2 β¨pb, hββ©,
hβ, by rw hβ; refl, by simp [pb, hβ]β© }
end
theorem exists_or_eq_self_of_erasep (p : Ξ± β Prop) [decidable_pred p] (l : list Ξ±) :
l.erasep p = l β¨ β a lβ lβ, (β b β lβ, Β¬ p b) β§ p a β§ l = lβ ++ a :: lβ β§ l.erasep p = lβ ++ lβ :=
begin
by_cases h : β a β l, p a,
{ rcases h with β¨a, ha, paβ©,
exact or.inr (exists_of_erasep ha pa) },
{ simp at h, exact or.inl (erasep_of_forall_not h) }
end
@[simp] theorem length_erasep_of_mem {l : list Ξ±} {a} (al : a β l) (pa : p a) :
length (l.erasep p) = pred (length l) :=
by rcases exists_of_erasep al pa with β¨_, lβ, lβ, _, _, eβ, eββ©;
rw eβ; simp [-add_comm, eβ]; refl
@[simp] lemma length_erasep_add_one {l : list Ξ±} {a} (al : a β l) (pa : p a) :
(l.erasep p).length + 1 = l.length :=
let β¨_, lβ, lβ, _, _, hβ, hββ© := exists_of_erasep al pa in
by { rw [hβ, hβ, length_append, length_append], refl }
theorem erasep_append_left {a : Ξ±} (pa : p a) :
β {lβ : list Ξ±} (lβ), a β lβ β (lβ++lβ).erasep p = lβ.erasep p ++ lβ
| (x::xs) lβ h := begin
by_cases h' : p x; simp [h'],
rw erasep_append_left lβ (mem_of_ne_of_mem (mt _ h') h),
rintro rfl, exact pa
end
theorem erasep_append_right :
β {lβ : list Ξ±} (lβ), (β b β lβ, Β¬ p b) β (lβ++lβ).erasep p = lβ ++ lβ.erasep p
| [] lβ h := rfl
| (x::xs) lβ h := by simp [(forall_mem_cons.1 h).1,
erasep_append_right _ (forall_mem_cons.1 h).2]
theorem erasep_sublist (l : list Ξ±) : l.erasep p <+ l :=
by rcases exists_or_eq_self_of_erasep p l with h | β¨c, lβ, lβ, hβ, hβ, hβ, hββ©;
[rw h, {rw [hβ, hβ], simp}]
theorem erasep_subset (l : list Ξ±) : l.erasep p β l :=
(erasep_sublist l).subset
theorem sublist.erasep {lβ lβ : list Ξ±} (s : lβ <+ lβ) : lβ.erasep p <+ lβ.erasep p :=
begin
induction s,
case list.sublist.slnil { refl },
case list.sublist.cons : lβ lβ a s IH
{ by_cases h : p a; simp [h],
exacts [IH.trans (erasep_sublist _), IH.cons _ _ _] },
case list.sublist.cons2 : lβ lβ a s IH
{ by_cases h : p a; simp [h],
exacts [s, IH.cons2 _ _ _] }
end
theorem mem_of_mem_erasep {a : Ξ±} {l : list Ξ±} : a β l.erasep p β a β l :=
@erasep_subset _ _ _ _ _
@[simp] theorem mem_erasep_of_neg {a : Ξ±} {l : list Ξ±} (pa : Β¬ p a) : a β l.erasep p β a β l :=
β¨mem_of_mem_erasep, Ξ» al, begin
rcases exists_or_eq_self_of_erasep p l with h | β¨c, lβ, lβ, hβ, hβ, hβ, hββ©,
{ rwa h },
{ rw hβ, rw hβ at al,
have : a β c, {rintro rfl, exact pa.elim hβ},
simpa [this] using al }
endβ©
theorem erasep_map (f : Ξ² β Ξ±) :
β (l : list Ξ²), (map f l).erasep p = map f (l.erasep (p β f))
| [] := rfl
| (b::l) := by by_cases p (f b); simp [h, erasep_map l]
@[simp] theorem extractp_eq_find_erasep :
β l : list Ξ±, extractp p l = (find p l, erasep p l)
| [] := rfl
| (a::l) := by by_cases pa : p a; simp [extractp, pa, extractp_eq_find_erasep l]
end erasep
/-! ### erase -/
section erase
variable [decidable_eq Ξ±]
@[simp] theorem erase_nil (a : Ξ±) : [].erase a = [] := rfl
theorem erase_cons (a b : Ξ±) (l : list Ξ±) :
(b :: l).erase a = if b = a then l else b :: l.erase a := rfl
@[simp] theorem erase_cons_head (a : Ξ±) (l : list Ξ±) : (a :: l).erase a = l :=
by simp only [erase_cons, if_pos rfl]
@[simp] theorem erase_cons_tail {a b : Ξ±} (l : list Ξ±) (h : b β a) :
(b::l).erase a = b :: l.erase a :=
by simp only [erase_cons, if_neg h]; split; refl
theorem erase_eq_erasep (a : Ξ±) (l : list Ξ±) : l.erase a = l.erasep (eq a) :=
by { induction l with b l, {refl},
by_cases a = b; [simp [h], simp [h, ne.symm h, *]] }
@[simp, priority 980]
theorem erase_of_not_mem {a : Ξ±} {l : list Ξ±} (h : a β l) : l.erase a = l :=
by rw [erase_eq_erasep, erasep_of_forall_not]; rintro b h' rfl; exact h h'
theorem exists_erase_eq {a : Ξ±} {l : list Ξ±} (h : a β l) :
β lβ lβ, a β lβ β§ l = lβ ++ a :: lβ β§ l.erase a = lβ ++ lβ :=
by rcases exists_of_erasep h rfl with β¨_, lβ, lβ, hβ, rfl, hβ, hββ©;
rw erase_eq_erasep; exact β¨lβ, lβ, Ξ» h, hβ _ h rfl, hβ, hββ©
@[simp] theorem length_erase_of_mem {a : Ξ±} {l : list Ξ±} (h : a β l) :
length (l.erase a) = pred (length l) :=
by rw erase_eq_erasep; exact length_erasep_of_mem h rfl
@[simp] lemma length_erase_add_one {a : Ξ±} {l : list Ξ±} (h : a β l) :
(l.erase a).length + 1 = l.length :=
by rw [erase_eq_erasep, length_erasep_add_one h rfl]
theorem erase_append_left {a : Ξ±} {lβ : list Ξ±} (lβ) (h : a β lβ) :
(lβ++lβ).erase a = lβ.erase a ++ lβ :=
by simp [erase_eq_erasep]; exact erasep_append_left (by refl) lβ h
theorem erase_append_right {a : Ξ±} {lβ : list Ξ±} (lβ) (h : a β lβ) :
(lβ++lβ).erase a = lβ ++ lβ.erase a :=
by rw [erase_eq_erasep, erase_eq_erasep, erasep_append_right];
rintro b h' rfl; exact h h'
theorem erase_sublist (a : Ξ±) (l : list Ξ±) : l.erase a <+ l :=
by rw erase_eq_erasep; apply erasep_sublist
theorem erase_subset (a : Ξ±) (l : list Ξ±) : l.erase a β l :=
(erase_sublist a l).subset
theorem sublist.erase (a : Ξ±) {lβ lβ : list Ξ±} (h : lβ <+ lβ) : lβ.erase a <+ lβ.erase a :=
by simp [erase_eq_erasep]; exact sublist.erasep h
theorem mem_of_mem_erase {a b : Ξ±} {l : list Ξ±} : a β l.erase b β a β l :=
@erase_subset _ _ _ _ _
@[simp] theorem mem_erase_of_ne {a b : Ξ±} {l : list Ξ±} (ab : a β b) : a β l.erase b β a β l :=
by rw erase_eq_erasep; exact mem_erasep_of_neg ab.symm
theorem erase_comm (a b : Ξ±) (l : list Ξ±) : (l.erase a).erase b = (l.erase b).erase a :=
if ab : a = b then by rw ab else
if ha : a β l then
if hb : b β l then match l, l.erase a, exists_erase_eq ha, hb with
| ._, ._, β¨lβ, lβ, ha', rfl, rflβ©, hb :=
if hβ : b β lβ then
by rw [erase_append_left _ hβ, erase_append_left _ hβ,
erase_append_right _ (mt mem_of_mem_erase ha'), erase_cons_head]
else
by rw [erase_append_right _ hβ, erase_append_right _ hβ, erase_append_right _ ha',
erase_cons_tail _ ab, erase_cons_head]
end
else by simp only [erase_of_not_mem hb, erase_of_not_mem (mt mem_of_mem_erase hb)]
else by simp only [erase_of_not_mem ha, erase_of_not_mem (mt mem_of_mem_erase ha)]
theorem map_erase [decidable_eq Ξ²] {f : Ξ± β Ξ²} (finj : injective f) {a : Ξ±}
(l : list Ξ±) : map f (l.erase a) = (map f l).erase (f a) :=
have this : eq a = eq (f a) β f, { ext b, simp [finj.eq_iff] },
by simp [erase_eq_erasep, erase_eq_erasep, erasep_map, this]
theorem map_foldl_erase [decidable_eq Ξ²] {f : Ξ± β Ξ²} (finj : injective f) {lβ lβ : list Ξ±} :
map f (foldl list.erase lβ lβ) = foldl (Ξ» l a, l.erase (f a)) (map f lβ) lβ :=
by induction lβ generalizing lβ; [refl,
simp only [foldl_cons, map_erase finj, *]]
end erase
/-! ### diff -/
section diff
variable [decidable_eq Ξ±]
@[simp] theorem diff_nil (l : list Ξ±) : l.diff [] = l := rfl
@[simp] theorem diff_cons (lβ lβ : list Ξ±) (a : Ξ±) : lβ.diff (a::lβ) = (lβ.erase a).diff lβ :=
if h : a β lβ then by simp only [list.diff, if_pos h]
else by simp only [list.diff, if_neg h, erase_of_not_mem h]
lemma diff_cons_right (lβ lβ : list Ξ±) (a : Ξ±) : lβ.diff (a::lβ) = (lβ.diff lβ).erase a :=
begin
induction lβ with b lβ ih generalizing lβ a,
{ simp_rw [diff_cons, diff_nil] },
{ rw [diff_cons, diff_cons, erase_comm, β diff_cons, ih, β diff_cons] }
end
lemma diff_erase (lβ lβ : list Ξ±) (a : Ξ±) : (lβ.diff lβ).erase a = (lβ.erase a).diff lβ :=
by rw [β diff_cons_right, diff_cons]
@[simp] theorem nil_diff (l : list Ξ±) : [].diff l = [] :=
by induction l; [refl, simp only [*, diff_cons, erase_of_not_mem (not_mem_nil _)]]
lemma cons_diff (a : Ξ±) (lβ lβ : list Ξ±) :
(a :: lβ).diff lβ = if a β lβ then lβ.diff (lβ.erase a) else a :: lβ.diff lβ :=
begin
induction lβ with b lβ ih, { refl },
rcases eq_or_ne a b with rfl|hne,
{ simp },
{ simp only [mem_cons_iff, *, false_or, diff_cons_right],
split_ifs with hβ; simp [diff_erase, list.erase, hne, hne.symm] }
end
lemma cons_diff_of_mem {a : Ξ±} {lβ : list Ξ±} (h : a β lβ) (lβ : list Ξ±) :
(a :: lβ).diff lβ = lβ.diff (lβ.erase a) :=
by rw [cons_diff, if_pos h]
lemma cons_diff_of_not_mem {a : Ξ±} {lβ : list Ξ±} (h : a β lβ) (lβ : list Ξ±) :
(a :: lβ).diff lβ = a :: lβ.diff lβ :=
by rw [cons_diff, if_neg h]
theorem diff_eq_foldl : β (lβ lβ : list Ξ±), lβ.diff lβ = foldl list.erase lβ lβ
| lβ [] := rfl
| lβ (a::lβ) := (diff_cons lβ lβ a).trans (diff_eq_foldl _ _)
@[simp] theorem diff_append (lβ lβ lβ : list Ξ±) : lβ.diff (lβ ++ lβ) = (lβ.diff lβ).diff lβ :=
by simp only [diff_eq_foldl, foldl_append]
@[simp] theorem map_diff [decidable_eq Ξ²] {f : Ξ± β Ξ²} (finj : injective f) {lβ lβ : list Ξ±} :
map f (lβ.diff lβ) = (map f lβ).diff (map f lβ) :=
by simp only [diff_eq_foldl, foldl_map, map_foldl_erase finj]
theorem diff_sublist : β lβ lβ : list Ξ±, lβ.diff lβ <+ lβ
| lβ [] := sublist.refl _
| lβ (a::lβ) := calc lβ.diff (a :: lβ) = (lβ.erase a).diff lβ : diff_cons _ _ _
... <+ lβ.erase a : diff_sublist _ _
... <+ lβ : list.erase_sublist _ _
theorem diff_subset (lβ lβ : list Ξ±) : lβ.diff lβ β lβ :=
(diff_sublist _ _).subset
theorem mem_diff_of_mem {a : Ξ±} : β {lβ lβ : list Ξ±}, a β lβ β a β lβ β a β lβ.diff lβ
| lβ [] hβ hβ := hβ
| lβ (b::lβ) hβ hβ := by rw diff_cons; exact
mem_diff_of_mem ((mem_erase_of_ne (ne_of_not_mem_cons hβ)).2 hβ) (not_mem_of_not_mem_cons hβ)
theorem sublist.diff_right : β {lβ lβ lβ: list Ξ±}, lβ <+ lβ β lβ.diff lβ <+ lβ.diff lβ
| lβ lβ [] h := h
| lβ lβ (a::lβ) h := by simp only
[diff_cons, (h.erase _).diff_right]
theorem erase_diff_erase_sublist_of_sublist {a : Ξ±} : β {lβ lβ : list Ξ±},
lβ <+ lβ β (lβ.erase a).diff (lβ.erase a) <+ lβ.diff lβ
| [] lβ h := erase_sublist _ _
| (b::lβ) lβ h := if heq : b = a then by simp only [heq, erase_cons_head, diff_cons]
else by simpa only [erase_cons_head, erase_cons_tail _ heq, diff_cons,
erase_comm a b lβ]
using erase_diff_erase_sublist_of_sublist (h.erase b)
end diff
/-! ### enum -/
theorem length_enum_from : β n (l : list Ξ±), length (enum_from n l) = length l
| n [] := rfl
| n (a::l) := congr_arg nat.succ (length_enum_from _ _)
theorem length_enum : β (l : list Ξ±), length (enum l) = length l := length_enum_from _
@[simp] theorem enum_from_nth : β n (l : list Ξ±) m,
nth (enum_from n l) m = (Ξ» a, (n + m, a)) <$> nth l m
| n [] m := rfl
| n (a :: l) 0 := rfl
| n (a :: l) (m+1) := (enum_from_nth (n+1) l m).trans $
by rw [add_right_comm]; refl
@[simp] theorem enum_nth : β (l : list Ξ±) n,
nth (enum l) n = (Ξ» a, (n, a)) <$> nth l n :=
by simp only [enum, enum_from_nth, zero_add]; intros; refl
@[simp] theorem enum_from_map_snd : β n (l : list Ξ±),
map prod.snd (enum_from n l) = l
| n [] := rfl
| n (a :: l) := congr_arg (cons _) (enum_from_map_snd _ _)
@[simp] theorem enum_map_snd : β (l : list Ξ±),
map prod.snd (enum l) = l := enum_from_map_snd _
theorem mem_enum_from {x : Ξ±} {i : β} :
β {j : β} (xs : list Ξ±), (i, x) β xs.enum_from j β j β€ i β§ i < j + xs.length β§ x β xs
| j [] := by simp [enum_from]
| j (y :: ys) :=
suffices i = j β§ x = y β¨ (i, x) β enum_from (j + 1) ys β
j β€ i β§ i < j + (length ys + 1) β§ (x = y β¨ x β ys),
by simpa [enum_from, mem_enum_from ys],
begin
rintro (h|h),
{ refine β¨le_of_eq h.1.symm,h.1 βΈ _,or.inl h.2β©,
apply nat.lt_add_of_pos_right; simp },
{ obtain β¨hji, hijlen, hmemβ© := mem_enum_from _ h,
refine β¨_, _, _β©,
{ exact le_trans (nat.le_succ _) hji },
{ convert hijlen using 1, ac_refl },
{ simp [hmem] } }
end
@[simp] lemma enum_nil : enum ([] : list Ξ±) = [] := rfl
@[simp] lemma enum_from_nil (n : β) : enum_from n ([] : list Ξ±) = [] := rfl
@[simp] lemma enum_from_cons (x : Ξ±) (xs : list Ξ±) (n : β) :
enum_from n (x :: xs) = (n, x) :: enum_from (n + 1) xs := rfl
@[simp] lemma enum_cons (x : Ξ±) (xs : list Ξ±) :
enum (x :: xs) = (0, x) :: enum_from 1 xs := rfl
@[simp] lemma enum_from_singleton (x : Ξ±) (n : β) :
enum_from n [x] = [(n, x)] := rfl
@[simp] lemma enum_singleton (x : Ξ±) :
enum [x] = [(0, x)] := rfl
lemma enum_from_append (xs ys : list Ξ±) (n : β) :
enum_from n (xs ++ ys) = enum_from n xs ++ enum_from (n + xs.length) ys :=
begin
induction xs with x xs IH generalizing ys n,
{ simp },
{ rw [cons_append, enum_from_cons, IH, βcons_append, βenum_from_cons,
length, add_right_comm, add_assoc] }
end
lemma enum_append (xs ys : list Ξ±) :
enum (xs ++ ys) = enum xs ++ enum_from xs.length ys :=
by simp [enum, enum_from_append]
lemma map_fst_add_enum_from_eq_enum_from (l : list Ξ±) (n k : β) :
map (prod.map (+ n) id) (enum_from k l) = enum_from (n + k) l :=
begin
induction l with hd tl IH generalizing n k,
{ simp [enum_from] },
{ simp only [enum_from, map, zero_add, prod.map_mk, id.def,
eq_self_iff_true, true_and],
simp [IH, add_comm n k, add_assoc, add_left_comm] }
end
lemma map_fst_add_enum_eq_enum_from (l : list Ξ±) (n : β) :
map (prod.map (+ n) id) (enum l) = enum_from n l :=
map_fst_add_enum_from_eq_enum_from l _ _
lemma nth_le_enum_from (l : list Ξ±) (n i : β)
(hi' : i < (l.enum_from n).length)
(hi : i < l.length := by simpa [length_enum_from] using hi') :
(l.enum_from n).nth_le i hi' = (n + i, l.nth_le i hi) :=
begin
rw [βoption.some_inj, βnth_le_nth],
simp [enum_from_nth, nth_le_nth hi]
end
lemma nth_le_enum (l : list Ξ±) (i : β)
(hi' : i < l.enum.length)
(hi : i < l.length := by simpa [length_enum] using hi') :
l.enum.nth_le i hi' = (i, l.nth_le i hi) :=
by { convert nth_le_enum_from _ _ _ hi', exact (zero_add _).symm }
section choose
variables (p : Ξ± β Prop) [decidable_pred p] (l : list Ξ±)
lemma choose_spec (hp : β a, a β l β§ p a) : choose p l hp β l β§ p (choose p l hp) :=
(choose_x p l hp).property
lemma choose_mem (hp : β a, a β l β§ p a) : choose p l hp β l := (choose_spec _ _ _).1
lemma choose_property (hp : β a, a β l β§ p a) : p (choose p l hp) := (choose_spec _ _ _).2
end choose
/-! ### mapβ_left' -/
section mapβ_left'
-- The definitional equalities for `mapβ_left'` can already be used by the
-- simplifie because `mapβ_left'` is marked `@[simp]`.
@[simp] theorem mapβ_left'_nil_right (f : Ξ± β option Ξ² β Ξ³) (as) :
mapβ_left' f as [] = (as.map (Ξ» a, f a none), []) :=
by cases as; refl
end mapβ_left'
/-! ### mapβ_right' -/
section mapβ_right'
variables (f : option Ξ± β Ξ² β Ξ³) (a : Ξ±) (as : list Ξ±) (b : Ξ²) (bs : list Ξ²)
@[simp] theorem mapβ_right'_nil_left :
mapβ_right' f [] bs = (bs.map (f none), []) :=
by cases bs; refl
@[simp] theorem mapβ_right'_nil_right :
mapβ_right' f as [] = ([], as) :=
rfl
@[simp] theorem mapβ_right'_nil_cons :
mapβ_right' f [] (b :: bs) = (f none b :: bs.map (f none), []) :=
rfl
@[simp] theorem mapβ_right'_cons_cons :
mapβ_right' f (a :: as) (b :: bs) =
let rec := mapβ_right' f as bs in
(f (some a) b :: rec.fst, rec.snd) :=
rfl
end mapβ_right'
/-! ### zip_left' -/
section zip_left'
variables (a : Ξ±) (as : list Ξ±) (b : Ξ²) (bs : list Ξ²)
@[simp] theorem zip_left'_nil_right :
zip_left' as ([] : list Ξ²) = (as.map (Ξ» a, (a, none)), []) :=
by cases as; refl
@[simp] theorem zip_left'_nil_left :
zip_left' ([] : list Ξ±) bs = ([], bs) :=
rfl
@[simp] theorem zip_left'_cons_nil :
zip_left' (a :: as) ([] : list Ξ²) = ((a, none) :: as.map (Ξ» a, (a, none)), []) :=
rfl
@[simp] theorem zip_left'_cons_cons :
zip_left' (a :: as) (b :: bs) =
let rec := zip_left' as bs in
((a, some b) :: rec.fst, rec.snd) :=
rfl
end zip_left'
/-! ### zip_right' -/
section zip_right'
variables (a : Ξ±) (as : list Ξ±) (b : Ξ²) (bs : list Ξ²)
@[simp] theorem zip_right'_nil_left :
zip_right' ([] : list Ξ±) bs = (bs.map (Ξ» b, (none, b)), []) :=
by cases bs; refl
@[simp] theorem zip_right'_nil_right :
zip_right' as ([] : list Ξ²) = ([], as) :=
rfl
@[simp] theorem zip_right'_nil_cons :
zip_right' ([] : list Ξ±) (b :: bs) = ((none, b) :: bs.map (Ξ» b, (none, b)), []) :=
rfl
@[simp] theorem zip_right'_cons_cons :
zip_right' (a :: as) (b :: bs) =
let rec := zip_right' as bs in
((some a, b) :: rec.fst, rec.snd) :=
rfl
end zip_right'
/-! ### mapβ_left -/
section mapβ_left
variables (f : Ξ± β option Ξ² β Ξ³) (as : list Ξ±)
-- The definitional equalities for `mapβ_left` can already be used by the
-- simplifier because `mapβ_left` is marked `@[simp]`.
@[simp] theorem mapβ_left_nil_right :
mapβ_left f as [] = as.map (Ξ» a, f a none) :=
by cases as; refl
theorem mapβ_left_eq_mapβ_left' : β as bs,
mapβ_left f as bs = (mapβ_left' f as bs).fst
| [] bs := by simp!
| (a :: as) [] := by simp!
| (a :: as) (b :: bs) := by simp! [*]
theorem mapβ_left_eq_mapβ : β as bs,
length as β€ length bs β
mapβ_left f as bs = mapβ (Ξ» a b, f a (some b)) as bs
| [] [] h := by simp!
| [] (b :: bs) h := by simp!
| (a :: as) [] h := by { simp at h, contradiction }
| (a :: as) (b :: bs) h := by { simp at h, simp! [*] }
end mapβ_left
/-! ### mapβ_right -/
section mapβ_right
variables (f : option Ξ± β Ξ² β Ξ³) (a : Ξ±) (as : list Ξ±) (b : Ξ²) (bs : list Ξ²)
@[simp] theorem mapβ_right_nil_left :
mapβ_right f [] bs = bs.map (f none) :=
by cases bs; refl
@[simp] theorem mapβ_right_nil_right :
mapβ_right f as [] = [] :=
rfl
@[simp] theorem mapβ_right_nil_cons :
mapβ_right f [] (b :: bs) = f none b :: bs.map (f none) :=
rfl
@[simp] theorem mapβ_right_cons_cons :
mapβ_right f (a :: as) (b :: bs) = f (some a) b :: mapβ_right f as bs :=
rfl
theorem mapβ_right_eq_mapβ_right' :
mapβ_right f as bs = (mapβ_right' f as bs).fst :=
by simp only [mapβ_right, mapβ_right', mapβ_left_eq_mapβ_left']
theorem mapβ_right_eq_mapβ (h : length bs β€ length as) :
mapβ_right f as bs = mapβ (Ξ» a b, f (some a) b) as bs :=
begin
have : (Ξ» a b, flip f a (some b)) = (flip (Ξ» a b, f (some a) b)) := rfl,
simp only [mapβ_right, mapβ_left_eq_mapβ, mapβ_flip, *]
end
end mapβ_right
/-! ### zip_left -/
section zip_left
variables (a : Ξ±) (as : list Ξ±) (b : Ξ²) (bs : list Ξ²)
@[simp] theorem zip_left_nil_right :
zip_left as ([] : list Ξ²) = as.map (Ξ» a, (a, none)) :=
by cases as; refl
@[simp] theorem zip_left_nil_left :
zip_left ([] : list Ξ±) bs = [] :=
rfl
@[simp] theorem zip_left_cons_nil :
zip_left (a :: as) ([] : list Ξ²) = (a, none) :: as.map (Ξ» a, (a, none)) :=
rfl
@[simp] theorem zip_left_cons_cons :
zip_left (a :: as) (b :: bs) = (a, some b) :: zip_left as bs :=
rfl
theorem zip_left_eq_zip_left' :
zip_left as bs = (zip_left' as bs).fst :=
by simp only [zip_left, zip_left', mapβ_left_eq_mapβ_left']
end zip_left
/-! ### zip_right -/
section zip_right
variables (a : Ξ±) (as : list Ξ±) (b : Ξ²) (bs : list Ξ²)
@[simp] theorem zip_right_nil_left :
zip_right ([] : list Ξ±) bs = bs.map (Ξ» b, (none, b)) :=
by cases bs; refl
@[simp] theorem zip_right_nil_right :
zip_right as ([] : list Ξ²) = [] :=
rfl
@[simp] theorem zip_right_nil_cons :
zip_right ([] : list Ξ±) (b :: bs) = (none, b) :: bs.map (Ξ» b, (none, b)) :=
rfl
@[simp] theorem zip_right_cons_cons :
zip_right (a :: as) (b :: bs) = (some a, b) :: zip_right as bs :=
rfl
theorem zip_right_eq_zip_right' :
zip_right as bs = (zip_right' as bs).fst :=
by simp only [zip_right, zip_right', mapβ_right_eq_mapβ_right']
end zip_right
/-! ### to_chunks -/
section to_chunks
@[simp] theorem to_chunks_nil (n) : @to_chunks Ξ± n [] = [] := by cases n; refl
theorem to_chunks_aux_eq (n) : β xs i,
@to_chunks_aux Ξ± n xs i = (xs.take i, (xs.drop i).to_chunks (n+1))
| [] i := by cases i; refl
| (x::xs) 0 := by rw [to_chunks_aux, drop, to_chunks]; cases to_chunks_aux n xs n; refl
| (x::xs) (i+1) := by rw [to_chunks_aux, to_chunks_aux_eq]; refl
theorem to_chunks_eq_cons' (n) : β {xs : list Ξ±} (h : xs β []),
xs.to_chunks (n+1) = xs.take (n+1) :: (xs.drop (n+1)).to_chunks (n+1)
| [] e := (e rfl).elim
| (x::xs) _ := by rw [to_chunks, to_chunks_aux_eq]; refl
theorem to_chunks_eq_cons : β {n} {xs : list Ξ±} (n0 : n β 0) (x0 : xs β []),
xs.to_chunks n = xs.take n :: (xs.drop n).to_chunks n
| 0 _ e := (e rfl).elim
| (n+1) xs _ := to_chunks_eq_cons' _
theorem to_chunks_aux_join {n} : β {xs i l L}, @to_chunks_aux Ξ± n xs i = (l, L) β l ++ L.join = xs
| [] _ _ _ rfl := rfl
| (x::xs) i l L e := begin
cases i; [
cases e' : to_chunks_aux n xs n with l L,
cases e' : to_chunks_aux n xs i with l L];
{ rw [to_chunks_aux, e', to_chunks_aux] at e, cases e,
exact (congr_arg (cons x) (to_chunks_aux_join e') : _) }
end
@[simp] theorem to_chunks_join : β n xs, (@to_chunks Ξ± n xs).join = xs
| n [] := by cases n; refl
| 0 (x::xs) := by simp only [to_chunks, join]; rw append_nil
| (n+1) (x::xs) := begin
rw to_chunks,
cases e : to_chunks_aux n xs n with l L,
exact (congr_arg (cons x) (to_chunks_aux_join e) : _),
end
theorem to_chunks_length_le : β n xs, n β 0 β β l : list Ξ±,
l β @to_chunks Ξ± n xs β l.length β€ n
| 0 _ e _ := (e rfl).elim
| (n+1) xs _ l := begin
refine (measure_wf length).induction xs _, intros xs IH h,
by_cases x0 : xs = [], {subst xs, cases h},
rw to_chunks_eq_cons' _ x0 at h, rcases h with rfl|h,
{ apply length_take_le },
{ refine IH _ _ h,
simp only [measure, inv_image, length_drop],
exact tsub_lt_self (length_pos_iff_ne_nil.2 x0) (succ_pos _) },
end
end to_chunks
/-! ### allβ -/
section allβ
variables {p q : Ξ± β Prop} {l : list Ξ±}
@[simp] lemma allβ_cons (p : Ξ± β Prop) (x : Ξ±) : β (l : list Ξ±), allβ p (x :: l) β p x β§ allβ p l
| [] := (and_true _).symm
| (x :: l) := iff.rfl
lemma allβ_iff_forall : β {l : list Ξ±}, allβ p l β β x β l, p x
| [] := (iff_true_intro $ ball_nil _).symm
| (x :: l) := by rw [ball_cons, allβ_cons, allβ_iff_forall]
lemma allβ.imp (h : β x, p x β q x) : β {l : list Ξ±}, allβ p l β allβ q l
| [] := id
| (x :: l) := by simpa using and.imp (h x) allβ.imp
@[simp] lemma allβ_map_iff {p : Ξ² β Prop} (f : Ξ± β Ξ²) : allβ p (l.map f) β allβ (p β f) l :=
by induction l; simp *
instance (p : Ξ± β Prop) [decidable_pred p] : decidable_pred (allβ p) :=
Ξ» l, decidable_of_iff' _ allβ_iff_forall
end allβ
/-! ### Retroattributes
The list definitions happen earlier than `to_additive`, so here we tag the few multiplicative
definitions that couldn't be tagged earlier.
-/
attribute [to_additive] list.prod -- `list.sum`
attribute [to_additive] alternating_prod -- `list.alternating_sum`
/-! ### Miscellaneous lemmas -/
lemma last_reverse {l : list Ξ±} (hl : l.reverse β [])
(hl' : 0 < l.length := by { contrapose! hl, simpa [length_eq_zero] using hl }) :
l.reverse.last hl = l.nth_le 0 hl' :=
begin
rw [last_eq_nth_le, nth_le_reverse'],
{ simp, },
{ simpa using hl' }
end
theorem ilast'_mem : β a l, @ilast' Ξ± a l β a :: l
| a [] := or.inl rfl
| a (b::l) := or.inr (ilast'_mem b l)
@[simp] lemma nth_le_attach (L : list Ξ±) (i) (H : i < L.attach.length) :
(L.attach.nth_le i H).1 = L.nth_le i (length_attach L βΈ H) :=
calc (L.attach.nth_le i H).1
= (L.attach.map subtype.val).nth_le i (by simpa using H) : by rw nth_le_map'
... = L.nth_le i _ : by congr; apply attach_map_val
@[simp]
theorem mem_map_swap (x : Ξ±) (y : Ξ²) (xs : list (Ξ± Γ Ξ²)) :
(y, x) β map prod.swap xs β (x, y) β xs :=
begin
induction xs with x xs,
{ simp only [not_mem_nil, map_nil] },
{ cases x with a b,
simp only [mem_cons_iff, prod.mk.inj_iff, map, prod.swap_prod_mk,
prod.exists, xs_ih, and_comm] },
end
lemma slice_eq (xs : list Ξ±) (n m : β) :
slice n m xs = xs.take n ++ xs.drop (n+m) :=
begin
induction n generalizing xs,
{ simp [slice] },
{ cases xs; simp [slice, *, nat.succ_add], }
end
lemma sizeof_slice_lt [has_sizeof Ξ±] (i j : β) (hj : 0 < j) (xs : list Ξ±) (hi : i < xs.length) :
sizeof (list.slice i j xs) < sizeof xs :=
begin
induction xs generalizing i j,
case list.nil : i j h
{ cases hi },
case list.cons : x xs xs_ih i j h
{ cases i; simp only [-slice_eq, list.slice],
{ cases j, cases h,
dsimp only [drop], unfold_wf,
apply @lt_of_le_of_lt _ _ _ xs.sizeof,
{ clear_except,
induction xs generalizing j; unfold_wf,
case list.nil : j
{ refl },
case list.cons : xs_hd xs_tl xs_ih j
{ cases j; unfold_wf, refl,
transitivity, apply xs_ih,
simp }, },
unfold_wf, },
{ unfold_wf, apply xs_ih _ _ h,
apply lt_of_succ_lt_succ hi, } },
end
/-! ### nthd and inth -/
section nthd
variables (l : list Ξ±) (x : Ξ±) (xs : list Ξ±) (d : Ξ±) (n : β)
@[simp] lemma nthd_nil : nthd d [] n = d := rfl
@[simp] lemma nthd_cons_zero : nthd d (x::xs) 0 = x := rfl
@[simp] lemma nthd_cons_succ : nthd d (x::xs) (n + 1) = nthd d xs n := rfl
lemma nthd_eq_nth_le {n : β} (hn : n < l.length) : l.nthd d n = l.nth_le n hn :=
begin
induction l with hd tl IH generalizing n,
{ exact absurd hn (not_lt_of_ge (nat.zero_le _)) },
{ cases n,
{ exact nthd_cons_zero _ _ _ },
{ exact IH _ } }
end
lemma nthd_eq_default {n : β} (hn : l.length β€ n) : l.nthd d n = d :=
begin
induction l with hd tl IH generalizing n,
{ exact nthd_nil _ _ },
{ cases n,
{ refine absurd (nat.zero_lt_succ _) (not_lt_of_ge hn) },
{ exact IH (nat.le_of_succ_le_succ hn) } }
end
/-- An empty list can always be decidably checked for the presence of an element.
Not an instance because it would clash with `decidable_eq Ξ±`. -/
def decidable_nthd_nil_ne {Ξ±} (a : Ξ±) : decidable_pred
(Ξ» (i : β), nthd a ([] : list Ξ±) i β a) := Ξ» i, is_false $ Ξ» H, H (nthd_nil _ _)
@[simp] lemma nthd_singleton_default_eq (n : β) : [d].nthd d n = d :=
by { cases n; simp }
@[simp] lemma nthd_repeat_default_eq (r n : β) : (repeat d r).nthd d n = d :=
begin
induction r with r IH generalizing n,
{ simp },
{ cases n;
simp [IH] }
end
lemma nthd_append (l l' : list Ξ±) (d : Ξ±) (n : β) (h : n < l.length)
(h' : n < (l ++ l').length := h.trans_le ((length_append l l').symm βΈ le_self_add)) :
(l ++ l').nthd d n = l.nthd d n :=
by rw [nthd_eq_nth_le _ _ h', nth_le_append h' h, nthd_eq_nth_le]
lemma nthd_append_right (l l' : list Ξ±) (d : Ξ±) (n : β) (h : l.length β€ n) :
(l ++ l').nthd d n = l'.nthd d (n - l.length) :=
begin
cases lt_or_le _ _ with h' h',
{ rw [nthd_eq_nth_le _ _ h', nth_le_append_right h h', nthd_eq_nth_le] },
{ rw [nthd_eq_default _ _ h', nthd_eq_default],
rwa [le_tsub_iff_left h, βlength_append] }
end
lemma nthd_eq_get_or_else_nth (n : β) :
l.nthd d n = (l.nth n).get_or_else d :=
begin
cases lt_or_le _ _ with h h,
{ rw [nthd_eq_nth_le _ _ h, nth_le_nth h, option.get_or_else_some] },
{ rw [nthd_eq_default _ _ h, nth_eq_none_iff.mpr h, option.get_or_else_none] }
end
end nthd
section inth
variables [inhabited Ξ±] (l : list Ξ±) (x : Ξ±) (xs : list Ξ±) (n : β)
@[simp] lemma inth_nil : inth ([] : list Ξ±) n = default := rfl
@[simp] lemma inth_cons_zero : inth (x::xs) 0 = x := rfl
@[simp] lemma inth_cons_succ : inth (x::xs) (n + 1) = inth xs n := rfl
lemma inth_eq_nth_le {n : β} (hn : n < l.length) : l.inth n = l.nth_le n hn := nthd_eq_nth_le _ _ _
lemma inth_eq_default {n : β} (hn : l.length β€ n) : l.inth n = default := nthd_eq_default _ _ hn
lemma nthd_default_eq_inth : l.nthd default = l.inth := rfl
lemma inth_append (l l' : list Ξ±) (n : β) (h : n < l.length)
(h' : n < (l ++ l').length := h.trans_le ((length_append l l').symm βΈ le_self_add)) :
(l ++ l').inth n = l.inth n :=
nthd_append _ _ _ _ h h'
lemma inth_append_right (l l' : list Ξ±) (n : β) (h : l.length β€ n) :
(l ++ l').inth n = l'.inth (n - l.length) :=
nthd_append_right _ _ _ _ h
lemma inth_eq_iget_nth (n : β) :
l.inth n = (l.nth n).iget :=
by rw [βnthd_default_eq_inth, nthd_eq_get_or_else_nth, option.get_or_else_default_eq_iget]
lemma inth_zero_eq_head : l.inth 0 = l.head :=
by { cases l; refl, }
end inth
end list
|
203f1f4424155f242fb12b6428dc1d338a20d886 | 217bb195841a8be2d1b4edd2084d6b69ccd62f50 | /library/init/data/ordering/basic.lean | 10771741482ff7dc5e3c239f0fc335d2e6f50308 | [
"Apache-2.0"
] | permissive | frank-lesser/lean4 | 717f56c9bacd5bf3a67542d2f5cea721d4743a30 | 79e2abe33f73162f773ea731265e456dbfe822f9 | refs/heads/master | 1,589,741,267,933 | 1,556,424,200,000 | 1,556,424,281,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,624 | lean | /-
Copyright (c) 2016 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
import init.data.repr
universes u v
inductive Ordering
| lt | Eq | gt
instance : HasRepr Ordering :=
β¨(Ξ» s, match s with | Ordering.lt := "lt" | Ordering.Eq := "Eq" | Ordering.gt := "gt")β©
namespace Ordering
def swap : Ordering β Ordering
| lt := gt
| Eq := Eq
| gt := lt
@[inline] def orElse : Ordering β Ordering β Ordering
| lt _ := lt
| Eq o := o
| gt _ := gt
theorem swapSwap : β (o : Ordering), o.swap.swap = o
| lt := rfl
| Eq := rfl
| gt := rfl
end Ordering
@[inline] def cmpUsing {Ξ± : Type u} (lt : Ξ± β Ξ± β Prop) [DecidableRel lt] (a b : Ξ±) : Ordering :=
if lt a b then Ordering.lt
else if lt b a then Ordering.gt
else Ordering.Eq
def cmp {Ξ± : Type u} [HasLess Ξ±] [DecidableRel ((<) : Ξ± β Ξ± β Prop)] (a b : Ξ±) : Ordering :=
cmpUsing (<) a b
instance : DecidableEq Ordering :=
{decEq := Ξ» a b,
match a with
| Ordering.lt :=
(match b with
| Ordering.lt := isTrue rfl
| Ordering.Eq := isFalse (Ξ» h, Ordering.noConfusion h)
| Ordering.gt := isFalse (Ξ» h, Ordering.noConfusion h))
| Ordering.Eq :=
(match b with
| Ordering.lt := isFalse (Ξ» h, Ordering.noConfusion h)
| Ordering.Eq := isTrue rfl
| Ordering.gt := isFalse (Ξ» h, Ordering.noConfusion h))
| Ordering.gt :=
match b with
| Ordering.lt := isFalse (Ξ» h, Ordering.noConfusion h)
| Ordering.Eq := isFalse (Ξ» h, Ordering.noConfusion h)
| Ordering.gt := isTrue rfl}
|
35b0afd671869c87956e7f59008d135448d531f9 | d9d511f37a523cd7659d6f573f990e2a0af93c6f | /src/topology/algebra/ordered/basic.lean | adfa05d7d0dac9b7afcbaff3041c24fd878f8574 | [
"Apache-2.0"
] | permissive | hikari0108/mathlib | b7ea2b7350497ab1a0b87a09d093ecc025a50dfa | a9e7d333b0cfd45f13a20f7b96b7d52e19fa2901 | refs/heads/master | 1,690,483,608,260 | 1,631,541,580,000 | 1,631,541,580,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 189,662 | 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 algebra.group_with_zero.power
import data.set.intervals.pi
import order.filter.interval
import topology.algebra.group
import tactic.linarith
import tactic.tfae
/-!
# Theory of topology on ordered spaces
## Main definitions
The order topology on an ordered space is the topology generated by all open intervals (or
equivalently by those of the form `(-β, a)` and `(b, +β)`). We define it as `preorder.topology Ξ±`.
However, we do *not* register it as an instance (as many existing ordered types already have
topologies, which would be equal but not definitionally equal to `preorder.topology Ξ±`). Instead,
we introduce a class `order_topology Ξ±` (which is a `Prop`, also known as a mixin) saying that on
the type `Ξ±` having already a topological space structure and a preorder structure, the topological
structure is equal to the order topology.
We also introduce another (mixin) class `order_closed_topology Ξ±` saying that the set of points
`(x, y)` with `x β€ y` is closed in the product space. This is automatically satisfied on a linear
order with the order topology.
We prove many basic properties of such topologies.
## Main statements
This file contains the proofs of the following facts. For exact requirements
(`order_closed_topology` vs `order_topology`, `preorder` vs `partial_order` vs `linear_order` etc)
see their statements.
### Open / closed sets
* `is_open_lt` : if `f` and `g` are continuous functions, then `{x | f x < g x}` is open;
* `is_open_Iio`, `is_open_Ioi`, `is_open_Ioo` : open intervals are open;
* `is_closed_le` : if `f` and `g` are continuous functions, then `{x | f x β€ g x}` is closed;
* `is_closed_Iic`, `is_closed_Ici`, `is_closed_Icc` : closed intervals are closed;
* `frontier_le_subset_eq`, `frontier_lt_subset_eq` : frontiers of both `{x | f x β€ g x}`
and `{x | f x < g x}` are included by `{x | f x = g x}`;
* `exists_Ioc_subset_of_mem_nhds`, `exists_Ico_subset_of_mem_nhds` : if `x < y`, then any
neighborhood of `x` includes an interval `[x, z)` for some `z β (x, y]`, and any neighborhood
of `y` includes an interval `(z, y]` for some `z β [x, y)`.
### Convergence and inequalities
* `le_of_tendsto_of_tendsto` : if `f` converges to `a`, `g` converges to `b`, and eventually
`f x β€ g x`, then `a β€ b`
* `le_of_tendsto`, `ge_of_tendsto` : if `f` converges to `a` and eventually `f x β€ b`
(resp., `b β€ f x`), then `a β€ b` (resp., `b β€ a); we also provide primed versions
that assume the inequalities to hold for all `x`.
### Min, max, `Sup` and `Inf`
* `continuous.min`, `continuous.max`: pointwise `min`/`max` of two continuous functions is
continuous.
* `tendsto.min`, `tendsto.max` : if `f` tends to `a` and `g` tends to `b`, then their pointwise
`min`/`max` tend to `min a b` and `max a b`, respectively.
* `tendsto_of_tendsto_of_tendsto_of_le_of_le` : theorem known as squeeze theorem,
sandwich theorem, theorem of Carabinieri, and two policemen (and a drunk) theorem; if `g` and `h`
both converge to `a`, and eventually `g x β€ f x β€ h x`, then `f` converges to `a`.
### Connected sets and Intermediate Value Theorem
* `is_preconnected_I??` : all intervals `I??` are preconnected,
* `is_preconnected.intermediate_value`, `intermediate_value_univ` : Intermediate Value Theorem for
connected sets and connected spaces, respectively;
* `intermediate_value_Icc`, `intermediate_value_Icc'`: Intermediate Value Theorem for functions
on closed intervals.
### Miscellaneous facts
* `is_compact.exists_forall_le`, `is_compact.exists_forall_ge` : extreme value theorem, a continuous
function on a compact set takes its minimum and maximum values.
* `is_closed.Icc_subset_of_forall_mem_nhds_within` : βContinuous inductionβ principle;
if `s β© [a, b]` is closed, `a β s`, and for each `x β [a, b) β© s` some of its right neighborhoods
is included `s`, then `[a, b] β s`.
* `is_closed.Icc_subset_of_forall_exists_gt`, `is_closed.mem_of_ge_of_forall_exists_gt` : two
other versions of the βcontinuous inductionβ principle.
## Implementation
We do _not_ register the order topology as an instance on a preorder (or even on a linear order).
Indeed, on many such spaces, a topology has already been constructed in a different way (think
of the discrete spaces `β` or `β€`, or `β` that could inherit a topology as the completion of `β`),
and is in general not defeq to the one generated by the intervals. We make it available as a
definition `preorder.topology Ξ±` though, that can be registered as an instance when necessary, or
for specific types.
-/
open classical set filter topological_space
open function
open_locale topological_space classical filter
universes u v w
variables {Ξ± : Type u} {Ξ² : Type v} {Ξ³ : Type w}
/-- A topology on a set which is both a topological space and a preorder is _order-closed_ if the
set of points `(x, y)` with `x β€ y` is closed in the product space. We introduce this as a mixin.
This property is satisfied for the order topology on a linear order, but it can be satisfied more
generally, and suffices to derive many interesting properties relating order and topology. -/
class order_closed_topology (Ξ± : Type*) [topological_space Ξ±] [preorder Ξ±] : Prop :=
(is_closed_le' : is_closed {p:Ξ±ΓΞ± | p.1 β€ p.2})
instance : Ξ [topological_space Ξ±], topological_space (order_dual Ξ±) := id
instance [topological_space Ξ±] [h : first_countable_topology Ξ±] :
first_countable_topology (order_dual Ξ±) := h
@[to_additive]
instance [topological_space Ξ±] [has_mul Ξ±] [h : has_continuous_mul Ξ±] :
has_continuous_mul (order_dual Ξ±) := h
section order_closed_topology
section preorder
variables [topological_space Ξ±] [preorder Ξ±] [t : order_closed_topology Ξ±]
include t
namespace subtype
instance {p : Ξ± β Prop} : order_closed_topology (subtype p) :=
have this : continuous (Ξ» (p : (subtype p) Γ (subtype p)), ((p.fst : Ξ±), (p.snd : Ξ±))) :=
(continuous_subtype_coe.comp continuous_fst).prod_mk
(continuous_subtype_coe.comp continuous_snd),
order_closed_topology.mk (t.is_closed_le'.preimage this)
end subtype
lemma is_closed_le_prod : is_closed {p : Ξ± Γ Ξ± | p.1 β€ p.2} :=
t.is_closed_le'
lemma is_closed_le [topological_space Ξ²] {f g : Ξ² β Ξ±} (hf : continuous f) (hg : continuous g) :
is_closed {b | f b β€ g b} :=
continuous_iff_is_closed.mp (hf.prod_mk hg) _ is_closed_le_prod
lemma is_closed_le' (a : Ξ±) : is_closed {b | b β€ a} :=
is_closed_le continuous_id continuous_const
lemma is_closed_Iic {a : Ξ±} : is_closed (Iic a) :=
is_closed_le' a
lemma is_closed_ge' (a : Ξ±) : is_closed {b | a β€ b} :=
is_closed_le continuous_const continuous_id
lemma is_closed_Ici {a : Ξ±} : is_closed (Ici a) :=
is_closed_ge' a
instance : order_closed_topology (order_dual Ξ±) :=
β¨(@order_closed_topology.is_closed_le' Ξ± _ _ _).preimage continuous_swapβ©
lemma is_closed_Icc {a b : Ξ±} : is_closed (Icc a b) :=
is_closed.inter is_closed_Ici is_closed_Iic
@[simp] lemma closure_Icc (a b : Ξ±) : closure (Icc a b) = Icc a b :=
is_closed_Icc.closure_eq
@[simp] lemma closure_Iic (a : Ξ±) : closure (Iic a) = Iic a :=
is_closed_Iic.closure_eq
@[simp] lemma closure_Ici (a : Ξ±) : closure (Ici a) = Ici a :=
is_closed_Ici.closure_eq
lemma le_of_tendsto_of_tendsto {f g : Ξ² β Ξ±} {b : filter Ξ²} {aβ aβ : Ξ±} [ne_bot b]
(hf : tendsto f b (π aβ)) (hg : tendsto g b (π aβ)) (h : f β€αΆ [b] g) :
aβ β€ aβ :=
have tendsto (Ξ»b, (f b, g b)) b (π (aβ, aβ)),
by rw [nhds_prod_eq]; exact hf.prod_mk hg,
show (aβ, aβ) β {p:Ξ±ΓΞ± | p.1 β€ p.2},
from t.is_closed_le'.mem_of_tendsto this h
lemma le_of_tendsto_of_tendsto' {f g : Ξ² β Ξ±} {b : filter Ξ²} {aβ aβ : Ξ±} [ne_bot b]
(hf : tendsto f b (π aβ)) (hg : tendsto g b (π aβ)) (h : β x, f x β€ g x) :
aβ β€ aβ :=
le_of_tendsto_of_tendsto hf hg (eventually_of_forall h)
lemma le_of_tendsto {f : Ξ² β Ξ±} {a b : Ξ±} {x : filter Ξ²}
[ne_bot x] (lim : tendsto f x (π a)) (h : βαΆ c in x, f c β€ b) : a β€ b :=
le_of_tendsto_of_tendsto lim tendsto_const_nhds h
lemma le_of_tendsto' {f : Ξ² β Ξ±} {a b : Ξ±} {x : filter Ξ²}
[ne_bot x] (lim : tendsto f x (π a)) (h : β c, f c β€ b) : a β€ b :=
le_of_tendsto lim (eventually_of_forall h)
lemma ge_of_tendsto {f : Ξ² β Ξ±} {a b : Ξ±} {x : filter Ξ²} [ne_bot x]
(lim : tendsto f x (π a)) (h : βαΆ c in x, b β€ f c) : b β€ a :=
le_of_tendsto_of_tendsto tendsto_const_nhds lim h
lemma ge_of_tendsto' {f : Ξ² β Ξ±} {a b : Ξ±} {x : filter Ξ²} [ne_bot x]
(lim : tendsto f x (π a)) (h : β c, b β€ f c) : b β€ a :=
ge_of_tendsto lim (eventually_of_forall h)
@[simp]
lemma closure_le_eq [topological_space Ξ²] {f g : Ξ² β Ξ±} (hf : continuous f) (hg : continuous g) :
closure {b | f b β€ g b} = {b | f b β€ g b} :=
(is_closed_le hf hg).closure_eq
lemma closure_lt_subset_le [topological_space Ξ²] {f g : Ξ² β Ξ±} (hf : continuous f)
(hg : continuous g) :
closure {b | f b < g b} β {b | f b β€ g b} :=
by { rw [βclosure_le_eq hf hg], exact closure_mono (Ξ» b, le_of_lt) }
lemma continuous_within_at.closure_le [topological_space Ξ²]
{f g : Ξ² β Ξ±} {s : set Ξ²} {x : Ξ²} (hx : x β closure s)
(hf : continuous_within_at f s x)
(hg : continuous_within_at g s x)
(h : β y β s, f y β€ g y) : f x β€ g x :=
show (f x, g x) β {p : Ξ± Γ Ξ± | p.1 β€ p.2},
from order_closed_topology.is_closed_le'.closure_subset ((hf.prod hg).mem_closure hx h)
/-- If `s` is a closed set and two functions `f` and `g` are continuous on `s`,
then the set `{x β s | f x β€ g x}` is a closed set. -/
lemma is_closed.is_closed_le [topological_space Ξ²] {f g : Ξ² β Ξ±} {s : set Ξ²} (hs : is_closed s)
(hf : continuous_on f s) (hg : continuous_on g s) :
is_closed {x β s | f x β€ g x} :=
(hf.prod hg).preimage_closed_of_closed hs order_closed_topology.is_closed_le'
omit t
lemma nhds_within_Ici_ne_bot {a b : Ξ±} (Hβ : a β€ b) :
ne_bot (π[Ici a] b) :=
nhds_within_ne_bot_of_mem Hβ
@[instance] lemma nhds_within_Ici_self_ne_bot (a : Ξ±) :
ne_bot (π[Ici a] a) :=
nhds_within_Ici_ne_bot (le_refl a)
lemma nhds_within_Iic_ne_bot {a b : Ξ±} (H : a β€ b) :
ne_bot (π[Iic b] a) :=
nhds_within_ne_bot_of_mem H
@[instance] lemma nhds_within_Iic_self_ne_bot (a : Ξ±) :
ne_bot (π[Iic a] a) :=
nhds_within_Iic_ne_bot (le_refl a)
end preorder
section partial_order
variables [topological_space Ξ±] [partial_order Ξ±] [t : order_closed_topology Ξ±]
include t
private lemma is_closed_eq_aux : is_closed {p : Ξ± Γ Ξ± | p.1 = p.2} :=
by simp only [le_antisymm_iff];
exact is_closed.inter t.is_closed_le' (is_closed_le continuous_snd continuous_fst)
@[priority 90] -- see Note [lower instance priority]
instance order_closed_topology.to_t2_space : t2_space Ξ± :=
{ t2 :=
have is_open {p : Ξ± Γ Ξ± | p.1 β p.2} := is_closed_eq_aux.is_open_compl,
assume a b h,
let β¨u, v, hu, hv, ha, hb, hβ© := is_open_prod_iff.mp this a b h in
β¨u, v, hu, hv, ha, hb,
set.eq_empty_iff_forall_not_mem.2 $ assume a β¨hβ, hββ©,
have a β a, from @h (a, a) β¨hβ, hββ©,
this rflβ© }
end partial_order
section linear_order
variables [topological_space Ξ±] [linear_order Ξ±] [order_closed_topology Ξ±]
lemma is_open_lt_prod : is_open {p : Ξ± Γ Ξ± | p.1 < p.2} :=
by { simp_rw [β is_closed_compl_iff, compl_set_of, not_lt],
exact is_closed_le continuous_snd continuous_fst }
lemma is_open_lt [topological_space Ξ²] {f g : Ξ² β Ξ±} (hf : continuous f) (hg : continuous g) :
is_open {b | f b < g b} :=
by simp [lt_iff_not_ge, -not_le]; exact (is_closed_le hg hf).is_open_compl
variables {a b : Ξ±}
lemma is_open_Iio : is_open (Iio a) :=
is_open_lt continuous_id continuous_const
lemma is_open_Ioi : is_open (Ioi a) :=
is_open_lt continuous_const continuous_id
lemma is_open_Ioo : is_open (Ioo a b) :=
is_open.inter is_open_Ioi is_open_Iio
@[simp] lemma interior_Ioi : interior (Ioi a) = Ioi a :=
is_open_Ioi.interior_eq
@[simp] lemma interior_Iio : interior (Iio a) = Iio a :=
is_open_Iio.interior_eq
@[simp] lemma interior_Ioo : interior (Ioo a b) = Ioo a b :=
is_open_Ioo.interior_eq
lemma eventually_le_of_tendsto_lt {l : filter Ξ³} {f : Ξ³ β Ξ±} {u v : Ξ±} (hv : v < u)
(h : tendsto f l (π v)) : βαΆ a in l, f a β€ u :=
eventually.mono (tendsto_nhds.1 h (< u) is_open_Iio hv) (Ξ» v, le_of_lt)
lemma eventually_ge_of_tendsto_gt {l : filter Ξ³} {f : Ξ³ β Ξ±} {u v : Ξ±} (hv : u < v)
(h : tendsto f l (π v)) : βαΆ a in l, u β€ f a :=
eventually.mono (tendsto_nhds.1 h (> u) is_open_Ioi hv) (Ξ» v, le_of_lt)
variables [topological_space Ξ³]
/-- Intermediate value theorem for two functions: if `f` and `g` are two continuous functions
on a preconnected space and `f a β€ g a` and `g b β€ f b`, then for some `x` we have `f x = g x`. -/
lemma intermediate_value_univβ [preconnected_space Ξ³] {a b : Ξ³} {f g : Ξ³ β Ξ±} (hf : continuous f)
(hg : continuous g) (ha : f a β€ g a) (hb : g b β€ f b) :
β x, f x = g x :=
begin
obtain β¨x, h, hfg, hgfβ© : (univ β© {x | f x β€ g x β§ g x β€ f x}).nonempty,
from is_preconnected_closed_iff.1 preconnected_space.is_preconnected_univ _ _
(is_closed_le hf hg) (is_closed_le hg hf) (Ξ» x hx, le_total _ _) β¨a, trivial, haβ©
β¨b, trivial, hbβ©,
exact β¨x, le_antisymm hfg hgfβ©
end
lemma intermediate_value_univβ_eventuallyβ [preconnected_space Ξ³] {a : Ξ³} {l : filter Ξ³} [ne_bot l]
{f g : Ξ³ β Ξ±} (hf : continuous f) (hg : continuous g) (ha : f a β€ g a) (he : g β€αΆ [l] f) :
β x, f x = g x :=
let β¨c, hcβ© := he.frequently.exists in intermediate_value_univβ hf hg ha hc
lemma intermediate_value_univβ_eventuallyβ [preconnected_space Ξ³] {lβ lβ : filter Ξ³}
[ne_bot lβ] [ne_bot lβ] {f g : Ξ³ β Ξ±} (hf : continuous f) (hg : continuous g)
(heβ : f β€αΆ [lβ] g ) (heβ : g β€αΆ [lβ] f) :
β x, f x = g x :=
let β¨cβ, hcββ© := heβ.frequently.exists, β¨cβ, hcββ© := heβ.frequently.exists in
intermediate_value_univβ hf hg hcβ hcβ
/-- Intermediate value theorem for two functions: if `f` and `g` are two functions continuous
on a preconnected set `s` and for some `a b β s` we have `f a β€ g a` and `g b β€ f b`,
then for some `x β s` we have `f x = g x`. -/
lemma is_preconnected.intermediate_valueβ {s : set Ξ³} (hs : is_preconnected s)
{a b : Ξ³} (ha : a β s) (hb : b β s) {f g : Ξ³ β Ξ±}
(hf : continuous_on f s) (hg : continuous_on g s) (ha' : f a β€ g a) (hb' : g b β€ f b) :
β x β s, f x = g x :=
let β¨x, hxβ© := @intermediate_value_univβ Ξ± s _ _ _ _ (subtype.preconnected_space hs) β¨a, haβ© β¨b, hbβ©
_ _ (continuous_on_iff_continuous_restrict.1 hf) (continuous_on_iff_continuous_restrict.1 hg)
ha' hb'
in β¨x, x.2, hxβ©
lemma is_preconnected.intermediate_valueβ_eventuallyβ {s : set Ξ³} (hs : is_preconnected s)
{a : Ξ³} {l : filter Ξ³} (ha : a β s) [ne_bot l] (hl : l β€ π s) {f g : Ξ³ β Ξ±}
(hf : continuous_on f s) (hg : continuous_on g s) (ha' : f a β€ g a) (he : g β€αΆ [l] f) :
β x β s, f x = g x :=
begin
rw continuous_on_iff_continuous_restrict at hf hg,
obtain β¨b, hβ© := @intermediate_value_univβ_eventuallyβ _ _ _ _ _ _ (subtype.preconnected_space hs)
β¨a, haβ© _ (comap_coe_ne_bot_of_le_principal hl) _ _ hf hg ha' (eventually_comap' he),
exact β¨b, b.prop, hβ©,
end
lemma is_preconnected.intermediate_valueβ_eventuallyβ {s : set Ξ³} (hs : is_preconnected s)
{lβ lβ : filter Ξ³} [ne_bot lβ] [ne_bot lβ] (hlβ : lβ β€ π s) (hlβ : lβ β€ π s) {f g : Ξ³ β Ξ±}
(hf : continuous_on f s) (hg : continuous_on g s) (heβ : f β€αΆ [lβ] g) (heβ : g β€αΆ [lβ] f) :
β x β s, f x = g x :=
begin
rw continuous_on_iff_continuous_restrict at hf hg,
obtain β¨b, hβ© := @intermediate_value_univβ_eventuallyβ _ _ _ _ _ _ (subtype.preconnected_space hs)
_ _ (comap_coe_ne_bot_of_le_principal hlβ) (comap_coe_ne_bot_of_le_principal hlβ)
_ _ hf hg (eventually_comap' heβ) (eventually_comap' heβ),
exact β¨b, b.prop, hβ©,
end
/-- **Intermediate Value Theorem** for continuous functions on connected sets. -/
lemma is_preconnected.intermediate_value {s : set Ξ³} (hs : is_preconnected s)
{a b : Ξ³} (ha : a β s) (hb : b β s) {f : Ξ³ β Ξ±} (hf : continuous_on f s) :
Icc (f a) (f b) β f '' s :=
Ξ» x hx, mem_image_iff_bex.2 $ hs.intermediate_valueβ ha hb hf continuous_on_const hx.1 hx.2
lemma is_preconnected.intermediate_value_Ico {s : set Ξ³} (hs : is_preconnected s)
{a : Ξ³} {l : filter Ξ³} (ha : a β s) [ne_bot l] (hl : l β€ π s) {f : Ξ³ β Ξ±}
(hf : continuous_on f s) {v : Ξ±} (ht : tendsto f l (π v)) :
Ico (f a) v β f '' s :=
Ξ» y h, bex_def.1 $ hs.intermediate_valueβ_eventuallyβ ha hl
hf continuous_on_const h.1 (eventually_ge_of_tendsto_gt h.2 ht)
lemma is_preconnected.intermediate_value_Ioc {s : set Ξ³} (hs : is_preconnected s)
{a : Ξ³} {l : filter Ξ³} (ha : a β s) [ne_bot l] (hl : l β€ π s) {f : Ξ³ β Ξ±}
(hf : continuous_on f s) {v : Ξ±} (ht : tendsto f l (π v)) :
Ioc v (f a) β f '' s :=
Ξ» y h, bex_def.1 $ bex.imp_right (Ξ» x _, eq.symm) $ hs.intermediate_valueβ_eventuallyβ ha hl
continuous_on_const hf h.2 (eventually_le_of_tendsto_lt h.1 ht)
lemma is_preconnected.intermediate_value_Ioo {s : set Ξ³} (hs : is_preconnected s)
{lβ lβ : filter Ξ³} [ne_bot lβ] [ne_bot lβ] (hlβ : lβ β€ π s) (hlβ : lβ β€ π s) {f : Ξ³ β Ξ±}
(hf : continuous_on f s) {vβ vβ : Ξ±} (htβ : tendsto f lβ (π vβ)) (htβ : tendsto f lβ (π vβ)) :
Ioo vβ vβ β f '' s :=
Ξ» y h, bex_def.1 $ hs.intermediate_valueβ_eventuallyβ hlβ hlβ
hf continuous_on_const (eventually_le_of_tendsto_lt h.1 htβ) (eventually_ge_of_tendsto_gt h.2 htβ)
lemma is_preconnected.intermediate_value_Ici {s : set Ξ³} (hs : is_preconnected s)
{a : Ξ³} {l : filter Ξ³} (ha : a β s) [ne_bot l] (hl : l β€ π s) {f : Ξ³ β Ξ±}
(hf : continuous_on f s) (ht : tendsto f l at_top) :
Ici (f a) β f '' s :=
Ξ» y h, bex_def.1 $ hs.intermediate_valueβ_eventuallyβ ha hl
hf continuous_on_const h (tendsto_at_top.1 ht y)
lemma is_preconnected.intermediate_value_Iic {s : set Ξ³} (hs : is_preconnected s)
{a : Ξ³} {l : filter Ξ³} (ha : a β s) [ne_bot l] (hl : l β€ π s) {f : Ξ³ β Ξ±}
(hf : continuous_on f s) (ht : tendsto f l at_bot) :
Iic (f a) β f '' s :=
Ξ» y h, bex_def.1 $ bex.imp_right (Ξ» x _, eq.symm) $ hs.intermediate_valueβ_eventuallyβ ha hl
continuous_on_const hf h (tendsto_at_bot.1 ht y)
lemma is_preconnected.intermediate_value_Ioi {s : set Ξ³} (hs : is_preconnected s)
{lβ lβ : filter Ξ³} [ne_bot lβ] [ne_bot lβ] (hlβ : lβ β€ π s) (hlβ : lβ β€ π s) {f : Ξ³ β Ξ±}
(hf : continuous_on f s) {v : Ξ±} (htβ : tendsto f lβ (π v)) (htβ : tendsto f lβ at_top) :
Ioi v β f '' s :=
Ξ» y h, bex_def.1 $ hs.intermediate_valueβ_eventuallyβ hlβ hlβ
hf continuous_on_const (eventually_le_of_tendsto_lt h htβ) (tendsto_at_top.1 htβ y)
lemma is_preconnected.intermediate_value_Iio {s : set Ξ³} (hs : is_preconnected s)
{lβ lβ : filter Ξ³} [ne_bot lβ] [ne_bot lβ] (hlβ : lβ β€ π s) (hlβ : lβ β€ π s) {f : Ξ³ β Ξ±}
(hf : continuous_on f s) {v : Ξ±} (htβ : tendsto f lβ at_bot) (htβ : tendsto f lβ (π v)) :
Iio v β f '' s :=
Ξ» y h, bex_def.1 $ hs.intermediate_valueβ_eventuallyβ hlβ hlβ
hf continuous_on_const (tendsto_at_bot.1 htβ y) (eventually_ge_of_tendsto_gt h htβ)
lemma is_preconnected.intermediate_value_Iii {s : set Ξ³} (hs : is_preconnected s)
{lβ lβ : filter Ξ³} [ne_bot lβ] [ne_bot lβ] (hlβ : lβ β€ π s) (hlβ : lβ β€ π s) {f : Ξ³ β Ξ±}
(hf : continuous_on f s) (htβ : tendsto f lβ at_bot) (htβ : tendsto f lβ at_top) :
univ β f '' s :=
Ξ» y h, bex_def.1 $ hs.intermediate_valueβ_eventuallyβ hlβ hlβ
hf continuous_on_const (tendsto_at_bot.1 htβ y) (tendsto_at_top.1 htβ y)
/-- **Intermediate Value Theorem** for continuous functions on connected spaces. -/
lemma intermediate_value_univ [preconnected_space Ξ³] (a b : Ξ³) {f : Ξ³ β Ξ±} (hf : continuous f) :
Icc (f a) (f b) β range f :=
Ξ» x hx, intermediate_value_univβ hf continuous_const hx.1 hx.2
/-- **Intermediate Value Theorem** for continuous functions on connected spaces. -/
lemma mem_range_of_exists_le_of_exists_ge [preconnected_space Ξ³] {c : Ξ±} {f : Ξ³ β Ξ±}
(hf : continuous f) (hβ : β a, f a β€ c) (hβ : β b, c β€ f b) :
c β range f :=
let β¨a, haβ© := hβ, β¨b, hbβ© := hβ in intermediate_value_univ a b hf β¨ha, hbβ©
/-- If a preconnected set contains endpoints of an interval, then it includes the whole interval. -/
lemma is_preconnected.Icc_subset {s : set Ξ±} (hs : is_preconnected s)
{a b : Ξ±} (ha : a β s) (hb : b β s) :
Icc a b β s :=
by simpa only [image_id] using hs.intermediate_value ha hb continuous_on_id
/-- If a preconnected set contains endpoints of an interval, then it includes the whole interval. -/
lemma is_connected.Icc_subset {s : set Ξ±} (hs : is_connected s)
{a b : Ξ±} (ha : a β s) (hb : b β s) :
Icc a b β s :=
hs.2.Icc_subset ha hb
/-- If preconnected set in a linear order space is unbounded below and above, then it is the whole
space. -/
lemma is_preconnected.eq_univ_of_unbounded {s : set Ξ±} (hs : is_preconnected s) (hb : Β¬bdd_below s)
(ha : Β¬bdd_above s) :
s = univ :=
begin
refine eq_univ_of_forall (Ξ» x, _),
obtain β¨y, ys, hyβ© : β y β s, y < x := not_bdd_below_iff.1 hb x,
obtain β¨z, zs, hzβ© : β z β s, x < z := not_bdd_above_iff.1 ha x,
exact hs.Icc_subset ys zs β¨le_of_lt hy, le_of_lt hzβ©
end
/-!
### Neighborhoods to the left and to the right on an `order_closed_topology`
Limits to the left and to the right of real functions are defined in terms of neighborhoods to
the left and to the right, either open or closed, i.e., members of `π[Ioi a] a` and
`π[Ici a] a` on the right, and similarly on the left. Here we simply prove that all
right-neighborhoods of a point are equal, and we'll prove later other useful characterizations which
require the stronger hypothesis `order_topology Ξ±` -/
/-!
#### Right neighborhoods, point excluded
-/
lemma Ioo_mem_nhds_within_Ioi {a b c : Ξ±} (H : b β Ico a c) :
Ioo a c β π[Ioi b] b :=
mem_nhds_within.2 β¨Iio c, is_open_Iio, H.2,
by rw [inter_comm, Ioi_inter_Iio]; exact Ioo_subset_Ioo_left H.1β©
lemma Ioc_mem_nhds_within_Ioi {a b c : Ξ±} (H : b β Ico a c) :
Ioc a c β π[Ioi b] b :=
mem_of_superset (Ioo_mem_nhds_within_Ioi H) Ioo_subset_Ioc_self
lemma Ico_mem_nhds_within_Ioi {a b c : Ξ±} (H : b β Ico a c) :
Ico a c β π[Ioi b] b :=
mem_of_superset (Ioo_mem_nhds_within_Ioi H) Ioo_subset_Ico_self
lemma Icc_mem_nhds_within_Ioi {a b c : Ξ±} (H : b β Ico a c) :
Icc a c β π[Ioi b] b :=
mem_of_superset (Ioo_mem_nhds_within_Ioi H) Ioo_subset_Icc_self
@[simp] lemma nhds_within_Ioc_eq_nhds_within_Ioi {a b : Ξ±} (h : a < b) :
π[Ioc a b] a = π[Ioi a] a :=
le_antisymm (nhds_within_mono _ Ioc_subset_Ioi_self) $
nhds_within_le_of_mem $ Ioc_mem_nhds_within_Ioi $ left_mem_Ico.2 h
@[simp] lemma nhds_within_Ioo_eq_nhds_within_Ioi {a b : Ξ±} (h : a < b) :
π[Ioo a b] a = π[Ioi a] a :=
le_antisymm (nhds_within_mono _ Ioo_subset_Ioi_self) $
nhds_within_le_of_mem $ Ioo_mem_nhds_within_Ioi $ left_mem_Ico.2 h
@[simp]
lemma continuous_within_at_Ioc_iff_Ioi [topological_space Ξ²] {a b : Ξ±} {f : Ξ± β Ξ²} (h : a < b) :
continuous_within_at f (Ioc a b) a β continuous_within_at f (Ioi a) a :=
by simp only [continuous_within_at, nhds_within_Ioc_eq_nhds_within_Ioi h]
@[simp]
lemma continuous_within_at_Ioo_iff_Ioi [topological_space Ξ²] {a b : Ξ±} {f : Ξ± β Ξ²} (h : a < b) :
continuous_within_at f (Ioo a b) a β continuous_within_at f (Ioi a) a :=
by simp only [continuous_within_at, nhds_within_Ioo_eq_nhds_within_Ioi h]
/-!
#### Left neighborhoods, point excluded
-/
lemma Ioo_mem_nhds_within_Iio {a b c : Ξ±} (H : b β Ioc a c) :
Ioo a c β π[Iio b] b :=
by simpa only [dual_Ioo] using @Ioo_mem_nhds_within_Ioi (order_dual Ξ±) _ _ _ _ _ _ β¨H.2, H.1β©
lemma Ico_mem_nhds_within_Iio {a b c : Ξ±} (H : b β Ioc a c) :
Ico a c β π[Iio b] b :=
mem_of_superset (Ioo_mem_nhds_within_Iio H) Ioo_subset_Ico_self
lemma Ioc_mem_nhds_within_Iio {a b c : Ξ±} (H : b β Ioc a c) :
Ioc a c β π[Iio b] b :=
mem_of_superset (Ioo_mem_nhds_within_Iio H) Ioo_subset_Ioc_self
lemma Icc_mem_nhds_within_Iio {a b c : Ξ±} (H : b β Ioc a c) :
Icc a c β π[Iio b] b :=
mem_of_superset (Ioo_mem_nhds_within_Iio H) Ioo_subset_Icc_self
@[simp] lemma nhds_within_Ico_eq_nhds_within_Iio {a b : Ξ±} (h : a < b) :
π[Ico a b] b = π[Iio b] b :=
by simpa only [dual_Ioc] using @nhds_within_Ioc_eq_nhds_within_Ioi (order_dual Ξ±) _ _ _ _ _ h
@[simp] lemma nhds_within_Ioo_eq_nhds_within_Iio {a b : Ξ±} (h : a < b) :
π[Ioo a b] b = π[Iio b] b :=
by simpa only [dual_Ioo] using @nhds_within_Ioo_eq_nhds_within_Ioi (order_dual Ξ±) _ _ _ _ _ h
@[simp] lemma continuous_within_at_Ico_iff_Iio {a b : Ξ±} {f : Ξ± β Ξ³} (h : a < b) :
continuous_within_at f (Ico a b) b β continuous_within_at f (Iio b) b :=
by simp only [continuous_within_at, nhds_within_Ico_eq_nhds_within_Iio h]
@[simp] lemma continuous_within_at_Ioo_iff_Iio {a b : Ξ±} {f : Ξ± β Ξ³} (h : a < b) :
continuous_within_at f (Ioo a b) b β continuous_within_at f (Iio b) b :=
by simp only [continuous_within_at, nhds_within_Ioo_eq_nhds_within_Iio h]
/-!
#### Right neighborhoods, point included
-/
lemma Ioo_mem_nhds_within_Ici {a b c : Ξ±} (H : b β Ioo a c) :
Ioo a c β π[Ici b] b :=
mem_nhds_within_of_mem_nhds $ is_open.mem_nhds is_open_Ioo H
lemma Ioc_mem_nhds_within_Ici {a b c : Ξ±} (H : b β Ioo a c) :
Ioc a c β π[Ici b] b :=
mem_of_superset (Ioo_mem_nhds_within_Ici H) Ioo_subset_Ioc_self
lemma Ico_mem_nhds_within_Ici {a b c : Ξ±} (H : b β Ico a c) :
Ico a c β π[Ici b] b :=
mem_nhds_within.2 β¨Iio c, is_open_Iio, H.2,
by simp only [inter_comm, Ici_inter_Iio, Ico_subset_Ico_left H.1]β©
lemma Icc_mem_nhds_within_Ici {a b c : Ξ±} (H : b β Ico a c) :
Icc a c β π[Ici b] b :=
mem_of_superset (Ico_mem_nhds_within_Ici H) Ico_subset_Icc_self
@[simp] lemma nhds_within_Icc_eq_nhds_within_Ici {a b : Ξ±} (h : a < b) :
π[Icc a b] a = π[Ici a] a :=
le_antisymm (nhds_within_mono _ Icc_subset_Ici_self) $
nhds_within_le_of_mem $ Icc_mem_nhds_within_Ici $ left_mem_Ico.2 h
@[simp] lemma nhds_within_Ico_eq_nhds_within_Ici {a b : Ξ±} (h : a < b) :
π[Ico a b] a = π[Ici a] a :=
le_antisymm (nhds_within_mono _ (Ξ» x, and.left)) $
nhds_within_le_of_mem $ Ico_mem_nhds_within_Ici $ left_mem_Ico.2 h
@[simp]
lemma continuous_within_at_Icc_iff_Ici [topological_space Ξ²] {a b : Ξ±} {f : Ξ± β Ξ²} (h : a < b) :
continuous_within_at f (Icc a b) a β continuous_within_at f (Ici a) a :=
by simp only [continuous_within_at, nhds_within_Icc_eq_nhds_within_Ici h]
@[simp]
lemma continuous_within_at_Ico_iff_Ici [topological_space Ξ²] {a b : Ξ±} {f : Ξ± β Ξ²} (h : a < b) :
continuous_within_at f (Ico a b) a β continuous_within_at f (Ici a) a :=
by simp only [continuous_within_at, nhds_within_Ico_eq_nhds_within_Ici h]
/-!
#### Left neighborhoods, point included
-/
lemma Ioo_mem_nhds_within_Iic {a b c : Ξ±} (H : b β Ioo a c) :
Ioo a c β π[Iic b] b :=
mem_nhds_within_of_mem_nhds $ is_open.mem_nhds is_open_Ioo H
lemma Ico_mem_nhds_within_Iic {a b c : Ξ±} (H : b β Ioo a c) :
Ico a c β π[Iic b] b :=
mem_of_superset (Ioo_mem_nhds_within_Iic H) Ioo_subset_Ico_self
lemma Ioc_mem_nhds_within_Iic {a b c : Ξ±} (H : b β Ioc a c) :
Ioc a c β π[Iic b] b :=
by simpa only [dual_Ico] using @Ico_mem_nhds_within_Ici (order_dual Ξ±) _ _ _ _ _ _ β¨H.2, H.1β©
lemma Icc_mem_nhds_within_Iic {a b c : Ξ±} (H : b β Ioc a c) :
Icc a c β π[Iic b] b :=
mem_of_superset (Ioc_mem_nhds_within_Iic H) Ioc_subset_Icc_self
@[simp] lemma nhds_within_Icc_eq_nhds_within_Iic {a b : Ξ±} (h : a < b) :
π[Icc a b] b = π[Iic b] b :=
by simpa only [dual_Icc] using @nhds_within_Icc_eq_nhds_within_Ici (order_dual Ξ±) _ _ _ _ _ h
@[simp] lemma nhds_within_Ioc_eq_nhds_within_Iic {a b : Ξ±} (h : a < b) :
π[Ioc a b] b = π[Iic b] b :=
by simpa only [dual_Ico] using @nhds_within_Ico_eq_nhds_within_Ici (order_dual Ξ±) _ _ _ _ _ h
@[simp]
lemma continuous_within_at_Icc_iff_Iic [topological_space Ξ²] {a b : Ξ±} {f : Ξ± β Ξ²} (h : a < b) :
continuous_within_at f (Icc a b) b β continuous_within_at f (Iic b) b :=
by simp only [continuous_within_at, nhds_within_Icc_eq_nhds_within_Iic h]
@[simp]
lemma continuous_within_at_Ioc_iff_Iic [topological_space Ξ²] {a b : Ξ±} {f : Ξ± β Ξ²} (h : a < b) :
continuous_within_at f (Ioc a b) b β continuous_within_at f (Iic b) b :=
by simp only [continuous_within_at, nhds_within_Ioc_eq_nhds_within_Iic h]
end linear_order
section linear_order
variables [topological_space Ξ±] [linear_order Ξ±] [order_closed_topology Ξ±] {f g : Ξ² β Ξ±}
section
variables [topological_space Ξ²]
lemma frontier_le_subset_eq (hf : continuous f) (hg : continuous g) :
frontier {b | f b β€ g b} β {b | f b = g b} :=
begin
rw [frontier_eq_closure_inter_closure, closure_le_eq hf hg],
rintros b β¨hbβ, hbββ©,
refine le_antisymm hbβ (closure_lt_subset_le hg hf _),
convert hbβ using 2, simp only [not_le.symm], refl
end
lemma frontier_Iic_subset (a : Ξ±) : frontier (Iic a) β {a} :=
frontier_le_subset_eq (@continuous_id Ξ± _) continuous_const
lemma frontier_Ici_subset (a : Ξ±) : frontier (Ici a) β {a} :=
@frontier_Iic_subset (order_dual Ξ±) _ _ _ _
lemma frontier_lt_subset_eq (hf : continuous f) (hg : continuous g) :
frontier {b | f b < g b} β {b | f b = g b} :=
by rw β frontier_compl;
convert frontier_le_subset_eq hg hf; simp [ext_iff, eq_comm]
lemma continuous_if_le [topological_space Ξ³] [Ξ x, decidable (f x β€ g x)]
{f' g' : Ξ² β Ξ³} (hf : continuous f) (hg : continuous g)
(hf' : continuous_on f' {x | f x β€ g x}) (hg' : continuous_on g' {x | g x β€ f x})
(hfg : β x, f x = g x β f' x = g' x) :
continuous (Ξ» x, if f x β€ g x then f' x else g' x) :=
begin
refine continuous_if (Ξ» a ha, hfg _ (frontier_le_subset_eq hf hg ha)) _ (hg'.mono _),
{ rwa [(is_closed_le hf hg).closure_eq] },
{ simp only [not_le], exact closure_lt_subset_le hg hf }
end
lemma continuous.if_le [topological_space Ξ³] [Ξ x, decidable (f x β€ g x)] {f' g' : Ξ² β Ξ³}
(hf' : continuous f') (hg' : continuous g') (hf : continuous f) (hg : continuous g)
(hfg : β x, f x = g x β f' x = g' x) :
continuous (Ξ» x, if f x β€ g x then f' x else g' x) :=
continuous_if_le hf hg hf'.continuous_on hg'.continuous_on hfg
@[continuity] lemma continuous.min (hf : continuous f) (hg : continuous g) :
continuous (Ξ»b, min (f b) (g b)) :=
hf.if_le hg hf hg (Ξ» x, id)
@[continuity] lemma continuous.max (hf : continuous f) (hg : continuous g) :
continuous (Ξ»b, max (f b) (g b)) :=
@continuous.min (order_dual Ξ±) _ _ _ _ _ _ _ hf hg
end
lemma continuous_min : continuous (Ξ» p : Ξ± Γ Ξ±, min p.1 p.2) := continuous_fst.min continuous_snd
lemma continuous_max : continuous (Ξ» p : Ξ± Γ Ξ±, max p.1 p.2) := continuous_fst.max continuous_snd
lemma filter.tendsto.max {b : filter Ξ²} {aβ aβ : Ξ±} (hf : tendsto f b (π aβ))
(hg : tendsto g b (π aβ)) :
tendsto (Ξ»b, max (f b) (g b)) b (π (max aβ aβ)) :=
(continuous_max.tendsto (aβ, aβ)).comp (hf.prod_mk_nhds hg)
lemma filter.tendsto.min {b : filter Ξ²} {aβ aβ : Ξ±} (hf : tendsto f b (π aβ))
(hg : tendsto g b (π aβ)) :
tendsto (Ξ»b, min (f b) (g b)) b (π (min aβ aβ)) :=
(continuous_min.tendsto (aβ, aβ)).comp (hf.prod_mk_nhds hg)
lemma is_preconnected.ord_connected {s : set Ξ±} (h : is_preconnected s) :
ord_connected s :=
β¨Ξ» x hx y hy, h.Icc_subset hx hyβ©
end linear_order
end order_closed_topology
instance [preorder Ξ±] [topological_space Ξ±] [order_closed_topology Ξ±]
[preorder Ξ²] [topological_space Ξ²] [order_closed_topology Ξ²] :
order_closed_topology (Ξ± Γ Ξ²) :=
β¨(is_closed_le (continuous_fst.comp continuous_fst) (continuous_fst.comp continuous_snd)).inter
(is_closed_le (continuous_snd.comp continuous_fst) (continuous_snd.comp continuous_snd))β©
instance {ΞΉ : Type*} {Ξ± : ΞΉ β Type*} [Ξ i, preorder (Ξ± i)] [Ξ i, topological_space (Ξ± i)]
[Ξ i, order_closed_topology (Ξ± i)] : order_closed_topology (Ξ i, Ξ± i) :=
begin
constructor,
simp only [pi.le_def, set_of_forall],
exact is_closed_Inter (Ξ» i, is_closed_le ((continuous_apply i).comp continuous_fst)
((continuous_apply i).comp continuous_snd))
end
instance pi.order_closed_topology' [preorder Ξ²] [topological_space Ξ²]
[order_closed_topology Ξ²] : order_closed_topology (Ξ± β Ξ²) :=
pi.order_closed_topology
/-- The order topology on an ordered type is the topology generated by open intervals. We register
it on a preorder, but it is mostly interesting in linear orders, where it is also order-closed.
We define it as a mixin. If you want to introduce the order topology on a preorder, use
`preorder.topology`. -/
class order_topology (Ξ± : Type*) [t : topological_space Ξ±] [preorder Ξ±] : Prop :=
(topology_eq_generate_intervals : t = generate_from {s | βa, s = Ioi a β¨ s = Iio a})
/-- (Order) topology on a partial order `Ξ±` generated by the subbase of open intervals
`(a, β) = { x β£ a < x }, (-β , b) = {x β£ x < b}` for all `a, b` in `Ξ±`. We do not register it as an
instance as many ordered sets are already endowed with the same topology, most often in a non-defeq
way though. Register as a local instance when necessary. -/
def preorder.topology (Ξ± : Type*) [preorder Ξ±] : topological_space Ξ± :=
generate_from {s : set Ξ± | β (a : Ξ±), s = {b : Ξ± | a < b} β¨ s = {b : Ξ± | b < a}}
section order_topology
instance {Ξ± : Type*} [topological_space Ξ±] [partial_order Ξ±] [order_topology Ξ±] :
order_topology (order_dual Ξ±) :=
β¨by convert @order_topology.topology_eq_generate_intervals Ξ± _ _ _;
conv in (_ β¨ _) { rw or.comm }; reflβ©
section partial_order
variables [topological_space Ξ±] [partial_order Ξ±] [t : order_topology Ξ±]
include t
lemma is_open_iff_generate_intervals {s : set Ξ±} :
is_open s β generate_open {s | βa, s = Ioi a β¨ s = Iio a} s :=
by rw [t.topology_eq_generate_intervals]; refl
lemma is_open_lt' (a : Ξ±) : is_open {b:Ξ± | a < b} :=
by rw [@is_open_iff_generate_intervals Ξ± _ _ t]; exact generate_open.basic _ β¨a, or.inl rflβ©
lemma is_open_gt' (a : Ξ±) : is_open {b:Ξ± | b < a} :=
by rw [@is_open_iff_generate_intervals Ξ± _ _ t]; exact generate_open.basic _ β¨a, or.inr rflβ©
lemma lt_mem_nhds {a b : Ξ±} (h : a < b) : βαΆ x in π b, a < x :=
is_open.mem_nhds (is_open_lt' _) h
lemma le_mem_nhds {a b : Ξ±} (h : a < b) : βαΆ x in π b, a β€ x :=
(π b).sets_of_superset (lt_mem_nhds h) $ assume b hb, le_of_lt hb
lemma gt_mem_nhds {a b : Ξ±} (h : a < b) : βαΆ x in π a, x < b :=
is_open.mem_nhds (is_open_gt' _) h
lemma ge_mem_nhds {a b : Ξ±} (h : a < b) : βαΆ x in π a, x β€ b :=
(π a).sets_of_superset (gt_mem_nhds h) $ assume b hb, le_of_lt hb
lemma nhds_eq_order (a : Ξ±) :
π a = (β¨
b β Iio a, π (Ioi b)) β (β¨
b β Ioi a, π (Iio b)) :=
by rw [t.topology_eq_generate_intervals, nhds_generate_from];
from le_antisymm
(le_inf
(le_binfi $ assume b hb, infi_le_of_le {c : Ξ± | b < c} $ infi_le _ β¨hb, b, or.inl rflβ©)
(le_binfi $ assume b hb, infi_le_of_le {c : Ξ± | c < b} $ infi_le _ β¨hb, b, or.inr rflβ©))
(le_infi $ assume s, le_infi $ assume β¨ha, b, hsβ©,
match s, ha, hs with
| _, h, (or.inl rfl) := inf_le_of_left_le $ infi_le_of_le b $ infi_le _ h
| _, h, (or.inr rfl) := inf_le_of_right_le $ infi_le_of_le b $ infi_le _ h
end)
lemma tendsto_order {f : Ξ² β Ξ±} {a : Ξ±} {x : filter Ξ²} :
tendsto f x (π a) β (β a' < a, βαΆ b in x, a' < f b) β§ (β a' > a, βαΆ b in x, f b < a') :=
by simp [nhds_eq_order a, tendsto_inf, tendsto_infi, tendsto_principal]
instance tendsto_Icc_class_nhds (a : Ξ±) : tendsto_Ixx_class Icc (π a) (π a) :=
begin
simp only [nhds_eq_order, infi_subtype'],
refine ((has_basis_infi_principal_finite _).inf
(has_basis_infi_principal_finite _)).tendsto_Ixx_class (Ξ» s hs, _),
refine ((ord_connected_bInter _).inter (ord_connected_bInter _)).out; intros _ _,
exacts [ord_connected_Ioi, ord_connected_Iio]
end
instance tendsto_Ico_class_nhds (a : Ξ±) : tendsto_Ixx_class Ico (π a) (π a) :=
tendsto_Ixx_class_of_subset (Ξ» _ _, Ico_subset_Icc_self)
instance tendsto_Ioc_class_nhds (a : Ξ±) : tendsto_Ixx_class Ioc (π a) (π a) :=
tendsto_Ixx_class_of_subset (Ξ» _ _, Ioc_subset_Icc_self)
instance tendsto_Ioo_class_nhds (a : Ξ±) : tendsto_Ixx_class Ioo (π a) (π a) :=
tendsto_Ixx_class_of_subset (Ξ» _ _, Ioo_subset_Icc_self)
/-- Also known as squeeze or sandwich theorem. This version assumes that inequalities hold
eventually for the filter. -/
lemma tendsto_of_tendsto_of_tendsto_of_le_of_le' {f g h : Ξ² β Ξ±} {b : filter Ξ²} {a : Ξ±}
(hg : tendsto g b (π a)) (hh : tendsto h b (π a))
(hgf : βαΆ b in b, g b β€ f b) (hfh : βαΆ b in b, f b β€ h b) :
tendsto f b (π a) :=
tendsto_order.2
β¨assume a' h',
have βαΆ b in b, a' < g b, from (tendsto_order.1 hg).left a' h',
by filter_upwards [this, hgf] assume a, lt_of_lt_of_le,
assume a' h',
have βαΆ b in b, h b < a', from (tendsto_order.1 hh).right a' h',
by filter_upwards [this, hfh] assume a hβ hβ, lt_of_le_of_lt hβ hββ©
/-- Also known as squeeze or sandwich theorem. This version assumes that inequalities hold
everywhere. -/
lemma tendsto_of_tendsto_of_tendsto_of_le_of_le {f g h : Ξ² β Ξ±} {b : filter Ξ²} {a : Ξ±}
(hg : tendsto g b (π a)) (hh : tendsto h b (π a)) (hgf : g β€ f) (hfh : f β€ h) :
tendsto f b (π a) :=
tendsto_of_tendsto_of_tendsto_of_le_of_le' hg hh
(eventually_of_forall hgf) (eventually_of_forall hfh)
lemma nhds_order_unbounded {a : Ξ±} (hu : βu, a < u) (hl : βl, l < a) :
π a = (β¨
l (hβ : l < a) u (hβ : a < u), π (Ioo l u)) :=
have β u, u β Ioi a, from hu, have β l, l β Iio a, from hl,
by { simp only [nhds_eq_order, inf_binfi, binfi_inf, *, inf_principal, Ioi_inter_Iio], refl }
lemma tendsto_order_unbounded {f : Ξ² β Ξ±} {a : Ξ±} {x : filter Ξ²}
(hu : βu, a < u) (hl : βl, l < a) (h : βl u, l < a β a < u β βαΆ b in x, l < f b β§ f b < u) :
tendsto f x (π a) :=
by rw [nhds_order_unbounded hu hl];
from (tendsto_infi.2 $ assume l, tendsto_infi.2 $ assume hl,
tendsto_infi.2 $ assume u, tendsto_infi.2 $ assume hu, tendsto_principal.2 $ h l u hl hu)
end partial_order
instance tendsto_Ixx_nhds_within {Ξ± : Type*} [preorder Ξ±] [topological_space Ξ±]
(a : Ξ±) {s t : set Ξ±} {Ixx}
[tendsto_Ixx_class Ixx (π a) (π a)] [tendsto_Ixx_class Ixx (π s) (π t)]:
tendsto_Ixx_class Ixx (π[s] a) (π[t] a) :=
filter.tendsto_Ixx_class_inf
instance tendsto_Icc_class_nhds_pi {ΞΉ : Type*} {Ξ± : ΞΉ β Type*}
[Ξ i, partial_order (Ξ± i)] [Ξ i, topological_space (Ξ± i)] [β i, order_topology (Ξ± i)]
(f : Ξ i, Ξ± i) :
tendsto_Ixx_class Icc (π f) (π f) :=
begin
constructor,
conv in ((π f).lift' powerset) { rw [nhds_pi] },
simp only [lift'_infi_powerset, comap_lift'_eq2 monotone_powerset, tendsto_infi, tendsto_lift',
mem_powerset_iff, subset_def, mem_preimage],
intros i s hs,
have : tendsto (Ξ» g : Ξ i, Ξ± i, g i) (π f) (π (f i)) := ((continuous_apply i).tendsto f),
refine (tendsto_lift'.1 ((this.comp tendsto_fst).Icc (this.comp tendsto_snd)) s hs).mono _,
exact Ξ» p hp g hg, hp β¨hg.1 _, hg.2 _β©
end
theorem induced_order_topology' {Ξ± : Type u} {Ξ² : Type v}
[partial_order Ξ±] [ta : topological_space Ξ²] [partial_order Ξ²] [order_topology Ξ²]
(f : Ξ± β Ξ²) (hf : β {x y}, f x < f y β x < y)
(Hβ : β {a x}, x < f a β β b < a, x β€ f b)
(Hβ : β {a x}, f a < x β β b > a, f b β€ x) :
@order_topology _ (induced f ta) _ :=
begin
letI := induced f ta,
refine β¨eq_of_nhds_eq_nhds (Ξ» a, _)β©,
rw [nhds_induced, nhds_generate_from, nhds_eq_order (f a)],
apply le_antisymm,
{ refine le_infi (Ξ» s, le_infi $ Ξ» hs, le_principal_iff.2 _),
rcases hs with β¨ab, b, rfl|rflβ©,
{ exact mem_comap.2 β¨{x | f b < x},
mem_inf_of_left $ mem_infi_of_mem _ $ mem_infi_of_mem (hf.2 ab) $ mem_principal_self _,
Ξ» x, hf.1β© },
{ exact mem_comap.2 β¨{x | x < f b},
mem_inf_of_right $ mem_infi_of_mem _ $ mem_infi_of_mem (hf.2 ab) $ mem_principal_self _,
Ξ» x, hf.1β© } },
{ rw [β map_le_iff_le_comap],
refine le_inf _ _; refine le_infi (Ξ» x, le_infi $ Ξ» h, le_principal_iff.2 _); simp,
{ rcases Hβ h with β¨b, ab, xbβ©,
refine mem_infi_of_mem _ (mem_infi_of_mem β¨ab, b, or.inl rflβ© (mem_principal.2 _)),
exact Ξ» c hc, lt_of_le_of_lt xb (hf.2 hc) },
{ rcases Hβ h with β¨b, ab, xbβ©,
refine mem_infi_of_mem _ (mem_infi_of_mem β¨ab, b, or.inr rflβ© (mem_principal.2 _)),
exact Ξ» c hc, lt_of_lt_of_le (hf.2 hc) xb } },
end
theorem induced_order_topology {Ξ± : Type u} {Ξ² : Type v}
[partial_order Ξ±] [ta : topological_space Ξ²] [partial_order Ξ²] [order_topology Ξ²]
(f : Ξ± β Ξ²) (hf : β {x y}, f x < f y β x < y)
(H : β {x y}, x < y β β a, x < f a β§ f a < y) :
@order_topology _ (induced f ta) _ :=
induced_order_topology' f @hf
(Ξ» a x xa, let β¨b, xb, baβ© := H xa in β¨b, hf.1 ba, le_of_lt xbβ©)
(Ξ» a x ax, let β¨b, ab, bxβ© := H ax in β¨b, hf.1 ab, le_of_lt bxβ©)
/-- On an `ord_connected` subset of a linear order, the order topology for the restriction of the
order is the same as the restriction to the subset of the order topology. -/
instance order_topology_of_ord_connected {Ξ± : Type u}
[ta : topological_space Ξ±] [linear_order Ξ±] [order_topology Ξ±]
{t : set Ξ±} [ht : ord_connected t] :
order_topology t :=
begin
letI := induced (coe : t β Ξ±) ta,
refine β¨eq_of_nhds_eq_nhds (Ξ» a, _)β©,
rw [nhds_induced, nhds_generate_from, nhds_eq_order (a : Ξ±)],
apply le_antisymm,
{ refine le_infi (Ξ» s, le_infi $ Ξ» hs, le_principal_iff.2 _),
rcases hs with β¨ab, b, rfl|rflβ©,
{ refine β¨Ioi b, _, Ξ» _, idβ©,
refine mem_inf_of_left (mem_infi_of_mem b _),
exact mem_infi_of_mem ab (mem_principal_self (Ioi βb)) },
{ refine β¨Iio b, _, Ξ» _, idβ©,
refine mem_inf_of_right (mem_infi_of_mem b _),
exact mem_infi_of_mem ab (mem_principal_self (Iio b)) } },
{ rw [β map_le_iff_le_comap],
refine le_inf _ _,
{ refine le_infi (Ξ» x, le_infi $ Ξ» h, le_principal_iff.2 _),
by_cases hx : x β t,
{ refine mem_infi_of_mem (Ioi β¨x, hxβ©) (mem_infi_of_mem β¨h, β¨β¨x, hxβ©, or.inl rflβ©β© _),
exact Ξ» _, id },
simp only [set_coe.exists, mem_set_of_eq, mem_map'],
convert univ_sets _,
suffices hx' : β (y : t), βy β Ioi x,
{ simp [hx'] },
intros y,
revert hx,
contrapose!,
-- here we use the `ord_connected` hypothesis
exact Ξ» hx, ht.out y.2 a.2 β¨le_of_not_gt hx, le_of_lt hβ© },
{ refine le_infi (Ξ» x, le_infi $ Ξ» h, le_principal_iff.2 _),
by_cases hx : x β t,
{ refine mem_infi_of_mem (Iio β¨x, hxβ©) (mem_infi_of_mem β¨h, β¨β¨x, hxβ©, or.inr rflβ©β© _),
exact Ξ» _, id },
simp only [set_coe.exists, mem_set_of_eq, mem_map'],
convert univ_sets _,
suffices hx' : β (y : t), βy β Iio x,
{ simp [hx'] },
intros y,
revert hx,
contrapose!,
-- here we use the `ord_connected` hypothesis
exact Ξ» hx, ht.out a.2 y.2 β¨le_of_lt h, le_of_not_gt hxβ© } }
end
lemma nhds_top_order [topological_space Ξ±] [order_top Ξ±] [order_topology Ξ±] :
π (β€:Ξ±) = (β¨
l (hβ : l < β€), π (Ioi l)) :=
by simp [nhds_eq_order (β€:Ξ±)]
lemma nhds_bot_order [topological_space Ξ±] [order_bot Ξ±] [order_topology Ξ±] :
π (β₯:Ξ±) = (β¨
l (hβ : β₯ < l), π (Iio l)) :=
by simp [nhds_eq_order (β₯:Ξ±)]
lemma nhds_top_basis [topological_space Ξ±] [semilattice_sup_top Ξ±] [is_total Ξ± has_le.le]
[order_topology Ξ±] [nontrivial Ξ±] :
(π β€).has_basis (Ξ» a : Ξ±, a < β€) (Ξ» a : Ξ±, Ioi a) :=
β¨ begin
simp only [nhds_top_order],
refine @filter.mem_binfi_of_directed Ξ± Ξ± (Ξ» a, π (Ioi a)) (Ξ» a, a < β€) _ _,
{ rintros a (ha : a < β€) b (hb : b < β€),
use a β b,
simp only [filter.le_principal_iff, ge_iff_le, order.preimage],
exact β¨sup_lt_iff.mpr β¨ha, hbβ©, Ioi_subset_Ioi le_sup_left, Ioi_subset_Ioi le_sup_rightβ© },
{ obtain β¨a, haβ© : β a : Ξ±, a β β€ := exists_ne β€,
exact β¨a, lt_top_iff_ne_top.mpr haβ© }
end β©
lemma nhds_bot_basis [topological_space Ξ±] [semilattice_inf_bot Ξ±] [is_total Ξ± has_le.le]
[order_topology Ξ±] [nontrivial Ξ±] :
(π β₯).has_basis (Ξ» a : Ξ±, β₯ < a) (Ξ» a : Ξ±, Iio a) :=
@nhds_top_basis (order_dual Ξ±) _ _ _ _ _
lemma nhds_top_basis_Ici [topological_space Ξ±] [semilattice_sup_top Ξ±] [is_total Ξ± has_le.le]
[order_topology Ξ±] [nontrivial Ξ±] [densely_ordered Ξ±] :
(π β€).has_basis (Ξ» a : Ξ±, a < β€) Ici :=
nhds_top_basis.to_has_basis
(Ξ» a ha, let β¨b, hab, hbβ© := exists_between ha in β¨b, hb, Ici_subset_Ioi.mpr habβ©)
(Ξ» a ha, β¨a, ha, Ioi_subset_Ici_selfβ©)
lemma nhds_bot_basis_Iic [topological_space Ξ±] [semilattice_inf_bot Ξ±] [is_total Ξ± has_le.le]
[order_topology Ξ±] [nontrivial Ξ±] [densely_ordered Ξ±] :
(π β₯).has_basis (Ξ» a : Ξ±, β₯ < a) Iic :=
@nhds_top_basis_Ici (order_dual Ξ±) _ _ _ _ _ _
lemma tendsto_nhds_top_mono [topological_space Ξ²] [order_top Ξ²] [order_topology Ξ²] {l : filter Ξ±}
{f g : Ξ± β Ξ²} (hf : tendsto f l (π β€)) (hg : f β€αΆ [l] g) :
tendsto g l (π β€) :=
begin
simp only [nhds_top_order, tendsto_infi, tendsto_principal] at hf β’,
intros x hx,
filter_upwards [hf x hx, hg],
exact Ξ» x, lt_of_lt_of_le
end
lemma tendsto_nhds_bot_mono [topological_space Ξ²] [order_bot Ξ²] [order_topology Ξ²] {l : filter Ξ±}
{f g : Ξ± β Ξ²} (hf : tendsto f l (π β₯)) (hg : g β€αΆ [l] f) :
tendsto g l (π β₯) :=
@tendsto_nhds_top_mono Ξ± (order_dual Ξ²) _ _ _ _ _ _ hf hg
lemma tendsto_nhds_top_mono' [topological_space Ξ²] [order_top Ξ²] [order_topology Ξ²] {l : filter Ξ±}
{f g : Ξ± β Ξ²} (hf : tendsto f l (π β€)) (hg : f β€ g) :
tendsto g l (π β€) :=
tendsto_nhds_top_mono hf (eventually_of_forall hg)
lemma tendsto_nhds_bot_mono' [topological_space Ξ²] [order_bot Ξ²] [order_topology Ξ²] {l : filter Ξ±}
{f g : Ξ± β Ξ²} (hf : tendsto f l (π β₯)) (hg : g β€ f) :
tendsto g l (π β₯) :=
tendsto_nhds_bot_mono hf (eventually_of_forall hg)
section linear_order
variables [topological_space Ξ±] [linear_order Ξ±] [order_topology Ξ±]
lemma exists_Ioc_subset_of_mem_nhds' {a : Ξ±} {s : set Ξ±} (hs : s β π a) {l : Ξ±} (hl : l < a) :
β l' β Ico l a, Ioc l' a β s :=
begin
rw [nhds_eq_order a] at hs,
rcases hs with β¨tβ, htβ, tβ, htβ, rflβ©,
-- First we show that `tβ` includes `(-β, a]`, so it suffices to show `(l', β) β tβ`
suffices : β l' β Ico l a, Ioi l' β tβ,
{ have A : π (Iic a) β€ β¨
b β Ioi a, π (Iio b),
from (le_infi $ Ξ» b, le_infi $ Ξ» hb, principal_mono.2 $ Iic_subset_Iio.2 hb),
have B : tβ β© Iic a β tβ β© tβ,
from inter_subset_inter_right _ (A htβ),
from this.imp (Ξ» l', Exists.imp $ Ξ» hl' hl x hx, B β¨hl hx.1, hx.2β©) },
clear htβ tβ,
-- Now we find `l` such that `(l', β) β tβ`
rw [mem_binfi_of_directed] at htβ,
{ rcases htβ with β¨b, hb, hb'β©,
exact β¨max b l, β¨le_max_right _ _, max_lt hb hlβ©,
Ξ» x hx, hb' $ Ioi_subset_Ioi (le_max_left _ _) hxβ© },
{ intros b hb b' hb', simp only [mem_Iio] at hb hb',
use [max b b', max_lt hb hb'],
simp [le_refl] },
exact β¨l, hlβ©
end
lemma exists_Ico_subset_of_mem_nhds' {a : Ξ±} {s : set Ξ±} (hs : s β π a) {u : Ξ±} (hu : a < u) :
β u' β Ioc a u, Ico a u' β s :=
begin
convert @exists_Ioc_subset_of_mem_nhds' (order_dual Ξ±) _ _ _ _ _ hs _ hu,
ext, rw [dual_Ico, dual_Ioc]
end
lemma exists_Ioc_subset_of_mem_nhds {a : Ξ±} {s : set Ξ±} (hs : s β π a) (h : β l, l < a) :
β l < a, Ioc l a β s :=
let β¨l', hl'β© := h in let β¨l, hlβ© := exists_Ioc_subset_of_mem_nhds' hs hl' in β¨l, hl.fst.2, hl.sndβ©
lemma exists_Ico_subset_of_mem_nhds {a : Ξ±} {s : set Ξ±} (hs : s β π a) (h : β u, a < u) :
β u (_ : a < u), Ico a u β s :=
let β¨l', hl'β© := h in let β¨l, hlβ© := exists_Ico_subset_of_mem_nhds' hs hl' in β¨l, hl.fst.1, hl.sndβ©
lemma order_separated {aβ aβ : Ξ±} (h : aβ < aβ) :
βu v : set Ξ±, is_open u β§ is_open v β§ aβ β u β§ aβ β v β§ (βbββu, βbββv, bβ < bβ) :=
match dense_or_discrete aβ aβ with
| or.inl β¨a, haβ, haββ© := β¨{a' | a' < a}, {a' | a < a'}, is_open_gt' a, is_open_lt' a, haβ, haβ,
assume bβ hβ bβ hβ, lt_trans hβ hββ©
| or.inr β¨hβ, hββ© := β¨{a | a < aβ}, {a | aβ < a}, is_open_gt' aβ, is_open_lt' aβ, h, h,
assume bβ hbβ bβ hbβ,
calc bβ β€ aβ : hβ _ hbβ
... < aβ : h
... β€ bβ : hβ _ hbββ©
end
@[priority 100] -- see Note [lower instance priority]
instance order_topology.to_order_closed_topology : order_closed_topology Ξ± :=
{ is_closed_le' :=
is_open_compl_iff.1 $ is_open_prod_iff.mpr $ assume aβ aβ (h : Β¬ aβ β€ aβ),
have h : aβ < aβ, from lt_of_not_ge h,
let β¨u, v, hu, hv, haβ, haβ, hβ© := order_separated h in
β¨v, u, hv, hu, haβ, haβ, assume β¨bβ, bββ© β¨hβ, hββ©, not_le_of_gt $ h bβ hβ bβ hββ© }
lemma order_topology.t2_space : t2_space Ξ± := by apply_instance
@[priority 100] -- see Note [lower instance priority]
instance order_topology.regular_space : regular_space Ξ± :=
{ regular := assume s a hs ha,
have hs' : sαΆ β π a, from is_open.mem_nhds hs.is_open_compl ha,
have βt:set Ξ±, is_open t β§ (βlβ s, l < a β l β t) β§ π[t] a = β₯,
from by_cases
(assume h : βl, l < a,
let β¨l, hl, hβ© := exists_Ioc_subset_of_mem_nhds hs' h in
match dense_or_discrete l a with
| or.inl β¨b, hbβ, hbββ© := β¨{a | a < b}, is_open_gt' _,
assume c hcs hca, show c < b,
from lt_of_not_ge $ assume hbc, h β¨lt_of_lt_of_le hbβ hbc, le_of_lt hcaβ© hcs,
inf_principal_eq_bot.2 $ (π a).sets_of_superset ((is_open_lt' _).mem_nhds hbβ) $
assume x (hx : b < x), show Β¬ x < b, from not_lt.2 $ le_of_lt hxβ©
| or.inr β¨hβ, hββ© := β¨{a' | a' < a}, is_open_gt' _, assume b hbs hba, hba,
inf_principal_eq_bot.2 $ (π a).sets_of_superset ((is_open_lt' _).mem_nhds hl) $
assume x (hx : l < x), show Β¬ x < a, from not_lt.2 $ hβ _ hxβ©
end)
(assume : Β¬ βl, l < a, β¨β
, is_open_empty, assume l _ hl, (this β¨l, hlβ©).elim,
nhds_within_empty _β©),
let β¨tβ, htβo, htβs, htβaβ© := this in
have βt:set Ξ±, is_open t β§ (βuβ s, u>a β u β t) β§ π[t] a = β₯,
from by_cases
(assume h : βu, u > a,
let β¨u, hu, hβ© := exists_Ico_subset_of_mem_nhds hs' h in
match dense_or_discrete a u with
| or.inl β¨b, hbβ, hbββ© := β¨{a | b < a}, is_open_lt' _,
assume c hcs hca, show c > b,
from lt_of_not_ge $ assume hbc, h β¨le_of_lt hca, lt_of_le_of_lt hbc hbββ© hcs,
inf_principal_eq_bot.2 $ (π a).sets_of_superset ((is_open_gt' _).mem_nhds hbβ) $
assume x (hx : b > x), show Β¬ x > b, from not_lt.2 $ le_of_lt hxβ©
| or.inr β¨hβ, hββ© := β¨{a' | a' > a}, is_open_lt' _, assume b hbs hba, hba,
inf_principal_eq_bot.2 $ (π a).sets_of_superset ((is_open_gt' _).mem_nhds hu) $
assume x (hx : u > x), show Β¬ x > a, from not_lt.2 $ hβ _ hxβ©
end)
(assume : Β¬ βu, u > a, β¨β
, is_open_empty, assume l _ hl, (this β¨l, hlβ©).elim,
nhds_within_empty _β©),
let β¨tβ, htβo, htβs, htβaβ© := this in
β¨tβ βͺ tβ, is_open.union htβo htβo,
assume x hx,
have x β a, from assume eq, ha $ eq βΈ hx,
(ne_iff_lt_or_gt.mp this).imp (htβs _ hx) (htβs _ hx),
by rw [nhds_within_union, htβa, htβa, bot_sup_eq]β©,
..order_topology.t2_space }
/-- A set is a neighborhood of `a` if and only if it contains an interval `(l, u)` containing `a`,
provided `a` is neither a bottom element nor a top element. -/
lemma mem_nhds_iff_exists_Ioo_subset' {a : Ξ±} {s : set Ξ±} (hl : β l, l < a) (hu : β u, a < u) :
s β π a β βl u, a β Ioo l u β§ Ioo l u β s :=
begin
split,
{ assume h,
rcases exists_Ico_subset_of_mem_nhds h hu with β¨u, au, huβ©,
rcases exists_Ioc_subset_of_mem_nhds h hl with β¨l, la, hlβ©,
refine β¨l, u, β¨la, auβ©, Ξ»x hx, _β©,
cases le_total a x with hax hax,
{ exact hu β¨hax, hx.2β© },
{ exact hl β¨hx.1, haxβ© } },
{ rintros β¨l, u, ha, hβ©,
apply mem_of_superset (is_open.mem_nhds is_open_Ioo ha) h }
end
/-- A set is a neighborhood of `a` if and only if it contains an interval `(l, u)` containing `a`.
-/
lemma mem_nhds_iff_exists_Ioo_subset [no_top_order Ξ±] [no_bot_order Ξ±] {a : Ξ±} {s : set Ξ±} :
s β π a β βl u, a β Ioo l u β§ Ioo l u β s :=
mem_nhds_iff_exists_Ioo_subset' (no_bot a) (no_top a)
lemma nhds_basis_Ioo' {a : Ξ±} (hl : β l, l < a) (hu : β u, a < u) :
(π a).has_basis (Ξ» b : Ξ± Γ Ξ±, b.1 < a β§ a < b.2) (Ξ» b, Ioo b.1 b.2) :=
β¨Ξ» s, (mem_nhds_iff_exists_Ioo_subset' hl hu).trans $ by simpβ©
lemma nhds_basis_Ioo [no_top_order Ξ±] [no_bot_order Ξ±] (a : Ξ±) :
(π a).has_basis (Ξ» b : Ξ± Γ Ξ±, b.1 < a β§ a < b.2) (Ξ» b, Ioo b.1 b.2) :=
nhds_basis_Ioo' (no_bot a) (no_top a)
lemma filter.eventually.exists_Ioo_subset [no_top_order Ξ±] [no_bot_order Ξ±] {a : Ξ±} {p : Ξ± β Prop}
(hp : βαΆ x in π a, p x) :
β l u, a β Ioo l u β§ Ioo l u β {x | p x} :=
mem_nhds_iff_exists_Ioo_subset.1 hp
lemma Iio_mem_nhds {a b : Ξ±} (h : a < b) : Iio b β π a :=
is_open.mem_nhds is_open_Iio h
lemma Ioi_mem_nhds {a b : Ξ±} (h : a < b) : Ioi a β π b :=
is_open.mem_nhds is_open_Ioi h
lemma Iic_mem_nhds {a b : Ξ±} (h : a < b) : Iic b β π a :=
mem_of_superset (Iio_mem_nhds h) Iio_subset_Iic_self
lemma Ici_mem_nhds {a b : Ξ±} (h : a < b) : Ici a β π b :=
mem_of_superset (Ioi_mem_nhds h) Ioi_subset_Ici_self
lemma Ioo_mem_nhds {a b x : Ξ±} (ha : a < x) (hb : x < b) : Ioo a b β π x :=
is_open.mem_nhds is_open_Ioo β¨ha, hbβ©
lemma Ioc_mem_nhds {a b x : Ξ±} (ha : a < x) (hb : x < b) : Ioc a b β π x :=
mem_of_superset (Ioo_mem_nhds ha hb) Ioo_subset_Ioc_self
lemma Ico_mem_nhds {a b x : Ξ±} (ha : a < x) (hb : x < b) : Ico a b β π x :=
mem_of_superset (Ioo_mem_nhds ha hb) Ioo_subset_Ico_self
lemma Icc_mem_nhds {a b x : Ξ±} (ha : a < x) (hb : x < b) : Icc a b β π x :=
mem_of_superset (Ioo_mem_nhds ha hb) Ioo_subset_Icc_self
section pi
/-!
### Intervals in `Ξ i, Ο i` belong to `π x`
For each lemma `pi_Ixx_mem_nhds` we add a non-dependent version `pi_Ixx_mem_nhds'` because
sometimes Lean fails to unify different instances while trying to apply the dependent version to,
e.g., `ΞΉ β β`.
-/
variables {ΞΉ : Type*} {Ο : ΞΉ β Type*} [fintype ΞΉ] [Ξ i, linear_order (Ο i)]
[Ξ i, topological_space (Ο i)] [β i, order_topology (Ο i)] {a b x : Ξ i, Ο i} {a' b' x' : ΞΉ β Ξ±}
lemma pi_Iic_mem_nhds (ha : β i, x i < a i) : Iic a β π x :=
pi_univ_Iic a βΈ set_pi_mem_nhds (finite.of_fintype _) (Ξ» i _, Iic_mem_nhds (ha _))
lemma pi_Iic_mem_nhds' (ha : β i, x' i < a' i) : Iic a' β π x' :=
pi_Iic_mem_nhds ha
lemma pi_Ici_mem_nhds (ha : β i, a i < x i) : Ici a β π x :=
pi_univ_Ici a βΈ set_pi_mem_nhds (finite.of_fintype _) (Ξ» i _, Ici_mem_nhds (ha _))
lemma pi_Ici_mem_nhds' (ha : β i, a' i < x' i) : Ici a' β π x' :=
pi_Ici_mem_nhds ha
lemma pi_Icc_mem_nhds (ha : β i, a i < x i) (hb : β i, x i < b i) : Icc a b β π x :=
pi_univ_Icc a b βΈ set_pi_mem_nhds (finite.of_fintype _) (Ξ» i _, Icc_mem_nhds (ha _) (hb _))
lemma pi_Icc_mem_nhds' (ha : β i, a' i < x' i) (hb : β i, x' i < b' i) : Icc a' b' β π x' :=
pi_Icc_mem_nhds ha hb
variables [nonempty ΞΉ]
lemma pi_Iio_mem_nhds (ha : β i, x i < a i) : Iio a β π x :=
begin
refine mem_of_superset (set_pi_mem_nhds (finite.of_fintype _) (Ξ» i _, _))
(pi_univ_Iio_subset a),
exact Iio_mem_nhds (ha i)
end
lemma pi_Iio_mem_nhds' (ha : β i, x' i < a' i) : Iio a' β π x' :=
pi_Iio_mem_nhds ha
lemma pi_Ioi_mem_nhds (ha : β i, a i < x i) : Ioi a β π x :=
@pi_Iio_mem_nhds ΞΉ (Ξ» i, order_dual (Ο i)) _ _ _ _ _ _ _ ha
lemma pi_Ioi_mem_nhds' (ha : β i, a' i < x' i) : Ioi a' β π x' :=
pi_Ioi_mem_nhds ha
lemma pi_Ioc_mem_nhds (ha : β i, a i < x i) (hb : β i, x i < b i) : Ioc a b β π x :=
begin
refine mem_of_superset (set_pi_mem_nhds (finite.of_fintype _) (Ξ» i _, _))
(pi_univ_Ioc_subset a b),
exact Ioc_mem_nhds (ha i) (hb i)
end
lemma pi_Ioc_mem_nhds' (ha : β i, a' i < x' i) (hb : β i, x' i < b' i) : Ioc a' b' β π x' :=
pi_Ioc_mem_nhds ha hb
lemma pi_Ico_mem_nhds (ha : β i, a i < x i) (hb : β i, x i < b i) : Ico a b β π x :=
begin
refine mem_of_superset (set_pi_mem_nhds (finite.of_fintype _) (Ξ» i _, _))
(pi_univ_Ico_subset a b),
exact Ico_mem_nhds (ha i) (hb i)
end
lemma pi_Ico_mem_nhds' (ha : β i, a' i < x' i) (hb : β i, x' i < b' i) : Ico a' b' β π x' :=
pi_Ico_mem_nhds ha hb
lemma pi_Ioo_mem_nhds (ha : β i, a i < x i) (hb : β i, x i < b i) : Ioo a b β π x :=
begin
refine mem_of_superset (set_pi_mem_nhds (finite.of_fintype _) (Ξ» i _, _))
(pi_univ_Ioo_subset a b),
exact Ioo_mem_nhds (ha i) (hb i)
end
lemma pi_Ioo_mem_nhds' (ha : β i, a' i < x' i) (hb : β i, x' i < b' i) : Ioo a' b' β π x' :=
pi_Ioo_mem_nhds ha hb
end pi
lemma disjoint_nhds_at_top [no_top_order Ξ±] (x : Ξ±) :
disjoint (π x) at_top :=
begin
rw filter.disjoint_iff,
cases no_top x with a ha,
use [Iio a, Iio_mem_nhds ha, Ici a, mem_at_top a],
rw [inter_comm, Ici_inter_Iio, Ico_self]
end
@[simp] lemma inf_nhds_at_top [no_top_order Ξ±] (x : Ξ±) :
π x β at_top = β₯ :=
disjoint_iff.1 (disjoint_nhds_at_top x)
lemma disjoint_nhds_at_bot [no_bot_order Ξ±] (x : Ξ±) :
disjoint (π x) at_bot :=
@disjoint_nhds_at_top (order_dual Ξ±) _ _ _ _ x
@[simp] lemma inf_nhds_at_bot [no_bot_order Ξ±] (x : Ξ±) :
π x β at_bot = β₯ :=
@inf_nhds_at_top (order_dual Ξ±) _ _ _ _ x
lemma not_tendsto_nhds_of_tendsto_at_top [no_top_order Ξ±]
{F : filter Ξ²} [ne_bot F] {f : Ξ² β Ξ±} (hf : tendsto f F at_top) (x : Ξ±) :
Β¬ tendsto f F (π x) :=
hf.not_tendsto (disjoint_nhds_at_top x).symm
lemma not_tendsto_at_top_of_tendsto_nhds [no_top_order Ξ±]
{F : filter Ξ²} [ne_bot F] {f : Ξ² β Ξ±} {x : Ξ±} (hf : tendsto f F (π x)) :
Β¬ tendsto f F at_top :=
hf.not_tendsto (disjoint_nhds_at_top x)
lemma not_tendsto_nhds_of_tendsto_at_bot [no_bot_order Ξ±]
{F : filter Ξ²} [ne_bot F] {f : Ξ² β Ξ±} (hf : tendsto f F at_bot) (x : Ξ±) :
Β¬ tendsto f F (π x) :=
hf.not_tendsto (disjoint_nhds_at_bot x).symm
lemma not_tendsto_at_bot_of_tendsto_nhds [no_bot_order Ξ±]
{F : filter Ξ²} [ne_bot F] {f : Ξ² β Ξ±} {x : Ξ±} (hf : tendsto f F (π x)) :
Β¬ tendsto f F at_bot :=
hf.not_tendsto (disjoint_nhds_at_bot x)
/-!
### Neighborhoods to the left and to the right on an `order_topology`
We've seen some properties of left and right neighborhood of a point in an `order_closed_topology`.
In an `order_topology`, such neighborhoods can be characterized as the sets containing suitable
intervals to the right or to the left of `a`. We give now these characterizations. -/
-- NB: If you extend the list, append to the end please to avoid breaking the API
/-- The following statements are equivalent:
0. `s` is a neighborhood of `a` within `(a, +β)`
1. `s` is a neighborhood of `a` within `(a, b]`
2. `s` is a neighborhood of `a` within `(a, b)`
3. `s` includes `(a, u)` for some `u β (a, b]`
4. `s` includes `(a, u)` for some `u > a` -/
lemma tfae_mem_nhds_within_Ioi {a b : Ξ±} (hab : a < b) (s : set Ξ±) :
tfae [s β π[Ioi a] a, -- 0 : `s` is a neighborhood of `a` within `(a, +β)`
s β π[Ioc a b] a, -- 1 : `s` is a neighborhood of `a` within `(a, b]`
s β π[Ioo a b] a, -- 2 : `s` is a neighborhood of `a` within `(a, b)`
β u β Ioc a b, Ioo a u β s, -- 3 : `s` includes `(a, u)` for some `u β (a, b]`
β u β Ioi a, Ioo a u β s] := -- 4 : `s` includes `(a, u)` for some `u > a`
begin
tfae_have : 1 β 2, by rw [nhds_within_Ioc_eq_nhds_within_Ioi hab],
tfae_have : 1 β 3, by rw [nhds_within_Ioo_eq_nhds_within_Ioi hab],
tfae_have : 4 β 5, from Ξ» β¨u, umem, huβ©, β¨u, umem.1, huβ©,
tfae_have : 5 β 1,
{ rintros β¨u, hau, huβ©,
exact mem_of_superset (Ioo_mem_nhds_within_Ioi β¨le_refl a, hauβ©) hu },
tfae_have : 1 β 4,
{ assume h,
rcases mem_nhds_within_iff_exists_mem_nhds_inter.1 h with β¨v, va, hvβ©,
rcases exists_Ico_subset_of_mem_nhds' va hab with β¨u, au, huβ©,
refine β¨u, au, Ξ»x hx, _β©,
refine hv β¨hu β¨le_of_lt hx.1, hx.2β©, _β©,
exact hx.1 },
tfae_finish
end
lemma mem_nhds_within_Ioi_iff_exists_mem_Ioc_Ioo_subset {a u' : Ξ±} {s : set Ξ±} (hu' : a < u') :
s β π[Ioi a] a β βu β Ioc a u', Ioo a u β s :=
(tfae_mem_nhds_within_Ioi hu' s).out 0 3
/-- A set is a neighborhood of `a` within `(a, +β)` if and only if it contains an interval `(a, u)`
with `a < u < u'`, provided `a` is not a top element. -/
lemma mem_nhds_within_Ioi_iff_exists_Ioo_subset' {a u' : Ξ±} {s : set Ξ±} (hu' : a < u') :
s β π[Ioi a] a β βu β Ioi a, Ioo a u β s :=
(tfae_mem_nhds_within_Ioi hu' s).out 0 4
/-- A set is a neighborhood of `a` within `(a, +β)` if and only if it contains an interval `(a, u)`
with `a < u`. -/
lemma mem_nhds_within_Ioi_iff_exists_Ioo_subset [no_top_order Ξ±] {a : Ξ±} {s : set Ξ±} :
s β π[Ioi a] a β βu β Ioi a, Ioo a u β s :=
let β¨u', hu'β© := no_top a in mem_nhds_within_Ioi_iff_exists_Ioo_subset' hu'
/-- A set is a neighborhood of `a` within `(a, +β)` if and only if it contains an interval `(a, u]`
with `a < u`. -/
lemma mem_nhds_within_Ioi_iff_exists_Ioc_subset [no_top_order Ξ±] [densely_ordered Ξ±]
{a : Ξ±} {s : set Ξ±} : s β π[Ioi a] a β βu β Ioi a, Ioc a u β s :=
begin
rw mem_nhds_within_Ioi_iff_exists_Ioo_subset,
split,
{ rintros β¨u, au, asβ©,
rcases exists_between au with β¨v, hvβ©,
exact β¨v, hv.1, Ξ»x hx, as β¨hx.1, lt_of_le_of_lt hx.2 hv.2β©β© },
{ rintros β¨u, au, asβ©,
exact β¨u, au, subset.trans Ioo_subset_Ioc_self asβ© }
end
/-- The following statements are equivalent:
0. `s` is a neighborhood of `b` within `(-β, b)`
1. `s` is a neighborhood of `b` within `[a, b)`
2. `s` is a neighborhood of `b` within `(a, b)`
3. `s` includes `(l, b)` for some `l β [a, b)`
4. `s` includes `(l, b)` for some `l < b` -/
lemma tfae_mem_nhds_within_Iio {a b : Ξ±} (h : a < b) (s : set Ξ±) :
tfae [s β π[Iio b] b, -- 0 : `s` is a neighborhood of `b` within `(-β, b)`
s β π[Ico a b] b, -- 1 : `s` is a neighborhood of `b` within `[a, b)`
s β π[Ioo a b] b, -- 2 : `s` is a neighborhood of `b` within `(a, b)`
β l β Ico a b, Ioo l b β s, -- 3 : `s` includes `(l, b)` for some `l β [a, b)`
β l β Iio b, Ioo l b β s] := -- 4 : `s` includes `(l, b)` for some `l < b`
begin
have := @tfae_mem_nhds_within_Ioi (order_dual Ξ±) _ _ _ _ _ h s,
-- If we call `convert` here, it generates wrong equations, so we need to simplify first
simp only [exists_prop] at this β’,
rw [dual_Ioi, dual_Ioc, dual_Ioo] at this,
convert this; ext l; rw [dual_Ioo]
end
lemma mem_nhds_within_Iio_iff_exists_mem_Ico_Ioo_subset {a l' : Ξ±} {s : set Ξ±} (hl' : l' < a) :
s β π[Iio a] a β βl β Ico l' a, Ioo l a β s :=
(tfae_mem_nhds_within_Iio hl' s).out 0 3
/-- A set is a neighborhood of `a` within `(-β, a)` if and only if it contains an interval `(l, a)`
with `l < a`, provided `a` is not a bottom element. -/
lemma mem_nhds_within_Iio_iff_exists_Ioo_subset' {a l' : Ξ±} {s : set Ξ±} (hl' : l' < a) :
s β π[Iio a] a β βl β Iio a, Ioo l a β s :=
(tfae_mem_nhds_within_Iio hl' s).out 0 4
/-- A set is a neighborhood of `a` within `(-β, a)` if and only if it contains an interval `(l, a)`
with `l < a`. -/
lemma mem_nhds_within_Iio_iff_exists_Ioo_subset [no_bot_order Ξ±] {a : Ξ±} {s : set Ξ±} :
s β π[Iio a] a β βl β Iio a, Ioo l a β s :=
let β¨l', hl'β© := no_bot a in mem_nhds_within_Iio_iff_exists_Ioo_subset' hl'
/-- A set is a neighborhood of `a` within `(-β, a)` if and only if it contains an interval `[l, a)`
with `l < a`. -/
lemma mem_nhds_within_Iio_iff_exists_Ico_subset [no_bot_order Ξ±] [densely_ordered Ξ±]
{a : Ξ±} {s : set Ξ±} : s β π[Iio a] a β βl β Iio a, Ico l a β s :=
begin
convert @mem_nhds_within_Ioi_iff_exists_Ioc_subset (order_dual Ξ±) _ _ _ _ _ _ _,
simp only [dual_Ioc], refl
end
/-- The following statements are equivalent:
0. `s` is a neighborhood of `a` within `[a, +β)`
1. `s` is a neighborhood of `a` within `[a, b]`
2. `s` is a neighborhood of `a` within `[a, b)`
3. `s` includes `[a, u)` for some `u β (a, b]`
4. `s` includes `[a, u)` for some `u > a` -/
lemma tfae_mem_nhds_within_Ici {a b : Ξ±} (hab : a < b) (s : set Ξ±) :
tfae [s β π[Ici a] a, -- 0 : `s` is a neighborhood of `a` within `[a, +β)`
s β π[Icc a b] a, -- 1 : `s` is a neighborhood of `a` within `[a, b]`
s β π[Ico a b] a, -- 2 : `s` is a neighborhood of `a` within `[a, b)`
β u β Ioc a b, Ico a u β s, -- 3 : `s` includes `[a, u)` for some `u β (a, b]`
β u β Ioi a, Ico a u β s] := -- 4 : `s` includes `[a, u)` for some `u > a`
begin
tfae_have : 1 β 2, by rw [nhds_within_Icc_eq_nhds_within_Ici hab],
tfae_have : 1 β 3, by rw [nhds_within_Ico_eq_nhds_within_Ici hab],
tfae_have : 4 β 5, from Ξ» β¨u, umem, huβ©, β¨u, umem.1, huβ©,
tfae_have : 5 β 1,
{ rintros β¨u, hau, huβ©,
exact mem_of_superset (Ico_mem_nhds_within_Ici β¨le_refl a, hauβ©) hu },
tfae_have : 1 β 4,
{ assume h,
rcases mem_nhds_within_iff_exists_mem_nhds_inter.1 h with β¨v, va, hvβ©,
rcases exists_Ico_subset_of_mem_nhds' va hab with β¨u, au, huβ©,
refine β¨u, au, Ξ»x hx, _β©,
refine hv β¨hu β¨hx.1, hx.2β©, _β©,
exact hx.1 },
tfae_finish
end
lemma mem_nhds_within_Ici_iff_exists_mem_Ioc_Ico_subset {a u' : Ξ±} {s : set Ξ±} (hu' : a < u') :
s β π[Ici a] a β βu β Ioc a u', Ico a u β s :=
(tfae_mem_nhds_within_Ici hu' s).out 0 3 (by norm_num) (by norm_num)
/-- A set is a neighborhood of `a` within `[a, +β)` if and only if it contains an interval `[a, u)`
with `a < u < u'`, provided `a` is not a top element. -/
lemma mem_nhds_within_Ici_iff_exists_Ico_subset' {a u' : Ξ±} {s : set Ξ±} (hu' : a < u') :
s β π[Ici a] a β βu β Ioi a, Ico a u β s :=
(tfae_mem_nhds_within_Ici hu' s).out 0 4 (by norm_num) (by norm_num)
/-- A set is a neighborhood of `a` within `[a, +β)` if and only if it contains an interval `[a, u)`
with `a < u`. -/
lemma mem_nhds_within_Ici_iff_exists_Ico_subset [no_top_order Ξ±] {a : Ξ±} {s : set Ξ±} :
s β π[Ici a] a β βu β Ioi a, Ico a u β s :=
let β¨u', hu'β© := no_top a in mem_nhds_within_Ici_iff_exists_Ico_subset' hu'
/-- A set is a neighborhood of `a` within `[a, +β)` if and only if it contains an interval `[a, u]`
with `a < u`. -/
lemma mem_nhds_within_Ici_iff_exists_Icc_subset' [no_top_order Ξ±] [densely_ordered Ξ±]
{a : Ξ±} {s : set Ξ±} : s β π[Ici a] a β βu β Ioi a, Icc a u β s :=
begin
rw mem_nhds_within_Ici_iff_exists_Ico_subset,
split,
{ rintros β¨u, au, asβ©,
rcases exists_between au with β¨v, hvβ©,
exact β¨v, hv.1, Ξ»x hx, as β¨hx.1, lt_of_le_of_lt hx.2 hv.2β©β© },
{ rintros β¨u, au, asβ©,
exact β¨u, au, subset.trans Ico_subset_Icc_self asβ© }
end
/-- The following statements are equivalent:
0. `s` is a neighborhood of `b` within `(-β, b]`
1. `s` is a neighborhood of `b` within `[a, b]`
2. `s` is a neighborhood of `b` within `(a, b]`
3. `s` includes `(l, b]` for some `l β [a, b)`
4. `s` includes `(l, b]` for some `l < b` -/
lemma tfae_mem_nhds_within_Iic {a b : Ξ±} (h : a < b) (s : set Ξ±) :
tfae [s β π[Iic b] b, -- 0 : `s` is a neighborhood of `b` within `(-β, b]`
s β π[Icc a b] b, -- 1 : `s` is a neighborhood of `b` within `[a, b]`
s β π[Ioc a b] b, -- 2 : `s` is a neighborhood of `b` within `(a, b]`
β l β Ico a b, Ioc l b β s, -- 3 : `s` includes `(l, b]` for some `l β [a, b)`
β l β Iio b, Ioc l b β s] := -- 4 : `s` includes `(l, b]` for some `l < b`
begin
have := @tfae_mem_nhds_within_Ici (order_dual Ξ±) _ _ _ _ _ h s,
-- If we call `convert` here, it generates wrong equations, so we need to simplify first
simp only [exists_prop] at this β’,
rw [dual_Icc, dual_Ioc, dual_Ioi] at this,
convert this; ext l; rw [dual_Ico]
end
lemma mem_nhds_within_Iic_iff_exists_mem_Ico_Ioc_subset {a l' : Ξ±} {s : set Ξ±} (hl' : l' < a) :
s β π[Iic a] a β βl β Ico l' a, Ioc l a β s :=
(tfae_mem_nhds_within_Iic hl' s).out 0 3 (by norm_num) (by norm_num)
/-- A set is a neighborhood of `a` within `(-β, a]` if and only if it contains an interval `(l, a]`
with `l < a`, provided `a` is not a bottom element. -/
lemma mem_nhds_within_Iic_iff_exists_Ioc_subset' {a l' : Ξ±} {s : set Ξ±} (hl' : l' < a) :
s β π[Iic a] a β βl β Iio a, Ioc l a β s :=
(tfae_mem_nhds_within_Iic hl' s).out 0 4 (by norm_num) (by norm_num)
/-- A set is a neighborhood of `a` within `(-β, a]` if and only if it contains an interval `(l, a]`
with `l < a`. -/
lemma mem_nhds_within_Iic_iff_exists_Ioc_subset [no_bot_order Ξ±] {a : Ξ±} {s : set Ξ±} :
s β π[Iic a] a β βl β Iio a, Ioc l a β s :=
let β¨l', hl'β© := no_bot a in mem_nhds_within_Iic_iff_exists_Ioc_subset' hl'
/-- A set is a neighborhood of `a` within `(-β, a]` if and only if it contains an interval `[l, a]`
with `l < a`. -/
lemma mem_nhds_within_Iic_iff_exists_Icc_subset' [no_bot_order Ξ±] [densely_ordered Ξ±]
{a : Ξ±} {s : set Ξ±} : s β π[Iic a] a β βl β Iio a, Icc l a β s :=
begin
convert @mem_nhds_within_Ici_iff_exists_Icc_subset' (order_dual Ξ±) _ _ _ _ _ _ _,
simp_rw (show β u : order_dual Ξ±, @Icc (order_dual Ξ±) _ a u = @Icc Ξ± _ u a, from Ξ» u, dual_Icc),
refl,
end
/-- A set is a neighborhood of `a` within `[a, +β)` if and only if it contains an interval `[a, u]`
with `a < u`. -/
lemma mem_nhds_within_Ici_iff_exists_Icc_subset [no_top_order Ξ±] [densely_ordered Ξ±]
{a : Ξ±} {s : set Ξ±} : s β π[Ici a] a β βu, a < u β§ Icc a u β s :=
begin
rw mem_nhds_within_Ici_iff_exists_Ico_subset,
split,
{ rintros β¨u, au, asβ©,
rcases exists_between au with β¨v, hvβ©,
exact β¨v, hv.1, Ξ»x hx, as β¨hx.1, lt_of_le_of_lt hx.2 hv.2β©β© },
{ rintros β¨u, au, asβ©,
exact β¨u, au, subset.trans Ico_subset_Icc_self asβ© }
end
/-- A set is a neighborhood of `a` within `(-β, a]` if and only if it contains an interval `[l, a]`
with `l < a`. -/
lemma mem_nhds_within_Iic_iff_exists_Icc_subset [no_bot_order Ξ±] [densely_ordered Ξ±]
{a : Ξ±} {s : set Ξ±} : s β π[Iic a] a β βl, l < a β§ Icc l a β s :=
begin
rw mem_nhds_within_Iic_iff_exists_Ioc_subset,
split,
{ rintros β¨l, la, asβ©,
rcases exists_between la with β¨v, hvβ©,
refine β¨v, hv.2, Ξ»x hx, as β¨lt_of_lt_of_le hv.1 hx.1, hx.2β©β©, },
{ rintros β¨l, la, asβ©,
exact β¨l, la, subset.trans Ioc_subset_Icc_self asβ© }
end
end linear_order
section linear_ordered_add_comm_group
variables [topological_space Ξ±] [linear_ordered_add_comm_group Ξ±] [order_topology Ξ±]
variables {l : filter Ξ²} {f g : Ξ² β Ξ±}
local notation `|` x `|` := abs x
lemma nhds_eq_infi_abs_sub (a : Ξ±) : π a = (β¨
r>0, π {b | |a - b| < r}) :=
begin
simp only [le_antisymm_iff, nhds_eq_order, le_inf_iff, le_infi_iff, le_principal_iff, mem_Ioi,
mem_Iio, abs_sub_lt_iff, @sub_lt_iff_lt_add _ _ _ _ _ _ a, @sub_lt _ _ _ _ a, set_of_and],
refine β¨_, _, _β©,
{ intros Ξ΅ Ξ΅0,
exact inter_mem_inf
(mem_infi_of_mem (a - Ξ΅) $ mem_infi_of_mem (sub_lt_self a Ξ΅0) (mem_principal_self _))
(mem_infi_of_mem (Ξ΅ + a) $ mem_infi_of_mem (by simpa) (mem_principal_self _)) },
{ intros b hb,
exact mem_infi_of_mem (a - b) (mem_infi_of_mem (sub_pos.2 hb) (by simp [Ioi])) },
{ intros b hb,
exact mem_infi_of_mem (b - a) (mem_infi_of_mem (sub_pos.2 hb) (by simp [Iio])) }
end
lemma order_topology_of_nhds_abs {Ξ± : Type*} [topological_space Ξ±] [linear_ordered_add_comm_group Ξ±]
(h_nhds : βa:Ξ±, π a = (β¨
r>0, π {b | |a - b| < r})) : order_topology Ξ± :=
begin
refine β¨eq_of_nhds_eq_nhds $ Ξ» a, _β©,
rw [h_nhds],
letI := preorder.topology Ξ±, letI : order_topology Ξ± := β¨rflβ©,
exact (nhds_eq_infi_abs_sub a).symm
end
lemma linear_ordered_add_comm_group.tendsto_nhds {x : filter Ξ²} {a : Ξ±} :
tendsto f x (π a) β β Ξ΅ > (0 : Ξ±), βαΆ b in x, |f b - a| < Ξ΅ :=
by simp [nhds_eq_infi_abs_sub, abs_sub_comm a]
lemma eventually_abs_sub_lt (a : Ξ±) {Ξ΅ : Ξ±} (hΞ΅ : 0 < Ξ΅) : βαΆ x in π a, |x - a| < Ξ΅ :=
(nhds_eq_infi_abs_sub a).symm βΈ mem_infi_of_mem Ξ΅
(mem_infi_of_mem hΞ΅ $ by simp only [abs_sub_comm, mem_principal_self])
@[priority 100] -- see Note [lower instance priority]
instance linear_ordered_add_comm_group.topological_add_group : topological_add_group Ξ± :=
{ continuous_add :=
begin
refine continuous_iff_continuous_at.2 _,
rintro β¨a, bβ©,
refine linear_ordered_add_comm_group.tendsto_nhds.2 (Ξ» Ξ΅ Ξ΅0, _),
rcases dense_or_discrete 0 Ξ΅ with (β¨Ξ΄, Ξ΄0, δΡβ©|β¨hβ, hββ©),
{ -- If there exists `Ξ΄ β (0, Ξ΅)`, then we choose `Ξ΄`-nhd of `a` and `(Ξ΅-Ξ΄)`-nhd of `b`
filter_upwards [prod_is_open.mem_nhds (eventually_abs_sub_lt a Ξ΄0)
(eventually_abs_sub_lt b (sub_pos.2 δΡ))],
rintros β¨x, yβ© β¨hx : |x - a| < Ξ΄, hy : |y - b| < Ξ΅ - Ξ΄β©,
rw [add_sub_comm],
calc |x - a + (y - b)| β€ |x - a| + |y - b| : abs_add _ _
... < Ξ΄ + (Ξ΅ - Ξ΄) : add_lt_add hx hy
... = Ξ΅ : add_sub_cancel'_right _ _ },
{ -- Otherewise `Ξ΅`-nhd of each point `a` is `{a}`
have hΞ΅ : β {x y}, abs (x - y) < Ξ΅ β x = y,
{ intros x y h,
simpa [sub_eq_zero] using hβ _ h },
filter_upwards [prod_is_open.mem_nhds (eventually_abs_sub_lt a Ξ΅0)
(eventually_abs_sub_lt b Ξ΅0)],
rintros β¨x, yβ© β¨hx : |x - a| < Ξ΅, hy : |y - b| < Ξ΅β©,
simpa [hΞ΅ hx, hΞ΅ hy] }
end,
continuous_neg := continuous_iff_continuous_at.2 $ Ξ» a,
linear_ordered_add_comm_group.tendsto_nhds.2 $ Ξ» Ξ΅ Ξ΅0,
(eventually_abs_sub_lt a Ξ΅0).mono $ Ξ» x hx, by rwa [neg_sub_neg, abs_sub_comm] }
@[continuity]
lemma continuous_abs : continuous (abs : Ξ± β Ξ±) := continuous_id.max continuous_neg
lemma filter.tendsto.abs {f : Ξ² β Ξ±} {a : Ξ±} {l : filter Ξ²} (h : tendsto f l (π a)) :
tendsto (Ξ» x, |f x|) l (π (|a|)) :=
(continuous_abs.tendsto _).comp h
lemma nhds_basis_Ioo_pos [no_bot_order Ξ±] [no_top_order Ξ±] (a : Ξ±) :
(π a).has_basis (Ξ» Ξ΅ : Ξ±, (0 : Ξ±) < Ξ΅) (Ξ» Ξ΅, Ioo (a-Ξ΅) (a+Ξ΅)) :=
β¨begin
refine Ξ» t, (nhds_basis_Ioo a).mem_iff.trans β¨_, _β©,
{ rintros β¨β¨l, uβ©, β¨hl : l < a, hu : a < uβ©, h' : Ioo l u β tβ©,
refine β¨min (a-l) (u-a), by apply lt_min; rwa sub_pos, _β©,
rintros x β¨hx, hx'β©,
apply h',
rw [sub_lt, lt_min_iff, sub_lt_sub_iff_left] at hx,
rw [β sub_lt_iff_lt_add', lt_min_iff, sub_lt_sub_iff_right] at hx',
exact β¨hx.1, hx'.2β© },
{ rintros β¨Ξ΅, Ξ΅_pos, hβ©,
exact β¨(a-Ξ΅, a+Ξ΅), by simp [Ξ΅_pos], hβ© },
endβ©
lemma nhds_basis_abs_sub_lt [no_bot_order Ξ±] [no_top_order Ξ±] (a : Ξ±) :
(π a).has_basis (Ξ» Ξ΅ : Ξ±, (0 : Ξ±) < Ξ΅) (Ξ» Ξ΅, {b | abs (b - a) < Ξ΅}) :=
begin
convert nhds_basis_Ioo_pos a,
{ ext Ξ΅,
change abs (x - a) < Ξ΅ β a - Ξ΅ < x β§ x < a + Ξ΅,
simp [abs_lt, sub_lt_iff_lt_add, add_comm Ξ΅ a, add_comm x Ξ΅] }
end
variable (Ξ±)
lemma nhds_basis_zero_abs_sub_lt [no_bot_order Ξ±] [no_top_order Ξ±] :
(π (0 : Ξ±)).has_basis (Ξ» Ξ΅ : Ξ±, (0 : Ξ±) < Ξ΅) (Ξ» Ξ΅, {b | abs b < Ξ΅}) :=
by simpa using nhds_basis_abs_sub_lt (0 : Ξ±)
variable {Ξ±}
/-- If `a` is positive we can form a basis from only nonnegative `Ioo` intervals -/
lemma nhds_basis_Ioo_pos_of_pos [no_bot_order Ξ±] [no_top_order Ξ±]
{a : Ξ±} (ha : 0 < a) :
(π a).has_basis (Ξ» Ξ΅ : Ξ±, (0 : Ξ±) < Ξ΅ β§ Ξ΅ β€ a) (Ξ» Ξ΅, Ioo (a-Ξ΅) (a+Ξ΅)) :=
β¨ Ξ» t, (nhds_basis_Ioo_pos a).mem_iff.trans
β¨Ξ» h, let β¨i, hi, hitβ© := h in
β¨min i a, β¨lt_min hi ha, min_le_right i aβ©, trans (Ioo_subset_Ioo
(sub_le_sub_left (min_le_left i a) a) (add_le_add_left (min_le_left i a) a)) hitβ©,
Ξ» h, let β¨i, hi, hitβ© := h in β¨i, hi.1, hitβ© β© β©
section
variables [topological_space Ξ²] {b : Ξ²} {a : Ξ±} {s : set Ξ²}
lemma continuous.abs (h : continuous f) : continuous (Ξ» x, |f x|) := continuous_abs.comp h
lemma continuous_at.abs (h : continuous_at f b) : continuous_at (Ξ» x, |f x|) b := h.abs
lemma continuous_within_at.abs (h : continuous_within_at f s b) :
continuous_within_at (Ξ» x, |f x|) s b := h.abs
lemma continuous_on.abs (h : continuous_on f s) : continuous_on (Ξ» x, |f x|) s :=
Ξ» x hx, (h x hx).abs
lemma tendsto_abs_nhds_within_zero : tendsto (abs : Ξ± β Ξ±) (π[{0}αΆ] 0) (π[Ioi 0] 0) :=
(continuous_abs.tendsto' (0 : Ξ±) 0 abs_zero).inf $ tendsto_principal_principal.2 $ Ξ» x, abs_pos.2
end
/-- In a linearly ordered additive commutative group with the order topology, if `f` tends to `C`
and `g` tends to `at_top` then `f + g` tends to `at_top`. -/
lemma filter.tendsto.add_at_top {C : Ξ±} (hf : tendsto f l (π C)) (hg : tendsto g l at_top) :
tendsto (Ξ» x, f x + g x) l at_top :=
begin
nontriviality Ξ±,
obtain β¨C', hC'β© : β C', C' < C := no_bot C,
refine tendsto_at_top_add_left_of_le' _ C' _ hg,
exact (hf.eventually (lt_mem_nhds hC')).mono (Ξ» x, le_of_lt)
end
/-- In a linearly ordered additive commutative group with the order topology, if `f` tends to `C`
and `g` tends to `at_bot` then `f + g` tends to `at_bot`. -/
lemma filter.tendsto.add_at_bot {C : Ξ±} (hf : tendsto f l (π C)) (hg : tendsto g l at_bot) :
tendsto (Ξ» x, f x + g x) l at_bot :=
@filter.tendsto.add_at_top (order_dual Ξ±) _ _ _ _ _ _ _ _ hf hg
/-- In a linearly ordered additive commutative group with the order topology, if `f` tends to
`at_top` and `g` tends to `C` then `f + g` tends to `at_top`. -/
lemma filter.tendsto.at_top_add {C : Ξ±} (hf : tendsto f l at_top) (hg : tendsto g l (π C)) :
tendsto (Ξ» x, f x + g x) l at_top :=
by { conv in (_ + _) { rw add_comm }, exact hg.add_at_top hf }
/-- In a linearly ordered additive commutative group with the order topology, if `f` tends to
`at_bot` and `g` tends to `C` then `f + g` tends to `at_bot`. -/
lemma filter.tendsto.at_bot_add {C : Ξ±} (hf : tendsto f l at_bot) (hg : tendsto g l (π C)) :
tendsto (Ξ» x, f x + g x) l at_bot :=
by { conv in (_ + _) { rw add_comm }, exact hg.add_at_bot hf }
end linear_ordered_add_comm_group
section linear_ordered_field
variables [linear_ordered_field Ξ±] [topological_space Ξ±] [order_topology Ξ±]
variables {l : filter Ξ²} {f g : Ξ² β Ξ±}
section continuous_mul
lemma mul_tendsto_nhds_zero_right (x : Ξ±) :
tendsto (uncurry ((*) : Ξ± β Ξ± β Ξ±)) (π 0 ΓαΆ π x) $ π 0 :=
begin
have hx : 0 < 2 * (1 + abs x) := (mul_pos (zero_lt_two) $
lt_of_lt_of_le zero_lt_one $ le_add_of_le_of_nonneg le_rfl (abs_nonneg x)),
rw ((nhds_basis_zero_abs_sub_lt Ξ±).prod $ nhds_basis_abs_sub_lt x).tendsto_iff
(nhds_basis_zero_abs_sub_lt Ξ±),
refine Ξ» Ξ΅ Ξ΅_pos, β¨(Ξ΅/(2 * (1 + abs x)), 1), β¨div_pos Ξ΅_pos hx, zero_lt_oneβ©, _β©,
suffices : β (a b : Ξ±), abs a < Ξ΅ / (2 * (1 + abs x)) β abs (b - x) < 1 β (abs a) * (abs b) < Ξ΅,
by simpa only [and_imp, prod.forall, mem_prod, β abs_mul],
intros a b h h',
refine lt_of_le_of_lt (mul_le_mul_of_nonneg_left _ (abs_nonneg a)) ((lt_div_iff hx).1 h),
calc abs b = abs ((b - x) + x) : by rw sub_add_cancel b x
... β€ abs (b - x) + abs x : abs_add (b - x) x
... β€ 1 + abs x : add_le_add_right (le_of_lt h') (abs x)
... β€ 2 * (1 + abs x) : by linarith,
end
lemma mul_tendsto_nhds_zero_left (x : Ξ±) :
tendsto (uncurry ((*) : Ξ± β Ξ± β Ξ±)) (π x ΓαΆ π 0) $ π 0 :=
begin
intros s hs,
have := mul_tendsto_nhds_zero_right x hs,
rw [filter.mem_map, mem_prod_iff] at this β’,
obtain β¨U, hU, V, hV, hβ© := this,
exact β¨V, hV, U, hU, Ξ» y hy, ((mul_comm y.2 y.1) βΈ
h (β¨hy.2, hy.1β© : (prod.mk y.2 y.1) β (U.prod V)) : y.1 * y.2 β s)β©,
end
lemma nhds_eq_map_mul_left_nhds_one {xβ : Ξ±} (hxβ : xβ β 0) :
π xβ = map (Ξ» x, xβ*x) (π 1) :=
begin
have hxβ' : 0 < abs xβ := abs_pos.2 hxβ,
refine filter.ext (Ξ» t, _),
simp only [exists_prop, set_of_subset_set_of, (nhds_basis_abs_sub_lt xβ).mem_iff,
(nhds_basis_abs_sub_lt (1 : Ξ±)).mem_iff, filter.mem_map'],
refine β¨Ξ» h, _, Ξ» h, _β©,
{ obtain β¨i, hi, hitβ© := h,
refine β¨i / (abs xβ), div_pos hi (abs_pos.2 hxβ), Ξ» x hx, hit _β©,
calc abs (xβ * x - xβ) = abs (xβ * (x - 1)) : congr_arg abs (by ring_nf)
... = abs xβ * abs (x - 1) : abs_mul xβ (x - 1)
... < abs xβ * (i / abs xβ) : mul_lt_mul' le_rfl hx (abs_nonneg (x - 1)) (abs_pos.2 hxβ)
... = abs xβ * i / abs xβ : by ring
... = i : mul_div_cancel_left i (Ξ» h, hxβ (abs_eq_zero.1 h)) },
{ obtain β¨i, hi, hitβ© := h,
refine β¨i * (abs xβ), mul_pos hi (abs_pos.2 hxβ), Ξ» x hx, _β©,
have : abs (x / xβ - 1) < i,
calc abs (x / xβ - 1) = abs (x / xβ - xβ / xβ) : (by rw div_self hxβ)
... = abs ((x - xβ) / xβ) : congr_arg abs (sub_div x xβ xβ).symm
... = abs (x - xβ) / abs xβ : abs_div (x - xβ) xβ
... < i * abs xβ / abs xβ : div_lt_div hx le_rfl
(mul_nonneg (le_of_lt hi) (abs_nonneg xβ)) (abs_pos.2 hxβ)
... = i : by rw [β mul_div_assoc', div_self (ne_of_lt $ abs_pos.2 hxβ).symm, mul_one],
specialize hit (x / xβ) this,
rwa [mul_div_assoc', mul_div_cancel_left x hxβ] at hit }
end
lemma nhds_eq_map_mul_right_nhds_one {xβ : Ξ±} (hxβ : xβ β 0) :
π xβ = map (Ξ» x, x*xβ) (π 1) :=
by simp_rw [mul_comm _ xβ, nhds_eq_map_mul_left_nhds_one hxβ]
lemma mul_tendsto_nhds_one_nhds_one :
tendsto (uncurry ((*) : Ξ± β Ξ± β Ξ±)) (π 1 ΓαΆ π 1) $ π 1 :=
begin
rw ((nhds_basis_Ioo_pos (1 : Ξ±)).prod $ nhds_basis_Ioo_pos (1 : Ξ±)).tendsto_iff
(nhds_basis_Ioo_pos_of_pos (zero_lt_one : (0 : Ξ±) < 1)),
intros Ξ΅ hΞ΅,
have hΞ΅' : 0 β€ 1 - Ξ΅ / 4 := by linarith,
have Ξ΅_pos : 0 < Ξ΅ / 4 := by linarith,
have Ξ΅_pos' : 0 < Ξ΅ / 2 := by linarith,
simp only [and_imp, prod.forall, mem_Ioo, function.uncurry_apply_pair, mem_prod, prod.exists],
refine β¨Ξ΅/4, Ξ΅/4, β¨Ξ΅_pos, Ξ΅_posβ©, Ξ» a b ha ha' hb hb', _β©,
have ha0 : 0 β€ a := le_trans hΞ΅' (le_of_lt ha),
have hb0 : 0 β€ b := le_trans hΞ΅' (le_of_lt hb),
refine β¨lt_of_le_of_lt _ (mul_lt_mul'' ha hb hΞ΅' hΞ΅'),
lt_of_lt_of_le (mul_lt_mul'' ha' hb' ha0 hb0) _β©,
{ calc 1 - Ξ΅ = 1 - Ξ΅ / 2 - Ξ΅/2 : by ring_nf
... β€ 1 - Ξ΅/2 - Ξ΅/2 + (Ξ΅/2)*(Ξ΅/2) : le_add_of_nonneg_right (le_of_lt (mul_pos Ξ΅_pos' Ξ΅_pos'))
... = (1 - Ξ΅/2) * (1 - Ξ΅/2) : by ring_nf
... β€ (1 - Ξ΅/4) * (1 - Ξ΅/4) : mul_le_mul (by linarith) (by linarith) (by linarith) hΞ΅' },
{ calc (1 + Ξ΅/4) * (1 + Ξ΅/4) = 1 + Ξ΅/2 + (Ξ΅/4)*(Ξ΅/4) : by ring_nf
... = 1 + Ξ΅/2 + (Ξ΅ * Ξ΅) / 16 : by ring_nf
... β€ 1 + Ξ΅/2 + Ξ΅/2 : add_le_add_left (div_le_div (le_of_lt hΞ΅.1) (le_trans
((mul_le_mul_left hΞ΅.1).2 hΞ΅.2) (le_of_eq $ mul_one Ξ΅)) zero_lt_two (by linarith)) (1 + Ξ΅/2)
... β€ 1 + Ξ΅ : by ring_nf }
end
@[priority 100]
instance linear_ordered_field.has_continuous_mul : has_continuous_mul Ξ± :=
β¨begin
rw continuous_iff_continuous_at,
rintro β¨xβ, yββ©,
by_cases hxβ : xβ = 0,
{ rw [hxβ, continuous_at, zero_mul, nhds_prod_eq],
exact mul_tendsto_nhds_zero_right yβ },
by_cases hyβ : yβ = 0,
{ rw [hyβ, continuous_at, mul_zero, nhds_prod_eq],
exact mul_tendsto_nhds_zero_left xβ },
have hxy : xβ * yβ β 0 := mul_ne_zero hxβ hyβ,
have key : (Ξ» p : Ξ± Γ Ξ±, xβ * p.1 * (p.2 * yβ)) = ((Ξ» x, xβ*x) β (Ξ» x, x*yβ)) β (uncurry (*)),
{ ext p, simp [uncurry, mul_assoc] },
have keyβ : (Ξ» x, xβ*x) β (Ξ» x, yβ*x) = Ξ» x, (xβ *yβ)*x,
{ ext x, simp },
calc map (uncurry (*)) (π (xβ, yβ))
= map (uncurry (*)) (π xβ ΓαΆ π yβ) : by rw nhds_prod_eq
... = map (Ξ» (p : Ξ± Γ Ξ±), xβ * p.1 * (p.2 * yβ)) ((π 1) ΓαΆ (π 1))
: by rw [uncurry, nhds_eq_map_mul_left_nhds_one hxβ, nhds_eq_map_mul_right_nhds_one hyβ,
prod_map_map_eq, filter.map_map]
... = map ((Ξ» x, xβ * x) β Ξ» x, x * yβ) (map (uncurry (*)) (π 1 ΓαΆ π 1))
: by rw [key, β filter.map_map]
... β€ map ((Ξ» (x : Ξ±), xβ * x) β Ξ» x, x * yβ) (π 1) : map_mono (mul_tendsto_nhds_one_nhds_one)
... = π (xβ*yβ) : by rw [β filter.map_map, β nhds_eq_map_mul_right_nhds_one hyβ,
nhds_eq_map_mul_left_nhds_one hyβ, filter.map_map, keyβ, β nhds_eq_map_mul_left_nhds_one hxy],
endβ©
end continuous_mul
/-- In a linearly ordered field with the order topology, if `f` tends to `at_top` and `g` tends to
a positive constant `C` then `f * g` tends to `at_top`. -/
lemma filter.tendsto.at_top_mul {C : Ξ±} (hC : 0 < C) (hf : tendsto f l at_top)
(hg : tendsto g l (π C)) :
tendsto (Ξ» x, (f x * g x)) l at_top :=
begin
refine tendsto_at_top_mono' _ _ (hf.at_top_mul_const (half_pos hC)),
filter_upwards [hg.eventually (lt_mem_nhds (half_lt_self hC)),
hf.eventually (eventually_ge_at_top 0)],
exact Ξ» x hg hf, mul_le_mul_of_nonneg_left hg.le hf
end
/-- In a linearly ordered field with the order topology, if `f` tends to a positive constant `C` and
`g` tends to `at_top` then `f * g` tends to `at_top`. -/
lemma filter.tendsto.mul_at_top {C : Ξ±} (hC : 0 < C) (hf : tendsto f l (π C))
(hg : tendsto g l at_top) :
tendsto (Ξ» x, (f x * g x)) l at_top :=
by simpa only [mul_comm] using hg.at_top_mul hC hf
/-- In a linearly ordered field with the order topology, if `f` tends to `at_top` and `g` tends to
a negative constant `C` then `f * g` tends to `at_bot`. -/
lemma filter.tendsto.at_top_mul_neg {C : Ξ±} (hC : C < 0) (hf : tendsto f l at_top)
(hg : tendsto g l (π C)) :
tendsto (Ξ» x, (f x * g x)) l at_bot :=
by simpa only [(β), neg_mul_eq_mul_neg, neg_neg]
using tendsto_neg_at_top_at_bot.comp (hf.at_top_mul (neg_pos.2 hC) hg.neg)
/-- In a linearly ordered field with the order topology, if `f` tends to a negative constant `C` and
`g` tends to `at_top` then `f * g` tends to `at_bot`. -/
lemma filter.tendsto.neg_mul_at_top {C : Ξ±} (hC : C < 0) (hf : tendsto f l (π C))
(hg : tendsto g l at_top) :
tendsto (Ξ» x, (f x * g x)) l at_bot :=
by simpa only [mul_comm] using hg.at_top_mul_neg hC hf
/-- In a linearly ordered field with the order topology, if `f` tends to `at_bot` and `g` tends to
a positive constant `C` then `f * g` tends to `at_bot`. -/
lemma filter.tendsto.at_bot_mul {C : Ξ±} (hC : 0 < C) (hf : tendsto f l at_bot)
(hg : tendsto g l (π C)) :
tendsto (Ξ» x, (f x * g x)) l at_bot :=
by simpa [(β)]
using tendsto_neg_at_top_at_bot.comp ((tendsto_neg_at_bot_at_top.comp hf).at_top_mul hC hg)
/-- In a linearly ordered field with the order topology, if `f` tends to `at_bot` and `g` tends to
a negative constant `C` then `f * g` tends to `at_top`. -/
lemma filter.tendsto.at_bot_mul_neg {C : Ξ±} (hC : C < 0) (hf : tendsto f l at_bot)
(hg : tendsto g l (π C)) :
tendsto (Ξ» x, (f x * g x)) l at_top :=
by simpa [(β)]
using tendsto_neg_at_bot_at_top.comp ((tendsto_neg_at_bot_at_top.comp hf).at_top_mul_neg hC hg)
/-- In a linearly ordered field with the order topology, if `f` tends to a positive constant `C` and
`g` tends to `at_bot` then `f * g` tends to `at_bot`. -/
lemma filter.tendsto.mul_at_bot {C : Ξ±} (hC : 0 < C) (hf : tendsto f l (π C))
(hg : tendsto g l at_bot) :
tendsto (Ξ» x, (f x * g x)) l at_bot :=
by simpa only [mul_comm] using hg.at_bot_mul hC hf
/-- In a linearly ordered field with the order topology, if `f` tends to a negative constant `C` and
`g` tends to `at_bot` then `f * g` tends to `at_top`. -/
lemma filter.tendsto.neg_mul_at_bot {C : Ξ±} (hC : C < 0) (hf : tendsto f l (π C))
(hg : tendsto g l at_bot) :
tendsto (Ξ» x, (f x * g x)) l at_top :=
by simpa only [mul_comm] using hg.at_bot_mul_neg hC hf
/-- The function `x β¦ xβ»ΒΉ` tends to `+β` on the right of `0`. -/
lemma tendsto_inv_zero_at_top : tendsto (Ξ»x:Ξ±, xβ»ΒΉ) (π[set.Ioi (0:Ξ±)] 0) at_top :=
begin
refine (at_top_basis' 1).tendsto_right_iff.2 (Ξ» b hb, _),
have hb' : 0 < b := zero_lt_one.trans_le hb,
filter_upwards [Ioc_mem_nhds_within_Ioi β¨le_rfl, inv_pos.2 hb'β©],
exact Ξ» x hx, (le_inv hx.1 hb').1 hx.2
end
/-- The function `r β¦ rβ»ΒΉ` tends to `0` on the right as `r β +β`. -/
lemma tendsto_inv_at_top_zero' : tendsto (Ξ»r:Ξ±, rβ»ΒΉ) at_top (π[set.Ioi (0:Ξ±)] 0) :=
begin
refine (has_basis.tendsto_iff at_top_basis β¨Ξ» s, mem_nhds_within_Ioi_iff_exists_Ioc_subsetβ©).2 _,
refine Ξ» b hb, β¨bβ»ΒΉ, trivial, Ξ» x hx, _β©,
have : 0 < x := lt_of_lt_of_le (inv_pos.2 hb) hx,
exact β¨inv_pos.2 this, (inv_le this hb).2 hxβ©
end
lemma tendsto_inv_at_top_zero : tendsto (Ξ»r:Ξ±, rβ»ΒΉ) at_top (π 0) :=
tendsto_inv_at_top_zero'.mono_right inf_le_left
lemma filter.tendsto.div_at_top [has_continuous_mul Ξ±] {f g : Ξ² β Ξ±} {l : filter Ξ²} {a : Ξ±}
(h : tendsto f l (π a)) (hg : tendsto g l at_top) : tendsto (Ξ» x, f x / g x) l (π 0) :=
by { simp only [div_eq_mul_inv], exact mul_zero a βΈ h.mul (tendsto_inv_at_top_zero.comp hg) }
lemma filter.tendsto.inv_tendsto_at_top (h : tendsto f l at_top) : tendsto (fβ»ΒΉ) l (π 0) :=
tendsto_inv_at_top_zero.comp h
lemma filter.tendsto.inv_tendsto_zero (h : tendsto f l (π[set.Ioi 0] 0)) :
tendsto (fβ»ΒΉ) l at_top :=
tendsto_inv_zero_at_top.comp h
/-- The function `x^(-n)` tends to `0` at `+β` for any positive natural `n`.
A version for positive real powers exists as `tendsto_rpow_neg_at_top`. -/
lemma tendsto_pow_neg_at_top {n : β} (hn : 1 β€ n) : tendsto (Ξ» x : Ξ±, x ^ (-(n:β€))) at_top (π 0) :=
tendsto.congr (Ξ» x, (fpow_neg x n).symm)
(filter.tendsto.inv_tendsto_at_top (by simpa [gpow_coe_nat] using tendsto_pow_at_top hn))
lemma tendsto_fpow_at_top_zero {n : β€} (hn : n < 0) :
tendsto (Ξ» x : Ξ±, x^n) at_top (π 0) :=
begin
have : 1 β€ -n := le_neg.mp (int.le_of_lt_add_one (hn.trans_le (neg_add_self 1).symm.le)),
apply tendsto.congr (show β x : Ξ±, x^-(-n) = x^n, by simp),
lift -n to β using le_of_lt (neg_pos.mpr hn) with N,
exact tendsto_pow_neg_at_top (by exact_mod_cast this)
end
lemma tendsto_const_mul_fpow_at_top_zero {n : β€} {c : Ξ±} (hn : n < 0) :
tendsto (Ξ» x, c * x ^ n) at_top (π 0) :=
(mul_zero c) βΈ (filter.tendsto.const_mul c (tendsto_fpow_at_top_zero hn))
lemma tendsto_const_mul_pow_nhds_iff {n : β} {c d : Ξ±} (hc : c β 0) :
tendsto (Ξ» x : Ξ±, c * x ^ n) at_top (π d) β n = 0 β§ c = d :=
begin
refine β¨Ξ» h, _, Ξ» h, _β©,
{ have hn : n = 0,
{ by_contradiction hn,
have hn : 1 β€ n := nat.succ_le_iff.2 (lt_of_le_of_ne (zero_le _) (ne.symm hn)),
by_cases hc' : 0 < c,
{ have := (tendsto_const_mul_pow_at_top_iff c n).2 β¨hn, hc'β©,
exact not_tendsto_nhds_of_tendsto_at_top this d h },
{ have := (tendsto_neg_const_mul_pow_at_top_iff c n).2 β¨hn, lt_of_le_of_ne (not_lt.1 hc') hcβ©,
exact not_tendsto_nhds_of_tendsto_at_bot this d h } },
have : (Ξ» x : Ξ±, c * x ^ n) = (Ξ» x : Ξ±, c), by simp [hn],
rw [this, tendsto_const_nhds_iff] at h,
exact β¨hn, hβ© },
{ obtain β¨hn, hcdβ© := h,
simpa [hn, hcd] using tendsto_const_nhds }
end
lemma tendsto_const_mul_fpow_at_top_zero_iff {n : β€} {c d : Ξ±} (hc : c β 0) :
tendsto (Ξ» x : Ξ±, c * x ^ n) at_top (π d) β
(n = 0 β§ c = d) β¨ (n < 0 β§ d = 0) :=
begin
refine β¨Ξ» h, _, Ξ» h, _β©,
{ by_cases hn : 0 β€ n,
{ lift n to β using hn,
simp only [gpow_coe_nat] at h,
rw [tendsto_const_mul_pow_nhds_iff hc, β int.coe_nat_eq_zero] at h,
exact or.inl h },
{ rw not_le at hn,
refine or.inr β¨hn, tendsto_nhds_unique h (tendsto_const_mul_fpow_at_top_zero hn)β© } },
{ cases h,
{ simp only [h.left, h.right, gpow_zero, mul_one],
exact tendsto_const_nhds },
{ exact h.2.symm βΈ tendsto_const_mul_fpow_at_top_zero h.1} }
end
end linear_ordered_field
lemma preimage_neg [add_group Ξ±] : preimage (has_neg.neg : Ξ± β Ξ±) = image (has_neg.neg : Ξ± β Ξ±) :=
(image_eq_preimage_of_inverse neg_neg neg_neg).symm
lemma filter.map_neg [add_group Ξ±] : map (has_neg.neg : Ξ± β Ξ±) = comap (has_neg.neg : Ξ± β Ξ±) :=
funext $ assume f, map_eq_comap_of_inverse (funext neg_neg) (funext neg_neg)
section order_topology
variables [topological_space Ξ±] [topological_space Ξ²]
[linear_order Ξ±] [linear_order Ξ²] [order_topology Ξ±] [order_topology Ξ²]
lemma is_lub.frequently_mem {a : Ξ±} {s : set Ξ±} (ha : is_lub s a) (hs : s.nonempty) :
βαΆ x in π[Iic a] a, x β s :=
begin
rcases hs with β¨a', ha'β©,
intro h,
rcases (ha.1 ha').eq_or_lt with (rfl|ha'a),
{ exact h.self_of_nhds_within le_rfl ha' },
{ rcases (mem_nhds_within_Iic_iff_exists_Ioc_subset' ha'a).1 h
with β¨b, hba, hbβ©,
rcases ha.exists_between hba with β¨b', hb's, hb'β©,
exact hb hb' hb's },
end
lemma is_lub.frequently_nhds_mem {a : Ξ±} {s : set Ξ±} (ha : is_lub s a) (hs : s.nonempty) :
βαΆ x in π a, x β s :=
(ha.frequently_mem hs).filter_mono inf_le_left
lemma is_glb.frequently_mem {a : Ξ±} {s : set Ξ±} (ha : is_glb s a) (hs : s.nonempty) :
βαΆ x in π[Ici a] a, x β s :=
@is_lub.frequently_mem (order_dual Ξ±) _ _ _ _ _ ha hs
lemma is_glb.frequently_nhds_mem {a : Ξ±} {s : set Ξ±} (ha : is_glb s a) (hs : s.nonempty) :
βαΆ x in π a, x β s :=
(ha.frequently_mem hs).filter_mono inf_le_left
lemma is_lub.mem_closure {a : Ξ±} {s : set Ξ±} (ha : is_lub s a) (hs : s.nonempty) :
a β closure s :=
(ha.frequently_nhds_mem hs).mem_closure
lemma is_glb.mem_closure {a : Ξ±} {s : set Ξ±} (ha : is_glb s a) (hs : s.nonempty) :
a β closure s :=
(ha.frequently_nhds_mem hs).mem_closure
lemma is_lub.nhds_within_ne_bot {a : Ξ±} {s : set Ξ±} (ha : is_lub s a) (hs : s.nonempty) :
ne_bot (π[s] a) :=
mem_closure_iff_nhds_within_ne_bot.1 (ha.mem_closure hs)
lemma is_glb.nhds_within_ne_bot : β {a : Ξ±} {s : set Ξ±}, is_glb s a β s.nonempty β
ne_bot (π[s] a) :=
@is_lub.nhds_within_ne_bot (order_dual Ξ±) _ _ _
lemma is_lub_of_mem_nhds {s : set Ξ±} {a : Ξ±} {f : filter Ξ±}
(hsa : a β upper_bounds s) (hsf : s β f) [ne_bot (f β π a)] : is_lub s a :=
β¨hsa, assume b hb,
not_lt.1 $ assume hba,
have s β© {a | b < a} β f β π a,
from inter_mem_inf hsf (is_open.mem_nhds (is_open_lt' _) hba),
let β¨x, β¨hxs, hxbβ©β© := filter.nonempty_of_mem this in
have b < b, from lt_of_lt_of_le hxb $ hb hxs,
lt_irrefl b thisβ©
lemma is_lub_of_mem_closure {s : set Ξ±} {a : Ξ±} (hsa : a β upper_bounds s) (hsf : a β closure s) :
is_lub s a :=
begin
rw [mem_closure_iff_cluster_pt, cluster_pt, inf_comm] at hsf,
haveI : (π s β π a).ne_bot := hsf,
exact is_lub_of_mem_nhds hsa (mem_principal_self s),
end
lemma is_glb_of_mem_nhds : β {s : set Ξ±} {a : Ξ±} {f : filter Ξ±},
a β lower_bounds s β s β f β ne_bot (f β π a) β is_glb s a :=
@is_lub_of_mem_nhds (order_dual Ξ±) _ _ _
lemma is_glb_of_mem_closure {s : set Ξ±} {a : Ξ±} (hsa : a β lower_bounds s) (hsf : a β closure s) :
is_glb s a :=
@is_lub_of_mem_closure (order_dual Ξ±) _ _ _ s a hsa hsf
lemma is_lub.mem_upper_bounds_of_tendsto [preorder Ξ³] [topological_space Ξ³]
[order_closed_topology Ξ³] {f : Ξ± β Ξ³} {s : set Ξ±} {a : Ξ±} {b : Ξ³}
(hf : βxβs, βyβs, x β€ y β f x β€ f y) (ha : is_lub s a)
(hb : tendsto f (π[s] a) (π b)) : b β upper_bounds (f '' s) :=
begin
rintro _ β¨x, hx, rflβ©,
replace ha := ha.inter_Ici_of_mem hx,
haveI := ha.nhds_within_ne_bot β¨x, hx, le_rflβ©,
refine ge_of_tendsto (hb.mono_left (nhds_within_mono _ (inter_subset_left s (Ici x)))) _,
exact mem_of_superset self_mem_nhds_within (Ξ» y hy, hf _ hx _ hy.1 hy.2)
end
-- For a version of this theorem in which the convergence considered on the domain `Ξ±` is as
-- `x : Ξ±` tends to infinity, rather than tending to a point `x` in `Ξ±`, see `is_lub_of_tendsto`,
-- below
lemma is_lub.is_lub_of_tendsto [preorder Ξ³] [topological_space Ξ³]
[order_closed_topology Ξ³] {f : Ξ± β Ξ³} {s : set Ξ±} {a : Ξ±} {b : Ξ³}
(hf : βxβs, βyβs, x β€ y β f x β€ f y) (ha : is_lub s a) (hs : s.nonempty)
(hb : tendsto f (π[s] a) (π b)) : is_lub (f '' s) b :=
begin
haveI := ha.nhds_within_ne_bot hs,
exact β¨ha.mem_upper_bounds_of_tendsto hf hb, Ξ» b' hb', le_of_tendsto hb
(mem_of_superset self_mem_nhds_within $ Ξ» x hx, hb' $ mem_image_of_mem _ hx)β©
end
lemma is_glb.mem_lower_bounds_of_tendsto [preorder Ξ³] [topological_space Ξ³]
[order_closed_topology Ξ³] {f : Ξ± β Ξ³} {s : set Ξ±} {a : Ξ±} {b : Ξ³}
(hf : βxβs, βyβs, x β€ y β f x β€ f y) (ha : is_glb s a)
(hb : tendsto f (π[s] a) (π b)) : b β lower_bounds (f '' s) :=
@is_lub.mem_upper_bounds_of_tendsto (order_dual Ξ±) (order_dual Ξ³) _ _ _ _ _ _ _ _ _ _
(Ξ» x hx y hy, hf y hy x hx) ha hb
-- For a version of this theorem in which the convergence considered on the domain `Ξ±` is as
-- `x : Ξ±` tends to negative infinity, rather than tending to a point `x` in `Ξ±`, see
-- `is_glb_of_tendsto`, below
lemma is_glb.is_glb_of_tendsto [preorder Ξ³] [topological_space Ξ³]
[order_closed_topology Ξ³] {f : Ξ± β Ξ³} {s : set Ξ±} {a : Ξ±} {b : Ξ³}
(hf : βxβs, βyβs, x β€ y β f x β€ f y) : is_glb s a β s.nonempty β
tendsto f (π[s] a) (π b) β is_glb (f '' s) b :=
@is_lub.is_lub_of_tendsto (order_dual Ξ±) (order_dual Ξ³) _ _ _ _ _ _ f s a b
(Ξ» x hx y hy, hf y hy x hx)
lemma is_lub.mem_lower_bounds_of_tendsto [preorder Ξ³] [topological_space Ξ³]
[order_closed_topology Ξ³] {f : Ξ± β Ξ³} {s : set Ξ±} {a : Ξ±} {b : Ξ³}
(hf : βxβs, βyβs, x β€ y β f y β€ f x) (ha : is_lub s a)
(hb : tendsto f (π[s] a) (π b)) : b β lower_bounds (f '' s) :=
@is_lub.mem_upper_bounds_of_tendsto Ξ± (order_dual Ξ³) _ _ _ _ _ _ _ _ _ _ hf ha hb
lemma is_lub.is_glb_of_tendsto [preorder Ξ³] [topological_space Ξ³]
[order_closed_topology Ξ³] : β {f : Ξ± β Ξ³} {s : set Ξ±} {a : Ξ±} {b : Ξ³},
(βxβs, βyβs, x β€ y β f y β€ f x) β is_lub s a β s.nonempty β
tendsto f (π[s] a) (π b) β is_glb (f '' s) b :=
@is_lub.is_lub_of_tendsto Ξ± (order_dual Ξ³) _ _ _ _ _ _
lemma is_glb.mem_upper_bounds_of_tendsto [preorder Ξ³] [topological_space Ξ³]
[order_closed_topology Ξ³] {f : Ξ± β Ξ³} {s : set Ξ±} {a : Ξ±} {b : Ξ³}
(hf : βxβs, βyβs, x β€ y β f y β€ f x) (ha : is_glb s a)
(hb : tendsto f (π[s] a) (π b)) : b β upper_bounds (f '' s) :=
@is_glb.mem_lower_bounds_of_tendsto Ξ± (order_dual Ξ³) _ _ _ _ _ _ _ _ _ _ hf ha hb
lemma is_glb.is_lub_of_tendsto [preorder Ξ³] [topological_space Ξ³]
[order_closed_topology Ξ³] : β {f : Ξ± β Ξ³} {s : set Ξ±} {a : Ξ±} {b : Ξ³},
(βxβs, βyβs, x β€ y β f y β€ f x) β is_glb s a β s.nonempty β
tendsto f (π[s] a) (π b) β is_lub (f '' s) b :=
@is_glb.is_glb_of_tendsto Ξ± (order_dual Ξ³) _ _ _ _ _ _
lemma is_lub.mem_of_is_closed {a : Ξ±} {s : set Ξ±} (ha : is_lub s a) (hs : s.nonempty)
(sc : is_closed s) : a β s :=
sc.closure_subset $ ha.mem_closure hs
alias is_lub.mem_of_is_closed β is_closed.is_lub_mem
lemma is_glb.mem_of_is_closed {a : Ξ±} {s : set Ξ±} (ha : is_glb s a) (hs : s.nonempty)
(sc : is_closed s) : a β s :=
sc.closure_subset $ ha.mem_closure hs
alias is_glb.mem_of_is_closed β is_closed.is_glb_mem
/-!
### Existence of sequences tending to Inf or Sup of a given set
-/
lemma is_lub.exists_seq_strict_mono_tendsto_of_not_mem' {t : set Ξ±} {x : Ξ±}
(htx : is_lub t x) (not_mem : x β t) (ht : t.nonempty) (hx : is_countably_generated (π x)) :
β u : β β Ξ±, strict_mono u β§ (β n, u n < x) β§ tendsto u at_top (π x) β§ (β n, u n β t) :=
begin
rcases ht with β¨l, hlβ©,
have hl : l < x,
{ rcases lt_or_eq_of_le (htx.1 hl) with h|rfl,
{ exact h },
{ exact (not_mem hl).elim } },
obtain β¨s, hsβ© : β s : β β set Ξ±, (π x).has_basis (Ξ» (_x : β), true) s :=
let β¨s, hsβ© := hx.exists_antimono_basis in β¨s, hs.to_has_basisβ©,
have : β n k, k < x β β y, Icc y x β s n β§ k < y β§ y < x β§ y β t,
{ assume n k hk,
obtain β¨L, hL, hβ© : β (L : Ξ±) (hL : L β Ico k x), Ioc L x β s n :=
exists_Ioc_subset_of_mem_nhds' (hs.mem_of_mem trivial) hk,
obtain β¨y, hyβ© : β (y : Ξ±), L < y β§ y < x β§ y β t,
{ rcases htx.exists_between' not_mem hL.2 with β¨y, yt, hyβ©,
refine β¨y, hy.1, hy.2, ytβ© },
exact β¨y, Ξ» z hz, h β¨hy.1.trans_le hz.1, hz.2β©, hL.1.trans_lt hy.1, hy.2β© },
choose! f hf using this,
let u : β β Ξ± := Ξ» n, nat.rec_on n (f 0 l) (Ξ» n h, f n.succ h),
have I : β n, u n < x,
{ assume n,
induction n with n IH,
{ exact (hf 0 l hl).2.2.1 },
{ exact (hf n.succ _ IH).2.2.1 } },
have S : strict_mono u := strict_mono_nat_of_lt_succ (Ξ» n, (hf n.succ _ (I n)).2.1),
refine β¨u, S, I, hs.tendsto_right_iff.2 (Ξ» n _, _), (Ξ» n, _)β©,
{ simp only [ge_iff_le, eventually_at_top],
refine β¨n, Ξ» p hp, _β©,
have up : u p β Icc (u n) x := β¨S.monotone hp, (I p).leβ©,
have : Icc (u n) x β s n,
by { cases n, { exact (hf 0 l hl).1 }, { exact (hf n.succ (u n) (I n)).1 } },
exact this up },
{ cases n,
{ exact (hf 0 l hl).2.2.2 },
{ exact (hf n.succ _ (I n)).2.2.2 } }
end
lemma is_lub.exists_seq_monotone_tendsto' {t : set Ξ±} {x : Ξ±}
(htx : is_lub t x) (ht : t.nonempty) (hx : is_countably_generated (π x)) :
β u : β β Ξ±, monotone u β§ (β n, u n β€ x) β§ tendsto u at_top (π x) β§ (β n, u n β t) :=
begin
by_cases h : x β t,
{ exact β¨Ξ» n, x, monotone_const, Ξ» n, le_rfl, tendsto_const_nhds, Ξ» n, hβ© },
{ rcases htx.exists_seq_strict_mono_tendsto_of_not_mem' h ht hx with β¨u, huβ©,
exact β¨u, hu.1.monotone, Ξ» n, (hu.2.1 n).le, hu.2.2β© }
end
lemma is_lub.exists_seq_strict_mono_tendsto_of_not_mem [first_countable_topology Ξ±]
{t : set Ξ±} {x : Ξ±} (htx : is_lub t x) (ht : t.nonempty) (not_mem : x β t) :
β u : β β Ξ±, strict_mono u β§ (β n, u n < x) β§ tendsto u at_top (π x) β§ (β n, u n β t) :=
htx.exists_seq_strict_mono_tendsto_of_not_mem' not_mem ht (is_countably_generated_nhds x)
lemma is_lub.exists_seq_monotone_tendsto [first_countable_topology Ξ±]
{t : set Ξ±} {x : Ξ±} (htx : is_lub t x) (ht : t.nonempty) :
β u : β β Ξ±, monotone u β§ (β n, u n β€ x) β§ tendsto u at_top (π x) β§ (β n, u n β t) :=
htx.exists_seq_monotone_tendsto' ht (is_countably_generated_nhds x)
lemma exists_seq_strict_mono_tendsto' {Ξ± : Type*} [linear_order Ξ±] [topological_space Ξ±]
[densely_ordered Ξ±] [order_topology Ξ±]
[first_countable_topology Ξ±] {x y : Ξ±} (hy : y < x) :
β u : β β Ξ±, strict_mono u β§ (β n, u n < x) β§ tendsto u at_top (π x) :=
begin
have hx : x β Iio x := Ξ» h, (lt_irrefl x h).elim,
have ht : set.nonempty (Iio x) := β¨y, hyβ©,
rcases is_lub_Iio.exists_seq_strict_mono_tendsto_of_not_mem ht hx with β¨u, huβ©,
exact β¨u, hu.1, hu.2.1, hu.2.2.1β©,
end
lemma exists_seq_strict_mono_tendsto [densely_ordered Ξ±] [no_bot_order Ξ±]
[first_countable_topology Ξ±] (x : Ξ±) :
β u : β β Ξ±, strict_mono u β§ (β n, u n < x) β§ tendsto u at_top (π x) :=
begin
obtain β¨y, hyβ© : β y, y < x := no_bot _,
exact exists_seq_strict_mono_tendsto' hy
end
lemma exists_seq_tendsto_Sup {Ξ± : Type*} [conditionally_complete_linear_order Ξ±]
[topological_space Ξ±] [order_topology Ξ±] [first_countable_topology Ξ±]
{S : set Ξ±} (hS : S.nonempty) (hS' : bdd_above S) :
β (u : β β Ξ±), monotone u β§ tendsto u at_top (π (Sup S)) β§ (β n, u n β S) :=
begin
rcases (is_lub_cSup hS hS').exists_seq_monotone_tendsto hS with β¨u, huβ©,
exact β¨u, hu.1, hu.2.2β©,
end
lemma is_glb.exists_seq_strict_mono_tendsto_of_not_mem' {t : set Ξ±} {x : Ξ±}
(htx : is_glb t x) (not_mem : x β t) (ht : t.nonempty) (hx : is_countably_generated (π x)) :
β u : β β Ξ±, (β m n, m < n β u n < u m) β§ (β n, x < u n) β§
tendsto u at_top (π x) β§ (β n, u n β t) :=
@is_lub.exists_seq_strict_mono_tendsto_of_not_mem' (order_dual Ξ±) _ _ _ t x htx not_mem ht hx
lemma is_glb.exists_seq_monotone_tendsto' {t : set Ξ±} {x : Ξ±}
(htx : is_glb t x) (ht : t.nonempty) (hx : is_countably_generated (π x)) :
β u : β β Ξ±, (β m n, m β€ n β u n β€ u m) β§ (β n, x β€ u n) β§
tendsto u at_top (π x) β§ (β n, u n β t) :=
@is_lub.exists_seq_monotone_tendsto' (order_dual Ξ±) _ _ _ t x htx ht hx
lemma is_glb.exists_seq_strict_mono_tendsto_of_not_mem [first_countable_topology Ξ±]
{t : set Ξ±} {x : Ξ±} (htx : is_glb t x) (ht : t.nonempty) (not_mem : x β t) :
β u : β β Ξ±, (β m n, m < n β u n < u m) β§ (β n, x < u n) β§
tendsto u at_top (π x) β§ (β n, u n β t) :=
htx.exists_seq_strict_mono_tendsto_of_not_mem' not_mem ht (is_countably_generated_nhds x)
lemma is_glb.exists_seq_monotone_tendsto [first_countable_topology Ξ±]
{t : set Ξ±} {x : Ξ±} (htx : is_glb t x) (ht : t.nonempty) :
β u : β β Ξ±, (β m n, m β€ n β u n β€ u m) β§ (β n, x β€ u n) β§
tendsto u at_top (π x) β§ (β n, u n β t) :=
htx.exists_seq_monotone_tendsto' ht (is_countably_generated_nhds x)
lemma exists_seq_strict_antimono_tendsto' [densely_ordered Ξ±]
[first_countable_topology Ξ±] {x y : Ξ±} (hy : x < y) :
β u : β β Ξ±, (β m n, m < n β u n < u m) β§ (β n, x < u n) β§ tendsto u at_top (π x) :=
@exists_seq_strict_mono_tendsto' (order_dual Ξ±) _ _ _ _ _ x y hy
lemma exists_seq_strict_antimono_tendsto [densely_ordered Ξ±] [no_top_order Ξ±]
[first_countable_topology Ξ±] (x : Ξ±) :
β u : β β Ξ±, (β m n, m < n β u n < u m) β§ (β n, x < u n) β§ tendsto u at_top (π x) :=
@exists_seq_strict_mono_tendsto (order_dual Ξ±) _ _ _ _ _ _ x
lemma exists_seq_tendsto_Inf {Ξ± : Type*} [conditionally_complete_linear_order Ξ±]
[topological_space Ξ±] [order_topology Ξ±] [first_countable_topology Ξ±]
{S : set Ξ±} (hS : S.nonempty) (hS' : bdd_below S) :
β (u : β β Ξ±), (β m n, m β€ n β u n β€ u m) β§ tendsto u at_top (π (Inf S)) β§ (β n, u n β S) :=
@exists_seq_tendsto_Sup (order_dual Ξ±) _ _ _ _ S hS hS'
/-- A compact set is bounded below -/
lemma is_compact.bdd_below {Ξ± : Type u} [topological_space Ξ±] [linear_order Ξ±]
[order_closed_topology Ξ±] [nonempty Ξ±] {s : set Ξ±} (hs : is_compact s) : bdd_below s :=
begin
by_contra H,
rcases hs.elim_finite_subcover_image (Ξ» x (_ : x β s), @is_open_Ioi _ _ _ _ x) _
with β¨t, st, ft, htβ©,
{ refine H (ft.bdd_below.imp $ Ξ» C hC y hy, _),
rcases mem_bUnion_iff.1 (ht hy) with β¨x, hx, xyβ©,
exact le_trans (hC hx) (le_of_lt xy) },
{ refine Ξ» x hx, mem_bUnion_iff.2 (not_imp_comm.1 _ H),
exact Ξ» h, β¨x, Ξ» y hy, le_of_not_lt (h.imp $ Ξ» ys, β¨_, hy, ysβ©)β© }
end
/-- A compact set is bounded above -/
lemma is_compact.bdd_above {Ξ± : Type u} [topological_space Ξ±] [linear_order Ξ±]
[order_topology Ξ±] : Ξ [nonempty Ξ±] {s : set Ξ±}, is_compact s β bdd_above s :=
@is_compact.bdd_below (order_dual Ξ±) _ _ _
end order_topology
section densely_ordered
variables [topological_space Ξ±] [linear_order Ξ±] [order_topology Ξ±] [densely_ordered Ξ±]
{a b : Ξ±} {s : set Ξ±}
/-- The closure of the interval `(a, +β)` is the closed interval `[a, +β)`, unless `a` is a top
element. -/
lemma closure_Ioi' {a b : Ξ±} (hab : a < b) :
closure (Ioi a) = Ici a :=
begin
apply subset.antisymm,
{ exact closure_minimal Ioi_subset_Ici_self is_closed_Ici },
{ rw [β diff_subset_closure_iff, Ici_diff_Ioi_same, singleton_subset_iff],
exact is_glb_Ioi.mem_closure β¨_, habβ© }
end
/-- The closure of the interval `(a, +β)` is the closed interval `[a, +β)`. -/
@[simp] lemma closure_Ioi (a : Ξ±) [no_top_order Ξ±] :
closure (Ioi a) = Ici a :=
let β¨b, hbβ© := no_top a in closure_Ioi' hb
/-- The closure of the interval `(-β, a)` is the closed interval `(-β, a]`, unless `a` is a bottom
element. -/
lemma closure_Iio' {a b : Ξ±} (hab : b < a) :
closure (Iio a) = Iic a :=
@closure_Ioi' (order_dual Ξ±) _ _ _ _ _ _ hab
/-- The closure of the interval `(-β, a)` is the interval `(-β, a]`. -/
@[simp] lemma closure_Iio (a : Ξ±) [no_bot_order Ξ±] :
closure (Iio a) = Iic a :=
let β¨b, hbβ© := no_bot a in closure_Iio' hb
/-- The closure of the open interval `(a, b)` is the closed interval `[a, b]`. -/
@[simp] lemma closure_Ioo {a b : Ξ±} (hab : a < b) :
closure (Ioo a b) = Icc a b :=
begin
apply subset.antisymm,
{ exact closure_minimal Ioo_subset_Icc_self is_closed_Icc },
{ rw [β diff_subset_closure_iff, Icc_diff_Ioo_same hab.le],
have hab' : (Ioo a b).nonempty, from nonempty_Ioo.2 hab,
simp only [insert_subset, singleton_subset_iff],
exact β¨(is_glb_Ioo hab).mem_closure hab', (is_lub_Ioo hab).mem_closure hab'β© }
end
/-- The closure of the interval `(a, b]` is the closed interval `[a, b]`. -/
@[simp] lemma closure_Ioc {a b : Ξ±} (hab : a < b) :
closure (Ioc a b) = Icc a b :=
begin
apply subset.antisymm,
{ exact closure_minimal Ioc_subset_Icc_self is_closed_Icc },
{ apply subset.trans _ (closure_mono Ioo_subset_Ioc_self),
rw closure_Ioo hab }
end
/-- The closure of the interval `[a, b)` is the closed interval `[a, b]`. -/
@[simp] lemma closure_Ico {a b : Ξ±} (hab : a < b) :
closure (Ico a b) = Icc a b :=
begin
apply subset.antisymm,
{ exact closure_minimal Ico_subset_Icc_self is_closed_Icc },
{ apply subset.trans _ (closure_mono Ioo_subset_Ico_self),
rw closure_Ioo hab }
end
@[simp] lemma interior_Ici [no_bot_order Ξ±] {a : Ξ±} : interior (Ici a) = Ioi a :=
by rw [β compl_Iio, interior_compl, closure_Iio, compl_Iic]
@[simp] lemma interior_Iic [no_top_order Ξ±] {a : Ξ±} : interior (Iic a) = Iio a :=
by rw [β compl_Ioi, interior_compl, closure_Ioi, compl_Ici]
@[simp] lemma interior_Icc [no_bot_order Ξ±] [no_top_order Ξ±] {a b : Ξ±}:
interior (Icc a b) = Ioo a b :=
by rw [β Ici_inter_Iic, interior_inter, interior_Ici, interior_Iic, Ioi_inter_Iio]
@[simp] lemma interior_Ico [no_bot_order Ξ±] {a b : Ξ±} : interior (Ico a b) = Ioo a b :=
by rw [β Ici_inter_Iio, interior_inter, interior_Ici, interior_Iio, Ioi_inter_Iio]
@[simp] lemma interior_Ioc [no_top_order Ξ±] {a b : Ξ±} : interior (Ioc a b) = Ioo a b :=
by rw [β Ioi_inter_Iic, interior_inter, interior_Ioi, interior_Iic, Ioi_inter_Iio]
@[simp] lemma frontier_Ici [no_bot_order Ξ±] {a : Ξ±} : frontier (Ici a) = {a} :=
by simp [frontier]
@[simp] lemma frontier_Iic [no_top_order Ξ±] {a : Ξ±} : frontier (Iic a) = {a} :=
by simp [frontier]
@[simp] lemma frontier_Ioi [no_top_order Ξ±] {a : Ξ±} : frontier (Ioi a) = {a} :=
by simp [frontier]
@[simp] lemma frontier_Iio [no_bot_order Ξ±] {a : Ξ±} : frontier (Iio a) = {a} :=
by simp [frontier]
@[simp] lemma frontier_Icc [no_bot_order Ξ±] [no_top_order Ξ±] {a b : Ξ±} (h : a < b) :
frontier (Icc a b) = {a, b} :=
by simp [frontier, le_of_lt h, Icc_diff_Ioo_same]
@[simp] lemma frontier_Ioo {a b : Ξ±} (h : a < b) : frontier (Ioo a b) = {a, b} :=
by simp [frontier, h, le_of_lt h, Icc_diff_Ioo_same]
@[simp] lemma frontier_Ico [no_bot_order Ξ±] {a b : Ξ±} (h : a < b) : frontier (Ico a b) = {a, b} :=
by simp [frontier, h, le_of_lt h, Icc_diff_Ioo_same]
@[simp] lemma frontier_Ioc [no_top_order Ξ±] {a b : Ξ±} (h : a < b) : frontier (Ioc a b) = {a, b} :=
by simp [frontier, h, le_of_lt h, Icc_diff_Ioo_same]
lemma nhds_within_Ioi_ne_bot' {a b c : Ξ±} (Hβ : a < c) (Hβ : a β€ b) :
ne_bot (π[Ioi a] b) :=
mem_closure_iff_nhds_within_ne_bot.1 $ by { rw [closure_Ioi' Hβ], exact Hβ }
lemma nhds_within_Ioi_ne_bot [no_top_order Ξ±] {a b : Ξ±} (H : a β€ b) :
ne_bot (π[Ioi a] b) :=
let β¨c, hcβ© := no_top a in nhds_within_Ioi_ne_bot' hc H
lemma nhds_within_Ioi_self_ne_bot' {a b : Ξ±} (H : a < b) :
ne_bot (π[Ioi a] a) :=
nhds_within_Ioi_ne_bot' H (le_refl a)
@[instance]
lemma nhds_within_Ioi_self_ne_bot [no_top_order Ξ±] (a : Ξ±) :
ne_bot (π[Ioi a] a) :=
nhds_within_Ioi_ne_bot (le_refl a)
lemma nhds_within_Iio_ne_bot' {a b c : Ξ±} (Hβ : a < c) (Hβ : b β€ c) :
ne_bot (π[Iio c] b) :=
mem_closure_iff_nhds_within_ne_bot.1 $ by { rw [closure_Iio' Hβ], exact Hβ }
lemma nhds_within_Iio_ne_bot [no_bot_order Ξ±] {a b : Ξ±} (H : a β€ b) :
ne_bot (π[Iio b] a) :=
let β¨c, hcβ© := no_bot b in nhds_within_Iio_ne_bot' hc H
lemma nhds_within_Iio_self_ne_bot' {a b : Ξ±} (H : a < b) :
ne_bot (π[Iio b] b) :=
nhds_within_Iio_ne_bot' H (le_refl b)
@[instance]
lemma nhds_within_Iio_self_ne_bot [no_bot_order Ξ±] (a : Ξ±) :
ne_bot (π[Iio a] a) :=
nhds_within_Iio_ne_bot (le_refl a)
lemma right_nhds_within_Ico_ne_bot {a b : Ξ±} (H : a < b) : ne_bot (π[Ico a b] b) :=
(is_lub_Ico H).nhds_within_ne_bot (nonempty_Ico.2 H)
lemma left_nhds_within_Ioc_ne_bot {a b : Ξ±} (H : a < b) : ne_bot (π[Ioc a b] a) :=
(is_glb_Ioc H).nhds_within_ne_bot (nonempty_Ioc.2 H)
lemma left_nhds_within_Ioo_ne_bot {a b : Ξ±} (H : a < b) : ne_bot (π[Ioo a b] a) :=
(is_glb_Ioo H).nhds_within_ne_bot (nonempty_Ioo.2 H)
lemma right_nhds_within_Ioo_ne_bot {a b : Ξ±} (H : a < b) : ne_bot (π[Ioo a b] b) :=
(is_lub_Ioo H).nhds_within_ne_bot (nonempty_Ioo.2 H)
lemma comap_coe_nhds_within_Iio_of_Ioo_subset (hb : s β Iio b)
(hs : s.nonempty β β a < b, Ioo a b β s) :
comap (coe : s β Ξ±) (π[Iio b] b) = at_top :=
begin
nontriviality,
haveI : nonempty s := nontrivial_iff_nonempty.1 βΉ_βΊ,
rcases hs (nonempty_subtype.1 βΉ_βΊ) with β¨a, h, hsβ©,
ext u, split,
{ rintros β¨t, ht, htsβ©,
obtain β¨x, β¨hxa : a β€ x, hxb : x < bβ©, hxt : Ioo x b β tβ© :=
(mem_nhds_within_Iio_iff_exists_mem_Ico_Ioo_subset h).mp ht,
obtain β¨y, hxy, hybβ© := exists_between hxb,
refine mem_of_superset (mem_at_top β¨y, hs β¨hxa.trans_lt hxy, hybβ©β©) _,
rintros β¨z, hzsβ© (hyz : y β€ z),
refine hts (hxt β¨hxy.trans_le _, hb _β©); assumption },
{ intros hu,
obtain β¨x : s, hx : β z, x β€ z β z β uβ© := mem_at_top_sets.1 hu,
exact β¨Ioo x b, Ioo_mem_nhds_within_Iio (right_mem_Ioc.2 $ hb x.2), Ξ» z hz, hx _ hz.1.leβ© }
end
lemma comap_coe_nhds_within_Ioi_of_Ioo_subset (ha : s β Ioi a)
(hs : s.nonempty β β b > a, Ioo a b β s) :
comap (coe : s β Ξ±) (π[Ioi a] a) = at_bot :=
begin
refine @comap_coe_nhds_within_Iio_of_Ioo_subset (order_dual Ξ±) _ _ _ _ _ _ ha (Ξ» h, _),
rcases hs h with β¨b, hab, hβ©,
use [b, hab],
rwa dual_Ioo
end
lemma map_coe_at_top_of_Ioo_subset (hb : s β Iio b)
(hs : β a' < b, β a < b, Ioo a b β s) :
map (coe : s β Ξ±) at_top = π[Iio b] b :=
begin
rcases eq_empty_or_nonempty (Iio b) with (hb'|β¨a, haβ©),
{ rw [filter_eq_bot_of_is_empty at_top, map_bot, hb', nhds_within_empty],
exact β¨Ξ» x, hb'.subset (hb x.2)β© },
{ rw [β comap_coe_nhds_within_Iio_of_Ioo_subset hb (Ξ» _, hs a ha), map_comap_of_mem],
rw subtype.range_coe,
exact (mem_nhds_within_Iio_iff_exists_Ioo_subset' ha).2 (hs a ha) },
end
lemma map_coe_at_bot_of_Ioo_subset (ha : s β Ioi a)
(hs : β b' > a, β b > a, Ioo a b β s) :
map (coe : s β Ξ±) at_bot = (π[Ioi a] a) :=
begin
refine @map_coe_at_top_of_Ioo_subset (order_dual Ξ±) _ _ _ _ a s ha (Ξ» b' hb', _),
rcases hs b' hb' with β¨b, hab, hbsβ©,
use [b, hab],
rwa dual_Ioo
end
/-- The `at_top` filter for an open interval `Ioo a b` comes from the left-neighbourhoods filter at
the right endpoint in the ambient order. -/
lemma comap_coe_Ioo_nhds_within_Iio (a b : Ξ±) :
comap (coe : Ioo a b β Ξ±) (π[Iio b] b) = at_top :=
comap_coe_nhds_within_Iio_of_Ioo_subset Ioo_subset_Iio_self $
Ξ» h, β¨a, nonempty_Ioo.1 h, subset.refl _β©
/-- The `at_bot` filter for an open interval `Ioo a b` comes from the right-neighbourhoods filter at
the left endpoint in the ambient order. -/
lemma comap_coe_Ioo_nhds_within_Ioi (a b : Ξ±) :
comap (coe : Ioo a b β Ξ±) (π[Ioi a] a) = at_bot :=
comap_coe_nhds_within_Ioi_of_Ioo_subset Ioo_subset_Ioi_self $
Ξ» h, β¨b, nonempty_Ioo.1 h, subset.refl _β©
lemma comap_coe_Ioi_nhds_within_Ioi (a : Ξ±) : comap (coe : Ioi a β Ξ±) (π[Ioi a] a) = at_bot :=
comap_coe_nhds_within_Ioi_of_Ioo_subset (subset.refl _) $
Ξ» β¨x, hxβ©, β¨x, hx, Ioo_subset_Ioi_selfβ©
lemma comap_coe_Iio_nhds_within_Iio (a : Ξ±) :
comap (coe : Iio a β Ξ±) (π[Iio a] a) = at_top :=
@comap_coe_Ioi_nhds_within_Ioi (order_dual Ξ±) _ _ _ _ a
@[simp] lemma map_coe_Ioo_at_top {a b : Ξ±} (h : a < b) :
map (coe : Ioo a b β Ξ±) at_top = π[Iio b] b :=
map_coe_at_top_of_Ioo_subset Ioo_subset_Iio_self $ Ξ» _ _, β¨_, h, subset.refl _β©
@[simp] lemma map_coe_Ioo_at_bot {a b : Ξ±} (h : a < b) :
map (coe : Ioo a b β Ξ±) at_bot = π[Ioi a] a :=
map_coe_at_bot_of_Ioo_subset Ioo_subset_Ioi_self $ Ξ» _ _, β¨_, h, subset.refl _β©
@[simp] lemma map_coe_Ioi_at_bot (a : Ξ±) :
map (coe : Ioi a β Ξ±) at_bot = π[Ioi a] a :=
map_coe_at_bot_of_Ioo_subset (subset.refl _) $ Ξ» b hb, β¨b, hb, Ioo_subset_Ioi_selfβ©
@[simp] lemma map_coe_Iio_at_top (a : Ξ±) :
map (coe : Iio a β Ξ±) at_top = π[Iio a] a :=
@map_coe_Ioi_at_bot (order_dual Ξ±) _ _ _ _ _
variables {l : filter Ξ²} {f : Ξ± β Ξ²}
@[simp] lemma tendsto_comp_coe_Ioo_at_top (h : a < b) :
tendsto (Ξ» x : Ioo a b, f x) at_top l β tendsto f (π[Iio b] b) l :=
by rw [β map_coe_Ioo_at_top h, tendsto_map'_iff]
@[simp] lemma tendsto_comp_coe_Ioo_at_bot (h : a < b) :
tendsto (Ξ» x : Ioo a b, f x) at_bot l β tendsto f (π[Ioi a] a) l :=
by rw [β map_coe_Ioo_at_bot h, tendsto_map'_iff]
@[simp] lemma tendsto_comp_coe_Ioi_at_bot :
tendsto (Ξ» x : Ioi a, f x) at_bot l β tendsto f (π[Ioi a] a) l :=
by rw [β map_coe_Ioi_at_bot, tendsto_map'_iff]
@[simp] lemma tendsto_comp_coe_Iio_at_top :
tendsto (Ξ» x : Iio a, f x) at_top l β tendsto f (π[Iio a] a) l :=
by rw [β map_coe_Iio_at_top, tendsto_map'_iff]
@[simp] lemma tendsto_Ioo_at_top {f : Ξ² β Ioo a b} :
tendsto f l at_top β tendsto (Ξ» x, (f x : Ξ±)) l (π[Iio b] b) :=
by rw [β comap_coe_Ioo_nhds_within_Iio, tendsto_comap_iff]
@[simp] lemma tendsto_Ioo_at_bot {f : Ξ² β Ioo a b} :
tendsto f l at_bot β tendsto (Ξ» x, (f x : Ξ±)) l (π[Ioi a] a) :=
by rw [β comap_coe_Ioo_nhds_within_Ioi, tendsto_comap_iff]
@[simp] lemma tendsto_Ioi_at_bot {f : Ξ² β Ioi a} :
tendsto f l at_bot β tendsto (Ξ» x, (f x : Ξ±)) l (π[Ioi a] a) :=
by rw [β comap_coe_Ioi_nhds_within_Ioi, tendsto_comap_iff]
@[simp] lemma tendsto_Iio_at_top {f : Ξ² β Iio a} :
tendsto f l at_top β tendsto (Ξ» x, (f x : Ξ±)) l (π[Iio a] a) :=
by rw [β comap_coe_Iio_nhds_within_Iio, tendsto_comap_iff]
end densely_ordered
section complete_linear_order
variables [complete_linear_order Ξ±] [topological_space Ξ±] [order_topology Ξ±]
[complete_linear_order Ξ²] [topological_space Ξ²] [order_topology Ξ²] [nonempty Ξ³]
lemma Sup_mem_closure {Ξ± : Type u} [topological_space Ξ±] [complete_linear_order Ξ±]
[order_topology Ξ±] {s : set Ξ±} (hs : s.nonempty) :
Sup s β closure s :=
(is_lub_Sup s).mem_closure hs
lemma Inf_mem_closure {Ξ± : Type u} [topological_space Ξ±] [complete_linear_order Ξ±]
[order_topology Ξ±] {s : set Ξ±} (hs : s.nonempty) :
Inf s β closure s :=
(is_glb_Inf s).mem_closure hs
lemma is_closed.Sup_mem {Ξ± : Type u} [topological_space Ξ±] [complete_linear_order Ξ±]
[order_topology Ξ±] {s : set Ξ±} (hs : s.nonempty) (hc : is_closed s) :
Sup s β s :=
(is_lub_Sup s).mem_of_is_closed hs hc
lemma is_closed.Inf_mem {Ξ± : Type u} [topological_space Ξ±] [complete_linear_order Ξ±]
[order_topology Ξ±] {s : set Ξ±} (hs : s.nonempty) (hc : is_closed s) :
Inf s β s :=
(is_glb_Inf s).mem_of_is_closed hs hc
/-- A monotone function continuous at the supremum of a nonempty set sends this supremum to
the supremum of the image of this set. -/
lemma map_Sup_of_continuous_at_of_monotone' {f : Ξ± β Ξ²} {s : set Ξ±} (Cf : continuous_at f (Sup s))
(Mf : monotone f) (hs : s.nonempty) :
f (Sup s) = Sup (f '' s) :=
--This is a particular case of the more general is_lub.is_lub_of_tendsto
((is_lub_Sup _).is_lub_of_tendsto (Ξ» x hx y hy xy, Mf xy) hs $
Cf.mono_left inf_le_left).Sup_eq.symm
/-- A monotone function `s` sending `bot` to `bot` and continuous at the supremum of a set sends
this supremum to the supremum of the image of this set. -/
lemma map_Sup_of_continuous_at_of_monotone {f : Ξ± β Ξ²} {s : set Ξ±} (Cf : continuous_at f (Sup s))
(Mf : monotone f) (fbot : f β₯ = β₯) :
f (Sup s) = Sup (f '' s) :=
begin
cases s.eq_empty_or_nonempty with h h,
{ simp [h, fbot] },
{ exact map_Sup_of_continuous_at_of_monotone' Cf Mf h }
end
/-- A monotone function continuous at the indexed supremum over a nonempty `Sort` sends this indexed
supremum to the indexed supremum of the composition. -/
lemma map_supr_of_continuous_at_of_monotone' {ΞΉ : Sort*} [nonempty ΞΉ] {f : Ξ± β Ξ²} {g : ΞΉ β Ξ±}
(Cf : continuous_at f (supr g)) (Mf : monotone f) :
f (β¨ i, g i) = β¨ i, f (g i) :=
by rw [supr, map_Sup_of_continuous_at_of_monotone' Cf Mf (range_nonempty g), β range_comp, supr]
/-- If a monotone function sending `bot` to `bot` is continuous at the indexed supremum over
a `Sort`, then it sends this indexed supremum to the indexed supremum of the composition. -/
lemma map_supr_of_continuous_at_of_monotone {ΞΉ : Sort*} {f : Ξ± β Ξ²} {g : ΞΉ β Ξ±}
(Cf : continuous_at f (supr g)) (Mf : monotone f) (fbot : f β₯ = β₯) :
f (β¨ i, g i) = β¨ i, f (g i) :=
by rw [supr, map_Sup_of_continuous_at_of_monotone Cf Mf fbot, β range_comp, supr]
/-- A monotone function continuous at the infimum of a nonempty set sends this infimum to
the infimum of the image of this set. -/
lemma map_Inf_of_continuous_at_of_monotone' {f : Ξ± β Ξ²} {s : set Ξ±} (Cf : continuous_at f (Inf s))
(Mf : monotone f) (hs : s.nonempty) :
f (Inf s) = Inf (f '' s) :=
@map_Sup_of_continuous_at_of_monotone' (order_dual Ξ±) (order_dual Ξ²) _ _ _ _ _ _ f s Cf
Mf.order_dual hs
/-- A monotone function `s` sending `top` to `top` and continuous at the infimum of a set sends
this infimum to the infimum of the image of this set. -/
lemma map_Inf_of_continuous_at_of_monotone {f : Ξ± β Ξ²} {s : set Ξ±} (Cf : continuous_at f (Inf s))
(Mf : monotone f) (ftop : f β€ = β€) :
f (Inf s) = Inf (f '' s) :=
@map_Sup_of_continuous_at_of_monotone (order_dual Ξ±) (order_dual Ξ²) _ _ _ _ _ _ f s Cf
Mf.order_dual ftop
/-- A monotone function continuous at the indexed infimum over a nonempty `Sort` sends this indexed
infimum to the indexed infimum of the composition. -/
lemma map_infi_of_continuous_at_of_monotone' {ΞΉ : Sort*} [nonempty ΞΉ] {f : Ξ± β Ξ²} {g : ΞΉ β Ξ±}
(Cf : continuous_at f (infi g)) (Mf : monotone f) :
f (β¨
i, g i) = β¨
i, f (g i) :=
@map_supr_of_continuous_at_of_monotone' (order_dual Ξ±) (order_dual Ξ²) _ _ _ _ _ _ ΞΉ _ f g Cf
Mf.order_dual
/-- If a monotone function sending `top` to `top` is continuous at the indexed infimum over
a `Sort`, then it sends this indexed infimum to the indexed infimum of the composition. -/
lemma map_infi_of_continuous_at_of_monotone {ΞΉ : Sort*} {f : Ξ± β Ξ²} {g : ΞΉ β Ξ±}
(Cf : continuous_at f (infi g)) (Mf : monotone f) (ftop : f β€ = β€) :
f (infi g) = infi (f β g) :=
@map_supr_of_continuous_at_of_monotone (order_dual Ξ±) (order_dual Ξ²) _ _ _ _ _ _ ΞΉ f g Cf
Mf.order_dual ftop
end complete_linear_order
section conditionally_complete_linear_order
variables [conditionally_complete_linear_order Ξ±] [topological_space Ξ±] [order_topology Ξ±]
[conditionally_complete_linear_order Ξ²] [topological_space Ξ²] [order_topology Ξ²] [nonempty Ξ³]
lemma cSup_mem_closure {s : set Ξ±} (hs : s.nonempty) (B : bdd_above s) : Sup s β closure s :=
(is_lub_cSup hs B).mem_closure hs
lemma cInf_mem_closure {s : set Ξ±} (hs : s.nonempty) (B : bdd_below s) : Inf s β closure s :=
(is_glb_cInf hs B).mem_closure hs
lemma is_closed.cSup_mem {s : set Ξ±} (hc : is_closed s) (hs : s.nonempty) (B : bdd_above s) :
Sup s β s :=
(is_lub_cSup hs B).mem_of_is_closed hs hc
lemma is_closed.cInf_mem {s : set Ξ±} (hc : is_closed s) (hs : s.nonempty) (B : bdd_below s) :
Inf s β s :=
(is_glb_cInf hs B).mem_of_is_closed hs hc
/-- If a monotone function is continuous at the supremum of a nonempty bounded above set `s`,
then it sends this supremum to the supremum of the image of `s`. -/
lemma map_cSup_of_continuous_at_of_monotone {f : Ξ± β Ξ²} {s : set Ξ±} (Cf : continuous_at f (Sup s))
(Mf : monotone f) (ne : s.nonempty) (H : bdd_above s) :
f (Sup s) = Sup (f '' s) :=
begin
refine ((is_lub_cSup (ne.image f) (Mf.map_bdd_above H)).unique _).symm,
refine (is_lub_cSup ne H).is_lub_of_tendsto (Ξ»x hx y hy xy, Mf xy) ne _,
exact Cf.mono_left inf_le_left
end
/-- If a monotone function is continuous at the indexed supremum of a bounded function on
a nonempty `Sort`, then it sends this supremum to the supremum of the composition. -/
lemma map_csupr_of_continuous_at_of_monotone {f : Ξ± β Ξ²} {g : Ξ³ β Ξ±}
(Cf : continuous_at f (β¨ i, g i)) (Mf : monotone f) (H : bdd_above (range g)) :
f (β¨ i, g i) = β¨ i, f (g i) :=
by rw [supr, map_cSup_of_continuous_at_of_monotone Cf Mf (range_nonempty _) H, β range_comp, supr]
/-- If a monotone function is continuous at the infimum of a nonempty bounded below set `s`,
then it sends this infimum to the infimum of the image of `s`. -/
lemma map_cInf_of_continuous_at_of_monotone {f : Ξ± β Ξ²} {s : set Ξ±} (Cf : continuous_at f (Inf s))
(Mf : monotone f) (ne : s.nonempty) (H : bdd_below s) :
f (Inf s) = Inf (f '' s) :=
@map_cSup_of_continuous_at_of_monotone (order_dual Ξ±) (order_dual Ξ²) _ _ _ _ _ _ f s Cf
Mf.order_dual ne H
/-- A continuous monotone function sends indexed infimum to indexed infimum in conditionally
complete linear order, under a boundedness assumption. -/
lemma map_cinfi_of_continuous_at_of_monotone {f : Ξ± β Ξ²} {g : Ξ³ β Ξ±}
(Cf : continuous_at f (β¨
i, g i)) (Mf : monotone f) (H : bdd_below (range g)) :
f (β¨
i, g i) = β¨
i, f (g i) :=
@map_csupr_of_continuous_at_of_monotone (order_dual Ξ±) (order_dual Ξ²) _ _ _ _ _ _ _ _ _ _
Cf Mf.order_dual H
/-- A monotone map has a limit to the left of any point `x`, equal to `Sup (f '' (Iio x))`. -/
lemma monotone.tendsto_nhds_within_Iio
{Ξ± : Type*} [linear_order Ξ±] [topological_space Ξ±] [order_topology Ξ±]
{f : Ξ± β Ξ²} (Mf : monotone f) (x : Ξ±) :
tendsto f (π[Iio x] x) (π (Sup (f '' (Iio x)))) :=
begin
rcases eq_empty_or_nonempty (Iio x) with h|h, { simp [h] },
refine tendsto_order.2 β¨Ξ» l hl, _, Ξ» m hm, _β©,
{ obtain β¨z, zx, lzβ© : β (a : Ξ±), a < x β§ l < f a,
by simpa only [mem_image, exists_prop, exists_exists_and_eq_and]
using exists_lt_of_lt_cSup (nonempty_image_iff.2 h) hl,
exact (mem_nhds_within_Iio_iff_exists_Ioo_subset' zx).2
β¨z, zx, Ξ» y hy, lz.trans_le (Mf (hy.1.le))β© },
{ filter_upwards [self_mem_nhds_within],
assume y hy,
apply lt_of_le_of_lt _ hm,
exact le_cSup (Mf.map_bdd_above bdd_above_Iio) (mem_image_of_mem _ hy) }
end
/-- A monotone map has a limit to the right of any point `x`, equal to `Inf (f '' (Ioi x))`. -/
lemma monotone.tendsto_nhds_within_Ioi
{Ξ± : Type*} [linear_order Ξ±] [topological_space Ξ±] [order_topology Ξ±]
{f : Ξ± β Ξ²} (Mf : monotone f) (x : Ξ±) :
tendsto f (π[Ioi x] x) (π (Inf (f '' (Ioi x)))) :=
@monotone.tendsto_nhds_within_Iio (order_dual Ξ²) _ _ _ (order_dual Ξ±) _ _ _ f Mf.order_dual x
/-- A bounded connected subset of a conditionally complete linear order includes the open interval
`(Inf s, Sup s)`. -/
lemma is_connected.Ioo_cInf_cSup_subset {s : set Ξ±} (hs : is_connected s) (hb : bdd_below s)
(ha : bdd_above s) :
Ioo (Inf s) (Sup s) β s :=
Ξ» x hx, let β¨y, ys, hyβ© := (is_glb_lt_iff (is_glb_cInf hs.nonempty hb)).1 hx.1 in
let β¨z, zs, hzβ© := (lt_is_lub_iff (is_lub_cSup hs.nonempty ha)).1 hx.2 in
hs.Icc_subset ys zs β¨le_of_lt hy, le_of_lt hzβ©
lemma eq_Icc_cInf_cSup_of_connected_bdd_closed {s : set Ξ±} (hc : is_connected s) (hb : bdd_below s)
(ha : bdd_above s) (hcl : is_closed s) :
s = Icc (Inf s) (Sup s) :=
subset.antisymm (subset_Icc_cInf_cSup hb ha) $
hc.Icc_subset (hcl.cInf_mem hc.nonempty hb) (hcl.cSup_mem hc.nonempty ha)
lemma is_preconnected.Ioi_cInf_subset {s : set Ξ±} (hs : is_preconnected s) (hb : bdd_below s)
(ha : Β¬bdd_above s) :
Ioi (Inf s) β s :=
begin
have sne : s.nonempty := @nonempty_of_not_bdd_above Ξ± _ s β¨Inf β
β© ha,
intros x hx,
obtain β¨y, ys, hyβ© : β y β s, y < x := (is_glb_lt_iff (is_glb_cInf sne hb)).1 hx,
obtain β¨z, zs, hzβ© : β z β s, x < z := not_bdd_above_iff.1 ha x,
exact hs.Icc_subset ys zs β¨le_of_lt hy, le_of_lt hzβ©
end
lemma is_preconnected.Iio_cSup_subset {s : set Ξ±} (hs : is_preconnected s) (hb : Β¬bdd_below s)
(ha : bdd_above s) :
Iio (Sup s) β s :=
@is_preconnected.Ioi_cInf_subset (order_dual Ξ±) _ _ _ s hs ha hb
/-- A preconnected set in a conditionally complete linear order is either one of the intervals
`[Inf s, Sup s]`, `[Inf s, Sup s)`, `(Inf s, Sup s]`, `(Inf s, Sup s)`, `[Inf s, +β)`,
`(Inf s, +β)`, `(-β, Sup s]`, `(-β, Sup s)`, `(-β, +β)`, or `β
`. The converse statement requires
`Ξ±` to be densely ordererd. -/
lemma is_preconnected.mem_intervals {s : set Ξ±} (hs : is_preconnected s) :
s β ({Icc (Inf s) (Sup s), Ico (Inf s) (Sup s), Ioc (Inf s) (Sup s), Ioo (Inf s) (Sup s),
Ici (Inf s), Ioi (Inf s), Iic (Sup s), Iio (Sup s), univ, β
} : set (set Ξ±)) :=
begin
rcases s.eq_empty_or_nonempty with rfl|hne,
{ apply_rules [or.inr, mem_singleton] },
have hs' : is_connected s := β¨hne, hsβ©,
by_cases hb : bdd_below s; by_cases ha : bdd_above s,
{ rcases mem_Icc_Ico_Ioc_Ioo_of_subset_of_subset (hs'.Ioo_cInf_cSup_subset hb ha)
(subset_Icc_cInf_cSup hb ha) with hs|hs|hs|hs,
{ exact (or.inl hs) },
{ exact (or.inr $ or.inl hs) },
{ exact (or.inr $ or.inr $ or.inl hs) },
{ exact (or.inr $ or.inr $ or.inr $ or.inl hs) } },
{ refine (or.inr $ or.inr $ or.inr $ or.inr _),
cases mem_Ici_Ioi_of_subset_of_subset (hs.Ioi_cInf_subset hb ha) (Ξ» x hx, cInf_le hb hx)
with hs hs,
{ exact or.inl hs },
{ exact or.inr (or.inl hs) } },
{ iterate 6 { apply or.inr },
cases mem_Iic_Iio_of_subset_of_subset (hs.Iio_cSup_subset hb ha) (Ξ» x hx, le_cSup ha hx)
with hs hs,
{ exact or.inl hs },
{ exact or.inr (or.inl hs) } },
{ iterate 8 { apply or.inr },
exact or.inl (hs.eq_univ_of_unbounded hb ha) }
end
/-- A preconnected set is either one of the intervals `Icc`, `Ico`, `Ioc`, `Ioo`, `Ici`, `Ioi`,
`Iic`, `Iio`, or `univ`, or `β
`. The converse statement requires `Ξ±` to be densely ordered. Though
one can represent `β
` as `(Inf s, Inf s)`, we include it into the list of possible cases to improve
readability. -/
lemma set_of_is_preconnected_subset_of_ordered :
{s : set Ξ± | is_preconnected s} β
-- bounded intervals
(range (uncurry Icc) βͺ range (uncurry Ico) βͺ range (uncurry Ioc) βͺ range (uncurry Ioo)) βͺ
-- unbounded intervals and `univ`
(range Ici βͺ range Ioi βͺ range Iic βͺ range Iio βͺ {univ, β
}) :=
begin
intros s hs,
rcases hs.mem_intervals with hs|hs|hs|hs|hs|hs|hs|hs|hs|hs,
{ exact (or.inl $ or.inl $ or.inl $ or.inl β¨(Inf s, Sup s), hs.symmβ©) },
{ exact (or.inl $ or.inl $ or.inl $ or.inr β¨(Inf s, Sup s), hs.symmβ©) },
{ exact (or.inl $ or.inl $ or.inr β¨(Inf s, Sup s), hs.symmβ©) },
{ exact (or.inl $ or.inr β¨(Inf s, Sup s), hs.symmβ©) },
{ exact (or.inr $ or.inl $ or.inl $ or.inl $ or.inl β¨Inf s, hs.symmβ©) },
{ exact (or.inr $ or.inl $ or.inl $ or.inl $ or.inr β¨Inf s, hs.symmβ©) },
{ exact (or.inr $ or.inl $ or.inl $ or.inr β¨Sup s, hs.symmβ©) },
{ exact (or.inr $ or.inl $ or.inr β¨Sup s, hs.symmβ©) },
{ exact (or.inr $ or.inr $ or.inl hs) },
{ exact (or.inr $ or.inr $ or.inr hs) }
end
/-- A "continuous induction principle" for a closed interval: if a set `s` meets `[a, b]`
on a closed subset, contains `a`, and the set `s β© [a, b)` has no maximal point, then `b β s`. -/
lemma is_closed.mem_of_ge_of_forall_exists_gt {a b : Ξ±} {s : set Ξ±} (hs : is_closed (s β© Icc a b))
(ha : a β s) (hab : a β€ b) (hgt : β x β s β© Ico a b, (s β© Ioc x b).nonempty) :
b β s :=
begin
let S := s β© Icc a b,
replace ha : a β S, from β¨ha, left_mem_Icc.2 habβ©,
have Sbd : bdd_above S, from β¨b, Ξ» z hz, hz.2.2β©,
let c := Sup (s β© Icc a b),
have c_mem : c β S, from hs.cSup_mem β¨_, haβ© Sbd,
have c_le : c β€ b, from cSup_le β¨_, haβ© (Ξ» x hx, hx.2.2),
cases eq_or_lt_of_le c_le with hc hc, from hc βΈ c_mem.1,
exfalso,
rcases hgt c β¨c_mem.1, c_mem.2.1, hcβ© with β¨x, xs, cx, xbβ©,
exact not_lt_of_le (le_cSup Sbd β¨xs, le_trans (le_cSup Sbd ha) (le_of_lt cx), xbβ©) cx
end
/-- A "continuous induction principle" for a closed interval: if a set `s` meets `[a, b]`
on a closed subset, contains `a`, and for any `a β€ x < y β€ b`, `x β s`, the set `s β© (x, y]`
is not empty, then `[a, b] β s`. -/
lemma is_closed.Icc_subset_of_forall_exists_gt {a b : Ξ±} {s : set Ξ±} (hs : is_closed (s β© Icc a b))
(ha : a β s) (hgt : β x β s β© Ico a b, β y β Ioi x, (s β© Ioc x y).nonempty) :
Icc a b β s :=
begin
assume y hy,
have : is_closed (s β© Icc a y),
{ suffices : s β© Icc a y = s β© Icc a b β© Icc a y,
{ rw this, exact is_closed.inter hs is_closed_Icc },
rw [inter_assoc],
congr,
exact (inter_eq_self_of_subset_right $ Icc_subset_Icc_right hy.2).symm },
exact is_closed.mem_of_ge_of_forall_exists_gt this ha hy.1
(Ξ» x hx, hgt x β¨hx.1, Ico_subset_Ico_right hy.2 hx.2β© y hx.2.2)
end
section densely_ordered
variables [densely_ordered Ξ±] {a b : Ξ±}
/-- A "continuous induction principle" for a closed interval: if a set `s` meets `[a, b]`
on a closed subset, contains `a`, and for any `x β s β© [a, b)` the set `s` includes some open
neighborhood of `x` within `(x, +β)`, then `[a, b] β s`. -/
lemma is_closed.Icc_subset_of_forall_mem_nhds_within {a b : Ξ±} {s : set Ξ±}
(hs : is_closed (s β© Icc a b)) (ha : a β s)
(hgt : β x β s β© Ico a b, s β π[Ioi x] x) :
Icc a b β s :=
begin
apply hs.Icc_subset_of_forall_exists_gt ha,
rintros x β¨hxs, hxabβ© y hyxb,
have : s β© Ioc x y β π[Ioi x] x,
from inter_mem (hgt x β¨hxs, hxabβ©) (Ioc_mem_nhds_within_Ioi β¨le_refl _, hyxbβ©),
exact (nhds_within_Ioi_self_ne_bot' hxab.2).nonempty_of_mem this
end
/-- A closed interval in a densely ordered conditionally complete linear order is preconnected. -/
lemma is_preconnected_Icc : is_preconnected (Icc a b) :=
is_preconnected_closed_iff.2
begin
rintros s t hs ht hab β¨x, hxβ© β¨y, hyβ©,
wlog hxy : x β€ y := le_total x y using [x y s t, y x t s],
have xyab : Icc x y β Icc a b := Icc_subset_Icc hx.1.1 hy.1.2,
by_contradiction hst,
suffices : Icc x y β s,
from hst β¨y, xyab $ right_mem_Icc.2 hxy, this $ right_mem_Icc.2 hxy, hy.2β©,
apply (is_closed.inter hs is_closed_Icc).Icc_subset_of_forall_mem_nhds_within hx.2,
rintros z β¨zs, hzβ©,
have zt : z β tαΆ, from Ξ» zt, hst β¨z, xyab $ Ico_subset_Icc_self hz, zs, ztβ©,
have : tαΆ β© Ioc z y β π[Ioi z] z,
{ rw [β nhds_within_Ioc_eq_nhds_within_Ioi hz.2],
exact mem_nhds_within.2 β¨tαΆ, ht.is_open_compl, zt, subset.refl _β©},
apply mem_of_superset this,
have : Ioc z y β s βͺ t, from Ξ» w hw, hab (xyab β¨le_trans hz.1 (le_of_lt hw.1), hw.2β©),
exact Ξ» w β¨wt, wzyβ©, (this wzy).elim id (Ξ» h, (wt h).elim)
end
lemma is_preconnected_interval : is_preconnected (interval a b) := is_preconnected_Icc
lemma set.ord_connected.is_preconnected {s : set Ξ±} (h : s.ord_connected) :
is_preconnected s :=
is_preconnected_of_forall_pair $ Ξ» x y hx hy, β¨interval x y, h.interval_subset hx hy,
left_mem_interval, right_mem_interval, is_preconnected_intervalβ©
lemma is_preconnected_iff_ord_connected {s : set Ξ±} :
is_preconnected s β ord_connected s :=
β¨is_preconnected.ord_connected, set.ord_connected.is_preconnectedβ©
lemma is_preconnected_Ici : is_preconnected (Ici a) := ord_connected_Ici.is_preconnected
lemma is_preconnected_Iic : is_preconnected (Iic a) := ord_connected_Iic.is_preconnected
lemma is_preconnected_Iio : is_preconnected (Iio a) := ord_connected_Iio.is_preconnected
lemma is_preconnected_Ioi : is_preconnected (Ioi a) := ord_connected_Ioi.is_preconnected
lemma is_preconnected_Ioo : is_preconnected (Ioo a b) := ord_connected_Ioo.is_preconnected
lemma is_preconnected_Ioc : is_preconnected (Ioc a b) := ord_connected_Ioc.is_preconnected
lemma is_preconnected_Ico : is_preconnected (Ico a b) := ord_connected_Ico.is_preconnected
@[priority 100]
instance ordered_connected_space : preconnected_space Ξ± :=
β¨ord_connected_univ.is_preconnectedβ©
/-- In a dense conditionally complete linear order, the set of preconnected sets is exactly
the set of the intervals `Icc`, `Ico`, `Ioc`, `Ioo`, `Ici`, `Ioi`, `Iic`, `Iio`, `(-β, +β)`,
or `β
`. Though one can represent `β
` as `(Inf s, Inf s)`, we include it into the list of
possible cases to improve readability. -/
lemma set_of_is_preconnected_eq_of_ordered :
{s : set Ξ± | is_preconnected s} =
-- bounded intervals
(range (uncurry Icc) βͺ range (uncurry Ico) βͺ range (uncurry Ioc) βͺ range (uncurry Ioo)) βͺ
-- unbounded intervals and `univ`
(range Ici βͺ range Ioi βͺ range Iic βͺ range Iio βͺ {univ, β
}) :=
begin
refine subset.antisymm set_of_is_preconnected_subset_of_ordered _,
simp only [subset_def, -mem_range, forall_range_iff, uncurry, or_imp_distrib, forall_and_distrib,
mem_union, mem_set_of_eq, insert_eq, mem_singleton_iff, forall_eq, forall_true_iff, and_true,
is_preconnected_Icc, is_preconnected_Ico, is_preconnected_Ioc,
is_preconnected_Ioo, is_preconnected_Ioi, is_preconnected_Iio, is_preconnected_Ici,
is_preconnected_Iic, is_preconnected_univ, is_preconnected_empty],
end
variables {Ξ΄ : Type*} [linear_order Ξ΄] [topological_space Ξ΄] [order_closed_topology Ξ΄]
/-- **Intermediate Value Theorem** for continuous functions on closed intervals, case
`f a β€ t β€ f b`.-/
lemma intermediate_value_Icc {a b : Ξ±} (hab : a β€ b) {f : Ξ± β Ξ΄} (hf : continuous_on f (Icc a b)) :
Icc (f a) (f b) β f '' (Icc a b) :=
is_preconnected_Icc.intermediate_value (left_mem_Icc.2 hab) (right_mem_Icc.2 hab) hf
/-- **Intermediate Value Theorem** for continuous functions on closed intervals, case
`f a β₯ t β₯ f b`.-/
lemma intermediate_value_Icc' {a b : Ξ±} (hab : a β€ b) {f : Ξ± β Ξ΄} (hf : continuous_on f (Icc a b)) :
Icc (f b) (f a) β f '' (Icc a b) :=
is_preconnected_Icc.intermediate_value (right_mem_Icc.2 hab) (left_mem_Icc.2 hab) hf
/-- **Intermediate Value Theorem** for continuous functions on closed intervals, unordered case. -/
lemma intermediate_value_interval {a b : Ξ±} {f : Ξ± β Ξ΄} (hf : continuous_on f (interval a b)) :
interval (f a) (f b) β f '' interval a b :=
by cases le_total (f a) (f b); simp [*, is_preconnected_interval.intermediate_value]
lemma intermediate_value_Ico {a b : Ξ±} (hab : a β€ b) {f : Ξ± β Ξ΄} (hf : continuous_on f (Icc a b)) :
Ico (f a) (f b) β f '' (Ico a b) :=
or.elim (eq_or_lt_of_le hab) (Ξ» he y h, absurd h.2 (not_lt_of_le (he βΈ h.1)))
(Ξ» hlt, @is_preconnected.intermediate_value_Ico _ _ _ _ _ _ _ (is_preconnected_Ico)
_ _ β¨refl a, hltβ© (right_nhds_within_Ico_ne_bot hlt) inf_le_right _ (hf.mono Ico_subset_Icc_self)
_ ((hf.continuous_within_at β¨hab, refl bβ©).mono Ico_subset_Icc_self))
lemma intermediate_value_Ico' {a b : Ξ±} (hab : a β€ b) {f : Ξ± β Ξ΄} (hf : continuous_on f (Icc a b)) :
Ioc (f b) (f a) β f '' (Ico a b) :=
or.elim (eq_or_lt_of_le hab) (Ξ» he y h, absurd h.1 (not_lt_of_le (he βΈ h.2)))
(Ξ» hlt, @is_preconnected.intermediate_value_Ioc _ _ _ _ _ _ _ (is_preconnected_Ico)
_ _ β¨refl a, hltβ© (right_nhds_within_Ico_ne_bot hlt) inf_le_right _ (hf.mono Ico_subset_Icc_self)
_ ((hf.continuous_within_at β¨hab, refl bβ©).mono Ico_subset_Icc_self))
lemma intermediate_value_Ioc {a b : Ξ±} (hab : a β€ b) {f : Ξ± β Ξ΄} (hf : continuous_on f (Icc a b)) :
Ioc (f a) (f b) β f '' (Ioc a b) :=
or.elim (eq_or_lt_of_le hab) (Ξ» he y h, absurd h.2 (not_le_of_lt (he βΈ h.1)))
(Ξ» hlt, @is_preconnected.intermediate_value_Ioc _ _ _ _ _ _ _ (is_preconnected_Ioc)
_ _ β¨hlt, refl bβ© (left_nhds_within_Ioc_ne_bot hlt) inf_le_right _ (hf.mono Ioc_subset_Icc_self)
_ ((hf.continuous_within_at β¨refl a, habβ©).mono Ioc_subset_Icc_self))
lemma intermediate_value_Ioc' {a b : Ξ±} (hab : a β€ b) {f : Ξ± β Ξ΄} (hf : continuous_on f (Icc a b)) :
Ico (f b) (f a) β f '' (Ioc a b) :=
or.elim (eq_or_lt_of_le hab) (Ξ» he y h, absurd h.1 (not_le_of_lt (he βΈ h.2)))
(Ξ» hlt, @is_preconnected.intermediate_value_Ico _ _ _ _ _ _ _ (is_preconnected_Ioc)
_ _ β¨hlt, refl bβ© (left_nhds_within_Ioc_ne_bot hlt) inf_le_right _ (hf.mono Ioc_subset_Icc_self)
_ ((hf.continuous_within_at β¨refl a, habβ©).mono Ioc_subset_Icc_self))
lemma intermediate_value_Ioo {a b : Ξ±} (hab : a β€ b) {f : Ξ± β Ξ΄} (hf : continuous_on f (Icc a b)) :
Ioo (f a) (f b) β f '' (Ioo a b) :=
or.elim (eq_or_lt_of_le hab) (Ξ» he y h, absurd h.2 (not_lt_of_lt (he βΈ h.1)))
(Ξ» hlt, @is_preconnected.intermediate_value_Ioo _ _ _ _ _ _ _ (is_preconnected_Ioo)
_ _ (left_nhds_within_Ioo_ne_bot hlt) (right_nhds_within_Ioo_ne_bot hlt)
inf_le_right inf_le_right _ (hf.mono Ioo_subset_Icc_self)
_ _ ((hf.continuous_within_at β¨refl a, habβ©).mono Ioo_subset_Icc_self)
((hf.continuous_within_at β¨hab, refl bβ©).mono Ioo_subset_Icc_self))
lemma intermediate_value_Ioo' {a b : Ξ±} (hab : a β€ b) {f : Ξ± β Ξ΄} (hf : continuous_on f (Icc a b)) :
Ioo (f b) (f a) β f '' (Ioo a b) :=
or.elim (eq_or_lt_of_le hab) (Ξ» he y h, absurd h.1 (not_lt_of_lt (he βΈ h.2)))
(Ξ» hlt, @is_preconnected.intermediate_value_Ioo _ _ _ _ _ _ _ (is_preconnected_Ioo)
_ _ (right_nhds_within_Ioo_ne_bot hlt) (left_nhds_within_Ioo_ne_bot hlt)
inf_le_right inf_le_right _ (hf.mono Ioo_subset_Icc_self)
_ _ ((hf.continuous_within_at β¨hab, refl bβ©).mono Ioo_subset_Icc_self)
((hf.continuous_within_at β¨refl a, habβ©).mono Ioo_subset_Icc_self))
/-- A continuous function which tendsto `at_top` `at_top` and to `at_bot` `at_bot` is surjective. -/
lemma continuous.surjective {f : Ξ± β Ξ΄} (hf : continuous f) (h_top : tendsto f at_top at_top)
(h_bot : tendsto f at_bot at_bot) :
function.surjective f :=
Ξ» p, mem_range_of_exists_le_of_exists_ge hf
(h_bot.eventually (eventually_le_at_bot p)).exists
(h_top.eventually (eventually_ge_at_top p)).exists
/-- A continuous function which tendsto `at_bot` `at_top` and to `at_top` `at_bot` is surjective. -/
lemma continuous.surjective' {f : Ξ± β Ξ΄} (hf : continuous f) (h_top : tendsto f at_bot at_top)
(h_bot : tendsto f at_top at_bot) :
function.surjective f :=
@continuous.surjective (order_dual Ξ±) _ _ _ _ _ _ _ _ _ hf h_top h_bot
/-- If a function `f : Ξ± β Ξ²` is continuous on a nonempty interval `s`, its restriction to `s`
tends to `at_bot : filter Ξ²` along `at_bot : filter β₯s` and tends to `at_top : filter Ξ²` along
`at_top : filter β₯s`, then the restriction of `f` to `s` is surjective. We formulate the
conclusion as `surj_on f s univ`. -/
lemma continuous_on.surj_on_of_tendsto {f : Ξ± β Ξ²} {s : set Ξ±} [ord_connected s]
(hs : s.nonempty) (hf : continuous_on f s) (hbot : tendsto (Ξ» x : s, f x) at_bot at_bot)
(htop : tendsto (Ξ» x : s, f x) at_top at_top) :
surj_on f s univ :=
by haveI := inhabited_of_nonempty hs.to_subtype;
exact (surj_on_iff_surjective.2 $
(continuous_on_iff_continuous_restrict.1 hf).surjective htop hbot)
/-- If a function `f : Ξ± β Ξ²` is continuous on a nonempty interval `s`, its restriction to `s`
tends to `at_top : filter Ξ²` along `at_bot : filter β₯s` and tends to `at_bot : filter Ξ²` along
`at_top : filter β₯s`, then the restriction of `f` to `s` is surjective. We formulate the
conclusion as `surj_on f s univ`. -/
lemma continuous_on.surj_on_of_tendsto' {f : Ξ± β Ξ²} {s : set Ξ±} [ord_connected s]
(hs : s.nonempty) (hf : continuous_on f s) (hbot : tendsto (Ξ» x : s, f x) at_bot at_top)
(htop : tendsto (Ξ» x : s, f x) at_top at_bot) :
surj_on f s univ :=
@continuous_on.surj_on_of_tendsto Ξ± (order_dual Ξ²) _ _ _ _ _ _ _ _ _ _ hs hf hbot htop
end densely_ordered
/-- A closed interval in a conditionally complete linear order is compact. -/
lemma is_compact_Icc {a b : Ξ±} : is_compact (Icc a b) :=
begin
cases le_or_lt a b with hab hab, swap, { simp [hab] },
refine is_compact_iff_ultrafilter_le_nhds.2 (Ξ» f hf, _),
contrapose! hf,
rw [le_principal_iff],
have hpt : β x β Icc a b, {x} β f,
from Ξ» x hx hxf, hf x hx ((le_pure_iff.2 hxf).trans (pure_le_nhds x)),
set s := {x β Icc a b | Icc a x β f},
have hsb : b β upper_bounds s, from Ξ» x hx, hx.1.2,
have sbd : bdd_above s, from β¨b, hsbβ©,
have ha : a β s, by simp [hpt, hab],
rcases hab.eq_or_lt with rfl|hlt, { exact ha.2 },
set c := Sup s,
have hsc : is_lub s c, from is_lub_cSup β¨a, haβ© sbd,
have hc : c β Icc a b, from β¨hsc.1 ha, hsc.2 hsbβ©,
specialize hf c hc,
have hcs : c β s,
{ cases hc.1.eq_or_lt with heq hlt, { rwa β heq },
refine β¨hc, Ξ» hcf, hf (Ξ» U hU, _)β©,
rcases (mem_nhds_within_Iic_iff_exists_Ioc_subset' hlt).1 (mem_nhds_within_of_mem_nhds hU)
with β¨x, hxc, hxUβ©,
rcases ((hsc.frequently_mem β¨a, haβ©).and_eventually
(Ioc_mem_nhds_within_Iic β¨hxc, le_rflβ©)).exists
with β¨y, β¨hyab, hyfβ©, hyβ©,
refine mem_of_superset(f.diff_mem_iff.2 β¨hcf, hyfβ©) (subset.trans _ hxU),
rw diff_subset_iff,
exact subset.trans Icc_subset_Icc_union_Ioc
(union_subset_union subset.rfl $ Ioc_subset_Ioc_left hy.1.le) },
cases hc.2.eq_or_lt with heq hlt, { rw β heq, exact hcs.2 },
contrapose! hf,
intros U hU,
rcases (mem_nhds_within_Ici_iff_exists_mem_Ioc_Ico_subset hlt).1 (mem_nhds_within_of_mem_nhds hU)
with β¨y, hxy, hyUβ©,
refine mem_of_superset _ hyU, clear_dependent U,
have hy : y β Icc a b, from β¨hc.1.trans hxy.1.le, hxy.2β©,
by_cases hay : Icc a y β f,
{ refine mem_of_superset (f.diff_mem_iff.2 β¨f.diff_mem_iff.2 β¨hay, hcs.2β©, hpt y hyβ©) _,
rw [diff_subset_iff, union_comm, Ico_union_right hxy.1.le, diff_subset_iff],
exact Icc_subset_Icc_union_Icc },
{ exact ((hsc.1 β¨hy, hayβ©).not_lt hxy.1).elim },
end
/-- An unordered closed interval in a conditionally complete linear order is compact. -/
lemma is_compact_interval {a b : Ξ±} : is_compact (interval a b) := is_compact_Icc
lemma is_compact_pi_Icc {ΞΉ : Type*} {Ξ± : ΞΉ β Type*} [Ξ i, conditionally_complete_linear_order (Ξ± i)]
[Ξ i, topological_space (Ξ± i)] [Ξ i, order_topology (Ξ± i)] (a b : Ξ i, Ξ± i) :
is_compact (Icc a b) :=
pi_univ_Icc a b βΈ is_compact_univ_pi $ Ξ» i, is_compact_Icc
instance compact_space_Icc (a b : Ξ±) : compact_space (Icc a b) :=
is_compact_iff_compact_space.mp is_compact_Icc
instance compact_space_pi_Icc {ΞΉ : Type*} {Ξ± : ΞΉ β Type*}
[Ξ i, conditionally_complete_linear_order (Ξ± i)] [Ξ i, topological_space (Ξ± i)]
[Ξ i, order_topology (Ξ± i)] (a b : Ξ i, Ξ± i) : compact_space (Icc a b) :=
is_compact_iff_compact_space.mp (is_compact_pi_Icc a b)
@[priority 100] -- See note [lower instance priority]
instance compact_space_of_complete_linear_order {Ξ± : Type*} [complete_linear_order Ξ±]
[topological_space Ξ±] [order_topology Ξ±] :
compact_space Ξ± :=
β¨by simp only [β Icc_bot_top, is_compact_Icc]β©
lemma is_compact.Inf_mem {s : set Ξ±} (hs : is_compact s) (ne_s : s.nonempty) :
Inf s β s :=
hs.is_closed.cInf_mem ne_s hs.bdd_below
lemma is_compact.Sup_mem {s : set Ξ±} (hs : is_compact s) (ne_s : s.nonempty) :
Sup s β s :=
@is_compact.Inf_mem (order_dual Ξ±) _ _ _ _ hs ne_s
lemma is_compact.is_glb_Inf {s : set Ξ±} (hs : is_compact s) (ne_s : s.nonempty) :
is_glb s (Inf s) :=
is_glb_cInf ne_s hs.bdd_below
lemma is_compact.is_lub_Sup {s : set Ξ±} (hs : is_compact s) (ne_s : s.nonempty) :
is_lub s (Sup s) :=
@is_compact.is_glb_Inf (order_dual Ξ±) _ _ _ _ hs ne_s
lemma is_compact.is_least_Inf {s : set Ξ±} (hs : is_compact s) (ne_s : s.nonempty) :
is_least s (Inf s) :=
β¨hs.Inf_mem ne_s, (hs.is_glb_Inf ne_s).1β©
lemma is_compact.is_greatest_Sup {s : set Ξ±} (hs : is_compact s) (ne_s : s.nonempty) :
is_greatest s (Sup s) :=
@is_compact.is_least_Inf (order_dual Ξ±) _ _ _ _ hs ne_s
lemma is_compact.exists_is_least {s : set Ξ±} (hs : is_compact s) (ne_s : s.nonempty) :
β x, is_least s x :=
β¨_, hs.is_least_Inf ne_sβ©
lemma is_compact.exists_is_greatest {s : set Ξ±} (hs : is_compact s) (ne_s : s.nonempty) :
β x, is_greatest s x :=
β¨_, hs.is_greatest_Sup ne_sβ©
lemma is_compact.exists_is_glb {s : set Ξ±} (hs : is_compact s) (ne_s : s.nonempty) :
β x β s, is_glb s x :=
β¨_, hs.Inf_mem ne_s, hs.is_glb_Inf ne_sβ©
lemma is_compact.exists_is_lub {s : set Ξ±} (hs : is_compact s) (ne_s : s.nonempty) :
β x β s, is_lub s x :=
β¨_, hs.Sup_mem ne_s, hs.is_lub_Sup ne_sβ©
lemma is_compact.exists_Inf_image_eq {Ξ± : Type u} [topological_space Ξ±]
{s : set Ξ±} (hs : is_compact s) (ne_s : s.nonempty) {f : Ξ± β Ξ²} (hf : continuous_on f s) :
β x β s, Inf (f '' s) = f x :=
let β¨x, hxs, hxβ© := (hs.image_of_continuous_on hf).Inf_mem (ne_s.image f)
in β¨x, hxs, hx.symmβ©
lemma is_compact.exists_Sup_image_eq {Ξ± : Type u} [topological_space Ξ±]:
β {s : set Ξ±}, is_compact s β s.nonempty β β {f : Ξ± β Ξ²}, continuous_on f s β
β x β s, Sup (f '' s) = f x :=
@is_compact.exists_Inf_image_eq (order_dual Ξ²) _ _ _ _ _
lemma eq_Icc_of_connected_compact {s : set Ξ±} (hβ : is_connected s) (hβ : is_compact s) :
s = Icc (Inf s) (Sup s) :=
eq_Icc_cInf_cSup_of_connected_bdd_closed hβ hβ.bdd_below hβ.bdd_above hβ.is_closed
/-- The extreme value theorem: a continuous function realizes its minimum on a compact set -/
lemma is_compact.exists_forall_le {Ξ± : Type u} [topological_space Ξ±]
{s : set Ξ±} (hs : is_compact s) (ne_s : s.nonempty) {f : Ξ± β Ξ²} (hf : continuous_on f s) :
βxβs, βyβs, f x β€ f y :=
begin
rcases hs.exists_Inf_image_eq ne_s hf with β¨x, hxs, hxβ©,
refine β¨x, hxs, Ξ» y hy, _β©,
rw β hx,
exact ((hs.image_of_continuous_on hf).is_glb_Inf (ne_s.image f)).1 (mem_image_of_mem _ hy)
end
/-- The extreme value theorem: a continuous function realizes its maximum on a compact set -/
lemma is_compact.exists_forall_ge {Ξ± : Type u} [topological_space Ξ±]:
β {s : set Ξ±}, is_compact s β s.nonempty β β {f : Ξ± β Ξ²}, continuous_on f s β
βxβs, βyβs, f y β€ f x :=
@is_compact.exists_forall_le (order_dual Ξ²) _ _ _ _ _
/-- The extreme value theorem: if a continuous function `f` tends to infinity away from compact
sets, then it has a global minimum. -/
lemma continuous.exists_forall_le {Ξ± : Type*} [topological_space Ξ±] [nonempty Ξ±] {f : Ξ± β Ξ²}
(hf : continuous f) (hlim : tendsto f (cocompact Ξ±) at_top) :
β x, β y, f x β€ f y :=
begin
inhabit Ξ±,
obtain β¨s : set Ξ±, hsc : is_compact s, hsf : β x β s, f (default Ξ±) β€ f xβ© :=
(has_basis_cocompact.tendsto_iff at_top_basis).1 hlim (f $ default Ξ±) trivial,
obtain β¨x, -, hxβ© :=
(hsc.insert (default Ξ±)).exists_forall_le (nonempty_insert _ _) hf.continuous_on,
refine β¨x, Ξ» y, _β©,
by_cases hy : y β s,
exacts [hx y (or.inr hy), (hx _ (or.inl rfl)).trans (hsf y hy)]
end
/-- The extreme value theorem: if a continuous function `f` tends to negative infinity away from
compactx sets, then it has a global maximum. -/
lemma continuous.exists_forall_ge {Ξ± : Type*} [topological_space Ξ±] [nonempty Ξ±] {f : Ξ± β Ξ²}
(hf : continuous f) (hlim : tendsto f (cocompact Ξ±) at_bot) :
β x, β y, f y β€ f x :=
@continuous.exists_forall_le (order_dual Ξ²) _ _ _ _ _ _ _ hf hlim
end conditionally_complete_linear_order
end order_topology
/-!
### Bounded monotone sequences converge
In this section we prove a few theorems of the form βif the range of a monotone function `f : ΞΉ β Ξ±`
admits a least upper bound `a`, then `f x` tends to `a` as `x β β`β, as well as version of this
statement for (conditionally) complete lattices that use `β¨ x, f x` instead of `is_lub`.
These theorems work for linear orders with order topologies as well as their products (both in terms
of `prod` and in terms of function types). In order to reduce code duplication, we introduce two
typeclasses (one for the property formulated above and one for the dual property), prove theorems
assuming one of these typeclasses, and provide instances for linear orders and their products.
-/
/-- We say that `Ξ±` is a `Sup_convergence_class` if the following holds. Let `f : ΞΉ β Ξ±` be a
monotone function, let `a : Ξ±` be a least upper bound of `set.range f`. Then `f x` tends to `π a` as
`x β β` (formally, at the filter `filter.at_top`). We require this for `ΞΉ = (s : set Ξ±)`, `f = coe`
in the definition, then prove it for any `f` in `tendsto_at_top_is_lub`.
This property holds for linear orders with order topology as well as their products. -/
class Sup_convergence_class (Ξ± : Type*) [preorder Ξ±] [topological_space Ξ±] : Prop :=
(tendsto_coe_at_top_is_lub : β (a : Ξ±) (s : set Ξ±), is_lub s a β tendsto (coe : s β Ξ±) at_top (π a))
/-- We say that `Ξ±` is an `Inf_convergence_class` if the following holds. Let `f : ΞΉ β Ξ±` be a
monotone function, let `a : Ξ±` be a greatest lower bound of `set.range f`. Then `f x` tends to `π a`
as `x β -β` (formally, at the filter `filter.at_bot`). We require this for `ΞΉ = (s : set Ξ±)`,
`f = coe` in the definition, then prove it for any `f` in `tendsto_at_bot_is_glb`.
This property holds for linear orders with order topology as well as their products. -/
class Inf_convergence_class (Ξ± : Type*) [preorder Ξ±] [topological_space Ξ±] : Prop :=
(tendsto_coe_at_bot_is_glb : β (a : Ξ±) (s : set Ξ±), is_glb s a β tendsto (coe : s β Ξ±) at_bot (π a))
instance order_dual.Sup_convergence_class [preorder Ξ±] [topological_space Ξ±]
[Inf_convergence_class Ξ±] : Sup_convergence_class (order_dual Ξ±) :=
β¨βΉInf_convergence_class Ξ±βΊ.1β©
instance order_dual.Inf_convergence_class [preorder Ξ±] [topological_space Ξ±]
[Sup_convergence_class Ξ±] : Inf_convergence_class (order_dual Ξ±) :=
β¨βΉSup_convergence_class Ξ±βΊ.1β©
@[priority 100] -- see Note [lower instance priority]
instance linear_order.Sup_convergence_class [topological_space Ξ±] [linear_order Ξ±]
[order_topology Ξ±] : Sup_convergence_class Ξ± :=
begin
refine β¨Ξ» a s ha, tendsto_order.2 β¨Ξ» b hb, _, Ξ» b hb, _β©β©,
{ rcases ha.exists_between hb with β¨c, hcs, bc, bcaβ©,
lift c to s using hcs,
refine (eventually_ge_at_top c).mono (Ξ» x hx, bc.trans_le hx) },
{ exact eventually_of_forall (Ξ» x, (ha.1 x.2).trans_lt hb) }
end
@[priority 100] -- see Note [lower instance priority]
instance linear_order.Inf_convergence_class [topological_space Ξ±] [linear_order Ξ±]
[order_topology Ξ±] : Inf_convergence_class Ξ± :=
show Inf_convergence_class (order_dual $ order_dual Ξ±), from order_dual.Inf_convergence_class
section
variables {ΞΉ : Type*} [preorder ΞΉ] [topological_space Ξ±]
section is_lub
variables [preorder Ξ±] [Sup_convergence_class Ξ±] {f : ΞΉ β Ξ±} {a : Ξ±}
lemma tendsto_at_top_is_lub (h_mono : monotone f) (ha : is_lub (set.range f) a) :
tendsto f at_top (π a) :=
begin
suffices : tendsto (range_factorization f) at_top at_top,
from (Sup_convergence_class.tendsto_coe_at_top_is_lub _ _ ha).comp this,
exact h_mono.range_factorization.tendsto_at_top_at_top (Ξ» b, b.2.imp $ Ξ» a ha, ha.ge)
end
lemma tendsto_at_bot_is_lub (h_mono : monotone (order_dual.to_dual β f))
(ha : is_lub (set.range f) a) : tendsto f at_bot (π a) :=
@tendsto_at_top_is_lub Ξ± (order_dual ΞΉ) _ _ _ _ f a h_mono.order_dual ha
end is_lub
section is_glb
variables [preorder Ξ±] [Inf_convergence_class Ξ±] {f : ΞΉ β Ξ±} {a : Ξ±}
lemma tendsto_at_bot_is_glb (h_mono : monotone f) (ha : is_glb (set.range f) a) :
tendsto f at_bot (π a) :=
@tendsto_at_top_is_lub (order_dual Ξ±) (order_dual ΞΉ) _ _ _ _ f a h_mono.order_dual ha
lemma tendsto_at_top_is_glb (h_mono : monotone (order_dual.to_dual β f))
(ha : is_glb (set.range f) a) :
tendsto f at_top (π a) :=
@tendsto_at_top_is_lub (order_dual Ξ±) ΞΉ _ _ _ _ f a h_mono ha
end is_glb
section csupr
variables [conditionally_complete_lattice Ξ±] [Sup_convergence_class Ξ±] {f : ΞΉ β Ξ±} {a : Ξ±}
lemma tendsto_at_top_csupr (h_mono : monotone f) (hbdd : bdd_above $ range f) :
tendsto f at_top (π (β¨i, f i)) :=
begin
casesI is_empty_or_nonempty ΞΉ,
exacts [tendsto_of_is_empty, tendsto_at_top_is_lub h_mono (is_lub_csupr hbdd)]
end
lemma tendsto_at_bot_csupr (h_mono : monotone (order_dual.to_dual β f))
(hbdd : bdd_above $ range f) :
tendsto f at_bot (π (β¨i, f i)) :=
@tendsto_at_top_csupr Ξ± (order_dual ΞΉ) _ _ _ _ _ h_mono.order_dual hbdd
end csupr
section cinfi
variables [conditionally_complete_lattice Ξ±] [Inf_convergence_class Ξ±] {f : ΞΉ β Ξ±} {a : Ξ±}
lemma tendsto_at_bot_cinfi (h_mono : monotone f) (hbdd : bdd_below $ range f) :
tendsto f at_bot (π (β¨
i, f i)) :=
@tendsto_at_top_csupr (order_dual Ξ±) (order_dual ΞΉ) _ _ _ _ _ h_mono.order_dual hbdd
lemma tendsto_at_top_cinfi (h_mono : monotone (order_dual.to_dual β f))
(hbdd : bdd_below $ range f) :
tendsto f at_top (π (β¨
i, f i)) :=
@tendsto_at_top_csupr (order_dual Ξ±) ΞΉ _ _ _ _ _ h_mono hbdd
end cinfi
section supr
variables [complete_lattice Ξ±] [Sup_convergence_class Ξ±] {f : ΞΉ β Ξ±} {a : Ξ±}
lemma tendsto_at_top_supr (h_mono : monotone f) : tendsto f at_top (π (β¨i, f i)) :=
tendsto_at_top_csupr h_mono (order_top.bdd_above _)
lemma tendsto_at_bot_supr (h_mono : monotone (order_dual.to_dual β f)) :
tendsto f at_bot (π (β¨i, f i)) :=
tendsto_at_bot_csupr h_mono (order_top.bdd_above _)
end supr
section infi
variables [complete_lattice Ξ±] [Inf_convergence_class Ξ±] {f : ΞΉ β Ξ±} {a : Ξ±}
lemma tendsto_at_bot_infi (h_mono : monotone f) : tendsto f at_bot (π (β¨
i, f i)) :=
tendsto_at_bot_cinfi h_mono (order_bot.bdd_below _)
lemma tendsto_at_top_infi (h_mono : monotone (order_dual.to_dual β f)) :
tendsto f at_top (π (β¨
i, f i)) :=
tendsto_at_top_cinfi h_mono (order_bot.bdd_below _)
end infi
end
instance [preorder Ξ±] [preorder Ξ²] [topological_space Ξ±] [topological_space Ξ²]
[Sup_convergence_class Ξ±] [Sup_convergence_class Ξ²] : Sup_convergence_class (Ξ± Γ Ξ²) :=
begin
constructor,
rintro β¨a, bβ© s h,
rw [is_lub_prod, β range_restrict, β range_restrict] at h,
have A : tendsto (Ξ» x : s, (x : Ξ± Γ Ξ²).1) at_top (π a),
from tendsto_at_top_is_lub (monotone_fst.restrict s) h.1,
have B : tendsto (Ξ» x : s, (x : Ξ± Γ Ξ²).2) at_top (π b),
from tendsto_at_top_is_lub (monotone_snd.restrict s) h.2,
convert A.prod_mk_nhds B,
ext1 β¨β¨x, yβ©, hβ©, refl
end
instance [preorder Ξ±] [preorder Ξ²] [topological_space Ξ±] [topological_space Ξ²]
[Inf_convergence_class Ξ±] [Inf_convergence_class Ξ²] : Inf_convergence_class (Ξ± Γ Ξ²) :=
show Inf_convergence_class (order_dual $ (order_dual Ξ± Γ order_dual Ξ²)),
from order_dual.Inf_convergence_class
instance {ΞΉ : Type*} {Ξ± : ΞΉ β Type*} [Ξ i, preorder (Ξ± i)] [Ξ i, topological_space (Ξ± i)]
[Ξ i, Sup_convergence_class (Ξ± i)] : Sup_convergence_class (Ξ i, Ξ± i) :=
begin
refine β¨Ξ» f s h, _β©,
simp only [is_lub_pi, β range_restrict] at h,
exact tendsto_pi.2 (Ξ» i, tendsto_at_top_is_lub ((monotone_eval _).restrict _) (h i))
end
instance {ΞΉ : Type*} {Ξ± : ΞΉ β Type*} [Ξ i, preorder (Ξ± i)] [Ξ i, topological_space (Ξ± i)]
[Ξ i, Inf_convergence_class (Ξ± i)] : Inf_convergence_class (Ξ i, Ξ± i) :=
show Inf_convergence_class (order_dual $ Ξ i, order_dual (Ξ± i)),
from order_dual.Inf_convergence_class
instance pi.Sup_convergence_class' {ΞΉ : Type*} [preorder Ξ±] [topological_space Ξ±]
[Sup_convergence_class Ξ±] : Sup_convergence_class (ΞΉ β Ξ±) :=
pi.Sup_convergence_class
instance pi.Inf_convergence_class' {ΞΉ : Type*} [preorder Ξ±] [topological_space Ξ±]
[Inf_convergence_class Ξ±] : Inf_convergence_class (ΞΉ β Ξ±) :=
pi.Inf_convergence_class
lemma tendsto_of_monotone {ΞΉ Ξ± : Type*} [preorder ΞΉ] [topological_space Ξ±]
[conditionally_complete_linear_order Ξ±] [order_topology Ξ±] {f : ΞΉ β Ξ±} (h_mono : monotone f) :
tendsto f at_top at_top β¨ (β l, tendsto f at_top (π l)) :=
if H : bdd_above (range f) then or.inr β¨_, tendsto_at_top_csupr h_mono Hβ©
else or.inl $ tendsto_at_top_at_top_of_monotone' h_mono H
lemma tendsto_iff_tendsto_subseq_of_monotone {ΞΉβ ΞΉβ Ξ± : Type*} [semilattice_sup ΞΉβ] [preorder ΞΉβ]
[nonempty ΞΉβ] [topological_space Ξ±] [conditionally_complete_linear_order Ξ±] [order_topology Ξ±]
[no_top_order Ξ±] {f : ΞΉβ β Ξ±} {Ο : ΞΉβ β ΞΉβ} {l : Ξ±} (hf : monotone f)
(hg : tendsto Ο at_top at_top) :
tendsto f at_top (π l) β tendsto (f β Ο) at_top (π l) :=
begin
split; intro h,
{ exact h.comp hg },
{ rcases tendsto_of_monotone hf with h' | β¨l', hl'β©,
{ exact (not_tendsto_at_top_of_tendsto_nhds h (h'.comp hg)).elim },
{ rwa tendsto_nhds_unique h (hl'.comp hg) } }
end
/-! The next family of results, such as `is_lub_of_tendsto` and `supr_eq_of_tendsto`, are converses
to the standard fact that bounded monotone functions converge. They state, that if a monotone
function `f` tends to `a` along `at_top`, then that value `a` is a least upper bound for the range
of `f`.
Related theorems above (`is_lub.is_lub_of_tendsto`, `is_glb.is_glb_of_tendsto` etc) cover the case
when `f x` tends to `a` as `x` tends to some point `b` in the domain. -/
lemma monotone.ge_of_tendsto {Ξ± Ξ² : Type*} [topological_space Ξ±] [preorder Ξ±]
[order_closed_topology Ξ±] [semilattice_sup Ξ²] {f : Ξ² β Ξ±} {a : Ξ±} (hf : monotone f)
(ha : tendsto f at_top (π a)) (b : Ξ²) :
f b β€ a :=
begin
haveI : nonempty Ξ² := nonempty.intro b,
exact ge_of_tendsto ha ((eventually_ge_at_top b).mono (Ξ» _ hxy, hf hxy))
end
lemma monotone.le_of_tendsto {Ξ± Ξ² : Type*} [topological_space Ξ±] [preorder Ξ±]
[order_closed_topology Ξ±] [semilattice_inf Ξ²] {f : Ξ² β Ξ±} {a : Ξ±} (hf : monotone f)
(ha : tendsto f at_bot (π a)) (b : Ξ²) :
a β€ f b :=
@monotone.ge_of_tendsto (order_dual Ξ±) (order_dual Ξ²) _ _ _ _ f _ hf.order_dual ha b
lemma is_lub_of_tendsto {Ξ± Ξ² : Type*} [topological_space Ξ±] [preorder Ξ±] [order_closed_topology Ξ±]
[nonempty Ξ²] [semilattice_sup Ξ²] {f : Ξ² β Ξ±} {a : Ξ±} (hf : monotone f)
(ha : tendsto f at_top (π a)) :
is_lub (set.range f) a :=
begin
split,
{ rintros _ β¨b, rflβ©,
exact hf.ge_of_tendsto ha b },
{ exact Ξ» _ hb, le_of_tendsto' ha (Ξ» x, hb (set.mem_range_self x)) }
end
lemma is_glb_of_tendsto {Ξ± Ξ² : Type*} [topological_space Ξ±] [preorder Ξ±] [order_closed_topology Ξ±]
[nonempty Ξ²] [semilattice_inf Ξ²] {f : Ξ² β Ξ±} {a : Ξ±} (hf : monotone f)
(ha : tendsto f at_bot (π a)) :
is_glb (set.range f) a :=
@is_lub_of_tendsto (order_dual Ξ±) (order_dual Ξ²) _ _ _ _ _ _ _ hf.order_dual ha
lemma supr_eq_of_tendsto {Ξ± Ξ²} [topological_space Ξ±] [complete_linear_order Ξ±] [order_topology Ξ±]
[nonempty Ξ²] [semilattice_sup Ξ²] {f : Ξ² β Ξ±} {a : Ξ±} (hf : monotone f) :
tendsto f at_top (π a) β supr f = a :=
tendsto_nhds_unique (tendsto_at_top_supr hf)
lemma infi_eq_of_tendsto {Ξ±} [topological_space Ξ±] [complete_linear_order Ξ±] [order_topology Ξ±]
[nonempty Ξ²] [semilattice_sup Ξ²] {f : Ξ² β Ξ±} {a : Ξ±} (hf : βn m, n β€ m β f m β€ f n) :
tendsto f at_top (π a) β infi f = a :=
tendsto_nhds_unique (tendsto_at_top_infi hf)
lemma supr_eq_supr_subseq_of_monotone {ΞΉβ ΞΉβ Ξ± : Type*} [preorder ΞΉβ] [complete_lattice Ξ±]
{l : filter ΞΉβ} [l.ne_bot] {f : ΞΉβ β Ξ±} {Ο : ΞΉβ β ΞΉβ} (hf : monotone f)
(hΟ : tendsto Ο l at_top) :
(β¨ i, f i) = (β¨ i, f (Ο i)) :=
le_antisymm
(supr_le_supr2 $ Ξ» i, exists_imp_exists (Ξ» j (hj : i β€ Ο j), hf hj)
(hΟ.eventually $ eventually_ge_at_top i).exists)
(supr_le_supr2 $ Ξ» i, β¨Ο i, le_refl _β©)
lemma infi_eq_infi_subseq_of_monotone {ΞΉβ ΞΉβ Ξ± : Type*} [preorder ΞΉβ] [complete_lattice Ξ±]
{l : filter ΞΉβ} [l.ne_bot] {f : ΞΉβ β Ξ±} {Ο : ΞΉβ β ΞΉβ} (hf : monotone f)
(hΟ : tendsto Ο l at_bot) :
(β¨
i, f i) = (β¨
i, f (Ο i)) :=
supr_eq_supr_subseq_of_monotone hf.order_dual hΟ
@[to_additive] lemma tendsto_inv_nhds_within_Ioi [ordered_comm_group Ξ±]
[topological_space Ξ±] [topological_group Ξ±] {a : Ξ±} :
tendsto has_inv.inv (π[Ioi a] a) (π[Iio (aβ»ΒΉ)] (aβ»ΒΉ)) :=
(continuous_inv.tendsto a).inf $ by simp [tendsto_principal_principal]
@[to_additive] lemma tendsto_inv_nhds_within_Iio [ordered_comm_group Ξ±]
[topological_space Ξ±] [topological_group Ξ±] {a : Ξ±} :
tendsto has_inv.inv (π[Iio a] a) (π[Ioi (aβ»ΒΉ)] (aβ»ΒΉ)) :=
(continuous_inv.tendsto a).inf $ by simp [tendsto_principal_principal]
@[to_additive] lemma tendsto_inv_nhds_within_Ioi_inv [ordered_comm_group Ξ±]
[topological_space Ξ±] [topological_group Ξ±] {a : Ξ±} :
tendsto has_inv.inv (π[Ioi (aβ»ΒΉ)] (aβ»ΒΉ)) (π[Iio a] a) :=
by simpa only [inv_inv] using @tendsto_inv_nhds_within_Ioi _ _ _ _ (aβ»ΒΉ)
@[to_additive] lemma tendsto_inv_nhds_within_Iio_inv [ordered_comm_group Ξ±]
[topological_space Ξ±] [topological_group Ξ±] {a : Ξ±} :
tendsto has_inv.inv (π[Iio (aβ»ΒΉ)] (aβ»ΒΉ)) (π[Ioi a] a) :=
by simpa only [inv_inv] using @tendsto_inv_nhds_within_Iio _ _ _ _ (aβ»ΒΉ)
@[to_additive] lemma tendsto_inv_nhds_within_Ici [ordered_comm_group Ξ±]
[topological_space Ξ±] [topological_group Ξ±] {a : Ξ±} :
tendsto has_inv.inv (π[Ici a] a) (π[Iic (aβ»ΒΉ)] (aβ»ΒΉ)) :=
(continuous_inv.tendsto a).inf $ by simp [tendsto_principal_principal]
@[to_additive] lemma tendsto_inv_nhds_within_Iic [ordered_comm_group Ξ±]
[topological_space Ξ±] [topological_group Ξ±] {a : Ξ±} :
tendsto has_inv.inv (π[Iic a] a) (π[Ici (aβ»ΒΉ)] (aβ»ΒΉ)) :=
(continuous_inv.tendsto a).inf $ by simp [tendsto_principal_principal]
@[to_additive] lemma tendsto_inv_nhds_within_Ici_inv [ordered_comm_group Ξ±]
[topological_space Ξ±] [topological_group Ξ±] {a : Ξ±} :
tendsto has_inv.inv (π[Ici (aβ»ΒΉ)] (aβ»ΒΉ)) (π[Iic a] a) :=
by simpa only [inv_inv] using @tendsto_inv_nhds_within_Ici _ _ _ _ (aβ»ΒΉ)
@[to_additive] lemma tendsto_inv_nhds_within_Iic_inv [ordered_comm_group Ξ±]
[topological_space Ξ±] [topological_group Ξ±] {a : Ξ±} :
tendsto has_inv.inv (π[Iic (aβ»ΒΉ)] (aβ»ΒΉ)) (π[Ici a] a) :=
by simpa only [inv_inv] using @tendsto_inv_nhds_within_Iic _ _ _ _ (aβ»ΒΉ)
lemma nhds_left_sup_nhds_right (a : Ξ±) [topological_space Ξ±] [linear_order Ξ±] :
π[Iic a] a β π[Ici a] a = π a :=
by rw [β nhds_within_union, Iic_union_Ici, nhds_within_univ]
lemma nhds_left'_sup_nhds_right (a : Ξ±) [topological_space Ξ±] [linear_order Ξ±] :
π[Iio a] a β π[Ici a] a = π a :=
by rw [β nhds_within_union, Iio_union_Ici, nhds_within_univ]
lemma nhds_left_sup_nhds_right' (a : Ξ±) [topological_space Ξ±] [linear_order Ξ±] :
π[Iic a] a β π[Ioi a] a = π a :=
by rw [β nhds_within_union, Iic_union_Ioi, nhds_within_univ]
lemma continuous_at_iff_continuous_left_right [topological_space Ξ±] [linear_order Ξ±]
[topological_space Ξ²] {a : Ξ±} {f : Ξ± β Ξ²} :
continuous_at f a β continuous_within_at f (Iic a) a β§ continuous_within_at f (Ici a) a :=
by simp only [continuous_within_at, continuous_at, β tendsto_sup, nhds_left_sup_nhds_right]
lemma continuous_within_at_Ioi_iff_Ici {Ξ± Ξ² : Type*} [topological_space Ξ±] [partial_order Ξ±]
[topological_space Ξ²] {a : Ξ±} {f : Ξ± β Ξ²} :
continuous_within_at f (Ioi a) a β continuous_within_at f (Ici a) a :=
by simp only [β Ici_diff_left, continuous_within_at_diff_self]
lemma continuous_within_at_Iio_iff_Iic
{Ξ± Ξ² : Type*} [topological_space Ξ±] [linear_order Ξ±] [topological_space Ξ²] {a : Ξ±} {f : Ξ± β Ξ²} :
continuous_within_at f (Iio a) a β continuous_within_at f (Iic a) a :=
begin
have := @continuous_within_at_Ioi_iff_Ici (order_dual Ξ±) _ _ _ _ _ f,
erw [dual_Ici, dual_Ioi] at this,
exact this,
end
lemma continuous_at_iff_continuous_left'_right' [topological_space Ξ±] [linear_order Ξ±]
[topological_space Ξ²] {a : Ξ±} {f : Ξ± β Ξ²} :
continuous_at f a β continuous_within_at f (Iio a) a β§ continuous_within_at f (Ioi a) a :=
by rw [continuous_within_at_Ioi_iff_Ici, continuous_within_at_Iio_iff_Iic,
continuous_at_iff_continuous_left_right]
/-!
### Continuity of monotone functions
In this section we prove the following fact: if `f` is a monotone function on a neighborhood of `a`
and the image of this neighborhood is a neighborhood of `f a`, then `f` is continuous at `a`, see
`continuous_at_of_mono_incr_on_of_image_mem_nhds`, as well as several similar facts.
-/
section linear_order
variables [linear_order Ξ±] [topological_space Ξ±] [order_topology Ξ±]
variables [linear_order Ξ²] [topological_space Ξ²] [order_topology Ξ²]
/-- If `f` is a function strictly monotonically increasing on a right neighborhood of `a` and the
image of this neighborhood under `f` meets every interval `(f a, b]`, `b > f a`, then `f` is
continuous at `a` from the right.
The assumption `hfs : β b > f a, β c β s, f c β Ioc (f a) b` is required because otherwise the
function `f : β β β` given by `f x = if x β€ 0 then x else x + 1` would be a counter-example at
`a = 0`. -/
lemma strict_mono_incr_on.continuous_at_right_of_exists_between {f : Ξ± β Ξ²} {s : set Ξ±} {a : Ξ±}
(h_mono : strict_mono_incr_on f s) (hs : s β π[Ici a] a)
(hfs : β b > f a, β c β s, f c β Ioc (f a) b) :
continuous_within_at f (Ici a) a :=
begin
have ha : a β Ici a := left_mem_Ici,
have has : a β s := mem_of_mem_nhds_within ha hs,
refine tendsto_order.2 β¨Ξ» b hb, _, Ξ» b hb, _β©,
{ filter_upwards [hs, self_mem_nhds_within],
intros x hxs hxa,
exact hb.trans_le ((h_mono.le_iff_le has hxs).2 hxa) },
{ rcases hfs b hb with β¨c, hcs, hac, hcbβ©,
rw [h_mono.lt_iff_lt has hcs] at hac,
filter_upwards [hs, Ico_mem_nhds_within_Ici (left_mem_Ico.2 hac)],
rintros x hx β¨hax, hxcβ©,
exact ((h_mono.lt_iff_lt hx hcs).2 hxc).trans_le hcb }
end
/-- If `f` is a function monotonically increasing function on a right neighborhood of `a` and the
image of this neighborhood under `f` meets every interval `(f a, b)`, `b > f a`, then `f` is
continuous at `a` from the right.
The assumption `hfs : β b > f a, β c β s, f c β Ioo (f a) b` cannot be replaced by the weaker
assumption `hfs : β b > f a, β c β s, f c β Ioc (f a) b` we use for strictly monotone functions
because otherwise the function `ceil : β β β€` would be a counter-example at `a = 0`. -/
lemma continuous_at_right_of_mono_incr_on_of_exists_between {f : Ξ± β Ξ²} {s : set Ξ±} {a : Ξ±}
(h_mono : β (x β s) (y β s), x β€ y β f x β€ f y) (hs : s β π[Ici a] a)
(hfs : β b > f a, β c β s, f c β Ioo (f a) b) :
continuous_within_at f (Ici a) a :=
begin
have ha : a β Ici a := left_mem_Ici,
have has : a β s := mem_of_mem_nhds_within ha hs,
refine tendsto_order.2 β¨Ξ» b hb, _, Ξ» b hb, _β©,
{ filter_upwards [hs, self_mem_nhds_within],
intros x hxs hxa,
exact hb.trans_le (h_mono _ has _ hxs hxa) },
{ rcases hfs b hb with β¨c, hcs, hac, hcbβ©,
have : a < c, from not_le.1 (Ξ» h, hac.not_le $ h_mono _ hcs _ has h),
filter_upwards [hs, Ico_mem_nhds_within_Ici (left_mem_Ico.2 this)],
rintros x hx β¨hax, hxcβ©,
exact (h_mono _ hx _ hcs hxc.le).trans_lt hcb }
end
/-- If a function `f` with a densely ordered codomain is monotonically increasing on a right
neighborhood of `a` and the closure of the image of this neighborhood under `f` is a right
neighborhood of `f a`, then `f` is continuous at `a` from the right. -/
lemma continuous_at_right_of_mono_incr_on_of_closure_image_mem_nhds_within [densely_ordered Ξ²]
{f : Ξ± β Ξ²} {s : set Ξ±} {a : Ξ±} (h_mono : β (x β s) (y β s), x β€ y β f x β€ f y)
(hs : s β π[Ici a] a) (hfs : closure (f '' s) β π[Ici (f a)] (f a)) :
continuous_within_at f (Ici a) a :=
begin
refine continuous_at_right_of_mono_incr_on_of_exists_between h_mono hs (Ξ» b hb, _),
rcases (mem_nhds_within_Ici_iff_exists_mem_Ioc_Ico_subset hb).1 hfs with β¨b', β¨hab', hbb'β©, hb'β©,
rcases exists_between hab' with β¨c', hc'β©,
rcases mem_closure_iff.1 (hb' β¨hc'.1.le, hc'.2β©) (Ioo (f a) b') is_open_Ioo hc'
with β¨_, hc, β¨c, hcs, rflβ©β©,
exact β¨c, hcs, hc.1, hc.2.trans_le hbb'β©
end
/-- If a function `f` with a densely ordered codomain is monotonically increasing on a right
neighborhood of `a` and the image of this neighborhood under `f` is a right neighborhood of `f a`,
then `f` is continuous at `a` from the right. -/
lemma continuous_at_right_of_mono_incr_on_of_image_mem_nhds_within [densely_ordered Ξ²] {f : Ξ± β Ξ²}
{s : set Ξ±} {a : Ξ±} (h_mono : β (x β s) (y β s), x β€ y β f x β€ f y) (hs : s β π[Ici a] a)
(hfs : f '' s β π[Ici (f a)] (f a)) :
continuous_within_at f (Ici a) a :=
continuous_at_right_of_mono_incr_on_of_closure_image_mem_nhds_within h_mono hs $
mem_of_superset hfs subset_closure
/-- If a function `f` with a densely ordered codomain is strictly monotonically increasing on a
right neighborhood of `a` and the closure of the image of this neighborhood under `f` is a right
neighborhood of `f a`, then `f` is continuous at `a` from the right. -/
lemma strict_mono_incr_on.continuous_at_right_of_closure_image_mem_nhds_within [densely_ordered Ξ²]
{f : Ξ± β Ξ²} {s : set Ξ±} {a : Ξ±} (h_mono : strict_mono_incr_on f s) (hs : s β π[Ici a] a)
(hfs : closure (f '' s) β π[Ici (f a)] (f a)) :
continuous_within_at f (Ici a) a :=
continuous_at_right_of_mono_incr_on_of_closure_image_mem_nhds_within
(Ξ» x hx y hy, (h_mono.le_iff_le hx hy).2) hs hfs
/-- If a function `f` with a densely ordered codomain is strictly monotonically increasing on a
right neighborhood of `a` and the image of this neighborhood under `f` is a right neighborhood of
`f a`, then `f` is continuous at `a` from the right. -/
lemma strict_mono_incr_on.continuous_at_right_of_image_mem_nhds_within [densely_ordered Ξ²]
{f : Ξ± β Ξ²} {s : set Ξ±} {a : Ξ±} (h_mono : strict_mono_incr_on f s) (hs : s β π[Ici a] a)
(hfs : f '' s β π[Ici (f a)] (f a)) :
continuous_within_at f (Ici a) a :=
h_mono.continuous_at_right_of_closure_image_mem_nhds_within hs
(mem_of_superset hfs subset_closure)
/-- If a function `f` is strictly monotonically increasing on a right neighborhood of `a` and the
image of this neighborhood under `f` includes `Ioi (f a)`, then `f` is continuous at `a` from the
right. -/
lemma strict_mono_incr_on.continuous_at_right_of_surj_on {f : Ξ± β Ξ²} {s : set Ξ±} {a : Ξ±}
(h_mono : strict_mono_incr_on f s) (hs : s β π[Ici a] a) (hfs : surj_on f s (Ioi (f a))) :
continuous_within_at f (Ici a) a :=
h_mono.continuous_at_right_of_exists_between hs $ Ξ» b hb, let β¨c, hcs, hcbβ© := hfs hb in
β¨c, hcs, hcb.symm βΈ hb, hcb.leβ©
/-- If `f` is a function strictly monotonically increasing on a left neighborhood of `a` and the
image of this neighborhood under `f` meets every interval `[b, f a)`, `b < f a`, then `f` is
continuous at `a` from the left.
The assumption `hfs : β b < f a, β c β s, f c β Ico b (f a)` is required because otherwise the
function `f : β β β` given by `f x = if x < 0 then x else x + 1` would be a counter-example at
`a = 0`. -/
lemma strict_mono_incr_on.continuous_at_left_of_exists_between {f : Ξ± β Ξ²} {s : set Ξ±} {a : Ξ±}
(h_mono : strict_mono_incr_on f s) (hs : s β π[Iic a] a)
(hfs : β b < f a, β c β s, f c β Ico b (f a)) :
continuous_within_at f (Iic a) a :=
h_mono.dual.continuous_at_right_of_exists_between hs $
Ξ» b hb, let β¨c, hcs, hcb, hcaβ© := hfs b hb in β¨c, hcs, hca, hcbβ©
/-- If `f` is a function monotonically increasing function on a left neighborhood of `a` and the
image of this neighborhood under `f` meets every interval `(b, f a)`, `b < f a`, then `f` is
continuous at `a` from the left.
The assumption `hfs : β b < f a, β c β s, f c β Ioo b (f a)` cannot be replaced by the weaker
assumption `hfs : β b < f a, β c β s, f c β Ico b (f a)` we use for strictly monotone functions
because otherwise the function `floor : β β β€` would be a counter-example at `a = 0`. -/
lemma continuous_at_left_of_mono_incr_on_of_exists_between {f : Ξ± β Ξ²} {s : set Ξ±} {a : Ξ±}
(h_mono : β (x β s) (y β s), x β€ y β f x β€ f y) (hs : s β π[Iic a] a)
(hfs : β b < f a, β c β s, f c β Ioo b (f a)) :
continuous_within_at f (Iic a) a :=
@continuous_at_right_of_mono_incr_on_of_exists_between (order_dual Ξ±) (order_dual Ξ²) _ _ _ _ _ _
f s a (Ξ» x hx y hy, h_mono y hy x hx) hs $
Ξ» b hb, let β¨c, hcs, hcb, hcaβ© := hfs b hb in β¨c, hcs, hca, hcbβ©
/-- If a function `f` with a densely ordered codomain is monotonically increasing on a left
neighborhood of `a` and the closure of the image of this neighborhood under `f` is a left
neighborhood of `f a`, then `f` is continuous at `a` from the left -/
lemma continuous_at_left_of_mono_incr_on_of_closure_image_mem_nhds_within [densely_ordered Ξ²]
{f : Ξ± β Ξ²} {s : set Ξ±} {a : Ξ±} (h_mono : β (x β s) (y β s), x β€ y β f x β€ f y)
(hs : s β π[Iic a] a) (hfs : closure (f '' s) β π[Iic (f a)] (f a)) :
continuous_within_at f (Iic a) a :=
@continuous_at_right_of_mono_incr_on_of_closure_image_mem_nhds_within (order_dual Ξ±) (order_dual Ξ²)
_ _ _ _ _ _ _ f s a (Ξ» x hx y hy, h_mono y hy x hx) hs hfs
/-- If a function `f` with a densely ordered codomain is monotonically increasing on a left
neighborhood of `a` and the image of this neighborhood under `f` is a left neighborhood of `f a`,
then `f` is continuous at `a` from the left. -/
lemma continuous_at_left_of_mono_incr_on_of_image_mem_nhds_within [densely_ordered Ξ²]
{f : Ξ± β Ξ²} {s : set Ξ±} {a : Ξ±} (h_mono : β (x β s) (y β s), x β€ y β f x β€ f y)
(hs : s β π[Iic a] a) (hfs : f '' s β π[Iic (f a)] (f a)) :
continuous_within_at f (Iic a) a :=
continuous_at_left_of_mono_incr_on_of_closure_image_mem_nhds_within h_mono hs
(mem_of_superset hfs subset_closure)
/-- If a function `f` with a densely ordered codomain is strictly monotonically increasing on a
left neighborhood of `a` and the closure of the image of this neighborhood under `f` is a left
neighborhood of `f a`, then `f` is continuous at `a` from the left. -/
lemma strict_mono_incr_on.continuous_at_left_of_closure_image_mem_nhds_within [densely_ordered Ξ²]
{f : Ξ± β Ξ²} {s : set Ξ±} {a : Ξ±} (h_mono : strict_mono_incr_on f s) (hs : s β π[Iic a] a)
(hfs : closure (f '' s) β π[Iic (f a)] (f a)) :
continuous_within_at f (Iic a) a :=
h_mono.dual.continuous_at_right_of_closure_image_mem_nhds_within hs hfs
/-- If a function `f` with a densely ordered codomain is strictly monotonically increasing on a
left neighborhood of `a` and the image of this neighborhood under `f` is a left neighborhood of
`f a`, then `f` is continuous at `a` from the left. -/
lemma strict_mono_incr_on.continuous_at_left_of_image_mem_nhds_within [densely_ordered Ξ²]
{f : Ξ± β Ξ²} {s : set Ξ±} {a : Ξ±} (h_mono : strict_mono_incr_on f s) (hs : s β π[Iic a] a)
(hfs : f '' s β π[Iic (f a)] (f a)) :
continuous_within_at f (Iic a) a :=
h_mono.dual.continuous_at_right_of_image_mem_nhds_within hs hfs
/-- If a function `f` is strictly monotonically increasing on a left neighborhood of `a` and the
image of this neighborhood under `f` includes `Iio (f a)`, then `f` is continuous at `a` from the
left. -/
lemma strict_mono_incr_on.continuous_at_left_of_surj_on {f : Ξ± β Ξ²} {s : set Ξ±} {a : Ξ±}
(h_mono : strict_mono_incr_on f s) (hs : s β π[Iic a] a) (hfs : surj_on f s (Iio (f a))) :
continuous_within_at f (Iic a) a :=
h_mono.dual.continuous_at_right_of_surj_on hs hfs
/-- If a function `f` is strictly monotonically increasing on a neighborhood of `a` and the image of
this neighborhood under `f` meets every interval `[b, f a)`, `b < f a`, and every interval
`(f a, b]`, `b > f a`, then `f` is continuous at `a`. -/
lemma strict_mono_incr_on.continuous_at_of_exists_between {f : Ξ± β Ξ²} {s : set Ξ±} {a : Ξ±}
(h_mono : strict_mono_incr_on f s) (hs : s β π a)
(hfs_l : β b < f a, β c β s, f c β Ico b (f a)) (hfs_r : β b > f a, β c β s, f c β Ioc (f a) b) :
continuous_at f a :=
continuous_at_iff_continuous_left_right.2
β¨h_mono.continuous_at_left_of_exists_between (mem_nhds_within_of_mem_nhds hs) hfs_l,
h_mono.continuous_at_right_of_exists_between (mem_nhds_within_of_mem_nhds hs) hfs_rβ©
/-- If a function `f` with a densely ordered codomain is strictly monotonically increasing on a
neighborhood of `a` and the closure of the image of this neighborhood under `f` is a neighborhood of
`f a`, then `f` is continuous at `a`. -/
lemma strict_mono_incr_on.continuous_at_of_closure_image_mem_nhds [densely_ordered Ξ²] {f : Ξ± β Ξ²}
{s : set Ξ±} {a : Ξ±} (h_mono : strict_mono_incr_on f s) (hs : s β π a)
(hfs : closure (f '' s) β π (f a)) :
continuous_at f a :=
continuous_at_iff_continuous_left_right.2
β¨h_mono.continuous_at_left_of_closure_image_mem_nhds_within (mem_nhds_within_of_mem_nhds hs)
(mem_nhds_within_of_mem_nhds hfs),
h_mono.continuous_at_right_of_closure_image_mem_nhds_within (mem_nhds_within_of_mem_nhds hs)
(mem_nhds_within_of_mem_nhds hfs)β©
/-- If a function `f` with a densely ordered codomain is strictly monotonically increasing on a
neighborhood of `a` and the image of this set under `f` is a neighborhood of `f a`, then `f` is
continuous at `a`. -/
lemma strict_mono_incr_on.continuous_at_of_image_mem_nhds [densely_ordered Ξ²] {f : Ξ± β Ξ²}
{s : set Ξ±} {a : Ξ±} (h_mono : strict_mono_incr_on f s) (hs : s β π a) (hfs : f '' s β π (f a)) :
continuous_at f a :=
h_mono.continuous_at_of_closure_image_mem_nhds hs (mem_of_superset hfs subset_closure)
/-- If `f` is a function monotonically increasing function on a neighborhood of `a` and the image of
this neighborhood under `f` meets every interval `(b, f a)`, `b < f a`, and every interval `(f a,
b)`, `b > f a`, then `f` is continuous at `a`. -/
lemma continuous_at_of_mono_incr_on_of_exists_between {f : Ξ± β Ξ²} {s : set Ξ±} {a : Ξ±}
(h_mono : β (x β s) (y β s), x β€ y β f x β€ f y) (hs : s β π a)
(hfs_l : β b < f a, β c β s, f c β Ioo b (f a)) (hfs_r : β b > f a, β c β s, f c β Ioo (f a) b) :
continuous_at f a :=
continuous_at_iff_continuous_left_right.2
β¨continuous_at_left_of_mono_incr_on_of_exists_between h_mono
(mem_nhds_within_of_mem_nhds hs) hfs_l,
continuous_at_right_of_mono_incr_on_of_exists_between h_mono
(mem_nhds_within_of_mem_nhds hs) hfs_rβ©
/-- If a function `f` with a densely ordered codomain is monotonically increasing on a neighborhood
of `a` and the closure of the image of this neighborhood under `f` is a neighborhood of `f a`, then
`f` is continuous at `a`. -/
lemma continuous_at_of_mono_incr_on_of_closure_image_mem_nhds [densely_ordered Ξ²] {f : Ξ± β Ξ²}
{s : set Ξ±} {a : Ξ±} (h_mono : β (x β s) (y β s), x β€ y β f x β€ f y) (hs : s β π a)
(hfs : closure (f '' s) β π (f a)) :
continuous_at f a :=
continuous_at_iff_continuous_left_right.2
β¨continuous_at_left_of_mono_incr_on_of_closure_image_mem_nhds_within h_mono
(mem_nhds_within_of_mem_nhds hs) (mem_nhds_within_of_mem_nhds hfs),
continuous_at_right_of_mono_incr_on_of_closure_image_mem_nhds_within h_mono
(mem_nhds_within_of_mem_nhds hs) (mem_nhds_within_of_mem_nhds hfs)β©
/-- If a function `f` with a densely ordered codomain is monotonically increasing on a neighborhood
of `a` and the image of this neighborhood under `f` is a neighborhood of `f a`, then `f` is
continuous at `a`. -/
lemma continuous_at_of_mono_incr_on_of_image_mem_nhds [densely_ordered Ξ²] {f : Ξ± β Ξ²}
{s : set Ξ±} {a : Ξ±} (h_mono : β (x β s) (y β s), x β€ y β f x β€ f y) (hs : s β π a)
(hfs : f '' s β π (f a)) :
continuous_at f a :=
continuous_at_of_mono_incr_on_of_closure_image_mem_nhds h_mono hs
(mem_of_superset hfs subset_closure)
/-- A monotone function with densely ordered codomain and a dense range is continuous. -/
lemma monotone.continuous_of_dense_range [densely_ordered Ξ²] {f : Ξ± β Ξ²}
(h_mono : monotone f) (h_dense : dense_range f) :
continuous f :=
continuous_iff_continuous_at.mpr $ Ξ» a,
continuous_at_of_mono_incr_on_of_closure_image_mem_nhds (Ξ» x hx y hy hxy, h_mono hxy)
univ_mem $ by simp only [image_univ, h_dense.closure_eq, univ_mem]
/-- A monotone surjective function with a densely ordered codomain is surjective. -/
lemma monotone.continuous_of_surjective [densely_ordered Ξ²] {f : Ξ± β Ξ²} (h_mono : monotone f)
(h_surj : function.surjective f) :
continuous f :=
h_mono.continuous_of_dense_range h_surj.dense_range
end linear_order
/-!
### Continuity of order isomorphisms
In this section we prove that an `order_iso` is continuous, hence it is a `homeomorph`. We prove
this for an `order_iso` between to partial orders with order topology.
-/
namespace order_iso
variables [partial_order Ξ±] [partial_order Ξ²] [topological_space Ξ±] [topological_space Ξ²]
[order_topology Ξ±] [order_topology Ξ²]
protected lemma continuous (e : Ξ± βo Ξ²) : continuous e :=
begin
rw [βΉorder_topology Ξ²βΊ.topology_eq_generate_intervals],
refine continuous_generated_from (Ξ» s hs, _),
rcases hs with β¨a, rfl|rflβ©,
{ rw e.preimage_Ioi, apply is_open_lt' },
{ rw e.preimage_Iio, apply is_open_gt' }
end
/-- An order isomorphism between two linear order `order_topology` spaces is a homeomorphism. -/
def to_homeomorph (e : Ξ± βo Ξ²) : Ξ± ββ Ξ² :=
{ continuous_to_fun := e.continuous,
continuous_inv_fun := e.symm.continuous,
.. e }
@[simp] lemma coe_to_homeomorph (e : Ξ± βo Ξ²) : βe.to_homeomorph = e := rfl
@[simp] lemma coe_to_homeomorph_symm (e : Ξ± βo Ξ²) : βe.to_homeomorph.symm = e.symm := rfl
end order_iso
|
808a35663e1143afea67d3477a0ea77e6cb0872c | 491068d2ad28831e7dade8d6dff871c3e49d9431 | /tests/lean/run/eq6.lean | fbd73ecaef3ba52cc8d1e24ca719cb185731c90e | [
"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 | 421 | lean | import data.list
open list
definition append {A : Type} : list A β list A β list A
| append nil l := l
| append (h :: t) l := h :: (append t l)
theorem append_nil {A : Type} (l : list A) : append nil l = l :=
rfl
theorem append_cons {A : Type} (h : A) (t l : list A) : append (h :: t) l = h :: (append t l) :=
rfl
example : append (1 :: 2 :: nil) (3 :: 4 :: 5 :: nil) = (1 :: 2 :: 3 :: 4 :: 5 :: nil) :=
rfl
|
eaf887e60049c7980c152e8f20e4f94b3f107e21 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/data/pfunctor/multivariate/W.lean | 3923df2b0c67f6d4852bda6b8f56e8206b6298a1 | [
"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 | 10,502 | lean | /-
Copyright (c) 2018 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad, Simon Hudon
-/
import data.pfunctor.multivariate.basic
/-!
# The W construction as a multivariate polynomial functor.
W types are well-founded tree-like structures. They are defined
as the least fixpoint of a polynomial functor.
## Main definitions
* `W_mk` - constructor
* `W_dest - destructor
* `W_rec` - recursor: basis for defining functions by structural recursion on `P.W Ξ±`
* `W_rec_eq` - defining equation for `W_rec`
* `W_ind` - induction principle for `P.W Ξ±`
## Implementation notes
Three views of M-types:
* `Wp`: polynomial functor
* `W`: data type inductively defined by a triple:
shape of the root, data in the root and children of the root
* `W`: least fixed point of a polynomial functor
Specifically, we define the polynomial functor `Wp` as:
* A := a tree-like structure without information in the nodes
* B := given the tree-like structure `t`, `B t` is a valid path
(specified inductively by `W_path`) from the root of `t` to any given node.
As a result `Wp.obj Ξ±` is made of a dataless tree and a function from
its valid paths to values of `Ξ±`
## Reference
* Jeremy Avigad, Mario M. Carneiro and Simon Hudon.
[*Data Types as Quotients of Polynomial Functors*][avigad-carneiro-hudon2019]
-/
universes u v
namespace mvpfunctor
open typevec
open_locale mvfunctor
variables {n : β} (P : mvpfunctor.{u} (n+1))
/-- A path from the root of a tree to one of its node -/
inductive W_path : P.last.W β fin2 n β Type u
| root (a : P.A) (f : P.last.B a β P.last.W) (i : fin2 n) (c : P.drop.B a i) :
W_path β¨a, fβ© i
| child (a : P.A) (f : P.last.B a β P.last.W) (i : fin2 n) (j : P.last.B a) (c : W_path (f j) i) :
W_path β¨a, fβ© i
instance W_path.inhabited (x : P.last.W) {i} [I : inhabited (P.drop.B x.head i)] :
inhabited (W_path P x i) :=
β¨ match x, I with
| β¨a, f β©, I := W_path.root a f i (@default _ I)
end β©
/-- Specialized destructor on `W_path` -/
def W_path_cases_on {Ξ± : typevec n} {a : P.A} {f : P.last.B a β P.last.W}
(g' : P.drop.B a βΉ Ξ±) (g : Ξ j : P.last.B a, P.W_path (f j) βΉ Ξ±) :
P.W_path β¨a, fβ© βΉ Ξ± :=
begin
intros i x, cases x,
case W_path.root : _ _ i c { exact g' i c },
case W_path.child : _ _ i j c { exact g j i c}
end
/-- Specialized destructor on `W_path` -/
def W_path_dest_left {Ξ± : typevec n} {a : P.A} {f : P.last.B a β P.last.W}
(h : P.W_path β¨a, fβ© βΉ Ξ±) :
P.drop.B a βΉ Ξ± :=
Ξ» i c, h i (W_path.root a f i c)
/-- Specialized destructor on `W_path` -/
def W_path_dest_right {Ξ± : typevec n} {a : P.A} {f : P.last.B a β P.last.W}
(h : P.W_path β¨a, fβ© βΉ Ξ±) :
Ξ j : P.last.B a, P.W_path (f j) βΉ Ξ± :=
Ξ» j i c, h i (W_path.child a f i j c)
theorem W_path_dest_left_W_path_cases_on
{Ξ± : typevec n} {a : P.A} {f : P.last.B a β P.last.W}
(g' : P.drop.B a βΉ Ξ±) (g : Ξ j : P.last.B a, P.W_path (f j) βΉ Ξ±) :
P.W_path_dest_left (P.W_path_cases_on g' g) = g' := rfl
theorem W_path_dest_right_W_path_cases_on
{Ξ± : typevec n} {a : P.A} {f : P.last.B a β P.last.W}
(g' : P.drop.B a βΉ Ξ±) (g : Ξ j : P.last.B a, P.W_path (f j) βΉ Ξ±) :
P.W_path_dest_right (P.W_path_cases_on g' g) = g := rfl
theorem W_path_cases_on_eta {Ξ± : typevec n} {a : P.A} {f : P.last.B a β P.last.W}
(h : P.W_path β¨a, fβ© βΉ Ξ±) :
P.W_path_cases_on (P.W_path_dest_left h) (P.W_path_dest_right h) = h :=
by ext i x; cases x; reflexivity
theorem comp_W_path_cases_on {Ξ± Ξ² : typevec n} (h : Ξ± βΉ Ξ²) {a : P.A} {f : P.last.B a β P.last.W}
(g' : P.drop.B a βΉ Ξ±) (g : Ξ j : P.last.B a, P.W_path (f j) βΉ Ξ±) :
h β P.W_path_cases_on g' g = P.W_path_cases_on (h β g') (Ξ» i, h β g i) :=
by ext i x; cases x; reflexivity
/-- Polynomial functor for the W-type of `P`. `A` is a data-less well-founded
tree whereas, for a given `a : A`, `B a` is a valid path in tree `a` so
that `Wp.obj Ξ±` is made of a tree and a function from its valid paths to
the values it contains -/
def Wp : mvpfunctor n :=
{ A := P.last.W, B := P.W_path }
/-- W-type of `P` -/
@[nolint has_nonempty_instance]
def W (Ξ± : typevec n) : Type* := P.Wp.obj Ξ±
instance mvfunctor_W : mvfunctor P.W := by delta mvpfunctor.W; apply_instance
/-!
First, describe operations on `W` as a polynomial functor.
-/
/-- Constructor for `Wp` -/
def Wp_mk {Ξ± : typevec n} (a : P.A) (f : P.last.B a β P.last.W) (f' : P.W_path β¨a, fβ© βΉ Ξ±) :
P.W Ξ± :=
β¨β¨a, fβ©, f'β©
/-- Recursor for `Wp` -/
def Wp_rec {Ξ± : typevec n} {C : Type*}
(g : Ξ (a : P.A) (f : P.last.B a β P.last.W),
(P.W_path β¨a, fβ© βΉ Ξ±) β (P.last.B a β C) β C) :
Ξ (x : P.last.W) (f' : P.W_path x βΉ Ξ±), C
| β¨a, fβ© f' := g a f f' (Ξ» i, Wp_rec (f i) (P.W_path_dest_right f' i))
theorem Wp_rec_eq {Ξ± : typevec n} {C : Type*}
(g : Ξ (a : P.A) (f : P.last.B a β P.last.W),
(P.W_path β¨a, fβ© βΉ Ξ±) β (P.last.B a β C) β C)
(a : P.A) (f : P.last.B a β P.last.W) (f' : P.W_path β¨a, fβ© βΉ Ξ±) :
P.Wp_rec g β¨a, fβ© f' = g a f f' (Ξ» i, P.Wp_rec g (f i) (P.W_path_dest_right f' i)) :=
rfl
-- Note: we could replace Prop by Type* and obtain a dependent recursor
theorem Wp_ind {Ξ± : typevec n} {C : Ξ x : P.last.W, P.W_path x βΉ Ξ± β Prop}
(ih : β (a : P.A) (f : P.last.B a β P.last.W)
(f' : P.W_path β¨a, fβ© βΉ Ξ±),
(β i : P.last.B a, C (f i) (P.W_path_dest_right f' i)) β C β¨a, fβ© f') :
Ξ (x : P.last.W) (f' : P.W_path x βΉ Ξ±), C x f'
| β¨a, fβ© f' := ih a f f' (Ξ» i, Wp_ind _ _)
/-!
Now think of W as defined inductively by the data β¨a, f', fβ© where
- `a : P.A` is the shape of the top node
- `f' : P.drop.B a βΉ Ξ±` is the contents of the top node
- `f : P.last.B a β P.last.W` are the subtrees
-/
/-- Constructor for `W` -/
def W_mk {Ξ± : typevec n} (a : P.A) (f' : P.drop.B a βΉ Ξ±) (f : P.last.B a β P.W Ξ±) :
P.W Ξ± :=
let g : P.last.B a β P.last.W := Ξ» i, (f i).fst,
g' : P.W_path β¨a, gβ© βΉ Ξ± := P.W_path_cases_on f' (Ξ» i, (f i).snd) in
β¨β¨a, gβ©, g'β©
/-- Recursor for `W` -/
def W_rec {Ξ± : typevec n} {C : Type*}
(g : Ξ a : P.A, ((P.drop).B a βΉ Ξ±) β ((P.last).B a β P.W Ξ±) β ((P.last).B a β C) β C) :
P.W Ξ± β C
| β¨a, f'β© :=
let g' (a : P.A) (f : P.last.B a β P.last.W) (h : P.W_path β¨a, fβ© βΉ Ξ±)
(h' : P.last.B a β C) : C :=
g a (P.W_path_dest_left h) (Ξ» i, β¨f i, P.W_path_dest_right h iβ©) h' in
P.Wp_rec g' a f'
/-- Defining equation for the recursor of `W` -/
theorem W_rec_eq {Ξ± : typevec n} {C : Type*}
(g : Ξ a : P.A, ((P.drop).B a βΉ Ξ±) β ((P.last).B a β P.W Ξ±) β ((P.last).B a β C) β C)
(a : P.A) (f' : P.drop.B a βΉ Ξ±) (f : P.last.B a β P.W Ξ±) :
P.W_rec g (P.W_mk a f' f) = g a f' f (Ξ» i, P.W_rec g (f i)) :=
begin
rw [W_mk, W_rec], dsimp, rw [Wp_rec_eq],
dsimp only [W_path_dest_left_W_path_cases_on, W_path_dest_right_W_path_cases_on],
congr; ext1 i; cases (f i); refl
end
/-- Induction principle for `W` -/
theorem W_ind {Ξ± : typevec n} {C : P.W Ξ± β Prop}
(ih : β (a : P.A) (f' : P.drop.B a βΉ Ξ±) (f : P.last.B a β P.W Ξ±),
(β i, C (f i)) β C (P.W_mk a f' f)) :
β x, C x :=
begin
intro x, cases x with a f,
apply @Wp_ind n P Ξ± (Ξ» a f, C β¨a, fβ©), dsimp,
intros a f f' ih',
dsimp [W_mk] at ih,
let ih'' := ih a (P.W_path_dest_left f') (Ξ» i, β¨f i, P.W_path_dest_right f' iβ©),
dsimp at ih'', rw W_path_cases_on_eta at ih'',
apply ih'',
apply ih'
end
theorem W_cases {Ξ± : typevec n} {C : P.W Ξ± β Prop}
(ih : β (a : P.A) (f' : P.drop.B a βΉ Ξ±) (f : P.last.B a β P.W Ξ±), C (P.W_mk a f' f)) :
β x, C x :=
P.W_ind (Ξ» a f' f ih', ih a f' f)
/-- W-types are functorial -/
def W_map {Ξ± Ξ² : typevec n} (g : Ξ± βΉ Ξ²) : P.W Ξ± β P.W Ξ² :=
Ξ» x, g <$$> x
theorem W_mk_eq {Ξ± : typevec n} (a : P.A) (f : P.last.B a β P.last.W)
(g' : P.drop.B a βΉ Ξ±) (g : Ξ j : P.last.B a, P.W_path (f j) βΉ Ξ±) :
P.W_mk a g' (Ξ» i, β¨f i, g iβ©) =
β¨β¨a, fβ©, P.W_path_cases_on g' gβ© := rfl
theorem W_map_W_mk {Ξ± Ξ² : typevec n} (g : Ξ± βΉ Ξ²)
(a : P.A) (f' : P.drop.B a βΉ Ξ±) (f : P.last.B a β P.W Ξ±) :
g <$$> P.W_mk a f' f = P.W_mk a (g β f') (Ξ» i, g <$$> f i) :=
begin
show _ = P.W_mk a (g β f') (mvfunctor.map g β f),
have : mvfunctor.map g β f = Ξ» i, β¨(f i).fst, g β ((f i).snd)β©,
{ ext i : 1, dsimp [function.comp], cases (f i), refl },
rw this,
have : f = Ξ» i, β¨(f i).fst, (f i).sndβ©,
{ ext1, cases (f x), refl },
rw this, dsimp,
rw [W_mk_eq, W_mk_eq],
have h := mvpfunctor.map_eq P.Wp g,
rw [h, comp_W_path_cases_on]
end
-- TODO: this technical theorem is used in one place in constructing the initial algebra.
-- Can it be avoided?
/-- Constructor of a value of `P.obj (Ξ± ::: Ξ²)` from components.
Useful to avoid complicated type annotation -/
@[reducible] def obj_append1 {Ξ± : typevec n} {Ξ² : Type*}
(a : P.A) (f' : P.drop.B a βΉ Ξ±) (f : P.last.B a β Ξ²) :
P.obj (Ξ± ::: Ξ²) :=
β¨a, split_fun f' fβ©
theorem map_obj_append1 {Ξ± Ξ³ : typevec n} (g : Ξ± βΉ Ξ³)
(a : P.A) (f' : P.drop.B a βΉ Ξ±) (f : P.last.B a β P.W Ξ±) :
append_fun g (P.W_map g) <$$> P.obj_append1 a f' f =
P.obj_append1 a (g β f') (Ξ» x, P.W_map g (f x)) :=
by rw [obj_append1, obj_append1, map_eq, append_fun, β split_fun_comp]; refl
/-!
Yet another view of the W type: as a fixed point for a multivariate polynomial functor.
These are needed to use the W-construction to construct a fixed point of a qpf, since
the qpf axioms are expressed in terms of `map` on `P`.
-/
/-- Constructor for the W-type of `P` -/
def W_mk' {Ξ± : typevec n} : P.obj (Ξ± ::: P.W Ξ±) β P.W Ξ±
| β¨a, fβ© := P.W_mk a (drop_fun f) (last_fun f)
/-- Destructor for the W-type of `P` -/
def W_dest' {Ξ± : typevec.{u} n} : P.W Ξ± β P.obj (Ξ±.append1 (P.W Ξ±)) :=
P.W_rec (Ξ» a f' f _, β¨a, split_fun f' fβ©)
theorem W_dest'_W_mk {Ξ± : typevec n}
(a : P.A) (f' : P.drop.B a βΉ Ξ±) (f : P.last.B a β P.W Ξ±) :
P.W_dest' (P.W_mk a f' f) = β¨a, split_fun f' fβ© :=
by rw [W_dest', W_rec_eq]
theorem W_dest'_W_mk' {Ξ± : typevec n} (x : P.obj (Ξ±.append1 (P.W Ξ±))) :
P.W_dest' (P.W_mk' x) = x :=
by cases x with a f; rw [W_mk', W_dest'_W_mk, split_drop_fun_last_fun]
end mvpfunctor
|
a459a3bb2b2011395bca310ccfcf1a44fc5f7d73 | abd85493667895c57a7507870867b28124b3998f | /src/algebra/group_power.lean | 13472a3314e52ed23baa6c8bd3d18e13dfd39883 | [
"Apache-2.0"
] | permissive | pechersky/mathlib | d56eef16bddb0bfc8bc552b05b7270aff5944393 | f1df14c2214ee114c9738e733efd5de174deb95d | refs/heads/master | 1,666,714,392,571 | 1,591,747,567,000 | 1,591,747,567,000 | 270,557,274 | 0 | 0 | Apache-2.0 | 1,591,597,975,000 | 1,591,597,974,000 | null | UTF-8 | Lean | false | false | 35,371 | lean | /-
Copyright (c) 2015 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad, Robert Y. Lewis
-/
import data.int.basic
import data.equiv.basic
import deprecated.ring
/-!
# Power operations on monoids and groups
The power operation on monoids and groups.
We separate this from group, because it depends on `β`,
which in turn depends on other parts of algebra.
## Notation
The class `has_pow Ξ± Ξ²` provides the notation `a^b` for powers.
We define instances of `has_pow M β`, for monoids `M`, and `has_pow G β€` for groups `G`.
We also define infix operators `β’β` and `β’β€` for scalar multiplication by a natural and an integer
numbers, respectively.
## Implementation details
We adopt the convention that `0^0 = 1`.
-/
universes u v w x y z uβ uβ
variables {M : Type u} {N : Type v} {G : Type w} {H : Type x} {A : Type y} {B : Type z}
{R : Type uβ} {S : Type uβ}
/-- The power operation in a monoid. `a^n = a*a*...*a` n times. -/
def monoid.pow [has_mul M] [has_one M] (a : M) : β β M
| 0 := 1
| (n+1) := a * monoid.pow n
/-- The scalar multiplication in an additive monoid.
`n β’β a = a+a+...+a` n times. -/
def nsmul [has_add A] [has_zero A] (n : β) (a : A) : A :=
@monoid.pow (multiplicative A) _ { one := (0 : A) } a n
infix ` β’β `:70 := nsmul
@[priority 5] instance monoid.has_pow [monoid M] : has_pow M β := β¨monoid.powβ©
/-!
### Commutativity
First we prove some facts about `semiconj_by` and `commute`. They do not require any theory about
`pow` and/or `nsmul` and will be useful later in this file.
-/
namespace semiconj_by
variables [monoid M]
@[simp] lemma pow_right {a x y : M} (h : semiconj_by a x y) (n : β) : semiconj_by a (x^n) (y^n) :=
nat.rec_on n (one_right a) $ Ξ» n ihn, h.mul_right ihn
end semiconj_by
namespace commute
variables [monoid M] {a b : M}
@[simp] theorem pow_right (h : commute a b) (n : β) : commute a (b ^ n) := h.pow_right n
@[simp] theorem pow_left (h : commute a b) (n : β) : commute (a ^ n) b := (h.symm.pow_right n).symm
@[simp] theorem pow_pow (h : commute a b) (m n : β) : commute (a ^ m) (b ^ n) :=
(h.pow_left m).pow_right n
@[simp] theorem self_pow (a : M) (n : β) : commute a (a ^ n) := (commute.refl a).pow_right n
@[simp] theorem pow_self (a : M) (n : β) : commute (a ^ n) a := (commute.refl a).pow_left n
@[simp] theorem pow_pow_self (a : M) (m n : β) : commute (a ^ m) (a ^ n) :=
(commute.refl a).pow_pow m n
end commute
/-!
### (Additive) monoid
-/
section monoid
variables [monoid M] [monoid N] [add_monoid A] [add_monoid B]
@[simp] theorem pow_zero (a : M) : a^0 = 1 := rfl
@[simp] theorem zero_nsmul (a : A) : 0 β’β a = 0 := rfl
theorem pow_succ (a : M) (n : β) : a^(n+1) = a * a^n := rfl
theorem succ_nsmul (a : A) (n : β) : (n+1) β’β a = a + n β’β a := rfl
@[simp] theorem pow_one (a : M) : a^1 = a := mul_one _
@[simp] theorem one_nsmul (a : A) : 1 β’β a = a := add_zero _
@[simp] lemma pow_ite (P : Prop) [decidable P] (a : M) (b c : β) :
a ^ (if P then b else c) = if P then a ^ b else a ^ c :=
by split_ifs; refl
@[simp] lemma ite_pow (P : Prop) [decidable P] (a b : M) (c : β) :
(if P then a else b) ^ c = if P then a ^ c else b ^ c :=
by split_ifs; refl
@[simp] lemma pow_boole (P : Prop) [decidable P] (a : M) :
a ^ (if P then 1 else 0) = if P then a else 1 :=
by simp
theorem pow_mul_comm' (a : M) (n : β) : a^n * a = a * a^n := commute.pow_self a n
theorem nsmul_add_comm' : β (a : A) (n : β), n β’β a + a = a + n β’β a :=
@pow_mul_comm' (multiplicative A) _
theorem pow_succ' (a : M) (n : β) : a^(n+1) = a^n * a :=
by rw [pow_succ, pow_mul_comm']
theorem succ_nsmul' (a : A) (n : β) : (n+1) β’β a = n β’β a + a :=
@pow_succ' (multiplicative A) _ _ _
theorem pow_two (a : M) : a^2 = a * a :=
show a*(a*1)=a*a, by rw mul_one
theorem two_nsmul (a : A) : 2 β’β a = a + a :=
@pow_two (multiplicative A) _ a
theorem pow_add (a : M) (m n : β) : a^(m + n) = a^m * a^n :=
by induction n with n ih; [rw [add_zero, pow_zero, mul_one],
rw [pow_succ', β mul_assoc, β ih, β pow_succ', add_assoc]]
theorem add_nsmul : β (a : A) (m n : β), (m + n) β’β a = m β’β a + n β’β a :=
@pow_add (multiplicative A) _
@[simp] theorem one_pow (n : β) : (1 : M)^n = 1 :=
by induction n with n ih; [refl, rw [pow_succ, ih, one_mul]]
@[simp] theorem nsmul_zero (n : β) : n β’β (0 : A) = 0 :=
by induction n with n ih; [refl, rw [succ_nsmul, ih, zero_add]]
theorem pow_mul (a : M) (m n : β) : a^(m * n) = (a^m)^n :=
by induction n with n ih; [rw mul_zero, rw [nat.mul_succ, pow_add, pow_succ', ih]]; refl
theorem mul_nsmul' : β (a : A) (m n : β), m * n β’β a = n β’β (m β’β a) :=
@pow_mul (multiplicative A) _
theorem pow_mul' (a : M) (m n : β) : a^(m * n) = (a^n)^m :=
by rw [mul_comm, pow_mul]
theorem mul_nsmul (a : A) (m n : β) : m * n β’β a = m β’β (n β’β a) :=
@pow_mul' (multiplicative A) _ a m n
@[simp] theorem nsmul_one [has_one A] : β n : β, n β’β (1 : A) = n :=
add_monoid_hom.eq_nat_cast
β¨Ξ» n, n β’β (1 : A), zero_nsmul _, Ξ» _ _, add_nsmul _ _ _β©
(one_nsmul _)
theorem pow_bit0 (a : M) (n : β) : a ^ bit0 n = a^n * a^n := pow_add _ _ _
theorem bit0_nsmul (a : A) (n : β) : bit0 n β’β a = n β’β a + n β’β a := add_nsmul _ _ _
theorem pow_bit1 (a : M) (n : β) : a ^ bit1 n = a^n * a^n * a :=
by rw [bit1, pow_succ', pow_bit0]
theorem bit1_nsmul : β (a : A) (n : β), bit1 n β’β a = n β’β a + n β’β a + a :=
@pow_bit1 (multiplicative A) _
theorem pow_mul_comm (a : M) (m n : β) : a^m * a^n = a^n * a^m :=
commute.pow_pow_self a m n
theorem nsmul_add_comm : β (a : A) (m n : β), m β’β a + n β’β a = n β’β a + m β’β a :=
@pow_mul_comm (multiplicative A) _
@[simp, priority 500]
theorem list.prod_repeat (a : M) (n : β) : (list.repeat a n).prod = a ^ n :=
by induction n with n ih; [refl, rw [list.repeat_succ, list.prod_cons, ih]]; refl
@[simp, priority 500]
theorem list.sum_repeat : β (a : A) (n : β), (list.repeat a n).sum = n β’β a :=
@list.prod_repeat (multiplicative A) _
theorem monoid_hom.map_pow (f : M β* N) (a : M) : β(n : β), f (a ^ n) = (f a) ^ n
| 0 := f.map_one
| (n+1) := by rw [pow_succ, pow_succ, f.map_mul, monoid_hom.map_pow]
theorem add_monoid_hom.map_nsmul (f : A β+ B) (a : A) (n : β) : f (n β’β a) = n β’β f a :=
f.to_multiplicative.map_pow a n
theorem is_monoid_hom.map_pow (f : M β N) [is_monoid_hom f] (a : M) :
β(n : β), f (a ^ n) = (f a) ^ n :=
(monoid_hom.of f).map_pow a
theorem is_add_monoid_hom.map_nsmul (f : A β B) [is_add_monoid_hom f] (a : A) (n : β) :
f (n β’β a) = n β’β f a :=
(add_monoid_hom.of f).map_nsmul a n
@[simp, norm_cast] lemma units.coe_pow (u : units M) (n : β) : ((u ^ n : units M) : M) = u ^ n :=
(units.coe_hom M).map_pow u n
lemma commute.mul_pow {a b : M} (h : commute a b) (n : β) : (a * b) ^ n = a ^ n * b ^ n :=
nat.rec_on n (by simp) $ Ξ» n ihn,
by simp only [pow_succ, ihn, β mul_assoc, (h.pow_left n).right_comm]
theorem neg_pow [ring R] (a : R) (n : β) : (- a) ^ n = (-1) ^ n * a ^ n :=
(neg_one_mul a) βΈ (commute.neg_one_left a).mul_pow n
end monoid
@[simp] theorem nat.pow_eq_pow (p q : β) :
@has_pow.pow _ _ monoid.has_pow p q = p ^ q :=
by induction q with q ih; [refl, rw [nat.pow_succ, pow_succ', ih]]
theorem nat.nsmul_eq_mul (m n : β) : m β’β n = m * n :=
by induction m with m ih; [rw [zero_nsmul, zero_mul],
rw [succ_nsmul', ih, nat.succ_mul]]
/-!
### Commutative (additive) monoid
-/
section comm_monoid
variables [comm_monoid M] [add_comm_monoid A]
theorem mul_pow (a b : M) (n : β) : (a * b)^n = a^n * b^n :=
(commute.all a b).mul_pow n
theorem nsmul_add : β (a b : A) (n : β), n β’β (a + b) = n β’β a + n β’β b :=
@mul_pow (multiplicative A) _
instance pow.is_monoid_hom (n : β) : is_monoid_hom ((^ n) : M β M) :=
{ map_mul := Ξ» _ _, mul_pow _ _ _, map_one := one_pow _ }
instance nsmul.is_add_monoid_hom (n : β) : is_add_monoid_hom (nsmul n : A β A) :=
{ map_add := Ξ» _ _, nsmul_add _ _ _, map_zero := nsmul_zero _ }
end comm_monoid
section group
variables [group G] [group H] [add_group A] [add_group B]
section nat
@[simp] theorem inv_pow (a : G) (n : β) : (aβ»ΒΉ)^n = (a^n)β»ΒΉ :=
by induction n with n ih; [exact one_inv.symm,
rw [pow_succ', pow_succ, ih, mul_inv_rev]]
@[simp] theorem neg_nsmul : β (a : A) (n : β), n β’β (-a) = -(n β’β a) :=
@inv_pow (multiplicative A) _
theorem pow_sub (a : G) {m n : β} (h : n β€ m) : a^(m - n) = a^m * (a^n)β»ΒΉ :=
have h1 : m - n + n = m, from nat.sub_add_cancel h,
have h2 : a^(m - n) * a^n = a^m, by rw [βpow_add, h1],
eq_mul_inv_of_mul_eq h2
theorem nsmul_sub : β (a : A) {m n : β}, n β€ m β (m - n) β’β a = m β’β a - n β’β a :=
@pow_sub (multiplicative A) _
theorem pow_inv_comm (a : G) (m n : β) : (aβ»ΒΉ)^m * a^n = a^n * (aβ»ΒΉ)^m :=
(commute.refl a).inv_left.pow_pow m n
theorem nsmul_neg_comm : β (a : A) (m n : β), m β’β (-a) + n β’β a = n β’β a + m β’β (-a) :=
@pow_inv_comm (multiplicative A) _
end nat
open int
/--
The power operation in a group. This extends `monoid.pow` to negative integers
with the definition `a^(-n) = (a^n)β»ΒΉ`.
-/
def gpow (a : G) : β€ β G
| (of_nat n) := a^n
| -[1+n] := (a^(nat.succ n))β»ΒΉ
/--
The scalar multiplication by integers on an additive group.
This extends `nsmul` to negative integers
with the definition `(-n) β’β€ a = -(n β’β a)`.
-/
def gsmul (n : β€) (a : A) : A :=
@gpow (multiplicative A) _ a n
@[priority 10] instance group.has_pow : has_pow G β€ := β¨gpowβ©
infix ` β’β€ `:70 := gsmul
@[simp] theorem gpow_coe_nat (a : G) (n : β) : a ^ (n:β€) = a ^ n := rfl
@[simp] theorem gsmul_coe_nat (a : A) (n : β) : n β’β€ a = n β’β a := rfl
theorem gpow_of_nat (a : G) (n : β) : a ^ of_nat n = a ^ n := rfl
theorem gsmul_of_nat (a : A) (n : β) : of_nat n β’β€ a = n β’β a := rfl
@[simp] theorem gpow_neg_succ_of_nat (a : G) (n : β) : a ^ -[1+n] = (a ^ n.succ)β»ΒΉ := rfl
@[simp] theorem gsmul_neg_succ_of_nat (a : A) (n : β) : -[1+n] β’β€ a = - (n.succ β’β a) := rfl
local attribute [ematch] le_of_lt
open nat
@[simp] theorem gpow_zero (a : G) : a ^ (0:β€) = 1 := rfl
@[simp] theorem zero_gsmul (a : A) : (0:β€) β’β€ a = 0 := rfl
@[simp] theorem gpow_one (a : G) : a ^ (1:β€) = a := pow_one a
@[simp] theorem one_gsmul (a : A) : (1:β€) β’β€ a = a := add_zero _
@[simp] theorem one_gpow : β (n : β€), (1 : G) ^ n = 1
| (n : β) := one_pow _
| -[1+ n] := show _β»ΒΉ=(1:G), by rw [_root_.one_pow, one_inv]
@[simp] theorem gsmul_zero : β (n : β€), n β’β€ (0 : A) = 0 :=
@one_gpow (multiplicative A) _
@[simp] theorem gpow_neg (a : G) : β (n : β€), a ^ -n = (a ^ n)β»ΒΉ
| (n+1:β) := rfl
| 0 := one_inv.symm
| -[1+ n] := (inv_inv _).symm
@[simp] theorem neg_gsmul : β (a : A) (n : β€), -n β’β€ a = -(n β’β€ a) :=
@gpow_neg (multiplicative A) _
theorem gpow_neg_one (x : G) : x ^ (-1:β€) = xβ»ΒΉ := congr_arg has_inv.inv $ pow_one x
theorem neg_one_gsmul (x : A) : (-1:β€) β’β€ x = -x := congr_arg has_neg.neg $ one_nsmul x
theorem gsmul_one [has_one A] (n : β€) : n β’β€ (1 : A) = n :=
by cases n; simp
theorem inv_gpow (a : G) : βn:β€, aβ»ΒΉ ^ n = (a ^ n)β»ΒΉ
| (n : β) := inv_pow a n
| -[1+ n] := congr_arg has_inv.inv $ inv_pow a (n+1)
theorem gsmul_neg (a : A) (n : β€) : gsmul n (- a) = - gsmul n a :=
@inv_gpow (multiplicative A) _ a n
lemma gpow_add_one (a : G) : β n : β€, a ^ (n + 1) = a ^ n * a
| (of_nat n) := by simp [β int.coe_nat_succ, pow_succ']
| -[1+0] := by simp [int.neg_succ_of_nat_eq]
| -[1+(n+1)] := by rw [int.neg_succ_of_nat_eq, gpow_neg, neg_add, neg_add_cancel_right, gpow_neg,
β int.coe_nat_succ, gpow_coe_nat, gpow_coe_nat, _root_.pow_succ _ (n + 1), mul_inv_rev,
inv_mul_cancel_right]
theorem add_one_gsmul : β (a : A) (i : β€), (i + 1) β’β€ a = i β’β€ a + a :=
@gpow_add_one (multiplicative A) _
lemma gpow_sub_one (a : G) (n : β€) : a ^ (n - 1) = a ^ n * aβ»ΒΉ :=
calc a ^ (n - 1) = a ^ (n - 1) * a * aβ»ΒΉ : (mul_inv_cancel_right _ _).symm
... = a^n * aβ»ΒΉ : by rw [β gpow_add_one, sub_add_cancel]
lemma gpow_add (a : G) (m n : β€) : a ^ (m + n) = a ^ m * a ^ n :=
begin
induction n using int.induction_on with n ihn n ihn,
case hz : { simp },
{ simp only [β add_assoc, gpow_add_one, ihn, mul_assoc] },
{ rw [gpow_sub_one, β mul_assoc, β ihn, β gpow_sub_one, add_sub_assoc] }
end
theorem add_gsmul : β (a : A) (i j : β€), (i + j) β’β€ a = i β’β€ a + j β’β€ a :=
@gpow_add (multiplicative A) _
lemma gpow_sub (a : G) (m n : β€) : a ^ (m - n) = a ^ m * (a ^ n)β»ΒΉ :=
by rw [sub_eq_add_neg, gpow_add, gpow_neg]
lemma sub_gsmul (m n : β€) (a : A) : (m - n) β’β€ a = m β’β€ a - n β’β€ a :=
@gpow_sub (multiplicative A) _ _ _ _
theorem gpow_one_add (a : G) (i : β€) : a ^ (1 + i) = a * a ^ i :=
by rw [gpow_add, gpow_one]
theorem one_add_gsmul : β (a : A) (i : β€), (1 + i) β’β€ a = a + i β’β€ a :=
@gpow_one_add (multiplicative A) _
theorem gpow_mul_comm (a : G) (i j : β€) : a ^ i * a ^ j = a ^ j * a ^ i :=
by rw [β gpow_add, β gpow_add, add_comm]
theorem gsmul_add_comm : β (a : A) (i j), i β’β€ a + j β’β€ a = j β’β€ a + i β’β€ a :=
@gpow_mul_comm (multiplicative A) _
theorem gpow_mul (a : G) (m n : β€) : a ^ (m * n) = (a ^ m) ^ n :=
int.induction_on n (by simp) (Ξ» n ihn, by simp [mul_add, gpow_add, ihn])
(Ξ» n ihn, by simp only [mul_sub, gpow_sub, ihn, mul_one, gpow_one])
theorem gsmul_mul' : β (a : A) (m n : β€), m * n β’β€ a = n β’β€ (m β’β€ a) :=
@gpow_mul (multiplicative A) _
theorem gpow_mul' (a : G) (m n : β€) : a ^ (m * n) = (a ^ n) ^ m :=
by rw [mul_comm, gpow_mul]
theorem gsmul_mul (a : A) (m n : β€) : m * n β’β€ a = m β’β€ (n β’β€ a) :=
by rw [mul_comm, gsmul_mul']
theorem gpow_bit0 (a : G) (n : β€) : a ^ bit0 n = a ^ n * a ^ n := gpow_add _ _ _
theorem bit0_gsmul (a : A) (n : β€) : bit0 n β’β€ a = n β’β€ a + n β’β€ a := gpow_add _ _ _
theorem gpow_bit1 (a : G) (n : β€) : a ^ bit1 n = a ^ n * a ^ n * a :=
by rw [bit1, gpow_add]; simp [gpow_bit0]
theorem bit1_gsmul : β (a : A) (n : β€), bit1 n β’β€ a = n β’β€ a + n β’β€ a + a :=
@gpow_bit1 (multiplicative A) _
theorem monoid_hom.map_gpow (f : G β* H) (a : G) (n : β€) : f (a ^ n) = f a ^ n :=
by cases n; [exact f.map_pow _ _, exact (f.map_inv _).trans (congr_arg _ $ f.map_pow _ _)]
theorem add_monoid_hom.map_gsmul (f : A β+ B) (a : A) (n : β€) : f (n β’β€ a) = n β’β€ f a :=
f.to_multiplicative.map_gpow a n
@[simp, norm_cast] lemma units.coe_gpow (u : units G) (n : β€) : ((u ^ n : units G) : G) = u ^ n :=
(units.coe_hom G).map_gpow u n
theorem commute.mul_gpow {a b : G} (h : commute a b) : β n : β€, (a * b) ^ n = a ^ n * b ^ n
| (n : β) := h.mul_pow n
| -[1+n] := by simp [h.mul_pow, (h.pow_pow n.succ n.succ).inv_inv.symm.eq]
end group
section comm_group
variables [comm_group G] [add_comm_group A]
theorem mul_gpow (a b : G) (n : β€) : (a * b)^n = a^n * b^n := (commute.all a b).mul_gpow n
theorem gsmul_add : β (a b : A) (n : β€), n β’β€ (a + b) = n β’β€ a + n β’β€ b :=
@mul_gpow (multiplicative A) _
theorem gsmul_sub (a b : A) (n : β€) : gsmul n (a - b) = gsmul n a - gsmul n b :=
by simp only [gsmul_add, gsmul_neg, sub_eq_add_neg]
instance gpow.is_group_hom (n : β€) : is_group_hom ((^ n) : G β G) :=
{ map_mul := Ξ» _ _, mul_gpow _ _ n }
instance gsmul.is_add_group_hom (n : β€) : is_add_group_hom (gsmul n : A β A) :=
{ map_add := Ξ» _ _, gsmul_add _ _ n }
end comm_group
@[simp] lemma with_bot.coe_nsmul [add_monoid A] (a : A) (n : β) :
((nsmul n a : A) : with_bot A) = nsmul n a :=
add_monoid_hom.map_nsmul β¨_, with_bot.coe_zero, with_bot.coe_addβ© a n
theorem nsmul_eq_mul' [semiring R] (a : R) (n : β) : n β’β a = a * n :=
by induction n with n ih; [rw [zero_nsmul, nat.cast_zero, mul_zero],
rw [succ_nsmul', ih, nat.cast_succ, mul_add, mul_one]]
@[simp] theorem nsmul_eq_mul [semiring R] (n : β) (a : R) : n β’β a = n * a :=
by rw [nsmul_eq_mul', (n.cast_commute a).eq]
theorem mul_nsmul_left [semiring R] (a b : R) (n : β) : n β’β (a * b) = a * (n β’β b) :=
by rw [nsmul_eq_mul', nsmul_eq_mul', mul_assoc]
theorem mul_nsmul_assoc [semiring R] (a b : R) (n : β) : n β’β (a * b) = n β’β a * b :=
by rw [nsmul_eq_mul, nsmul_eq_mul, mul_assoc]
lemma zero_pow [semiring R] : β {n : β}, 0 < n β (0 : R) ^ n = 0
| (n+1) _ := zero_mul _
@[simp, norm_cast] theorem nat.cast_pow [semiring R] (n m : β) : (β(n ^ m) : R) = βn ^ m :=
by induction m with m ih; [exact nat.cast_one, rw [nat.pow_succ, pow_succ', nat.cast_mul, ih]]
@[simp, norm_cast] theorem int.coe_nat_pow (n m : β) : ((n ^ m : β) : β€) = n ^ m :=
by induction m with m ih; [exact int.coe_nat_one, rw [nat.pow_succ, pow_succ', int.coe_nat_mul, ih]]
theorem int.nat_abs_pow (n : β€) (k : β) : int.nat_abs (n ^ k) = (int.nat_abs n) ^ k :=
by induction k with k ih; [refl, rw [pow_succ', int.nat_abs_mul, nat.pow_succ, ih]]
namespace ring_hom
variables [semiring R] [semiring S]
@[simp] lemma map_pow (f : R β+* S) (a) :
β n : β, f (a ^ n) = (f a) ^ n :=
f.to_monoid_hom.map_pow a
end ring_hom
lemma is_semiring_hom.map_pow [semiring R] [semiring S] (f : R β S) [is_semiring_hom f] (a) :
β n : β, f (a ^ n) = (f a) ^ n :=
is_monoid_hom.map_pow f a
theorem neg_one_pow_eq_or [ring R] : β n : β, (-1 : R)^n = 1 β¨ (-1 : R)^n = -1
| 0 := or.inl rfl
| (n+1) := (neg_one_pow_eq_or n).swap.imp
(Ξ» h, by rw [pow_succ, h, neg_one_mul, neg_neg])
(Ξ» h, by rw [pow_succ, h, mul_one])
lemma pow_dvd_pow [comm_semiring R] (a : R) {m n : β} (h : m β€ n) :
a ^ m β£ a ^ n := β¨a ^ (n - m), by rw [β pow_add, nat.add_sub_cancel' h]β©
-- The next four lemmas allow us to replace multiplication by a numeral with a `gsmul` expression.
-- They are used by the `noncomm_ring` tactic, to normalise expressions before passing to `abel`.
lemma bit0_mul [ring R] {n r : R} : bit0 n * r = gsmul 2 (n * r) :=
by { dsimp [bit0], rw [add_mul, add_gsmul, one_gsmul], }
lemma mul_bit0 [ring R] {n r : R} : r * bit0 n = gsmul 2 (r * n) :=
by { dsimp [bit0], rw [mul_add, add_gsmul, one_gsmul], }
lemma bit1_mul [ring R] {n r : R} : bit1 n * r = gsmul 2 (n * r) + r :=
by { dsimp [bit1], rw [add_mul, bit0_mul, one_mul], }
lemma mul_bit1 [ring R] {n r : R} : r * bit1 n = gsmul 2 (r * n) + r :=
by { dsimp [bit1], rw [mul_add, mul_bit0, mul_one], }
@[simp] theorem gsmul_eq_mul [ring R] (a : R) : β n, n β’β€ a = n * a
| (n : β) := nsmul_eq_mul _ _
| -[1+ n] := show -(_ β’β _)=-_*_, by rw [neg_mul_eq_neg_mul_symm, nsmul_eq_mul, nat.cast_succ]
theorem gsmul_eq_mul' [ring R] (a : R) (n : β€) : n β’β€ a = a * n :=
by rw [gsmul_eq_mul, (n.cast_commute a).eq]
theorem mul_gsmul_left [ring R] (a b : R) (n : β€) : n β’β€ (a * b) = a * (n β’β€ b) :=
by rw [gsmul_eq_mul', gsmul_eq_mul', mul_assoc]
theorem mul_gsmul_assoc [ring R] (a b : R) (n : β€) : n β’β€ (a * b) = n β’β€ a * b :=
by rw [gsmul_eq_mul, gsmul_eq_mul, mul_assoc]
@[simp]
lemma gsmul_int_int (a b : β€) : a β’β€ b = a * b := by simp [gsmul_eq_mul]
lemma gsmul_int_one (n : β€) : n β’β€ 1 = n := by simp
@[simp, norm_cast] theorem int.cast_pow [ring R] (n : β€) (m : β) : (β(n ^ m) : R) = βn ^ m :=
by induction m with m ih; [exact int.cast_one,
rw [pow_succ, pow_succ, int.cast_mul, ih]]
lemma neg_one_pow_eq_pow_mod_two [ring R] {n : β} : (-1 : R) ^ n = -1 ^ (n % 2) :=
by rw [β nat.mod_add_div n 2, pow_add, pow_mul]; simp [pow_two]
theorem sq_sub_sq [comm_ring R] (a b : R) : a ^ 2 - b ^ 2 = (a + b) * (a - b) :=
by rw [pow_two, pow_two, mul_self_sub_mul_self]
theorem pow_eq_zero [domain R] {x : R} {n : β} (H : x^n = 0) : x = 0 :=
begin
induction n with n ih,
{ rw pow_zero at H,
rw [β mul_one x, H, mul_zero] },
exact or.cases_on (mul_eq_zero.1 H) id ih
end
@[field_simps] theorem pow_ne_zero [domain R] {a : R} (n : β) (h : a β 0) : a ^ n β 0 :=
mt pow_eq_zero h
theorem nsmul_nonneg [ordered_add_comm_monoid R] {a : R} (H : 0 β€ a) : β n : β, 0 β€ n β’β a
| 0 := le_refl _
| (n+1) := add_nonneg' H (nsmul_nonneg n)
lemma pow_abs [decidable_linear_ordered_comm_ring R] (a : R) (n : β) : (abs a)^n = abs (a^n) :=
by induction n with n ih; [exact (abs_one).symm,
rw [pow_succ, pow_succ, ih, abs_mul]]
lemma abs_neg_one_pow [decidable_linear_ordered_comm_ring R] (n : β) : abs ((-1 : R)^n) = 1 :=
by rw [βpow_abs, abs_neg, abs_one, one_pow]
section add_monoid
variable [ordered_add_comm_monoid A]
theorem nsmul_le_nsmul {a : A} {n m : β} (ha : 0 β€ a) (h : n β€ m) : n β’β a β€ m β’β a :=
let β¨k, hkβ© := nat.le.dest h in
calc n β’β a = n β’β a + 0 : (add_zero _).symm
... β€ n β’β a + k β’β a : add_le_add_left' (nsmul_nonneg ha _)
... = m β’β a : by rw [β hk, add_nsmul]
lemma nsmul_le_nsmul_of_le_right {a b : A} (hab : a β€ b) : β i : β, i β’β a β€ i β’β b
| 0 := by simp
| (k+1) := add_le_add' hab (nsmul_le_nsmul_of_le_right _)
end add_monoid
namespace canonically_ordered_semiring
variable [canonically_ordered_comm_semiring R]
theorem pow_pos {a : R} (H : 0 < a) : β n : β, 0 < a ^ n
| 0 := canonically_ordered_semiring.zero_lt_one
| (n+1) := canonically_ordered_semiring.mul_pos.2 β¨H, pow_pos nβ©
lemma pow_le_pow_of_le_left {a b : R} (hab : a β€ b) : β i : β, a^i β€ b^i
| 0 := by simp
| (k+1) := canonically_ordered_semiring.mul_le_mul hab (pow_le_pow_of_le_left k)
theorem one_le_pow_of_one_le {a : R} (H : 1 β€ a) (n : β) : 1 β€ a ^ n :=
by simpa only [one_pow] using pow_le_pow_of_le_left H n
theorem pow_le_one {a : R} (H : a β€ 1) (n : β) : a ^ n β€ 1:=
by simpa only [one_pow] using pow_le_pow_of_le_left H n
end canonically_ordered_semiring
section linear_ordered_semiring
variable [linear_ordered_semiring R]
theorem pow_pos {a : R} (H : 0 < a) : β (n : β), 0 < a ^ n
| 0 := zero_lt_one
| (n+1) := mul_pos H (pow_pos _)
theorem pow_nonneg {a : R} (H : 0 β€ a) : β (n : β), 0 β€ a ^ n
| 0 := zero_le_one
| (n+1) := mul_nonneg H (pow_nonneg _)
theorem pow_lt_pow_of_lt_left {x y : R} {n : β} (Hxy : x < y) (Hxpos : 0 β€ x) (Hnpos : 0 < n) :
x ^ n < y ^ n :=
begin
cases lt_or_eq_of_le Hxpos,
{ rw βnat.sub_add_cancel Hnpos,
induction (n - 1), { simpa only [pow_one] },
rw [pow_add, pow_add, nat.succ_eq_add_one, pow_one, pow_one],
apply mul_lt_mul ih (le_of_lt Hxy) h (le_of_lt (pow_pos (lt_trans h Hxy) _)) },
{ rw [βh, zero_pow Hnpos], apply pow_pos (by rwa βh at Hxy : 0 < y),}
end
theorem pow_left_inj {x y : R} {n : β} (Hxpos : 0 β€ x) (Hypos : 0 β€ y) (Hnpos : 0 < n)
(Hxyn : x ^ n = y ^ n) : x = y :=
begin
rcases lt_trichotomy x y with hxy | rfl | hyx,
{ exact absurd Hxyn (ne_of_lt (pow_lt_pow_of_lt_left hxy Hxpos Hnpos)) },
{ refl },
{ exact absurd Hxyn (ne_of_gt (pow_lt_pow_of_lt_left hyx Hypos Hnpos)) },
end
theorem one_le_pow_of_one_le {a : R} (H : 1 β€ a) : β (n : β), 1 β€ a ^ n
| 0 := le_refl _
| (n+1) := by simpa only [mul_one] using mul_le_mul H (one_le_pow_of_one_le n)
zero_le_one (le_trans zero_le_one H)
/-- Bernoulli's inequality. This version works for semirings but requires
an additional hypothesis `0 β€ a * a`. -/
theorem one_add_mul_le_pow' {a : R} (Hsqr : 0 β€ a * a) (H : 0 β€ 1 + a) :
β (n : β), 1 + n β’β a β€ (1 + a) ^ n
| 0 := le_of_eq $ add_zero _
| (n+1) :=
calc 1 + (n + 1) β’β a β€ (1 + a) * (1 + n β’β a) :
by simpa [succ_nsmul, mul_add, add_mul, mul_nsmul_left, add_comm, add_left_comm]
using nsmul_nonneg Hsqr n
... β€ (1 + a)^(n+1) : mul_le_mul_of_nonneg_left (one_add_mul_le_pow' n) H
theorem pow_le_pow {a : R} {n m : β} (ha : 1 β€ a) (h : n β€ m) : a ^ n β€ a ^ m :=
let β¨k, hkβ© := nat.le.dest h in
calc a ^ n = a ^ n * 1 : (mul_one _).symm
... β€ a ^ n * a ^ k : mul_le_mul_of_nonneg_left
(one_le_pow_of_one_le ha _)
(pow_nonneg (le_trans zero_le_one ha) _)
... = a ^ m : by rw [βhk, pow_add]
lemma pow_lt_pow {a : R} {n m : β} (h : 1 < a) (h2 : n < m) : a ^ n < a ^ m :=
begin
have h' : 1 β€ a := le_of_lt h,
have h'' : 0 < a := lt_trans zero_lt_one h,
cases m, cases h2, rw [pow_succ, βone_mul (a ^ n)],
exact mul_lt_mul h (pow_le_pow h' (nat.le_of_lt_succ h2)) (pow_pos h'' _) (le_of_lt h'')
end
lemma pow_le_pow_of_le_left {a b : R} (ha : 0 β€ a) (hab : a β€ b) : β i : β, a^i β€ b^i
| 0 := by simp
| (k+1) := mul_le_mul hab (pow_le_pow_of_le_left _) (pow_nonneg ha _) (le_trans ha hab)
lemma lt_of_pow_lt_pow {a b : R} (n : β) (hb : 0 β€ b) (h : a ^ n < b ^ n) : a < b :=
lt_of_not_ge $ Ξ» hn, not_lt_of_ge (pow_le_pow_of_le_left hb hn _) h
private lemma pow_lt_pow_of_lt_one_aux {a : R} (h : 0 < a) (ha : a < 1) (i : β) :
β k : β, a ^ (i + k + 1) < a ^ i
| 0 :=
begin
simp only [add_zero],
rw βone_mul (a^i), exact mul_lt_mul ha (le_refl _) (pow_pos h _) zero_le_one
end
| (k+1) :=
begin
rw βone_mul (a^i),
apply mul_lt_mul ha _ _ zero_le_one,
{ apply le_of_lt, apply pow_lt_pow_of_lt_one_aux },
{ show 0 < a ^ (i + (k + 1) + 0), apply pow_pos h }
end
private lemma pow_le_pow_of_le_one_aux {a : R} (h : 0 β€ a) (ha : a β€ 1) (i : β) :
β k : β, a ^ (i + k) β€ a ^ i
| 0 := by simp
| (k+1) := by rw [βadd_assoc, βone_mul (a^i)];
exact mul_le_mul ha (pow_le_pow_of_le_one_aux _) (pow_nonneg h _) zero_le_one
lemma pow_lt_pow_of_lt_one {a : R} (h : 0 < a) (ha : a < 1)
{i j : β} (hij : i < j) : a ^ j < a ^ i :=
let β¨k, hkβ© := nat.exists_eq_add_of_lt hij in
by rw hk; exact pow_lt_pow_of_lt_one_aux h ha _ _
lemma pow_le_pow_of_le_one {a : R} (h : 0 β€ a) (ha : a β€ 1)
{i j : β} (hij : i β€ j) : a ^ j β€ a ^ i :=
let β¨k, hkβ© := nat.exists_eq_add_of_le hij in
by rw hk; exact pow_le_pow_of_le_one_aux h ha _ _
lemma pow_le_one {x : R} : β (n : β) (h0 : 0 β€ x) (h1 : x β€ 1), x ^ n β€ 1
| 0 h0 h1 := le_refl (1 : R)
| (n+1) h0 h1 := mul_le_one h1 (pow_nonneg h0 _) (pow_le_one n h0 h1)
end linear_ordered_semiring
theorem pow_two_nonneg [linear_ordered_ring R] (a : R) : 0 β€ a ^ 2 :=
by { rw pow_two, exact mul_self_nonneg _ }
/-- Bernoulli's inequality for `n : β`, `-2 β€ a`. -/
theorem one_add_mul_le_pow [linear_ordered_ring R] {a : R} (H : -2 β€ a) :
β (n : β), 1 + n β’β a β€ (1 + a) ^ n
| 0 := le_of_eq $ add_zero _
| 1 := by simp
| (n+2) :=
have H' : 0 β€ 2 + a,
from neg_le_iff_add_nonneg.1 H,
have 0 β€ n β’β (a * a * (2 + a)) + a * a,
from add_nonneg (nsmul_nonneg (mul_nonneg (mul_self_nonneg a) H') n)
(mul_self_nonneg a),
calc 1 + (n + 2) β’β a β€ 1 + (n + 2) β’β a + (n β’β (a * a * (2 + a)) + a * a) :
(le_add_iff_nonneg_right _).2 this
... = (1 + a) * (1 + a) * (1 + n β’β a) :
by { simp only [add_mul, mul_add, mul_two, mul_one, one_mul, succ_nsmul, nsmul_add,
mul_nsmul_assoc, (mul_nsmul_left _ _ _).symm],
ac_refl }
... β€ (1 + a) * (1 + a) * (1 + a)^n :
mul_le_mul_of_nonneg_left (one_add_mul_le_pow n) (mul_self_nonneg (1 + a))
... = (1 + a)^(n + 2) : by simp only [pow_succ, mul_assoc]
/-- Bernoulli's inequality reformulated to estimate `a^n`. -/
theorem one_add_sub_mul_le_pow [linear_ordered_ring R]
{a : R} (H : -1 β€ a) (n : β) : 1 + n β’β (a - 1) β€ a ^ n :=
have -2 β€ a - 1, by { rw [bit0, neg_add], exact sub_le_sub_right H 1 },
by simpa only [add_sub_cancel'_right] using one_add_mul_le_pow this n
namespace int
lemma units_pow_two (u : units β€) : u ^ 2 = 1 :=
(units_eq_one_or u).elim (Ξ» h, h.symm βΈ rfl) (Ξ» h, h.symm βΈ rfl)
lemma units_pow_eq_pow_mod_two (u : units β€) (n : β) : u ^ n = u ^ (n % 2) :=
by conv {to_lhs, rw β nat.mod_add_div n 2}; rw [pow_add, pow_mul, units_pow_two, one_pow, mul_one]
@[simp] lemma nat_abs_pow_two (x : β€) : (x.nat_abs ^ 2 : β€) = x ^ 2 :=
by rw [pow_two, int.nat_abs_mul_self', pow_two]
end int
@[simp] lemma neg_square {Ξ±} [ring Ξ±] (z : Ξ±) : (-z)^2 = z^2 :=
by simp [pow, monoid.pow]
lemma of_add_nsmul [add_monoid A] (x : A) (n : β) :
multiplicative.of_add (n β’β x) = (multiplicative.of_add x)^n := rfl
lemma of_add_gsmul [add_group A] (x : A) (n : β€) :
multiplicative.of_add (n β’β€ x) = (multiplicative.of_add x)^n := rfl
variables (M G A)
/-- Monoid homomorphisms from `multiplicative β` are defined by the image
of `multiplicative.of_add 1`. -/
def powers_hom [monoid M] : M β (multiplicative β β* M) :=
{ to_fun := Ξ» x, β¨Ξ» n, x ^ n.to_add, pow_zero x, Ξ» m n, pow_add x m nβ©,
inv_fun := Ξ» f, f (multiplicative.of_add 1),
left_inv := pow_one,
right_inv := Ξ» f, monoid_hom.ext $ Ξ» n, by { simp [β f.map_pow, β of_add_nsmul] } }
/-- Monoid homomorphisms from `multiplicative β€` are defined by the image
of `multiplicative.of_add 1`. -/
def gpowers_hom [group G] : G β (multiplicative β€ β* G) :=
{ to_fun := Ξ» x, β¨Ξ» n, x ^ n.to_add, gpow_zero x, Ξ» m n, gpow_add x m nβ©,
inv_fun := Ξ» f, f (multiplicative.of_add 1),
left_inv := gpow_one,
right_inv := Ξ» f, monoid_hom.ext $ Ξ» n, by { simp [β f.map_gpow, β of_add_gsmul ] } }
/-- Additive homomorphisms from `β` are defined by the image of `1`. -/
def multiples_hom [add_monoid A] : A β (β β+ A) :=
{ to_fun := Ξ» x, β¨Ξ» n, n β’β x, zero_nsmul x, Ξ» m n, add_nsmul _ _ _β©,
inv_fun := Ξ» f, f 1,
left_inv := one_nsmul,
right_inv := Ξ» f, add_monoid_hom.ext_nat $ one_nsmul (f 1) }
/-- Additive homomorphisms from `β€` are defined by the image of `1`. -/
def gmultiples_hom [add_group A] : A β (β€ β+ A) :=
{ to_fun := Ξ» x, β¨Ξ» n, n β’β€ x, zero_gsmul x, Ξ» m n, add_gsmul _ _ _β©,
inv_fun := Ξ» f, f 1,
left_inv := one_gsmul,
right_inv := Ξ» f, add_monoid_hom.ext_int $ one_gsmul (f 1) }
variables {M G A}
@[simp] lemma powers_hom_apply [monoid M] (x : M) (n : multiplicative β) :
powers_hom M x n = x ^ n.to_add := rfl
@[simp] lemma powers_hom_symm_apply [monoid M] (f : multiplicative β β* M) :
(powers_hom M).symm f = f (multiplicative.of_add 1) := rfl
lemma mnat_monoid_hom_eq [monoid M] (f : multiplicative β β* M) (n : multiplicative β) :
f n = (f (multiplicative.of_add 1)) ^ n.to_add :=
by rw [β powers_hom_symm_apply, β powers_hom_apply, equiv.apply_symm_apply]
lemma mnat_monoid_hom_ext [monoid M] β¦f g : multiplicative β β* Mβ¦
(h : f (multiplicative.of_add 1) = g (multiplicative.of_add 1)) : f = g :=
monoid_hom.ext $ Ξ» n, by rw [mnat_monoid_hom_eq f, mnat_monoid_hom_eq g, h]
/-!
### Commutativity (again)
Facts about `semiconj_by` and `commute` that require `gpow` or `gsmul`, or the fact that integer
multiplication equals semiring multiplication.
-/
namespace semiconj_by
section
variables [semiring R] {a x y : R}
@[simp] lemma cast_nat_mul_right (h : semiconj_by a x y) (n : β) : semiconj_by a ((n : R) * x) (n * y) :=
semiconj_by.mul_right (nat.commute_cast _ _) h
@[simp] lemma cast_nat_mul_left (h : semiconj_by a x y) (n : β) : semiconj_by ((n : R) * a) x y :=
semiconj_by.mul_left (nat.cast_commute _ _) h
@[simp] lemma cast_nat_mul_cast_nat_mul (h : semiconj_by a x y) (m n : β) :
semiconj_by ((m : R) * a) (n * x) (n * y) :=
(h.cast_nat_mul_left m).cast_nat_mul_right n
end
variables [monoid M] [group G] [ring R]
@[simp] lemma units_gpow_right {a : M} {x y : units M} (h : semiconj_by a x y) :
β m : β€, semiconj_by a (β(x^m)) (β(y^m))
| (n : β) := by simp only [gpow_coe_nat, units.coe_pow, h, pow_right]
| -[1+n] := by simp only [gpow_neg_succ_of_nat, units.coe_pow, units_inv_right, h, pow_right]
@[simp] lemma gpow_right {a x y : G} (h : semiconj_by a x y) : β m : β€, semiconj_by a (x^m) (y^m)
| (n : β) := h.pow_right n
| -[1+n] := (h.pow_right n.succ).inv_right
variables {a b x y x' y' : R}
@[simp] lemma cast_int_mul_right (h : semiconj_by a x y) (m : β€) :
semiconj_by a ((m : β€) * x) (m * y) :=
semiconj_by.mul_right (int.commute_cast _ _) h
@[simp] lemma cast_int_mul_left (h : semiconj_by a x y) (m : β€) : semiconj_by ((m : R) * a) x y :=
semiconj_by.mul_left (int.cast_commute _ _) h
@[simp] lemma cast_int_mul_cast_int_mul (h : semiconj_by a x y) (m n : β€) :
semiconj_by ((m : R) * a) (n * x) (n * y) :=
(h.cast_int_mul_left m).cast_int_mul_right n
end semiconj_by
namespace commute
section
variables [semiring R] {a b : R}
@[simp] theorem cast_nat_mul_right (h : commute a b) (n : β) : commute a ((n : R) * b) :=
h.cast_nat_mul_right n
@[simp] theorem cast_nat_mul_left (h : commute a b) (n : β) : commute ((n : R) * a) b :=
h.cast_nat_mul_left n
@[simp] theorem cast_nat_mul_cast_nat_mul (h : commute a b) (m n : β) :
commute ((m : R) * a) (n * b) :=
h.cast_nat_mul_cast_nat_mul m n
@[simp] theorem self_cast_nat_mul (n : β) : commute a (n * a) :=
(commute.refl a).cast_nat_mul_right n
@[simp] theorem cast_nat_mul_self (n : β) : commute ((n : R) * a) a :=
(commute.refl a).cast_nat_mul_left n
@[simp] theorem self_cast_nat_mul_cast_nat_mul (m n : β) : commute ((m : R) * a) (n * a) :=
(commute.refl a).cast_nat_mul_cast_nat_mul m n
end
variables [monoid M] [group G] [ring R]
@[simp] lemma units_gpow_right {a : M} {u : units M} (h : commute a u) (m : β€) :
commute a (β(u^m)) :=
h.units_gpow_right m
@[simp] lemma units_gpow_left {u : units M} {a : M} (h : commute βu a) (m : β€) :
commute (β(u^m)) a :=
(h.symm.units_gpow_right m).symm
section
variables {a b : G}
@[simp] lemma gpow_right (h : commute a b) (m : β€) : commute a (b^m) :=
h.gpow_right m
@[simp] lemma gpow_left (h : commute a b) (m : β€) : commute (a^m) b :=
(h.symm.gpow_right m).symm
lemma gpow_gpow (h : commute a b) (m n : β€) : commute (a^m) (b^n) := (h.gpow_left m).gpow_right n
variables (a) (m n : β)
@[simp] theorem self_gpow : commute a (a ^ n) := (commute.refl a).gpow_right n
@[simp] theorem gpow_self : commute (a ^ n) a := (commute.refl a).gpow_left n
@[simp] theorem gpow_gpow_self : commute (a ^ m) (a ^ n) := (commute.refl a).gpow_gpow m n
end
variables {a b : R}
@[simp] lemma cast_int_mul_right (h : commute a b) (m : β€) : commute a (m * b) :=
h.cast_int_mul_right m
@[simp] lemma cast_int_mul_left (h : commute a b) (m : β€) : commute ((m : R) * a) b :=
h.cast_int_mul_left m
lemma cast_int_mul_cast_int_mul (h : commute a b) (m n : β€) : commute ((m : R) * a) (n * b) :=
h.cast_int_mul_cast_int_mul m n
variables (a) (m n : β€)
@[simp] theorem self_cast_int_mul : commute a (n * a) := (commute.refl a).cast_int_mul_right n
@[simp] theorem cast_int_mul_self : commute ((n : R) * a) a := (commute.refl a).cast_int_mul_left n
theorem self_cast_int_mul_cast_int_mul : commute ((m : R) * a) (n * a) :=
(commute.refl a).cast_int_mul_cast_int_mul m n
end commute
namespace units
variables [monoid M]
lemma conj_pow (u : units M) (x : M) (n : β) : (βu * x * β(uβ»ΒΉ))^n = u * x^n * β(uβ»ΒΉ) :=
(divp_eq_iff_mul_eq.2 ((u.mk_semiconj_by x).pow_right n).eq.symm).symm
lemma conj_pow' (u : units M) (x : M) (n : β) : (β(uβ»ΒΉ) * x * u)^n = β(uβ»ΒΉ) * x^n * u:=
(uβ»ΒΉ).conj_pow x n
end units
|
d272b6e31ace33b561f0ef2019ccfc450954d17e | 367134ba5a65885e863bdc4507601606690974c1 | /src/data/polynomial/degree/definitions.lean | b42d334b435c352981191642fb5dd1e240c2c770 | [
"Apache-2.0"
] | permissive | kodyvajjha/mathlib | 9bead00e90f68269a313f45f5561766cfd8d5cad | b98af5dd79e13a38d84438b850a2e8858ec21284 | refs/heads/master | 1,624,350,366,310 | 1,615,563,062,000 | 1,615,563,062,000 | 162,666,963 | 0 | 0 | Apache-2.0 | 1,545,367,651,000 | 1,545,367,651,000 | null | UTF-8 | Lean | false | false | 38,277 | 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 data.polynomial.coeff
import data.nat.with_bot
/-!
# Theory of univariate polynomials
The definitions include
`degree`, `monic`, `leading_coeff`
Results include
- `degree_mul` : The degree of the product is the sum of degrees
- `leading_coeff_add_of_degree_eq` and `leading_coeff_add_of_degree_lt` :
The leading_coefficient of a sum is determined by the leading coefficients and degrees
-/
noncomputable theory
local attribute [instance, priority 100] classical.prop_decidable
open finsupp finset
open_locale big_operators
namespace polynomial
universes u v
variables {R : Type u} {S : Type v} {a b : R} {n m : β}
section semiring
variables [semiring R] {p q r : polynomial R}
/-- `degree p` is the degree of the polynomial `p`, i.e. the largest `X`-exponent in `p`.
`degree p = some n` when `p β 0` and `n` is the highest power of `X` that appears in `p`, otherwise
`degree 0 = β₯`. -/
def degree (p : polynomial R) : with_bot β := p.support.sup some
lemma degree_lt_wf : well_founded (Ξ»p q : polynomial R, degree p < degree q) :=
inv_image.wf degree (with_bot.well_founded_lt nat.lt_wf)
instance : has_well_founded (polynomial R) := β¨_, degree_lt_wfβ©
/-- `nat_degree p` forces `degree p` to β, by defining nat_degree 0 = 0. -/
def nat_degree (p : polynomial R) : β := (degree p).get_or_else 0
/-- `leading_coeff p` gives the coefficient of the highest power of `X` in `p`-/
def leading_coeff (p : polynomial R) : R := coeff p (nat_degree p)
/-- a polynomial is `monic` if its leading coefficient is 1 -/
def monic (p : polynomial R) := leading_coeff p = (1 : R)
@[nontriviality] lemma monic_of_subsingleton [subsingleton R] (p : polynomial R) : monic p :=
subsingleton.elim _ _
lemma monic.def : monic p β leading_coeff p = 1 := iff.rfl
instance monic.decidable [decidable_eq R] : decidable (monic p) :=
by unfold monic; apply_instance
@[simp] lemma monic.leading_coeff {p : polynomial R} (hp : p.monic) :
leading_coeff p = 1 := hp
lemma monic.coeff_nat_degree {p : polynomial R} (hp : p.monic) : p.coeff p.nat_degree = 1 := hp
@[simp] lemma degree_zero : degree (0 : polynomial R) = β₯ := rfl
@[simp] lemma nat_degree_zero : nat_degree (0 : polynomial R) = 0 := rfl
@[simp] lemma coeff_nat_degree : coeff p (nat_degree p) = leading_coeff p := rfl
lemma degree_eq_bot : degree p = β₯ β p = 0 :=
β¨Ξ» h, by rw [degree, β max_eq_sup_with_bot] at h;
exact support_eq_empty.1 (max_eq_none.1 h),
Ξ» h, h.symm βΈ rflβ©
@[nontriviality] lemma degree_of_subsingleton [subsingleton R] : degree p = β₯ :=
by rw [subsingleton.elim p 0, degree_zero]
@[nontriviality] lemma nat_degree_of_subsingleton [subsingleton R] : nat_degree p = 0 :=
by rw [subsingleton.elim p 0, nat_degree_zero]
lemma degree_eq_nat_degree (hp : p β 0) : degree p = (nat_degree p : with_bot β) :=
let β¨n, hnβ© :=
not_forall.1 (mt option.eq_none_iff_forall_not_mem.2 (mt degree_eq_bot.1 hp)) in
have hn : degree p = some n := not_not.1 hn,
by rw [nat_degree, hn]; refl
lemma degree_eq_iff_nat_degree_eq {p : polynomial R} {n : β} (hp : p β 0) :
p.degree = n β p.nat_degree = n :=
by rw [degree_eq_nat_degree hp, with_bot.coe_eq_coe]
lemma degree_eq_iff_nat_degree_eq_of_pos {p : polynomial R} {n : β} (hn : 0 < n) :
p.degree = n β p.nat_degree = n :=
begin
split,
{ intro H, rwa β degree_eq_iff_nat_degree_eq, rintro rfl,
rw degree_zero at H, exact option.no_confusion H },
{ intro H, rwa degree_eq_iff_nat_degree_eq, rintro rfl,
rw nat_degree_zero at H, rw H at hn, exact lt_irrefl _ hn }
end
lemma nat_degree_eq_of_degree_eq_some {p : polynomial R} {n : β}
(h : degree p = n) : nat_degree p = n :=
have hp0 : p β 0, from Ξ» hp0, by rw hp0 at h; exact option.no_confusion h,
option.some_inj.1 $ show (nat_degree p : with_bot β) = n,
by rwa [β degree_eq_nat_degree hp0]
@[simp] lemma degree_le_nat_degree : degree p β€ nat_degree p :=
with_bot.gi_get_or_else_bot.gc.le_u_l _
lemma nat_degree_eq_of_degree_eq [semiring S] {q : polynomial S} (h : degree p = degree q) :
nat_degree p = nat_degree q :=
by unfold nat_degree; rw h
lemma le_degree_of_ne_zero (h : coeff p n β 0) : (n : with_bot β) β€ degree p :=
show @has_le.le (with_bot β) _ (some n : with_bot β) (p.support.sup some : with_bot β),
from finset.le_sup (finsupp.mem_support_iff.2 h)
lemma le_nat_degree_of_ne_zero (h : coeff p n β 0) : n β€ nat_degree p :=
begin
rw [β with_bot.coe_le_coe, β degree_eq_nat_degree],
exact le_degree_of_ne_zero h,
{ assume h, subst h, exact h rfl }
end
lemma le_nat_degree_of_mem_supp (a : β) :
a β p.support β a β€ nat_degree p:=
le_nat_degree_of_ne_zero β mem_support_iff.mp
lemma supp_subset_range (h : nat_degree p < m) : p.support β finset.range m :=
Ξ» n hn, mem_range.2 $ (le_nat_degree_of_mem_supp _ hn).trans_lt h
lemma supp_subset_range_nat_degree_succ : p.support β finset.range (nat_degree p + 1) :=
supp_subset_range (nat.lt_succ_self _)
lemma degree_le_degree (h : coeff q (nat_degree p) β 0) : degree p β€ degree q :=
begin
by_cases hp : p = 0,
{ rw hp, exact bot_le },
{ rw degree_eq_nat_degree hp, exact le_degree_of_ne_zero h }
end
lemma degree_ne_of_nat_degree_ne {n : β} :
p.nat_degree β n β degree p β n :=
mt $ Ξ» h, by rw [nat_degree, h, option.get_or_else_coe]
theorem nat_degree_le_iff_degree_le {n : β} : nat_degree p β€ n β degree p β€ n :=
with_bot.get_or_else_bot_le_iff
alias polynomial.nat_degree_le_iff_degree_le β . .
lemma nat_degree_le_nat_degree (hpq : p.degree β€ q.degree) : p.nat_degree β€ q.nat_degree :=
with_bot.gi_get_or_else_bot.gc.monotone_l hpq
@[simp] lemma degree_C (ha : a β 0) : degree (C a) = (0 : with_bot β) :=
show sup (ite (a = 0) β
{0}) some = 0, by rw if_neg ha; refl
lemma degree_C_le : degree (C a) β€ (0 : with_bot β) :=
by by_cases h : a = 0; [rw [h, C_0], rw [degree_C h]]; [exact bot_le, exact le_refl _]
lemma degree_one_le : degree (1 : polynomial R) β€ (0 : with_bot β) :=
by rw [β C_1]; exact degree_C_le
@[simp] lemma nat_degree_C (a : R) : nat_degree (C a) = 0 :=
begin
by_cases ha : a = 0,
{ have : C a = 0, { rw [ha, C_0] },
rw [nat_degree, degree_eq_bot.2 this],
refl },
{ rw [nat_degree, degree_C ha], refl }
end
@[simp] lemma nat_degree_one : nat_degree (1 : polynomial R) = 0 := nat_degree_C 1
@[simp] lemma nat_degree_nat_cast (n : β) : nat_degree (n : polynomial R) = 0 :=
by simp only [βC_eq_nat_cast, nat_degree_C]
@[simp] lemma degree_monomial (n : β) (ha : a β 0) : degree (monomial n a) = n :=
by rw [degree, support_monomial _ _ ha]; refl
@[simp] lemma degree_C_mul_X_pow (n : β) (ha : a β 0) : degree (C a * X ^ n) = n :=
by rw [β single_eq_C_mul_X, degree_monomial n ha]
lemma degree_monomial_le (n : β) (a : R) : degree (monomial n a) β€ n :=
if h : a = 0 then by rw [h, (monomial n).map_zero]; exact bot_le else le_of_eq (degree_monomial n h)
lemma degree_C_mul_X_pow_le (n : β) (a : R) : degree (C a * X ^ n) β€ n :=
by { rw C_mul_X_pow_eq_monomial, apply degree_monomial_le }
@[simp] lemma nat_degree_C_mul_X_pow (n : β) (a : R) (ha : a β 0) : nat_degree (C a * X ^ n) = n :=
nat_degree_eq_of_degree_eq_some (degree_C_mul_X_pow n ha)
@[simp] lemma nat_degree_C_mul_X (a : R) (ha : a β 0) : nat_degree (C a * X) = 1 :=
by simpa only [pow_one] using nat_degree_C_mul_X_pow 1 a ha
@[simp] lemma nat_degree_monomial (i : β) (r : R) (hr : r β 0) :
nat_degree (monomial i r) = i :=
by rw [β C_mul_X_pow_eq_monomial, nat_degree_C_mul_X_pow i r hr]
lemma coeff_eq_zero_of_degree_lt (h : degree p < n) : coeff p n = 0 :=
not_not.1 (mt le_degree_of_ne_zero (not_le_of_gt h))
lemma coeff_eq_zero_of_nat_degree_lt {p : polynomial R} {n : β} (h : p.nat_degree < n) :
p.coeff n = 0 :=
begin
apply coeff_eq_zero_of_degree_lt,
by_cases hp : p = 0,
{ subst hp, exact with_bot.bot_lt_coe n },
{ rwa [degree_eq_nat_degree hp, with_bot.coe_lt_coe] }
end
@[simp] lemma coeff_nat_degree_succ_eq_zero {p : polynomial R} : p.coeff (p.nat_degree + 1) = 0 :=
coeff_eq_zero_of_nat_degree_lt (lt_add_one _)
-- We need the explicit `decidable` argument here because an exotic one shows up in a moment!
lemma ite_le_nat_degree_coeff (p : polynomial R) (n : β) (I : decidable (n < 1 + nat_degree p)) :
@ite _ (n < 1 + nat_degree p) I (coeff p n) 0 = coeff p n :=
begin
split_ifs,
{ refl },
{ exact (coeff_eq_zero_of_nat_degree_lt (not_le.1 (Ξ» w, h (nat.lt_one_add_iff.2 w)))).symm, }
end
lemma as_sum_support (p : polynomial R) :
p = β i in p.support, monomial i (p.coeff i) :=
p.sum_single.symm
lemma as_sum_support_C_mul_X_pow (p : polynomial R) :
p = β i in p.support, C (p.coeff i) * X^i :=
trans p.as_sum_support $ by simp only [C_mul_X_pow_eq_monomial]
/--
We can reexpress a sum over `p.support` as a sum over `range n`,
for any `n` satisfying `p.nat_degree < n`.
-/
lemma sum_over_range' [add_comm_monoid S] (p : polynomial R) {f : β β R β S} (h : β n, f n 0 = 0)
(n : β) (w : p.nat_degree < n) :
p.sum f = β (a : β) in range n, f a (coeff p a) :=
finsupp.sum_of_support_subset _ (supp_subset_range w) _ $ Ξ» n hn, h n
/--
We can reexpress a sum over `p.support` as a sum over `range (p.nat_degree + 1)`.
-/
lemma sum_over_range [add_comm_monoid S] (p : polynomial R) {f : β β R β S} (h : β n, f n 0 = 0) :
p.sum f = β (a : β) in range (p.nat_degree + 1), f a (coeff p a) :=
sum_over_range' p h (p.nat_degree + 1) (lt_add_one _)
lemma as_sum_range' (p : polynomial R) (n : β) (w : p.nat_degree < n) :
p = β i in range n, monomial i (coeff p i) :=
p.sum_single.symm.trans $ p.sum_over_range' (Ξ» n, single_zero) _ w
lemma as_sum_range (p : polynomial R) :
p = β i in range (p.nat_degree + 1), monomial i (coeff p i) :=
p.sum_single.symm.trans $ p.sum_over_range $ Ξ» n, single_zero
lemma as_sum_range_C_mul_X_pow (p : polynomial R) :
p = β i in range (p.nat_degree + 1), C (coeff p i) * X ^ i :=
p.as_sum_range.trans $ by simp only [C_mul_X_pow_eq_monomial]
lemma coeff_ne_zero_of_eq_degree (hn : degree p = n) :
coeff p n β 0 :=
Ξ» h, mem_support_iff.mp (mem_of_max hn) h
lemma eq_X_add_C_of_degree_le_one (h : degree p β€ 1) :
p = C (p.coeff 1) * X + C (p.coeff 0) :=
ext (Ξ» n, nat.cases_on n (by simp)
(Ξ» n, nat.cases_on n (by simp [coeff_C])
(Ξ» m, have degree p < m.succ.succ, from lt_of_le_of_lt h dec_trivial,
by simp [coeff_eq_zero_of_degree_lt this, coeff_C, nat.succ_ne_zero, coeff_X,
nat.succ_inj', @eq_comm β 0])))
lemma eq_X_add_C_of_degree_eq_one (h : degree p = 1) :
p = C (p.leading_coeff) * X + C (p.coeff 0) :=
(eq_X_add_C_of_degree_le_one (show degree p β€ 1, from h βΈ le_refl _)).trans
(by simp [leading_coeff, nat_degree_eq_of_degree_eq_some h])
lemma eq_X_add_C_of_nat_degree_le_one (h : nat_degree p β€ 1) :
p = C (p.coeff 1) * X + C (p.coeff 0) :=
eq_X_add_C_of_degree_le_one $ degree_le_of_nat_degree_le h
lemma exists_eq_X_add_C_of_nat_degree_le_one (h : nat_degree p β€ 1) :
β a b, p = C a * X + C b :=
β¨p.coeff 1, p.coeff 0, eq_X_add_C_of_nat_degree_le_one hβ©
theorem degree_X_pow_le (n : β) : degree (X^n : polynomial R) β€ n :=
by simpa only [C_1, one_mul] using degree_C_mul_X_pow_le n (1:R)
theorem degree_X_le : degree (X : polynomial R) β€ 1 :=
degree_monomial_le _ _
lemma nat_degree_X_le : (X : polynomial R).nat_degree β€ 1 :=
nat_degree_le_of_degree_le degree_X_le
lemma support_C_mul_X_pow (c : R) (n : β) : (C c * X ^ n).support β singleton n :=
begin
rw [C_mul_X_pow_eq_monomial],
exact support_single_subset
end
lemma mem_support_C_mul_X_pow {n a : β} {c : R} (h : a β (C c * X ^ n).support) : a = n :=
mem_singleton.1 $ support_C_mul_X_pow _ _ h
lemma card_support_C_mul_X_pow_le_one {c : R} {n : β} : (C c * X ^ n).support.card β€ 1 :=
begin
rw β card_singleton n,
apply card_le_of_subset (support_C_mul_X_pow c n),
end
lemma card_supp_le_succ_nat_degree (p : polynomial R) : p.support.card β€ p.nat_degree + 1 :=
begin
rw β finset.card_range (p.nat_degree + 1),
exact finset.card_le_of_subset supp_subset_range_nat_degree_succ,
end
lemma le_degree_of_mem_supp (a : β) :
a β p.support β βa β€ degree p :=
le_degree_of_ne_zero β mem_support_iff.mp
lemma nonempty_support_iff : p.support.nonempty β p β 0 :=
by rw [ne.def, nonempty_iff_ne_empty, ne.def, β support_eq_empty]
lemma support_C_mul_X_pow_nonzero {c : R} {n : β} (h : c β 0) :
(C c * X ^ n).support = singleton n :=
begin
rw [C_mul_X_pow_eq_monomial],
exact support_single_ne_zero h
end
end semiring
section nonzero_semiring
variables [semiring R] [nontrivial R] {p q : polynomial R}
@[simp] lemma degree_one : degree (1 : polynomial R) = (0 : with_bot β) :=
degree_C (show (1 : R) β 0, from zero_ne_one.symm)
@[simp] lemma degree_X : degree (X : polynomial R) = 1 :=
degree_monomial _ one_ne_zero
@[simp] lemma nat_degree_X : (X : polynomial R).nat_degree = 1 :=
nat_degree_eq_of_degree_eq_some degree_X
end nonzero_semiring
section ring
variables [ring R]
lemma coeff_mul_X_sub_C {p : polynomial R} {r : R} {a : β} :
coeff (p * (X - C r)) (a + 1) = coeff p a - coeff p (a + 1) * r :=
by simp [mul_sub]
lemma C_eq_int_cast (n : β€) : C (n : R) = n :=
(C : R β+* _).map_int_cast n
@[simp] lemma degree_neg (p : polynomial R) : degree (-p) = degree p :=
by unfold degree; rw support_neg
@[simp] lemma nat_degree_neg (p : polynomial R) : nat_degree (-p) = nat_degree p :=
by simp [nat_degree]
@[simp] lemma nat_degree_int_cast (n : β€) : nat_degree (n : polynomial R) = 0 :=
by simp only [βC_eq_int_cast, nat_degree_C]
end ring
section semiring
variables [semiring R]
/-- The second-highest coefficient, or 0 for constants -/
def next_coeff (p : polynomial R) : R :=
if p.nat_degree = 0 then 0 else p.coeff (p.nat_degree - 1)
@[simp]
lemma next_coeff_C_eq_zero (c : R) :
next_coeff (C c) = 0 := by { rw next_coeff, simp }
lemma next_coeff_of_pos_nat_degree (p : polynomial R) (hp : 0 < p.nat_degree) :
next_coeff p = p.coeff (p.nat_degree - 1) :=
by { rw [next_coeff, if_neg], contrapose! hp, simpa }
end semiring
section semiring
variables [semiring R] {p q : polynomial R} {ΞΉ : Type*}
lemma coeff_nat_degree_eq_zero_of_degree_lt (h : degree p < degree q) :
coeff p (nat_degree q) = 0 :=
coeff_eq_zero_of_degree_lt (lt_of_lt_of_le h degree_le_nat_degree)
lemma ne_zero_of_degree_gt {n : with_bot β} (h : n < degree p) : p β 0 :=
mt degree_eq_bot.2 (ne.symm (ne_of_lt (lt_of_le_of_lt bot_le h)))
lemma ne_zero_of_nat_degree_gt {n : β} (h : n < nat_degree p) : p β 0 :=
Ξ» H, by simpa [H, nat.not_lt_zero] using h
lemma degree_lt_degree (h : nat_degree p < nat_degree q) : degree p < degree q :=
begin
by_cases hp : p = 0,
{ simp [hp],
rw bot_lt_iff_ne_bot,
intro hq,
simpa [hp, degree_eq_bot.mp hq, lt_irrefl] using h },
{ rw [degree_eq_nat_degree hp, degree_eq_nat_degree $ ne_zero_of_nat_degree_gt h],
exact_mod_cast h }
end
lemma nat_degree_lt_nat_degree_iff (hp : p β 0) :
nat_degree p < nat_degree q β degree p < degree q :=
β¨degree_lt_degree, begin
intro h,
have hq : q β 0 := ne_zero_of_degree_gt h,
rw [degree_eq_nat_degree hp, degree_eq_nat_degree hq] at h,
exact_mod_cast h
endβ©
lemma eq_C_of_degree_le_zero (h : degree p β€ 0) : p = C (coeff p 0) :=
begin
ext (_|n), { simp },
rw [coeff_C, if_neg (nat.succ_ne_zero _), coeff_eq_zero_of_degree_lt],
exact h.trans_lt (with_bot.some_lt_some.2 n.succ_pos),
end
lemma eq_C_of_degree_eq_zero (h : degree p = 0) : p = C (coeff p 0) :=
eq_C_of_degree_le_zero (h βΈ le_refl _)
lemma degree_le_zero_iff : degree p β€ 0 β p = C (coeff p 0) :=
β¨eq_C_of_degree_le_zero, Ξ» h, h.symm βΈ degree_C_leβ©
lemma degree_add_le (p q : polynomial R) : degree (p + q) β€ max (degree p) (degree q) :=
calc degree (p + q) = ((p + q).support).sup some : rfl
... β€ (p.support βͺ q.support).sup some : by convert sup_mono support_add
... = p.support.sup some β q.support.sup some : by convert sup_union
... = _ : with_bot.sup_eq_max _ _
@[simp] lemma leading_coeff_zero : leading_coeff (0 : polynomial R) = 0 := rfl
@[simp] lemma leading_coeff_eq_zero : leading_coeff p = 0 β p = 0 :=
β¨Ξ» h, by_contradiction $ Ξ» hp, mt mem_support_iff.1
(not_not.2 h) (mem_of_max (degree_eq_nat_degree hp)),
Ξ» h, h.symm βΈ leading_coeff_zeroβ©
lemma leading_coeff_ne_zero : leading_coeff p β 0 β p β 0 :=
by rw [ne.def, leading_coeff_eq_zero]
lemma leading_coeff_eq_zero_iff_deg_eq_bot : leading_coeff p = 0 β degree p = β₯ :=
by rw [leading_coeff_eq_zero, degree_eq_bot]
lemma nat_degree_mem_support_of_nonzero (H : p β 0) : p.nat_degree β p.support :=
(p.mem_support_to_fun p.nat_degree).mpr ((not_congr leading_coeff_eq_zero).mpr H)
lemma nat_degree_eq_support_max' (h : p β 0) :
p.nat_degree = p.support.max' (nonempty_support_iff.mpr h) :=
(le_max' _ _ $ nat_degree_mem_support_of_nonzero h).antisymm $
max'_le _ _ _ le_nat_degree_of_mem_supp
lemma nat_degree_C_mul_X_pow_le (a : R) (n : β) : nat_degree (C a * X ^ n) β€ n :=
nat_degree_le_iff_degree_le.2 $ degree_C_mul_X_pow_le _ _
lemma degree_add_eq_left_of_degree_lt (h : degree q < degree p) : degree (p + q) = degree p :=
le_antisymm (max_eq_left_of_lt h βΈ degree_add_le _ _) $ degree_le_degree $
begin
rw [coeff_add, coeff_nat_degree_eq_zero_of_degree_lt h, add_zero],
exact mt leading_coeff_eq_zero.1 (ne_zero_of_degree_gt h)
end
lemma degree_add_eq_right_of_degree_lt (h : degree p < degree q) : degree (p + q) = degree q :=
by rw [add_comm, degree_add_eq_left_of_degree_lt h]
lemma degree_add_C (hp : 0 < degree p) : degree (p + C a) = degree p :=
add_comm (C a) p βΈ degree_add_eq_right_of_degree_lt $ lt_of_le_of_lt degree_C_le hp
lemma degree_add_eq_of_leading_coeff_add_ne_zero (h : leading_coeff p + leading_coeff q β 0) :
degree (p + q) = max p.degree q.degree :=
le_antisymm (degree_add_le _ _) $
match lt_trichotomy (degree p) (degree q) with
| or.inl hlt :=
by rw [degree_add_eq_right_of_degree_lt hlt, max_eq_right_of_lt hlt]; exact le_refl _
| or.inr (or.inl heq) :=
le_of_not_gt $
assume hlt : max (degree p) (degree q) > degree (p + q),
h $ show leading_coeff p + leading_coeff q = 0,
begin
rw [heq, max_self] at hlt,
rw [leading_coeff, leading_coeff, nat_degree_eq_of_degree_eq heq, β coeff_add],
exact coeff_nat_degree_eq_zero_of_degree_lt hlt
end
| or.inr (or.inr hlt) :=
by rw [degree_add_eq_left_of_degree_lt hlt, max_eq_left_of_lt hlt]; exact le_refl _
end
lemma degree_erase_le (p : polynomial R) (n : β) : degree (p.erase n) β€ degree p :=
by convert sup_mono (erase_subset _ _)
lemma degree_erase_lt (hp : p β 0) : degree (p.erase (nat_degree p)) < degree p :=
lt_of_le_of_ne (degree_erase_le _ _) $
(degree_eq_nat_degree hp).symm βΈ (by convert Ξ» h, not_mem_erase _ _ (mem_of_max h))
lemma degree_sum_le (s : finset ΞΉ) (f : ΞΉ β polynomial R) :
degree (β i in s, f i) β€ s.sup (Ξ» b, degree (f b)) :=
finset.induction_on s (by simp only [sum_empty, sup_empty, degree_zero, le_refl]) $
assume a s has ih,
calc degree (β i in insert a s, f i) β€ max (degree (f a)) (degree (β i in s, f i)) :
by rw sum_insert has; exact degree_add_le _ _
... β€ _ : by rw [sup_insert, with_bot.sup_eq_max]; exact max_le_max (le_refl _) ih
lemma degree_mul_le (p q : polynomial R) : degree (p * q) β€ degree p + degree q :=
calc degree (p * q) β€ (p.support).sup (Ξ»i, degree (sum q (Ξ»j a, C (coeff p i * a) * X ^ (i + j)))) :
by simp only [single_eq_C_mul_X.symm]; exact degree_sum_le _ _
... β€ p.support.sup (Ξ»i, q.support.sup (Ξ»j, degree (C (coeff p i * coeff q j) * X ^ (i + j)))) :
finset.sup_mono_fun (assume i hi, degree_sum_le _ _)
... β€ degree p + degree q :
begin
refine finset.sup_le (Ξ» a ha, finset.sup_le (Ξ» b hb, le_trans (degree_C_mul_X_pow_le _ _) _)),
rw [with_bot.coe_add],
rw mem_support_iff at ha hb,
exact add_le_add (le_degree_of_ne_zero ha) (le_degree_of_ne_zero hb)
end
lemma degree_pow_le (p : polynomial R) : β n, degree (p ^ n) β€ n β’β (degree p)
| 0 := by rw [pow_zero, zero_nsmul]; exact degree_one_le
| (n+1) := calc degree (p ^ (n + 1)) β€ degree p + degree (p ^ n) :
by rw pow_succ; exact degree_mul_le _ _
... β€ _ : by rw succ_nsmul; exact add_le_add (le_refl _) (degree_pow_le _)
@[simp] lemma leading_coeff_monomial (a : R) (n : β) : leading_coeff (monomial n a) = a :=
begin
by_cases ha : a = 0,
{ simp only [ha, (monomial n).map_zero, leading_coeff_zero] },
{ rw [leading_coeff, nat_degree_monomial _ _ ha],
exact @finsupp.single_eq_same _ _ _ n a }
end
lemma leading_coeff_C_mul_X_pow (a : R) (n : β) : leading_coeff (C a * X ^ n) = a :=
by rw [C_mul_X_pow_eq_monomial, leading_coeff_monomial]
@[simp] lemma leading_coeff_C (a : R) : leading_coeff (C a) = a :=
leading_coeff_monomial a 0
@[simp] lemma leading_coeff_X_pow (n : β) : leading_coeff ((X : polynomial R) ^ n) = 1 :=
by simpa only [C_1, one_mul] using leading_coeff_C_mul_X_pow (1 : R) n
@[simp] lemma leading_coeff_X : leading_coeff (X : polynomial R) = 1 :=
by simpa only [pow_one] using @leading_coeff_X_pow R _ 1
@[simp] lemma monic_X_pow (n : β) : monic (X ^ n : polynomial R) := leading_coeff_X_pow n
@[simp] lemma monic_X : monic (X : polynomial R) := leading_coeff_X
@[simp] lemma leading_coeff_one : leading_coeff (1 : polynomial R) = 1 :=
leading_coeff_C 1
@[simp] lemma monic_one : monic (1 : polynomial R) := leading_coeff_C _
lemma monic.ne_zero {R : Type*} [semiring R] [nontrivial R] {p : polynomial R} (hp : p.monic) :
p β 0 :=
by { rintro rfl, simpa [monic] using hp }
lemma monic.ne_zero_of_ne (h : (0:R) β 1) {p : polynomial R} (hp : p.monic) :
p β 0 :=
by { nontriviality R, exact hp.ne_zero }
lemma monic.ne_zero_of_polynomial_ne {r} (hp : monic p) (hne : q β r) : p β 0 :=
by { haveI := nontrivial.of_polynomial_ne hne, exact hp.ne_zero }
lemma leading_coeff_add_of_degree_lt (h : degree p < degree q) :
leading_coeff (p + q) = leading_coeff q :=
have coeff p (nat_degree q) = 0, from coeff_nat_degree_eq_zero_of_degree_lt h,
by simp only [leading_coeff, nat_degree_eq_of_degree_eq (degree_add_eq_right_of_degree_lt h),
this, coeff_add, zero_add]
lemma leading_coeff_add_of_degree_eq (h : degree p = degree q)
(hlc : leading_coeff p + leading_coeff q β 0) :
leading_coeff (p + q) = leading_coeff p + leading_coeff q :=
have nat_degree (p + q) = nat_degree p,
by apply nat_degree_eq_of_degree_eq;
rw [degree_add_eq_of_leading_coeff_add_ne_zero hlc, h, max_self],
by simp only [leading_coeff, this, nat_degree_eq_of_degree_eq h, coeff_add]
@[simp] lemma coeff_mul_degree_add_degree (p q : polynomial R) :
coeff (p * q) (nat_degree p + nat_degree q) = leading_coeff p * leading_coeff q :=
calc coeff (p * q) (nat_degree p + nat_degree q) =
β x in nat.antidiagonal (nat_degree p + nat_degree q),
coeff p x.1 * coeff q x.2 : coeff_mul _ _ _
... = coeff p (nat_degree p) * coeff q (nat_degree q) :
begin
refine finset.sum_eq_single (nat_degree p, nat_degree q) _ _,
{ rintro β¨i,jβ© hβ hβ, rw nat.mem_antidiagonal at hβ,
by_cases H : nat_degree p < i,
{ rw [coeff_eq_zero_of_degree_lt
(lt_of_le_of_lt degree_le_nat_degree (with_bot.coe_lt_coe.2 H)), zero_mul] },
{ rw not_lt_iff_eq_or_lt at H, cases H,
{ subst H, rw add_left_cancel_iff at hβ, dsimp at hβ, subst hβ, exfalso, exact hβ rfl },
{ suffices : nat_degree q < j,
{ rw [coeff_eq_zero_of_degree_lt
(lt_of_le_of_lt degree_le_nat_degree (with_bot.coe_lt_coe.2 this)), mul_zero] },
{ by_contra H', rw not_lt at H',
exact ne_of_lt (nat.lt_of_lt_of_le
(nat.add_lt_add_right H j) (nat.add_le_add_left H' _)) hβ } } } },
{ intro H, exfalso, apply H, rw nat.mem_antidiagonal }
end
lemma degree_mul' (h : leading_coeff p * leading_coeff q β 0) :
degree (p * q) = degree p + degree q :=
have hp : p β 0 := by refine mt _ h; exact Ξ» hp, by rw [hp, leading_coeff_zero, zero_mul],
have hq : q β 0 := by refine mt _ h; exact Ξ» hq, by rw [hq, leading_coeff_zero, mul_zero],
le_antisymm (degree_mul_le _ _)
begin
rw [degree_eq_nat_degree hp, degree_eq_nat_degree hq],
refine le_degree_of_ne_zero _,
rwa coeff_mul_degree_add_degree
end
lemma degree_mul_monic (hq : monic q) : degree (p * q) = degree p + degree q :=
if hp : p = 0 then by simp [hp]
else degree_mul' $ by rwa [hq.leading_coeff, mul_one, ne.def, leading_coeff_eq_zero]
lemma nat_degree_mul' (h : leading_coeff p * leading_coeff q β 0) :
nat_degree (p * q) = nat_degree p + nat_degree q :=
have hp : p β 0 := mt leading_coeff_eq_zero.2 (Ξ» hβ, h $ by rw [hβ, zero_mul]),
have hq : q β 0 := mt leading_coeff_eq_zero.2 (Ξ» hβ, h $ by rw [hβ, mul_zero]),
nat_degree_eq_of_degree_eq_some $
by rw [degree_mul' h, with_bot.coe_add, degree_eq_nat_degree hp, degree_eq_nat_degree hq]
lemma leading_coeff_mul' (h : leading_coeff p * leading_coeff q β 0) :
leading_coeff (p * q) = leading_coeff p * leading_coeff q :=
begin
unfold leading_coeff,
rw [nat_degree_mul' h, coeff_mul_degree_add_degree],
refl
end
lemma leading_coeff_pow' : leading_coeff p ^ n β 0 β
leading_coeff (p ^ n) = leading_coeff p ^ n :=
nat.rec_on n (by simp) $
Ξ» n ih h,
have hβ : leading_coeff p ^ n β 0 :=
Ξ» hβ, h $ by rw [pow_succ, hβ, mul_zero],
have hβ : leading_coeff p * leading_coeff (p ^ n) β 0 :=
by rwa [pow_succ, β ih hβ] at h,
by rw [pow_succ, pow_succ, leading_coeff_mul' hβ, ih hβ]
lemma degree_pow' : β {n}, leading_coeff p ^ n β 0 β
degree (p ^ n) = n β’β (degree p)
| 0 := Ξ» h, by rw [pow_zero, β C_1] at *;
rw [degree_C h, zero_nsmul]
| (n+1) := Ξ» h,
have hβ : leading_coeff p ^ n β 0 := Ξ» hβ, h $
by rw [pow_succ, hβ, mul_zero],
have hβ : leading_coeff p * leading_coeff (p ^ n) β 0 :=
by rwa [pow_succ, β leading_coeff_pow' hβ] at h,
by rw [pow_succ, degree_mul' hβ, succ_nsmul, degree_pow' hβ]
lemma nat_degree_pow' {n : β} (h : leading_coeff p ^ n β 0) :
nat_degree (p ^ n) = n * nat_degree p :=
if hp0 : p = 0 then
if hn0 : n = 0 then by simp *
else by rw [hp0, zero_pow (nat.pos_of_ne_zero hn0)]; simp
else
have hpn : p ^ n β 0, from Ξ» hpn0, have h1 : _ := h,
by rw [β leading_coeff_pow' h1, hpn0, leading_coeff_zero] at h;
exact h rfl,
option.some_inj.1 $ show (nat_degree (p ^ n) : with_bot β) = (n * nat_degree p : β),
by rw [β degree_eq_nat_degree hpn, degree_pow' h, degree_eq_nat_degree hp0,
β with_bot.coe_nsmul]; simp
theorem leading_coeff_mul_monic {p q : polynomial R} (hq : monic q) :
leading_coeff (p * q) = leading_coeff p :=
decidable.by_cases
(Ξ» H : leading_coeff p = 0, by rw [H, leading_coeff_eq_zero.1 H, zero_mul, leading_coeff_zero])
(Ξ» H : leading_coeff p β 0,
by rw [leading_coeff_mul', hq.leading_coeff, mul_one];
rwa [hq.leading_coeff, mul_one])
@[simp] theorem leading_coeff_mul_X_pow {p : polynomial R} {n : β} :
leading_coeff (p * X ^ n) = leading_coeff p :=
leading_coeff_mul_monic (monic_X_pow n)
@[simp] theorem leading_coeff_mul_X {p : polynomial R} :
leading_coeff (p * X) = leading_coeff p :=
leading_coeff_mul_monic monic_X
lemma nat_degree_mul_le {p q : polynomial R} : nat_degree (p * q) β€ nat_degree p + nat_degree q :=
begin
apply nat_degree_le_of_degree_le,
apply le_trans (degree_mul_le p q),
rw with_bot.coe_add,
refine add_le_add _ _; apply degree_le_nat_degree,
end
lemma subsingleton_of_monic_zero (h : monic (0 : polynomial R)) :
(β p q : polynomial R, p = q) β§ (β a b : R, a = b) :=
by rw [monic.def, leading_coeff_zero] at h;
exact β¨Ξ» p q, by rw [β mul_one p, β mul_one q, β C_1, β h, C_0, mul_zero, mul_zero],
Ξ» a b, by rw [β mul_one a, β mul_one b, β h, mul_zero, mul_zero]β©
lemma zero_le_degree_iff {p : polynomial R} : 0 β€ degree p β p β 0 :=
by rw [ne.def, β degree_eq_bot];
cases degree p; exact dec_trivial
lemma degree_nonneg_iff_ne_zero : 0 β€ degree p β p β 0 :=
β¨Ξ» h0p hp0, absurd h0p (by rw [hp0, degree_zero]; exact dec_trivial),
Ξ» hp0, le_of_not_gt (Ξ» h, by simp [gt, degree_eq_bot, *] at *)β©
lemma nat_degree_eq_zero_iff_degree_le_zero : p.nat_degree = 0 β p.degree β€ 0 :=
by rw [β nonpos_iff_eq_zero, nat_degree_le_iff_degree_le, with_bot.coe_zero]
theorem degree_le_iff_coeff_zero (f : polynomial R) (n : with_bot β) :
degree f β€ n β β m : β, n < m β coeff f m = 0 :=
β¨Ξ» (H : finset.sup (f.support) some β€ n) m (Hm : n < (m : with_bot β)), decidable.of_not_not $ Ξ» H4,
have H1 : m β f.support,
from Ξ» H2, not_lt_of_ge ((finset.sup_le_iff.1 H) m H2 : ((m : with_bot β) β€ n)) Hm,
H1 $ (finsupp.mem_support_to_fun f m).2 H4,
Ξ» H, finset.sup_le $ Ξ» b Hb, decidable.of_not_not $ Ξ» Hn,
(finsupp.mem_support_to_fun f b).1 Hb $ H b $ lt_of_not_ge Hnβ©
theorem degree_lt_iff_coeff_zero (f : polynomial R) (n : β) :
degree f < n β β m : β, n β€ m β coeff f m = 0 :=
begin
refine β¨Ξ» hf m hm, coeff_eq_zero_of_degree_lt (lt_of_lt_of_le hf (with_bot.coe_le_coe.2 hm)), _β©,
simp only [degree, finset.sup_lt_iff (with_bot.bot_lt_coe n), mem_support_iff,
with_bot.some_eq_coe, with_bot.coe_lt_coe, β @not_le β],
exact Ξ» h m, mt (h m),
end
lemma degree_lt_degree_mul_X (hp : p β 0) : p.degree < (p * X).degree :=
by haveI := nontrivial.of_polynomial_ne hp; exact
have leading_coeff p * leading_coeff X β 0, by simpa,
by erw [degree_mul' this, degree_eq_nat_degree hp,
degree_X, β with_bot.coe_one, β with_bot.coe_add, with_bot.coe_lt_coe];
exact nat.lt_succ_self _
lemma nat_degree_pos_iff_degree_pos :
0 < nat_degree p β 0 < degree p :=
lt_iff_lt_of_le_iff_le nat_degree_le_iff_degree_le
lemma eq_C_of_nat_degree_le_zero (h : nat_degree p β€ 0) : p = C (coeff p 0) :=
eq_C_of_degree_le_zero $ degree_le_of_nat_degree_le h
lemma eq_C_of_nat_degree_eq_zero (h : nat_degree p = 0) : p = C (coeff p 0) :=
eq_C_of_nat_degree_le_zero h.le
lemma ne_zero_of_coe_le_degree (hdeg : βn β€ p.degree) : p β 0 :=
by rw β degree_nonneg_iff_ne_zero; exact trans (by exact_mod_cast n.zero_le) hdeg
lemma le_nat_degree_of_coe_le_degree (hdeg : βn β€ p.degree) :
n β€ p.nat_degree :=
with_bot.coe_le_coe.mp ((degree_eq_nat_degree $ ne_zero_of_coe_le_degree hdeg) βΈ hdeg)
end semiring
section nonzero_semiring
variables [semiring R] [nontrivial R] {p q : polynomial R}
@[simp] lemma degree_X_pow (n : β) : degree ((X : polynomial R) ^ n) = n :=
by rw [X_pow_eq_monomial, degree_monomial _ (@one_ne_zero R _ _)]
@[simp] lemma nat_degree_X_pow (n : β) : nat_degree ((X : polynomial R) ^ n) = n :=
nat_degree_eq_of_degree_eq_some (degree_X_pow n)
theorem not_is_unit_X : Β¬ is_unit (X : polynomial R) :=
Ξ» β¨β¨_, g, hfg, hgfβ©, rflβ©, @zero_ne_one R _ _ $ by { rw [β coeff_one_zero, β hgf], simp }
@[simp] lemma degree_mul_X : degree (p * X) = degree p + 1 := by simp [degree_mul_monic monic_X]
@[simp] lemma degree_mul_X_pow : degree (p * X ^ n) = degree p + n :=
by simp [degree_mul_monic (monic_X_pow n)]
end nonzero_semiring
section ring
variables [ring R] {p q : polynomial R}
lemma degree_sub_le (p q : polynomial R) : degree (p - q) β€ max (degree p) (degree q) :=
by simpa only [sub_eq_add_neg, degree_neg q] using degree_add_le p (-q)
lemma degree_sub_lt (hd : degree p = degree q)
(hp0 : p β 0) (hlc : leading_coeff p = leading_coeff q) :
degree (p - q) < degree p :=
have hp : single (nat_degree p) (leading_coeff p) + p.erase (nat_degree p) = p :=
finsupp.single_add_erase _ _,
have hq : single (nat_degree q) (leading_coeff q) + q.erase (nat_degree q) = q :=
finsupp.single_add_erase _ _,
have hd' : nat_degree p = nat_degree q := by unfold nat_degree; rw hd,
have hq0 : q β 0 := mt degree_eq_bot.2 (hd βΈ mt degree_eq_bot.1 hp0),
calc degree (p - q) = degree (erase (nat_degree q) p + -erase (nat_degree q) q) :
by conv {to_lhs, rw [β hp, β hq, hlc, hd', add_sub_add_left_eq_sub, sub_eq_add_neg]}
... β€ max (degree (erase (nat_degree q) p)) (degree (erase (nat_degree q) q))
: degree_neg (erase (nat_degree q) q) βΈ degree_add_le _ _
... < degree p : max_lt_iff.2 β¨hd' βΈ degree_erase_lt hp0, hd.symm βΈ degree_erase_lt hq0β©
lemma nat_degree_X_sub_C_le {r : R} : (X - C r).nat_degree β€ 1 :=
nat_degree_le_iff_degree_le.2 $ le_trans (degree_sub_le _ _) $ max_le degree_X_le $
le_trans degree_C_le $ with_bot.coe_le_coe.2 zero_le_one
lemma degree_sum_fin_lt {n : β} (f : fin n β R) :
degree (β i : fin n, C (f i) * X ^ (i : β)) < n :=
begin
haveI : is_commutative (with_bot β) max := β¨max_commβ©,
haveI : is_associative (with_bot β) max := β¨max_assocβ©,
calc (β i, C (f i) * X ^ (i : β)).degree
β€ finset.univ.fold (β) β₯ (Ξ» i, (C (f i) * X ^ (i : β)).degree) : degree_sum_le _ _
... = finset.univ.fold max β₯ (Ξ» i, (C (f i) * X ^ (i : β)).degree) :
(@finset.fold_hom _ _ _ (β) _ _ _ β₯ finset.univ _ _ _ id (with_bot.sup_eq_max)).symm
... < n : (finset.fold_max_lt (n : with_bot β)).mpr β¨with_bot.bot_lt_some _, _β©,
rintros β¨i, hiβ© -,
calc (C (f β¨i, hiβ©) * X ^ i).degree
β€ (C _).degree + (X ^ i).degree : degree_mul_le _ _
... β€ 0 + i : add_le_add degree_C_le (degree_X_pow_le i)
... = i : zero_add _
... < n : with_bot.some_lt_some.mpr hi,
end
lemma degree_sub_eq_left_of_degree_lt (h : degree q < degree p) : degree (p - q) = degree p :=
by { rw β degree_neg q at h, rw [sub_eq_add_neg, degree_add_eq_left_of_degree_lt h] }
lemma degree_sub_eq_right_of_degree_lt (h : degree p < degree q) : degree (p - q) = degree q :=
by { rw β degree_neg q at h, rw [sub_eq_add_neg, degree_add_eq_right_of_degree_lt h, degree_neg] }
end ring
section nonzero_ring
variables [nontrivial R] [ring R]
@[simp] lemma degree_X_sub_C (a : R) : degree (X - C a) = 1 :=
have degree (C a) < degree (X : polynomial R),
from calc degree (C a) β€ 0 : degree_C_le
... < 1 : with_bot.some_lt_some.mpr zero_lt_one
... = degree X : degree_X.symm,
by rw [degree_sub_eq_left_of_degree_lt this, degree_X]
@[simp] lemma degree_X_add_C (a : R) : degree (X + C a) = 1 :=
have degree (C a) < degree (X : polynomial R),
from calc degree (C a) β€ 0 : degree_C_le
... < 1 : with_bot.some_lt_some.mpr zero_lt_one
... = degree X : degree_X.symm,
by rw [degree_add_eq_left_of_degree_lt this, degree_X]
@[simp] lemma nat_degree_X_sub_C (x : R) : (X - C x).nat_degree = 1 :=
nat_degree_eq_of_degree_eq_some $ degree_X_sub_C x
@[simp]
lemma next_coeff_X_sub_C (c : R) : next_coeff (X - C c) = - c :=
by simp [next_coeff_of_pos_nat_degree]
lemma degree_X_pow_sub_C {n : β} (hn : 0 < n) (a : R) :
degree ((X : polynomial R) ^ n - C a) = n :=
have degree (C a) < degree ((X : polynomial R) ^ n),
from calc degree (C a) β€ 0 : degree_C_le
... < degree ((X : polynomial R) ^ n) : by rwa [degree_X_pow];
exact with_bot.coe_lt_coe.2 hn,
by rw [degree_sub_eq_left_of_degree_lt this, degree_X_pow]
lemma X_pow_sub_C_ne_zero {n : β} (hn : 0 < n) (a : R) :
(X : polynomial R) ^ n - C a β 0 :=
mt degree_eq_bot.2 (show degree ((X : polynomial R) ^ n - C a) β β₯,
by rw degree_X_pow_sub_C hn a; exact dec_trivial)
theorem X_sub_C_ne_zero (r : R) : X - C r β 0 :=
pow_one (X : polynomial R) βΈ X_pow_sub_C_ne_zero zero_lt_one r
theorem zero_nmem_multiset_map_X_sub_C {Ξ± : Type*} (m : multiset Ξ±) (f : Ξ± β R) :
(0 : polynomial R) β m.map (Ξ» a, X - C (f a)) :=
Ξ» mem, let β¨a, _, haβ© := multiset.mem_map.mp mem in X_sub_C_ne_zero _ ha
lemma nat_degree_X_pow_sub_C {n : β} {r : R} :
(X ^ n - C r).nat_degree = n :=
begin
by_cases hn : n = 0,
{ rw [hn, pow_zero, βC_1, βring_hom.map_sub, nat_degree_C] },
{ exact nat_degree_eq_of_degree_eq_some (degree_X_pow_sub_C (pos_iff_ne_zero.mpr hn) r) },
end
end nonzero_ring
section no_zero_divisors
variables [semiring R] [no_zero_divisors R] {p q : polynomial R}
@[simp] lemma degree_mul : degree (p * q) = degree p + degree q :=
if hp0 : p = 0 then by simp only [hp0, degree_zero, zero_mul, with_bot.bot_add]
else if hq0 : q = 0 then by simp only [hq0, degree_zero, mul_zero, with_bot.add_bot]
else degree_mul' $ mul_ne_zero (mt leading_coeff_eq_zero.1 hp0)
(mt leading_coeff_eq_zero.1 hq0)
@[simp] lemma degree_pow [nontrivial R] (p : polynomial R) (n : β) :
degree (p ^ n) = n β’β (degree p) :=
by induction n; [simp only [pow_zero, degree_one, zero_nsmul],
simp only [*, pow_succ, succ_nsmul, degree_mul]]
@[simp] lemma leading_coeff_mul (p q : polynomial R) : leading_coeff (p * q) =
leading_coeff p * leading_coeff q :=
begin
by_cases hp : p = 0,
{ simp only [hp, zero_mul, leading_coeff_zero] },
{ by_cases hq : q = 0,
{ simp only [hq, mul_zero, leading_coeff_zero] },
{ rw [leading_coeff_mul'],
exact mul_ne_zero (mt leading_coeff_eq_zero.1 hp) (mt leading_coeff_eq_zero.1 hq) } }
end
@[simp] lemma leading_coeff_X_add_C [nontrivial R] (a b : R) (ha : a β 0):
leading_coeff (C a * X + C b) = a :=
begin
rw [add_comm, leading_coeff_add_of_degree_lt],
{ simp },
{ simpa [degree_C ha] using lt_of_le_of_lt degree_C_le (with_bot.coe_lt_coe.2 zero_lt_one)}
end
/-- `polynomial.leading_coeff` bundled as a `monoid_hom` when `R` has `no_zero_divisors`, and thus
`leading_coeff` is multiplicative -/
def leading_coeff_hom : polynomial R β* R :=
{ to_fun := leading_coeff,
map_one' := by simp,
map_mul' := leading_coeff_mul }
@[simp] lemma leading_coeff_hom_apply (p : polynomial R) :
leading_coeff_hom p = leading_coeff p := rfl
@[simp] lemma leading_coeff_pow (p : polynomial R) (n : β) :
leading_coeff (p ^ n) = leading_coeff p ^ n :=
leading_coeff_hom.map_pow p n
end no_zero_divisors
end polynomial
|
33627aee752faf525ab119cb957bc77499d49f60 | 8b9f17008684d796c8022dab552e42f0cb6fb347 | /tests/lean/protected_consts.lean | b5e15624cedbda0882da7eb874f426dbaea05f66 | [
"Apache-2.0"
] | permissive | chubbymaggie/lean | 0d06ae25f9dd396306fb02190e89422ea94afd7b | d2c7b5c31928c98f545b16420d37842c43b4ae9a | refs/heads/master | 1,611,313,622,901 | 1,430,266,839,000 | 1,430,267,083,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 596 | lean | namespace foo
protected axiom A : Prop
axiom B : Prop
protected constant a : A
constant b : B
protected axioms (Aβ Aβ : Prop)
protected constants (aβ aβ : A)
axioms (Bβ Bβ : Prop)
constants (bβ bβ : B)
end foo
open foo
check foo.A
check A -- error
check foo.a
check a -- error
check foo.Aβ
check foo.Aβ
check Aβ -- error
check Aβ -- error
check foo.aβ
check foo.aβ
check aβ -- error
check aβ -- error
check foo.B
check B
check foo.b
check b
check foo.bβ
check foo.bβ
check bβ
check bβ
check foo.Bβ
check foo.Bβ
check Bβ
check Bβ
|
8204ad7317dbc64a3829fae5a1225690f75b0644 | 3dd1b66af77106badae6edb1c4dea91a146ead30 | /tests/lean/run/n5.lean | ce8dc27bd8c6fb1a8fa18419f1d4efc09690377a | [
"Apache-2.0"
] | permissive | silky/lean | 79c20c15c93feef47bb659a2cc139b26f3614642 | df8b88dca2f8da1a422cb618cd476ef5be730546 | refs/heads/master | 1,610,737,587,697 | 1,406,574,534,000 | 1,406,574,534,000 | 22,362,176 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 422 | lean | variable N : Type.{1}
variable f : N β N β N β N
variable g : N β N β N
variable h : N β N β N β N
variable s : N β N β N β N β N
precedence `*`:75
precedence `|`:75
notation a * b:prev | c:prev := f a b c
notation a * b := g a b
notation a * b * c:prev := h a b c
notation a * b | c * d:prev := s a b c d
variables a b c d e : N
check a * b
check a * b | d
check a * b * c
check a * b | d * e
|
f65d68456b508ed145d7fdc64a6330637cec1b1d | 4727251e0cd73359b15b664c3170e5d754078599 | /src/tactic/induction.lean | 0c3e6d5b1296115811b195f88d2cdea12b2709cf | [
"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 | 55,028 | lean | /-
Copyright (c) 2020 Jannis Limperg. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jannis Limperg
-/
import tactic.clear
import tactic.dependencies
import tactic.fresh_names
import tactic.generalizes
import tactic.has_variable_names
import tactic.unify_equations
/-!
# A better tactic for induction and case analysis
This module defines the tactics `tactic.interactive.induction'` and
`tactic.interactive.cases'`, which are variations on Lean's builtin `induction`
and `cases`. The primed variants feature various improvements over the builtin
tactics; in particular, they generate more human-friendly names and `induction'`
deals much better with indexed inductive types. See the tactics' documentation
for more details. We also provide corresponding non-interactive induction
tactics `tactic.eliminate_hyp` and `tactic.eliminate_expr`.
The design and implementation of these tactics is described in a
[draft paper](https://limperg.de/paper/cpp2021-induction/).
-/
open expr native
open tactic.interactive (case_tag.from_tag_hyps)
namespace tactic
namespace eliminate
/-!
## Tracing
We set up two tracing functions to be used by `eliminate_hyp` and its supporting
tactics. Their output is enabled by setting `trace.eliminate_hyp` to `true`.
-/
declare_trace eliminate_hyp
/--
`trace_eliminate_hyp msg` traces `msg` if the option `trace.eliminate_hyp` is
`true`.
-/
meta def trace_eliminate_hyp {Ξ±} [has_to_format Ξ±] (msg : thunk Ξ±) : tactic unit :=
when_tracing `eliminate_hyp $ trace $ to_fmt "eliminate_hyp: " ++ to_fmt (msg ())
/--
`trace_state_eliminate_hyp msg` traces `msg` followed by the tactic state if the
option `trace.eliminate_hyp` is `true`.
-/
meta def trace_state_eliminate_hyp {Ξ±} [has_to_format Ξ±] (msg : thunk Ξ±) :
tactic unit := do
state β read,
trace_eliminate_hyp $ format.join
[to_fmt (msg ()), "\n-----\n", to_fmt state, "\n-----"]
/-!
## Information Gathering
We define data structures for information relevant to the induction, and
functions to collect this information for a specific goal.
-/
/--
Information about a constructor argument. E.g. given the declaration
```
induction β : Type
| zero : β
| suc (n : β) : β
```
the `zero` constructor has no arguments and the `suc` constructor has one
argument, `n`.
We record the following information:
- `aname`: the argument's name. If the argument was not explicitly named in the
declaration, the elaborator generates a name for it.
- `type` : the argument's type.
- `dependent`: whether the argument is dependent, i.e. whether it occurs in the
remainder of the constructor type.
- `index_occurrences`: the index arguments of the constructor's return type
in which this argument occurs. If the constructor return type is
`I iβ ... iβ` and the argument under consideration is `a`, and `a` occurs in
`iβ` and `iβ`, then the `index_occurrences` are `1, 2`. As an additional
requirement, for `iβ±Ό` to be considered an index occurrences,
the type of `iβ±Ό` must match that of `a` according to
`index_occurrence_type_match`.
- `recursive_leading_pis`: `none` if this constructor is not recursive.
Otherwise, the argument has type `Ξ (xβ : Tβ) ... (xβ : Tβ), I ...`
where `I` is the inductive type to which this constructor belongs. In this
case, `recursive_leading_pis` is `some n` with `n` the number of leading Ξ
binders in the argument's type.
-/
@[derive has_reflect]
meta structure constructor_argument_info :=
(aname : name)
(type : expr)
(dependent : bool)
(index_occurrences : list β)
(recursive_leading_pis : option β)
namespace constructor_argument_info
/--
`is_recursive c` is true iff the constructor argument described by `c` is
recursive.
-/
meta def is_recursive (c : constructor_argument_info) :=
c.recursive_leading_pis.is_some
end constructor_argument_info
/--
Information about a constructor. Contains:
- `cname`: the constructor's name.
- `non_param_args`: information about the arguments of the constructor,
excluding the arguments induced by the parameters of the inductive type.
- `num_non_param_args`: the length of `non_param_args`.
- `rec_args`: the subset of `non_param_args` which are recursive constructor
arguments.
- `num_rec_args`: the length of `rec_args`.
For example, take the constructor
```
list.cons : β {Ξ±} (x : Ξ±) (xs : list Ξ±), list Ξ±
```
`Ξ±` is a parameter of `list`, so `non_param_args` contains information about `x`
and `xs`. `rec_args` contains information about `xs`.
-/
@[derive has_reflect]
meta structure constructor_info :=
(cname : name)
(non_param_args : list constructor_argument_info)
(num_non_param_args : β)
(rec_args : list constructor_argument_info)
(num_rec_args : β)
/--
When we construct the goal for the minor premise of a given constructor, this is
the number of hypotheses we must name.
-/
meta def constructor_info.num_nameable_hypotheses (c : constructor_info) : β :=
c.num_non_param_args + c.num_rec_args
/--
Information about an inductive type. Contains:
- `iname`: the type's name.
- `constructors`: information about the type's constructors.
- `num_constructors`: the length of `constructors`.
- `type`: the type's type.
- `num_param`: the type's number of parameters.
- `num_indices`: the type's number of indices.
-/
@[derive has_reflect]
meta structure inductive_info :=
(iname : name)
(constructors : list constructor_info)
(num_constructors : β)
(type : expr)
(num_params : β)
(num_indices : β)
/--
Information about a major premise (i.e. the hypothesis on which we are
performing induction). Contains:
- `mpname`: the major premise's name.
- `mpexpr`: the major premise itself.
- `type`: the type of `mpexpr`.
- `args`: the arguments of the major premise. The major premise has type
`I xβ ... xβ`, where `I` is an inductive type. `args` is the map
`[0 β xβ, ..., n β xβ]`.
-/
meta structure major_premise_info :=
(mpname : name)
(mpexpr : expr)
(type : expr)
(args : rb_map β expr)
/--
`index_occurrence_type_match t s` is true iff `t` and `s` are definitionally
equal.
-/
-- We could extend this check to be more permissive. E.g. if a constructor
-- argument has type `list Ξ±` and the index has type `list Ξ²`, we may want to
-- consider these types sufficiently similar to inherit the name. Same (but even
-- more obvious) with `vec Ξ± n` and `vec Ξ± (n + 1)`.
meta def index_occurrence_type_match (t s : expr) : tactic bool :=
succeeds $ is_def_eq t s
/--
From the return type of a constructor `C` of an inductive type `I`, determine
the index occurrences of the constructor arguments of `C`.
Input:
- `num_params:` the number of parameters of `I`.
- `ret_type`: the return type of `C`. `e` must be of the form `I xβ ... xβ`.
Output: A map associating each local constant `c` that appears in any of the `xα΅’`
with the set of indexes `j` such that `c` appears in `xβ±Ό` and `xβ±Ό`'s type
matches that of `c` according to `tactic.index_occurrence_type_match`.
-/
meta def get_index_occurrences (num_params : β) (ret_type : expr) :
tactic (rb_lmap expr β) := do
ret_args β get_app_args_whnf ret_type,
ret_args.mfoldl_with_index
(Ξ» i occ_map ret_arg, do
if i < num_params
then pure occ_map
else do
let ret_arg_consts := ret_arg.list_local_consts',
ret_arg_consts.mfold occ_map $ Ξ» c occ_map, do
ret_arg_type β infer_type ret_arg,
eq β index_occurrence_type_match c.local_type ret_arg_type,
pure $ if eq then occ_map.insert c i else occ_map)
mk_rb_map
/--
`match_recursive_constructor_arg I T`, given `I` the name of an inductive type
and `T` the type of an argument of a constructor of `I`, returns `none` if the
argument is non-recursive (i.e. `I` does not appear in `T`). If the argument is
recursive, `T` is of the form `Ξ (xβ : Tβ) ... (xβ : Tβ), I ...`, in which case
`match_recursive_constructor_arg` returns `some n`. Matching is performed up to
WHNF with semireducible transparency.
-/
meta def match_recursive_constructor_arg (I : name) (T : expr) :
tactic (option β) := do
(pis, base) β open_pis_whnf T,
base β get_app_fn_whnf base,
pure $
match base with
| (const c _) := if c = I then some pis.length else none
| _ := none
end
/--
Get information about the arguments of a constructor `C` of an inductive type
`I`.
Input:
- `inductive_name`: the name of `I`.
- `num_params`: the number of parameters of `I`.
- `T`: the type of `C`.
Output: a `constructor_argument_info` structure for each argument of `C`.
-/
meta def get_constructor_argument_info (inductive_name : name)
(num_params : β) (T : expr) :
tactic (list constructor_argument_info) := do
β¨args, retβ© β open_pis_whnf_dep T,
index_occs β get_index_occurrences num_params ret,
args.mmap $ Ξ» β¨c, depβ©, do
let occs := rb_set.of_list $ index_occs.find c,
let type := c.local_type,
recursive_leading_pis β match_recursive_constructor_arg inductive_name type,
pure β¨c.local_pp_name, type, dep, occs.to_list, recursive_leading_pisβ©
/--
Get information about a constructor `C` of an inductive type `I`.
Input:
- `iname`: the name of `I`.
- `num_params`: the number of parameters of `I`.
- `c` : the name of `C`.
Output:
A `constructor_info` structure for `C`.
-/
meta def get_constructor_info (iname : name) (num_params : β) (c : name) :
tactic constructor_info := do
env β get_env,
when (Β¬ env.is_constructor c) $ fail! "Expected {c} to be a constructor.",
decl β env.get c,
args β get_constructor_argument_info iname num_params decl.type,
let non_param_args := args.drop num_params,
let rec_args := non_param_args.filter $ Ξ» ainfo, ainfo.is_recursive,
pure
{ cname := decl.to_name,
non_param_args := non_param_args,
num_non_param_args := non_param_args.length,
rec_args := rec_args,
num_rec_args := rec_args.length }
/--
Get information about an inductive type `I`, given `I`'s name.
-/
meta def get_inductive_info (I : name) : tactic inductive_info := do
env β get_env,
when (Β¬ env.is_inductive I) $ fail! "Expected {I} to be an inductive type.",
decl β env.get I,
let type := decl.type,
let num_params := env.inductive_num_params I,
let num_indices := env.inductive_num_indices I,
let constructor_names := env.constructors_of I,
constructors β constructor_names.mmap
(get_constructor_info I num_params),
pure
{ iname := I,
constructors := constructors,
num_constructors := constructors.length,
type := type,
num_params := num_params,
num_indices := num_indices }
/--
Get information about a major premise. The given `expr` must be a local
hypothesis.
-/
meta def get_major_premise_info (major_premise : expr) :
tactic major_premise_info := do
type β infer_type major_premise,
β¨f, argsβ© β get_app_fn_args_whnf type,
pure
{ mpname := major_premise.local_pp_name,
mpexpr := major_premise,
type := type,
args := args.to_rb_map }
/-!
## Constructor Argument Naming
We define the algorithm for naming constructor arguments (which is a remarkably
big part of the tactic).
-/
/--
Information used when naming a constructor argument.
-/
meta structure constructor_argument_naming_info :=
(mpinfo : major_premise_info)
(iinfo : inductive_info)
(cinfo : constructor_info)
(ainfo : constructor_argument_info)
/--
A constructor argument naming rule takes a `constructor_argument_naming_info`
structure and returns a list of suitable names for the argument. If the rule is
not applicable to the given constructor argument, the returned list is empty.
-/
@[reducible] meta def constructor_argument_naming_rule : Type :=
constructor_argument_naming_info β tactic (list name)
/--
Naming rule for recursive constructor arguments.
-/
meta def constructor_argument_naming_rule_rec : constructor_argument_naming_rule :=
Ξ» i, pure $ if i.ainfo.is_recursive then [i.mpinfo.mpname] else []
/--
Naming rule for constructor arguments associated with an index.
-/
meta def constructor_argument_naming_rule_index : constructor_argument_naming_rule :=
Ξ» i,
let index_occs := i.ainfo.index_occurrences in
let major_premise_args := i.mpinfo.args in
let get_major_premise_arg_local_names : β β option (name Γ name) := Ξ» i, do
{ arg β major_premise_args.find i,
(uname, ppname, _) β arg.match_local_const,
pure (uname, ppname) } in
let local_index_instantiations :=
(index_occs.map get_major_premise_arg_local_names).all_some in
/-
Right now, this rule only triggers if the major premise arg is exactly a
local const. We could consider a more permissive rule where the major premise
arg can be an arbitrary term as long as that term *contains* only a single local
const.
-/
pure $
match local_index_instantiations with
| none := []
| some [] := []
| some ((uname, ppname) :: is) :=
if is.all (Ξ» β¨uname', _β©, uname' = uname)
then [ppname]
else []
end
/--
Naming rule for constructor arguments which are named in the constructor
declaration.
-/
meta def constructor_argument_naming_rule_named : constructor_argument_naming_rule :=
Ξ» i,
let arg_name := i.ainfo.aname in
let arg_dep := i.ainfo.dependent in
pure $
if ! arg_dep && arg_name.is_likely_generated_binder_name
then []
else [arg_name]
/--
Naming rule for constructor arguments whose type is associated with a list of
typical variable names. See `tactic.typical_variable_names`.
-/
meta def constructor_argument_naming_rule_type : constructor_argument_naming_rule :=
Ξ» i, typical_variable_names i.ainfo.type <|> pure []
/--
Naming rule for constructor arguments whose type is in `Prop`.
-/
meta def constructor_argument_naming_rule_prop : constructor_argument_naming_rule :=
Ξ» i, do
(sort level.zero) β infer_type i.ainfo.type | pure [],
pure [`h]
/--
Fallback constructor argument naming rule. This rule never fails.
-/
meta def constructor_argument_naming_rule_fallback : constructor_argument_naming_rule :=
Ξ» _, pure [`x]
/--
`apply_constructor_argument_naming_rules info rules` applies the constructor
argument naming rules in `rules` to the constructor argument given by `info`.
Returns the result of the first applicable rule. Fails if no rule is applicable.
-/
meta def apply_constructor_argument_naming_rules
(info : constructor_argument_naming_info)
(rules : list constructor_argument_naming_rule) : tactic (list name) := do
names β try_core $ rules.mfirst (Ξ» r, do
names β r info,
match names with
| [] := failed
| _ := pure names
end),
match names with
| none := fail
"apply_constructor_argument_naming_rules: no applicable naming rule"
| (some names) := pure names
end
/--
Get possible names for a constructor argument. This tactic applies all the
previously defined rules in order. It cannot fail and always returns a nonempty
list.
-/
meta def constructor_argument_names (info : constructor_argument_naming_info) :
tactic (list name) :=
apply_constructor_argument_naming_rules info
[ constructor_argument_naming_rule_rec
, constructor_argument_naming_rule_index
, constructor_argument_naming_rule_named
, constructor_argument_naming_rule_type
, constructor_argument_naming_rule_prop
, constructor_argument_naming_rule_fallback ]
/--
`intron_fresh n` introduces `n` hypotheses with names generated by
`tactic.mk_fresh_name`.
-/
meta def intron_fresh (n : β) : tactic (list expr) :=
iterate_exactly n (mk_fresh_name >>= intro)
/--
Introduce the new hypotheses generated by the minor premise for a given
constructor. The new hypotheses are given fresh (unique, non-human-friendly)
names. They are later renamed by `constructor_renames`. We delay the generation
of the human-friendly names because when `constructor_renames` is called, more
names may have become unused.
Input:
- `generate_induction_hyps`: whether we generate induction hypotheses (i.e.
whether `eliminate_hyp` is in `induction` or `cases` mode).
- `cinfo`: information about the constructor.
Output:
- For each constructor argument: (1) the pretty name of the newly introduced
hypothesis corresponding to the argument; (2) the argument's
`constructor_argument_info`.
- For each newly introduced induction hypothesis: (1) its pretty name; (2) the
pretty name of the hypothesis corresponding to the constructor argument from
which this induction hypothesis was derived; (3) that constructor argument's
`constructor_argument_info`.
-/
meta def constructor_intros (generate_induction_hyps : bool)
(cinfo : constructor_info) :
tactic (list (name Γ constructor_argument_info) Γ
list (name Γ name Γ constructor_argument_info)) := do
let args := cinfo.non_param_args,
arg_hyps β intron_fresh cinfo.num_non_param_args,
let args := (arg_hyps.map expr.local_pp_name).zip args,
tt β pure generate_induction_hyps | pure (args, []),
let rec_args := args.filter $ Ξ» x, x.2.is_recursive,
ih_hyps β intron_fresh cinfo.num_rec_args,
let ihs := (ih_hyps.map expr.local_pp_name).zip rec_args,
pure (args, ihs)
/--
`ih_name arg_name` is the name `ih_<arg_name>`.
-/
meta def ih_name (arg_name : name) : name :=
mk_simple_name ("ih_" ++ arg_name.to_string)
/--
Representation of a pattern in the `with n ...` syntax supported by
`induction'` and `cases'`. A `with_pattern` can be:
- `with_pattern.auto` (`with _` or no `with` clause): use the name generated by the tactic.
- `with_pattern.clear` (`with -`): clear this hypothesis and any hypotheses depending on it.
- `with_pattern.exact n` (`with n`): use the name `n` for this hypothesis.
-/
@[derive has_reflect]
meta inductive with_pattern
| auto
| clear
| exact (n : name)
namespace with_pattern
open lean (parser)
open lean.parser
/-- Parser for a `with_pattern`. -/
protected meta def parser : lean.parser with_pattern :=
(tk "-" *> pure with_pattern.clear) <|>
(tk "_" *> pure with_pattern.auto) <|>
(with_pattern.exact <$> ident)
/-- Parser for a `with` clause. -/
meta def clause_parser : lean.parser (list with_pattern) :=
(tk "with" *> many with_pattern.parser) <|> pure []
/--
`to_name_spec auto_candidates p` returns a description of how the hypothesis to
which the `with_pattern` `p` applies should be named. If this function returns
`none`, the hypothesis should be cleared. If it returns `some (inl n)`, it
should receive exactly the name `n`, even if this shadows other hypotheses. If
it returns `some (inr ns)`, it should receive the first unused name from `ns`.
If `p = auto`, the `auto_candidates` tactic is run to determine candidate names
for the hypothesis (from which the first fresh one is later chosen).
`auto_candidates` must return a nonempty list.
-/
meta def to_name_spec (auto_candidates : tactic (list name)) :
with_pattern β tactic (option (name β list name))
| auto := (some β sum.inr) <$> auto_candidates
| clear := pure none
| (exact n) := pure $ some $ sum.inl n
end with_pattern
/--
If `h` refers to a hypothesis, `clear_dependent_if_exists h` clears `h` and any
hypotheses which depend on it. Otherwise, the tactic does nothing.
-/
meta def clear_dependent_if_exists (h : name) : tactic unit := do
(some h) β try_core $ get_local h | pure (),
clear' tt [h]
/--
Rename the new hypotheses in the goal for a minor premise.
Input:
- `generate_induction_hyps`: whether we generate induction hypotheses (i.e.
whether `eliminate_hyp` is in `induction` or `cases` mode).
- `mpinfo`: information about the major premise.
- `iinfo`: information about the inductive type.
- `cinfo`: information about the constructor whose minor premise we are
processing.
- `with_patterns`: a list of `with` patterns given by the user. These are used
to name constructor arguments and induction hypotheses. If the list does not
contain enough patterns for all introduced hypotheses, the remaining ones are
treated as if the user had given `with_pattern.auto` (`_`).
- `args` and `ihs`: the output of `constructor_intros`.
Output:
- The newly introduced hypotheses corresponding to constructor arguments.
- The newly introduced induction hypotheses.
-/
meta def constructor_renames (generate_induction_hyps : bool)
(mpinfo : major_premise_info) (iinfo : inductive_info)
(cinfo : constructor_info) (with_patterns : list with_pattern)
(args : list (name Γ constructor_argument_info))
(ihs : list (name Γ name Γ constructor_argument_info)) :
tactic (list expr Γ list expr) := do
-- Rename constructor arguments
let arg_pp_name_set := name_set.of_list $ args.map prod.fst,
let iname := iinfo.iname,
let β¨args, with_patternsβ© :=
args.mapβ_left' (Ξ» arg p, (arg, p.get_or_else with_pattern.auto))
with_patterns,
arg_renames β args.mmap_filter $ Ξ» β¨β¨old_ppname, ainfoβ©, with_patβ©, do
{ (some new) β with_pat.to_name_spec
(constructor_argument_names β¨mpinfo, iinfo, cinfo, ainfoβ©)
| clear_dependent_if_exists old_ppname >> pure none,
-- Some of the arg hyps may have been cleared by earlier simplification
-- steps, so get_local may fail.
(some old) β try_core $ get_local old_ppname | pure none,
pure $ some (old.local_uniq_name, new) },
let arg_renames := rb_map.of_list arg_renames,
arg_hyp_map β rename_fresh arg_renames mk_name_set,
let new_arg_hyps := arg_hyp_map.filter_map $ Ξ» β¨old, newβ©,
if arg_pp_name_set.contains old.local_pp_name then some new else none,
let arg_hyp_map : name_map expr :=
rb_map.of_list $ arg_hyp_map.map $ Ξ» β¨old, newβ©, (old.local_pp_name, new),
-- Rename induction hypotheses (if we generated them)
tt β pure generate_induction_hyps | pure (new_arg_hyps, []),
let ih_pp_name_set := name_set.of_list $ ihs.map prod.fst,
let ihs :=
ihs.mapβ_left (Ξ» ih p, (ih, p.get_or_else with_pattern.auto)) with_patterns,
let single_ih := ihs.length = 1,
ih_renames β ihs.mmap_filter $ Ξ» β¨β¨ih_hyp_ppname, arg_hyp_ppname, _β©, with_patβ©, do
{ some arg_hyp β pure $ arg_hyp_map.find arg_hyp_ppname
| fail! "internal error in constructor_renames: {arg_hyp_ppname} not found in arg_hyp_map",
(some new) β with_pat.to_name_spec (pure $
if single_ih
then [`ih, ih_name arg_hyp.local_pp_name]
-- If we have only a single IH which hasn't been named explicitly in a
-- `with` clause, the preferred name is "ih". If that is taken, we fall
-- back to the name the IH would ordinarily receive.
else [ih_name arg_hyp.local_pp_name])
| clear_dependent_if_exists ih_hyp_ppname >> pure none,
(some ih_hyp) β try_core $ get_local ih_hyp_ppname | pure none,
pure $ some (ih_hyp.local_uniq_name, new) },
ih_hyp_map β rename_fresh (rb_map.of_list ih_renames) mk_name_set,
let new_ih_hyps := ih_hyp_map.filter_map $ Ξ» β¨old, newβ©,
if ih_pp_name_set.contains old.local_pp_name then some new else none,
pure (new_arg_hyps, new_ih_hyps)
/-!
## Generalisation
`induction'` can generalise the goal before performing an induction, which gives
us a more general induction hypothesis. We call this 'auto-generalisation'.
-/
/--
A value of `generalization_mode` describes the behaviour of the
auto-generalisation functionality:
- `generalize_all_except hs` means that the `hs` remain fixed and all other
hypotheses are generalised. However, there are three exceptions:
* Hypotheses depending on any `h` in `hs` also remain fixed. If we were to
generalise them, we would have to generalise `h` as well.
* Hypotheses which do not occur in the target and which do not mention the
major premise or its dependencies are never generalised. Generalising them
would not lead to a more general induction hypothesis.
* Local definitions (hypotheses of the form `h : T := t`) and their
dependencies are not generalised. This is due to limitations of the
implementation; local definitions could in principle be generalised.
- `generalize_only hs` means that only the `hs` are generalised. Exception:
hypotheses which depend on the major premise are generalised even if they do
not appear in `hs`.
-/
@[derive has_reflect]
inductive generalization_mode
| generalize_all_except (hs : list name) : generalization_mode
| generalize_only (hs : list name) : generalization_mode
instance : inhabited generalization_mode :=
β¨ generalization_mode.generalize_all_except []β©
namespace generalization_mode
/--
Given the major premise and a generalization_mode, this function returns the
unique names of the hypotheses that should be generalized. See
`generalization_mode` for what these are.
-/
meta def to_generalize (major_premise : expr) :
generalization_mode β tactic name_set
| (generalize_only ns) := do
major_premise_rev_deps β reverse_dependencies_of_hyps [major_premise],
let major_premise_rev_deps :=
name_set.of_list $ major_premise_rev_deps.map local_uniq_name,
ns β ns.mmap (functor.map local_uniq_name β get_local),
pure $ major_premise_rev_deps.insert_list ns
| (generalize_all_except fixed) := do
fixed β fixed.mmap get_local,
tgt β target,
let tgt_dependencies := tgt.list_local_const_unique_names,
major_premise_type β infer_type major_premise,
major_premise_dependencies β dependency_name_set_of_hyp_inclusive major_premise,
defs β local_defs,
fixed_dependencies β
(major_premise :: defs ++ fixed).mmap dependency_name_set_of_hyp_inclusive,
let fixed_dependencies := fixed_dependencies.foldl name_set.union mk_name_set,
ctx β local_context,
to_revert β ctx.mmap_filter $ Ξ» h, do
{ h_depends_on_major_premise_deps β
-- TODO `hyp_depends_on_local_name_set` is somewhat expensive
hyp_depends_on_local_name_set h major_premise_dependencies,
let h_name := h.local_uniq_name,
let rev :=
Β¬ fixed_dependencies.contains h_name β§
(h_depends_on_major_premise_deps β¨ tgt_dependencies.contains h_name),
/-
I think `h_depends_on_major_premise_deps` is an overapproximation. What we
actually want is any hyp that depends either on the major_premise or on one
of the major_premise's index args. (But the overapproximation seems to work
okay in practice as well.)
-/
pure $ if rev then some h_name else none },
pure $ name_set.of_list to_revert
end generalization_mode
/--
Generalize hypotheses for the given major premise and generalization mode. See
`generalization_mode` and `to_generalize`.
-/
meta def generalize_hyps (major_premise : expr) (gm : generalization_mode) :
tactic β := do
to_revert β gm.to_generalize major_premise,
β¨n, _β© β unfreezing (revert_name_set to_revert),
pure n
/-!
## Complex Index Generalisation
A *complex* expression is any expression that is not merely a local constant.
When such a complex expression appears as an argument of the major premise, and
when that argument is an index of the inductive type, we must generalise the
complex expression. E.g. when we operate on the major premise `fin (2 + n)`
(assuming that `fin` is encoded as an inductive type), the `2 + n` is a complex
index argument. To generalise it, we replace it with a new hypothesis
`index : β` and add an equation `induction_eq : index = 2 + n`.
-/
/--
Generalise the complex index arguments.
Input:
- `major premise`: the major premise.
- `num_params`: the number of parameters of the inductive type.
- `generate_induction_hyps`: whether we generate induction hypotheses (i.e.
whether `eliminate_hyp` is in `induction` or `cases` mode).
Output:
- The new major premise. This procedure may change the major premise's type
signature, so the old major premise hypothesis is invalidated.
- The number of index placeholder hypotheses we introduced.
- The index placeholder hypotheses we introduced.
- The number of hypotheses which were reverted because they contain complex
indices.
-/
/-
TODO The following function currently replaces complex index arguments
everywhere in the goal, not only in the major premise. Such replacements are
sometimes necessary to make sure that the goal remains type-correct. However,
the replacements can also have the opposite effect, yielding unprovable
subgoals. The test suite contains one such case. There is probably a middle
ground between 'replace everywhere' and 'replace only in the major premise', but
I don't know what exactly this middle ground is. See also the discussion at
https://github.com/leanprover-community/mathlib/pull/5027#discussion_r538902424
-/
meta def generalize_complex_index_args (major_premise : expr) (num_params : β)
(generate_induction_hyps : bool) : tactic (expr Γ β Γ list name Γ β) :=
focus1 $ do
major_premise_type β infer_type major_premise,
(major_premise_head, major_premise_args) β
get_app_fn_args_whnf major_premise_type,
let β¨major_premise_param_args, major_premise_index_argsβ© :=
major_premise_args.split_at num_params,
-- TODO Add equations only for complex index args (not all index args).
-- This shouldn't matter semantically, but we'd get simpler terms.
let js := major_premise_index_args,
ctx β local_context,
tgt β target,
major_premise_deps β dependency_name_set_of_hyp_inclusive major_premise,
-- Revert the hypotheses which depend on the index args or the major_premise.
-- We exclude dependencies of the major premise because we can't replace their
-- index occurrences anyway when we apply the recursor.
relevant_ctx β ctx.mfilter $ Ξ» h, do
{ let dep_of_major_premise := major_premise_deps.contains h.local_uniq_name,
dep_on_major_premise β hyp_depends_on_locals h [major_premise],
H β infer_type h,
dep_of_index β js.many $ Ξ» j, kdepends_on H j,
-- TODO We need a variant of `kdepends_on` that takes local defs into account.
pure $
(dep_on_major_premise β§ h β major_premise) β¨
(dep_of_index β§ Β¬ dep_of_major_premise) },
β¨relevant_ctx_size, relevant_ctxβ© β unfreezing $ do
{ r β revert_lst' relevant_ctx,
revert major_premise,
pure r },
-- Create the local constants that will replace the index args. We have to be
-- careful to get the right types.
let go : expr β list expr β tactic (list expr) :=
Ξ» j ks, do
{ J β infer_type j,
k β mk_local' `index binder_info.default J,
ks β ks.mmap $ Ξ» k', kreplace k' j k,
pure $ k :: ks },
ks β js.mfoldr go [],
let js_ks := js.zip ks,
-- Replace the index args in the relevant context.
new_ctx β relevant_ctx.mmap $ Ξ» h, js_ks.mfoldr (Ξ» β¨j, kβ© h, kreplace h j k) h,
-- Replace the index args in the major premise.
let new_major_premise_type :=
major_premise_head.mk_app (major_premise_param_args ++ ks),
let new_major_premise :=
local_const major_premise.local_uniq_name major_premise.local_pp_name
major_premise.binding_info new_major_premise_type,
-- Replace the index args in the target.
new_tgt β js_ks.mfoldr (Ξ» β¨j, kβ© tgt, kreplace tgt j k) tgt,
let new_tgt := new_tgt.pis (new_major_premise :: new_ctx),
-- Generate the index equations and their proofs.
let eq_name := if generate_induction_hyps then `induction_eq else `cases_eq,
let step2_input := js_ks.map $ Ξ» β¨j, kβ©, (eq_name, j, k),
eqs_and_proofs β generalizes.step2 reducible step2_input,
let eqs := eqs_and_proofs.map prod.fst,
let eq_proofs := eqs_and_proofs.map prod.snd,
-- Assert the generalized goal and derive the current goal from it.
generalizes.step3 new_tgt js ks eqs eq_proofs,
-- Introduce the index variables and major premise. The index equations
-- and the relevant context remain reverted.
let num_index_vars := js.length,
index_vars β intron' num_index_vars,
index_equations β intron' num_index_vars,
major_premise β intro1,
revert_lst index_equations,
let index_vars := index_vars.map local_pp_name,
pure (major_premise, index_vars.length, index_vars, relevant_ctx_size)
/-!
## Simplification of Induction Hypotheses
Auto-generalisation and complex index generalisation may produce unnecessarily
complex induction hypotheses. We define a simplification algorithm that recovers
understandable induction hypotheses in many practical cases.
-/
/--
Process one index equation for `simplify_ih`.
Input: a local constant `h : x = y` or `h : x == y`.
Output: A proof of `x = y` or `x == y` and possibly a local constant of type
`x = y` or `x == y` used in the proof. More specifically:
- For `h : x = y` and `x` defeq `y`, we return the proof of `x = y` by
reflexivity and `none`.
- For `h : x = y` and `x` not defeq `y`, we return `h` and `h`.
- For `h : x == y` where `x` and `y` have defeq types:
- If `x` defeq `y`, we return the proof of `x == y` by reflexivity and `none`.
- If `x` not defeq `y`, we return `heq_of_eq h'` and a fresh local constant
`h' : x = y`.
- For `h : x == y` where `x` and `y` do not have defeq types, we return
`h` and `h`.
Checking for definitional equality of the left- and right-hand sides may assign
metavariables.
-/
meta def process_index_equation : expr β tactic (expr Γ option expr)
| h@(local_const _ ppname binfo
T@(app (app (app (const `eq [u]) type) lhs) rhs)) := do
rhs_eq_lhs β succeeds $ unify rhs lhs,
-- Note: It is important that we `unify rhs lhs` rather than `unify lhs rhs`.
-- This is because `lhs` and `rhs` may be metavariables which represent
-- Ξ -bound variables, so if they unify, we want to assign `rhs := lhs`.
-- If we assign `lhs := rhs` instead, it can happen that `lhs` is used before
-- `rhs` is bound, so the generated term becomes ill-typed.
if rhs_eq_lhs
then pure ((const `eq.refl [u]) type lhs, none)
else do
pure (h, some h)
| h@(local_const uname ppname binfo
T@(app (app (app (app (const `heq [u]) lhs_type) lhs) rhs_type) rhs)) := do
lhs_type_eq_rhs_type β succeeds $ is_def_eq lhs_type rhs_type,
if Β¬ lhs_type_eq_rhs_type
then do
pure (h, some h)
else do
lhs_eq_rhs β succeeds $ unify rhs lhs,
-- See note above about `unify rhs lhs`.
if lhs_eq_rhs
then pure ((const `heq.refl [u]) lhs_type lhs, none)
else do
c β mk_local' ppname binfo $ (const `eq [u]) lhs_type lhs rhs,
let arg := (const `heq_of_eq [u]) lhs_type lhs rhs c,
pure (arg, some c)
| (local_const _ _ _ T) := fail!
"process_index_equation: expected a homogeneous or heterogeneous equation, but got:\n{T}"
| e := fail!
"process_index_equation: expected a local constant, but got:\n{e}"
/--
`assign_local_to_unassigned_mvar mv pp_name binfo`, where `mv` is a
metavariable, acts as follows:
- If `mv` is assigned, it is not changed and the tactic returns `none`.
- If `mv` is not assigned, it is assigned a fresh local constant with
the type of `mv`, pretty name `pp_name` and binder info `binfo`. This local
constant is returned.
-/
meta def assign_local_to_unassigned_mvar (mv : expr) (pp_name : name)
(binfo : binder_info) : tactic (option expr) := do
ff β is_assigned mv | pure none,
type β infer_type mv,
c β mk_local' pp_name binfo type,
unify mv c,
pure c
/--
Apply `assign_local_to_unassigned_mvar` to a list of metavariables. Returns the
newly created local constants.
-/
meta def assign_locals_to_unassigned_mvars
(mvars : list (expr Γ name Γ binder_info)) : tactic (list expr) :=
mvars.mmap_filter $ Ξ» β¨mv, pp_name, binfoβ©,
assign_local_to_unassigned_mvar mv pp_name binfo
/--
Simplify an induction hypothesis.
Input: a local constant
```
ih : β (aβ : Aβ) ... (aβ : Aβ) (bβ : Bβ) ... (bβ : Bβ)
(eqβ : yβ = zβ) ... (eqβ : yβ = zβ), P
```
where `n = num_leading_pis`, `m = num_generalized` and `o = num_index_vars`.
The `aα΅’` are arguments of the type of the constructor argument to which this
induction hypothesis belongs (usually zero). The `xα΅’` are hypotheses that we
generalised over before performing induction. The `eqα΅’` are index equations.
Output: a new local constant
```
ih' : β (a'β : A'β) ... (b'β : B'β) (eq'β : y'β = z'β) ... (eq'β : y'β = z'β), P'
```
This new induction hypothesis is derived from `ih` by removing those `eqα΅’` whose
left- and right-hand sides can be unified. This unification may also determine
some of the `aα΅’` and `bα΅’`. The `a'α΅’`, `b'α΅’` and `eq'α΅’` are those `aα΅’`, `bα΅’` and
`eqα΅’` that were not removed by this process.
Some of the `eqα΅’` may be heterogeneous: `eqα΅’ : yα΅’ == zα΅’`. In this case, we
proceed as follows:
- If `yα΅’` and `zα΅’` are defeq, then `eqα΅’` is removed.
- If `yα΅’` and `zα΅’` are not defeq but their types are, then `eqα΅’` is replaced by
`eq'α΅’ : x = y`.
- Otherwise `eqα΅’` remains unchanged.
-/
/-
TODO `simplify_ih` currently uses Lean's builtin unification procedure to
process the index equations. This procedure has some limitations. For example,
we would like to clear an IH that assumes `0 = 1` since this IH can never be
applied, but Lean's unification doesn't allow us to conclude this.
It would therefore be preferable to use the algorithm from
`tactic.unify_equations` instead. There is no problem with this in principle,
but it requires a complete refactoring of `unify_equations` so that it works
not only on hypotheses but on arbitrary terms.
-/
meta def simplify_ih (num_leading_pis : β) (num_generalized : β)
(num_index_vars : β) (ih : expr) : tactic expr := do
T β infer_type ih,
-- Replace the `xα΅’` with fresh metavariables.
(generalized_arg_mvars, body) β open_n_pis_metas' T (num_leading_pis + num_generalized),
-- Replace the `eqα΅’` with fresh local constants.
(index_eq_lcs, body) β open_n_pis body num_index_vars,
-- Process the `eqα΅’` local constants, yielding
-- - `new_args`: proofs of `yα΅’ = zα΅’`.
-- - `new_index_eq_lcs`: local constants of type `yα΅’ = zα΅’` or `yα΅’ == zα΅’` used
-- in `new_args`.
new_index_eq_lcs_new_args β index_eq_lcs.mmap process_index_equation,
let (new_args, new_index_eq_lcs) := new_index_eq_lcs_new_args.unzip,
let new_index_eq_lcs := new_index_eq_lcs.reduce_option,
-- Assign fresh local constants to those `xα΅’` metavariables that were not
-- assigned by the previous step.
new_generalized_arg_lcs β
assign_locals_to_unassigned_mvars generalized_arg_mvars,
-- Instantiate the metavariables assigned in the previous steps.
new_generalized_arg_lcs β new_generalized_arg_lcs.mmap instantiate_mvars,
new_index_eq_lcs β new_index_eq_lcs.mmap instantiate_mvars,
-- Construct a proof of the new induction hypothesis by applying `ih` to the
-- `xα΅’` metavariables and the `new_args`, then abstracting over the
-- `new_index_eq_lcs` and the `new_generalized_arg_lcs`.
b β instantiate_mvars $
ih.mk_app (generalized_arg_mvars.map prod.fst ++ new_args),
new_ih β lambdas (new_generalized_arg_lcs ++ new_index_eq_lcs) b,
-- Type-check the new induction hypothesis as a sanity check.
type_check new_ih <|> fail!
"internal error in simplify_ih: constructed term does not type check:\n{new_ih}",
-- Replace the old induction hypothesis with the new one.
ih' β note ih.local_pp_name none new_ih,
clear ih,
pure ih'
/-!
## Temporary utilities
The utility functions in this section should be removed pending certain changes
to Lean's standard library.
-/
/--
Updates the tags of new subgoals produced by `cases` or `induction`. `in_tag`
is the initial tag, i.e. the tag of the goal on which `cases`/`induction` was
applied. `rs` should contain, for each subgoal, the constructor name
associated with that goal and the hypotheses that were introduced.
-/
-- TODO Copied from init.meta.interactive. Make that function non-private.
meta def set_cases_tags (in_tag : tag) (rs : list (name Γ list expr)) : tactic unit :=
do gs β get_goals,
match gs with
-- if only one goal was produced, we should not make the tag longer
| [g] := set_tag g in_tag
| _ :=
let tgs : list (name Γ list expr Γ expr) :=
rs.mapβ (Ξ» β¨n, new_hypsβ© g, β¨n, new_hyps, gβ©) gs in
tgs.mmap' $ Ξ» β¨n, new_hyps, gβ©, with_enable_tags $
set_tag g $
(case_tag.from_tag_hyps (n :: in_tag) (new_hyps.map expr.local_uniq_name)).render
end
end eliminate
/-!
## The Elimination Tactics
Finally, we define the tactics `induction'` and `cases'` tactics as well as the
non-interactive variant `eliminate_hyp.`
-/
open eliminate
/--
`eliminate_hyp generate_ihs h gm with_patterns` performs induction or case
analysis on the hypothesis `h`. If `generate_ihs` is true, the tactic performs
induction, otherwise case analysis.
In case analysis mode, `eliminate_hyp` is very similar to `tactic.cases`. The
only differences (assuming no bugs in `eliminate_hyp`) are that `eliminate_hyp`
can do case analysis on a slightly larger class of hypotheses and that it
generates more human-friendly names.
In induction mode, `eliminate_hyp` is similar to `tactic.induction`, but with
more significant differences:
- If `h` (the hypothesis we are performing induction on) has complex indices,
`eliminate_hyp` 'remembers' them. A complex expression is any expression that
is not merely a local hypothesis. A hypothesis `h : I pβ ... pβ jβ ... jβ`,
where `I` is an inductive type with `n` parameters and `m` indices, has a
complex index if any of the `jα΅’` are complex. In this situation, standard
`induction` effectively forgets the exact values of the complex indices,
which often leads to unprovable goals. `eliminate_hyp` 'remembers' them by
adding propositional equalities. As a result, you may find equalities named
`induction_eq` in your goal, and the induction hypotheses may also quantify
over additional equalities.
- `eliminate_hyp` generalises induction hypotheses as much as possible by
default. This means that if you eliminate `n` in the goal
```
n m : β
β’ P n m
```
the induction hypothesis is `β m, P n m` instead of `P n m`.
You can modify this behaviour by giving a different generalisation mode `gm`;
see `tactic.eliminate.generalization_mode`.
- `eliminate_hyp` generates much more human-friendly names than `induction`. It
also clears more redundant hypotheses.
- `eliminate_hyp` currently does not support custom induction principles a la
`induction using`.
The `with_patterns` can be used to give names for the hypotheses introduced by
`eliminate_hyp`. See `tactic.eliminate.with_pattern` for details.
To debug this tactic, use
```
set_option trace.eliminate_hyp true
```
-/
meta def eliminate_hyp (generate_ihs : bool) (major_premise : expr)
(gm := generalization_mode.generalize_all_except [])
(with_patterns : list with_pattern := []) : tactic unit :=
focus1 $ do
mpinfo β get_major_premise_info major_premise,
let major_premise_type := mpinfo.type,
let major_premise_args := mpinfo.args.values.reverse,
env β get_env,
-- Get info about the inductive type
iname β get_app_fn_const_whnf major_premise_type <|> fail!
"The type of {major_premise} should be an inductive type, but it is\n{major_premise_type}",
iinfo β get_inductive_info iname,
-- We would like to disallow mutual/nested inductive types, since these have
-- complicated recursors which we probably don't support. However, there seems
-- to be no way to find out whether an inductive type is mutual/nested.
-- (`environment.is_ginductive` doesn't seem to work.)
trace_state_eliminate_hyp "State before complex index generalisation:",
-- Generalise complex indices
(major_premise, num_index_vars, index_var_names, num_index_generalized) β
generalize_complex_index_args major_premise iinfo.num_params generate_ihs,
trace_state_eliminate_hyp
"State after complex index generalisation and before auto-generalisation:",
-- Generalise hypotheses according to the given generalization_mode.
num_auto_generalized β generalize_hyps major_premise gm,
let num_generalized := num_index_generalized + num_auto_generalized,
-- NOTE: The previous step may have changed the unique names of all hyps in
-- the context.
-- Record the current case tag.
in_tag β get_main_tag,
trace_state_eliminate_hyp
"State after auto-generalisation and before recursor application:",
-- Apply the recursor. We first try the nondependent recursor, then the
-- dependent recursor (if available).
-- Construct a pexpr `@rec _ ... _ major_premise`. Why not
-- ```(%%rec %%major_premise)?` Because for whatever reason, `false.rec_on`
-- takes the motive not as an implicit argument, like any other recursor, but
-- as an explicit one. Why not something based on `mk_app` or `mk_mapp`?
-- Because we need the special elaborator support for `elab_as_eliminator`
-- definitions.
let rec_app : name β pexpr := Ξ» rec_suffix,
(unchecked_cast expr.mk_app : pexpr β list pexpr β pexpr)
(pexpr.mk_explicit (const (iname ++ rec_suffix) []))
(list.repeat pexpr.mk_placeholder (major_premise_args.length + 1) ++
[to_pexpr major_premise]),
let rec_suffix := if generate_ihs then "rec_on" else "cases_on",
let drec_suffix := if generate_ihs then "drec_on" else "dcases_on",
interactive.apply (rec_app rec_suffix)
<|> interactive.apply (rec_app drec_suffix)
<|> fail! "Failed to apply the (dependent) recursor for {iname} on {major_premise}.",
-- Prepare the "with" names for each constructor case.
let with_patterns := prod.fst $
with_patterns.take_list
(iinfo.constructors.map constructor_info.num_nameable_hypotheses),
let constrs := iinfo.constructors.zip with_patterns,
-- For each case (constructor):
cases : list (option (name Γ list expr)) β
focus $ constrs.map $ Ξ» β¨cinfo, with_patternsβ©, do
{ trace_eliminate_hyp "============",
trace_eliminate_hyp $ format! "Case {cinfo.cname}",
trace_state_eliminate_hyp "Initial state:",
-- Get the major premise's arguments. (Some of these may have changed due
-- to the generalising step above.)
major_premise_type β infer_type major_premise,
major_premise_args β get_app_args_whnf major_premise_type,
-- Clear the eliminated hypothesis (if possible)
try $ clear major_premise,
-- Clear the index args (unless other stuff in the goal depends on them)
major_premise_args.mmap' (try β clear),
trace_state_eliminate_hyp
"State after clearing the major premise (and its arguments) and before introductions:",
-- Introduce the constructor arguments
(constructor_args, ihs) β
constructor_intros generate_ihs cinfo,
-- Introduce the auto-generalised hypotheses.
intron num_auto_generalized,
-- Introduce the index equations
index_equations β intron' num_index_vars,
let index_equations := index_equations.map local_pp_name,
-- Introduce the hypotheses that were generalised during index
-- generalisation.
intron num_index_generalized,
trace_state_eliminate_hyp
"State after introductions and before simplifying index equations:",
-- Simplify the index equations. Stop after this step if the goal has been
-- solved by the simplification.
ff β unify_equations index_equations
| trace_eliminate_hyp "Case solved while simplifying index equations." >>
pure none,
trace_state_eliminate_hyp
"State after simplifying index equations and before simplifying IHs:",
-- Simplify the induction hypotheses
-- NOTE: The previous step may have changed the unique names of the
-- induction hypotheses, so we have to locate them again. Their pretty
-- names should be unique in the context, so we can use these.
ihs.mmap' $ Ξ» β¨ih, _, arg_infoβ©, do
{ ih β get_local ih,
(some num_leading_pis) β pure arg_info.recursive_leading_pis
| fail! "eliminate_hyp: internal error: unexpected non-recursive argument info",
simplify_ih num_leading_pis num_auto_generalized num_index_vars ih },
trace_state_eliminate_hyp
"State after simplifying IHs and before clearing index variables:",
-- Try to clear the index variables. These often become unused during
-- the index equation simplification step.
index_var_names.mmap $ Ξ» h, try (get_local h >>= clear),
trace_state_eliminate_hyp
"State after clearing index variables and before renaming:",
-- Rename the constructor names and IHs. We do this here (rather than
-- earlier, when we introduced them) because there may now be less
-- hypotheses in the context, and therefore more of the desired
-- names may be free.
(constructor_arg_hyps, ih_hyps) β
constructor_renames generate_ihs mpinfo iinfo cinfo with_patterns
constructor_args ihs,
trace_state_eliminate_hyp "Final state:",
-- Return the constructor name and the renamable new hypotheses. These are
-- the hypotheses that can later be renamed by the `case` tactic. Note
-- that index variables and index equations are not renamable. This may be
-- counterintuitive in some cases, but it's surprisingly difficult to
-- catch exactly the relevant hyps here.
pure $ some (cinfo.cname, constructor_arg_hyps ++ ih_hyps) },
set_cases_tags in_tag cases.reduce_option,
pure ()
/--
A variant of `tactic.eliminate_hyp` which performs induction or case analysis on
an arbitrary expression. `eliminate_hyp` requires that the major premise is a
hypothesis. `eliminate_expr` lifts this restriction by generalising the goal
over the major premise before calling `eliminate_hyp`. The generalisation
replaces the major premise with a new hypothesis `x` everywhere in the goal.
If `eq_name` is `some h`, an equation `h : major_premise = x` is added to
remember the value of the major premise.
-/
meta def eliminate_expr (generate_induction_hyps : bool) (major_premise : expr)
(eq_name : option name := none) (gm := generalization_mode.generalize_all_except [])
(with_patterns : list with_pattern := []) : tactic unit := do
major_premise_revdeps β reverse_dependencies_of_hyps [major_premise],
num_reverted β unfreezing (revert_lst major_premise_revdeps),
hyp β match eq_name with
| some h := do
x β get_unused_name `x,
interactive.generalize h () (to_pexpr major_premise, x),
get_local x
| none := do
if major_premise.is_local_constant
then pure major_premise
else do
x β get_unused_name `x,
generalize' major_premise x
end,
intron num_reverted,
eliminate_hyp generate_induction_hyps hyp gm with_patterns
end tactic
namespace tactic.interactive
open tactic tactic.eliminate interactive interactive.types lean.parser
/--
Parse a `fixing` or `generalizing` clause for `induction'` or `cases'`.
-/
meta def generalisation_mode_parser : lean.parser generalization_mode :=
(tk "fixing" *>
((tk "*" *> pure (generalization_mode.generalize_only []))
<|>
generalization_mode.generalize_all_except <$> many ident))
<|>
(tk "generalizing" *> generalization_mode.generalize_only <$> many ident)
<|>
pure (generalization_mode.generalize_all_except [])
/--
A variant of `tactic.interactive.induction`, with the following differences:
- If the major premise (the hypothesis we are performing induction on) has
complex indices, `induction'` 'remembers' them. A complex expression is any
expression that is not merely a local hypothesis. A major premise
`h : I pβ ... pβ jβ ... jβ`, where `I` is an inductive type with `n`
parameters and `m` indices, has a complex index if any of the `jα΅’` are
complex. In this situation, standard `induction` effectively forgets the exact
values of the complex indices, which often leads to unprovable goals.
`induction'` 'remembers' them by adding propositional equalities. As a
result, you may find equalities named `induction_eq` in your goal, and the
induction hypotheses may also quantify over additional equalities.
- `induction'` generalises induction hypotheses as much as possible by default.
This means that if you eliminate `n` in the goal
```
n m : β
β’ P n m
```
the induction hypothesis is `β m, P n m` instead of `P n m`.
- `induction'` generates much more human-friendly names than `induction`. It
also clears redundant hypotheses more aggressively.
- `induction'` currently does not support custom induction principles a la
`induction using`.
Like `induction`, `induction'` supports some modifiers:
`induction' e with nβ ... nβ` uses the names `nα΅’` for the new hypotheses.
Instead of a name, you can also give an underscore (`_`) to have `induction'`
generate a name for you, or a hyphen (`-`) to clear the hypothesis and any
hypotheses that depend on it.
`induction' e fixing hβ ... hβ` fixes the hypotheses `hα΅’`, so the induction
hypothesis is not generalised over these hypotheses.
`induction' e fixing *` fixes all hypotheses. This disables the generalisation
functionality, so this mode behaves like standard `induction`.
`induction' e generalizing hβ ... hβ` generalises only the hypotheses `hα΅’`. This
mode behaves like `induction e generalizing hβ ... hβ`.
`induction' t`, where `t` is an arbitrary term (rather than a hypothesis),
generalises the goal over `t`, then performs induction on the generalised goal.
`induction' h : t = x` is similar, but also adds an equation `h : t = x` to
remember the value of `t`.
To debug this tactic, use
```
set_option trace.eliminate_hyp true
```
-/
meta def induction' (major_premise : parse cases_arg_p)
(gm : parse generalisation_mode_parser)
(with_patterns : parse with_pattern.clause_parser) :
tactic unit := do
let β¨eq_name, eβ© := major_premise,
e β to_expr e,
eliminate_expr tt e eq_name gm with_patterns
/--
A variant of `tactic.interactive.cases`, with minor changes:
- `cases'` can perform case analysis on some (rare) goals that `cases` does not
support.
- `cases'` generates much more human-friendly names for the new hypotheses it
introduces.
This tactic supports the same modifiers as `cases`, e.g.
```
cases' H : e = x with n _ o
```
This is almost exactly the same as `tactic.interactive.induction'`, only that no
induction hypotheses are generated.
To debug this tactic, use
```
set_option trace.eliminate_hyp true
```
-/
meta def cases' (major_premise : parse cases_arg_p)
(with_patterns : parse with_pattern.clause_parser) :
tactic unit := do
let β¨eq_name, eβ© := major_premise,
e β to_expr e,
eliminate_expr ff e eq_name (generalization_mode.generalize_only [])
with_patterns
end tactic.interactive
|
4feab82e3f813c6de2c730de95455b93a9314aa8 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/field_theory/cardinality.lean | 049050e06e1350430e00a7f7d6359594378519e3 | [
"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,349 | lean | /-
Copyright (c) 2022 Eric Rodriguez. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Eric Rodriguez
-/
import algebra.ring.ulift
import data.mv_polynomial.cardinal
import data.rat.denumerable
import field_theory.finite.galois_field
import logic.equiv.transfer_instance
import ring_theory.localization.cardinality
import set_theory.cardinal.divisibility
import data.nat.factorization.prime_pow
/-!
# Cardinality of Fields
In this file we show all the possible cardinalities of fields. All infinite cardinals can harbour
a field structure, and so can all types with prime power cardinalities, and this is sharp.
## Main statements
* `fintype.nonempty_field_iff`: A `fintype` can be given a field structure iff its cardinality is a
prime power.
* `infinite.nonempty_field` : Any infinite type can be endowed a field structure.
* `field.nonempty_iff` : There is a field structure on type iff its cardinality is a prime power.
-/
local notation `β` x `β` := fintype.card x
open_locale cardinal non_zero_divisors
universe u
/-- A finite field has prime power cardinality. -/
lemma fintype.is_prime_pow_card_of_field {Ξ±} [fintype Ξ±] [field Ξ±] : is_prime_pow (βΞ±β) :=
begin
casesI char_p.exists Ξ± with p _,
haveI hp := fact.mk (char_p.char_is_prime Ξ± p),
let b := is_noetherian.finset_basis (zmod p) Ξ±,
rw [module.card_fintype b, zmod.card, is_prime_pow_pow_iff],
{ exact hp.1.is_prime_pow },
rw βfinite_dimensional.finrank_eq_card_basis b,
exact finite_dimensional.finrank_pos.ne'
end
/-- A `fintype` can be given a field structure iff its cardinality is a prime power. -/
lemma fintype.nonempty_field_iff {Ξ±} [fintype Ξ±] : nonempty (field Ξ±) β is_prime_pow (βΞ±β) :=
begin
refine β¨Ξ» β¨hβ©, by exactI fintype.is_prime_pow_card_of_field, _β©,
rintros β¨p, n, hp, hn, hΞ±β©,
haveI := fact.mk hp.nat_prime,
exact β¨(fintype.equiv_of_card_eq ((galois_field.card p n hn.ne').trans hΞ±)).symm.fieldβ©,
end
lemma fintype.not_is_field_of_card_not_prime_pow {Ξ±} [fintype Ξ±] [ring Ξ±] :
Β¬ is_prime_pow (βΞ±β) β Β¬ is_field Ξ± :=
mt $ Ξ» h, fintype.nonempty_field_iff.mp β¨h.to_fieldβ©
/-- Any infinite type can be endowed a field structure. -/
lemma infinite.nonempty_field {Ξ± : Type u} [infinite Ξ±] : nonempty (field Ξ±) :=
begin
letI K := fraction_ring (mv_polynomial Ξ± $ ulift.{u} β),
suffices : #Ξ± = #K,
{ obtain β¨eβ© := cardinal.eq.1 this,
exact β¨e.fieldβ© },
rw βis_localization.card (mv_polynomial Ξ± $ ulift.{u} β)β° K le_rfl,
apply le_antisymm,
{ refine β¨β¨Ξ» a, mv_polynomial.monomial (finsupp.single a 1) (1 : ulift.{u} β), Ξ» x y h, _β©β©,
simpa [mv_polynomial.monomial_eq_monomial_iff, finsupp.single_eq_single_iff] using h },
{ simp }
end
/-- There is a field structure on type if and only if its cardinality is a prime power. -/
lemma field.nonempty_iff {Ξ± : Type u} : nonempty (field Ξ±) β is_prime_pow (#Ξ±) :=
begin
rw cardinal.is_prime_pow_iff,
casesI fintype_or_infinite Ξ± with h h,
{ simpa only [cardinal.mk_fintype, nat.cast_inj, exists_eq_left',
(cardinal.nat_lt_aleph_0 _).not_le, false_or]
using fintype.nonempty_field_iff },
{ simpa only [β cardinal.infinite_iff, h, true_or, iff_true]
using infinite.nonempty_field },
end
|
542733fb217bd318ee669923b2fc65b595e9b8c4 | 1abd1ed12aa68b375cdef28959f39531c6e95b84 | /src/algebra/tropical/basic.lean | 26fe607601e0bc60222b6698e77ca198389ddfbd | [
"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 | 12,091 | lean | /-
Copyright (c) 2021 Yakov Pechersky. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yakov Pechersky
-/
import algebra.group_power.order
import algebra.smul_with_zero
/-!
# Tropical algebraic structures
This file defines algebraic structures of the (min-)tropical numbers, up to the tropical semiring.
Some basic lemmas about conversion from the base type `R` to `tropical R` are provided, as
well as the expected implementations of tropical addition and tropical multiplication.
## Main declarations
* `tropical R`: The type synonym of the tropical interpretation of `R`.
If `[linear_order R]`, then addition on `R` is via `min`.
* `semiring (tropical R)`: A `linear_ordered_add_comm_monoid_with_top R`
induces a `semiring (tropical R)`. If one solely has `[linear_ordered_add_comm_monoid R]`,
then the "tropicalization of `R`" would be `tropical (with_top R)`.
## Implementation notes
The tropical structure relies on `has_top` and `min`. For the max-tropical numbers, use
`order_dual R`.
Inspiration was drawn from the implementation of `additive`/`multiplicative`/`opposite`,
where a type synonym is created with some barebones API, and quickly made irreducible.
Algebraic structures are provided with as few typeclass assumptions as possible, even though
most references rely on `semiring (tropical R)` for building up the whole theory.
## References followed
* https://arxiv.org/pdf/math/0408099.pdf
* https://www.mathenjeans.fr/sites/default/files/sujets/tropical_geometry_-_casagrande.pdf
-/
universes u v
variables (R : Type u)
/-- The tropicalization of a type `R`. -/
def tropical : Type u := R
variables {R}
namespace tropical
/-- Reinterpret `x : R` as an element of `tropical R`.
See `tropical.trop_equiv` for the equivalence.
-/
@[pp_nodot]
def trop : R β tropical R := id
/-- Reinterpret `x : tropical R` as an element of `R`.
See `tropical.trop_equiv` for the equivalence. -/
@[pp_nodot]
def untrop : tropical R β R := id
lemma trop_injective : function.injective (trop : R β tropical R) := Ξ» _ _, id
lemma untrop_injective : function.injective (untrop : tropical R β R) := Ξ» _ _, id
@[simp] lemma trop_inj_iff (x y : R) : trop x = trop y β x = y := iff.rfl
@[simp] lemma untrop_inj_iff (x y : tropical R) : untrop x = untrop y β x = y := iff.rfl
@[simp] lemma trop_untrop (x : tropical R) : trop (untrop x) = x := rfl
@[simp] lemma untrop_trop (x : R) : untrop (trop x) = x := rfl
lemma left_inverse_trop : function.left_inverse (trop : R β tropical R) untrop := trop_untrop
lemma right_inverse_trop : function.right_inverse (trop : R β tropical R) untrop := trop_untrop
attribute [irreducible] tropical
/-- Reinterpret `x : R` as an element of `tropical R`.
See `tropical.trop_order_iso` for the order-preserving equivalence. -/
def trop_equiv : R β tropical R :=
{ to_fun := trop,
inv_fun := untrop,
left_inv := untrop_trop,
right_inv := trop_untrop }
@[simp]
lemma trop_equiv_coe_fn : (trop_equiv : R β tropical R) = trop := rfl
@[simp]
lemma trop_equiv_symm_coe_fn : (trop_equiv.symm : tropical R β R) = untrop := rfl
lemma trop_eq_iff_eq_untrop {x : R} {y} : trop x = y β x = untrop y :=
trop_equiv.apply_eq_iff_eq_symm_apply
lemma untrop_eq_iff_eq_trop {x} {y : R} : untrop x = y β x = trop y :=
trop_equiv.symm.apply_eq_iff_eq_symm_apply
lemma injective_trop : function.injective (trop : R β tropical R) := trop_equiv.injective
lemma injective_untrop : function.injective (untrop : tropical R β R) := trop_equiv.symm.injective
lemma surjective_trop : function.surjective (trop : R β tropical R) := trop_equiv.surjective
lemma surjective_untrop : function.surjective (untrop : tropical R β R) :=
trop_equiv.symm.surjective
instance [inhabited R] : inhabited (tropical R) := β¨trop (default _)β©
/-- Recursing on a `x' : tropical R` is the same as recursing on an `x : R` reinterpreted
as a term of `tropical R` via `trop x`. -/
@[simp]
def trop_rec {F : Ξ (X : tropical R), Sort v} (h : Ξ X, F (trop X)) : Ξ X, F X :=
Ξ» X, h (untrop X)
section order
instance [preorder R] : preorder (tropical R) :=
{ le := Ξ» x y, untrop x β€ untrop y,
le_refl := Ξ» _, le_refl _,
le_trans := Ξ» _ _ _ h h', le_trans h h', }
@[simp] lemma untrop_le_iff [preorder R] {x y : tropical R} :
untrop x β€ untrop y β x β€ y := iff.rfl
/-- Reinterpret `x : R` as an element of `tropical R`, preserving the order. -/
def trop_order_iso [preorder R] : R βo tropical R :=
{ map_rel_iff' := Ξ» _ _, untrop_le_iff,
..trop_equiv }
@[simp]
lemma trop_order_iso_coe_fn [preorder R] : (trop_order_iso : R β tropical R) = trop := rfl
@[simp]
lemma trop_order_iso_symm_coe_fn [preorder R] : (trop_order_iso.symm : tropical R β R) = untrop :=
rfl
instance [partial_order R] : partial_order (tropical R) :=
{ le_antisymm := Ξ» _ _ h h', untrop_injective (le_antisymm h h'),
..tropical.preorder }
instance [has_top R] : has_zero (tropical R) := β¨trop β€β©
instance [has_top R] : has_top (tropical R) := β¨0β©
@[simp] lemma untrop_zero [has_top R] : untrop (0 : tropical R) = β€ := rfl
@[simp] lemma trop_top [has_top R] : trop (β€ : R) = 0 := rfl
@[simp] lemma trop_coe_ne_zero (x : R) : trop (x : with_top R) β 0 .
@[simp] lemma zero_ne_trop_coe (x : R) : (0 : tropical (with_top R)) β trop x .
@[simp] lemma le_zero [order_top R] (x : tropical R) : x β€ 0 := le_top
instance [partial_order R] : order_top (tropical (with_top R)) :=
{ le_top := Ξ» a a' h, option.no_confusion h,
..tropical.partial_order,
..tropical.has_top }
variable [linear_order R]
/-- Tropical addition is the minimum of two underlying elements of `R`. -/
protected def add (x y : tropical R) : tropical R :=
trop (min (untrop x) (untrop y))
instance : add_comm_semigroup (tropical R) :=
{ add := tropical.add,
add_assoc := Ξ» _ _ _, untrop_injective (min_assoc _ _ _),
add_comm := Ξ» _ _, untrop_injective (min_comm _ _) }
instance : linear_order (tropical R) :=
{ le_total := Ξ» a b, le_total (untrop a) (untrop b),
decidable_le := Ξ» x y, if h : (untrop x) β€ (untrop y) then is_true h else is_false h,
..tropical.partial_order }
@[simp] lemma untrop_add (x y : tropical R) : untrop (x + y) = min (untrop x) (untrop y) := rfl
lemma trop_add_def (x y : tropical R) : x + y = trop (min (untrop x) (untrop y)) := rfl
@[simp] lemma add_eq_left β¦x y : tropical Rβ¦ (h : x β€ y) :
x + y = x := untrop_injective (by simpa using h)
@[simp] lemma add_eq_right β¦x y : tropical Rβ¦ (h : y β€ x) :
x + y = y := untrop_injective (by simpa using h)
@[simp] lemma add_self (x : tropical R) : x + x = x := untrop_injective (min_eq_right le_rfl)
@[simp] lemma bit0 (x : tropical R) : bit0 x = x := add_self x
lemma add_eq_iff {x y z : tropical R} :
x + y = z β x = z β§ x β€ y β¨ y = z β§ y β€ x :=
by simp [trop_add_def, trop_eq_iff_eq_untrop, min_eq_iff]
@[simp] lemma add_eq_zero_iff {a b : tropical (with_top R)} :
a + b = 0 β a = 0 β§ b = 0 :=
begin
rw add_eq_iff,
split,
{ rintro (β¨rfl, hβ©|β¨rfl, hβ©),
{ exact β¨rfl, le_antisymm (le_zero _) hβ© },
{ exact β¨le_antisymm (le_zero _) h, rflβ© } },
{ rintro β¨rfl, rflβ©,
simp }
end
-- We cannot define `add_comm_monoid` here because there is no class that is solely
-- `[linear_order R] [order_top R]`
end order
section monoid
/-- Tropical multiplication is the addition in the underlying `R`. -/
protected def mul [has_add R] (x y : tropical R) : tropical R := trop (untrop x + untrop y)
instance [has_add R] : has_mul (tropical R) := β¨tropical.mulβ©
@[simp] lemma untrop_mul [has_add R] (x y : tropical R) :
untrop (x * y) = untrop x + untrop y := rfl
lemma trop_mul_def [has_add R] (x y : tropical R) :
x * y = trop (untrop x + untrop y) := rfl
instance [has_zero R] : has_one (tropical R) := β¨trop 0β©
instance [has_zero R] : nontrivial (tropical (with_top R)) :=
β¨β¨0, 1, trop_injective.ne with_top.top_ne_coeβ©β©
instance [has_neg R] : has_inv (tropical R) := β¨Ξ» x, trop (- untrop x)β©
@[simp] lemma untrop_inv [has_neg R] (x : tropical R) : untrop xβ»ΒΉ = - untrop x := rfl
instance [has_sub R] : has_div (tropical R) := β¨Ξ» x y, trop (untrop x - untrop y)β©
@[simp] lemma untrop_div [has_sub R] (x y : tropical R) :
untrop (x / y) = untrop x - untrop y := rfl
instance [add_semigroup R] : semigroup (tropical R) :=
{ mul := tropical.mul,
mul_assoc := Ξ» _ _ _, untrop_injective (add_assoc _ _ _) }
instance [add_comm_semigroup R] : comm_semigroup (tropical R) :=
{ mul_comm := Ξ» _ _, untrop_injective (add_comm _ _),
..tropical.semigroup }
instance [add_monoid R] : monoid (tropical R) :=
{ one := trop 0,
one_mul := Ξ» _, untrop_injective (zero_add _),
mul_one := Ξ» _, untrop_injective (add_zero _),
..tropical.semigroup }
@[simp] lemma untrop_one [add_monoid R] : untrop (1 : tropical R) = 0 := rfl
@[simp] lemma untrop_pow [add_monoid R] (x : tropical R) (n : β) :
untrop (x ^ n) = n β’ untrop x :=
begin
induction n with n IH,
{ simp, },
{ rw [pow_succ, untrop_mul, IH, succ_nsmul] }
end
@[simp] lemma trop_nsmul [add_monoid R] (x : R) (n : β) :
trop (n β’ x) = trop x ^ n :=
by simp [trop_eq_iff_eq_untrop]
instance [add_comm_monoid R] : comm_monoid (tropical R) :=
{ ..tropical.monoid, ..tropical.comm_semigroup }
instance [add_group R] : group (tropical R) :=
{ inv := Ξ» x, trop (- untrop x),
mul_left_inv := Ξ» _, untrop_injective (add_left_neg _),
..tropical.monoid }
instance [add_comm_group R] : comm_group (tropical R) :=
{ mul_comm := Ξ» _ _, untrop_injective (add_comm _ _),
..tropical.group }
end monoid
section distrib
instance covariant_mul [preorder R] [has_add R] [covariant_class R R (+) (β€)] :
covariant_class (tropical R) (tropical R) (*) (β€) :=
β¨Ξ» x y z h, add_le_add_left h _β©
instance covariant_swap_mul [preorder R] [has_add R] [covariant_class R R (function.swap (+)) (β€)] :
covariant_class (tropical R) (tropical R) (function.swap (*)) (β€) :=
β¨Ξ» x y z h, add_le_add_right h _β©
instance [linear_order R] [has_add R]
[covariant_class R R (+) (β€)] [covariant_class R R (function.swap (+)) (β€)] :
distrib (tropical R) :=
{ mul := tropical.mul,
add := tropical.add,
left_distrib := Ξ» _ _ _, untrop_injective (min_add_add_left _ _ _).symm,
right_distrib := Ξ» _ _ _, untrop_injective (min_add_add_right _ _ _).symm }
@[simp] lemma add_pow [linear_order R] [add_monoid R]
[covariant_class R R (+) (β€)] [covariant_class R R (function.swap (+)) (β€)]
(x y : tropical R) (n : β) :
(x + y) ^ n = x ^ n + y ^ n :=
begin
cases le_total x y with h h,
{ rw [add_eq_left h, add_eq_left (pow_le_pow_of_le_left' h _)] },
{ rw [add_eq_right h, add_eq_right (pow_le_pow_of_le_left' h _)] }
end
end distrib
section semiring
variable [linear_ordered_add_comm_monoid_with_top R]
instance : comm_semiring (tropical R) :=
{ zero_add := Ξ» _, untrop_injective (min_top_left _),
add_zero := Ξ» _, untrop_injective (min_top_right _),
zero_mul := Ξ» _, untrop_injective (top_add _),
mul_zero := Ξ» _, untrop_injective (add_top _),
..tropical.has_zero,
..tropical.distrib,
..tropical.add_comm_semigroup,
..tropical.comm_monoid }
-- This could be stated on something like `linear_order_with_top Ξ±` if that existed
@[simp] lemma succ_nsmul (x : tropical R) (n : β) :
(n + 1) β’ x = x :=
begin
induction n with n IH,
{ simp },
{ rw [add_nsmul, IH, one_nsmul, add_self] }
end
-- TODO: find/create the right classes to make this hold (for enat, ennreal, etc)
-- Requires `zero_eq_bot` to be true
-- lemma add_eq_zero_iff {a b : tropical R} :
-- a + b = 1 β a = 1 β¨ b = 1 := sorry
@[simp] lemma mul_eq_zero_iff {R : Type*} [linear_ordered_add_comm_monoid R]
{a b : tropical (with_top R)} :
a * b = 0 β a = 0 β¨ b = 0 :=
by simp [βuntrop_inj_iff, with_top.add_eq_top]
instance {R : Type*} [linear_ordered_add_comm_monoid R] :
no_zero_divisors (tropical (with_top R)) :=
β¨Ξ» _ _, mul_eq_zero_iff.mpβ©
end semiring
end tropical
|
d4f83ac81654a96f130b77e288e1b8eac5934bf6 | 46125763b4dbf50619e8846a1371029346f4c3db | /src/topology/metric_space/closeds.lean | 6bde51e333c1c8556068b633648bf2e7ab264c89 | [
"Apache-2.0"
] | permissive | thjread/mathlib | a9d97612cedc2c3101060737233df15abcdb9eb1 | 7cffe2520a5518bba19227a107078d83fa725ddc | refs/heads/master | 1,615,637,696,376 | 1,583,953,063,000 | 1,583,953,063,000 | 246,680,271 | 0 | 0 | Apache-2.0 | 1,583,960,875,000 | 1,583,960,875,000 | null | UTF-8 | Lean | false | false | 21,582 | lean | /-
Copyright (c) 2019 SΓ©bastien GouΓ«zel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: SΓ©bastien GouΓ«zel
-/
import topology.metric_space.hausdorff_distance topology.opens analysis.specific_limits
/-!
# Closed subsets
This file defines the metric and emetric space structure on the types of closed subsets and nonempty compact
subsets of a metric or emetric space.
The Hausdorff distance induces an emetric space structure on the type of closed subsets
of an emetric space, called `closeds`. Its completeness, resp. compactness, resp.
second-countability, follow from the corresponding properties of the original space.
In a metric space, the type of nonempty compact subsets (called `nonempty_compacts`) also
inherits a metric space structure from the Hausdorff distance, as the Hausdorff edistance is
always finite in this context.
-/
noncomputable theory
open_locale classical
open_locale topological_space
universe u
open classical lattice set function topological_space filter
namespace emetric
section
variables {Ξ± : Type u} [emetric_space Ξ±] {s : set Ξ±}
/-- In emetric spaces, the Hausdorff edistance defines an emetric space structure
on the type of closed subsets -/
instance closeds.emetric_space : emetric_space (closeds Ξ±) :=
{ edist := Ξ»s t, Hausdorff_edist s.val t.val,
edist_self := Ξ»s, Hausdorff_edist_self,
edist_comm := Ξ»s t, Hausdorff_edist_comm,
edist_triangle := Ξ»s t u, Hausdorff_edist_triangle,
eq_of_edist_eq_zero :=
Ξ»s t h, subtype.eq ((Hausdorff_edist_zero_iff_eq_of_closed s.property t.property).1 h) }
/-- The edistance to a closed set depends continuously on the point and the set -/
lemma continuous_inf_edist_Hausdorff_edist :
continuous (Ξ»p : Ξ± Γ (closeds Ξ±), inf_edist p.1 (p.2).val) :=
begin
refine continuous_of_le_add_edist 2 (by simp) _,
rintros β¨x, sβ© β¨y, tβ©,
calc inf_edist x (s.val) β€ inf_edist x (t.val) + Hausdorff_edist (t.val) (s.val) :
inf_edist_le_inf_edist_add_Hausdorff_edist
... β€ (inf_edist y (t.val) + edist x y) + Hausdorff_edist (t.val) (s.val) :
add_le_add_right' inf_edist_le_inf_edist_add_edist
... = inf_edist y (t.val) + (edist x y + Hausdorff_edist (s.val) (t.val)) :
by simp [add_comm, add_left_comm, Hausdorff_edist_comm]
... β€ inf_edist y (t.val) + (edist (x, s) (y, t) + edist (x, s) (y, t)) :
add_le_add_left' (add_le_add' (by simp [edist, le_refl]) (by simp [edist, le_refl]))
... = inf_edist y (t.val) + 2 * edist (x, s) (y, t) :
by rw [β mul_two, mul_comm]
end
/-- Subsets of a given closed subset form a closed set -/
lemma is_closed_subsets_of_is_closed (hs : is_closed s) :
is_closed {t : closeds Ξ± | t.val β s} :=
begin
refine is_closed_of_closure_subset (Ξ»t ht x hx, _),
-- t : closeds Ξ±, ht : t β closure {t : closeds Ξ± | t.val β s},
-- x : Ξ±, hx : x β t.val
-- goal : x β s
have : x β closure s,
{ refine mem_closure_iff.2 (λΡ Ρpos, _),
rcases mem_closure_iff.1 ht Ξ΅ Ξ΅pos with β¨u, hu, Dtuβ©,
-- u : closeds Ξ±, hu : u β {t : closeds Ξ± | t.val β s}, hu' : edist t u < Ξ΅
rcases exists_edist_lt_of_Hausdorff_edist_lt hx Dtu with β¨y, hy, Dxyβ©,
-- y : Ξ±, hy : y β u.val, Dxy : edist x y < Ξ΅
exact β¨y, hu hy, Dxyβ© },
rwa closure_eq_of_is_closed hs at this,
end
/-- By definition, the edistance on `closeds Ξ±` is given by the Hausdorff edistance -/
lemma closeds.edist_eq {s t : closeds Ξ±} : edist s t = Hausdorff_edist s.val t.val := rfl
/-- In a complete space, the type of closed subsets is complete for the
Hausdorff edistance. -/
instance closeds.complete_space [complete_space Ξ±] : complete_space (closeds Ξ±) :=
begin
/- We will show that, if a sequence of sets `s n` satisfies
`edist (s n) (s (n+1)) < 2^{-n}`, then it converges. This is enough to guarantee
completeness, by a standard completeness criterion.
We use the shorthand `B n = 2^{-n}` in ennreal. -/
let B : β β ennreal := Ξ» n, (2β»ΒΉ)^n,
have B_pos : β n, (0:ennreal) < B n,
by simp [B, ennreal.pow_pos],
have B_ne_top : β n, B n β β€,
by simp [B, ennreal.div_def, ennreal.pow_ne_top],
/- Consider a sequence of closed sets `s n` with `edist (s n) (s (n+1)) < B n`.
We will show that it converges. The limit set is t0 = βn, closure (βmβ₯n, s m).
We will have to show that a point in `s n` is close to a point in `t0`, and a point
in `t0` is close to a point in `s n`. The completeness then follows from a
standard criterion. -/
refine complete_of_convergent_controlled_sequences B B_pos (Ξ»s hs, _),
let t0 := βn, closure (βmβ₯n, (s m).val),
let t : closeds Ξ± := β¨t0, is_closed_Inter (Ξ»_, is_closed_closure)β©,
use t,
-- The inequality is written this way to agree with `edist_le_of_edist_le_geometric_of_tendstoβ`
have I1 : βn:β, βx β (s n).val, βy β t0, edist x y β€ 2 * B n,
{ /- This is the main difficulty of the proof. Starting from `x β s n`, we want
to find a point in `t0` which is close to `x`. Define inductively a sequence of
points `z m` with `z n = x` and `z m β s m` and `edist (z m) (z (m+1)) β€ B m`. This is
possible since the Hausdorff distance between `s m` and `s (m+1)` is at most `B m`.
This sequence is a Cauchy sequence, therefore converging as the space is complete, to
a limit which satisfies the required properties. -/
assume n x hx,
obtain β¨z, hzβ, hzβ© : β z : Ξ l, (s (n+l)).val, (z 0:Ξ±) = x β§
β k, edist (z k:Ξ±) (z (k+1):Ξ±) β€ B n / 2^k,
{ -- We prove existence of the sequence by induction.
have : β (l : β) (z : (s (n+l)).val), β z' : (s (n+l+1)).val, edist (z:Ξ±) z' β€ B n / 2^l,
{ assume l z,
obtain β¨z', z'_mem, hz'β© : β z' β (s (n+l+1)).val, edist (z:Ξ±) z' < B n / 2^l,
{ apply exists_edist_lt_of_Hausdorff_edist_lt z.2,
simp only [B, ennreal.div_def, ennreal.inv_pow'],
rw [β pow_add],
apply hs; simp },
exact β¨β¨z', z'_memβ©, le_of_lt hz'β© },
use [Ξ» k, nat.rec_on k β¨x, hxβ© (Ξ»l z, some (this l z)), rfl],
exact Ξ» k, some_spec (this k _) },
-- it follows from the previous bound that `z` is a Cauchy sequence
have : cauchy_seq (Ξ» k, ((z k):Ξ±)),
from cauchy_seq_of_edist_le_geometric_two (B n) (B_ne_top n) hz,
-- therefore, it converges
rcases cauchy_seq_tendsto_of_complete this with β¨y, y_limβ©,
use y,
-- the limit point `y` will be the desired point, in `t0` and close to our initial point `x`.
-- First, we check it belongs to `t0`.
have : y β t0 := mem_Inter.2 (Ξ»k, mem_closure_of_tendsto (by simp) y_lim
begin
simp only [exists_prop, set.mem_Union, filter.mem_at_top_sets, set.mem_preimage, set.preimage_Union],
exact β¨k, Ξ» m hm, β¨n+m, zero_add k βΈ add_le_add (zero_le n) hm, (z m).2β©β©
end),
use this,
-- Then, we check that `y` is close to `x = z n`. This follows from the fact that `y`
-- is the limit of `z k`, and the distance between `z n` and `z k` has already been estimated.
rw [β hzβ],
exact edist_le_of_edist_le_geometric_two_of_tendstoβ (B n) hz y_lim },
have I2 : βn:β, βx β t0, βy β (s n).val, edist x y β€ 2 * B n,
{ /- For the (much easier) reverse inequality, we start from a point `x β t0` and we want
to find a point `y β s n` which is close to `x`.
`x` belongs to `t0`, the intersection of the closures. In particular, it is well
approximated by a point `z` in `βmβ₯n, s m`, say in `s m`. Since `s m` and
`s n` are close, this point is itself well approximated by a point `y` in `s n`,
as required. -/
assume n x xt0,
have : x β closure (βmβ₯n, (s m).val), by apply mem_Inter.1 xt0 n,
rcases mem_closure_iff.1 this (B n) (B_pos n) with β¨z, hz, Dxzβ©,
-- z : Ξ±, Dxz : edist x z < B n,
simp only [exists_prop, set.mem_Union] at hz,
rcases hz with β¨m, β¨m_ge_n, hmβ©β©,
-- m : β, m_ge_n : m β₯ n, hm : z β (s m).val
have : Hausdorff_edist (s m).val (s n).val < B n := hs n m n m_ge_n (le_refl n),
rcases exists_edist_lt_of_Hausdorff_edist_lt hm this with β¨y, hy, Dzyβ©,
-- y : Ξ±, hy : y β (s n).val, Dzy : edist z y < B n
exact β¨y, hy, calc
edist x y β€ edist x z + edist z y : edist_triangle _ _ _
... β€ B n + B n : add_le_add' (le_of_lt Dxz) (le_of_lt Dzy)
... = 2 * B n : (two_mul _).symm β© },
-- Deduce from the above inequalities that the distance between `s n` and `t0` is at most `2 B n`.
have main : βn:β, edist (s n) t β€ 2 * B n := Ξ»n, Hausdorff_edist_le_of_mem_edist (I1 n) (I2 n),
-- from this, the convergence of `s n` to `t0` follows.
refine (tendsto_at_top _).2 (λΡ Ρpos, _),
have : tendsto (Ξ»n, 2 * B n) at_top (π (2 * 0)),
from ennreal.tendsto.const_mul
(ennreal.tendsto_pow_at_top_nhds_0_of_lt_1 $ by simp [ennreal.one_lt_two])
(or.inr $ by simp),
rw mul_zero at this,
obtain β¨N, hNβ© : β N, β b β₯ N, Ξ΅ > 2 * B b,
from ((tendsto_order.1 this).2 Ξ΅ Ξ΅pos).exists_forall_of_at_top,
exact β¨N, Ξ»n hn, lt_of_le_of_lt (main n) (hN n hn)β©
end
/-- In a compact space, the type of closed subsets is compact. -/
instance closeds.compact_space [compact_space Ξ±] : compact_space (closeds Ξ±) :=
β¨begin
/- by completeness, it suffices to show that it is totally bounded,
i.e., for all Ξ΅>0, there is a finite set which is Ξ΅-dense.
start from a set `s` which is Ξ΅-dense in Ξ±. Then the subsets of `s`
are finitely many, and Ξ΅-dense for the Hausdorff distance. -/
refine compact_of_totally_bounded_is_closed (emetric.totally_bounded_iff.2 (λΡ Ρpos, _)) is_closed_univ,
rcases dense Ξ΅pos with β¨Ξ΄, Ξ΄pos, Ξ΄ltβ©,
rcases emetric.totally_bounded_iff.1 (compact_iff_totally_bounded_complete.1 (@compact_univ Ξ± _ _)).1 Ξ΄ Ξ΄pos
with β¨s, fs, hsβ©,
-- s : set Ξ±, fs : finite s, hs : univ β β (y : Ξ±) (H : y β s), eball y Ξ΄
-- we first show that any set is well approximated by a subset of `s`.
have main : β u : set Ξ±, βv β s, Hausdorff_edist u v β€ Ξ΄,
{ assume u,
let v := {x : Ξ± | x β s β§ βyβu, edist x y < Ξ΄},
existsi [v, ((Ξ»x hx, hx.1) : v β s)],
refine Hausdorff_edist_le_of_mem_edist _ _,
{ assume x hx,
have : x β βy β s, ball y Ξ΄ := hs (by simp),
rcases mem_bUnion_iff.1 this with β¨y, ys, dyβ©,
have : edist y x < Ξ΄ := by simp at dy; rwa [edist_comm] at dy,
exact β¨y, β¨ys, β¨x, hx, thisβ©β©, le_of_lt dyβ© },
{ rintros x β¨hx1, β¨y, yu, hyβ©β©,
exact β¨y, yu, le_of_lt hyβ© }},
-- introduce the set F of all subsets of `s` (seen as members of `closeds Ξ±`).
let F := {f : closeds Ξ± | f.val β s},
use F,
split,
-- `F` is finite
{ apply @finite_of_finite_image _ _ F (Ξ»f, f.val),
{ exact subtype.val_injective.inj_on F },
{ refine finite_subset (finite_subsets_of_finite fs) (Ξ»b, _),
simp only [and_imp, set.mem_image, set.mem_set_of_eq, exists_imp_distrib],
assume x hx hx',
rwa hx' at hx }},
-- `F` is Ξ΅-dense
{ assume u _,
rcases main u.val with β¨t0, t0s, Dut0β©,
have : is_closed t0 := closed_of_compact _ (finite_subset fs t0s).compact,
let t : closeds Ξ± := β¨t0, thisβ©,
have : t β F := t0s,
have : edist u t < Ξ΅ := lt_of_le_of_lt Dut0 Ξ΄lt,
apply mem_bUnion_iff.2,
exact β¨t, βΉt β FβΊ, thisβ© }
endβ©
/-- In an emetric space, the type of non-empty compact subsets is an emetric space,
where the edistance is the Hausdorff edistance -/
instance nonempty_compacts.emetric_space : emetric_space (nonempty_compacts Ξ±) :=
{ edist := Ξ»s t, Hausdorff_edist s.val t.val,
edist_self := Ξ»s, Hausdorff_edist_self,
edist_comm := Ξ»s t, Hausdorff_edist_comm,
edist_triangle := Ξ»s t u, Hausdorff_edist_triangle,
eq_of_edist_eq_zero := Ξ»s t h, subtype.eq $ begin
have : closure (s.val) = closure (t.val) := Hausdorff_edist_zero_iff_closure_eq_closure.1 h,
rwa [closure_eq_iff_is_closed.2 (closed_of_compact _ s.property.2),
closure_eq_iff_is_closed.2 (closed_of_compact _ t.property.2)] at this,
end }
/-- `nonempty_compacts.to_closeds` is a uniform embedding (as it is an isometry) -/
lemma nonempty_compacts.to_closeds.uniform_embedding :
uniform_embedding (@nonempty_compacts.to_closeds Ξ± _ _) :=
isometry.uniform_embedding $ Ξ»x y, rfl
/-- The range of `nonempty_compacts.to_closeds` is closed in a complete space -/
lemma nonempty_compacts.is_closed_in_closeds [complete_space Ξ±] :
is_closed (range $ @nonempty_compacts.to_closeds Ξ± _ _) :=
begin
have : range nonempty_compacts.to_closeds = {s : closeds Ξ± | s.val.nonempty β§ compact s.val},
from range_inclusion _,
rw this,
refine is_closed_of_closure_subset (Ξ»s hs, β¨_, _β©),
{ -- take a set set t which is nonempty and at a finite distance of s
rcases mem_closure_iff.1 hs β€ ennreal.coe_lt_top with β¨t, ht, Dstβ©,
rw edist_comm at Dst,
-- since `t` is nonempty, so is `s`
exact nonempty_of_Hausdorff_edist_ne_top ht.1 (ne_of_lt Dst) },
{ refine compact_iff_totally_bounded_complete.2 β¨_, is_complete_of_is_closed s.propertyβ©,
refine totally_bounded_iff.2 (λΡ Ρpos, _),
-- we have to show that s is covered by finitely many eballs of radius Ξ΅
-- pick a nonempty compact set t at distance at most Ξ΅/2 of s
rcases mem_closure_iff.1 hs (Ξ΅/2) (ennreal.half_pos Ξ΅pos) with β¨t, ht, Dstβ©,
-- cover this space with finitely many balls of radius Ξ΅/2
rcases totally_bounded_iff.1 (compact_iff_totally_bounded_complete.1 ht.2).1 (Ξ΅/2) (ennreal.half_pos Ξ΅pos)
with β¨u, fu, utβ©,
refine β¨u, β¨fu, Ξ»x hx, _β©β©,
-- u : set Ξ±, fu : finite u, ut : t.val β β (y : Ξ±) (H : y β u), eball y (Ξ΅ / 2)
-- then s is covered by the union of the balls centered at u of radius Ξ΅
rcases exists_edist_lt_of_Hausdorff_edist_lt hx Dst with β¨z, hz, Dxzβ©,
rcases mem_bUnion_iff.1 (ut hz) with β¨y, hy, Dzyβ©,
have : edist x y < Ξ΅ := calc
edist x y β€ edist x z + edist z y : edist_triangle _ _ _
... < Ξ΅/2 + Ξ΅/2 : ennreal.add_lt_add Dxz Dzy
... = Ξ΅ : ennreal.add_halves _,
exact mem_bUnion hy this },
end
/-- In a complete space, the type of nonempty compact subsets is complete. This follows
from the same statement for closed subsets -/
instance nonempty_compacts.complete_space [complete_space Ξ±] :
complete_space (nonempty_compacts Ξ±) :=
(complete_space_iff_is_complete_range nonempty_compacts.to_closeds.uniform_embedding).2 $
is_complete_of_is_closed nonempty_compacts.is_closed_in_closeds
/-- In a compact space, the type of nonempty compact subsets is compact. This follows from
the same statement for closed subsets -/
instance nonempty_compacts.compact_space [compact_space Ξ±] : compact_space (nonempty_compacts Ξ±) :=
β¨begin
rw embedding.compact_iff_compact_image nonempty_compacts.to_closeds.uniform_embedding.embedding,
rw [image_univ],
exact nonempty_compacts.is_closed_in_closeds.compact
endβ©
/-- In a second countable space, the type of nonempty compact subsets is second countable -/
instance nonempty_compacts.second_countable_topology [second_countable_topology Ξ±] :
second_countable_topology (nonempty_compacts Ξ±) :=
begin
haveI : separable_space (nonempty_compacts Ξ±) :=
begin
/- To obtain a countable dense subset of `nonempty_compacts Ξ±`, start from
a countable dense subset `s` of Ξ±, and then consider all its finite nonempty subsets.
This set is countable and made of nonempty compact sets. It turns out to be dense:
by total boundedness, any compact set `t` can be covered by finitely many small balls, and
approximations in `s` of the centers of these balls give the required finite approximation
of `t`. -/
have : separable_space Ξ± := by apply_instance,
rcases this.exists_countable_closure_eq_univ with β¨s, cs, s_denseβ©,
let v0 := {t : set Ξ± | finite t β§ t β s},
let v : set (nonempty_compacts Ξ±) := {t : nonempty_compacts Ξ± | t.val β v0},
refine β¨β¨v, β¨_, _β©β©β©,
{ have : countable (subtype.val '' v),
{ refine countable_subset (Ξ»x hx, _) (countable_set_of_finite_subset cs),
rcases (mem_image _ _ _).1 hx with β¨y, β¨hy, yxβ©β©,
rw β yx,
exact hy },
apply countable_of_injective_of_countable_image _ this,
apply subtype.val_injective.inj_on },
{ refine subset.antisymm (subset_univ _) (λt ht, mem_closure_iff.2 (λΡ Ρpos, _)),
-- t is a compact nonempty set, that we have to approximate uniformly by a a set in `v`.
rcases dense Ξ΅pos with β¨Ξ΄, Ξ΄pos, Ξ΄ltβ©,
-- construct a map F associating to a point in Ξ± an approximating point in s, up to Ξ΄/2.
have Exy : βx, βy, y β s β§ edist x y < Ξ΄/2,
{ assume x,
have : x β closure s := by rw s_dense; exact mem_univ _,
rcases mem_closure_iff.1 this (Ξ΄/2) (ennreal.half_pos Ξ΄pos) with β¨y, ys, hyβ©,
exact β¨y, β¨ys, hyβ©β© },
let F := Ξ»x, some (Exy x),
have Fspec : βx, F x β s β§ edist x (F x) < Ξ΄/2 := Ξ»x, some_spec (Exy x),
-- cover `t` with finitely many balls. Their centers form a set `a`
have : totally_bounded t.val := (compact_iff_totally_bounded_complete.1 t.property.2).1,
rcases totally_bounded_iff.1 this (Ξ΄/2) (ennreal.half_pos Ξ΄pos) with β¨a, af, taβ©,
-- a : set Ξ±, af : finite a, ta : t.val β β (y : Ξ±) (H : y β a), eball y (Ξ΄ / 2)
-- replace each center by a nearby approximation in `s`, giving a new set `b`
let b := F '' a,
have : finite b := finite_image _ af,
have tb : βx β t.val, βy β b, edist x y < Ξ΄,
{ assume x hx,
rcases mem_bUnion_iff.1 (ta hx) with β¨z, za, Dxzβ©,
existsi [F z, mem_image_of_mem _ za],
calc edist x (F z) β€ edist x z + edist z (F z) : edist_triangle _ _ _
... < Ξ΄/2 + Ξ΄/2 : ennreal.add_lt_add Dxz (Fspec z).2
... = Ξ΄ : ennreal.add_halves _ },
-- keep only the points in `b` that are close to point in `t`, yielding a new set `c`
let c := {y β b | βxβt.val, edist x y < Ξ΄},
have : finite c := finite_subset βΉfinite bβΊ (Ξ»x hx, hx.1),
-- points in `t` are well approximated by points in `c`
have tc : βx β t.val, βy β c, edist x y β€ Ξ΄,
{ assume x hx,
rcases tb x hx with β¨y, yv, Dxyβ©,
have : y β c := by simp [c, -mem_image]; exact β¨yv, β¨x, hx, Dxyβ©β©,
exact β¨y, this, le_of_lt Dxyβ© },
-- points in `c` are well approximated by points in `t`
have ct : βy β c, βx β t.val, edist y x β€ Ξ΄,
{ rintros y β¨hy1, β¨x, xt, Dyxβ©β©,
have : edist y x β€ Ξ΄ := calc
edist y x = edist x y : edist_comm _ _
... β€ Ξ΄ : le_of_lt Dyx,
exact β¨x, xt, thisβ© },
-- it follows that their Hausdorff distance is small
have : Hausdorff_edist t.val c β€ Ξ΄ :=
Hausdorff_edist_le_of_mem_edist tc ct,
have Dtc : Hausdorff_edist t.val c < Ξ΅ := lt_of_le_of_lt this Ξ΄lt,
-- the set `c` is not empty, as it is well approximated by a nonempty set
have hc : c.nonempty,
from nonempty_of_Hausdorff_edist_ne_top t.property.1 (lattice.ne_top_of_lt Dtc),
-- let `d` be the version of `c` in the type `nonempty_compacts Ξ±`
let d : nonempty_compacts Ξ± := β¨c, β¨hc, βΉfinite cβΊ.compactβ©β©,
have : c β s,
{ assume x hx,
rcases (mem_image _ _ _).1 hx.1 with β¨y, β¨ya, yxβ©β©,
rw β yx,
exact (Fspec y).1 },
have : d β v := β¨βΉfinite cβΊ, thisβ©,
-- we have proved that `d` is a good approximation of `t` as requested
exact β¨d, βΉd β vβΊ, Dtcβ© },
end,
apply second_countable_of_separable,
end
end --section
end emetric --namespace
namespace metric
section
variables {Ξ± : Type u} [metric_space Ξ±]
/-- `nonempty_compacts Ξ±` inherits a metric space structure, as the Hausdorff
edistance between two such sets is finite. -/
instance nonempty_compacts.metric_space : metric_space (nonempty_compacts Ξ±) :=
emetric_space.to_metric_space $ Ξ»x y, Hausdorff_edist_ne_top_of_nonempty_of_bounded x.2.1 y.2.1
(bounded_of_compact x.2.2) (bounded_of_compact y.2.2)
/-- The distance on `nonempty_compacts Ξ±` is the Hausdorff distance, by construction -/
lemma nonempty_compacts.dist_eq {x y : nonempty_compacts Ξ±} :
dist x y = Hausdorff_dist x.val y.val := rfl
lemma lipschitz_inf_dist_set (x : Ξ±) :
lipschitz_with 1 (Ξ» s : nonempty_compacts Ξ±, inf_dist x s.val) :=
lipschitz_with.of_le_add $ assume s t,
by { rw dist_comm,
exact inf_dist_le_inf_dist_add_Hausdorff_dist (edist_ne_top t s) }
lemma lipschitz_inf_dist :
lipschitz_with 2 (Ξ» p : Ξ± Γ (nonempty_compacts Ξ±), inf_dist p.1 p.2.val) :=
@lipschitz_with.uncurry' _ _ _ _ _ _ (Ξ» (x : Ξ±) (s : nonempty_compacts Ξ±), inf_dist x s.val) 1 1
(Ξ» s, lipschitz_inf_dist_pt s.val) lipschitz_inf_dist_set
lemma uniform_continuous_inf_dist_Hausdorff_dist :
uniform_continuous (Ξ»p : Ξ± Γ (nonempty_compacts Ξ±), inf_dist p.1 (p.2).val) :=
lipschitz_inf_dist.uniform_continuous
end --section
end metric --namespace
|
56cac4f3afd7558ab1d3141145e2287adfe88da9 | ae1c30bdeac9e0b1ceea786a6c72c058f01b2eed | /tools/auto/auto.lean | 76ae0abddb051e02b268880424c29b08f272cdb6 | [] | no_license | Kha/library_dev | 7e88c7592b43b939f053dabd708944f22c09627a | 812d543fbf732df80c31d0fdbfae700e6f8535d2 | refs/heads/master | 1,610,378,810,349 | 1,487,673,201,000 | 1,487,673,201,000 | 80,344,419 | 0 | 0 | null | 1,485,692,341,000 | 1,485,692,341,000 | null | UTF-8 | Lean | false | false | 45,989 | lean | /-
Copyright (c) 2016 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad
Automated tableaux reasoners, inspired by the ones in Isabelle.
clarify : applies safe rules only, doesn't split goals
safe : applies safe rules only, though it may split goals
auto : applies safe rules, and then does a backtracking search with unsafe rules.
All can be called in classical or intuitionistic mode, and all can use the simplifier as well
to simplify the goal and hypotheses.
To do:
- use attribute manager to keep track of rules
- use tactics to build introduction and elimination rules automatically
- need version of apply that doesn't instantiate metavariables
- bound split depth in safe, backtracking depth in force, and iterations in clarify
- rules should match goal w/ or w/out reduction to whnf (reducible)
- improve tracing output by keeping track of backtracking depth
- provide better error messages when the name of a theorem doesn't exist; for example,
replace mk_const with mk_const_with_warning (when installing the rule?)
- write a splitter, to help simp
- use a backtracking version of reflexivity
- do more instantiations of quantifiers
- with bintro and belim rules, "num_subgoals" is really "num_branches"
- for intuitionistic logic, add a safe elimination rule for A β B, A, and also for A β¨ B β C,
and similarly for (β x, A x) β C
- in fact, for intuitionistic logic, add the rules for Dyckhoff's LJT
- add safe rules for quantifiers, e.g. β x, P x |- P a
Questions:
- In backtracking search, use better selection of rules, e.g. to chose those with fewest subgoals?
- Should calls to simplifier use clarify as the prover?
- Use more sophisticated handling of quantifiers?
- Should we ever call cases, or induction? Maybe with user hints?
- Better handling of equality? E.g. use symmetry as an elim rule?
- When backtracking, can we intelligently detect that some subgoals can be thrown away?
For example, in the intuitionistic elim rule for A β B, we may end up not using B.
The tactic that handles that can detect this.
- Check and remove duplicate hypotheses?
Note: the rules are not complete for intuitionistic propositional logic, which may require
using an elim rule on A β B multiple times.
-/
open expr tactic list nat
universe uβ
declare_trace auto
set_option trace.auto false
/- logic rules for the tableau prover -/
theorem not_or_of_imp {A B : Prop} (H : A β B) : Β¬ A β¨ B :=
or.elim (classical.em A) (Ξ» H', or.inr (H H')) (Ξ» H', or.inl H')
lemma classical_swap {A : Prop} (B : Prop) (Hβ : Β¬ A) (Hβ : Β¬ B β A) : B :=
classical.by_contradiction (Ξ» H, Hβ (Hβ H))
theorem imp_classical_elim {A B C : Prop} (H : A β B) (Hβ : Β¬ A β C) (Hβ : B β C) : C :=
or.elim (not_or_of_imp H) (Ξ» H', Hβ H') (Ξ» H', Hβ H')
theorem imp_intuit_elim {A B C : Prop} (H : A β B) (Hβ : A) (Hβ : B β C) : C :=
Hβ (H Hβ)
theorem or_classical_intro {A B : Prop} (H : Β¬ A β B) : A β¨ B :=
or.elim (classical.em A) (Ξ» H', or.inl H') (Ξ» H', or.inr (H H'))
theorem iff_elim {A B C : Prop} (H : A β B) (H' : (A β B) β (B β A) β C) : C :=
iff.elim H' H
theorem exists.intro2 {A : Type} {P : A β Prop} (aβ aβ : A) (H : P aβ β¨ P aβ) : β x, P x :=
or.elim H (Ξ» H', exists.intro _ H') (Ξ» H', exists.intro _ H')
theorem forall_elim {A : Type} {P : A β Prop} {C : Prop} (H : β x, P x)
{y : A} (H' : P y β C) : C :=
H' (H y)
theorem forall_elim2 {A : Type} {P : A β Prop} {C : Prop} (H : β x, P x)
{yβ yβ : A} (H' : P yβ β§ P yβ β C) : C :=
H' (and.intro (H yβ) (H yβ))
theorem not_true_elim {C : Prop} (H : Β¬ true) : C := false.elim (H trivial)
theorem not_of_not_or_left {A B : Prop} (H : Β¬ (A β¨ B)) : Β¬ A := Ξ» H', H (or.inl H')
theorem not_of_not_or_right {A B : Prop} (H : Β¬ (A β¨ B)) : Β¬ B := Ξ» H', H (or.inr H')
theorem exists_not_of_not_forall {A : Type} {P : A β Prop} (H : Β¬ β x, P x) : β x, Β¬ P x :=
classical.by_contradiction
(assume H' : Β¬ β x, Β¬ P x,
H (take x, show P x, from classical.by_contradiction (Ξ» H'', H' (exists.intro x H''))))
theorem not_not_dest {A : Prop} (H : Β¬ Β¬ A) : A :=
classical.by_contradiction (Ξ» H', H H')
theorem not_not_not_dest {A : Prop} (H : Β¬ Β¬ Β¬ A) : Β¬ A :=
Ξ» H', H (Ξ» H'', H'' H')
theorem not_not_of_not_imp {A B : Prop} (H : Β¬ (A β B)) : Β¬ Β¬ A :=
Ξ» H', H (Ξ» H'', absurd H'' H')
theorem of_not_imp {A B : Prop} (H : Β¬ (A β B)) : A :=
not_not_dest (not_not_of_not_imp H)
theorem not_of_not_imp {A B : Prop} (H : Β¬ (A β B)) : Β¬ B :=
Ξ» H', H (Ξ» H'', H')
theorem not_or_not_of_not_and {A B : Prop} (H : Β¬ (A β§ B)) : Β¬ A β¨ Β¬ B :=
or.elim (classical.em A)
(Ξ» HA, or.inr (Ξ» HB, H (and.intro HA HB)))
(Ξ» HnA, or.inl HnA)
theorem contrapos {A B : Prop} (H : A β B) : Β¬ B β Β¬ A := Ξ» Hβ Hβ, Hβ (H Hβ)
theorem not_iff {A B : Prop} (H : Β¬ (A β B)) : Β¬ ((A β B) β§ (B β A)) :=
Ξ» H', H ((iff_iff_implies_and_implies A B)^.mpr H')
theorem not_of_imp_false {A : Prop} (H : A β false) : Β¬ A := H
theorem imp_of_or_imp_left {A B C : Prop} (H : A β¨ B β C) : A β C :=
Ξ» H', H (or.inl H')
theorem imp_of_or_imp_right {A B C : Prop} (H : A β¨ B β C) : B β C :=
Ξ» H', H (or.inr H')
namespace tactic
/- utils -/
meta def collect_props : list expr β tactic (list expr)
| [] := return []
| (h :: hs) := do
props β collect_props hs,
ht β infer_type h,
htt β infer_type ht,
(unify htt prop >> return (h :: props)) <|> return props
meta def unfold_all (ns : list name) : tactic unit :=
do dunfold ns, local_context >>= collect_props >>= monad.mapm' (dunfold_at ns)
meta def head_symbol : expr β name
| (const n a) := n
| (app e a) := match (get_app_fn e) with
| (const n l) := n
| a := `none
end
| (pi aβ aβ aβ aβ) := `pi
| a := `none
private meta def whnf_red (e : expr) : tactic expr := whnf e reducible
meta def is_forall (e : expr) : tactic bool :=
if head_symbol e β `pi then return ff
else do
et β infer_type e,
if et β prop then return ff
else do
dt β infer_type (binding_domain e),
if dt β prop then return tt else return ff
meta def is_negation (e : expr) : tactic bool :=
do e' β whnf_red e,
if head_symbol e' = `not then return tt
else if is_pi e' = tt then
(do b' β whnf_red (binding_body e'),
cond (is_false b')
(return tt)
(return ff))
else return ff
meta def at_least_once (t : tactic unit) : tactic unit :=
t >> repeat t
-- assert_fresh P infers the type T of P, creates a fresh name H, and
-- asserts H : T
meta def assert_fresh (P : expr) : tactic unit :=
do n β mk_fresh_name,
t β infer_type P,
assertv n t P
meta def expr_with_type_to_string (h : expr) : tactic string :=
do ht β infer_type h,
pph β pp h,
ppht β pp ht,
return (to_string pph ++ " : " ++ to_string ppht)
/- versions of the simplifier that call themselves recursively -/
-- FIXME(gabriel): figure out how to provide custom tactic to simplifier
meta def simplify_goal' (t : command) (lemmas : list expr) : command :=
simp_using lemmas
meta def simp_core_at' (t : command) (lemmas : list expr) (h : expr) : command :=
simp_at h lemmas
-- simp_add_prove_max_depth l d uses the simplifier as its own prover, recursing up to depth d
meta def simp_add_prove_max_depth (lemmas : list expr) : β β tactic unit
| 0 := failed
| (succ d) := do l β local_context >>= collect_props,
simplify_goal' (simp_add_prove_max_depth d) (l ++ lemmas),
triv
meta def strong_simp_add (lemmas : list expr) : tactic unit :=
do l β local_context >>= collect_props,
simplify_goal' (simp_add_prove_max_depth lemmas 10) (l ++ lemmas),
try triv
meta def strong_simp : tactic unit :=
strong_simp_add []
meta def strong_simp_at_add (h : expr) (lemmas : list expr) : tactic unit :=
do simp_core_at' (simp_add_prove_max_depth lemmas 10) lemmas h
meta def strong_simp_at (h : expr) : tactic unit :=
do strong_simp_at_add h []
-- TODO: how to inline this?
private meta def strong_simp_hyps_add_aux (lemmas : list expr) : list expr β tactic unit
| [] := skip
| (h :: hs) := try (strong_simp_at_add h lemmas) >> strong_simp_hyps_add_aux hs
meta def strong_simp_hyps_add (lemmas : list expr) : tactic unit :=
do l β local_context,
strong_simp_hyps_add_aux lemmas l
meta def strong_simp_hyps : tactic unit :=
strong_simp_hyps_add []
/-
These are for tracing. We use a thunk to avoid computing a string when it is not needed.
-/
-- show a trace message
meta def auto_trace (s : unit β string) : tactic unit :=
if is_trace_enabled_for `auto = tt then trace (s ()) else skip
-- a version where the string is in the tactic monad
meta def auto_traceM (s : unit β tactic string) : tactic unit :=
if is_trace_enabled_for `auto = tt then s () >>= trace else skip
-- trace a step, e.g. an application of a rule, and show the result
meta def auto_trace_step (tac : tactic unit) (s : unit β string) : tactic unit :=
if is_trace_enabled_for `auto = tt then do
trace (s ()),
(tac >> trace ("result:") >> trace_state >> trace "-----") <|>
(trace ("failed:") >> trace_state >> trace "-----" >> failed)
else
tac
-- a version where the string is in the tactic monad
meta def auto_trace_stepM (tac : tactic unit) (s : unit β tactic string) : tactic unit :=
if is_trace_enabled_for `auto = tt then do
s () >>= trace,
(tac >> trace ("result:") >> trace_state >> trace "-----") <|>
(trace ("failed:") >> trace_state >> trace "-----" >> failed)
else
tac
-- this can be used to print a message after a tactic if it fails, e.g. a continuation.
meta def auto_trace_with_fail_message (tac : tactic unit) (s : unit β string) :
tactic unit :=
if is_trace_enabled_for `auto = tt then do
tac <|> (trace (s ()) >> failed)
else
tac
meta def auto_trace_with_fail_messageM (tac : tactic unit) (s : unit β tactic string) :
tactic unit :=
if is_trace_enabled_for `auto = tt then do
tac <|> (s () >>= trace >> failed)
else
tac
/-
Safe versions of some tactics, i.e. tactics that do not instantiate metavariables and
hence can be applied in safe mode.
-/
check unify
-- we really want: eβ and eβ can be unified without instantiating metavariables
meta def unify_safe_core (t : transparency) (eβ eβ : expr) : tactic unit :=
cond (has_meta_var eβ || has_meta_var eβ) failed (unify eβ eβ t)
meta def unify_safe (eβ eβ : expr) : tactic unit :=
unify_safe_core semireducible eβ eβ
check apply
-- we really want: try to apply e, without instantiation any metavariables in the goal
-- maybe we also want the same for fapply?
meta def apply_safe_core (t : transparency) (all : bool) (insts : bool) (e : expr) :
tactic (list expr) :=
apply_core e {md := t, approx := tt, all := all, use_instances := insts }
meta def apply_safe (e : expr) : tactic (list expr) :=
apply_core e {md := semireducible, approx := ff, all := tt}
/- a safe version of assumption -/
meta def find_same_type_safe (e : expr) (l : list expr) : tactic expr :=
first $ list.for l (Ξ» h, do ht β infer_type h, unify_safe e ht >> return h)
meta def find_hyp_with_type (e : expr) : tactic expr :=
local_context >>= find_same_type_safe e
meta def assumption_safe : tactic unit :=
do goal β target,
h β find_hyp_with_type goal,
auto_trace_stepM (exact h)
(Ξ» u, do s β expr_with_type_to_string h,
return ("applying assumption " ++ s))
/- a safe version of contradiction -/
private meta def contra_A_not_A_safe : list expr β list expr β tactic unit
| [] Hs := failed
| (H1 :: Rs) Hs :=
do t_0 β infer_type H1,
t β whnf t_0,
(do a β match_not t,
H2 β find_same_type_safe a Hs,
tgt β target,
pr β mk_app `absurd [tgt, H2, H1],
auto_trace_stepM (exact pr)
(Ξ» u, do s2 β expr_with_type_to_string H2,
s1 β expr_with_type_to_string H1,
return ("using contradiction, " ++ s2 ++ ", " ++ s1)))
<|> contra_A_not_A_safe Rs Hs
meta def contradiction_safe : tactic unit :=
do ctx β local_context, contra_A_not_A_safe ctx ctx
/-
The structure storing a rule has the following data:
key : name := the head symbol that triggers the rule
num_subgoals : nat := number of subgoals introduced
classical : bool := whether to use in classical mode
intuit : bool := whether to use in intuitionistic mode
tac : ... := the tactic used to execute the rule
Notice that all the work is done by tac, which is arbitrary. Helper functions build suitable
tactics in common situations, but users can write more complicated ones. All the other data
is used to find the rules quickly and decide when to apply them.
Currently, the only thing that varies is the type of the tactic, so this is given as a parameter:
intro_rule == tactic unit
elim_rule == expr β tactic unit
bintro_rule == tactic unit β tactic unit
belim_rule == tactic unit β expr β tactic unit
Intro rules are keyed on the head symbol of the goal. Elim rules are keyed on the head symbol of
a hypothesis, and take that hypothesis as an argument. We actually have a separate rule
database for rules where they head symbol is a negation, keyed to the next head symbol.
The intro and elim rules should be safe, which is to say, they can be applied without
backtracking. In the other rules, the letter "b" is for "backtracking." Those rules take
continuations that carry out the rest of the search, so that they can backtrack on failure.
Note that many some elimination rules that would otherwise be safe become unsafe when there are
metavariables involved. For example, applying (or.elim H) is unsafe if H has metavariables; if
those metavariables are not instantiated by the end of the search, then the attempt was
unsuccessful, and needs to be retracted. So there are both safe and unsafe versions of the rule
for or.
-/
structure rule_data (A : Type) :=
(key : name) (num_subgoals : β) (classical : bool) (intuit : bool) (tac : A)
meta def rule_key {A : Type} : rule_data A β name := rule_data.key
meta def intro_rule : Type := rule_data (tactic unit)
meta def elim_rule : Type := rule_data (expr β tactic unit)
meta def bintro_rule : Type := rule_data (tactic unit β tactic unit)
meta def belim_rule : Type := rule_data (expr β tactic unit β tactic unit)
meta def rule_database (A : Type) : Type := rb_lmap name (rule_data A)
meta def intro_rule_database : Type := rb_lmap name intro_rule
meta def elim_rule_database : Type := rb_lmap name elim_rule
meta def nelim_rule_database : Type := rb_lmap name elim_rule
meta def bintro_rule_database : Type := rb_lmap name bintro_rule
meta def belim_rule_database : Type := rb_lmap name belim_rule
meta def bnelim_rule_database : Type := rb_lmap name belim_rule
meta def mk_rule_database (A : Type) : rule_database A := rb_lmap.mk _ _
meta def insert_rule {A : Type} (db : rule_database A) (r : rule_data A) :
rule_database A :=
rb_lmap.insert db (rule_key r) r
meta def insert_rule_list {A : Type} (db : rule_database A) :
list (rule_data A) β rule_database A
| [] := db
| (r :: rs) := insert_rule (insert_rule_list rs) r
meta def initialize_rule_database {A : Type} (l : list (rule_data A)) : rule_database A :=
insert_rule_list (mk_rule_database A) l
meta def find_rules {A : Type} (db : rule_database A) (key : name) : list (rule_data A) :=
rb_lmap.find db key
/- set up attributes -/
meta def intro_rule_database_of_list_name (ns : list name) : tactic (intro_rule_database) :=
do env β get_env,
rule_list β monad.for ns (Ξ» n, do
e β mk_const n,
eval_expr intro_rule e),
return (initialize_rule_database rule_list)
meta def intro_rule_attr : caching_user_attribute (intro_rule_database) :=
{ name := `auto.intro_rule,
descr := "intro rule for tableau provers",
mk_cache := intro_rule_database_of_list_name,
dependencies := [] }
run_command attribute.register ``intro_rule_attr
meta def elim_rule_database_of_list_name (ns : list name) : tactic (elim_rule_database) :=
do env β get_env,
rule_list β monad.for ns (Ξ» n, do
e β mk_const n,
eval_expr elim_rule e),
return (initialize_rule_database rule_list)
meta def elim_rule_attr : caching_user_attribute (elim_rule_database) :=
{ name := `auto.elim_rule,
descr := "elim rule for tableau provers",
mk_cache := elim_rule_database_of_list_name,
dependencies := [] }
run_command attribute.register ``elim_rule_attr
meta def nelim_rule_attr : caching_user_attribute (elim_rule_database) :=
{ name := `auto.nelim_rule,
descr := "negated elim rule for tableau provers",
mk_cache := elim_rule_database_of_list_name,
dependencies := [] }
run_command attribute.register ``nelim_rule_attr
meta def bintro_rule_database_of_list_name (ns : list name) : tactic (bintro_rule_database) :=
do env β get_env,
rule_list β monad.for ns (Ξ» n, do
e β mk_const n,
eval_expr bintro_rule e),
return (initialize_rule_database rule_list)
meta def bintro_rule_attr : caching_user_attribute (bintro_rule_database) :=
{ name := `auto.bintro_rule,
descr := "backtracking intro rule for tableau provers",
mk_cache := bintro_rule_database_of_list_name,
dependencies := [] }
run_command attribute.register ``bintro_rule_attr
meta def belim_rule_database_of_list_name (ns : list name) : tactic (belim_rule_database) :=
do env β get_env,
rule_list β monad.for ns (Ξ» n, do
e β mk_const n,
eval_expr belim_rule e),
return (initialize_rule_database rule_list)
meta def belim_rule_attr : caching_user_attribute (belim_rule_database) :=
{ name := `auto.belim_rule,
descr := "backtracking elim rule for tableau provers",
mk_cache := belim_rule_database_of_list_name,
dependencies := [] }
run_command attribute.register ``belim_rule_attr
meta def bnelim_rule_attr : caching_user_attribute (belim_rule_database) :=
{ name := `auto.bnelim_rule,
descr := "backtracking negated elim rule for tableau provers",
mk_cache := belim_rule_database_of_list_name,
dependencies := [] }
run_command attribute.register ``bnelim_rule_attr
/- intro rules -/
meta def apply_intro_rule (db : intro_rule_database) (max_subgoals : β) (classical : bool) :
tactic unit :=
do goal β target >>= whnf_red,
first $ list.for (find_rules db (head_symbol goal))
(Ξ» r, if rule_data.num_subgoals r β€ max_subgoals β§
cond classical (rule_data.classical r) (rule_data.intuit r) = tt then
rule_data.tac r
else failed)
/- procedures for building particular intro rules -/
meta def deploy_intro (op : name) : tactic unit :=
auto_trace_step (mk_const op >>= apply)
(Ξ» u, "applying introduction " ++ to_string op)
meta def deploy_intro_then_intros (op : name) : tactic unit :=
auto_trace_step (mk_const op >>= apply >> intros >> return ())
(Ξ» u, "applying introduction " ++ to_string op)
/- elim rules -/
meta def apply_elim_rule_at (edb : elim_rule_database) (nedb : nelim_rule_database)
(h : expr) (max_subgoals : β) (classical : bool) :
tactic unit :=
do ht β infer_type h >>= whnf_red,
(first $ list.for (find_rules edb (head_symbol ht))
(Ξ» r, if rule_data.num_subgoals r β€ max_subgoals β§
cond classical (rule_data.classical r) (rule_data.intuit r) = tt then
rule_data.tac r h
else failed)) <|>
if head_symbol ht = `not then do
unneg β return (app_arg ht) >>= whnf_red,
first $ list.for (find_rules nedb (head_symbol (unneg)))
(Ξ» r, if rule_data.num_subgoals r β€ max_subgoals β§
cond classical (rule_data.classical r) (rule_data.intuit r) = tt then
rule_data.tac r h
else failed)
else failed
meta def apply_elim_rule (edb : elim_rule_database) (nedb : nelim_rule_database)
(max_subgoals : β) (classical : bool) :
tactic unit :=
do hs β local_context >>= collect_props,
first $ list.for hs (Ξ» h, apply_elim_rule_at edb nedb h max_subgoals classical)
/- procedures for building particular elim rules
general elimination rules: All the arguments are assumed to be inferrable from the motive and
major premise. The rule is not applied if the hypothesis has metavariables -- backtracking
is needed for that.
destruct rules: This eliminates a hypothesis by applying a single theorem or a list of
theorems in the forward direction. The arguments are assume to be inferrable from the premise.
It is safe even if the hypothesis has variables.
-/
private meta def elim_instance_mapp_args (motive major : β) (emotive emajor : expr) :
list (option expr) :=
let diff := major - motive in
nat.rec_on major []
(Ξ» n l, if n = diff then some emotive :: l
else if n = 0 then some emajor :: l else none :: l)
meta def deploy_elim_at (op : name) (motive : β) (major : β) : expr β tactic unit :=
Ξ» h : expr,
do auto_trace_stepM
(do goal β target,
el β mk_mapp op (elim_instance_mapp_args motive major goal h),
clear h,
apply el ; (intros >> skip),
return ())
(Ξ» u, do s β expr_with_type_to_string h,
return ("applying elimination " ++ to_string op ++ " at " ++ s))
-- only apply the elim rule if there are no metavars
meta def deploy_elim_at_safe (op : name) (motive : β) (major : β) :
expr β tactic unit :=
Ξ» h : expr,
do ht β infer_type h,
when (has_meta_var ht = tt) failed,
deploy_elim_at op motive major h
private meta def dest_instance_mapp_args (prem : β) (hyp : expr) :
list (option expr) :=
nat.rec_on (prem - 1) [some hyp] (Ξ» n l, none :: l)
meta def deploy_dest_at (op : name) (prem : β) : expr β tactic unit :=
Ξ» h : expr,
auto_trace_stepM
(mk_mapp op (dest_instance_mapp_args prem h) >>= assert_fresh >> clear h)
(Ξ» u, do s β expr_with_type_to_string h,
return ("applying destructor " ++ to_string op ++ " at " ++ s))
meta def deploy_dests_at (ops : list (name Γ β)) : expr β tactic unit :=
Ξ» h : expr,
auto_trace_stepM
(monad.for' ops (Ξ» p, mk_mapp (p.1) (dest_instance_mapp_args (p.2) h) >>= assert_fresh) >>
clear h)
(Ξ» u, do s β expr_with_type_to_string h,
return ("applying destructors " ++ (map prod.fst ops)^.to_string ++ " at " ++ s))
meta def deploy_clear_at : expr β tactic unit :=
Ξ» h : expr,
auto_trace_stepM (clear h)
(Ξ» u, do s β expr_with_type_to_string h,
return ("clearing " ++ s))
-- convert (... h : Β¬ A ... ==> B) to (... hn : Β¬ B ... ==> A), where h' has a fresh name
meta def do_classical_swap (h : expr) : tactic expr :=
do goal β target,
mk_mapp `classical_swap [none, some goal, some h] >>= apply,
clear h,
mk_fresh_name >>= intro
meta def classical_apply_intro_rule_at (db : intro_rule_database) (h : expr)
(max_subgoals : β) (classical : bool) :
tactic unit :=
do n β mk_fresh_name,
negated_concl β do_classical_swap h,
apply_intro_rule db max_subgoals classical ;
(intros >> do_classical_swap negated_concl >> skip)
/- backtracking intro rules -/
meta def apply_bintro_rule (db : bintro_rule_database) (max_subgoals : β)
(classical : bool) (cont : tactic unit) :
tactic unit :=
do goal β target >>= whnf_red,
first $ list.for (find_rules db (head_symbol goal))
(Ξ» r, if rule_data.num_subgoals r β€ max_subgoals β§
cond classical (rule_data.classical r) (rule_data.intuit r) = tt then
rule_data.tac r cont
else failed)
/- procedure for building particular bintro rules -/
meta def deploy_bintro_choices (l : list (tactic unit)) : tactic unit β tactic unit :=
take cont, first $ list.for l (Ξ» t, do
auto_trace (Ξ» u, "setting backtracking point for intro rule"),
t,
auto_trace_with_fail_message cont (Ξ» u, "backtracking intro rule"))
/- backtracking elim rules -/
meta def classical_apply_bintro_rule_at (db : bintro_rule_database) (h : expr)
(max_subgoals : β) (classical : bool) (cont : tactic unit) :
tactic unit :=
do n β mk_fresh_name,
negated_concl β do_classical_swap h,
apply_bintro_rule db max_subgoals classical (intros >> do_classical_swap negated_concl >> cont)
meta def apply_belim_rule_at (bedb : belim_rule_database) (bnedb : belim_rule_database)
(h : expr) (max_subgoals : β) (classical : bool) (cont : tactic unit) :
tactic unit :=
do ht β infer_type h >>= whnf_red,
(first $ list.for (find_rules bedb (head_symbol ht))
(Ξ» r, if rule_data.num_subgoals r β€ max_subgoals β§
cond classical (rule_data.classical r) (rule_data.intuit r) = tt then
rule_data.tac r h cont
else failed)) <|>
(monad.cond (is_negation ht)
(do dt β infer_type (binding_domain h),
first $ list.for (find_rules bnedb (head_symbol dt))
(Ξ» r, if rule_data.num_subgoals r β€ max_subgoals β§
cond classical (rule_data.classical r) (rule_data.intuit r) = tt then
rule_data.tac r h cont
else failed))
failed)
meta def apply_belim_rule (bedb : belim_rule_database) (bnedb : belim_rule_database)
(max_subgoals : β) (classical : bool) (cont : tactic unit) :
tactic unit :=
do hs β local_context >>= collect_props,
first (list.for hs (Ξ» h, apply_belim_rule_at bedb bnedb h max_subgoals classical cont))
/- procedure for building particular belim rules -/
meta def deploy_belim_choices (l : list (expr β tactic unit)) :
expr β tactic unit β tactic unit :=
take h cont,
(first $ list.for l (Ξ» t, do
auto_traceM (Ξ» u, do s β expr_with_type_to_string h,
return ("setting backtracking point for elim rule at " ++ s)),
t h,
auto_trace_with_fail_messageM cont
(Ξ» u, do s β expr_with_type_to_string h,
return ("backtracking elim rule at " ++ s))))
/- try to do a subst or injection on a hypothesis -/
meta def has_eq_type (h : expr) : tactic bool :=
do htype β infer_type h >>= whnf_red,
return (match (expr.is_eq htype) with (some _) := tt | none := ff end)
meta def try_subst_and_injection_on_hyps : tactic unit :=
do ctx β local_context,
first $ list.for ctx (Ξ» h, do
b β has_eq_type h,
when (b = ff) failed,
(do subst h,
auto_trace_stepM skip
(Ξ» u, do s β expr_with_type_to_string h,
return ("performing subst with " ++ s)),
clear h) <|>
(do injection h,
auto_trace_stepM skip
(Ξ» u, do s β expr_with_type_to_string h,
return ("performing injection with " ++ s)),
clear h))
/-
Standard rule sets
-/
/- standard introduction rules -/
@[auto.intro_rule]
meta def true_intro_rule : intro_rule :=
{ key := ``true, num_subgoals := 0, classical := tt, intuit := tt,
tac := deploy_intro ``true.intro }
@[auto.intro_rule]
meta def and_intro_rule : intro_rule :=
{ key := ``and, num_subgoals := 2, classical := tt, intuit := tt,
tac := deploy_intro ``and.intro }
@[auto.intro_rule]
meta def or_classical_intro_rule : intro_rule :=
{ key := ``or, num_subgoals := 1, classical := tt, intuit := ff,
tac := deploy_intro_then_intros ``or_classical_intro }
-- TODO: eliminate trick to get the recursive call
private meta def auto_intros_aux : unit β tactic unit
| unit.star :=
do goal β target >>= whnf_red,
when (head_symbol goal = `pi β¨ head_symbol goal = `not)
(do n β mk_fresh_name, intro n, auto_intros_aux unit.star)
meta def auto_intros : tactic unit :=
auto_intros_aux unit.star
meta def deploy_intros : tactic unit :=
auto_trace_step auto_intros (Ξ» u, "applying intros")
@[auto.intro_rule]
meta def Pi_intro_rule : intro_rule :=
{ key := `pi, num_subgoals := 1, classical := tt, intuit := tt,
tac := deploy_intros }
@[auto.intro_rule]
meta def not_intro_rule : intro_rule :=
{ key := ``not, num_subgoals := 1, classical := tt, intuit := tt,
tac := deploy_intros }
@[auto.intro_rule]
meta def iff_intro_rule : intro_rule :=
{ key := ``iff, num_subgoals := 2, classical := tt, intuit := tt,
tac := deploy_intro ``iff.intro }
/- standard backtracking intro rules -/
@[auto.bintro_rule]
meta def or_intuit_bintro_rule : bintro_rule :=
{ key := ``or, num_subgoals := 2, classical := ff, intuit := tt,
tac := deploy_bintro_choices [deploy_intro ``or.inl, deploy_intro ``or.inr] }
@[auto.bintro_rule]
meta def exists_bintro_rule : bintro_rule :=
{ key := ``Exists, num_subgoals := 2, classical := tt, intuit := tt,
tac := deploy_bintro_choices [deploy_intro ``exists.intro, deploy_intro ``false.elim] }
/- standard elimination rules -/
@[auto.elim_rule]
meta def and_elim_rule : elim_rule :=
{ key := ``and, num_subgoals := 1, classical := tt, intuit := tt,
tac := deploy_dests_at [(``and.left, 3), (``and.right, 3)] }
@[auto.elim_rule]
meta def iff_elim_rule : elim_rule :=
{ key := ``iff, num_subgoals := 1, classical := tt, intuit := tt,
tac := deploy_dests_at [(``iff.mp, 3), (``iff.mpr, 3)] }
@[auto.elim_rule]
meta def or_elim_rule : elim_rule :=
{ key := ``or, num_subgoals := 2, classical := tt, intuit := tt,
tac := deploy_elim_at_safe ``or.elim 3 4 }
@[auto.elim_rule]
meta def false_elim_rule : elim_rule :=
{ key := ``false, num_subgoals := 0, classical := tt, intuit := tt,
tac := deploy_elim_at ``false.elim 1 2 }
@[auto.elim_rule]
meta def exists_elim_rule : elim_rule :=
{ key := ``Exists, num_subgoals := 1, classical := tt, intuit := tt,
tac := deploy_elim_at_safe ``exists.elim 3 4 }
-- given a hypothesis h with type ht, an implication, try to find something
-- in the context to apply it to
meta def try_modus_ponens_at (h : expr) (ht : expr) : tactic unit :=
do h' β find_hyp_with_type (binding_domain ht),
auto_trace_stepM
(assert_fresh (expr.app h h') >> clear h)
(Ξ» u, do sβ β expr_with_type_to_string h,
sβ β expr_with_type_to_string h',
return ("applying " ++ sβ ++ " to " ++ sβ))
-- if h is of the form A β B:
-- if B = false, replace by h' : Β¬ A
-- if h' : A is in the context, apply h to h'
-- if A if of the form C β¨ D, replace with C β B, D β B
meta def deploy_imp_elim_at (h : expr) : tactic unit :=
do ht β infer_type h >>= whnf_red,
dt β infer_type (binding_domain ht),
if dt β prop then failed
else do
conc β return (binding_body ht) >>= whnf_red,
if head_symbol conc = ``false then
deploy_dest_at ``not_of_imp_false 2 h
else try_modus_ponens_at h ht <|>
(do hyp β return (binding_domain ht) >>= whnf_red,
if head_symbol hyp = `or then
deploy_dests_at [(``imp_of_or_imp_left, 4), (``imp_of_or_imp_right, 4)] h
else failed)
@[auto.elim_rule]
meta def imp_elim_rule : elim_rule :=
{ key := `pi, num_subgoals := 1, classical := tt, intuit := tt,
tac := deploy_imp_elim_at }
meta def deploy_imp_classical_elim_at (h : expr) : tactic unit :=
do ht β infer_type h >>= whnf_red,
dt β infer_type (binding_domain ht),
if dt β prop then failed
else monad.cond (is_negation ht)
failed
(deploy_elim_at ``imp_classical_elim 3 4 h)
@[auto.elim_rule]
meta def imp_classical_elim_rule : elim_rule :=
{ key := `pi, num_subgoals := 2, classical := tt, intuit := ff,
tac := deploy_imp_classical_elim_at }
-- try to find a contradiction
meta def deploy_not_elim_at (h : expr) : tactic unit :=
do ht β infer_type h,
h' β find_hyp_with_type (app_arg ht),
goal β target,
t β mk_app `absurd [goal, h', h],
auto_trace_stepM
(exact t)
(Ξ» u, do sβ β expr_with_type_to_string h',
sβ β expr_with_type_to_string h,
return ("using contradiction, " ++ sβ ++ " and " ++ sβ))
@[auto.elim_rule]
meta def not_elim_rule : elim_rule :=
{ key := `not, num_subgoals := 0, classical := tt, intuit := tt,
tac := deploy_not_elim_at }
/- elimination rules for negated formulas -/
@[auto.nelim_rule]
meta def not_true_elim_rule : elim_rule :=
{ key := ``true, num_subgoals := 0, classical := tt, intuit := tt,
tac := deploy_elim_at ``not_true_elim 1 2 }
@[auto.nelim_rule]
meta def not_or_elim_rule : elim_rule :=
{ key := ``or, num_subgoals := 1, classical := tt, intuit := tt,
tac := deploy_dests_at [(``not_of_not_or_left, 3), (``not_of_not_or_right, 3)] }
@[auto.nelim_rule]
meta def not_and_elim_rule : elim_rule :=
{ key := ``and, num_subgoals := 1, classical := tt, intuit := ff,
tac := deploy_dest_at ``not_or_not_of_not_and 3 }
@[auto.nelim_rule]
meta def not_imp_elim_rule : elim_rule :=
{ key := `pi, num_subgoals := 1, classical := tt, intuit := ff,
tac := deploy_dests_at [(``of_not_imp, 3), (``not_of_not_imp, 3)] }
@[auto.nelim_rule]
meta def not_not_elim_rule : elim_rule :=
{ key := ``not, num_subgoals := 1, classical := tt, intuit := ff,
tac := deploy_dest_at ``not_not_dest 2 }
@[auto.nelim_rule]
meta def not_not_not_elim_rule : elim_rule :=
{ key := ``not, num_subgoals := 1, classical := ff, intuit := tt,
tac := deploy_dest_at ``not_not_not_dest 2 }
@[auto.nelim_rule]
meta def not_iff_elim_rule : elim_rule :=
{ key := ``iff, num_subgoals := 1, classical := tt, intuit := tt,
tac := deploy_dest_at ``not_iff 3 }
@[auto.nelim_rule]
meta def not_exists_elim_rule : elim_rule :=
{ key := ``Exists, num_subgoals := 1, classical := tt, intuit := tt,
tac := deploy_dest_at ``forall_not_of_not_exists 3 }
/- standard backtracking elim rules -/
meta def deploy_imp_intuit_belim_at (h : expr) (cont : tactic unit) : tactic unit :=
do ht β infer_type h >>= whnf_red,
dt β infer_type (binding_domain ht),
if dt β prop then failed
else deploy_belim_choices [deploy_elim_at ``imp_intuit_elim 3 4, deploy_clear_at] h cont
@[auto.belim_rule]
meta def imp_intuit_belim_rule : belim_rule :=
{ key := `pi, num_subgoals := 2, classical := ff, intuit := tt,
tac := deploy_imp_intuit_belim_at }
--meta def imp_classical_belim_rule :=
--{ key := `pi, num_subgoals := 2, classical := tt, intuit := ff,
-- tac := deploy_belim_choices [deploy_clear_at, deploy_imp_classical_elim_at] }
@[auto.belim_rule]
meta def or_belim_rule : belim_rule :=
{ key := `or, num_subgoals := 2, classical := ff, intuit := tt,
tac := deploy_belim_choices [deploy_clear_at, deploy_elim_at ``or.elim 3 4] }
/- standard backtracking negated elim rules -/
-- none yet
/- backtracking assumption tactic -/
meta def try_assumptions (cont : tactic unit) :=
do ctx β local_context,
goal β target,
first $ list.for ctx
(Ξ» h, do ht β infer_type h,
unify ht goal,
auto_trace_stepM (exact h)
(Ξ» u, do s β expr_with_type_to_string h,
return ("try applying assumption " ++ s)),
auto_trace_with_fail_messageM cont
(Ξ» u, do s β expr_with_type_to_string h,
return ("backtracking assumption " ++ s)))
meta def try_contradictions (cont : tactic unit) :=
do ctx β local_context,
goal β target,
first $ list.for ctx (Ξ» h, do
ht β infer_type h,
unneg_ht β match_not ht,
first $ list.for ctx (Ξ» h', do
ht' β infer_type h',
unify ht' unneg_ht,
t β mk_app ``absurd [goal, h', h],
auto_trace_stepM (exact t)
(Ξ» u, do sβ β expr_with_type_to_string h',
sβ β expr_with_type_to_string h,
return ("try using contradiction " ++ sβ ++ ", " ++ sβ)),
auto_trace_with_fail_messageM cont
(Ξ» u, do sβ β expr_with_type_to_string h',
sβ β expr_with_type_to_string h,
return ("backtracking contradiction " ++ sβ ++ ", " ++ sβ))))
/- instantiating quantifiers in the backtracking search -/
meta def has_forall_type (h : expr) : tactic bool :=
do ht β infer_type h,
is_forall ht
-- TODO: eliminate
meta def apply_to_metavars_while_universal_aux : unit β expr β tactic expr
| unit.star h :=
do ht β infer_type h,
if head_symbol ht β `pi then return h
else do
htt β infer_type ht,
if htt β prop then return h
else do
dt β infer_type (binding_domain ht),
if dt = prop then return h
else do
v β mk_meta_var (binding_domain ht),
apply_to_metavars_while_universal_aux unit.star (expr.app h v)
meta def apply_to_metavars_while_universal (h : expr) : tactic expr :=
apply_to_metavars_while_universal_aux unit.star h
meta def try_instantiate_quantifiers (cont : tactic unit) : tactic unit :=
do hs β (local_context >>= monad.filter has_forall_type),
gt β target,
when (hs = []/- β§ head_symbol gt β `Exists-/) failed,
monad.for' hs
(Ξ» h, do h' β apply_to_metavars_while_universal h,
assert_fresh h'),
-- when (head_symbol gt = `Exists) split,
monad.for' hs clear,
monad.whenb (is_trace_enabled_for `auto)
(trace "instantiating quantifiers" >> trace_state >> trace "-----"),
cont
/-
Safe automation. These operate on the first goal, and apply only safe rules (the new
state is logically equivalent to the original one). They make whatever progress they
can, and leave the user with zero or more goals.
They take the following arguments:
classical : classical or intuitionistic
use_simp : whether to use the simplifier
simp_lemmas : in the latter case, extra lemmas to use
-/
-- perform safe rules that do not split the goal
meta def clarify_core (classical : bool) (use_simp : bool)
(idb : intro_rule_database) (edb : elim_rule_database) (nedb : nelim_rule_database)
(simp_lemmas : list expr) :
tactic unit :=
do repeat (apply_intro_rule idb 1 classical),
repeat (apply_elim_rule edb nedb 1 classical),
repeat try_subst_and_injection_on_hyps,
(now <|> assumption_safe <|> -- contradiction_safe <|>
when (use_simp = tt)
(do when_tracing `auto (trace "calling simplifier"),
try (strong_simp_hyps_add simp_lemmas),
try (strong_simp_add simp_lemmas),
try (now <|> assumption_safe)))
-- perform safe rules
-- TODO: fix recursion
meta def safe_core (classical : bool) (use_simp : bool)
(idb : intro_rule_database) (edb : elim_rule_database) (nedb : nelim_rule_database)
(simp_lemmas : list expr) :
unit β tactic unit
| unit.star :=
do clarify_core classical use_simp idb edb nedb simp_lemmas,
now <|>
try ((apply_intro_rule idb 10 classical <|> apply_elim_rule edb nedb 10 classical) ;
(safe_core /- classical use_simp idb edb nedb simp_lemmas -/ unit.star))
/-
The backtracking tableau prover.
The function force_all_core_aux is the heart of the tableau prover. It takes a list of goals,
which are assumed to have been processed with the safe rules already and are not visible on the
goal stack. It applies safe rules to the goals in the current goal list (if any),
and then starts calling backtracking rules.
This function is meant to be passed as a continuation to the backtracking tactics, which are
called with a single goal. The tactics should operate on the goal, resulting in a new goal
list. They should then call the continuation to finish the backtracking search.
The function succeeds if all the goals are ultimately proved, and it fails otherwise.
-/
-- TODO: fix recursion
meta def force_all_core_aux (classical : bool) (use_simp : bool)
(idb : intro_rule_database) (edb : elim_rule_database) (nedb : elim_rule_database)
(bidb : bintro_rule_database) (bedb : belim_rule_database) (bnedb : belim_rule_database)
(simp_lemmas : list expr)
(final_check : tactic unit) /- (preprocessed_goals : list expr) -/ :
unit β list expr β tactic unit
| unit.star :=
let force_core_rec := force_all_core_aux /- classical use_simp idb edb nedb bidb bedb bnedb
simp_lemmas final_check -/ unit.star in
let process_goals_with_backtracking : list expr β tactic unit :=
Ξ» l, match l with
| [] := final_check -- if it passes, we have success!
| (g :: gs) :=
do {set_goals [g],
try_assumptions (force_core_rec gs) <|>
try_contradictions (force_core_rec gs) <|>
try_instantiate_quantifiers (force_core_rec gs) <|>
apply_bintro_rule bidb 10 classical (force_core_rec gs) <|>
apply_belim_rule bedb bnedb 10 classical (force_core_rec gs)}
end in
Ξ» preprocessed_goals,
do n β num_goals,
if n β 0 then do
all_goals (safe_core classical use_simp idb edb nedb simp_lemmas unit.star),
gs β get_goals,
set_goals [],
force_all_core_aux /-classical use_simp idb edb nedb bidb bedb bnedb simp_lemmas final_check-/
unit.star (gs ++ preprocessed_goals)
else do
process_goals_with_backtracking preprocessed_goals
meta def final_check_for_metavariables (g : expr) : tactic unit :=
do result β instantiate_mvars g,
monad.whenb (has_meta_var result)
(when_tracing `auto (trace_state >> trace "result has metavariables:" >> trace result) >>
failed)
-- the main tableaux prover: acts on one goal, makes sure there are no metavariables at the end
meta def force_core (classical : bool) (use_simp : bool)
(idb : intro_rule_database) (edb : elim_rule_database) (nedb : elim_rule_database)
(bidb : bintro_rule_database) (bedb : belim_rule_database) (bnedb : belim_rule_database)
(simp_lemmas : list expr) :
tactic unit :=
do auto_trace_step skip (Ξ» u, ">>> entering force"),
gs β get_goals,
match gs with
| [] := fail "force failed, there are no goals"
| (g :: gs') := do set_goals [g],
force_all_core_aux classical use_simp idb edb nedb bidb bedb bnedb simp_lemmas
(final_check_for_metavariables g) unit.star [],
set_goals gs
end
/- front ends -/
/-
-- TODO: a temporary hack: using trace_option to declare a boolean option
declare_trace auto.classical
set_option trace.auto.classical true
declare_trace auto.use_simp
set_option trace.auto.use_simp false
-/
-- applies to first goal, never splits it, applies only safe rules, always succeeds
meta def clarify (classical : bool) (use_simp : bool)
(irules : list intro_rule) (erules : list elim_rule) (nerules : list elim_rule)
(simp_lemmas : list expr) :
tactic unit :=
do idb β caching_user_attribute.get_cache intro_rule_attr,
edb β caching_user_attribute.get_cache elim_rule_attr,
nedb β caching_user_attribute.get_cache nelim_rule_attr,
clarify_core classical use_simp idb edb nedb simp_lemmas
-- applies to first goal, applies only safe rules, always succeeds
meta def safe (classical : bool) (use_simp : bool)
(irules : list intro_rule) (erules : list elim_rule) (nerules : list elim_rule)
(simp_lemmas : list expr) :
tactic unit :=
do idb β caching_user_attribute.get_cache intro_rule_attr,
edb β caching_user_attribute.get_cache elim_rule_attr,
nedb β caching_user_attribute.get_cache nelim_rule_attr,
safe_core classical use_simp idb edb nedb simp_lemmas unit.star
-- applies safe to all goals
meta def safe_all (classical : bool) (use_simp : bool)
(irules : list intro_rule) (erules : list elim_rule) (nerules : list elim_rule)
(simp_lemmas : list expr) :
tactic unit :=
all_goals (safe classical use_simp irules erules nerules simp_lemmas)
-- applies to first goal, fails if it does not solve it
meta def force (classical : bool) (use_simp : bool)
(irules : list intro_rule) (erules : list elim_rule) (nerules : list elim_rule)
(birules : list bintro_rule) (berules : list belim_rule) (bnerules : list belim_rule)
(simp_lemmas : list expr) :
tactic unit :=
do idb β caching_user_attribute.get_cache intro_rule_attr,
edb β caching_user_attribute.get_cache elim_rule_attr,
nedb β caching_user_attribute.get_cache nelim_rule_attr,
bidb β caching_user_attribute.get_cache bintro_rule_attr,
bedb β caching_user_attribute.get_cache belim_rule_attr,
bnedb β caching_user_attribute.get_cache bnelim_rule_attr,
force_core classical use_simp idb edb nedb bidb bedb bnedb simp_lemmas
-- applies to all goals, always succeeds
meta def auto (classical : bool) (use_simp : bool)
(irules : list intro_rule) (erules : list elim_rule) (nerules : list elim_rule)
(birules : list bintro_rule) (berules : list belim_rule) (bnerules : list belim_rule)
(simp_lemmas : list expr) :
tactic unit :=
safe_all classical use_simp irules erules nerules simp_lemmas >>
all_goals
(try (force classical use_simp irules erules nerules birules berules bnerules simp_lemmas))
/- for testing -/
meta def clarify' : tactic unit := clarify tt ff [] [] [] []
meta def safe' : tactic unit := safe tt ff [] [] [] []
meta def ssafe' : tactic unit := safe tt tt [] [] [] [] -- with simplification
meta def force' : tactic unit := force tt ff [] [] [] [] [] [] []
meta def sforce' : tactic unit := force tt tt [] [] [] [] [] [] []
meta def auto' : tactic unit := auto tt ff [] [] [] [] [] [] []
meta def sauto' : tactic unit := auto tt tt [] [] [] [] [] [] []
meta def iauto' : tactic unit := auto ff ff [] [] [] [] [] [] []
meta def isauto' : tactic unit := auto ff tt [] [] [] [] [] [] []
end tactic
|
1811240e784f78bd5bdebf897ca6175797e0b88e | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/run/classAbbrev.lean | 6e209b8222159de3195c27c8c284d4df9e781e34 | [
"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 | 332 | lean | class Refl.{U} {Ξ± : Type U} (R : Ξ± β Ξ± β Prop) : Prop :=
refl (a : Ξ±) : R a a
class Symm.{U} {Ξ± : Type U} (R : Ξ± β Ξ± β Prop) : Prop :=
symm {a b : Ξ±} : R a b β R b a
/--
An example decl modifier (a doc comment).
-/
class abbrev PEquiv.{U} {Ξ± : Type U} (R : Ξ± β Ξ± β Prop) : Prop
:= Refl R, Symm R
|
dc250761133206b8478d24108edb54365c088066 | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/algebra/big_operators/order.lean | 68bb20bb7cc68509de5dc48e4cdf64f8fff471a3 | [
"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,603 | lean | /-
Copyright (c) 2017 Johannes HΓΆlzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes HΓΆlzl
-/
import algebra.order.absolute_value
import algebra.big_operators.basic
/-!
# Results about big operators with values in an ordered algebraic structure.
Mostly monotonicity results for the `β` and `β` operations.
-/
open function
open_locale big_operators
variables {ΞΉ Ξ± Ξ² M N G k R : Type*}
namespace finset
section ordered_comm_monoid
variables [comm_monoid M] [ordered_comm_monoid N]
/-- Let `{x | p x}` be a subsemigroup of a commutative monoid `M`. Let `f : M β N` be a map
submultiplicative on `{x | p x}`, i.e., `p x β p y β f (x * y) β€ f x * f y`. Let `g i`, `i β s`, be
a nonempty finite family of elements of `M` such that `β i β s, p (g i)`. Then
`f (β x in s, g x) β€ β x in s, f (g x)`. -/
@[to_additive le_sum_nonempty_of_subadditive_on_pred]
lemma le_prod_nonempty_of_submultiplicative_on_pred
(f : M β N) (p : M β Prop) (h_mul : β x y, p x β p y β f (x * y) β€ f x * f y)
(hp_mul : β x y, p x β p y β p (x * y)) (g : ΞΉ β M) (s : finset ΞΉ) (hs_nonempty : s.nonempty)
(hs : β i β s, p (g i)) :
f (β i in s, g i) β€ β i in s, f (g i) :=
begin
refine le_trans (multiset.le_prod_nonempty_of_submultiplicative_on_pred f p h_mul hp_mul _ _ _) _,
{ simp [hs_nonempty.ne_empty], },
{ exact multiset.forall_mem_map_iff.mpr hs, },
rw multiset.map_map,
refl,
end
/-- Let `{x | p x}` be an additive subsemigroup of an additive commutative monoid `M`. Let
`f : M β N` be a map subadditive on `{x | p x}`, i.e., `p x β p y β f (x + y) β€ f x + f y`. Let
`g i`, `i β s`, be a nonempty finite family of elements of `M` such that `β i β s, p (g i)`. Then
`f (β i in s, g i) β€ β i in s, f (g i)`. -/
add_decl_doc le_sum_nonempty_of_subadditive_on_pred
/-- If `f : M β N` is a submultiplicative function, `f (x * y) β€ f x * f y` and `g i`, `i β s`, is a
nonempty finite family of elements of `M`, then `f (β i in s, g i) β€ β i in s, f (g i)`. -/
@[to_additive le_sum_nonempty_of_subadditive]
lemma le_prod_nonempty_of_submultiplicative
(f : M β N) (h_mul : β x y, f (x * y) β€ f x * f y) {s : finset ΞΉ} (hs : s.nonempty) (g : ΞΉ β M) :
f (β i in s, g i) β€ β i in s, f (g i) :=
le_prod_nonempty_of_submultiplicative_on_pred f (Ξ» i, true) (Ξ» x y _ _, h_mul x y)
(Ξ» _ _ _ _, trivial) g s hs (Ξ» _ _, trivial)
/-- If `f : M β N` is a subadditive function, `f (x + y) β€ f x + f y` and `g i`, `i β s`, is a
nonempty finite family of elements of `M`, then `f (β i in s, g i) β€ β i in s, f (g i)`. -/
add_decl_doc le_sum_nonempty_of_subadditive
/-- Let `{x | p x}` be a subsemigroup of a commutative monoid `M`. Let `f : M β N` be a map
such that `f 1 = 1` and `f` is submultiplicative on `{x | p x}`, i.e.,
`p x β p y β f (x * y) β€ f x * f y`. Let `g i`, `i β s`, be a finite family of elements of `M` such
that `β i β s, p (g i)`. Then `f (β i in s, g i) β€ β i in s, f (g i)`. -/
@[to_additive le_sum_of_subadditive_on_pred]
lemma le_prod_of_submultiplicative_on_pred (f : M β N) (p : M β Prop) (h_one : f 1 = 1)
(h_mul : β x y, p x β p y β f (x * y) β€ f x * f y)
(hp_mul : β x y, p x β p y β p (x * y)) (g : ΞΉ β M) {s : finset ΞΉ} (hs : β i β s, p (g i)) :
f (β i in s, g i) β€ β i in s, f (g i) :=
begin
rcases eq_empty_or_nonempty s with rfl|hs_nonempty,
{ simp [h_one] },
{ exact le_prod_nonempty_of_submultiplicative_on_pred f p h_mul hp_mul g s hs_nonempty hs, },
end
/-- Let `{x | p x}` be a subsemigroup of a commutative additive monoid `M`. Let `f : M β N` be a map
such that `f 0 = 0` and `f` is subadditive on `{x | p x}`, i.e. `p x β p y β f (x + y) β€ f x + f y`.
Let `g i`, `i β s`, be a finite family of elements of `M` such that `β i β s, p (g i)`. Then
`f (β x in s, g x) β€ β x in s, f (g x)`. -/
add_decl_doc le_sum_of_subadditive_on_pred
/-- If `f : M β N` is a submultiplicative function, `f (x * y) β€ f x * f y`, `f 1 = 1`, and `g i`,
`i β s`, is a finite family of elements of `M`, then `f (β i in s, g i) β€ β i in s, f (g i)`. -/
@[to_additive le_sum_of_subadditive]
lemma le_prod_of_submultiplicative (f : M β N) (h_one : f 1 = 1)
(h_mul : β x y, f (x * y) β€ f x * f y) (s : finset ΞΉ) (g : ΞΉ β M) :
f (β i in s, g i) β€ β i in s, f (g i) :=
begin
refine le_trans (multiset.le_prod_of_submultiplicative f h_one h_mul _) _,
rw multiset.map_map,
refl,
end
/-- If `f : M β N` is a subadditive function, `f (x + y) β€ f x + f y`, `f 0 = 0`, and `g i`,
`i β s`, is a finite family of elements of `M`, then `f (β i in s, g i) β€ β i in s, f (g i)`. -/
add_decl_doc le_sum_of_subadditive
variables {f g : ΞΉ β N} {s t : finset ΞΉ}
/-- In an ordered commutative monoid, if each factor `f i` of one finite product is less than or
equal to the corresponding factor `g i` of another finite product, then
`β i in s, f i β€ β i in s, g i`. -/
@[to_additive sum_le_sum]
lemma prod_le_prod'' (h : β i β s, f i β€ g i) : β i in s, f i β€ β i in s, g i :=
multiset.prod_map_le_prod_map f g h
/-- In an ordered additive commutative monoid, if each summand `f i` of one finite sum is less than
or equal to the corresponding summand `g i` of another finite sum, then
`β i in s, f i β€ β i in s, g i`. -/
add_decl_doc sum_le_sum
@[to_additive sum_nonneg] lemma one_le_prod' (h : βi β s, 1 β€ f i) : 1 β€ (β i in s, f i) :=
le_trans (by rw prod_const_one) (prod_le_prod'' h)
@[to_additive finset.sum_nonneg']
lemma one_le_prod'' (h : β (i : ΞΉ), 1 β€ f i) : 1 β€ β (i : ΞΉ) in s, f i :=
finset.one_le_prod' (Ξ» i hi, h i)
@[to_additive sum_nonpos] lemma prod_le_one' (h : βi β s, f i β€ 1) : (β i in s, f i) β€ 1 :=
(prod_le_prod'' h).trans_eq (by rw prod_const_one)
@[to_additive sum_le_sum_of_subset_of_nonneg]
lemma prod_le_prod_of_subset_of_one_le' (h : s β t) (hf : β i β t, i β s β 1 β€ f i) :
β i in s, f i β€ β i in t, f i :=
by classical;
calc (β i in s, f i) β€ (β i in t \ s, f i) * (β i in s, f i) :
le_mul_of_one_le_left' $ one_le_prod' $ by simpa only [mem_sdiff, and_imp]
... = β i in t \ s βͺ s, f i : (prod_union sdiff_disjoint).symm
... = β i in t, f i : by rw [sdiff_union_of_subset h]
@[to_additive sum_mono_set_of_nonneg]
lemma prod_mono_set_of_one_le' (hf : β x, 1 β€ f x) : monotone (Ξ» s, β x in s, f x) :=
Ξ» s t hst, prod_le_prod_of_subset_of_one_le' hst $ Ξ» x _ _, hf x
@[to_additive sum_le_univ_sum_of_nonneg]
lemma prod_le_univ_prod_of_one_le' [fintype ΞΉ] {s : finset ΞΉ} (w : β x, 1 β€ f x) :
β x in s, f x β€ β x, f x :=
prod_le_prod_of_subset_of_one_le' (subset_univ s) (Ξ» a _ _, w a)
@[to_additive sum_eq_zero_iff_of_nonneg]
lemma prod_eq_one_iff_of_one_le' : (β i β s, 1 β€ f i) β (β i in s, f i = 1 β β i β s, f i = 1) :=
begin
classical,
apply finset.induction_on s,
exact Ξ» _, β¨Ξ» _ _, false.elim, Ξ» _, rflβ©,
assume a s ha ih H,
have : β i β s, 1 β€ f i, from Ξ» _, H _ β mem_insert_of_mem,
rw [prod_insert ha, mul_eq_one_iff' (H _ $ mem_insert_self _ _) (one_le_prod' this),
forall_mem_insert, ih this]
end
@[to_additive sum_eq_zero_iff_of_nonneg]
lemma prod_eq_one_iff_of_le_one' : (β i β s, f i β€ 1) β (β i in s, f i = 1 β β i β s, f i = 1) :=
@prod_eq_one_iff_of_one_le' _ Nα΅α΅ _ _ _
@[to_additive single_le_sum]
lemma single_le_prod' (hf : β i β s, 1 β€ f i) {a} (h : a β s) : f a β€ (β x in s, f x) :=
calc f a = β i in {a}, f i : prod_singleton.symm
... β€ β i in s, f i :
prod_le_prod_of_subset_of_one_le' (singleton_subset_iff.2 h) $ Ξ» i hi _, hf i hi
@[to_additive sum_le_card_nsmul]
lemma prod_le_pow_card (s : finset ΞΉ) (f : ΞΉ β N) (n : N) (h : β x β s, f x β€ n) :
s.prod f β€ n ^ s.card :=
begin
refine (multiset.prod_le_pow_card (s.val.map f) n _).trans _,
{ simpa using h },
{ simpa }
end
@[to_additive card_nsmul_le_sum]
lemma pow_card_le_prod (s : finset ΞΉ) (f : ΞΉ β N) (n : N) (h : β x β s, n β€ f x) :
n ^ s.card β€ s.prod f :=
@finset.prod_le_pow_card _ Nα΅α΅ _ _ _ _ h
lemma card_bUnion_le_card_mul [decidable_eq Ξ²] (s : finset ΞΉ) (f : ΞΉ β finset Ξ²) (n : β)
(h : β a β s, (f a).card β€ n) :
(s.bUnion f).card β€ s.card * n :=
card_bUnion_le.trans $ sum_le_card_nsmul _ _ _ h
variables {ΞΉ' : Type*} [decidable_eq ΞΉ']
@[to_additive sum_fiberwise_le_sum_of_sum_fiber_nonneg]
lemma prod_fiberwise_le_prod_of_one_le_prod_fiber' {t : finset ΞΉ'}
{g : ΞΉ β ΞΉ'} {f : ΞΉ β N} (h : β y β t, (1 : N) β€ β x in s.filter (Ξ» x, g x = y), f x) :
β y in t, β x in s.filter (Ξ» x, g x = y), f x β€ β x in s, f x :=
calc (β y in t, β x in s.filter (Ξ» x, g x = y), f x) β€
(β y in t βͺ s.image g, β x in s.filter (Ξ» x, g x = y), f x) :
prod_le_prod_of_subset_of_one_le' (subset_union_left _ _) $ Ξ» y hyts, h y
... = β x in s, f x :
prod_fiberwise_of_maps_to (Ξ» x hx, mem_union.2 $ or.inr $ mem_image_of_mem _ hx) _
@[to_additive sum_le_sum_fiberwise_of_sum_fiber_nonpos]
lemma prod_le_prod_fiberwise_of_prod_fiber_le_one' {t : finset ΞΉ'}
{g : ΞΉ β ΞΉ'} {f : ΞΉ β N} (h : β y β t, (β x in s.filter (Ξ» x, g x = y), f x) β€ 1) :
(β x in s, f x) β€ β y in t, β x in s.filter (Ξ» x, g x = y), f x :=
@prod_fiberwise_le_prod_of_one_le_prod_fiber' _ Nα΅α΅ _ _ _ _ _ _ _ h
end ordered_comm_monoid
lemma abs_sum_le_sum_abs {G : Type*} [linear_ordered_add_comm_group G] (f : ΞΉ β G) (s : finset ΞΉ) :
|β i in s, f i| β€ β i in s, |f i| :=
le_sum_of_subadditive _ abs_zero abs_add s f
lemma abs_sum_of_nonneg {G : Type*} [linear_ordered_add_comm_group G] {f : ΞΉ β G} {s : finset ΞΉ}
(hf : β i β s, 0 β€ f i) :
|β (i : ΞΉ) in s, f i| = β (i : ΞΉ) in s, f i :=
by rw abs_of_nonneg (finset.sum_nonneg hf)
lemma abs_sum_of_nonneg' {G : Type*} [linear_ordered_add_comm_group G] {f : ΞΉ β G} {s : finset ΞΉ}
(hf : β i, 0 β€ f i) :
|β (i : ΞΉ) in s, f i| = β (i : ΞΉ) in s, f i :=
by rw abs_of_nonneg (finset.sum_nonneg' hf)
lemma abs_prod {R : Type*} [linear_ordered_comm_ring R] {f : ΞΉ β R} {s : finset ΞΉ} :
|β x in s, f x| = β x in s, |f x| :=
(abs_hom.to_monoid_hom : R β* R).map_prod _ _
section pigeonhole
variable [decidable_eq Ξ²]
theorem card_le_mul_card_image_of_maps_to {f : Ξ± β Ξ²} {s : finset Ξ±} {t : finset Ξ²}
(Hf : β a β s, f a β t) (n : β) (hn : β a β t, (s.filter (Ξ» x, f x = a)).card β€ n) :
s.card β€ n * t.card :=
calc s.card = (β a in t, (s.filter (Ξ» x, f x = a)).card) : card_eq_sum_card_fiberwise Hf
... β€ (β _ in t, n) : sum_le_sum hn
... = _ : by simp [mul_comm]
theorem card_le_mul_card_image {f : Ξ± β Ξ²} (s : finset Ξ±)
(n : β) (hn : β a β s.image f, (s.filter (Ξ» x, f x = a)).card β€ n) :
s.card β€ n * (s.image f).card :=
card_le_mul_card_image_of_maps_to (Ξ» x, mem_image_of_mem _) n hn
theorem mul_card_image_le_card_of_maps_to {f : Ξ± β Ξ²} {s : finset Ξ±} {t : finset Ξ²}
(Hf : β a β s, f a β t) (n : β) (hn : β a β t, n β€ (s.filter (Ξ» x, f x = a)).card) :
n * t.card β€ s.card :=
calc n * t.card = (β _ in t, n) : by simp [mul_comm]
... β€ (β a in t, (s.filter (Ξ» x, f x = a)).card) : sum_le_sum hn
... = s.card : by rw β card_eq_sum_card_fiberwise Hf
theorem mul_card_image_le_card {f : Ξ± β Ξ²} (s : finset Ξ±)
(n : β) (hn : β a β s.image f, n β€ (s.filter (Ξ» x, f x = a)).card) :
n * (s.image f).card β€ s.card :=
mul_card_image_le_card_of_maps_to (Ξ» x, mem_image_of_mem _) n hn
end pigeonhole
section double_counting
variables [decidable_eq Ξ±] {s : finset Ξ±} {B : finset (finset Ξ±)} {n : β}
/-- If every element belongs to at most `n` finsets, then the sum of their sizes is at most `n`
times how many they are. -/
lemma sum_card_inter_le (h : β a β s, (B.filter $ (β) a).card β€ n) :
β t in B, (s β© t).card β€ s.card * n :=
begin
refine le_trans _ (s.sum_le_card_nsmul _ _ h),
simp_rw [βfilter_mem_eq_inter, card_eq_sum_ones, sum_filter],
exact sum_comm.le,
end
/-- If every element belongs to at most `n` finsets, then the sum of their sizes is at most `n`
times how many they are. -/
lemma sum_card_le [fintype Ξ±] (h : β a, (B.filter $ (β) a).card β€ n) :
β s in B, s.card β€ fintype.card Ξ± * n :=
calc β s in B, s.card = β s in B, (univ β© s).card : by simp_rw univ_inter
... β€ fintype.card Ξ± * n : sum_card_inter_le (Ξ» a _, h a)
/-- If every element belongs to at least `n` finsets, then the sum of their sizes is at least `n`
times how many they are. -/
lemma le_sum_card_inter (h : β a β s, n β€ (B.filter $ (β) a).card) :
s.card * n β€ β t in B, (s β© t).card :=
begin
apply (s.card_nsmul_le_sum _ _ h).trans,
simp_rw [βfilter_mem_eq_inter, card_eq_sum_ones, sum_filter],
exact sum_comm.le,
end
/-- If every element belongs to at least `n` finsets, then the sum of their sizes is at least `n`
times how many they are. -/
lemma le_sum_card [fintype Ξ±] (h : β a, n β€ (B.filter $ (β) a).card) :
fintype.card Ξ± * n β€ β s in B, s.card :=
calc fintype.card Ξ± * n β€ β s in B, (univ β© s).card : le_sum_card_inter (Ξ» a _, h a)
... = β s in B, s.card : by simp_rw univ_inter
/-- If every element belongs to exactly `n` finsets, then the sum of their sizes is `n` times how
many they are. -/
lemma sum_card_inter (h : β a β s, (B.filter $ (β) a).card = n) :
β t in B, (s β© t).card = s.card * n :=
(sum_card_inter_le $ Ξ» a ha, (h a ha).le).antisymm (le_sum_card_inter $ Ξ» a ha, (h a ha).ge)
/-- If every element belongs to exactly `n` finsets, then the sum of their sizes is `n` times how
many they are. -/
lemma sum_card [fintype Ξ±] (h : β a, (B.filter $ (β) a).card = n) :
β s in B, s.card = fintype.card Ξ± * n :=
by simp_rw [fintype.card, βsum_card_inter (Ξ» a _, h a), univ_inter]
lemma card_le_card_bUnion {s : finset ΞΉ} {f : ΞΉ β finset Ξ±} (hs : (s : set ΞΉ).pairwise_disjoint f)
(hf : β i β s, (f i).nonempty) :
s.card β€ (s.bUnion f).card :=
by { rw [card_bUnion hs, card_eq_sum_ones], exact sum_le_sum (Ξ» i hi, (hf i hi).card_pos) }
lemma card_le_card_bUnion_add_card_fiber {s : finset ΞΉ} {f : ΞΉ β finset Ξ±}
(hs : (s : set ΞΉ).pairwise_disjoint f) :
s.card β€ (s.bUnion f).card + (s.filter $ Ξ» i, f i = β
).card :=
begin
rw [βfinset.filter_card_add_filter_neg_card_eq_card (Ξ» i, f i = β
), add_comm],
exact add_le_add_right ((card_le_card_bUnion (hs.subset $ filter_subset _ _) $ Ξ» i hi,
nonempty_of_ne_empty $ (mem_filter.1 hi).2).trans $ card_le_of_subset $
bUnion_subset_bUnion_of_subset_left _ $ filter_subset _ _) _,
end
lemma card_le_card_bUnion_add_one {s : finset ΞΉ} {f : ΞΉ β finset Ξ±} (hf : injective f)
(hs : (s : set ΞΉ).pairwise_disjoint f) :
s.card β€ (s.bUnion f).card + 1 :=
(card_le_card_bUnion_add_card_fiber hs).trans $ add_le_add_left (card_le_one.2 $ Ξ» i hi j hj, hf $
(mem_filter.1 hi).2.trans (mem_filter.1 hj).2.symm) _
end double_counting
section canonically_ordered_monoid
variables [canonically_ordered_monoid M] {f : ΞΉ β M} {s t : finset ΞΉ}
@[simp, to_additive sum_eq_zero_iff]
lemma prod_eq_one_iff' : β x in s, f x = 1 β β x β s, f x = 1 :=
prod_eq_one_iff_of_one_le' $ Ξ» x hx, one_le (f x)
@[to_additive sum_le_sum_of_subset]
lemma prod_le_prod_of_subset' (h : s β t) : β x in s, f x β€ β x in t, f x :=
prod_le_prod_of_subset_of_one_le' h $ assume x hβ hβ, one_le _
@[to_additive sum_mono_set]
lemma prod_mono_set' (f : ΞΉ β M) : monotone (Ξ» s, β x in s, f x) :=
Ξ» sβ sβ hs, prod_le_prod_of_subset' hs
@[to_additive sum_le_sum_of_ne_zero]
lemma prod_le_prod_of_ne_one' (h : β x β s, f x β 1 β x β t) :
β x in s, f x β€ β x in t, f x :=
by classical;
calc β x in s, f x = (β x in s.filter (Ξ» x, f x = 1), f x) * β x in s.filter (Ξ» x, f x β 1), f x :
by rw [β prod_union, filter_union_filter_neg_eq];
exact disjoint_filter.2 (assume _ _ h n_h, n_h h)
... β€ (β x in t, f x) : mul_le_of_le_one_of_le
(prod_le_one' $ by simp only [mem_filter, and_imp]; exact Ξ» _ _, le_of_eq)
(prod_le_prod_of_subset' $ by simpa only [subset_iff, mem_filter, and_imp])
end canonically_ordered_monoid
section ordered_cancel_comm_monoid
variables [ordered_cancel_comm_monoid M] {f g : ΞΉ β M} {s t : finset ΞΉ}
@[to_additive sum_lt_sum]
theorem prod_lt_prod' (Hle : β i β s, f i β€ g i) (Hlt : β i β s, f i < g i) :
β i in s, f i < β i in s, g i :=
begin
classical,
rcases Hlt with β¨i, hi, hltβ©,
rw [β insert_erase hi, prod_insert (not_mem_erase _ _), prod_insert (not_mem_erase _ _)],
exact mul_lt_mul_of_lt_of_le hlt (prod_le_prod'' $ Ξ» j hj, Hle j $ mem_of_mem_erase hj)
end
@[to_additive sum_lt_sum_of_nonempty]
lemma prod_lt_prod_of_nonempty' (hs : s.nonempty) (Hlt : β i β s, f i < g i) :
β i in s, f i < β i in s, g i :=
begin
apply prod_lt_prod',
{ intros i hi, apply le_of_lt (Hlt i hi) },
cases hs with i hi,
exact β¨i, hi, Hlt i hiβ©,
end
@[to_additive sum_lt_sum_of_subset]
lemma prod_lt_prod_of_subset' (h : s β t) {i : ΞΉ} (ht : i β t) (hs : i β s) (hlt : 1 < f i)
(hle : β j β t, j β s β 1 β€ f j) :
β j in s, f j < β j in t, f j :=
by classical;
calc β j in s, f j < β j in insert i s, f j :
begin
rw prod_insert hs,
exact lt_mul_of_one_lt_left' (β j in s, f j) hlt,
end
... β€ β j in t, f j :
begin
apply prod_le_prod_of_subset_of_one_le',
{ simp [finset.insert_subset, h, ht] },
{ assume x hx h'x,
simp only [mem_insert, not_or_distrib] at h'x,
exact hle x hx h'x.2 }
end
@[to_additive single_lt_sum]
lemma single_lt_prod' {i j : ΞΉ} (hij : j β i) (hi : i β s) (hj : j β s) (hlt : 1 < f j)
(hle : β k β s, k β i β 1 β€ f k) :
f i < β k in s, f k :=
calc f i = β k in {i}, f k : prod_singleton.symm
... < β k in s, f k :
prod_lt_prod_of_subset' (singleton_subset_iff.2 hi) hj (mt mem_singleton.1 hij) hlt $
Ξ» k hks hki, hle k hks (mt mem_singleton.2 hki)
@[to_additive sum_pos] lemma one_lt_prod (h : βi β s, 1 < f i) (hs : s.nonempty) :
1 < (β i in s, f i) :=
lt_of_le_of_lt (by rw prod_const_one) $ prod_lt_prod_of_nonempty' hs h
@[to_additive] lemma prod_lt_one (h : βi β s, f i < 1) (hs : s.nonempty) :
(β i in s, f i) < 1 :=
(prod_lt_prod_of_nonempty' hs h).trans_le (by rw prod_const_one)
@[to_additive] lemma prod_eq_prod_iff_of_le {f g : ΞΉ β M} (h : β i β s, f i β€ g i) :
β i in s, f i = β i in s, g i β β i β s, f i = g i :=
begin
classical,
revert h,
refine finset.induction_on s (Ξ» _, β¨Ξ» _ _, false.elim, Ξ» _, rflβ©) (Ξ» a s ha ih H, _),
specialize ih (Ξ» i, H i β finset.mem_insert_of_mem),
rw [finset.prod_insert ha, finset.prod_insert ha, finset.forall_mem_insert, βih],
exact mul_eq_mul_iff_eq_and_eq (H a (s.mem_insert_self a)) (finset.prod_le_prod''
(Ξ» i, H i β finset.mem_insert_of_mem)),
end
end ordered_cancel_comm_monoid
section linear_ordered_cancel_comm_monoid
variables [linear_ordered_cancel_comm_monoid M] {f g : ΞΉ β M} {s t : finset ΞΉ}
@[to_additive exists_lt_of_sum_lt]
theorem exists_lt_of_prod_lt' (Hlt : β i in s, f i < β i in s, g i) :
β i β s, f i < g i :=
begin
contrapose! Hlt with Hle,
exact prod_le_prod'' Hle
end
@[to_additive exists_le_of_sum_le]
theorem exists_le_of_prod_le' (hs : s.nonempty) (Hle : β i in s, f i β€ β i in s, g i) :
β i β s, f i β€ g i :=
begin
contrapose! Hle with Hlt,
exact prod_lt_prod_of_nonempty' hs Hlt
end
@[to_additive exists_pos_of_sum_zero_of_exists_nonzero]
lemma exists_one_lt_of_prod_one_of_exists_ne_one' (f : ΞΉ β M)
(hβ : β i in s, f i = 1) (hβ : β i β s, f i β 1) :
β i β s, 1 < f i :=
begin
contrapose! hβ,
obtain β¨i, m, i_neβ© : β i β s, f i β 1 := hβ,
apply ne_of_lt,
calc β j in s, f j < β j in s, 1 : prod_lt_prod' hβ β¨i, m, (hβ i m).lt_of_ne i_neβ©
... = 1 : prod_const_one
end
end linear_ordered_cancel_comm_monoid
section ordered_comm_semiring
variables [ordered_comm_semiring R] {f g : ΞΉ β R} {s t : finset ΞΉ}
open_locale classical
/- this is also true for a ordered commutative multiplicative monoid -/
lemma prod_nonneg (h0 : β i β s, 0 β€ f i) : 0 β€ β i in s, f i :=
prod_induction f (Ξ» i, 0 β€ i) (Ξ» _ _ ha hb, mul_nonneg ha hb) zero_le_one h0
/- this is also true for a ordered commutative multiplicative monoid -/
lemma prod_pos [nontrivial R] (h0 : β i β s, 0 < f i) :
0 < β i in s, f i :=
prod_induction f (Ξ» x, 0 < x) (Ξ» _ _ ha hb, mul_pos ha hb) zero_lt_one h0
/-- If all `f i`, `i β s`, are nonnegative and each `f i` is less than or equal to `g i`, then the
product of `f i` is less than or equal to the product of `g i`. See also `finset.prod_le_prod''` for
the case of an ordered commutative multiplicative monoid. -/
lemma prod_le_prod (h0 : β i β s, 0 β€ f i) (h1 : β i β s, f i β€ g i) :
β i in s, f i β€ β i in s, g i :=
begin
induction s using finset.induction with a s has ih h,
{ simp },
{ simp only [prod_insert has], apply mul_le_mul,
{ exact h1 a (mem_insert_self a s) },
{ apply ih (Ξ» x H, h0 _ _) (Ξ» x H, h1 _ _); exact (mem_insert_of_mem H) },
{ apply prod_nonneg (Ξ» x H, h0 x (mem_insert_of_mem H)) },
{ apply le_trans (h0 a (mem_insert_self a s)) (h1 a (mem_insert_self a s)) } }
end
/-- If each `f i`, `i β s` belongs to `[0, 1]`, then their product is less than or equal to one.
See also `finset.prod_le_one'` for the case of an ordered commutative multiplicative monoid. -/
lemma prod_le_one (h0 : β i β s, 0 β€ f i) (h1 : β i β s, f i β€ 1) :
β i in s, f i β€ 1 :=
begin
convert β prod_le_prod h0 h1,
exact finset.prod_const_one
end
/-- If `g, h β€ f` and `g i + h i β€ f i`, then the product of `f` over `s` is at least the
sum of the products of `g` and `h`. This is the version for `ordered_comm_semiring`. -/
lemma prod_add_prod_le {i : ΞΉ} {f g h : ΞΉ β R}
(hi : i β s) (h2i : g i + h i β€ f i) (hgf : β j β s, j β i β g j β€ f j)
(hhf : β j β s, j β i β h j β€ f j) (hg : β i β s, 0 β€ g i) (hh : β i β s, 0 β€ h i) :
β i in s, g i + β i in s, h i β€ β i in s, f i :=
begin
simp_rw [prod_eq_mul_prod_diff_singleton hi],
refine le_trans _ (mul_le_mul_of_nonneg_right h2i _),
{ rw [right_distrib],
apply add_le_add; apply mul_le_mul_of_nonneg_left; try { apply_assumption; assumption };
apply prod_le_prod; simp * { contextual := tt } },
{ apply prod_nonneg, simp only [and_imp, mem_sdiff, mem_singleton],
intros j h1j h2j, exact le_trans (hg j h1j) (hgf j h1j h2j) }
end
end ordered_comm_semiring
section canonically_ordered_comm_semiring
variables [canonically_ordered_comm_semiring R] {f g h : ΞΉ β R} {s : finset ΞΉ} {i : ΞΉ}
lemma prod_le_prod' (h : β i β s, f i β€ g i) :
β i in s, f i β€ β i in s, g i :=
begin
classical,
induction s using finset.induction with a s has ih h,
{ simp },
{ rw [finset.prod_insert has, finset.prod_insert has],
apply mul_le_mul',
{ exact h _ (finset.mem_insert_self a s) },
{ exact ih (Ξ» i hi, h _ (finset.mem_insert_of_mem hi)) } }
end
/-- If `g, h β€ f` and `g i + h i β€ f i`, then the product of `f` over `s` is at least the
sum of the products of `g` and `h`. This is the version for `canonically_ordered_comm_semiring`.
-/
lemma prod_add_prod_le' (hi : i β s) (h2i : g i + h i β€ f i)
(hgf : β j β s, j β i β g j β€ f j) (hhf : β j β s, j β i β h j β€ f j) :
β i in s, g i + β i in s, h i β€ β i in s, f i :=
begin
classical, simp_rw [prod_eq_mul_prod_diff_singleton hi],
refine le_trans _ (mul_le_mul_right' h2i _),
rw [right_distrib],
apply add_le_add; apply mul_le_mul_left'; apply prod_le_prod';
simp only [and_imp, mem_sdiff, mem_singleton]; intros; apply_assumption; assumption
end
end canonically_ordered_comm_semiring
end finset
namespace fintype
variables [fintype ΞΉ]
@[to_additive sum_mono, mono]
lemma prod_mono' [ordered_comm_monoid M] : monotone (Ξ» f : ΞΉ β M, β i, f i) :=
Ξ» f g hfg, finset.prod_le_prod'' $ Ξ» x _, hfg x
attribute [mono] sum_mono
@[to_additive sum_strict_mono]
lemma prod_strict_mono' [ordered_cancel_comm_monoid M] : strict_mono (Ξ» f : ΞΉ β M, β x, f x) :=
Ξ» f g hfg, let β¨hle, i, hltβ© := pi.lt_def.mp hfg in
finset.prod_lt_prod' (Ξ» i _, hle i) β¨i, finset.mem_univ i, hltβ©
end fintype
namespace with_top
open finset
/-- A product of finite numbers is still finite -/
lemma prod_lt_top [canonically_ordered_comm_semiring R] [nontrivial R] [decidable_eq R]
{s : finset ΞΉ} {f : ΞΉ β with_top R} (h : β i β s, f i β β€) :
β i in s, f i < β€ :=
prod_induction f (Ξ» a, a < β€) (Ξ» a b hβ hβ, mul_lt_top hβ.ne hβ.ne) (coe_lt_top 1) $
Ξ» a ha, lt_top_iff_ne_top.2 (h a ha)
/-- A sum of finite numbers is still finite -/
lemma sum_lt_top [ordered_add_comm_monoid M] {s : finset ΞΉ} {f : ΞΉ β with_top M}
(h : β i β s, f i β β€) : (β i in s, f i) < β€ :=
sum_induction f (Ξ» a, a < β€) (Ξ» a b hβ hβ, add_lt_top.2 β¨hβ, hββ©) zero_lt_top $
Ξ» i hi, lt_top_iff_ne_top.2 (h i hi)
/-- A sum of numbers is infinite iff one of them is infinite -/
lemma sum_eq_top_iff [ordered_add_comm_monoid M] {s : finset ΞΉ} {f : ΞΉ β with_top M} :
β i in s, f i = β€ β β i β s, f i = β€ :=
begin
classical,
split,
{ contrapose!,
exact Ξ» h, (sum_lt_top $ Ξ» i hi, (h i hi)).ne },
{ rintro β¨i, his, hiβ©,
rw [sum_eq_add_sum_diff_singleton his, hi, top_add] }
end
/-- A sum of finite numbers is still finite -/
lemma sum_lt_top_iff [ordered_add_comm_monoid M] {s : finset ΞΉ} {f : ΞΉ β with_top M} :
β i in s, f i < β€ β β i β s, f i < β€ :=
by simp only [lt_top_iff_ne_top, ne.def, sum_eq_top_iff, not_exists]
end with_top
section absolute_value
variables {S : Type*}
lemma absolute_value.sum_le [semiring R] [ordered_semiring S]
(abv : absolute_value R S) (s : finset ΞΉ) (f : ΞΉ β R) :
abv (β i in s, f i) β€ β i in s, abv (f i) :=
begin
letI := classical.dec_eq ΞΉ,
refine finset.induction_on s _ (Ξ» i s hi ih, _),
{ simp },
{ simp only [finset.sum_insert hi],
exact (abv.add_le _ _).trans (add_le_add le_rfl ih) },
end
lemma is_absolute_value.abv_sum [semiring R] [ordered_semiring S] (abv : R β S)
[is_absolute_value abv] (f : ΞΉ β R) (s : finset ΞΉ) :
abv (β i in s, f i) β€ β i in s, abv (f i) :=
(is_absolute_value.to_absolute_value abv).sum_le _ _
lemma absolute_value.map_prod [comm_semiring R] [nontrivial R] [linear_ordered_comm_ring S]
(abv : absolute_value R S) (f : ΞΉ β R) (s : finset ΞΉ) :
abv (β i in s, f i) = β i in s, abv (f i) :=
abv.to_monoid_hom.map_prod f s
lemma is_absolute_value.map_prod [comm_semiring R] [nontrivial R] [linear_ordered_comm_ring S]
(abv : R β S) [is_absolute_value abv] (f : ΞΉ β R) (s : finset ΞΉ) :
abv (β i in s, f i) = β i in s, abv (f i) :=
(is_absolute_value.to_absolute_value abv).map_prod _ _
end absolute_value
|
cf27e04bdd86dfb64b63c422f8a4ec7d85e200b3 | 4d3f29a7b2eff44af8fd0d3176232e039acb9ee3 | /LAMR/Util/Propositional/Transformations.lean | ea64d7e3feff8e0cced4c7ecd3c1f227f50ed5f4 | [] | no_license | marijnheule/lamr | 5fc5d69d326ff92e321242cfd7f72e78d7f99d7e | 28cc4114c7361059bb54f407fa312bf38b48728b | refs/heads/main | 1,689,338,013,620 | 1,630,359,632,000 | 1,630,359,632,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,332 | lean | import LAMR.Util.Propositional.Syntax
namespace NnfForm
def neg : NnfForm β NnfForm
| tr => fls
| fls => tr
| var n => negVar n
| negVar n => var n
| conj p q => disj (neg p) (neg q)
| disj p q => conj (neg p) (neg q)
end NnfForm
namespace PropForm
def toNnfForm : PropForm β NnfForm
| tr => NnfForm.tr
| fls => NnfForm.fls
| var n => NnfForm.var n
| neg p => p.toNnfForm.neg
| conj p q => NnfForm.conj p.toNnfForm q.toNnfForm
| disj p q => NnfForm.disj p.toNnfForm q.toNnfForm
| impl p q => NnfForm.disj p.toNnfForm.neg q.toNnfForm
| biImpl p q => NnfForm.conj (NnfForm.disj p.toNnfForm.neg q.toNnfForm)
(NnfForm.disj q.toNnfForm.neg p.toNnfForm)
end PropForm
/-
Translation to CNF.
-/
def List.Union [DecidableEq Ξ±]: List (List Ξ±) β List Ξ±
| [] => []
| (l ::ls) => l.union (ls.Union)
def CnfForm.disj (cnf1 cnf2 : CnfForm) : CnfForm :=
(cnf1.map (fun cls => cnf2.map cls.union)).Union
def NnfForm.toCnfForm : NnfForm β CnfForm
| NnfForm.var s => [ [Lit.pos s] ]
| NnfForm.negVar s => [ [Lit.neg s] ]
| NnfForm.tr => []
| NnfForm.fls => [ [] ]
| NnfForm.conj A B => A.toCnfForm.union B.toCnfForm
| NnfForm.disj A B => A.toCnfForm.disj B.toCnfForm
def PropForm.toCnfForm (A : PropForm) : CnfForm := A.toNnfForm.toCnfForm |
1eb6f4680254266390ad44d37039d75c5f6f8197 | c3f2fcd060adfa2ca29f924839d2d925e8f2c685 | /library/init/prod.lean | a515e9bcb01db17498c091d68d58aad4b6763975 | [
"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 | 3,445 | lean | /-
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Module: init.prod
Author: Leonardo de Moura, Jeremy Avigad
-/
prelude
import init.num init.wf
definition pair := @prod.mk
notation A Γ B := prod A B
-- notation for n-ary tuples
notation `(` h `,` t:(foldl `,` (e r, prod.mk r e) h) `)` := t
namespace prod
notation A * B := prod A B
notation A Γ B := prod A B -- repeat, so this takes precedence
namespace low_precedence_times
reserve infixr `*`:30 -- conflicts with notation for multiplication
infixr `*` := prod
end low_precedence_times
notation `prβ` := pr1
notation `prβ` := pr2
namespace ops
postfix `.1`:(max+1) := pr1
postfix `.2`:(max+1) := pr2
end ops
open well_founded
section
variables {A B : Type}
variable (Ra : A β A β Prop)
variable (Rb : B β B β Prop)
-- Lexicographical order based on Ra and Rb
inductive lex : A Γ B β A Γ B β Prop :=
| left : β{aβ bβ} aβ bβ, Ra aβ aβ β lex (aβ, bβ) (aβ, bβ)
| right : βa {bβ bβ}, Rb bβ bβ β lex (a, bβ) (a, bβ)
-- Relational product based on Ra and Rb
inductive rprod : A Γ B β A Γ B β Prop :=
intro : β{aβ bβ aβ bβ}, Ra aβ aβ β Rb bβ bβ β rprod (aβ, bβ) (aβ, bβ)
end
context
parameters {A B : Type}
parameters {Ra : A β A β Prop} {Rb : B β B β Prop}
infix `βΊ`:50 := lex Ra Rb
definition lex.accessible {a} (aca : acc Ra a) (acb : βb, acc Rb b): βb, acc (lex Ra Rb) (a, b) :=
acc.rec_on aca
(Ξ»xa aca (iHa : βy, Ra y xa β βb, acc (lex Ra Rb) (y, b)),
Ξ»b, acc.rec_on (acb b)
(Ξ»xb acb
(iHb : βy, Rb y xb β acc (lex Ra Rb) (xa, y)),
acc.intro (xa, xb) (Ξ»p (lt : p βΊ (xa, xb)),
have aux : xa = xa β xb = xb β acc (lex Ra Rb) p, from
@prod.lex.rec_on A B Ra Rb (Ξ»pβ pβ, prβ pβ = xa β prβ pβ = xb β acc (lex Ra Rb) pβ)
p (xa, xb) lt
(Ξ»aβ bβ aβ bβ (H : Ra aβ aβ) (eqβ : aβ = xa) (eqβ : bβ = xb),
show acc (lex Ra Rb) (aβ, bβ), from
have Raβ : Ra aβ xa, from eq.rec_on eqβ H,
iHa aβ Raβ bβ)
(Ξ»a bβ bβ (H : Rb bβ bβ) (eqβ : a = xa) (eqβ : bβ = xb),
show acc (lex Ra Rb) (a, bβ), from
have Rbβ : Rb bβ xb, from eq.rec_on eqβ H,
have eqβ' : xa = a, from eq.rec_on eqβ rfl,
eq.rec_on eqβ' (iHb bβ Rbβ)),
aux rfl rfl)))
-- The lexicographical order of well founded relations is well-founded
definition lex.wf (Ha : well_founded Ra) (Hb : well_founded Rb) : well_founded (lex Ra Rb) :=
well_founded.intro (Ξ»p, destruct p (Ξ»a b, lex.accessible (Ha a) (well_founded.apply Hb) b))
-- Relational product is a subrelation of the lex
definition rprod.sub_lex : β a b, rprod Ra Rb a b β lex Ra Rb a b :=
Ξ»a b H, prod.rprod.rec_on H (Ξ» aβ bβ aβ bβ Hβ Hβ, lex.left Rb aβ bβ Hβ)
-- The relational product of well founded relations is well-founded
definition rprod.wf (Ha : well_founded Ra) (Hb : well_founded Rb) : well_founded (rprod Ra Rb) :=
subrelation.wf (rprod.sub_lex) (lex.wf Ha Hb)
end
end prod
|
c4dc4e3c8322a21a17fc2ff42cbdcc4278e897d1 | 4d2583807a5ac6caaffd3d7a5f646d61ca85d532 | /src/tactic/cache.lean | b34d3af036d5d67cf1303a45cdf56d5d27485fb5 | [
"Apache-2.0"
] | permissive | AntoineChambert-Loir/mathlib | 64aabb896129885f12296a799818061bc90da1ff | 07be904260ab6e36a5769680b6012f03a4727134 | refs/heads/master | 1,693,187,631,771 | 1,636,719,886,000 | 1,636,719,886,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 5,234 | 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 tactic.doc_commands
/-!
# Instance cache tactics
For performance reasons, Lean does not automatically update its database
of class instances during a proof. The group of tactics in this file
helps to force such updates.
-/
open lean.parser
open interactive interactive.types
local postfix `?`:9001 := optional
local postfix *:9001 := many
namespace tactic
/-- Reset the instance cache for the main goal. -/
meta def reset_instance_cache : tactic unit := do
unfreeze_local_instances,
freeze_local_instances
/-- Unfreeze the local instances while executing `tac` on the main goal. -/
meta def unfreezing {Ξ±} (tac : tactic Ξ±) : tactic Ξ± :=
focus1 $ unfreeze_local_instances *> tac <* all_goals freeze_local_instances
/--
Unfreeze local instances while executing `tac`,
if the passed expression is amongst the frozen instances.
-/
meta def unfreezing_hyp (h : expr) (tac : tactic unit) : tactic unit :=
do frozen β frozen_local_instances,
if h β frozen.get_or_else [] then unfreezing tac else tac
namespace interactive
/--
`unfreezingI { tac }` executes tac while temporarily unfreezing the instance cache.
-/
meta def unfreezingI (tac : itactic) :=
unfreezing tac
/-- Reset the instance cache. This allows any new instances
added to the context to be used in typeclass inference. -/
meta def resetI := reset_instance_cache
/-- Like `subst`, but can also substitute in instance arguments. -/
meta def substI (q : parse texpr) : tactic unit :=
unfreezingI (subst q)
/-- Like `cases`, but can also be used with instance arguments. -/
meta def casesI (p : parse cases_arg_p) (q : parse with_ident_list) : tactic unit :=
unfreezingI (cases p q)
/-- Like `intro`, but uses the introduced variable
in typeclass inference. -/
meta def introI (p : parse ident_?) : tactic unit :=
intro p >> reset_instance_cache
/-- Like `intros`, but uses the introduced variable(s)
in typeclass inference. -/
meta def introsI (p : parse ident_*) : tactic unit :=
intros p >> reset_instance_cache
/-- Used to add typeclasses to the context so that they can
be used in typeclass inference. The syntax is the same as `have`. -/
meta def haveI (h : parse ident?) (qβ : parse (tk ":" *> texpr)?) (qβ : parse (tk ":=" *> texpr)?) :
tactic unit :=
do h β match h with
| none := get_unused_name "_inst"
| some a := return a
end,
Β«haveΒ» (some h) qβ qβ,
match qβ with
| none := swap >> reset_instance_cache >> swap
| some pβ := reset_instance_cache
end
/-- Used to add typeclasses to the context so that they can
be used in typeclass inference. The syntax is the same as `let`. -/
meta def letI
(h : parse ident?) (qβ : parse (tk ":" *> texpr)?) (qβ : parse $ (tk ":=" *> texpr)?) :
tactic unit :=
do h β match h with
| none := get_unused_name "_inst"
| some a := return a
end,
Β«letΒ» (some h) qβ qβ,
match qβ with
| none := swap >> reset_instance_cache >> swap
| some pβ := reset_instance_cache
end
/-- Like `exact`, but uses all variables in the context
for typeclass inference. -/
meta def exactI (q : parse texpr) : tactic unit :=
reset_instance_cache >> exact q
/--
For performance reasons, Lean does not automatically update its database
of class instances during a proof. The group of tactics described below
helps to force such updates. For a simple (but very artificial) example,
consider the function `default` from the core library. It has type
`Ξ (Ξ± : Sort u) [inhabited Ξ±], Ξ±`, so one can use `default Ξ±` only if Lean
can find a registered instance of `inhabited Ξ±`. Because the database of
such instance is not automatically updated during a proof, the following
attempt won't work (Lean will not pick up the instance from the local
context):
```lean
def my_id (Ξ± : Type) : Ξ± β Ξ± :=
begin
intro x,
have : inhabited Ξ± := β¨xβ©,
exact default Ξ±, -- Won't work!
end
```
However, it will work, producing the identity function, if one replaces `have`
by its variant `haveI` described below.
* `resetI`: Reset the instance cache. This allows any instances
currently in the context to be used in typeclass inference.
* `unfreezingI { tac }`: Unfreeze local instances while executing the tactic `tac`.
* `introI`/`introsI`: `intro`/`intros` followed by `resetI`. Like
`intro`/`intros`, but uses the introduced variable in typeclass inference.
* `casesI`: like `cases`, but can also be used with instance arguments.
* `substI`: like `subst`, but can also substitute in type-class arguments
* `haveI`/`letI`: `have`/`let` followed by `resetI`. Used to add typeclasses
to the context so that they can be used in typeclass inference.
* `exactI`: `resetI` followed by `exact`. Like `exact`, but uses all
variables in the context for typeclass inference.
-/
add_tactic_doc
{ name := "Instance cache tactics",
category := doc_category.tactic,
decl_names := [``resetI, ``unfreezingI, ``casesI, ``substI, ``introI, ``introsI, ``haveI, ``letI,
``exactI],
tags := ["type class", "context management"] }
end interactive
end tactic
|
fd38f3146755ce14d1dae9c36672bda2a0642f00 | a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940 | /src/Lean/Elab/InfoTree.lean | 8bd57827ba0be236f6a13a7e473b3a617b91a665 | [
"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 | 15,236 | lean | /-
Copyright (c) 2020 Wojciech Nawrocki. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Wojciech Nawrocki, Leonardo de Moura, Sebastian Ullrich
-/
import Lean.Data.Position
import Lean.Expr
import Lean.Message
import Lean.Data.Json
import Lean.Meta.Basic
import Lean.Meta.PPGoal
namespace Lean.Elab
open Std (PersistentArray PersistentArray.empty PersistentHashMap)
/- Context after executing `liftTermElabM`.
Note that the term information collected during elaboration may contain metavariables, and their
assignments are stored at `mctx`. -/
structure ContextInfo where
env : Environment
fileMap : FileMap
mctx : MetavarContext := {}
options : Options := {}
currNamespace : Name := Name.anonymous
openDecls : List OpenDecl := []
deriving Inhabited
/-- An elaboration step -/
structure ElabInfo where
elaborator : Name
stx : Syntax
deriving Inhabited
structure TermInfo extends ElabInfo where
lctx : LocalContext -- The local context when the term was elaborated.
expectedType? : Option Expr
expr : Expr
deriving Inhabited
structure CommandInfo extends ElabInfo where
deriving Inhabited
inductive CompletionInfo where
| dot (termInfo : TermInfo) (field? : Option Syntax) (expectedType? : Option Expr)
| id (stx : Syntax) (id : Name) (danglingDot : Bool) (lctx : LocalContext) (expectedType? : Option Expr)
| namespaceId (stx : Syntax)
| option (stx : Syntax)
| endSection (stx : Syntax) (scopeNames : List String)
| tactic (stx : Syntax) (goals : List MVarId)
-- TODO `import`
def CompletionInfo.stx : CompletionInfo β Syntax
| dot i .. => i.stx
| id stx .. => stx
| namespaceId stx => stx
| option stx => stx
| endSection stx .. => stx
| tactic stx .. => stx
structure FieldInfo where
/-- Name of the projection. -/
projName : Name
/-- Name of the field as written. -/
fieldName : Name
lctx : LocalContext
val : Expr
stx : Syntax
deriving Inhabited
/- We store the list of goals before and after the execution of a tactic.
We also store the metavariable context at each time since, we want to unassigned metavariables
at tactic execution time to be displayed as `?m...`. -/
structure TacticInfo extends ElabInfo where
mctxBefore : MetavarContext
goalsBefore : List MVarId
mctxAfter : MetavarContext
goalsAfter : List MVarId
deriving Inhabited
structure MacroExpansionInfo where
lctx : LocalContext -- The local context when the macro was expanded.
stx : Syntax
output : Syntax
deriving Inhabited
inductive Info where
| ofTacticInfo (i : TacticInfo)
| ofTermInfo (i : TermInfo)
| ofCommandInfo (i : CommandInfo)
| ofMacroExpansionInfo (i : MacroExpansionInfo)
| ofFieldInfo (i : FieldInfo)
| ofCompletionInfo (i : CompletionInfo)
deriving Inhabited
inductive InfoTree where
| context (i : ContextInfo) (t : InfoTree) -- The context object is created by `liftTermElabM` at `Command.lean`
| node (i : Info) (children : PersistentArray InfoTree) -- The children contains information for nested term elaboration and tactic evaluation
| ofJson (j : Json) -- For user data
| hole (mvarId : MVarId) -- The elaborator creates holes (aka metavariables) for tactics and postponed terms
deriving Inhabited
partial def InfoTree.findInfo? (p : Info β Bool) (t : InfoTree) : Option InfoTree :=
match t with
| context _ t => findInfo? p t
| node i ts =>
if p i then
some t
else
ts.findSome? (findInfo? p)
| _ => none
structure InfoState where
enabled : Bool := false
assignment : PersistentHashMap MVarId InfoTree := {} -- map from holeId to InfoTree
trees : PersistentArray InfoTree := {}
deriving Inhabited
class MonadInfoTree (m : Type β Type) where
getInfoState : m InfoState
modifyInfoState : (InfoState β InfoState) β m Unit
export MonadInfoTree (getInfoState modifyInfoState)
instance [MonadLift m n] [MonadInfoTree m] : MonadInfoTree n where
getInfoState := liftM (getInfoState : m _)
modifyInfoState f := liftM (modifyInfoState f : m _)
partial def InfoTree.substitute (tree : InfoTree) (assignment : PersistentHashMap MVarId InfoTree) : InfoTree :=
match tree with
| node i c => node i <| c.map (substitute Β· assignment)
| context i t => context i (substitute t assignment)
| ofJson j => ofJson j
| hole id => match assignment.find? id with
| none => hole id
| some tree => substitute tree assignment
def ContextInfo.runMetaM (info : ContextInfo) (lctx : LocalContext) (x : MetaM Ξ±) : IO Ξ± := do
let x := x.run { lctx := lctx } { mctx := info.mctx }
let ((a, _), _) β x.toIO { options := info.options, currNamespace := info.currNamespace, openDecls := info.openDecls } { env := info.env }
return a
def ContextInfo.toPPContext (info : ContextInfo) (lctx : LocalContext) : PPContext :=
{ env := info.env, mctx := info.mctx, lctx := lctx,
opts := info.options, currNamespace := info.currNamespace, openDecls := info.openDecls }
def ContextInfo.ppSyntax (info : ContextInfo) (lctx : LocalContext) (stx : Syntax) : IO Format := do
ppTerm (info.toPPContext lctx) stx
private def formatStxRange (ctx : ContextInfo) (stx : Syntax) : Format := do
let pos := stx.getPos?.getD 0
let endPos := stx.getTailPos?.getD pos
return f!"{fmtPos pos stx.getHeadInfo}-{fmtPos endPos stx.getTailInfo}"
where fmtPos pos info :=
let pos := format <| ctx.fileMap.toPosition pos
match info with
| SourceInfo.original .. => pos
| _ => f!"{pos}β "
private def formatElabInfo (ctx : ContextInfo) (info : ElabInfo) : Format :=
if info.elaborator.isAnonymous then
formatStxRange ctx info.stx
else
f!"{formatStxRange ctx info.stx} @ {info.elaborator}"
def TermInfo.runMetaM (info : TermInfo) (ctx : ContextInfo) (x : MetaM Ξ±) : IO Ξ± :=
ctx.runMetaM info.lctx x
def TermInfo.format (ctx : ContextInfo) (info : TermInfo) : IO Format := do
info.runMetaM ctx do
try
return f!"{β Meta.ppExpr info.expr} : {β Meta.ppExpr (β Meta.inferType info.expr)} @ {formatElabInfo ctx info.toElabInfo}"
catch _ =>
return f!"{β Meta.ppExpr info.expr} : <failed-to-infer-type> @ {formatElabInfo ctx info.toElabInfo}"
def CompletionInfo.format (ctx : ContextInfo) (info : CompletionInfo) : IO Format :=
match info with
| CompletionInfo.dot i (expectedType? := expectedType?) .. => return f!"[.] {β i.format ctx} : {expectedType?}"
| CompletionInfo.id stx _ _ lctx expectedType? => ctx.runMetaM lctx do return f!"[.] {stx} : {expectedType?} @ {formatStxRange ctx info.stx}"
| _ => return f!"[.] {info.stx} @ {formatStxRange ctx info.stx}"
def CommandInfo.format (ctx : ContextInfo) (info : CommandInfo) : IO Format := do
return f!"command @ {formatElabInfo ctx info.toElabInfo}"
def FieldInfo.format (ctx : ContextInfo) (info : FieldInfo) : IO Format := do
ctx.runMetaM info.lctx do
return f!"{info.fieldName} : {β Meta.ppExpr (β Meta.inferType info.val)} := {β Meta.ppExpr info.val} @ {formatStxRange ctx info.stx}"
def ContextInfo.ppGoals (ctx : ContextInfo) (goals : List MVarId) : IO Format :=
if goals.isEmpty then
return "no goals"
else
ctx.runMetaM {} (return Std.Format.prefixJoin "\n" (β goals.mapM Meta.ppGoal))
def TacticInfo.format (ctx : ContextInfo) (info : TacticInfo) : IO Format := do
let ctxB := { ctx with mctx := info.mctxBefore }
let ctxA := { ctx with mctx := info.mctxAfter }
let goalsBefore β ctxB.ppGoals info.goalsBefore
let goalsAfter β ctxA.ppGoals info.goalsAfter
return f!"Tactic @ {formatElabInfo ctx info.toElabInfo}\n{info.stx}\nbefore {goalsBefore}\nafter {goalsAfter}"
def MacroExpansionInfo.format (ctx : ContextInfo) (info : MacroExpansionInfo) : IO Format := do
let stx β ctx.ppSyntax info.lctx info.stx
let output β ctx.ppSyntax info.lctx info.output
return f!"Macro expansion\n{stx}\n===>\n{output}"
def Info.format (ctx : ContextInfo) : Info β IO Format
| ofTacticInfo i => i.format ctx
| ofTermInfo i => i.format ctx
| ofCommandInfo i => i.format ctx
| ofMacroExpansionInfo i => i.format ctx
| ofFieldInfo i => i.format ctx
| ofCompletionInfo i => i.format ctx
def Info.toElabInfo? : Info β Option ElabInfo
| ofTacticInfo i => some i.toElabInfo
| ofTermInfo i => some i.toElabInfo
| ofCommandInfo i => some i.toElabInfo
| ofMacroExpansionInfo i => none
| ofFieldInfo i => none
| ofCompletionInfo i => none
/--
Helper function for propagating the tactic metavariable context to its children nodes.
We need this function because we preserve `TacticInfo` nodes during backtracking *and* their
children. Moreover, we backtrack the metavariable context to undo metavariable assignments.
`TacticInfo` nodes save the metavariable context before/after the tactic application, and
can be pretty printed without any extra information. This is not the case for `TermInfo` nodes.
Without this function, the formatting method would often fail when processing `TermInfo` nodes
that are children of `TacticInfo` nodes that have been preserved during backtracking.
Saving the metavariable context at `TermInfo` nodes is also not a good option because
at `TermInfo` creation time, the metavariable context often miss information, e.g.,
a TC problem has not been resolved, a postponed subterm has not been elaborated, etc.
See `Term.SavedState.restore`.
-/
def Info.updateContext? : Option ContextInfo β Info β Option ContextInfo
| some ctx, ofTacticInfo i => some { ctx with mctx := i.mctxAfter }
| ctx?, _ => ctx?
partial def InfoTree.format (tree : InfoTree) (ctx? : Option ContextInfo := none) : IO Format := do
match tree with
| ofJson j => return toString j
| hole id => return toString id
| context i t => format t i
| node i cs => match ctx? with
| none => return "<context-not-available>"
| some ctx =>
let fmt β i.format ctx
if cs.size == 0 then
return fmt
else
let ctx? := i.updateContext? ctx?
return f!"{fmt}{Std.Format.nestD <| Std.Format.prefixJoin "\n" (β cs.toList.mapM fun c => format c ctx?)}"
section
variable [Monad m] [MonadInfoTree m]
@[inline] private def modifyInfoTrees (f : PersistentArray InfoTree β PersistentArray InfoTree) : m Unit :=
modifyInfoState fun s => { s with trees := f s.trees }
def getResetInfoTrees : m (PersistentArray InfoTree) := do
let trees := (β getInfoState).trees
modifyInfoTrees fun _ => {}
return trees
def pushInfoTree (t : InfoTree) : m Unit := do
if (β getInfoState).enabled then
modifyInfoTrees fun ts => ts.push t
def pushInfoLeaf (t : Info) : m Unit := do
if (β getInfoState).enabled then
pushInfoTree <| InfoTree.node (children := {}) t
def addCompletionInfo (info : CompletionInfo) : m Unit := do
pushInfoLeaf <| Info.ofCompletionInfo info
def resolveGlobalConstNoOverloadWithInfo [MonadResolveName m] [MonadEnv m] [MonadError m] (stx : Syntax) (id := stx.getId) (expectedType? : Option Expr := none) : m Name := do
let n β resolveGlobalConstNoOverload id
if (β getInfoState).enabled then
-- we do not store a specific elaborator since identifiers are special-cased by the server anyway
pushInfoLeaf <| Info.ofTermInfo { elaborator := Name.anonymous, lctx := LocalContext.empty, expr := (β mkConstWithLevelParams n), stx, expectedType? }
return n
def resolveGlobalConstWithInfos [MonadResolveName m] [MonadEnv m] [MonadError m] (stx : Syntax) (id := stx.getId) (expectedType? : Option Expr := none) : m (List Name) := do
let ns β resolveGlobalConst id
if (β getInfoState).enabled then
for n in ns do
pushInfoLeaf <| Info.ofTermInfo { elaborator := Name.anonymous, lctx := LocalContext.empty, expr := (β mkConstWithLevelParams n), stx, expectedType? }
return ns
def withInfoContext' [MonadFinally m] (x : m Ξ±) (mkInfo : Ξ± β m (Sum Info MVarId)) : m Ξ± := do
if (β getInfoState).enabled then
let treesSaved β getResetInfoTrees
Prod.fst <$> MonadFinally.tryFinally' x fun a? => do
match a? with
| none => modifyInfoTrees fun _ => treesSaved
| some a =>
let info β mkInfo a
modifyInfoTrees fun trees =>
match info with
| Sum.inl info => treesSaved.push <| InfoTree.node info trees
| Sum.inr mvaId => treesSaved.push <| InfoTree.hole mvaId
else
x
def withInfoTreeContext [MonadFinally m] (x : m Ξ±) (mkInfoTree : PersistentArray InfoTree β m InfoTree) : m Ξ± := do
if (β getInfoState).enabled then
let treesSaved β getResetInfoTrees
Prod.fst <$> MonadFinally.tryFinally' x fun _ => do
let st β getInfoState
let tree β mkInfoTree st.trees
modifyInfoTrees fun _ => treesSaved.push tree
else
x
@[inline] def withInfoContext [MonadFinally m] (x : m Ξ±) (mkInfo : m Info) : m Ξ± := do
withInfoTreeContext x (fun trees => do return InfoTree.node (β mkInfo) trees)
def withSaveInfoContext [MonadFinally m] [MonadEnv m] [MonadOptions m] [MonadMCtx m] [MonadResolveName m] [MonadFileMap m] (x : m Ξ±) : m Ξ± := do
if (β getInfoState).enabled then
let treesSaved β getResetInfoTrees
Prod.fst <$> MonadFinally.tryFinally' x fun _ => do
let st β getInfoState
let trees β st.trees.mapM fun tree => do
let tree := tree.substitute st.assignment
InfoTree.context {
env := (β getEnv), fileMap := (β getFileMap), mctx := (β getMCtx), currNamespace := (β getCurrNamespace), openDecls := (β getOpenDecls), options := (β getOptions)
} tree
modifyInfoTrees fun _ => treesSaved ++ trees
else
x
def getInfoHoleIdAssignment? (mvarId : MVarId) : m (Option InfoTree) :=
return (β getInfoState).assignment[mvarId]
def assignInfoHoleId (mvarId : MVarId) (infoTree : InfoTree) : m Unit := do
assert! (β getInfoHoleIdAssignment? mvarId).isNone
modifyInfoState fun s => { s with assignment := s.assignment.insert mvarId infoTree }
end
def withMacroExpansionInfo [MonadFinally m] [Monad m] [MonadInfoTree m] [MonadLCtx m] (stx output : Syntax) (x : m Ξ±) : m Ξ± :=
let mkInfo : m Info := do
return Info.ofMacroExpansionInfo {
lctx := (β getLCtx)
stx, output
}
withInfoContext x mkInfo
@[inline] def withInfoHole [MonadFinally m] [Monad m] [MonadInfoTree m] (mvarId : MVarId) (x : m Ξ±) : m Ξ± := do
if (β getInfoState).enabled then
let treesSaved β getResetInfoTrees
Prod.fst <$> MonadFinally.tryFinally' x fun a? => modifyInfoState fun s =>
if s.trees.size > 0 then
{ s with trees := treesSaved, assignment := s.assignment.insert mvarId s.trees[s.trees.size - 1] }
else
{ s with trees := treesSaved }
else
x
def enableInfoTree [MonadInfoTree m] (flag := true) : m Unit :=
modifyInfoState fun s => { s with enabled := flag }
def getInfoTrees [MonadInfoTree m] [Monad m] : m (PersistentArray InfoTree) :=
return (β getInfoState).trees
end Lean.Elab
|
90ce523f97a4d262749ad6618726054168a83f55 | 8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3 | /src/data/equiv/basic.lean | 5349e7d32cb58d790ed1bc1ba88416b37ebf3bc6 | [
"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 | 103,837 | lean | /-
Copyright (c) 2015 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura, Mario Carneiro
-/
import data.set.function
import data.sigma.basic
/-!
# Equivalence between types
In this file we define two types:
* `equiv Ξ± Ξ²` a.k.a. `Ξ± β Ξ²`: a bijective map `Ξ± β Ξ²` bundled with its inverse map; we use this (and
not equality!) to express that various `Type`s or `Sort`s are equivalent.
* `equiv.perm Ξ±`: the group of permutations `Ξ± β Ξ±`. More lemmas about `equiv.perm` can be found in
`group_theory/perm`.
Then we define
* canonical isomorphisms between various types: e.g.,
- `equiv.refl Ξ±` is the identity map interpreted as `Ξ± β Ξ±`;
- `equiv.sum_equiv_sigma_bool` is the canonical equivalence between the sum of two types `Ξ± β Ξ²`
and the sigma-type `Ξ£ b : bool, cond b Ξ± Ξ²`;
- `equiv.prod_sum_distrib : Ξ± Γ (Ξ² β Ξ³) β (Ξ± Γ Ξ²) β (Ξ± Γ Ξ³)` shows that type product and type sum
satisfy the distributive law up to a canonical equivalence;
* operations on equivalences: e.g.,
- `equiv.symm e : Ξ² β Ξ±` is the inverse of `e : Ξ± β Ξ²`;
- `equiv.trans eβ eβ : Ξ± β Ξ³` is the composition of `eβ : Ξ± β Ξ²` and `eβ : Ξ² β Ξ³` (note the order
of the arguments!);
- `equiv.prod_congr ea eb : Ξ±β Γ Ξ²β β Ξ±β Γ Ξ²β`: combine two equivalences `ea : Ξ±β β Ξ±β` and
`eb : Ξ²β β Ξ²β` using `prod.map`.
* definitions that transfer some instances along an equivalence. By convention, we transfer
instances from right to left.
- `equiv.inhabited` takes `e : Ξ± β Ξ²` and `[inhabited Ξ²]` and returns `inhabited Ξ±`;
- `equiv.unique` takes `e : Ξ± β Ξ²` and `[unique Ξ²]` and returns `unique Ξ±`;
- `equiv.decidable_eq` takes `e : Ξ± β Ξ²` and `[decidable_eq Ξ²]` and returns `decidable_eq Ξ±`.
More definitions of this kind can be found in other files. E.g., `data/equiv/transfer_instance`
does it for many algebraic type classes like `group`, `module`, etc.
## Tags
equivalence, congruence, bijective map
-/
open function
universes u v w z
variables {Ξ± : Sort u} {Ξ² : Sort v} {Ξ³ : Sort w}
/-- `Ξ± β Ξ²` is the type of functions from `Ξ± β Ξ²` with a two-sided inverse. -/
@[nolint has_inhabited_instance]
structure equiv (Ξ± : Sort*) (Ξ² : Sort*) :=
(to_fun : Ξ± β Ξ²)
(inv_fun : Ξ² β Ξ±)
(left_inv : left_inverse inv_fun to_fun)
(right_inv : right_inverse inv_fun to_fun)
infix ` β `:25 := equiv
/-- Convert an involutive function `f` to an equivalence with `to_fun = inv_fun = f`. -/
def function.involutive.to_equiv (f : Ξ± β Ξ±) (h : involutive f) : Ξ± β Ξ± :=
β¨f, f, h.left_inverse, h.right_inverseβ©
namespace equiv
/-- `perm Ξ±` is the type of bijections from `Ξ±` to itself. -/
@[reducible] def perm (Ξ± : Sort*) := equiv Ξ± Ξ±
instance : has_coe_to_fun (Ξ± β Ξ²) :=
β¨_, to_funβ©
@[simp] theorem coe_fn_mk (f : Ξ± β Ξ²) (g l r) : (equiv.mk f g l r : Ξ± β Ξ²) = f :=
rfl
/-- The map `coe_fn : (r β s) β (r β s)` is injective. -/
theorem coe_fn_injective : @function.injective (Ξ± β Ξ²) (Ξ± β Ξ²) coe_fn
| β¨fβ, gβ, lβ, rββ© β¨fβ, gβ, lβ, rββ© h :=
have fβ = fβ, from h,
have gβ = gβ, from lβ.eq_right_inverse (this.symm βΈ rβ),
by simp *
@[simp, norm_cast] protected lemma coe_inj {eβ eβ : Ξ± β Ξ²} : βeβ = eβ β eβ = eβ :=
coe_fn_injective.eq_iff
@[ext] lemma ext {f g : equiv Ξ± Ξ²} (H : β x, f x = g x) : f = g :=
coe_fn_injective (funext H)
protected lemma congr_arg {f : equiv Ξ± Ξ²} : Ξ {x x' : Ξ±}, x = x' β f x = f x'
| _ _ rfl := rfl
protected lemma congr_fun {f g : equiv Ξ± Ξ²} (h : f = g) (x : Ξ±) : f x = g x := h βΈ rfl
lemma ext_iff {f g : equiv Ξ± Ξ²} : f = g β β x, f x = g x :=
β¨Ξ» h x, h βΈ rfl, extβ©
@[ext] lemma perm.ext {Ο Ο : equiv.perm Ξ±} (H : β x, Ο x = Ο x) : Ο = Ο :=
equiv.ext H
protected lemma perm.congr_arg {f : equiv.perm Ξ±} {x x' : Ξ±} : x = x' β f x = f x' :=
equiv.congr_arg
protected lemma perm.congr_fun {f g : equiv.perm Ξ±} (h : f = g) (x : Ξ±) : f x = g x :=
equiv.congr_fun h x
lemma perm.ext_iff {Ο Ο : equiv.perm Ξ±} : Ο = Ο β β x, Ο x = Ο x :=
ext_iff
/-- Any type is equivalent to itself. -/
@[refl] protected def refl (Ξ± : Sort*) : Ξ± β Ξ± := β¨id, id, Ξ» x, rfl, Ξ» x, rflβ©
instance inhabited' : inhabited (Ξ± β Ξ±) := β¨equiv.refl Ξ±β©
/-- Inverse of an equivalence `e : Ξ± β Ξ²`. -/
@[symm] protected def symm (e : Ξ± β Ξ²) : Ξ² β Ξ± := β¨e.inv_fun, e.to_fun, e.right_inv, e.left_invβ©
/-- See Note [custom simps projection] -/
def simps.symm_apply (e : Ξ± β Ξ²) : Ξ² β Ξ± := e.symm
initialize_simps_projections equiv (to_fun β apply, inv_fun β symm_apply)
-- Generate the `simps` projections for previously defined equivs.
attribute [simps] function.involutive.to_equiv
/-- Composition of equivalences `eβ : Ξ± β Ξ²` and `eβ : Ξ² β Ξ³`. -/
@[trans] protected def trans (eβ : Ξ± β Ξ²) (eβ : Ξ² β Ξ³) : Ξ± β Ξ³ :=
β¨eβ β eβ, eβ.symm β eβ.symm,
eβ.left_inv.comp eβ.left_inv, eβ.right_inv.comp eβ.right_invβ©
@[simp]
lemma to_fun_as_coe (e : Ξ± β Ξ²) : e.to_fun = e := rfl
@[simp]
lemma inv_fun_as_coe (e : Ξ± β Ξ²) : e.inv_fun = e.symm := rfl
protected theorem injective (e : Ξ± β Ξ²) : injective e :=
e.left_inv.injective
protected theorem surjective (e : Ξ± β Ξ²) : surjective e :=
e.right_inv.surjective
protected theorem bijective (f : Ξ± β Ξ²) : bijective f :=
β¨f.injective, f.surjectiveβ©
@[simp] lemma range_eq_univ {Ξ± : Type*} {Ξ² : Type*} (e : Ξ± β Ξ²) : set.range e = set.univ :=
set.eq_univ_of_forall e.surjective
protected theorem subsingleton (e : Ξ± β Ξ²) [subsingleton Ξ²] : subsingleton Ξ± :=
e.injective.subsingleton
protected theorem subsingleton.symm (e : Ξ± β Ξ²) [subsingleton Ξ±] : subsingleton Ξ² :=
e.symm.injective.subsingleton
lemma subsingleton_congr (e : Ξ± β Ξ²) : subsingleton Ξ± β subsingleton Ξ² :=
β¨Ξ» h, by exactI e.symm.subsingleton, Ξ» h, by exactI e.subsingletonβ©
instance equiv_subsingleton_cod [subsingleton Ξ²] :
subsingleton (Ξ± β Ξ²) :=
β¨Ξ» f g, equiv.ext $ Ξ» x, subsingleton.elim _ _β©
instance equiv_subsingleton_dom [subsingleton Ξ±] :
subsingleton (Ξ± β Ξ²) :=
β¨Ξ» f g, equiv.ext $ Ξ» x, @subsingleton.elim _ (equiv.subsingleton.symm f) _ _β©
instance perm_unique [subsingleton Ξ±] : unique (perm Ξ±) :=
unique_of_subsingleton (equiv.refl Ξ±)
lemma perm.subsingleton_eq_refl [subsingleton Ξ±] (e : perm Ξ±) :
e = equiv.refl Ξ± := subsingleton.elim _ _
/-- Transfer `decidable_eq` across an equivalence. -/
protected def decidable_eq (e : Ξ± β Ξ²) [decidable_eq Ξ²] : decidable_eq Ξ± :=
e.injective.decidable_eq
lemma nonempty_iff_nonempty (e : Ξ± β Ξ²) : nonempty Ξ± β nonempty Ξ² :=
nonempty.congr e e.symm
/-- If `Ξ± β Ξ²` and `Ξ²` is inhabited, then so is `Ξ±`. -/
protected def inhabited [inhabited Ξ²] (e : Ξ± β Ξ²) : inhabited Ξ± :=
β¨e.symm (default _)β©
/-- If `Ξ± β Ξ²` and `Ξ²` is a singleton type, then so is `Ξ±`. -/
protected def unique [unique Ξ²] (e : Ξ± β Ξ²) : unique Ξ± :=
e.symm.surjective.unique
/-- Equivalence between equal types. -/
protected def cast {Ξ± Ξ² : Sort*} (h : Ξ± = Ξ²) : Ξ± β Ξ² :=
β¨cast h, cast h.symm, Ξ» x, by { cases h, refl }, Ξ» x, by { cases h, refl }β©
@[simp] theorem coe_fn_symm_mk (f : Ξ± β Ξ²) (g l r) : ((equiv.mk f g l r).symm : Ξ² β Ξ±) = g :=
rfl
@[simp] theorem coe_refl : β(equiv.refl Ξ±) = id := rfl
@[simp] theorem perm.coe_subsingleton {Ξ± : Type*} [subsingleton Ξ±] (e : perm Ξ±) : β(e) = id :=
by rw [perm.subsingleton_eq_refl e, coe_refl]
theorem refl_apply (x : Ξ±) : equiv.refl Ξ± x = x := rfl
@[simp] theorem coe_trans (f : Ξ± β Ξ²) (g : Ξ² β Ξ³) : β(f.trans g) = g β f := rfl
theorem trans_apply (f : Ξ± β Ξ²) (g : Ξ² β Ξ³) (a : Ξ±) : (f.trans g) a = g (f a) := rfl
@[simp] theorem apply_symm_apply (e : Ξ± β Ξ²) (x : Ξ²) : e (e.symm x) = x :=
e.right_inv x
@[simp] theorem symm_apply_apply (e : Ξ± β Ξ²) (x : Ξ±) : e.symm (e x) = x :=
e.left_inv x
@[simp] theorem symm_comp_self (e : Ξ± β Ξ²) : e.symm β e = id := funext e.symm_apply_apply
@[simp] theorem self_comp_symm (e : Ξ± β Ξ²) : e β e.symm = id := funext e.apply_symm_apply
@[simp] lemma symm_trans_apply (f : Ξ± β Ξ²) (g : Ξ² β Ξ³) (a : Ξ³) :
(f.trans g).symm a = f.symm (g.symm a) := rfl
-- The `simp` attribute is needed to make this a `dsimp` lemma.
-- `simp` will always rewrite with `equiv.symm_symm` before this has a chance to fire.
@[simp, nolint simp_nf] theorem symm_symm_apply (f : Ξ± β Ξ²) (b : Ξ±) : f.symm.symm b = f b := rfl
@[simp] theorem apply_eq_iff_eq (f : Ξ± β Ξ²) {x y : Ξ±} : f x = f y β x = y :=
f.injective.eq_iff
theorem apply_eq_iff_eq_symm_apply {Ξ± Ξ² : Sort*} (f : Ξ± β Ξ²) {x : Ξ±} {y : Ξ²} :
f x = y β x = f.symm y :=
begin
conv_lhs { rw βapply_symm_apply f y, },
rw apply_eq_iff_eq,
end
@[simp] theorem cast_apply {Ξ± Ξ²} (h : Ξ± = Ξ²) (x : Ξ±) : equiv.cast h x = cast h x := rfl
@[simp] theorem cast_symm {Ξ± Ξ²} (h : Ξ± = Ξ²) : (equiv.cast h).symm = equiv.cast h.symm := rfl
@[simp] theorem cast_refl {Ξ±} (h : Ξ± = Ξ± := rfl) : equiv.cast h = equiv.refl Ξ± := rfl
@[simp] theorem cast_trans {Ξ± Ξ² Ξ³} (h : Ξ± = Ξ²) (h2 : Ξ² = Ξ³) :
(equiv.cast h).trans (equiv.cast h2) = equiv.cast (h.trans h2) :=
ext $ Ξ» x, by { substs h h2, refl }
lemma cast_eq_iff_heq {Ξ± Ξ²} (h : Ξ± = Ξ²) {a : Ξ±} {b : Ξ²} : equiv.cast h a = b β a == b :=
by { subst h, simp }
lemma symm_apply_eq {Ξ± Ξ²} (e : Ξ± β Ξ²) {x y} : e.symm x = y β x = e y :=
β¨Ξ» H, by simp [H.symm], Ξ» H, by simp [H]β©
lemma eq_symm_apply {Ξ± Ξ²} (e : Ξ± β Ξ²) {x y} : y = e.symm x β e y = x :=
(eq_comm.trans e.symm_apply_eq).trans eq_comm
@[simp] theorem symm_symm (e : Ξ± β Ξ²) : e.symm.symm = e := by { cases e, refl }
@[simp] theorem trans_refl (e : Ξ± β Ξ²) : e.trans (equiv.refl Ξ²) = e := by { cases e, refl }
@[simp] theorem refl_symm : (equiv.refl Ξ±).symm = equiv.refl Ξ± := rfl
@[simp] theorem refl_trans (e : Ξ± β Ξ²) : (equiv.refl Ξ±).trans e = e := by { cases e, refl }
@[simp] theorem symm_trans (e : Ξ± β Ξ²) : e.symm.trans e = equiv.refl Ξ² := ext (by simp)
@[simp] theorem trans_symm (e : Ξ± β Ξ²) : e.trans e.symm = equiv.refl Ξ± := ext (by simp)
lemma trans_assoc {Ξ΄} (ab : Ξ± β Ξ²) (bc : Ξ² β Ξ³) (cd : Ξ³ β Ξ΄) :
(ab.trans bc).trans cd = ab.trans (bc.trans cd) :=
equiv.ext $ assume a, rfl
theorem left_inverse_symm (f : equiv Ξ± Ξ²) : left_inverse f.symm f := f.left_inv
theorem right_inverse_symm (f : equiv Ξ± Ξ²) : function.right_inverse f.symm f := f.right_inv
@[simp] lemma injective_comp (e : Ξ± β Ξ²) (f : Ξ² β Ξ³) : injective (f β e) β injective f :=
injective.of_comp_iff' f e.bijective
@[simp] lemma comp_injective (f : Ξ± β Ξ²) (e : Ξ² β Ξ³) : injective (e β f) β injective f :=
e.injective.of_comp_iff f
@[simp] lemma surjective_comp (e : Ξ± β Ξ²) (f : Ξ² β Ξ³) : surjective (f β e) β surjective f :=
e.surjective.of_comp_iff f
@[simp] lemma comp_surjective (f : Ξ± β Ξ²) (e : Ξ² β Ξ³) : surjective (e β f) β surjective f :=
surjective.of_comp_iff' e.bijective f
@[simp] lemma bijective_comp (e : Ξ± β Ξ²) (f : Ξ² β Ξ³) : bijective (f β e) β bijective f :=
e.bijective.of_comp_iff f
@[simp] lemma comp_bijective (f : Ξ± β Ξ²) (e : Ξ² β Ξ³) : bijective (e β f) β bijective f :=
bijective.of_comp_iff' e.bijective f
/-- If `Ξ±` is equivalent to `Ξ²` and `Ξ³` is equivalent to `Ξ΄`, then the type of equivalences `Ξ± β Ξ³`
is equivalent to the type of equivalences `Ξ² β Ξ΄`. -/
def equiv_congr {Ξ΄} (ab : Ξ± β Ξ²) (cd : Ξ³ β Ξ΄) : (Ξ± β Ξ³) β (Ξ² β Ξ΄) :=
β¨ Ξ»ac, (ab.symm.trans ac).trans cd, Ξ»bd, ab.trans $ bd.trans $ cd.symm,
assume ac, by { ext x, simp }, assume ac, by { ext x, simp } β©
@[simp] lemma equiv_congr_refl {Ξ± Ξ²} :
(equiv.refl Ξ±).equiv_congr (equiv.refl Ξ²) = equiv.refl (Ξ± β Ξ²) := by { ext, refl }
@[simp] lemma equiv_congr_symm {Ξ΄} (ab : Ξ± β Ξ²) (cd : Ξ³ β Ξ΄) :
(ab.equiv_congr cd).symm = ab.symm.equiv_congr cd.symm := by { ext, refl }
@[simp] lemma equiv_congr_trans {Ξ΄ Ξ΅ ΞΆ} (ab : Ξ± β Ξ²) (de : Ξ΄ β Ξ΅) (bc : Ξ² β Ξ³) (ef : Ξ΅ β ΞΆ) :
(ab.equiv_congr de).trans (bc.equiv_congr ef) = (ab.trans bc).equiv_congr (de.trans ef) :=
by { ext, refl }
@[simp] lemma equiv_congr_refl_left {Ξ± Ξ² Ξ³} (bg : Ξ² β Ξ³) (e : Ξ± β Ξ²) :
(equiv.refl Ξ±).equiv_congr bg e = e.trans bg := rfl
@[simp] lemma equiv_congr_refl_right {Ξ± Ξ²} (ab e : Ξ± β Ξ²) :
ab.equiv_congr (equiv.refl Ξ²) e = ab.symm.trans e := rfl
@[simp] lemma equiv_congr_apply_apply {Ξ΄} (ab : Ξ± β Ξ²) (cd : Ξ³ β Ξ΄) (e : Ξ± β Ξ³) (x) :
ab.equiv_congr cd e x = cd (e (ab.symm x)) := rfl
section perm_congr
variables {Ξ±' Ξ²' : Type*} (e : Ξ±' β Ξ²')
/-- If `Ξ±` is equivalent to `Ξ²`, then `perm Ξ±` is equivalent to `perm Ξ²`. -/
def perm_congr : perm Ξ±' β perm Ξ²' :=
equiv_congr e e
lemma perm_congr_def (p : equiv.perm Ξ±') :
e.perm_congr p = (e.symm.trans p).trans e := rfl
@[simp] lemma perm_congr_refl :
e.perm_congr (equiv.refl _) = equiv.refl _ :=
by simp [perm_congr_def]
@[simp] lemma perm_congr_symm :
e.perm_congr.symm = e.symm.perm_congr := rfl
@[simp] lemma perm_congr_apply (p : equiv.perm Ξ±') (x) :
e.perm_congr p x = e (p (e.symm x)) := rfl
lemma perm_congr_symm_apply (p : equiv.perm Ξ²') (x) :
e.perm_congr.symm p x = e.symm (p (e x)) := rfl
lemma perm_congr_trans (p p' : equiv.perm Ξ±') :
(e.perm_congr p).trans (e.perm_congr p') = e.perm_congr (p.trans p') :=
by { ext, simp }
end perm_congr
protected lemma image_eq_preimage {Ξ± Ξ²} (e : Ξ± β Ξ²) (s : set Ξ±) : e '' s = e.symm β»ΒΉ' s :=
set.ext $ assume x, set.mem_image_iff_of_inverse e.left_inv e.right_inv
lemma _root_.set.mem_image_equiv {Ξ± Ξ²} {S : set Ξ±} {f : Ξ± β Ξ²} {x : Ξ²} :
x β f '' S β f.symm x β S :=
set.ext_iff.mp (f.image_eq_preimage S) x
/-- Alias for `equiv.image_eq_preimage` -/
lemma _root_.set.image_equiv_eq_preimage_symm {Ξ± Ξ²} (S : set Ξ±) (f : Ξ± β Ξ²) :
f '' S = f.symm β»ΒΉ' S :=
f.image_eq_preimage S
/-- Alias for `equiv.image_eq_preimage` -/
lemma _root_.set.preimage_equiv_eq_image_symm {Ξ± Ξ²} (S : set Ξ±) (f : Ξ² β Ξ±) :
f β»ΒΉ' S = f.symm '' S :=
(f.symm.image_eq_preimage S).symm
protected lemma subset_image {Ξ± Ξ²} (e : Ξ± β Ξ²) (s : set Ξ±) (t : set Ξ²) :
t β e '' s β e.symm '' t β s :=
by rw [set.image_subset_iff, e.image_eq_preimage]
@[simp] lemma symm_image_image {Ξ± Ξ²} (e : Ξ± β Ξ²) (s : set Ξ±) : e.symm '' (e '' s) = s :=
e.left_inverse_symm.image_image s
lemma eq_image_iff_symm_image_eq {Ξ± Ξ²} (e : Ξ± β Ξ²) (s : set Ξ±) (t : set Ξ²) :
t = e '' s β e.symm '' t = s :=
(e.symm.injective.image_injective.eq_iff' (e.symm_image_image s)).symm
@[simp] lemma image_symm_image {Ξ± Ξ²} (e : Ξ± β Ξ²) (s : set Ξ²) : e '' (e.symm '' s) = s :=
e.symm.symm_image_image s
@[simp] lemma image_preimage {Ξ± Ξ²} (e : Ξ± β Ξ²) (s : set Ξ²) : e '' (e β»ΒΉ' s) = s :=
e.surjective.image_preimage s
@[simp] lemma preimage_image {Ξ± Ξ²} (e : Ξ± β Ξ²) (s : set Ξ±) : e β»ΒΉ' (e '' s) = s :=
e.injective.preimage_image s
protected lemma image_compl {Ξ± Ξ²} (f : equiv Ξ± Ξ²) (s : set Ξ±) :
f '' sαΆ = (f '' s)αΆ :=
set.image_compl_eq f.bijective
@[simp] lemma symm_preimage_preimage {Ξ± Ξ²} (e : Ξ± β Ξ²) (s : set Ξ²) :
e.symm β»ΒΉ' (e β»ΒΉ' s) = s :=
e.right_inverse_symm.preimage_preimage s
@[simp] lemma preimage_symm_preimage {Ξ± Ξ²} (e : Ξ± β Ξ²) (s : set Ξ±) :
e β»ΒΉ' (e.symm β»ΒΉ' s) = s :=
e.left_inverse_symm.preimage_preimage s
@[simp] lemma preimage_subset {Ξ± Ξ²} (e : Ξ± β Ξ²) (s t : set Ξ²) : e β»ΒΉ' s β e β»ΒΉ' t β s β t :=
e.surjective.preimage_subset_preimage_iff
@[simp] lemma image_subset {Ξ± Ξ²} (e : Ξ± β Ξ²) (s t : set Ξ±) : e '' s β e '' t β s β t :=
set.image_subset_image_iff e.injective
@[simp] lemma image_eq_iff_eq {Ξ± Ξ²} (e : Ξ± β Ξ²) (s t : set Ξ±) : e '' s = e '' t β s = t :=
set.image_eq_image e.injective
lemma preimage_eq_iff_eq_image {Ξ± Ξ²} (e : Ξ± β Ξ²) (s t) : e β»ΒΉ' s = t β s = e '' t :=
set.preimage_eq_iff_eq_image e.bijective
lemma eq_preimage_iff_image_eq {Ξ± Ξ²} (e : Ξ± β Ξ²) (s t) : s = e β»ΒΉ' t β e '' s = t :=
set.eq_preimage_iff_image_eq e.bijective
/-- If `Ξ±` is an empty type, then it is equivalent to the `empty` type. -/
def equiv_empty (Ξ± : Sort u) [is_empty Ξ±] : Ξ± β empty :=
β¨is_empty_elim, Ξ» e, e.rec _, is_empty_elim, Ξ» e, e.rec _β©
/-- `Ξ±` is equivalent to an empty type iff `Ξ±` is empty. -/
def equiv_empty_equiv (Ξ± : Sort u) : (Ξ± β empty) β is_empty Ξ± :=
β¨Ξ» e, function.is_empty e, @equiv_empty Ξ±, Ξ» e, ext $ Ξ» x, (e x).elim, Ξ» p, rflβ©
/-- `false` is equivalent to `empty`. -/
def false_equiv_empty : false β empty :=
equiv_empty _
/-- If `Ξ±` is an empty type, then it is equivalent to the `pempty` type in any universe. -/
def {u' v'} equiv_pempty (Ξ± : Sort v') [is_empty Ξ±] : Ξ± β pempty.{u'} :=
β¨is_empty_elim, Ξ» e, e.rec _, is_empty_elim, Ξ» e, e.rec _β©
/-- `false` is equivalent to `pempty`. -/
def false_equiv_pempty : false β pempty :=
equiv_pempty _
/-- `empty` is equivalent to `pempty`. -/
def empty_equiv_pempty : empty β pempty :=
equiv_pempty _
/-- `pempty` types from any two universes are equivalent. -/
def pempty_equiv_pempty : pempty.{v} β pempty.{w} :=
equiv_pempty _
/-- The `Sort` of proofs of a true proposition is equivalent to `punit`. -/
def prop_equiv_punit {p : Prop} (h : p) : p β punit :=
β¨Ξ» x, (), Ξ» x, h, Ξ» _, rfl, Ξ» β¨β©, rflβ©
/-- `true` is equivalent to `punit`. -/
def true_equiv_punit : true β punit := prop_equiv_punit trivial
/-- `ulift Ξ±` is equivalent to `Ξ±`. -/
@[simps apply symm_apply {fully_applied := ff}]
protected def ulift {Ξ± : Type v} : ulift.{u} Ξ± β Ξ± :=
β¨ulift.down, ulift.up, ulift.up_down, Ξ» a, rflβ©
/-- `plift Ξ±` is equivalent to `Ξ±`. -/
@[simps apply symm_apply {fully_applied := ff}]
protected def plift : plift Ξ± β Ξ± :=
β¨plift.down, plift.up, plift.up_down, plift.down_upβ©
/-- equivalence of propositions is the same as iff -/
def of_iff {P Q : Prop} (h : P β Q) : P β Q :=
{ to_fun := h.mp,
inv_fun := h.mpr,
left_inv := Ξ» x, rfl,
right_inv := Ξ» y, rfl }
/-- If `Ξ±β` is equivalent to `Ξ±β` and `Ξ²β` is equivalent to `Ξ²β`, then the type of maps `Ξ±β β Ξ²β`
is equivalent to the type of maps `Ξ±β β Ξ²β`. -/
@[congr, simps apply] def arrow_congr {Ξ±β Ξ²β Ξ±β Ξ²β : Sort*} (eβ : Ξ±β β Ξ±β) (eβ : Ξ²β β Ξ²β) :
(Ξ±β β Ξ²β) β (Ξ±β β Ξ²β) :=
{ to_fun := Ξ» f, eβ β f β eβ.symm,
inv_fun := Ξ» f, eβ.symm β f β eβ,
left_inv := Ξ» f, funext $ Ξ» x, by simp,
right_inv := Ξ» f, funext $ Ξ» x, by simp }
lemma arrow_congr_comp {Ξ±β Ξ²β Ξ³β Ξ±β Ξ²β Ξ³β : Sort*}
(ea : Ξ±β β Ξ±β) (eb : Ξ²β β Ξ²β) (ec : Ξ³β β Ξ³β) (f : Ξ±β β Ξ²β) (g : Ξ²β β Ξ³β) :
arrow_congr ea ec (g β f) = (arrow_congr eb ec g) β (arrow_congr ea eb f) :=
by { ext, simp only [comp, arrow_congr_apply, eb.symm_apply_apply] }
@[simp] lemma arrow_congr_refl {Ξ± Ξ² : Sort*} :
arrow_congr (equiv.refl Ξ±) (equiv.refl Ξ²) = equiv.refl (Ξ± β Ξ²) := rfl
@[simp] lemma arrow_congr_trans {Ξ±β Ξ²β Ξ±β Ξ²β Ξ±β Ξ²β : Sort*}
(eβ : Ξ±β β Ξ±β) (eβ' : Ξ²β β Ξ²β) (eβ : Ξ±β β Ξ±β) (eβ' : Ξ²β β Ξ²β) :
arrow_congr (eβ.trans eβ) (eβ'.trans eβ') = (arrow_congr eβ eβ').trans (arrow_congr eβ eβ') :=
rfl
@[simp] lemma arrow_congr_symm {Ξ±β Ξ²β Ξ±β Ξ²β : Sort*} (eβ : Ξ±β β Ξ±β) (eβ : Ξ²β β Ξ²β) :
(arrow_congr eβ eβ).symm = arrow_congr eβ.symm eβ.symm :=
rfl
/--
A version of `equiv.arrow_congr` in `Type`, rather than `Sort`.
The `equiv_rw` tactic is not able to use the default `Sort` level `equiv.arrow_congr`,
because Lean's universe rules will not unify `?l_1` with `imax (1 ?m_1)`.
-/
@[congr, simps apply]
def arrow_congr' {Ξ±β Ξ²β Ξ±β Ξ²β : Type*} (hΞ± : Ξ±β β Ξ±β) (hΞ² : Ξ²β β Ξ²β) : (Ξ±β β Ξ²β) β (Ξ±β β Ξ²β) :=
equiv.arrow_congr hΞ± hΞ²
@[simp] lemma arrow_congr'_refl {Ξ± Ξ² : Type*} :
arrow_congr' (equiv.refl Ξ±) (equiv.refl Ξ²) = equiv.refl (Ξ± β Ξ²) := rfl
@[simp] lemma arrow_congr'_trans {Ξ±β Ξ²β Ξ±β Ξ²β Ξ±β Ξ²β : Type*}
(eβ : Ξ±β β Ξ±β) (eβ' : Ξ²β β Ξ²β) (eβ : Ξ±β β Ξ±β) (eβ' : Ξ²β β Ξ²β) :
arrow_congr' (eβ.trans eβ) (eβ'.trans eβ') = (arrow_congr' eβ eβ').trans (arrow_congr' eβ eβ') :=
rfl
@[simp] lemma arrow_congr'_symm {Ξ±β Ξ²β Ξ±β Ξ²β : Type*} (eβ : Ξ±β β Ξ±β) (eβ : Ξ²β β Ξ²β) :
(arrow_congr' eβ eβ).symm = arrow_congr' eβ.symm eβ.symm :=
rfl
/-- Conjugate a map `f : Ξ± β Ξ±` by an equivalence `Ξ± β Ξ²`. -/
@[simps apply]
def conj (e : Ξ± β Ξ²) : (Ξ± β Ξ±) β (Ξ² β Ξ²) := arrow_congr e e
@[simp] lemma conj_refl : conj (equiv.refl Ξ±) = equiv.refl (Ξ± β Ξ±) := rfl
@[simp] lemma conj_symm (e : Ξ± β Ξ²) : e.conj.symm = e.symm.conj := rfl
@[simp] lemma conj_trans (eβ : Ξ± β Ξ²) (eβ : Ξ² β Ξ³) :
(eβ.trans eβ).conj = eβ.conj.trans eβ.conj :=
rfl
-- This should not be a simp lemma as long as `(β)` is reducible:
-- when `(β)` is reducible, Lean can unify `fβ β fβ` with any `g` using
-- `fβ := g` and `fβ := Ξ» x, x`. This causes nontermination.
lemma conj_comp (e : Ξ± β Ξ²) (fβ fβ : Ξ± β Ξ±) :
e.conj (fβ β fβ) = (e.conj fβ) β (e.conj fβ) :=
by apply arrow_congr_comp
section binary_op
variables {Ξ±β Ξ²β : Type*} (e : Ξ±β β Ξ²β) (f : Ξ±β β Ξ±β β Ξ±β)
lemma semiconj_conj (f : Ξ±β β Ξ±β) : semiconj e f (e.conj f) := Ξ» x, by simp
lemma semiconjβ_conj : semiconjβ e f (e.arrow_congr e.conj f) := Ξ» x y, by simp
instance [is_associative Ξ±β f] :
is_associative Ξ²β (e.arrow_congr (e.arrow_congr e) f) :=
(e.semiconjβ_conj f).is_associative_right e.surjective
instance [is_idempotent Ξ±β f] :
is_idempotent Ξ²β (e.arrow_congr (e.arrow_congr e) f) :=
(e.semiconjβ_conj f).is_idempotent_right e.surjective
instance [is_left_cancel Ξ±β f] :
is_left_cancel Ξ²β (e.arrow_congr (e.arrow_congr e) f) :=
β¨e.surjective.forallβ.2 $ Ξ» x y z, by simpa using @is_left_cancel.left_cancel _ f _ x y zβ©
instance [is_right_cancel Ξ±β f] :
is_right_cancel Ξ²β (e.arrow_congr (e.arrow_congr e) f) :=
β¨e.surjective.forallβ.2 $ Ξ» x y z, by simpa using @is_right_cancel.right_cancel _ f _ x y zβ©
end binary_op
/-- `punit` sorts in any two universes are equivalent. -/
def punit_equiv_punit : punit.{v} β punit.{w} :=
β¨Ξ» _, punit.star, Ξ» _, punit.star, Ξ» u, by { cases u, refl }, Ξ» u, by { cases u, reflexivity }β©
section
/-- The sort of maps to `punit.{v}` is equivalent to `punit.{w}`. -/
def arrow_punit_equiv_punit (Ξ± : Sort*) : (Ξ± β punit.{v}) β punit.{w} :=
β¨Ξ» f, punit.star, Ξ» u f, punit.star,
Ξ» f, by { funext x, cases f x, refl }, Ξ» u, by { cases u, reflexivity }β©
/-- If `Ξ±` has a unique term, then the type of function `Ξ± β Ξ²` is equivalent to `Ξ²`. -/
@[simps] def fun_unique (Ξ± Ξ²) [unique Ξ±] : (Ξ± β Ξ²) β Ξ² :=
{ to_fun := Ξ» f, f (default Ξ±),
inv_fun := Ξ» b a, b,
left_inv := Ξ» f, funext $ Ξ» a, congr_arg f $ subsingleton.elim _ _,
right_inv := Ξ» b, rfl }
/-- The sort of maps from `punit` is equivalent to the codomain. -/
def punit_arrow_equiv (Ξ± : Sort*) : (punit.{u} β Ξ±) β Ξ± :=
fun_unique _ _
/-- The sort of maps from `true` is equivalent to the codomain. -/
def true_arrow_equiv (Ξ± : Sort*) : (true β Ξ±) β Ξ± :=
fun_unique _ _
/-- The sort of maps from a type that `is_empty` is equivalent to `punit`. -/
def arrow_punit_of_is_empty (Ξ± Ξ² : Sort*) [is_empty Ξ±] : (Ξ± β Ξ²) β punit.{u} :=
β¨Ξ» f, punit.star, Ξ» u, is_empty_elim, Ξ» f, funext is_empty_elim, Ξ» u, by { cases u, refl }β©
/-- The sort of maps from `empty` is equivalent to `punit`. -/
def empty_arrow_equiv_punit (Ξ± : Sort*) : (empty β Ξ±) β punit.{u} :=
arrow_punit_of_is_empty _ _
/-- The sort of maps from `pempty` is equivalent to `punit`. -/
def pempty_arrow_equiv_punit (Ξ± : Sort*) : (pempty β Ξ±) β punit.{u} :=
arrow_punit_of_is_empty _ _
/-- The sort of maps from `false` is equivalent to `punit`. -/
def false_arrow_equiv_punit (Ξ± : Sort*) : (false β Ξ±) β punit.{u} :=
arrow_punit_of_is_empty _ _
end
/-- Product of two equivalences. If `Ξ±β β Ξ±β` and `Ξ²β β Ξ²β`, then `Ξ±β Γ Ξ²β β Ξ±β Γ Ξ²β`. -/
@[congr, simps apply]
def prod_congr {Ξ±β Ξ²β Ξ±β Ξ²β : Type*} (eβ : Ξ±β β Ξ±β) (eβ : Ξ²β β Ξ²β) : Ξ±β Γ Ξ²β β Ξ±β Γ Ξ²β :=
β¨prod.map eβ eβ, prod.map eβ.symm eβ.symm, Ξ» β¨a, bβ©, by simp, Ξ» β¨a, bβ©, by simpβ©
@[simp] theorem prod_congr_symm {Ξ±β Ξ²β Ξ±β Ξ²β : Type*} (eβ : Ξ±β β Ξ±β) (eβ : Ξ²β β Ξ²β) :
(prod_congr eβ eβ).symm = prod_congr eβ.symm eβ.symm :=
rfl
/-- Type product is commutative up to an equivalence: `Ξ± Γ Ξ² β Ξ² Γ Ξ±`. -/
@[simps apply] def prod_comm (Ξ± Ξ² : Type*) : Ξ± Γ Ξ² β Ξ² Γ Ξ± :=
β¨prod.swap, prod.swap, Ξ»β¨a, bβ©, rfl, Ξ»β¨a, bβ©, rflβ©
@[simp] lemma prod_comm_symm (Ξ± Ξ²) : (prod_comm Ξ± Ξ²).symm = prod_comm Ξ² Ξ± := rfl
/-- Type product is associative up to an equivalence. -/
@[simps] def prod_assoc (Ξ± Ξ² Ξ³ : Sort*) : (Ξ± Γ Ξ²) Γ Ξ³ β Ξ± Γ (Ξ² Γ Ξ³) :=
β¨Ξ» p, (p.1.1, p.1.2, p.2), Ξ»p, ((p.1, p.2.1), p.2.2), Ξ» β¨β¨a, bβ©, cβ©, rfl, Ξ» β¨a, β¨b, cβ©β©, rflβ©
lemma prod_assoc_preimage {Ξ± Ξ² Ξ³} {s : set Ξ±} {t : set Ξ²} {u : set Ξ³} :
equiv.prod_assoc Ξ± Ξ² Ξ³ β»ΒΉ' s.prod (t.prod u) = (s.prod t).prod u :=
by { ext, simp [and_assoc] }
/-- Functions on `Ξ± Γ Ξ²` are equivalent to functions `Ξ± β Ξ² β Ξ³`. -/
@[simps {fully_applied := ff}] def curry (Ξ± Ξ² Ξ³ : Type*) :
(Ξ± Γ Ξ² β Ξ³) β (Ξ± β Ξ² β Ξ³) :=
{ to_fun := curry,
inv_fun := uncurry,
left_inv := uncurry_curry,
right_inv := curry_uncurry }
section
/-- `punit` is a right identity for type product up to an equivalence. -/
@[simps] def prod_punit (Ξ± : Type*) : Ξ± Γ punit.{u+1} β Ξ± :=
β¨Ξ» p, p.1, Ξ» a, (a, punit.star), Ξ» β¨_, punit.starβ©, rfl, Ξ» a, rflβ©
/-- `punit` is a left identity for type product up to an equivalence. -/
@[simps] def punit_prod (Ξ± : Type*) : punit.{u+1} Γ Ξ± β Ξ± :=
calc punit Γ Ξ± β Ξ± Γ punit : prod_comm _ _
... β Ξ± : prod_punit _
/-- `empty` type is a right absorbing element for type product up to an equivalence. -/
def prod_empty (Ξ± : Type*) : Ξ± Γ empty β empty :=
equiv_empty _
/-- `empty` type is a left absorbing element for type product up to an equivalence. -/
def empty_prod (Ξ± : Type*) : empty Γ Ξ± β empty :=
equiv_empty _
/-- `pempty` type is a right absorbing element for type product up to an equivalence. -/
def prod_pempty (Ξ± : Type*) : Ξ± Γ pempty β pempty :=
equiv_pempty _
/-- `pempty` type is a left absorbing element for type product up to an equivalence. -/
def pempty_prod (Ξ± : Type*) : pempty Γ Ξ± β pempty :=
equiv_pempty _
end
section
open sum
/-- `psum` is equivalent to `sum`. -/
def psum_equiv_sum (Ξ± Ξ² : Type*) : psum Ξ± Ξ² β Ξ± β Ξ² :=
β¨Ξ» s, psum.cases_on s inl inr,
Ξ» s, sum.cases_on s psum.inl psum.inr,
Ξ» s, by cases s; refl,
Ξ» s, by cases s; reflβ©
/-- If `Ξ± β Ξ±'` and `Ξ² β Ξ²'`, then `Ξ± β Ξ² β Ξ±' β Ξ²'`. -/
@[simps apply]
def sum_congr {Ξ±β Ξ²β Ξ±β Ξ²β : Type*} (ea : Ξ±β β Ξ±β) (eb : Ξ²β β Ξ²β) : Ξ±β β Ξ²β β Ξ±β β Ξ²β :=
β¨sum.map ea eb, sum.map ea.symm eb.symm, Ξ» x, by simp, Ξ» x, by simpβ©
@[simp] lemma sum_congr_trans {Ξ±β Ξ±β Ξ²β Ξ²β Ξ³β Ξ³β : Sort*}
(e : Ξ±β β Ξ²β) (f : Ξ±β β Ξ²β) (g : Ξ²β β Ξ³β) (h : Ξ²β β Ξ³β) :
(equiv.sum_congr e f).trans (equiv.sum_congr g h) = (equiv.sum_congr (e.trans g) (f.trans h)) :=
by { ext i, cases i; refl }
@[simp] lemma sum_congr_symm {Ξ± Ξ² Ξ³ Ξ΄ : Sort*} (e : Ξ± β Ξ²) (f : Ξ³ β Ξ΄) :
(equiv.sum_congr e f).symm = (equiv.sum_congr (e.symm) (f.symm)) :=
rfl
@[simp] lemma sum_congr_refl {Ξ± Ξ² : Sort*} :
equiv.sum_congr (equiv.refl Ξ±) (equiv.refl Ξ²) = equiv.refl (Ξ± β Ξ²) :=
by { ext i, cases i; refl }
namespace perm
/-- Combine a permutation of `Ξ±` and of `Ξ²` into a permutation of `Ξ± β Ξ²`. -/
@[reducible]
def sum_congr {Ξ± Ξ² : Type*} (ea : equiv.perm Ξ±) (eb : equiv.perm Ξ²) : equiv.perm (Ξ± β Ξ²) :=
equiv.sum_congr ea eb
@[simp] lemma sum_congr_apply {Ξ± Ξ² : Type*} (ea : equiv.perm Ξ±) (eb : equiv.perm Ξ²) (x : Ξ± β Ξ²) :
sum_congr ea eb x = sum.map βea βeb x := equiv.sum_congr_apply ea eb x
@[simp] lemma sum_congr_trans {Ξ± Ξ² : Sort*}
(e : equiv.perm Ξ±) (f : equiv.perm Ξ²) (g : equiv.perm Ξ±) (h : equiv.perm Ξ²) :
(sum_congr e f).trans (sum_congr g h) = sum_congr (e.trans g) (f.trans h) :=
equiv.sum_congr_trans e f g h
@[simp] lemma sum_congr_symm {Ξ± Ξ² : Sort*} (e : equiv.perm Ξ±) (f : equiv.perm Ξ²) :
(sum_congr e f).symm = sum_congr (e.symm) (f.symm) :=
equiv.sum_congr_symm e f
@[simp] lemma sum_congr_refl {Ξ± Ξ² : Sort*} :
sum_congr (equiv.refl Ξ±) (equiv.refl Ξ²) = equiv.refl (Ξ± β Ξ²) :=
equiv.sum_congr_refl
end perm
/-- `bool` is equivalent the sum of two `punit`s. -/
def bool_equiv_punit_sum_punit : bool β punit.{u+1} β punit.{v+1} :=
β¨Ξ» b, cond b (inr punit.star) (inl punit.star),
Ξ» s, sum.rec_on s (Ξ»_, ff) (Ξ»_, tt),
Ξ» b, by cases b; refl,
Ξ» s, by rcases s with β¨β¨β©β© | β¨β¨β©β©; reflβ©
/-- `Prop` is noncomputably equivalent to `bool`. -/
noncomputable def Prop_equiv_bool : Prop β bool :=
β¨Ξ» p, @to_bool p (classical.prop_decidable _),
Ξ» b, b, Ξ» p, by simp, Ξ» b, by simpβ©
/-- Sum of types is commutative up to an equivalence. -/
@[simps apply]
def sum_comm (Ξ± Ξ² : Sort*) : Ξ± β Ξ² β Ξ² β Ξ± :=
β¨sum.swap, sum.swap, sum.swap_swap, sum.swap_swapβ©
@[simp] lemma sum_comm_symm (Ξ± Ξ²) : (sum_comm Ξ± Ξ²).symm = sum_comm Ξ² Ξ± := rfl
/-- Sum of types is associative up to an equivalence. -/
def sum_assoc (Ξ± Ξ² Ξ³ : Sort*) : (Ξ± β Ξ²) β Ξ³ β Ξ± β (Ξ² β Ξ³) :=
β¨sum.elim (sum.elim sum.inl (sum.inr β sum.inl)) (sum.inr β sum.inr),
sum.elim (sum.inl β sum.inl) $ sum.elim (sum.inl β sum.inr) sum.inr,
by rintros (β¨_ | _β© | _); refl,
by rintros (_ | β¨_ | _β©); reflβ©
@[simp] theorem sum_assoc_apply_in1 {Ξ± Ξ² Ξ³} (a) : sum_assoc Ξ± Ξ² Ξ³ (inl (inl a)) = inl a := rfl
@[simp] theorem sum_assoc_apply_in2 {Ξ± Ξ² Ξ³} (b) : sum_assoc Ξ± Ξ² Ξ³ (inl (inr b)) = inr (inl b) := rfl
@[simp] theorem sum_assoc_apply_in3 {Ξ± Ξ² Ξ³} (c) : sum_assoc Ξ± Ξ² Ξ³ (inr c) = inr (inr c) := rfl
/-- Sum with `empty` is equivalent to the original type. -/
@[simps symm_apply] def sum_empty (Ξ± Ξ² : Type*) [is_empty Ξ²] : Ξ± β Ξ² β Ξ± :=
β¨sum.elim id is_empty_elim,
inl,
Ξ» s, by { rcases s with _ | x, refl, exact is_empty_elim x },
Ξ» a, rflβ©
@[simp] lemma sum_empty_apply_inl {Ξ± Ξ² : Type*} [is_empty Ξ²] (a : Ξ±) :
sum_empty Ξ± Ξ² (sum.inl a) = a := rfl
/-- The sum of `empty` with any `Sort*` is equivalent to the right summand. -/
@[simps symm_apply] def empty_sum (Ξ± Ξ² : Type*) [is_empty Ξ±] : Ξ± β Ξ² β Ξ² :=
(sum_comm _ _).trans $ sum_empty _ _
@[simp] lemma empty_sum_apply_inr {Ξ± Ξ² : Type*} [is_empty Ξ±] (b : Ξ²) :
empty_sum Ξ± Ξ² (sum.inr b) = b := rfl
/-- `option Ξ±` is equivalent to `Ξ± β punit` -/
def option_equiv_sum_punit (Ξ± : Type*) : option Ξ± β Ξ± β punit.{u+1} :=
β¨Ξ» o, match o with none := inr punit.star | some a := inl a end,
Ξ» s, match s with inr _ := none | inl a := some a end,
Ξ» o, by cases o; refl,
Ξ» s, by rcases s with _ | β¨β¨β©β©; reflβ©
@[simp] lemma option_equiv_sum_punit_none {Ξ±} :
option_equiv_sum_punit Ξ± none = sum.inr punit.star := rfl
@[simp] lemma option_equiv_sum_punit_some {Ξ±} (a) :
option_equiv_sum_punit Ξ± (some a) = sum.inl a := rfl
@[simp] lemma option_equiv_sum_punit_coe {Ξ±} (a : Ξ±) :
option_equiv_sum_punit Ξ± a = sum.inl a := rfl
@[simp] lemma option_equiv_sum_punit_symm_inl {Ξ±} (a) :
(option_equiv_sum_punit Ξ±).symm (sum.inl a) = a :=
rfl
@[simp] lemma option_equiv_sum_punit_symm_inr {Ξ±} (a) :
(option_equiv_sum_punit Ξ±).symm (sum.inr a) = none :=
rfl
/-- The set of `x : option Ξ±` such that `is_some x` is equivalent to `Ξ±`. -/
def option_is_some_equiv (Ξ± : Type*) : {x : option Ξ± // x.is_some} β Ξ± :=
{ to_fun := Ξ» o, option.get o.2,
inv_fun := Ξ» x, β¨some x, dec_trivialβ©,
left_inv := Ξ» o, subtype.eq $ option.some_get _,
right_inv := Ξ» x, option.get_some _ _ }
/-- `Ξ± β Ξ²` is equivalent to a `sigma`-type over `bool`. Note that this definition assumes `Ξ±` and
`Ξ²` to be types from the same universe, so it cannot by used directly to transfer theorems about
sigma types to theorems about sum types. In many cases one can use `ulift` to work around this
difficulty. -/
def sum_equiv_sigma_bool (Ξ± Ξ² : Type u) : Ξ± β Ξ² β (Ξ£ b: bool, cond b Ξ± Ξ²) :=
β¨Ξ» s, s.elim (Ξ» x, β¨tt, xβ©) (Ξ» x, β¨ff, xβ©),
Ξ» s, match s with β¨tt, aβ© := inl a | β¨ff, bβ© := inr b end,
Ξ» s, by cases s; refl,
Ξ» s, by rcases s with β¨_|_, _β©; reflβ©
/-- `sigma_preimage_equiv f` for `f : Ξ± β Ξ²` is the natural equivalence between
the type of all fibres of `f` and the total space `Ξ±`. -/
@[simps]
def sigma_preimage_equiv {Ξ± Ξ² : Type*} (f : Ξ± β Ξ²) :
(Ξ£ y : Ξ², {x // f x = y}) β Ξ± :=
β¨Ξ» x, βx.2, Ξ» x, β¨f x, x, rflβ©, Ξ» β¨y, x, rflβ©, rfl, Ξ» x, rflβ©
/-- A set `s` in `Ξ± Γ Ξ²` is equivalent to the sigma-type `Ξ£ x, {y | (x, y) β s}`. -/
def set_prod_equiv_sigma {Ξ± Ξ² : Type*} (s : set (Ξ± Γ Ξ²)) :
s β Ξ£ x : Ξ±, {y | (x, y) β s} :=
{ to_fun := Ξ» x, β¨x.1.1, x.1.2, by simpβ©,
inv_fun := Ξ» x, β¨(x.1, x.2.1), x.2.2β©,
left_inv := Ξ» β¨β¨x, yβ©, hβ©, rfl,
right_inv := Ξ» β¨x, y, hβ©, rfl }
end
section sum_compl
/-- For any predicate `p` on `Ξ±`,
the sum of the two subtypes `{a // p a}` and its complement `{a // Β¬ p a}`
is naturally equivalent to `Ξ±`.
See `subtype_or_equiv` for sum types over subtypes `{x // p x}` and `{x // q x}`
that are not necessarily `is_compl p q`. -/
def sum_compl {Ξ± : Type*} (p : Ξ± β Prop) [decidable_pred p] :
{a // p a} β {a // Β¬ p a} β Ξ± :=
{ to_fun := sum.elim coe coe,
inv_fun := Ξ» a, if h : p a then sum.inl β¨a, hβ© else sum.inr β¨a, hβ©,
left_inv := by { rintros (β¨x,hxβ©|β¨x,hxβ©); dsimp; [rw dif_pos, rw dif_neg], },
right_inv := Ξ» a, by { dsimp, split_ifs; refl } }
@[simp] lemma sum_compl_apply_inl {Ξ± : Type*} (p : Ξ± β Prop) [decidable_pred p]
(x : {a // p a}) :
sum_compl p (sum.inl x) = x := rfl
@[simp] lemma sum_compl_apply_inr {Ξ± : Type*} (p : Ξ± β Prop) [decidable_pred p]
(x : {a // Β¬ p a}) :
sum_compl p (sum.inr x) = x := rfl
@[simp] lemma sum_compl_apply_symm_of_pos {Ξ± : Type*} (p : Ξ± β Prop) [decidable_pred p]
(a : Ξ±) (h : p a) :
(sum_compl p).symm a = sum.inl β¨a, hβ© := dif_pos h
@[simp] lemma sum_compl_apply_symm_of_neg {Ξ± : Type*} (p : Ξ± β Prop) [decidable_pred p]
(a : Ξ±) (h : Β¬ p a) :
(sum_compl p).symm a = sum.inr β¨a, hβ© := dif_neg h
/-- Combines an `equiv` between two subtypes with an `equiv` between their complements to form a
permutation. -/
def subtype_congr {Ξ± : Type*} {p q : Ξ± β Prop} [decidable_pred p] [decidable_pred q]
(e : {x // p x} β {x // q x}) (f : {x // Β¬p x} β {x // Β¬q x}) : perm Ξ± :=
(sum_compl p).symm.trans ((sum_congr e f).trans
(sum_compl q))
open equiv
variables {Ξ΅ : Type*} {p : Ξ΅ β Prop} [decidable_pred p]
variables (ep ep' : perm {a // p a}) (en en' : perm {a // Β¬ p a})
/-- Combining permutations on `Ξ΅` that permute only inside or outside the subtype
split induced by `p : Ξ΅ β Prop` constructs a permutation on `Ξ΅`. -/
def perm.subtype_congr : equiv.perm Ξ΅ :=
perm_congr (sum_compl p) (sum_congr ep en)
lemma perm.subtype_congr.apply (a : Ξ΅) :
ep.subtype_congr en a = if h : p a then ep β¨a, hβ© else en β¨a, hβ© :=
by { by_cases h : p a; simp [perm.subtype_congr, h] }
@[simp] lemma perm.subtype_congr.left_apply {a : Ξ΅} (h : p a) :
ep.subtype_congr en a = ep β¨a, hβ© :=
by simp [perm.subtype_congr.apply, h]
@[simp] lemma perm.subtype_congr.left_apply_subtype (a : {a // p a}) :
ep.subtype_congr en a = ep a :=
by { convert perm.subtype_congr.left_apply _ _ a.property, simp }
@[simp] lemma perm.subtype_congr.right_apply {a : Ξ΅} (h : Β¬ p a) :
ep.subtype_congr en a = en β¨a, hβ© :=
by simp [perm.subtype_congr.apply, h]
@[simp] lemma perm.subtype_congr.right_apply_subtype (a : {a // Β¬ p a}) :
ep.subtype_congr en a = en a :=
by { convert perm.subtype_congr.right_apply _ _ a.property, simp }
@[simp] lemma perm.subtype_congr.refl :
perm.subtype_congr (equiv.refl {a // p a}) (equiv.refl {a // Β¬ p a}) = equiv.refl Ξ΅ :=
by { ext x, by_cases h : p x; simp [h] }
@[simp] lemma perm.subtype_congr.symm :
(ep.subtype_congr en).symm = perm.subtype_congr ep.symm en.symm :=
begin
ext x,
by_cases h : p x,
{ have : p (ep.symm β¨x, hβ©) := subtype.property _,
simp [perm.subtype_congr.apply, h, symm_apply_eq, this] },
{ have : Β¬ p (en.symm β¨x, hβ©) := subtype.property (en.symm _),
simp [perm.subtype_congr.apply, h, symm_apply_eq, this] }
end
@[simp] lemma perm.subtype_congr.trans :
(ep.subtype_congr en).trans (ep'.subtype_congr en') =
perm.subtype_congr (ep.trans ep') (en.trans en') :=
begin
ext x,
by_cases h : p x,
{ have : p (ep β¨x, hβ©) := subtype.property _,
simp [perm.subtype_congr.apply, h, this] },
{ have : Β¬ p (en β¨x, hβ©) := subtype.property (en _),
simp [perm.subtype_congr.apply, h, symm_apply_eq, this] }
end
end sum_compl
section subtype_preimage
variables (p : Ξ± β Prop) [decidable_pred p] (xβ : {a // p a} β Ξ²)
/-- For a fixed function `xβ : {a // p a} β Ξ²` defined on a subtype of `Ξ±`,
the subtype of functions `x : Ξ± β Ξ²` that agree with `xβ` on the subtype `{a // p a}`
is naturally equivalent to the type of functions `{a // Β¬ p a} β Ξ²`. -/
@[simps]
def subtype_preimage :
{x : Ξ± β Ξ² // x β coe = xβ} β ({a // Β¬ p a} β Ξ²) :=
{ to_fun := Ξ» (x : {x : Ξ± β Ξ² // x β coe = xβ}) a, (x : Ξ± β Ξ²) a,
inv_fun := Ξ» x, β¨Ξ» a, if h : p a then xβ β¨a, hβ© else x β¨a, hβ©,
funext $ Ξ» β¨a, hβ©, dif_pos hβ©,
left_inv := Ξ» β¨x, hxβ©, subtype.val_injective $ funext $ Ξ» a,
(by { dsimp, split_ifs; [ rw β hx, skip ]; refl }),
right_inv := Ξ» x, funext $ Ξ» β¨a, hβ©,
show dite (p a) _ _ = _, by { dsimp, rw [dif_neg h] } }
lemma subtype_preimage_symm_apply_coe_pos (x : {a // Β¬ p a} β Ξ²) (a : Ξ±) (h : p a) :
((subtype_preimage p xβ).symm x : Ξ± β Ξ²) a = xβ β¨a, hβ© :=
dif_pos h
lemma subtype_preimage_symm_apply_coe_neg (x : {a // Β¬ p a} β Ξ²) (a : Ξ±) (h : Β¬ p a) :
((subtype_preimage p xβ).symm x : Ξ± β Ξ²) a = x β¨a, hβ© :=
dif_neg h
end subtype_preimage
section
/-- A family of equivalences `Ξ a, Ξ²β a β Ξ²β a` generates an equivalence between `Ξ a, Ξ²β a` and
`Ξ a, Ξ²β a`. -/
def Pi_congr_right {Ξ±} {Ξ²β Ξ²β : Ξ± β Sort*} (F : Ξ a, Ξ²β a β Ξ²β a) : (Ξ a, Ξ²β a) β (Ξ a, Ξ²β a) :=
β¨Ξ» H a, F a (H a), Ξ» H a, (F a).symm (H a),
Ξ» H, funext $ by simp, Ξ» H, funext $ by simpβ©
/-- Dependent `curry` equivalence: the type of dependent functions on `Ξ£ i, Ξ² i` is equivalent
to the type of dependent functions of two arguments (i.e., functions to the space of functions).
This is `sigma.curry` and `sigma.uncurry` together as an equiv. -/
def Pi_curry {Ξ±} {Ξ² : Ξ± β Sort*} (Ξ³ : Ξ a, Ξ² a β Sort*) :
(Ξ x : Ξ£ i, Ξ² i, Ξ³ x.1 x.2) β (Ξ a b, Ξ³ a b) :=
{ to_fun := sigma.curry,
inv_fun := sigma.uncurry,
left_inv := sigma.uncurry_curry,
right_inv := sigma.curry_uncurry }
end
section
/-- A `psigma`-type is equivalent to the corresponding `sigma`-type. -/
@[simps apply symm_apply] def psigma_equiv_sigma {Ξ±} (Ξ² : Ξ± β Sort*) : (Ξ£' i, Ξ² i) β Ξ£ i, Ξ² i :=
β¨Ξ» a, β¨a.1, a.2β©, Ξ» a, β¨a.1, a.2β©, Ξ» β¨a, bβ©, rfl, Ξ» β¨a, bβ©, rflβ©
/-- A family of equivalences `Ξ a, Ξ²β a β Ξ²β a` generates an equivalence between `Ξ£ a, Ξ²β a` and
`Ξ£ a, Ξ²β a`. -/
@[simps apply]
def sigma_congr_right {Ξ±} {Ξ²β Ξ²β : Ξ± β Sort*} (F : Ξ a, Ξ²β a β Ξ²β a) : (Ξ£ a, Ξ²β a) β Ξ£ a, Ξ²β a :=
β¨Ξ» a, β¨a.1, F a.1 a.2β©, Ξ» a, β¨a.1, (F a.1).symm a.2β©,
Ξ» β¨a, bβ©, congr_arg (sigma.mk a) $ symm_apply_apply (F a) b,
Ξ» β¨a, bβ©, congr_arg (sigma.mk a) $ apply_symm_apply (F a) bβ©
@[simp] lemma sigma_congr_right_trans {Ξ±} {Ξ²β Ξ²β Ξ²β : Ξ± β Sort*}
(F : Ξ a, Ξ²β a β Ξ²β a) (G : Ξ a, Ξ²β a β Ξ²β a) :
(sigma_congr_right F).trans (sigma_congr_right G) = sigma_congr_right (Ξ» a, (F a).trans (G a)) :=
by { ext1 x, cases x, refl }
@[simp] lemma sigma_congr_right_symm {Ξ±} {Ξ²β Ξ²β : Ξ± β Sort*} (F : Ξ a, Ξ²β a β Ξ²β a) :
(sigma_congr_right F).symm = sigma_congr_right (Ξ» a, (F a).symm) :=
by { ext1 x, cases x, refl }
@[simp] lemma sigma_congr_right_refl {Ξ±} {Ξ² : Ξ± β Sort*} :
(sigma_congr_right (Ξ» a, equiv.refl (Ξ² a))) = equiv.refl (Ξ£ a, Ξ² a) :=
by { ext1 x, cases x, refl }
namespace perm
/-- A family of permutations `Ξ a, perm (Ξ² a)` generates a permuation `perm (Ξ£ a, Ξ²β a)`. -/
@[reducible]
def sigma_congr_right {Ξ±} {Ξ² : Ξ± β Sort*} (F : Ξ a, perm (Ξ² a)) : perm (Ξ£ a, Ξ² a) :=
equiv.sigma_congr_right F
@[simp] lemma sigma_congr_right_trans {Ξ±} {Ξ² : Ξ± β Sort*}
(F : Ξ a, perm (Ξ² a)) (G : Ξ a, perm (Ξ² a)) :
(sigma_congr_right F).trans (sigma_congr_right G) = sigma_congr_right (Ξ» a, (F a).trans (G a)) :=
equiv.sigma_congr_right_trans F G
@[simp] lemma sigma_congr_right_symm {Ξ±} {Ξ² : Ξ± β Sort*} (F : Ξ a, perm (Ξ² a)) :
(sigma_congr_right F).symm = sigma_congr_right (Ξ» a, (F a).symm) :=
equiv.sigma_congr_right_symm F
@[simp] lemma sigma_congr_right_refl {Ξ±} {Ξ² : Ξ± β Sort*} :
(sigma_congr_right (Ξ» a, equiv.refl (Ξ² a))) = equiv.refl (Ξ£ a, Ξ² a) :=
equiv.sigma_congr_right_refl
end perm
/-- An equivalence `f : Ξ±β β Ξ±β` generates an equivalence between `Ξ£ a, Ξ² (f a)` and `Ξ£ a, Ξ² a`. -/
@[simps apply]
def sigma_congr_left {Ξ±β Ξ±β} {Ξ² : Ξ±β β Sort*} (e : Ξ±β β Ξ±β) : (Ξ£ a:Ξ±β, Ξ² (e a)) β (Ξ£ a:Ξ±β, Ξ² a) :=
β¨Ξ» a, β¨e a.1, a.2β©, Ξ» a, β¨e.symm a.1, @@eq.rec Ξ² a.2 (e.right_inv a.1).symmβ©,
Ξ» β¨a, bβ©, match e.symm (e a), e.left_inv a : β a' (h : a' = a),
@sigma.mk _ (Ξ² β e) _ (@@eq.rec Ξ² b (congr_arg e h.symm)) = β¨a, bβ© with
| _, rfl := rfl end,
Ξ» β¨a, bβ©, match e (e.symm a), _ : β a' (h : a' = a),
sigma.mk a' (@@eq.rec Ξ² b h.symm) = β¨a, bβ© with
| _, rfl := rfl endβ©
/-- Transporting a sigma type through an equivalence of the base -/
def sigma_congr_left' {Ξ±β Ξ±β} {Ξ² : Ξ±β β Sort*} (f : Ξ±β β Ξ±β) :
(Ξ£ a:Ξ±β, Ξ² a) β (Ξ£ a:Ξ±β, Ξ² (f.symm a)) :=
(sigma_congr_left f.symm).symm
/-- Transporting a sigma type through an equivalence of the base and a family of equivalences
of matching fibers -/
def sigma_congr {Ξ±β Ξ±β} {Ξ²β : Ξ±β β Sort*} {Ξ²β : Ξ±β β Sort*} (f : Ξ±β β Ξ±β)
(F : β a, Ξ²β a β Ξ²β (f a)) :
sigma Ξ²β β sigma Ξ²β :=
(sigma_congr_right F).trans (sigma_congr_left f)
/-- `sigma` type with a constant fiber is equivalent to the product. -/
@[simps apply symm_apply] def sigma_equiv_prod (Ξ± Ξ² : Type*) : (Ξ£_:Ξ±, Ξ²) β Ξ± Γ Ξ² :=
β¨Ξ» a, β¨a.1, a.2β©, Ξ» a, β¨a.1, a.2β©, Ξ» β¨a, bβ©, rfl, Ξ» β¨a, bβ©, rflβ©
/-- If each fiber of a `sigma` type is equivalent to a fixed type, then the sigma type
is equivalent to the product. -/
def sigma_equiv_prod_of_equiv {Ξ± Ξ²} {Ξ²β : Ξ± β Sort*} (F : Ξ a, Ξ²β a β Ξ²) : sigma Ξ²β β Ξ± Γ Ξ² :=
(sigma_congr_right F).trans (sigma_equiv_prod Ξ± Ξ²)
end
section prod_congr
variables {Ξ±β Ξ²β Ξ²β : Type*} (e : Ξ±β β Ξ²β β Ξ²β)
/-- A family of equivalences `Ξ (a : Ξ±β), Ξ²β β Ξ²β` generates an equivalence
between `Ξ²β Γ Ξ±β` and `Ξ²β Γ Ξ±β`. -/
def prod_congr_left : Ξ²β Γ Ξ±β β Ξ²β Γ Ξ±β :=
{ to_fun := Ξ» ab, β¨e ab.2 ab.1, ab.2β©,
inv_fun := Ξ» ab, β¨(e ab.2).symm ab.1, ab.2β©,
left_inv := by { rintros β¨a, bβ©, simp },
right_inv := by { rintros β¨a, bβ©, simp } }
@[simp] lemma prod_congr_left_apply (b : Ξ²β) (a : Ξ±β) :
prod_congr_left e (b, a) = (e a b, a) := rfl
lemma prod_congr_refl_right (e : Ξ²β β Ξ²β) :
prod_congr e (equiv.refl Ξ±β) = prod_congr_left (Ξ» _, e) :=
by { ext β¨a, bβ© : 1, simp }
/-- A family of equivalences `Ξ (a : Ξ±β), Ξ²β β Ξ²β` generates an equivalence
between `Ξ±β Γ Ξ²β` and `Ξ±β Γ Ξ²β`. -/
def prod_congr_right : Ξ±β Γ Ξ²β β Ξ±β Γ Ξ²β :=
{ to_fun := Ξ» ab, β¨ab.1, e ab.1 ab.2β©,
inv_fun := Ξ» ab, β¨ab.1, (e ab.1).symm ab.2β©,
left_inv := by { rintros β¨a, bβ©, simp },
right_inv := by { rintros β¨a, bβ©, simp } }
@[simp] lemma prod_congr_right_apply (a : Ξ±β) (b : Ξ²β) :
prod_congr_right e (a, b) = (a, e a b) := rfl
lemma prod_congr_refl_left (e : Ξ²β β Ξ²β) :
prod_congr (equiv.refl Ξ±β) e = prod_congr_right (Ξ» _, e) :=
by { ext β¨a, bβ© : 1, simp }
@[simp] lemma prod_congr_left_trans_prod_comm :
(prod_congr_left e).trans (prod_comm _ _) = (prod_comm _ _).trans (prod_congr_right e) :=
by { ext β¨a, bβ© : 1, simp }
@[simp] lemma prod_congr_right_trans_prod_comm :
(prod_congr_right e).trans (prod_comm _ _) = (prod_comm _ _).trans (prod_congr_left e) :=
by { ext β¨a, bβ© : 1, simp }
lemma sigma_congr_right_sigma_equiv_prod :
(sigma_congr_right e).trans (sigma_equiv_prod Ξ±β Ξ²β) =
(sigma_equiv_prod Ξ±β Ξ²β).trans (prod_congr_right e) :=
by { ext β¨a, bβ© : 1, simp }
lemma sigma_equiv_prod_sigma_congr_right :
(sigma_equiv_prod Ξ±β Ξ²β).symm.trans (sigma_congr_right e) =
(prod_congr_right e).trans (sigma_equiv_prod Ξ±β Ξ²β).symm :=
by { ext β¨a, bβ© : 1, simp }
/-- A variation on `equiv.prod_congr` where the equivalence in the second component can depend
on the first component. A typical example is a shear mapping, explaining the name of this
declaration. -/
@[simps {fully_applied := ff}]
def prod_shear {Ξ±β Ξ²β Ξ±β Ξ²β : Type*} (eβ : Ξ±β β Ξ±β) (eβ : Ξ±β β Ξ²β β Ξ²β) : Ξ±β Γ Ξ²β β Ξ±β Γ Ξ²β :=
{ to_fun := Ξ» x : Ξ±β Γ Ξ²β, (eβ x.1, eβ x.1 x.2),
inv_fun := Ξ» y : Ξ±β Γ Ξ²β, (eβ.symm y.1, (eβ $ eβ.symm y.1).symm y.2),
left_inv := by { rintro β¨xβ, yββ©, simp only [symm_apply_apply] },
right_inv := by { rintro β¨xβ, yββ©, simp only [apply_symm_apply] } }
end prod_congr
namespace perm
variables {Ξ±β Ξ²β Ξ²β : Type*} [decidable_eq Ξ±β] (a : Ξ±β) (e : perm Ξ²β)
/-- `prod_extend_right a e` extends `e : perm Ξ²` to `perm (Ξ± Γ Ξ²)` by sending `(a, b)` to
`(a, e b)` and keeping the other `(a', b)` fixed. -/
def prod_extend_right : perm (Ξ±β Γ Ξ²β) :=
{ to_fun := Ξ» ab, if ab.fst = a then (a, e ab.snd) else ab,
inv_fun := Ξ» ab, if ab.fst = a then (a, e.symm ab.snd) else ab,
left_inv := by { rintros β¨k', xβ©, simp only, split_ifs with h; simp [h] },
right_inv := by { rintros β¨k', xβ©, simp only, split_ifs with h; simp [h] } }
@[simp] lemma prod_extend_right_apply_eq (b : Ξ²β) :
prod_extend_right a e (a, b) = (a, e b) := if_pos rfl
lemma prod_extend_right_apply_ne {a a' : Ξ±β} (h : a' β a) (b : Ξ²β) :
prod_extend_right a e (a', b) = (a', b) := if_neg h
lemma eq_of_prod_extend_right_ne {e : perm Ξ²β} {a a' : Ξ±β} {b : Ξ²β}
(h : prod_extend_right a e (a', b) β (a', b)) : a' = a :=
by { contrapose! h, exact prod_extend_right_apply_ne _ h _ }
@[simp] lemma fst_prod_extend_right (ab : Ξ±β Γ Ξ²β) :
(prod_extend_right a e ab).fst = ab.fst :=
begin
rw [prod_extend_right, coe_fn_mk],
split_ifs with h,
{ rw h },
{ refl }
end
end perm
section
/-- The type of functions to a product `Ξ± Γ Ξ²` is equivalent to the type of pairs of functions
`Ξ³ β Ξ±` and `Ξ³ β Ξ²`. -/
def arrow_prod_equiv_prod_arrow (Ξ± Ξ² Ξ³ : Type*) : (Ξ³ β Ξ± Γ Ξ²) β (Ξ³ β Ξ±) Γ (Ξ³ β Ξ²) :=
β¨Ξ» f, (Ξ» c, (f c).1, Ξ» c, (f c).2),
Ξ» p c, (p.1 c, p.2 c),
Ξ» f, funext $ Ξ» c, prod.mk.eta,
Ξ» p, by { cases p, refl }β©
open sum
/-- The type of functions on a sum type `Ξ± β Ξ²` is equivalent to the type of pairs of functions
on `Ξ±` and on `Ξ²`. -/
def sum_arrow_equiv_prod_arrow (Ξ± Ξ² Ξ³ : Type*) : ((Ξ± β Ξ²) β Ξ³) β (Ξ± β Ξ³) Γ (Ξ² β Ξ³) :=
β¨Ξ» f, (f β inl, f β inr),
Ξ» p, sum.elim p.1 p.2,
Ξ» f, by { ext β¨β©; refl },
Ξ» p, by { cases p, refl }β©
@[simp] lemma sum_arrow_equiv_prod_arrow_apply_fst {Ξ± Ξ² Ξ³} (f : (Ξ± β Ξ²) β Ξ³) (a : Ξ±) :
(sum_arrow_equiv_prod_arrow Ξ± Ξ² Ξ³ f).1 a = f (inl a) := rfl
@[simp] lemma sum_arrow_equiv_prod_arrow_apply_snd {Ξ± Ξ² Ξ³} (f : (Ξ± β Ξ²) β Ξ³) (b : Ξ²) :
(sum_arrow_equiv_prod_arrow Ξ± Ξ² Ξ³ f).2 b = f (inr b) := rfl
@[simp] lemma sum_arrow_equiv_prod_arrow_symm_apply_inl {Ξ± Ξ² Ξ³} (f : Ξ± β Ξ³) (g : Ξ² β Ξ³) (a : Ξ±) :
((sum_arrow_equiv_prod_arrow Ξ± Ξ² Ξ³).symm (f, g)) (inl a) = f a := rfl
@[simp] lemma sum_arrow_equiv_prod_arrow_symm_apply_inr {Ξ± Ξ² Ξ³} (f : Ξ± β Ξ³) (g : Ξ² β Ξ³) (b : Ξ²) :
((sum_arrow_equiv_prod_arrow Ξ± Ξ² Ξ³).symm (f, g)) (inr b) = g b := rfl
/-- Type product is right distributive with respect to type sum up to an equivalence. -/
def sum_prod_distrib (Ξ± Ξ² Ξ³ : Sort*) : (Ξ± β Ξ²) Γ Ξ³ β (Ξ± Γ Ξ³) β (Ξ² Γ Ξ³) :=
β¨Ξ» p, match p with (inl a, c) := inl (a, c) | (inr b, c) := inr (b, c) end,
Ξ» s, match s with inl q := (inl q.1, q.2) | inr q := (inr q.1, q.2) end,
Ξ» p, by rcases p with β¨_ | _, _β©; refl,
Ξ» s, by rcases s with β¨_, _β© | β¨_, _β©; reflβ©
@[simp] theorem sum_prod_distrib_apply_left {Ξ± Ξ² Ξ³} (a : Ξ±) (c : Ξ³) :
sum_prod_distrib Ξ± Ξ² Ξ³ (sum.inl a, c) = sum.inl (a, c) := rfl
@[simp] theorem sum_prod_distrib_apply_right {Ξ± Ξ² Ξ³} (b : Ξ²) (c : Ξ³) :
sum_prod_distrib Ξ± Ξ² Ξ³ (sum.inr b, c) = sum.inr (b, c) := rfl
/-- Type product is left distributive with respect to type sum up to an equivalence. -/
def prod_sum_distrib (Ξ± Ξ² Ξ³ : Sort*) : Ξ± Γ (Ξ² β Ξ³) β (Ξ± Γ Ξ²) β (Ξ± Γ Ξ³) :=
calc Ξ± Γ (Ξ² β Ξ³) β (Ξ² β Ξ³) Γ Ξ± : prod_comm _ _
... β (Ξ² Γ Ξ±) β (Ξ³ Γ Ξ±) : sum_prod_distrib _ _ _
... β (Ξ± Γ Ξ²) β (Ξ± Γ Ξ³) : sum_congr (prod_comm _ _) (prod_comm _ _)
@[simp] theorem prod_sum_distrib_apply_left {Ξ± Ξ² Ξ³} (a : Ξ±) (b : Ξ²) :
prod_sum_distrib Ξ± Ξ² Ξ³ (a, sum.inl b) = sum.inl (a, b) := rfl
@[simp] theorem prod_sum_distrib_apply_right {Ξ± Ξ² Ξ³} (a : Ξ±) (c : Ξ³) :
prod_sum_distrib Ξ± Ξ² Ξ³ (a, sum.inr c) = sum.inr (a, c) := rfl
/-- The product of an indexed sum of types (formally, a `sigma`-type `Ξ£ i, Ξ± i`) by a type `Ξ²` is
equivalent to the sum of products `Ξ£ i, (Ξ± i Γ Ξ²)`. -/
def sigma_prod_distrib {ΞΉ : Type*} (Ξ± : ΞΉ β Type*) (Ξ² : Type*) :
((Ξ£ i, Ξ± i) Γ Ξ²) β (Ξ£ i, (Ξ± i Γ Ξ²)) :=
β¨Ξ» p, β¨p.1.1, (p.1.2, p.2)β©,
Ξ» p, (β¨p.1, p.2.1β©, p.2.2),
Ξ» p, by { rcases p with β¨β¨_, _β©, _β©, refl },
Ξ» p, by { rcases p with β¨_, β¨_, _β©β©, refl }β©
/-- The product `bool Γ Ξ±` is equivalent to `Ξ± β Ξ±`. -/
def bool_prod_equiv_sum (Ξ± : Type u) : bool Γ Ξ± β Ξ± β Ξ± :=
calc bool Γ Ξ± β (unit β unit) Γ Ξ± : prod_congr bool_equiv_punit_sum_punit (equiv.refl _)
... β (unit Γ Ξ±) β (unit Γ Ξ±) : sum_prod_distrib _ _ _
... β Ξ± β Ξ± : sum_congr (punit_prod _) (punit_prod _)
/-- The function type `bool β Ξ±` is equivalent to `Ξ± Γ Ξ±`. -/
def bool_to_equiv_prod (Ξ± : Type u) : (bool β Ξ±) β Ξ± Γ Ξ± :=
calc (bool β Ξ±) β ((unit β unit) β Ξ±) : (arrow_congr bool_equiv_punit_sum_punit (equiv.refl Ξ±))
... β (unit β Ξ±) Γ (unit β Ξ±) : sum_arrow_equiv_prod_arrow _ _ _
... β Ξ± Γ Ξ± : prod_congr (punit_arrow_equiv _) (punit_arrow_equiv _)
@[simp] lemma bool_to_equiv_prod_apply {Ξ± : Type u} (f : bool β Ξ±) :
bool_to_equiv_prod Ξ± f = (f ff, f tt) := rfl
@[simp] lemma bool_to_equiv_prod_symm_apply_ff {Ξ± : Type u} (p : Ξ± Γ Ξ±) :
(bool_to_equiv_prod Ξ±).symm p ff = p.1 := rfl
@[simp] lemma bool_to_equiv_prod_symm_apply_tt {Ξ± : Type u} (p : Ξ± Γ Ξ±) :
(bool_to_equiv_prod Ξ±).symm p tt = p.2 := rfl
end
section
open sum nat
/-- The set of natural numbers is equivalent to `β β punit`. -/
def nat_equiv_nat_sum_punit : β β β β punit.{u+1} :=
β¨Ξ» n, match n with zero := inr punit.star | succ a := inl a end,
Ξ» s, match s with inl n := succ n | inr punit.star := zero end,
Ξ» n, begin cases n, repeat { refl } end,
Ξ» s, begin cases s with a u, { refl }, {cases u, { refl }} endβ©
/-- `β β punit` is equivalent to `β`. -/
def nat_sum_punit_equiv_nat : β β punit.{u+1} β β :=
nat_equiv_nat_sum_punit.symm
/-- The type of integer numbers is equivalent to `β β β`. -/
def int_equiv_nat_sum_nat : β€ β β β β :=
by refine β¨_, _, _, _β©; intro z; {cases z; [left, right]; assumption} <|> {cases z; refl}
end
/-- An equivalence between `Ξ±` and `Ξ²` generates an equivalence between `list Ξ±` and `list Ξ²`. -/
def list_equiv_of_equiv {Ξ± Ξ² : Type*} (e : Ξ± β Ξ²) : list Ξ± β list Ξ² :=
{ to_fun := list.map e,
inv_fun := list.map e.symm,
left_inv := Ξ» l, by rw [list.map_map, e.symm_comp_self, list.map_id],
right_inv := Ξ» l, by rw [list.map_map, e.self_comp_symm, list.map_id] }
/-- `fin n` is equivalent to `{m // m < n}`. -/
def fin_equiv_subtype (n : β) : fin n β {m // m < n} :=
β¨Ξ» x, β¨x.1, x.2β©, Ξ» x, β¨x.1, x.2β©, Ξ» β¨a, bβ©, rfl,Ξ» β¨a, bβ©, rflβ©
/-- If `Ξ±` is equivalent to `Ξ²`, then `unique Ξ±` is equivalent to `unique Ξ²`. -/
def unique_congr (e : Ξ± β Ξ²) : unique Ξ± β unique Ξ² :=
{ to_fun := Ξ» h, @equiv.unique _ _ h e.symm,
inv_fun := Ξ» h, @equiv.unique _ _ h e,
left_inv := Ξ» _, subsingleton.elim _ _,
right_inv := Ξ» _, subsingleton.elim _ _ }
/-- If `Ξ±` is equivalent to `Ξ²`, then `is_empty Ξ±` is equivalent to `is_empty Ξ²`. -/
lemma is_empty_congr (e : Ξ± β Ξ²) : is_empty Ξ± β is_empty Ξ² :=
β¨Ξ» h, @function.is_empty _ _ h e.symm, Ξ» h, @function.is_empty _ _ h eβ©
section
open subtype
/-- If `Ξ±` is equivalent to `Ξ²` and the predicates `p : Ξ± β Prop` and `q : Ξ² β Prop` are equivalent
at corresponding points, then `{a // p a}` is equivalent to `{b // q b}`.
For the statement where `Ξ± = Ξ²`, that is, `e : perm Ξ±`, see `perm.subtype_perm`. -/
def subtype_equiv {p : Ξ± β Prop} {q : Ξ² β Prop}
(e : Ξ± β Ξ²) (h : β a, p a β q (e a)) : {a : Ξ± // p a} β {b : Ξ² // q b} :=
β¨Ξ» x, β¨e x, (h _).1 x.2β©,
Ξ» y, β¨e.symm y, (h _).2 (by { simp, exact y.2 })β©,
Ξ» β¨x, hβ©, subtype.ext_val $ by simp,
Ξ» β¨y, hβ©, subtype.ext_val $ by simpβ©
@[simp] lemma subtype_equiv_refl {p : Ξ± β Prop}
(h : β a, p a β p (equiv.refl _ a) := Ξ» a, iff.rfl) :
(equiv.refl Ξ±).subtype_equiv h = equiv.refl {a : Ξ± // p a} :=
by { ext, refl }
@[simp] lemma subtype_equiv_symm {p : Ξ± β Prop} {q : Ξ² β Prop} (e : Ξ± β Ξ²)
(h : β (a : Ξ±), p a β q (e a)) :
(e.subtype_equiv h).symm = e.symm.subtype_equiv (Ξ» a, by {
convert (h $ e.symm a).symm,
exact (e.apply_symm_apply a).symm,
}) :=
rfl
@[simp] lemma subtype_equiv_trans {p : Ξ± β Prop} {q : Ξ² β Prop} {r : Ξ³ β Prop}
(e : Ξ± β Ξ²) (f : Ξ² β Ξ³)
(h : β (a : Ξ±), p a β q (e a)) (h' : β (b : Ξ²), q b β r (f b)):
(e.subtype_equiv h).trans (f.subtype_equiv h') =
(e.trans f).subtype_equiv (Ξ» a, (h a).trans (h' $ e a)) :=
rfl
@[simp] lemma subtype_equiv_apply {p : Ξ± β Prop} {q : Ξ² β Prop} (e : Ξ± β Ξ²)
(h : β (a : Ξ±), p a β q (e a)) (x : {x // p x}) :
e.subtype_equiv h x = β¨e x, (h _).1 x.2β© :=
rfl
/-- If two predicates `p` and `q` are pointwise equivalent, then `{x // p x}` is equivalent to
`{x // q x}`. -/
@[simps]
def subtype_equiv_right {p q : Ξ± β Prop} (e : βx, p x β q x) : {x // p x} β {x // q x} :=
subtype_equiv (equiv.refl _) e
/-- If `Ξ± β Ξ²`, then for any predicate `p : Ξ² β Prop` the subtype `{a // p (e a)}` is equivalent
to the subtype `{b // p b}`. -/
def subtype_equiv_of_subtype {p : Ξ² β Prop} (e : Ξ± β Ξ²) :
{a : Ξ± // p (e a)} β {b : Ξ² // p b} :=
subtype_equiv e $ by simp
/-- If `Ξ± β Ξ²`, then for any predicate `p : Ξ± β Prop` the subtype `{a // p a}` is equivalent
to the subtype `{b // p (e.symm b)}`. This version is used by `equiv_rw`. -/
def subtype_equiv_of_subtype' {p : Ξ± β Prop} (e : Ξ± β Ξ²) :
{a : Ξ± // p a} β {b : Ξ² // p (e.symm b)} :=
e.symm.subtype_equiv_of_subtype.symm
/-- If two predicates are equal, then the corresponding subtypes are equivalent. -/
def subtype_equiv_prop {Ξ± : Type*} {p q : Ξ± β Prop} (h : p = q) : subtype p β subtype q :=
subtype_equiv (equiv.refl Ξ±) (assume a, h βΈ iff.rfl)
/-- The subtypes corresponding to equal sets are equivalent. -/
@[simps apply]
def set_congr {Ξ± : Type*} {s t : set Ξ±} (h : s = t) : s β t :=
subtype_equiv_prop h
/-- A subtype of a subtype is equivalent to the subtype of elements satisfying both predicates. This
version allows the βinnerβ predicate to depend on `h : p a`. -/
def subtype_subtype_equiv_subtype_exists {Ξ± : Type u} (p : Ξ± β Prop) (q : subtype p β Prop) :
subtype q β {a : Ξ± // βh:p a, q β¨a, hβ© } :=
β¨Ξ»β¨β¨a, haβ©, ha'β©, β¨a, ha, ha'β©,
Ξ»β¨a, haβ©, β¨β¨a, ha.cases_on $ assume h _, hβ©, by { cases ha, exact ha_h }β©,
assume β¨β¨a, haβ©, hβ©, rfl, assume β¨a, hβ, hββ©, rflβ©
@[simp] lemma subtype_subtype_equiv_subtype_exists_apply {Ξ± : Type u} (p : Ξ± β Prop)
(q : subtype p β Prop) (a) : (subtype_subtype_equiv_subtype_exists p q a : Ξ±) = a :=
by { cases a, cases a_val, refl }
/-- A subtype of a subtype is equivalent to the subtype of elements satisfying both predicates. -/
def subtype_subtype_equiv_subtype_inter {Ξ± : Type u} (p q : Ξ± β Prop) :
{x : subtype p // q x.1} β subtype (Ξ» x, p x β§ q x) :=
(subtype_subtype_equiv_subtype_exists p _).trans $
subtype_equiv_right $ Ξ» x, exists_prop
@[simp] lemma subtype_subtype_equiv_subtype_inter_apply {Ξ± : Type u} (p q : Ξ± β Prop) (a) :
(subtype_subtype_equiv_subtype_inter p q a : Ξ±) = a :=
by { cases a, cases a_val, refl }
/-- If the outer subtype has more restrictive predicate than the inner one,
then we can drop the latter. -/
def subtype_subtype_equiv_subtype {Ξ± : Type u} {p q : Ξ± β Prop} (h : β {x}, q x β p x) :
{x : subtype p // q x.1} β subtype q :=
(subtype_subtype_equiv_subtype_inter p _).trans $
subtype_equiv_right $
assume x,
β¨and.right, Ξ» hβ, β¨h hβ, hββ©β©
@[simp] lemma subtype_subtype_equiv_subtype_apply {Ξ± : Type u} {p q : Ξ± β Prop} (h : β x, q x β p x)
(a : {x : subtype p // q x.1}) :
(subtype_subtype_equiv_subtype h a : Ξ±) = a :=
by { cases a, cases a_val, refl }
/-- If a proposition holds for all elements, then the subtype is
equivalent to the original type. -/
@[simps apply symm_apply]
def subtype_univ_equiv {Ξ± : Type u} {p : Ξ± β Prop} (h : β x, p x) :
subtype p β Ξ± :=
β¨Ξ» x, x, Ξ» x, β¨x, h xβ©, Ξ» x, subtype.eq rfl, Ξ» x, rflβ©
/-- A subtype of a sigma-type is a sigma-type over a subtype. -/
def subtype_sigma_equiv {Ξ± : Type u} (p : Ξ± β Type v) (q : Ξ± β Prop) :
{ y : sigma p // q y.1 } β Ξ£(x : subtype q), p x.1 :=
β¨Ξ» x, β¨β¨x.1.1, x.2β©, x.1.2β©,
Ξ» x, β¨β¨x.1.1, x.2β©, x.1.2β©,
Ξ» β¨β¨x, hβ©, yβ©, rfl,
Ξ» β¨β¨x, yβ©, hβ©, rflβ©
/-- A sigma type over a subtype is equivalent to the sigma set over the original type,
if the fiber is empty outside of the subset -/
def sigma_subtype_equiv_of_subset {Ξ± : Type u} (p : Ξ± β Type v) (q : Ξ± β Prop)
(h : β x, p x β q x) :
(Ξ£ x : subtype q, p x) β Ξ£ x : Ξ±, p x :=
(subtype_sigma_equiv p q).symm.trans $ subtype_univ_equiv $ Ξ» x, h x.1 x.2
/-- If a predicate `p : Ξ² β Prop` is true on the range of a map `f : Ξ± β Ξ²`, then
`Ξ£ y : {y // p y}, {x // f x = y}` is equivalent to `Ξ±`. -/
def sigma_subtype_preimage_equiv {Ξ± : Type u} {Ξ² : Type v} (f : Ξ± β Ξ²) (p : Ξ² β Prop)
(h : β x, p (f x)) :
(Ξ£ y : subtype p, {x : Ξ± // f x = y}) β Ξ± :=
calc _ β Ξ£ y : Ξ², {x : Ξ± // f x = y} : sigma_subtype_equiv_of_subset _ p (Ξ» y β¨x, h'β©, h' βΈ h x)
... β Ξ± : sigma_preimage_equiv f
/-- If for each `x` we have `p x β q (f x)`, then `Ξ£ y : {y // q y}, f β»ΒΉ' {y}` is equivalent
to `{x // p x}`. -/
def sigma_subtype_preimage_equiv_subtype {Ξ± : Type u} {Ξ² : Type v} (f : Ξ± β Ξ²)
{p : Ξ± β Prop} {q : Ξ² β Prop} (h : β x, p x β q (f x)) :
(Ξ£ y : subtype q, {x : Ξ± // f x = y}) β subtype p :=
calc (Ξ£ y : subtype q, {x : Ξ± // f x = y}) β
Ξ£ y : subtype q, {x : subtype p // subtype.mk (f x) ((h x).1 x.2) = y} :
begin
apply sigma_congr_right,
assume y,
symmetry,
refine (subtype_subtype_equiv_subtype_exists _ _).trans (subtype_equiv_right _),
assume x,
exact β¨Ξ» β¨hp, h'β©, congr_arg subtype.val h', Ξ» h', β¨(h x).2 (h'.symm βΈ y.2), subtype.eq h'β©β©
end
... β subtype p : sigma_preimage_equiv (Ξ» x : subtype p, (β¨f x, (h x).1 x.propertyβ© : subtype q))
/-- The `pi`-type `Ξ i, Ο i` is equivalent to the type of sections `f : ΞΉ β Ξ£ i, Ο i` of the
`sigma` type such that for all `i` we have `(f i).fst = i`. -/
def pi_equiv_subtype_sigma (ΞΉ : Type*) (Ο : ΞΉ β Type*) :
(Ξ i, Ο i) β {f : ΞΉ β Ξ£i, Ο i | βi, (f i).1 = i } :=
β¨ Ξ»f, β¨Ξ»i, β¨i, f iβ©, assume i, rflβ©, Ξ»f i, begin rw β f.2 i, exact (f.1 i).2 end,
assume f, funext $ assume i, rfl,
assume β¨f, hfβ©, subtype.eq $ funext $ assume i, sigma.eq (hf i).symm $
eq_of_heq $ rec_heq_of_heq _ $ rec_heq_of_heq _ $ heq.refl _β©
/-- The set of functions `f : Ξ a, Ξ² a` such that for all `a` we have `p a (f a)` is equivalent
to the set of functions `Ξ a, {b : Ξ² a // p a b}`. -/
def subtype_pi_equiv_pi {Ξ± : Sort u} {Ξ² : Ξ± β Sort v} {p : Ξ a, Ξ² a β Prop} :
{f : Ξ a, Ξ² a // βa, p a (f a) } β Ξ a, { b : Ξ² a // p a b } :=
β¨Ξ»f a, β¨f.1 a, f.2 aβ©, Ξ»f, β¨Ξ»a, (f a).1, Ξ»a, (f a).2β©,
by { rintro β¨f, hβ©, refl },
by { rintro f, funext a, exact subtype.ext_val rfl }β©
/-- A subtype of a product defined by componentwise conditions
is equivalent to a product of subtypes. -/
def subtype_prod_equiv_prod {Ξ± : Type u} {Ξ² : Type v} {p : Ξ± β Prop} {q : Ξ² β Prop} :
{c : Ξ± Γ Ξ² // p c.1 β§ q c.2} β ({a // p a} Γ {b // q b}) :=
β¨Ξ» x, β¨β¨x.1.1, x.2.1β©, β¨x.1.2, x.2.2β©β©,
Ξ» x, β¨β¨x.1.1, x.2.1β©, β¨x.1.2, x.2.2β©β©,
Ξ» β¨β¨_, _β©, β¨_, _β©β©, rfl,
Ξ» β¨β¨_, _β©, β¨_, _β©β©, rflβ©
/-- A subtype of a `prod` is equivalent to a sigma type whose fibers are subtypes. -/
def subtype_prod_equiv_sigma_subtype {Ξ± Ξ² : Type*} (p : Ξ± β Ξ² β Prop) :
{x : Ξ± Γ Ξ² // p x.1 x.2} β Ξ£ a, {b : Ξ² // p a b} :=
{ to_fun := Ξ» x, β¨x.1.1, x.1.2, x.propβ©,
inv_fun := Ξ» x, β¨β¨x.1, x.2β©, x.2.propβ©,
left_inv := Ξ» x, by ext; refl,
right_inv := Ξ» β¨a, b, pabβ©, rfl }
end
section subtype_equiv_codomain
variables {X : Type*} {Y : Type*} [decidable_eq X] {x : X}
/-- The type of all functions `X β Y` with prescribed values for all `x' β x`
is equivalent to the codomain `Y`. -/
def subtype_equiv_codomain (f : {x' // x' β x} β Y) : {g : X β Y // g β coe = f} β Y :=
(subtype_preimage _ f).trans $
@fun_unique {x' // Β¬ x' β x} _ $
show unique {x' // Β¬ x' β x}, from @equiv.unique _ _
(show unique {x' // x' = x}, from
{ default := β¨x, rflβ©, uniq := Ξ» β¨x', hβ©, subtype.val_injective h })
(subtype_equiv_right $ Ξ» a, not_not)
@[simp] lemma coe_subtype_equiv_codomain (f : {x' // x' β x} β Y) :
(subtype_equiv_codomain f : {g : X β Y // g β coe = f} β Y) = Ξ» g, (g : X β Y) x := rfl
@[simp] lemma subtype_equiv_codomain_apply (f : {x' // x' β x} β Y)
(g : {g : X β Y // g β coe = f}) :
subtype_equiv_codomain f g = (g : X β Y) x := rfl
lemma coe_subtype_equiv_codomain_symm (f : {x' // x' β x} β Y) :
((subtype_equiv_codomain f).symm : Y β {g : X β Y // g β coe = f}) =
Ξ» y, β¨Ξ» x', if h : x' β x then f β¨x', hβ© else y,
by { funext x', dsimp, erw [dif_pos x'.2, subtype.coe_eta] }β© := rfl
@[simp] lemma subtype_equiv_codomain_symm_apply (f : {x' // x' β x} β Y) (y : Y) (x' : X) :
((subtype_equiv_codomain f).symm y : X β Y) x' = if h : x' β x then f β¨x', hβ© else y :=
rfl
@[simp] lemma subtype_equiv_codomain_symm_apply_eq (f : {x' // x' β x} β Y) (y : Y) :
((subtype_equiv_codomain f).symm y : X β Y) x = y :=
dif_neg (not_not.mpr rfl)
lemma subtype_equiv_codomain_symm_apply_ne (f : {x' // x' β x} β Y) (y : Y) (x' : X) (h : x' β x) :
((subtype_equiv_codomain f).symm y : X β Y) x' = f β¨x', hβ© :=
dif_pos h
end subtype_equiv_codomain
/--
A set is equivalent to its image under an equivalence.
-/
-- We could construct this using `equiv.set.image e s e.injective`,
-- but this definition provides an explicit inverse.
@[simps]
def image {Ξ± Ξ² : Type*} (e : Ξ± β Ξ²) (s : set Ξ±) : s β e '' s :=
{ to_fun := Ξ» x, β¨e x.1, by simpβ©,
inv_fun := Ξ» y, β¨e.symm y.1, by { rcases y with β¨-, β¨a, β¨m, rflβ©β©β©, simpa using m, }β©,
left_inv := Ξ» x, by simp,
right_inv := Ξ» y, by simp, }.
namespace set
open set
/-- `univ Ξ±` is equivalent to `Ξ±`. -/
@[simps apply symm_apply]
protected def univ (Ξ±) : @univ Ξ± β Ξ± :=
β¨coe, Ξ» a, β¨a, trivialβ©, Ξ» β¨a, _β©, rfl, Ξ» a, rflβ©
/-- An empty set is equivalent to the `empty` type. -/
protected def empty (Ξ±) : (β
: set Ξ±) β empty :=
equiv_empty _
/-- An empty set is equivalent to a `pempty` type. -/
protected def pempty (Ξ±) : (β
: set Ξ±) β pempty :=
equiv_pempty _
/-- If sets `s` and `t` are separated by a decidable predicate, then `s βͺ t` is equivalent to
`s β t`. -/
protected def union' {Ξ±} {s t : set Ξ±}
(p : Ξ± β Prop) [decidable_pred p]
(hs : β x β s, p x)
(ht : β x β t, Β¬ p x) : (s βͺ t : set Ξ±) β s β t :=
{ to_fun := Ξ» x, if hp : p x
then sum.inl β¨_, x.2.resolve_right (Ξ» xt, ht _ xt hp)β©
else sum.inr β¨_, x.2.resolve_left (Ξ» xs, hp (hs _ xs))β©,
inv_fun := Ξ» o, match o with
| (sum.inl x) := β¨x, or.inl x.2β©
| (sum.inr x) := β¨x, or.inr x.2β©
end,
left_inv := Ξ» β¨x, h'β©, by by_cases p x; simp [union'._match_1, h]; congr,
right_inv := Ξ» o, begin
rcases o with β¨x, hβ© | β¨x, hβ©;
dsimp [union'._match_1];
[simp [hs _ h], simp [ht _ h]]
end }
/-- If sets `s` and `t` are disjoint, then `s βͺ t` is equivalent to `s β t`. -/
protected def union {Ξ±} {s t : set Ξ±} [decidable_pred (Ξ» x, x β s)] (H : s β© t β β
) :
(s βͺ t : set Ξ±) β s β t :=
set.union' (Ξ» x, x β s) (Ξ» _, id) (Ξ» x xt xs, H β¨xs, xtβ©)
lemma union_apply_left {Ξ±} {s t : set Ξ±} [decidable_pred (Ξ» x, x β s)] (H : s β© t β β
)
{a : (s βͺ t : set Ξ±)} (ha : βa β s) : equiv.set.union H a = sum.inl β¨a, haβ© :=
dif_pos ha
lemma union_apply_right {Ξ±} {s t : set Ξ±} [decidable_pred (Ξ» x, x β s)] (H : s β© t β β
)
{a : (s βͺ t : set Ξ±)} (ha : βa β t) : equiv.set.union H a = sum.inr β¨a, haβ© :=
dif_neg $ Ξ» h, H β¨h, haβ©
@[simp] lemma union_symm_apply_left {Ξ±} {s t : set Ξ±} [decidable_pred (Ξ» x, x β s)] (H : s β© t β β
)
(a : s) : (equiv.set.union H).symm (sum.inl a) = β¨a, subset_union_left _ _ a.2β© :=
rfl
@[simp] lemma union_symm_apply_right {Ξ±} {s t : set Ξ±} [decidable_pred (Ξ» x, x β s)] (H : s β© t β β
)
(a : t) : (equiv.set.union H).symm (sum.inr a) = β¨a, subset_union_right _ _ a.2β© :=
rfl
/-- A singleton set is equivalent to a `punit` type. -/
protected def singleton {Ξ±} (a : Ξ±) : ({a} : set Ξ±) β punit.{u} :=
β¨Ξ» _, punit.star, Ξ» _, β¨a, mem_singleton _β©,
Ξ» β¨x, hβ©, by { simp at h, subst x },
Ξ» β¨β©, rflβ©
/-- Equal sets are equivalent. -/
@[simps apply symm_apply]
protected def of_eq {Ξ± : Type u} {s t : set Ξ±} (h : s = t) : s β t :=
{ to_fun := Ξ» x, β¨x, h βΈ x.2β©,
inv_fun := Ξ» x, β¨x, h.symm βΈ x.2β©,
left_inv := Ξ» _, subtype.eq rfl,
right_inv := Ξ» _, subtype.eq rfl }
/-- If `a β s`, then `insert a s` is equivalent to `s β punit`. -/
protected def insert {Ξ±} {s : set.{u} Ξ±} [decidable_pred (β s)] {a : Ξ±} (H : a β s) :
(insert a s : set Ξ±) β s β punit.{u+1} :=
calc (insert a s : set Ξ±) β β₯(s βͺ {a}) : equiv.set.of_eq (by simp)
... β s β ({a} : set Ξ±) : equiv.set.union (by finish [set.subset_def])
... β s β punit.{u+1} : sum_congr (equiv.refl _) (equiv.set.singleton _)
@[simp] lemma insert_symm_apply_inl {Ξ±} {s : set.{u} Ξ±} [decidable_pred (β s)] {a : Ξ±} (H : a β s)
(b : s) : (equiv.set.insert H).symm (sum.inl b) = β¨b, or.inr b.2β© :=
rfl
@[simp] lemma insert_symm_apply_inr {Ξ±} {s : set.{u} Ξ±} [decidable_pred (β s)] {a : Ξ±} (H : a β s)
(b : punit.{u+1}) : (equiv.set.insert H).symm (sum.inr b) = β¨a, or.inl rflβ© :=
rfl
@[simp] lemma insert_apply_left {Ξ±} {s : set.{u} Ξ±} [decidable_pred (β s)] {a : Ξ±} (H : a β s) :
equiv.set.insert H β¨a, or.inl rflβ© = sum.inr punit.star :=
(equiv.set.insert H).apply_eq_iff_eq_symm_apply.2 rfl
@[simp] lemma insert_apply_right {Ξ±} {s : set.{u} Ξ±} [decidable_pred (β s)] {a : Ξ±} (H : a β s)
(b : s) : equiv.set.insert H β¨b, or.inr b.2β© = sum.inl b :=
(equiv.set.insert H).apply_eq_iff_eq_symm_apply.2 rfl
/-- If `s : set Ξ±` is a set with decidable membership, then `s β sαΆ` is equivalent to `Ξ±`. -/
protected def sum_compl {Ξ±} (s : set Ξ±) [decidable_pred (β s)] : s β (sαΆ : set Ξ±) β Ξ± :=
calc s β (sαΆ : set Ξ±) β β₯(s βͺ sαΆ) : (equiv.set.union (by simp [set.ext_iff])).symm
... β @univ Ξ± : equiv.set.of_eq (by simp)
... β Ξ± : equiv.set.univ _
@[simp] lemma sum_compl_apply_inl {Ξ± : Type u} (s : set Ξ±) [decidable_pred (β s)] (x : s) :
equiv.set.sum_compl s (sum.inl x) = x := rfl
@[simp] lemma sum_compl_apply_inr {Ξ± : Type u} (s : set Ξ±) [decidable_pred (β s)] (x : sαΆ) :
equiv.set.sum_compl s (sum.inr x) = x := rfl
lemma sum_compl_symm_apply_of_mem {Ξ± : Type u} {s : set Ξ±} [decidable_pred (β s)] {x : Ξ±}
(hx : x β s) : (equiv.set.sum_compl s).symm x = sum.inl β¨x, hxβ© :=
have β(β¨x, or.inl hxβ© : (s βͺ sαΆ : set Ξ±)) β s, from hx,
by { rw [equiv.set.sum_compl], simpa using set.union_apply_left _ this }
lemma sum_compl_symm_apply_of_not_mem {Ξ± : Type u} {s : set Ξ±} [decidable_pred (β s)] {x : Ξ±}
(hx : x β s) : (equiv.set.sum_compl s).symm x = sum.inr β¨x, hxβ© :=
have β(β¨x, or.inr hxβ© : (s βͺ sαΆ : set Ξ±)) β sαΆ, from hx,
by { rw [equiv.set.sum_compl], simpa using set.union_apply_right _ this }
@[simp] lemma 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 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
/-- `sum_diff_subset s t` is the natural equivalence between
`s β (t \ s)` and `t`, where `s` and `t` are two sets. -/
protected def sum_diff_subset {Ξ±} {s t : set Ξ±} (h : s β t) [decidable_pred (β s)] :
s β (t \ s : set Ξ±) β t :=
calc s β (t \ s : set Ξ±) β (s βͺ (t \ s) : set Ξ±) :
(equiv.set.union (by simp [inter_diff_self])).symm
... β t : equiv.set.of_eq (by { simp [union_diff_self, union_eq_self_of_subset_left h] })
@[simp] lemma sum_diff_subset_apply_inl
{Ξ±} {s t : set Ξ±} (h : s β t) [decidable_pred (β s)] (x : s) :
equiv.set.sum_diff_subset h (sum.inl x) = inclusion h x := rfl
@[simp] lemma sum_diff_subset_apply_inr
{Ξ±} {s t : set Ξ±} (h : s β t) [decidable_pred (β s)] (x : t \ s) :
equiv.set.sum_diff_subset h (sum.inr x) = inclusion (diff_subset t s) x := rfl
lemma sum_diff_subset_symm_apply_of_mem
{Ξ±} {s t : set Ξ±} (h : s β t) [decidable_pred (β s)] {x : t} (hx : x.1 β s) :
(equiv.set.sum_diff_subset h).symm x = sum.inl β¨x, hxβ© :=
begin
apply (equiv.set.sum_diff_subset h).injective,
simp only [apply_symm_apply, sum_diff_subset_apply_inl],
exact subtype.eq rfl,
end
lemma sum_diff_subset_symm_apply_of_not_mem
{Ξ±} {s t : set Ξ±} (h : s β t) [decidable_pred (β s)] {x : t} (hx : x.1 β s) :
(equiv.set.sum_diff_subset h).symm x = sum.inr β¨x, β¨x.2, hxβ©β© :=
begin
apply (equiv.set.sum_diff_subset h).injective,
simp only [apply_symm_apply, sum_diff_subset_apply_inr],
exact subtype.eq rfl,
end
/-- If `s` is a set with decidable membership, then the sum of `s βͺ t` and `s β© t` is equivalent
to `s β t`. -/
protected def union_sum_inter {Ξ± : Type u} (s t : set Ξ±) [decidable_pred (β s)] :
(s βͺ t : set Ξ±) β (s β© t : set Ξ±) β s β t :=
calc (s βͺ t : set Ξ±) β (s β© t : set Ξ±)
β (s βͺ t \ s : set Ξ±) β (s β© t : set Ξ±) : by rw [union_diff_self]
... β (s β (t \ s : set Ξ±)) β (s β© t : set Ξ±) :
sum_congr (set.union $ subset_empty_iff.2 (inter_diff_self _ _)) (equiv.refl _)
... β s β (t \ s : set Ξ±) β (s β© t : set Ξ±) : sum_assoc _ _ _
... β s β (t \ s βͺ s β© t : set Ξ±) : sum_congr (equiv.refl _) begin
refine (set.union' (β s) _ _).symm,
exacts [Ξ» x hx, hx.2, Ξ» x hx, not_not_intro hx.1]
end
... β s β t : by { rw (_ : t \ s βͺ s β© t = t), rw [union_comm, inter_comm, inter_union_diff] }
/-- Given an equivalence `eβ` between sets `s : set Ξ±` and `t : set Ξ²`, the set of equivalences
`e : Ξ± β Ξ²` such that `e βx = β(eβ x)` for each `x : s` is equivalent to the set of equivalences
between `sαΆ` and `tαΆ`. -/
protected def compl {Ξ± : Type u} {Ξ² : Type v} {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_equiv e
(Ξ» a, not_congr $ iff.symm $ maps_to.mem_iff
(maps_to_iff_exists_map_subtype.2 β¨eβ, e.2β©)
(surj_on.maps_to_compl (surj_on_iff_exists_map_subtype.2
β¨t, eβ, subset.refl t, eβ.surjective, e.2β©) e.1.injective)),
inv_fun := Ξ» eβ,
subtype.mk
(calc Ξ± β s β (sαΆ : set Ξ±) : (set.sum_compl s).symm
... β t β (tαΆ : set Ξ²) : eβ.sum_congr 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_equiv_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_equiv_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] }
/-- The set product of two sets is equivalent to the type product of their coercions to types. -/
protected def prod {Ξ± Ξ²} (s : set Ξ±) (t : set Ξ²) :
s.prod t β s Γ t :=
@subtype_prod_equiv_prod Ξ± Ξ² s t
/-- If a function `f` is injective on a set `s`, then `s` is equivalent to `f '' s`. -/
protected noncomputable def image_of_inj_on {Ξ± Ξ²} (f : Ξ± β Ξ²) (s : set Ξ±) (H : inj_on f s) :
s β (f '' s) :=
β¨Ξ» p, β¨f p, mem_image_of_mem f p.2β©,
Ξ» p, β¨classical.some p.2, (classical.some_spec p.2).1β©,
Ξ» β¨x, hβ©, subtype.eq (H (classical.some_spec (mem_image_of_mem f h)).1 h
(classical.some_spec (mem_image_of_mem f h)).2),
Ξ» β¨y, hβ©, subtype.eq (classical.some_spec h).2β©
/-- If `f` is an injective function, then `s` is equivalent to `f '' s`. -/
@[simps apply]
protected noncomputable def image {Ξ± Ξ²} (f : Ξ± β Ξ²) (s : set Ξ±) (H : injective f) : s β (f '' s) :=
equiv.set.image_of_inj_on f s (H.inj_on s)
@[simp] protected lemma image_symm_apply {Ξ± Ξ²} (f : Ξ± β Ξ²) (s : set Ξ±) (H : injective f)
(x : Ξ±) (h : x β s) :
(set.image f s H).symm β¨f x, β¨x, β¨h, rflβ©β©β© = β¨x, hβ© :=
begin
apply (set.image f s H).injective,
simp [(set.image f s H).apply_symm_apply],
end
lemma image_symm_preimage {Ξ± Ξ²} {f : Ξ± β Ξ²} (hf : injective f) (u s : set Ξ±) :
(Ξ» x, (set.image f s hf).symm x : f '' s β Ξ±) β»ΒΉ' u = coe β»ΒΉ' (f '' u) :=
begin
ext β¨b, a, has, rflβ©,
have : β(h : βa', a' β s β§ a' = a), classical.some h = a := Ξ» h, (classical.some_spec h).2,
simp [equiv.set.image, equiv.set.image_of_inj_on, hf.eq_iff, this],
end
/-- If `Ξ±` is equivalent to `Ξ²`, then `set Ξ±` is equivalent to `set Ξ²`. -/
@[simps]
protected def congr {Ξ± Ξ² : Type*} (e : Ξ± β Ξ²) : set Ξ± β set Ξ² :=
β¨Ξ» s, e '' s, Ξ» t, e.symm '' t, symm_image_image e, symm_image_image e.symmβ©
/-- The set `{x β s | t x}` is equivalent to the set of `x : s` such that `t x`. -/
protected def sep {Ξ± : Type u} (s : set Ξ±) (t : Ξ± β Prop) :
({ x β s | t x } : set Ξ±) β { x : s | t x } :=
(equiv.subtype_subtype_equiv_subtype_inter s t).symm
/-- The set `π« S := {x | x β S}` is equivalent to the type `set S`. -/
protected def powerset {Ξ±} (S : set Ξ±) : π« S β set S :=
{ to_fun := Ξ» x : π« S, coe β»ΒΉ' (x : set Ξ±),
inv_fun := Ξ» x : set S, β¨coe '' x, by rintro _ β¨a : S, _, rflβ©; exact a.2β©,
left_inv := Ξ» x, by ext y; exact β¨Ξ» β¨β¨_, _β©, h, rflβ©, h, Ξ» h, β¨β¨_, x.2 hβ©, h, rflβ©β©,
right_inv := Ξ» x, by ext; simp }
/--
If `s` is a set in `range f`,
then its image under `range_splitting f` is in bijection (via `f`) with `s`.
-/
@[simps]
noncomputable def range_splitting_image_equiv {Ξ± Ξ² : Type*} (f : Ξ± β Ξ²) (s : set (range f)) :
range_splitting f '' s β s :=
{ to_fun := Ξ» x, β¨β¨f x, by simpβ©,
(by { rcases x with β¨x, β¨y, β¨m, rflβ©β©β©, simpa [apply_range_splitting f] using m, })β©,
inv_fun := Ξ» x, β¨range_splitting f x, β¨x, β¨x.2, rflβ©β©β©,
left_inv := Ξ» x, by { rcases x with β¨x, β¨y, β¨m, rflβ©β©β©, simp [apply_range_splitting f] },
right_inv := Ξ» x, by simp [apply_range_splitting f], }
end set
/-- If `f : Ξ± β Ξ²` has a left-inverse when `Ξ±` is nonempty, then `Ξ±` is computably equivalent to the
range of `f`.
While awkward, the `nonempty Ξ±` hypothesis on `f_inv` and `hf` allows this to be used when `Ξ±` is
empty too. This hypothesis is absent on analogous definitions on stronger `equiv`s like
`linear_equiv.of_left_inverse` and `ring_equiv.of_left_inverse` as their typeclass assumptions
are already sufficient to ensure non-emptiness. -/
@[simps]
def of_left_inverse {Ξ± Ξ² : Sort*}
(f : Ξ± β Ξ²) (f_inv : nonempty Ξ± β Ξ² β Ξ±) (hf : Ξ h : nonempty Ξ±, left_inverse (f_inv h) f) :
Ξ± β set.range f :=
{ to_fun := Ξ» a, β¨f a, a, rflβ©,
inv_fun := Ξ» b, f_inv (nonempty_of_exists b.2) b,
left_inv := Ξ» a, hf β¨aβ© a,
right_inv := Ξ» β¨b, a, haβ©, subtype.eq $ show f (f_inv β¨aβ© b) = b,
from eq.trans (congr_arg f $ by exact ha βΈ (hf _ a)) ha }
/-- If `f : Ξ± β Ξ²` has a left-inverse, then `Ξ±` is computably equivalent to the range of `f`.
Note that if `Ξ±` is empty, no such `f_inv` exists and so this definition can't be used, unlike
the stronger but less convenient `of_left_inverse`. -/
abbreviation of_left_inverse' {Ξ± Ξ² : Sort*}
(f : Ξ± β Ξ²) (f_inv : Ξ² β Ξ±) (hf : left_inverse f_inv f) :
Ξ± β set.range f :=
of_left_inverse f (Ξ» _, f_inv) (Ξ» _, hf)
/-- If `f : Ξ± β Ξ²` is an injective function, then domain `Ξ±` is equivalent to the range of `f`. -/
@[simps apply]
noncomputable def of_injective {Ξ± Ξ²} (f : Ξ± β Ξ²) (hf : injective f) : Ξ± β set.range f :=
equiv.of_left_inverse f
(Ξ» h, by exactI function.inv_fun f) (Ξ» h, by exactI function.left_inverse_inv_fun hf)
theorem apply_of_injective_symm {Ξ± Ξ²} (f : Ξ± β Ξ²) (hf : injective f) (b : set.range f) :
f ((of_injective f hf).symm b) = b :=
subtype.ext_iff.1 $ (of_injective f hf).apply_symm_apply b
@[simp] theorem of_injective_symm_apply {Ξ± Ξ²} (f : Ξ± β Ξ²) (hf : injective f) (a : Ξ±) :
(of_injective f hf).symm β¨f a, β¨a, rflβ©β© = a :=
begin
apply (of_injective f hf).injective,
simp [apply_of_injective_symm f hf],
end
@[simp] lemma self_comp_of_injective_symm {Ξ± Ξ²} (f : Ξ± β Ξ²) (hf : injective f) :
f β ((of_injective f hf).symm) = coe :=
funext (Ξ» x, apply_of_injective_symm f hf x)
lemma of_left_inverse_eq_of_injective {Ξ± Ξ² : Type*}
(f : Ξ± β Ξ²) (f_inv : nonempty Ξ± β Ξ² β Ξ±) (hf : Ξ h : nonempty Ξ±, left_inverse (f_inv h) f) :
of_left_inverse f f_inv hf = of_injective f
((em (nonempty Ξ±)).elim (Ξ» h, (hf h).injective) (Ξ» h _ _ _, by {
haveI : subsingleton Ξ± := subsingleton_of_not_nonempty h, simp })) :=
by { ext, simp }
lemma of_left_inverse'_eq_of_injective {Ξ± Ξ² : Type*}
(f : Ξ± β Ξ²) (f_inv : Ξ² β Ξ±) (hf : left_inverse f_inv f) :
of_left_inverse' f f_inv hf = of_injective f hf.injective :=
by { ext, simp }
/-- If `f` is a bijective function, then its domain is equivalent to its codomain. -/
@[simps apply]
noncomputable def of_bijective {Ξ± Ξ²} (f : Ξ± β Ξ²) (hf : bijective f) : Ξ± β Ξ² :=
(of_injective f hf.1).trans $ (set_congr hf.2.range_eq).trans $ equiv.set.univ Ξ²
lemma of_bijective_apply_symm_apply {Ξ± Ξ²} (f : Ξ± β Ξ²) (hf : bijective f) (x : Ξ²) :
f ((of_bijective f hf).symm x) = x :=
(of_bijective f hf).apply_symm_apply x
@[simp] lemma of_bijective_symm_apply_apply {Ξ± Ξ²} (f : Ξ± β Ξ²) (hf : bijective f) (x : Ξ±) :
(of_bijective f hf).symm (f x) = x :=
(of_bijective f hf).symm_apply_apply x
section
variables {Ξ±' Ξ²' : Type*} (e : perm Ξ±') {p : Ξ²' β Prop} [decidable_pred p]
(f : Ξ±' β subtype p)
/--
Extend the domain of `e : equiv.perm Ξ±` to one that is over `Ξ²` via `f : Ξ± β subtype p`,
where `p : Ξ² β Prop`, permuting only the `b : Ξ²` that satisfy `p b`.
This can be used to extend the domain across a function `f : Ξ± β Ξ²`,
keeping everything outside of `set.range f` fixed. For this use-case `equiv` given by `f` can
be constructed by `equiv.of_left_inverse'` or `equiv.of_left_inverse` when there is a known
inverse, or `equiv.of_injective` in the general case.`.
-/
def perm.extend_domain : perm Ξ²' :=
(perm_congr f e).subtype_congr (equiv.refl _)
@[simp] lemma perm.extend_domain_apply_image (a : Ξ±') :
e.extend_domain f (f a) = f (e a) :=
by simp [perm.extend_domain]
lemma perm.extend_domain_apply_subtype {b : Ξ²'} (h : p b) :
e.extend_domain f b = f (e (f.symm β¨b, hβ©)) :=
by simp [perm.extend_domain, h]
lemma perm.extend_domain_apply_not_subtype {b : Ξ²'} (h : Β¬ p b) :
e.extend_domain f b = b :=
by simp [perm.extend_domain, h]
@[simp] lemma perm.extend_domain_refl : perm.extend_domain (equiv.refl _) f = equiv.refl _ :=
by simp [perm.extend_domain]
@[simp] lemma perm.extend_domain_symm :
(e.extend_domain f).symm = perm.extend_domain e.symm f := rfl
lemma perm.extend_domain_trans (e e' : perm Ξ±') :
(e.extend_domain f).trans (e'.extend_domain f) = perm.extend_domain (e.trans e') f :=
by simp [perm.extend_domain, perm_congr_trans]
end
/-- Subtype of the quotient is equivalent to the quotient of the subtype. Let `Ξ±` be a setoid with
equivalence relation `~`. Let `pβ` be a predicate on the quotient type `Ξ±/~`, and `pβ` be the lift
of this predicate to `Ξ±`: `pβ a β pβ β¦aβ§`. Let `~β` be the restriction of `~` to `{x // pβ x}`.
Then `{x // pβ x}` is equivalent to the quotient of `{x // pβ x}` by `~β`. -/
def subtype_quotient_equiv_quotient_subtype (pβ : Ξ± β Prop) [sβ : setoid Ξ±]
[sβ : setoid (subtype pβ)] (pβ : quotient sβ β Prop) (hpβ : β a, pβ a β pβ β¦aβ§)
(h : β x y : subtype pβ, @setoid.r _ sβ x y β (x : Ξ±) β y) :
{x // pβ x} β quotient sβ :=
{ to_fun := Ξ» a, quotient.hrec_on a.1 (Ξ» a h, β¦β¨a, (hpβ _).2 hβ©β§)
(Ξ» a b hab, hfunext (by rw quotient.sound hab)
(Ξ» hβ hβ _, heq_of_eq (quotient.sound ((h _ _).2 hab)))) a.2,
inv_fun := Ξ» a, quotient.lift_on a (Ξ» a, (β¨β¦a.1β§, (hpβ _).1 a.2β© : {x // pβ x}))
(Ξ» a b hab, subtype.ext_val (quotient.sound ((h _ _).1 hab))),
left_inv := Ξ» β¨a, haβ©, quotient.induction_on a (Ξ» a ha, rfl) ha,
right_inv := Ξ» a, quotient.induction_on a (Ξ» β¨a, haβ©, rfl) }
section swap
variable [decidable_eq Ξ±]
/-- A helper function for `equiv.swap`. -/
def swap_core (a b r : Ξ±) : Ξ± :=
if r = a then b
else if r = b then a
else r
theorem swap_core_self (r a : Ξ±) : swap_core a a r = r :=
by { unfold swap_core, split_ifs; cc }
theorem swap_core_swap_core (r a b : Ξ±) : swap_core a b (swap_core a b r) = r :=
by { unfold swap_core, split_ifs; cc }
theorem swap_core_comm (r a b : Ξ±) : swap_core a b r = swap_core b a r :=
by { unfold swap_core, split_ifs; cc }
/-- `swap a b` is the permutation that swaps `a` and `b` and
leaves other values as is. -/
def swap (a b : Ξ±) : perm Ξ± :=
β¨swap_core a b, swap_core a b, Ξ»r, swap_core_swap_core r a b, Ξ»r, swap_core_swap_core r a bβ©
@[simp] theorem swap_self (a : Ξ±) : swap a a = equiv.refl _ :=
ext $ Ξ» r, swap_core_self r a
theorem swap_comm (a b : Ξ±) : swap a b = swap b a :=
ext $ Ξ» r, swap_core_comm r _ _
theorem swap_apply_def (a b x : Ξ±) : swap a b x = if x = a then b else if x = b then a else x :=
rfl
@[simp] theorem swap_apply_left (a b : Ξ±) : swap a b a = b :=
if_pos rfl
@[simp] theorem swap_apply_right (a b : Ξ±) : swap a b b = a :=
by { by_cases h : b = a; simp [swap_apply_def, h], }
theorem swap_apply_of_ne_of_ne {a b x : Ξ±} : x β a β x β b β swap a b x = x :=
by simp [swap_apply_def] {contextual := tt}
@[simp] theorem swap_swap (a b : Ξ±) : (swap a b).trans (swap a b) = equiv.refl _ :=
ext $ Ξ» x, swap_core_swap_core _ _ _
@[simp] lemma symm_swap (a b : Ξ±) : (swap a b).symm = swap a b := rfl
@[simp] lemma swap_eq_refl_iff {x y : Ξ±} : swap x y = equiv.refl _ β x = y :=
begin
refine β¨Ξ» h, (equiv.refl _).injective _, Ξ» h, h βΈ (swap_self _)β©,
rw [βh, swap_apply_left, h, refl_apply]
end
theorem swap_comp_apply {a b x : Ξ±} (Ο : perm Ξ±) :
Ο.trans (swap a b) x = if Ο x = a then b else if Ο x = b then a else Ο x :=
by { cases Ο, refl }
lemma swap_eq_update (i j : Ξ±) :
β(equiv.swap i j) = update (update id j i) i j :=
funext $ Ξ» x, by rw [update_apply _ i j, update_apply _ j i, equiv.swap_apply_def, id.def]
lemma comp_swap_eq_update (i j : Ξ±) (f : Ξ± β Ξ²) :
f β equiv.swap i j = update (update f j (f i)) i (f j) :=
by rw [swap_eq_update, comp_update, comp_update, comp.right_id]
@[simp] lemma symm_trans_swap_trans [decidable_eq Ξ²] (a b : Ξ±) (e : Ξ± β Ξ²) :
(e.symm.trans (swap a b)).trans e = swap (e a) (e b) :=
equiv.ext (Ξ» x, begin
have : β a, e.symm x = a β x = e a :=
Ξ» a, by { rw @eq_comm _ (e.symm x), split; intros; simp * at * },
simp [swap_apply_def, this],
split_ifs; simp
end)
@[simp] lemma trans_swap_trans_symm [decidable_eq Ξ²] (a b : Ξ²)
(e : Ξ± β Ξ²) : (e.trans (swap a b)).trans e.symm = swap (e.symm a) (e.symm b) :=
symm_trans_swap_trans a b e.symm
@[simp] lemma swap_apply_self (i j a : Ξ±) :
swap i j (swap i j a) = a :=
by rw [β equiv.trans_apply, equiv.swap_swap, equiv.refl_apply]
/-- A function is invariant to a swap if it is equal at both elements -/
lemma apply_swap_eq_self {v : Ξ± β Ξ²} {i j : Ξ±} (hv : v i = v j) (k : Ξ±) : v (swap i j k) = v k :=
begin
by_cases hi : k = i, { rw [hi, swap_apply_left, hv] },
by_cases hj : k = j, { rw [hj, swap_apply_right, hv] },
rw swap_apply_of_ne_of_ne hi hj,
end
lemma swap_apply_eq_iff {x y z w : Ξ±} :
swap x y z = w β z = swap x y w :=
by rw [apply_eq_iff_eq_symm_apply, symm_swap]
lemma swap_apply_ne_self_iff {a b x : Ξ±} : swap a b x β x β a β b β§ (x = a β¨ x = b) :=
begin
by_cases hab : a = b,
{ simp [hab] },
by_cases hax : x = a,
{ simp [hax, eq_comm] },
by_cases hbx : x = b,
{ simp [hbx] },
simp [hab, hax, hbx, swap_apply_of_ne_of_ne]
end
namespace perm
@[simp] lemma sum_congr_swap_refl {Ξ± Ξ² : Sort*} [decidable_eq Ξ±] [decidable_eq Ξ²] (i j : Ξ±) :
equiv.perm.sum_congr (equiv.swap i j) (equiv.refl Ξ²) = equiv.swap (sum.inl i) (sum.inl j) :=
begin
ext x,
cases x,
{ simp [sum.map, swap_apply_def],
split_ifs; refl},
{ simp [sum.map, swap_apply_of_ne_of_ne] },
end
@[simp] lemma sum_congr_refl_swap {Ξ± Ξ² : Sort*} [decidable_eq Ξ±] [decidable_eq Ξ²] (i j : Ξ²) :
equiv.perm.sum_congr (equiv.refl Ξ±) (equiv.swap i j) = equiv.swap (sum.inr i) (sum.inr j) :=
begin
ext x,
cases x,
{ simp [sum.map, swap_apply_of_ne_of_ne] },
{ simp [sum.map, swap_apply_def],
split_ifs; refl},
end
end perm
/-- Augment an equivalence with a prescribed mapping `f a = b` -/
def set_value (f : Ξ± β Ξ²) (a : Ξ±) (b : Ξ²) : Ξ± β Ξ² :=
(swap a (f.symm b)).trans f
@[simp] theorem set_value_eq (f : Ξ± β Ξ²) (a : Ξ±) (b : Ξ²) : set_value f a b a = b :=
by { dsimp [set_value], simp [swap_apply_left] }
end swap
end equiv
lemma plift.eq_up_iff_down_eq {x : plift Ξ±} {y : Ξ±} : x = plift.up y β x.down = y :=
equiv.plift.eq_symm_apply
lemma function.injective.map_swap {Ξ± Ξ² : Type*} [decidable_eq Ξ±] [decidable_eq Ξ²]
{f : Ξ± β Ξ²} (hf : function.injective f) (x y z : Ξ±) :
f (equiv.swap x y z) = equiv.swap (f x) (f y) (f z) :=
begin
conv_rhs { rw equiv.swap_apply_def },
split_ifs with hβ hβ,
{ rw [hf hβ, equiv.swap_apply_left] },
{ rw [hf hβ, equiv.swap_apply_right] },
{ rw [equiv.swap_apply_of_ne_of_ne (mt (congr_arg f) hβ) (mt (congr_arg f) hβ)] }
end
namespace equiv
protected lemma exists_unique_congr {p : Ξ± β Prop} {q : Ξ² β Prop} (f : Ξ± β Ξ²)
(h : β{x}, p x β q (f x)) : (β! x, p x) β β! y, q y :=
begin
split,
{ rintro β¨a, haβ, haββ©,
exact β¨f a, h.1 haβ, Ξ» b hb, f.symm_apply_eq.1 (haβ (f.symm b) (h.2 (by simpa using hb)))β© },
{ rintro β¨b, hbβ, hbββ©,
exact β¨f.symm b, h.2 (by simpa using hbβ), Ξ» y hy, (eq_symm_apply f).2 (hbβ _ (h.1 hy))β© }
end
protected lemma exists_unique_congr_left' {p : Ξ± β Prop} (f : Ξ± β Ξ²) :
(β! x, p x) β (β! y, p (f.symm y)) :=
equiv.exists_unique_congr f (Ξ»x, by simp)
protected lemma exists_unique_congr_left {p : Ξ² β Prop} (f : Ξ± β Ξ²) :
(β! x, p (f x)) β (β! y, p y) :=
(equiv.exists_unique_congr_left' f.symm).symm
protected lemma forall_congr {p : Ξ± β Prop} {q : Ξ² β Prop} (f : Ξ± β Ξ²)
(h : β{x}, p x β q (f x)) : (βx, p x) β (βy, q y) :=
begin
split; intros hβ x,
{ rw [βf.right_inv x], apply h.mp, apply hβ },
apply h.mpr, apply hβ
end
protected lemma forall_congr' {p : Ξ± β Prop} {q : Ξ² β Prop} (f : Ξ± β Ξ²)
(h : β{x}, p (f.symm x) β q x) : (βx, p x) β (βy, q y) :=
(equiv.forall_congr f.symm (Ξ» x, h.symm)).symm
-- We next build some higher arity versions of `equiv.forall_congr`.
-- Although they appear to just be repeated applications of `equiv.forall_congr`,
-- unification of metavariables works better with these versions.
-- In particular, they are necessary in `equiv_rw`.
-- (Stopping at ternary functions seems reasonable: at least in 1-categorical mathematics,
-- it's rare to have axioms involving more than 3 elements at once.)
universes ua1 ua2 ub1 ub2 ug1 ug2
variables {Ξ±β : Sort ua1} {Ξ±β : Sort ua2}
{Ξ²β : Sort ub1} {Ξ²β : Sort ub2}
{Ξ³β : Sort ug1} {Ξ³β : Sort ug2}
protected lemma forallβ_congr {p : Ξ±β β Ξ²β β Prop} {q : Ξ±β β Ξ²β β Prop} (eΞ± : Ξ±β β Ξ±β)
(eΞ² : Ξ²β β Ξ²β) (h : β{x y}, p x y β q (eΞ± x) (eΞ² y)) :
(βx y, p x y) β (βx y, q x y) :=
begin
apply equiv.forall_congr,
intros,
apply equiv.forall_congr,
intros,
apply h,
end
protected lemma forallβ_congr' {p : Ξ±β β Ξ²β β Prop} {q : Ξ±β β Ξ²β β Prop} (eΞ± : Ξ±β β Ξ±β)
(eΞ² : Ξ²β β Ξ²β) (h : β{x y}, p (eΞ±.symm x) (eΞ².symm y) β q x y) :
(βx y, p x y) β (βx y, q x y) :=
(equiv.forallβ_congr eΞ±.symm eΞ².symm (Ξ» x y, h.symm)).symm
protected lemma forallβ_congr {p : Ξ±β β Ξ²β β Ξ³β β Prop} {q : Ξ±β β Ξ²β β Ξ³β β Prop}
(eΞ± : Ξ±β β Ξ±β) (eΞ² : Ξ²β β Ξ²β) (eΞ³ : Ξ³β β Ξ³β)
(h : β{x y z}, p x y z β q (eΞ± x) (eΞ² y) (eΞ³ z)) : (βx y z, p x y z) β (βx y z, q x y z) :=
begin
apply equiv.forallβ_congr,
intros,
apply equiv.forall_congr,
intros,
apply h,
end
protected lemma forallβ_congr' {p : Ξ±β β Ξ²β β Ξ³β β Prop} {q : Ξ±β β Ξ²β β Ξ³β β Prop}
(eΞ± : Ξ±β β Ξ±β) (eΞ² : Ξ²β β Ξ²β) (eΞ³ : Ξ³β β Ξ³β)
(h : β{x y z}, p (eΞ±.symm x) (eΞ².symm y) (eΞ³.symm z) β q x y z) :
(βx y z, p x y z) β (βx y z, q x y z) :=
(equiv.forallβ_congr eΞ±.symm eΞ².symm eΞ³.symm (Ξ» x y z, h.symm)).symm
protected lemma forall_congr_left' {p : Ξ± β Prop} (f : Ξ± β Ξ²) :
(βx, p x) β (βy, p (f.symm y)) :=
equiv.forall_congr f (Ξ»x, by simp)
protected lemma forall_congr_left {p : Ξ² β Prop} (f : Ξ± β Ξ²) :
(βx, p (f x)) β (βy, p y) :=
(equiv.forall_congr_left' f.symm).symm
protected lemma exists_congr_left {Ξ± Ξ²} (f : Ξ± β Ξ²) {p : Ξ± β Prop} :
(β a, p a) β (β b, p (f.symm b)) :=
β¨Ξ» β¨a, hβ©, β¨f a, by simpa using hβ©, Ξ» β¨b, hβ©, β¨_, hβ©β©
protected lemma set_forall_iff {Ξ± Ξ²} (e : Ξ± β Ξ²) {p : set Ξ± β Prop} :
(β a, p a) β (β a, p (e β»ΒΉ' a)) :=
by simpa [equiv.image_eq_preimage] using (equiv.set.congr e).forall_congr_left'
protected lemma preimage_sUnion {Ξ± Ξ²} (f : Ξ± β Ξ²) {s : set (set Ξ²)} :
f β»ΒΉ' (ββ s) = ββ (_root_.set.image f β»ΒΉ' s) :=
by { ext x, simp [(equiv.set.congr f).symm.exists_congr_left] }
section
variables (P : Ξ± β Sort w) (e : Ξ± β Ξ²)
/--
Transport dependent functions through an equivalence of the base space.
-/
@[simps] def Pi_congr_left' : (Ξ a, P a) β (Ξ b, P (e.symm b)) :=
{ to_fun := Ξ» f x, f (e.symm x),
inv_fun := Ξ» f x, begin rw [β e.symm_apply_apply x], exact f (e x) end,
left_inv := Ξ» f, funext $ Ξ» x, eq_of_heq ((eq_rec_heq _ _).trans
(by { dsimp, rw e.symm_apply_apply })),
right_inv := Ξ» f, funext $ Ξ» x, eq_of_heq ((eq_rec_heq _ _).trans
(by { rw e.apply_symm_apply })) }
end
section
variables (P : Ξ² β Sort w) (e : Ξ± β Ξ²)
/--
Transporting dependent functions through an equivalence of the base,
expressed as a "simplification".
-/
def Pi_congr_left : (Ξ a, P (e a)) β (Ξ b, P b) :=
(Pi_congr_left' P e.symm).symm
end
section
variables
{W : Ξ± β Sort w} {Z : Ξ² β Sort z} (hβ : Ξ± β Ξ²) (hβ : Ξ a : Ξ±, (W a β Z (hβ a)))
/--
Transport dependent functions through
an equivalence of the base spaces and a family
of equivalences of the matching fibers.
-/
def Pi_congr : (Ξ a, W a) β (Ξ b, Z b) :=
(equiv.Pi_congr_right hβ).trans (equiv.Pi_congr_left _ hβ)
end
section
variables
{W : Ξ± β Sort w} {Z : Ξ² β Sort z} (hβ : Ξ± β Ξ²) (hβ : Ξ b : Ξ², (W (hβ.symm b) β Z b))
/--
Transport dependent functions through
an equivalence of the base spaces and a family
of equivalences of the matching fibres.
-/
def Pi_congr' : (Ξ a, W a) β (Ξ b, Z b) :=
(Pi_congr hβ.symm (Ξ» b, (hβ b).symm)).symm
end
end equiv
lemma function.injective.swap_apply [decidable_eq Ξ±] [decidable_eq Ξ²] {f : Ξ± β Ξ²}
(hf : function.injective f) (x y z : Ξ±) :
equiv.swap (f x) (f y) (f z) = f (equiv.swap x y z) :=
begin
by_cases hx : z = x, by simp [hx],
by_cases hy : z = y, by simp [hy],
rw [equiv.swap_apply_of_ne_of_ne hx hy, equiv.swap_apply_of_ne_of_ne (hf.ne hx) (hf.ne hy)]
end
lemma function.injective.swap_comp [decidable_eq Ξ±] [decidable_eq Ξ²] {f : Ξ± β Ξ²}
(hf : function.injective f) (x y : Ξ±) :
equiv.swap (f x) (f y) β f = f β equiv.swap x y :=
funext $ Ξ» z, hf.swap_apply _ _ _
instance {Ξ±} [subsingleton Ξ±] : subsingleton (ulift Ξ±) := equiv.ulift.subsingleton
instance {Ξ±} [subsingleton Ξ±] : subsingleton (plift Ξ±) := equiv.plift.subsingleton
instance {Ξ±} [unique Ξ±] : unique (ulift Ξ±) := equiv.ulift.unique
instance {Ξ±} [unique Ξ±] : unique (plift Ξ±) := equiv.plift.unique
instance {Ξ±} [decidable_eq Ξ±] : decidable_eq (ulift Ξ±) := equiv.ulift.decidable_eq
instance {Ξ±} [decidable_eq Ξ±] : decidable_eq (plift Ξ±) := equiv.plift.decidable_eq
/-- If both `Ξ±` and `Ξ²` are singletons, then `Ξ± β Ξ²`. -/
def equiv_of_unique_of_unique [unique Ξ±] [unique Ξ²] : Ξ± β Ξ² :=
{ to_fun := Ξ» _, default Ξ²,
inv_fun := Ξ» _, default Ξ±,
left_inv := Ξ» _, subsingleton.elim _ _,
right_inv := Ξ» _, subsingleton.elim _ _ }
/-- If `Ξ±` is a singleton, then it is equivalent to any `punit`. -/
def equiv_punit_of_unique [unique Ξ±] : Ξ± β punit.{v} :=
equiv_of_unique_of_unique
/-- If `Ξ±` is a subsingleton, then it is equivalent to `Ξ± Γ Ξ±`. -/
def subsingleton_prod_self_equiv {Ξ± : Type*} [subsingleton Ξ±] : Ξ± Γ Ξ± β Ξ± :=
{ to_fun := Ξ» p, p.1,
inv_fun := Ξ» a, (a, a),
left_inv := Ξ» p, subsingleton.elim _ _,
right_inv := Ξ» p, subsingleton.elim _ _, }
/-- To give an equivalence between two subsingleton types, it is sufficient to give any two
functions between them. -/
def equiv_of_subsingleton_of_subsingleton [subsingleton Ξ±] [subsingleton Ξ²]
(f : Ξ± β Ξ²) (g : Ξ² β Ξ±) : Ξ± β Ξ² :=
{ to_fun := f,
inv_fun := g,
left_inv := Ξ» _, subsingleton.elim _ _,
right_inv := Ξ» _, subsingleton.elim _ _ }
/-- A nonempty subsingleton type is (noncomputably) equivalent to `punit`. -/
noncomputable
def equiv.punit_of_nonempty_of_subsingleton {Ξ± : Sort*} [h : nonempty Ξ±] [subsingleton Ξ±] :
Ξ± β punit.{v} :=
equiv_of_subsingleton_of_subsingleton
(Ξ» _, punit.star) (Ξ» _, h.some)
/-- `unique (unique Ξ±)` is equivalent to `unique Ξ±`. -/
def unique_unique_equiv : unique (unique Ξ±) β unique Ξ± :=
equiv_of_subsingleton_of_subsingleton (Ξ» h, h.default)
(Ξ» h, { default := h, uniq := Ξ» _, subsingleton.elim _ _ })
namespace quot
/-- An equivalence `e : Ξ± β Ξ²` generates an equivalence between quotient spaces,
if `ra aβ aβ β rb (e aβ) (e aβ). -/
protected def congr {ra : Ξ± β Ξ± β Prop} {rb : Ξ² β Ξ² β Prop} (e : Ξ± β Ξ²)
(eq : βaβ aβ, ra aβ aβ β rb (e aβ) (e aβ)) :
quot ra β quot rb :=
{ to_fun := quot.map e (assume aβ aβ, (eq aβ aβ).1),
inv_fun := quot.map e.symm
(assume bβ bβ h,
(eq (e.symm bβ) (e.symm bβ)).2
((e.apply_symm_apply bβ).symm βΈ (e.apply_symm_apply bβ).symm βΈ h)),
left_inv := by { rintros β¨aβ©, dunfold quot.map, simp only [equiv.symm_apply_apply] },
right_inv := by { rintros β¨aβ©, dunfold quot.map, simp only [equiv.apply_symm_apply] } }
@[simp]
lemma congr_mk {ra : Ξ± β Ξ± β Prop} {rb : Ξ² β Ξ² β Prop} (e : Ξ± β Ξ²)
(eq : β (aβ aβ : Ξ±), ra aβ aβ β rb (e aβ) (e aβ)) (a : Ξ±) :
quot.congr e eq (quot.mk ra a) = quot.mk rb (e a) := rfl
/-- Quotients are congruent on equivalences under equality of their relation.
An alternative is just to use rewriting with `eq`, but then computational proofs get stuck. -/
protected def congr_right {r r' : Ξ± β Ξ± β Prop} (eq : βaβ aβ, r aβ aβ β r' aβ aβ) :
quot r β quot r' :=
quot.congr (equiv.refl Ξ±) eq
/-- An equivalence `e : Ξ± β Ξ²` generates an equivalence between the quotient space of `Ξ±`
by a relation `ra` and the quotient space of `Ξ²` by the image of this relation under `e`. -/
protected def congr_left {r : Ξ± β Ξ± β Prop} (e : Ξ± β Ξ²) :
quot r β quot (Ξ» b b', r (e.symm b) (e.symm b')) :=
@quot.congr Ξ± Ξ² r (Ξ» b b', r (e.symm b) (e.symm b')) e (Ξ» aβ aβ, by simp only [e.symm_apply_apply])
end quot
namespace quotient
/-- An equivalence `e : Ξ± β Ξ²` generates an equivalence between quotient spaces,
if `ra aβ aβ β rb (e aβ) (e aβ). -/
protected def congr {ra : setoid Ξ±} {rb : setoid Ξ²} (e : Ξ± β Ξ²)
(eq : βaβ aβ, @setoid.r Ξ± ra aβ aβ β @setoid.r Ξ² rb (e aβ) (e aβ)) :
quotient ra β quotient rb :=
quot.congr e eq
@[simp]
lemma congr_mk {ra : setoid Ξ±} {rb : setoid Ξ²} (e : Ξ± β Ξ²)
(eq : β (aβ aβ : Ξ±), setoid.r aβ aβ β setoid.r (e aβ) (e aβ)) (a : Ξ±):
quotient.congr e eq (quotient.mk a) = quotient.mk (e a) :=
rfl
/-- Quotients are congruent on equivalences under equality of their relation.
An alternative is just to use rewriting with `eq`, but then computational proofs get stuck. -/
protected def congr_right {r r' : setoid Ξ±}
(eq : βaβ aβ, @setoid.r Ξ± r aβ aβ β @setoid.r Ξ± r' aβ aβ) : quotient r β quotient r' :=
quot.congr_right eq
end quotient
/-- If a function is a bijection between two sets `s` and `t`, then it induces an
equivalence between the the types `β₯s` and ``β₯t`. -/
noncomputable def set.bij_on.equiv {Ξ± : Type*} {Ξ² : Type*} {s : set Ξ±} {t : set Ξ²} (f : Ξ± β Ξ²)
(h : set.bij_on f s t) : s β t :=
equiv.of_bijective _ h.bijective
namespace function
lemma update_comp_equiv {Ξ± Ξ² Ξ±' : Sort*} [decidable_eq Ξ±'] [decidable_eq Ξ±] (f : Ξ± β Ξ²) (g : Ξ±' β Ξ±)
(a : Ξ±) (v : Ξ²) :
update f a v β g = update (f β g) (g.symm a) v :=
by rw [β update_comp_eq_of_injective _ g.injective, g.apply_symm_apply]
lemma update_apply_equiv_apply {Ξ± Ξ² Ξ±' : Sort*} [decidable_eq Ξ±'] [decidable_eq Ξ±]
(f : Ξ± β Ξ²) (g : Ξ±' β Ξ±) (a : Ξ±) (v : Ξ²) (a' : Ξ±') :
update f a v (g a') = update (f β g) (g.symm a) v a' :=
congr_fun (update_comp_equiv f g a v) a'
end function
/-- The composition of an updated function with an equiv on a subset can be expressed as an
updated function. -/
lemma dite_comp_equiv_update {Ξ± : Type*} {Ξ² : Sort*} {Ξ³ : Sort*} {s : set Ξ±} (e : Ξ² β s)
(v : Ξ² β Ξ³) (w : Ξ± β Ξ³) (j : Ξ²) (x : Ξ³) [decidable_eq Ξ²] [decidable_eq Ξ±]
[β j, decidable (j β s)] :
(Ξ» (i : Ξ±), if h : i β s then (function.update v j x) (e.symm β¨i, hβ©) else w i) =
function.update (Ξ» (i : Ξ±), if h : i β s then v (e.symm β¨i, hβ©) else w i) (e j) x :=
begin
ext i,
by_cases h : i β s,
{ rw [dif_pos h,
function.update_apply_equiv_apply, equiv.symm_symm, function.comp,
function.update_apply, function.update_apply,
dif_pos h],
have h_coe : (β¨i, hβ© : s) = e j β i = e j := subtype.ext_iff.trans (by rw subtype.coe_mk),
simp_rw h_coe,
congr, },
{ have : i β e j,
by { contrapose! h, have : (e j : Ξ±) β s := (e j).2, rwa β h at this },
simp [h, this] }
end
|
46d67c2f28a7a74de9004bda03c10d4f07013b0a | 55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5 | /src/field_theory/chevalley_warning.lean | b4e592a7e799c754b57e29658025c228836f8084 | [
"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 | 8,463 | lean | /-
Copyright (c) 2019 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin
-/
import data.mv_polynomial
import field_theory.finite
/-!
# The ChevalleyβWarning theorem
This file contains a proof of the ChevalleyβWarning theorem.
Throughout most of this file, `K` denotes a finite field
and `q` is notation for the cardinality of `K`.
## Main results
1. Let `f` be a multivariate polynomial in finitely many variables (`X s`, `s : Ο`)
such that the total degree of `f` is less than `(q-1)` times the cardinality of `Ο`.
Then the evaluation of `f` on all points of `Ο β K` (aka `K^Ο`) sums to `0`.
(`sum_mv_polynomial_eq_zero`)
2. The ChevalleyβWarning theorem (`char_dvd_card_solutions`).
Let `f i` be a finite family of multivariate polynomials
in finitely many variables (`X s`, `s : Ο`) such that
the sum of the total degrees of the `f i` is less than the cardinality of `Ο`.
Then the number of common solutions of the `f i`
is divisible by the characteristic of `K`.
## Notation
- `K` is a finite field
- `q` is notation for the cardinality of `K`
- `Ο` is the indexing type for the variables of a multivariate polynomial ring over `K`
-/
universes u v
open_locale big_operators
section finite_field
open mv_polynomial function finset finite_field
variables {K : Type*} {Ο : Type*} [fintype K] [field K] [fintype Ο]
local notation `q` := fintype.card K
lemma mv_polynomial.sum_mv_polynomial_eq_zero [decidable_eq Ο] (f : mv_polynomial Ο K)
(h : f.total_degree < (q - 1) * fintype.card Ο) :
(β x, f.eval x) = 0 :=
begin
haveI : decidable_eq K := classical.dec_eq K,
calc (β x, f.eval x)
= β x : Ο β K, β d in f.support, f.coeff d * β i, x i ^ d i : by simp only [eval_eq']
... = β d in f.support, β x : Ο β K, f.coeff d * β i, x i ^ d i : sum_comm
... = 0 : sum_eq_zero _,
intros d hd,
obtain β¨i, hiβ© : β i, d i < q - 1, from f.exists_degree_lt (q - 1) h hd,
calc (β x : Ο β K, f.coeff d * β i, x i ^ d i)
= f.coeff d * (β x : Ο β K, β i, x i ^ d i) : mul_sum.symm
... = 0 : (mul_eq_zero.mpr β or.inr) _,
calc (β x : Ο β K, β i, x i ^ d i)
= β (xβ : {j // j β i} β K) (x : {x : Ο β K // x β coe = xβ}), β j, (x : Ο β K) j ^ d j :
(fintype.sum_fiberwise _ _).symm
... = 0 : fintype.sum_eq_zero _ _,
intros xβ,
let e : K β {x // x β coe = xβ} := (equiv.subtype_equiv_codomain _).symm,
calc (β x : {x : Ο β K // x β coe = xβ}, β j, (x : Ο β K) j ^ d j)
= β a : K, β j : Ο, (e a : Ο β K) j ^ d j : (finset.sum_equiv e _).symm
... = β a : K, (β j, xβ j ^ d j) * a ^ d i : fintype.sum_congr _ _ _
... = (β j, xβ j ^ d j) * β a : K, a ^ d i : by rw mul_sum
... = 0 : by rw [sum_pow_lt_card_sub_one _ hi, mul_zero],
intros a,
let e' : {j // j = i} β {j // j β i} β Ο := equiv.sum_compl _,
letI : unique {j // j = i} :=
{ default := β¨i, rflβ©, uniq := Ξ» β¨j, hβ©, subtype.val_injective h },
calc (β j : Ο, (e a : Ο β K) j ^ d j)
= (e a : Ο β K) i ^ d i * (β (j : {j // j β i}), (e a : Ο β K) j ^ d j) :
by { rw [β finset.prod_equiv e', fintype.prod_sum_type, univ_unique, prod_singleton], refl }
... = a ^ d i * (β (j : {j // j β i}), (e a : Ο β K) j ^ d j) : by rw equiv.subtype_equiv_codomain_symm_apply_eq
... = a ^ d i * (β j, xβ j ^ d j) : congr_arg _ (fintype.prod_congr _ _ _) -- see below
... = (β j, xβ j ^ d j) * a ^ d i : mul_comm _ _,
{ -- the remaining step of the calculation above
rintros β¨j, hjβ©,
show (e a : Ο β K) j ^ d j = xβ β¨j, hjβ© ^ d j,
rw equiv.subtype_equiv_codomain_symm_apply_ne, }
end
variables [decidable_eq K] [decidable_eq Ο]
/-- The ChevalleyβWarning theorem.
Let `(f i)` be a finite family of multivariate polynomials
in finitely many variables (`X s`, `s : Ο`) over a finite field of characteristic `p`.
Assume that the sum of the total degrees of the `f i` is less than the cardinality of `Ο`.
Then the number of common solutions of the `f i` is divisible by `p`. -/
theorem char_dvd_card_solutions_family (p : β) [char_p K p]
{ΞΉ : Type*} {s : finset ΞΉ} {f : ΞΉ β mv_polynomial Ο K}
(h : (β i in s, (f i).total_degree) < fintype.card Ο) :
p β£ fintype.card {x : Ο β K // β i β s, (f i).eval x = 0} :=
begin
have hq : 0 < q - 1, { rw [β card_units, fintype.card_pos_iff], exact β¨1β© },
let S : finset (Ο β K) := { x β univ | β i β s, (f i).eval x = 0 },
have hS : β (x : Ο β K), x β S β β (i : ΞΉ), i β s β eval x (f i) = 0,
{ intros x, simp only [S, true_and, sep_def, mem_filter, mem_univ], },
/- The polynomial `F = β i in s, (1 - (f i)^(q - 1))` has the nice property
that it takes the value `1` on elements of `{x : Ο β K // β i β s, (f i).eval x = 0}`
while it is `0` outside that locus.
Hence the sum of its values is equal to the cardinality of
`{x : Ο β K // β i β s, (f i).eval x = 0}` modulo `p`. -/
let F : mv_polynomial Ο K := β i in s, (1 - (f i)^(q - 1)),
have hF : β x, F.eval x = if x β S then 1 else 0,
{ intro x,
calc F.eval x = β i in s, (1 - f i ^ (q - 1)).eval x : eval_prod s _ x
... = if x β S then 1 else 0 : _,
simp only [eval_sub, eval_pow, eval_one],
split_ifs with hx hx,
{ apply finset.prod_eq_one,
intros i hi,
rw hS at hx,
rw [hx i hi, zero_pow hq, sub_zero], },
{ obtain β¨i, hi, hxβ© : β (i : ΞΉ), i β s β§ (f i).eval x β 0,
{ simpa only [hS, classical.not_forall, classical.not_imp] using hx },
apply finset.prod_eq_zero hi,
rw [pow_card_sub_one_eq_one ((f i).eval x) hx, sub_self], } },
-- In particular, we can now show:
have key : β x, F.eval x = fintype.card {x : Ο β K // β i β s, (f i).eval x = 0},
rw [fintype.card_of_subtype S hS, card_eq_sum_ones, sum_nat_cast, nat.cast_one,
β fintype.sum_extend_by_zero S, sum_congr rfl (Ξ» x hx, hF x)],
-- With these preparations under our belt, we will approach the main goal.
show p β£ fintype.card {x // β (i : ΞΉ), i β s β (f i).eval x = 0},
rw [β char_p.cast_eq_zero_iff K, β key],
show β x, F.eval x = 0,
-- We are now ready to apply the main machine, proven before.
apply F.sum_mv_polynomial_eq_zero,
-- It remains to verify the crucial assumption of this machine
show F.total_degree < (q - 1) * fintype.card Ο,
calc F.total_degree β€ β i in s, (1 - (f i)^(q - 1)).total_degree : total_degree_finset_prod s _
... β€ β i in s, (q - 1) * (f i).total_degree : sum_le_sum $ Ξ» i hi, _ -- see β
... = (q - 1) * (β i in s, (f i).total_degree) : mul_sum.symm
... < (q - 1) * (fintype.card Ο) : by rwa mul_lt_mul_left hq,
-- Now we prove the remaining step from the preceding calculation
show (1 - f i ^ (q - 1)).total_degree β€ (q - 1) * (f i).total_degree,
calc (1 - f i ^ (q - 1)).total_degree
β€ max (1 : mv_polynomial Ο K).total_degree (f i ^ (q - 1)).total_degree : total_degree_sub _ _
... β€ (f i ^ (q - 1)).total_degree : by simp only [max_eq_right, nat.zero_le, total_degree_one]
... β€ (q - 1) * (f i).total_degree : total_degree_pow _ _
end
/-- The ChevalleyβWarning theorem.
Let `f` be a multivariate polynomial in finitely many variables (`X s`, `s : Ο`)
over a finite field of characteristic `p`.
Assume that the total degree of `f` is less than the cardinality of `Ο`.
Then the number of solutions of `f` is divisible by `p`.
See `char_dvd_card_solutions_family` for a version that takes a family of polynomials `f i`. -/
theorem char_dvd_card_solutions (p : β) [char_p K p]
{f : mv_polynomial Ο K} (h : f.total_degree < fintype.card Ο) :
p β£ fintype.card {x : Ο β K // f.eval x = 0} :=
begin
let F : unit β mv_polynomial Ο K := Ξ» _, f,
have : β i : unit, (F i).total_degree < fintype.card Ο,
{ simpa only [fintype.univ_punit, sum_singleton] using h, },
have key := char_dvd_card_solutions_family p this,
simp only [F, fintype.univ_punit, forall_eq, mem_singleton] at key,
convert key,
end
end finite_field
|
bfe563ec758ee0ca1fe8f6c6b64030044d731f41 | 4fa161becb8ce7378a709f5992a594764699e268 | /src/algebra/ordered_group.lean | 7e83220e4a2283ed4a6fd1aa5c310f3fff44b05f | [
"Apache-2.0"
] | permissive | laughinggas/mathlib | e4aa4565ae34e46e834434284cb26bd9d67bc373 | 86dcd5cda7a5017c8b3c8876c89a510a19d49aad | refs/heads/master | 1,669,496,232,688 | 1,592,831,995,000 | 1,592,831,995,000 | 274,155,979 | 0 | 0 | Apache-2.0 | 1,592,835,190,000 | 1,592,835,189,000 | null | UTF-8 | Lean | false | false | 64,035 | lean | /-
Copyright (c) 2016 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad, Leonardo de Moura, Mario Carneiro, Johannes HΓΆlzl
-/
import algebra.group.units
import algebra.group.with_one
import algebra.group.type_tags
import order.bounded_lattice
set_option old_structure_cmd true
set_option default_priority 100 -- see Note [default priority]
/-!
# Ordered monoids and groups
This file develops the basics of ordered monoids and groups.
## Implementation details
Unfortunately, the number of `'` appended to lemmas in this file
may differ between the multiplicative and the additive version of a lemma.
The reason is that we did not want to change existing names in the library.
-/
universe u
variable {Ξ± : Type u}
/-- An ordered commutative monoid is a commutative monoid
with a partial order such that
* `a β€ b β c * a β€ c * b` (multiplication is monotone)
* `a * b < a * c β b < c`.
-/
@[protect_proj, ancestor comm_monoid partial_order]
class ordered_comm_monoid (Ξ± : Type*) extends comm_monoid Ξ±, partial_order Ξ± :=
(mul_le_mul_left : β a b : Ξ±, a β€ b β β c : Ξ±, c * a β€ c * b)
(lt_of_mul_lt_mul_left : β a b c : Ξ±, a * b < a * c β b < c)
/-- An ordered (additive) commutative monoid is a commutative monoid
with a partial order such that
* `a β€ b β c + a β€ c + b` (addition is monotone)
* `a + b < a + c β b < c`.
-/
@[protect_proj, ancestor add_comm_monoid partial_order]
class ordered_add_comm_monoid (Ξ± : Type*) extends add_comm_monoid Ξ±, partial_order Ξ± :=
(add_le_add_left : β a b : Ξ±, a β€ b β β c : Ξ±, c + a β€ c + b)
(lt_of_add_lt_add_left : β a b c : Ξ±, a + b < a + c β b < c)
attribute [to_additive ordered_add_comm_monoid] ordered_comm_monoid
section ordered_comm_monoid
variables [ordered_comm_monoid Ξ±] {a b c d : Ξ±}
@[to_additive]
lemma mul_le_mul_left' (h : a β€ b) : c * a β€ c * b :=
ordered_comm_monoid.mul_le_mul_left a b h c
@[to_additive]
lemma mul_le_mul_right' (h : a β€ b) : a * c β€ b * c :=
mul_comm c a βΈ mul_comm c b βΈ mul_le_mul_left' h
@[to_additive]
lemma lt_of_mul_lt_mul_left' : a * b < a * c β b < c :=
ordered_comm_monoid.lt_of_mul_lt_mul_left a b c
@[to_additive]
lemma mul_le_mul' (hβ : a β€ b) (hβ : c β€ d) : a * c β€ b * d :=
le_trans (mul_le_mul_right' hβ) (mul_le_mul_left' hβ)
@[to_additive le_add_of_nonneg_right']
lemma le_mul_of_one_le_right'' (h : 1 β€ b) : a β€ a * b :=
have a * 1 β€ a * b, from mul_le_mul_left' h,
by rwa mul_one at this
@[to_additive le_add_of_nonneg_left']
lemma le_mul_of_one_le_left'' (h : 1 β€ b) : a β€ b * a :=
have 1 * a β€ b * a, from mul_le_mul_right' h,
by rwa one_mul at this
@[to_additive]
lemma lt_of_mul_lt_mul_right' (h : a * b < c * b) : a < c :=
lt_of_mul_lt_mul_left'
(show b * a < b * c, begin rw [mul_comm b a, mul_comm b c], assumption end)
-- here we start using properties of one.
@[to_additive]
lemma le_mul_of_one_le_of_le' (ha : 1 β€ a) (hbc : b β€ c) : b β€ a * c :=
one_mul b βΈ mul_le_mul' ha hbc
@[to_additive]
lemma le_mul_of_le_of_one_le' (hbc : b β€ c) (ha : 1 β€ a) : b β€ c * a :=
mul_one b βΈ mul_le_mul' hbc ha
@[to_additive add_nonneg']
lemma one_le_mul' (ha : 1 β€ a) (hb : 1 β€ b) : 1 β€ a * b :=
le_mul_of_one_le_of_le' ha hb
@[to_additive]
lemma mul_one_lt_of_one_lt_of_one_le' (ha : 1 < a) (hb : 1 β€ b) : 1 < a * b :=
lt_of_lt_of_le ha $ le_mul_of_one_le_right'' hb
@[to_additive]
lemma mul_one_lt' (ha : 1 < a) (hb : 1 < b) : 1 < a * b :=
mul_one_lt_of_one_lt_of_one_le' ha $ le_of_lt hb
@[to_additive]
lemma mul_one_lt_of_one_le_of_one_lt' (ha : 1 β€ a) (hb : 1 < b) : 1 < a * b :=
lt_of_lt_of_le hb $ le_mul_of_one_le_left'' ha
@[to_additive]
lemma mul_le_one' (ha : a β€ 1) (hb : b β€ 1) : a * b β€ 1 :=
one_mul (1:Ξ±) βΈ (mul_le_mul' ha hb)
@[to_additive]
lemma mul_le_of_le_one_of_le' (ha : a β€ 1) (hbc : b β€ c) : a * b β€ c :=
one_mul c βΈ mul_le_mul' ha hbc
@[to_additive]
lemma mul_le_of_le_of_le_one' (hbc : b β€ c) (ha : a β€ 1) : b * a β€ c :=
mul_one c βΈ mul_le_mul' hbc ha
@[to_additive]
lemma mul_lt_one_of_lt_one_of_le_one' (ha : a < 1) (hb : b β€ 1) : a * b < 1 :=
lt_of_le_of_lt (mul_le_of_le_of_le_one' (le_refl _) hb) ha
@[to_additive]
lemma mul_lt_one_of_le_one_of_lt_one' (ha : a β€ 1) (hb : b < 1) : a * b < 1 :=
lt_of_le_of_lt (mul_le_of_le_one_of_le' ha (le_refl _)) hb
@[to_additive]
lemma mul_lt_one' (ha : a < 1) (hb : b < 1) : a * b < 1 :=
mul_lt_one_of_le_one_of_lt_one' (le_of_lt ha) hb
@[to_additive]
lemma lt_mul_of_one_le_of_lt' (ha : 1 β€ a) (hbc : b < c) : b < a * c :=
lt_of_lt_of_le hbc $ le_mul_of_one_le_left'' ha
@[to_additive]
lemma lt_mul_of_lt_of_one_le' (hbc : b < c) (ha : 1 β€ a) : b < c * a :=
lt_of_lt_of_le hbc $ le_mul_of_one_le_right'' ha
@[to_additive]
lemma lt_mul_of_one_lt_of_lt' (ha : 1 < a) (hbc : b < c) : b < a * c :=
lt_mul_of_one_le_of_lt' (le_of_lt ha) hbc
@[to_additive]
lemma lt_mul_of_lt_of_one_lt' (hbc : b < c) (ha : 1 < a) : b < c * a :=
lt_mul_of_lt_of_one_le' hbc (le_of_lt ha)
@[to_additive]
lemma mul_lt_of_le_one_of_lt' (ha : a β€ 1) (hbc : b < c) : a * b < c :=
lt_of_le_of_lt (mul_le_of_le_one_of_le' ha (le_refl _)) hbc
@[to_additive]
lemma mul_lt_of_lt_of_le_one' (hbc : b < c) (ha : a β€ 1) : b * a < c :=
lt_of_le_of_lt (mul_le_of_le_of_le_one' (le_refl _) ha) hbc
@[to_additive]
lemma mul_lt_of_lt_one_of_lt' (ha : a < 1) (hbc : b < c) : a * b < c :=
mul_lt_of_le_one_of_lt' (le_of_lt ha) hbc
@[to_additive]
lemma mul_lt_of_lt_of_lt_one' (hbc : b < c) (ha : a < 1) : b * a < c :=
mul_lt_of_lt_of_le_one' hbc (le_of_lt ha)
@[to_additive]
lemma mul_eq_one_iff' (ha : 1 β€ a) (hb : 1 β€ b) : a * b = 1 β a = 1 β§ b = 1 :=
iff.intro
(assume hab : a * b = 1,
have a β€ 1, from hab βΈ le_mul_of_le_of_one_le' (le_refl _) hb,
have a = 1, from le_antisymm this ha,
have b β€ 1, from hab βΈ le_mul_of_one_le_of_le' ha (le_refl _),
have b = 1, from le_antisymm this hb,
and.intro βΉa = 1βΊ βΉb = 1βΊ)
(assume β¨ha', hb'β©, by rw [ha', hb', mul_one])
section mono
variables {Ξ² : Type*} [preorder Ξ²] {f g : Ξ² β Ξ±}
@[to_additive monotone.add]
lemma monotone.mul' (hf : monotone f) (hg : monotone g) : monotone (Ξ» x, f x * g x) :=
Ξ» x y h, mul_le_mul' (hf h) (hg h)
@[to_additive monotone.add_const]
lemma monotone.mul_const' (hf : monotone f) (a : Ξ±) : monotone (Ξ» x, f x * a) :=
hf.mul' monotone_const
@[to_additive monotone.const_add]
lemma monotone.const_mul' (hf : monotone f) (a : Ξ±) : monotone (Ξ» x, a * f x) :=
monotone_const.mul' hf
end mono
end ordered_comm_monoid
lemma bit0_pos [ordered_add_comm_monoid Ξ±] {a : Ξ±} (h : 0 < a) : 0 < bit0 a :=
add_pos' h h
namespace units
@[to_additive]
instance [monoid Ξ±] [preorder Ξ±] : preorder (units Ξ±) :=
preorder.lift (coe : units Ξ± β Ξ±)
@[simp, to_additive]
theorem coe_le_coe [monoid Ξ±] [preorder Ξ±] {a b : units Ξ±} :
(a : Ξ±) β€ b β a β€ b := iff.rfl
@[simp, to_additive]
theorem coe_lt_coe [monoid Ξ±] [preorder Ξ±] {a b : units Ξ±} :
(a : Ξ±) < b β a < b := iff.rfl
@[to_additive]
instance [monoid Ξ±] [partial_order Ξ±] : partial_order (units Ξ±) :=
partial_order.lift coe units.ext
@[to_additive]
instance [monoid Ξ±] [linear_order Ξ±] : linear_order (units Ξ±) :=
linear_order.lift coe units.ext
@[to_additive]
instance [monoid Ξ±] [decidable_linear_order Ξ±] : decidable_linear_order (units Ξ±) :=
decidable_linear_order.lift coe units.ext
@[simp, to_additive]
theorem max_coe [monoid Ξ±] [decidable_linear_order Ξ±] {a b : units Ξ±} :
(β(max a b) : Ξ±) = max a b :=
by by_cases a β€ b; simp [max, h]
@[simp, to_additive]
theorem min_coe [monoid Ξ±] [decidable_linear_order Ξ±] {a b : units Ξ±} :
(β(min a b) : Ξ±) = min a b :=
by by_cases a β€ b; simp [min, h]
end units
namespace with_zero
instance [preorder Ξ±] : preorder (with_zero Ξ±) := with_bot.preorder
instance [partial_order Ξ±] : partial_order (with_zero Ξ±) := with_bot.partial_order
instance [partial_order Ξ±] : order_bot (with_zero Ξ±) := with_bot.order_bot
instance [lattice Ξ±] : lattice (with_zero Ξ±) := with_bot.lattice
instance [linear_order Ξ±] : linear_order (with_zero Ξ±) := with_bot.linear_order
instance [decidable_linear_order Ξ±] :
decidable_linear_order (with_zero Ξ±) := with_bot.decidable_linear_order
/--
If `0` is the least element in `Ξ±`, then `with_zero Ξ±` is an `ordered_add_comm_monoid`.
-/
def ordered_add_comm_monoid [ordered_add_comm_monoid Ξ±]
(zero_le : β a : Ξ±, 0 β€ a) : ordered_add_comm_monoid (with_zero Ξ±) :=
begin
suffices, refine {
add_le_add_left := this,
..with_zero.partial_order,
..with_zero.add_comm_monoid, .. },
{ intros a b c h,
have h' := lt_iff_le_not_le.1 h,
rw lt_iff_le_not_le at β’,
refine β¨Ξ» b hβ, _, Ξ» hβ, h'.2 $ this _ _ hβ _β©,
cases hβ, cases c with c,
{ cases h'.2 (this _ _ bot_le a) },
{ refine β¨_, rfl, _β©,
cases a with a,
{ exact with_bot.some_le_some.1 h'.1 },
{ exact le_of_lt (lt_of_add_lt_add_left' $
with_bot.some_lt_some.1 h), } } },
{ intros a b h c ca hβ,
cases b with b,
{ rw le_antisymm h bot_le at hβ,
exact β¨_, hβ, le_refl _β© },
cases a with a,
{ change c + 0 = some ca at hβ,
simp at hβ, simp [hβ],
exact β¨_, rfl, by simpa using add_le_add_left' (zero_le b)β© },
{ simp at h,
cases c with c; change some _ = _ at hβ;
simp [-add_comm] at hβ; subst ca; refine β¨_, rfl, _β©,
{ exact h },
{ exact add_le_add_left' h } } }
end
end with_zero
namespace with_top
instance [add_semigroup Ξ±] : add_semigroup (with_top Ξ±) :=
{ add := Ξ» oβ oβ, oβ.bind (Ξ» a, oβ.map (Ξ» b, a + b)),
..@additive.add_semigroup _ $ @with_zero.semigroup (multiplicative Ξ±) _ }
lemma coe_add [add_semigroup Ξ±] {a b : Ξ±} : ((a + b : Ξ±) : with_top Ξ±) = a + b := rfl
instance [add_comm_semigroup Ξ±] : add_comm_semigroup (with_top Ξ±) :=
{ ..@additive.add_comm_semigroup _ $
@with_zero.comm_semigroup (multiplicative Ξ±) _ }
instance [add_monoid Ξ±] : add_monoid (with_top Ξ±) :=
{ zero := some 0,
add := (+),
..@additive.add_monoid _ $ @with_zero.monoid (multiplicative Ξ±) _ }
instance [add_comm_monoid Ξ±] : add_comm_monoid (with_top Ξ±) :=
{ zero := 0,
add := (+),
..@additive.add_comm_monoid _ $
@with_zero.comm_monoid (multiplicative Ξ±) _ }
instance [ordered_add_comm_monoid Ξ±] : ordered_add_comm_monoid (with_top Ξ±) :=
begin
suffices, refine {
add_le_add_left := this,
..with_top.partial_order,
..with_top.add_comm_monoid, ..},
{ intros a b c h,
have h' := h,
rw lt_iff_le_not_le at h' β’,
refine β¨Ξ» c hβ, _, Ξ» hβ, h'.2 $ this _ _ hβ _β©,
cases hβ, cases a with a,
{ exact (not_le_of_lt h).elim le_top },
cases b with b,
{ exact (not_le_of_lt h).elim le_top },
{ exact β¨_, rfl, le_of_lt (lt_of_add_lt_add_left' $
with_top.some_lt_some.1 h)β© } },
{ intros a b h c ca hβ,
cases c with c, {cases hβ},
cases b with b; cases hβ,
cases a with a, {cases le_antisymm h le_top },
simp at h,
exact β¨_, rfl, add_le_add_left' hβ©, }
end
@[simp] lemma zero_lt_top [ordered_add_comm_monoid Ξ±] : (0 : with_top Ξ±) < β€ :=
coe_lt_top 0
@[simp] lemma zero_lt_coe [ordered_add_comm_monoid Ξ±] (a : Ξ±) : (0 : with_top Ξ±) < a β 0 < a :=
coe_lt_coe
@[simp] lemma add_top [ordered_add_comm_monoid Ξ±] : β{a : with_top Ξ±}, a + β€ = β€
| none := rfl
| (some a) := rfl
@[simp] lemma top_add [ordered_add_comm_monoid Ξ±] {a : with_top Ξ±} : β€ + a = β€ := rfl
lemma add_eq_top [ordered_add_comm_monoid Ξ±] (a b : with_top Ξ±) : a + b = β€ β a = β€ β¨ b = β€ :=
by cases a; cases b; simp [none_eq_top, some_eq_coe, coe_add.symm]
lemma add_lt_top [ordered_add_comm_monoid Ξ±] (a b : with_top Ξ±) : a + b < β€ β a < β€ β§ b < β€ :=
begin
apply not_iff_not.1,
simp [lt_top_iff_ne_top, add_eq_top],
finish,
apply classical.dec _,
apply classical.dec _,
end
end with_top
namespace with_bot
instance [add_semigroup Ξ±] : add_semigroup (with_bot Ξ±) := with_top.add_semigroup
instance [add_comm_semigroup Ξ±] : add_comm_semigroup (with_bot Ξ±) := with_top.add_comm_semigroup
instance [add_monoid Ξ±] : add_monoid (with_bot Ξ±) := with_top.add_monoid
instance [add_comm_monoid Ξ±] : add_comm_monoid (with_bot Ξ±) := with_top.add_comm_monoid
instance [ordered_add_comm_monoid Ξ±] : ordered_add_comm_monoid (with_bot Ξ±) :=
begin
suffices, refine {
add_le_add_left := this,
..with_bot.partial_order,
..with_bot.add_comm_monoid, ..},
{ intros a b c h,
have h' := h,
rw lt_iff_le_not_le at h' β’,
refine β¨Ξ» b hβ, _, Ξ» hβ, h'.2 $ this _ _ hβ _β©,
cases hβ, cases a with a,
{ exact (not_le_of_lt h).elim bot_le },
cases c with c,
{ exact (not_le_of_lt h).elim bot_le },
{ exact β¨_, rfl, le_of_lt (lt_of_add_lt_add_left' $
with_bot.some_lt_some.1 h)β© } },
{ intros a b h c ca hβ,
cases c with c, {cases hβ},
cases a with a; cases hβ,
cases b with b, {cases le_antisymm h bot_le},
simp at h,
exact β¨_, rfl, add_le_add_left' hβ©, }
end
@[simp] lemma coe_zero [add_monoid Ξ±] : ((0 : Ξ±) : with_bot Ξ±) = 0 := rfl
@[simp] lemma coe_add [add_semigroup Ξ±] (a b : Ξ±) : ((a + b : Ξ±) : with_bot Ξ±) = a + b := rfl
@[simp] lemma bot_add [ordered_add_comm_monoid Ξ±] (a : with_bot Ξ±) : β₯ + a = β₯ := rfl
@[simp] lemma add_bot [ordered_add_comm_monoid Ξ±] (a : with_bot Ξ±) : a + β₯ = β₯ := by cases a; refl
instance has_one [has_one Ξ±] : has_one (with_bot Ξ±) := β¨(1 : Ξ±)β©
@[simp] lemma coe_one [has_one Ξ±] : ((1 : Ξ±) : with_bot Ξ±) = 1 := rfl
end with_bot
/-- A canonically ordered monoid is an ordered commutative monoid
in which the ordering coincides with the divisibility relation,
which is to say, `a β€ b` iff there exists `c` with `b = a + c`.
This is satisfied by the natural numbers, for example, but not
the integers or other ordered groups. -/
@[protect_proj]
class canonically_ordered_add_monoid (Ξ± : Type*) extends ordered_add_comm_monoid Ξ±, order_bot Ξ± :=
(le_iff_exists_add : βa b:Ξ±, a β€ b β βc, b = a + c)
section canonically_ordered_add_monoid
variables [canonically_ordered_add_monoid Ξ±] {a b c d : Ξ±}
lemma le_iff_exists_add : a β€ b β βc, b = a + c :=
canonically_ordered_add_monoid.le_iff_exists_add a b
@[simp] lemma zero_le (a : Ξ±) : 0 β€ a := le_iff_exists_add.mpr β¨a, by simpβ©
@[simp] lemma bot_eq_zero : (β₯ : Ξ±) = 0 :=
le_antisymm bot_le (zero_le β₯)
@[simp] lemma add_eq_zero_iff : a + b = 0 β a = 0 β§ b = 0 :=
add_eq_zero_iff' (zero_le _) (zero_le _)
@[simp] lemma le_zero_iff_eq : a β€ 0 β a = 0 :=
iff.intro
(assume h, le_antisymm h (zero_le a))
(assume h, h βΈ le_refl a)
lemma zero_lt_iff_ne_zero : 0 < a β a β 0 :=
iff.intro ne_of_gt $ assume hne, lt_of_le_of_ne (zero_le _) hne.symm
@[nolint ge_or_gt]
lemma exists_pos_add_of_lt (h : a < b) : β c > 0, a + c = b :=
begin
obtain β¨c, hcβ© := le_iff_exists_add.1 (le_of_lt h),
refine β¨c, zero_lt_iff_ne_zero.2 _, hc.symmβ©,
rintro rfl,
simpa [hc, lt_irrefl] using h
end
lemma le_add_left (h : a β€ c) : a β€ b + c :=
calc a = 0 + a : by simp
... β€ b + c : add_le_add' (zero_le _) h
lemma le_add_right (h : a β€ b) : a β€ b + c :=
calc a = a + 0 : by simp
... β€ b + c : add_le_add' h (zero_le _)
instance with_zero.canonically_ordered_add_monoid :
canonically_ordered_add_monoid (with_zero Ξ±) :=
{ le_iff_exists_add := Ξ» a b, begin
cases a with a,
{ exact iff_of_true bot_le β¨b, (zero_add b).symmβ© },
cases b with b,
{ exact iff_of_false
(mt (le_antisymm bot_le) (by simp))
(Ξ» β¨c, hβ©, by cases c; cases h) },
{ simp [le_iff_exists_add, -add_comm],
split; intro h; rcases h with β¨c, hβ©,
{ exact β¨some c, congr_arg some hβ© },
{ cases c; cases h,
{ exact β¨_, (add_zero _).symmβ© },
{ exact β¨_, rflβ© } } }
end,
bot := 0,
bot_le := assume a a' h, option.no_confusion h,
.. with_zero.ordered_add_comm_monoid zero_le }
instance with_top.canonically_ordered_add_monoid : canonically_ordered_add_monoid (with_top Ξ±) :=
{ le_iff_exists_add := assume a b,
match a, b with
| a, none := show a β€ β€ β βc, β€ = a + c, by simp; refine β¨β€, _β©; cases a; refl
| (some a), (some b) := show (a:with_top Ξ±) β€ βb β βc:with_top Ξ±, βb = βa + c,
begin
simp [canonically_ordered_add_monoid.le_iff_exists_add, -add_comm],
split,
{ rintro β¨c, rflβ©, refine β¨c, _β©, simp [with_top.coe_add] },
{ exact assume h, match b, h with _, β¨some c, rflβ© := β¨_, rflβ© end }
end
| none, some b := show (β€ : with_top Ξ±) β€ b β βc:with_top Ξ±, βb = β€ + c, by simp
end,
.. with_top.order_bot,
.. with_top.ordered_add_comm_monoid }
end canonically_ordered_add_monoid
/-- An ordered cancellative additive commutative monoid
is an additive commutative monoid with a partial order,
in which addition is cancellative and strictly monotone. -/
@[protect_proj, ancestor add_comm_monoid add_left_cancel_semigroup add_right_cancel_semigroup partial_order]
class ordered_cancel_add_comm_monoid (Ξ± : Type u)
extends add_comm_monoid Ξ±, add_left_cancel_semigroup Ξ±,
add_right_cancel_semigroup Ξ±, partial_order Ξ± :=
(add_le_add_left : β a b : Ξ±, a β€ b β β c : Ξ±, c + a β€ c + b)
(le_of_add_le_add_left : β a b c : Ξ±, a + b β€ a + c β b β€ c)
/-- An ordered cancellative commutative monoid
is a commutative monoid with a partial order,
in which multiplication is cancellative and strictly monotone. -/
@[protect_proj, ancestor comm_monoid left_cancel_semigroup right_cancel_semigroup partial_order]
class ordered_cancel_comm_monoid (Ξ± : Type u)
extends comm_monoid Ξ±, left_cancel_semigroup Ξ±,
right_cancel_semigroup Ξ±, partial_order Ξ± :=
(mul_le_mul_left : β a b : Ξ±, a β€ b β β c : Ξ±, c * a β€ c * b)
(le_of_mul_le_mul_left : β a b c : Ξ±, a * b β€ a * c β b β€ c)
attribute [to_additive ordered_cancel_add_comm_monoid] ordered_cancel_comm_monoid
section ordered_cancel_comm_monoid
variables [ordered_cancel_comm_monoid Ξ±] {a b c d : Ξ±}
@[to_additive]
instance ordered_cancel_comm_monoid.to_left_cancel_monoid :
left_cancel_monoid Ξ± := { ..βΉordered_cancel_comm_monoid Ξ±βΊ }
@[to_additive add_le_add_left]
lemma mul_le_mul_left'' : β {a b : Ξ±} (h : a β€ b) (c : Ξ±), c * a β€ c * b :=
ordered_cancel_comm_monoid.mul_le_mul_left
@[to_additive le_of_add_le_add_left]
lemma le_of_mul_le_mul_left' : β {a b c : Ξ±}, a * b β€ a * c β b β€ c :=
ordered_cancel_comm_monoid.le_of_mul_le_mul_left
@[to_additive add_lt_add_left]
lemma mul_lt_mul_left' (h : a < b) (c : Ξ±) : c * a < c * b :=
lt_of_le_not_le (mul_le_mul_left'' (le_of_lt h) _) $
mt le_of_mul_le_mul_left' (not_le_of_gt h)
@[to_additive lt_of_add_lt_add_left]
lemma lt_of_mul_lt_mul_left'' (h : a * b < a * c) : b < c :=
lt_of_le_not_le (le_of_mul_le_mul_left' (le_of_lt h)) $
mt (Ξ» h, mul_le_mul_left'' h _) (not_le_of_gt h)
@[to_additive]
instance ordered_cancel_comm_monoid.to_ordered_comm_monoid : ordered_comm_monoid Ξ± :=
{ lt_of_mul_lt_mul_left := @lt_of_mul_lt_mul_left'' _ _, ..βΉordered_cancel_comm_monoid Ξ±βΊ }
@[to_additive add_le_add_right]
lemma mul_le_mul_right'' (h : a β€ b) (c : Ξ±) : a * c β€ b * c :=
mul_comm c a βΈ mul_comm c b βΈ mul_le_mul_left' h
@[to_additive add_lt_add_right]
lemma mul_lt_mul_right' (h : a < b) (c : Ξ±) : a * c < b * c :=
begin
rw [mul_comm a c, mul_comm b c],
exact (mul_lt_mul_left' h c)
end
@[to_additive add_le_add]
lemma mul_le_mul'' {a b c d : Ξ±} (hβ : a β€ b) (hβ : c β€ d) : a * c β€ b * d :=
le_trans (mul_le_mul_right' hβ) (mul_le_mul_left' hβ)
@[to_additive]
lemma le_mul_of_one_le_right (h : 1 β€ b) : a β€ a * b :=
have a * 1 β€ a * b, from mul_le_mul_left' h,
by rwa mul_one at this
@[to_additive]
lemma le_mul_of_one_le_left (h : 1 β€ b) : a β€ b * a :=
have 1 * a β€ b * a, from mul_le_mul_right' h,
by rwa one_mul at this
@[to_additive add_lt_add]
lemma mul_lt_mul''' (hβ : a < b) (hβ : c < d) : a * c < b * d :=
lt_trans (mul_lt_mul_right' hβ c) (mul_lt_mul_left' hβ b)
@[to_additive]
lemma mul_lt_mul_of_le_of_lt (hβ : a β€ b) (hβ : c < d) : a * c < b * d :=
lt_of_le_of_lt (mul_le_mul_right' hβ) (mul_lt_mul_left' hβ b)
@[to_additive]
lemma mul_lt_mul_of_lt_of_le (hβ : a < b) (hβ : c β€ d) : a * c < b * d :=
lt_of_lt_of_le (mul_lt_mul_right' hβ c) (mul_le_mul_left' hβ)
@[to_additive]
lemma lt_mul_of_one_lt_right (a : Ξ±) {b : Ξ±} (h : 1 < b) : a < a * b :=
have a * 1 < a * b, from mul_lt_mul_left' h a,
by rwa [mul_one] at this
@[to_additive]
lemma lt_mul_of_one_lt_left (a : Ξ±) {b : Ξ±} (h : 1 < b) : a < b * a :=
have 1 * a < b * a, from mul_lt_mul_right' h a,
by rwa [one_mul] at this
@[to_additive le_of_add_le_add_right]
lemma le_of_mul_le_mul_right' (h : a * b β€ c * b) : a β€ c :=
le_of_mul_le_mul_left'
(show b * a β€ b * c, begin rw [mul_comm b a, mul_comm b c], assumption end)
@[to_additive lt_of_add_lt_add_right]
lemma lt_of_mul_lt_mul_right'' (h : a * b < c * b) : a < c :=
lt_of_mul_lt_mul_left''
(show b * a < b * c, begin rw [mul_comm b a, mul_comm b c], assumption end)
-- here we start using properties of one.
@[to_additive add_nonneg]
lemma one_le_mul (ha : 1 β€ a) (hb : 1 β€ b) : 1 β€ a * b :=
one_mul (1:Ξ±) βΈ (mul_le_mul'' ha hb)
@[to_additive]
lemma mul_one_lt (ha : 1 < a) (hb : 1 < b) : 1 < a * b :=
one_mul (1:Ξ±) βΈ (mul_lt_mul''' ha hb)
@[to_additive]
lemma mul_one_lt_of_one_lt_of_one_le (ha : 1 < a) (hb : 1 β€ b) : 1 < a * b :=
one_mul (1:Ξ±) βΈ (mul_lt_mul_of_lt_of_le ha hb)
@[to_additive]
lemma mul_one_lt_of_one_le_of_one_lt (ha : 1 β€ a) (hb : 1 < b) : 1 < a * b :=
one_mul (1:Ξ±) βΈ (mul_lt_mul_of_le_of_lt ha hb)
@[to_additive add_nonpos]
lemma mul_le_one'' (ha : a β€ 1) (hb : b β€ 1) : a * b β€ 1 :=
one_mul (1:Ξ±) βΈ (mul_le_mul'' ha hb)
@[to_additive]
lemma mul_lt_one (ha : a < 1) (hb : b < 1) : a * b < 1 :=
one_mul (1:Ξ±) βΈ (mul_lt_mul''' ha hb)
@[to_additive]
lemma mul_lt_one_of_lt_one_of_le_one (ha : a < 1) (hb : b β€ 1) : a * b < 1 :=
one_mul (1:Ξ±) βΈ (mul_lt_mul_of_lt_of_le ha hb)
@[to_additive]
lemma mul_lt_one_of_le_one_of_lt_one (ha : a β€ 1) (hb : b < 1) : a * b < 1 :=
one_mul (1:Ξ±) βΈ (mul_lt_mul_of_le_of_lt ha hb)
@[to_additive]
lemma mul_eq_one_iff_eq_one_and_eq_one_of_one_le_of_one_le
(ha : 1 β€ a) (hb : 1 β€ b) : a * b = 1 β a = 1 β§ b = 1 :=
iff.intro
(assume hab : a * b = 1,
have ha' : a β€ 1, from
calc
a = a * 1 : by rw mul_one
... β€ a * b : mul_le_mul_left' hb
... = 1 : hab,
have haz : a = 1, from le_antisymm ha' ha,
have hb' : b β€ 1, from
calc
b = 1 * b : by rw one_mul
... β€ a * b : by exact mul_le_mul_right' ha
... = 1 : hab,
have hbz : b = 1, from le_antisymm hb' hb,
and.intro haz hbz)
(assume β¨ha', hb'β©,
by rw [ha', hb', mul_one])
@[to_additive]
lemma le_mul_of_one_le_of_le (ha : 1 β€ a) (hbc : b β€ c) : b β€ a * c :=
one_mul b βΈ mul_le_mul'' ha hbc
@[to_additive]
lemma le_mul_of_le_of_one_le (hbc : b β€ c) (ha : 1 β€ a) : b β€ c * a :=
mul_one b βΈ mul_le_mul'' hbc ha
@[to_additive]
lemma lt_mul_of_one_lt_of_le (ha : 1 < a) (hbc : b β€ c) : b < a * c :=
one_mul b βΈ mul_lt_mul_of_lt_of_le ha hbc
@[to_additive]
lemma lt_mul_of_le_of_one_lt (hbc : b β€ c) (ha : 1 < a) : b < c * a :=
mul_one b βΈ mul_lt_mul_of_le_of_lt hbc ha
@[to_additive]
lemma mul_le_of_le_one_of_le (ha : a β€ 1) (hbc : b β€ c) : a * b β€ c :=
one_mul c βΈ mul_le_mul'' ha hbc
@[to_additive]
lemma mul_le_of_le_of_le_one (hbc : b β€ c) (ha : a β€ 1) : b * a β€ c :=
mul_one c βΈ mul_le_mul'' hbc ha
@[to_additive]
lemma mul_lt_of_lt_one_of_le (ha : a < 1) (hbc : b β€ c) : a * b < c :=
one_mul c βΈ mul_lt_mul_of_lt_of_le ha hbc
@[to_additive]
lemma mul_lt_of_le_of_lt_one (hbc : b β€ c) (ha : a < 1) : b * a < c :=
mul_one c βΈ mul_lt_mul_of_le_of_lt hbc ha
@[to_additive]
lemma lt_mul_of_one_le_of_lt (ha : 1 β€ a) (hbc : b < c) : b < a * c :=
one_mul b βΈ mul_lt_mul_of_le_of_lt ha hbc
@[to_additive]
lemma lt_mul_of_lt_of_one_le (hbc : b < c) (ha : 1 β€ a) : b < c * a :=
mul_one b βΈ mul_lt_mul_of_lt_of_le hbc ha
@[to_additive]
lemma lt_mul_of_one_lt_of_lt (ha : 1 < a) (hbc : b < c) : b < a * c :=
one_mul b βΈ mul_lt_mul''' ha hbc
@[to_additive]
lemma lt_mul_of_lt_of_one_lt (hbc : b < c) (ha : 1 < a) : b < c * a :=
mul_one b βΈ mul_lt_mul''' hbc ha
@[to_additive]
lemma mul_lt_of_le_one_of_lt (ha : a β€ 1) (hbc : b < c) : a * b < c :=
one_mul c βΈ mul_lt_mul_of_le_of_lt ha hbc
@[to_additive]
lemma mul_lt_of_lt_of_le_one (hbc : b < c) (ha : a β€ 1) : b * a < c :=
mul_one c βΈ mul_lt_mul_of_lt_of_le hbc ha
@[to_additive]
lemma mul_lt_of_lt_one_of_lt (ha : a < 1) (hbc : b < c) : a * b < c :=
one_mul c βΈ mul_lt_mul''' ha hbc
@[to_additive]
lemma mul_lt_of_lt_of_lt_one (hbc : b < c) (ha : a < 1) : b * a < c :=
mul_one c βΈ mul_lt_mul''' hbc ha
@[simp, to_additive]
lemma mul_le_mul_iff_left (a : Ξ±) {b c : Ξ±} : a * b β€ a * c β b β€ c :=
β¨le_of_mul_le_mul_left', Ξ» h, mul_le_mul_left' hβ©
@[simp, to_additive]
lemma mul_le_mul_iff_right (c : Ξ±) : a * c β€ b * c β a β€ b :=
mul_comm c a βΈ mul_comm c b βΈ mul_le_mul_iff_left c
@[simp, to_additive]
lemma mul_lt_mul_iff_left (a : Ξ±) {b c : Ξ±} : a * b < a * c β b < c :=
β¨lt_of_mul_lt_mul_left'', Ξ» h, mul_lt_mul_left' h _β©
@[simp, to_additive]
lemma mul_lt_mul_iff_right (c : Ξ±) : a * c < b * c β a < b :=
mul_comm c a βΈ mul_comm c b βΈ mul_lt_mul_iff_left c
@[simp, to_additive le_add_iff_nonneg_right]
lemma le_mul_iff_one_le_right' (a : Ξ±) {b : Ξ±} : a β€ a * b β 1 β€ b :=
have a * 1 β€ a * b β 1 β€ b, from mul_le_mul_iff_left a,
by rwa mul_one at this
@[simp, to_additive le_add_iff_nonneg_left]
lemma le_mul_iff_one_le_left' (a : Ξ±) {b : Ξ±} : a β€ b * a β 1 β€ b :=
by rw [mul_comm, le_mul_iff_one_le_right']
@[simp, to_additive lt_add_iff_pos_right]
lemma lt_mul_iff_one_lt_right' (a : Ξ±) {b : Ξ±} : a < a * b β 1 < b :=
have a * 1 < a * b β 1 < b, from mul_lt_mul_iff_left a,
by rwa mul_one at this
@[simp, to_additive lt_add_iff_pos_left]
lemma lt_mul_iff_one_lt_left' (a : Ξ±) {b : Ξ±} : a < b * a β 1 < b :=
by rw [mul_comm, lt_mul_iff_one_lt_right']
@[simp, to_additive add_le_iff_nonpos_left]
lemma mul_le_iff_le_one_left' : a * b β€ b β a β€ 1 :=
by { convert mul_le_mul_iff_right b, rw [one_mul] }
@[simp, to_additive add_le_iff_nonpos_right]
lemma mul_le_iff_le_one_right' : a * b β€ a β b β€ 1 :=
by { convert mul_le_mul_iff_left a, rw [mul_one] }
@[simp, to_additive add_lt_iff_neg_right]
lemma mul_lt_iff_lt_one_right' : a * b < b β a < 1 :=
by { convert mul_lt_mul_iff_right b, rw [one_mul] }
@[simp, to_additive add_lt_iff_neg_left]
lemma mul_lt_iff_lt_one_left' : a * b < a β b < 1 :=
by { convert mul_lt_mul_iff_left a, rw [mul_one] }
@[to_additive]
lemma mul_eq_one_iff_eq_one_of_one_le
(ha : 1 β€ a) (hb : 1 β€ b) : a * b = 1 β a = 1 β§ b = 1 :=
β¨Ξ» hab : a * b = 1,
by split; apply le_antisymm; try {assumption};
rw β hab; simp [ha, hb],
Ξ» β¨ha', hb'β©, by rw [ha', hb', mul_one]β©
section mono
variables {Ξ² : Type*} [preorder Ξ²] {f g : Ξ² β Ξ±}
@[to_additive monotone.add_strict_mono]
lemma monotone.mul_strict_mono' (hf : monotone f) (hg : strict_mono g) :
strict_mono (Ξ» x, f x * g x) :=
Ξ» x y h, mul_lt_mul_of_le_of_lt (hf $ le_of_lt h) (hg h)
@[to_additive strict_mono.add_monotone]
lemma strict_mono.mul_monotone' (hf : strict_mono f) (hg : monotone g) :
strict_mono (Ξ» x, f x * g x) :=
Ξ» x y h, mul_lt_mul_of_lt_of_le (hf h) (hg $ le_of_lt h)
@[to_additive strict_mono.add_const]
lemma strict_mono.mul_const' (hf : strict_mono f) (c : Ξ±) :
strict_mono (Ξ» x, f x * c) :=
hf.mul_monotone' monotone_const
@[to_additive strict_mono.const_add]
lemma strict_mono.const_mul' (hf : strict_mono f) (c : Ξ±) :
strict_mono (Ξ» x, c * f x) :=
monotone_const.mul_strict_mono' hf
end mono
end ordered_cancel_comm_monoid
section ordered_cancel_add_comm_monoid
variable [ordered_cancel_add_comm_monoid Ξ±]
lemma with_top.add_lt_add_iff_left :
β{a b c : with_top Ξ±}, a < β€ β (a + c < a + b β c < b)
| none := assume b c h, (lt_irrefl β€ h).elim
| (some a) :=
begin
assume b c h,
cases b; cases c;
simp [with_top.none_eq_top, with_top.some_eq_coe, with_top.coe_lt_top, with_top.coe_lt_coe],
{ rw [β with_top.coe_add], exact with_top.coe_lt_top _ },
{ rw [β with_top.coe_add, β with_top.coe_add, with_top.coe_lt_coe],
exact add_lt_add_iff_left _ }
end
lemma with_top.add_lt_add_iff_right
{a b c : with_top Ξ±} : a < β€ β (c + a < b + a β c < b) :=
by simpa [add_comm] using @with_top.add_lt_add_iff_left _ _ a b c
end ordered_cancel_add_comm_monoid
/-- An ordered additive commutative group is an additive commutative group
with a partial order in which addition is strictly monotone. -/
@[protect_proj, ancestor add_comm_group partial_order]
class ordered_add_comm_group (Ξ± : Type u) extends add_comm_group Ξ±, partial_order Ξ± :=
(add_le_add_left : β a b : Ξ±, a β€ b β β c : Ξ±, c + a β€ c + b)
/-- An ordered commutative group is an commutative group
with a partial order in which multiplication is strictly monotone. -/
@[protect_proj, ancestor comm_group partial_order]
class ordered_comm_group (Ξ± : Type u) extends comm_group Ξ±, partial_order Ξ± :=
(mul_le_mul_left : β a b : Ξ±, a β€ b β β c : Ξ±, c * a β€ c * b)
attribute [to_additive ordered_add_comm_group] ordered_comm_group
/--The units of an ordered commutative monoid form an ordered commutative group. -/
@[to_additive]
instance units.ordered_comm_group [ordered_comm_monoid Ξ±] : ordered_comm_group (units Ξ±) :=
{ mul_le_mul_left := Ξ» a b h c, mul_le_mul_left' h,
.. units.partial_order,
.. (infer_instance : comm_group (units Ξ±)) }
section ordered_comm_group
variables [ordered_comm_group Ξ±] {a b c d : Ξ±}
@[to_additive ordered_add_comm_group.add_lt_add_left]
lemma ordered_comm_group.mul_lt_mul_left' (a b : Ξ±) (h : a < b) (c : Ξ±) : c * a < c * b :=
begin
rw lt_iff_le_not_le at h β’,
split,
{ apply ordered_comm_group.mul_le_mul_left _ _ h.1 },
{ intro w,
replace w : cβ»ΒΉ * (c * b) β€ cβ»ΒΉ * (c * a) := ordered_comm_group.mul_le_mul_left _ _ w _,
simp only [mul_one, mul_comm, mul_left_inv, mul_left_comm] at w,
exact h.2 w },
end
@[to_additive ordered_add_comm_group.le_of_add_le_add_left]
lemma ordered_comm_group.le_of_mul_le_mul_left (h : a * b β€ a * c) : b β€ c :=
have aβ»ΒΉ * (a * b) β€ aβ»ΒΉ * (a * c), from ordered_comm_group.mul_le_mul_left _ _ h _,
begin simp [inv_mul_cancel_left] at this, assumption end
@[to_additive]
lemma ordered_comm_group.lt_of_mul_lt_mul_left (h : a * b < a * c) : b < c :=
have aβ»ΒΉ * (a * b) < aβ»ΒΉ * (a * c), from ordered_comm_group.mul_lt_mul_left' _ _ h _,
begin simp [inv_mul_cancel_left] at this, assumption end
@[to_additive]
instance ordered_comm_group.to_ordered_cancel_comm_monoid (Ξ± : Type u)
[s : ordered_comm_group Ξ±] : ordered_cancel_comm_monoid Ξ± :=
{ mul_left_cancel := @mul_left_cancel Ξ± _,
mul_right_cancel := @mul_right_cancel Ξ± _,
le_of_mul_le_mul_left := @ordered_comm_group.le_of_mul_le_mul_left Ξ± _,
..s }
@[to_additive neg_le_neg]
lemma inv_le_inv' (h : a β€ b) : bβ»ΒΉ β€ aβ»ΒΉ :=
have 1 β€ aβ»ΒΉ * b, from mul_left_inv a βΈ mul_le_mul_left' h,
have 1 * bβ»ΒΉ β€ aβ»ΒΉ * b * bβ»ΒΉ, from mul_le_mul_right' this,
by rwa [mul_inv_cancel_right, one_mul] at this
@[to_additive]
lemma le_of_inv_le_inv (h : bβ»ΒΉ β€ aβ»ΒΉ) : a β€ b :=
suffices (aβ»ΒΉ)β»ΒΉ β€ (bβ»ΒΉ)β»ΒΉ, from
begin simp [inv_inv] at this, assumption end,
inv_le_inv' h
@[to_additive]
lemma one_le_of_inv_le_one (h : aβ»ΒΉ β€ 1) : 1 β€ a :=
have aβ»ΒΉ β€ 1β»ΒΉ, by rwa one_inv,
le_of_inv_le_inv this
@[to_additive]
lemma inv_le_one_of_one_le (h : 1 β€ a) : aβ»ΒΉ β€ 1 :=
have aβ»ΒΉ β€ 1β»ΒΉ, from inv_le_inv' h,
by rwa one_inv at this
@[to_additive nonpos_of_neg_nonneg]
lemma le_one_of_one_le_inv (h : 1 β€ aβ»ΒΉ) : a β€ 1 :=
have 1β»ΒΉ β€ aβ»ΒΉ, by rwa one_inv,
le_of_inv_le_inv this
@[to_additive neg_nonneg_of_nonpos]
lemma one_le_inv_of_le_one (h : a β€ 1) : 1 β€ aβ»ΒΉ :=
have 1β»ΒΉ β€ aβ»ΒΉ, from inv_le_inv' h,
by rwa one_inv at this
@[to_additive neg_lt_neg]
lemma inv_lt_inv' (h : a < b) : bβ»ΒΉ < aβ»ΒΉ :=
have 1 < aβ»ΒΉ * b, from mul_left_inv a βΈ mul_lt_mul_left' h (aβ»ΒΉ),
have 1 * bβ»ΒΉ < aβ»ΒΉ * b * bβ»ΒΉ, from mul_lt_mul_right' this (bβ»ΒΉ),
by rwa [mul_inv_cancel_right, one_mul] at this
@[to_additive]
lemma lt_of_inv_lt_inv (h : bβ»ΒΉ < aβ»ΒΉ) : a < b :=
inv_inv a βΈ inv_inv b βΈ inv_lt_inv' h
@[to_additive]
lemma one_lt_of_inv_inv (h : aβ»ΒΉ < 1) : 1 < a :=
have aβ»ΒΉ < 1β»ΒΉ, by rwa one_inv,
lt_of_inv_lt_inv this
@[to_additive]
lemma inv_inv_of_one_lt (h : 1 < a) : aβ»ΒΉ < 1 :=
have aβ»ΒΉ < 1β»ΒΉ, from inv_lt_inv' h,
by rwa one_inv at this
@[to_additive neg_of_neg_pos]
lemma inv_of_one_lt_inv (h : 1 < aβ»ΒΉ) : a < 1 :=
have 1β»ΒΉ < aβ»ΒΉ, by rwa one_inv,
lt_of_inv_lt_inv this
@[to_additive neg_pos_of_neg]
lemma one_lt_inv_of_inv (h : a < 1) : 1 < aβ»ΒΉ :=
have 1β»ΒΉ < aβ»ΒΉ, from inv_lt_inv' h,
by rwa one_inv at this
@[to_additive]
lemma le_inv_of_le_inv (h : a β€ bβ»ΒΉ) : b β€ aβ»ΒΉ :=
begin
have h := inv_le_inv' h,
rwa inv_inv at h
end
@[to_additive]
lemma inv_le_of_inv_le (h : aβ»ΒΉ β€ b) : bβ»ΒΉ β€ a :=
begin
have h := inv_le_inv' h,
rwa inv_inv at h
end
@[to_additive]
lemma lt_inv_of_lt_inv (h : a < bβ»ΒΉ) : b < aβ»ΒΉ :=
begin
have h := inv_lt_inv' h,
rwa inv_inv at h
end
@[to_additive]
lemma inv_lt_of_inv_lt (h : aβ»ΒΉ < b) : bβ»ΒΉ < a :=
begin
have h := inv_lt_inv' h,
rwa inv_inv at h
end
@[to_additive]
lemma mul_le_of_le_inv_mul (h : b β€ aβ»ΒΉ * c) : a * b β€ c :=
begin
have h := mul_le_mul_left' h,
rwa mul_inv_cancel_left at h
end
@[to_additive]
lemma le_inv_mul_of_mul_le (h : a * b β€ c) : b β€ aβ»ΒΉ * c :=
begin
have h := mul_le_mul_left' h,
rwa inv_mul_cancel_left at h
end
@[to_additive]
lemma le_mul_of_inv_mul_le (h : bβ»ΒΉ * a β€ c) : a β€ b * c :=
begin
have h := mul_le_mul_left' h,
rwa mul_inv_cancel_left at h
end
@[to_additive]
lemma inv_mul_le_of_le_mul (h : a β€ b * c) : bβ»ΒΉ * a β€ c :=
begin
have h := mul_le_mul_left' h,
rwa inv_mul_cancel_left at h
end
@[to_additive]
lemma le_mul_of_inv_mul_le_left (h : bβ»ΒΉ * a β€ c) : a β€ b * c :=
le_mul_of_inv_mul_le h
@[to_additive]
lemma inv_mul_le_left_of_le_mul (h : a β€ b * c) : bβ»ΒΉ * a β€ c :=
inv_mul_le_of_le_mul h
@[to_additive]
lemma le_mul_of_inv_mul_le_right (h : cβ»ΒΉ * a β€ b) : a β€ b * c :=
by { rw mul_comm, exact le_mul_of_inv_mul_le h }
@[to_additive]
lemma inv_mul_le_right_of_le_mul (h : a β€ b * c) : cβ»ΒΉ * a β€ b :=
by { rw mul_comm at h, apply inv_mul_le_left_of_le_mul h }
@[to_additive]
lemma mul_lt_of_lt_inv_mul (h : b < aβ»ΒΉ * c) : a * b < c :=
begin
have h := mul_lt_mul_left' h a,
rwa mul_inv_cancel_left at h
end
@[to_additive]
lemma lt_inv_mul_of_mul_lt (h : a * b < c) : b < aβ»ΒΉ * c :=
begin
have h := mul_lt_mul_left' h (aβ»ΒΉ),
rwa inv_mul_cancel_left at h
end
@[to_additive]
lemma lt_mul_of_inv_mul_lt (h : bβ»ΒΉ * a < c) : a < b * c :=
begin
have h := mul_lt_mul_left' h b,
rwa mul_inv_cancel_left at h
end
@[to_additive]
lemma inv_mul_lt_of_lt_mul (h : a < b * c) : bβ»ΒΉ * a < c :=
begin
have h := mul_lt_mul_left' h (bβ»ΒΉ),
rwa inv_mul_cancel_left at h
end
@[to_additive]
lemma lt_mul_of_inv_mul_lt_left (h : bβ»ΒΉ * a < c) : a < b * c :=
lt_mul_of_inv_mul_lt h
@[to_additive]
lemma inv_mul_lt_left_of_lt_mul (h : a < b * c) : bβ»ΒΉ * a < c :=
inv_mul_lt_of_lt_mul h
@[to_additive]
lemma lt_mul_of_inv_mul_lt_right (h : cβ»ΒΉ * a < b) : a < b * c :=
by { rw mul_comm, exact lt_mul_of_inv_mul_lt h }
@[to_additive]
lemma inv_mul_lt_right_of_lt_mul (h : a < b * c) : cβ»ΒΉ * a < b :=
by { rw mul_comm at h, exact inv_mul_lt_of_lt_mul h }
@[to_additive]
lemma mul_le_mul_three {a b c d e f : Ξ±} (hβ : a β€ d) (hβ : b β€ e) (hβ : c β€ f) :
a * b * c β€ d * e * f :=
begin
apply le_trans,
apply mul_le_mul'',
apply mul_le_mul'',
assumption',
apply le_refl
end
@[simp, to_additive]
lemma inv_lt_one_iff_one_lt : aβ»ΒΉ < 1 β 1 < a :=
β¨ one_lt_of_inv_inv, inv_inv_of_one_lt β©
@[simp, to_additive]
lemma inv_le_inv_iff : aβ»ΒΉ β€ bβ»ΒΉ β b β€ a :=
have a * b * aβ»ΒΉ β€ a * b * bβ»ΒΉ β aβ»ΒΉ β€ bβ»ΒΉ, from mul_le_mul_iff_left _,
by { rw [mul_inv_cancel_right, mul_comm a, mul_inv_cancel_right] at this, rw [this] }
@[to_additive neg_le]
lemma inv_le' : aβ»ΒΉ β€ b β bβ»ΒΉ β€ a :=
have aβ»ΒΉ β€ (bβ»ΒΉ)β»ΒΉ β bβ»ΒΉ β€ a, from inv_le_inv_iff,
by rwa inv_inv at this
@[to_additive le_neg]
lemma le_inv' : a β€ bβ»ΒΉ β b β€ aβ»ΒΉ :=
have (aβ»ΒΉ)β»ΒΉ β€ bβ»ΒΉ β b β€ aβ»ΒΉ, from inv_le_inv_iff,
by rwa inv_inv at this
@[to_additive neg_le_iff_add_nonneg]
lemma inv_le_iff_one_le_mul : aβ»ΒΉ β€ b β 1 β€ a * b :=
(mul_le_mul_iff_left a).symm.trans $ by rw mul_inv_self
@[to_additive]
lemma le_inv_iff_mul_le_one : a β€ bβ»ΒΉ β a * b β€ 1 :=
(mul_le_mul_iff_right b).symm.trans $ by rw inv_mul_self
@[simp, to_additive neg_nonpos]
lemma inv_le_one' : aβ»ΒΉ β€ 1 β 1 β€ a :=
have aβ»ΒΉ β€ 1β»ΒΉ β 1 β€ a, from inv_le_inv_iff,
by rwa one_inv at this
@[simp, to_additive neg_nonneg]
lemma one_le_inv' : 1 β€ aβ»ΒΉ β a β€ 1 :=
have 1β»ΒΉ β€ aβ»ΒΉ β a β€ 1, from inv_le_inv_iff,
by rwa one_inv at this
@[to_additive]
lemma inv_le_self (h : 1 β€ a) : aβ»ΒΉ β€ a :=
le_trans (inv_le_one'.2 h) h
@[to_additive]
lemma self_le_inv (h : a β€ 1) : a β€ aβ»ΒΉ :=
le_trans h (one_le_inv'.2 h)
@[simp, to_additive]
lemma inv_lt_inv_iff : aβ»ΒΉ < bβ»ΒΉ β b < a :=
have a * b * aβ»ΒΉ < a * b * bβ»ΒΉ β aβ»ΒΉ < bβ»ΒΉ, from mul_lt_mul_iff_left _,
by { rw [mul_inv_cancel_right, mul_comm a, mul_inv_cancel_right] at this, rw [this] }
@[to_additive neg_lt_zero]
lemma inv_lt_one' : aβ»ΒΉ < 1 β 1 < a :=
have aβ»ΒΉ < 1β»ΒΉ β 1 < a, from inv_lt_inv_iff,
by rwa one_inv at this
@[to_additive neg_pos]
lemma one_lt_inv' : 1 < aβ»ΒΉ β a < 1 :=
have 1β»ΒΉ < aβ»ΒΉ β a < 1, from inv_lt_inv_iff,
by rwa one_inv at this
@[to_additive neg_lt]
lemma inv_lt' : aβ»ΒΉ < b β bβ»ΒΉ < a :=
have aβ»ΒΉ < (bβ»ΒΉ)β»ΒΉ β bβ»ΒΉ < a, from inv_lt_inv_iff,
by rwa inv_inv at this
@[to_additive lt_neg]
lemma lt_inv' : a < bβ»ΒΉ β b < aβ»ΒΉ :=
have (aβ»ΒΉ)β»ΒΉ < bβ»ΒΉ β b < aβ»ΒΉ, from inv_lt_inv_iff,
by rwa inv_inv at this
@[to_additive]
lemma le_inv_mul_iff_mul_le : b β€ aβ»ΒΉ * c β a * b β€ c :=
have aβ»ΒΉ * (a * b) β€ aβ»ΒΉ * c β a * b β€ c, from mul_le_mul_iff_left _,
by rwa inv_mul_cancel_left at this
@[simp, to_additive]
lemma inv_mul_le_iff_le_mul : bβ»ΒΉ * a β€ c β a β€ b * c :=
have bβ»ΒΉ * a β€ bβ»ΒΉ * (b * c) β a β€ b * c, from mul_le_mul_iff_left _,
by rwa inv_mul_cancel_left at this
@[to_additive]
lemma mul_inv_le_iff_le_mul : a * cβ»ΒΉ β€ b β a β€ b * c :=
by rw [mul_comm a, mul_comm b, inv_mul_le_iff_le_mul]
@[simp, to_additive]
lemma mul_inv_le_iff_le_mul' : a * bβ»ΒΉ β€ c β a β€ b * c :=
by rw [β inv_mul_le_iff_le_mul, mul_comm]
@[to_additive]
lemma inv_mul_le_iff_le_mul' : cβ»ΒΉ * a β€ b β a β€ b * c :=
by rw [inv_mul_le_iff_le_mul, mul_comm]
@[simp, to_additive]
lemma lt_inv_mul_iff_mul_lt : b < aβ»ΒΉ * c β a * b < c :=
have aβ»ΒΉ * (a * b) < aβ»ΒΉ * c β a * b < c, from mul_lt_mul_iff_left _,
by rwa inv_mul_cancel_left at this
@[simp, to_additive]
lemma inv_mul_lt_iff_lt_mul : bβ»ΒΉ * a < c β a < b * c :=
have bβ»ΒΉ * a < bβ»ΒΉ * (b * c) β a < b * c, from mul_lt_mul_iff_left _,
by rwa inv_mul_cancel_left at this
@[to_additive]
lemma inv_mul_lt_iff_lt_mul_right : cβ»ΒΉ * a < b β a < b * c :=
by rw [inv_mul_lt_iff_lt_mul, mul_comm]
@[to_additive sub_le_sub_iff]
lemma div_le_div_iff' (a b c d : Ξ±) : a * bβ»ΒΉ β€ c * dβ»ΒΉ β a * d β€ c * b :=
begin
split ; intro h,
have := mul_le_mul_right'' (mul_le_mul_right'' h b) d,
rwa [inv_mul_cancel_right, mul_assoc _ _ b, mul_comm _ b, β mul_assoc, inv_mul_cancel_right] at this,
have := mul_le_mul_right'' (mul_le_mul_right'' h dβ»ΒΉ) bβ»ΒΉ,
rwa [mul_inv_cancel_right, _root_.mul_assoc, _root_.mul_comm dβ»ΒΉ bβ»ΒΉ, β mul_assoc, mul_inv_cancel_right] at this,
end
end ordered_comm_group
section ordered_add_comm_group
variables [ordered_add_comm_group Ξ±] {a b c d : Ξ±}
lemma sub_nonneg_of_le (h : b β€ a) : 0 β€ a - b :=
begin
have h := add_le_add_right h (-b),
rwa add_right_neg at h
end
lemma le_of_sub_nonneg (h : 0 β€ a - b) : b β€ a :=
begin
have h := add_le_add_right h b,
rwa [sub_add_cancel, zero_add] at h
end
lemma sub_nonpos_of_le (h : a β€ b) : a - b β€ 0 :=
begin
have h := add_le_add_right h (-b),
rwa add_right_neg at h
end
lemma le_of_sub_nonpos (h : a - b β€ 0) : a β€ b :=
begin
have h := add_le_add_right h b,
rwa [sub_add_cancel, zero_add] at h
end
lemma sub_pos_of_lt (h : b < a) : 0 < a - b :=
begin
have h := add_lt_add_right h (-b),
rwa add_right_neg at h
end
lemma lt_of_sub_pos (h : 0 < a - b) : b < a :=
begin
have h := add_lt_add_right h b,
rwa [sub_add_cancel, zero_add] at h
end
lemma sub_neg_of_lt (h : a < b) : a - b < 0 :=
begin
have h := add_lt_add_right h (-b),
rwa add_right_neg at h
end
lemma lt_of_sub_neg (h : a - b < 0) : a < b :=
begin
have h := add_lt_add_right h b,
rwa [sub_add_cancel, zero_add] at h
end
lemma add_le_of_le_sub_left (h : b β€ c - a) : a + b β€ c :=
begin
have h := add_le_add_left h a,
rwa [β add_sub_assoc, add_comm a c, add_sub_cancel] at h
end
lemma le_sub_left_of_add_le (h : a + b β€ c) : b β€ c - a :=
begin
have h := add_le_add_right h (-a),
rwa [add_comm a b, add_neg_cancel_right] at h
end
lemma add_le_of_le_sub_right (h : a β€ c - b) : a + b β€ c :=
begin
have h := add_le_add_right h b,
rwa sub_add_cancel at h
end
lemma le_sub_right_of_add_le (h : a + b β€ c) : a β€ c - b :=
begin
have h := add_le_add_right h (-b),
rwa add_neg_cancel_right at h
end
lemma le_add_of_sub_left_le (h : a - b β€ c) : a β€ b + c :=
begin
have h := add_le_add_right h b,
rwa [sub_add_cancel, add_comm] at h
end
lemma sub_left_le_of_le_add (h : a β€ b + c) : a - b β€ c :=
begin
have h := add_le_add_right h (-b),
rwa [add_comm b c, add_neg_cancel_right] at h
end
lemma le_add_of_sub_right_le (h : a - c β€ b) : a β€ b + c :=
begin
have h := add_le_add_right h c,
rwa sub_add_cancel at h
end
lemma sub_right_le_of_le_add (h : a β€ b + c) : a - c β€ b :=
begin
have h := add_le_add_right h (-c),
rwa add_neg_cancel_right at h
end
lemma le_add_of_neg_le_sub_left (h : -a β€ b - c) : c β€ a + b :=
le_add_of_neg_add_le_left (add_le_of_le_sub_right h)
lemma neg_le_sub_left_of_le_add (h : c β€ a + b) : -a β€ b - c :=
begin
have h := le_neg_add_of_add_le (sub_left_le_of_le_add h),
rwa add_comm at h
end
lemma le_add_of_neg_le_sub_right (h : -b β€ a - c) : c β€ a + b :=
le_add_of_sub_right_le (add_le_of_le_sub_left h)
lemma neg_le_sub_right_of_le_add (h : c β€ a + b) : -b β€ a - c :=
le_sub_left_of_add_le (sub_right_le_of_le_add h)
lemma sub_le_of_sub_le (h : a - b β€ c) : a - c β€ b :=
sub_left_le_of_le_add (le_add_of_sub_right_le h)
lemma sub_le_sub_left (h : a β€ b) (c : Ξ±) : c - b β€ c - a :=
add_le_add_left (neg_le_neg h) c
lemma sub_le_sub_right (h : a β€ b) (c : Ξ±) : a - c β€ b - c :=
add_le_add_right h (-c)
lemma sub_le_sub (hab : a β€ b) (hcd : c β€ d) : a - d β€ b - c :=
add_le_add hab (neg_le_neg hcd)
lemma add_lt_of_lt_sub_left (h : b < c - a) : a + b < c :=
begin
have h := add_lt_add_left h a,
rwa [β add_sub_assoc, add_comm a c, add_sub_cancel] at h
end
lemma lt_sub_left_of_add_lt (h : a + b < c) : b < c - a :=
begin
have h := add_lt_add_right h (-a),
rwa [add_comm a b, add_neg_cancel_right] at h
end
lemma add_lt_of_lt_sub_right (h : a < c - b) : a + b < c :=
begin
have h := add_lt_add_right h b,
rwa sub_add_cancel at h
end
lemma lt_sub_right_of_add_lt (h : a + b < c) : a < c - b :=
begin
have h := add_lt_add_right h (-b),
rwa add_neg_cancel_right at h
end
lemma lt_add_of_sub_left_lt (h : a - b < c) : a < b + c :=
begin
have h := add_lt_add_right h b,
rwa [sub_add_cancel, add_comm] at h
end
lemma sub_left_lt_of_lt_add (h : a < b + c) : a - b < c :=
begin
have h := add_lt_add_right h (-b),
rwa [add_comm b c, add_neg_cancel_right] at h
end
lemma lt_add_of_sub_right_lt (h : a - c < b) : a < b + c :=
begin
have h := add_lt_add_right h c,
rwa sub_add_cancel at h
end
lemma sub_right_lt_of_lt_add (h : a < b + c) : a - c < b :=
begin
have h := add_lt_add_right h (-c),
rwa add_neg_cancel_right at h
end
lemma lt_add_of_neg_lt_sub_left (h : -a < b - c) : c < a + b :=
lt_add_of_neg_add_lt_left (add_lt_of_lt_sub_right h)
lemma neg_lt_sub_left_of_lt_add (h : c < a + b) : -a < b - c :=
begin
have h := lt_neg_add_of_add_lt (sub_left_lt_of_lt_add h),
rwa add_comm at h
end
lemma lt_add_of_neg_lt_sub_right (h : -b < a - c) : c < a + b :=
lt_add_of_sub_right_lt (add_lt_of_lt_sub_left h)
lemma neg_lt_sub_right_of_lt_add (h : c < a + b) : -b < a - c :=
lt_sub_left_of_add_lt (sub_right_lt_of_lt_add h)
lemma sub_lt_of_sub_lt (h : a - b < c) : a - c < b :=
sub_left_lt_of_lt_add (lt_add_of_sub_right_lt h)
lemma sub_lt_sub_left (h : a < b) (c : Ξ±) : c - b < c - a :=
add_lt_add_left (neg_lt_neg h) c
lemma sub_lt_sub_right (h : a < b) (c : Ξ±) : a - c < b - c :=
add_lt_add_right h (-c)
lemma sub_lt_sub (hab : a < b) (hcd : c < d) : a - d < b - c :=
add_lt_add hab (neg_lt_neg hcd)
lemma sub_lt_sub_of_le_of_lt (hab : a β€ b) (hcd : c < d) : a - d < b - c :=
add_lt_add_of_le_of_lt hab (neg_lt_neg hcd)
lemma sub_lt_sub_of_lt_of_le (hab : a < b) (hcd : c β€ d) : a - d < b - c :=
add_lt_add_of_lt_of_le hab (neg_le_neg hcd)
lemma sub_le_self (a : Ξ±) {b : Ξ±} (h : 0 β€ b) : a - b β€ a :=
calc
a - b = a + -b : rfl
... β€ a + 0 : add_le_add_left (neg_nonpos_of_nonneg h) _
... = a : by rw add_zero
lemma sub_lt_self (a : Ξ±) {b : Ξ±} (h : 0 < b) : a - b < a :=
calc
a - b = a + -b : rfl
... < a + 0 : add_lt_add_left (neg_neg_of_pos h) _
... = a : by rw add_zero
@[simp]
lemma sub_le_sub_iff_left (a : Ξ±) {b c : Ξ±} : a - b β€ a - c β c β€ b :=
(add_le_add_iff_left _).trans neg_le_neg_iff
@[simp]
lemma sub_le_sub_iff_right (c : Ξ±) : a - c β€ b - c β a β€ b :=
add_le_add_iff_right _
@[simp]
lemma sub_lt_sub_iff_left (a : Ξ±) {b c : Ξ±} : a - b < a - c β c < b :=
(add_lt_add_iff_left _).trans neg_lt_neg_iff
@[simp]
lemma sub_lt_sub_iff_right (c : Ξ±) : a - c < b - c β a < b :=
add_lt_add_iff_right _
@[simp] lemma sub_nonneg : 0 β€ a - b β b β€ a :=
have a - a β€ a - b β b β€ a, from sub_le_sub_iff_left a,
by rwa sub_self at this
@[simp] lemma sub_nonpos : a - b β€ 0 β a β€ b :=
have a - b β€ b - b β a β€ b, from sub_le_sub_iff_right b,
by rwa sub_self at this
@[simp] lemma sub_pos : 0 < a - b β b < a :=
have a - a < a - b β b < a, from sub_lt_sub_iff_left a,
by rwa sub_self at this
@[simp] lemma sub_lt_zero : a - b < 0 β a < b :=
have a - b < b - b β a < b, from sub_lt_sub_iff_right b,
by rwa sub_self at this
lemma le_sub_iff_add_le' : b β€ c - a β a + b β€ c :=
by rw [sub_eq_add_neg, add_comm, le_neg_add_iff_add_le]
lemma le_sub_iff_add_le : a β€ c - b β a + b β€ c :=
by rw [le_sub_iff_add_le', add_comm]
lemma sub_le_iff_le_add' : a - b β€ c β a β€ b + c :=
by rw [sub_eq_add_neg, add_comm, neg_add_le_iff_le_add]
lemma sub_le_iff_le_add : a - c β€ b β a β€ b + c :=
by rw [sub_le_iff_le_add', add_comm]
@[simp] lemma neg_le_sub_iff_le_add : -b β€ a - c β c β€ a + b :=
le_sub_iff_add_le.trans neg_add_le_iff_le_add'
lemma neg_le_sub_iff_le_add' : -a β€ b - c β c β€ a + b :=
by rw [neg_le_sub_iff_le_add, add_comm]
lemma sub_le : a - b β€ c β a - c β€ b :=
sub_le_iff_le_add'.trans sub_le_iff_le_add.symm
theorem le_sub : a β€ b - c β c β€ b - a :=
le_sub_iff_add_le'.trans le_sub_iff_add_le.symm
lemma lt_sub_iff_add_lt' : b < c - a β a + b < c :=
by rw [sub_eq_add_neg, add_comm, lt_neg_add_iff_add_lt]
lemma lt_sub_iff_add_lt : a < c - b β a + b < c :=
by rw [lt_sub_iff_add_lt', add_comm]
lemma sub_lt_iff_lt_add' : a - b < c β a < b + c :=
by rw [sub_eq_add_neg, add_comm, neg_add_lt_iff_lt_add]
lemma sub_lt_iff_lt_add : a - c < b β a < b + c :=
by rw [sub_lt_iff_lt_add', add_comm]
@[simp] lemma neg_lt_sub_iff_lt_add : -b < a - c β c < a + b :=
lt_sub_iff_add_lt.trans neg_add_lt_iff_lt_add_right
lemma neg_lt_sub_iff_lt_add' : -a < b - c β c < a + b :=
by rw [neg_lt_sub_iff_lt_add, add_comm]
lemma sub_lt : a - b < c β a - c < b :=
sub_lt_iff_lt_add'.trans sub_lt_iff_lt_add.symm
theorem lt_sub : a < b - c β c < b - a :=
lt_sub_iff_add_lt'.trans lt_sub_iff_add_lt.symm
lemma sub_le_self_iff (a : Ξ±) {b : Ξ±} : a - b β€ a β 0 β€ b :=
sub_le_iff_le_add'.trans (le_add_iff_nonneg_left _)
lemma sub_lt_self_iff (a : Ξ±) {b : Ξ±} : a - b < a β 0 < b :=
sub_lt_iff_lt_add'.trans (lt_add_iff_pos_left _)
end ordered_add_comm_group
/-
TODO:
The `add_lt_add_left` field of `ordered_add_comm_group` is redundant,
and it is no longer in core so we can remove it now.
This alternative constructor is a workaround until someone fixes this.
-/
/-- Alternative constructor for ordered commutative groups,
that avoids the field `mul_lt_mul_left`. -/
@[to_additive "Alternative constructor for ordered commutative groups,
that avoids the field `mul_lt_mul_left`."]
def ordered_comm_group.mk' {Ξ± : Type u} [comm_group Ξ±] [partial_order Ξ±]
(mul_le_mul_left : β a b : Ξ±, a β€ b β β c : Ξ±, c * a β€ c * b) :
ordered_comm_group Ξ± :=
{ mul_le_mul_left := mul_le_mul_left,
..(by apply_instance : comm_group Ξ±),
..(by apply_instance : partial_order Ξ±) }
/-- A decidable linearly ordered cancellative additive commutative monoid
is an additive commutative monoid with a decidable linear order
in which addition is cancellative and strictly monotone. -/
@[protect_proj] class decidable_linear_ordered_cancel_add_comm_monoid (Ξ± : Type u)
extends ordered_cancel_add_comm_monoid Ξ±, decidable_linear_order Ξ±
section decidable_linear_ordered_cancel_add_comm_monoid
variables [decidable_linear_ordered_cancel_add_comm_monoid Ξ±]
lemma min_add_add_left (a b c : Ξ±) : min (a + b) (a + c) = a + min b c :=
eq.symm (eq_min
(show a + min b c β€ a + b, from add_le_add_left (min_le_left _ _) _)
(show a + min b c β€ a + c, from add_le_add_left (min_le_right _ _) _)
(assume d,
assume : d β€ a + b,
assume : d β€ a + c,
decidable.by_cases
(assume : b β€ c, by rwa [min_eq_left this])
(assume : Β¬ b β€ c, by rwa [min_eq_right (le_of_lt (lt_of_not_ge this))])))
lemma min_add_add_right (a b c : Ξ±) : min (a + c) (b + c) = min a b + c :=
begin rw [add_comm a c, add_comm b c, add_comm _ c], apply min_add_add_left end
lemma max_add_add_left (a b c : Ξ±) : max (a + b) (a + c) = a + max b c :=
eq.symm (eq_max
(add_le_add_left (le_max_left _ _) _)
(add_le_add_left (le_max_right _ _) _)
(assume d,
assume : a + b β€ d,
assume : a + c β€ d,
decidable.by_cases
(assume : b β€ c, by rwa [max_eq_right this])
(assume : Β¬ b β€ c, by rwa [max_eq_left (le_of_lt (lt_of_not_ge this))])))
lemma max_add_add_right (a b c : Ξ±) : max (a + c) (b + c) = max a b + c :=
begin rw [add_comm a c, add_comm b c, add_comm _ c], apply max_add_add_left end
end decidable_linear_ordered_cancel_add_comm_monoid
/-- A decidable linearly ordered additive commutative group is an
additive commutative group with a decidable linear order in which
addition is strictly monotone. -/
@[protect_proj] class decidable_linear_ordered_add_comm_group (Ξ± : Type u)
extends add_comm_group Ξ±, decidable_linear_order Ξ± :=
(add_le_add_left : β a b : Ξ±, a β€ b β β c : Ξ±, c + a β€ c + b)
instance decidable_linear_ordered_comm_group.to_ordered_add_comm_group (Ξ± : Type u)
[s : decidable_linear_ordered_add_comm_group Ξ±] : ordered_add_comm_group Ξ± :=
{ add := s.add, ..s }
section decidable_linear_ordered_add_comm_group
variables [decidable_linear_ordered_add_comm_group Ξ±]
@[priority 100] -- see Note [lower instance priority]
instance decidable_linear_ordered_add_comm_group.to_decidable_linear_ordered_cancel_add_comm_monoid :
decidable_linear_ordered_cancel_add_comm_monoid Ξ± :=
{ le_of_add_le_add_left := Ξ» x y z, le_of_add_le_add_left,
add_left_cancel := Ξ» x y z, add_left_cancel,
add_right_cancel := Ξ» x y z, add_right_cancel,
..βΉdecidable_linear_ordered_add_comm_group Ξ±βΊ }
lemma decidable_linear_ordered_add_comm_group.add_lt_add_left
(a b : Ξ±) (h : a < b) (c : Ξ±) : c + a < c + b :=
ordered_add_comm_group.add_lt_add_left a b h c
lemma max_neg_neg (a b : Ξ±) : max (-a) (-b) = - min a b :=
eq.symm (eq_max
(show -a β€ -(min a b), from neg_le_neg $ min_le_left a b)
(show -b β€ -(min a b), from neg_le_neg $ min_le_right a b)
(assume d,
assume Hβ : -a β€ d,
assume Hβ : -b β€ d,
have H : -d β€ min a b,
from le_min (neg_le_of_neg_le Hβ) (neg_le_of_neg_le Hβ),
show -(min a b) β€ d, from neg_le_of_neg_le H))
lemma min_eq_neg_max_neg_neg (a b : Ξ±) : min a b = - max (-a) (-b) :=
by rw [max_neg_neg, neg_neg]
lemma min_neg_neg (a b : Ξ±) : min (-a) (-b) = - max a b :=
by rw [min_eq_neg_max_neg_neg, neg_neg, neg_neg]
lemma max_eq_neg_min_neg_neg (a b : Ξ±) : max a b = - min (-a) (-b) :=
by rw [min_neg_neg, neg_neg]
/-- `abs a` is the absolute value of `a`. -/
def abs (a : Ξ±) : Ξ± := max a (-a)
lemma abs_of_nonneg {a : Ξ±} (h : 0 β€ a) : abs a = a :=
have h' : -a β€ a, from le_trans (neg_nonpos_of_nonneg h) h,
max_eq_left h'
lemma abs_of_pos {a : Ξ±} (h : 0 < a) : abs a = a :=
abs_of_nonneg (le_of_lt h)
lemma abs_of_nonpos {a : Ξ±} (h : a β€ 0) : abs a = -a :=
have h' : a β€ -a, from le_trans h (neg_nonneg_of_nonpos h),
max_eq_right h'
lemma abs_of_neg {a : Ξ±} (h : a < 0) : abs a = -a :=
abs_of_nonpos (le_of_lt h)
lemma abs_zero : abs 0 = (0:Ξ±) :=
abs_of_nonneg (le_refl _)
lemma abs_neg (a : Ξ±) : abs (-a) = abs a :=
begin unfold abs, rw [max_comm, neg_neg] end
lemma abs_pos_of_pos {a : Ξ±} (h : 0 < a) : 0 < abs a :=
by rwa (abs_of_pos h)
lemma abs_pos_of_neg {a : Ξ±} (h : a < 0) : 0 < abs a :=
abs_neg a βΈ abs_pos_of_pos (neg_pos_of_neg h)
lemma abs_sub (a b : Ξ±) : abs (a - b) = abs (b - a) :=
by rw [β neg_sub, abs_neg]
lemma ne_zero_of_abs_ne_zero {a : Ξ±} (h : abs a β 0) : a β 0 :=
assume ha, h (eq.symm ha βΈ abs_zero)
/- these assume a linear order -/
lemma eq_zero_of_neg_eq {a : Ξ±} (h : -a = a) : a = 0 :=
match lt_trichotomy a 0 with
| or.inl hβ :=
have 0 < a, from h βΈ neg_pos_of_neg hβ,
absurd hβ (lt_asymm this)
| or.inr (or.inl hβ) := hβ
| or.inr (or.inr hβ) :=
have a < 0, from h βΈ neg_neg_of_pos hβ,
absurd hβ (lt_asymm this)
end
lemma abs_nonneg (a : Ξ±) : 0 β€ abs a :=
or.elim (le_total 0 a)
(assume h : 0 β€ a, by rwa (abs_of_nonneg h))
(assume h : a β€ 0, calc
0 β€ -a : neg_nonneg_of_nonpos h
... = abs a : eq.symm (abs_of_nonpos h))
lemma abs_abs (a : Ξ±) : abs (abs a) = abs a :=
abs_of_nonneg $ abs_nonneg a
lemma le_abs_self (a : Ξ±) : a β€ abs a :=
or.elim (le_total 0 a)
(assume h : 0 β€ a,
begin rw [abs_of_nonneg h] end)
(assume h : a β€ 0, le_trans h $ abs_nonneg a)
lemma neg_le_abs_self (a : Ξ±) : -a β€ abs a :=
abs_neg a βΈ le_abs_self (-a)
lemma eq_zero_of_abs_eq_zero {a : Ξ±} (h : abs a = 0) : a = 0 :=
have hβ : a β€ 0, from h βΈ le_abs_self a,
have hβ : -a β€ 0, from h βΈ abs_neg a βΈ le_abs_self (-a),
le_antisymm hβ (nonneg_of_neg_nonpos hβ)
lemma eq_of_abs_sub_eq_zero {a b : Ξ±} (h : abs (a - b) = 0) : a = b :=
have a - b = 0, from eq_zero_of_abs_eq_zero h,
show a = b, from eq_of_sub_eq_zero this
lemma abs_pos_of_ne_zero {a : Ξ±} (h : a β 0) : 0 < abs a :=
or.elim (lt_or_gt_of_ne h) abs_pos_of_neg abs_pos_of_pos
lemma abs_by_cases (P : Ξ± β Prop) {a : Ξ±} (h1 : P a) (h2 : P (-a)) : P (abs a) :=
or.elim (le_total 0 a)
(assume h : 0 β€ a, eq.symm (abs_of_nonneg h) βΈ h1)
(assume h : a β€ 0, eq.symm (abs_of_nonpos h) βΈ h2)
lemma abs_le_of_le_of_neg_le {a b : Ξ±} (h1 : a β€ b) (h2 : -a β€ b) : abs a β€ b :=
abs_by_cases (Ξ» x : Ξ±, x β€ b) h1 h2
lemma abs_lt_of_lt_of_neg_lt {a b : Ξ±} (h1 : a < b) (h2 : -a < b) : abs a < b :=
abs_by_cases (Ξ» x : Ξ±, x < b) h1 h2
private lemma aux1 {a b : Ξ±} (h1 : 0 β€ a + b) (h2 : 0 β€ a) : abs (a + b) β€ abs a + abs b :=
decidable.by_cases
(assume h3 : 0 β€ b, calc
abs (a + b) β€ abs (a + b) : by apply le_refl
... = a + b : by rw (abs_of_nonneg h1)
... = abs a + b : by rw (abs_of_nonneg h2)
... = abs a + abs b : by rw (abs_of_nonneg h3))
(assume h3 : Β¬ 0 β€ b,
have h4 : b β€ 0, from le_of_lt (lt_of_not_ge h3),
calc
abs (a + b) = a + b : by rw (abs_of_nonneg h1)
... = abs a + b : by rw (abs_of_nonneg h2)
... β€ abs a + 0 : add_le_add_left h4 _
... β€ abs a + -b : add_le_add_left (neg_nonneg_of_nonpos h4) _
... = abs a + abs b : by rw (abs_of_nonpos h4))
private lemma aux2 {a b : Ξ±} (h1 : 0 β€ a + b) : abs (a + b) β€ abs a + abs b :=
or.elim (le_total b 0)
(assume h2 : b β€ 0,
have h3 : Β¬ a < 0, from
assume h4 : a < 0,
have h5 : a + b < 0,
begin
have aux := add_lt_add_of_lt_of_le h4 h2,
rwa [add_zero] at aux
end,
not_lt_of_ge h1 h5,
aux1 h1 (le_of_not_gt h3))
(assume h2 : 0 β€ b,
begin
have h3 : abs (b + a) β€ abs b + abs a,
begin
rw add_comm at h1,
exact aux1 h1 h2
end,
rw [add_comm, add_comm (abs a)],
exact h3
end)
lemma abs_add_le_abs_add_abs (a b : Ξ±) : abs (a + b) β€ abs a + abs b :=
or.elim (le_total 0 (a + b))
(assume h2 : 0 β€ a + b, aux2 h2)
(assume h2 : a + b β€ 0,
have h3 : -a + -b = -(a + b), by rw neg_add,
have h4 : 0 β€ -(a + b), from neg_nonneg_of_nonpos h2,
have h5 : 0 β€ -a + -b, begin rw [β h3] at h4, exact h4 end,
calc
abs (a + b) = abs (-a + -b) : by rw [β abs_neg, neg_add]
... β€ abs (-a) + abs (-b) : aux2 h5
... = abs a + abs b : by rw [abs_neg, abs_neg])
lemma abs_sub_abs_le_abs_sub (a b : Ξ±) : abs a - abs b β€ abs (a - b) :=
have h1 : abs a - abs b + abs b β€ abs (a - b) + abs b, from
calc
abs a - abs b + abs b = abs a : by rw sub_add_cancel
... = abs (a - b + b) : by rw sub_add_cancel
... β€ abs (a - b) + abs b : by apply abs_add_le_abs_add_abs,
le_of_add_le_add_right h1
lemma abs_sub_le (a b c : Ξ±) : abs (a - c) β€ abs (a - b) + abs (b - c) :=
calc
abs (a - c) = abs (a - b + (b - c)) : by rw [sub_eq_add_neg, sub_eq_add_neg, sub_eq_add_neg,
add_assoc, neg_add_cancel_left]
... β€ abs (a - b) + abs (b - c) : by apply abs_add_le_abs_add_abs
lemma abs_add_three (a b c : Ξ±) : abs (a + b + c) β€ abs a + abs b + abs c :=
begin
apply le_trans,
apply abs_add_le_abs_add_abs,
apply le_trans,
apply add_le_add_right,
apply abs_add_le_abs_add_abs,
apply le_refl
end
lemma dist_bdd_within_interval {a b lb ub : Ξ±} (hal : lb β€ a) (hau : a β€ ub)
(hbl : lb β€ b) (hbu : b β€ ub) : abs (a - b) β€ ub - lb :=
begin
cases (decidable.em (b β€ a)) with hba hba,
rw (abs_of_nonneg (sub_nonneg_of_le hba)),
apply sub_le_sub,
apply hau,
apply hbl,
rw [abs_of_neg (sub_neg_of_lt (lt_of_not_ge hba)), neg_sub],
apply sub_le_sub,
apply hbu,
apply hal
end
lemma decidable_linear_ordered_add_comm_group.eq_of_abs_sub_nonpos
{a b : Ξ±} (h : abs (a - b) β€ 0) : a = b :=
eq_of_abs_sub_eq_zero (le_antisymm h (abs_nonneg (a - b)))
end decidable_linear_ordered_add_comm_group
set_option old_structure_cmd true
section prio
set_option default_priority 100 -- see Note [default priority]
/-- This is not so much a new structure as a construction mechanism
for ordered groups, by specifying only the "positive cone" of the group. -/
class nonneg_add_comm_group (Ξ± : Type*) extends add_comm_group Ξ± :=
(nonneg : Ξ± β Prop)
(pos : Ξ± β Prop := Ξ» a, nonneg a β§ Β¬ nonneg (neg a))
(pos_iff : β a, pos a β nonneg a β§ Β¬ nonneg (-a) . order_laws_tac)
(zero_nonneg : nonneg 0)
(add_nonneg : β {a b}, nonneg a β nonneg b β nonneg (a + b))
(nonneg_antisymm : β {a}, nonneg a β nonneg (-a) β a = 0)
end prio
namespace nonneg_add_comm_group
variable [s : nonneg_add_comm_group Ξ±]
include s
@[reducible, priority 100] -- see Note [lower instance priority]
instance to_ordered_add_comm_group : ordered_add_comm_group Ξ± :=
{ le := Ξ» a b, nonneg (b - a),
lt := Ξ» a b, pos (b - a),
lt_iff_le_not_le := Ξ» a b, by simp; rw [pos_iff]; simp,
le_refl := Ξ» a, by simp [zero_nonneg],
le_trans := Ξ» a b c nab nbc, by simp [-sub_eq_add_neg];
rw β sub_add_sub_cancel; exact add_nonneg nbc nab,
le_antisymm := Ξ» a b nab nba, eq_of_sub_eq_zero $
nonneg_antisymm nba (by rw neg_sub; exact nab),
add_le_add_left := Ξ» a b nab c, by simpa [(β€), preorder.le] using nab,
..s }
theorem nonneg_def {a : Ξ±} : nonneg a β 0 β€ a :=
show _ β nonneg _, by simp
theorem pos_def {a : Ξ±} : pos a β 0 < a :=
show _ β pos _, by simp
theorem not_zero_pos : Β¬ pos (0 : Ξ±) :=
mt pos_def.1 (lt_irrefl _)
theorem zero_lt_iff_nonneg_nonneg {a : Ξ±} :
0 < a β nonneg a β§ Β¬ nonneg (-a) :=
pos_def.symm.trans (pos_iff _)
theorem nonneg_total_iff :
(β a : Ξ±, nonneg a β¨ nonneg (-a)) β
(β a b : Ξ±, a β€ b β¨ b β€ a) :=
β¨Ξ» h a b, by have := h (b - a); rwa [neg_sub] at this,
Ξ» h a, by rw [nonneg_def, nonneg_def, neg_nonneg]; apply hβ©
/--
A `nonneg_add_comm_group` is a `decidable_linear_ordered_add_comm_group`
if `nonneg` is total and decidable.
-/
def to_decidable_linear_ordered_add_comm_group
[decidable_pred (@nonneg Ξ± _)]
(nonneg_total : β a : Ξ±, nonneg a β¨ nonneg (-a))
: decidable_linear_ordered_add_comm_group Ξ± :=
{ le := (β€),
lt := (<),
lt_iff_le_not_le := @lt_iff_le_not_le _ _,
le_refl := @le_refl _ _,
le_trans := @le_trans _ _,
le_antisymm := @le_antisymm _ _,
le_total := nonneg_total_iff.1 nonneg_total,
decidable_le := by apply_instance,
decidable_lt := by apply_instance,
..@nonneg_add_comm_group.to_ordered_add_comm_group _ s }
end nonneg_add_comm_group
namespace order_dual
instance [ordered_add_comm_monoid Ξ±] : ordered_add_comm_monoid (order_dual Ξ±) :=
{ add_le_add_left := Ξ» a b h c, @add_le_add_left' Ξ± _ b a c h,
lt_of_add_lt_add_left := Ξ» a b c h, @lt_of_add_lt_add_left' Ξ± _ a c b h,
..order_dual.partial_order Ξ±,
..show add_comm_monoid Ξ±, by apply_instance }
instance [ordered_cancel_add_comm_monoid Ξ±] : ordered_cancel_add_comm_monoid (order_dual Ξ±) :=
{ le_of_add_le_add_left := Ξ» a b c : Ξ±, le_of_add_le_add_left,
add_left_cancel := @add_left_cancel Ξ± _,
add_right_cancel := @add_right_cancel Ξ± _,
..order_dual.ordered_add_comm_monoid }
instance [ordered_add_comm_group Ξ±] : ordered_add_comm_group (order_dual Ξ±) :=
{ add_left_neg := Ξ» a : Ξ±, add_left_neg a,
..order_dual.ordered_add_comm_monoid,
..show add_comm_group Ξ±, by apply_instance }
end order_dual
|
c109f95ac5b4fb9429413610ce4e047fc2e423b2 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/data/polynomial/cardinal.lean | 25880dfa7c6ed54ea641568740ec14b507a6c560 | [
"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 | 789 | lean | /-
Copyright (c) 2021 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes
-/
import data.mv_polynomial.cardinal
import data.mv_polynomial.equiv
/-!
# Cardinality of Polynomial Ring
The reuslt in this file is that the cardinality of `polynomial R` is at most the maximum
of `#R` and `Ο`.
-/
universe u
open_locale cardinal polynomial
open cardinal
namespace polynomial
lemma cardinal_mk_le_max {R : Type u} [comm_semiring R] : #R[X] β€ max (#R) Ο :=
calc #R[X] = #(mv_polynomial punit.{u + 1} R) :
cardinal.eq.2 β¨(mv_polynomial.punit_alg_equiv.{u u} R).to_equiv.symmβ©
... β€ _ : mv_polynomial.cardinal_mk_le_max
... β€ _ : by rw [max_assoc, max_eq_right (lt_omega_of_fintype punit).le]
end polynomial
|
d88086eaae21944c2698ece102832d7c1f4f40c0 | 35677d2df3f081738fa6b08138e03ee36bc33cad | /src/category_theory/hom_functor.lean | 26ea273aa570a0e078a2377dd6b8a3a061605f0b | [
"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 | 929 | lean | /-
Copyright (c) 2018 Reid Barton. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Reid Barton, Scott Morrison
-/
import category_theory.opposites
import category_theory.products.basic
/-!
The hom functor, sending `(X, Y)` to the type `X βΆ Y`.
-/
universes v u
open opposite
open category_theory
namespace category_theory.functor
variables (C : Type u) [π : category.{v} C]
include π
/-- `functor.hom` is the hom-pairing, sending (X,Y) to X β Y, contravariant in X and covariant in Y. -/
definition hom : Cα΅α΅ Γ C β₯€ Type v :=
{ obj := Ξ» p, unop p.1 βΆ p.2,
map := Ξ» X Y f, Ξ» h, f.1.unop β« h β« f.2 }
@[simp] lemma hom_obj (X : Cα΅α΅ Γ C) : (hom C).obj X = (unop X.1 βΆ X.2) := rfl
@[simp] lemma hom_pairing_map {X Y : Cα΅α΅ Γ C} (f : X βΆ Y) :
(hom C).map f = Ξ» h, f.1.unop β« h β« f.2 := rfl
end category_theory.functor
|
bab4ebb5de63bb6e2d2f674739383137bdcda0db | 947b78d97130d56365ae2ec264df196ce769371a | /stage0/src/Lean/Meta/Match/Match.lean | ce358a32cda6bf2e5c8d689f4af37c5469343432 | [
"Apache-2.0"
] | permissive | shyamalschandra/lean4 | 27044812be8698f0c79147615b1d5090b9f4b037 | 6e7a883b21eaf62831e8111b251dc9b18f40e604 | refs/heads/master | 1,671,417,126,371 | 1,601,859,995,000 | 1,601,860,020,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 42,657 | lean | /-
Copyright (c) 2020 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Util.CollectLevelParams
import Lean.Util.Recognizers
import Lean.Meta.Check
import Lean.Meta.Closure
import Lean.Meta.Tactic.Cases
import Lean.Meta.GeneralizeTelescope
import Lean.Meta.Match.MVarRenaming
import Lean.Meta.Match.CaseValues
import Lean.Meta.Match.CaseArraySizes
namespace Lean
namespace Meta
namespace Match
inductive Pattern : Type
| inaccessible (e : Expr) : Pattern
| var (fvarId : FVarId) : Pattern
| ctor (ctorName : Name) (us : List Level) (params : List Expr) (fields : List Pattern) : Pattern
| val (e : Expr) : Pattern
| arrayLit (type : Expr) (xs : List Pattern) : Pattern
| as (varId : FVarId) (p : Pattern) : Pattern
namespace Pattern
instance : Inhabited Pattern := β¨Pattern.inaccessible (arbitrary _)β©
partial def toMessageData : Pattern β MessageData
| inaccessible e => ".(" ++ e ++ ")"
| var varId => mkFVar varId
| ctor ctorName _ _ [] => ctorName
| ctor ctorName _ _ pats => "(" ++ ctorName ++ pats.foldl (fun (msg : MessageData) pat => msg ++ " " ++ toMessageData pat) Format.nil ++ ")"
| val e => e
| arrayLit _ pats => "#[" ++ MessageData.joinSep (pats.map toMessageData) ", " ++ "]"
| as varId p => mkFVar varId ++ "@" ++toMessageData p
partial def toExpr : Pattern β MetaM Expr
| inaccessible e => pure e
| var fvarId => pure $ mkFVar fvarId
| val e => pure e
| as _ p => toExpr p
| arrayLit type xs => do
xs β xs.mapM toExpr;
mkArrayLit type xs
| ctor ctorName us params fields => do
fields β fields.mapM toExpr;
pure $ mkAppN (mkConst ctorName us) (params ++ fields).toArray
/- Apply the free variable substitution `s` to the given pattern -/
partial def applyFVarSubst (s : FVarSubst) : Pattern β Pattern
| inaccessible e => inaccessible $ s.apply e
| ctor n us ps fs => ctor n us (ps.map s.apply) $ fs.map applyFVarSubst
| val e => val $ s.apply e
| arrayLit t xs => arrayLit (s.apply t) $ xs.map applyFVarSubst
| var fvarId => match s.find? fvarId with
| some e => inaccessible e
| none => var fvarId
| as fvarId p => match s.find? fvarId with
| none => as fvarId $ applyFVarSubst p
| some _ => applyFVarSubst p
def replaceFVarId (fvarId : FVarId) (v : Expr) (p : Pattern) : Pattern :=
let s : FVarSubst := {};
p.applyFVarSubst (s.insert fvarId v)
end Pattern
structure AltLHS :=
(ref : Syntax)
(fvarDecls : List LocalDecl) -- Free variables used in the patterns.
(patterns : List Pattern) -- We use `List Pattern` since we have nary match-expressions.
structure Alt :=
(ref : Syntax)
(idx : Nat) -- for generating error messages
(rhs : Expr)
(fvarDecls : List LocalDecl)
(patterns : List Pattern)
namespace Alt
instance : Inhabited Alt := β¨β¨arbitrary _, 0, arbitrary _, [], []β©β©
partial def toMessageData (alt : Alt) : MetaM MessageData := do
withExistingLocalDecls alt.fvarDecls do
let msg : List MessageData := alt.fvarDecls.map fun d => d.toExpr ++ ":(" ++ d.type ++ ")";
let msg : MessageData := msg ++ " |- " ++ (alt.patterns.map Pattern.toMessageData) ++ " => " ++ alt.rhs;
addMessageContext msg
def applyFVarSubst (s : FVarSubst) (alt : Alt) : Alt :=
{ alt with
patterns := alt.patterns.map fun p => p.applyFVarSubst s,
fvarDecls := alt.fvarDecls.map fun d => d.applyFVarSubst s,
rhs := alt.rhs.applyFVarSubst s }
def replaceFVarId (fvarId : FVarId) (v : Expr) (alt : Alt) : Alt :=
{ alt with
patterns := alt.patterns.map fun p => p.replaceFVarId fvarId v,
fvarDecls :=
let decls := alt.fvarDecls.filter fun d => d.fvarId != fvarId;
decls.map $ replaceFVarIdAtLocalDecl fvarId v,
rhs := alt.rhs.replaceFVarId fvarId v }
/-
Similar to `checkAndReplaceFVarId`, but ensures type of `v` is definitionally equal to type of `fvarId`.
This extra check is necessary when performing dependent elimination and inaccessible terms have been used.
For example, consider the following code fragment:
```
inductive Vec (Ξ± : Type u) : Nat β Type u
| nil : Vec Ξ± 0
| cons {n} (head : Ξ±) (tail : Vec Ξ± n) : Vec Ξ± (n+1)
inductive VecPred {Ξ± : Type u} (P : Ξ± β Prop) : {n : Nat} β Vec Ξ± n β Prop
| nil : VecPred P Vec.nil
| cons {n : Nat} {head : Ξ±} {tail : Vec Ξ± n} : P head β VecPred P tail β VecPred P (Vec.cons head tail)
theorem ex {Ξ± : Type u} (P : Ξ± β Prop) : {n : Nat} β (v : Vec Ξ± (n+1)) β VecPred P v β Exists P
| _, Vec.cons head _, VecPred.cons h (w : VecPred P Vec.nil) => β¨head, hβ©
```
Recall that `_` in a pattern can be elaborated into pattern variable or an inaccessible term.
The elaborator uses an inaccessible term when typing constraints restrict its value.
Thus, in the example above, the `_` at `Vec.cons head _` becomes the inaccessible pattern `.(Vec.nil)`
because the type ascription `(w : VecPred P Vec.nil)` propagates typing constraints that restrict its value to be `Vec.nil`.
After elaboration the alternative becomes:
```
| .(0), @Vec.cons .(Ξ±) .(0) head .(Vec.nil), @VecPred.cons .(Ξ±) .(P) .(0) .(head) .(Vec.nil) h w => β¨head, hβ©
```
where
```
(head : Ξ±), (h: P head), (w : VecPred P Vec.nil)
```
Then, when we process this alternative in this module, the following check will detect that
`w` has type `VecPred P Vec.nil`, when it is supposed to have type `VecPred P tail`.
Note that if we had written
```
theorem ex {Ξ± : Type u} (P : Ξ± β Prop) : {n : Nat} β (v : Vec Ξ± (n+1)) β VecPred P v β Exists P
| _, Vec.cons head Vec.nil, VecPred.cons h (w : VecPred P Vec.nil) => β¨head, hβ©
```
we would get the easier to digest error message
```
missing cases:
_, (Vec.cons _ _ (Vec.cons _ _ _)), _
```
-/
def checkAndReplaceFVarId (fvarId : FVarId) (v : Expr) (alt : Alt) : MetaM Alt := do
match alt.fvarDecls.find? fun (fvarDecl : LocalDecl) => fvarDecl.fvarId == fvarId with
| none => throwErrorAt alt.ref "unknown free pattern variable"
| some fvarDecl => do
vType β inferType v;
unlessM (isDefEqGuarded fvarDecl.type vType) $
withExistingLocalDecls alt.fvarDecls $ throwErrorAt alt.ref $
"type mismatch during dependent match-elimination at pattern variable '" ++ mkFVar fvarDecl.fvarId ++ "' with type" ++ indentExpr fvarDecl.type ++
Format.line ++ "expected type" ++ indentExpr vType;
pure $ replaceFVarId fvarId v alt
end Alt
inductive Example
| var : FVarId β Example
| underscore : Example
| ctor : Name β List Example β Example
| val : Expr β Example
| arrayLit : List Example β Example
namespace Example
partial def replaceFVarId (fvarId : FVarId) (ex : Example) : Example β Example
| var x => if x == fvarId then ex else var x
| ctor n exs => ctor n $ exs.map replaceFVarId
| arrayLit exs => arrayLit $ exs.map replaceFVarId
| ex => ex
partial def applyFVarSubst (s : FVarSubst) : Example β Example
| var fvarId =>
match s.get fvarId with
| Expr.fvar fvarId' _ => var fvarId'
| _ => underscore
| ctor n exs => ctor n $ exs.map applyFVarSubst
| arrayLit exs => arrayLit $ exs.map applyFVarSubst
| ex => ex
partial def varsToUnderscore : Example β Example
| var x => underscore
| ctor n exs => ctor n $ exs.map varsToUnderscore
| arrayLit exs => arrayLit $ exs.map varsToUnderscore
| ex => ex
partial def toMessageData : Example β MessageData
| var fvarId => mkFVar fvarId
| ctor ctorName [] => mkConst ctorName
| ctor ctorName exs => "(" ++ mkConst ctorName ++ exs.foldl (fun (msg : MessageData) pat => msg ++ " " ++ toMessageData pat) Format.nil ++ ")"
| arrayLit exs => "#" ++ MessageData.ofList (exs.map toMessageData)
| val e => e
| underscore => "_"
end Example
def examplesToMessageData (cex : List Example) : MessageData :=
MessageData.joinSep (cex.map (Example.toMessageData β Example.varsToUnderscore)) ", "
structure Problem :=
(mvarId : MVarId)
(vars : List Expr)
(alts : List Alt)
(examples : List Example)
def withGoalOf {Ξ±} (p : Problem) (x : MetaM Ξ±) : MetaM Ξ± :=
withMVarContext p.mvarId x
namespace Problem
instance : Inhabited Problem := β¨{ mvarId := arbitrary _, vars := [], alts := [], examples := []}β©
def toMessageData (p : Problem) : MetaM MessageData :=
withGoalOf p do
alts β p.alts.mapM Alt.toMessageData;
vars : List MessageData β p.vars.mapM fun x => do { xType β inferType x; pure (x ++ ":(" ++ xType ++ ")" : MessageData) };
pure $ "vars " ++ vars
-- ++ Format.line ++ "var ids " ++ toString (p.vars.map (fun x => match x with | Expr.fvar id _ => toString id | _ => "[nonvar]"))
++ Format.line ++ MessageData.joinSep alts Format.line
++ Format.line ++ "examples: " ++ examplesToMessageData p.examples
++ Format.line
end Problem
abbrev CounterExample := List Example
def counterExampleToMessageData (cex : CounterExample) : MessageData :=
examplesToMessageData cex
def counterExamplesToMessageData (cexs : List CounterExample) : MessageData :=
MessageData.joinSep (cexs.map counterExampleToMessageData) Format.line
structure MatcherResult :=
(matcher : Expr) -- The matcher. It is not just `Expr.const matcherName` because the type of the major premises may contain free variables.
(counterExamples : List CounterExample)
(unusedAltIdxs : List Nat)
/- The number of patterns in each AltLHS must be equal to majors.length -/
private def checkNumPatterns (majors : Array Expr) (lhss : List AltLHS) : MetaM Unit :=
let num := majors.size;
when (lhss.any (fun lhs => lhs.patterns.length != num)) $
throwError "incorrect number of patterns"
private partial def withAltsAux {Ξ±} (motive : Expr) : List AltLHS β List Alt β Array (Expr Γ Nat) β (List Alt β Array (Expr Γ Nat) β MetaM Ξ±) β MetaM Ξ±
| [], alts, minors, k => k alts.reverse minors
| lhs::lhss, alts, minors, k => do
let xs := lhs.fvarDecls.toArray.map LocalDecl.toExpr;
minorType β withExistingLocalDecls lhs.fvarDecls do {
args β lhs.patterns.toArray.mapM Pattern.toExpr;
let minorType := mkAppN motive args;
mkForallFVars xs minorType
};
let (minorType, minorNumParams) := if !xs.isEmpty then (minorType, xs.size) else (mkThunkType minorType, 1);
let idx := alts.length;
let minorName := (`h).appendIndexAfter (idx+1);
trace! `Meta.Match.debug ("minor premise " ++ minorName ++ " : " ++ minorType);
withLocalDeclD minorName minorType fun minor => do
let rhs := if xs.isEmpty then mkApp minor (mkConst `Unit.unit) else mkAppN minor xs;
let minors := minors.push (minor, minorNumParams);
fvarDecls β lhs.fvarDecls.mapM instantiateLocalDeclMVars;
let alts := { ref := lhs.ref, idx := idx, rhs := rhs, fvarDecls := fvarDecls, patterns := lhs.patterns : Alt } :: alts;
withAltsAux lhss alts minors k
/- Given a list of `AltLHS`, create a minor premise for each one, convert them into `Alt`, and then execute `k` -/
private partial def withAlts {Ξ±} (motive : Expr) (lhss : List AltLHS) (k : List Alt β Array (Expr Γ Nat) β MetaM Ξ±) : MetaM Ξ± :=
withAltsAux motive lhss [] #[] k
def assignGoalOf (p : Problem) (e : Expr) : MetaM Unit :=
withGoalOf p (assignExprMVar p.mvarId e)
structure State :=
(used : Std.HashSet Nat := {}) -- used alternatives
(counterExamples : List (List Example) := [])
/-- Return true if the given (sub-)problem has been solved. -/
private def isDone (p : Problem) : Bool :=
p.vars.isEmpty
/-- Return true if the next element on the `p.vars` list is a variable. -/
private def isNextVar (p : Problem) : Bool :=
match p.vars with
| Expr.fvar _ _ :: _ => true
| _ => false
private def hasAsPattern (p : Problem) : Bool :=
p.alts.any fun alt => match alt.patterns with
| Pattern.as _ _ :: _ => true
| _ => false
private def hasCtorPattern (p : Problem) : Bool :=
p.alts.any fun alt => match alt.patterns with
| Pattern.ctor _ _ _ _ :: _ => true
| _ => false
private def hasValPattern (p : Problem) : Bool :=
p.alts.any fun alt => match alt.patterns with
| Pattern.val _ :: _ => true
| _ => false
private def hasNatValPattern (p : Problem) : Bool :=
p.alts.any fun alt => match alt.patterns with
| Pattern.val v :: _ => v.isNatLit
| _ => false
private def hasVarPattern (p : Problem) : Bool :=
p.alts.any fun alt => match alt.patterns with
| Pattern.var _ :: _ => true
| _ => false
private def hasArrayLitPattern (p : Problem) : Bool :=
p.alts.any fun alt => match alt.patterns with
| Pattern.arrayLit _ _ :: _ => true
| _ => false
private def isVariableTransition (p : Problem) : Bool :=
p.alts.all fun alt => match alt.patterns with
| Pattern.inaccessible _ :: _ => true
| Pattern.var _ :: _ => true
| _ => false
private def isConstructorTransition (p : Problem) : Bool :=
(hasCtorPattern p || p.alts.isEmpty)
&& p.alts.all fun alt => match alt.patterns with
| Pattern.ctor _ _ _ _ :: _ => true
| Pattern.var _ :: _ => true
| Pattern.inaccessible _ :: _ => true
| _ => false
private def isValueTransition (p : Problem) : Bool :=
hasVarPattern p && hasValPattern p
&& p.alts.all fun alt => match alt.patterns with
| Pattern.val _ :: _ => true
| Pattern.var _ :: _ => true
| _ => false
private def isArrayLitTransition (p : Problem) : Bool :=
hasArrayLitPattern p && hasVarPattern p
&& p.alts.all fun alt => match alt.patterns with
| Pattern.arrayLit _ _ :: _ => true
| Pattern.var _ :: _ => true
| _ => false
private def isNatValueTransition (p : Problem) : Bool :=
hasNatValPattern p
&& (!isNextVar p ||
p.alts.any fun alt => match alt.patterns with
| Pattern.ctor _ _ _ _ :: _ => true
| Pattern.inaccessible _ :: _ => true
| _ => false)
private def processSkipInaccessible (p : Problem) : Problem :=
match p.vars with
| [] => unreachable!
| x :: xs => do
let alts := p.alts.map fun alt => match alt.patterns with
| Pattern.inaccessible _ :: ps => { alt with patterns := ps }
| _ => unreachable!;
{ p with alts := alts, vars := xs }
private def processLeaf (p : Problem) : StateRefT State MetaM Unit :=
match p.alts with
| [] => do
liftM $ admit p.mvarId;
modify fun s => { s with counterExamples := p.examples :: s.counterExamples }
| alt :: _ => do
-- TODO: check whether we have unassigned metavars in rhs
liftM $ assignGoalOf p alt.rhs;
modify fun s => { s with used := s.used.insert alt.idx }
private def processAsPattern (p : Problem) : MetaM Problem :=
match p.vars with
| [] => unreachable!
| x :: xs => withGoalOf p do
alts β p.alts.mapM fun alt => match alt.patterns with
| Pattern.as fvarId p :: ps => { alt with patterns := p :: ps }.checkAndReplaceFVarId fvarId x
| _ => pure alt;
pure { p with alts := alts }
private def processVariable (p : Problem) : MetaM Problem :=
match p.vars with
| [] => unreachable!
| x :: xs => withGoalOf p do
alts β p.alts.mapM fun alt => match alt.patterns with
| Pattern.inaccessible _ :: ps => pure { alt with patterns := ps }
| Pattern.var fvarId :: ps => { alt with patterns := ps }.checkAndReplaceFVarId fvarId x
| _ => unreachable!;
pure { p with alts := alts, vars := xs }
private def throwInductiveTypeExpected {Ξ±} (e : Expr) : MetaM Ξ± := do
t β inferType e;
throwError ("failed to compile pattern matching, inductive type expected" ++ indentExpr e ++ Format.line ++ "has type" ++ indentExpr t)
private def inLocalDecls (localDecls : List LocalDecl) (fvarId : FVarId) : Bool :=
localDecls.any fun d => d.fvarId == fvarId
namespace Unify
structure Context :=
(altFVarDecls : List LocalDecl)
structure State :=
(fvarSubst : FVarSubst := {})
abbrev M := ReaderT Context $ StateRefT State MetaM
def isAltVar (fvarId : FVarId) : M Bool := do
ctx β read;
pure $ inLocalDecls ctx.altFVarDecls fvarId
def expandIfVar (e : Expr) : M Expr := do
match e with
| Expr.fvar _ _ => do s β get; pure $ s.fvarSubst.apply e
| _ => pure e
def occurs (fvarId : FVarId) (v : Expr) : Bool :=
(v.find? fun e => match e with
| Expr.fvar fvarId' _ => fvarId == fvarId'
| _=> false).isSome
def assign (fvarId : FVarId) (v : Expr) : M Bool :=
if occurs fvarId v then do
trace! `Meta.Match.unify ("assign occurs check failed, " ++ mkFVar fvarId ++ " := " ++ v);
pure false
else do
ctx β read;
condM (isAltVar fvarId)
(do
trace! `Meta.Match.unify (mkFVar fvarId ++ " := " ++ v);
modify fun s => { s with fvarSubst := s.fvarSubst.insert fvarId v }; pure true)
(do
trace! `Meta.Match.unify ("assign failed variable is not local, " ++ mkFVar fvarId ++ " := " ++ v);
pure false)
partial def unify : Expr β Expr β M Bool
| a, b => do
trace! `Meta.Match.unify (a ++ " =?= " ++ b);
condM (isDefEq a b) (pure true) do
a' β expandIfVar a;
b' β expandIfVar b;
if a != a' || b != b' then unify a' b'
else match a, b with
| Expr.mdata _ a _, b => unify a b
| a, Expr.mdata _ b _ => unify a b
| Expr.fvar aFvarId _, Expr.fvar bFVarId _ => assign aFvarId b <||> assign bFVarId a
| Expr.fvar aFvarId _, b => assign aFvarId b
| a, Expr.fvar bFVarId _ => assign bFVarId a
| Expr.app aFn aArg _, Expr.app bFn bArg _ => unify aFn bFn <&&> unify aArg bArg
| _, _ => do
trace! `Meta.Match.unify ("unify failed @" ++ a ++ " =?= " ++ b);
pure false
end Unify
private def unify? (altFVarDecls : List LocalDecl) (a b : Expr) : MetaM (Option FVarSubst) := do
a β instantiateMVars a;
b β instantiateMVars b;
(b, s) β (Unify.unify a b { altFVarDecls := altFVarDecls}).run {};
if b then pure s.fvarSubst else pure none
private def expandVarIntoCtor? (alt : Alt) (fvarId : FVarId) (ctorName : Name) : MetaM (Option Alt) :=
withExistingLocalDecls alt.fvarDecls do
env β getEnv;
ldecl β getLocalDecl fvarId;
expectedType β inferType (mkFVar fvarId);
expectedType β whnfD expectedType;
(ctorLevels, ctorParams) β getInductiveUniverseAndParams expectedType;
let ctor := mkAppN (mkConst ctorName ctorLevels) ctorParams;
ctorType β inferType ctor;
forallTelescopeReducing ctorType fun ctorFields resultType => do
let ctor := mkAppN ctor ctorFields;
let alt := alt.replaceFVarId fvarId ctor;
ctorFieldDecls β ctorFields.mapM fun ctorField => getLocalDecl ctorField.fvarId!;
let newAltDecls := ctorFieldDecls.toList ++ alt.fvarDecls;
subst? β unify? newAltDecls resultType expectedType;
match subst? with
| none => pure none
| some subst => do
let newAltDecls := newAltDecls.filter fun d => !subst.contains d.fvarId; -- remove declarations that were assigned
let newAltDecls := newAltDecls.map fun d => d.applyFVarSubst subst; -- apply substitution to remaining declaration types
let patterns := alt.patterns.map fun p => p.applyFVarSubst subst;
let rhs := subst.apply alt.rhs;
let ctorFieldPatterns := ctorFields.toList.map fun ctorField => match subst.get ctorField.fvarId! with
| e@(Expr.fvar fvarId _) => if inLocalDecls newAltDecls fvarId then Pattern.var fvarId else Pattern.inaccessible e
| e => Pattern.inaccessible e;
pure $ some { alt with fvarDecls := newAltDecls, rhs := rhs, patterns := ctorFieldPatterns ++ patterns }
private def getInductiveVal? (x : Expr) : MetaM (Option InductiveVal) := do
xType β inferType x;
xType β whnfD xType;
match xType.getAppFn with
| Expr.const constName _ _ => do
cinfo β getConstInfo constName;
match cinfo with
| ConstantInfo.inductInfo val => pure (some val)
| _ => pure none
| _ => pure none
private def hasRecursiveType (x : Expr) : MetaM Bool := do
val? β getInductiveVal? x;
match val? with
| some val => pure val.isRec
| _ => pure false
/- Given `alt` s.t. the next pattern is an inaccessible pattern `e`,
try to normalize `e` into a constructor application.
If it is not a constructor, throw an error.
Otherwise, if it is a constructor application of `ctorName`,
update the next patterns with the fields of the constructor.
Otherwise, return none. -/
def processInaccessibleAsCtor (alt : Alt) (ctorName : Name) : MetaM (Option Alt) := do
env β getEnv;
match alt.patterns with
| p@(Pattern.inaccessible e) :: ps => do
trace! `Meta.Match.match ("inaccessible in ctor step " ++ e);
withExistingLocalDecls alt.fvarDecls do
-- Try to push inaccessible annotations.
e β whnfD e;
match e.constructorApp? env with
| some (ctorVal, ctorArgs) => do
if ctorVal.name == ctorName then
let fields := ctorArgs.extract ctorVal.nparams ctorArgs.size;
let fields := fields.toList.map Pattern.inaccessible;
pure $ some { alt with patterns := fields ++ ps }
else
pure none
| _ => throwErrorAt alt.ref $
"dependent match elimination failed, inaccessible pattern found " ++ indentD p.toMessageData ++
Format.line ++ "constructor expected"
| _ => unreachable!
private def processConstructor (p : Problem) : MetaM (Array Problem) := do
trace! `Meta.Match.match ("constructor step");
env β getEnv;
match p.vars with
| [] => unreachable!
| x :: xs => do
subgoals? β commitWhenSome? do {
subgoals β cases p.mvarId x.fvarId!;
if subgoals.isEmpty then
/- Easy case: we have solved problem `p` since there are no subgoals -/
pure (some #[])
else if !p.alts.isEmpty then
pure (some subgoals)
else do
isRec β withGoalOf p $ hasRecursiveType x;
/- If there are no alternatives and the type of the current variable is recursive, we do NOT consider
a constructor-transition to avoid nontermination.
TODO: implement a more general approach if this is not sufficient in practice -/
if isRec then pure none
else pure (some subgoals)
};
match subgoals? with
| none => pure #[{ p with vars := xs }]
| some subgoals => do
subgoals.mapM fun subgoal => withMVarContext subgoal.mvarId do
let subst := subgoal.subst;
let fields := subgoal.fields.toList;
let newVars := fields ++ xs;
let newVars := newVars.map fun x => x.applyFVarSubst subst;
let subex := Example.ctor subgoal.ctorName $ fields.map fun field => match field with
| Expr.fvar fvarId _ => Example.var fvarId
| _ => Example.underscore; -- This case can happen due to dependent elimination
let examples := p.examples.map $ Example.replaceFVarId x.fvarId! subex;
let examples := examples.map $ Example.applyFVarSubst subst;
let newAlts := p.alts.filter fun alt => match alt.patterns with
| Pattern.ctor n _ _ _ :: _ => n == subgoal.ctorName
| Pattern.var _ :: _ => true
| Pattern.inaccessible _ :: _ => true
| _ => false;
let newAlts := newAlts.map fun alt => alt.applyFVarSubst subst;
newAlts β newAlts.filterMapM fun alt => match alt.patterns with
| Pattern.ctor _ _ _ fields :: ps => pure $ some { alt with patterns := fields ++ ps }
| Pattern.var fvarId :: ps => expandVarIntoCtor? { alt with patterns := ps } fvarId subgoal.ctorName
| Pattern.inaccessible _ :: _ => processInaccessibleAsCtor alt subgoal.ctorName
| _ => unreachable!;
pure { mvarId := subgoal.mvarId, vars := newVars, alts := newAlts, examples := examples }
private def processNonVariable (p : Problem) : MetaM Problem :=
match p.vars with
| [] => unreachable!
| x :: xs => withGoalOf p do
x β whnfD x;
env β getEnv;
match x.constructorApp? env with
| some (ctorVal, xArgs) => do
alts β p.alts.filterMapM fun alt => match alt.patterns with
| Pattern.ctor n _ _ fields :: ps =>
if n != ctorVal.name then
pure none
else
pure $ some { alt with patterns := fields ++ ps }
| Pattern.inaccessible _ :: _ => processInaccessibleAsCtor alt ctorVal.name
| p :: _ => throwError ("failed to compile pattern matching, inaccessible pattern or constructor expected" ++ indentD p.toMessageData)
| _ => unreachable!;
let xFields := xArgs.extract ctorVal.nparams xArgs.size;
pure { p with alts := alts, vars := xFields.toList ++ xs }
| none =>
throwError ("failed to compile pattern matching, constructor expected" ++ indentExpr x)
private def collectValues (p : Problem) : Array Expr :=
p.alts.foldl
(fun (values : Array Expr) alt =>
match alt.patterns with
| Pattern.val v :: _ => if values.contains v then values else values.push v
| _ => values)
#[]
private def isFirstPatternVar (alt : Alt) : Bool :=
match alt.patterns with
| Pattern.var _ :: _ => true
| _ => false
private def processValue (p : Problem) : MetaM (Array Problem) := do
trace! `Meta.Match.match ("value step");
match p.vars with
| [] => unreachable!
| x :: xs => do
let values := collectValues p;
subgoals β caseValues p.mvarId x.fvarId! values;
subgoals.mapIdxM fun i subgoal =>
if h : i < values.size then do
let value := values.get β¨i, hβ©;
-- (x = value) branch
let subst := subgoal.subst;
let examples := p.examples.map $ Example.replaceFVarId x.fvarId! (Example.val value);
let examples := examples.map $ Example.applyFVarSubst subst;
let newAlts := p.alts.filter fun alt => match alt.patterns with
| Pattern.val v :: _ => v == value
| Pattern.var _ :: _ => true
| _ => false;
let newAlts := newAlts.map fun alt => alt.applyFVarSubst subst;
let newAlts := newAlts.map fun alt => match alt.patterns with
| Pattern.val _ :: ps => { alt with patterns := ps }
| Pattern.var fvarId :: ps => do
let alt := { alt with patterns := ps };
alt.replaceFVarId fvarId value
| _ => unreachable!;
let newVars := xs.map fun x => x.applyFVarSubst subst;
pure { mvarId := subgoal.mvarId, vars := newVars, alts := newAlts, examples := examples }
else do
-- else branch
let newAlts := p.alts.filter isFirstPatternVar;
pure { p with mvarId := subgoal.mvarId, alts := newAlts, vars := x::xs }
private def collectArraySizes (p : Problem) : Array Nat :=
p.alts.foldl
(fun (sizes : Array Nat) alt =>
match alt.patterns with
| Pattern.arrayLit _ ps :: _ => let sz := ps.length; if sizes.contains sz then sizes else sizes.push sz
| _ => sizes)
#[]
private def expandVarIntoArrayLitAux (alt : Alt) (fvarId : FVarId) (arrayElemType : Expr) (varNamePrefix : Name) : Nat β Array Expr β MetaM Alt
| n+1, newVars =>
withLocalDeclD (varNamePrefix.appendIndexAfter (n+1)) arrayElemType fun x =>
expandVarIntoArrayLitAux n (newVars.push x)
| 0, newVars => do
arrayLit β mkArrayLit arrayElemType newVars.toList;
let alt := alt.replaceFVarId fvarId arrayLit;
newDecls β newVars.toList.mapM fun newVar => getLocalDecl newVar.fvarId!;
let newPatterns := newVars.toList.map fun newVar => Pattern.var newVar.fvarId!;
pure { alt with fvarDecls := newDecls ++ alt.fvarDecls, patterns := newPatterns ++ alt.patterns }
private def expandVarIntoArrayLit (alt : Alt) (fvarId : FVarId) (arrayElemType : Expr) (arraySize : Nat) : MetaM Alt :=
withExistingLocalDecls alt.fvarDecls do
fvarDecl β getLocalDecl fvarId;
expandVarIntoArrayLitAux alt fvarId arrayElemType fvarDecl.userName arraySize #[]
private def processArrayLit (p : Problem) : MetaM (Array Problem) := do
trace! `Meta.Match.match ("array literal step");
match p.vars with
| [] => unreachable!
| x :: xs => do
let sizes := collectArraySizes p;
subgoals β caseArraySizes p.mvarId x.fvarId! sizes;
subgoals.mapIdxM fun i subgoal =>
if h : i < sizes.size then do
let size := sizes.get! i;
let subst := subgoal.subst;
let elems := subgoal.elems.toList;
let newVars := elems.map mkFVar ++ xs;
let newVars := newVars.map fun x => x.applyFVarSubst subst;
let subex := Example.arrayLit $ elems.map Example.var;
let examples := p.examples.map $ Example.replaceFVarId x.fvarId! subex;
let examples := examples.map $ Example.applyFVarSubst subst;
let newAlts := p.alts.filter fun alt => match alt.patterns with
| Pattern.arrayLit _ ps :: _ => ps.length == size
| Pattern.var _ :: _ => true
| _ => false;
let newAlts := newAlts.map fun alt => alt.applyFVarSubst subst;
newAlts β newAlts.mapM fun alt => match alt.patterns with
| Pattern.arrayLit _ pats :: ps => pure { alt with patterns := pats ++ ps }
| Pattern.var fvarId :: ps => do Ξ± β getArrayArgType x; expandVarIntoArrayLit { alt with patterns := ps } fvarId Ξ± size
| _ => unreachable!;
pure { mvarId := subgoal.mvarId, vars := newVars, alts := newAlts, examples := examples }
else do
-- else branch
let newAlts := p.alts.filter isFirstPatternVar;
pure { p with mvarId := subgoal.mvarId, alts := newAlts, vars := x::xs }
private def expandNatValuePattern (p : Problem) : Problem := do
let alts := p.alts.map fun alt => match alt.patterns with
| Pattern.val (Expr.lit (Literal.natVal 0) _) :: ps => { alt with patterns := Pattern.ctor `Nat.zero [] [] [] :: ps }
| Pattern.val (Expr.lit (Literal.natVal (n+1)) _) :: ps => { alt with patterns := Pattern.ctor `Nat.succ [] [] [Pattern.val (mkNatLit n)] :: ps }
| _ => alt;
{ p with alts := alts }
private def traceStep (msg : String) : StateRefT State MetaM Unit :=
liftM (trace! `Meta.Match.match (msg ++ " step") : MetaM Unit)
private def traceState (p : Problem) : MetaM Unit :=
withGoalOf p (traceM `Meta.Match.match p.toMessageData)
private def throwNonSupported (p : Problem) : MetaM Unit := do
msg β p.toMessageData;
throwError ("not implement yet " ++ msg)
def isCurrVarInductive (p : Problem) : MetaM Bool := do
match p.vars with
| [] => pure false
| x::_ => withGoalOf p do
val? β getInductiveVal? x;
pure val?.isSome
private partial def process : Problem β StateRefT State MetaM Unit
| p => withIncRecDepth do
liftM $ traceState p;
isInductive β liftM $ isCurrVarInductive p;
if isDone p then
processLeaf p
else if hasAsPattern p then do
traceStep ("as-pattern");
p β liftM $ processAsPattern p;
process p
else if isNatValueTransition p then do
traceStep ("nat value to constructor");
process (expandNatValuePattern p)
else if !isNextVar p then do
traceStep ("non variable");
p β liftM $ processNonVariable p;
process p
else if isInductive && isConstructorTransition p then do
ps β liftM $ processConstructor p;
ps.forM process
else if isVariableTransition p then do
traceStep ("variable");
p β liftM $ processVariable p;
process p
else if isValueTransition p then do
ps β liftM $ processValue p;
ps.forM process
else if isArrayLitTransition p then do
ps β liftM $ processArrayLit p;
ps.forM process
else
liftM $ throwNonSupported p
/--
A "matcher" auxiliary declaration has the following structure:
- `numParams` parameters
- motive
- `numDiscrs` discriminators (aka major premises)
- `altNumParams.size` alternatives (aka minor premises) where alternative `i` has `altNumParams[i]` alternatives
- `uElimPos?` is `some pos` when the matcher can eliminate in different universe levels, and
`pos` is the position of the universe level parameter that specifies the elimination universe.
It is `none` if the matcher only eliminates into `Prop`. -/
structure MatcherInfo :=
(numParams : Nat) (numDiscrs : Nat) (altNumParams : Array Nat) (uElimPos? : Option Nat)
def MatcherInfo.numAlts (matcherInfo : MatcherInfo) : Nat :=
matcherInfo.altNumParams.size
namespace Extension
structure Entry :=
(name : Name) (info : MatcherInfo)
structure State :=
(map : SMap Name MatcherInfo := {})
instance State.inhabited : Inhabited State :=
β¨{}β©
def State.addEntry (s : State) (e : Entry) : State := { s with map := s.map.insert e.name e.info }
def State.switch (s : State) : State := { s with map := s.map.switch }
def mkExtension : IO (SimplePersistentEnvExtension Entry State) :=
registerSimplePersistentEnvExtension {
name := `matcher,
addEntryFn := State.addEntry,
addImportedFn := fun es => (mkStateFromImportedEntries State.addEntry {} es).switch
}
@[init mkExtension]
constant extension : SimplePersistentEnvExtension Entry State :=
arbitrary _
def addMatcherInfo (env : Environment) (matcherName : Name) (info : MatcherInfo) : Environment :=
extension.addEntry env { name := matcherName, info := info }
def getMatcherInfo? (env : Environment) (declName : Name) : Option MatcherInfo :=
(extension.getState env).map.find? declName
end Extension
def addMatcherInfo (matcherName : Name) (info : MatcherInfo) : MetaM Unit :=
modifyEnv fun env => Extension.addMatcherInfo env matcherName info
private def getUElimPos? (matcherLevels : List Level) (uElim : Level) : MetaM (Option Nat) :=
if uElim == levelZero then pure none
else match matcherLevels.toArray.indexOf? uElim with
| none => throwError "dependent match elimination failed, universe level not found"
| some pos => pure $ some pos.val
/-
Create a dependent matcher for `matchType` where `matchType` is of the form
`(a_1 : A_1) -> (a_2 : A_2[a_1]) -> ... -> (a_n : A_n[a_1, a_2, ... a_{n-1}]) -> B[a_1, ..., a_n]`
where `n = numDiscrs`, and the `lhss` are the left-hand-sides of the `match`-expression alternatives.
Each `AltLHS` has a list of local declarations and a list of patterns.
The number of patterns must be the same in each `AltLHS`.
The generated matcher has the structure described at `MatcherInfo`. The motive argument is of the form
`(motive : (a_1 : A_1) -> (a_2 : A_2[a_1]) -> ... -> (a_n : A_n[a_1, a_2, ... a_{n-1}]) -> Sort v)`
where `v` is a universe parameter or 0 if `B[a_1, ..., a_n]` is a proposition.
-/
def mkMatcher (matcherName : Name) (matchType : Expr) (numDiscrs : Nat) (lhss : List AltLHS) : MetaM MatcherResult :=
forallBoundedTelescope matchType numDiscrs fun majors matchTypeBody => do
checkNumPatterns majors lhss;
/- We generate an matcher that can eliminate using different motives with different universe levels.
`uElim` is the universe level the caller wants to eliminate to.
If it is not levelZero, we create a matcher that can eliminate in any universe level.
This is useful for implementing `MatcherApp.addArg` because it may have to change the universe level. -/
uElim β getLevel matchTypeBody;
uElimGen β if uElim == levelZero then pure levelZero else mkFreshLevelMVar;
motiveType β mkForallFVars majors (mkSort uElimGen);
withLocalDeclD `motive motiveType fun motive => do
trace! `Meta.Match.debug ("motiveType: " ++ motiveType);
let mvarType := mkAppN motive majors;
trace! `Meta.Match.debug ("target: " ++ mvarType);
withAlts motive lhss fun alts minors => do
mvar β mkFreshExprMVar mvarType;
let examples := majors.toList.map fun major => Example.var major.fvarId!;
(_, s) β (process { mvarId := mvar.mvarId!, vars := majors.toList, alts := alts, examples := examples }).run {};
let args := #[motive] ++ majors ++ minors.map Prod.fst;
type β mkForallFVars args mvarType;
val β mkLambdaFVars args mvar;
trace! `Meta.Match.debug ("matcher value: " ++ val ++ "\ntype: " ++ type);
matcher β mkAuxDefinition matcherName type val;
trace! `Meta.Match.debug ("matcher levels: " ++ toString matcher.getAppFn.constLevels! ++ ", uElim: " ++ toString uElimGen);
uElimPos? β getUElimPos? matcher.getAppFn.constLevels! uElimGen;
isLevelDefEq uElimGen uElim;
addMatcherInfo matcherName { numParams := matcher.getAppNumArgs, numDiscrs := numDiscrs, altNumParams := minors.map Prod.snd, uElimPos? := uElimPos? };
setInlineAttribute matcherName;
trace! `Meta.Match.debug ("matcher: " ++ matcher);
let unusedAltIdxs : List Nat := lhss.length.fold
(fun i r => if s.used.contains i then r else i::r)
[];
pure { matcher := matcher, counterExamples := s.counterExamples, unusedAltIdxs := unusedAltIdxs.reverse }
end Match
export Match (MatcherInfo)
def getMatcherInfo? (declName : Name) : MetaM (Option MatcherInfo) := do
env β getEnv;
pure $ Match.Extension.getMatcherInfo? env declName
def isMatcher (declName : Name) : MetaM Bool := do
info? β getMatcherInfo? declName;
pure info?.isSome
structure MatcherApp :=
(matcherName : Name)
(matcherLevels : Array Level)
(uElimPos? : Option Nat)
(params : Array Expr)
(motive : Expr)
(discrs : Array Expr)
(altNumParams : Array Nat)
(alts : Array Expr)
(remaining : Array Expr)
def matchMatcherApp? (e : Expr) : MetaM (Option MatcherApp) :=
match e.getAppFn with
| Expr.const declName declLevels _ => do
some info β getMatcherInfo? declName | pure none;
let args := e.getAppArgs;
if args.size < info.numParams + 1 + info.numDiscrs + info.numAlts then pure none
else
pure $ some {
matcherName := declName,
matcherLevels := declLevels.toArray,
uElimPos? := info.uElimPos?,
params := args.extract 0 info.numParams,
motive := args.get! info.numParams,
discrs := args.extract (info.numParams + 1) (info.numParams + 1 + info.numDiscrs),
altNumParams := info.altNumParams,
alts := args.extract (info.numParams + 1 + info.numDiscrs) (info.numParams + 1 + info.numDiscrs + info.numAlts),
remaining := args.extract (info.numParams + 1 + info.numDiscrs + info.numAlts) args.size
}
| _ => pure none
def MatcherApp.toExpr (matcherApp : MatcherApp) : Expr :=
let result := mkAppN (mkConst matcherApp.matcherName matcherApp.matcherLevels.toList) matcherApp.params;
let result := mkApp result matcherApp.motive;
let result := mkAppN result matcherApp.discrs;
let result := mkAppN result matcherApp.alts;
mkAppN result matcherApp.remaining
/- Auxiliary function for MatcherApp.addArg -/
private partial def updateAlts : Expr β Array Nat β Array Expr β Nat β MetaM (Array Nat Γ Array Expr)
| typeNew, altNumParams, alts, i =>
if h : i < alts.size then do
let alt := alts.get β¨i, hβ©;
let numParams := altNumParams.get! i;
typeNew β whnfD typeNew;
match typeNew with
| Expr.forallE n d b _ => do
alt β forallBoundedTelescope d (some numParams) fun xs d => do {
alt β catch (instantiateLambda alt xs) (fun _ => throwError "unexpected matcher application, insufficient number of parameters in alternative");
forallBoundedTelescope d (some 1) fun x d => do
alt β mkLambdaFVars x alt; -- x is the new argument we are adding to the alternative
alt β mkLambdaFVars xs alt;
pure alt
};
updateAlts (b.instantiate1 alt) (altNumParams.set! i (numParams+1)) (alts.set β¨i, hβ© alt) (i+1)
| _ => throwError "unexpected type at MatcherApp.addArg"
else
pure (altNumParams, alts)
/- Given
- matcherApp `match_i As (fun xs => motive[xs]) discrs (fun ys_1 => (alt_1 : motive (C_1[ys_1])) ... (fun ys_n => (alt_n : motive (C_n[ys_n]) remaining`, and
- expression `e : B[discrs]`,
Construct the term
`match_i As (fun xs => B[xs] -> motive[xs]) discrs (fun ys_1 (y : B[C_1[ys_1]]) => alt_1) ... (fun ys_n (y : B[C_n[ys_n]]) => alt_n) e remaining`, and
We use `kabstract` to abstract the discriminants from `B[discrs]`.
This method assumes
- the `matcherApp.motive` is a lambda abstraction where `xs.size == discrs.size`
- each alternative is a lambda abstraction where `ys_i.size == matcherApp.altNumParams[i]`
-/
def MatcherApp.addArg (matcherApp : MatcherApp) (e : Expr) : MetaM MatcherApp :=
lambdaTelescope matcherApp.motive fun motiveArgs motiveBody => do
unless (motiveArgs.size == matcherApp.discrs.size) $
-- This error can only happen if someone implemented a transformation that rewrites the motive created by `mkMatcher`.
throwError ("unexpected matcher application, motive must be lambda expression with #" ++ toString matcherApp.discrs.size ++ " arguments");
eType β inferType e;
eTypeAbst β matcherApp.discrs.size.foldRevM
(fun i eTypeAbst => do
let motiveArg := motiveArgs.get! i;
let discr := matcherApp.discrs.get! i;
eTypeAbst β kabstract eTypeAbst discr;
pure $ eTypeAbst.instantiate1 motiveArg)
eType;
motiveBody β mkArrow eTypeAbst motiveBody;
matcherLevels β match matcherApp.uElimPos? with
| none => pure matcherApp.matcherLevels
| some pos => do {
uElim β getLevel motiveBody;
pure $ matcherApp.matcherLevels.set! pos uElim
};
motive β mkLambdaFVars motiveArgs motiveBody;
-- Construct `aux` `match_i As (fun xs => B[xs] β motive[xs]) discrs`, and infer its type `auxType`.
-- We use `auxType` to infer the type `B[C_i[ys_i]]` of the new argument in each alternative.
let aux := mkAppN (mkConst matcherApp.matcherName matcherLevels.toList) matcherApp.params;
let aux := mkApp aux motive;
let aux := mkAppN aux matcherApp.discrs;
trace! `Meta.debug aux;
check aux;
unlessM (isTypeCorrect aux) $
throwError "failed to add argument to matcher application, type error when constructing the new motive";
auxType β inferType aux;
(altNumParams, alts) β updateAlts auxType matcherApp.altNumParams matcherApp.alts 0;
pure { matcherApp with
matcherLevels := matcherLevels,
motive := motive,
alts := alts,
altNumParams := altNumParams,
remaining := #[e] ++ matcherApp.remaining }
@[init] private def regTraceClasses : IO Unit := do
registerTraceClass `Meta.Match.match;
registerTraceClass `Meta.Match.debug;
registerTraceClass `Meta.Match.unify;
pure ()
end Meta
end Lean
|
b3b58d06de40b894a531adefd2fc4ad102c039a3 | 3f7026ea8bef0825ca0339a275c03b911baef64d | /src/category_theory/monoidal/category.lean | 4a7560333ddfafbc46049ecc6d2b557fe9cc70ce | [
"Apache-2.0"
] | permissive | rspencer01/mathlib | b1e3afa5c121362ef0881012cc116513ab09f18c | c7d36292c6b9234dc40143c16288932ae38fdc12 | refs/heads/master | 1,595,010,346,708 | 1,567,511,503,000 | 1,567,511,503,000 | 206,071,681 | 0 | 0 | Apache-2.0 | 1,567,513,643,000 | 1,567,513,643,000 | null | UTF-8 | Lean | false | false | 16,316 | lean | /-
Copyright (c) 2018 Michael Jendrusch. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Michael Jendrusch, Scott Morrison
-/
import category_theory.products
import category_theory.natural_isomorphism
import tactic.basic
import tactic.slice
open category_theory
universes v u
open category_theory
open category_theory.category
open category_theory.iso
namespace category_theory
/--
In a monoidal category, we can take the tensor product of objects, `X β Y` and of morphisms `f β g`.
Tensor product does not need to be strictly associative on objects, but there is a
specified associator, `Ξ±_ X Y Z : (X β Y) β Z β
X β (Y β Z)`. There is a tensor unit `π_ C`,
with specified left and right unitor isomorphisms `Ξ»_ X : π_ C β X β
X` and `Ο_ X : X β π_ C β
X`.
These associators and unitors satisfy the pentagon and triangle equations.
-/
class monoidal_category (C : Type u) [π : category.{v} C] :=
-- curried tensor product of objects:
(tensor_obj : C β C β C)
(infixr ` β `:70 := tensor_obj) -- This notation is only temporary
-- curried tensor product of morphisms:
(tensor_hom :
Ξ {Xβ Yβ Xβ Yβ : C}, (Xβ βΆ Yβ) β (Xβ βΆ Yβ) β ((Xβ β Xβ) βΆ (Yβ β Yβ)))
(infixr ` β' `:69 := tensor_hom) -- This notation is only temporary
-- tensor product laws:
(tensor_id' :
β (Xβ Xβ : C), (π Xβ) β' (π Xβ) = π (Xβ β Xβ) . obviously)
(tensor_comp' :
β {Xβ Yβ Zβ Xβ Yβ Zβ : C} (fβ : Xβ βΆ Yβ) (fβ : Xβ βΆ Yβ) (gβ : Yβ βΆ Zβ) (gβ : Yβ βΆ Zβ),
(fβ β« gβ) β' (fβ β« gβ) = (fβ β' fβ) β« (gβ β' gβ) . obviously)
-- tensor unit:
(tensor_unit : C)
(notation `π_` := tensor_unit)
-- associator:
(associator :
Ξ X Y Z : C, (X β Y) β Z β
X β (Y β Z))
(notation `Ξ±_` := associator)
(associator_naturality' :
β {Xβ Xβ Xβ Yβ Yβ Yβ : C} (fβ : Xβ βΆ Yβ) (fβ : Xβ βΆ Yβ) (fβ : Xβ βΆ Yβ),
((fβ β' fβ) β' fβ) β« (Ξ±_ Yβ Yβ Yβ).hom = (Ξ±_ Xβ Xβ Xβ).hom β« (fβ β' (fβ β' fβ)) . obviously)
-- left unitor:
(left_unitor : Ξ X : C, π_ β X β
X)
(notation `Ξ»_` := left_unitor)
(left_unitor_naturality' :
β {X Y : C} (f : X βΆ Y), ((π π_) β' f) β« (Ξ»_ Y).hom = (Ξ»_ X).hom β« f . obviously)
-- right unitor:
(right_unitor : Ξ X : C, X β π_ β
X)
(notation `Ο_` := right_unitor)
(right_unitor_naturality' :
β {X Y : C} (f : X βΆ Y), (f β' (π π_)) β« (Ο_ Y).hom = (Ο_ X).hom β« f . obviously)
-- pentagon identity:
(pentagon' : β W X Y Z : C,
((Ξ±_ W X Y).hom β' (π Z)) β« (Ξ±_ W (X β Y) Z).hom β« ((π W) β' (Ξ±_ X Y Z).hom)
= (Ξ±_ (W β X) Y Z).hom β« (Ξ±_ W X (Y β Z)).hom . obviously)
-- triangle identity:
(triangle' :
β X Y : C, (Ξ±_ X π_ Y).hom β« ((π X) β' (Ξ»_ Y).hom) = (Ο_ X).hom β' (π Y) . obviously)
restate_axiom monoidal_category.tensor_id'
attribute [simp] monoidal_category.tensor_id
restate_axiom monoidal_category.tensor_comp'
attribute [simp] monoidal_category.tensor_comp
restate_axiom monoidal_category.associator_naturality'
restate_axiom monoidal_category.left_unitor_naturality'
restate_axiom monoidal_category.right_unitor_naturality'
restate_axiom monoidal_category.pentagon'
restate_axiom monoidal_category.triangle'
attribute [simp] monoidal_category.triangle
open monoidal_category
infixr ` β `:70 := tensor_obj
infixr ` β `:70 := tensor_hom
notation `π_` := tensor_unit
notation `Ξ±_` := associator
notation `Ξ»_` := left_unitor
notation `Ο_` := right_unitor
/-- The tensor product of two isomorphisms is an isomorphism. -/
def tensor_iso {C : Type u} {X Y X' Y' : C} [category.{v} C] [monoidal_category.{v} C] (f : X β
Y) (g : X' β
Y') :
X β X' β
Y β Y' :=
{ hom := f.hom β g.hom,
inv := f.inv β g.inv,
hom_inv_id' := by rw [βtensor_comp, iso.hom_inv_id, iso.hom_inv_id, βtensor_id],
inv_hom_id' := by rw [βtensor_comp, iso.inv_hom_id, iso.inv_hom_id, βtensor_id] }
infixr ` β `:70 := tensor_iso
namespace monoidal_category
section
variables {C : Type u} [category.{v} C] [π : monoidal_category.{v} C]
include π
instance tensor_is_iso {W X Y Z : C} (f : W βΆ X) [is_iso f] (g : Y βΆ Z) [is_iso g] : is_iso (f β g) :=
{ ..(as_iso f β as_iso g) }
@[simp] lemma inv_tensor {W X Y Z : C} (f : W βΆ X) [is_iso f] (g : Y βΆ Z) [is_iso g] :
inv (f β g) = inv f β inv g := rfl
variables {U V W X Y Z : C}
-- When `rewrite_search` lands, add @[search] attributes to
-- monoidal_category.tensor_id monoidal_category.tensor_comp monoidal_category.associator_naturality
-- monoidal_category.left_unitor_naturality monoidal_category.right_unitor_naturality
-- monoidal_category.pentagon monoidal_category.triangle
-- tensor_comp_id tensor_id_comp comp_id_tensor_tensor_id
-- triangle_assoc_comp_left triangle_assoc_comp_right triangle_assoc_comp_left_inv triangle_assoc_comp_right_inv
-- left_unitor_tensor left_unitor_tensor_inv
-- right_unitor_tensor right_unitor_tensor_inv
-- pentagon_inv
-- associator_inv_naturality
-- left_unitor_inv_naturality
-- right_unitor_inv_naturality
@[simp] lemma comp_tensor_id (f : W βΆ X) (g : X βΆ Y) :
(f β« g) β (π Z) = (f β (π Z)) β« (g β (π Z)) :=
by { rw βtensor_comp, simp }
@[simp] lemma id_tensor_comp (f : W βΆ X) (g : X βΆ Y) :
(π Z) β (f β« g) = (π Z β f) β« (π Z β g) :=
by { rw βtensor_comp, simp }
@[simp] lemma id_tensor_comp_tensor_id (f : W βΆ X) (g : Y βΆ Z) :
((π Y) β f) β« (g β (π X)) = g β f :=
by { rw [βtensor_comp], simp }
@[simp] lemma tensor_id_comp_id_tensor (f : W βΆ X) (g : Y βΆ Z) :
(g β (π W)) β« ((π Z) β f) = g β f :=
by { rw [βtensor_comp], simp }
lemma left_unitor_inv_naturality {X X' : C} (f : X βΆ X') :
f β« (Ξ»_ X').inv = (Ξ»_ X).inv β« (π _ β f) :=
begin
apply (cancel_mono (Ξ»_ X').hom).1,
simp only [assoc, comp_id, iso.inv_hom_id],
rw [left_unitor_naturality, βcategory.assoc, iso.inv_hom_id, category.id_comp]
end
lemma right_unitor_inv_naturality {X X' : C} (f : X βΆ X') :
f β« (Ο_ X').inv = (Ο_ X).inv β« (f β π _) :=
begin
apply (cancel_mono (Ο_ X').hom).1,
simp only [assoc, comp_id, iso.inv_hom_id],
rw [right_unitor_naturality, βcategory.assoc, iso.inv_hom_id, category.id_comp]
end
@[simp] lemma tensor_left_iff
{X Y : C} (f g : X βΆ Y) :
((π (π_ C)) β f = (π (π_ C)) β g) β (f = g) :=
begin
split,
{ intro h,
have h' := congr_arg (Ξ» k, (Ξ»_ _).inv β« k) h,
dsimp at h',
rw [βleft_unitor_inv_naturality, βleft_unitor_inv_naturality] at h',
exact (cancel_mono _).1 h', },
{ intro h, subst h, }
end
@[simp] lemma tensor_right_iff
{X Y : C} (f g : X βΆ Y) :
(f β (π (π_ C)) = g β (π (π_ C))) β (f = g) :=
begin
split,
{ intro h,
have h' := congr_arg (Ξ» k, (Ο_ _).inv β« k) h,
dsimp at h',
rw [βright_unitor_inv_naturality, βright_unitor_inv_naturality] at h',
exact (cancel_mono _).1 h' },
{ intro h, subst h, }
end
-- We now prove:
-- ((Ξ±_ (π_ C) X Y).hom) β«
-- ((Ξ»_ (X β Y)).hom)
-- = ((Ξ»_ X).hom β (π Y))
-- (and the corresponding fact for right unitors)
-- following the proof on nLab:
-- Lemma 2.2 at https://ncatlab.org/nlab/revision/monoidal+category/115
lemma left_unitor_product_aux_perimeter (X Y : C) :
((Ξ±_ (π_ C) (π_ C) X).hom β (π Y)) β«
(Ξ±_ (π_ C) ((π_ C) β X) Y).hom β«
((π (π_ C)) β (Ξ±_ (π_ C) X Y).hom) β«
((π (π_ C)) β (Ξ»_ (X β Y)).hom)
= (((Ο_ (π_ C)).hom β (π X)) β (π Y)) β«
(Ξ±_ (π_ C) X Y).hom :=
begin
conv_lhs { congr, skip, rw [βcategory.assoc] },
rw [βcategory.assoc, monoidal_category.pentagon, associator_naturality, tensor_id,
βmonoidal_category.triangle, βcategory.assoc]
end
lemma left_unitor_product_aux_triangle (X Y : C) :
((Ξ±_ (π_ C) (π_ C) X).hom β (π Y)) β«
(((π (π_ C)) β (Ξ»_ X).hom) β (π Y))
= ((Ο_ (π_ C)).hom β (π X)) β (π Y) :=
by rw [βcomp_tensor_id, βmonoidal_category.triangle]
lemma left_unitor_product_aux_square (X Y : C) :
(Ξ±_ (π_ C) ((π_ C) β X) Y).hom β«
((π (π_ C)) β (Ξ»_ X).hom β (π Y))
= (((π (π_ C)) β (Ξ»_ X).hom) β (π Y)) β«
(Ξ±_ (π_ C) X Y).hom :=
by rw associator_naturality
lemma left_unitor_product_aux (X Y : C) :
((π (π_ C)) β (Ξ±_ (π_ C) X Y).hom) β«
((π (π_ C)) β (Ξ»_ (X β Y)).hom)
= (π (π_ C)) β ((Ξ»_ X).hom β (π Y)) :=
begin
rw β(cancel_epi (Ξ±_ (π_ C) ((π_ C) β X) Y).hom),
rw left_unitor_product_aux_square,
rw β(cancel_epi ((Ξ±_ (π_ C) (π_ C) X).hom β (π Y))),
slice_rhs 1 2 { rw left_unitor_product_aux_triangle },
conv_lhs { rw [left_unitor_product_aux_perimeter] }
end
lemma right_unitor_product_aux_perimeter (X Y : C) :
((Ξ±_ X Y (π_ C)).hom β (π (π_ C))) β«
(Ξ±_ X (Y β (π_ C)) (π_ C)).hom β«
((π X) β (Ξ±_ Y (π_ C) (π_ C)).hom) β«
((π X) β (π Y) β (Ξ»_ (π_ C)).hom)
= ((Ο_ (X β Y)).hom β (π (π_ C))) β«
(Ξ±_ X Y (π_ C)).hom :=
begin
transitivity (((Ξ±_ X Y _).hom β π _) β« (Ξ±_ X _ _).hom β«
(π X β (Ξ±_ Y _ _).hom)) β«
(π X β π Y β (Ξ»_ _).hom),
{ conv_lhs { congr, skip, rw [βcategory.assoc] },
conv_rhs { rw [category.assoc] } },
{ conv_lhs { congr, rw [monoidal_category.pentagon] },
conv_rhs { congr, rw [βmonoidal_category.triangle] },
conv_rhs { rw [category.assoc] },
conv_rhs { congr, skip, congr, congr, rw [βtensor_id] },
conv_rhs { congr, skip, rw [associator_naturality] },
conv_rhs { rw [βcategory.assoc] } }
end
lemma right_unitor_product_aux_triangle (X Y : C) :
((π X) β (Ξ±_ Y (π_ C) (π_ C)).hom) β«
((π X) β (π Y) β (Ξ»_ (π_ C)).hom)
= (π X) β (Ο_ Y).hom β (π (π_ C)) :=
by rw [βid_tensor_comp, βmonoidal_category.triangle]
lemma right_unitor_product_aux_square (X Y : C) :
(Ξ±_ X (Y β (π_ C)) (π_ C)).hom β«
((π X) β (Ο_ Y).hom β (π (π_ C)))
= (((π X) β (Ο_ Y).hom) β (π (π_ C))) β«
(Ξ±_ X Y (π_ C)).hom :=
by rw [associator_naturality]
lemma right_unitor_product_aux (X Y : C) :
((Ξ±_ X Y (π_ C)).hom β (π (π_ C))) β«
(((π X) β (Ο_ Y).hom) β (π (π_ C)))
= ((Ο_ (X β Y)).hom β (π (π_ C))) :=
begin
rw β(cancel_mono (Ξ±_ X Y (π_ C)).hom),
slice_lhs 2 3 { rw βright_unitor_product_aux_square },
rw [βright_unitor_product_aux_triangle, βright_unitor_product_aux_perimeter],
end
-- See Proposition 2.2.4 of http://www-math.mit.edu/~etingof/egnobookfinal.pdf
@[simp] lemma left_unitor_tensor (X Y : C) :
((Ξ±_ (π_ C) X Y).hom) β« ((Ξ»_ (X β Y)).hom) =
((Ξ»_ X).hom β (π Y)) :=
by rw [βtensor_left_iff, id_tensor_comp, left_unitor_product_aux]
@[simp] lemma left_unitor_tensor_inv (X Y : C) :
((Ξ»_ (X β Y)).inv) β« ((Ξ±_ (π_ C) X Y).inv) =
((Ξ»_ X).inv β (π Y)) :=
eq_of_inv_eq_inv (by simp)
@[simp] lemma right_unitor_tensor (X Y : C) :
((Ξ±_ X Y (π_ C)).hom) β« ((π X) β (Ο_ Y).hom) =
((Ο_ (X β Y)).hom) :=
by rw [βtensor_right_iff, comp_tensor_id, right_unitor_product_aux]
@[simp] lemma right_unitor_tensor_inv (X Y : C) :
((π X) β (Ο_ Y).inv) β« ((Ξ±_ X Y (π_ C)).inv) =
((Ο_ (X β Y)).inv) :=
eq_of_inv_eq_inv (by simp)
lemma associator_inv_naturality {X Y Z X' Y' Z' : C} (f : X βΆ X') (g : Y βΆ Y') (h : Z βΆ Z') :
(f β (g β h)) β« (Ξ±_ X' Y' Z').inv = (Ξ±_ X Y Z).inv β« ((f β g) β h) :=
begin
apply (cancel_mono (Ξ±_ X' Y' Z').hom).1,
simp only [assoc, comp_id, iso.inv_hom_id],
rw [associator_naturality, βcategory.assoc, iso.inv_hom_id, category.id_comp]
end
lemma pentagon_inv (W X Y Z : C) :
((π W) β (Ξ±_ X Y Z).inv) β« (Ξ±_ W (X β Y) Z).inv β« ((Ξ±_ W X Y).inv β (π Z))
= (Ξ±_ W X (Y β Z)).inv β« (Ξ±_ (W β X) Y Z).inv :=
begin
apply category_theory.eq_of_inv_eq_inv,
dsimp,
rw [category.assoc, monoidal_category.pentagon]
end
@[simp] lemma triangle_assoc_comp_left (X Y : C) :
(Ξ±_ X (π_ C) Y).hom β« ((π X) β (Ξ»_ Y).hom) = (Ο_ X).hom β π Y :=
monoidal_category.triangle C X Y
@[simp] lemma triangle_assoc_comp_right (X Y : C) :
(Ξ±_ X (π_ C) Y).inv β« ((Ο_ X).hom β π Y) = ((π X) β (Ξ»_ Y).hom) :=
by rw [βtriangle_assoc_comp_left, βcategory.assoc, iso.inv_hom_id, category.id_comp]
@[simp] lemma triangle_assoc_comp_right_inv (X Y : C) :
((Ο_ X).inv β π Y) β« (Ξ±_ X (π_ C) Y).hom = ((π X) β (Ξ»_ Y).inv) :=
begin
apply (cancel_mono (π X β (Ξ»_ Y).hom)).1,
simp only [assoc, triangle_assoc_comp_left],
rw [βcomp_tensor_id, iso.inv_hom_id, βid_tensor_comp, iso.inv_hom_id]
end
@[simp] lemma triangle_assoc_comp_left_inv (X Y : C) :
((π X) β (Ξ»_ Y).inv) β« (Ξ±_ X (π_ C) Y).inv = ((Ο_ X).inv β π Y) :=
begin
apply (cancel_mono ((Ο_ X).hom β π Y)).1,
simp only [triangle_assoc_comp_right, assoc],
rw [βid_tensor_comp, iso.inv_hom_id, βcomp_tensor_id, iso.inv_hom_id]
end
end
section
-- In order to be able to describe the tensor product as a functor, we
-- need to be up in at least `Type 0` for both objects and morphisms,
-- so that we can construct products.
variables (C : Type u) [category.{v+1} C] [π : monoidal_category.{v+1} C]
include π
/-- The tensor product expressed as a functor. -/
def tensor : (C Γ C) β₯€ C :=
{ obj := Ξ» X, X.1 β X.2,
map := Ξ» {X Y : C Γ C} (f : X βΆ Y), f.1 β f.2 }
/-- The left-associated triple tensor product as a functor. -/
def left_assoc_tensor : (C Γ C Γ C) β₯€ C :=
{ obj := Ξ» X, (X.1 β X.2.1) β X.2.2,
map := Ξ» {X Y : C Γ C Γ C} (f : X βΆ Y), (f.1 β f.2.1) β f.2.2 }
@[simp] lemma left_assoc_tensor_obj (X) :
(left_assoc_tensor C).obj X = (X.1 β X.2.1) β X.2.2 := rfl
@[simp] lemma left_assoc_tensor_map {X Y} (f : X βΆ Y) :
(left_assoc_tensor C).map f = (f.1 β f.2.1) β f.2.2 := rfl
/-- The right-associated triple tensor product as a functor. -/
def right_assoc_tensor : (C Γ C Γ C) β₯€ C :=
{ obj := Ξ» X, X.1 β (X.2.1 β X.2.2),
map := Ξ» {X Y : C Γ C Γ C} (f : X βΆ Y), f.1 β (f.2.1 β f.2.2) }
@[simp] lemma right_assoc_tensor_obj (X) :
(right_assoc_tensor C).obj X = X.1 β (X.2.1 β X.2.2) := rfl
@[simp] lemma right_assoc_tensor_map {X Y} (f : X βΆ Y) :
(right_assoc_tensor C).map f = f.1 β (f.2.1 β f.2.2) := rfl
/-- The functor `Ξ» X, π_ C β X`. -/
def tensor_unit_left : C β₯€ C :=
{ obj := Ξ» X, π_ C β X,
map := Ξ» {X Y : C} (f : X βΆ Y), (π (π_ C)) β f }
/-- The functor `Ξ» X, X β π_ C`. -/
def tensor_unit_right : C β₯€ C :=
{ obj := Ξ» X, X β π_ C,
map := Ξ» {X Y : C} (f : X βΆ Y), f β (π (π_ C)) }
-- We can express the associator and the unitors, given componentwise above,
-- as natural isomorphisms.
/-- The associator as a natural isomorphism. -/
def associator_nat_iso :
left_assoc_tensor C β
right_assoc_tensor C :=
nat_iso.of_components
(by { intros, apply monoidal_category.associator })
(by { intros, apply monoidal_category.associator_naturality })
/-- The left unitor as a natural isomorphism. -/
def left_unitor_nat_iso :
tensor_unit_left C β
π C :=
nat_iso.of_components
(by { intros, apply monoidal_category.left_unitor })
(by { intros, apply monoidal_category.left_unitor_naturality })
/-- The right unitor as a natural isomorphism. -/
def right_unitor_nat_iso :
tensor_unit_right C β
π C :=
nat_iso.of_components
(by { intros, apply monoidal_category.right_unitor })
(by { intros, apply monoidal_category.right_unitor_naturality })
end
end monoidal_category
end category_theory
|
e35999855d01ebf3d77b5262fe7f8b41fc0c6779 | e514e8b939af519a1d5e9b30a850769d058df4e9 | /src/tactic/rewrite_search/tracer/graph.lean | 5f31f10085494bd8187f85921af3ea3061b00ec0 | [] | no_license | semorrison/lean-rewrite-search | dca317c5a52e170fb6ffc87c5ab767afb5e3e51a | e804b8f2753366b8957be839908230ee73f9e89f | refs/heads/master | 1,624,051,754,485 | 1,614,160,817,000 | 1,614,160,817,000 | 162,660,605 | 0 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 5,289 | lean | import tactic.iconfig
import tactic.rewrite_search.core
import tactic.rewrite_search.module
import system.io
open tactic.rewrite_search
namespace tactic.rewrite_search.tracer.graph
open tactic
open io.process.stdio
def SUCCESS_CHAR : string := "S"
def ERROR_CHAR : string := "E"
def SEARCH_PATHS : list string := [
"_target/deps/lean-rewrite-search/res/graph_tracer",
"res/graph_tracer"
]
def get_app_path (dir : string) (app : string) : string :=
dir ++ "/" ++ app ++ ".py"
def args (dir : string) (app : string) : io.process.spawn_args := {
cmd := "python3",
args := [get_app_path dir app],
stdin := piped,
stdout := piped,
stderr := inherit,
env := [
("PYTHONPATH", some (dir ++ "/pygraphvis.zip/pygraphvis")),
("PYTHONIOENCODING", "utf-8")
],
}
structure visualiser :=
(proc : io.proc.child)
meta def visualiser.publish (v : visualiser) (f : format) : tactic unit :=
tactic.unsafe_run_io $ do
io.fs.write v.proc.stdin (f.to_string.to_char_buffer.push_back '\n'),
io.fs.flush v.proc.stdin
meta def visualiser.pause (v : visualiser) : tactic unit :=
tactic.unsafe_run_io (do io.fs.read v.proc.stdout 1, return ())
def file_exists (path : string) : io bool := do
c β io.proc.spawn { cmd := "test", args := ["-f", path] },
retval β io.proc.wait c,
return (retval = 0)
inductive spawn_result
| success : io.proc.child β spawn_result -- Client launched and the client reported success status
| abort : string β spawn_result -- Client launched and we got a bad response code
| failure -- Could not launch client
| missing -- The script we tried to launch does't exist
meta def read_until_nl (h : io.handle) : io string := do
c β io.fs.read h 1,
match c.to_list with
| ['\n'] := return ""
| [c] := do r β read_until_nl, return (c.to_string ++ r)
| _ := return ""
end
meta def try_launch_with_path (path : string) : io spawn_result := do
ex β file_exists (get_app_path path "client"),
if ex then do
c β io.proc.spawn (args path "client"),
buff β io.fs.read c.stdout 1,
str β pure buff.to_string,
if str = SUCCESS_CHAR then
return (spawn_result.success c)
else if str = ERROR_CHAR then do
reason β read_until_nl c.stdout,
return (spawn_result.abort reason)
else if str = "" then
return spawn_result.failure
else
return $ spawn_result.abort (format!"bug: unknown client status character \"{str}\"").to_string
else
return spawn_result.missing
meta def try_launch_with_paths : list string β io spawn_result
| [] := return spawn_result.failure
| (p :: rest) := do
sr β try_launch_with_path p,
match sr with
| spawn_result.missing := try_launch_with_paths rest
| _ := return sr
end
meta def diagnose_launch_failure : io string := do
c β io.proc.spawn { cmd := "python3", args := ["--version"], stdin := piped, stdout := piped, stderr := piped },
r β io.proc.wait c,
match r with
| 255 := return "python3 is missing, and the graph visualiser requires it. Please install python3."
| 0 := return "bug: python3 present but could not launch client!"
| ret := return (format!"bug: unexpected return code {ret} during launch failure diagnosis").to_string
end
meta def init : tactic (init_result visualiser) :=
do c β tactic.unsafe_run_io (try_launch_with_paths SEARCH_PATHS),
match c with
| spawn_result.success c :=
let vs : visualiser := β¨cβ© in do
vs.publish "S", init_result.pure vs
| spawn_result.abort reason :=
init_result.fail ("Abort! " ++ reason)
| spawn_result.failure := do
reason β tactic.unsafe_run_io diagnose_launch_failure,
init_result.fail ("Failure! " ++ reason)
| spawn_result.missing :=
init_result.fail "Error! bug: could not determine client location"
end
meta def publish_vertex (vs : visualiser) (v : vertex) : tactic unit :=
vs.publish format!"V|{v.id.to_string}|{v.s.to_string}|{v.id}"
meta def publish_edge (vs : visualiser) (e : edge) : tactic unit :=
vs.publish format!"E|{e.f.to_string}|{e.t.to_string}"
meta def publish_visited (vs : visualiser) (v : vertex) : tactic unit :=
vs.publish format!"B|{v.id.to_string}"
meta def publish_finished (vs : visualiser) (es : list edge) : tactic unit :=
do es.mmap' (Ξ» e : edge, vs.publish format!"F|{e.f.to_string}|{e.t.to_string}"),
vs.publish format!"D"
meta def dump (vs : visualiser) (str : string) : tactic unit :=
vs.publish (str ++ "\n")
meta def pause (vs : visualiser) : tactic unit :=
vs.pause
end tactic.rewrite_search.tracer.graph
namespace tactic.rewrite_search.tracer
open tactic.rewrite_search.tracer.graph
meta def graph_cnst := Ξ» Ξ± Ξ² Ξ³,
tracer.mk Ξ± Ξ² Ξ³ graph.init graph.publish_vertex graph.publish_edge graph.publish_visited graph.publish_finished graph.dump graph.pause
meta def graph : tactic expr := generic `tactic.rewrite_search.tracer.graph_cnst
meta def visualiser_cfg (_ : name) : cfgtactic unit :=
iconfig.publish `tracer $ cfgopt.value.pexpr $ expr.const `graph []
iconfig_add rewrite_search [
tracer.graph : custom visualiser_cfg
visualiser : custom visualiser_cfg
]
end tactic.rewrite_search.tracer
|
ddeb7b40737a182f3fb0c54d78340ad2c2bde8e2 | 9be442d9ec2fcf442516ed6e9e1660aa9071b7bd | /src/Lean/Elab/Util.lean | c78e9d2cc62b6a3b510a1160f73a9dc5515a91c0 | [
"Apache-2.0",
"LLVM-exception",
"NCSA",
"LGPL-3.0-only",
"LicenseRef-scancode-inner-net-2.0",
"BSD-3-Clause",
"LGPL-2.0-or-later",
"Spencer-94",
"LGPL-2.1-or-later",
"HPND",
"LicenseRef-scancode-pcre",
"ISC",
"LGPL-2.1-only",
"LicenseRef-scancode-other-permissive",
"SunPro",
"CMU-Mach"... | permissive | EdAyers/lean4 | 57ac632d6b0789cb91fab2170e8c9e40441221bd | 37ba0df5841bde51dbc2329da81ac23d4f6a4de4 | refs/heads/master | 1,676,463,245,298 | 1,660,619,433,000 | 1,660,619,433,000 | 183,433,437 | 1 | 0 | Apache-2.0 | 1,657,612,672,000 | 1,556,196,574,000 | Lean | UTF-8 | Lean | false | false | 10,277 | 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.Util.Trace
import Lean.Parser.Syntax
import Lean.Parser.Extension
import Lean.KeyedDeclsAttribute
import Lean.Elab.Exception
import Lean.Elab.InfoTree
import Lean.DocString
import Lean.DeclarationRange
import Lean.Compiler.InitAttr
import Lean.Log
namespace Lean
def Syntax.prettyPrint (stx : Syntax) : Format :=
match stx.unsetTrailing.reprint with -- TODO use syntax pretty printer
| some str => format str.toFormat
| none => format stx
def MacroScopesView.format (view : MacroScopesView) (mainModule : Name) : Format :=
Std.format <|
if view.scopes.isEmpty then
view.name
else if view.mainModule == mainModule then
view.scopes.foldl Name.mkNum (view.name ++ view.imported)
else
view.scopes.foldl Name.mkNum (view.name ++ view.imported ++ view.mainModule)
namespace Elab
def expandOptNamedPrio (stx : Syntax) : MacroM Nat :=
if stx.isNone then
return eval_prio default
else match stx[0] with
| `(Parser.Command.namedPrio| (priority := $prio)) => evalPrio prio
| _ => Macro.throwUnsupported
structure MacroStackElem where
before : Syntax
after : Syntax
abbrev MacroStack := List MacroStackElem
/-- If `ref` does not have position information, then try to use macroStack -/
def getBetterRef (ref : Syntax) (macroStack : MacroStack) : Syntax :=
match ref.getPos? with
| some _ => ref
| none =>
match macroStack.find? (Β·.before.getPos? != none) with
| some elem => elem.before
| none => ref
register_builtin_option pp.macroStack : Bool := {
defValue := false
group := "pp"
descr := "dispaly macro expansion stack"
}
def addMacroStack {m} [Monad m] [MonadOptions m] (msgData : MessageData) (macroStack : MacroStack) : m MessageData := do
if !pp.macroStack.get (β getOptions) then pure msgData else
match macroStack with
| [] => pure msgData
| stack@(top::_) =>
let msgData := msgData ++ Format.line ++ "with resulting expansion" ++ indentD top.after
pure $ stack.foldl
(fun (msgData : MessageData) (elem : MacroStackElem) =>
msgData ++ Format.line ++ "while expanding" ++ indentD elem.before)
msgData
def checkSyntaxNodeKind [Monad m] [MonadEnv m] [MonadError m] (k : Name) : m Name := do
if Parser.isValidSyntaxNodeKind (β getEnv) k then pure k
else throwError "failed"
def checkSyntaxNodeKindAtNamespaces [Monad m] [MonadEnv m] [MonadError m] (k : Name) : Name β m Name
| n@(.str p _) => checkSyntaxNodeKind (n ++ k) <|> checkSyntaxNodeKindAtNamespaces k p
| .anonymous => checkSyntaxNodeKind k
| _ => throwError "failed"
def checkSyntaxNodeKindAtCurrentNamespaces (k : Name) : AttrM Name := do
let ctx β read
checkSyntaxNodeKindAtNamespaces k ctx.currNamespace
def syntaxNodeKindOfAttrParam (defaultParserNamespace : Name) (stx : Syntax) : AttrM SyntaxNodeKind := do
let k β Attribute.Builtin.getId stx
checkSyntaxNodeKindAtCurrentNamespaces k
<|>
checkSyntaxNodeKind (defaultParserNamespace ++ k)
<|>
throwError "invalid syntax node kind '{k}'"
private unsafe def evalSyntaxConstantUnsafe (env : Environment) (opts : Options) (constName : Name) : ExceptT String Id Syntax :=
env.evalConstCheck Syntax opts `Lean.Syntax constName
@[implementedBy evalSyntaxConstantUnsafe]
opaque evalSyntaxConstant (env : Environment) (opts : Options) (constName : Name) : ExceptT String Id Syntax := throw ""
unsafe def mkElabAttribute (Ξ³) (attrDeclName attrBuiltinName attrName : Name) (parserNamespace : Name) (typeName : Name) (kind : String)
: IO (KeyedDeclsAttribute Ξ³) :=
KeyedDeclsAttribute.init {
builtinName := attrBuiltinName
name := attrName
descr := kind ++ " elaborator"
valueTypeName := typeName
evalKey := fun _ stx => do
let kind β syntaxNodeKindOfAttrParam parserNamespace stx
/- Recall that a `SyntaxNodeKind` is often the name of the parser, but this is not always true, and we must check it. -/
if (β getEnv).contains kind && (β getInfoState).enabled then
pushInfoLeaf <| Info.ofTermInfo {
elaborator := .anonymous
lctx := {}
expr := mkConst kind
stx := stx[1]
expectedType? := none
}
return kind
onAdded := fun builtin declName => do
if builtin then
if let some doc β findDocString? (β getEnv) declName then
declareBuiltin (declName ++ `docString) (mkAppN (mkConst ``addBuiltinDocString) #[toExpr declName, toExpr doc])
if let some declRanges β findDeclarationRanges? declName then
declareBuiltin (declName ++ `declRange) (mkAppN (mkConst ``addBuiltinDeclarationRanges) #[toExpr declName, toExpr declRanges])
} attrDeclName
unsafe def mkMacroAttributeUnsafe : IO (KeyedDeclsAttribute Macro) :=
mkElabAttribute Macro `Lean.Elab.macroAttribute `builtinMacro `macro Name.anonymous `Lean.Macro "macro"
@[implementedBy mkMacroAttributeUnsafe]
opaque mkMacroAttribute : IO (KeyedDeclsAttribute Macro)
builtin_initialize macroAttribute : KeyedDeclsAttribute Macro β mkMacroAttribute
/--
Try to expand macro at syntax tree root and return macro declaration name and new syntax if successful.
Return none if all macros threw `Macro.Exception.unsupportedSyntax`.
-/
def expandMacroImpl? (env : Environment) : Syntax β MacroM (Option (Name Γ Except Macro.Exception Syntax)) := fun stx => do
for e in macroAttribute.getEntries env stx.getKind do
try
let stx' β withFreshMacroScope (e.value stx)
return (e.declName, Except.ok stx')
catch
| Macro.Exception.unsupportedSyntax => pure ()
| ex => return (e.declName, Except.error ex)
return none
class MonadMacroAdapter (m : Type β Type) where
getCurrMacroScope : m MacroScope
getNextMacroScope : m MacroScope
setNextMacroScope : MacroScope β m Unit
instance (m n) [MonadLift m n] [MonadMacroAdapter m] : MonadMacroAdapter n := {
getCurrMacroScope := liftM (MonadMacroAdapter.getCurrMacroScope : m _)
getNextMacroScope := liftM (MonadMacroAdapter.getNextMacroScope : m _)
setNextMacroScope := fun s => liftM (MonadMacroAdapter.setNextMacroScope s : m _)
}
def liftMacroM {Ξ±} {m : Type β Type} [Monad m] [MonadMacroAdapter m] [MonadEnv m] [MonadRecDepth m] [MonadError m] [MonadResolveName m] [MonadTrace m] [MonadOptions m] [AddMessageContext m] [MonadLiftT IO m] (x : MacroM Ξ±) : m Ξ± := do
let env β getEnv
let currNamespace β getCurrNamespace
let openDecls β getOpenDecls
let methods := Macro.mkMethods {
-- TODO: record recursive expansions in info tree?
expandMacro? := fun stx => do
match (β expandMacroImpl? env stx) with
| some (_, stx?) => liftExcept stx?
| none => return none
hasDecl := fun declName => return env.contains declName
getCurrNamespace := return currNamespace
resolveNamespace := fun n => return ResolveName.resolveNamespace env currNamespace openDecls n
resolveGlobalName := fun n => return ResolveName.resolveGlobalName env currNamespace openDecls n
}
match x { methods := methods
ref := β getRef
currMacroScope := β MonadMacroAdapter.getCurrMacroScope
mainModule := env.mainModule
currRecDepth := β MonadRecDepth.getRecDepth
maxRecDepth := β MonadRecDepth.getMaxRecDepth
} { macroScope := (β MonadMacroAdapter.getNextMacroScope) } with
| EStateM.Result.error Macro.Exception.unsupportedSyntax _ => throwUnsupportedSyntax
| EStateM.Result.error (Macro.Exception.error ref msg) _ =>
if msg == maxRecDepthErrorMessage then
-- Make sure we can detect exception using `Exception.isMaxRecDepth`
throwMaxRecDepthAt ref
else
throwErrorAt ref msg
| EStateM.Result.ok a s =>
MonadMacroAdapter.setNextMacroScope s.macroScope
s.traceMsgs.reverse.forM fun (clsName, msg) => trace clsName fun _ => msg
return a
@[inline] def adaptMacro {m : Type β Type} [Monad m] [MonadMacroAdapter m] [MonadEnv m] [MonadRecDepth m] [MonadError m] [MonadResolveName m] [MonadTrace m] [MonadOptions m] [AddMessageContext m] [MonadLiftT IO m] (x : Macro) (stx : Syntax) : m Syntax :=
liftMacroM (x stx)
partial def mkUnusedBaseName (baseName : Name) : MacroM Name := do
let currNamespace β Macro.getCurrNamespace
if β Macro.hasDecl (currNamespace ++ baseName) then
let rec loop (idx : Nat) := do
let name := baseName.appendIndexAfter idx
if β Macro.hasDecl (currNamespace ++ name) then
loop (idx+1)
else
return name
loop 1
else
return baseName
def logException [Monad m] [MonadLog m] [AddMessageContext m] [MonadOptions m] [MonadLiftT IO m] (ex : Exception) : m Unit := do
match ex with
| Exception.error ref msg => logErrorAt ref msg
| Exception.internal id _ =>
unless isAbortExceptionId id do
let name β id.getName
logError m!"internal exception: {name}"
def withLogging [Monad m] [MonadLog m] [MonadExcept Exception m] [AddMessageContext m] [MonadOptions m] [MonadLiftT IO m]
(x : m Unit) : m Unit := do
try x catch ex => logException ex
def nestedExceptionToMessageData [Monad m] [MonadLog m] (ex : Exception) : m MessageData := do
let pos β getRefPos
match ex.getRef.getPos? with
| none => return ex.toMessageData
| some exPos =>
if pos == exPos then
return ex.toMessageData
else
let exPosition := (β getFileMap).toPosition exPos
return m!"{exPosition.line}:{exPosition.column} {ex.toMessageData}"
def throwErrorWithNestedErrors [MonadError m] [Monad m] [MonadLog m] (msg : MessageData) (exs : Array Exception) : m Ξ± := do
throwError "{msg}, errors {toMessageList (β exs.mapM fun | ex => nestedExceptionToMessageData ex)}"
builtin_initialize
registerTraceClass `Elab
registerTraceClass `Elab.step
registerTraceClass `Elab.step.result (inherited := true)
end Lean.Elab
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.