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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
4b73741b78388bb2e4378ebf2c9d590065bd6abe | c777c32c8e484e195053731103c5e52af26a25d1 | /src/field_theory/minpoly/field.lean | 4bfcbc603bd40c03689b746edb39e4f10302d53b | [
"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 | 11,772 | lean | /-
Copyright (c) 2019 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Riccardo Brasca, Johan Commelin
-/
import data.polynomial.field_division
import field_theory.minpoly.basic
import ring_theory.algebraic
/-!
# Minimal polynomials on an algebra over a field
This file specializes the theory of minpoly to the setting of field extensions
and derives some well-known properties, amongst which the fact that minimal polynomials
are irreducible, and uniquely determined by their defining property.
-/
open_locale classical polynomial
open polynomial set function minpoly
namespace minpoly
variables {A B : Type*}
variables (A) [field A]
section ring
variables [ring B] [algebra A B] (x : B)
/-- If an element `x` is a root of a nonzero polynomial `p`, then the degree of `p` is at least the
degree of the minimal polynomial of `x`. See also `gcd_domain_degree_le_of_ne_zero` which relaxes
the assumptions on `A` in exchange for stronger assumptions on `B`. -/
lemma degree_le_of_ne_zero
{p : A[X]} (pnz : p ≠ 0) (hp : polynomial.aeval x p = 0) :
degree (minpoly A x) ≤ degree p :=
calc degree (minpoly A x) ≤ degree (p * C (leading_coeff p)⁻¹) :
min A x (monic_mul_leading_coeff_inv pnz) (by simp [hp])
... = degree p : degree_mul_leading_coeff_inv p pnz
lemma ne_zero_of_finite_field_extension (e : B) [finite_dimensional A B] : minpoly A e ≠ 0 :=
minpoly.ne_zero $ is_integral_of_noetherian (is_noetherian.iff_fg.2 infer_instance) _
/-- The minimal polynomial of an element `x` is uniquely characterized by its defining property:
if there is another monic polynomial of minimal degree that has `x` as a root, then this polynomial
is equal to the minimal polynomial of `x`. See also `minpoly.gcd_unique` which relaxes the
assumptions on `A` in exchange for stronger assumptions on `B`. -/
lemma unique {p : A[X]}
(pmonic : p.monic) (hp : polynomial.aeval x p = 0)
(pmin : ∀ q : A[X], q.monic → polynomial.aeval x q = 0 → degree p ≤ degree q) :
p = minpoly A x :=
begin
have hx : is_integral A x := ⟨p, pmonic, hp⟩,
symmetry, apply eq_of_sub_eq_zero,
by_contra hnz,
have := degree_le_of_ne_zero A x hnz (by simp [hp]),
contrapose! this,
apply degree_sub_lt _ (ne_zero hx),
{ rw [(monic hx).leading_coeff, pmonic.leading_coeff] },
{ exact le_antisymm (min A x pmonic hp)
(pmin (minpoly A x) (monic hx) (aeval A x)) }
end
/-- If an element `x` is a root of a polynomial `p`, then the minimal polynomial of `x` divides `p`.
See also `minpoly.gcd_domain_dvd` which relaxes the assumptions on `A` in exchange for stronger
assumptions on `B`. -/
lemma dvd {p : A[X]} (hp : polynomial.aeval x p = 0) : minpoly A x ∣ p :=
begin
by_cases hp0 : p = 0,
{ simp only [hp0, dvd_zero] },
have hx : is_integral A x,
{ rw ← is_algebraic_iff_is_integral, exact ⟨p, hp0, hp⟩ },
rw ← dvd_iff_mod_by_monic_eq_zero (monic hx),
by_contra hnz,
have := degree_le_of_ne_zero A x hnz _,
{ contrapose! this,
exact degree_mod_by_monic_lt _ (monic hx) },
{ rw ← mod_by_monic_add_div p (monic hx) at hp,
simpa using hp }
end
lemma dvd_map_of_is_scalar_tower (A K : Type*) {R : Type*} [comm_ring A] [field K] [comm_ring R]
[algebra A K] [algebra A R] [algebra K R] [is_scalar_tower A K R] (x : R) :
minpoly K x ∣ (minpoly A x).map (algebra_map A K) :=
by { refine minpoly.dvd K x _, rw [aeval_map_algebra_map, minpoly.aeval] }
lemma dvd_map_of_is_scalar_tower' (R : Type*) {S : Type*} (K L : Type*) [comm_ring R]
[comm_ring S] [field K] [comm_ring L] [algebra R S] [algebra R K] [algebra S L] [algebra K L]
[algebra R L] [is_scalar_tower R K L] [is_scalar_tower R S L] (s : S):
minpoly K (algebra_map S L s) ∣ (map (algebra_map R K) (minpoly R s)) :=
begin
apply minpoly.dvd K (algebra_map S L s),
rw [← map_aeval_eq_aeval_map, minpoly.aeval, map_zero],
rw [← is_scalar_tower.algebra_map_eq, ← is_scalar_tower.algebra_map_eq]
end
/-- If `y` is a conjugate of `x` over a field `K`, then it is a conjugate over a subring `R`. -/
lemma aeval_of_is_scalar_tower (R : Type*) {K T U : Type*} [comm_ring R] [field K] [comm_ring T]
[algebra R K] [algebra K T] [algebra R T] [is_scalar_tower R K T]
[comm_semiring U] [algebra K U] [algebra R U] [is_scalar_tower R K U]
(x : T) (y : U)
(hy : polynomial.aeval y (minpoly K x) = 0) : polynomial.aeval y (minpoly R x) = 0 :=
aeval_map_algebra_map K y (minpoly R x) ▸ eval₂_eq_zero_of_dvd_of_eval₂_eq_zero (algebra_map K U)
y (minpoly.dvd_map_of_is_scalar_tower R K x) hy
variables {A x}
theorem eq_of_irreducible_of_monic
[nontrivial B] {p : A[X]} (hp1 : _root_.irreducible p)
(hp2 : polynomial.aeval x p = 0) (hp3 : p.monic) : p = minpoly A x :=
let ⟨q, hq⟩ := dvd A x hp2 in
eq_of_monic_of_associated hp3 (monic ⟨p, ⟨hp3, hp2⟩⟩) $
mul_one (minpoly A x) ▸ hq.symm ▸ associated.mul_left _ $
associated_one_iff_is_unit.2 $ (hp1.is_unit_or_is_unit hq).resolve_left $ not_is_unit A x
lemma eq_of_irreducible [nontrivial B] {p : A[X]}
(hp1 : _root_.irreducible p) (hp2 : polynomial.aeval x p = 0) :
p * C p.leading_coeff⁻¹ = minpoly A x :=
begin
have : p.leading_coeff ≠ 0 := leading_coeff_ne_zero.mpr hp1.ne_zero,
apply eq_of_irreducible_of_monic,
{ exact associated.irreducible ⟨⟨C p.leading_coeff⁻¹, C p.leading_coeff,
by rwa [←C_mul, inv_mul_cancel, C_1], by rwa [←C_mul, mul_inv_cancel, C_1]⟩, rfl⟩ hp1 },
{ rw [aeval_mul, hp2, zero_mul] },
{ rwa [polynomial.monic, leading_coeff_mul, leading_coeff_C, mul_inv_cancel] },
end
/-- If `y` is the image of `x` in an extension, their minimal polynomials coincide.
We take `h : y = algebra_map L T x` as an argument because `rw h` typically fails
since `is_integral R y` depends on y.
-/
lemma eq_of_algebra_map_eq {K S T : Type*} [field K] [comm_ring S] [comm_ring T]
[algebra K S] [algebra K T] [algebra S T]
[is_scalar_tower K S T] (hST : function.injective (algebra_map S T))
{x : S} {y : T} (hx : is_integral K x) (h : y = algebra_map S T x) :
minpoly K x = minpoly K y :=
minpoly.unique _ _ (minpoly.monic hx)
(by rw [h, aeval_algebra_map_apply, minpoly.aeval, ring_hom.map_zero])
(λ q q_monic root_q, minpoly.min _ _ q_monic
((aeval_algebra_map_eq_zero_iff_of_injective hST).mp
(h ▸ root_q : polynomial.aeval (algebra_map S T x) q = 0)))
lemma add_algebra_map {B : Type*} [comm_ring B] [algebra A B] {x : B}
(hx : is_integral A x) (a : A) :
minpoly A (x + (algebra_map A B a)) = (minpoly A x).comp (X - C a) :=
begin
refine (minpoly.unique _ _ ((minpoly.monic hx).comp_X_sub_C _) _ (λ q qmo hq, _)).symm,
{ simp [aeval_comp] },
{ have : (polynomial.aeval x) (q.comp (X + C a)) = 0 := by simpa [aeval_comp] using hq,
have H := minpoly.min A x (qmo.comp_X_add_C _) this,
rw [degree_eq_nat_degree qmo.ne_zero, degree_eq_nat_degree
((minpoly.monic hx).comp_X_sub_C _).ne_zero, with_bot.coe_le_coe, nat_degree_comp,
nat_degree_X_sub_C, mul_one],
rwa [degree_eq_nat_degree (minpoly.ne_zero hx), degree_eq_nat_degree
(qmo.comp_X_add_C _).ne_zero, with_bot.coe_le_coe, nat_degree_comp,
nat_degree_X_add_C, mul_one] at H }
end
lemma sub_algebra_map {B : Type*} [comm_ring B] [algebra A B] {x : B}
(hx : is_integral A x) (a : A) :
minpoly A (x - (algebra_map A B a)) = (minpoly A x).comp (X + C a) :=
by simpa [sub_eq_add_neg] using add_algebra_map hx (-a)
section alg_hom_fintype
/-- A technical finiteness result. -/
noncomputable def fintype.subtype_prod {E : Type*} {X : set E} (hX : X.finite) {L : Type*}
(F : E → multiset L) : fintype (Π x : X, {l : L // l ∈ F x}) :=
let hX := finite.fintype hX in by exactI pi.fintype
variables (F E K : Type*) [field F] [ring E] [comm_ring K] [is_domain K]
[algebra F E] [algebra F K] [finite_dimensional F E]
/-- Function from Hom_K(E,L) to pi type Π (x : basis), roots of min poly of x -/
-- Marked as `noncomputable!` since this definition takes multiple seconds to compile,
-- and isn't very computable in practice (since neither `finrank` nor `fin_basis` are).
noncomputable! def roots_of_min_poly_pi_type (φ : E →ₐ[F] K)
(x : range (finite_dimensional.fin_basis F E : _ → E)) :
{l : K // l ∈ (((minpoly F x.1).map (algebra_map F K)).roots : multiset K)} :=
⟨φ x, by rw [mem_roots_map (minpoly.ne_zero_of_finite_field_extension F x.val),
subtype.val_eq_coe, ←aeval_def, aeval_alg_hom_apply, minpoly.aeval, map_zero]⟩
lemma aux_inj_roots_of_min_poly : injective (roots_of_min_poly_pi_type F E K) :=
begin
intros f g h,
suffices : (f : E →ₗ[F] K) = g,
{ rwa fun_like.ext'_iff at this ⊢ },
rw funext_iff at h,
exact linear_map.ext_on (finite_dimensional.fin_basis F E).span_eq
(λ e he, subtype.ext_iff.mp (h ⟨e, he⟩)),
end
/-- Given field extensions `E/F` and `K/F`, with `E/F` finite, there are finitely many `F`-algebra
homomorphisms `E →ₐ[K] K`. -/
noncomputable instance alg_hom.fintype : fintype (E →ₐ[F] K) :=
@fintype.of_injective _ _ (fintype.subtype_prod (finite_range (finite_dimensional.fin_basis F E))
(λ e, ((minpoly F e).map (algebra_map F K)).roots)) _ (aux_inj_roots_of_min_poly F E K)
end alg_hom_fintype
variables (B) [nontrivial B]
/-- If `B/K` is a nontrivial algebra over a field, and `x` is an element of `K`,
then the minimal polynomial of `algebra_map K B x` is `X - C x`. -/
lemma eq_X_sub_C (a : A) : minpoly A (algebra_map A B a) = X - C a :=
eq_X_sub_C_of_algebra_map_inj a (algebra_map A B).injective
lemma eq_X_sub_C' (a : A) : minpoly A a = X - C a := eq_X_sub_C A a
variables (A)
/-- The minimal polynomial of `0` is `X`. -/
@[simp] lemma zero : minpoly A (0:B) = X :=
by simpa only [add_zero, C_0, sub_eq_add_neg, neg_zero, ring_hom.map_zero]
using eq_X_sub_C B (0:A)
/-- The minimal polynomial of `1` is `X - 1`. -/
@[simp] lemma one : minpoly A (1:B) = X - 1 :=
by simpa only [ring_hom.map_one, C_1, sub_eq_add_neg] using eq_X_sub_C B (1:A)
end ring
section is_domain
variables [ring B] [is_domain B] [algebra A B]
variables {A} {x : B}
/-- A minimal polynomial is prime. -/
lemma prime (hx : is_integral A x) : prime (minpoly A x) :=
begin
refine ⟨ne_zero hx, not_is_unit A x, _⟩,
rintros p q ⟨d, h⟩,
have : polynomial.aeval x (p*q) = 0 := by simp [h, aeval A x],
replace : polynomial.aeval x p = 0 ∨ polynomial.aeval x q = 0 := by simpa,
exact or.imp (dvd A x) (dvd A x) this
end
/-- If `L/K` is a field extension and an element `y` of `K` is a root of the minimal polynomial
of an element `x ∈ L`, then `y` maps to `x` under the field embedding. -/
lemma root {x : B} (hx : is_integral A x) {y : A} (h : is_root (minpoly A x) y) :
algebra_map A B y = x :=
have key : minpoly A x = X - C y :=
eq_of_monic_of_associated (monic hx) (monic_X_sub_C y) (associated_of_dvd_dvd
((irreducible_X_sub_C y).dvd_symm (irreducible hx) (dvd_iff_is_root.2 h))
(dvd_iff_is_root.2 h)),
by { have := aeval A x, rwa [key, alg_hom.map_sub, aeval_X, aeval_C, sub_eq_zero, eq_comm] at this }
/-- The constant coefficient of the minimal polynomial of `x` is `0` if and only if `x = 0`. -/
@[simp] lemma coeff_zero_eq_zero (hx : is_integral A x) : coeff (minpoly A x) 0 = 0 ↔ x = 0 :=
begin
split,
{ intro h,
have zero_root := zero_is_root_of_coeff_zero_eq_zero h,
rw ← root hx zero_root,
exact ring_hom.map_zero _ },
{ rintro rfl, simp }
end
/-- The minimal polynomial of a nonzero element has nonzero constant coefficient. -/
lemma coeff_zero_ne_zero (hx : is_integral A x) (h : x ≠ 0) : coeff (minpoly A x) 0 ≠ 0 :=
by { contrapose! h, simpa only [hx, coeff_zero_eq_zero] using h }
end is_domain
end minpoly
|
ac2a2da41f58ba333d3eeabdaa71c9ead37c0ea1 | 4d2583807a5ac6caaffd3d7a5f646d61ca85d532 | /src/tactic/rewrite.lean | fafae007841d8bbdfbe9713426610e8ded99a43a | [
"Apache-2.0"
] | permissive | AntoineChambert-Loir/mathlib | 64aabb896129885f12296a799818061bc90da1ff | 07be904260ab6e36a5769680b6012f03a4727134 | refs/heads/master | 1,693,187,631,771 | 1,636,719,886,000 | 1,636,719,886,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 7,928 | lean | /-
Copyright (c) 2018 Simon Hudon. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Simon Hudon
-/
import data.dlist
import tactic.core
namespace tactic
open expr list
meta def match_fn (fn : expr) : expr → tactic (expr × expr)
| (app (app fn' e₀) e₁) := unify fn fn' $> (e₀, e₁)
| _ := failed
meta def fill_args : expr → tactic (expr × list expr)
| (pi n bi d b) :=
do v ← mk_meta_var d,
(r, vs) ← fill_args (b.instantiate_var v),
return (r, v::vs)
| e := return (e, [])
meta def mk_assoc_pattern' (fn : expr) : expr → tactic (dlist expr)
| e :=
(do (e₀, e₁) ← match_fn fn e,
(++) <$> mk_assoc_pattern' e₀ <*> mk_assoc_pattern' e₁) <|>
pure (dlist.singleton e)
meta def mk_assoc_pattern (fn e : expr) : tactic (list expr) :=
dlist.to_list <$> mk_assoc_pattern' fn e
meta def mk_assoc (fn : expr) : list expr → tactic expr
| [] := failed
| [x] := pure x
| (x₀ :: x₁ :: xs) := mk_assoc (fn x₀ x₁ :: xs)
meta def chain_eq_trans : list expr → tactic expr
| [] := to_expr ``(rfl)
| [e] := pure e
| (e :: es) := chain_eq_trans es >>= mk_eq_trans e
meta def unify_prefix : list expr → list expr → tactic unit
| [] _ := pure ()
| _ [] := failed
| (x :: xs) (y :: ys) :=
unify x y >> unify_prefix xs ys
meta def match_assoc_pattern' (p : list expr) : list expr → tactic (list expr × list expr) | es :=
unify_prefix p es $> ([], es.drop p.length) <|>
match es with
| [] := failed
| (x :: xs) := prod.map (cons x) id <$> match_assoc_pattern' xs
end
meta def match_assoc_pattern (fn p e : expr) : tactic (list expr × list expr) :=
do p' ← mk_assoc_pattern fn p,
e' ← mk_assoc_pattern fn e,
match_assoc_pattern' p' e'
meta def mk_eq_proof (fn : expr) (e₀ e₁ : list expr) (p : expr) : tactic (expr × expr × expr) :=
do (l, r) ← infer_type p >>= match_eq,
if e₀.empty ∧ e₁.empty then pure (l, r, p)
else do
l' ← mk_assoc fn (e₀ ++ [l] ++ e₁),
r' ← mk_assoc fn (e₀ ++ [r] ++ e₁),
t ← infer_type l',
v ← mk_local_def `x t,
e ← mk_assoc fn (e₀ ++ [v] ++ e₁),
p ← mk_congr_arg (e.lambdas [v]) p,
p' ← mk_id_eq l' r' p,
return (l', r', p')
meta def assoc_root (fn assoc : expr) : expr → tactic (expr × expr) | e :=
(do (e₀, e₁) ← match_fn fn e,
(ea, eb) ← match_fn fn e₁,
let e' := fn (fn e₀ ea) eb,
p' ← mk_eq_symm (assoc e₀ ea eb),
(e'', p'') ← assoc_root e',
prod.mk e'' <$> mk_eq_trans p' p'') <|>
prod.mk e <$> mk_eq_refl e
meta def assoc_refl' (fn assoc : expr) : expr → expr → tactic expr
| l r := (is_def_eq l r >> mk_eq_refl l) <|> do
(l', l_p) ← assoc_root fn assoc l <|> fail "A",
(el₀, el₁) ← match_fn fn l' <|> fail "B",
(r', r_p) ← assoc_root fn assoc r <|> fail "C",
(er₀, er₁) ← match_fn fn r' <|> fail "D",
p₀ ← assoc_refl' el₀ er₀,
p₁ ← is_def_eq el₁ er₁ >> mk_eq_refl el₁,
f_eq ← mk_congr_arg fn p₀ <|> fail "G",
p' ← mk_congr f_eq p₁ <|> fail "H",
r_p' ← mk_eq_symm r_p,
chain_eq_trans [l_p, p', r_p']
meta def assoc_refl (fn : expr) : tactic unit :=
do (l, r) ← target >>= match_eq,
assoc ← mk_mapp ``is_associative.assoc [none, fn, none]
<|> fail format!"{fn} is not associative",
assoc_refl' fn assoc l r >>= tactic.exact
meta def flatten (fn assoc e : expr) : tactic (expr × expr) :=
do ls ← mk_assoc_pattern fn e,
e' ← mk_assoc fn ls,
p ← assoc_refl' fn assoc e e',
return (e', p)
meta def assoc_rewrite_intl (assoc h e : expr) : tactic (expr × expr) :=
do t ← infer_type h,
(lhs, rhs) ← match_eq t,
let fn := lhs.app_fn.app_fn,
(l, r) ← match_assoc_pattern fn lhs e,
(lhs', rhs', h') ← mk_eq_proof fn l r h,
e_p ← assoc_refl' fn assoc e lhs',
(rhs'', rhs_p) ← flatten fn assoc rhs',
final_p ← chain_eq_trans [e_p, h', rhs_p],
return (rhs'', final_p)
-- TODO(Simon): visit expressions built of `fn` nested inside other such expressions:
-- e.g.: x + f (a + b + c) + y should generate two rewrite candidates
meta def enum_assoc_subexpr' (fn : expr) : expr → tactic (dlist expr)
| e :=
dlist.singleton e <$ (match_fn fn e >> guard (¬ e.has_var)) <|>
expr.mfoldl (λ es e', (++ es) <$> enum_assoc_subexpr' e') dlist.empty e
meta def enum_assoc_subexpr (fn e : expr) : tactic (list expr) :=
dlist.to_list <$> enum_assoc_subexpr' fn e
meta def mk_assoc_instance (fn : expr) : tactic expr :=
do t ← mk_mapp ``is_associative [none, fn],
inst ← prod.snd <$> solve_aux t assumption <|>
(mk_instance t >>= assertv `_inst t) <|>
fail format!"{fn} is not associative",
mk_mapp ``is_associative.assoc [none, fn, inst]
meta def assoc_rewrite (h e : expr) (opt_assoc : option expr := none) :
tactic (expr × expr × list expr) :=
do (t, vs) ← infer_type h >>= fill_args,
(lhs, rhs) ← match_eq t,
let fn := lhs.app_fn.app_fn,
es ← enum_assoc_subexpr fn e,
assoc ← match opt_assoc with
| none := mk_assoc_instance fn
| (some assoc) := pure assoc
end,
(_, p) ← mfirst (assoc_rewrite_intl assoc $ h.mk_app vs) es,
(e', p', _) ← tactic.rewrite p e,
pure (e', p', vs)
meta def assoc_rewrite_target (h : expr) (opt_assoc : option expr := none) :
tactic unit :=
do tgt ← target,
(tgt', p, _) ← assoc_rewrite h tgt opt_assoc,
replace_target tgt' p
meta def assoc_rewrite_hyp (h hyp : expr) (opt_assoc : option expr := none) :
tactic expr :=
do tgt ← infer_type hyp,
(tgt', p, _) ← assoc_rewrite h tgt opt_assoc,
replace_hyp hyp tgt' p
namespace interactive
setup_tactic_parser
private meta def assoc_rw_goal (rs : list rw_rule) : tactic unit :=
rs.mmap' $ λ r, do
save_info r.pos,
eq_lemmas ← get_rule_eqn_lemmas r,
orelse'
(do e ← to_expr' r.rule, assoc_rewrite_target e)
(eq_lemmas.mfirst $ λ n, do e ← mk_const n, assoc_rewrite_target e)
(eq_lemmas.empty)
private meta def uses_hyp (e : expr) (h : expr) : bool :=
e.fold ff $ λ t _ r, r || (t = h)
private meta def assoc_rw_hyp : list rw_rule → expr → tactic unit
| [] hyp := skip
| (r::rs) hyp := do
save_info r.pos,
eq_lemmas ← get_rule_eqn_lemmas r,
orelse'
(do e ← to_expr' r.rule, when (¬ uses_hyp e hyp) $ assoc_rewrite_hyp e hyp >>= assoc_rw_hyp rs)
(eq_lemmas.mfirst $ λ n, do e ← mk_const n, assoc_rewrite_hyp e hyp >>= assoc_rw_hyp rs)
(eq_lemmas.empty)
private meta def assoc_rw_core (rs : parse rw_rules) (loca : parse location) : tactic unit :=
match loca with
| loc.wildcard := loca.try_apply (assoc_rw_hyp rs.rules) (assoc_rw_goal rs.rules)
| _ := loca.apply (assoc_rw_hyp rs.rules) (assoc_rw_goal rs.rules)
end >> try reflexivity
>> try (returnopt rs.end_pos >>= save_info)
/--
`assoc_rewrite [h₀,← h₁] at ⊢ h₂` behaves like `rewrite [h₀,← h₁] at ⊢ h₂`
with the exception that associativity is used implicitly to make rewriting
possible.
It works for any function `f` for which an `is_associative f` instance can be found.
```
example {α : Type*} (f : α → α → α) [is_associative α f] (a b c d x : α) :
let infix `~` := f in
b ~ c = x → (a ~ b ~ c ~ d) = (a ~ x ~ d) :=
begin
intro h,
assoc_rw h,
end
```
-/
meta def assoc_rewrite (q : parse rw_rules) (l : parse location) : tactic unit :=
propagate_tags (assoc_rw_core q l)
/-- synonym for `assoc_rewrite` -/
meta def assoc_rw (q : parse rw_rules) (l : parse location) : tactic unit :=
assoc_rewrite q l
add_tactic_doc
{ name := "assoc_rewrite",
category := doc_category.tactic,
decl_names := [`tactic.interactive.assoc_rewrite, `tactic.interactive.assoc_rw],
tags := ["rewriting"],
inherit_description_from := `tactic.interactive.assoc_rewrite }
end interactive
end tactic
|
81d686530e041fa0b9316db60b72716dea09bb06 | 77c5b91fae1b966ddd1db969ba37b6f0e4901e88 | /src/algebraic_geometry/Spec.lean | 35bcb09fb84551e6fcd9db9786a4e48509eb83ff | [
"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 | 9,792 | lean | /-
Copyright (c) 2020 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Justus Springer
-/
import algebraic_geometry.locally_ringed_space
import algebraic_geometry.structure_sheaf
import data.equiv.transfer_instance
/-!
# $Spec$ as a functor to locally ringed spaces.
We define the functor $Spec$ from commutative rings to locally ringed spaces.
## Implementation notes
We define $Spec$ in three consecutive steps, each with more structure than the last:
1. `Spec.to_Top`, valued in the category of topological spaces,
2. `Spec.to_SheafedSpace`, valued in the category of sheafed spaces and
3. `Spec.to_LocallyRingedSpace`, valued in the category of locally ringed spaces.
Additionally, we provide `Spec.to_PresheafedSpace` as a composition of `Spec.to_SheafedSpace` with
a forgetful functor.
## In progress
Adjunction between `Γ` and `Spec`: Currently, the counit of the adjunction is proven to be a
natural transformation in `Spec_Γ_naturality`, and realized as a natural isomorphism in
`Spec_Γ_identity`.
TODO: provide the unit, and prove the triangle identities.
-/
noncomputable theory
universe variables u v
namespace algebraic_geometry
open opposite
open category_theory
open structure_sheaf
/--
The spectrum of a commutative ring, as a topological space.
-/
def Spec.Top_obj (R : CommRing) : Top := Top.of (prime_spectrum R)
/--
The induced map of a ring homomorphism on the ring spectra, as a morphism of topological spaces.
-/
@[simps] def Spec.Top_map {R S : CommRing} (f : R ⟶ S) :
Spec.Top_obj S ⟶ Spec.Top_obj R :=
{ to_fun := prime_spectrum.comap f,
continuous_to_fun := prime_spectrum.comap_continuous f }
@[simp] lemma Spec.Top_map_id (R : CommRing) :
Spec.Top_map (𝟙 R) = 𝟙 (Spec.Top_obj R) :=
continuous_map.ext $ λ x,
by erw [Spec.Top_map_to_fun, prime_spectrum.comap_id, id.def, Top.id_app]
lemma Spec.Top_map_comp {R S T : CommRing} (f : R ⟶ S) (g : S ⟶ T) :
Spec.Top_map (f ≫ g) = Spec.Top_map g ≫ Spec.Top_map f :=
continuous_map.ext $ λ x,
begin
dsimp only [Spec.Top_map_to_fun, Top.comp_app],
erw prime_spectrum.comap_comp,
end
/--
The spectrum, as a contravariant functor from commutative rings to topological spaces.
-/
@[simps] def Spec.to_Top : CommRingᵒᵖ ⥤ Top :=
{ obj := λ R, Spec.Top_obj (unop R),
map := λ R S f, Spec.Top_map f.unop,
map_id' := λ R, by rw [unop_id, Spec.Top_map_id],
map_comp' := λ R S T f g, by rw [unop_comp, Spec.Top_map_comp] }
/--
The spectrum of a commutative ring, as a `SheafedSpace`.
-/
@[simps] def Spec.SheafedSpace_obj (R : CommRing) : SheafedSpace CommRing :=
{ carrier := Spec.Top_obj R, ..structure_sheaf R }
/--
The induced map of a ring homomorphism on the ring spectra, as a morphism of sheafed spaces.
-/
@[simps] def Spec.SheafedSpace_map {R S : CommRing.{u}} (f : R ⟶ S) :
Spec.SheafedSpace_obj S ⟶ Spec.SheafedSpace_obj R :=
{ base := Spec.Top_map f,
c :=
{ app := λ U, comap f (unop U) ((topological_space.opens.map (Spec.Top_map f)).obj (unop U))
(λ p, id),
naturality' := λ U V i, ring_hom.ext $ λ s, subtype.eq $ funext $ λ p, rfl } }
@[simp] lemma Spec.SheafedSpace_map_id {R : CommRing} :
Spec.SheafedSpace_map (𝟙 R) = 𝟙 (Spec.SheafedSpace_obj R) :=
PresheafedSpace.ext _ _ (Spec.Top_map_id R) $ nat_trans.ext _ _ $ funext $ λ U,
begin
dsimp,
erw [PresheafedSpace.id_c_app, comap_id], swap,
{ rw [Spec.Top_map_id, topological_space.opens.map_id_obj_unop] },
rw [eq_to_hom_op, eq_to_hom_map, eq_to_hom_trans],
refl,
end
lemma Spec.SheafedSpace_map_comp {R S T : CommRing} (f : R ⟶ S) (g : S ⟶ T) :
Spec.SheafedSpace_map (f ≫ g) = Spec.SheafedSpace_map g ≫ Spec.SheafedSpace_map f :=
PresheafedSpace.ext _ _ (Spec.Top_map_comp f g) $ nat_trans.ext _ _ $ funext $ λ U,
begin
dsimp,
erw [Top.presheaf.pushforward.comp_inv_app, ← category.assoc, category.comp_id,
(structure_sheaf T).presheaf.map_id, category.comp_id, comap_comp],
refl,
end
/--
Spec, as a contravariant functor from commutative rings to sheafed spaces.
-/
@[simps] def Spec.to_SheafedSpace : CommRingᵒᵖ ⥤ SheafedSpace CommRing :=
{ obj := λ R, Spec.SheafedSpace_obj (unop R),
map := λ R S f, Spec.SheafedSpace_map f.unop,
map_id' := λ R, by rw [unop_id, Spec.SheafedSpace_map_id],
map_comp' := λ R S T f g, by rw [unop_comp, Spec.SheafedSpace_map_comp] }
/--
Spec, as a contravariant functor from commutative rings to presheafed spaces.
-/
def Spec.to_PresheafedSpace : CommRingᵒᵖ ⥤ PresheafedSpace CommRing :=
Spec.to_SheafedSpace ⋙ SheafedSpace.forget_to_PresheafedSpace
@[simp] lemma Spec.to_PresheafedSpace_obj (R : CommRingᵒᵖ) :
Spec.to_PresheafedSpace.obj R = (Spec.SheafedSpace_obj (unop R)).to_PresheafedSpace := rfl
lemma Spec.to_PresheafedSpace_obj_op (R : CommRing) :
Spec.to_PresheafedSpace.obj (op R) = (Spec.SheafedSpace_obj R).to_PresheafedSpace := rfl
@[simp] lemma Spec.to_PresheafedSpace_map (R S : CommRingᵒᵖ) (f : R ⟶ S) :
Spec.to_PresheafedSpace.map f = Spec.SheafedSpace_map f.unop := rfl
lemma Spec.to_PresheafedSpace_map_op (R S : CommRing) (f : R ⟶ S) :
Spec.to_PresheafedSpace.map f.op = Spec.SheafedSpace_map f := rfl
/--
The spectrum of a commutative ring, as a `LocallyRingedSpace`.
-/
@[simps] def Spec.LocallyRingedSpace_obj (R : CommRing) : LocallyRingedSpace :=
{ local_ring := λ x, @@ring_equiv.local_ring _
(show local_ring (localization.at_prime _), by apply_instance) _
(iso.CommRing_iso_to_ring_equiv $ stalk_iso R x).symm,
.. Spec.SheafedSpace_obj R }
@[elementwise]
lemma stalk_map_to_stalk {R S : CommRing} (f : R ⟶ S) (p : prime_spectrum S) :
to_stalk R (prime_spectrum.comap f p) ≫
PresheafedSpace.stalk_map (Spec.SheafedSpace_map f) p =
f ≫ to_stalk S p :=
begin
erw [← to_open_germ S ⊤ ⟨p, trivial⟩, ← to_open_germ R ⊤ ⟨prime_spectrum.comap f p, trivial⟩,
category.assoc, PresheafedSpace.stalk_map_germ (Spec.SheafedSpace_map f) ⊤ ⟨p, trivial⟩,
Spec.SheafedSpace_map_c_app, to_open_comp_comap_assoc],
refl
end
/--
Under the isomorphisms `stalk_iso`, the map `stalk_map (Spec.SheafedSpace_map f) p` corresponds
to the induced local ring homomorphism `localization.local_ring_hom`.
-/
@[elementwise]
lemma local_ring_hom_comp_stalk_iso {R S : CommRing} (f : R ⟶ S) (p : prime_spectrum S) :
(stalk_iso R (prime_spectrum.comap f p)).hom ≫
@category_struct.comp _ _
(CommRing.of (localization.at_prime (prime_spectrum.comap f p).as_ideal))
(CommRing.of (localization.at_prime p.as_ideal)) _
(localization.local_ring_hom (prime_spectrum.comap f p).as_ideal p.as_ideal f rfl)
(stalk_iso S p).inv =
PresheafedSpace.stalk_map (Spec.SheafedSpace_map f) p :=
(stalk_iso R (prime_spectrum.comap f p)).eq_inv_comp.mp $ (stalk_iso S p).comp_inv_eq.mpr $
localization.local_ring_hom_unique _ _ _ _ $ λ x, by
rw [stalk_iso_hom, stalk_iso_inv, comp_apply, comp_apply, localization_to_stalk_of,
stalk_map_to_stalk_apply, stalk_to_fiber_ring_hom_to_stalk]
/--
The induced map of a ring homomorphism on the prime spectra, as a morphism of locally ringed spaces.
-/
@[simps] def Spec.LocallyRingedSpace_map {R S : CommRing} (f : R ⟶ S) :
Spec.LocallyRingedSpace_obj S ⟶ Spec.LocallyRingedSpace_obj R :=
subtype.mk (Spec.SheafedSpace_map f) $ λ p, is_local_ring_hom.mk $ λ a ha,
begin
-- Here, we are showing that the map on prime spectra induced by `f` is really a morphism of
-- *locally* ringed spaces, i.e. that the induced map on the stalks is a local ring homomorphism.
rw ← local_ring_hom_comp_stalk_iso_apply at ha,
replace ha := (stalk_iso S p).hom.is_unit_map ha,
rw coe_inv_hom_id at ha,
replace ha := is_local_ring_hom.map_nonunit _ ha,
convert ring_hom.is_unit_map (stalk_iso R (prime_spectrum.comap f p)).inv ha,
rw coe_hom_inv_id,
end
@[simp] lemma Spec.LocallyRingedSpace_map_id (R : CommRing) :
Spec.LocallyRingedSpace_map (𝟙 R) = 𝟙 (Spec.LocallyRingedSpace_obj R) :=
subtype.ext $ by { rw [Spec.LocallyRingedSpace_map_coe, Spec.SheafedSpace_map_id], refl }
lemma Spec.LocallyRingedSpace_map_comp {R S T : CommRing} (f : R ⟶ S) (g : S ⟶ T) :
Spec.LocallyRingedSpace_map (f ≫ g) =
Spec.LocallyRingedSpace_map g ≫ Spec.LocallyRingedSpace_map f :=
subtype.ext $ by { rw [Spec.LocallyRingedSpace_map_coe, Spec.SheafedSpace_map_comp], refl }
/--
Spec, as a contravariant functor from commutative rings to locally ringed spaces.
-/
@[simps] def Spec.to_LocallyRingedSpace : CommRingᵒᵖ ⥤ LocallyRingedSpace :=
{ obj := λ R, Spec.LocallyRingedSpace_obj (unop R),
map := λ R S f, Spec.LocallyRingedSpace_map f.unop,
map_id' := λ R, by rw [unop_id, Spec.LocallyRingedSpace_map_id],
map_comp' := λ R S T f g, by rw [unop_comp, Spec.LocallyRingedSpace_map_comp] }
section Spec_Γ
open algebraic_geometry.LocallyRingedSpace
/-- The morphism `R ⟶ Γ(Spec R)` given by `algebraic_geometry.structure_sheaf.to_open`. -/
@[simps] def to_Spec_Γ (R : CommRing) : R ⟶ Γ.obj (op (Spec.to_LocallyRingedSpace.obj (op R))) :=
structure_sheaf.to_open R ⊤
instance is_iso_to_Spec_Γ (R : CommRing) : is_iso (to_Spec_Γ R) :=
by { cases R, apply structure_sheaf.is_iso_to_global }
lemma Spec_Γ_naturality {R S : CommRing} (f : R ⟶ S) :
f ≫ to_Spec_Γ S = to_Spec_Γ R ≫ Γ.map (Spec.to_LocallyRingedSpace.map f.op).op :=
by { ext, symmetry, apply localization.local_ring_hom_to_map }
/-- The counit of the adjunction `Γ ⊣ Spec` is an isomorphism. -/
@[simps] def Spec_Γ_identity : Spec.to_LocallyRingedSpace.right_op ⋙ Γ ≅ 𝟭 _ :=
iso.symm $ nat_iso.of_components (λ R, as_iso (to_Spec_Γ R) : _) (λ _ _, Spec_Γ_naturality)
end Spec_Γ
end algebraic_geometry
|
de625fa28aae5de76aa959f9c2aab5a457fddd38 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/category_theory/monoidal/Mod_auto.lean | c1ada29325d919e26c0dc902f34a7212967a56a1 | [] | no_license | AurelienSaue/Mathlib4_auto | f538cfd0980f65a6361eadea39e6fc639e9dae14 | 590df64109b08190abe22358fabc3eae000943f2 | refs/heads/master | 1,683,906,849,776 | 1,622,564,669,000 | 1,622,564,669,000 | 371,723,747 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 5,652 | 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.Mon_
import Mathlib.PostPort
universes v₁ u₁ l
namespace Mathlib
/-!
# The category of module objects over a monoid object.
-/
/-- A module object for a monoid object, all internal to some monoidal category. -/
structure Mod {C : Type u₁} [category_theory.category C] [category_theory.monoidal_category C]
(A : Mon_ C)
where
X : C
act : Mon_.X A ⊗ X ⟶ X
one_act' :
autoParam ((Mon_.one A ⊗ 𝟙) ≫ act = category_theory.iso.hom λ_)
(Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.obviously")
(Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "obviously") [])
assoc' :
autoParam ((Mon_.mul A ⊗ 𝟙) ≫ act = category_theory.iso.hom α_ ≫ (𝟙 ⊗ act) ≫ act)
(Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.obviously")
(Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "obviously") [])
@[simp] theorem Mod.one_act {C : Type u₁} [category_theory.category C]
[category_theory.monoidal_category C] {A : Mon_ C} (c : Mod A) :
(Mon_.one A ⊗ 𝟙) ≫ Mod.act c = category_theory.iso.hom λ_ :=
sorry
@[simp] theorem Mod.assoc {C : Type u₁} [category_theory.category C]
[category_theory.monoidal_category C] {A : Mon_ C} (c : Mod A) :
(Mon_.mul A ⊗ 𝟙) ≫ Mod.act c = category_theory.iso.hom α_ ≫ (𝟙 ⊗ Mod.act c) ≫ Mod.act c :=
sorry
@[simp] theorem Mod.one_act_assoc {C : Type u₁} [category_theory.category C]
[category_theory.monoidal_category C] {A : Mon_ C} (c : Mod A) {X' : C} (f' : Mod.X c ⟶ X') :
(Mon_.one A ⊗ 𝟙) ≫ Mod.act c ≫ f' = category_theory.iso.hom λ_ ≫ f' :=
sorry
namespace Mod
theorem assoc_flip {C : Type u₁} [category_theory.category C] [category_theory.monoidal_category C]
{A : Mon_ C} (M : Mod A) :
(𝟙 ⊗ act M) ≫ act M = category_theory.iso.inv α_ ≫ (Mon_.mul A ⊗ 𝟙) ≫ act M :=
sorry
/-- A morphism of module objects. -/
structure hom {C : Type u₁} [category_theory.category C] [category_theory.monoidal_category C]
{A : Mon_ C} (M : Mod A) (N : Mod A)
where
hom : X M ⟶ X N
act_hom' :
autoParam (act M ≫ hom = (𝟙 ⊗ hom) ≫ act N)
(Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.obviously")
(Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "obviously") [])
@[simp] theorem hom.act_hom {C : Type u₁} [category_theory.category C]
[category_theory.monoidal_category C] {A : Mon_ C} {M : Mod A} {N : Mod A} (c : hom M N) :
act M ≫ hom.hom c = (𝟙 ⊗ hom.hom c) ≫ act N :=
sorry
@[simp] theorem hom.act_hom_assoc {C : Type u₁} [category_theory.category C]
[category_theory.monoidal_category C] {A : Mon_ C} {M : Mod A} {N : Mod A} (c : hom M N)
{X' : C} (f' : X N ⟶ X') : act M ≫ hom.hom c ≫ f' = (𝟙 ⊗ hom.hom c) ≫ act N ≫ f' :=
sorry
/-- The identity morphism on a module object. -/
def id {C : Type u₁} [category_theory.category C] [category_theory.monoidal_category C] {A : Mon_ C}
(M : Mod A) : hom M M :=
hom.mk 𝟙
protected instance hom_inhabited {C : Type u₁} [category_theory.category C]
[category_theory.monoidal_category C] {A : Mon_ C} (M : Mod A) : Inhabited (hom M M) :=
{ default := id M }
/-- Composition of module object morphisms. -/
def comp {C : Type u₁} [category_theory.category C] [category_theory.monoidal_category C]
{A : Mon_ C} {M : Mod A} {N : Mod A} {O : Mod A} (f : hom M N) (g : hom N O) : hom M O :=
hom.mk (hom.hom f ≫ hom.hom g)
protected instance category_theory.category {C : Type u₁} [category_theory.category C]
[category_theory.monoidal_category C] {A : Mon_ C} : category_theory.category (Mod A) :=
category_theory.category.mk
@[simp] theorem id_hom' {C : Type u₁} [category_theory.category C]
[category_theory.monoidal_category C] {A : Mon_ C} (M : Mod A) : hom.hom 𝟙 = 𝟙 :=
rfl
@[simp] theorem comp_hom' {C : Type u₁} [category_theory.category C]
[category_theory.monoidal_category C] {A : Mon_ C} {M : Mod A} {N : Mod A} {K : Mod A}
(f : M ⟶ N) (g : N ⟶ K) : hom.hom (f ≫ g) = hom.hom f ≫ hom.hom g :=
rfl
/-- A monoid object as a module over itself. -/
@[simp] theorem regular_X {C : Type u₁} [category_theory.category C]
[category_theory.monoidal_category C] (A : Mon_ C) : X (regular A) = Mon_.X A :=
Eq.refl (X (regular A))
protected instance inhabited {C : Type u₁} [category_theory.category C]
[category_theory.monoidal_category C] (A : Mon_ C) : Inhabited (Mod A) :=
{ default := regular A }
/-- The forgetful functor from module objects to the ambient category. -/
def forget {C : Type u₁} [category_theory.category C] [category_theory.monoidal_category C]
(A : Mon_ C) : Mod A ⥤ C :=
category_theory.functor.mk (fun (A_1 : Mod A) => X A_1)
fun (A_1 B : Mod A) (f : A_1 ⟶ B) => hom.hom f
/--
A morphism of monoid objects induces a "restriction" or "comap" functor
between the categories of module objects.
-/
@[simp] theorem comap_obj_act {C : Type u₁} [category_theory.category C]
[category_theory.monoidal_category C] {A : Mon_ C} {B : Mon_ C} (f : A ⟶ B) (M : Mod B) :
act (category_theory.functor.obj (comap f) M) = (Mon_.hom.hom f ⊗ 𝟙) ≫ act M :=
Eq.refl (act (category_theory.functor.obj (comap f) M))
end Mathlib |
a2d56a5602d797bda2c8740e46814bf0b2fbc3b1 | 302c785c90d40ad3d6be43d33bc6a558354cc2cf | /src/representation_theory/maschke.lean | cea8f9f5e1d8af367d8d7c4b453a0d80c6ce1e23 | [
"Apache-2.0"
] | permissive | ilitzroth/mathlib | ea647e67f1fdfd19a0f7bdc5504e8acec6180011 | 5254ef14e3465f6504306132fe3ba9cec9ffff16 | refs/heads/master | 1,680,086,661,182 | 1,617,715,647,000 | 1,617,715,647,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 6,334 | 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 algebra.monoid_algebra
import algebra.char_p.invertible
import linear_algebra.basis
import ring_theory.simple_module
/-!
# Maschke's theorem
We prove Maschke's theorem for finite groups,
in the formulation that every submodule of a `k[G]` module has a complement,
when `k` is a field with `invertible (fintype.card G : k)`.
We do the core computation in greater generality.
For any `[comm_ring k]` in which `[invertible (fintype.card G : k)]`,
and a `k[G]`-linear map `i : V → W` which admits a `k`-linear retraction `π`,
we produce a `k[G]`-linear retraction by
taking the average over `G` of the conjugates of `π`.
## Implementation Notes
* These results assume `invertible (fintype.card G : k)` which is equivalent to the more
familiar `¬(ring_char k ∣ fintype.card G)`. It is possible to convert between them using
`invertible_of_ring_char_not_dvd` and `ring_char_not_dvd_of_invertible`.
## Future work
It's not so far to give the usual statement, that every finite dimensional representation
of a finite group is semisimple (i.e. a direct sum of irreducibles).
-/
universes u
noncomputable theory
open semimodule
open monoid_algebra
open_locale big_operators
section
-- At first we work with any `[comm_ring k]`, and add the assumption that
-- `[invertible (fintype.card G : k)]` when it is required.
variables {k : Type u} [comm_ring k] {G : Type u} [group G]
variables {V : Type u} [add_comm_group V] [module k V] [module (monoid_algebra k G) V]
variables [is_scalar_tower k (monoid_algebra k G) V]
variables {W : Type u} [add_comm_group W] [module k W] [module (monoid_algebra k G) W]
variables [is_scalar_tower k (monoid_algebra k G) W]
/-!
We now do the key calculation in Maschke's theorem.
Given `V → W`, an inclusion of `k[G]` modules,,
assume we have some retraction `π` (i.e. `∀ v, π (i v) = v`),
just as a `k`-linear map.
(When `k` is a field, this will be available cheaply, by choosing a basis.)
We now construct a retraction of the inclusion as a `k[G]`-linear map,
by the formula
$$ \frac{1}{|G|} \sum_{g \in G} g⁻¹ • π(g • -). $$
-/
namespace linear_map
variables (π : W →ₗ[k] V)
include π
/--
We define the conjugate of `π` by `g`, as a `k`-linear map.
-/
def conjugate (g : G) : W →ₗ[k] V :=
((group_smul.linear_map k V g⁻¹).comp π).comp (group_smul.linear_map k W g)
variables (i : V →ₗ[monoid_algebra k G] W) (h : ∀ v : V, π (i v) = v)
section
include h
lemma conjugate_i (g : G) (v : V) : (conjugate π g) (i v) = v :=
begin
dsimp [conjugate],
simp only [←i.map_smul, h, ←mul_smul, single_mul_single, mul_one, mul_left_inv],
change (1 : monoid_algebra k G) • v = v,
simp,
end
end
variables (G) [fintype G]
/--
The sum of the conjugates of `π` by each element `g : G`, as a `k`-linear map.
(We postpone dividing by the size of the group as long as possible.)
-/
def sum_of_conjugates : W →ₗ[k] V :=
∑ g : G, π.conjugate g
/--
In fact, the sum over `g : G` of the conjugate of `π` by `g` is a `k[G]`-linear map.
-/
def sum_of_conjugates_equivariant : W →ₗ[monoid_algebra k G] V :=
monoid_algebra.equivariant_of_linear_of_comm (π.sum_of_conjugates G) (λ g v,
begin
dsimp [sum_of_conjugates],
simp only [linear_map.sum_apply, finset.smul_sum],
dsimp [conjugate],
conv_lhs {
rw [←finset.univ_map_embedding (mul_right_embedding g⁻¹)],
simp only [mul_right_embedding],
},
simp only [←mul_smul, single_mul_single, mul_inv_rev, mul_one, function.embedding.coe_fn_mk,
finset.sum_map, inv_inv, inv_mul_cancel_right],
recover,
end)
section
variables [inv : invertible (fintype.card G : k)]
include inv
/--
We construct our `k[G]`-linear retraction of `i` as
$$ \frac{1}{|G|} \sum_{g \in G} g⁻¹ • π(g • -). $$
-/
def equivariant_projection : W →ₗ[monoid_algebra k G] V :=
⅟(fintype.card G : k) • (π.sum_of_conjugates_equivariant G)
include h
lemma equivariant_projection_condition (v : V) : (π.equivariant_projection G) (i v) = v :=
begin
rw [equivariant_projection, smul_apply, sum_of_conjugates_equivariant,
equivariant_of_linear_of_comm_apply, sum_of_conjugates],
rw [linear_map.sum_apply],
simp only [conjugate_i π i h],
rw [finset.sum_const, finset.card_univ, nsmul_eq_smul_cast k,
←mul_smul, invertible.inv_of_mul_self, one_smul],
end
end
end linear_map
end
namespace char_zero
variables {k : Type u} [field k] {G : Type u} [fintype G] [group G] [char_zero k]
instance : invertible (fintype.card G : k) :=
invertible_of_ring_char_not_dvd (by simp [fintype.card_eq_zero_iff])
end char_zero
namespace monoid_algebra
-- Now we work over a `[field k]`.
variables {k : Type u} [field k] {G : Type u} [fintype G] [invertible (fintype.card G : k)]
variables [group G]
variables {V : Type u} [add_comm_group V] [module k V] [module (monoid_algebra k G) V]
variables [is_scalar_tower k (monoid_algebra k G) V]
variables {W : Type u} [add_comm_group W] [module k W] [module (monoid_algebra k G) W]
variables [is_scalar_tower k (monoid_algebra k G) W]
lemma exists_left_inverse_of_injective
(f : V →ₗ[monoid_algebra k G] W) (hf : f.ker = ⊥) :
∃ (g : W →ₗ[monoid_algebra k G] V), g.comp f = linear_map.id :=
begin
obtain ⟨φ, hφ⟩ := (f.restrict_scalars k).exists_left_inverse_of_injective
(by simp only [hf, submodule.restrict_scalars_bot, linear_map.ker_restrict_scalars]),
refine ⟨φ.equivariant_projection G, _⟩,
ext v,
simp only [linear_map.id_coe, id.def, linear_map.comp_apply],
apply linear_map.equivariant_projection_condition,
intro v,
have := congr_arg linear_map.to_fun hφ,
exact congr_fun this v
end
namespace submodule
lemma exists_is_compl
(p : submodule (monoid_algebra k G) V) :
∃ q : submodule (monoid_algebra k G) V, is_compl p q :=
let ⟨f, hf⟩ := monoid_algebra.exists_left_inverse_of_injective p.subtype p.ker_subtype in
⟨f.ker, linear_map.is_compl_of_proj $ linear_map.ext_iff.1 hf⟩
/-- This also implies an instance `is_semisimple_module (monoid_algebra k G) V`. -/
instance is_complemented : is_complemented (submodule (monoid_algebra k G) V) :=
⟨exists_is_compl⟩
end submodule
end monoid_algebra
|
5e705311ff645b430a1ca59dbce4b36c1f7bbd6f | 82b86ba2ae0d5aed0f01f49c46db5afec0eb2bd7 | /tests/lean/run/induction1.lean | 362aa757f8a47e6d29840a58125dd98685a2556f | [
"Apache-2.0"
] | permissive | banksonian/lean4 | 3a2e6b0f1eb63aa56ff95b8d07b2f851072d54dc | 78da6b3aa2840693eea354a41e89fc5b212a5011 | refs/heads/master | 1,673,703,624,165 | 1,605,123,551,000 | 1,605,123,551,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,409 | lean | @[recursor 4]
def Or.elim2 {p q r : Prop} (major : p ∨ q) (left : p → r) (right : q → r) : r :=
match major with
| Or.inl h => left h
| Or.inr h => right h
theorem tst0 {p q : Prop } (h : p ∨ q) : q ∨ p :=
by {
induction h;
{ apply Or.inr; assumption };
{ apply Or.inl; assumption }
}
theorem tst0' {p q : Prop } (h : p ∨ q) : q ∨ p := by
induction h
focus
apply Or.inr
assumption
focus
apply Or.inl
assumption
theorem tst1 {p q : Prop } (h : p ∨ q) : q ∨ p := by
induction h
| inr h2 => exact Or.inl h2
| inl h1 => exact Or.inr h1
theorem tst2 {p q : Prop } (h : p ∨ q) : q ∨ p := by
induction h using elim2
| left _ => apply Or.inr; assumption
| right _ => apply Or.inl; assumption
theorem tst2b {p q : Prop } (h : p ∨ q) : q ∨ p := by
induction h using elim2
| left => apply Or.inr; assumption
| _ => apply Or.inl; assumption
theorem tst3 {p q : Prop } (h : p ∨ q) : q ∨ p := by
induction h using elim2
| right h => exact Or.inl h
| left h => exact Or.inr h
theorem tst4 {p q : Prop } (h : p ∨ q) : q ∨ p := by
induction h using elim2
| right h => ?myright
| left h => ?myleft
case myleft => exact Or.inr h
case myright => exact Or.inl h
theorem tst5 {p q : Prop } (h : p ∨ q) : q ∨ p := by
induction h using elim2
| right h => _
| left h =>
refine Or.inr ?_
exact h
case right => exact Or.inl h
theorem tst6 {p q : Prop } (h : p ∨ q) : q ∨ p :=
by {
cases h
| inr h2 => exact Or.inl h2
| inl h1 => exact Or.inr h1
}
theorem tst7 {α : Type} (xs : List α) (h : (a : α) → (as : List α) → xs ≠ a :: as) : xs = [] :=
by {
induction xs
| nil => exact rfl
| cons z zs ih => exact absurd rfl (h z zs)
}
theorem tst8 {α : Type} (xs : List α) (h : (a : α) → (as : List α) → xs ≠ a :: as) : xs = [] := by {
induction xs;
exact rfl;
exact absurd rfl $ h _ _
}
theorem tst9 {α : Type} (xs : List α) (h : (a : α) → (as : List α) → xs ≠ a :: as) : xs = [] := by
cases xs
| nil => exact rfl
| cons z zs => exact absurd rfl (h z zs)
theorem tst10 {p q : Prop } (h₁ : p ↔ q) (h₂ : p) : q := by
induction h₁
| intro h _ => exact h h₂
def Iff2 (m p q : Prop) := p ↔ q
theorem tst11 {p q r : Prop } (h₁ : Iff2 r p q) (h₂ : p) : q := by
induction h₁ using Iff.rec
| intro h _ => exact h h₂
theorem tst12 {p q : Prop } (h₁ : p ∨ q) (h₂ : p ↔ q) (h₃ : p) : q := by
failIfSuccess induction h₁ using Iff.casesOn
induction h₂ using Iff.casesOn
| intro h _ =>
exact h h₃
inductive Tree
| leaf₁
| leaf₂
| node : Tree → Tree → Tree
def Tree.isLeaf₁ : Tree → Bool
| leaf₁ => true
| _ => false
theorem tst13 (x : Tree) (h : x = Tree.leaf₁) : x.isLeaf₁ = true := by
cases x
| leaf₁ => rfl
| _ => injection h
theorem tst14 (x : Tree) (h : x = Tree.leaf₁) : x.isLeaf₁ = true := by
induction x
| leaf₁ => rfl
| _ => injection h
inductive Vec (α : Type) : Nat → Type
| nil : Vec α 0
| cons : (a : α) → {n : Nat} → (as : Vec α n) → Vec α (n+1)
def getHeads {α β} {n} (xs : Vec α (n+1)) (ys : Vec β (n+1)) : α × β := by
cases xs
cases ys
apply Prod.mk
repeat
traceState
assumption
done
theorem ex1 (n m o : Nat) : n = m + 0 → m = o → m = o := by
intro (h₁ : n = m) h₂
rw [← h₁, ← h₂]
assumption
|
b720eabad4f68b20a43049c0d209d51ef1823349 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /src/Lean/Compiler/ConstFolding.lean | a044cdab6d629bf97f7c51e799ba73d2b5e69176 | [
"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 | 7,622 | 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.Expr
/-! Constant folding for primitives that have special runtime support. -/
namespace Lean.Compiler
def mkLcProof (p : Expr) :=
mkApp (mkConst ``lcProof []) p
abbrev BinFoldFn := Bool → Expr → Expr → Option Expr
abbrev UnFoldFn := Bool → Expr → Option Expr
def mkUIntTypeName (nbytes : Nat) : Name :=
Name.mkSimple ("UInt" ++ toString nbytes)
structure NumScalarTypeInfo where
nbits : Nat
id : Name := mkUIntTypeName nbits
ofNatFn : Name := Name.mkStr id "ofNat"
toNatFn : Name := Name.mkStr id "toNat"
size : Nat := 2^nbits
def numScalarTypes : List NumScalarTypeInfo :=
[{nbits := 8}, {nbits := 16}, {nbits := 32}, {nbits := 64},
{id := ``USize, nbits := System.Platform.numBits}]
def isOfNat (fn : Name) : Bool :=
numScalarTypes.any (fun info => info.ofNatFn == fn)
def isToNat (fn : Name) : Bool :=
numScalarTypes.any (fun info => info.toNatFn == fn)
def getInfoFromFn (fn : Name) : List NumScalarTypeInfo → Option NumScalarTypeInfo
| [] => none
| info::infos =>
if info.ofNatFn == fn then some info
else getInfoFromFn fn infos
def getInfoFromVal : Expr → Option NumScalarTypeInfo
| Expr.app (Expr.const fn _) _ => getInfoFromFn fn numScalarTypes
| _ => none
@[export lean_get_num_lit]
def getNumLit : Expr → Option Nat
| Expr.lit (Literal.natVal n) => some n
| Expr.app (Expr.const fn _) a => if isOfNat fn then getNumLit a else none
| _ => none
def mkUIntLit (info : NumScalarTypeInfo) (n : Nat) : Expr :=
mkApp (mkConst info.ofNatFn) (mkRawNatLit (n%info.size))
def mkUInt32Lit (n : Nat) : Expr :=
mkUIntLit {nbits := 32} n
def foldBinUInt (fn : NumScalarTypeInfo → Bool → Nat → Nat → Nat) (beforeErasure : Bool) (a₁ a₂ : Expr) : Option Expr := do
let n₁ ← getNumLit a₁
let n₂ ← getNumLit a₂
let info ← getInfoFromVal a₁
return mkUIntLit info (fn info beforeErasure n₁ n₂)
def foldUIntAdd := foldBinUInt fun _ _ => Add.add
def foldUIntMul := foldBinUInt fun _ _ => Mul.mul
def foldUIntDiv := foldBinUInt fun _ _ => Div.div
def foldUIntMod := foldBinUInt fun _ _ => Mod.mod
def foldUIntSub := foldBinUInt fun info _ a b => (a + (info.size - b))
def preUIntBinFoldFns : List (Name × BinFoldFn) :=
[(`add, foldUIntAdd), (`mul, foldUIntMul), (`div, foldUIntDiv),
(`mod, foldUIntMod), (`sub, foldUIntSub)]
def uintBinFoldFns : List (Name × BinFoldFn) :=
numScalarTypes.foldl (fun r info => r ++ (preUIntBinFoldFns.map (fun ⟨suffix, fn⟩ => (info.id ++ suffix, fn)))) []
def foldNatBinOp (fn : Nat → Nat → Nat) (a₁ a₂ : Expr) : Option Expr := do
let n₁ ← getNumLit a₁
let n₂ ← getNumLit a₂
return mkRawNatLit (fn n₁ n₂)
def foldNatAdd (_ : Bool) := foldNatBinOp Add.add
def foldNatMul (_ : Bool) := foldNatBinOp Mul.mul
def foldNatDiv (_ : Bool) := foldNatBinOp Div.div
def foldNatMod (_ : Bool) := foldNatBinOp Mod.mod
-- TODO: add option for controlling the limit
def natPowThreshold := 256
def foldNatPow (_ : Bool) (a₁ a₂ : Expr) : Option Expr := do
let n₁ ← getNumLit a₁
let n₂ ← getNumLit a₂
if n₂ < natPowThreshold then
return mkRawNatLit (n₁ ^ n₂)
else
failure
def mkNatEq (a b : Expr) : Expr :=
mkAppN (mkConst ``Eq [levelOne]) #[(mkConst `Nat), a, b]
def mkNatLt (a b : Expr) : Expr :=
mkAppN (mkConst ``LT.lt [levelZero]) #[mkConst ``Nat, mkConst ``Nat.lt, a, b]
def mkNatLe (a b : Expr) : Expr :=
mkAppN (mkConst ``LE.le [levelZero]) #[mkConst ``Nat, mkConst ``Nat.le, a, b]
def toDecidableExpr (beforeErasure : Bool) (pred : Expr) (r : Bool) : Expr :=
match beforeErasure, r with
| false, true => mkConst ``Bool.true
| false, false => mkConst ``Bool.false
| true, true => mkDecIsTrue pred (mkLcProof pred)
| true, false => mkDecIsFalse pred (mkLcProof pred)
def foldNatBinPred (mkPred : Expr → Expr → Expr) (fn : Nat → Nat → Bool)
(beforeErasure : Bool) (a₁ a₂ : Expr) : Option Expr := do
let n₁ ← getNumLit a₁
let n₂ ← getNumLit a₂
return toDecidableExpr beforeErasure (mkPred a₁ a₂) (fn n₁ n₂)
def foldNatDecEq := foldNatBinPred mkNatEq (fun a b => a = b)
def foldNatDecLt := foldNatBinPred mkNatLt (fun a b => a < b)
def foldNatDecLe := foldNatBinPred mkNatLe (fun a b => a ≤ b)
def foldNatBinBoolPred (fn : Nat → Nat → Bool) (a₁ a₂ : Expr) : Option Expr := do
let n₁ ← getNumLit a₁
let n₂ ← getNumLit a₂
if fn n₁ n₂ then
return mkConst ``Bool.true
else
return mkConst ``Bool.false
def foldNatBeq := fun _ : Bool => foldNatBinBoolPred (fun a b => a == b)
def foldNatBle := fun _ : Bool => foldNatBinBoolPred (fun a b => a < b)
def foldNatBlt := fun _ : Bool => foldNatBinBoolPred (fun a b => a ≤ b)
def natFoldFns : List (Name × BinFoldFn) :=
[(``Nat.add, foldNatAdd),
(``Nat.mul, foldNatMul),
(``Nat.div, foldNatDiv),
(``Nat.mod, foldNatMod),
(``Nat.pow, foldNatPow),
(``Nat.decEq, foldNatDecEq),
(``Nat.decLt, foldNatDecLt),
(``Nat.decLe, foldNatDecLe),
(``Nat.beq, foldNatBeq),
(``Nat.blt, foldNatBlt),
(``Nat.ble, foldNatBle)
]
def getBoolLit : Expr → Option Bool
| Expr.const ``Bool.true _ => some true
| Expr.const ``Bool.false _ => some false
| _ => none
def foldStrictAnd (_ : Bool) (a₁ a₂ : Expr) : Option Expr :=
let v₁ := getBoolLit a₁
let v₂ := getBoolLit a₂
match v₁, v₂ with
| some true, _ => a₂
| some false, _ => a₁
| _, some true => a₁
| _, some false => a₂
| _, _ => none
def foldStrictOr (_ : Bool) (a₁ a₂ : Expr) : Option Expr :=
let v₁ := getBoolLit a₁
let v₂ := getBoolLit a₂
match v₁, v₂ with
| some true, _ => a₁
| some false, _ => a₂
| _, some true => a₂
| _, some false => a₁
| _, _ => none
def boolFoldFns : List (Name × BinFoldFn) :=
[(``strictOr, foldStrictOr), (``strictAnd, foldStrictAnd)]
def binFoldFns : List (Name × BinFoldFn) :=
boolFoldFns ++ uintBinFoldFns ++ natFoldFns
def foldNatSucc (_ : Bool) (a : Expr) : Option Expr := do
let n ← getNumLit a
return mkRawNatLit (n+1)
def foldCharOfNat (beforeErasure : Bool) (a : Expr) : Option Expr := do
guard (!beforeErasure)
let n ← getNumLit a
if isValidChar n.toUInt32 then
return mkUInt32Lit n
else
return mkUInt32Lit 0
def foldToNat (_ : Bool) (a : Expr) : Option Expr := do
let n ← getNumLit a
return mkRawNatLit n
def uintFoldToNatFns : List (Name × UnFoldFn) :=
numScalarTypes.foldl (fun r info => (info.toNatFn, foldToNat) :: r) []
def unFoldFns : List (Name × UnFoldFn) :=
[(``Nat.succ, foldNatSucc),
(``Char.ofNat, foldCharOfNat)]
++ uintFoldToNatFns
def findBinFoldFn (fn : Name) : Option BinFoldFn :=
binFoldFns.lookup fn
def findUnFoldFn (fn : Name) : Option UnFoldFn :=
unFoldFns.lookup fn
@[export lean_fold_bin_op]
def foldBinOp (beforeErasure : Bool) (f : Expr) (a : Expr) (b : Expr) : Option Expr := do
match f with
| Expr.const fn _ =>
let foldFn ← findBinFoldFn fn
foldFn beforeErasure a b
| _ =>
failure
@[export lean_fold_un_op]
def foldUnOp (beforeErasure : Bool) (f : Expr) (a : Expr) : Option Expr := do
match f with
| Expr.const fn _ =>
let foldFn ← findUnFoldFn fn
foldFn beforeErasure a
| _ => failure
end Lean.Compiler
|
1c7bdb90a9ffc231e5e0a6ceb9d897ec37e085da | 9dd3f3912f7321eb58ee9aa8f21778ad6221f87c | /tests/lean/run/pack_unpack1.lean | 4eab0c83f7e7318d12f58fcfc67fb5f9e30e1d24 | [
"Apache-2.0"
] | permissive | bre7k30/lean | de893411bcfa7b3c5572e61b9e1c52951b310aa4 | 5a924699d076dab1bd5af23a8f910b433e598d7a | refs/heads/master | 1,610,900,145,817 | 1,488,006,845,000 | 1,488,006,845,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,281 | lean | inductive {u} tree_core (A : Type u) : bool → (Type u)
| leaf' : A → tree_core ff
| node' : tree_core tt → tree_core ff
| nil' {} : tree_core tt
| cons' : tree_core ff → tree_core tt → tree_core tt
attribute [reducible]
definition tree (A : Sort*) := tree_core A ff
attribute [reducible]
definition tree_list (A : Sort*) := tree_core A tt
open tree_core
definition pack {A : Sort*} : list (tree A) → tree_core A tt
| [] := nil'
| (a::l) := cons' a (pack l)
definition unpack {A : Sort*} : ∀ {b}, tree_core A b → list (tree A)
| .tt nil' := []
| .tt (cons' a t) := a :: unpack t
| .ff (leaf' a) := []
| .ff (node' l) := []
attribute [inverse]
lemma unpack_pack {A : Sort*} : ∀ (l : list (tree A)), unpack (pack l) = l
| [] := rfl
| (a::l) :=
show a :: unpack (pack l) = a :: l, from
congr_arg (λ x, a :: x) (unpack_pack l)
attribute [inverse]
lemma pack_unpack {A : Sort*} : ∀ t : tree_core A tt, pack (unpack t) = t :=
λ t,
@tree_core.rec_on
A
(λ b, bool.cases_on b (λ t, true) (λ t, pack (unpack t) = t))
tt t
(λ a, trivial)
(λ t ih, trivial)
rfl
(λ h t ih1 ih2,
show cons' h (pack (unpack t)) = cons' h t, from
congr_arg (λ x, cons' h x) ih2)
attribute [pattern]
definition tree.node {A : Sort*} (l : list (tree A)) : tree A :=
tree_core.node' (pack l)
attribute [pattern]
definition tree.leaf {A : Sort*} : A → tree A :=
tree_core.leaf'
set_option trace.eqn_compiler true
definition sz {A : Sort*} : tree A → nat
| (tree.leaf a) := 1
| (tree.node l) := list.length l + 1
constant P {A : Sort*} : tree A → Type 1
constant mk1 {A : Sort*} (a : A) : P (tree.leaf a)
constant mk2 {A : Sort*} (l : list (tree A)) : P (tree.node l)
noncomputable definition bla {A : Sort*} : ∀ n : tree A, P n
| (tree.leaf a) := mk1 a
| (tree.node l) := mk2 l
check bla._main.equations._eqn_1
check bla._main.equations._eqn_2
definition foo {A : Sort*} : nat → tree A → nat
| 0 _ := 0
| (n+1) (tree.leaf a) := 0
| (n+1) (tree.node []) := foo n (tree.node [])
| (n+1) (tree.node (x::xs)) := foo n x
check @foo._main.equations._eqn_1
check @foo._main.equations._eqn_2
check @foo._main.equations._eqn_3
check @foo._main.equations._eqn_4
|
0b6d70aaf884354cc7f9d130d5e1f70575e43fe8 | 5d166a16ae129621cb54ca9dde86c275d7d2b483 | /library/init/data/list/instances.lean | 08009b92c4a6ca5b934c3d2a56fcfcc59e3511f4 | [
"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 | 5,729 | lean | /-
Copyright (c) 2016 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
-/
prelude
import init.data.list.lemmas
import init.meta.mk_dec_eq_instance
open list
universes u v
local attribute [simp] map join ret list.append append_nil
section
variables {α : Type u} {β : Type v} (x : α) (xs ys : list α) (f : α → list β)
private lemma nil_bind : list.bind nil f = nil :=
by simp [list.bind]
private lemma cons_bind : list.bind (x :: xs) f = f x ++ list.bind xs f :=
by simp [list.bind]
private lemma append_bind : list.bind (xs ++ ys) f = list.bind xs f ++ list.bind ys f :=
begin
induction xs,
{ refl },
{ simph [cons_bind] }
end
end
local attribute [simp] nil_bind cons_bind append_bind
instance : monad list :=
{pure := @list.ret, bind := @list.bind,
id_map := begin
intros _ xs, induction xs with x xs ih,
{ refl },
{ dsimp at ih, dsimp, simph }
end,
pure_bind := by simp_intros,
bind_assoc := begin
intros _ _ _ xs _ _, induction xs,
{ refl },
{ simph }
end}
instance : alternative list :=
{ list.monad with
failure := @list.nil,
orelse := @list.append }
instance {α : Type u} [decidable_eq α] : decidable_eq (list α) :=
by tactic.mk_dec_eq_instance
instance : decidable_eq string :=
by tactic.mk_dec_eq_instance
namespace list
variables {α β : Type u} (p : α → Prop) [decidable_pred p]
lemma mem_bind_iff {b : β} {l : list α} {f : α → list β} : b ∈ l >>= f ↔ ∃ a ∈ l, b ∈ f a :=
iff.trans mem_join_iff
⟨λ ⟨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 _ al, bfa⟩⟩
lemma exists_of_mem_bind {b : β} {l : list α} {f : α → list β} : b ∈ l >>= f → ∃ a ∈ l, b ∈ f a :=
mem_bind_iff.1
lemma mem_bind {b : β} {l : list α} {f : α → list β} {a} (al : a ∈ l) (h : b ∈ f a) : b ∈ l >>= f :=
mem_bind_iff.2 ⟨a, al, h⟩
instance decidable_bex : ∀ (l : list α), decidable (∃ x ∈ l, p x)
| [] := is_false (by intro; cases a; cases a_2; cases a)
| (x::xs) :=
if hx : p x then
is_true ⟨x, or.inl rfl, hx⟩
else
match decidable_bex xs with
| is_true hxs := is_true $ begin
cases hxs with x' hx', cases hx' with hx' hpx',
existsi x', existsi (or.inr hx'), assumption
end
| is_false hxs := is_false $ begin
intro hxxs, cases hxxs with x' hx', cases hx' with hx' hpx',
cases hx', cc,
apply hxs, existsi x', existsi a, assumption
end
end
instance decidable_ball (l : list α) : decidable (∀ x ∈ l, p x) :=
if h : ∃ x ∈ l, ¬ p x then
is_false $ begin cases h with x h, cases h with hx h, intro h', apply h, apply h', assumption end
else
is_true $ λ x hx, if h' : p x then h' else false.elim $ h ⟨x, hx, h'⟩
instance decidable_prefix [decidable_eq α] : ∀ (l₁ l₂ : list α), decidable (l₁ <+: l₂)
| [] l₂ := is_true ⟨l₂, rfl⟩
| (a::l₁) [] := is_false $ λ⟨t, te⟩, list.no_confusion te
| (a::l₁) (b::l₂) :=
if h : a = b then
decidable_of_decidable_of_iff (decidable_prefix l₁ l₂) $ by rw -h; exact
⟨λ⟨t, te⟩, ⟨t, by rw -te; refl⟩,
λ⟨t, te⟩, list.no_confusion te (λ_ te, ⟨t, te⟩)⟩
else
is_false $ λ⟨t, te⟩, list.no_confusion te $ λh', absurd h' h
-- Alternatively, use mem_tails
instance decidable_suffix [decidable_eq α] : ∀ (l₁ l₂ : list α), decidable (l₁ <:+ l₂)
| [] l₂ := is_true ⟨l₂, append_nil _⟩
| (a::l₁) [] := is_false $ λ⟨t, te⟩, absurd te $
append_ne_nil_of_ne_nil_right _ _ $ λh, list.no_confusion h
| l₁ l₂ := let len1 := length l₁, len2 := length l₂ in
if hl : len1 ≤ len2 then
if he : dropn (len2 - len1) l₂ = l₁ then is_true $
⟨taken (len2 - len1) l₂, by rw [-he, taken_append_dropn]⟩
else is_false $
suffices length l₁ ≤ length l₂ → l₁ <:+ l₂ → dropn (length l₂ - length l₁) l₂ = l₁,
from λsuf, he (this hl suf),
λ hl ⟨t, te⟩, and.right $
append_right_inj (eq.trans (taken_append_dropn (length l₂ - length l₁) l₂) te.symm) $
by simp; exact nat.sub_sub_self hl
else is_false $ λ⟨t, te⟩, hl $
show length l₁ ≤ length l₂, by rw [-te, length_append]; apply nat.le_add_left
instance decidable_infix [decidable_eq α] : ∀ (l₁ l₂ : list α), decidable (l₁ <:+: l₂)
| [] l₂ := is_true ⟨[], l₂, rfl⟩
| (a::l₁) [] := is_false $ λ⟨s, t, te⟩, absurd te $ append_ne_nil_of_ne_nil_left _ _ $
append_ne_nil_of_ne_nil_right _ _ $ λh, list.no_confusion h
| l₁ l₂ := decidable_of_decidable_of_iff (list.decidable_bex (λt, l₁ <+: t) (tails l₂)) $
by refine (exists_congr (λt, _)).trans (infix_iff_prefix_suffix _ _).symm;
exact ⟨λ⟨h1, h2⟩, ⟨h2, (mem_tails _ _).1 h1⟩, λ⟨h2, h1⟩, ⟨(mem_tails _ _).2 h1, h2⟩⟩
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 ⟨cons_sublist_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⟩
end list
|
227a5c367c5bd27af8e8d09cf558a035709fe251 | b7f22e51856f4989b970961f794f1c435f9b8f78 | /library/data/pnat.lean | dcb6a70676210633de3d6672da234d2ed653f7c1 | [
"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 | 12,520 | lean | /-
Copyright (c) 2015 Robert Y. Lewis. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Robert Y. Lewis
Basic facts about the positive natural numbers.
Developed primarily for use in the construction of ℝ. For the most part, the only theorems here
are those needed for that construction.
-/
import data.rat.order data.nat
open nat rat subtype eq.ops
definition pnat := { n : ℕ | n > 0 }
namespace pnat
protected definition prio := num.pred nat.prio
notation `ℕ+` := pnat
definition pos (n : ℕ) (H : n > 0) : ℕ+ := tag n H
definition nat_of_pnat (p : ℕ+) : ℕ := elt_of p
reserve postfix `~`:std.prec.max_plus
local postfix ~ := nat_of_pnat
theorem pnat_pos (p : ℕ+) : p~ > 0 := has_property p
protected definition add (p q : ℕ+) : ℕ+ :=
tag (p~ + q~) (add_pos (pnat_pos p) (pnat_pos q))
protected definition mul (p q : ℕ+) : ℕ+ :=
tag (p~ * q~) (mul_pos (pnat_pos p) (pnat_pos q))
protected definition le (p q : ℕ+) := p~ ≤ q~
protected definition lt (p q : ℕ+) := p~ < q~
definition pnat_has_add [instance] [priority pnat.prio] : has_add pnat :=
has_add.mk pnat.add
definition pnat_has_mul [instance] [priority pnat.prio] : has_mul pnat :=
has_mul.mk pnat.mul
definition pnat_has_le [instance] [priority pnat.prio] : has_le pnat :=
has_le.mk pnat.le
definition pnat_has_lt [instance] [priority pnat.prio] : has_lt pnat :=
has_lt.mk pnat.lt
definition pnat_has_one [instance] [priority pnat.prio] : has_one pnat :=
has_one.mk (pos (1:nat) dec_trivial)
protected lemma mul_def (p q : ℕ+) : p * q = tag (p~ * q~) (mul_pos (pnat_pos p) (pnat_pos q)) :=
rfl
protected lemma le_def (p q : ℕ+) : (p ≤ q) = (p~ ≤ q~) :=
rfl
protected lemma lt_def (p q : ℕ+) : (p < q) = (p~ < q~) :=
rfl
protected theorem pnat.eq {p q : ℕ+} : p~ = q~ → p = q :=
subtype.eq
protected definition decidable_lt : decidable_rel pnat.lt :=
λa b, nat.decidable_lt a~ b~
protected theorem le_refl (a : ℕ+) : a ≤ a :=
begin rewrite pnat.le_def end
protected theorem le_trans {p q r : ℕ+} : p ≤ q → q ≤ r → p ≤ r :=
begin rewrite *pnat.le_def, apply nat.le_trans end
protected theorem le_antisymm {n m : ℕ+} : n ≤ m → m ≤ n → n = m :=
begin rewrite +pnat.le_def, intros, apply (pnat.eq (nat.le_antisymm a a_1)) end
protected theorem le_iff_lt_or_eq (m n : ℕ+) : m ≤ n ↔ m < n ∨ m = n :=
begin
rewrite [pnat.lt_def, pnat.le_def], apply iff.intro,
{ intro, apply or.elim (nat.lt_or_eq_of_le a),
intro, apply or.intro_left, assumption,
intro, apply or.intro_right, apply pnat.eq, assumption },
{ intro, apply or.elim a, apply nat.le_of_lt, intro, rewrite a_1 }
end
protected theorem le_total (m n : ℕ+) : m ≤ n ∨ n ≤ m :=
begin rewrite pnat.le_def, apply nat.le_total end
protected theorem lt_irrefl (a : ℕ+) : ¬ a < a :=
begin rewrite pnat.lt_def, apply nat.lt_irrefl end
protected definition decidable_linear_order [trans_instance] : decidable_linear_order pnat :=
⦃ decidable_linear_order,
le := pnat.le,
le_refl := by apply pnat.le_refl,
le_trans := by apply @pnat.le_trans,
le_antisymm := by apply @pnat.le_antisymm,
lt := pnat.lt,
lt_irrefl := by apply pnat.lt_irrefl,
le_iff_lt_or_eq := by apply pnat.le_iff_lt_or_eq,
decidable_lt := pnat.decidable_lt,
le_total := by apply pnat.le_total ⦄
notation 2 := (tag 2 dec_trivial : ℕ+)
notation 3 := (tag 3 dec_trivial : ℕ+)
definition pone : ℕ+ := tag 1 dec_trivial
definition rat_of_pnat [reducible] (n : ℕ+) : ℚ :=
n~
theorem pnat.to_rat_of_nat (n : ℕ+) : rat_of_pnat n = of_nat n~ :=
rfl
-- these will come in rat
theorem rat_of_nat_nonneg (n : ℕ) : 0 ≤ of_nat n :=
trivial
theorem rat_of_pnat_ge_one (n : ℕ+) : rat_of_pnat n ≥ 1 :=
of_nat_le_of_nat_of_le (pnat_pos n)
theorem rat_of_pnat_is_pos (n : ℕ+) : rat_of_pnat n > 0 :=
of_nat_lt_of_nat_of_lt (pnat_pos n)
theorem of_nat_le_of_nat_of_le {m n : ℕ} (H : m ≤ n) : of_nat m ≤ of_nat n :=
of_nat_le_of_nat_of_le H
theorem of_nat_lt_of_nat_of_lt {m n : ℕ} (H : m < n) : of_nat m < of_nat n :=
of_nat_lt_of_nat_of_lt H
theorem rat_of_pnat_le_of_pnat_le {m n : ℕ+} (H : m ≤ n) : rat_of_pnat m ≤ rat_of_pnat n :=
begin rewrite pnat.le_def at H, exact of_nat_le_of_nat_of_le H end
theorem rat_of_pnat_lt_of_pnat_lt {m n : ℕ+} (H : m < n) : rat_of_pnat m < rat_of_pnat n :=
begin rewrite pnat.lt_def at H, exact of_nat_lt_of_nat_of_lt H end
theorem pnat_le_of_rat_of_pnat_le {m n : ℕ+} (H : rat_of_pnat m ≤ rat_of_pnat n) : m ≤ n :=
begin rewrite pnat.le_def, exact le_of_of_nat_le_of_nat H end
definition inv (n : ℕ+) : ℚ :=
(1 : ℚ) / rat_of_pnat n
local postfix `⁻¹` := inv
protected theorem inv_pos (n : ℕ+) : n⁻¹ > 0 := one_div_pos_of_pos !rat_of_pnat_is_pos
theorem inv_le_one (n : ℕ+) : n⁻¹ ≤ (1 : ℚ) :=
begin
unfold inv,
change 1 / rat_of_pnat n ≤ 1 / 1,
apply one_div_le_one_div_of_le,
apply zero_lt_one,
apply rat_of_pnat_ge_one
end
theorem inv_lt_one_of_gt {n : ℕ+} (H : n~ > 1) : n⁻¹ < (1 : ℚ) :=
begin
unfold inv,
change 1 / rat_of_pnat n < 1 / 1,
apply one_div_lt_one_div_of_lt,
apply zero_lt_one,
rewrite pnat.to_rat_of_nat,
apply (of_nat_lt_of_nat_of_lt H)
end
theorem pone_inv : pone⁻¹ = 1 := rfl
theorem add_invs_nonneg (m n : ℕ+) : 0 ≤ m⁻¹ + n⁻¹ :=
begin
apply le_of_lt,
apply add_pos,
repeat apply pnat.inv_pos
end
protected theorem one_mul (n : ℕ+) : pone * n = n :=
begin
apply pnat.eq,
unfold pone,
rewrite [pnat.mul_def, ↑nat_of_pnat, one_mul]
end
theorem pone_le (n : ℕ+) : pone ≤ n :=
begin rewrite pnat.le_def, exact succ_le_of_lt (pnat_pos n) end
theorem pnat_to_rat_mul (a b : ℕ+) : rat_of_pnat (a * b) = rat_of_pnat a * rat_of_pnat b := rfl
theorem mul_lt_mul_left {a b c : ℕ+} (H : a < b) : a * c < b * c :=
begin rewrite [pnat.lt_def at *], exact mul_lt_mul_of_pos_right H !pnat_pos end
theorem one_lt_two : pone < 2 :=
!nat.le_refl
theorem inv_two_mul_lt_inv (n : ℕ+) : (2 * n)⁻¹ < n⁻¹ :=
begin
rewrite ↑inv,
apply one_div_lt_one_div_of_lt,
apply rat_of_pnat_is_pos,
have H : n~ < (2 * n)~, begin
rewrite -(pnat.one_mul n) at {1},
rewrite -pnat.lt_def,
apply mul_lt_mul_left,
apply one_lt_two
end,
apply of_nat_lt_of_nat_of_lt,
apply H
end
theorem inv_two_mul_le_inv (n : ℕ+) : (2 * n)⁻¹ ≤ n⁻¹ := rat.le_of_lt !inv_two_mul_lt_inv
theorem inv_ge_of_le {p q : ℕ+} (H : p ≤ q) : q⁻¹ ≤ p⁻¹ :=
one_div_le_one_div_of_le !rat_of_pnat_is_pos (rat_of_pnat_le_of_pnat_le H)
theorem inv_gt_of_lt {p q : ℕ+} (H : p < q) : q⁻¹ < p⁻¹ :=
one_div_lt_one_div_of_lt !rat_of_pnat_is_pos (rat_of_pnat_lt_of_pnat_lt H)
theorem ge_of_inv_le {p q : ℕ+} (H : p⁻¹ ≤ q⁻¹) : q ≤ p :=
pnat_le_of_rat_of_pnat_le (le_of_one_div_le_one_div !rat_of_pnat_is_pos H)
theorem two_mul (p : ℕ+) : rat_of_pnat (2 * p) = (1 + 1) * rat_of_pnat p :=
by rewrite pnat_to_rat_mul
protected theorem add_halves (p : ℕ+) : (2 * p)⁻¹ + (2 * p)⁻¹ = p⁻¹ :=
begin
rewrite [↑inv, -(add_halves (1 / (rat_of_pnat p))), div_div_eq_div_mul],
have H : rat_of_pnat (2 * p) = rat_of_pnat p * (1 + 1), by rewrite [rat.mul_comm, two_mul],
rewrite *H
end
theorem add_halves_double (m n : ℕ+) :
m⁻¹ + n⁻¹ = ((2 * m)⁻¹ + (2 * n)⁻¹) + ((2 * m)⁻¹ + (2 * n)⁻¹) :=
have hsimp : ∀ a b : ℚ, (a + a) + (b + b) = (a + b) + (a + b),
by intros; rewrite [rat.add_assoc, -(rat.add_assoc a b b), {_+b}rat.add_comm, -*rat.add_assoc],
by rewrite [-pnat.add_halves m, -pnat.add_halves n, hsimp]
protected theorem inv_mul_eq_mul_inv {p q : ℕ+} : (p * q)⁻¹ = p⁻¹ * q⁻¹ :=
begin rewrite [↑inv, pnat_to_rat_mul, one_div_mul_one_div] end
protected theorem inv_mul_le_inv (p q : ℕ+) : (p * q)⁻¹ ≤ q⁻¹ :=
begin
rewrite [pnat.inv_mul_eq_mul_inv, -{q⁻¹}rat.one_mul at {2}],
apply mul_le_mul,
apply inv_le_one,
apply le.refl,
apply le_of_lt,
apply pnat.inv_pos,
apply rat.le_of_lt rat.zero_lt_one
end
theorem pnat_mul_le_mul_left' (a b c : ℕ+) : a ≤ b → c * a ≤ c * b :=
begin
rewrite +pnat.le_def, intro H,
apply mul_le_mul_of_nonneg_left H,
apply le_of_lt,
apply pnat_pos
end
protected theorem mul_assoc (a b c : ℕ+) : a * b * c = a * (b * c) :=
pnat.eq !mul.assoc
protected theorem mul_comm (a b : ℕ+) : a * b = b * a :=
pnat.eq !mul.comm
protected theorem add_assoc (a b c : ℕ+) : a + b + c = a + (b + c) :=
pnat.eq !add.assoc
protected theorem mul_le_mul_left (p q : ℕ+) : q ≤ p * q :=
begin
rewrite [-pnat.one_mul q at {1}, pnat.mul_comm, pnat.mul_comm p],
apply pnat_mul_le_mul_left',
apply pone_le
end
protected theorem mul_le_mul_right (p q : ℕ+) : p ≤ p * q :=
by rewrite pnat.mul_comm; apply pnat.mul_le_mul_left
protected theorem inv_cancel_left (p : ℕ+) : rat_of_pnat p * p⁻¹ = (1 : ℚ) :=
mul_one_div_cancel (ne.symm (ne_of_lt !rat_of_pnat_is_pos))
protected theorem inv_cancel_right (p : ℕ+) : p⁻¹ * rat_of_pnat p = (1 : ℚ) :=
by rewrite rat.mul_comm; apply pnat.inv_cancel_left
theorem lt_add_left (p q : ℕ+) : p < p + q :=
begin
have H : p~ < p~ + q~, begin
rewrite -(nat.add_zero (p~)) at {1},
apply nat.add_lt_add_left,
apply pnat_pos
end,
apply H
end
theorem inv_add_lt_left (p q : ℕ+) : (p + q)⁻¹ < p⁻¹ :=
by apply inv_gt_of_lt; apply lt_add_left
theorem div_le_pnat (q : ℚ) (n : ℕ+) (H : q ≥ n⁻¹) : 1 / q ≤ rat_of_pnat n :=
begin
apply div_le_of_le_mul,
apply lt_of_lt_of_le,
apply pnat.inv_pos,
rotate 1,
apply H,
apply le_mul_of_div_le,
apply rat_of_pnat_is_pos,
apply H
end
theorem pnat_cancel' (n m : ℕ+) : (n * n * m)⁻¹ * (rat_of_pnat n * rat_of_pnat n) = m⁻¹ :=
have hsimp : ∀ a b c : ℚ, (a * a * (b * b * c)) = (a * b) * (a * b) * c,
begin
intro a b c,
rewrite[-*rat.mul_assoc],
exact (!mul.right_comm ▸ rfl),
end,
by rewrite [rat.mul_comm, *pnat.inv_mul_eq_mul_inv, hsimp, *pnat.inv_cancel_left, *rat.one_mul]
definition pceil (a : ℚ) : ℕ+ := tag (ubound a) !ubound_pos
theorem pceil_helper {a : ℚ} {n : ℕ+} (H : pceil a ≤ n) (Ha : a > 0) : n⁻¹ ≤ 1 / a :=
le.trans (inv_ge_of_le H) (one_div_le_one_div_of_le Ha (ubound_ge a))
theorem inv_pceil_div (a b : ℚ) (Ha : a > 0) (Hb : b > 0) : (pceil (a / b))⁻¹ ≤ b / a :=
have (pceil (a / b))⁻¹ ≤ 1 / (1 / (b / a)),
begin
apply one_div_le_one_div_of_le,
show 0 < 1 / (b / a), from
one_div_pos_of_pos (div_pos_of_pos_of_pos Hb Ha),
show 1 / (b / a) ≤ rat_of_pnat (pceil (a / b)),
begin
rewrite div_div_eq_mul_div,
rewrite one_mul,
apply ubound_ge
end
end,
begin
rewrite one_div_one_div at this,
exact this
end
theorem sep_by_inv {a b : ℚ} : a > b → ∃ N : ℕ+, a > (b + N⁻¹ + N⁻¹) :=
begin
change b < a → ∃ N : ℕ+, (b + N⁻¹ + N⁻¹) < a,
intro H,
apply exists.elim (exists_add_lt_and_pos_of_lt H),
intro c Hc,
existsi (pceil ((1 + 1 + 1) / c)),
apply lt.trans,
rotate 1,
apply and.left Hc,
rewrite rat.add_assoc,
apply rat.add_lt_add_left,
rewrite -(add_halves c) at {3},
apply add_lt_add,
repeat (apply lt_of_le_of_lt;
apply inv_pceil_div;
apply dec_trivial;
apply and.right Hc;
apply div_lt_div_of_pos_of_lt_of_pos;
apply two_pos;
exact dec_trivial;
apply and.right Hc)
end
theorem nonneg_of_ge_neg_invs (a : ℚ) : (∀ n : ℕ+, -n⁻¹ ≤ a) → 0 ≤ a :=
begin
intro H,
apply le_of_not_gt,
suppose a < 0,
have H2 : 0 < -a, from neg_pos_of_neg this,
(not_lt_of_ge !H) (iff.mp !lt_neg_iff_lt_neg (calc
(pceil (of_num 2 / -a))⁻¹ ≤ -a / of_num 2
: !inv_pceil_div dec_trivial H2
... < -a / 1
: div_lt_div_of_pos_of_lt_of_pos dec_trivial dec_trivial H2
... = -a : !div_one))
end
theorem pnat_bound {ε : ℚ} (Hε : ε > 0) : ∃ p : ℕ+, p⁻¹ ≤ ε :=
begin
existsi (pceil (1 / ε)),
rewrite -(one_div_one_div ε) at {2},
apply pceil_helper,
apply pnat.le_refl,
apply one_div_pos_of_pos Hε
end
theorem p_add_fractions (n : ℕ+) : (2 * n)⁻¹ + (2 * 3 * n)⁻¹ + (3 * n)⁻¹ = n⁻¹ :=
have T : 2⁻¹ + 2⁻¹ * 3⁻¹ + 3⁻¹ = 1, from dec_trivial,
by rewrite[*pnat.inv_mul_eq_mul_inv,-*right_distrib,T,rat.one_mul]
theorem rat_power_two_le (k : ℕ+) : rat_of_pnat k ≤ 2^k~ :=
!binary_nat_bound
end pnat
|
8efbbeffcf00863512d2c11ea472c0fc70072ed3 | dd0f5513e11c52db157d2fcc8456d9401a6cd9da | /12_Axioms.org.32.lean | 565ddae25679c9b0be76d45c63f3b27199be44e7 | [] | no_license | cjmazey/lean-tutorial | ba559a49f82aa6c5848b9bf17b7389bf7f4ba645 | 381f61c9fcac56d01d959ae0fa6e376f2c4e3b34 | refs/heads/master | 1,610,286,098,832 | 1,447,124,923,000 | 1,447,124,923,000 | 43,082,433 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 812 | lean | import standard
import data.encodable
open encodable function
section
parameters {A B : Type}
parameter (f : A → B)
parameter [inhA : inhabited A]
parameter [dex : ∀ b, decidable (∃ a, f a = b)]
parameter [encB : encodable A]
parameter [deqB : decidable_eq B]
include inhA dex encB deqB
definition finv : B → A :=
λ b : B, if ex : (∃ a, f a = b) then choose ex else arbitrary A
theorem has_left_inverse_of_injective : injective f → has_left_inverse f :=
assume inj : ∀ a₁ a₂, f a₁ = f a₂ → a₁ = a₂,
have is_linv : ∀ a, finv (f a) = a, from
(take a,
assert ex : ∃ a₁, f a₁ = f a, from exists.intro a rfl,
have feq : f (choose ex) = f a, from !choose_spec,
calc finv (f a) = choose ex : dif_pos ex
... = a : inj _ _ feq),
exists.intro finv is_linv
end
|
c2ed9f37aabff70706ca902744c000f2f1d1113a | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/analysis/hofer.lean | 969be1837e0e05f44c7444bc4540bb30b5ceb9ba | [
"Apache-2.0"
] | permissive | jjgarzella/mathlib | 96a345378c4e0bf26cf604aed84f90329e4896a2 | 395d8716c3ad03747059d482090e2bb97db612c8 | refs/heads/master | 1,686,480,124,379 | 1,625,163,323,000 | 1,625,163,323,000 | 281,190,421 | 2 | 0 | Apache-2.0 | 1,595,268,170,000 | 1,595,268,169,000 | null | UTF-8 | Lean | false | false | 4,598 | lean | /-
Copyright (c) 2020 Patrick Massot. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Patrick Massot
-/
import analysis.specific_limits
/-!
# Hofer's lemma
This is an elementary lemma about complete metric spaces. It is motivated by an
application to the bubbling-off analysis for holomorphic curves in symplectic topology.
We are *very* far away from having these applications, but the proof here is a nice
example of a proof needing to construct a sequence by induction in the middle of the proof.
## References:
* H. Hofer and C. Viterbo, *The Weinstein conjecture in the presence of holomorphic spheres*
-/
open_locale classical topological_space big_operators
open filter finset
local notation `d` := dist
lemma hofer {X: Type*} [metric_space X] [complete_space X]
(x : X) (ε : ℝ) (ε_pos : 0 < ε)
{ϕ : X → ℝ} (cont : continuous ϕ) (nonneg : ∀ y, 0 ≤ ϕ y) :
∃ (ε' > 0) (x' : X), ε' ≤ ε ∧
d x' x ≤ 2*ε ∧
ε * ϕ(x) ≤ ε' * ϕ x' ∧
∀ y, d x' y ≤ ε' → ϕ y ≤ 2*ϕ x' :=
begin
by_contradiction H,
have reformulation : ∀ x' (k : ℕ), ε * ϕ x ≤ ε / 2 ^ k * ϕ x' ↔ 2^k * ϕ x ≤ ϕ x',
{ intros x' k,
rw [div_mul_eq_mul_div, le_div_iff, mul_assoc, mul_le_mul_left ε_pos, mul_comm],
exact pow_pos (by norm_num) k, },
-- Now let's specialize to `ε/2^k`
replace H : ∀ k : ℕ, ∀ x', d x' x ≤ 2 * ε ∧ 2^k * ϕ x ≤ ϕ x' →
∃ y, d x' y ≤ ε/2^k ∧ 2 * ϕ x' < ϕ y,
{ intros k x',
push_neg at H,
simpa [reformulation] using
H (ε/2^k) (by simp [ε_pos, zero_lt_two]) x' (by simp [ε_pos, zero_lt_two, one_le_two]) },
clear reformulation,
haveI : nonempty X := ⟨x⟩,
choose! F hF using H, -- Use the axiom of choice
-- Now define u by induction starting at x, with u_{n+1} = F(n, u_n)
let u : ℕ → X := λ n, nat.rec_on n x F,
have hu0 : u 0 = x := rfl,
-- The properties of F translate to properties of u
have hu :
∀ n,
d (u n) x ≤ 2 * ε ∧ 2^n * ϕ x ≤ ϕ (u n) →
d (u n) (u $ n + 1) ≤ ε / 2 ^ n ∧ 2 * ϕ (u n) < ϕ (u $ n + 1),
{ intro n,
exact hF n (u n) },
clear hF,
-- Key properties of u, to be proven by induction
have key : ∀ n, d (u n) (u (n + 1)) ≤ ε / 2 ^ n ∧ 2 * ϕ (u n) < ϕ (u (n + 1)),
{ intro n,
induction n using nat.case_strong_induction_on with n IH,
{ specialize hu 0,
simpa [hu0, mul_nonneg_iff, zero_le_one, ε_pos.le, le_refl] using hu },
have A : d (u (n+1)) x ≤ 2 * ε,
{ rw [dist_comm],
let r := range (n+1), -- range (n+1) = {0, ..., n}
calc
d (u 0) (u (n + 1))
≤ ∑ i in r, d (u i) (u $ i+1) : dist_le_range_sum_dist u (n + 1)
... ≤ ∑ i in r, ε/2^i : sum_le_sum (λ i i_in, (IH i $ nat.lt_succ_iff.mp $
finset.mem_range.mp i_in).1)
... = ∑ i in r, (1/2)^i*ε : by { congr' with i, field_simp }
... = (∑ i in r, (1/2)^i)*ε : finset.sum_mul.symm
... ≤ 2*ε : mul_le_mul_of_nonneg_right (sum_geometric_two_le _)
(le_of_lt ε_pos), },
have B : 2^(n+1) * ϕ x ≤ ϕ (u (n + 1)),
{ refine @geom_le (ϕ ∘ u) _ zero_le_two (n + 1) (λ m hm, _),
exact (IH _ $ nat.lt_add_one_iff.1 hm).2.le },
exact hu (n+1) ⟨A, B⟩, },
cases forall_and_distrib.mp key with key₁ key₂,
clear hu key,
-- Hence u is Cauchy
have cauchy_u : cauchy_seq u,
{ refine cauchy_seq_of_le_geometric _ ε one_half_lt_one (λ n, _),
simpa only [one_div, inv_pow'] using key₁ n },
-- So u converges to some y
obtain ⟨y, limy⟩ : ∃ y, tendsto u at_top (𝓝 y),
from complete_space.complete cauchy_u,
-- And ϕ ∘ u goes to +∞
have lim_top : tendsto (ϕ ∘ u) at_top at_top,
{ let v := λ n, (ϕ ∘ u) (n+1),
suffices : tendsto v at_top at_top,
by rwa tendsto_add_at_top_iff_nat at this,
have hv₀ : 0 < v 0,
{ have : 0 ≤ ϕ (u 0) := nonneg x,
calc 0 ≤ 2 * ϕ (u 0) : by linarith
... < ϕ (u (0 + 1)) : key₂ 0 },
apply tendsto_at_top_of_geom_le hv₀ one_lt_two,
exact λ n, (key₂ (n+1)).le },
-- But ϕ ∘ u also needs to go to ϕ(y)
have lim : tendsto (ϕ ∘ u) at_top (𝓝 (ϕ y)),
from tendsto.comp cont.continuous_at limy,
-- So we have our contradiction!
exact not_tendsto_at_top_of_tendsto_nhds lim lim_top,
end
|
a39ffaef209b148d825cc5eb3caf462851c08749 | d436468d80b739ba7e06843c4d0d2070e43448e5 | /src/algebra/lie_algebra.lean | dc342b6476b55eefe1251dfd53640a39af9cb10b | [
"Apache-2.0"
] | permissive | roro47/mathlib | 761fdc002aef92f77818f3fef06bf6ec6fc1a28e | 80aa7d52537571a2ca62a3fdf71c9533a09422cf | refs/heads/master | 1,599,656,410,625 | 1,573,649,488,000 | 1,573,649,488,000 | 221,452,951 | 0 | 0 | Apache-2.0 | 1,573,647,693,000 | 1,573,647,692,000 | null | UTF-8 | Lean | false | false | 7,160 | lean | /-
Copyright (c) 2019 Oliver Nash. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Oliver Nash
-/
import ring_theory.algebra data.matrix.basic
/-!
# Lie algebras
This file defines Lie rings, and Lie algebras over a commutative ring. It shows how these arise from
associative rings and algebras via the ring commutator. In particular it defines the Lie algebra
of endomorphisms of a module as well as of the algebra of square matrices over a commutative ring.
## Notations
We introduce the notation ⁅x, y⁆ for the Lie bracket. Note that these are the Unicode "square with
quill" brackets rather than the usual square brackets.
## Implementation notes
Lie algebras are defined as modules with a compatible Lie ring structure, and thus are partially
unbundled. Since they extend Lie rings, these are also partially unbundled.
## References
* [N. Bourbaki, *Lie Groups and Lie Algebras, Chapters 1--3*][bourbaki1975]
## Tags
lie bracket, ring commutator, jacobi identity, lie ring, lie algebra
-/
universes u v
/--
A binary operation, intended use in Lie algebras and similar structures.
-/
class has_bracket (L : Type v) := (bracket : L → L → L)
notation `⁅`x`,` y`⁆` := has_bracket.bracket x y
namespace ring_commutator
variables {A : Type v} [ring A]
/--
The ring commutator captures the extent to which a ring is commutative. It is identically zero
exactly when the ring is commutative.
-/
def commutator (x y : A) := x*y - y*x
local notation `⁅`x`,` y`⁆` := commutator x y
@[simp] lemma add_left (x y z : A) :
⁅x + y, z⁆ = ⁅x, z⁆ + ⁅y, z⁆ :=
by simp [commutator, right_distrib, left_distrib]
@[simp] lemma add_right (x y z : A) :
⁅z, x + y⁆ = ⁅z, x⁆ + ⁅z, y⁆ :=
by simp [commutator, right_distrib, left_distrib]
@[simp] lemma alternate (x : A) :
⁅x, x⁆ = 0 :=
by simp [commutator]
lemma jacobi (x y z : A) :
⁅x, ⁅y, z⁆⁆ + ⁅y, ⁅z, x⁆⁆ + ⁅z, ⁅x, y⁆⁆ = 0 :=
begin
unfold commutator,
repeat { rw mul_sub_left_distrib },
repeat { rw mul_sub_right_distrib },
repeat { rw add_sub },
repeat { rw ←sub_add },
repeat { rw ←mul_assoc },
have h : ∀ (x y z : A), x - y + z + y = x+z := by simp,
repeat { rw h },
simp,
end
end ring_commutator
/--
A Lie ring is an additive group with compatible product, known as the bracket, satisfying the
Jacobi identity. The bracket is not associative unless it is identically zero.
-/
class lie_ring (L : Type v) [add_comm_group L] extends has_bracket L :=
(add_lie : ∀ (x y z : L), ⁅x + y, z⁆ = ⁅x, z⁆ + ⁅y, z⁆)
(lie_add : ∀ (x y z : L), ⁅z, x + y⁆ = ⁅z, x⁆ + ⁅z, y⁆)
(lie_self : ∀ (x : L), ⁅x, x⁆ = 0)
(jacobi : ∀ (x y z : L), ⁅x, ⁅y, z⁆⁆ + ⁅y, ⁅z, x⁆⁆ + ⁅z, ⁅x, y⁆⁆ = 0)
section lie_ring
variables {L : Type v} [add_comm_group L] [lie_ring L]
@[simp] lemma add_lie (x y z : L) : ⁅x + y, z⁆ = ⁅x, z⁆ + ⁅y, z⁆ := lie_ring.add_lie x y z
@[simp] lemma lie_add (x y z : L) : ⁅z, x + y⁆ = ⁅z, x⁆ + ⁅z, y⁆ := lie_ring.lie_add x y z
@[simp] lemma lie_self (x : L) : ⁅x, x⁆ = 0 := lie_ring.lie_self x
@[simp] lemma lie_skew (x y : L) :
-⁅y, x⁆ = ⁅x, y⁆ :=
begin
symmetry,
rw [←sub_eq_zero_iff_eq, sub_neg_eq_add],
have H : ⁅x + y, x + y⁆ = 0, from lie_self _,
rw add_lie at H,
simpa using H,
end
@[simp] lemma lie_zero (x : L) :
⁅x, 0⁆ = 0 :=
begin
have H : ⁅x, 0⁆ + ⁅x, 0⁆ = ⁅x, 0⁆ + 0 := by { rw ←lie_add, simp, },
exact add_left_cancel H,
end
@[simp] lemma zero_lie (x : L) :
⁅0, x⁆ = 0 := by { rw [←lie_skew, lie_zero], simp, }
@[simp] lemma neg_lie (x y : L) :
⁅-x, y⁆ = -⁅x, y⁆ := by { rw [←sub_eq_zero_iff_eq, sub_neg_eq_add, ←add_lie], simp, }
@[simp] lemma lie_neg (x y : L) :
⁅x, -y⁆ = -⁅x, y⁆ := by { rw [←lie_skew, ←lie_skew], simp, }
@[simp] lemma gsmul_lie (x y : L) (n : ℤ) :
⁅n • x, y⁆ = n • ⁅x, y⁆ :=
begin
let Ad := λ z, ⁅z, y⁆,
haveI : is_add_group_hom Ad := { map_add := by simp [Ad], },
apply is_add_group_hom.map_gsmul Ad,
end
@[simp] lemma lie_gsmul (x y : L) (n : ℤ) :
⁅x, n • y⁆ = n • ⁅x, y⁆ :=
begin
rw [←lie_skew, ←lie_skew x, gsmul_lie],
unfold has_scalar.smul, rw gsmul_neg,
end
/--
An associative ring gives rise to a Lie ring by taking the bracket to be the ring commutator.
-/
def lie_ring.of_associative_ring (A : Type v) [ring A] : lie_ring A :=
{ bracket := ring_commutator.commutator,
add_lie := ring_commutator.add_left,
lie_add := ring_commutator.add_right,
lie_self := ring_commutator.alternate,
jacobi := ring_commutator.jacobi }
end lie_ring
/--
A Lie algebra is a module with compatible product, known as the bracket, satisfying the Jacobi
identity. Forgetting the scalar multiplication, every Lie algebra is a Lie ring.
-/
class lie_algebra (R : Type u) (L : Type v)
[comm_ring R] [add_comm_group L] extends module R L, lie_ring L :=
(lie_smul : ∀ (t : R) (x y : L), ⁅x, t • y⁆ = t • ⁅x, y⁆)
@[simp] lemma lie_smul (R : Type u) (L : Type v) [comm_ring R] [add_comm_group L] [lie_algebra R L]
(t : R) (x y : L) : ⁅x, t • y⁆ = t • ⁅x, y⁆ :=
lie_algebra.lie_smul t x y
@[simp] lemma smul_lie (R : Type u) (L : Type v) [comm_ring R] [add_comm_group L] [lie_algebra R L]
(t : R) (x y : L) : ⁅t • x, y⁆ = t • ⁅x, y⁆ :=
by { rw [←lie_skew, ←lie_skew x y], simp [-lie_skew], }
namespace lie_algebra
variables (R : Type u) (L : Type v) [comm_ring R] [add_comm_group L] [lie_algebra R L]
/--
The adjoint action of a Lie algebra on itself.
-/
def Ad (x : L) : L →ₗ[R] L :=
{ to_fun := has_bracket.bracket x,
add := by { intros, apply lie_add },
smul := by { intros, apply lie_smul } }
/--
The bracket of a Lie algebra as a bilinear map.
-/
def bil_lie : L →ₗ[R] L →ₗ[R] L :=
{ to_fun := lie_algebra.Ad R L,
add := by { unfold lie_algebra.Ad, intros, ext, simp [add_lie], },
smul := by { unfold lie_algebra.Ad, intros, ext, simp, } }
/--
An associative algebra gives rise to a Lie algebra by taking the bracket to be the ring commutator.
-/
def of_associative_algebra (A : Type v) [ring A] [algebra R A] : lie_algebra R A :=
{ bracket := ring_commutator.commutator,
lie_smul := by { intros, unfold has_bracket.bracket, unfold ring_commutator.commutator,
rw [algebra.mul_smul_comm, algebra.smul_mul_assoc, smul_sub], },
..lie_ring.of_associative_ring A }
/--
An important class of Lie algebras are those arising from the associative algebra structure on
module endomorphisms.
-/
def of_endomorphism_algebra (M : Type v)
[add_comm_group M] [module R M] : lie_algebra R (module.End R M) :=
of_associative_algebra R (module.End R M)
end lie_algebra
/--
An important class of Lie algebras are those arising from the associative algebra structure on
square matrices over a commutative ring.
-/
def matrix.lie_algebra (n : Type u) (R : Type v)
[fintype n] [decidable_eq n] [comm_ring R] : lie_algebra R (matrix n n R) :=
lie_algebra.of_associative_algebra R (matrix n n R)
|
e11ef562f674f3bf8a761d01ba2a1de0761ed7e3 | a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91 | /tests/lean/run/secnot.lean | 40e96f6c0a9e21dc4efb666fa5c4b01cec67a138 | [
"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 | 392 | lean | section
variable {A : Type}
definition f (a b : A) := a
infixl ` ◀ `:65 := f
variables a b : A
check a ◀ b
end
inductive List (T : Type) : Type
| nil {} : List
| cons : T → List → List
namespace List
section
variable {T : Type}
notation `[` l:(foldr `,` (h t, cons h t) nil) `]` := l
check [(10:num), 20, 30]
end
end List
open List
check [(10:num), 20, 40]
check (10:num) ◀ 20
|
2c72618f72fc2f539db203728d61def6a693f9ee | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/category_theory/limits/shapes/concrete_category_auto.lean | dfdd27dbf4fe66b48d2893b4911ba5ce20e679d0 | [] | no_license | AurelienSaue/Mathlib4_auto | f538cfd0980f65a6361eadea39e6fc639e9dae14 | 590df64109b08190abe22358fabc3eae000943f2 | refs/heads/master | 1,683,906,849,776 | 1,622,564,669,000 | 1,622,564,669,000 | 371,723,747 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,181 | lean | /-
Copyright (c) 2017 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.limits.shapes.kernels
import Mathlib.category_theory.concrete_category.basic
import Mathlib.PostPort
universes u u_1
namespace Mathlib
/-!
# Facts about limits of functors into concrete categories
This file doesn't yet attempt to be exhaustive;
it just contains lemmas that are useful
while comparing categorical limits with existing constructions in concrete categories.
-/
namespace category_theory.limits
@[simp] theorem kernel_condition_apply {C : Type (u + 1)} [large_category C] [concrete_category C]
[has_zero_morphisms C] {X : C} {Y : C} (f : X ⟶ Y) [has_kernel f] (x : ↥(kernel f)) :
coe_fn f (coe_fn (kernel.ι f) x) = coe_fn 0 x :=
sorry
@[simp] theorem cokernel_condition_apply {C : Type (u + 1)} [large_category C] [concrete_category C]
[has_zero_morphisms C] {X : C} {Y : C} (f : X ⟶ Y) [has_cokernel f] (x : ↥X) :
coe_fn (cokernel.π f) (coe_fn f x) = coe_fn 0 x :=
sorry
end Mathlib |
ac9c5101a03122732781c91dca29379539fe8763 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /test/lint_coe_to_fun.lean | f8a6f6a3ef5dda11f572501a4401b29397590963 | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 1,436 | lean | import tactic.lint
-- see Note [function coercion]
structure equiv (α β : Sort*) :=
(to_fun : α → β)
(inv_fun : β → α)
instance {α β} : has_coe_to_fun (equiv α β) (λ _, α → β) := ⟨equiv.to_fun⟩
structure sparkling_equiv (α β) extends equiv α β
instance {α β} : has_coe (sparkling_equiv α β) (equiv α β) :=
⟨sparkling_equiv.to_equiv⟩
-- should complain
open tactic
run_cmd do
decl ← get_decl ``sparkling_equiv,
res ← linter.has_coe_to_fun.test decl,
-- linter complains
guard res.is_some
instance {α β} : has_coe_to_fun (sparkling_equiv α β) (λ _, α → β) :=
⟨λ f, f.to_equiv.to_fun⟩
-- prima!
run_cmd do
decl ← get_decl ``sparkling_equiv,
res ← linter.has_coe_to_fun.test decl,
-- linter doesn't complain
guard res.is_none
-- Test support for type-class parameters in the `has_coe_to_fun` instance.
namespace with_tc_param
structure equiv (α β : Sort*) :=
(to_fun : α → β)
(inv_fun : β → α)
instance {α β} [nonempty α] : has_coe_to_fun (equiv α β) (λ _, α → β) :=
⟨equiv.to_fun⟩
structure sparkling_equiv (α β) [nonempty α] extends equiv α β
instance {α β} [nonempty α] : has_coe (sparkling_equiv α β) (equiv α β) :=
⟨sparkling_equiv.to_equiv⟩
-- should complain
open tactic
run_cmd do
decl ← get_decl ``sparkling_equiv,
res ← linter.has_coe_to_fun.test decl,
-- linter complains
guard res.is_some
end with_tc_param
|
e110c320f924af6f48c0af9d40930f9f1dda60fe | 947fa6c38e48771ae886239b4edce6db6e18d0fb | /src/analysis/inner_product_space/adjoint.lean | 58905af024388d325b4104ad77b554c24e0e7251 | [
"Apache-2.0"
] | permissive | ramonfmir/mathlib | c5dc8b33155473fab97c38bd3aa6723dc289beaa | 14c52e990c17f5a00c0cc9e09847af16fabbed25 | refs/heads/master | 1,661,979,343,526 | 1,660,830,384,000 | 1,660,830,384,000 | 182,072,989 | 0 | 0 | null | 1,555,585,876,000 | 1,555,585,876,000 | null | UTF-8 | Lean | false | false | 23,507 | lean | /-
Copyright (c) 2021 Frédéric Dupuis. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Frédéric Dupuis, Heather Macbeth
-/
import analysis.inner_product_space.dual
import analysis.inner_product_space.pi_L2
/-!
# Adjoint of operators on Hilbert spaces
Given an operator `A : E →L[𝕜] F`, where `E` and `F` are Hilbert spaces, its adjoint
`adjoint A : F →L[𝕜] E` is the unique operator such that `⟪x, A y⟫ = ⟪adjoint A x, y⟫` for all
`x` and `y`.
We then use this to put a C⋆-algebra structure on `E →L[𝕜] E` with the adjoint as the star
operation.
This construction is used to define an adjoint for linear maps (i.e. not continuous) between
finite dimensional spaces.
## Main definitions
* `continuous_linear_map.adjoint : (E →L[𝕜] F) ≃ₗᵢ⋆[𝕜] (F →L[𝕜] E)`: the adjoint of a continuous
linear map, bundled as a conjugate-linear isometric equivalence.
* `linear_map.adjoint : (E →ₗ[𝕜] F) ≃ₗ⋆[𝕜] (F →ₗ[𝕜] E)`: the adjoint of a linear map between
finite-dimensional spaces, this time only as a conjugate-linear equivalence, since there is no
norm defined on these maps.
## Implementation notes
* The continuous conjugate-linear version `adjoint_aux` is only an intermediate
definition and is not meant to be used outside this file.
## Tags
adjoint
-/
noncomputable theory
open is_R_or_C
open_locale complex_conjugate
variables {𝕜 E F G : Type*} [is_R_or_C 𝕜]
variables [inner_product_space 𝕜 E] [inner_product_space 𝕜 F] [inner_product_space 𝕜 G]
local notation `⟪`x`, `y`⟫` := @inner 𝕜 _ _ x y
namespace linear_map
/-! ### Symmetric operators -/
/-- A (not necessarily bounded) operator on an inner product space is symmetric, if for all
`x`, `y`, we have `⟪T x, y⟫ = ⟪x, T y⟫`. -/
def is_symmetric (T : E →ₗ[𝕜] E) : Prop := ∀ x y, ⟪T x, y⟫ = ⟪x, T y⟫
section real
variables {E' : Type*} [inner_product_space ℝ E']
-- Todo: Generalize this to `is_R_or_C`.
/-- An operator `T` on a `ℝ`-inner product space is symmetric if and only if it is
`bilin_form.is_self_adjoint` with respect to the bilinear form given by the inner product. -/
lemma is_symmetric_iff_bilin_form (T : E' →ₗ[ℝ] E') :
is_symmetric T ↔ bilin_form_of_real_inner.is_self_adjoint T :=
by simp [is_symmetric, bilin_form.is_self_adjoint, bilin_form.is_adjoint_pair]
end real
lemma is_symmetric.conj_inner_sym {T : E →ₗ[𝕜] E} (hT : is_symmetric T) (x y : E) :
conj ⟪T x, y⟫ = ⟪T y, x⟫ :=
by rw [hT x y, inner_conj_sym]
@[simp] lemma is_symmetric.apply_clm {T : E →L[𝕜] E} (hT : is_symmetric (T : E →ₗ[𝕜] E))
(x y : E) :
⟪T x, y⟫ = ⟪x, T y⟫ :=
hT x y
lemma is_symmetric_zero : (0 : E →ₗ[𝕜] E).is_symmetric :=
λ x y, (inner_zero_right : ⟪x, 0⟫ = 0).symm ▸ (inner_zero_left : ⟪0, y⟫ = 0)
lemma is_symmetric_id : (linear_map.id : E →ₗ[𝕜] E).is_symmetric :=
λ x y, rfl
lemma is_symmetric.add {T S : E →ₗ[𝕜] E} (hT : T.is_symmetric) (hS : S.is_symmetric) :
(T + S).is_symmetric :=
begin
intros x y,
rw [linear_map.add_apply, inner_add_left, hT x y, hS x y, ← inner_add_right],
refl
end
/-- The orthogonal projection is symmetric. -/
lemma _root_.orthogonal_projection_is_symmetric [complete_space E] (U : submodule 𝕜 E)
[complete_space U] :
(U.subtypeL ∘L orthogonal_projection U : E →ₗ[𝕜] E).is_symmetric :=
inner_orthogonal_projection_left_eq_right U
/-- The **Hellinger--Toeplitz theorem**: if a symmetric operator is defined everywhere, then
it is automatically continuous. -/
lemma is_symmetric.continuous [complete_space E] {T : E →ₗ[𝕜] E} (hT : is_symmetric T) :
continuous T :=
begin
-- We prove it by using the closed graph theorem
refine T.continuous_of_seq_closed_graph (λ u x y hu hTu, _),
rw [←sub_eq_zero, ←inner_self_eq_zero],
have hlhs : ∀ k : ℕ, ⟪T (u k) - T x, y - T x⟫ = ⟪u k - x, T (y - T x)⟫ :=
by { intro k, rw [←T.map_sub, hT] },
refine tendsto_nhds_unique ((hTu.sub_const _).inner tendsto_const_nhds) _,
simp_rw hlhs,
rw ←@inner_zero_left 𝕜 E _ _ (T (y - T x)),
refine filter.tendsto.inner _ tendsto_const_nhds,
rw ←sub_self x,
exact hu.sub_const _,
end
/-- For a symmetric operator `T`, the function `λ x, ⟪T x, x⟫` is real-valued. -/
@[simp] lemma is_symmetric.coe_re_apply_inner_self_apply
{T : E →L[𝕜] E} (hT : is_symmetric (T : E →ₗ[𝕜] E)) (x : E) :
(T.re_apply_inner_self x : 𝕜) = ⟪T x, x⟫ :=
begin
suffices : ∃ r : ℝ, ⟪T x, x⟫ = r,
{ obtain ⟨r, hr⟩ := this,
simp [hr, T.re_apply_inner_self_apply] },
rw ← eq_conj_iff_real,
exact hT.conj_inner_sym x x
end
/-- If a symmetric operator preserves a submodule, its restriction to that submodule is
symmetric. -/
lemma is_symmetric.restrict_invariant {T : E →ₗ[𝕜] E} (hT : is_symmetric T)
{V : submodule 𝕜 E} (hV : ∀ v ∈ V, T v ∈ V) :
is_symmetric (T.restrict hV) :=
λ v w, hT v w
lemma is_symmetric.restrict_scalars {T : E →ₗ[𝕜] E} (hT : T.is_symmetric) :
@linear_map.is_symmetric ℝ E _ (inner_product_space.is_R_or_C_to_real 𝕜 E)
(@linear_map.restrict_scalars ℝ 𝕜 _ _ _ _ _ _
(inner_product_space.is_R_or_C_to_real 𝕜 E).to_module
(inner_product_space.is_R_or_C_to_real 𝕜 E).to_module _ _ _ T) :=
λ x y, by simp [hT x y, real_inner_eq_re_inner, linear_map.coe_restrict_scalars_eq_coe]
section complex
variables {V : Type*}
[inner_product_space ℂ V]
/-- A linear operator on a complex inner product space is symmetric precisely when
`⟪T v, v⟫_ℂ` is real for all v.-/
lemma is_symmetric_iff_inner_map_self_real (T : V →ₗ[ℂ] V):
is_symmetric T ↔ ∀ (v : V), conj ⟪T v, v⟫_ℂ = ⟪T v, v⟫_ℂ :=
begin
split,
{ intros hT v,
apply is_symmetric.conj_inner_sym hT },
{ intros h x y,
nth_rewrite 1 ← inner_conj_sym,
nth_rewrite 1 inner_map_polarization,
simp only [star_ring_end_apply, star_div', star_sub, star_add, star_mul],
simp only [← star_ring_end_apply],
rw [h (x + y), h (x - y), h (x + complex.I • y), h (x - complex.I • y)],
simp only [complex.conj_I],
rw inner_map_polarization',
norm_num,
ring },
end
end complex
end linear_map
/-! ### Adjoint operator -/
open inner_product_space
namespace continuous_linear_map
variables [complete_space E] [complete_space G]
/-- The adjoint, as a continuous conjugate-linear map. This is only meant as an auxiliary
definition for the main definition `adjoint`, where this is bundled as a conjugate-linear isometric
equivalence. -/
def adjoint_aux : (E →L[𝕜] F) →L⋆[𝕜] (F →L[𝕜] E) :=
(continuous_linear_map.compSL _ _ _ _ _ ((to_dual 𝕜 E).symm : normed_space.dual 𝕜 E →L⋆[𝕜] E)).comp
(to_sesq_form : (E →L[𝕜] F) →L[𝕜] F →L⋆[𝕜] normed_space.dual 𝕜 E)
@[simp] lemma adjoint_aux_apply (A : E →L[𝕜] F) (x : F) :
adjoint_aux A x = ((to_dual 𝕜 E).symm : (normed_space.dual 𝕜 E) → E) ((to_sesq_form A) x) := rfl
lemma adjoint_aux_inner_left (A : E →L[𝕜] F) (x : E) (y : F) : ⟪adjoint_aux A y, x⟫ = ⟪y, A x⟫ :=
by { simp only [adjoint_aux_apply, to_dual_symm_apply, to_sesq_form_apply_coe, coe_comp',
innerSL_apply_coe]}
lemma adjoint_aux_inner_right (A : E →L[𝕜] F) (x : E) (y : F) : ⟪x, adjoint_aux A y⟫ = ⟪A x, y⟫ :=
by rw [←inner_conj_sym, adjoint_aux_inner_left, inner_conj_sym]
variables [complete_space F]
lemma adjoint_aux_adjoint_aux (A : E →L[𝕜] F) : adjoint_aux (adjoint_aux A) = A :=
begin
ext v,
refine ext_inner_left 𝕜 (λ w, _),
rw [adjoint_aux_inner_right, adjoint_aux_inner_left],
end
@[simp] lemma adjoint_aux_norm (A : E →L[𝕜] F) : ∥adjoint_aux A∥ = ∥A∥ :=
begin
refine le_antisymm _ _,
{ refine continuous_linear_map.op_norm_le_bound _ (norm_nonneg _) (λ x, _),
rw [adjoint_aux_apply, linear_isometry_equiv.norm_map],
exact to_sesq_form_apply_norm_le },
{ nth_rewrite_lhs 0 [←adjoint_aux_adjoint_aux A],
refine continuous_linear_map.op_norm_le_bound _ (norm_nonneg _) (λ x, _),
rw [adjoint_aux_apply, linear_isometry_equiv.norm_map],
exact to_sesq_form_apply_norm_le }
end
/-- The adjoint of a bounded operator from Hilbert space E to Hilbert space F. -/
def adjoint : (E →L[𝕜] F) ≃ₗᵢ⋆[𝕜] (F →L[𝕜] E) :=
linear_isometry_equiv.of_surjective
{ norm_map' := adjoint_aux_norm,
..adjoint_aux }
(λ A, ⟨adjoint_aux A, adjoint_aux_adjoint_aux A⟩)
localized "postfix `†`:1000 := continuous_linear_map.adjoint" in inner_product
/-- The fundamental property of the adjoint. -/
lemma adjoint_inner_left (A : E →L[𝕜] F) (x : E) (y : F) : ⟪A† y, x⟫ = ⟪y, A x⟫ :=
adjoint_aux_inner_left A x y
/-- The fundamental property of the adjoint. -/
lemma adjoint_inner_right (A : E →L[𝕜] F) (x : E) (y : F) : ⟪x, A† y⟫ = ⟪A x, y⟫ :=
adjoint_aux_inner_right A x y
/-- The adjoint is involutive -/
@[simp] lemma adjoint_adjoint (A : E →L[𝕜] F) : A†† = A :=
adjoint_aux_adjoint_aux A
/-- The adjoint of the composition of two operators is the composition of the two adjoints
in reverse order. -/
@[simp] lemma adjoint_comp (A : F →L[𝕜] G) (B : E →L[𝕜] F) : (A ∘L B)† = B† ∘L A† :=
begin
ext v,
refine ext_inner_left 𝕜 (λ w, _),
simp only [adjoint_inner_right, continuous_linear_map.coe_comp', function.comp_app],
end
lemma apply_norm_sq_eq_inner_adjoint_left (A : E →L[𝕜] E) (x : E) : ∥A x∥^2 = re ⟪(A† * A) x, x⟫ :=
have h : ⟪(A† * A) x, x⟫ = ⟪A x, A x⟫ := by { rw [←adjoint_inner_left], refl },
by rw [h, ←inner_self_eq_norm_sq _]
lemma apply_norm_eq_sqrt_inner_adjoint_left (A : E →L[𝕜] E) (x : E) :
∥A x∥ = real.sqrt (re ⟪(A† * A) x, x⟫) :=
by rw [←apply_norm_sq_eq_inner_adjoint_left, real.sqrt_sq (norm_nonneg _)]
lemma apply_norm_sq_eq_inner_adjoint_right (A : E →L[𝕜] E) (x : E) : ∥A x∥^2 = re ⟪x, (A† * A) x⟫ :=
have h : ⟪x, (A† * A) x⟫ = ⟪A x, A x⟫ := by { rw [←adjoint_inner_right], refl },
by rw [h, ←inner_self_eq_norm_sq _]
lemma apply_norm_eq_sqrt_inner_adjoint_right (A : E →L[𝕜] E) (x : E) :
∥A x∥ = real.sqrt (re ⟪x, (A† * A) x⟫) :=
by rw [←apply_norm_sq_eq_inner_adjoint_right, real.sqrt_sq (norm_nonneg _)]
/-- The adjoint is unique: a map `A` is the adjoint of `B` iff it satisfies `⟪A x, y⟫ = ⟪x, B y⟫`
for all `x` and `y`. -/
lemma eq_adjoint_iff (A : E →L[𝕜] F) (B : F →L[𝕜] E) :
A = B† ↔ (∀ x y, ⟪A x, y⟫ = ⟪x, B y⟫) :=
begin
refine ⟨λ h x y, by rw [h, adjoint_inner_left], λ h, _⟩,
ext x,
exact ext_inner_right 𝕜 (λ y, by simp only [adjoint_inner_left, h x y])
end
@[simp] lemma adjoint_id : (continuous_linear_map.id 𝕜 E).adjoint = continuous_linear_map.id 𝕜 E :=
begin
refine eq.symm _,
rw eq_adjoint_iff,
simp,
end
lemma _root_.submodule.adjoint_subtypeL (U : submodule 𝕜 E)
[complete_space U] :
(U.subtypeL)† = orthogonal_projection U :=
begin
symmetry,
rw eq_adjoint_iff,
intros x u,
rw [U.coe_inner, inner_orthogonal_projection_left_eq_right,
orthogonal_projection_mem_subspace_eq_self],
refl
end
lemma _root_.submodule.adjoint_orthogonal_projection (U : submodule 𝕜 E)
[complete_space U] :
(orthogonal_projection U : E →L[𝕜] U)† = U.subtypeL :=
by rw [← U.adjoint_subtypeL, adjoint_adjoint]
/-- `E →L[𝕜] E` is a star algebra with the adjoint as the star operation. -/
instance : has_star (E →L[𝕜] E) := ⟨adjoint⟩
instance : has_involutive_star (E →L[𝕜] E) := ⟨adjoint_adjoint⟩
instance : star_semigroup (E →L[𝕜] E) := ⟨adjoint_comp⟩
instance : star_ring (E →L[𝕜] E) := ⟨linear_isometry_equiv.map_add adjoint⟩
instance : star_module 𝕜 (E →L[𝕜] E) := ⟨linear_isometry_equiv.map_smulₛₗ adjoint⟩
lemma star_eq_adjoint (A : E →L[𝕜] E) : star A = A† := rfl
/-- A continuous linear operator is self-adjoint iff it is equal to its adjoint. -/
lemma is_self_adjoint_iff' {A : E →L[𝕜] E} : is_self_adjoint A ↔ A.adjoint = A := iff.rfl
instance : cstar_ring (E →L[𝕜] E) :=
⟨begin
intros A,
rw [star_eq_adjoint],
refine le_antisymm _ _,
{ calc ∥A† * A∥ ≤ ∥A†∥ * ∥A∥ : op_norm_comp_le _ _
... = ∥A∥ * ∥A∥ : by rw [linear_isometry_equiv.norm_map] },
{ rw [←sq, ←real.sqrt_le_sqrt_iff (norm_nonneg _), real.sqrt_sq (norm_nonneg _)],
refine op_norm_le_bound _ (real.sqrt_nonneg _) (λ x, _),
have := calc
re ⟪(A† * A) x, x⟫ ≤ ∥(A† * A) x∥ * ∥x∥ : re_inner_le_norm _ _
... ≤ ∥A† * A∥ * ∥x∥ * ∥x∥ : mul_le_mul_of_nonneg_right
(le_op_norm _ _) (norm_nonneg _),
calc ∥A x∥ = real.sqrt (re ⟪(A† * A) x, x⟫) : by rw [apply_norm_eq_sqrt_inner_adjoint_left]
... ≤ real.sqrt (∥A† * A∥ * ∥x∥ * ∥x∥) : real.sqrt_le_sqrt this
... = real.sqrt (∥A† * A∥) * ∥x∥
: by rw [mul_assoc, real.sqrt_mul (norm_nonneg _), real.sqrt_mul_self (norm_nonneg _)] }
end⟩
section real
variables {E' : Type*} {F' : Type*} [inner_product_space ℝ E'] [inner_product_space ℝ F']
variables [complete_space E'] [complete_space F']
-- Todo: Generalize this to `is_R_or_C`.
lemma is_adjoint_pair_inner (A : E' →L[ℝ] F') :
linear_map.is_adjoint_pair (sesq_form_of_inner : E' →ₗ[ℝ] E' →ₗ[ℝ] ℝ)
(sesq_form_of_inner : F' →ₗ[ℝ] F' →ₗ[ℝ] ℝ) A (A†) :=
λ x y, by simp only [sesq_form_of_inner_apply_apply, adjoint_inner_left, to_linear_map_eq_coe,
coe_coe]
end real
end continuous_linear_map
/-! ### Self-adjoint operators -/
namespace is_self_adjoint
open continuous_linear_map
variables [complete_space E] [complete_space F]
lemma adjoint_eq {A : E →L[𝕜] E} (hA : is_self_adjoint A) : A.adjoint = A := hA
/-- Every self-adjoint operator on an inner product space is symmetric. -/
lemma is_symmetric {A : E →L[𝕜] E} (hA : is_self_adjoint A) :
(A : E →ₗ[𝕜] E).is_symmetric :=
λ x y, by rw_mod_cast [←A.adjoint_inner_right, hA.adjoint_eq]
/-- Conjugating preserves self-adjointness -/
lemma conj_adjoint {T : E →L[𝕜] E} (hT : is_self_adjoint T) (S : E →L[𝕜] F) :
is_self_adjoint (S ∘L T ∘L S.adjoint) :=
begin
rw is_self_adjoint_iff' at ⊢ hT,
simp only [hT, adjoint_comp, adjoint_adjoint],
exact continuous_linear_map.comp_assoc _ _ _,
end
/-- Conjugating preserves self-adjointness -/
lemma adjoint_conj {T : E →L[𝕜] E} (hT : is_self_adjoint T) (S : F →L[𝕜] E) :
is_self_adjoint (S.adjoint ∘L T ∘L S) :=
begin
rw is_self_adjoint_iff' at ⊢ hT,
simp only [hT, adjoint_comp, adjoint_adjoint],
exact continuous_linear_map.comp_assoc _ _ _,
end
lemma _root_.continuous_linear_map.is_self_adjoint_iff_is_symmetric {A : E →L[𝕜] E} :
is_self_adjoint A ↔ (A : E →ₗ[𝕜] E).is_symmetric :=
⟨λ hA, hA.is_symmetric, λ hA, ext $ λ x, ext_inner_right 𝕜 $
λ y, (A.adjoint_inner_left y x).symm ▸ (hA x y).symm⟩
lemma _root_.linear_map.is_symmetric.is_self_adjoint {A : E →L[𝕜] E}
(hA : (A : E →ₗ[𝕜] E).is_symmetric) : is_self_adjoint A :=
by rwa ←continuous_linear_map.is_self_adjoint_iff_is_symmetric at hA
/-- The orthogonal projection is self-adjoint. -/
lemma _root_.orthogonal_projection_is_self_adjoint (U : submodule 𝕜 E)
[complete_space U] :
is_self_adjoint (U.subtypeL ∘L orthogonal_projection U) :=
(orthogonal_projection_is_symmetric U).is_self_adjoint
lemma conj_orthogonal_projection {T : E →L[𝕜] E}
(hT : is_self_adjoint T) (U : submodule 𝕜 E) [complete_space U] :
is_self_adjoint (U.subtypeL ∘L orthogonal_projection U ∘L T ∘L U.subtypeL ∘L
orthogonal_projection U) :=
begin
rw ←continuous_linear_map.comp_assoc,
nth_rewrite 0 ←(orthogonal_projection_is_self_adjoint U).adjoint_eq,
refine hT.adjoint_conj _,
end
end is_self_adjoint
namespace linear_map
variables [complete_space E]
variables {T : E →ₗ[𝕜] E}
/-- The **Hellinger--Toeplitz theorem**: Construct a self-adjoint operator from an everywhere
defined symmetric operator.-/
def is_symmetric.to_self_adjoint (hT : is_symmetric T) : self_adjoint (E →L[𝕜] E) :=
⟨⟨T, hT.continuous⟩, continuous_linear_map.is_self_adjoint_iff_is_symmetric.mpr hT⟩
lemma is_symmetric.coe_to_self_adjoint (hT : is_symmetric T) :
(hT.to_self_adjoint : E →ₗ[𝕜] E) = T := rfl
lemma is_symmetric.to_self_adjoint_apply (hT : is_symmetric T) {x : E} :
hT.to_self_adjoint x = T x := rfl
end linear_map
namespace linear_map
variables [finite_dimensional 𝕜 E] [finite_dimensional 𝕜 F] [finite_dimensional 𝕜 G]
local attribute [instance, priority 20] finite_dimensional.complete
/-- The adjoint of an operator from the finite-dimensional inner product space E to the finite-
dimensional inner product space F. -/
def adjoint : (E →ₗ[𝕜] F) ≃ₗ⋆[𝕜] (F →ₗ[𝕜] E) :=
((linear_map.to_continuous_linear_map : (E →ₗ[𝕜] F) ≃ₗ[𝕜] (E →L[𝕜] F)).trans
continuous_linear_map.adjoint.to_linear_equiv).trans
linear_map.to_continuous_linear_map.symm
lemma adjoint_to_continuous_linear_map (A : E →ₗ[𝕜] F) :
A.adjoint.to_continuous_linear_map = A.to_continuous_linear_map.adjoint := rfl
lemma adjoint_eq_to_clm_adjoint (A : E →ₗ[𝕜] F) :
A.adjoint = A.to_continuous_linear_map.adjoint := rfl
/-- The fundamental property of the adjoint. -/
lemma adjoint_inner_left (A : E →ₗ[𝕜] F) (x : E) (y : F) : ⟪adjoint A y, x⟫ = ⟪y, A x⟫ :=
begin
rw [←coe_to_continuous_linear_map A, adjoint_eq_to_clm_adjoint],
exact continuous_linear_map.adjoint_inner_left _ x y,
end
/-- The fundamental property of the adjoint. -/
lemma adjoint_inner_right (A : E →ₗ[𝕜] F) (x : E) (y : F) : ⟪x, adjoint A y⟫ = ⟪A x, y⟫ :=
begin
rw [←coe_to_continuous_linear_map A, adjoint_eq_to_clm_adjoint],
exact continuous_linear_map.adjoint_inner_right _ x y,
end
/-- The adjoint is involutive -/
@[simp] lemma adjoint_adjoint (A : E →ₗ[𝕜] F) : A.adjoint.adjoint = A :=
begin
ext v,
refine ext_inner_left 𝕜 (λ w, _),
rw [adjoint_inner_right, adjoint_inner_left],
end
/-- The adjoint of the composition of two operators is the composition of the two adjoints
in reverse order. -/
@[simp] lemma adjoint_comp (A : F →ₗ[𝕜] G) (B : E →ₗ[𝕜] F) :
(A ∘ₗ B).adjoint = B.adjoint ∘ₗ A.adjoint :=
begin
ext v,
refine ext_inner_left 𝕜 (λ w, _),
simp only [adjoint_inner_right, linear_map.coe_comp, function.comp_app],
end
/-- The adjoint is unique: a map `A` is the adjoint of `B` iff it satisfies `⟪A x, y⟫ = ⟪x, B y⟫`
for all `x` and `y`. -/
lemma eq_adjoint_iff (A : E →ₗ[𝕜] F) (B : F →ₗ[𝕜] E) :
A = B.adjoint ↔ (∀ x y, ⟪A x, y⟫ = ⟪x, B y⟫) :=
begin
refine ⟨λ h x y, by rw [h, adjoint_inner_left], λ h, _⟩,
ext x,
exact ext_inner_right 𝕜 (λ y, by simp only [adjoint_inner_left, h x y])
end
/-- The adjoint is unique: a map `A` is the adjoint of `B` iff it satisfies `⟪A x, y⟫ = ⟪x, B y⟫`
for all basis vectors `x` and `y`. -/
lemma eq_adjoint_iff_basis {ι₁ : Type*} {ι₂ : Type*} (b₁ : basis ι₁ 𝕜 E) (b₂ : basis ι₂ 𝕜 F)
(A : E →ₗ[𝕜] F) (B : F →ₗ[𝕜] E) :
A = B.adjoint ↔ (∀ (i₁ : ι₁) (i₂ : ι₂), ⟪A (b₁ i₁), b₂ i₂⟫ = ⟪b₁ i₁, B (b₂ i₂)⟫) :=
begin
refine ⟨λ h x y, by rw [h, adjoint_inner_left], λ h, _⟩,
refine basis.ext b₁ (λ i₁, _),
exact ext_inner_right_basis b₂ (λ i₂, by simp only [adjoint_inner_left, h i₁ i₂]),
end
lemma eq_adjoint_iff_basis_left {ι : Type*} (b : basis ι 𝕜 E) (A : E →ₗ[𝕜] F) (B : F →ₗ[𝕜] E) :
A = B.adjoint ↔ (∀ i y, ⟪A (b i), y⟫ = ⟪b i, B y⟫) :=
begin
refine ⟨λ h x y, by rw [h, adjoint_inner_left], λ h, basis.ext b (λ i, _)⟩,
exact ext_inner_right 𝕜 (λ y, by simp only [h i, adjoint_inner_left]),
end
lemma eq_adjoint_iff_basis_right {ι : Type*} (b : basis ι 𝕜 F) (A : E →ₗ[𝕜] F) (B : F →ₗ[𝕜] E) :
A = B.adjoint ↔ (∀ i x, ⟪A x, b i⟫ = ⟪x, B (b i)⟫) :=
begin
refine ⟨λ h x y, by rw [h, adjoint_inner_left], λ h, _⟩,
ext x,
refine ext_inner_right_basis b (λ i, by simp only [h i, adjoint_inner_left]),
end
/-- `E →ₗ[𝕜] E` is a star algebra with the adjoint as the star operation. -/
instance : has_star (E →ₗ[𝕜] E) := ⟨adjoint⟩
instance : has_involutive_star (E →ₗ[𝕜] E) := ⟨adjoint_adjoint⟩
instance : star_semigroup (E →ₗ[𝕜] E) := ⟨adjoint_comp⟩
instance : star_ring (E →ₗ[𝕜] E) := ⟨linear_equiv.map_add adjoint⟩
instance : star_module 𝕜 (E →ₗ[𝕜] E) := ⟨linear_equiv.map_smulₛₗ adjoint⟩
lemma star_eq_adjoint (A : E →ₗ[𝕜] E) : star A = A.adjoint := rfl
/-- A continuous linear operator is self-adjoint iff it is equal to its adjoint. -/
lemma is_self_adjoint_iff' {A : E →ₗ[𝕜] E} : is_self_adjoint A ↔ A.adjoint = A := iff.rfl
lemma is_symmetric_iff_is_self_adjoint (A : E →ₗ[𝕜] E) :
is_symmetric A ↔ is_self_adjoint A :=
by { rw [is_self_adjoint_iff', is_symmetric, ← linear_map.eq_adjoint_iff], exact eq_comm }
section real
variables {E' : Type*} {F' : Type*} [inner_product_space ℝ E'] [inner_product_space ℝ F']
variables [finite_dimensional ℝ E'] [finite_dimensional ℝ F']
-- Todo: Generalize this to `is_R_or_C`.
lemma is_adjoint_pair_inner (A : E' →ₗ[ℝ] F') :
is_adjoint_pair (sesq_form_of_inner : E' →ₗ[ℝ] E' →ₗ[ℝ] ℝ)
(sesq_form_of_inner : F' →ₗ[ℝ] F' →ₗ[ℝ] ℝ) A A.adjoint :=
λ x y, by simp only [sesq_form_of_inner_apply_apply, adjoint_inner_left]
end real
/-- The Gram operator T†T is symmetric. -/
lemma is_symmetric_adjoint_mul_self (T : E →ₗ[𝕜] E) : is_symmetric (T.adjoint * T) :=
λ x y, by simp only [mul_apply, adjoint_inner_left, adjoint_inner_right]
/-- The Gram operator T†T is a positive operator. -/
lemma re_inner_adjoint_mul_self_nonneg (T : E →ₗ[𝕜] E) (x : E) :
0 ≤ re ⟪ x, (T.adjoint * T) x ⟫ := by {simp only [mul_apply, adjoint_inner_right,
inner_self_eq_norm_sq_to_K], norm_cast, exact sq_nonneg _}
@[simp] lemma im_inner_adjoint_mul_self_eq_zero (T : E →ₗ[𝕜] E) (x : E) :
im ⟪ x, linear_map.adjoint T (T x) ⟫ = 0 := by {simp only [mul_apply,
adjoint_inner_right, inner_self_eq_norm_sq_to_K], norm_cast}
end linear_map
namespace matrix
variables {m n : Type*} [fintype m] [decidable_eq m] [fintype n] [decidable_eq n]
open_locale complex_conjugate
/-- The adjoint of the linear map associated to a matrix is the linear map associated to the
conjugate transpose of that matrix. -/
lemma conj_transpose_eq_adjoint (A : matrix m n 𝕜) :
to_lin' A.conj_transpose =
@linear_map.adjoint _ (euclidean_space 𝕜 n) (euclidean_space 𝕜 m) _ _ _ _ _ (to_lin' A) :=
begin
rw @linear_map.eq_adjoint_iff _ (euclidean_space 𝕜 m) (euclidean_space 𝕜 n),
intros x y,
convert dot_product_assoc (conj ∘ (id x : m → 𝕜)) y A using 1,
simp [dot_product, mul_vec, ring_hom.map_sum, ← star_ring_end_apply, mul_comm],
end
end matrix
|
3d94958bef1654e8807993c87cfa0901d455cf75 | 08bd4ba4ca87dba1f09d2c96a26f5d65da81f4b4 | /src/Init/Data/Array/Subarray.lean | 6dc9525ac5bd07c6fa7a5cdca3ae139c91e1d0c7 | [
"LLVM-exception",
"NCSA",
"LGPL-3.0-only",
"LicenseRef-scancode-inner-net-2.0",
"BSD-3-Clause",
"LGPL-2.0-or-later",
"Spencer-94",
"LGPL-2.1-or-later",
"HPND",
"LicenseRef-scancode-pcre",
"ISC",
"LGPL-2.1-only",
"Apache-2.0",
"LicenseRef-scancode-other-permissive",
"SunPro",
"CMU-Mach"... | permissive | gebner/lean4 | d51c4922640a52a6f7426536ea669ef18a1d9af5 | 8cd9ce06843c9d42d6d6dc43d3e81e3b49dfc20f | refs/heads/master | 1,685,732,780,391 | 1,672,962,627,000 | 1,673,459,398,000 | 373,307,283 | 0 | 0 | Apache-2.0 | 1,691,316,730,000 | 1,622,669,271,000 | Lean | UTF-8 | Lean | false | false | 6,202 | lean | /-
Copyright (c) 2020 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
import Init.Data.Array.Basic
universe u v w
structure Subarray (α : Type u) where
as : Array α
start : Nat
stop : Nat
h₁ : start ≤ stop
h₂ : stop ≤ as.size
namespace Subarray
def size (s : Subarray α) : Nat :=
s.stop - s.start
def get (s : Subarray α) (i : Fin s.size) : α :=
have : s.start + i.val < s.as.size := by
apply Nat.lt_of_lt_of_le _ s.h₂
have := i.isLt
simp [size] at this
rw [Nat.add_comm]
exact Nat.add_lt_of_lt_sub this
s.as[s.start + i.val]
instance : GetElem (Subarray α) Nat α fun xs i => i < xs.size where
getElem xs i h := xs.get ⟨i, h⟩
@[inline] def getD (s : Subarray α) (i : Nat) (v₀ : α) : α :=
if h : i < s.size then s.get ⟨i, h⟩ else v₀
abbrev get! [Inhabited α] (s : Subarray α) (i : Nat) : α :=
getD s i default
def popFront (s : Subarray α) : Subarray α :=
if h : s.start < s.stop then
{ s with start := s.start + 1, h₁ := Nat.le_of_lt_succ (Nat.add_lt_add_right h 1) }
else
s
@[inline] unsafe def forInUnsafe {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (s : Subarray α) (b : β) (f : α → β → m (ForInStep β)) : m β :=
let sz := USize.ofNat s.stop
let rec @[specialize] loop (i : USize) (b : β) : m β := do
if i < sz then
let a := s.as.uget i lcProof
match (← f a b) with
| ForInStep.done b => pure b
| ForInStep.yield b => loop (i+1) b
else
pure b
loop (USize.ofNat s.start) b
-- TODO: provide reference implementation
@[implemented_by Subarray.forInUnsafe]
protected opaque forIn {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (s : Subarray α) (b : β) (f : α → β → m (ForInStep β)) : m β :=
pure b
instance : ForIn m (Subarray α) α where
forIn := Subarray.forIn
@[inline]
def foldlM {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f : β → α → m β) (init : β) (as : Subarray α) : m β :=
as.as.foldlM f (init := init) (start := as.start) (stop := as.stop)
@[inline]
def foldrM {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f : α → β → m β) (init : β) (as : Subarray α) : m β :=
as.as.foldrM f (init := init) (start := as.stop) (stop := as.start)
@[inline]
def anyM {α : Type u} {m : Type → Type w} [Monad m] (p : α → m Bool) (as : Subarray α) : m Bool :=
as.as.anyM p (start := as.start) (stop := as.stop)
@[inline]
def allM {α : Type u} {m : Type → Type w} [Monad m] (p : α → m Bool) (as : Subarray α) : m Bool :=
as.as.allM p (start := as.start) (stop := as.stop)
@[inline]
def forM {α : Type u} {m : Type v → Type w} [Monad m] (f : α → m PUnit) (as : Subarray α) : m PUnit :=
as.as.forM f (start := as.start) (stop := as.stop)
@[inline]
def forRevM {α : Type u} {m : Type v → Type w} [Monad m] (f : α → m PUnit) (as : Subarray α) : m PUnit :=
as.as.forRevM f (start := as.stop) (stop := as.start)
@[inline]
def foldl {α : Type u} {β : Type v} (f : β → α → β) (init : β) (as : Subarray α) : β :=
Id.run <| as.foldlM f (init := init)
@[inline]
def foldr {α : Type u} {β : Type v} (f : α → β → β) (init : β) (as : Subarray α) : β :=
Id.run <| as.foldrM f (init := init)
@[inline]
def any {α : Type u} (p : α → Bool) (as : Subarray α) : Bool :=
Id.run <| as.anyM p
@[inline]
def all {α : Type u} (p : α → Bool) (as : Subarray α) : Bool :=
Id.run <| as.allM p
@[inline]
def findSomeRevM? {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (as : Subarray α) (f : α → m (Option β)) : m (Option β) :=
let rec @[specialize] find : (i : Nat) → i ≤ as.size → m (Option β)
| 0, _ => pure none
| i+1, h => do
have : i < as.size := Nat.lt_of_lt_of_le (Nat.lt_succ_self _) h
let r ← f as[i]
match r with
| some _ => pure r
| none =>
have : i ≤ as.size := Nat.le_of_lt this
find i this
find as.size (Nat.le_refl _)
@[inline]
def findRevM? {α : Type} {m : Type → Type w} [Monad m] (as : Subarray α) (p : α → m Bool) : m (Option α) :=
as.findSomeRevM? fun a => return if (← p a) then some a else none
@[inline]
def findRev? {α : Type} (as : Subarray α) (p : α → Bool) : Option α :=
Id.run <| as.findRevM? p
end Subarray
namespace Array
variable {α : Type u}
def toSubarray (as : Array α) (start : Nat := 0) (stop : Nat := as.size) : Subarray α :=
if h₂ : stop ≤ as.size then
if h₁ : start ≤ stop then
{ as := as, start := start, stop := stop, h₁ := h₁, h₂ := h₂ }
else
{ as := as, start := stop, stop := stop, h₁ := Nat.le_refl _, h₂ := h₂ }
else
if h₁ : start ≤ as.size then
{ as := as, start := start, stop := as.size, h₁ := h₁, h₂ := Nat.le_refl _ }
else
{ as := as, start := as.size, stop := as.size, h₁ := Nat.le_refl _, h₂ := Nat.le_refl _ }
def ofSubarray (s : Subarray α) : Array α := Id.run do
let mut as := mkEmpty (s.stop - s.start)
for a in s do
as := as.push a
return as
def extract (as : Array α) (start stop : Nat) : Array α :=
ofSubarray (as.toSubarray start stop)
instance : Coe (Subarray α) (Array α) := ⟨ofSubarray⟩
syntax:max term noWs "[" withoutPosition(term ":" term) "]" : term
syntax:max term noWs "[" withoutPosition(term ":") "]" : term
syntax:max term noWs "[" withoutPosition(":" term) "]" : term
macro_rules
| `($a[$start : $stop]) => `(Array.toSubarray $a $start $stop)
| `($a[ : $stop]) => `(Array.toSubarray $a 0 $stop)
| `($a[$start : ]) => `(let a := $a; Array.toSubarray a $start a.size)
end Array
def Subarray.toArray (s : Subarray α) : Array α :=
Array.ofSubarray s
instance : Append (Subarray α) where
append x y :=
let a := x.toArray ++ y.toArray
a.toSubarray 0 a.size
instance [Repr α] : Repr (Subarray α) where
reprPrec s _ := repr s.toArray ++ ".toSubarray"
instance [ToString α] : ToString (Subarray α) where
toString s := toString s.toArray
|
11ca74a373358f073fd7a2a5edcd3b22f8648a81 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/topology/uniform_space/uniform_convergence.lean | dc3019f60f8472b3b5113bd02eab562c782b6e87 | [] | no_license | AurelienSaue/Mathlib4_auto | f538cfd0980f65a6361eadea39e6fc639e9dae14 | 590df64109b08190abe22358fabc3eae000943f2 | refs/heads/master | 1,683,906,849,776 | 1,622,564,669,000 | 1,622,564,669,000 | 371,723,747 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 19,569 | lean | /-
Copyright (c) 2020 Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sébastien Gouëzel
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.topology.uniform_space.basic
import Mathlib.PostPort
universes u v u_1 w
namespace Mathlib
/-!
# Uniform convergence
A sequence of functions `Fₙ` (with values in a metric space) converges uniformly on a set `s` to a
function `f` if, for all `ε > 0`, for all large enough `n`, one has for all `y ∈ s` the inequality
`dist (f y, Fₙ y) < ε`. Under uniform convergence, many properties of the `Fₙ` pass to the limit,
most notably continuity. We prove this in the file, defining the notion of uniform convergence
in the more general setting of uniform spaces, and with respect to an arbitrary indexing set
endowed with a filter (instead of just `ℕ` with `at_top`).
## Main results
Let `α` be a topological space, `β` a uniform space, `Fₙ` and `f` be functions from `α`to `β`
(where the index `n` belongs to an indexing type `ι` endowed with a filter `p`).
* `tendsto_uniformly_on F f p s`: the fact that `Fₙ` converges uniformly to `f` on `s`. This means
that, for any entourage `u` of the diagonal, for large enough `n` (with respect to `p`), one has
`(f y, Fₙ y) ∈ u` for all `y ∈ s`.
* `tendsto_uniformly F f p`: same notion with `s = univ`.
* `tendsto_uniformly_on.continuous_on`: a uniform limit on a set of functions which are continuous
on this set is itself continuous on this set.
* `tendsto_uniformly.continuous`: a uniform limit of continuous functions is continuous.
* `tendsto_uniformly_on.tendsto_comp`: If `Fₙ` tends uniformly to `f` on a set `s`, and `gₙ` tends
to `x` within `s`, then `Fₙ gₙ` tends to `f x` if `f` is continuous at `x` within `s`.
* `tendsto_uniformly.tendsto_comp`: If `Fₙ` tends uniformly to `f`, and `gₙ` tends to `x`, then
`Fₙ gₙ` tends to `f x`.
We also define notions where the convergence is locally uniform, called
`tendsto_locally_uniformly_on F f p s` and `tendsto_locally_uniformly F f p`. The previous theorems
all have corresponding versions under locally uniform convergence.
## Implementation notes
Most results hold under weaker assumptions of locally uniform approximation. In a first section,
we prove the results under these weaker assumptions. Then, we derive the results on uniform
convergence from them.
## Tags
Uniform limit, uniform convergence, tends uniformly to
-/
/-!
### Different notions of uniform convergence
We define uniform convergence and locally uniform convergence, on a set or in the whole space.
-/
/-- A sequence of functions `Fₙ` converges uniformly on a set `s` to a limiting function `f` with
respect to the filter `p` if, for any entourage of the diagonal `u`, one has `p`-eventually
`(f x, Fₙ x) ∈ u` for all `x ∈ s`. -/
def tendsto_uniformly_on {α : Type u} {β : Type v} {ι : Type u_1} [uniform_space β] (F : ι → α → β) (f : α → β) (p : filter ι) (s : set α) :=
∀ (u : set (β × β)), u ∈ uniformity β → filter.eventually (fun (n : ι) => ∀ (x : α), x ∈ s → (f x, F n x) ∈ u) p
/-- A sequence of functions `Fₙ` converges uniformly to a limiting function `f` with respect to a
filter `p` if, for any entourage of the diagonal `u`, one has `p`-eventually
`(f x, Fₙ x) ∈ u` for all `x`. -/
def tendsto_uniformly {α : Type u} {β : Type v} {ι : Type u_1} [uniform_space β] (F : ι → α → β) (f : α → β) (p : filter ι) :=
∀ (u : set (β × β)), u ∈ uniformity β → filter.eventually (fun (n : ι) => ∀ (x : α), (f x, F n x) ∈ u) p
theorem tendsto_uniformly_on_univ {α : Type u} {β : Type v} {ι : Type u_1} [uniform_space β] {F : ι → α → β} {f : α → β} {p : filter ι} : tendsto_uniformly_on F f p set.univ ↔ tendsto_uniformly F f p := sorry
theorem tendsto_uniformly_on.mono {α : Type u} {β : Type v} {ι : Type u_1} [uniform_space β] {F : ι → α → β} {f : α → β} {s : set α} {p : filter ι} {s' : set α} (h : tendsto_uniformly_on F f p s) (h' : s' ⊆ s) : tendsto_uniformly_on F f p s' :=
fun (u : set (β × β)) (hu : u ∈ uniformity β) =>
filter.eventually.mono (h u hu)
fun (n : ι) (hn : ∀ (x : α), x ∈ s → (f x, F n x) ∈ u) (x : α) (hx : x ∈ s') => hn x (h' hx)
theorem tendsto_uniformly.tendsto_uniformly_on {α : Type u} {β : Type v} {ι : Type u_1} [uniform_space β] {F : ι → α → β} {f : α → β} {s : set α} {p : filter ι} (h : tendsto_uniformly F f p) : tendsto_uniformly_on F f p s :=
tendsto_uniformly_on.mono (iff.mpr tendsto_uniformly_on_univ h) (set.subset_univ s)
/-- Composing on the right by a function preserves uniform convergence on a set -/
theorem tendsto_uniformly_on.comp {α : Type u} {β : Type v} {γ : Type w} {ι : Type u_1} [uniform_space β] {F : ι → α → β} {f : α → β} {s : set α} {p : filter ι} (h : tendsto_uniformly_on F f p s) (g : γ → α) : tendsto_uniformly_on (fun (n : ι) => F n ∘ g) (f ∘ g) p (g ⁻¹' s) := sorry
/-- Composing on the right by a function preserves uniform convergence -/
theorem tendsto_uniformly.comp {α : Type u} {β : Type v} {γ : Type w} {ι : Type u_1} [uniform_space β] {F : ι → α → β} {f : α → β} {p : filter ι} (h : tendsto_uniformly F f p) (g : γ → α) : tendsto_uniformly (fun (n : ι) => F n ∘ g) (f ∘ g) p :=
id
fun (u : set (β × β)) (hu : u ∈ uniformity β) =>
filter.eventually.mono (h u hu) fun (n : ι) (hn : ∀ (x : α), (f x, F n x) ∈ u) (x : γ) => hn (g x)
/-- A sequence of functions `Fₙ` converges locally uniformly on a set `s` to a limiting function
`f` with respect to a filter `p` if, for any entourage of the diagonal `u`, for any `x ∈ s`, one
has `p`-eventually `(f x, Fₙ x) ∈ u` for all `y` in a neighborhood of `x` in `s`. -/
def tendsto_locally_uniformly_on {α : Type u} {β : Type v} {ι : Type u_1} [uniform_space β] [topological_space α] (F : ι → α → β) (f : α → β) (p : filter ι) (s : set α) :=
∀ (u : set (β × β)) (H : u ∈ uniformity β) (x : α) (H : x ∈ s),
∃ (t : set α), ∃ (H : t ∈ nhds_within x s), filter.eventually (fun (n : ι) => ∀ (y : α), y ∈ t → (f y, F n y) ∈ u) p
/-- A sequence of functions `Fₙ` converges locally uniformly to a limiting function `f` with respect
to a filter `p` if, for any entourage of the diagonal `u`, for any `x`, one has `p`-eventually
`(f x, Fₙ x) ∈ u` for all `y` in a neighborhood of `x`. -/
def tendsto_locally_uniformly {α : Type u} {β : Type v} {ι : Type u_1} [uniform_space β] [topological_space α] (F : ι → α → β) (f : α → β) (p : filter ι) :=
∀ (u : set (β × β)) (H : u ∈ uniformity β) (x : α),
∃ (t : set α), ∃ (H : t ∈ nhds x), filter.eventually (fun (n : ι) => ∀ (y : α), y ∈ t → (f y, F n y) ∈ u) p
theorem tendsto_uniformly_on.tendsto_locally_uniformly_on {α : Type u} {β : Type v} {ι : Type u_1} [uniform_space β] {F : ι → α → β} {f : α → β} {s : set α} {p : filter ι} [topological_space α] (h : tendsto_uniformly_on F f p s) : tendsto_locally_uniformly_on F f p s :=
fun (u : set (β × β)) (hu : u ∈ uniformity β) (x : α) (hx : x ∈ s) =>
Exists.intro s (Exists.intro self_mem_nhds_within (h u hu))
theorem tendsto_uniformly.tendsto_locally_uniformly {α : Type u} {β : Type v} {ι : Type u_1} [uniform_space β] {F : ι → α → β} {f : α → β} {p : filter ι} [topological_space α] (h : tendsto_uniformly F f p) : tendsto_locally_uniformly F f p := sorry
theorem tendsto_locally_uniformly_on.mono {α : Type u} {β : Type v} {ι : Type u_1} [uniform_space β] {F : ι → α → β} {f : α → β} {s : set α} {s' : set α} {p : filter ι} [topological_space α] (h : tendsto_locally_uniformly_on F f p s) (h' : s' ⊆ s) : tendsto_locally_uniformly_on F f p s' := sorry
theorem tendsto_locally_uniformly_on_univ {α : Type u} {β : Type v} {ι : Type u_1} [uniform_space β] {F : ι → α → β} {f : α → β} {p : filter ι} [topological_space α] : tendsto_locally_uniformly_on F f p set.univ ↔ tendsto_locally_uniformly F f p := sorry
theorem tendsto_locally_uniformly_on.comp {α : Type u} {β : Type v} {γ : Type w} {ι : Type u_1} [uniform_space β] {F : ι → α → β} {f : α → β} {s : set α} {p : filter ι} [topological_space α] [topological_space γ] {t : set γ} (h : tendsto_locally_uniformly_on F f p s) (g : γ → α) (hg : set.maps_to g t s) (cg : continuous_on g t) : tendsto_locally_uniformly_on (fun (n : ι) => F n ∘ g) (f ∘ g) p t := sorry
theorem tendsto_locally_uniformly.comp {α : Type u} {β : Type v} {γ : Type w} {ι : Type u_1} [uniform_space β] {F : ι → α → β} {f : α → β} {p : filter ι} [topological_space α] [topological_space γ] (h : tendsto_locally_uniformly F f p) (g : γ → α) (cg : continuous g) : tendsto_locally_uniformly (fun (n : ι) => F n ∘ g) (f ∘ g) p := sorry
/-!
### Uniform approximation
In this section, we give lemmas ensuring that a function is continuous if it can be approximated
uniformly by continuous functions. We give various versions, within a set or the whole space, at
a single point or at all points, with locally uniform approximation or uniform approximation. All
the statements are derived from a statement about locally uniform approximation within a set at
a point, called `continuous_within_at_of_locally_uniform_approx_of_continuous_within_at`. -/
/-- A function which can be locally uniformly approximated by functions which are continuous
within a set at a point is continuous within this set at this point. -/
theorem continuous_within_at_of_locally_uniform_approx_of_continuous_within_at {α : Type u} {β : Type v} {ι : Type u_1} [uniform_space β] {F : ι → α → β} {f : α → β} {s : set α} {x : α} [topological_space α] (hx : x ∈ s) (L : ∀ (u : set (β × β)) (H : u ∈ uniformity β),
∃ (t : set α), ∃ (H : t ∈ nhds_within x s), ∃ (n : ι), ∀ (y : α), y ∈ t → (f y, F n y) ∈ u) (C : ∀ (n : ι), continuous_within_at (F n) s x) : continuous_within_at f s x := sorry
/-- A function which can be locally uniformly approximated by functions which are continuous at
a point is continuous at this point. -/
theorem continuous_at_of_locally_uniform_approx_of_continuous_at {α : Type u} {β : Type v} {ι : Type u_1} [uniform_space β] {F : ι → α → β} {f : α → β} {x : α} [topological_space α] (L : ∀ (u : set (β × β)) (H : u ∈ uniformity β),
∃ (t : set α), ∃ (H : t ∈ nhds x), ∃ (n : ι), ∀ (y : α), y ∈ t → (f y, F n y) ∈ u) (C : ∀ (n : ι), continuous_at (F n) x) : continuous_at f x := sorry
/-- A function which can be locally uniformly approximated by functions which are continuous
on a set is continuous on this set. -/
theorem continuous_on_of_locally_uniform_approx_of_continuous_on {α : Type u} {β : Type v} {ι : Type u_1} [uniform_space β] {F : ι → α → β} {f : α → β} {s : set α} [topological_space α] (L : ∀ (x : α) (H : x ∈ s) (u : set (β × β)) (H : u ∈ uniformity β),
∃ (t : set α), ∃ (H : t ∈ nhds_within x s), ∃ (n : ι), ∀ (y : α), y ∈ t → (f y, F n y) ∈ u) (C : ∀ (n : ι), continuous_on (F n) s) : continuous_on f s :=
fun (x : α) (hx : x ∈ s) =>
continuous_within_at_of_locally_uniform_approx_of_continuous_within_at hx (L x hx) fun (n : ι) => C n x hx
/-- A function which can be uniformly approximated by functions which are continuous on a set
is continuous on this set. -/
theorem continuous_on_of_uniform_approx_of_continuous_on {α : Type u} {β : Type v} {ι : Type u_1} [uniform_space β] {F : ι → α → β} {f : α → β} {s : set α} [topological_space α] (L : ∀ (u : set (β × β)), u ∈ uniformity β → ∃ (n : ι), ∀ (y : α), y ∈ s → (f y, F n y) ∈ u) : (∀ (n : ι), continuous_on (F n) s) → continuous_on f s :=
continuous_on_of_locally_uniform_approx_of_continuous_on
fun (x : α) (hx : x ∈ s) (u : set (β × β)) (hu : u ∈ uniformity β) =>
Exists.intro s (Exists.intro self_mem_nhds_within (L u hu))
/-- A function which can be locally uniformly approximated by continuous functions is continuous. -/
theorem continuous_of_locally_uniform_approx_of_continuous {α : Type u} {β : Type v} {ι : Type u_1} [uniform_space β] {F : ι → α → β} {f : α → β} [topological_space α] (L : ∀ (x : α) (u : set (β × β)) (H : u ∈ uniformity β),
∃ (t : set α), ∃ (H : t ∈ nhds x), ∃ (n : ι), ∀ (y : α), y ∈ t → (f y, F n y) ∈ u) (C : ∀ (n : ι), continuous (F n)) : continuous f := sorry
/-- A function which can be uniformly approximated by continuous functions is continuous. -/
theorem continuous_of_uniform_approx_of_continuous {α : Type u} {β : Type v} {ι : Type u_1} [uniform_space β] {F : ι → α → β} {f : α → β} [topological_space α] (L : ∀ (u : set (β × β)), u ∈ uniformity β → ∃ (N : ι), ∀ (y : α), (f y, F N y) ∈ u) : (∀ (n : ι), continuous (F n)) → continuous f := sorry
/-!
### Uniform limits
From the previous statements on uniform approximation, we deduce continuity results for uniform
limits.
-/
/-- A locally uniform limit on a set of functions which are continuous on this set is itself
continuous on this set. -/
theorem tendsto_locally_uniformly_on.continuous_on {α : Type u} {β : Type v} {ι : Type u_1} [uniform_space β] {F : ι → α → β} {f : α → β} {s : set α} {p : filter ι} [topological_space α] (h : tendsto_locally_uniformly_on F f p s) (hc : ∀ (n : ι), continuous_on (F n) s) [filter.ne_bot p] : continuous_on f s := sorry
/-- A uniform limit on a set of functions which are continuous on this set is itself continuous
on this set. -/
theorem tendsto_uniformly_on.continuous_on {α : Type u} {β : Type v} {ι : Type u_1} [uniform_space β] {F : ι → α → β} {f : α → β} {s : set α} {p : filter ι} [topological_space α] (h : tendsto_uniformly_on F f p s) (hc : ∀ (n : ι), continuous_on (F n) s) [filter.ne_bot p] : continuous_on f s :=
tendsto_locally_uniformly_on.continuous_on (tendsto_uniformly_on.tendsto_locally_uniformly_on h) hc
/-- A locally uniform limit of continuous functions is continuous. -/
theorem tendsto_locally_uniformly.continuous {α : Type u} {β : Type v} {ι : Type u_1} [uniform_space β] {F : ι → α → β} {f : α → β} {p : filter ι} [topological_space α] (h : tendsto_locally_uniformly F f p) (hc : ∀ (n : ι), continuous (F n)) [filter.ne_bot p] : continuous f := sorry
/-- A uniform limit of continuous functions is continuous. -/
theorem tendsto_uniformly.continuous {α : Type u} {β : Type v} {ι : Type u_1} [uniform_space β] {F : ι → α → β} {f : α → β} {p : filter ι} [topological_space α] (h : tendsto_uniformly F f p) (hc : ∀ (n : ι), continuous (F n)) [filter.ne_bot p] : continuous f :=
tendsto_locally_uniformly.continuous (tendsto_uniformly.tendsto_locally_uniformly h) hc
/-!
### Composing limits under uniform convergence
In general, if `Fₙ` converges pointwise to a function `f`, and `gₙ` tends to `x`, it is not true
that `Fₙ gₙ` tends to `f x`. It is true however if the convergence of `Fₙ` to `f` is uniform. In
this paragraph, we prove variations around this statement.
-/
/-- If `Fₙ` converges locally uniformly on a neighborhood of `x` within a set `s` to a function `f`
which is continuous at `x` within `s `, and `gₙ` tends to `x` within `s`, then `Fₙ (gₙ)` tends
to `f x`. -/
theorem tendsto_comp_of_locally_uniform_limit_within {α : Type u} {β : Type v} {ι : Type u_1} [uniform_space β] {F : ι → α → β} {f : α → β} {s : set α} {x : α} {p : filter ι} {g : ι → α} [topological_space α] (h : continuous_within_at f s x) (hg : filter.tendsto g p (nhds_within x s)) (hunif : ∀ (u : set (β × β)) (H : u ∈ uniformity β),
∃ (t : set α), ∃ (H : t ∈ nhds_within x s), filter.eventually (fun (n : ι) => ∀ (y : α), y ∈ t → (f y, F n y) ∈ u) p) : filter.tendsto (fun (n : ι) => F n (g n)) p (nhds (f x)) := sorry
/-- If `Fₙ` converges locally uniformly on a neighborhood of `x` to a function `f` which is
continuous at `x`, and `gₙ` tends to `x`, then `Fₙ (gₙ)` tends to `f x`. -/
theorem tendsto_comp_of_locally_uniform_limit {α : Type u} {β : Type v} {ι : Type u_1} [uniform_space β] {F : ι → α → β} {f : α → β} {x : α} {p : filter ι} {g : ι → α} [topological_space α] (h : continuous_at f x) (hg : filter.tendsto g p (nhds x)) (hunif : ∀ (u : set (β × β)) (H : u ∈ uniformity β),
∃ (t : set α), ∃ (H : t ∈ nhds x), filter.eventually (fun (n : ι) => ∀ (y : α), y ∈ t → (f y, F n y) ∈ u) p) : filter.tendsto (fun (n : ι) => F n (g n)) p (nhds (f x)) := sorry
/-- If `Fₙ` tends locally uniformly to `f` on a set `s`, and `gₙ` tends to `x` within `s`, then
`Fₙ gₙ` tends to `f x` if `f` is continuous at `x` within `s` and `x ∈ s`. -/
theorem tendsto_locally_uniformly_on.tendsto_comp {α : Type u} {β : Type v} {ι : Type u_1} [uniform_space β] {F : ι → α → β} {f : α → β} {s : set α} {x : α} {p : filter ι} {g : ι → α} [topological_space α] (h : tendsto_locally_uniformly_on F f p s) (hf : continuous_within_at f s x) (hx : x ∈ s) (hg : filter.tendsto g p (nhds_within x s)) : filter.tendsto (fun (n : ι) => F n (g n)) p (nhds (f x)) :=
tendsto_comp_of_locally_uniform_limit_within hf hg fun (u : set (β × β)) (hu : u ∈ uniformity β) => h u hu x hx
/-- If `Fₙ` tends uniformly to `f` on a set `s`, and `gₙ` tends to `x` within `s`, then `Fₙ gₙ` tends
to `f x` if `f` is continuous at `x` within `s`. -/
theorem tendsto_uniformly_on.tendsto_comp {α : Type u} {β : Type v} {ι : Type u_1} [uniform_space β] {F : ι → α → β} {f : α → β} {s : set α} {x : α} {p : filter ι} {g : ι → α} [topological_space α] (h : tendsto_uniformly_on F f p s) (hf : continuous_within_at f s x) (hg : filter.tendsto g p (nhds_within x s)) : filter.tendsto (fun (n : ι) => F n (g n)) p (nhds (f x)) :=
tendsto_comp_of_locally_uniform_limit_within hf hg
fun (u : set (β × β)) (hu : u ∈ uniformity β) => Exists.intro s (Exists.intro self_mem_nhds_within (h u hu))
/-- If `Fₙ` tends locally uniformly to `f`, and `gₙ` tends to `x`, then `Fₙ gₙ` tends to `f x`. -/
theorem tendsto_locally_uniformly.tendsto_comp {α : Type u} {β : Type v} {ι : Type u_1} [uniform_space β] {F : ι → α → β} {f : α → β} {x : α} {p : filter ι} {g : ι → α} [topological_space α] (h : tendsto_locally_uniformly F f p) (hf : continuous_at f x) (hg : filter.tendsto g p (nhds x)) : filter.tendsto (fun (n : ι) => F n (g n)) p (nhds (f x)) :=
tendsto_comp_of_locally_uniform_limit hf hg fun (u : set (β × β)) (hu : u ∈ uniformity β) => h u hu x
/-- If `Fₙ` tends uniformly to `f`, and `gₙ` tends to `x`, then `Fₙ gₙ` tends to `f x`. -/
theorem tendsto_uniformly.tendsto_comp {α : Type u} {β : Type v} {ι : Type u_1} [uniform_space β] {F : ι → α → β} {f : α → β} {x : α} {p : filter ι} {g : ι → α} [topological_space α] (h : tendsto_uniformly F f p) (hf : continuous_at f x) (hg : filter.tendsto g p (nhds x)) : filter.tendsto (fun (n : ι) => F n (g n)) p (nhds (f x)) :=
tendsto_locally_uniformly.tendsto_comp (tendsto_uniformly.tendsto_locally_uniformly h) hf hg
|
2c416ad04f82b130f9e029da59ca20117b558e81 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/category_theory/sites/sieves.lean | 41522c7061f28dac001cd2ab3c139b7a5a44d307 | [
"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 | 25,001 | lean | /-
Copyright (c) 2020 Bhavik Mehta. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Bhavik Mehta, E. W. Ayers
-/
import order.complete_lattice
import category_theory.over
import category_theory.yoneda
import category_theory.limits.shapes.pullbacks
import data.set.lattice
/-!
# Theory of sieves
- For an object `X` of a category `C`, a `sieve X` is a set of morphisms to `X`
which is closed under left-composition.
- The complete lattice structure on sieves is given, as well as the Galois insertion
given by downward-closing.
- A `sieve X` (functorially) induces a presheaf on `C` together with a monomorphism to
the yoneda embedding of `X`.
## Tags
sieve, pullback
-/
universes v₁ v₂ v₃ u₁ u₂ u₃
namespace category_theory
open category limits
variables {C : Type u₁} [category.{v₁} C] {D : Type u₂} [category.{v₂} D] (F : C ⥤ D)
variables {X Y Z : C} (f : Y ⟶ X)
/-- A set of arrows all with codomain `X`. -/
@[derive complete_lattice]
def presieve (X : C) := Π ⦃Y⦄, set (Y ⟶ X)
namespace presieve
instance : inhabited (presieve X) := ⟨⊤⟩
/-- Given a sieve `S` on `X : C`, its associated diagram `S.diagram` is defined to be
the natural functor from the full subcategory of the over category `C/X` consisting
of arrows in `S` to `C`. -/
abbreviation diagram (S : presieve X) : {f : over X // S f.hom} ⥤ C :=
full_subcategory_inclusion _ ⋙ over.forget X
/-- Given a sieve `S` on `X : C`, its associated cocone `S.cocone` is defined to be
the natural cocone over the diagram defined above with cocone point `X`. -/
abbreviation cocone (S : presieve X) : cocone S.diagram :=
(over.forget_cocone X).whisker (full_subcategory_inclusion _)
/--
Given a set of arrows `S` all with codomain `X`, and a set of arrows with codomain `Y` for each
`f : Y ⟶ X` in `S`, produce a set of arrows with codomain `X`:
`{ g ≫ f | (f : Y ⟶ X) ∈ S, (g : Z ⟶ Y) ∈ R f }`.
-/
def bind (S : presieve X) (R : Π ⦃Y⦄ ⦃f : Y ⟶ X⦄, S f → presieve Y) :
presieve X :=
λ Z h, ∃ (Y : C) (g : Z ⟶ Y) (f : Y ⟶ X) (H : S f), R H g ∧ g ≫ f = h
@[simp]
lemma bind_comp {S : presieve X}
{R : Π ⦃Y : C⦄ ⦃f : Y ⟶ X⦄, S f → presieve Y} {g : Z ⟶ Y} (h₁ : S f) (h₂ : R h₁ g) :
bind S R (g ≫ f) :=
⟨_, _, _, h₁, h₂, rfl⟩
/-- The singleton presieve. -/
-- Note we can't make this into `has_singleton` because of the out-param.
inductive singleton : presieve X
| mk : singleton f
@[simp] lemma singleton_eq_iff_domain (f g : Y ⟶ X) : singleton f g ↔ f = g :=
begin
split,
{ rintro ⟨a, rfl⟩,
refl },
{ rintro rfl,
apply singleton.mk, }
end
lemma singleton_self : singleton f f := singleton.mk
/--
Pullback a set of arrows with given codomain along a fixed map, by taking the pullback in the
category.
This is not the same as the arrow set of `sieve.pullback`, but there is a relation between them
in `pullback_arrows_comm`.
-/
inductive pullback_arrows [has_pullbacks C] (R : presieve X) :
presieve Y
| mk (Z : C) (h : Z ⟶ X) : R h → pullback_arrows (pullback.snd : pullback h f ⟶ Y)
lemma pullback_singleton [has_pullbacks C] (g : Z ⟶ X) :
pullback_arrows f (singleton g) = singleton (pullback.snd : pullback g f ⟶ _) :=
begin
ext W h,
split,
{ rintro ⟨W, _, _, _⟩,
exact singleton.mk },
{ rintro ⟨_⟩,
exact pullback_arrows.mk Z g singleton.mk }
end
/-- Construct the presieve given by the family of arrows indexed by `ι`. -/
inductive of_arrows {ι : Type*} (Y : ι → C) (f : Π i, Y i ⟶ X) : presieve X
| mk (i : ι) : of_arrows (f i)
lemma of_arrows_punit :
of_arrows _ (λ _ : punit, f) = singleton f :=
begin
ext Y g,
split,
{ rintro ⟨_⟩,
apply singleton.mk },
{ rintro ⟨_⟩,
exact of_arrows.mk punit.star },
end
lemma of_arrows_pullback [has_pullbacks C] {ι : Type*}
(Z : ι → C) (g : Π (i : ι), Z i ⟶ X) :
of_arrows (λ i, pullback (g i) f) (λ i, pullback.snd) =
pullback_arrows f (of_arrows Z g) :=
begin
ext T h,
split,
{ rintro ⟨hk⟩,
exact pullback_arrows.mk _ _ (of_arrows.mk hk) },
{ rintro ⟨W, k, hk₁⟩,
cases hk₁ with i hi,
apply of_arrows.mk },
end
lemma of_arrows_bind {ι : Type*} (Z : ι → C) (g : Π (i : ι), Z i ⟶ X)
(j : Π ⦃Y⦄ (f : Y ⟶ X), of_arrows Z g f → Type*)
(W : Π ⦃Y⦄ (f : Y ⟶ X) H, j f H → C)
(k : Π ⦃Y⦄ (f : Y ⟶ X) H i, W f H i ⟶ Y) :
(of_arrows Z g).bind (λ Y f H, of_arrows (W f H) (k f H)) =
of_arrows (λ (i : Σ i, j _ (of_arrows.mk i)), W (g i.1) _ i.2)
(λ ij, k (g ij.1) _ ij.2 ≫ g ij.1) :=
begin
ext Y f,
split,
{ rintro ⟨_, _, _, ⟨i⟩, ⟨i'⟩, rfl⟩,
exact of_arrows.mk (sigma.mk _ _) },
{ rintro ⟨i⟩,
exact bind_comp _ (of_arrows.mk _) (of_arrows.mk _) }
end
/-- Given a presieve on `F(X)`, we can define a presieve on `X` by taking the preimage via `F`. -/
def functor_pullback (R : presieve (F.obj X)) : presieve X := λ _ f, R (F.map f)
@[simp] lemma functor_pullback_mem (R : presieve (F.obj X)) {Y} (f : Y ⟶ X) :
R.functor_pullback F f ↔ R (F.map f) := iff.rfl
@[simp] lemma functor_pullback_id (R : presieve X) : R.functor_pullback (𝟭 _) = R := rfl
section functor_pushforward
variables {E : Type u₃} [category.{v₃} E] (G : D ⥤ E)
/--
Given a presieve on `X`, we can define a presieve on `F(X)` (which is actually a sieve)
by taking the sieve generated by the image via `F`.
-/
def functor_pushforward (S : presieve X) : presieve (F.obj X) :=
λ Y f, ∃ (Z : C) (g : Z ⟶ X) (h : Y ⟶ F.obj Z), S g ∧ f = h ≫ F.map g
/--
An auxillary definition in order to fix the choice of the preimages between various definitions.
-/
@[nolint has_inhabited_instance]
structure functor_pushforward_structure (S : presieve X) {Y} (f : Y ⟶ F.obj X) :=
(preobj : C) (premap : preobj ⟶ X) (lift : Y ⟶ F.obj preobj)
(cover : S premap) (fac : f = lift ≫ F.map premap)
/-- The fixed choice of a preimage. -/
noncomputable def get_functor_pushforward_structure {F : C ⥤ D} {S : presieve X} {Y : D}
{f : Y ⟶ F.obj X} (h : S.functor_pushforward F f) : functor_pushforward_structure F S f :=
by { choose Z f' g h₁ h using h, exact ⟨Z, f', g, h₁, h⟩ }
lemma functor_pushforward_comp (R : presieve X) :
R.functor_pushforward (F ⋙ G) = (R.functor_pushforward F).functor_pushforward G :=
begin
ext x f,
split,
{ rintro ⟨X, f₁, g₁, h₁, rfl⟩, exact ⟨F.obj X, F.map f₁, g₁, ⟨X, f₁, 𝟙 _, h₁, by simp⟩, rfl⟩ },
{ rintro ⟨X, f₁, g₁, ⟨X', f₂, g₂, h₁, rfl⟩, rfl⟩, use ⟨X', f₂, g₁ ≫ G.map g₂, h₁, by simp⟩ }
end
lemma image_mem_functor_pushforward (R : presieve X) {f : Y ⟶ X} (h : R f) :
R.functor_pushforward F (F.map f) := ⟨Y, f, 𝟙 _, h, by simp⟩
end functor_pushforward
end presieve
/--
For an object `X` of a category `C`, a `sieve X` is a set of morphisms to `X` which is closed under
left-composition.
-/
structure sieve {C : Type u₁} [category.{v₁} C] (X : C) :=
(arrows : presieve X)
(downward_closed' : ∀ {Y Z f} (hf : arrows f) (g : Z ⟶ Y), arrows (g ≫ f))
namespace sieve
instance : has_coe_to_fun (sieve X) (λ _, presieve X) := ⟨sieve.arrows⟩
initialize_simps_projections sieve (arrows → apply)
variables {S R : sieve X}
@[simp, priority 100] lemma downward_closed (S : sieve X) {f : Y ⟶ X} (hf : S f)
(g : Z ⟶ Y) : S (g ≫ f) :=
S.downward_closed' hf g
lemma arrows_ext : Π {R S : sieve X}, R.arrows = S.arrows → R = S
| ⟨Ra, _⟩ ⟨Sa, _⟩ rfl := rfl
@[ext]
protected lemma ext {R S : sieve X}
(h : ∀ ⦃Y⦄ (f : Y ⟶ X), R f ↔ S f) :
R = S :=
arrows_ext $ funext $ λ x, funext $ λ f, propext $ h f
protected lemma ext_iff {R S : sieve X} :
R = S ↔ (∀ ⦃Y⦄ (f : Y ⟶ X), R f ↔ S f) :=
⟨λ h Y f, h ▸ iff.rfl, sieve.ext⟩
open lattice
/-- The supremum of a collection of sieves: the union of them all. -/
protected def Sup (𝒮 : set (sieve X)) : (sieve X) :=
{ arrows := λ Y, {f | ∃ S ∈ 𝒮, sieve.arrows S f},
downward_closed' := λ Y Z f, by { rintro ⟨S, hS, hf⟩ g, exact ⟨S, hS, S.downward_closed hf _⟩ } }
/-- The infimum of a collection of sieves: the intersection of them all. -/
protected def Inf (𝒮 : set (sieve X)) : (sieve X) :=
{ arrows := λ Y, {f | ∀ S ∈ 𝒮, sieve.arrows S f},
downward_closed' := λ Y Z f hf g S H, S.downward_closed (hf S H) g }
/-- The union of two sieves is a sieve. -/
protected def union (S R : sieve X) : sieve X :=
{ arrows := λ Y f, S f ∨ R f,
downward_closed' := by { rintros Y Z f (h | h) g; simp [h] } }
/-- The intersection of two sieves is a sieve. -/
protected def inter (S R : sieve X) : sieve X :=
{ arrows := λ Y f, S f ∧ R f,
downward_closed' := by { rintros Y Z f ⟨h₁, h₂⟩ g, simp [h₁, h₂] } }
/--
Sieves on an object `X` form a complete lattice.
We generate this directly rather than using the galois insertion for nicer definitional properties.
-/
instance : complete_lattice (sieve X) :=
{ le := λ S R, ∀ ⦃Y⦄ (f : Y ⟶ X), S f → R f,
le_refl := λ S f q, id,
le_trans := λ S₁ S₂ S₃ S₁₂ S₂₃ Y f h, S₂₃ _ (S₁₂ _ h),
le_antisymm := λ S R p q, sieve.ext (λ Y f, ⟨p _, q _⟩),
top := { arrows := λ _, set.univ, downward_closed' := λ Y Z f g h, ⟨⟩ },
bot := { arrows := λ _, ∅, downward_closed' := λ _ _ _ p _, false.elim p },
sup := sieve.union,
inf := sieve.inter,
Sup := sieve.Sup,
Inf := sieve.Inf,
le_Sup := λ 𝒮 S hS Y f hf, ⟨S, hS, hf⟩,
Sup_le := λ ℰ S hS Y f, by { rintro ⟨R, hR, hf⟩, apply hS R hR _ hf },
Inf_le := λ _ _ hS _ _ h, h _ hS,
le_Inf := λ _ _ hS _ _ hf _ hR, hS _ hR _ hf,
le_sup_left := λ _ _ _ _, or.inl,
le_sup_right := λ _ _ _ _, or.inr,
sup_le := λ _ _ _ a b _ _ hf, hf.elim (a _) (b _),
inf_le_left := λ _ _ _ _, and.left,
inf_le_right := λ _ _ _ _, and.right,
le_inf := λ _ _ _ p q _ _ z, ⟨p _ z, q _ z⟩,
le_top := λ _ _ _ _, trivial,
bot_le := λ _ _ _, false.elim }
/-- The maximal sieve always exists. -/
instance sieve_inhabited : inhabited (sieve X) := ⟨⊤⟩
@[simp]
lemma Inf_apply {Ss : set (sieve X)} {Y} (f : Y ⟶ X) :
Inf Ss f ↔ ∀ (S : sieve X) (H : S ∈ Ss), S f :=
iff.rfl
@[simp]
lemma Sup_apply {Ss : set (sieve X)} {Y} (f : Y ⟶ X) :
Sup Ss f ↔ ∃ (S : sieve X) (H : S ∈ Ss), S f :=
iff.rfl
@[simp]
lemma inter_apply {R S : sieve X} {Y} (f : Y ⟶ X) :
(R ⊓ S) f ↔ R f ∧ S f :=
iff.rfl
@[simp]
lemma union_apply {R S : sieve X} {Y} (f : Y ⟶ X) :
(R ⊔ S) f ↔ R f ∨ S f :=
iff.rfl
@[simp]
lemma top_apply (f : Y ⟶ X) : (⊤ : sieve X) f := trivial
/-- Generate the smallest sieve containing the given set of arrows. -/
@[simps]
def generate (R : presieve X) : sieve X :=
{ arrows := λ Z f, ∃ Y (h : Z ⟶ Y) (g : Y ⟶ X), R g ∧ h ≫ g = f,
downward_closed' :=
begin
rintro Y Z _ ⟨W, g, f, hf, rfl⟩ h,
exact ⟨_, h ≫ g, _, hf, by simp⟩,
end }
/--
Given a presieve on `X`, and a sieve on each domain of an arrow in the presieve, we can bind to
produce a sieve on `X`.
-/
@[simps]
def bind (S : presieve X) (R : Π ⦃Y⦄ ⦃f : Y ⟶ X⦄, S f → sieve Y) : sieve X :=
{ arrows := S.bind (λ Y f h, R h),
downward_closed' :=
begin
rintro Y Z f ⟨W, f, h, hh, hf, rfl⟩ g,
exact ⟨_, g ≫ f, _, hh, by simp [hf]⟩,
end }
open order lattice
lemma sets_iff_generate (R : presieve X) (S : sieve X) :
generate R ≤ S ↔ R ≤ S :=
⟨λ H Y g hg, H _ ⟨_, 𝟙 _, _, hg, id_comp _⟩,
λ ss Y f,
begin
rintro ⟨Z, f, g, hg, rfl⟩,
exact S.downward_closed (ss Z hg) f,
end⟩
/-- Show that there is a galois insertion (generate, set_over). -/
def gi_generate : galois_insertion (generate : presieve X → sieve X) arrows :=
{ gc := sets_iff_generate,
choice := λ 𝒢 _, generate 𝒢,
choice_eq := λ _ _, rfl,
le_l_u := λ S Y f hf, ⟨_, 𝟙 _, _, hf, id_comp _⟩ }
lemma le_generate (R : presieve X) : R ≤ generate R :=
gi_generate.gc.le_u_l R
@[simp] lemma generate_sieve (S : sieve X) : generate S = S :=
gi_generate.l_u_eq S
/-- If the identity arrow is in a sieve, the sieve is maximal. -/
lemma id_mem_iff_eq_top : S (𝟙 X) ↔ S = ⊤ :=
⟨λ h, top_unique $ λ Y f _, by simpa using downward_closed _ h f,
λ h, h.symm ▸ trivial⟩
/-- If an arrow set contains a split epi, it generates the maximal sieve. -/
lemma generate_of_contains_split_epi {R : presieve X} (f : Y ⟶ X) [split_epi f]
(hf : R f) : generate R = ⊤ :=
begin
rw ← id_mem_iff_eq_top,
exact ⟨_, section_ f, f, hf, by simp⟩,
end
@[simp]
lemma generate_of_singleton_split_epi (f : Y ⟶ X) [split_epi f] :
generate (presieve.singleton f) = ⊤ :=
generate_of_contains_split_epi f (presieve.singleton_self _)
@[simp]
lemma generate_top : generate (⊤ : presieve X) = ⊤ :=
generate_of_contains_split_epi (𝟙 _) ⟨⟩
/-- Given a morphism `h : Y ⟶ X`, send a sieve S on X to a sieve on Y
as the inverse image of S with `_ ≫ h`.
That is, `sieve.pullback S h := (≫ h) '⁻¹ S`. -/
@[simps]
def pullback (h : Y ⟶ X) (S : sieve X) : sieve Y :=
{ arrows := λ Y sl, S (sl ≫ h),
downward_closed' := λ Z W f g h, by simp [g] }
@[simp]
lemma pullback_id : S.pullback (𝟙 _) = S :=
by simp [sieve.ext_iff]
@[simp]
lemma pullback_top {f : Y ⟶ X} : (⊤ : sieve X).pullback f = ⊤ :=
top_unique (λ _ g, id)
lemma pullback_comp {f : Y ⟶ X} {g : Z ⟶ Y} (S : sieve X) :
S.pullback (g ≫ f) = (S.pullback f).pullback g :=
by simp [sieve.ext_iff]
@[simp]
lemma pullback_inter {f : Y ⟶ X} (S R : sieve X) :
(S ⊓ R).pullback f = S.pullback f ⊓ R.pullback f :=
by simp [sieve.ext_iff]
lemma pullback_eq_top_iff_mem (f : Y ⟶ X) : S f ↔ S.pullback f = ⊤ :=
by rw [← id_mem_iff_eq_top, pullback_apply, id_comp]
lemma pullback_eq_top_of_mem (S : sieve X) {f : Y ⟶ X} : S f → S.pullback f = ⊤ :=
(pullback_eq_top_iff_mem f).1
/--
Push a sieve `R` on `Y` forward along an arrow `f : Y ⟶ X`: `gf : Z ⟶ X` is in the sieve if `gf`
factors through some `g : Z ⟶ Y` which is in `R`.
-/
@[simps]
def pushforward (f : Y ⟶ X) (R : sieve Y) : sieve X :=
{ arrows := λ Z gf, ∃ g, g ≫ f = gf ∧ R g,
downward_closed' := λ Z₁ Z₂ g ⟨j, k, z⟩ h, ⟨h ≫ j, by simp [k], by simp [z]⟩ }
lemma pushforward_apply_comp {R : sieve Y} {Z : C} {g : Z ⟶ Y} (hg : R g) (f : Y ⟶ X) :
R.pushforward f (g ≫ f) :=
⟨g, rfl, hg⟩
lemma pushforward_comp {f : Y ⟶ X} {g : Z ⟶ Y} (R : sieve Z) :
R.pushforward (g ≫ f) = (R.pushforward g).pushforward f :=
sieve.ext (λ W h, ⟨λ ⟨f₁, hq, hf₁⟩, ⟨f₁ ≫ g, by simpa, f₁, rfl, hf₁⟩,
λ ⟨y, hy, z, hR, hz⟩, ⟨z, by rwa reassoc_of hR, hz⟩⟩)
lemma galois_connection (f : Y ⟶ X) : galois_connection (sieve.pushforward f) (sieve.pullback f) :=
λ S R, ⟨λ hR Z g hg, hR _ ⟨g, rfl, hg⟩, λ hS Z g ⟨h, hg, hh⟩, hg ▸ hS h hh⟩
lemma pullback_monotone (f : Y ⟶ X) : monotone (sieve.pullback f) :=
(galois_connection f).monotone_u
lemma pushforward_monotone (f : Y ⟶ X) : monotone (sieve.pushforward f) :=
(galois_connection f).monotone_l
lemma le_pushforward_pullback (f : Y ⟶ X) (R : sieve Y) :
R ≤ (R.pushforward f).pullback f :=
(galois_connection f).le_u_l _
lemma pullback_pushforward_le (f : Y ⟶ X) (R : sieve X) :
(R.pullback f).pushforward f ≤ R :=
(galois_connection f).l_u_le _
lemma pushforward_union {f : Y ⟶ X} (S R : sieve Y) :
(S ⊔ R).pushforward f = S.pushforward f ⊔ R.pushforward f :=
(galois_connection f).l_sup
lemma pushforward_le_bind_of_mem (S : presieve X)
(R : Π ⦃Y : C⦄ ⦃f : Y ⟶ X⦄, S f → sieve Y) (f : Y ⟶ X) (h : S f) :
(R h).pushforward f ≤ bind S R :=
begin
rintro Z _ ⟨g, rfl, hg⟩,
exact ⟨_, g, f, h, hg, rfl⟩,
end
lemma le_pullback_bind (S : presieve X) (R : Π ⦃Y : C⦄ ⦃f : Y ⟶ X⦄, S f → sieve Y)
(f : Y ⟶ X) (h : S f) :
R h ≤ (bind S R).pullback f :=
begin
rw ← galois_connection f,
apply pushforward_le_bind_of_mem,
end
/-- If `f` is a monomorphism, the pushforward-pullback adjunction on sieves is coreflective. -/
def galois_coinsertion_of_mono (f : Y ⟶ X) [mono f] :
galois_coinsertion (sieve.pushforward f) (sieve.pullback f) :=
begin
apply (galois_connection f).to_galois_coinsertion,
rintros S Z g ⟨g₁, hf, hg₁⟩,
rw cancel_mono f at hf,
rwa ← hf,
end
/-- If `f` is a split epi, the pushforward-pullback adjunction on sieves is reflective. -/
def galois_insertion_of_split_epi (f : Y ⟶ X) [split_epi f] :
galois_insertion (sieve.pushforward f) (sieve.pullback f) :=
begin
apply (galois_connection f).to_galois_insertion,
intros S Z g hg,
refine ⟨g ≫ section_ f, by simpa⟩,
end
lemma pullback_arrows_comm [has_pullbacks C] {X Y : C} (f : Y ⟶ X)
(R : presieve X) :
sieve.generate (R.pullback_arrows f) = (sieve.generate R).pullback f :=
begin
ext Z g,
split,
{ rintro ⟨_, h, k, hk, rfl⟩,
cases hk with W g hg,
change (sieve.generate R).pullback f (h ≫ pullback.snd),
rw [sieve.pullback_apply, assoc, ← pullback.condition, ← assoc],
exact sieve.downward_closed _ (sieve.le_generate R W hg) (h ≫ pullback.fst)},
{ rintro ⟨W, h, k, hk, comm⟩,
exact ⟨_, _, _, presieve.pullback_arrows.mk _ _ hk, pullback.lift_snd _ _ comm⟩ },
end
section functor
variables {E : Type u₃} [category.{v₃} E] (G : D ⥤ E)
/--
If `R` is a sieve, then the `category_theory.presieve.functor_pullback` of `R` is actually a sieve.
-/
@[simps] def functor_pullback (R : sieve (F.obj X)) : sieve X :=
{ arrows := presieve.functor_pullback F R,
downward_closed' := λ _ _ f hf g,
begin
unfold presieve.functor_pullback,
rw F.map_comp,
exact R.downward_closed hf (F.map g),
end }
@[simp] lemma functor_pullback_arrows (R : sieve (F.obj X)) :
(R.functor_pullback F).arrows = R.arrows.functor_pullback F := rfl
@[simp] lemma functor_pullback_id (R : sieve X) : R.functor_pullback (𝟭 _) = R :=
by { ext, refl }
lemma functor_pullback_comp (R : sieve ((F ⋙ G).obj X)) :
R.functor_pullback (F ⋙ G) = (R.functor_pullback G).functor_pullback F := by { ext, refl }
lemma functor_pushforward_extend_eq {R : presieve X} :
(generate R).arrows.functor_pushforward F = R.functor_pushforward F :=
begin
ext Y f, split,
{ rintro ⟨X', g, f', ⟨X'', g', f'', h₁, rfl⟩, rfl⟩,
exact ⟨X'', f'', f' ≫ F.map g', h₁, by simp⟩ },
{ rintro ⟨X', g, f', h₁, h₂⟩, exact ⟨X', g, f', le_generate R _ h₁, h₂⟩ }
end
/-- The sieve generated by the image of `R` under `F`. -/
@[simps] def functor_pushforward (R : sieve X) : sieve (F.obj X) :=
{ arrows := R.arrows.functor_pushforward F,
downward_closed' := λ Y Z f h g, by
{ obtain ⟨X, α, β, hα, rfl⟩ := h,
exact ⟨X, α, g ≫ β, hα, by simp⟩ } }
@[simp] lemma functor_pushforward_id (R : sieve X) :
R.functor_pushforward (𝟭 _) = R :=
begin
ext X f,
split,
{ intro hf,
obtain ⟨X, g, h, hg, rfl⟩ := hf,
exact R.downward_closed hg h, },
{ intro hf,
exact ⟨X, f, 𝟙 _, hf, by simp⟩ }
end
lemma functor_pushforward_comp (R : sieve X) :
R.functor_pushforward (F ⋙ G) = (R.functor_pushforward F).functor_pushforward G :=
by { ext, simpa [R.arrows.functor_pushforward_comp F G] }
lemma functor_galois_connection (X : C) :
_root_.galois_connection
(sieve.functor_pushforward F : sieve X → sieve (F.obj X))
(sieve.functor_pullback F) :=
begin
intros R S,
split,
{ intros hle X f hf,
apply hle,
refine ⟨X, f, 𝟙 _, hf, _⟩,
rw id_comp, },
{ rintros hle Y f ⟨X, g, h, hg, rfl⟩,
apply sieve.downward_closed S,
exact hle g hg, }
end
lemma functor_pullback_monotone (X : C) :
monotone (sieve.functor_pullback F : sieve (F.obj X) → sieve X) :=
(functor_galois_connection F X).monotone_u
lemma functor_pushforward_monotone (X : C) :
monotone (sieve.functor_pushforward F : sieve X → sieve (F.obj X)) :=
(functor_galois_connection F X).monotone_l
lemma le_functor_pushforward_pullback (R : sieve X) :
R ≤ (R.functor_pushforward F).functor_pullback F :=
(functor_galois_connection F X).le_u_l _
lemma functor_pullback_pushforward_le (R : sieve (F.obj X)) :
(R.functor_pullback F).functor_pushforward F ≤ R :=
(functor_galois_connection F X).l_u_le _
lemma functor_pushforward_union (S R : sieve X) :
(S ⊔ R).functor_pushforward F = S.functor_pushforward F ⊔ R.functor_pushforward F :=
(functor_galois_connection F X).l_sup
lemma functor_pullback_union (S R : sieve (F.obj X)) :
(S ⊔ R).functor_pullback F = S.functor_pullback F ⊔ R.functor_pullback F := rfl
lemma functor_pullback_inter (S R : sieve (F.obj X)) :
(S ⊓ R).functor_pullback F = S.functor_pullback F ⊓ R.functor_pullback F := rfl
@[simp] lemma functor_pushforward_bot (F : C ⥤ D) (X : C) :
(⊥ : sieve X).functor_pushforward F = ⊥ := (functor_galois_connection F X).l_bot
@[simp] lemma functor_pushforward_top (F : C ⥤ D) (X : C) :
(⊤ : sieve X).functor_pushforward F = ⊤ :=
begin
refine (generate_sieve _).symm.trans _,
apply generate_of_contains_split_epi (𝟙 (F.obj X)),
refine ⟨X, 𝟙 _, 𝟙 _, trivial, by simp⟩
end
@[simp] lemma functor_pullback_bot (F : C ⥤ D) (X : C) :
(⊥ : sieve (F.obj X)).functor_pullback F = ⊥ := rfl
@[simp] lemma functor_pullback_top (F : C ⥤ D) (X : C) :
(⊤ : sieve (F.obj X)).functor_pullback F = ⊤ := rfl
lemma image_mem_functor_pushforward (R : sieve X) {V} {f : V ⟶ X} (h : R f) :
R.functor_pushforward F (F.map f) := ⟨V, f, 𝟙 _, h, by simp⟩
/-- When `F` is essentially surjective and full, the galois connection is a galois insertion. -/
def ess_surj_full_functor_galois_insertion [ess_surj F] [full F] (X : C) :
galois_insertion
(sieve.functor_pushforward F : sieve X → sieve (F.obj X))
(sieve.functor_pullback F) :=
begin
apply (functor_galois_connection F X).to_galois_insertion,
intros S Y f hf,
refine ⟨_, F.preimage ((F.obj_obj_preimage_iso Y).hom ≫ f), (F.obj_obj_preimage_iso Y).inv, _⟩,
simpa using S.downward_closed hf _,
end
/-- When `F` is fully faithful, the galois connection is a galois coinsertion. -/
def fully_faithful_functor_galois_coinsertion [full F] [faithful F] (X : C) :
galois_coinsertion
(sieve.functor_pushforward F : sieve X → sieve (F.obj X))
(sieve.functor_pullback F) :=
begin
apply (functor_galois_connection F X).to_galois_coinsertion,
rintros S Y f ⟨Z, g, h, h₁, h₂⟩,
rw [←F.image_preimage h, ←F.map_comp] at h₂,
rw F.map_injective h₂,
exact S.downward_closed h₁ _,
end
end functor
/-- A sieve induces a presheaf. -/
@[simps]
def functor (S : sieve X) : Cᵒᵖ ⥤ Type v₁ :=
{ obj := λ Y, {g : Y.unop ⟶ X // S g},
map := λ Y Z f g, ⟨f.unop ≫ g.1, downward_closed _ g.2 _⟩ }
/--
If a sieve S is contained in a sieve T, then we have a morphism of presheaves on their induced
presheaves.
-/
@[simps]
def nat_trans_of_le {S T : sieve X} (h : S ≤ T) : S.functor ⟶ T.functor :=
{ app := λ Y f, ⟨f.1, h _ f.2⟩ }.
/-- The natural inclusion from the functor induced by a sieve to the yoneda embedding. -/
@[simps]
def functor_inclusion (S : sieve X) : S.functor ⟶ yoneda.obj X :=
{ app := λ Y f, f.1 }.
lemma nat_trans_of_le_comm {S T : sieve X} (h : S ≤ T) :
nat_trans_of_le h ≫ functor_inclusion _ = functor_inclusion _ :=
rfl
/-- The presheaf induced by a sieve is a subobject of the yoneda embedding. -/
instance functor_inclusion_is_mono : mono S.functor_inclusion :=
⟨λ Z f g h, by { ext Y y, apply congr_fun (nat_trans.congr_app h Y) y }⟩
/--
A natural transformation to a representable functor induces a sieve. This is the left inverse of
`functor_inclusion`, shown in `sieve_of_functor_inclusion`.
-/
-- TODO: Show that when `f` is mono, this is right inverse to `functor_inclusion` up to isomorphism.
@[simps]
def sieve_of_subfunctor {R} (f : R ⟶ yoneda.obj X) : sieve X :=
{ arrows := λ Y g, ∃ t, f.app (opposite.op Y) t = g,
downward_closed' := λ Y Z _,
begin
rintro ⟨t, rfl⟩ g,
refine ⟨R.map g.op t, _⟩,
rw functor_to_types.naturality _ _ f,
simp,
end }
lemma sieve_of_subfunctor_functor_inclusion : sieve_of_subfunctor S.functor_inclusion = S :=
begin
ext,
simp only [functor_inclusion_app, sieve_of_subfunctor_apply, subtype.val_eq_coe],
split,
{ rintro ⟨⟨f, hf⟩, rfl⟩,
exact hf },
{ intro hf,
exact ⟨⟨_, hf⟩, rfl⟩ }
end
instance functor_inclusion_top_is_iso : is_iso ((⊤ : sieve X).functor_inclusion) :=
⟨⟨{ app := λ Y a, ⟨a, ⟨⟩⟩ }, by tidy⟩⟩
end sieve
end category_theory
|
a81ba5c79d3ae68363af84381f004ea626b9d7fd | a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91 | /tests/lean/run/elab6.lean | 305143e5448048b926940ef176c0b705cf06a00d | [
"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 | 148 | lean | constants a b : nat
constant p : nat → Prop
constant H1 : p (a + a + a)
constant H2 : a = b
check (eq.subst H2 H1 : p (a + b + a))
|
c25e11c2f962f383b1cbaf8efc11326887ab058a | 6dc0c8ce7a76229dd81e73ed4474f15f88a9e294 | /src/Init/System/ST.lean | ead573caee76013edd16c39b2579d8bafa9ed8b9 | [
"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 | 4,434 | lean | /-
Copyright (c) 2020 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
import Init.Classical
import Init.Control.EState
import Init.Control.Reader
def EST (ε : Type) (σ : Type) : Type → Type := EStateM ε σ
abbrev ST (σ : Type) := EST Empty σ
instance (ε σ : Type) : Monad (EST ε σ) := inferInstanceAs (Monad (EStateM _ _))
instance (ε σ : Type) : MonadExceptOf ε (EST ε σ) := inferInstanceAs (MonadExceptOf ε (EStateM _ _))
instance {ε σ : Type} {α : Type} [Inhabited ε] : Inhabited (EST ε σ α) := inferInstanceAs (Inhabited (EStateM _ _ _))
instance (σ : Type) : Monad (ST σ) := inferInstanceAs (Monad (EST _ _))
-- Auxiliary class for inferring the "state" of `EST` and `ST` monads
class STWorld (σ : outParam Type) (m : Type → Type)
instance {σ m n} [MonadLift m n] [STWorld σ m] : STWorld σ n := ⟨⟩
instance {ε σ} : STWorld σ (EST ε σ) := ⟨⟩
@[noinline, nospecialize]
def runEST {ε α : Type} (x : forall (σ : Type), EST ε σ α) : Except ε α :=
match x Unit () with
| EStateM.Result.ok a _ => Except.ok a
| EStateM.Result.error ex _ => Except.error ex
@[noinline, nospecialize]
def runST {α : Type} (x : forall (σ : Type), ST σ α) : α :=
match x Unit () with
| EStateM.Result.ok a _ => a
| EStateM.Result.error ex _ => nomatch ex
instance {ε σ} : MonadLift (ST σ) (EST ε σ) := ⟨fun x s =>
match x s with
| EStateM.Result.ok a s => EStateM.Result.ok a s
| EStateM.Result.error ex _ => nomatch ex⟩
namespace ST
/- References -/
constant RefPointed : PointedType.{0}
structure Ref (σ : Type) (α : Type) : Type where
ref : RefPointed.type
h : Nonempty α
instance {σ α} [Inhabited α] : Inhabited (Ref σ α) where
default := { ref := RefPointed.val, h := Nonempty.intro arbitrary }
namespace Prim
set_option pp.all true
/- Auxiliary definition for showing that `ST σ α` is inhabited when we have a `Ref σ α` -/
private noncomputable def inhabitedFromRef {σ α} (r : Ref σ α) : ST σ α :=
let inh : Inhabited α := Classical.inhabitedOfNonempty r.h
pure arbitrary
@[extern "lean_st_mk_ref"]
constant mkRef {σ α} (a : α) : ST σ (Ref σ α) := pure { ref := RefPointed.val, h := Nonempty.intro a }
@[extern "lean_st_ref_get"]
constant Ref.get {σ α} (r : @& Ref σ α) : ST σ α := inhabitedFromRef r
@[extern "lean_st_ref_set"]
constant Ref.set {σ α} (r : @& Ref σ α) (a : α) : ST σ Unit
@[extern "lean_st_ref_swap"]
constant Ref.swap {σ α} (r : @& Ref σ α) (a : α) : ST σ α := inhabitedFromRef r
@[extern "lean_st_ref_take"]
unsafe constant Ref.take {σ α} (r : @& Ref σ α) : ST σ α := inhabitedFromRef r
@[extern "lean_st_ref_ptr_eq"]
constant Ref.ptrEq {σ α} (r1 r2 : @& Ref σ α) : ST σ Bool
@[inline] unsafe def Ref.modifyUnsafe {σ α : Type} (r : Ref σ α) (f : α → α) : ST σ Unit := do
let v ← Ref.take r
Ref.set r (f v)
@[inline] unsafe def Ref.modifyGetUnsafe {σ α β : Type} (r : Ref σ α) (f : α → β × α) : ST σ β := do
let v ← Ref.take r
let (b, a) := f v
Ref.set r a
pure b
@[implementedBy Ref.modifyUnsafe]
def Ref.modify {σ α : Type} (r : Ref σ α) (f : α → α) : ST σ Unit := do
let v ← Ref.get r
Ref.set r (f v)
@[implementedBy Ref.modifyGetUnsafe]
def Ref.modifyGet {σ α β : Type} (r : Ref σ α) (f : α → β × α) : ST σ β := do
let v ← Ref.get r
let (b, a) := f v
Ref.set r a
pure b
end Prim
section
variable {σ : Type} {m : Type → Type} [Monad m] [MonadLiftT (ST σ) m]
@[inline] def mkRef {α : Type} (a : α) : m (Ref σ α) := liftM <| Prim.mkRef a
@[inline] def Ref.get {α : Type} (r : Ref σ α) : m α := liftM <| Prim.Ref.get r
@[inline] def Ref.set {α : Type} (r : Ref σ α) (a : α) : m Unit := liftM <| Prim.Ref.set r a
@[inline] def Ref.swap {α : Type} (r : Ref σ α) (a : α) : m α := liftM <| Prim.Ref.swap r a
@[inline] unsafe def Ref.take {α : Type} (r : Ref σ α) : m α := liftM <| Prim.Ref.take r
@[inline] def Ref.ptrEq {α : Type} (r1 r2 : Ref σ α) : m Bool := liftM <| Prim.Ref.ptrEq r1 r2
@[inline] def Ref.modify {α : Type} (r : Ref σ α) (f : α → α) : m Unit := liftM <| Prim.Ref.modify r f
@[inline] def Ref.modifyGet {α : Type} {β : Type} (r : Ref σ α) (f : α → β × α) : m β := liftM <| Prim.Ref.modifyGet r f
end
end ST
|
b861e80fe20078f185103b3b57dea1062f776492 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/algebra/group/type_tags.lean | 46f64cf757500c763927cd3fa6518b6ad36df036 | [
"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 | 15,123 | 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 algebra.hom.group
import logic.equiv.defs
import data.finite.defs
/-!
# Type tags that turn additive structures into multiplicative, and vice versa
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
We define two type tags:
* `additive α`: turns any multiplicative structure on `α` into the corresponding
additive structure on `additive α`;
* `multiplicative α`: turns any additive structure on `α` into the corresponding
multiplicative structure on `multiplicative α`.
We also define instances `additive.*` and `multiplicative.*` that actually transfer the structures.
## See also
This file is similar to `order.synonym`.
-/
universes u v
variables {α : Type u} {β : Type v}
/-- If `α` carries some multiplicative structure, then `additive α` carries the corresponding
additive structure. -/
def additive (α : Type*) := α
/-- If `α` carries some additive structure, then `multiplicative α` carries the corresponding
multiplicative structure. -/
def multiplicative (α : Type*) := α
namespace additive
/-- Reinterpret `x : α` as an element of `additive α`. -/
def of_mul : α ≃ additive α := ⟨λ x, x, λ x, x, λ x, rfl, λ x, rfl⟩
/-- Reinterpret `x : additive α` as an element of `α`. -/
def to_mul : additive α ≃ α := of_mul.symm
@[simp] lemma of_mul_symm_eq : (@of_mul α).symm = to_mul := rfl
@[simp] lemma to_mul_symm_eq : (@to_mul α).symm = of_mul := rfl
end additive
namespace multiplicative
/-- Reinterpret `x : α` as an element of `multiplicative α`. -/
def of_add : α ≃ multiplicative α := ⟨λ x, x, λ x, x, λ x, rfl, λ x, rfl⟩
/-- Reinterpret `x : multiplicative α` as an element of `α`. -/
def to_add : multiplicative α ≃ α := of_add.symm
@[simp] lemma of_add_symm_eq : (@of_add α).symm = to_add := rfl
@[simp] lemma to_add_symm_eq : (@to_add α).symm = of_add := rfl
end multiplicative
@[simp] lemma to_add_of_add (x : α) : (multiplicative.of_add x).to_add = x := rfl
@[simp] lemma of_add_to_add (x : multiplicative α) : multiplicative.of_add x.to_add = x := rfl
@[simp] lemma to_mul_of_mul (x : α) : (additive.of_mul x).to_mul = x := rfl
@[simp] lemma of_mul_to_mul (x : additive α) : additive.of_mul x.to_mul = x := rfl
instance [inhabited α] : inhabited (additive α) := ⟨additive.of_mul default⟩
instance [inhabited α] : inhabited (multiplicative α) := ⟨multiplicative.of_add default⟩
instance [finite α] : finite (additive α) := finite.of_equiv α (by refl)
instance [finite α] : finite (multiplicative α) := finite.of_equiv α (by refl)
instance [infinite α] : infinite (additive α) := by tauto
instance [infinite α] : infinite (multiplicative α) := by tauto
instance [nontrivial α] : nontrivial (additive α) :=
additive.of_mul.injective.nontrivial
instance [nontrivial α] : nontrivial (multiplicative α) :=
multiplicative.of_add.injective.nontrivial
instance additive.has_add [has_mul α] : has_add (additive α) :=
{ add := λ x y, additive.of_mul (x.to_mul * y.to_mul) }
instance [has_add α] : has_mul (multiplicative α) :=
{ mul := λ x y, multiplicative.of_add (x.to_add + y.to_add) }
@[simp] lemma of_add_add [has_add α] (x y : α) :
multiplicative.of_add (x + y) = multiplicative.of_add x * multiplicative.of_add y :=
rfl
@[simp] lemma to_add_mul [has_add α] (x y : multiplicative α) :
(x * y).to_add = x.to_add + y.to_add :=
rfl
@[simp] lemma of_mul_mul [has_mul α] (x y : α) :
additive.of_mul (x * y) = additive.of_mul x + additive.of_mul y :=
rfl
@[simp] lemma to_mul_add [has_mul α] (x y : additive α) :
(x + y).to_mul = x.to_mul * y.to_mul :=
rfl
instance [semigroup α] : add_semigroup (additive α) :=
{ add_assoc := @mul_assoc α _,
..additive.has_add }
instance [add_semigroup α] : semigroup (multiplicative α) :=
{ mul_assoc := @add_assoc α _,
..multiplicative.has_mul }
instance [comm_semigroup α] : add_comm_semigroup (additive α) :=
{ add_comm := @mul_comm _ _,
..additive.add_semigroup }
instance [add_comm_semigroup α] : comm_semigroup (multiplicative α) :=
{ mul_comm := @add_comm _ _,
..multiplicative.semigroup }
instance [has_mul α] [is_left_cancel_mul α] : is_left_cancel_add (additive α) :=
{ add_left_cancel := @mul_left_cancel α _ _ }
instance [has_add α] [is_left_cancel_add α] : is_left_cancel_mul (multiplicative α) :=
{ mul_left_cancel := @add_left_cancel α _ _ }
instance [has_mul α] [is_right_cancel_mul α] : is_right_cancel_add (additive α) :=
{ add_right_cancel := @mul_right_cancel α _ _ }
instance [has_add α] [is_right_cancel_add α] : is_right_cancel_mul (multiplicative α) :=
{ mul_right_cancel := @add_right_cancel α _ _ }
instance [has_mul α] [is_cancel_mul α] : is_cancel_add (additive α) :=
{ ..additive.is_left_cancel_add, ..additive.is_right_cancel_add }
instance [has_add α] [is_cancel_add α] : is_cancel_mul (multiplicative α) :=
{ ..multiplicative.is_left_cancel_mul, ..multiplicative.is_right_cancel_mul }
instance [left_cancel_semigroup α] : add_left_cancel_semigroup (additive α) :=
{ ..additive.add_semigroup, ..additive.is_left_cancel_add }
instance [add_left_cancel_semigroup α] : left_cancel_semigroup (multiplicative α) :=
{ ..multiplicative.semigroup, ..multiplicative.is_left_cancel_mul }
instance [right_cancel_semigroup α] : add_right_cancel_semigroup (additive α) :=
{ ..additive.add_semigroup, ..additive.is_right_cancel_add }
instance [add_right_cancel_semigroup α] : right_cancel_semigroup (multiplicative α) :=
{ ..multiplicative.semigroup, ..multiplicative.is_right_cancel_mul }
instance [has_one α] : has_zero (additive α) := ⟨additive.of_mul 1⟩
@[simp] lemma of_mul_one [has_one α] : @additive.of_mul α 1 = 0 := rfl
@[simp] lemma of_mul_eq_zero {A : Type*} [has_one A] {x : A} :
additive.of_mul x = 0 ↔ x = 1 := iff.rfl
@[simp] lemma to_mul_zero [has_one α] : (0 : additive α).to_mul = 1 := rfl
instance [has_zero α] : has_one (multiplicative α) := ⟨multiplicative.of_add 0⟩
@[simp] lemma of_add_zero [has_zero α] : @multiplicative.of_add α 0 = 1 := rfl
@[simp] lemma of_add_eq_one {A : Type*} [has_zero A] {x : A} :
multiplicative.of_add x = 1 ↔ x = 0 := iff.rfl
@[simp] lemma to_add_one [has_zero α] : (1 : multiplicative α).to_add = 0 := rfl
instance [mul_one_class α] : add_zero_class (additive α) :=
{ zero := 0,
add := (+),
zero_add := one_mul,
add_zero := mul_one }
instance [add_zero_class α] : mul_one_class (multiplicative α) :=
{ one := 1,
mul := (*),
one_mul := zero_add,
mul_one := add_zero }
instance [h : monoid α] : add_monoid (additive α) :=
{ zero := 0,
add := (+),
nsmul := @monoid.npow α h,
nsmul_zero' := monoid.npow_zero',
nsmul_succ' := monoid.npow_succ',
..additive.add_zero_class,
..additive.add_semigroup }
instance [h : add_monoid α] : monoid (multiplicative α) :=
{ one := 1,
mul := (*),
npow := @add_monoid.nsmul α h,
npow_zero' := add_monoid.nsmul_zero',
npow_succ' := add_monoid.nsmul_succ',
..multiplicative.mul_one_class,
..multiplicative.semigroup }
instance [left_cancel_monoid α] : add_left_cancel_monoid (additive α) :=
{ zero := 0, add := (+), .. additive.add_monoid, .. additive.add_left_cancel_semigroup }
instance [add_left_cancel_monoid α] : left_cancel_monoid (multiplicative α) :=
{ one := 1, mul := (*), .. multiplicative.monoid, .. multiplicative.left_cancel_semigroup }
instance [right_cancel_monoid α] : add_right_cancel_monoid (additive α) :=
{ zero := 0, add := (+), .. additive.add_monoid, .. additive.add_right_cancel_semigroup }
instance [add_right_cancel_monoid α] : right_cancel_monoid (multiplicative α) :=
{ one := 1, mul := (*), .. multiplicative.monoid, .. multiplicative.right_cancel_semigroup }
instance [comm_monoid α] : add_comm_monoid (additive α) :=
{ zero := 0, add := (+), .. additive.add_monoid, .. additive.add_comm_semigroup }
instance [add_comm_monoid α] : comm_monoid (multiplicative α) :=
{ one := 1, mul := (*), ..multiplicative.monoid, .. multiplicative.comm_semigroup }
instance [has_inv α] : has_neg (additive α) := ⟨λ x, multiplicative.of_add x.to_mul⁻¹⟩
@[simp] lemma of_mul_inv [has_inv α] (x : α) : additive.of_mul x⁻¹ = -(additive.of_mul x) := rfl
@[simp] lemma to_mul_neg [has_inv α] (x : additive α) : (-x).to_mul = x.to_mul⁻¹ := rfl
instance [has_neg α] : has_inv (multiplicative α) := ⟨λ x, additive.of_mul (-x.to_add)⟩
@[simp] lemma of_add_neg [has_neg α] (x : α) :
multiplicative.of_add (-x) = (multiplicative.of_add x)⁻¹ := rfl
@[simp] lemma to_add_inv [has_neg α] (x : multiplicative α) :
(x⁻¹).to_add = -x.to_add := rfl
instance additive.has_sub [has_div α] : has_sub (additive α) :=
{ sub := λ x y, additive.of_mul (x.to_mul / y.to_mul) }
instance multiplicative.has_div [has_sub α] : has_div (multiplicative α) :=
{ div := λ x y, multiplicative.of_add (x.to_add - y.to_add) }
@[simp] lemma of_add_sub [has_sub α] (x y : α) :
multiplicative.of_add (x - y) = multiplicative.of_add x / multiplicative.of_add y :=
rfl
@[simp] lemma to_add_div [has_sub α] (x y : multiplicative α) :
(x / y).to_add = x.to_add - y.to_add :=
rfl
@[simp] lemma of_mul_div [has_div α] (x y : α) :
additive.of_mul (x / y) = additive.of_mul x - additive.of_mul y :=
rfl
@[simp] lemma to_mul_sub [has_div α] (x y : additive α) :
(x - y).to_mul = x.to_mul / y.to_mul :=
rfl
instance [has_involutive_inv α] : has_involutive_neg (additive α) :=
{ neg_neg := @inv_inv _ _,
..additive.has_neg }
instance [has_involutive_neg α] : has_involutive_inv (multiplicative α) :=
{ inv_inv := @neg_neg _ _,
..multiplicative.has_inv }
instance [div_inv_monoid α] : sub_neg_monoid (additive α) :=
{ sub_eq_add_neg := @div_eq_mul_inv α _,
zsmul := @div_inv_monoid.zpow α _,
zsmul_zero' := div_inv_monoid.zpow_zero',
zsmul_succ' := div_inv_monoid.zpow_succ',
zsmul_neg' := div_inv_monoid.zpow_neg',
.. additive.has_neg, .. additive.has_sub, .. additive.add_monoid }
instance [sub_neg_monoid α] : div_inv_monoid (multiplicative α) :=
{ div_eq_mul_inv := @sub_eq_add_neg α _,
zpow := @sub_neg_monoid.zsmul α _,
zpow_zero' := sub_neg_monoid.zsmul_zero',
zpow_succ' := sub_neg_monoid.zsmul_succ',
zpow_neg' := sub_neg_monoid.zsmul_neg',
.. multiplicative.has_inv, .. multiplicative.has_div, .. multiplicative.monoid }
instance [division_monoid α] : subtraction_monoid (additive α) :=
{ neg_add_rev := @mul_inv_rev _ _,
neg_eq_of_add := @inv_eq_of_mul_eq_one_right _ _,
.. additive.sub_neg_monoid, .. additive.has_involutive_neg }
instance [subtraction_monoid α] : division_monoid (multiplicative α) :=
{ mul_inv_rev := @neg_add_rev _ _,
inv_eq_of_mul := @neg_eq_of_add_eq_zero_right _ _,
.. multiplicative.div_inv_monoid, .. multiplicative.has_involutive_inv }
instance [division_comm_monoid α] : subtraction_comm_monoid (additive α) :=
{ .. additive.subtraction_monoid, .. additive.add_comm_semigroup }
instance [subtraction_comm_monoid α] : division_comm_monoid (multiplicative α) :=
{ .. multiplicative.division_monoid, .. multiplicative.comm_semigroup }
instance [group α] : add_group (additive α) :=
{ add_left_neg := @mul_left_inv α _,
.. additive.sub_neg_monoid }
instance [add_group α] : group (multiplicative α) :=
{ mul_left_inv := @add_left_neg α _,
.. multiplicative.div_inv_monoid }
instance [comm_group α] : add_comm_group (additive α) :=
{ .. additive.add_group, .. additive.add_comm_monoid }
instance [add_comm_group α] : comm_group (multiplicative α) :=
{ .. multiplicative.group, .. multiplicative.comm_monoid }
open multiplicative (of_add)
open additive (of_mul)
/-- Reinterpret `α →+ β` as `multiplicative α →* multiplicative β`. -/
@[simps] def add_monoid_hom.to_multiplicative [add_zero_class α] [add_zero_class β] :
(α →+ β) ≃ (multiplicative α →* multiplicative β) :=
{ to_fun := λ f, ⟨λ a, of_add (f a.to_add), f.2, f.3⟩,
inv_fun := λ f, ⟨λ a, (f (of_add a)).to_add, f.2, f.3⟩,
left_inv := λ x, by { ext, refl, },
right_inv := λ x, by { ext, refl, } }
/-- Reinterpret `α →* β` as `additive α →+ additive β`. -/
@[simps] def monoid_hom.to_additive [mul_one_class α] [mul_one_class β] :
(α →* β) ≃ (additive α →+ additive β) :=
{ to_fun := λ f, ⟨λ a, of_mul (f a.to_mul), f.2, f.3⟩,
inv_fun := λ f, ⟨λ a, (f (of_mul a)).to_mul, f.2, f.3⟩,
left_inv := λ x, by { ext, refl, },
right_inv := λ x, by { ext, refl, } }
/-- Reinterpret `additive α →+ β` as `α →* multiplicative β`. -/
@[simps] def add_monoid_hom.to_multiplicative' [mul_one_class α] [add_zero_class β] :
(additive α →+ β) ≃ (α →* multiplicative β) :=
{ to_fun := λ f, ⟨λ a, of_add (f (of_mul a)), f.2, f.3⟩,
inv_fun := λ f, ⟨λ a, (f a.to_mul).to_add, f.2, f.3⟩,
left_inv := λ x, by { ext, refl, },
right_inv := λ x, by { ext, refl, } }
/-- Reinterpret `α →* multiplicative β` as `additive α →+ β`. -/
@[simps] def monoid_hom.to_additive' [mul_one_class α] [add_zero_class β] :
(α →* multiplicative β) ≃ (additive α →+ β) :=
add_monoid_hom.to_multiplicative'.symm
/-- Reinterpret `α →+ additive β` as `multiplicative α →* β`. -/
@[simps] def add_monoid_hom.to_multiplicative'' [add_zero_class α] [mul_one_class β] :
(α →+ additive β) ≃ (multiplicative α →* β) :=
{ to_fun := λ f, ⟨λ a, (f a.to_add).to_mul, f.2, f.3⟩,
inv_fun := λ f, ⟨λ a, of_mul (f (of_add a)), f.2, f.3⟩,
left_inv := λ x, by { ext, refl, },
right_inv := λ x, by { ext, refl, } }
/-- Reinterpret `multiplicative α →* β` as `α →+ additive β`. -/
@[simps] def monoid_hom.to_additive'' [add_zero_class α] [mul_one_class β] :
(multiplicative α →* β) ≃ (α →+ additive β) :=
add_monoid_hom.to_multiplicative''.symm
/-- If `α` has some multiplicative structure and coerces to a function,
then `additive α` should also coerce to the same function.
This allows `additive` to be used on bundled function types with a multiplicative structure, which
is often used for composition, without affecting the behavior of the function itself.
-/
instance additive.has_coe_to_fun {α : Type*} {β : α → Sort*} [has_coe_to_fun α β] :
has_coe_to_fun (additive α) (λ a, β a.to_mul) :=
⟨λ a, coe_fn a.to_mul⟩
/-- If `α` has some additive structure and coerces to a function,
then `multiplicative α` should also coerce to the same function.
This allows `multiplicative` to be used on bundled function types with an additive structure, which
is often used for composition, without affecting the behavior of the function itself.
-/
instance multiplicative.has_coe_to_fun {α : Type*} {β : α → Sort*} [has_coe_to_fun α β] :
has_coe_to_fun (multiplicative α) (λ a, β a.to_add) :=
⟨λ a, coe_fn a.to_add⟩
|
021671b4cb0cb2a264e0d760763a28e60a15fc47 | cf39355caa609c0f33405126beee2739aa3cb77e | /tests/lean/run/sizeof2.lean | d5e9ee402123e04917891c4db35fc854b3bd1907 | [
"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 | 438 | lean | open tactic
example : sizeof (λ a : nat, a) = 0 :=
rfl
example : sizeof Type = 0 :=
rfl
example : sizeof Prop = 0 :=
rfl
example (p : Prop) (H : p) : sizeof H = 0 :=
rfl
example (A : Type) (a b : A) : sizeof [a, b] = 3 :=
rfl
example : sizeof [(1:nat), 4] = 8 :=
rfl
example : sizeof [tt, ff, tt] = 7 :=
rfl
set_option pp.implicit true
#check sizeof Prop
#check sizeof [tt, ff, tt]
#check λ (A : Type) (a b : A), sizeof [a, b]
|
184020025acea61c987f553c47837cff0d1842ee | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/algebra/category/Ring/adjunctions.lean | 166a5e3c14e9786744ef504ce006bfbdd190313f | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 1,733 | lean | /-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Johannes Hölzl
-/
import algebra.category.Ring.basic
import data.mv_polynomial.comm_ring
/-!
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
Multivariable polynomials on a type is the left adjoint of the
forgetful functor from commutative rings to types.
-/
noncomputable theory
universe u
open mv_polynomial
open category_theory
namespace CommRing
open_locale classical
/--
The free functor `Type u ⥤ CommRing` sending a type `X` to the multivariable (commutative)
polynomials with variables `x : X`.
-/
def free : Type u ⥤ CommRing.{u} :=
{ obj := λ α, of (mv_polynomial α ℤ),
map := λ X Y f,
(↑(rename f : _ →ₐ[ℤ] _) : (mv_polynomial X ℤ →+* mv_polynomial Y ℤ)),
-- TODO these next two fields can be done by `tidy`, but the calls in `dsimp` and `simp` it
-- generates are too slow.
map_id' := λ X, ring_hom.ext $ rename_id,
map_comp' := λ X Y Z f g, ring_hom.ext $ λ p, (rename_rename f g p).symm }
@[simp] lemma free_obj_coe {α : Type u} :
(free.obj α : Type u) = mv_polynomial α ℤ := rfl
@[simp] lemma free_map_coe {α β : Type u} {f : α → β} :
⇑(free.map f) = rename f := rfl
/--
The free-forgetful adjunction for commutative rings.
-/
def adj : free ⊣ forget CommRing.{u} :=
adjunction.mk_of_hom_equiv
{ hom_equiv := λ X R, hom_equiv,
hom_equiv_naturality_left_symm' :=
λ _ _ Y f g, ring_hom.ext $ λ x, eval₂_cast_comp f (int.cast_ring_hom Y) g x }
instance : is_right_adjoint (forget CommRing.{u}) := ⟨_, adj⟩
end CommRing
|
c94fad2d8f84a0ebce055d0717553510c1480fb8 | 0e175f34f8dca5ea099671777e8d7446d7d74227 | /library/init/meta/widget/html_cmd.lean | dbe7d6faca3f2a9902adbe3926f75d962459a804 | [
"Apache-2.0"
] | permissive | utensil-contrib/lean | b31266738071c654d96dac8b35d9ccffc8172fda | a28b9c8f78d982a4e82b1e4f7ce7988d87183ae8 | refs/heads/master | 1,670,045,564,075 | 1,597,397,599,000 | 1,597,397,599,000 | 287,528,503 | 0 | 0 | Apache-2.0 | 1,597,408,338,000 | 1,597,408,337,000 | null | UTF-8 | Lean | false | false | 999 | lean | /-
Copyright (c) E.W.Ayers. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: E.W.Ayers
-/
prelude
import init.meta.widget.basic
import init.meta.lean.parser
import init.meta.interactive_base
import init.data.punit
open lean
open lean.parser
open interactive
open tactic
open widget
/-- Accepts terms with the type `component tactic_state empty` or `html empty` and
renders them interactively. -/
@[user_command]
meta def show_widget_cmd (x : parse $ tk "#html") : parser unit := do
⟨l,c⟩ ← cur_pos,
y ← parser.pexpr,
comp ← parser.of_tactic ((do
tactic.eval_pexpr (component tactic_state empty) y
) <|> (do
htm : html empty ← tactic.eval_pexpr (html empty) y,
c : component unit empty ← pure $ component.stateless (λ _, [htm]),
pure $ component.ignore_props $ component.ignore_action $ c
)),
save_widget ⟨l,c - ("#html").length - 1⟩ comp,
trace "successfully rendered widget"
pure ()
run_cmd skip
|
6c1a9ed4dcc4c7a7c16153518dfb2b9173eb78c2 | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/data/nat/pairing.lean | 0923cc48e740032e4356f4825d5e10d6792fdb4f | [
"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 | 6,402 | 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.nat.sqrt
import data.set.lattice
/-!
# Naturals pairing function
This file defines a pairing function for the naturals as follows:
```text
0 1 4 9 16
2 3 5 10 17
6 7 8 11 18
12 13 14 15 19
20 21 22 23 24
```
It has the advantage of being monotone in both directions and sending `⟦0, n^2 - 1⟧` to
`⟦0, n - 1⟧²`.
-/
open prod decidable function
namespace nat
/-- Pairing function for the natural numbers. -/
@[pp_nodot] def mkpair (a b : ℕ) : ℕ :=
if a < b then b*b + a else a*a + a + b
/-- Unpairing function for the natural numbers. -/
@[pp_nodot] def unpair (n : ℕ) : ℕ × ℕ :=
let s := sqrt n in
if n - s*s < s then (n - s*s, s) else (s, n - s*s - s)
@[simp] theorem mkpair_unpair (n : ℕ) : mkpair (unpair n).1 (unpair n).2 = n :=
begin
dsimp only [unpair], set s := sqrt n,
have sm : s * s + (n - s * s) = n := add_tsub_cancel_of_le (sqrt_le _),
split_ifs,
{ simp [mkpair, h, sm] },
{ have hl : n - s*s - s ≤ s :=
tsub_le_iff_left.mpr (tsub_le_iff_left.mpr $
by rw ← add_assoc; apply sqrt_le_add),
simp [mkpair, hl.not_lt, add_assoc, add_tsub_cancel_of_le (le_of_not_gt h), sm] }
end
theorem mkpair_unpair' {n a b} (H : unpair n = (a, b)) : mkpair a b = n :=
by simpa [H] using mkpair_unpair n
@[simp] theorem unpair_mkpair (a b : ℕ) : unpair (mkpair a b) = (a, b) :=
begin
dunfold mkpair, split_ifs,
{ show unpair (b * b + a) = (a, b),
have be : sqrt (b * b + a) = b,
from sqrt_add_eq _ (le_trans (le_of_lt h) (nat.le_add_left _ _)),
simp [unpair, be, add_tsub_cancel_right, h] },
{ show unpair (a * a + a + b) = (a, b),
have ae : sqrt (a * a + (a + b)) = a,
{ rw sqrt_add_eq, exact add_le_add_left (le_of_not_gt h) _ },
simp [unpair, ae, nat.not_lt_zero, add_assoc] }
end
/-- An equivalence between `ℕ × ℕ` and `ℕ`. -/
@[simps { fully_applied := ff }] def mkpair_equiv : ℕ × ℕ ≃ ℕ :=
⟨uncurry mkpair, unpair, λ ⟨a, b⟩, unpair_mkpair a b, mkpair_unpair⟩
lemma surjective_unpair : surjective unpair :=
mkpair_equiv.symm.surjective
@[simp] lemma mkpair_eq_mkpair {a b c d : ℕ} : mkpair a b = mkpair c d ↔ a = c ∧ b = d :=
mkpair_equiv.injective.eq_iff.trans (@prod.ext_iff ℕ ℕ (a, b) (c, d))
theorem unpair_lt {n : ℕ} (n1 : 1 ≤ n) : (unpair n).1 < n :=
let s := sqrt n in begin
simp [unpair], change sqrt n with s,
by_cases h : n - s * s < s; simp [h],
{ exact lt_of_lt_of_le h (sqrt_le_self _) },
{ simp at h,
have s0 : 0 < s := sqrt_pos.2 n1,
exact lt_of_le_of_lt h (tsub_lt_self n1 (mul_pos s0 s0)) }
end
@[simp] lemma unpair_zero : unpair 0 = 0 :=
by { rw unpair, simp }
theorem unpair_left_le : ∀ (n : ℕ), (unpair n).1 ≤ n
| 0 := by simp
| (n+1) := le_of_lt (unpair_lt (nat.succ_pos _))
theorem left_le_mkpair (a b : ℕ) : a ≤ mkpair a b :=
by simpa using unpair_left_le (mkpair a b)
theorem right_le_mkpair (a b : ℕ) : b ≤ mkpair a b :=
begin
by_cases h : a < b; simp [mkpair, h],
exact le_trans (le_mul_self _) (nat.le_add_right _ _)
end
theorem unpair_right_le (n : ℕ) : (unpair n).2 ≤ n :=
by simpa using right_le_mkpair n.unpair.1 n.unpair.2
theorem mkpair_lt_mkpair_left {a₁ a₂} (b) (h : a₁ < a₂) : mkpair a₁ b < mkpair a₂ b :=
begin
by_cases h₁ : a₁ < b; simp [mkpair, h₁, add_assoc],
{ by_cases h₂ : a₂ < b; simp [mkpair, h₂, h],
simp at h₂,
apply add_lt_add_of_le_of_lt,
exact mul_self_le_mul_self h₂,
exact lt_add_right _ _ _ h },
{ simp at h₁,
simp [not_lt_of_gt (lt_of_le_of_lt h₁ h)],
apply add_lt_add,
exact mul_self_lt_mul_self h,
apply add_lt_add_right; assumption }
end
theorem mkpair_lt_mkpair_right (a) {b₁ b₂} (h : b₁ < b₂) : mkpair a b₁ < mkpair a b₂ :=
begin
by_cases h₁ : a < b₁; simp [mkpair, h₁, add_assoc],
{ simp [mkpair, lt_trans h₁ h, h],
exact mul_self_lt_mul_self h },
{ by_cases h₂ : a < b₂; simp [mkpair, h₂, h],
simp at h₁,
rw [add_comm, add_comm _ a, add_assoc, add_lt_add_iff_left],
rwa [add_comm, ← sqrt_lt, sqrt_add_eq],
exact le_trans h₁ (nat.le_add_left _ _) }
end
theorem mkpair_lt_max_add_one_sq (m n : ℕ) : mkpair m n < (max m n + 1) ^ 2 :=
begin
rw [mkpair, add_sq, mul_one, two_mul, sq, add_assoc, add_assoc],
cases lt_or_le m n,
{ rw [if_pos h, max_eq_right h.le, add_lt_add_iff_left, add_assoc],
exact h.trans_le (self_le_add_right n _) },
{ rw [if_neg h.not_lt, max_eq_left h, add_lt_add_iff_left, add_assoc, add_lt_add_iff_left],
exact lt_succ_of_le h }
end
theorem max_sq_add_min_le_mkpair (m n : ℕ) : max m n ^ 2 + min m n ≤ mkpair m n :=
begin
rw mkpair,
cases lt_or_le m n,
{ rw [if_pos h, max_eq_right h.le, min_eq_left h.le, sq], },
{ rw [if_neg h.not_lt, max_eq_left h, min_eq_right h, sq, add_assoc, add_le_add_iff_left],
exact le_add_self }
end
theorem add_le_mkpair (m n : ℕ) : m + n ≤ mkpair m n :=
(max_sq_add_min_le_mkpair _ _).trans' $
by { rw [sq, ←min_add_max, add_comm, add_le_add_iff_right], exact le_mul_self _ }
theorem unpair_add_le (n : ℕ) : (unpair n).1 + (unpair n).2 ≤ n :=
(add_le_mkpair _ _).trans_eq (mkpair_unpair _)
end nat
open nat
section complete_lattice
lemma supr_unpair {α} [complete_lattice α] (f : ℕ → ℕ → α) :
(⨆ n : ℕ, f n.unpair.1 n.unpair.2) = ⨆ i j : ℕ, f i j :=
by rw [← (supr_prod : (⨆ i : ℕ × ℕ, f i.1 i.2) = _), ← nat.surjective_unpair.supr_comp]
lemma infi_unpair {α} [complete_lattice α] (f : ℕ → ℕ → α) :
(⨅ n : ℕ, f n.unpair.1 n.unpair.2) = ⨅ i j : ℕ, f i j :=
supr_unpair (show ℕ → ℕ → αᵒᵈ, from f)
end complete_lattice
namespace set
lemma Union_unpair_prod {α β} {s : ℕ → set α} {t : ℕ → set β} :
(⋃ n : ℕ, s n.unpair.fst ×ˢ t n.unpair.snd) = (⋃ n, s n) ×ˢ (⋃ n, t n) :=
by { rw [← Union_prod], convert surjective_unpair.Union_comp _, refl }
lemma Union_unpair {α} (f : ℕ → ℕ → set α) :
(⋃ n : ℕ, f n.unpair.1 n.unpair.2) = ⋃ i j : ℕ, f i j :=
supr_unpair f
lemma Inter_unpair {α} (f : ℕ → ℕ → set α) :
(⋂ n : ℕ, f n.unpair.1 n.unpair.2) = ⋂ i j : ℕ, f i j :=
infi_unpair f
end set
|
94eeed88225f75775ab165c5b4a7d6907be0b4b7 | ec62863c729b7eedee77b86d974f2c529fa79d25 | /12/a.lean | 2150df1272c14894f5825a711f66a3a969e2ecbd | [] | no_license | rwbarton/advent-of-lean-4 | 2ac9b17ba708f66051e3d8cd694b0249bc433b65 | 417c7e2718253ba7148c0279fcb251b6fc291477 | refs/heads/main | 1,675,917,092,057 | 1,609,864,581,000 | 1,609,864,581,000 | 317,700,289 | 24 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,186 | lean | structure Ship :=
x : Int
y : Int
dx : Int
dy : Int
instance : Inhabited Ship := ⟨⟨0,0,1,0⟩⟩
def fakeCos (n : Int) : Int :=
match (((n / 90) % 4 + 4) % 4 : Int) with
| 0 => 1
| 1 => 0
| 2 => -1
| 3 => 0
| _ => panic! "impossible"
def fakeSin (n : Int) : Int := fakeCos (90 - n)
def move (s : Ship) : Char → Int → Ship
| 'N', n => { s with y := s.y + n }
| 'S', n => { s with y := s.y - n }
| 'E', n => { s with x := s.x + n }
| 'W', n => { s with x := s.x - n }
| 'L', n => { s with dx := fakeCos n * s.dx - fakeSin n * s.dy, dy := fakeSin n * s.dx + fakeCos n * s.dy }
| 'R', n => { s with dx := fakeCos n * s.dx + fakeSin n * s.dy, dy := - fakeSin n * s.dx + fakeCos n * s.dy }
| 'F', n => { s with x := s.x + n * s.dx, y := s.y + n * s.dy }
| _, _ => panic! "invalid command"
def moveStr (s : Ship) (str : String) : Ship :=
match str.toList with
| c :: rest => move s c (String.mk rest).toNat!
| _ => panic! "invalid command"
def Int.abs (n : Int) : Int :=
if n < 0 then -n else n
def main : IO Unit := do
let instrs ← IO.FS.lines "a.in"
let result : Ship := instrs.foldl moveStr { x := 0, y := 0, dx := 1, dy := 0 }
IO.print s!"{result.x.abs + result.y.abs}\n"
|
e8026cf2eb99b8c86c53dccad736971eab290c6f | 4727251e0cd73359b15b664c3170e5d754078599 | /src/data/pnat/factors.lean | 25049173cd3ecc647f5a97bbecad7871acb7664d | [
"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 | 14,415 | lean | /-
Copyright (c) 2019 Neil Strickland. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Neil Strickland
-/
import data.pnat.prime
import data.multiset.sort
/-!
# Prime factors of nonzero naturals
This file defines the factorization of a nonzero natural number `n` as a multiset of primes,
the multiplicity of `p` in this factors multiset being the p-adic valuation of `n`.
## Main declarations
* `prime_multiset`: Type of multisets of prime numbers.
* `factor_multiset n`: Multiset of prime factors of `n`.
-/
/-- The type of multisets of prime numbers. Unique factorization
gives an equivalence between this set and ℕ+, as we will formalize
below. -/
@[derive [inhabited, has_repr, canonically_ordered_add_monoid, distrib_lattice,
semilattice_sup, order_bot, has_sub, has_ordered_sub]]
def prime_multiset := multiset nat.primes
namespace prime_multiset
/-- The multiset consisting of a single prime -/
def of_prime (p : nat.primes) : prime_multiset := ({p} : multiset nat.primes)
theorem card_of_prime (p : nat.primes) : multiset.card (of_prime p) = 1 := rfl
/-- We can forget the primality property and regard a multiset
of primes as just a multiset of positive integers, or a multiset
of natural numbers. In the opposite direction, if we have a
multiset of positive integers or natural numbers, together with
a proof that all the elements are prime, then we can regard it
as a multiset of primes. The next block of results records
obvious properties of these coercions.
-/
def to_nat_multiset : prime_multiset → multiset ℕ :=
λ v, v.map (λ p, (p : ℕ))
instance coe_nat : has_coe prime_multiset (multiset ℕ) := ⟨to_nat_multiset⟩
/-- `prime_multiset.coe`, the coercion from a multiset of primes to a multiset of
naturals, promoted to an `add_monoid_hom`. -/
def coe_nat_monoid_hom : prime_multiset →+ multiset ℕ :=
{ to_fun := coe,
.. multiset.map_add_monoid_hom coe }
@[simp] lemma coe_coe_nat_monoid_hom :
(coe_nat_monoid_hom : prime_multiset → multiset ℕ) = coe := rfl
theorem coe_nat_injective : function.injective (coe : prime_multiset → multiset ℕ) :=
multiset.map_injective nat.primes.coe_nat_inj
theorem coe_nat_of_prime (p : nat.primes) :
((of_prime p) : multiset ℕ) = {p} := rfl
theorem coe_nat_prime (v : prime_multiset)
(p : ℕ) (h : p ∈ (v : multiset ℕ)) : p.prime :=
by { rcases multiset.mem_map.mp h with ⟨⟨p', hp'⟩, ⟨h_mem, h_eq⟩⟩,
exact h_eq ▸ hp' }
/-- Converts a `prime_multiset` to a `multiset ℕ+`. -/
def to_pnat_multiset : prime_multiset → multiset ℕ+ :=
λ v, v.map (λ p, (p : ℕ+))
instance coe_pnat : has_coe prime_multiset (multiset ℕ+) := ⟨to_pnat_multiset⟩
/-- `coe_pnat`, the coercion from a multiset of primes to a multiset of positive
naturals, regarded as an `add_monoid_hom`. -/
def coe_pnat_monoid_hom : prime_multiset →+ multiset ℕ+ :=
{ to_fun := coe,
.. multiset.map_add_monoid_hom coe }
@[simp] lemma coe_coe_pnat_monoid_hom :
(coe_pnat_monoid_hom : prime_multiset → multiset ℕ+) = coe := rfl
theorem coe_pnat_injective : function.injective (coe : prime_multiset → multiset ℕ+) :=
multiset.map_injective nat.primes.coe_pnat_inj
theorem coe_pnat_of_prime (p : nat.primes) :
((of_prime p) : multiset ℕ+) = {(p : ℕ+)} := rfl
theorem coe_pnat_prime (v : prime_multiset)
(p : ℕ+) (h : p ∈ (v : multiset ℕ+)) : p.prime :=
by { rcases multiset.mem_map.mp h with ⟨⟨p', hp'⟩, ⟨h_mem, h_eq⟩⟩,
exact h_eq ▸ hp' }
instance coe_multiset_pnat_nat : has_coe (multiset ℕ+) (multiset ℕ) :=
⟨λ v, v.map (λ n, (n : ℕ))⟩
theorem coe_pnat_nat (v : prime_multiset) :
((v : (multiset ℕ+)) : (multiset ℕ)) = (v : multiset ℕ) :=
by { change (v.map (coe : nat.primes → ℕ+)).map subtype.val = v.map subtype.val,
rw [multiset.map_map], congr }
/-- The product of a `prime_multiset`, as a `ℕ+`. -/
def prod (v : prime_multiset) : ℕ+ := (v : multiset pnat).prod
theorem coe_prod (v : prime_multiset) : (v.prod : ℕ) = (v : multiset ℕ).prod :=
begin
let h : (v.prod : ℕ) = ((v.map coe).map coe).prod :=
(pnat.coe_monoid_hom.map_multiset_prod v.to_pnat_multiset),
rw [multiset.map_map] at h,
have : (coe : ℕ+ → ℕ) ∘ (coe : nat.primes → ℕ+) = coe := funext (λ p, rfl),
rw[this] at h, exact h,
end
theorem prod_of_prime (p : nat.primes) : (of_prime p).prod = (p : ℕ+) :=
multiset.prod_singleton _
/-- If a `multiset ℕ` consists only of primes, it can be recast as a `prime_multiset`. -/
def of_nat_multiset
(v : multiset ℕ) (h : ∀ (p : ℕ), p ∈ v → p.prime) : prime_multiset :=
@multiset.pmap ℕ nat.primes nat.prime (λ p hp, ⟨p, hp⟩) v h
theorem to_of_nat_multiset (v : multiset ℕ) (h) :
((of_nat_multiset v h) : multiset ℕ) = v :=
begin
unfold_coes,
dsimp [of_nat_multiset, to_nat_multiset],
have : (λ (p : ℕ) (h : p.prime), ((⟨p, h⟩ : nat.primes) : ℕ)) = (λ p h, id p) :=
by {funext p h, refl},
rw [multiset.map_pmap, this, multiset.pmap_eq_map, multiset.map_id]
end
theorem prod_of_nat_multiset (v : multiset ℕ) (h) :
((of_nat_multiset v h).prod : ℕ) = (v.prod : ℕ) :=
by rw[coe_prod, to_of_nat_multiset]
/-- If a `multiset ℕ+` consists only of primes, it can be recast as a `prime_multiset`. -/
def of_pnat_multiset
(v : multiset ℕ+) (h : ∀ (p : ℕ+), p ∈ v → p.prime) : prime_multiset :=
@multiset.pmap ℕ+ nat.primes pnat.prime (λ p hp, ⟨(p : ℕ), hp⟩) v h
theorem to_of_pnat_multiset (v : multiset ℕ+) (h) :
((of_pnat_multiset v h) : multiset ℕ+) = v :=
begin
unfold_coes, dsimp[of_pnat_multiset, to_pnat_multiset],
have : (λ (p : ℕ+) (h : p.prime), ((coe : nat.primes → ℕ+) ⟨p, h⟩)) = (λ p h, id p) :=
by {funext p h, apply subtype.eq, refl},
rw[multiset.map_pmap, this, multiset.pmap_eq_map, multiset.map_id]
end
theorem prod_of_pnat_multiset (v : multiset ℕ+) (h) :
((of_pnat_multiset v h).prod : ℕ+) = v.prod :=
by { dsimp [prod], rw [to_of_pnat_multiset] }
/-- Lists can be coerced to multisets; here we have some results
about how this interacts with our constructions on multisets. -/
def of_nat_list (l : list ℕ) (h : ∀ (p : ℕ), p ∈ l → p.prime) : prime_multiset :=
of_nat_multiset (l : multiset ℕ) h
theorem prod_of_nat_list (l : list ℕ) (h) : ((of_nat_list l h).prod : ℕ) = l.prod :=
by { have := prod_of_nat_multiset (l : multiset ℕ) h,
rw [multiset.coe_prod] at this, exact this }
/-- If a `list ℕ+` consists only of primes, it can be recast as a `prime_multiset` with
the coercion from lists to multisets. -/
def of_pnat_list (l : list ℕ+) (h : ∀ (p : ℕ+), p ∈ l → p.prime) : prime_multiset :=
of_pnat_multiset (l : multiset ℕ+) h
theorem prod_of_pnat_list (l : list ℕ+) (h) : (of_pnat_list l h).prod = l.prod :=
by { have := prod_of_pnat_multiset (l : multiset ℕ+) h,
rw [multiset.coe_prod] at this, exact this }
/-- The product map gives a homomorphism from the additive monoid
of multisets to the multiplicative monoid ℕ+. -/
theorem prod_zero : (0 : prime_multiset).prod = 1 :=
by { dsimp [prod], exact multiset.prod_zero }
theorem prod_add (u v : prime_multiset) : (u + v).prod = u.prod * v.prod :=
begin
change (coe_pnat_monoid_hom (u + v)).prod = _,
rw coe_pnat_monoid_hom.map_add,
exact multiset.prod_add _ _,
end
theorem prod_smul (d : ℕ) (u : prime_multiset) :
(d • u).prod = u.prod ^ d :=
by { induction d with d ih, refl,
rw [succ_nsmul, prod_add, ih, nat.succ_eq_add_one, pow_succ, mul_comm] }
end prime_multiset
namespace pnat
/-- The prime factors of n, regarded as a multiset -/
def factor_multiset (n : ℕ+) : prime_multiset :=
prime_multiset.of_nat_list (nat.factors n) (@nat.prime_of_mem_factors n)
/-- The product of the factors is the original number -/
theorem prod_factor_multiset (n : ℕ+) : (factor_multiset n).prod = n :=
eq $ by { dsimp [factor_multiset],
rw [prime_multiset.prod_of_nat_list],
exact nat.prod_factors n.ne_zero }
theorem coe_nat_factor_multiset (n : ℕ+) :
((factor_multiset n) : (multiset ℕ)) = ((nat.factors n) : multiset ℕ) :=
prime_multiset.to_of_nat_multiset (nat.factors n) (@nat.prime_of_mem_factors n)
end pnat
namespace prime_multiset
/-- If we start with a multiset of primes, take the product and
then factor it, we get back the original multiset. -/
theorem factor_multiset_prod (v : prime_multiset) :
v.prod.factor_multiset = v :=
begin
apply prime_multiset.coe_nat_injective,
rw [v.prod.coe_nat_factor_multiset, prime_multiset.coe_prod],
rcases v with ⟨l⟩,
unfold_coes,
dsimp [prime_multiset.to_nat_multiset],
rw [multiset.coe_prod],
let l' := l.map (coe : nat.primes → ℕ),
have : ∀ (p : ℕ), p ∈ l' → p.prime :=
λ p hp, by {rcases list.mem_map.mp hp with ⟨⟨p', hp'⟩, ⟨h_mem, h_eq⟩⟩,
exact h_eq ▸ hp'},
exact multiset.coe_eq_coe.mpr (@nat.factors_unique _ l' rfl this).symm,
end
end prime_multiset
namespace pnat
/-- Positive integers biject with multisets of primes. -/
def factor_multiset_equiv : ℕ+ ≃ prime_multiset :=
{ to_fun := factor_multiset,
inv_fun := prime_multiset.prod,
left_inv := prod_factor_multiset,
right_inv := prime_multiset.factor_multiset_prod }
/-- Factoring gives a homomorphism from the multiplicative
monoid ℕ+ to the additive monoid of multisets. -/
theorem factor_multiset_one : factor_multiset 1 = 0 :=
by simp [factor_multiset, prime_multiset.of_nat_list, prime_multiset.of_nat_multiset]
theorem factor_multiset_mul (n m : ℕ+) :
factor_multiset (n * m) = (factor_multiset n) + (factor_multiset m) :=
begin
let u := factor_multiset n,
let v := factor_multiset m,
have : n = u.prod := (prod_factor_multiset n).symm, rw[this],
have : m = v.prod := (prod_factor_multiset m).symm, rw[this],
rw[← prime_multiset.prod_add],
repeat {rw[prime_multiset.factor_multiset_prod]},
end
theorem factor_multiset_pow (n : ℕ+) (m : ℕ) :
factor_multiset (n ^ m) = m • (factor_multiset n) :=
begin
let u := factor_multiset n,
have : n = u.prod := (prod_factor_multiset n).symm,
rw[this, ← prime_multiset.prod_smul],
repeat {rw[prime_multiset.factor_multiset_prod]},
end
/-- Factoring a prime gives the corresponding one-element multiset. -/
theorem factor_multiset_of_prime (p : nat.primes) :
(p : ℕ+).factor_multiset = prime_multiset.of_prime p :=
begin
apply factor_multiset_equiv.symm.injective,
change (p : ℕ+).factor_multiset.prod = (prime_multiset.of_prime p).prod,
rw[(p : ℕ+).prod_factor_multiset, prime_multiset.prod_of_prime],
end
/-- We now have four different results that all encode the
idea that inequality of multisets corresponds to divisibility
of positive integers. -/
theorem factor_multiset_le_iff {m n : ℕ+} :
factor_multiset m ≤ factor_multiset n ↔ m ∣ n :=
begin
split,
{ intro h,
rw [← prod_factor_multiset m, ← prod_factor_multiset m],
apply dvd.intro (n.factor_multiset - m.factor_multiset).prod,
rw [← prime_multiset.prod_add, prime_multiset.factor_multiset_prod,
add_tsub_cancel_of_le h, prod_factor_multiset] },
{ intro h,
rw [← mul_div_exact h, factor_multiset_mul],
exact le_self_add }
end
theorem factor_multiset_le_iff' {m : ℕ+} {v : prime_multiset}:
factor_multiset m ≤ v ↔ m ∣ v.prod :=
by { let h := @factor_multiset_le_iff m v.prod,
rw [v.factor_multiset_prod] at h, exact h }
end pnat
namespace prime_multiset
theorem prod_dvd_iff {u v : prime_multiset} : u.prod ∣ v.prod ↔ u ≤ v :=
by { let h := @pnat.factor_multiset_le_iff' u.prod v,
rw [u.factor_multiset_prod] at h, exact h.symm }
theorem prod_dvd_iff' {u : prime_multiset} {n : ℕ+} : u.prod ∣ n ↔ u ≤ n.factor_multiset :=
by { let h := @prod_dvd_iff u n.factor_multiset,
rw [n.prod_factor_multiset] at h, exact h }
end prime_multiset
namespace pnat
/-- The gcd and lcm operations on positive integers correspond
to the inf and sup operations on multisets. -/
theorem factor_multiset_gcd (m n : ℕ+) :
factor_multiset (gcd m n) = (factor_multiset m) ⊓ (factor_multiset n) :=
begin
apply le_antisymm,
{ apply le_inf_iff.mpr; split; apply factor_multiset_le_iff.mpr,
exact gcd_dvd_left m n, exact gcd_dvd_right m n},
{ rw[← prime_multiset.prod_dvd_iff, prod_factor_multiset],
apply dvd_gcd; rw[prime_multiset.prod_dvd_iff'],
exact inf_le_left, exact inf_le_right}
end
theorem factor_multiset_lcm (m n : ℕ+) :
factor_multiset (lcm m n) = (factor_multiset m) ⊔ (factor_multiset n) :=
begin
apply le_antisymm,
{ rw[← prime_multiset.prod_dvd_iff, prod_factor_multiset],
apply lcm_dvd; rw[← factor_multiset_le_iff'],
exact le_sup_left, exact le_sup_right},
{ apply sup_le_iff.mpr; split; apply factor_multiset_le_iff.mpr,
exact dvd_lcm_left m n, exact dvd_lcm_right m n },
end
/-- The number of occurrences of p in the factor multiset of m
is the same as the p-adic valuation of m. -/
theorem count_factor_multiset (m : ℕ+) (p : nat.primes) (k : ℕ) :
(p : ℕ+) ^ k ∣ m ↔ k ≤ m.factor_multiset.count p :=
begin
intros,
rw [multiset.le_count_iff_repeat_le],
rw [← factor_multiset_le_iff, factor_multiset_pow, factor_multiset_of_prime],
congr' 2,
apply multiset.eq_repeat.mpr,
split,
{ rw [multiset.card_nsmul, prime_multiset.card_of_prime, mul_one] },
{ intros q h, rw [prime_multiset.of_prime, multiset.nsmul_singleton _ k] at h,
exact multiset.eq_of_mem_repeat h }
end
end pnat
namespace prime_multiset
theorem prod_inf (u v : prime_multiset) :
(u ⊓ v).prod = pnat.gcd u.prod v.prod :=
begin
let n := u.prod,
let m := v.prod,
change (u ⊓ v).prod = pnat.gcd n m,
have : u = n.factor_multiset := u.factor_multiset_prod.symm, rw [this],
have : v = m.factor_multiset := v.factor_multiset_prod.symm, rw [this],
rw [← pnat.factor_multiset_gcd n m, pnat.prod_factor_multiset]
end
theorem prod_sup (u v : prime_multiset) :
(u ⊔ v).prod = pnat.lcm u.prod v.prod :=
begin
let n := u.prod,
let m := v.prod,
change (u ⊔ v).prod = pnat.lcm n m,
have : u = n.factor_multiset := u.factor_multiset_prod.symm, rw [this],
have : v = m.factor_multiset := v.factor_multiset_prod.symm, rw [this],
rw[← pnat.factor_multiset_lcm n m, pnat.prod_factor_multiset]
end
end prime_multiset
|
83ca7fcf6040aa72fff4d823ae3e12dce4f01ce1 | c31182a012eec69da0a1f6c05f42b0f0717d212d | /src/locally_constant/Vhat.lean | 7b06abddda2a1c14ec5c3f321df30b49ef25c4ec | [] | no_license | Ja1941/lean-liquid | fbec3ffc7fc67df1b5ca95b7ee225685ab9ffbdc | 8e80ed0cbdf5145d6814e833a674eaf05a1495c1 | refs/heads/master | 1,689,437,983,362 | 1,628,362,719,000 | 1,628,362,719,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 4,572 | lean | import for_mathlib.SemiNormedGroup_Completion
import locally_constant.SemiNormedGroup
import normed_group.normed_with_aut
/-
# Completions of normed groups
This file contains an API for completions for seminormed groups equipped with
an automorphism which scales norms by a constant factor `r`.
## Main definitions
- `normed_with_aut_Completion` : if `V` is equipped with an automorphism changing norms
by a factor `r` then the completion also has such an automorphism.
- `LCC : SemiNormedGroup ⥤ Profiniteᵒᵖ ⥤ SemiNormedGroup` :
`LCC V S` is the seminormed group completion of the locally constant functions from `S` to `V`.
## TODO
Pull off the stuff about completions and put it into `normed_group/SemiNormedGroup`?
Then `system_of_complexes.basic` would not have to import this file.
-/
noncomputable theory
open_locale nnreal
universe u
namespace SemiNormedGroup
open uniform_space opposite category_theory
instance normed_with_aut_Completion (V : SemiNormedGroup.{u}) (r : ℝ≥0) [normed_with_aut r V] :
normed_with_aut r (Completion.obj V) :=
{ T := Completion.map_iso normed_with_aut.T,
norm_T :=
begin
rw ← function.funext_iff,
refine abstract_completion.funext completion.cpkg _ _ _,
{ apply continuous_norm.comp _, exact completion.continuous_map },
{ exact (continuous_const.mul continuous_norm : _) },
intro v,
calc _ = _ : congr_arg norm (completion.map_coe _ _)
... = _ : _,
{ exact normed_group_hom.uniform_continuous _ },
{ erw [completion.norm_coe, normed_with_aut.norm_T, completion.norm_coe] }
end }
@[simp] lemma Completion_T_inv_eq (V : SemiNormedGroup.{u}) (r : ℝ≥0) [normed_with_aut r V] :
(normed_with_aut.T.hom : Completion.obj V ⟶ _) = Completion.map normed_with_aut.T.hom := rfl
lemma T_hom_incl {V : SemiNormedGroup} {r : ℝ≥0} [normed_with_aut r V] :
(incl : V ⟶ _) ≫ normed_with_aut.T.hom = normed_with_aut.T.hom ≫ incl :=
begin
ext x,
simp only [incl_apply, category_theory.comp_apply, Completion_T_inv_eq],
change completion.map normed_with_aut.T.hom _ = _,
rw completion.map_coe,
exact normed_group_hom.uniform_continuous _,
end
lemma T_hom_eq {V : SemiNormedGroup} {r : ℝ≥0} [normed_with_aut r V] :
normed_with_aut.T.hom = Completion.lift ((normed_with_aut.T.hom : V ⟶ V) ≫ incl) :=
lift_unique _ _ T_hom_incl
/-- `LCC` (Locally Constant Completion) is the bifunctor
that sends a seminormed group `V` and a profinite space `S` to `V-hat(S)`.
Here `V-hat(S)` is the completion (for the sup norm) of the locally constant functions `S → V`. -/
def LCC : SemiNormedGroup ⥤ Profiniteᵒᵖ ⥤ SemiNormedGroup :=
curry.obj ((uncurry.obj LocallyConstant) ⋙ Completion)
lemma LCC_obj_map' (V : SemiNormedGroup) {X Y : Profiniteᵒᵖ} (f : Y ⟶ X) :
(LCC.obj V).map f = Completion.map ((LocallyConstant.obj V).map f) :=
begin
delta LCC,
simp only [curry.obj_obj_map, LocallyConstant_obj_map, functor.comp_map, uncurry.obj_map,
nat_trans.id_app, functor.map_comp, functor.map_id, category_theory.functor.map_id],
erw [← functor.map_comp, category.id_comp]
end
lemma LCC_obj_map (V : SemiNormedGroup) {X Y : Profiniteᵒᵖ} (f : Y ⟶ X) (v : (LCC.obj V).obj Y) :
(LCC.obj V).map f v = completion.map (locally_constant.comap f.unop) v :=
by { rw LCC_obj_map', refl }
lemma LCC_obj_map_norm_noninc (V : SemiNormedGroup) {X Y : Profiniteᵒᵖ} (f : Y ⟶ X) :
((LCC.obj V).map f).norm_noninc :=
begin
rw LCC_obj_map',
exact (Completion_map_norm_noninc _ $ LocallyConstant_obj_map_norm_noninc _ _ _ _)
end
variables (S : Type*) [topological_space S] [compact_space S]
@[simps]
instance normed_with_aut_LocallyConstant (V : SemiNormedGroup) (S : Profiniteᵒᵖ) (r : ℝ≥0)
[normed_with_aut r V] [hr : fact (0 < r)] :
normed_with_aut r ((LocallyConstant.obj V).obj S) :=
{ T := (LocallyConstant.map_iso normed_with_aut.T).app S,
norm_T :=
begin
rw ← op_unop S,
rintro (f : locally_constant (unop S : Profinite) V),
show Sup _ = ↑r * Sup _,
dsimp,
simp only [normed_with_aut.norm_T],
convert real.Sup_mul r _ hr.out,
ext,
simp only [exists_prop, set.mem_range, exists_exists_eq_and, set.mem_set_of_eq]
end }
instance normed_with_aut_LCC (V : SemiNormedGroup) (S : Profiniteᵒᵖ) (r : ℝ≥0)
[normed_with_aut r V] [hr : fact (0 < r)] :
normed_with_aut r ((LCC.obj V).obj S) :=
show normed_with_aut r (Completion.obj $ (LocallyConstant.obj V).obj S), by apply_instance
end SemiNormedGroup
#lint- only unused_arguments def_lemma doc_blame
|
374d7505cf4d751864a7e041f0234f4fda41af03 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/algebra/group/semiconj.lean | da6490fd032e0cec2b3b0a27ade8429db5afce55 | [] | no_license | AurelienSaue/Mathlib4_auto | f538cfd0980f65a6361eadea39e6fc639e9dae14 | 590df64109b08190abe22358fabc3eae000943f2 | refs/heads/master | 1,683,906,849,776 | 1,622,564,669,000 | 1,622,564,669,000 | 371,723,747 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 6,175 | lean | /-
Copyright (c) 2019 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury Kudryashov
Some proofs and docs came from `algebra/commute` (c) Neil Strickland
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.algebra.group.units
import Mathlib.PostPort
universes u
namespace Mathlib
/-!
# Semiconjugate elements of a semigroup
## Main definitions
We say that `x` is semiconjugate to `y` by `a` (`semiconj_by a x y`), if `a * x = y * a`.
In this file we provide operations on `semiconj_by _ _ _`.
In the names of these operations, we treat `a` as the “left” argument, and both `x` and `y` as
“right” arguments. This way most names in this file agree with the names of the corresponding lemmas
for `commute a b = semiconj_by a b b`. As a side effect, some lemmas have only `_right` version.
Lean does not immediately recognise these terms as equations, so for rewriting we need syntax like
`rw [(h.pow_right 5).eq]` rather than just `rw [h.pow_right 5]`.
This file provides only basic operations (`mul_left`, `mul_right`, `inv_right` etc). Other
operations (`pow_right`, field inverse etc) are in the files that define corresponding notions.
-/
/-- `x` is semiconjugate to `y` by `a`, if `a * x = y * a`. -/
def add_semiconj_by {M : Type u} [Add M] (a : M) (x : M) (y : M) :=
a + x = y + a
namespace semiconj_by
/-- Equality behind `semiconj_by a x y`; useful for rewriting. -/
protected theorem eq {S : Type u} [Mul S] {a : S} {x : S} {y : S} (h : semiconj_by a x y) : a * x = y * a :=
h
/-- If `a` semiconjugates `x` to `y` and `x'` to `y'`,
then it semiconjugates `x * x'` to `y * y'`. -/
@[simp] theorem Mathlib.add_semiconj_by.add_right {S : Type u} [add_semigroup S] {a : S} {x : S} {y : S} {x' : S} {y' : S} (h : add_semiconj_by a x y) (h' : add_semiconj_by a x' y') : add_semiconj_by a (x + x') (y + y') := sorry
/-- If both `a` and `b` semiconjugate `x` to `y`, then so does `a * b`. -/
theorem Mathlib.add_semiconj_by.add_left {S : Type u} [add_semigroup S] {a : S} {b : S} {x : S} {y : S} {z : S} (ha : add_semiconj_by a y z) (hb : add_semiconj_by b x y) : add_semiconj_by (a + b) x z := sorry
/-- Any element semiconjugates `1` to `1`. -/
@[simp] theorem one_right {M : Type u} [monoid M] (a : M) : semiconj_by a 1 1 :=
eq.mpr (id (Eq._oldrec (Eq.refl (semiconj_by a 1 1)) (equations._eqn_1 a 1 1)))
(eq.mpr (id (Eq._oldrec (Eq.refl (a * 1 = 1 * a)) (mul_one a)))
(eq.mpr (id (Eq._oldrec (Eq.refl (a = 1 * a)) (one_mul a))) (Eq.refl a)))
/-- One semiconjugates any element to itself. -/
@[simp] theorem one_left {M : Type u} [monoid M] (x : M) : semiconj_by 1 x x :=
Eq.symm (one_right x)
/-- If `a` semiconjugates a unit `x` to a unit `y`, then it semiconjugates `x⁻¹` to `y⁻¹`. -/
theorem Mathlib.add_semiconj_by.units_neg_right {M : Type u} [add_monoid M] {a : M} {x : add_units M} {y : add_units M} (h : add_semiconj_by a ↑x ↑y) : add_semiconj_by a ↑(-x) ↑(-y) := sorry
@[simp] theorem units_inv_right_iff {M : Type u} [monoid M] {a : M} {x : units M} {y : units M} : semiconj_by a ↑(x⁻¹) ↑(y⁻¹) ↔ semiconj_by a ↑x ↑y :=
{ mp := units_inv_right, mpr := units_inv_right }
/-- If a unit `a` semiconjugates `x` to `y`, then `a⁻¹` semiconjugates `y` to `x`. -/
theorem Mathlib.add_semiconj_by.units_neg_symm_left {M : Type u} [add_monoid M] {a : add_units M} {x : M} {y : M} (h : add_semiconj_by (↑a) x y) : add_semiconj_by (↑(-a)) y x := sorry
@[simp] theorem Mathlib.add_semiconj_by.units_neg_symm_left_iff {M : Type u} [add_monoid M] {a : add_units M} {x : M} {y : M} : add_semiconj_by (↑(-a)) y x ↔ add_semiconj_by (↑a) x y :=
{ mp := add_semiconj_by.units_neg_symm_left, mpr := add_semiconj_by.units_neg_symm_left }
theorem Mathlib.add_semiconj_by.units_coe {M : Type u} [add_monoid M] {a : add_units M} {x : add_units M} {y : add_units M} (h : add_semiconj_by a x y) : add_semiconj_by ↑a ↑x ↑y :=
congr_arg add_units.val h
theorem units_of_coe {M : Type u} [monoid M] {a : units M} {x : units M} {y : units M} (h : semiconj_by ↑a ↑x ↑y) : semiconj_by a x y :=
units.ext h
@[simp] theorem units_coe_iff {M : Type u} [monoid M] {a : units M} {x : units M} {y : units M} : semiconj_by ↑a ↑x ↑y ↔ semiconj_by a x y :=
{ mp := units_of_coe, mpr := units_coe }
@[simp] theorem inv_right_iff {G : Type u} [group G] {a : G} {x : G} {y : G} : semiconj_by a (x⁻¹) (y⁻¹) ↔ semiconj_by a x y :=
units_inv_right_iff
theorem Mathlib.add_semiconj_by.neg_right {G : Type u} [add_group G] {a : G} {x : G} {y : G} : add_semiconj_by a x y → add_semiconj_by a (-x) (-y) :=
iff.mpr add_semiconj_by.neg_right_iff
@[simp] theorem inv_symm_left_iff {G : Type u} [group G] {a : G} {x : G} {y : G} : semiconj_by (a⁻¹) y x ↔ semiconj_by a x y :=
units_inv_symm_left_iff
theorem Mathlib.add_semiconj_by.neg_symm_left {G : Type u} [add_group G] {a : G} {x : G} {y : G} : add_semiconj_by a x y → add_semiconj_by (-a) y x :=
iff.mpr add_semiconj_by.neg_symm_left_iff
theorem Mathlib.add_semiconj_by.neg_neg_symm {G : Type u} [add_group G] {a : G} {x : G} {y : G} (h : add_semiconj_by a x y) : add_semiconj_by (-a) (-y) (-x) :=
add_semiconj_by.neg_symm_left (add_semiconj_by.neg_right h)
-- this is not a simp lemma because it can be deduced from other simp lemmas
theorem inv_inv_symm_iff {G : Type u} [group G] {a : G} {x : G} {y : G} : semiconj_by (a⁻¹) (y⁻¹) (x⁻¹) ↔ semiconj_by a x y :=
iff.trans inv_right_iff inv_symm_left_iff
/-- `a` semiconjugates `x` to `a * x * a⁻¹`. -/
theorem conj_mk {G : Type u} [group G] (a : G) (x : G) : semiconj_by a x (a * x * (a⁻¹)) := sorry
end semiconj_by
/-- `a` semiconjugates `x` to `a * x * a⁻¹`. -/
theorem add_units.mk_semiconj_by {M : Type u} [add_monoid M] (u : add_units M) (x : M) : add_semiconj_by (↑u) x (↑u + x + ↑(-u)) :=
eq.mpr (id (add_semiconj_by.equations._eqn_1 (↑u) x (↑u + x + ↑(-u))))
(eq.mpr (id (Eq._oldrec (Eq.refl (↑u + x = ↑u + x + ↑(-u) + ↑u)) (add_units.neg_add_cancel_right (↑u + x) u)))
(Eq.refl (↑u + x)))
|
304029e0f38617e3ec0df35ce02145519d6ab07b | 0c1546a496eccfb56620165cad015f88d56190c5 | /library/init/meta/expr.lean | d06ca9f7c49fc0a8bb2962812298ad92e990aad4 | [
"Apache-2.0"
] | permissive | Solertis/lean | 491e0939957486f664498fbfb02546e042699958 | 84188c5aa1673fdf37a082b2de8562dddf53df3f | refs/heads/master | 1,610,174,257,606 | 1,486,263,620,000 | 1,486,263,620,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 8,217 | 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.level
inductive binder_info
| default | implicit | strict_implicit | inst_implicit | other
meta constant macro_def : Type
/- Reflect a C++ expr object. The VM replaces it with the C++ implementation. -/
meta inductive expr
| var : unsigned → expr
| sort : level → expr
| const : name → list level → expr
| mvar : name → expr → expr
| local_const : name → name → binder_info → expr → expr
| app : expr → expr → expr
| lam : name → binder_info → expr → expr → expr
| pi : name → binder_info → expr → expr → expr
| elet : name → expr → expr → expr → expr
| macro : macro_def → ∀ n : unsigned, (fin (unsigned.to_nat n) → expr) → expr
meta instance : inhabited expr :=
⟨expr.sort level.zero⟩
meta constant expr.mk_macro (d : macro_def) : list expr → expr
meta constant expr.macro_def_name (d : macro_def) : name
meta def expr.mk_var (n : nat) : expr :=
expr.var (unsigned.of_nat n)
/- Choice macros are used to implement overloading. -/
meta constant expr.is_choice_macro : expr → bool
-- Compares expressions, including binder names.
meta constant expr.has_decidable_eq : decidable_eq expr
attribute [instance] expr.has_decidable_eq
-- Compares expressions while ignoring binder names.
meta constant expr.alpha_eqv : expr → expr → bool
notation a ` =ₐ `:50 b:50 := expr.alpha_eqv a b = bool.tt
meta constant expr.to_string : expr → string
meta instance : has_to_string expr :=
has_to_string.mk expr.to_string
/- Coercion for letting users write (f a) instead of (expr.app f a) -/
meta instance : has_coe_to_fun expr :=
{ F := λ e, expr → expr, coe := λ e, expr.app e }
meta constant expr.hash : expr → nat
-- Compares expressions, ignoring binder names, and sorting by hash.
meta constant expr.lt : expr → expr → bool
-- Compares expressions, ignoring binder names.
meta constant expr.lex_lt : expr → expr → bool
-- Compares expressions, ignoring binder names, and sorting by hash.
meta def expr.cmp (a b : expr) : ordering :=
if expr.lt a b then ordering.lt
else if a =ₐ b then ordering.eq
else ordering.gt
meta constant expr.fold {α : Type} : expr → α → (expr → nat → α → α) → α
meta constant expr.replace : expr → (expr → nat → option expr) → expr
meta constant expr.abstract_local : expr → name → expr
meta constant expr.abstract_locals : expr → list name → expr
meta def expr.abstract : expr → expr → expr
| e (expr.local_const n m bi t) := e^.abstract_local n
| e _ := e
meta constant expr.instantiate_var : expr → expr → expr
meta constant expr.instantiate_vars : expr → list expr → expr
meta constant expr.has_var : expr → bool
meta constant expr.has_var_idx : expr → nat → bool
meta constant expr.has_local : expr → bool
meta constant expr.has_meta_var : expr → bool
meta constant expr.lift_vars : expr → nat → nat → expr
meta constant expr.lower_vars : expr → nat → nat → expr
/- (copy_pos_info src tgt) copy position information from src to tgt. -/
meta constant expr.copy_pos_info : expr → expr → expr
meta constant expr.is_internal_cnstr : expr → option unsigned
meta constant expr.get_nat_value : expr → option nat
meta constant expr.collect_univ_params : expr → list name
namespace expr
open decidable
-- Compares expressions, ignoring binder names, and sorting by hash.
meta instance : has_ordering expr :=
⟨ expr.cmp ⟩
meta def mk_true : expr :=
const `true []
meta def mk_false : expr :=
const `false []
meta def app_of_list : expr → list expr → expr
| f [] := f
| f (p::ps) := app_of_list (f p) ps
meta def is_app : expr → bool
| (app f a) := tt
| e := ff
meta def app_fn : expr → expr
| (app f a) := f
| a := a
meta def app_arg : expr → expr
| (app f a) := a
| a := a
meta def get_app_fn : expr → expr
| (app f a) := get_app_fn f
| a := a
meta def get_app_num_args : expr → nat
| (app f a) := get_app_num_args f + 1
| e := 0
meta def get_app_args_aux : list expr → expr → list expr
| r (app f a) := get_app_args_aux (a::r) f
| r e := r
meta def get_app_args : expr → list expr :=
get_app_args_aux []
meta def mk_app : expr → list expr → expr
| e [] := e
| e (x::xs) := mk_app (e x) xs
meta def ith_arg_aux : expr → nat → expr
| (app f a) 0 := a
| (app f a) (n+1) := ith_arg_aux f n
| e _ := e
meta def ith_arg (e : expr) (i : nat) : expr :=
ith_arg_aux e (get_app_num_args e - i - 1)
meta def const_name : expr → name
| (const n ls) := n
| e := name.anonymous
meta def is_constant : expr → bool
| (const n ls) := tt
| e := ff
meta def is_local_constant : expr → bool
| (local_const n m bi t) := tt
| e := ff
meta def local_uniq_name : expr → name
| (local_const n m bi t) := n
| e := name.anonymous
meta def local_pp_name : expr → name
| (local_const x n bi t) := n
| e := name.anonymous
meta def local_type : expr → expr
| (local_const _ _ _ t) := t
| e := e
meta def is_constant_of : expr → name → bool
| (const n₁ ls) n₂ := to_bool (n₁ = n₂)
| e n := ff
meta def is_app_of (e : expr) (n : name) : bool :=
is_constant_of (get_app_fn e) n
meta def is_napp_of (e : expr) (c : name) (n : nat) : bool :=
to_bool (is_app_of e c ∧ get_app_num_args e = n)
meta def is_false (e : expr) : bool :=
is_constant_of e `false
meta def is_not : expr → option expr
| (app f a) := if is_constant_of f `not then some a else none
| (pi n bi a b) := if is_false b then some a else none
| e := none
meta def is_eq (e : expr) : option (expr × expr) :=
if is_napp_of e `eq 3
then some (app_arg (app_fn e), app_arg e)
else none
meta def is_ne (e : expr) : option (expr × expr) :=
if is_napp_of e `ne 3
then some (app_arg (app_fn e), app_arg e)
else none
meta def is_bin_arith_app (e : expr) (op : name) : option (expr × expr) :=
if is_napp_of e op 4
then some (app_arg (app_fn e), app_arg e)
else none
meta def is_lt (e : expr) : option (expr × expr) :=
is_bin_arith_app e `lt
meta def is_gt (e : expr) : option (expr × expr) :=
is_bin_arith_app e `gt
meta def is_le (e : expr) : option (expr × expr) :=
is_bin_arith_app e `le
meta def is_ge (e : expr) : option (expr × expr) :=
is_bin_arith_app e `ge
meta def is_heq (e : expr) : option (expr × expr × expr × expr) :=
if is_napp_of e `heq 4
then some (app_arg (app_fn (app_fn (app_fn e))),
app_arg (app_fn (app_fn e)),
app_arg (app_fn e),
app_arg e)
else none
meta def is_pi : expr → bool
| (pi n bi d b) := tt
| e := ff
meta def is_arrow : expr → bool
| (pi n bi d b) := bnot (has_var b)
| e := ff
meta def is_let : expr → bool
| (elet n t v b) := tt
| e := ff
meta def binding_name : expr → name
| (pi n m d b) := n
| (lam n m d b) := n
| e := name.anonymous
meta def binding_info : expr → binder_info
| (pi n bi d b) := bi
| (lam n bi d b) := bi
| e := binder_info.default
meta def binding_domain : expr → expr
| (pi n bi d b) := d
| (lam n bi d b) := d
| e := e
meta def binding_body : expr → expr
| (pi n bi d b) := b
| (lam n bi d b) := b
| e := e
meta def prop : expr := expr.sort level.zero
meta def imp (a b : expr) : expr :=
pi `a binder_info.default a b
meta def and_ (a b : expr) : expr :=
app (app (const ``and []) a) b
meta def not_ (a : expr) : expr :=
app (const ``not []) a
meta def false_ : expr := const ``false []
meta def lambdas : list expr → expr → expr
| (local_const uniq pp info t :: es) f :=
lam pp info t (abstract_local (lambdas es f) uniq)
| _ f := f
meta def pis : list expr → expr → expr
| (local_const uniq pp info t :: es) f :=
pi pp info t (abstract_local (pis es f) uniq)
| _ f := f
end expr
|
fb59f72f71afe7502792aaa74ed387170c7bca40 | b66669ef1ec06ddbd9526fe3afe0a68566dfbad3 | /src/ant.lean | 472ec9e9529d90c5fd75b725e9e2255e15336dac | [
"MIT"
] | permissive | FerdoSil/LatticesAndCellularAutomata | 40185a9099bb3cf1371c85f1ca33a17c2e26fecb | 2a69d2e74a231addf0e446dca86ef90d50d60218 | refs/heads/master | 1,587,683,992,072 | 1,586,018,471,000 | 1,586,018,471,000 | 171,533,028 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,573 | lean | -- Implementation of Langton's Ant.
-- Cell states 'cellT' are represented as constructors 'color[cardinal]'.
-- The definition 'mk_ant' builds an instance of Ant CA
-- from an initial configuration of cell states.
import cell_automaton utils
open utils
namespace la
section la
-- (B)lack / (W)hite + [cardinal direction]
@[derive decidable_eq]
inductive cellT | B | W | BN | BE | BS | BW | WN | WE | WS | WW
-- Respective (A)nt cardinal direction or 'A' for none.
inductive antCardinal | AN | AW | AE | AS | A
open cellT antCardinal
def cellT_str : cellT → string
| B := "X"
| W := " "
| BN := "^"
| BE := ">"
| BS := "v"
| BW := "<"
| WN := "^"
| WE := ">"
| WS := "v"
| WW := "<"
instance cellT_to_str : has_to_string cellT := ⟨cellT_str⟩
instance cellT_repr : has_repr cellT := ⟨cellT_str⟩
attribute [reducible]
def ant := cell_automaton cellT
def step : cellT → antCardinal → cellT
| W AS := WS
| B AS := BS
| W AE := WE
| B AE := BE
| W AW := WW
| B AW := BW
| W AN := WN
| B AN := BN
| BN _ := W
| BW _ := W
| BE _ := W
| BS _ := W
| WN _ := B
| WW _ := B
| WE _ := B
| WS _ := B
| x A := x
def la_step (cell : cellT) (neigh : list cellT) :=
let north := list.nth neigh 0 in
match north with
| none := W
| (some c) := if c = WE ∨ c = BW then step cell AS else
let west := list.nth neigh 1 in
match west with
| none := W
| (some c) := if c = WN ∨ c = BS then step cell AE else
let east := list.nth neigh 2 in
match east with
| none := W
| (some c) := if c = WS ∨ c = BN then step cell AW else
let south := list.nth neigh 3 in
match south with
| none := W
| (some c) := if c = WW ∨ c = BE then step cell AN else
step cell A
end end end end
def mk_ant (g : vec_grid₀ cellT) : ant :=
⟨g, W, cell_automatons.neumann, la_step, cell_automatons.ext_one⟩
def ant_g :=
vec_grid₀.mk ⟨11, 11, dec_trivial,
⟨[W, W, W, W, W, W, W, W, W, W, W,
W, W, W, W, W, W, W, W, W, W, W,
W, W, W, W, W, W, W, W, W, W, W,
W, W, W, W, W, W, W, W, W, W, W,
W, W, W, W, W, W, W, W, W, W, W,
W, W, W, W, W, WW, W, W, W, W, W,
W, W, W, W, W, W, W, W, W, W, W,
W, W, W, W, W, W, W, W, W, W, W,
W, W, W, W, W, W, W, W, W, W, W,
W, W, W, W, W, W, W, W, W, W, W,
W, W, W, W, W, W, W, W, W, W, W], rfl⟩⟩
⟨0, 1⟩
def simple : ant := mk_ant ant_g
end la
end la |
13f2c5bf0ea7e59296adf472729cf04b731b6e68 | 69d4931b605e11ca61881fc4f66db50a0a875e39 | /src/algebra/homology/additive.lean | b6214aaf5db7459738b3cea88ddb258355ccf8bc | [
"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 | 8,469 | lean | /-
Copyright (c) 2021 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import algebra.homology.homology
import algebra.homology.single
import category_theory.preadditive.additive_functor
/-!
# Homology is an additive functor
When `V` is preadditive, `homological_complex V c` is also preadditive,
and `homology_functor` is additive.
TODO: similarly for `R`-linear.
-/
universes v u
open_locale classical
noncomputable theory
open category_theory category_theory.limits homological_complex
variables {ι : Type*}
variables {V : Type u} [category.{v} V] [preadditive V]
variables {c : complex_shape ι} {C D E : homological_complex V c}
variables (f g : C ⟶ D) (h k : D ⟶ E) (i : ι)
namespace homological_complex
instance : has_zero (C ⟶ D) := ⟨{ f := λ i, 0 }⟩
instance : has_add (C ⟶ D) := ⟨λ f g, { f := λ i, f.f i + g.f i, }⟩
instance : has_neg (C ⟶ D) := ⟨λ f, { f := λ i, -(f.f i), }⟩
instance : has_sub (C ⟶ D) := ⟨λ f g, { f := λ i, f.f i - g.f i, }⟩
@[simp] lemma zero_f_apply (i : ι) : (0 : C ⟶ D).f i = 0 := rfl
@[simp] lemma add_f_apply (f g : C ⟶ D) (i : ι) : (f + g).f i = f.f i + g.f i := rfl
@[simp] lemma neg_f_apply (f : C ⟶ D) (i : ι) : (-f).f i = -(f.f i) := rfl
@[simp] lemma sub_f_apply (f g : C ⟶ D) (i : ι) : (f - g).f i = f.f i - g.f i := rfl
/- TODO(jmc/Scott): the instance below doesn't have the correct defeq for `nsmul` and `gsmul`.
We should generalize `function.injective.add_comm_group` and friends.
For the `R`-linear version, it will be very convenient to have
a good definition of `nsmul` and `gsmul` that matches `smul`. -/
instance : add_comm_group (C ⟶ D) :=
function.injective.add_comm_group hom.f
homological_complex.hom_f_injective (by tidy) (by tidy) (by tidy) (by tidy)
instance : preadditive (homological_complex V c) := {}
end homological_complex
namespace homological_complex
variables [has_zero_object V]
instance cycles_additive [has_equalizers V] : (cycles_functor V c i).additive := {}
variables [has_images V] [has_image_maps V]
instance boundaries_additive : (boundaries_functor V c i).additive := {}
variables [has_equalizers V] [has_cokernels V]
instance homology_additive : (homology_functor V c i).additive :=
{ map_zero' := λ C D, begin
dsimp [homology_functor],
ext,
simp only [limits.cokernel.π_desc, limits.comp_zero, homology.π_map],
convert zero_comp,
ext,
simp,
end,
map_add' := λ C D f g, begin
dsimp [homology_functor],
ext,
simp only [homology.π_map, preadditive.comp_add, ←preadditive.add_comp],
congr,
ext, simp,
end }
end homological_complex
namespace category_theory
variables {W : Type*} [category W] [preadditive W]
/--
An additive functor induces a functor between homological complexes.
This is sometimes called the "prolongation".
-/
@[simps]
def functor.map_homological_complex (F : V ⥤ W) [F.additive] (c : complex_shape ι) :
homological_complex V c ⥤ homological_complex W c :=
{ obj := λ C,
{ X := λ i, F.obj (C.X i),
d := λ i j, F.map (C.d i j),
shape' := λ i j w, by rw [C.shape _ _ w, F.map_zero],
d_comp_d' := λ i j k _ _, by rw [←F.map_comp, C.d_comp_d, F.map_zero], },
map := λ C D f,
{ f := λ i, F.map (f.f i),
comm' := λ i j h, by { dsimp, rw [←F.map_comp, ←F.map_comp, f.comm], }, }, }.
instance functor.map_homogical_complex_additive
(F : V ⥤ W) [F.additive] (c : complex_shape ι) : (F.map_homological_complex c).additive := {}
/--
A natural transformation between functors induces a natural transformation
between those functors applied to homological complexes.
-/
@[simps]
def nat_trans.map_homological_complex {F G : V ⥤ W} [F.additive] [G.additive]
(α : F ⟶ G) (c : complex_shape ι) : F.map_homological_complex c ⟶ G.map_homological_complex c :=
{ app := λ C, { f := λ i, α.app _, }, }
@[simp] lemma nat_trans.map_homological_complex_id (c : complex_shape ι) (F : V ⥤ W) [F.additive] :
nat_trans.map_homological_complex (𝟙 F) c = 𝟙 (F.map_homological_complex c) :=
by tidy
@[simp] lemma nat_trans.map_homological_complex_comp (c : complex_shape ι)
{F G H : V ⥤ W} [F.additive] [G.additive] [H.additive]
(α : F ⟶ G) (β : G ⟶ H):
nat_trans.map_homological_complex (α ≫ β) c =
nat_trans.map_homological_complex α c ≫ nat_trans.map_homological_complex β c :=
by tidy
@[simp, reassoc] lemma nat_trans.map_homological_complex_naturality {c : complex_shape ι}
{F G : V ⥤ W} [F.additive] [G.additive] (α : F ⟶ G) {C D : homological_complex V c} (f : C ⟶ D) :
(F.map_homological_complex c).map f ≫ (nat_trans.map_homological_complex α c).app D =
(nat_trans.map_homological_complex α c).app C ≫ (G.map_homological_complex c).map f :=
by tidy
end category_theory
variables [has_zero_object V] {W : Type*} [category W] [preadditive W] [has_zero_object W]
namespace homological_complex
/--
Turning an object into a complex supported at `j` then applying a functor is
the same as applying the functor then forming the complex.
-/
def single_map_homological_complex (F : V ⥤ W) [F.additive] (c : complex_shape ι) (j : ι):
single V c j ⋙ F.map_homological_complex _ ≅ F ⋙ single W c j :=
nat_iso.of_components (λ X,
{ hom := { f := λ i, if h : i = j then
eq_to_hom (by simp [h])
else
0, },
inv := { f := λ i, if h : i = j then
eq_to_hom (by simp [h])
else
0, },
hom_inv_id' := begin
ext i,
dsimp,
split_ifs with h,
{ simp [h] },
{ rw [zero_comp, if_neg h],
exact (zero_of_source_iso_zero _ F.map_zero_object).symm, },
end,
inv_hom_id' := begin
ext i,
dsimp,
split_ifs with h,
{ simp [h] },
{ rw [zero_comp, if_neg h],
simp, },
end, })
(λ X Y f, begin
ext i,
dsimp,
split_ifs with h; simp [h],
end).
variables (F : V ⥤ W) [functor.additive F] (c)
@[simp] lemma single_map_homological_complex_hom_app_self (j : ι) (X : V) :
((single_map_homological_complex F c j).hom.app X).f j = eq_to_hom (by simp) :=
by simp [single_map_homological_complex]
@[simp] lemma single_map_homological_complex_hom_app_ne
{i j : ι} (h : i ≠ j) (X : V) :
((single_map_homological_complex F c j).hom.app X).f i = 0 :=
by simp [single_map_homological_complex, h]
@[simp] lemma single_map_homological_complex_inv_app_self (j : ι) (X : V) :
((single_map_homological_complex F c j).inv.app X).f j = eq_to_hom (by simp) :=
by simp [single_map_homological_complex]
@[simp] lemma single_map_homological_complex_inv_app_ne
{i j : ι} (h : i ≠ j) (X : V):
((single_map_homological_complex F c j).inv.app X).f i = 0 :=
by simp [single_map_homological_complex, h]
end homological_complex
namespace chain_complex
-- TODO: dualize to cochain complexes
/--
Turning an object into a chain complex supported at zero then applying a functor is
the same as applying the functor then forming the complex.
-/
def single₀_map_homological_complex (F : V ⥤ W) [F.additive] :
single₀ V ⋙ F.map_homological_complex _ ≅ F ⋙ single₀ W :=
nat_iso.of_components (λ X,
{ hom := { f := λ i, match i with
| 0 := 𝟙 _
| (i+1) := F.map_zero_object.hom
end, },
inv := { f := λ i, match i with
| 0 := 𝟙 _
| (i+1) := F.map_zero_object.inv
end, },
hom_inv_id' := begin
ext (_|i),
{ unfold_aux, simp, },
{ unfold_aux,
dsimp,
simp only [comp_f, id_f, zero_comp],
exact (zero_of_source_iso_zero _ F.map_zero_object).symm, }
end,
inv_hom_id' := by { ext (_|i); { unfold_aux, dsimp, simp, }, }, })
(λ X Y f, by { ext (_|i); { unfold_aux, dsimp, simp, }, }).
@[simp] lemma single₀_map_homological_complex_hom_app_zero (F : V ⥤ W) [F.additive] (X : V) :
((single₀_map_homological_complex F).hom.app X).f 0 = 𝟙 _ := rfl
@[simp] lemma single₀_map_homological_complex_hom_app_succ
(F : V ⥤ W) [F.additive] (X : V) (n : ℕ) :
((single₀_map_homological_complex F).hom.app X).f (n+1) = 0 := rfl
@[simp] lemma single₀_map_homological_complex_inv_app_zero (F : V ⥤ W) [F.additive] (X : V) :
((single₀_map_homological_complex F).inv.app X).f 0 = 𝟙 _ := rfl
@[simp] lemma single₀_map_homological_complex_inv_app_succ
(F : V ⥤ W) [F.additive] (X : V) (n : ℕ) :
((single₀_map_homological_complex F).inv.app X).f (n+1) = 0 := rfl
end chain_complex
|
82b935c3f775d9b6fe521708ec79761e9b46263e | a45212b1526d532e6e83c44ddca6a05795113ddc | /src/group_theory/quotient_group.lean | 1cb85558ffac12f7ec469e3ce2fe226545200861 | [
"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 | 9,899 | lean | /-
Copyright (c) 2018 Kevin Buzzard and Patrick Massot. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kevin Buzzard, Patrick Massot.
This file is to a certain extent based on `quotient_module.lean` by Johannes Hölzl.
-/
import group_theory.coset
universes u v
namespace quotient_group
variables {G : Type u} [group G] (N : set G) [normal_subgroup N] {H : Type v} [group H]
instance : group (quotient N) :=
{ one := (1 : G),
mul := λ a b, quotient.lift_on₂' a b
(λ a b, ((a * b : G) : quotient N))
(λ a₁ a₂ b₁ b₂ hab₁ hab₂,
quot.sound
((is_subgroup.mul_mem_cancel_left N (is_subgroup.inv_mem hab₂)).1
(by rw [mul_inv_rev, mul_inv_rev, ← mul_assoc (a₂⁻¹ * a₁⁻¹),
mul_assoc _ b₂, ← mul_assoc b₂, mul_inv_self, one_mul, mul_assoc (a₂⁻¹)];
exact normal_subgroup.normal _ hab₁ _))),
mul_assoc := λ a b c, quotient.induction_on₃' a b c
(λ a b c, congr_arg mk (mul_assoc a b c)),
one_mul := λ a, quotient.induction_on' a
(λ a, congr_arg mk (one_mul a)),
mul_one := λ a, quotient.induction_on' a
(λ a, congr_arg mk (mul_one a)),
inv := λ a, quotient.lift_on' a (λ a, ((a⁻¹ : G) : quotient N))
(λ a b hab, quotient.sound' begin
show a⁻¹⁻¹ * b⁻¹ ∈ N,
rw ← mul_inv_rev,
exact is_subgroup.inv_mem (is_subgroup.mem_norm_comm hab)
end),
mul_left_inv := λ a, quotient.induction_on' a
(λ a, congr_arg mk (mul_left_inv a)) }
attribute [to_additive quotient_add_group.add_group._proof_6] quotient_group.group._proof_6
attribute [to_additive quotient_add_group.add_group._proof_5] quotient_group.group._proof_5
attribute [to_additive quotient_add_group.add_group._proof_4] quotient_group.group._proof_4
attribute [to_additive quotient_add_group.add_group._proof_3] quotient_group.group._proof_3
attribute [to_additive quotient_add_group.add_group._proof_2] quotient_group.group._proof_2
attribute [to_additive quotient_add_group.add_group._proof_1] quotient_group.group._proof_1
attribute [to_additive quotient_add_group.add_group] quotient_group.group
attribute [to_additive quotient_add_group.quotient.equations._eqn_1] quotient_group.quotient.equations._eqn_1
attribute [to_additive quotient_add_group.add_group.equations._eqn_1] quotient_group.group.equations._eqn_1
instance : is_group_hom (mk : G → quotient N) := ⟨λ _ _, rfl⟩
attribute [to_additive quotient_add_group.is_add_group_hom] quotient_group.is_group_hom
attribute [to_additive quotient_add_group.is_add_group_hom.equations._eqn_1] quotient_group.is_group_hom.equations._eqn_1
@[simp] lemma ker_mk :
is_group_hom.ker (quotient_group.mk : G → quotient_group.quotient N) = N :=
begin
ext g,
rw [is_group_hom.mem_ker, eq_comm],
show (((1 : G) : quotient_group.quotient N)) = g ↔ _,
rw [quotient_group.eq, one_inv, one_mul],
end
attribute [to_additive quotient_add_group.ker_mk] quotient_group.ker_mk
instance {G : Type*} [comm_group G] (s : set G) [is_subgroup s] : comm_group (quotient s) :=
{ mul_comm := λ a b, quotient.induction_on₂' a b
(λ a b, congr_arg mk (mul_comm a b)),
..@quotient_group.group _ _ s (normal_subgroup_of_comm_group s) }
attribute [to_additive quotient_add_group.add_comm_group._proof_6] quotient_group.comm_group._proof_6
attribute [to_additive quotient_add_group.add_comm_group._proof_5] quotient_group.comm_group._proof_5
attribute [to_additive quotient_add_group.add_comm_group._proof_4] quotient_group.comm_group._proof_4
attribute [to_additive quotient_add_group.add_comm_group._proof_3] quotient_group.comm_group._proof_3
attribute [to_additive quotient_add_group.add_comm_group._proof_2] quotient_group.comm_group._proof_2
attribute [to_additive quotient_add_group.add_comm_group._proof_1] quotient_group.comm_group._proof_1
attribute [to_additive quotient_add_group.add_comm_group] quotient_group.comm_group
attribute [to_additive quotient_add_group.add_comm_group.equations._eqn_1] quotient_group.comm_group.equations._eqn_1
@[simp] lemma coe_one : ((1 : G) : quotient N) = 1 := rfl
@[simp] lemma coe_mul (a b : G) : ((a * b : G) : quotient N) = a * b := rfl
@[simp] lemma coe_inv (a : G) : ((a⁻¹ : G) : quotient N) = a⁻¹ := rfl
@[simp] lemma coe_pow (a : G) (n : ℕ) : ((a ^ n : G) : quotient N) = a ^ n :=
@is_group_hom.map_pow _ _ _ _ mk _ a n
attribute [to_additive quotient_add_group.coe_zero] coe_one
attribute [to_additive quotient_add_group.coe_add] coe_mul
attribute [to_additive quotient_add_group.coe_neg] coe_inv
@[simp] lemma coe_gpow (a : G) (n : ℤ) : ((a ^ n : G) : quotient N) = a ^ n :=
@is_group_hom.map_gpow _ _ _ _ mk _ a n
local notation ` Q ` := quotient N
def lift (φ : G → H) [is_group_hom φ] (HN : ∀x∈N, φ x = 1) (q : Q) : H :=
q.lift_on' φ $ assume a b (hab : a⁻¹ * b ∈ N),
(calc φ a = φ a * 1 : by simp
... = φ a * φ (a⁻¹ * b) : by rw HN (a⁻¹ * b) hab
... = φ (a * (a⁻¹ * b)) : by rw is_group_hom.map_mul φ a (a⁻¹ * b)
... = φ b : by simp)
attribute [to_additive quotient_add_group.lift._proof_1] lift._proof_1
attribute [to_additive quotient_add_group.lift] lift
attribute [to_additive quotient_add_group.lift.equations._eqn_1] lift.equations._eqn_1
@[simp] lemma lift_mk {φ : G → H} [is_group_hom φ] (HN : ∀x∈N, φ x = 1) (g : G) :
lift N φ HN (g : Q) = φ g := rfl
attribute [to_additive quotient_add_group.lift_mk] lift_mk
@[simp] lemma lift_mk' {φ : G → H} [is_group_hom φ] (HN : ∀x∈N, φ x = 1) (g : G) :
lift N φ HN (mk g : Q) = φ g := rfl
attribute [to_additive quotient_add_group.lift_mk'] lift_mk'
def map (M : set H) [normal_subgroup M] (f : G → H) [is_group_hom f] (h : N ⊆ f ⁻¹' M) :
quotient N → quotient M :=
begin
haveI : is_group_hom ((mk : H → quotient M) ∘ f) := is_group_hom.comp _ _,
refine quotient_group.lift N (mk ∘ f) _,
assume x hx,
refine quotient_group.eq.2 _,
rw [mul_one, is_subgroup.inv_mem_iff],
exact h hx,
end
attribute [to_additive quotient_add_group.map._proof_1] map._proof_1
attribute [to_additive quotient_add_group.map._proof_2] map._proof_2
attribute [to_additive quotient_add_group.map] map
variables (φ : G → H) [is_group_hom φ] (HN : ∀x∈N, φ x = 1)
instance is_group_hom_quotient_lift :
is_group_hom (lift N φ HN) :=
⟨λ q r, quotient.induction_on₂' q r $ λ a b,
show φ (a * b) = φ a * φ b, from is_group_hom.map_mul φ a b⟩
attribute [to_additive quotient_add_group.is_add_group_hom_quotient_lift] quotient_group.is_group_hom_quotient_lift
attribute [to_additive quotient_add_group.is_add_group_hom_quotient_lift.equations._eqn_1] quotient_group.is_group_hom_quotient_lift.equations._eqn_1
@[to_additive quotient_add_group.map_is_add_group_hom]
instance map_is_group_hom (M : set H) [normal_subgroup M]
(f : G → H) [is_group_hom f] (h : N ⊆ f ⁻¹' M) : is_group_hom (map N M f h) :=
quotient_group.is_group_hom_quotient_lift _ _ _
open function is_group_hom
/-- The induced map from the quotient by the kernel to the codomain. -/
def ker_lift : quotient (ker φ) → H :=
lift _ φ $ λ g, (mem_ker φ).mp
attribute [to_additive quotient_add_group.ker_lift._proof_1] quotient_group.ker_lift._proof_1
attribute [to_additive quotient_add_group.ker_lift._proof_2] quotient_group.ker_lift._proof_2
attribute [to_additive quotient_add_group.ker_lift] quotient_group.ker_lift
attribute [to_additive quotient_add_group.ker_lift.equations._eqn_1] quotient_group.ker_lift.equations._eqn_1
@[simp, to_additive quotient_add_group.ker_lift_mk]
lemma ker_lift_mk (g : G) : (ker_lift φ) g = φ g :=
lift_mk _ _ _
@[simp, to_additive quotient_add_group.ker_lift_mk']
lemma ker_lift_mk' (g : G) : (ker_lift φ) (mk g) = φ g :=
lift_mk' _ _ _
@[to_additive quotient_add_group.ker_lift_is_add_group_hom]
instance ker_lift_is_group_hom : is_group_hom (ker_lift φ) :=
quotient_group.is_group_hom_quotient_lift _ _ _
@[to_additive quotient_add_group.injective_ker_lift]
lemma injective_ker_lift : injective (ker_lift φ) :=
assume a b, quotient.induction_on₂' a b $ assume a b (h : φ a = φ b), quotient.sound' $
show a⁻¹ * b ∈ ker φ, by rw [mem_ker φ,
is_group_hom.map_mul φ, ← h, is_group_hom.map_inv φ, inv_mul_self]
--@[to_additive quotient_add_group.quotient_ker_equiv_range]
noncomputable def quotient_ker_equiv_range : (quotient (ker φ)) ≃ set.range φ :=
@equiv.of_bijective _ (set.range φ) (λ x, ⟨lift (ker φ) φ
(by simp [mem_ker]) x, by exact quotient.induction_on' x (λ x, ⟨x, rfl⟩)⟩)
⟨λ a b h, injective_ker_lift _ (subtype.mk.inj h),
λ ⟨x, y, hy⟩, ⟨mk y, subtype.eq hy⟩⟩
noncomputable def quotient_ker_equiv_of_surjective (hφ : function.surjective φ) :
(quotient (ker φ)) ≃ H :=
calc (quotient_group.quotient (is_group_hom.ker φ)) ≃ set.range φ : quotient_ker_equiv_range _
... ≃ H : ⟨λ a, a.1, λ b, ⟨b, hφ b⟩, λ ⟨_, _⟩, rfl, λ _, rfl⟩
end quotient_group
namespace quotient_add_group
open is_add_group_hom
variables {G : Type u} [_root_.add_group G] (N : set G) [normal_add_subgroup N] {H : Type v} [_root_.add_group H]
variables (φ : G → H) [_root_.is_add_group_hom φ]
noncomputable def quotient_ker_equiv_range : (quotient (ker φ)) ≃ set.range φ :=
@quotient_group.quotient_ker_equiv_range (multiplicative G) _ (multiplicative H) _ φ _
noncomputable def quotient_ker_equiv_of_surjective (hφ : function.surjective φ) : (quotient (ker φ)) ≃ H :=
@quotient_group.quotient_ker_equiv_of_surjective (multiplicative G) _ (multiplicative H) _ φ _ hφ
attribute [to_additive quotient_add_group.quotient_ker_equiv_range] quotient_group.quotient_ker_equiv_range
attribute [to_additive quotient_add_group.quotient_ker_equiv_of_surjective] quotient_group.quotient_ker_equiv_of_surjective
end quotient_add_group
|
eb113a6b89e825ed60ccfc9e664502eeee67b0d4 | ce6917c5bacabee346655160b74a307b4a5ab620 | /src/ch5/ex0211.lean | b48f6a31d8c0c815c1b9c5d50195ac25fb5ec140 | [] | no_license | Ailrun/Theorem_Proving_in_Lean | ae6a23f3c54d62d401314d6a771e8ff8b4132db2 | 2eb1b5caf93c6a5a555c79e9097cf2ba5a66cf68 | refs/heads/master | 1,609,838,270,467 | 1,586,846,743,000 | 1,586,846,743,000 | 240,967,761 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 78 | lean | example : ∃ a : ℕ, 5 = a :=
begin
apply exists.intro,
reflexivity
end
|
7c2d2271f92550b5ec879190d47de39a82b70882 | a7eef317ddec01b9fc6cfbb876fe7ac00f205ac7 | /src/topology/metric_space/gromov_hausdorff.lean | 143814f02c97236ee55c769900c7fe0d76a180c1 | [
"Apache-2.0"
] | permissive | kmill/mathlib | ea5a007b67ae4e9e18dd50d31d8aa60f650425ee | 1a419a9fea7b959317eddd556e1bb9639f4dcc05 | refs/heads/master | 1,668,578,197,719 | 1,593,629,163,000 | 1,593,629,163,000 | 276,482,939 | 0 | 0 | null | 1,593,637,960,000 | 1,593,637,959,000 | null | UTF-8 | Lean | false | false | 55,675 | lean | /-
Copyright (c) 2019 Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Sébastien Gouëzel
-/
import topology.metric_space.closeds
import set_theory.cardinal
import topology.metric_space.gromov_hausdorff_realized
import topology.metric_space.completion
/-!
# Gromov-Hausdorff distance
This file defines the Gromov-Hausdorff distance on the space of nonempty compact metric spaces
up to isometry.
We introduce the space of all nonempty compact metric spaces, up to isometry,
called `GH_space`, and endow it with a metric space structure. The distance,
known as the Gromov-Hausdorff distance, is defined as follows: given two
nonempty compact spaces `X` and `Y`, their distance is the minimum Hausdorff distance
between all possible isometric embeddings of `X` and `Y` in all metric spaces.
To define properly the Gromov-Hausdorff space, we consider the non-empty
compact subsets of `ℓ^∞(ℝ)` up to isometry, which is a well-defined type,
and define the distance as the infimum of the Hausdorff distance over all
embeddings in `ℓ^∞(ℝ)`. We prove that this coincides with the previous description,
as all separable metric spaces embed isometrically into `ℓ^∞(ℝ)`, through an
embedding called the Kuratowski embedding.
To prove that we have a distance, we should show that if spaces can be coupled
to be arbitrarily close, then they are isometric. More generally, the Gromov-Hausdorff
distance is realized, i.e., there is a coupling for which the Hausdorff distance
is exactly the Gromov-Hausdorff distance. This follows from a compactness
argument, essentially following from Arzela-Ascoli.
## Main results
We prove the most important properties of the Gromov-Hausdorff space: it is a polish space,
i.e., it is complete and second countable. We also prove the Gromov compactness criterion.
-/
noncomputable theory
open_locale classical topological_space
universes u v w
open classical set function topological_space filter metric quotient
open bounded_continuous_function nat Kuratowski_embedding
open sum (inl inr)
local attribute [instance] metric_space_sum
namespace Gromov_Hausdorff
section GH_space
/- In this section, we define the Gromov-Hausdorff space, denoted `GH_space` as the quotient
of nonempty compact subsets of `ℓ^∞(ℝ)` by identifying isometric sets.
Using the Kuratwoski embedding, we get a canonical map `to_GH_space` mapping any nonempty
compact type to `GH_space`. -/
/-- Equivalence relation identifying two nonempty compact sets which are isometric -/
private definition isometry_rel : nonempty_compacts ℓ_infty_ℝ → nonempty_compacts ℓ_infty_ℝ → Prop :=
λx y, nonempty (x.val ≃ᵢ y.val)
/-- This is indeed an equivalence relation -/
private lemma is_equivalence_isometry_rel : equivalence isometry_rel :=
⟨λx, ⟨isometric.refl _⟩, λx y ⟨e⟩, ⟨e.symm⟩, λ x y z ⟨e⟩ ⟨f⟩, ⟨e.trans f⟩⟩
/-- setoid instance identifying two isometric nonempty compact subspaces of ℓ^∞(ℝ) -/
instance isometry_rel.setoid : setoid (nonempty_compacts ℓ_infty_ℝ) :=
setoid.mk isometry_rel is_equivalence_isometry_rel
/-- The Gromov-Hausdorff space -/
definition GH_space : Type := quotient (isometry_rel.setoid)
/-- Map any nonempty compact type to `GH_space` -/
definition to_GH_space (α : Type u) [metric_space α] [compact_space α] [nonempty α] : GH_space :=
⟦nonempty_compacts.Kuratowski_embedding α⟧
instance : inhabited GH_space := ⟨quot.mk _ ⟨{0}, by simp⟩⟩
/-- A metric space representative of any abstract point in `GH_space` -/
definition GH_space.rep (p : GH_space) : Type := (quot.out p).val
lemma eq_to_GH_space_iff {α : Type u} [metric_space α] [compact_space α] [nonempty α] {p : nonempty_compacts ℓ_infty_ℝ} :
⟦p⟧ = to_GH_space α ↔ ∃Ψ : α → ℓ_infty_ℝ, isometry Ψ ∧ range Ψ = p.val :=
begin
simp only [to_GH_space, quotient.eq],
split,
{ assume h,
rcases setoid.symm h with ⟨e⟩,
have f := (Kuratowski_embedding.isometry α).isometric_on_range.trans e,
use λx, f x,
split,
{ apply isometry_subtype_coe.comp f.isometry },
{ rw [range_comp, f.range_coe, set.image_univ, subtype.range_coe] } },
{ rintros ⟨Ψ, ⟨isomΨ, rangeΨ⟩⟩,
have f := ((Kuratowski_embedding.isometry α).isometric_on_range.symm.trans
isomΨ.isometric_on_range).symm,
have E : (range Ψ ≃ᵢ (nonempty_compacts.Kuratowski_embedding α).val) = (p.val ≃ᵢ range (Kuratowski_embedding α)),
by { dunfold nonempty_compacts.Kuratowski_embedding, rw [rangeΨ]; refl },
have g := cast E f,
exact ⟨g⟩ }
end
lemma eq_to_GH_space {p : nonempty_compacts ℓ_infty_ℝ} : ⟦p⟧ = to_GH_space p.val :=
begin
refine eq_to_GH_space_iff.2 ⟨((λx, x) : p.val → ℓ_infty_ℝ), _, subtype.range_coe⟩,
apply isometry_subtype_coe
end
section
local attribute [reducible] GH_space.rep
instance rep_GH_space_metric_space {p : GH_space} : metric_space (p.rep) :=
by apply_instance
instance rep_GH_space_compact_space {p : GH_space} : compact_space (p.rep) :=
by apply_instance
instance rep_GH_space_nonempty {p : GH_space} : nonempty (p.rep) :=
by apply_instance
end
lemma GH_space.to_GH_space_rep (p : GH_space) : to_GH_space (p.rep) = p :=
begin
change to_GH_space (quot.out p).val = p,
rw ← eq_to_GH_space,
exact quot.out_eq p
end
/-- Two nonempty compact spaces have the same image in `GH_space` if and only if they are isometric -/
lemma to_GH_space_eq_to_GH_space_iff_isometric {α : Type u} [metric_space α] [compact_space α] [nonempty α]
{β : Type u} [metric_space β] [compact_space β] [nonempty β] :
to_GH_space α = to_GH_space β ↔ nonempty (α ≃ᵢ β) :=
⟨begin
simp only [to_GH_space, quotient.eq],
assume h,
rcases h with e,
have I : ((nonempty_compacts.Kuratowski_embedding α).val ≃ᵢ (nonempty_compacts.Kuratowski_embedding β).val)
= ((range (Kuratowski_embedding α)) ≃ᵢ (range (Kuratowski_embedding β))),
by { dunfold nonempty_compacts.Kuratowski_embedding, refl },
have e' := cast I e,
have f := (Kuratowski_embedding.isometry α).isometric_on_range,
have g := (Kuratowski_embedding.isometry β).isometric_on_range.symm,
have h := (f.trans e').trans g,
exact ⟨h⟩
end,
begin
rintros ⟨e⟩,
simp only [to_GH_space, quotient.eq],
have f := (Kuratowski_embedding.isometry α).isometric_on_range.symm,
have g := (Kuratowski_embedding.isometry β).isometric_on_range,
have h := (f.trans e).trans g,
have I : ((range (Kuratowski_embedding α)) ≃ᵢ (range (Kuratowski_embedding β))) =
((nonempty_compacts.Kuratowski_embedding α).val ≃ᵢ (nonempty_compacts.Kuratowski_embedding β).val),
by { dunfold nonempty_compacts.Kuratowski_embedding, refl },
have h' := cast I h,
exact ⟨h'⟩
end⟩
/-- Distance on `GH_space`: the distance between two nonempty compact spaces is the infimum
Hausdorff distance between isometric copies of the two spaces in a metric space. For the definition,
we only consider embeddings in `ℓ^∞(ℝ)`, but we will prove below that it works for all spaces. -/
instance : has_dist (GH_space) :=
{ dist := λx y, Inf ((λp : nonempty_compacts ℓ_infty_ℝ × nonempty_compacts ℓ_infty_ℝ, Hausdorff_dist p.1.val p.2.val) ''
(set.prod {a | ⟦a⟧ = x} {b | ⟦b⟧ = y})) }
/-- The Gromov-Hausdorff distance between two nonempty compact metric spaces, equal by definition to
the distance of the equivalence classes of these spaces in the Gromov-Hausdorff space. -/
def GH_dist (α : Type u) (β : Type v) [metric_space α] [nonempty α] [compact_space α]
[metric_space β] [nonempty β] [compact_space β] : ℝ := dist (to_GH_space α) (to_GH_space β)
lemma dist_GH_dist (p q : GH_space) : dist p q = GH_dist (p.rep) (q.rep) :=
by rw [GH_dist, p.to_GH_space_rep, q.to_GH_space_rep]
/-- The Gromov-Hausdorff distance between two spaces is bounded by the Hausdorff distance
of isometric copies of the spaces, in any metric space. -/
theorem GH_dist_le_Hausdorff_dist {α : Type u} [metric_space α] [compact_space α] [nonempty α]
{β : Type v} [metric_space β] [compact_space β] [nonempty β]
{γ : Type w} [metric_space γ] {Φ : α → γ} {Ψ : β → γ} (ha : isometry Φ) (hb : isometry Ψ) :
GH_dist α β ≤ Hausdorff_dist (range Φ) (range Ψ) :=
begin
/- For the proof, we want to embed `γ` in `ℓ^∞(ℝ)`, to say that the Hausdorff distance is realized
in `ℓ^∞(ℝ)` and therefore bounded below by the Gromov-Hausdorff-distance. However, `γ` is not
separable in general. We restrict to the union of the images of `α` and `β` in `γ`, which is
separable and therefore embeddable in `ℓ^∞(ℝ)`. -/
rcases exists_mem_of_nonempty α with ⟨xα, _⟩,
letI : inhabited α := ⟨xα⟩,
letI : inhabited β := classical.inhabited_of_nonempty (by assumption),
let s : set γ := (range Φ) ∪ (range Ψ),
let Φ' : α → subtype s := λy, ⟨Φ y, mem_union_left _ (mem_range_self _)⟩,
let Ψ' : β → subtype s := λy, ⟨Ψ y, mem_union_right _ (mem_range_self _)⟩,
have IΦ' : isometry Φ' := λx y, ha x y,
have IΨ' : isometry Ψ' := λx y, hb x y,
have : compact s, from (compact_range ha.continuous).union (compact_range hb.continuous),
letI : metric_space (subtype s) := by apply_instance,
haveI : compact_space (subtype s) := ⟨compact_iff_compact_univ.1 ‹compact s›⟩,
haveI : nonempty (subtype s) := ⟨Φ' xα⟩,
have ΦΦ' : Φ = subtype.val ∘ Φ', by { funext, refl },
have ΨΨ' : Ψ = subtype.val ∘ Ψ', by { funext, refl },
have : Hausdorff_dist (range Φ) (range Ψ) = Hausdorff_dist (range Φ') (range Ψ'),
{ rw [ΦΦ', ΨΨ', range_comp, range_comp],
exact Hausdorff_dist_image (isometry_subtype_coe) },
rw this,
-- Embed `s` in `ℓ^∞(ℝ)` through its Kuratowski embedding
let F := Kuratowski_embedding (subtype s),
have : Hausdorff_dist (F '' (range Φ')) (F '' (range Ψ')) = Hausdorff_dist (range Φ') (range Ψ') :=
Hausdorff_dist_image (Kuratowski_embedding.isometry _),
rw ← this,
-- Let `A` and `B` be the images of `α` and `β` under this embedding. They are in `ℓ^∞(ℝ)`, and
-- their Hausdorff distance is the same as in the original space.
let A : nonempty_compacts ℓ_infty_ℝ := ⟨F '' (range Φ'), ⟨(range_nonempty _).image _,
(compact_range IΦ'.continuous).image (Kuratowski_embedding.isometry _).continuous⟩⟩,
let B : nonempty_compacts ℓ_infty_ℝ := ⟨F '' (range Ψ'), ⟨(range_nonempty _).image _,
(compact_range IΨ'.continuous).image (Kuratowski_embedding.isometry _).continuous⟩⟩,
have Aα : ⟦A⟧ = to_GH_space α,
{ rw eq_to_GH_space_iff,
exact ⟨λx, F (Φ' x), ⟨(Kuratowski_embedding.isometry _).comp IΦ', by rw range_comp⟩⟩ },
have Bβ : ⟦B⟧ = to_GH_space β,
{ rw eq_to_GH_space_iff,
exact ⟨λx, F (Ψ' x), ⟨(Kuratowski_embedding.isometry _).comp IΨ', by rw range_comp⟩⟩ },
refine cInf_le ⟨0,
begin simp [lower_bounds], assume t _ _ _ _ ht, rw ← ht, exact Hausdorff_dist_nonneg end⟩ _,
apply (mem_image _ _ _).2,
existsi (⟨A, B⟩ : nonempty_compacts ℓ_infty_ℝ × nonempty_compacts ℓ_infty_ℝ),
simp [Aα, Bβ]
end
/-- The optimal coupling constructed above realizes exactly the Gromov-Hausdorff distance,
essentially by design. -/
lemma Hausdorff_dist_optimal {α : Type u} [metric_space α] [compact_space α] [nonempty α]
{β : Type v} [metric_space β] [compact_space β] [nonempty β] :
Hausdorff_dist (range (optimal_GH_injl α β)) (range (optimal_GH_injr α β)) = GH_dist α β :=
begin
inhabit α, inhabit β,
/- we only need to check the inequality `≤`, as the other one follows from the previous lemma.
As the Gromov-Hausdorff distance is an infimum, we need to check that the Hausdorff distance
in the optimal coupling is smaller than the Hausdorff distance of any coupling.
First, we check this for couplings which already have small Hausdorff distance: in this
case, the induced "distance" on `α ⊕ β` belongs to the candidates family introduced in the
definition of the optimal coupling, and the conclusion follows from the optimality
of the optimal coupling within this family.
-/
have A : ∀p q : nonempty_compacts (ℓ_infty_ℝ), ⟦p⟧ = to_GH_space α → ⟦q⟧ = to_GH_space β →
Hausdorff_dist (p.val) (q.val) < diam (univ : set α) + 1 + diam (univ : set β) →
Hausdorff_dist (range (optimal_GH_injl α β)) (range (optimal_GH_injr α β)) ≤ Hausdorff_dist (p.val) (q.val),
{ assume p q hp hq bound,
rcases eq_to_GH_space_iff.1 hp with ⟨Φ, ⟨Φisom, Φrange⟩⟩,
rcases eq_to_GH_space_iff.1 hq with ⟨Ψ, ⟨Ψisom, Ψrange⟩⟩,
have I : diam (range Φ ∪ range Ψ) ≤ 2 * diam (univ : set α) + 1 + 2 * diam (univ : set β),
{ rcases exists_mem_of_nonempty α with ⟨xα, _⟩,
have : ∃y ∈ range Ψ, dist (Φ xα) y < diam (univ : set α) + 1 + diam (univ : set β),
{ rw Ψrange,
have : Φ xα ∈ p.val := Φrange ▸ mem_range_self _,
exact exists_dist_lt_of_Hausdorff_dist_lt this bound
(Hausdorff_edist_ne_top_of_nonempty_of_bounded p.2.1 q.2.1 p.2.2.bounded q.2.2.bounded) },
rcases this with ⟨y, hy, dy⟩,
rcases mem_range.1 hy with ⟨z, hzy⟩,
rw ← hzy at dy,
have DΦ : diam (range Φ) = diam (univ : set α) := Φisom.diam_range,
have DΨ : diam (range Ψ) = diam (univ : set β) := Ψisom.diam_range,
calc
diam (range Φ ∪ range Ψ) ≤ diam (range Φ) + dist (Φ xα) (Ψ z) + diam (range Ψ) :
diam_union (mem_range_self _) (mem_range_self _)
... ≤ diam (univ : set α) + (diam (univ : set α) + 1 + diam (univ : set β)) + diam (univ : set β) :
by { rw [DΦ, DΨ], apply add_le_add (add_le_add (le_refl _) (le_of_lt dy)) (le_refl _) }
... = 2 * diam (univ : set α) + 1 + 2 * diam (univ : set β) : by ring },
let f : α ⊕ β → ℓ_infty_ℝ := λx, match x with | inl y := Φ y | inr z := Ψ z end,
let F : (α ⊕ β) × (α ⊕ β) → ℝ := λp, dist (f p.1) (f p.2),
-- check that the induced "distance" is a candidate
have Fgood : F ∈ candidates α β,
{ simp only [candidates, forall_const, and_true, add_comm, eq_self_iff_true, dist_eq_zero,
and_self, set.mem_set_of_eq],
repeat {split},
{ exact λx y, calc
F (inl x, inl y) = dist (Φ x) (Φ y) : rfl
... = dist x y : Φisom.dist_eq x y },
{ exact λx y, calc
F (inr x, inr y) = dist (Ψ x) (Ψ y) : rfl
... = dist x y : Ψisom.dist_eq x y },
{ exact λx y, dist_comm _ _ },
{ exact λx y z, dist_triangle _ _ _ },
{ exact λx y, calc
F (x, y) ≤ diam (range Φ ∪ range Ψ) :
begin
have A : ∀z : α ⊕ β, f z ∈ range Φ ∪ range Ψ,
{ assume z,
cases z,
{ apply mem_union_left, apply mem_range_self },
{ apply mem_union_right, apply mem_range_self } },
refine dist_le_diam_of_mem _ (A _) (A _),
rw [Φrange, Ψrange],
exact (p.2.2.union q.2.2).bounded,
end
... ≤ 2 * diam (univ : set α) + 1 + 2 * diam (univ : set β) : I } },
let Fb := candidates_b_of_candidates F Fgood,
have : Hausdorff_dist (range (optimal_GH_injl α β)) (range (optimal_GH_injr α β)) ≤ HD Fb :=
Hausdorff_dist_optimal_le_HD _ _ (candidates_b_of_candidates_mem F Fgood),
refine le_trans this (le_of_forall_le_of_dense (λr hr, _)),
have I1 : ∀x : α, infi (λy:β, Fb (inl x, inr y)) ≤ r,
{ assume x,
have : f (inl x) ∈ p.val, by { rw [← Φrange], apply mem_range_self },
rcases exists_dist_lt_of_Hausdorff_dist_lt this hr
(Hausdorff_edist_ne_top_of_nonempty_of_bounded p.2.1 q.2.1 p.2.2.bounded q.2.2.bounded)
with ⟨z, zq, hz⟩,
have : z ∈ range Ψ, by rwa [← Ψrange] at zq,
rcases mem_range.1 this with ⟨y, hy⟩,
calc infi (λy:β, Fb (inl x, inr y)) ≤ Fb (inl x, inr y) :
cinfi_le (by simpa using HD_below_aux1 0) y
... = dist (Φ x) (Ψ y) : rfl
... = dist (f (inl x)) z : by rw hy
... ≤ r : le_of_lt hz },
have I2 : ∀y : β, infi (λx:α, Fb (inl x, inr y)) ≤ r,
{ assume y,
have : f (inr y) ∈ q.val, by { rw [← Ψrange], apply mem_range_self },
rcases exists_dist_lt_of_Hausdorff_dist_lt' this hr
(Hausdorff_edist_ne_top_of_nonempty_of_bounded p.2.1 q.2.1 p.2.2.bounded q.2.2.bounded)
with ⟨z, zq, hz⟩,
have : z ∈ range Φ, by rwa [← Φrange] at zq,
rcases mem_range.1 this with ⟨x, hx⟩,
calc infi (λx:α, Fb (inl x, inr y)) ≤ Fb (inl x, inr y) :
cinfi_le (by simpa using HD_below_aux2 0) x
... = dist (Φ x) (Ψ y) : rfl
... = dist z (f (inr y)) : by rw hx
... ≤ r : le_of_lt hz },
simp [HD, csupr_le I1, csupr_le I2] },
/- Get the same inequality for any coupling. If the coupling is quite good, the desired
inequality has been proved above. If it is bad, then the inequality is obvious. -/
have B : ∀p q : nonempty_compacts (ℓ_infty_ℝ), ⟦p⟧ = to_GH_space α → ⟦q⟧ = to_GH_space β →
Hausdorff_dist (range (optimal_GH_injl α β)) (range (optimal_GH_injr α β)) ≤ Hausdorff_dist (p.val) (q.val),
{ assume p q hp hq,
by_cases h : Hausdorff_dist (p.val) (q.val) < diam (univ : set α) + 1 + diam (univ : set β),
{ exact A p q hp hq h },
{ calc Hausdorff_dist (range (optimal_GH_injl α β)) (range (optimal_GH_injr α β)) ≤ HD (candidates_b_dist α β) :
Hausdorff_dist_optimal_le_HD _ _ (candidates_b_dist_mem_candidates_b)
... ≤ diam (univ : set α) + 1 + diam (univ : set β) : HD_candidates_b_dist_le
... ≤ Hausdorff_dist (p.val) (q.val) : not_lt.1 h } },
refine le_antisymm _ _,
{ apply le_cInf,
{ refine (set.nonempty.prod _ _).image _; exact ⟨_, rfl⟩ },
{ rintro b ⟨⟨p, q⟩, ⟨hp, hq⟩, rfl⟩,
exact B p q hp hq } },
{ exact GH_dist_le_Hausdorff_dist (isometry_optimal_GH_injl α β) (isometry_optimal_GH_injr α β) }
end
/-- The Gromov-Hausdorff distance can also be realized by a coupling in `ℓ^∞(ℝ)`, by embedding
the optimal coupling through its Kuratowski embedding. -/
theorem GH_dist_eq_Hausdorff_dist (α : Type u) [metric_space α] [compact_space α] [nonempty α]
(β : Type v) [metric_space β] [compact_space β] [nonempty β] :
∃Φ : α → ℓ_infty_ℝ, ∃Ψ : β → ℓ_infty_ℝ, isometry Φ ∧ isometry Ψ ∧
GH_dist α β = Hausdorff_dist (range Φ) (range Ψ) :=
begin
let F := Kuratowski_embedding (optimal_GH_coupling α β),
let Φ := F ∘ optimal_GH_injl α β,
let Ψ := F ∘ optimal_GH_injr α β,
refine ⟨Φ, Ψ, _, _, _⟩,
{ exact (Kuratowski_embedding.isometry _).comp (isometry_optimal_GH_injl α β) },
{ exact (Kuratowski_embedding.isometry _).comp (isometry_optimal_GH_injr α β) },
{ rw [← image_univ, ← image_univ, image_comp F, image_univ, image_comp F (optimal_GH_injr α β),
image_univ, ← Hausdorff_dist_optimal],
exact (Hausdorff_dist_image (Kuratowski_embedding.isometry _)).symm },
end
-- without the next two lines, `{ exact hΦ.is_closed }` in the next
-- proof is very slow, as the `t2_space` instance is very hard to find
local attribute [instance, priority 10] order_topology.t2_space
local attribute [instance, priority 10] order_closed_topology.to_t2_space
/-- The Gromov-Hausdorff distance defines a genuine distance on the Gromov-Hausdorff space. -/
instance GH_space_metric_space : metric_space GH_space :=
{ dist_self := λx, begin
rcases exists_rep x with ⟨y, hy⟩,
refine le_antisymm _ _,
{ apply cInf_le,
{ exact ⟨0, by { rintro b ⟨⟨u, v⟩, ⟨hu, hv⟩, rfl⟩, exact Hausdorff_dist_nonneg } ⟩},
{ simp, existsi [y, y], simpa } },
{ apply le_cInf,
{ exact (nonempty.prod ⟨y, hy⟩ ⟨y, hy⟩).image _ },
{ rintro b ⟨⟨u, v⟩, ⟨hu, hv⟩, rfl⟩, exact Hausdorff_dist_nonneg } },
end,
dist_comm := λx y, begin
have A : (λ (p : nonempty_compacts ℓ_infty_ℝ × nonempty_compacts ℓ_infty_ℝ),
Hausdorff_dist ((p.fst).val) ((p.snd).val)) '' (set.prod {a | ⟦a⟧ = x} {b | ⟦b⟧ = y})
= ((λ (p : nonempty_compacts ℓ_infty_ℝ × nonempty_compacts ℓ_infty_ℝ),
Hausdorff_dist ((p.fst).val) ((p.snd).val)) ∘ prod.swap) '' (set.prod {a | ⟦a⟧ = x} {b | ⟦b⟧ = y}) :=
by { congr, funext, simp, rw Hausdorff_dist_comm },
simp only [dist, A, image_comp, prod.swap, image_swap_prod],
end,
eq_of_dist_eq_zero := λx y hxy, begin
/- To show that two spaces at zero distance are isometric, we argue that the distance
is realized by some coupling. In this coupling, the two spaces are at zero Hausdorff distance,
i.e., they coincide. Therefore, the original spaces are isometric. -/
rcases GH_dist_eq_Hausdorff_dist x.rep y.rep with ⟨Φ, Ψ, Φisom, Ψisom, DΦΨ⟩,
rw [← dist_GH_dist, hxy] at DΦΨ,
have : range Φ = range Ψ,
{ have hΦ : compact (range Φ) := compact_range Φisom.continuous,
have hΨ : compact (range Ψ) := compact_range Ψisom.continuous,
apply (Hausdorff_dist_zero_iff_eq_of_closed _ _ _).1 (DΦΨ.symm),
{ exact hΦ.is_closed },
{ exact hΨ.is_closed },
{ exact Hausdorff_edist_ne_top_of_nonempty_of_bounded (range_nonempty _)
(range_nonempty _) hΦ.bounded hΨ.bounded } },
have T : ((range Ψ) ≃ᵢ y.rep) = ((range Φ) ≃ᵢ y.rep), by rw this,
have eΨ := cast T Ψisom.isometric_on_range.symm,
have e := Φisom.isometric_on_range.trans eΨ,
rw [← x.to_GH_space_rep, ← y.to_GH_space_rep, to_GH_space_eq_to_GH_space_iff_isometric],
exact ⟨e⟩
end,
dist_triangle := λx y z, begin
/- To show the triangular inequality between `X`, `Y` and `Z`, realize an optimal coupling
between `X` and `Y` in a space `γ1`, and an optimal coupling between `Y`and `Z` in a space `γ2`.
Then, glue these metric spaces along `Y`. We get a new space `γ` in which `X` and `Y` are
optimally coupled, as well as `Y` and `Z`. Apply the triangle inequality for the Hausdorff
distance in `γ` to conclude. -/
let X := x.rep,
let Y := y.rep,
let Z := z.rep,
let γ1 := optimal_GH_coupling X Y,
let γ2 := optimal_GH_coupling Y Z,
let Φ : Y → γ1 := optimal_GH_injr X Y,
have hΦ : isometry Φ := isometry_optimal_GH_injr X Y,
let Ψ : Y → γ2 := optimal_GH_injl Y Z,
have hΨ : isometry Ψ := isometry_optimal_GH_injl Y Z,
let γ := glue_space hΦ hΨ,
letI : metric_space γ := metric.metric_space_glue_space hΦ hΨ,
have Comm : (to_glue_l hΦ hΨ) ∘ (optimal_GH_injr X Y) = (to_glue_r hΦ hΨ) ∘ (optimal_GH_injl Y Z) :=
to_glue_commute hΦ hΨ,
calc dist x z = dist (to_GH_space X) (to_GH_space Z) :
by rw [x.to_GH_space_rep, z.to_GH_space_rep]
... ≤ Hausdorff_dist (range ((to_glue_l hΦ hΨ) ∘ (optimal_GH_injl X Y)))
(range ((to_glue_r hΦ hΨ) ∘ (optimal_GH_injr Y Z))) :
GH_dist_le_Hausdorff_dist
((to_glue_l_isometry hΦ hΨ).comp (isometry_optimal_GH_injl X Y))
((to_glue_r_isometry hΦ hΨ).comp (isometry_optimal_GH_injr Y Z))
... ≤ Hausdorff_dist (range ((to_glue_l hΦ hΨ) ∘ (optimal_GH_injl X Y)))
(range ((to_glue_l hΦ hΨ) ∘ (optimal_GH_injr X Y)))
+ Hausdorff_dist (range ((to_glue_l hΦ hΨ) ∘ (optimal_GH_injr X Y)))
(range ((to_glue_r hΦ hΨ) ∘ (optimal_GH_injr Y Z))) :
begin
refine Hausdorff_dist_triangle (Hausdorff_edist_ne_top_of_nonempty_of_bounded
(range_nonempty _) (range_nonempty _) _ _),
{ exact (compact_range (isometry.continuous ((to_glue_l_isometry hΦ hΨ).comp
(isometry_optimal_GH_injl X Y)))).bounded },
{ exact (compact_range (isometry.continuous ((to_glue_l_isometry hΦ hΨ).comp
(isometry_optimal_GH_injr X Y)))).bounded }
end
... = Hausdorff_dist ((to_glue_l hΦ hΨ) '' (range (optimal_GH_injl X Y)))
((to_glue_l hΦ hΨ) '' (range (optimal_GH_injr X Y)))
+ Hausdorff_dist ((to_glue_r hΦ hΨ) '' (range (optimal_GH_injl Y Z)))
((to_glue_r hΦ hΨ) '' (range (optimal_GH_injr Y Z))) :
by simp only [eq.symm range_comp, Comm, eq_self_iff_true, add_right_inj]
... = Hausdorff_dist (range (optimal_GH_injl X Y))
(range (optimal_GH_injr X Y))
+ Hausdorff_dist (range (optimal_GH_injl Y Z))
(range (optimal_GH_injr Y Z)) :
by rw [Hausdorff_dist_image (to_glue_l_isometry hΦ hΨ),
Hausdorff_dist_image (to_glue_r_isometry hΦ hΨ)]
... = dist (to_GH_space X) (to_GH_space Y) + dist (to_GH_space Y) (to_GH_space Z) :
by rw [Hausdorff_dist_optimal, Hausdorff_dist_optimal, GH_dist, GH_dist]
... = dist x y + dist y z:
by rw [x.to_GH_space_rep, y.to_GH_space_rep, z.to_GH_space_rep]
end }
end GH_space --section
end Gromov_Hausdorff
/-- In particular, nonempty compacts of a metric space map to `GH_space`. We register this
in the topological_space namespace to take advantage of the notation `p.to_GH_space`. -/
definition topological_space.nonempty_compacts.to_GH_space {α : Type u} [metric_space α]
(p : nonempty_compacts α) : Gromov_Hausdorff.GH_space := Gromov_Hausdorff.to_GH_space p.val
open topological_space
namespace Gromov_Hausdorff
section nonempty_compacts
variables {α : Type u} [metric_space α]
theorem GH_dist_le_nonempty_compacts_dist (p q : nonempty_compacts α) :
dist p.to_GH_space q.to_GH_space ≤ dist p q :=
begin
have ha : isometry (coe : p.val → α) := isometry_subtype_coe,
have hb : isometry (coe : q.val → α) := isometry_subtype_coe,
have A : dist p q = Hausdorff_dist p.val q.val := rfl,
have I : p.val = range (coe : p.val → α), by simp,
have J : q.val = range (coe : q.val → α), by simp,
rw [I, J] at A,
rw A,
exact GH_dist_le_Hausdorff_dist ha hb
end
lemma to_GH_space_lipschitz :
lipschitz_with 1 (nonempty_compacts.to_GH_space : nonempty_compacts α → GH_space) :=
lipschitz_with.mk_one GH_dist_le_nonempty_compacts_dist
lemma to_GH_space_continuous :
continuous (nonempty_compacts.to_GH_space : nonempty_compacts α → GH_space) :=
to_GH_space_lipschitz.continuous
end nonempty_compacts
section
/- In this section, we show that if two metric spaces are isometric up to `ε₂`, then their
Gromov-Hausdorff distance is bounded by `ε₂ / 2`. More generally, if there are subsets which are
`ε₁`-dense and `ε₃`-dense in two spaces, and isometric up to `ε₂`, then the Gromov-Hausdorff distance
between the spaces is bounded by `ε₁ + ε₂/2 + ε₃`. For this, we construct a suitable coupling between
the two spaces, by gluing them (approximately) along the two matching subsets. -/
variables {α : Type u} [metric_space α] [compact_space α] [nonempty α]
{β : Type v} [metric_space β] [compact_space β] [nonempty β]
-- we want to ignore these instances in the following theorem
local attribute [instance, priority 10] sum.topological_space sum.uniform_space
/-- If there are subsets which are `ε₁`-dense and `ε₃`-dense in two spaces, and
isometric up to `ε₂`, then the Gromov-Hausdorff distance between the spaces is bounded by
`ε₁ + ε₂/2 + ε₃`. -/
theorem GH_dist_le_of_approx_subsets {s : set α} (Φ : s → β) {ε₁ ε₂ ε₃ : ℝ}
(hs : ∀x : α, ∃y ∈ s, dist x y ≤ ε₁) (hs' : ∀x : β, ∃y : s, dist x (Φ y) ≤ ε₃)
(H : ∀x y : s, abs (dist x y - dist (Φ x) (Φ y)) ≤ ε₂) :
GH_dist α β ≤ ε₁ + ε₂ / 2 + ε₃ :=
begin
refine real.le_of_forall_epsilon_le (λδ δ0, _),
rcases exists_mem_of_nonempty α with ⟨xα, _⟩,
rcases hs xα with ⟨xs, hxs, Dxs⟩,
have sne : s.nonempty := ⟨xs, hxs⟩,
letI : nonempty s := sne.to_subtype,
have : 0 ≤ ε₂ := le_trans (abs_nonneg _) (H ⟨xs, hxs⟩ ⟨xs, hxs⟩),
have : ∀ p q : s, abs (dist p q - dist (Φ p) (Φ q)) ≤ 2 * (ε₂/2 + δ) := λp q, calc
abs (dist p q - dist (Φ p) (Φ q)) ≤ ε₂ : H p q
... ≤ 2 * (ε₂/2 + δ) : by linarith,
-- glue `α` and `β` along the almost matching subsets
letI : metric_space (α ⊕ β) := glue_metric_approx (λ x:s, (x:α)) (λx, Φ x) (ε₂/2 + δ) (by linarith) this,
let Fl := @sum.inl α β,
let Fr := @sum.inr α β,
have Il : isometry Fl := isometry_emetric_iff_metric.2 (λx y, rfl),
have Ir : isometry Fr := isometry_emetric_iff_metric.2 (λx y, rfl),
/- The proof goes as follows : the `GH_dist` is bounded by the Hausdorff distance of the images in the
coupling, which is bounded (using the triangular inequality) by the sum of the Hausdorff distances
of `α` and `s` (in the coupling or, equivalently in the original space), of `s` and `Φ s`, and of
`Φ s` and `β` (in the coupling or, equivalently, in the original space). The first term is bounded
by `ε₁`, by `ε₁`-density. The third one is bounded by `ε₃`. And the middle one is bounded by `ε₂/2`
as in the coupling the points `x` and `Φ x` are at distance `ε₂/2` by construction of the coupling
(in fact `ε₂/2 + δ` where `δ` is an arbitrarily small positive constant where positivity is used
to ensure that the coupling is really a metric space and not a premetric space on `α ⊕ β`). -/
have : GH_dist α β ≤ Hausdorff_dist (range Fl) (range Fr) :=
GH_dist_le_Hausdorff_dist Il Ir,
have : Hausdorff_dist (range Fl) (range Fr) ≤ Hausdorff_dist (range Fl) (Fl '' s)
+ Hausdorff_dist (Fl '' s) (range Fr),
{ have B : bounded (range Fl) := (compact_range Il.continuous).bounded,
exact Hausdorff_dist_triangle (Hausdorff_edist_ne_top_of_nonempty_of_bounded
(range_nonempty _) (sne.image _) B (B.subset (image_subset_range _ _))) },
have : Hausdorff_dist (Fl '' s) (range Fr) ≤ Hausdorff_dist (Fl '' s) (Fr '' (range Φ))
+ Hausdorff_dist (Fr '' (range Φ)) (range Fr),
{ have B : bounded (range Fr) := (compact_range Ir.continuous).bounded,
exact Hausdorff_dist_triangle' (Hausdorff_edist_ne_top_of_nonempty_of_bounded
((range_nonempty _).image _) (range_nonempty _)
(bounded.subset (image_subset_range _ _) B) B) },
have : Hausdorff_dist (range Fl) (Fl '' s) ≤ ε₁,
{ rw [← image_univ, Hausdorff_dist_image Il],
have : 0 ≤ ε₁ := le_trans dist_nonneg Dxs,
refine Hausdorff_dist_le_of_mem_dist this (λx hx, hs x)
(λx hx, ⟨x, mem_univ _, by simpa⟩) },
have : Hausdorff_dist (Fl '' s) (Fr '' (range Φ)) ≤ ε₂/2 + δ,
{ refine Hausdorff_dist_le_of_mem_dist (by linarith) _ _,
{ assume x' hx',
rcases (set.mem_image _ _ _).1 hx' with ⟨x, ⟨x_in_s, xx'⟩⟩,
rw ← xx',
use [Fr (Φ ⟨x, x_in_s⟩), mem_image_of_mem Fr (mem_range_self _)],
exact le_of_eq (glue_dist_glued_points (λ x:s, (x:α)) Φ (ε₂/2 + δ) ⟨x, x_in_s⟩) },
{ assume x' hx',
rcases (set.mem_image _ _ _).1 hx' with ⟨y, ⟨y_in_s', yx'⟩⟩,
rcases mem_range.1 y_in_s' with ⟨x, xy⟩,
use [Fl x, mem_image_of_mem _ x.2],
rw [← yx', ← xy, dist_comm],
exact le_of_eq (glue_dist_glued_points (@subtype.val α s) Φ (ε₂/2 + δ) x) } },
have : Hausdorff_dist (Fr '' (range Φ)) (range Fr) ≤ ε₃,
{ rw [← @image_univ _ _ Fr, Hausdorff_dist_image Ir],
rcases exists_mem_of_nonempty β with ⟨xβ, _⟩,
rcases hs' xβ with ⟨xs', Dxs'⟩,
have : 0 ≤ ε₃ := le_trans dist_nonneg Dxs',
refine Hausdorff_dist_le_of_mem_dist this (λx hx, ⟨x, mem_univ _, by simpa⟩) (λx _, _),
rcases hs' x with ⟨y, Dy⟩,
exact ⟨Φ y, mem_range_self _, Dy⟩ },
linarith
end
end --section
/-- The Gromov-Hausdorff space is second countable. -/
instance second_countable : second_countable_topology GH_space :=
begin
refine second_countable_of_countable_discretization (λδ δpos, _),
let ε := (2/5) * δ,
have εpos : 0 < ε := mul_pos (by norm_num) δpos,
have : ∀p:GH_space, ∃s : set (p.rep), finite s ∧ (univ ⊆ (⋃x∈s, ball x ε)) :=
λp, by simpa using finite_cover_balls_of_compact (@compact_univ p.rep _ _) εpos,
-- for each `p`, `s p` is a finite `ε`-dense subset of `p` (or rather the metric space
-- `p.rep` representing `p`)
choose s hs using this,
have : ∀p:GH_space, ∀t:set (p.rep), finite t → ∃n:ℕ, ∃e:equiv t (fin n), true,
{ assume p t ht,
letI : fintype t := finite.fintype ht,
rcases fintype.exists_equiv_fin t with ⟨n, hn⟩,
rcases hn with e,
exact ⟨n, e, trivial⟩ },
choose N e hne using this,
-- cardinality of the nice finite subset `s p` of `p.rep`, called `N p`
let N := λp:GH_space, N p (s p) (hs p).1,
-- equiv from `s p`, a nice finite subset of `p.rep`, to `fin (N p)`, called `E p`
let E := λp:GH_space, e p (s p) (hs p).1,
-- A function `F` associating to `p : GH_space` the data of all distances between points
-- in the `ε`-dense set `s p`.
let F : GH_space → Σn:ℕ, (fin n → fin n → ℤ) :=
λp, ⟨N p, λa b, floor (ε⁻¹ * dist ((E p).symm a) ((E p).symm b))⟩,
refine ⟨_, by apply_instance, F, λp q hpq, _⟩,
/- As the target space of F is countable, it suffices to show that two points
`p` and `q` with `F p = F q` are at distance `≤ δ`.
For this, we construct a map `Φ` from `s p ⊆ p.rep` (representing `p`)
to `q.rep` (representing `q`) which is almost an isometry on `s p`, and
with image `s q`. For this, we compose the identification of `s p` with `fin (N p)`
and the inverse of the identification of `s q` with `fin (N q)`. Together with
the fact that `N p = N q`, this constructs `Ψ` between `s p` and `s q`, and then
composing with the canonical inclusion we get `Φ`. -/
have Npq : N p = N q := (sigma.mk.inj_iff.1 hpq).1,
let Ψ : s p → s q := λx, (E q).symm (fin.cast Npq ((E p) x)),
let Φ : s p → q.rep := λx, Ψ x,
-- Use the almost isometry `Φ` to show that `p.rep` and `q.rep`
-- are within controlled Gromov-Hausdorff distance.
have main : GH_dist p.rep q.rep ≤ ε + ε/2 + ε,
{ refine GH_dist_le_of_approx_subsets Φ _ _ _,
show ∀x : p.rep, ∃ (y : p.rep) (H : y ∈ s p), dist x y ≤ ε,
{ -- by construction, `s p` is `ε`-dense
assume x,
have : x ∈ ⋃y∈(s p), ball y ε := (hs p).2 (mem_univ _),
rcases mem_bUnion_iff.1 this with ⟨y, ys, hy⟩,
exact ⟨y, ys, le_of_lt hy⟩ },
show ∀x : q.rep, ∃ (z : s p), dist x (Φ z) ≤ ε,
{ -- by construction, `s q` is `ε`-dense, and it is the range of `Φ`
assume x,
have : x ∈ ⋃y∈(s q), ball y ε := (hs q).2 (mem_univ _),
rcases mem_bUnion_iff.1 this with ⟨y, ys, hy⟩,
let i := ((E q) ⟨y, ys⟩).1,
let hi := ((E q) ⟨y, ys⟩).2,
have ihi_eq : (⟨i, hi⟩ : fin (N q)) = (E q) ⟨y, ys⟩, by rw fin.ext_iff,
have hiq : i < N q := hi,
have hip : i < N p, { rwa Npq.symm at hiq },
let z := (E p).symm ⟨i, hip⟩,
use z,
have C1 : (E p) z = ⟨i, hip⟩ := (E p).apply_symm_apply ⟨i, hip⟩,
have C2 : fin.cast Npq ⟨i, hip⟩ = ⟨i, hi⟩ := rfl,
have C3 : (E q).symm ⟨i, hi⟩ = ⟨y, ys⟩, by { rw ihi_eq, exact (E q).symm_apply_apply ⟨y, ys⟩ },
have : Φ z = y :=
by { simp only [Φ, Ψ], rw [C1, C2, C3], refl },
rw this,
exact le_of_lt hy },
show ∀x y : s p, abs (dist x y - dist (Φ x) (Φ y)) ≤ ε,
{ /- the distance between `x` and `y` is encoded in `F p`, and the distance between
`Φ x` and `Φ y` (two points of `s q`) is encoded in `F q`, all this up to `ε`.
As `F p = F q`, the distances are almost equal. -/
assume x y,
have : dist (Φ x) (Φ y) = dist (Ψ x) (Ψ y) := rfl,
rw this,
-- introduce `i`, that codes both `x` and `Φ x` in `fin (N p) = fin (N q)`
let i := ((E p) x).1,
have hip : i < N p := ((E p) x).2,
have hiq : i < N q, by rwa Npq at hip,
have i' : i = ((E q) (Ψ x)).1, by { simp [Ψ] },
-- introduce `j`, that codes both `y` and `Φ y` in `fin (N p) = fin (N q)`
let j := ((E p) y).1,
have hjp : j < N p := ((E p) y).2,
have hjq : j < N q, by rwa Npq at hjp,
have j' : j = ((E q) (Ψ y)).1, by { simp [Ψ] },
-- Express `dist x y` in terms of `F p`
have : (F p).2 ((E p) x) ((E p) y) = floor (ε⁻¹ * dist x y),
by simp only [F, (E p).symm_apply_apply],
have Ap : (F p).2 ⟨i, hip⟩ ⟨j, hjp⟩ = floor (ε⁻¹ * dist x y),
by { rw ← this, congr; apply (fin.ext_iff _ _).2; refl },
-- Express `dist (Φ x) (Φ y)` in terms of `F q`
have : (F q).2 ((E q) (Ψ x)) ((E q) (Ψ y)) = floor (ε⁻¹ * dist (Ψ x) (Ψ y)),
by simp only [F, (E q).symm_apply_apply],
have Aq : (F q).2 ⟨i, hiq⟩ ⟨j, hjq⟩ = floor (ε⁻¹ * dist (Ψ x) (Ψ y)),
by { rw ← this, congr; apply (fin.ext_iff _ _).2; [exact i', exact j'] },
-- use the equality between `F p` and `F q` to deduce that the distances have equal
-- integer parts
have : (F p).2 ⟨i, hip⟩ ⟨j, hjp⟩ = (F q).2 ⟨i, hiq⟩ ⟨j, hjq⟩,
{ -- we want to `subst hpq` where `hpq : F p = F q`, except that `subst` only works
-- with a constant, so replace `F q` (and everything that depends on it) by a constant `f`
-- then `subst`
revert hiq hjq,
change N q with (F q).1,
generalize_hyp : F q = f at hpq ⊢,
subst hpq,
intros,
refl },
rw [Ap, Aq] at this,
-- deduce that the distances coincide up to `ε`, by a straightforward computation
-- that should be automated
have I := calc
abs (ε⁻¹) * abs (dist x y - dist (Ψ x) (Ψ y)) =
abs (ε⁻¹ * (dist x y - dist (Ψ x) (Ψ y))) : (abs_mul _ _).symm
... = abs ((ε⁻¹ * dist x y) - (ε⁻¹ * dist (Ψ x) (Ψ y))) : by { congr, ring }
... ≤ 1 : le_of_lt (abs_sub_lt_one_of_floor_eq_floor this),
calc
abs (dist x y - dist (Ψ x) (Ψ y)) = (ε * ε⁻¹) * abs (dist x y - dist (Ψ x) (Ψ y)) :
by rw [mul_inv_cancel (ne_of_gt εpos), one_mul]
... = ε * (abs (ε⁻¹) * abs (dist x y - dist (Ψ x) (Ψ y))) :
by rw [abs_of_nonneg (le_of_lt (inv_pos.2 εpos)), mul_assoc]
... ≤ ε * 1 : mul_le_mul_of_nonneg_left I (le_of_lt εpos)
... = ε : mul_one _ } },
calc dist p q = GH_dist (p.rep) (q.rep) : dist_GH_dist p q
... ≤ ε + ε/2 + ε : main
... = δ : by { simp [ε], ring }
end
/-- Compactness criterion: a closed set of compact metric spaces is compact if the spaces have
a uniformly bounded diameter, and for all `ε` the number of balls of radius `ε` required
to cover the spaces is uniformly bounded. This is an equivalence, but we only prove the
interesting direction that these conditions imply compactness. -/
lemma totally_bounded {t : set GH_space} {C : ℝ} {u : ℕ → ℝ} {K : ℕ → ℕ}
(ulim : tendsto u at_top (𝓝 0))
(hdiam : ∀p ∈ t, diam (univ : set (GH_space.rep p)) ≤ C)
(hcov : ∀p ∈ t, ∀n:ℕ, ∃s : set (GH_space.rep p), cardinal.mk s ≤ K n ∧ univ ⊆ ⋃x∈s, ball x (u n)) :
totally_bounded t :=
begin
/- Let `δ>0`, and `ε = δ/5`. For each `p`, we construct a finite subset `s p` of `p`, which
is `ε`-dense and has cardinality at most `K n`. Encoding the mutual distances of points in `s p`,
up to `ε`, we will get a map `F` associating to `p` finitely many data, and making it possible to
reconstruct `p` up to `ε`. This is enough to prove total boundedness. -/
refine metric.totally_bounded_of_finite_discretization (λδ δpos, _),
let ε := (1/5) * δ,
have εpos : 0 < ε := mul_pos (by norm_num) δpos,
-- choose `n` for which `u n < ε`
rcases metric.tendsto_at_top.1 ulim ε εpos with ⟨n, hn⟩,
have u_le_ε : u n ≤ ε,
{ have := hn n (le_refl _),
simp only [real.dist_eq, add_zero, sub_eq_add_neg, neg_zero] at this,
exact le_of_lt (lt_of_le_of_lt (le_abs_self _) this) },
-- construct a finite subset `s p` of `p` which is `ε`-dense and has cardinal `≤ K n`
have : ∀p:GH_space, ∃s : set (p.rep), ∃N ≤ K n, ∃E : equiv s (fin N),
p ∈ t → univ ⊆ ⋃x∈s, ball x (u n),
{ assume p,
by_cases hp : p ∉ t,
{ have : nonempty (equiv (∅ : set (p.rep)) (fin 0)),
{ rw ← fintype.card_eq, simp },
use [∅, 0, bot_le, choice (this)] },
{ rcases hcov _ (set.not_not_mem.1 hp) n with ⟨s, ⟨scard, scover⟩⟩,
rcases cardinal.lt_omega.1 (lt_of_le_of_lt scard (cardinal.nat_lt_omega _)) with ⟨N, hN⟩,
rw [hN, cardinal.nat_cast_le] at scard,
have : cardinal.mk s = cardinal.mk (fin N), by rw [hN, cardinal.mk_fin],
cases quotient.exact this with E,
use [s, N, scard, E],
simp [hp, scover] } },
choose s N hN E hs using this,
-- Define a function `F` taking values in a finite type and associating to `p` enough data
-- to reconstruct it up to `ε`, namely the (discretized) distances between elements of `s p`.
let M := (floor (ε⁻¹ * max C 0)).to_nat,
let F : GH_space → (Σk:fin ((K n).succ), (fin k → fin k → fin (M.succ))) :=
λp, ⟨⟨N p, lt_of_le_of_lt (hN p) (nat.lt_succ_self _)⟩,
λa b, ⟨min M (floor (ε⁻¹ * dist ((E p).symm a) ((E p).symm b))).to_nat,
lt_of_le_of_lt ( min_le_left _ _) (nat.lt_succ_self _) ⟩ ⟩,
refine ⟨_, by apply_instance, (λp, F p), _⟩,
-- It remains to show that if `F p = F q`, then `p` and `q` are `ε`-close
rintros ⟨p, pt⟩ ⟨q, qt⟩ hpq,
have Npq : N p = N q := (fin.ext_iff _ _).1 (sigma.mk.inj_iff.1 hpq).1,
let Ψ : s p → s q := λx, (E q).symm (fin.cast Npq ((E p) x)),
let Φ : s p → q.rep := λx, Ψ x,
have main : GH_dist (p.rep) (q.rep) ≤ ε + ε/2 + ε,
{ -- to prove the main inequality, argue that `s p` is `ε`-dense in `p`, and `s q` is `ε`-dense
-- in `q`, and `s p` and `s q` are almost isometric. Then closeness follows
-- from `GH_dist_le_of_approx_subsets`
refine GH_dist_le_of_approx_subsets Φ _ _ _,
show ∀x : p.rep, ∃ (y : p.rep) (H : y ∈ s p), dist x y ≤ ε,
{ -- by construction, `s p` is `ε`-dense
assume x,
have : x ∈ ⋃y∈(s p), ball y (u n) := (hs p pt) (mem_univ _),
rcases mem_bUnion_iff.1 this with ⟨y, ys, hy⟩,
exact ⟨y, ys, le_trans (le_of_lt hy) u_le_ε⟩ },
show ∀x : q.rep, ∃ (z : s p), dist x (Φ z) ≤ ε,
{ -- by construction, `s q` is `ε`-dense, and it is the range of `Φ`
assume x,
have : x ∈ ⋃y∈(s q), ball y (u n) := (hs q qt) (mem_univ _),
rcases mem_bUnion_iff.1 this with ⟨y, ys, hy⟩,
let i := ((E q) ⟨y, ys⟩).1,
let hi := ((E q) ⟨y, ys⟩).2,
have ihi_eq : (⟨i, hi⟩ : fin (N q)) = (E q) ⟨y, ys⟩, by rw fin.ext_iff,
have hiq : i < N q := hi,
have hip : i < N p, { rwa Npq.symm at hiq },
let z := (E p).symm ⟨i, hip⟩,
use z,
have C1 : (E p) z = ⟨i, hip⟩ := (E p).apply_symm_apply ⟨i, hip⟩,
have C2 : fin.cast Npq ⟨i, hip⟩ = ⟨i, hi⟩ := rfl,
have C3 : (E q).symm ⟨i, hi⟩ = ⟨y, ys⟩, by { rw ihi_eq, exact (E q).symm_apply_apply ⟨y, ys⟩ },
have : Φ z = y :=
by { simp only [Φ, Ψ], rw [C1, C2, C3], refl },
rw this,
exact le_trans (le_of_lt hy) u_le_ε },
show ∀x y : s p, abs (dist x y - dist (Φ x) (Φ y)) ≤ ε,
{ /- the distance between `x` and `y` is encoded in `F p`, and the distance between
`Φ x` and `Φ y` (two points of `s q`) is encoded in `F q`, all this up to `ε`.
As `F p = F q`, the distances are almost equal. -/
assume x y,
have : dist (Φ x) (Φ y) = dist (Ψ x) (Ψ y) := rfl,
rw this,
-- introduce `i`, that codes both `x` and `Φ x` in `fin (N p) = fin (N q)`
let i := ((E p) x).1,
have hip : i < N p := ((E p) x).2,
have hiq : i < N q, by rwa Npq at hip,
have i' : i = ((E q) (Ψ x)).1, by { simp [Ψ] },
-- introduce `j`, that codes both `y` and `Φ y` in `fin (N p) = fin (N q)`
let j := ((E p) y).1,
have hjp : j < N p := ((E p) y).2,
have hjq : j < N q, by rwa Npq at hjp,
have j' : j = ((E q) (Ψ y)).1, by { simp [Ψ] },
-- Express `dist x y` in terms of `F p`
have Ap : ((F p).2 ⟨i, hip⟩ ⟨j, hjp⟩).1 = (floor (ε⁻¹ * dist x y)).to_nat := calc
((F p).2 ⟨i, hip⟩ ⟨j, hjp⟩).1 = ((F p).2 ((E p) x) ((E p) y)).1 :
by { congr; apply (fin.ext_iff _ _).2; refl }
... = min M (floor (ε⁻¹ * dist x y)).to_nat :
by simp only [F, (E p).symm_apply_apply]
... = (floor (ε⁻¹ * dist x y)).to_nat :
begin
refine min_eq_right (int.to_nat_le_to_nat (floor_mono _)),
refine mul_le_mul_of_nonneg_left (le_trans _ (le_max_left _ _)) (le_of_lt (inv_pos.2 εpos)),
change dist (x : p.rep) y ≤ C,
refine le_trans (dist_le_diam_of_mem compact_univ.bounded (mem_univ _) (mem_univ _)) _,
exact hdiam p pt
end,
-- Express `dist (Φ x) (Φ y)` in terms of `F q`
have Aq : ((F q).2 ⟨i, hiq⟩ ⟨j, hjq⟩).1 = (floor (ε⁻¹ * dist (Ψ x) (Ψ y))).to_nat := calc
((F q).2 ⟨i, hiq⟩ ⟨j, hjq⟩).1 = ((F q).2 ((E q) (Ψ x)) ((E q) (Ψ y))).1 :
by { congr; apply (fin.ext_iff _ _).2; [exact i', exact j'] }
... = min M (floor (ε⁻¹ * dist (Ψ x) (Ψ y))).to_nat :
by simp only [F, (E q).symm_apply_apply]
... = (floor (ε⁻¹ * dist (Ψ x) (Ψ y))).to_nat :
begin
refine min_eq_right (int.to_nat_le_to_nat (floor_mono _)),
refine mul_le_mul_of_nonneg_left (le_trans _ (le_max_left _ _)) (le_of_lt (inv_pos.2 εpos)),
change dist (Ψ x : q.rep) (Ψ y) ≤ C,
refine le_trans (dist_le_diam_of_mem compact_univ.bounded (mem_univ _) (mem_univ _)) _,
exact hdiam q qt
end,
-- use the equality between `F p` and `F q` to deduce that the distances have equal
-- integer parts
have : ((F p).2 ⟨i, hip⟩ ⟨j, hjp⟩).1 = ((F q).2 ⟨i, hiq⟩ ⟨j, hjq⟩).1,
{ -- we want to `subst hpq` where `hpq : F p = F q`, except that `subst` only works
-- with a constant, so replace `F q` (and everything that depends on it) by a constant `f`
-- then `subst`
revert hiq hjq,
change N q with (F q).1,
generalize_hyp : F q = f at hpq ⊢,
subst hpq,
intros,
refl },
have : floor (ε⁻¹ * dist x y) = floor (ε⁻¹ * dist (Ψ x) (Ψ y)),
{ rw [Ap, Aq] at this,
have D : 0 ≤ floor (ε⁻¹ * dist x y) :=
floor_nonneg.2 (mul_nonneg (le_of_lt (inv_pos.2 εpos)) dist_nonneg),
have D' : floor (ε⁻¹ * dist (Ψ x) (Ψ y)) ≥ 0 :=
floor_nonneg.2 (mul_nonneg (le_of_lt (inv_pos.2 εpos)) dist_nonneg),
rw [← int.to_nat_of_nonneg D, ← int.to_nat_of_nonneg D', this] },
-- deduce that the distances coincide up to `ε`, by a straightforward computation
-- that should be automated
have I := calc
abs (ε⁻¹) * abs (dist x y - dist (Ψ x) (Ψ y)) =
abs (ε⁻¹ * (dist x y - dist (Ψ x) (Ψ y))) : (abs_mul _ _).symm
... = abs ((ε⁻¹ * dist x y) - (ε⁻¹ * dist (Ψ x) (Ψ y))) : by { congr, ring }
... ≤ 1 : le_of_lt (abs_sub_lt_one_of_floor_eq_floor this),
calc
abs (dist x y - dist (Ψ x) (Ψ y)) = (ε * ε⁻¹) * abs (dist x y - dist (Ψ x) (Ψ y)) :
by rw [mul_inv_cancel (ne_of_gt εpos), one_mul]
... = ε * (abs (ε⁻¹) * abs (dist x y - dist (Ψ x) (Ψ y))) :
by rw [abs_of_nonneg (le_of_lt (inv_pos.2 εpos)), mul_assoc]
... ≤ ε * 1 : mul_le_mul_of_nonneg_left I (le_of_lt εpos)
... = ε : mul_one _ } },
calc dist p q = GH_dist (p.rep) (q.rep) : dist_GH_dist p q
... ≤ ε + ε/2 + ε : main
... = δ/2 : by { simp [ε], ring }
... < δ : half_lt_self δpos
end
section complete
/- We will show that a sequence `u n` of compact metric spaces satisfying
`dist (u n) (u (n+1)) < 1/2^n` converges, which implies completeness of the Gromov-Hausdorff space.
We need to exhibit the limiting compact metric space. For this, start from
a sequence `X n` of representatives of `u n`, and glue in an optimal way `X n` to `X (n+1)`
for all `n`, in a common metric space. Formally, this is done as follows.
Start from `Y 0 = X 0`. Then, glue `X 0` to `X 1` in an optimal way, yielding a space
`Y 1` (with an embedding of `X 1`). Then, consider an optimal gluing of `X 1` and `X 2`, and
glue it to `Y 1` along their common subspace `X 1`. This gives a new space `Y 2`, with an
embedding of `X 2`. Go on, to obtain a sequence of spaces `Y n`. Let `Z0` be the inductive
limit of the `Y n`, and finally let `Z` be the completion of `Z0`.
The images `X2 n` of `X n` in `Z` are at Hausdorff distance `< 1/2^n` by construction, hence they
form a Cauchy sequence for the Hausdorff distance. By completeness (of `Z`, and therefore of its
set of nonempty compact subsets), they converge to a limit `L`. This is the nonempty
compact metric space we are looking for. -/
variables (X : ℕ → Type) [∀n, metric_space (X n)] [∀n, compact_space (X n)] [∀n, nonempty (X n)]
/-- Auxiliary structure used to glue metric spaces below, recording an isometric embedding
of a type `A` in another metric space. -/
structure aux_gluing_struct (A : Type) [metric_space A] : Type 1 :=
(space : Type)
(metric : metric_space space)
(embed : A → space)
(isom : isometry embed)
/-- Auxiliary sequence of metric spaces, containing copies of `X 0`, ..., `X n`, where each
`X i` is glued to `X (i+1)` in an optimal way. The space at step `n+1` is obtained from the space
at step `n` by adding `X (n+1)`, glued in an optimal way to the `X n` already sitting there. -/
def aux_gluing (n : ℕ) : aux_gluing_struct (X n) := nat.rec_on n
{ space := X 0,
metric := by apply_instance,
embed := id,
isom := λx y, rfl }
(λn a, by letI : metric_space a.space := a.metric; exact
{ space := glue_space a.isom (isometry_optimal_GH_injl (X n) (X n.succ)),
metric := metric.metric_space_glue_space a.isom (isometry_optimal_GH_injl (X n) (X n.succ)),
embed := (to_glue_r a.isom (isometry_optimal_GH_injl (X n) (X n.succ)))
∘ (optimal_GH_injr (X n) (X n.succ)),
isom := (to_glue_r_isometry _ _).comp (isometry_optimal_GH_injr (X n) (X n.succ)) })
/-- The Gromov-Hausdorff space is complete. -/
instance : complete_space (GH_space) :=
begin
have : ∀ (n : ℕ), 0 < ((1:ℝ) / 2) ^ n, by { apply _root_.pow_pos, norm_num },
-- start from a sequence of nonempty compact metric spaces within distance `1/2^n` of each other
refine metric.complete_of_convergent_controlled_sequences (λn, (1/2)^n) this (λu hu, _),
-- `X n` is a representative of `u n`
let X := λn, (u n).rep,
-- glue them together successively in an optimal way, getting a sequence of metric spaces `Y n`
let Y := aux_gluing X,
letI : ∀n, metric_space (Y n).space := λn, (Y n).metric,
have E : ∀n:ℕ, glue_space (Y n).isom (isometry_optimal_GH_injl (X n) (X n.succ)) = (Y n.succ).space :=
λn, by { simp [Y, aux_gluing], refl },
let c := λn, cast (E n),
have ic : ∀n, isometry (c n) := λn x y, rfl,
-- there is a canonical embedding of `Y n` in `Y (n+1)`, by construction
let f : Πn, (Y n).space → (Y n.succ).space :=
λn, (c n) ∘ (to_glue_l (aux_gluing X n).isom (isometry_optimal_GH_injl (X n) (X n.succ))),
have I : ∀n, isometry (f n),
{ assume n,
apply isometry.comp,
{ assume x y, refl },
{ apply to_glue_l_isometry } },
-- consider the inductive limit `Z0` of the `Y n`, and then its completion `Z`
let Z0 := metric.inductive_limit I,
let Z := uniform_space.completion Z0,
let Φ := to_inductive_limit I,
let coeZ := (coe : Z0 → Z),
-- let `X2 n` be the image of `X n` in the space `Z`
let X2 := λn, range (coeZ ∘ (Φ n) ∘ (Y n).embed),
have isom : ∀n, isometry (coeZ ∘ (Φ n) ∘ (Y n).embed),
{ assume n,
apply isometry.comp completion.coe_isometry _,
apply isometry.comp _ (Y n).isom,
apply to_inductive_limit_isometry },
-- The Hausdorff distance of `X2 n` and `X2 (n+1)` is by construction the distance between
-- `u n` and `u (n+1)`, therefore bounded by `1/2^n`
have D2 : ∀n, Hausdorff_dist (X2 n) (X2 n.succ) < (1/2)^n,
{ assume n,
have X2n : X2 n = range ((coeZ ∘ (Φ n.succ) ∘ (c n)
∘ (to_glue_r (Y n).isom (isometry_optimal_GH_injl (X n) (X n.succ))))
∘ (optimal_GH_injl (X n) (X n.succ))),
{ change X2 n = range (coeZ ∘ (Φ n.succ) ∘ (c n)
∘ (to_glue_r (Y n).isom (isometry_optimal_GH_injl (X n) (X n.succ)))
∘ (optimal_GH_injl (X n) (X n.succ))),
simp only [X2, Φ],
rw [← to_inductive_limit_commute I],
simp only [f],
rw ← to_glue_commute },
rw range_comp at X2n,
have X2nsucc : X2 n.succ = range ((coeZ ∘ (Φ n.succ) ∘ (c n)
∘ (to_glue_r (Y n).isom (isometry_optimal_GH_injl (X n) (X n.succ))))
∘ (optimal_GH_injr (X n) (X n.succ))), by refl,
rw range_comp at X2nsucc,
rw [X2n, X2nsucc, Hausdorff_dist_image, Hausdorff_dist_optimal, ← dist_GH_dist],
{ exact hu n n n.succ (le_refl n) (le_succ n) },
{ apply isometry.comp completion.coe_isometry _,
apply isometry.comp _ ((ic n).comp (to_glue_r_isometry _ _)),
apply to_inductive_limit_isometry } },
-- consider `X2 n` as a member `X3 n` of the type of nonempty compact subsets of `Z`, which
-- is a metric space
let X3 : ℕ → nonempty_compacts Z := λn, ⟨X2 n,
⟨range_nonempty _, compact_range (isom n).continuous ⟩⟩,
-- `X3 n` is a Cauchy sequence by construction, as the successive distances are
-- bounded by `(1/2)^n`
have : cauchy_seq X3,
{ refine cauchy_seq_of_le_geometric (1/2) 1 (by norm_num) (λn, _),
rw one_mul,
exact le_of_lt (D2 n) },
-- therefore, it converges to a limit `L`
rcases cauchy_seq_tendsto_of_complete this with ⟨L, hL⟩,
-- the images of `X3 n` in the Gromov-Hausdorff space converge to the image of `L`
have M : tendsto (λn, (X3 n).to_GH_space) at_top (𝓝 L.to_GH_space) :=
tendsto.comp (to_GH_space_continuous.tendsto _) hL,
-- By construction, the image of `X3 n` in the Gromov-Hausdorff space is `u n`.
have : ∀n, (X3 n).to_GH_space = u n,
{ assume n,
rw [nonempty_compacts.to_GH_space, ← (u n).to_GH_space_rep,
to_GH_space_eq_to_GH_space_iff_isometric],
constructor,
convert (isom n).isometric_on_range.symm,
},
-- Finally, we have proved the convergence of `u n`
exact ⟨L.to_GH_space, by simpa [this] using M⟩
end
end complete--section
end Gromov_Hausdorff --namespace
|
07d12bc5c45a4caf0ce696dd0deffd33be612235 | 6b45072eb2b3db3ecaace2a7a0241ce81f815787 | /data/num/bitwise.lean | 0562c2eb35c5b1e7829e5cb52f6b436a4a474c63 | [] | no_license | avigad/library_dev | 27b47257382667b5eb7e6476c4f5b0d685dd3ddc | 9d8ac7c7798ca550874e90fed585caad030bbfac | refs/heads/master | 1,610,452,468,791 | 1,500,712,839,000 | 1,500,713,478,000 | 69,311,142 | 1 | 0 | null | 1,474,942,903,000 | 1,474,942,902,000 | null | UTF-8 | Lean | false | false | 3,074 | lean | /-
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Mario Carneiro
Bitwise operations using binary representation of integers.
-/
import .basic
namespace pos_num
def lor : pos_num → pos_num → pos_num
| 1 (bit0 q) := bit1 q
| 1 q := q
| (bit0 p) 1 := bit1 p
| p 1 := p
| (bit0 p) (bit0 q) := bit0 (lor p q)
| (bit0 p) (bit1 q) := bit1 (lor p q)
| (bit1 p) (bit0 q) := bit1 (lor p q)
| (bit1 p) (bit1 q) := bit1 (lor p q)
def land : pos_num → pos_num → num
| 1 (bit0 q) := 0
| 1 _ := 1
| (bit0 p) 1 := 0
| _ 1 := 1
| (bit0 p) (bit0 q) := num.bit0 (land p q)
| (bit0 p) (bit1 q) := num.bit0 (land p q)
| (bit1 p) (bit0 q) := num.bit0 (land p q)
| (bit1 p) (bit1 q) := num.bit1 (land p q)
def ldiff : pos_num → pos_num → num
| 1 (bit0 q) := 1
| 1 _ := 0
| (bit0 p) 1 := bit0 p
| (bit1 p) 1 := bit0 p
| (bit0 p) (bit0 q) := num.bit0 (ldiff p q)
| (bit0 p) (bit1 q) := num.bit0 (ldiff p q)
| (bit1 p) (bit0 q) := num.bit1 (ldiff p q)
| (bit1 p) (bit1 q) := num.bit0 (ldiff p q)
def lxor : pos_num → pos_num → num
| 1 1 := 0
| 1 (bit0 q) := bit1 q
| 1 (bit1 q) := bit0 q
| (bit0 p) 1 := bit1 p
| (bit1 p) 1 := bit0 p
| (bit0 p) (bit0 q) := num.bit0 (lxor p q)
| (bit0 p) (bit1 q) := num.bit1 (lxor p q)
| (bit1 p) (bit0 q) := num.bit1 (lxor p q)
| (bit1 p) (bit1 q) := num.bit0 (lxor p q)
def test_bit : pos_num → nat → bool
| 1 0 := tt
| 1 (n+1) := ff
| (bit0 p) 0 := ff
| (bit0 p) (n+1) := test_bit p n
| (bit1 p) 0 := tt
| (bit1 p) (n+1) := test_bit p n
def one_bits : pos_num → nat → list nat
| 1 d := [d]
| (bit0 p) d := one_bits p (d+1)
| (bit1 p) d := d :: one_bits p (d+1)
def shiftl (p : pos_num) : nat → pos_num
| 0 := p
| (n+1) := bit0 (shiftl n)
def shiftr : pos_num → nat → num
| p 0 := p
| 1 (n+1) := 0
| (bit0 p) (n+1) := shiftr p n
| (bit1 p) (n+1) := shiftr p n
end pos_num
namespace num
def lor : num → num → num
| 0 q := q
| p 0 := p
| (pos p) (pos q) := p.lor q
def land : num → num → num
| 0 q := 0
| p 0 := 0
| (pos p) (pos q) := p.land q
def ldiff : num → num → num
| 0 q := 0
| p 0 := p
| (pos p) (pos q) := p.ldiff q
def lxor : num → num → num
| 0 q := q
| p 0 := p
| (pos p) (pos q) := p.lxor q
def shiftl : num → nat → num
| 0 n := 0
| (pos p) n := pos (p.shiftl n)
def shiftr : num → nat → num
| 0 n := 0
| (pos p) n := p.shiftr n
def test_bit : num → nat → bool
| 0 n := ff
| (pos p) n := p.test_bit n
def one_bits : num → list nat
| 0 := []
| (pos p) := p.one_bits 0
end num
|
be73dec18cddcf61f1b54f9a8fee0fe6499978a3 | 947b78d97130d56365ae2ec264df196ce769371a | /src/Lean/Meta/MatchUtil.lean | 69375a57279be1d259fd856a586498f6f3accac4 | [
"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 | 421 | 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.Recognizers
import Lean.Meta.Basic
namespace Lean
namespace Meta
def matchEq? (e : Expr) : MetaM (Option (Expr × Expr × Expr)) := do
match e.eq? with
| r@(some _) => pure r
| none => do e ← whnf e; pure e.eq?
end Meta
end Lean
|
e3054e82bebe004d1d7b0d76c4f603a4e56fa188 | aa5a655c05e5359a70646b7154e7cac59f0b4132 | /stage0/src/Lean/PrettyPrinter/Delaborator/Builtins.lean | 0f032a2dd60ebb097ca5b3a99e36b4ad830a1cb5 | [
"Apache-2.0"
] | permissive | lambdaxymox/lean4 | ae943c960a42247e06eff25c35338268d07454cb | 278d47c77270664ef29715faab467feac8a0f446 | refs/heads/master | 1,677,891,867,340 | 1,612,500,005,000 | 1,612,500,005,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 23,878 | lean | /-
Copyright (c) 2020 Sebastian Ullrich. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sebastian Ullrich
-/
import Lean.PrettyPrinter.Delaborator.Basic
import Lean.Parser
namespace Lean.PrettyPrinter.Delaborator
open Lean.Meta
open Lean.Parser.Term
@[builtinDelab fvar]
def delabFVar : Delab := do
let Expr.fvar id _ ← getExpr | unreachable!
try
let l ← getLocalDecl id
pure $ mkIdent l.userName
catch _ =>
-- loose free variable, use internal name
pure $ mkIdent id
-- loose bound variable, use pseudo syntax
@[builtinDelab bvar]
def delabBVar : Delab := do
let Expr.bvar idx _ ← getExpr | unreachable!
pure $ mkIdent $ Name.mkSimple $ "#" ++ toString idx
@[builtinDelab mvar]
def delabMVar : Delab := do
let Expr.mvar n _ ← getExpr | unreachable!
let n := n.replacePrefix `_uniq `m
`(?$(mkIdent n))
@[builtinDelab sort]
def delabSort : Delab := do
let Expr.sort l _ ← getExpr | unreachable!
match l with
| Level.zero _ => `(Prop)
| Level.succ (Level.zero _) _ => `(Type)
| _ => match l.dec with
| some l' => `(Type $(Level.quote l' maxPrec!))
| none => `(Sort $(Level.quote l maxPrec!))
-- find shorter names for constants, in reverse to Lean.Elab.ResolveName
private def unresolveQualifiedName (ns : Name) (c : Name) : DelabM Name := do
let c' := c.replacePrefix ns Name.anonymous;
let env ← getEnv
guard $ c' != c && !c'.isAnonymous && (!c'.isAtomic || !isProtected env c)
pure c'
private def unresolveUsingNamespace (c : Name) : Name → DelabM Name
| ns@(Name.str p _ _) => unresolveQualifiedName ns c <|> unresolveUsingNamespace c p
| _ => failure
private def unresolveOpenDecls (c : Name) : List OpenDecl → DelabM Name
| [] => failure
| OpenDecl.simple ns exs :: openDecls =>
let c' := c.replacePrefix ns Name.anonymous
if c' != c && exs.elem c' then unresolveOpenDecls c openDecls
else
unresolveQualifiedName ns c <|> unresolveOpenDecls c openDecls
| OpenDecl.explicit openedId resolvedId :: openDecls =>
guard (c == resolvedId) *> pure openedId <|> unresolveOpenDecls c openDecls
-- NOTE: not a registered delaborator, as `const` is never called (see [delab] description)
def delabConst : Delab := do
let Expr.const c ls _ ← getExpr | unreachable!
let c ← if (← getPPOption getPPFullNames) then pure c else
let ctx ← read
let env ← getEnv
let as := getRevAliases env c
-- might want to use a more clever heuristic such as selecting the shortest alias...
let c := as.headD c
unresolveUsingNamespace c ctx.currNamespace <|> unresolveOpenDecls c ctx.openDecls <|> pure c
let c ← if (← getPPOption getPPPrivateNames) then pure c else pure $ (privateToUserName? c).getD c
let ppUnivs ← getPPOption getPPUniverses
if ls.isEmpty || !ppUnivs then
pure $ mkIdent c
else
`($(mkIdent c).{$[$(ls.toArray.map quote)],*})
inductive ParamKind where
| explicit
-- combines implicit params, optParams, and autoParams
| implicit (defVal : Option Expr)
/-- Return array with n-th element set to kind of n-th parameter of `e`. -/
def getParamKinds (e : Expr) : MetaM (Array ParamKind) := do
let t ← inferType e
forallTelescopeReducing t fun params _ =>
params.mapM fun param => do
let l ← getLocalDecl param.fvarId!
match l.type.getOptParamDefault? with
| some val => pure $ ParamKind.implicit val
| _ =>
if l.type.isAutoParam || !l.binderInfo.isExplicit then
pure $ ParamKind.implicit none
else
pure ParamKind.explicit
@[builtinDelab app]
def delabAppExplicit : Delab := do
let (fnStx, argStxs) ← withAppFnArgs
(do
let fn ← getExpr
let stx ← if fn.isConst then delabConst else delab
let paramKinds ← liftM <| getParamKinds fn <|> pure #[]
let stx ← if paramKinds.any (fun | ParamKind.explicit => false | _ => true) = true then `(@$stx) else pure stx
pure (stx, #[]))
(fun ⟨fnStx, argStxs⟩ => do
let argStx ← delab
pure (fnStx, argStxs.push argStx))
Syntax.mkApp fnStx argStxs
@[builtinDelab app]
def delabAppImplicit : Delab := whenNotPPOption getPPExplicit do
let (fnStx, _, argStxs) ← withAppFnArgs
(do
let fn ← getExpr
let stx ← if fn.isConst then delabConst else delab
let paramKinds ← liftM (getParamKinds fn <|> pure #[])
pure (stx, paramKinds.toList, #[]))
(fun (fnStx, paramKinds, argStxs) => do
let arg ← getExpr;
let implicit : Bool := match paramKinds with -- TODO: check why we need `: Bool` here
| [ParamKind.implicit (some v)] => !v.hasLooseBVars && v == arg
| ParamKind.implicit none :: _ => true
| _ => false
if implicit then
pure (fnStx, paramKinds.tailD [], argStxs)
else do
let argStx ← delab
pure (fnStx, paramKinds.tailD [], argStxs.push argStx))
Syntax.mkApp fnStx argStxs
@[builtinDelab app]
def delabAppWithUnexpander : Delab := whenPPOption getPPNotation do
let Expr.const c _ _ ← pure (← getExpr).getAppFn | failure
let stx ← delabAppImplicit
match stx with
| `($cPP:ident $args*) => do
let some (f::_) ← pure <| (appUnexpanderAttribute.ext.getState (← getEnv)).table.find? c
| pure stx
let EStateM.Result.ok stx _ ← f stx |>.run ()
| pure stx
pure stx
| _ => pure stx
/-- State for `delabAppMatch` and helpers. -/
structure AppMatchState where
info : MatcherInfo
matcherTy : Expr
params : Array Expr := #[]
hasMotive : Bool := false
discrs : Array Syntax := #[]
varNames : Array (Array Name) := #[]
rhss : Array Syntax := #[]
-- additional arguments applied to the result of the `match` expression
moreArgs : Array Syntax := #[]
/--
Extract arguments of motive applications from the matcher type.
For the example below: `#[#[`([])], #[`(a::as)]]` -/
private partial def delabPatterns (st : AppMatchState) : DelabM (Array (Array Syntax)) := do
let ty ← instantiateForall st.matcherTy st.params
forallTelescope ty fun params _ => do
-- skip motive and discriminators
let alts := Array.ofSubarray $ params[1 + st.discrs.size:]
alts.mapIdxM fun idx alt => do
let ty ← inferType alt
withReader ({ · with expr := ty }) $
usingNames st.varNames[idx] do
withAppFnArgs (pure #[]) (fun pats => do pure $ pats.push (← delab))
where
usingNames {α} (varNames : Array Name) (x : DelabM α) : DelabM α :=
usingNamesAux 0 varNames x
usingNamesAux {α} (i : Nat) (varNames : Array Name) (x : DelabM α) : DelabM α :=
if i < varNames.size then
withBindingBody varNames[i] <| usingNamesAux (i+1) varNames x
else
x
/-- Skip `numParams` binders, and execute `x varNames` where `varNames` contains the new binder names. -/
private def skippingBinders {α} (numParams : Nat) (x : Array Name → DelabM α) : DelabM α :=
loop numParams #[]
where
loop : Nat → Array Name → DelabM α
| 0, varNames => x varNames
| n+1, varNames => do
let varName ← (← getExpr).bindingName!.eraseMacroScopes
-- Pattern variables cannot shadow each other
if varNames.contains varName then
let varName := (← getLCtx).getUnusedName varName
loop n (varNames.push varName)
else
withBindingBodyUnusedName fun id => do
loop n (varNames.push id.getId)
/--
Delaborate applications of "matchers" such as
```
List.map.match_1 : {α : Type _} →
(motive : List α → Sort _) →
(x : List α) → (Unit → motive List.nil) → ((a : α) → (as : List α) → motive (a :: as)) → motive x
```
-/
@[builtinDelab app]
def delabAppMatch : Delab := whenPPOption getPPNotation do
-- incrementally fill `AppMatchState` from arguments
let st ← withAppFnArgs
(do
let (Expr.const c us _) ← getExpr | failure
let (some info) ← getMatcherInfo? c | failure
{ matcherTy := (← getConstInfo c).instantiateTypeLevelParams us, info := info : AppMatchState })
(fun st => do
if st.params.size < st.info.numParams then
pure { st with params := st.params.push (← getExpr) }
else if !st.hasMotive then
-- discard motive argument
pure { st with hasMotive := true }
else if st.discrs.size < st.info.numDiscrs then
pure { st with discrs := st.discrs.push (← delab) }
else if st.rhss.size < st.info.altNumParams.size then
/- We save the variables names here to be able to implemente safe_shadowing.
The pattern delaboration must use the names saved here. -/
let (varNames, rhs) ← skippingBinders st.info.altNumParams[st.rhss.size] fun varNames => do
let rhs ← delab
return (varNames, rhs)
pure { st with rhss := st.rhss.push rhs, varNames := st.varNames.push varNames }
else
pure { st with moreArgs := st.moreArgs.push (← delab) })
if st.discrs.size < st.info.numDiscrs || st.rhss.size < st.info.altNumParams.size then
-- underapplied
failure
match st.discrs, st.rhss with
| #[discr], #[] =>
let stx ← `(nomatch $discr)
Syntax.mkApp stx st.moreArgs
| _, #[] => failure
| _, _ =>
let pats ← delabPatterns st
let stx ← `(match $[$st.discrs:term],* with $[| $pats,* => $st.rhss]*)
Syntax.mkApp stx st.moreArgs
@[builtinDelab mdata]
def delabMData : Delab := do
-- only interpret `pp.` values by default
let Expr.mdata m _ _ ← getExpr | unreachable!
let mut posOpts := (← read).optionsPerPos
let mut inaccessible := false
let pos := (← read).pos
for (k, v) in m do
if (`pp).isPrefixOf k then
let opts := posOpts.find? pos |>.getD {}
posOpts := posOpts.insert pos (opts.insert k v)
if k == `inaccessible then
inaccessible := true
withReader ({ · with optionsPerPos := posOpts }) do
let s ← withMDataExpr delab
if inaccessible then
`(.($s))
else
pure s
/--
Check for a `Syntax.ident` of the given name anywhere in the tree.
This is usually a bad idea since it does not check for shadowing bindings,
but in the delaborator we assume that bindings are never shadowed.
-/
partial def hasIdent (id : Name) : Syntax → Bool
| Syntax.ident _ _ id' _ => id == id'
| Syntax.node _ args => args.any (hasIdent id)
| _ => false
/--
Return `true` iff current binder should be merged with the nested
binder, if any, into a single binder group:
* both binders must have same binder info and domain
* they cannot be inst-implicit (`[a b : A]` is not valid syntax)
* `pp.binder_types` must be the same value for both terms
* prefer `fun a b` over `fun (a b)`
-/
private def shouldGroupWithNext : DelabM Bool := do
let e ← getExpr
let ppEType ← getPPOption getPPBinderTypes;
let go (e' : Expr) := do
let ppE'Type ← withBindingBody `_ $ getPPOption getPPBinderTypes
pure $ e.binderInfo == e'.binderInfo &&
e.bindingDomain! == e'.bindingDomain! &&
e'.binderInfo != BinderInfo.instImplicit &&
ppEType == ppE'Type &&
(e'.binderInfo != BinderInfo.default || ppE'Type)
match e with
| Expr.lam _ _ e'@(Expr.lam _ _ _ _) _ => go e'
| Expr.forallE _ _ e'@(Expr.forallE _ _ _ _) _ => go e'
| _ => pure false
private partial def delabBinders (delabGroup : Array Syntax → Syntax → Delab) : optParam (Array Syntax) #[] → Delab
-- Accumulate names (`Syntax.ident`s with position information) of the current, unfinished
-- binder group `(d e ...)` as determined by `shouldGroupWithNext`. We cannot do grouping
-- inside-out, on the Syntax level, because it depends on comparing the Expr binder types.
| curNames => do
if (← shouldGroupWithNext) then
-- group with nested binder => recurse immediately
withBindingBodyUnusedName fun stxN => delabBinders delabGroup (curNames.push stxN)
else
-- don't group => delab body and prepend current binder group
let (stx, stxN) ← withBindingBodyUnusedName fun stxN => do (← delab, stxN)
delabGroup (curNames.push stxN) stx
@[builtinDelab lam]
def delabLam : Delab :=
delabBinders fun curNames stxBody => do
let e ← getExpr
let stxT ← withBindingDomain delab
let ppTypes ← getPPOption getPPBinderTypes
let expl ← getPPOption getPPExplicit
-- leave lambda implicit if possible
let blockImplicitLambda := expl ||
e.binderInfo == BinderInfo.default ||
Elab.Term.blockImplicitLambda stxBody ||
curNames.any (fun n => hasIdent n.getId stxBody);
if !blockImplicitLambda then
pure stxBody
else
let group ← match e.binderInfo, ppTypes with
| BinderInfo.default, true =>
-- "default" binder group is the only one that expects binder names
-- as a term, i.e. a single `Syntax.ident` or an application thereof
let stxCurNames ←
if curNames.size > 1 then
`($(curNames.get! 0) $(curNames.eraseIdx 0)*)
else
pure $ curNames.get! 0;
`(funBinder| ($stxCurNames : $stxT))
| BinderInfo.default, false => pure curNames.back -- here `curNames.size == 1`
| BinderInfo.implicit, true => `(funBinder| {$curNames* : $stxT})
| BinderInfo.implicit, false => `(funBinder| {$curNames*})
| BinderInfo.instImplicit, _ => `(funBinder| [$curNames.back : $stxT]) -- here `curNames.size == 1`
| _ , _ => unreachable!;
match stxBody with
| `(fun $binderGroups* => $stxBody) => `(fun $group $binderGroups* => $stxBody)
| _ => `(fun $group => $stxBody)
@[builtinDelab forallE]
def delabForall : Delab :=
delabBinders fun curNames stxBody => do
let e ← getExpr
let prop ← try isProp e catch _ => false
let stxT ← withBindingDomain delab
let group ← match e.binderInfo with
| BinderInfo.default =>
-- heuristic: use non-dependent arrows only if possible for whole group to avoid
-- noisy mix like `(α : Type) → Type → (γ : Type) → ...`.
let dependent := curNames.any $ fun n => hasIdent n.getId stxBody
-- NOTE: non-dependent arrows are available only for the default binder info
if dependent then
if prop && !(← getPPOption getPPBinderTypes) then
return ← `(∀ $curNames:ident*, $stxBody)
else
`(bracketedBinderF|($curNames* : $stxT))
else
return ← curNames.foldrM (fun _ stxBody => `($stxT → $stxBody)) stxBody
| BinderInfo.implicit => `(bracketedBinderF|{$curNames* : $stxT})
-- here `curNames.size == 1`
| BinderInfo.instImplicit => `(bracketedBinderF|[$curNames.back : $stxT])
| _ => unreachable!
if prop then
match stxBody with
| `(∀ $groups*, $stxBody) => `(∀ $group $groups*, $stxBody)
| _ => `(∀ $group, $stxBody)
else
`($group:bracketedBinder → $stxBody)
@[builtinDelab letE]
def delabLetE : Delab := do
let Expr.letE n t v b _ ← getExpr | unreachable!
let n ← getUnusedName n b
let stxT ← descend t 0 delab
let stxV ← descend v 1 delab
let stxB ← withLetDecl n t v fun fvar =>
let b := b.instantiate1 fvar
descend b 2 delab
`(let $(mkIdent n) : $stxT := $stxV; $stxB)
@[builtinDelab lit]
def delabLit : Delab := do
let Expr.lit l _ ← getExpr | unreachable!
match l with
| Literal.natVal n => pure $ quote n
| Literal.strVal s => pure $ quote s
-- `@OfNat.ofNat _ n _` ~> `n`
@[builtinDelab app.OfNat.ofNat]
def delabOfNat : Delab := whenPPOption getPPCoercions do
let (Expr.app (Expr.app _ (Expr.lit (Literal.natVal n) _) _) _ _) ← getExpr | failure
return quote n
-- `@OfDecimal.ofDecimal _ _ m s e` ~> `m*10^(sign * e)` where `sign == 1` if `s = false` and `sign = -1` if `s = true`
@[builtinDelab app.OfScientific.ofScientific]
def delabOfScientific : Delab := whenPPOption getPPCoercions do
let expr ← getExpr
guard <| expr.getAppNumArgs == 5
let Expr.lit (Literal.natVal m) _ ← pure (expr.getArg! 2) | failure
let Expr.lit (Literal.natVal e) _ ← pure (expr.getArg! 4) | failure
let s ← match expr.getArg! 3 with
| Expr.const `Bool.true _ _ => pure true
| Expr.const `Bool.false _ _ => pure false
| _ => failure
let str := toString m
if s && e == str.length then
return Syntax.mkScientificLit ("0." ++ str)
else if s && e < str.length then
let mStr := str.extract 0 (str.length - e)
let eStr := str.extract (str.length - e) str.length
return Syntax.mkScientificLit (mStr ++ "." ++ eStr)
else
return Syntax.mkScientificLit (str ++ "e" ++ (if s then "-" else "") ++ toString e)
/--
Delaborate a projection primitive. These do not usually occur in
user code, but are pretty-printed when e.g. `#print`ing a projection
function.
-/
@[builtinDelab proj]
def delabProj : Delab := do
let Expr.proj _ idx _ _ ← getExpr | unreachable!
let e ← withProj delab
-- not perfectly authentic: elaborates to the `idx`-th named projection
-- function (e.g. `e.1` is `Prod.fst e`), which unfolds to the actual
-- `proj`.
let idx := Syntax.mkLit fieldIdxKind (toString (idx + 1));
`($(e).$idx:fieldIdx)
/-- Delaborate a call to a projection function such as `Prod.fst`. -/
@[builtinDelab app]
def delabProjectionApp : Delab := whenPPOption getPPStructureProjections $ do
let e@(Expr.app fn _ _) ← getExpr | failure
let Expr.const c@(Name.str _ f _) _ _ ← pure fn.getAppFn | failure
let env ← getEnv
let some info ← pure $ env.getProjectionFnInfo? c | failure
-- can't use with classes since the instance parameter is implicit
guard $ !info.fromClass
-- projection function should be fully applied (#struct params + 1 instance parameter)
-- TODO: support over-application
guard $ e.getAppNumArgs == info.nparams + 1
-- If pp.explicit is true, and the structure has parameters, we should not
-- use field notation because we will not be able to see the parameters.
let expl ← getPPOption getPPExplicit
guard $ !expl || info.nparams == 0
let appStx ← withAppArg delab
`($(appStx).$(mkIdent f):ident)
@[builtinDelab app]
def delabStructureInstance : Delab := whenPPOption getPPStructureInstances do
let env ← getEnv
let e ← getExpr
let some s ← pure $ e.isConstructorApp? env | failure
guard $ isStructure env s.induct;
/- If implicit arguments should be shown, and the structure has parameters, we should not
pretty print using { ... }, because we will not be able to see the parameters. -/
let explicit ← getPPOption getPPExplicit
guard !(explicit && s.numParams > 0)
let fieldNames := getStructureFields env s.induct
let (_, fields) ← withAppFnArgs (pure (0, #[])) fun ⟨idx, fields⟩ => do
if idx < s.numParams then
pure (idx + 1, fields)
else
let val ← delab
let field ← `(structInstField|$(mkIdent <| fieldNames.get! (idx - s.numParams)):ident := $val)
pure (idx + 1, fields.push field)
let lastField := fields[fields.size - 1]
let fields := fields.pop
let ty ←
if (← getPPOption getPPStructureInstanceType) then
let ty ← inferType e
-- `ty` is not actually part of `e`, but since `e` must be an application or constant, we know that
-- index 2 is unused.
pure <| some (← descend ty 2 delab)
else pure <| none
`({ $[$fields, ]* $lastField $[: $ty]? })
@[builtinDelab app.Prod.mk]
def delabTuple : Delab := whenPPOption getPPNotation do
let e ← getExpr
guard $ e.getAppNumArgs == 4
let a ← withAppFn $ withAppArg delab
let b ← withAppArg delab
match b with
| `(($b, $bs,*)) => `(($a, $b, $bs,*))
| _ => `(($a, $b))
-- abbrev coe {α : Sort u} {β : Sort v} (a : α) [CoeT α a β] : β
@[builtinDelab app.coe]
def delabCoe : Delab := whenPPOption getPPCoercions do
let e ← getExpr
guard $ e.getAppNumArgs >= 4
-- delab as application, then discard function
let stx ← delabAppImplicit
match stx with
| `($fn $arg) => arg
| `($fn $args*) => `($(args.get! 0) $(args.eraseIdx 0)*)
| _ => failure
-- abbrev coeFun {α : Sort u} {γ : α → Sort v} (a : α) [CoeFun α γ] : γ a
@[builtinDelab app.coeFun]
def delabCoeFun : Delab := delabCoe
@[builtinDelab app.List.nil]
def delabNil : Delab := whenPPOption getPPNotation do
guard $ (← getExpr).getAppNumArgs == 1
`([])
@[builtinDelab app.List.cons]
def delabConsList : Delab := whenPPOption getPPNotation do
guard $ (← getExpr).getAppNumArgs == 3
let x ← withAppFn (withAppArg delab)
match (← withAppArg delab) with
| `([]) => `([$x])
| `([$xs,*]) => `([$x, $xs,*])
| _ => failure
@[builtinDelab app.List.toArray]
def delabListToArray : Delab := whenPPOption getPPNotation do
guard $ (← getExpr).getAppNumArgs == 2
match (← withAppArg delab) with
| `([$xs,*]) => `(#[$xs,*])
| _ => failure
@[builtinDelab app.ite]
def delabIte : Delab := whenPPOption getPPNotation do
guard $ (← getExpr).getAppNumArgs == 5
let c ← withAppFn $ withAppFn $ withAppFn $ withAppArg delab
let t ← withAppFn $ withAppArg delab
let e ← withAppArg delab
`(if $c then $t else $e)
@[builtinDelab app.dite]
def delabDIte : Delab := whenPPOption getPPNotation do
guard $ (← getExpr).getAppNumArgs == 5
let c ← withAppFn $ withAppFn $ withAppFn $ withAppArg delab
let (t, h) ← withAppFn $ withAppArg $ delabBranch none
let (e, _) ← withAppArg $ delabBranch h
`(if $(mkIdent h):ident : $c then $t else $e)
where
delabBranch (h? : Option Name) : DelabM (Syntax × Name) := do
let e ← getExpr
guard e.isLambda
let h ← match h? with
| some h => return (← withBindingBody h delab, h)
| none => withBindingBodyUnusedName fun h => do
return (← delab, h.getId)
@[builtinDelab app.namedPattern]
def delabNamedPattern : Delab := do
guard $ (← getExpr).getAppNumArgs == 3
let x ← withAppFn $ withAppArg delab
let p ← withAppArg delab
guard x.isIdent
`($x:ident@$p:term)
partial def delabDoElems : DelabM (List Syntax) := do
let e ← getExpr
if e.isAppOfArity `Bind.bind 6 then
-- Bind.bind.{u, v} : {m : Type u → Type v} → [self : Bind m] → {α β : Type u} → m α → (α → m β) → m β
let ma ← withAppFn $ withAppArg delab
withAppArg do
match (← getExpr) with
| Expr.lam _ _ body _ =>
withBindingBodyUnusedName fun n => do
if body.hasLooseBVars then
prependAndRec `(doElem|let $n:term ← $ma)
else
prependAndRec `(doElem|$ma:term)
| _ => delabAndRet
else if e.isLet then
let Expr.letE n t v b _ ← getExpr | unreachable!
let n ← getUnusedName n b
let stxT ← descend t 0 delab
let stxV ← descend v 1 delab
withLetDecl n t v fun fvar =>
let b := b.instantiate1 fvar
descend b 2 $
prependAndRec `(doElem|let $(mkIdent n) : $stxT := $stxV)
else
delabAndRet
where
prependAndRec x : DelabM _ := List.cons <$> x <*> delabDoElems
delabAndRet : DelabM _ := do let stx ← delab; [←`(doElem|$stx:term)]
@[builtinDelab app.Bind.bind]
def delabDo : Delab := whenPPOption getPPNotation do
unless (← getExpr).isAppOfArity `Bind.bind 6 do
failure
let elems ← delabDoElems
let items ← elems.toArray.mapM (`(doSeqItem|$(·):doElem))
`(do $items:doSeqItem*)
@[builtinDelab app.sorryAx]
def delabSorryAx : Delab := whenPPOption getPPNotation do
unless (← getExpr).isAppOfArity ``sorryAx 2 do
failure
`(sorry)
end Lean.PrettyPrinter.Delaborator
|
0deb74177010c3e95f08b7ffab289f6cb28ee09f | 54d7e71c3616d331b2ec3845d31deb08f3ff1dea | /tests/lean/eta_tac.lean | 18dbc48d279c675529be6fd755ce02750d313f5b | [
"Apache-2.0"
] | permissive | pachugupta/lean | 6f3305c4292288311cc4ab4550060b17d49ffb1d | 0d02136a09ac4cf27b5c88361750e38e1f485a1a | refs/heads/master | 1,611,110,653,606 | 1,493,130,117,000 | 1,493,167,649,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 480 | lean | open tactic
set_option pp.binder_types true
set_option pp.implicit true
set_option pp.notation false
example (a : nat) : true :=
by do
mk_const `add >>= head_eta_expand >>= trace,
mk_const `nat.succ >>= head_eta_expand >>= trace,
to_expr `(add a) >>= head_eta_expand >>= trace,
to_expr `(λ x : nat, add x) >>= head_eta_expand >>= trace,
to_expr `(λ x : nat, add x) >>= head_eta >>= trace,
to_expr `(add a) >>= head_eta_expand >>= head_eta >>= trace,
constructor
|
7e3580b60424e4ca4f55ed40c6a09b11599f5208 | 94e33a31faa76775069b071adea97e86e218a8ee | /src/order/symm_diff.lean | d9ca1c00070271cde4d6c104e26597a62473cdfa | [
"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 | 11,858 | lean | /-
Copyright (c) 2021 Bryan Gin-ge Chen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Adam Topaz, Bryan Gin-ge Chen
-/
import order.boolean_algebra
/-!
# Symmetric difference
The symmetric difference or disjunctive union of sets `A` and `B` is the set of elements that are
in either `A` or `B` but not both. Translated into propositions, the symmetric difference is `xor`.
The symmetric difference operator (`symm_diff`) is defined in this file for any type with `⊔` and
`\` via the formula `(A \ B) ⊔ (B \ A)`, however the theorems proved about it only hold for
`generalized_boolean_algebra`s and `boolean_algebra`s.
The symmetric difference is the addition operator in the Boolean ring structure on Boolean algebras.
## Main declarations
* `symm_diff`: the symmetric difference operator, defined as `(A \ B) ⊔ (B \ A)`
In generalized Boolean algebras, the symmetric difference operator is:
* `symm_diff_comm`: commutative, and
* `symm_diff_assoc`: associative.
## Notations
* `a ∆ b`: `symm_diff a b`
## References
The proof of associativity follows the note "Associativity of the Symmetric Difference of Sets: A
Proof from the Book" by John McCuan:
* <https://people.math.gatech.edu/~mccuan/courses/4317/symmetricdifference.pdf>
## Tags
boolean ring, generalized boolean algebra, boolean algebra, symmetric differences
-/
open function
/-- The symmetric difference operator on a type with `⊔` and `\` is `(A \ B) ⊔ (B \ A)`. -/
def symm_diff {α : Type*} [has_sup α] [has_sdiff α] (A B : α) : α := (A \ B) ⊔ (B \ A)
/- This notation might conflict with the Laplacian once we have it. Feel free to put it in locale
`order` or `symm_diff` if that happens. -/
infix ` ∆ `:100 := symm_diff
lemma symm_diff_def {α : Type*} [has_sup α] [has_sdiff α] (A B : α) :
A ∆ B = (A \ B) ⊔ (B \ A) :=
rfl
lemma symm_diff_eq_xor (p q : Prop) : p ∆ q = xor p q := rfl
@[simp] lemma bool.symm_diff_eq_bxor : ∀ p q : bool, p ∆ q = bxor p q := dec_trivial
section generalized_boolean_algebra
variables {α : Type*} [generalized_boolean_algebra α] (a b c d : α)
lemma symm_diff_comm : a ∆ b = b ∆ a := by simp only [(∆), sup_comm]
instance symm_diff_is_comm : is_commutative α (∆) := ⟨symm_diff_comm⟩
@[simp] lemma symm_diff_self : a ∆ a = ⊥ := by rw [(∆), sup_idem, sdiff_self]
@[simp] lemma symm_diff_bot : a ∆ ⊥ = a := by rw [(∆), sdiff_bot, bot_sdiff, sup_bot_eq]
@[simp] lemma bot_symm_diff : ⊥ ∆ a = a := by rw [symm_diff_comm, symm_diff_bot]
lemma symm_diff_eq_sup_sdiff_inf : a ∆ b = (a ⊔ b) \ (a ⊓ b) :=
by simp [sup_sdiff, sdiff_inf, sup_comm, (∆)]
@[simp] lemma sup_sdiff_symm_diff : (a ⊔ b) \ (a ∆ b) = a ⊓ b :=
sdiff_eq_symm inf_le_sup (by rw symm_diff_eq_sup_sdiff_inf)
lemma disjoint_symm_diff_inf : disjoint (a ∆ b) (a ⊓ b) :=
begin
rw [symm_diff_eq_sup_sdiff_inf],
exact disjoint_sdiff_self_left,
end
lemma symm_diff_le_sup : a ∆ b ≤ a ⊔ b := by { rw symm_diff_eq_sup_sdiff_inf, exact sdiff_le }
lemma inf_symm_diff_distrib_left : a ⊓ (b ∆ c) = (a ⊓ b) ∆ (a ⊓ c) :=
by rw [symm_diff_eq_sup_sdiff_inf, inf_sdiff_distrib_left, inf_sup_left, inf_inf_distrib_left,
symm_diff_eq_sup_sdiff_inf]
lemma inf_symm_diff_distrib_right : (a ∆ b) ⊓ c = (a ⊓ c) ∆ (b ⊓ c) :=
by simp_rw [@inf_comm _ _ _ c, inf_symm_diff_distrib_left]
lemma sdiff_symm_diff : c \ (a ∆ b) = (c ⊓ a ⊓ b) ⊔ ((c \ a) ⊓ (c \ b)) :=
by simp only [(∆), sdiff_sdiff_sup_sdiff']
lemma sdiff_symm_diff' : c \ (a ∆ b) = (c ⊓ a ⊓ b) ⊔ (c \ (a ⊔ b)) :=
by rw [sdiff_symm_diff, sdiff_sup, sup_comm]
lemma symm_diff_sdiff : (a ∆ b) \ c = (a \ (b ⊔ c)) ⊔ (b \ (a ⊔ c)) :=
by rw [symm_diff_def, sup_sdiff, sdiff_sdiff_left, sdiff_sdiff_left]
@[simp] lemma symm_diff_sdiff_left : (a ∆ b) \ a = b \ a :=
by rw [symm_diff_def, sup_sdiff, sdiff_idem, sdiff_sdiff_self, bot_sup_eq]
@[simp] lemma symm_diff_sdiff_right : (a ∆ b) \ b = a \ b :=
by rw [symm_diff_comm, symm_diff_sdiff_left]
@[simp] lemma sdiff_symm_diff_self : a \ (a ∆ b) = a ⊓ b := by simp [sdiff_symm_diff]
lemma symm_diff_eq_iff_sdiff_eq {a b c : α} (ha : a ≤ c) :
a ∆ b = c ↔ c \ a = b :=
begin
split; intro h,
{ have hba : disjoint (a ⊓ b) c := begin
rw [←h, disjoint.comm],
exact disjoint_symm_diff_inf _ _,
end,
have hca : _ := congr_arg (\ a) h,
rw [symm_diff_sdiff_left] at hca,
rw [←hca, sdiff_eq_self_iff_disjoint],
exact hba.of_disjoint_inf_of_le ha },
{ have hd : disjoint a b := by { rw ←h, exact disjoint_sdiff_self_right },
rw [symm_diff_def, hd.sdiff_eq_left, hd.sdiff_eq_right, ←h, sup_sdiff_cancel_right ha] }
end
lemma disjoint.symm_diff_eq_sup {a b : α} (h : disjoint a b) : a ∆ b = a ⊔ b :=
by rw [(∆), h.sdiff_eq_left, h.sdiff_eq_right]
lemma symm_diff_eq_sup : a ∆ b = a ⊔ b ↔ disjoint a b :=
begin
split; intro h,
{ rw [symm_diff_eq_sup_sdiff_inf, sdiff_eq_self_iff_disjoint] at h,
exact h.of_disjoint_inf_of_le le_sup_left, },
{ exact h.symm_diff_eq_sup, },
end
@[simp] lemma le_symm_diff_iff_left : a ≤ a ∆ b ↔ disjoint a b :=
begin
refine ⟨λ h, _, λ h, h.symm_diff_eq_sup.symm ▸ le_sup_left⟩,
rw symm_diff_eq_sup_sdiff_inf at h,
exact (le_sdiff_iff.1 $ inf_le_of_left_le h).le,
end
@[simp] lemma le_symm_diff_iff_right : b ≤ a ∆ b ↔ disjoint a b :=
by rw [symm_diff_comm, le_symm_diff_iff_left, disjoint.comm]
lemma symm_diff_symm_diff_left :
a ∆ b ∆ c = (a \ (b ⊔ c)) ⊔ (b \ (a ⊔ c)) ⊔ (c \ (a ⊔ b)) ⊔ (a ⊓ b ⊓ c) :=
calc a ∆ b ∆ c = ((a ∆ b) \ c) ⊔ (c \ (a ∆ b)) : symm_diff_def _ _
... = (a \ (b ⊔ c)) ⊔ (b \ (a ⊔ c)) ⊔
((c \ (a ⊔ b)) ⊔ (c ⊓ a ⊓ b)) :
by rw [sdiff_symm_diff', @sup_comm _ _ (c ⊓ a ⊓ b), symm_diff_sdiff]
... = (a \ (b ⊔ c)) ⊔ (b \ (a ⊔ c)) ⊔
(c \ (a ⊔ b)) ⊔ (a ⊓ b ⊓ c) : by ac_refl
lemma symm_diff_symm_diff_right :
a ∆ (b ∆ c) = (a \ (b ⊔ c)) ⊔ (b \ (a ⊔ c)) ⊔ (c \ (a ⊔ b)) ⊔ (a ⊓ b ⊓ c) :=
calc a ∆ (b ∆ c) = (a \ (b ∆ c)) ⊔ ((b ∆ c) \ a) : symm_diff_def _ _
... = (a \ (b ⊔ c)) ⊔ (a ⊓ b ⊓ c) ⊔
(b \ (c ⊔ a) ⊔ c \ (b ⊔ a)) :
by rw [sdiff_symm_diff', @sup_comm _ _ (a ⊓ b ⊓ c), symm_diff_sdiff]
... = (a \ (b ⊔ c)) ⊔ (b \ (a ⊔ c)) ⊔
(c \ (a ⊔ b)) ⊔ (a ⊓ b ⊓ c) : by ac_refl
@[simp] lemma symm_diff_symm_diff_inf : a ∆ b ∆ (a ⊓ b) = a ⊔ b :=
by rw [symm_diff_eq_iff_sdiff_eq (symm_diff_le_sup _ _), sup_sdiff_symm_diff]
@[simp] lemma inf_symm_diff_symm_diff : (a ⊓ b) ∆ (a ∆ b) = a ⊔ b :=
by rw [symm_diff_comm, symm_diff_symm_diff_inf]
lemma symm_diff_triangle : a ∆ c ≤ a ∆ b ⊔ b ∆ c :=
begin
refine (sup_le_sup (sdiff_triangle a b c) $ sdiff_triangle _ b _).trans_eq _,
rw [@sup_comm _ _ (c \ b), sup_sup_sup_comm],
refl,
end
lemma symm_diff_assoc : a ∆ b ∆ c = a ∆ (b ∆ c) :=
by rw [symm_diff_symm_diff_left, symm_diff_symm_diff_right]
instance symm_diff_is_assoc : is_associative α (∆) := ⟨symm_diff_assoc⟩
lemma symm_diff_left_comm : a ∆ (b ∆ c) = b ∆ (a ∆ c) :=
by simp_rw [←symm_diff_assoc, symm_diff_comm]
lemma symm_diff_right_comm : a ∆ b ∆ c = a ∆ c ∆ b := by simp_rw [symm_diff_assoc, symm_diff_comm]
lemma symm_diff_symm_diff_symm_diff_comm : (a ∆ b) ∆ (c ∆ d) = (a ∆ c) ∆ (b ∆ d) :=
by simp_rw [symm_diff_assoc, symm_diff_left_comm]
@[simp] lemma symm_diff_symm_diff_cancel_left : a ∆ (a ∆ b) = b := by simp [←symm_diff_assoc]
@[simp] lemma symm_diff_symm_diff_cancel_right : b ∆ a ∆ a = b := by simp [symm_diff_assoc]
@[simp] lemma symm_diff_symm_diff_self' : a ∆ b ∆ a = b :=
by rw [symm_diff_comm,symm_diff_symm_diff_cancel_left]
lemma symm_diff_left_involutive (a : α) : involutive (∆ a) := symm_diff_symm_diff_cancel_right _
lemma symm_diff_right_involutive (a : α) : involutive ((∆) a) := symm_diff_symm_diff_cancel_left _
lemma symm_diff_left_injective (a : α) : injective (∆ a) := (symm_diff_left_involutive _).injective
lemma symm_diff_right_injective (a : α) : injective ((∆) a) :=
(symm_diff_right_involutive _).injective
lemma symm_diff_left_surjective (a : α) : surjective (∆ a) :=
(symm_diff_left_involutive _).surjective
lemma symm_diff_right_surjective (a : α) : surjective ((∆) a) :=
(symm_diff_right_involutive _).surjective
variables {a b c}
@[simp] lemma symm_diff_left_inj : a ∆ b = c ∆ b ↔ a = c := (symm_diff_left_injective _).eq_iff
@[simp] lemma symm_diff_right_inj : a ∆ b = a ∆ c ↔ b = c := (symm_diff_right_injective _).eq_iff
@[simp] lemma symm_diff_eq_left : a ∆ b = a ↔ b = ⊥ :=
calc a ∆ b = a ↔ a ∆ b = a ∆ ⊥ : by rw symm_diff_bot
... ↔ b = ⊥ : by rw symm_diff_right_inj
@[simp] lemma symm_diff_eq_right : a ∆ b = b ↔ a = ⊥ := by rw [symm_diff_comm, symm_diff_eq_left]
@[simp] lemma symm_diff_eq_bot : a ∆ b = ⊥ ↔ a = b :=
calc a ∆ b = ⊥ ↔ a ∆ b = a ∆ a : by rw symm_diff_self
... ↔ a = b : by rw [symm_diff_right_inj, eq_comm]
protected lemma disjoint.symm_diff_left (ha : disjoint a c) (hb : disjoint b c) :
disjoint (a ∆ b) c :=
by { rw symm_diff_eq_sup_sdiff_inf, exact (ha.sup_left hb).disjoint_sdiff_left }
protected lemma disjoint.symm_diff_right (ha : disjoint a b) (hb : disjoint a c) :
disjoint a (b ∆ c) :=
(ha.symm.symm_diff_left hb.symm).symm
end generalized_boolean_algebra
section boolean_algebra
variables {α : Type*} [boolean_algebra α] (a b c : α)
lemma symm_diff_eq : a ∆ b = (a ⊓ bᶜ) ⊔ (b ⊓ aᶜ) := by simp only [(∆), sdiff_eq]
@[simp] lemma symm_diff_top : a ∆ ⊤ = aᶜ := by simp [symm_diff_eq]
@[simp] lemma top_symm_diff : ⊤ ∆ a = aᶜ := by rw [symm_diff_comm, symm_diff_top]
lemma compl_symm_diff : (a ∆ b)ᶜ = (a ⊓ b) ⊔ (aᶜ ⊓ bᶜ) :=
by simp only [←top_sdiff, sdiff_symm_diff, top_inf_eq]
lemma symm_diff_eq_top_iff : a ∆ b = ⊤ ↔ is_compl a b :=
by rw [symm_diff_eq_iff_sdiff_eq le_top, top_sdiff, compl_eq_iff_is_compl]
lemma is_compl.symm_diff_eq_top (h : is_compl a b) : a ∆ b = ⊤ := (symm_diff_eq_top_iff a b).2 h
@[simp] lemma compl_symm_diff_self : aᶜ ∆ a = ⊤ :=
by simp only [symm_diff_eq, compl_compl, inf_idem, compl_sup_eq_top]
@[simp] lemma symm_diff_compl_self : a ∆ aᶜ = ⊤ := by rw [symm_diff_comm, compl_symm_diff_self]
lemma symm_diff_symm_diff_right' :
a ∆ (b ∆ c) = (a ⊓ b ⊓ c) ⊔ (a ⊓ bᶜ ⊓ cᶜ) ⊔ (aᶜ ⊓ b ⊓ cᶜ) ⊔ (aᶜ ⊓ bᶜ ⊓ c) :=
calc a ∆ (b ∆ c) = (a ⊓ ((b ⊓ c) ⊔ (bᶜ ⊓ cᶜ))) ⊔
(((b ⊓ cᶜ) ⊔ (c ⊓ bᶜ)) ⊓ aᶜ) : by rw [symm_diff_eq, compl_symm_diff,
symm_diff_eq]
... = (a ⊓ b ⊓ c) ⊔ (a ⊓ bᶜ ⊓ cᶜ) ⊔
(b ⊓ cᶜ ⊓ aᶜ) ⊔ (c ⊓ bᶜ ⊓ aᶜ) : by rw [inf_sup_left, inf_sup_right,
←sup_assoc, ←inf_assoc, ←inf_assoc]
... = (a ⊓ b ⊓ c) ⊔ (a ⊓ bᶜ ⊓ cᶜ) ⊔
(aᶜ ⊓ b ⊓ cᶜ) ⊔ (aᶜ ⊓ bᶜ ⊓ c) : begin
congr' 1,
{ congr' 1,
rw [inf_comm, inf_assoc], },
{ apply inf_left_right_swap }
end
end boolean_algebra
|
a92a672701a963466bb11865323dfb6455ddd923 | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/topology/extend_from.lean | 10b4b2f69bb0ad141d23d5e547da0f1af6e41ddf | [
"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 | 4,016 | lean | /-
Copyright (c) 2020 Anatole Dedecker. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Patrick Massot, Anatole Dedecker
-/
import topology.separation
/-!
# Extending a function from a subset
The main definition of this file is `extend_from A f` where `f : X → Y`
and `A : set X`. This defines a new function `g : X → Y` which maps any
`x₀ : X` to the limit of `f` as `x` tends to `x₀`, if such a limit exists.
This is analoguous to the way `dense_inducing.extend` "extends" a function
`f : X → Z` to a function `g : Y → Z` along a dense inducing `i : X → Y`.
The main theorem we prove about this definition is `continuous_on_extend_from`
which states that, for `extend_from A f` to be continuous on a set `B ⊆ closure A`,
it suffices that `f` converges within `A` at any point of `B`, provided that
`f` is a function to a T₃ space.
-/
noncomputable theory
open_locale topological_space
open filter set
variables {X Y : Type*} [topological_space X] [topological_space Y]
/-- Extend a function from a set `A`. The resulting function `g` is such that
at any `x₀`, if `f` converges to some `y` as `x` tends to `x₀` within `A`,
then `g x₀` is defined to be one of these `y`. Else, `g x₀` could be anything. -/
def extend_from (A : set X) (f : X → Y) : X → Y :=
λ x, @@lim _ ⟨f x⟩ (𝓝[A] x) f
/-- If `f` converges to some `y` as `x` tends to `x₀` within `A`,
then `f` tends to `extend_from A f x` as `x` tends to `x₀`. -/
lemma tendsto_extend_from {A : set X} {f : X → Y} {x : X}
(h : ∃ y, tendsto f (𝓝[A] x) (𝓝 y)) : tendsto f (𝓝[A] x) (𝓝 $ extend_from A f x) :=
tendsto_nhds_lim h
lemma extend_from_eq [t2_space Y] {A : set X} {f : X → Y} {x : X} {y : Y} (hx : x ∈ closure A)
(hf : tendsto f (𝓝[A] x) (𝓝 y)) : extend_from A f x = y :=
begin
haveI := mem_closure_iff_nhds_within_ne_bot.mp hx,
exact tendsto_nhds_unique (tendsto_nhds_lim ⟨y, hf⟩) hf,
end
lemma extend_from_extends [t2_space Y] {f : X → Y} {A : set X} (hf : continuous_on f A) :
∀ x ∈ A, extend_from A f x = f x :=
λ x x_in, extend_from_eq (subset_closure x_in) (hf x x_in)
/-- If `f` is a function to a T₃ space `Y` which has a limit within `A` at any
point of a set `B ⊆ closure A`, then `extend_from A f` is continuous on `B`. -/
lemma continuous_on_extend_from [t3_space Y] {f : X → Y} {A B : set X} (hB : B ⊆ closure A)
(hf : ∀ x ∈ B, ∃ y, tendsto f (𝓝[A] x) (𝓝 y)) : continuous_on (extend_from A f) B :=
begin
set φ := extend_from A f,
intros x x_in,
suffices : ∀ V' ∈ 𝓝 (φ x), is_closed V' → φ ⁻¹' V' ∈ 𝓝[B] x,
by simpa [continuous_within_at, (closed_nhds_basis _).tendsto_right_iff],
intros V' V'_in V'_closed,
obtain ⟨V, V_in, V_op, hV⟩ : ∃ V ∈ 𝓝 x, is_open V ∧ V ∩ A ⊆ f ⁻¹' V',
{ have := tendsto_extend_from (hf x x_in),
rcases (nhds_within_basis_open x A).tendsto_left_iff.mp this V' V'_in with ⟨V, ⟨hxV, V_op⟩, hV⟩,
use [V, is_open.mem_nhds V_op hxV, V_op, hV] },
suffices : ∀ y ∈ V ∩ B, φ y ∈ V',
from mem_of_superset (inter_mem_inf V_in $ mem_principal_self B) this,
rintros y ⟨hyV, hyB⟩,
haveI := mem_closure_iff_nhds_within_ne_bot.mp (hB hyB),
have limy : tendsto f (𝓝[A] y) (𝓝 $ φ y) := tendsto_extend_from (hf y hyB),
have hVy : V ∈ 𝓝 y := is_open.mem_nhds V_op hyV,
have : V ∩ A ∈ (𝓝[A] y),
by simpa [inter_comm] using inter_mem_nhds_within _ hVy,
exact V'_closed.mem_of_tendsto limy (mem_of_superset this hV)
end
/-- If a function `f` to a T₃ space `Y` has a limit within a
dense set `A` for any `x`, then `extend_from A f` is continuous. -/
lemma continuous_extend_from [t3_space Y] {f : X → Y} {A : set X} (hA : dense A)
(hf : ∀ x, ∃ y, tendsto f (𝓝[A] x) (𝓝 y)) : continuous (extend_from A f) :=
begin
rw continuous_iff_continuous_on_univ,
exact continuous_on_extend_from (λ x _, hA x) (by simpa using hf)
end
|
7e47783a39d8942d0820c00392e2a4845cd20073 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/run/funext.lean | 625f4fb07b368240525821ce220d40a7e47adbd4 | [
"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 | 920 | lean | theorem ex1 : (fun y => y + 0) = (fun x => 0 + x) := by
funext x
simp
theorem ex2 : (fun y x => y + x + 0) = (fun x y => y + x) := by
funext x y
rw [Nat.add_zero, Nat.add_comm]
theorem ex3 : (fun (x : Nat × Nat) => x.1 + x.2) = (fun (x : Nat × Nat) => x.2 + x.1) := by
funext (a, b)
show a + b = b + a
rw [Nat.add_comm]
theorem ex4 : (fun (x : Nat × Nat) (y : Nat × Nat) => x.1 + y.2) = (fun (x : Nat × Nat) (z : Nat × Nat) => z.2 + x.1) := by
funext (a, b) (c, d)
show a + d = d + a
rw [Nat.add_comm]
theorem ex5 : (fun (x : Id Nat) => x.succ + 0) = (fun (x : Id Nat) => 0 + x.succ) := by
funext (x : Nat)
have y := x + 1 -- if `(x : Nat)` is not used at `funext`, then `x+1` would fail to be elaborated since we don't have the instance `Add (Id Nat)`
rw [Nat.add_comm]
theorem ex6 : (fun (x : Nat) y z => x + y + z) = (fun x y z => x + (y + z)) := by
funext
rw [Nat.add_assoc]
|
5719225aca68dd18a519c03cf4122eb336a094e7 | 592ee40978ac7604005a4e0d35bbc4b467389241 | /Library/generated/mathscheme-lean/MiddleAbsorption.lean | ae52ad3cb03343c66cdf3daed0b7e8d1c6b73b25 | [] | no_license | ysharoda/Deriving-Definitions | 3e149e6641fae440badd35ac110a0bd705a49ad2 | dfecb27572022de3d4aa702cae8db19957523a59 | refs/heads/master | 1,679,127,857,700 | 1,615,939,007,000 | 1,615,939,007,000 | 229,785,731 | 4 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 6,172 | lean | import init.data.nat.basic
import init.data.fin.basic
import data.vector
import .Prelude
open Staged
open nat
open fin
open vector
section MiddleAbsorption
structure MiddleAbsorption (A : Type) : Type :=
(op : (A → (A → A)))
(middleAbsorb_times : (∀ {x y z : A} , (op (op x y) z) = (op x z)))
open MiddleAbsorption
structure Sig (AS : Type) : Type :=
(opS : (AS → (AS → AS)))
structure Product (A : Type) : Type :=
(opP : ((Prod A A) → ((Prod A A) → (Prod A A))))
(middleAbsorb_timesP : (∀ {xP yP zP : (Prod A A)} , (opP (opP xP yP) zP) = (opP xP zP)))
structure Hom {A1 : Type} {A2 : Type} (Mi1 : (MiddleAbsorption A1)) (Mi2 : (MiddleAbsorption A2)) : Type :=
(hom : (A1 → A2))
(pres_op : (∀ {x1 x2 : A1} , (hom ((op Mi1) x1 x2)) = ((op Mi2) (hom x1) (hom x2))))
structure RelInterp {A1 : Type} {A2 : Type} (Mi1 : (MiddleAbsorption A1)) (Mi2 : (MiddleAbsorption A2)) : Type 1 :=
(interp : (A1 → (A2 → Type)))
(interp_op : (∀ {x1 x2 : A1} {y1 y2 : A2} , ((interp x1 y1) → ((interp x2 y2) → (interp ((op Mi1) x1 x2) ((op Mi2) y1 y2))))))
inductive MiddleAbsorptionTerm : Type
| opL : (MiddleAbsorptionTerm → (MiddleAbsorptionTerm → MiddleAbsorptionTerm))
open MiddleAbsorptionTerm
inductive ClMiddleAbsorptionTerm (A : Type) : Type
| sing : (A → ClMiddleAbsorptionTerm)
| opCl : (ClMiddleAbsorptionTerm → (ClMiddleAbsorptionTerm → ClMiddleAbsorptionTerm))
open ClMiddleAbsorptionTerm
inductive OpMiddleAbsorptionTerm (n : ℕ) : Type
| v : ((fin n) → OpMiddleAbsorptionTerm)
| opOL : (OpMiddleAbsorptionTerm → (OpMiddleAbsorptionTerm → OpMiddleAbsorptionTerm))
open OpMiddleAbsorptionTerm
inductive OpMiddleAbsorptionTerm2 (n : ℕ) (A : Type) : Type
| v2 : ((fin n) → OpMiddleAbsorptionTerm2)
| sing2 : (A → OpMiddleAbsorptionTerm2)
| opOL2 : (OpMiddleAbsorptionTerm2 → (OpMiddleAbsorptionTerm2 → OpMiddleAbsorptionTerm2))
open OpMiddleAbsorptionTerm2
def simplifyCl {A : Type} : ((ClMiddleAbsorptionTerm A) → (ClMiddleAbsorptionTerm A))
| (opCl x1 x2) := (opCl (simplifyCl x1) (simplifyCl x2))
| (sing x1) := (sing x1)
def simplifyOpB {n : ℕ} : ((OpMiddleAbsorptionTerm n) → (OpMiddleAbsorptionTerm n))
| (opOL x1 x2) := (opOL (simplifyOpB x1) (simplifyOpB x2))
| (v x1) := (v x1)
def simplifyOp {n : ℕ} {A : Type} : ((OpMiddleAbsorptionTerm2 n A) → (OpMiddleAbsorptionTerm2 n A))
| (opOL2 x1 x2) := (opOL2 (simplifyOp x1) (simplifyOp x2))
| (v2 x1) := (v2 x1)
| (sing2 x1) := (sing2 x1)
def evalB {A : Type} : ((MiddleAbsorption A) → (MiddleAbsorptionTerm → A))
| Mi (opL x1 x2) := ((op Mi) (evalB Mi x1) (evalB Mi x2))
def evalCl {A : Type} : ((MiddleAbsorption A) → ((ClMiddleAbsorptionTerm A) → A))
| Mi (sing x1) := x1
| Mi (opCl x1 x2) := ((op Mi) (evalCl Mi x1) (evalCl Mi x2))
def evalOpB {A : Type} {n : ℕ} : ((MiddleAbsorption A) → ((vector A n) → ((OpMiddleAbsorptionTerm n) → A)))
| Mi vars (v x1) := (nth vars x1)
| Mi vars (opOL x1 x2) := ((op Mi) (evalOpB Mi vars x1) (evalOpB Mi vars x2))
def evalOp {A : Type} {n : ℕ} : ((MiddleAbsorption A) → ((vector A n) → ((OpMiddleAbsorptionTerm2 n A) → A)))
| Mi vars (v2 x1) := (nth vars x1)
| Mi vars (sing2 x1) := x1
| Mi vars (opOL2 x1 x2) := ((op Mi) (evalOp Mi vars x1) (evalOp Mi vars x2))
def inductionB {P : (MiddleAbsorptionTerm → Type)} : ((∀ (x1 x2 : MiddleAbsorptionTerm) , ((P x1) → ((P x2) → (P (opL x1 x2))))) → (∀ (x : MiddleAbsorptionTerm) , (P x)))
| popl (opL x1 x2) := (popl _ _ (inductionB popl x1) (inductionB popl x2))
def inductionCl {A : Type} {P : ((ClMiddleAbsorptionTerm A) → Type)} : ((∀ (x1 : A) , (P (sing x1))) → ((∀ (x1 x2 : (ClMiddleAbsorptionTerm A)) , ((P x1) → ((P x2) → (P (opCl x1 x2))))) → (∀ (x : (ClMiddleAbsorptionTerm A)) , (P x))))
| psing popcl (sing x1) := (psing x1)
| psing popcl (opCl x1 x2) := (popcl _ _ (inductionCl psing popcl x1) (inductionCl psing popcl x2))
def inductionOpB {n : ℕ} {P : ((OpMiddleAbsorptionTerm n) → Type)} : ((∀ (fin : (fin n)) , (P (v fin))) → ((∀ (x1 x2 : (OpMiddleAbsorptionTerm n)) , ((P x1) → ((P x2) → (P (opOL x1 x2))))) → (∀ (x : (OpMiddleAbsorptionTerm n)) , (P x))))
| pv popol (v x1) := (pv x1)
| pv popol (opOL x1 x2) := (popol _ _ (inductionOpB pv popol x1) (inductionOpB pv popol x2))
def inductionOp {n : ℕ} {A : Type} {P : ((OpMiddleAbsorptionTerm2 n A) → Type)} : ((∀ (fin : (fin n)) , (P (v2 fin))) → ((∀ (x1 : A) , (P (sing2 x1))) → ((∀ (x1 x2 : (OpMiddleAbsorptionTerm2 n A)) , ((P x1) → ((P x2) → (P (opOL2 x1 x2))))) → (∀ (x : (OpMiddleAbsorptionTerm2 n A)) , (P x)))))
| pv2 psing2 popol2 (v2 x1) := (pv2 x1)
| pv2 psing2 popol2 (sing2 x1) := (psing2 x1)
| pv2 psing2 popol2 (opOL2 x1 x2) := (popol2 _ _ (inductionOp pv2 psing2 popol2 x1) (inductionOp pv2 psing2 popol2 x2))
def stageB : (MiddleAbsorptionTerm → (Staged MiddleAbsorptionTerm))
| (opL x1 x2) := (stage2 opL (codeLift2 opL) (stageB x1) (stageB x2))
def stageCl {A : Type} : ((ClMiddleAbsorptionTerm A) → (Staged (ClMiddleAbsorptionTerm A)))
| (sing x1) := (Now (sing x1))
| (opCl x1 x2) := (stage2 opCl (codeLift2 opCl) (stageCl x1) (stageCl x2))
def stageOpB {n : ℕ} : ((OpMiddleAbsorptionTerm n) → (Staged (OpMiddleAbsorptionTerm n)))
| (v x1) := (const (code (v x1)))
| (opOL x1 x2) := (stage2 opOL (codeLift2 opOL) (stageOpB x1) (stageOpB x2))
def stageOp {n : ℕ} {A : Type} : ((OpMiddleAbsorptionTerm2 n A) → (Staged (OpMiddleAbsorptionTerm2 n A)))
| (sing2 x1) := (Now (sing2 x1))
| (v2 x1) := (const (code (v2 x1)))
| (opOL2 x1 x2) := (stage2 opOL2 (codeLift2 opOL2) (stageOp x1) (stageOp x2))
structure StagedRepr (A : Type) (Repr : (Type → Type)) : Type :=
(opT : ((Repr A) → ((Repr A) → (Repr A))))
end MiddleAbsorption |
abc439eba1efd22846c85acbe72c15d18e4af510 | 8b9f17008684d796c8022dab552e42f0cb6fb347 | /tests/lean/hott/len_eq.hlean | 65c32ac318746903eef4a68a5c82429217c69d54 | [
"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 | 3,070 | hlean | import init.axioms.ua
open nat unit equiv is_trunc
inductive vector (A : Type) : nat → Type :=
| nil {} : vector A zero
| cons : Π {n}, A → vector A n → vector A (succ n)
open vector
notation a :: b := cons a b
definition const {A : Type} : Π (n : nat), A → vector A n
| zero a := nil
| (succ n) a := a :: const n a
definition head {A : Type} : Π {n : nat}, vector A (succ n) → A
| n (x :: xs) := x
theorem singlenton_vector_unit : ∀ {n : nat} (v w : vector unit n), v = w
| zero nil nil := rfl
| (succ n) (star::xs) (star::ys) :=
begin
have h₁ : xs = ys, from singlenton_vector_unit xs ys,
rewrite h₁
end
private definition f (n m : nat) (v : vector unit n) : vector unit m := const m star
theorem vn_eqv_vm (n m : nat) : vector unit n ≃ vector unit m :=
equiv.MK (f n m) (f m n)
(take v : vector unit m, singlenton_vector_unit (f n m (f m n v)) v)
(take v : vector unit n, singlenton_vector_unit (f m n (f n m v)) v)
theorem vn_eq_vm (n m : nat) : vector unit n = vector unit m :=
ua (vn_eqv_vm n m)
definition vector_inj (A : Type) := ∀ (n m : nat), vector A n = vector A m → n = m
theorem not_vector_inj : ¬ vector_inj unit :=
assume H : vector_inj unit,
have aux₁ : 0 = 1, from H 0 1 (vn_eq_vm 0 1),
lift.down (nat.no_confusion aux₁)
definition cast {A B : Type} (H : A = B) (a : A) : B :=
eq.rec_on H a
open sigma
definition heq {A B : Type} (a : A) (b : B) :=
Σ (H : A = B), cast H a = b
infix `==`:50 := heq
definition heq.type_eq {A B : Type} {a : A} {b : B} : a == b → A = B
| ⟨H, e⟩ := H
definition heq.symm : ∀ {A B : Type} {a : A} {b : B}, a == b → b == a
| A A a a ⟨eq.refl A, eq.refl a⟩ := ⟨eq.refl A, eq.refl a⟩
definition heq.trans : ∀ {A B C : Type} {a : A} {b : B} {c : C}, a == b → b == c → a == c
| A A A a a a ⟨eq.refl A, eq.refl a⟩ ⟨eq.refl A, eq.refl a⟩ := ⟨eq.refl A, eq.refl a⟩
theorem cast_heq : ∀ {A B : Type} (H : A = B) (a : A), cast H a == a
| A A (eq.refl A) a := ⟨eq.refl A, eq.refl a⟩
definition default (A : Type) [H : inhabited A] : A :=
inhabited.rec_on H (λ a, a)
definition lem_eq (A : Type) : Type :=
∀ (n m : nat) (v : vector A n) (w : vector A m), v == w → n = m
theorem lem_eq_iff_vector_inj (A : Type) [inh : inhabited A] : lem_eq A ↔ vector_inj A :=
iff.intro
(assume Hl : lem_eq A,
assume n m he,
assert a : A, from default A,
assert v : vector A n, from const n a,
have e₁ : v == cast he v, from heq.symm (cast_heq he v),
Hl n m v (cast he v) e₁)
(assume Hr : vector_inj A,
assume n m v w he,
Hr n m (heq.type_eq he))
theorem lem_eq_of_not_inhabited (A : Type) [ninh : inhabited A → empty] : lem_eq A :=
take (n m : nat),
match n with
| zero :=
match m with
| zero := take v w He, rfl
| (succ m₁) :=
take (v : vector A zero) (w : vector A (succ m₁)),
empty.elim _ (ninh (inhabited.mk (head w)))
end
| (succ n₁) :=
take (v : vector A (succ n₁)) (w : vector A m),
empty.elim _ (ninh (inhabited.mk (head v)))
end
|
b7790953258fd017524a0b5a376940dd531f2a39 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /src/lake/Lake.lean | 2c05f7a86d548a1693f3716d96f80fca45e0c7e4 | [
"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 | 243 | lean | /-
Copyright (c) 2021 Mac Malone. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mac Malone
-/
import Lake.Build
import Lake.Config
import Lake.DSL
import Lake.Version
import Lake.CLI.Actions
|
df624ec8e9e1bf6d7d4ba8bbb56d913def3a4e68 | 947b78d97130d56365ae2ec264df196ce769371a | /tests/lean/run/def5.lean | 8f333ff37921afedb5b202f1b99c14ca4663d257 | [
"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 | 197 | lean | new_frontend
def fib : Nat → Nat
| 0 => 1
| 1 => 1
| n+2 => fib n + fib (n+1)
example : fib 0 = 1 := rfl
example : fib 1 = 1 := rfl
example (n : Nat) : fib (n+2) = fib n + fib (n+1) := rfl
|
7d555776f73c535e9853b152b64d84baea7561c7 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/algebra/pempty_instances.lean | 176f818cb4d65512de8c632724f250378f54e1f5 | [
"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 | 630 | lean | /-
Copyright (c) 2021 Julian Kuelshammer. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Julian Kuelshammer
-/
import algebra.group.defs
import tactic.to_additive
/-!
# Instances on pempty
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
This file collects facts about algebraic structures on the (universe-polymorphic) empty type, e.g.
that it is a semigroup.
-/
universes u
@[to_additive]
instance semigroup_pempty : semigroup pempty.{u+1} :=
{ mul := λ x y, by cases x,
mul_assoc := λ x y z, by cases x }
|
b8efdbede3719ccf37bfab42ac1abe82e4f65fd7 | 69d4931b605e11ca61881fc4f66db50a0a875e39 | /src/linear_algebra/pi.lean | a80ac80b7e0221e8d95be5e27c1027e339cccbd5 | [
"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 | 9,932 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Mario Carneiro, Kevin Buzzard, Yury Kudryashov, Eric Wieser
-/
import linear_algebra.basic
/-!
# Pi types of modules
This file defines constructors for linear maps whose domains or codomains are pi types.
It contains theorems relating these to each other, as well as to `linear_map.ker`.
## Main definitions
- pi types in the codomain:
- `linear_map.pi`
- `linear_map.single`
- pi types in the domain:
- `linear_map.proj`
- `linear_map.diag`
-/
universes u v w x y z u' v' w' y'
variables {R : Type u} {K : Type u'} {M : Type v} {V : Type v'} {M₂ : Type w} {V₂ : Type w'}
variables {M₃ : Type y} {V₃ : Type y'} {M₄ : Type z} {ι : Type x}
open function submodule
open_locale big_operators
namespace linear_map
universe i
variables [semiring R] [add_comm_monoid M₂] [module R M₂] [add_comm_monoid M₃] [module R M₃]
{φ : ι → Type i} [∀i, add_comm_monoid (φ i)] [∀i, module R (φ i)]
/-- `pi` construction for linear functions. From a family of linear functions it produces a linear
function into a family of modules. -/
def pi (f : Πi, M₂ →ₗ[R] φ i) : M₂ →ₗ[R] (Πi, φ i) :=
⟨λc i, f i c, λ c d, funext $ λ i, (f i).map_add _ _, λ c d, funext $ λ i, (f i).map_smul _ _⟩
@[simp] lemma pi_apply (f : Πi, M₂ →ₗ[R] φ i) (c : M₂) (i : ι) :
pi f c i = f i c := rfl
lemma ker_pi (f : Πi, M₂ →ₗ[R] φ i) : ker (pi f) = (⨅i:ι, ker (f i)) :=
by ext c; simp [funext_iff]; refl
lemma pi_eq_zero (f : Πi, M₂ →ₗ[R] φ i) : pi f = 0 ↔ (∀i, f i = 0) :=
by simp only [linear_map.ext_iff, pi_apply, funext_iff]; exact ⟨λh a b, h b a, λh a b, h b a⟩
lemma pi_zero : pi (λi, 0 : Πi, M₂ →ₗ[R] φ i) = 0 :=
by ext; refl
lemma pi_comp (f : Πi, M₂ →ₗ[R] φ i) (g : M₃ →ₗ[R] M₂) : (pi f).comp g = pi (λi, (f i).comp g) :=
rfl
/-- The projections from a family of modules are linear maps. -/
def proj (i : ι) : (Πi, φ i) →ₗ[R] φ i :=
⟨ λa, a i, assume f g, rfl, assume c f, rfl ⟩
@[simp] lemma coe_proj (i : ι) : ⇑(proj i : (Πi, φ i) →ₗ[R] φ i) = function.eval i := rfl
lemma proj_apply (i : ι) (b : Πi, φ i) : (proj i : (Πi, φ i) →ₗ[R] φ i) b = b i := rfl
lemma proj_pi (f : Πi, M₂ →ₗ[R] φ i) (i : ι) : (proj i).comp (pi f) = f i :=
ext $ assume c, rfl
lemma infi_ker_proj : (⨅i, ker (proj i) : submodule R (Πi, φ i)) = ⊥ :=
bot_unique $ set_like.le_def.2 $ assume a h,
begin
simp only [mem_infi, mem_ker, proj_apply] at h,
exact (mem_bot _).2 (funext $ assume i, h i)
end
lemma apply_single [add_comm_monoid M] [module R M] [decidable_eq ι]
(f : Π i, φ i →ₗ[R] M) (i j : ι) (x : φ i) :
f j (pi.single i x j) = pi.single i (f i x) j :=
pi.apply_single (λ i, f i) (λ i, (f i).map_zero) _ _ _
/-- The `linear_map` version of `add_monoid_hom.single` and `pi.single`. -/
def single [decidable_eq ι] (i : ι) : φ i →ₗ[R] (Πi, φ i) :=
{ to_fun := pi.single i,
map_smul' := pi.single_smul i,
.. add_monoid_hom.single φ i}
@[simp] lemma coe_single [decidable_eq ι] (i : ι) :
⇑(single i : φ i →ₗ[R] (Π i, φ i)) = pi.single i := rfl
variables (R φ)
/-- The linear equivalence between linear functions on a finite product of modules and
families of functions on these modules. See note [bundled maps over different rings]. -/
@[simps] def lsum (S) [add_comm_monoid M] [module R M] [fintype ι] [decidable_eq ι]
[semiring S] [module S M] [smul_comm_class R S M] :
(Π i, φ i →ₗ[R] M) ≃ₗ[S] ((Π i, φ i) →ₗ[R] M) :=
{ to_fun := λ f, ∑ i : ι, (f i).comp (proj i),
inv_fun := λ f i, f.comp (single i),
map_add' := λ f g, by simp only [pi.add_apply, add_comp, finset.sum_add_distrib],
map_smul' := λ c f, by simp only [pi.smul_apply, smul_comp, finset.smul_sum],
left_inv := λ f, by { ext i x, simp [apply_single] },
right_inv := λ f,
begin
ext,
suffices : f (∑ j, pi.single j (x j)) = f x, by simpa [apply_single],
rw finset.univ_sum_single
end }
variables {R φ}
section ext
variables [fintype ι] [decidable_eq ι] [add_comm_monoid M] [module R M]
{f g : (Π i, φ i) →ₗ[R] M}
lemma pi_ext (h : ∀ i x, f (pi.single i x) = g (pi.single i x)) :
f = g :=
to_add_monoid_hom_injective $ add_monoid_hom.functions_ext _ _ _ h
lemma pi_ext_iff : f = g ↔ ∀ i x, f (pi.single i x) = g (pi.single i x) :=
⟨λ h i x, h ▸ rfl, pi_ext⟩
/-- This is used as the ext lemma instead of `linear_map.pi_ext` for reasons explained in
note [partially-applied ext lemmas]. -/
@[ext] lemma pi_ext' (h : ∀ i, f.comp (single i) = g.comp (single i)) : f = g :=
begin
refine pi_ext (λ i x, _),
convert linear_map.congr_fun (h i) x
end
lemma pi_ext'_iff : f = g ↔ ∀ i, f.comp (single i) = g.comp (single i) :=
⟨λ h i, h ▸ rfl, pi_ext'⟩
end ext
section
variables (R φ)
/-- If `I` and `J` are disjoint index sets, the product of the kernels of the `J`th projections of
`φ` is linearly equivalent to the product over `I`. -/
def infi_ker_proj_equiv {I J : set ι} [decidable_pred (λi, i ∈ I)]
(hd : disjoint I J) (hu : set.univ ⊆ I ∪ J) :
(⨅i ∈ J, ker (proj i) : submodule R (Πi, φ i)) ≃ₗ[R] (Πi:I, φ i) :=
begin
refine linear_equiv.of_linear
(pi $ λi, (proj (i:ι)).comp (submodule.subtype _))
(cod_restrict _ (pi $ λi, if h : i ∈ I then proj (⟨i, h⟩ : I) else 0) _) _ _,
{ assume b,
simp only [mem_infi, mem_ker, funext_iff, proj_apply, pi_apply],
assume j hjJ,
have : j ∉ I := assume hjI, hd ⟨hjI, hjJ⟩,
rw [dif_neg this, zero_apply] },
{ simp only [pi_comp, comp_assoc, subtype_comp_cod_restrict, proj_pi, dif_pos, subtype.coe_prop],
ext b ⟨j, hj⟩, refl },
{ ext1 ⟨b, hb⟩,
apply subtype.ext,
ext j,
have hb : ∀i ∈ J, b i = 0,
{ simpa only [mem_infi, mem_ker, proj_apply] using (mem_infi _).1 hb },
simp only [comp_apply, pi_apply, id_apply, proj_apply, subtype_apply, cod_restrict_apply],
split_ifs,
{ refl },
{ exact (hb _ $ (hu trivial).resolve_left h).symm } }
end
end
section
variable [decidable_eq ι]
/-- `diag i j` is the identity map if `i = j`. Otherwise it is the constant 0 map. -/
def diag (i j : ι) : φ i →ₗ[R] φ j :=
@function.update ι (λj, φ i →ₗ[R] φ j) _ 0 i id j
lemma update_apply (f : Πi, M₂ →ₗ[R] φ i) (c : M₂) (i j : ι) (b : M₂ →ₗ[R] φ i) :
(update f i b j) c = update (λi, f i c) i (b c) j :=
begin
by_cases j = i,
{ rw [h, update_same, update_same] },
{ rw [update_noteq h, update_noteq h] }
end
end
end linear_map
namespace submodule
variables [semiring R] {φ : ι → Type*} [∀ i, add_comm_monoid (φ i)] [∀ i, module R (φ i)]
open linear_map
/-- A version of `set.pi` for submodules. Given an index set `I` and a family of submodules
`p : Π i, submodule R (φ i)`, `pi I s` is the submodule of dependent functions `f : Π i, φ i`
such that `f i` belongs to `p a` whenever `i ∈ I`. -/
def pi (I : set ι) (p : Π i, submodule R (φ i)) : submodule R (Π i, φ i) :=
{ carrier := set.pi I (λ i, p i),
zero_mem' := λ i hi, (p i).zero_mem,
add_mem' := λ x y hx hy i hi, (p i).add_mem (hx i hi) (hy i hi),
smul_mem' := λ c x hx i hi, (p i).smul_mem c (hx i hi) }
variables {I : set ι} {p : Π i, submodule R (φ i)} {x : Π i, φ i}
@[simp] lemma mem_pi : x ∈ pi I p ↔ ∀ i ∈ I, x i ∈ p i := iff.rfl
@[simp, norm_cast] lemma coe_pi : (pi I p : set (Π i, φ i)) = set.pi I (λ i, p i) := rfl
lemma binfi_comap_proj : (⨅ i ∈ I, comap (proj i) (p i)) = pi I p :=
by { ext x, simp }
lemma infi_comap_proj : (⨅ i, comap (proj i) (p i)) = pi set.univ p :=
by { ext x, simp }
lemma supr_map_single [decidable_eq ι] [fintype ι] :
(⨆ i, map (linear_map.single i) (p i)) = pi set.univ p :=
begin
refine (supr_le $ λ i, _).antisymm _,
{ rintro _ ⟨x, hx : x ∈ p i, rfl⟩ j -,
rcases em (j = i) with rfl|hj; simp * },
{ intros x hx,
rw [← finset.univ_sum_single x],
exact sum_mem_supr (λ i, mem_map_of_mem (hx i trivial)) }
end
end submodule
namespace linear_equiv
variables [semiring R] {φ ψ : ι → Type*} [∀i, add_comm_monoid (φ i)] [∀i, module R (φ i)]
[∀i, add_comm_monoid (ψ i)] [∀i, module R (ψ i)]
/-- Combine a family of linear equivalences into a linear equivalence of `pi`-types. -/
@[simps] def pi (e : Π i, φ i ≃ₗ[R] ψ i) : (Π i, φ i) ≃ₗ[R] (Π i, ψ i) :=
{ to_fun := λ f i, e i (f i),
inv_fun := λ f i, (e i).symm (f i),
map_add' := λ f g, by { ext, simp },
map_smul' := λ c f, by { ext, simp },
left_inv := λ f, by { ext, simp },
right_inv := λ f, by { ext, simp } }
variables (ι R M) (S : Type*) [fintype ι] [decidable_eq ι] [semiring S]
[add_comm_monoid M] [module R M] [module S M] [smul_comm_class R S M]
/-- Linear equivalence between linear functions `Rⁿ → M` and `Mⁿ`. The spaces `Rⁿ` and `Mⁿ`
are represented as `ι → R` and `ι → M`, respectively, where `ι` is a finite type.
This as an `S`-linear equivalence, under the assumption that `S` acts on `M` commuting with `R`.
When `R` is commutative, we can take this to be the usual action with `S = R`.
Otherwise, `S = ℕ` shows that the equivalence is additive.
See note [bundled maps over different rings]. -/
def pi_ring : ((ι → R) →ₗ[R] M) ≃ₗ[S] (ι → M) :=
(linear_map.lsum R (λ i : ι, R) S).symm.trans
(pi $ λ i, linear_map.ring_lmap_equiv_self R M S)
variables {ι R M}
@[simp] lemma pi_ring_apply (f : (ι → R) →ₗ[R] M) (i : ι) :
pi_ring R M ι S f i = f (pi.single i 1) :=
rfl
@[simp] lemma pi_ring_symm_apply (f : ι → M) (g : ι → R) :
(pi_ring R M ι S).symm f g = ∑ i, g i • f i :=
by simp [pi_ring, linear_map.lsum]
end linear_equiv
|
c3e00c389602c2fc3f358af8907449210698b65e | a047a4718edfa935d17231e9e6ecec8c7b701e05 | /src/ring_theory/algebra.lean | 94084958f712d2f1898e2d526b75a4b080a1f7f2 | [
"Apache-2.0"
] | permissive | utensil-contrib/mathlib | bae0c9fafe5e2bdb516efc89d6f8c1502ecc9767 | b91909e77e219098a2f8cc031f89d595fe274bd2 | refs/heads/master | 1,668,048,976,965 | 1,592,442,701,000 | 1,592,442,701,000 | 273,197,855 | 0 | 0 | null | 1,592,472,812,000 | 1,592,472,811,000 | null | UTF-8 | Lean | false | false | 34,413 | lean | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Yury Kudryashov
-/
import data.matrix.basic
import linear_algebra.tensor_product
import data.equiv.ring
/-!
# Algebra over Commutative Semiring (under category)
In this file we define algebra over commutative (semi)rings, algebra homomorphisms `alg_hom`,
algebra equivalences `alg_equiv`, and `subalgebra`s. We also define usual operations on `alg_hom`s
(`id`, `comp`) and subalgebras (`map`, `comap`).
## Notations
* `A →ₐ[R] B` : `R`-algebra homomorphism from `A` to `B`.
* `A ≃ₐ[R] B` : `R`-algebra equivalence from `A` to `B`.
-/
noncomputable theory
universes u v w u₁ v₁
open_locale tensor_product big_operators
section prio
-- We set this priority to 0 later in this file
set_option default_priority 200 -- see Note [default priority]
/-- The category of R-algebras where R is a commutative
ring is the under category R ↓ CRing. In the categorical
setting we have a forgetful functor R-Alg ⥤ R-Mod.
However here it extends module in order to preserve
definitional equality in certain cases. -/
@[nolint has_inhabited_instance]
class algebra (R : Type u) (A : Type v) [comm_semiring R] [semiring A]
extends has_scalar R A, R →+* A :=
(commutes' : ∀ r x, to_fun r * x = x * to_fun r)
(smul_def' : ∀ r x, r • x = to_fun r * x)
end prio
/-- Embedding `R →+* A` given by `algebra` structure. -/
def algebra_map (R : Type u) (A : Type v) [comm_semiring R] [semiring A] [algebra R A] : R →+* A :=
algebra.to_ring_hom
/-- Creating an algebra from a morphism to the center of a semiring. -/
def ring_hom.to_algebra' {R S} [comm_semiring R] [semiring S] (i : R →+* S)
(h : ∀ c x, i c * x = x * i c) :
algebra R S :=
{ smul := λ c x, i c * x,
commutes' := h,
smul_def' := λ c x, rfl,
.. i}
/-- Creating an algebra from a morphism to a commutative semiring. -/
def ring_hom.to_algebra {R S} [comm_semiring R] [comm_semiring S] (i : R →+* S) :
algebra R S :=
i.to_algebra' $ λ _, mul_comm _
namespace algebra
variables {R : Type u} {S : Type v} {A : Type w}
/-- Let `R` be a commutative semiring, let `A` be a semiring with a `semimodule R` structure.
If `(r • 1) * x = x * (r • 1) = r • x` for all `r : R` and `x : A`, then `A` is an `algebra`
over `R`. -/
def of_semimodule' [comm_semiring R] [semiring A] [semimodule R A]
(h₁ : ∀ (r : R) (x : A), (r • 1) * x = r • x)
(h₂ : ∀ (r : R) (x : A), x * (r • 1) = r • x) : algebra R A :=
{ to_fun := λ r, r • 1,
map_one' := one_smul _ _,
map_mul' := λ r₁ r₂, by rw [h₁, mul_smul],
map_zero' := zero_smul _ _,
map_add' := λ r₁ r₂, add_smul r₁ r₂ 1,
commutes' := λ r x, by simp only [h₁, h₂],
smul_def' := λ r x, by simp only [h₁] }
/-- Let `R` be a commutative semiring, let `A` be a semiring with a `semimodule R` structure.
If `(r • x) * y = x * (r • y) = r • (x * y)` for all `r : R` and `x y : A`, then `A`
is an `algebra` over `R`. -/
def of_semimodule [comm_semiring R] [semiring A] [semimodule R A]
(h₁ : ∀ (r : R) (x y : A), (r • x) * y = r • (x * y))
(h₂ : ∀ (r : R) (x y : A), x * (r • y) = r • (x * y)) : algebra R A :=
of_semimodule' (λ r x, by rw [h₁, one_mul]) (λ r x, by rw [h₂, mul_one])
section semiring
variables [comm_semiring R] [comm_semiring S] [semiring A] [algebra R A]
lemma smul_def'' (r : R) (x : A) : r • x = algebra_map R A r * x :=
algebra.smul_def' r x
@[priority 200] -- see Note [lower instance priority]
instance to_semimodule : semimodule R A :=
{ one_smul := by simp [smul_def''],
mul_smul := by simp [smul_def'', mul_assoc],
smul_add := by simp [smul_def'', mul_add],
smul_zero := by simp [smul_def''],
add_smul := by simp [smul_def'', add_mul],
zero_smul := by simp [smul_def''] }
-- from now on, we don't want to use the following instance anymore
attribute [instance, priority 0] algebra.to_has_scalar
lemma smul_def (r : R) (x : A) : r • x = algebra_map R A r * x :=
algebra.smul_def' r x
theorem commutes (r : R) (x : A) : algebra_map R A r * x = x * algebra_map R A r :=
algebra.commutes' r x
theorem left_comm (r : R) (x y : A) : x * (algebra_map R A r * y) = algebra_map R A r * (x * y) :=
by rw [← mul_assoc, ← commutes, mul_assoc]
@[simp] lemma mul_smul_comm (s : R) (x y : A) :
x * (s • y) = s • (x * y) :=
by rw [smul_def, smul_def, left_comm]
@[simp] lemma smul_mul_assoc (r : R) (x y : A) :
(r • x) * y = r • (x * y) :=
by rw [smul_def, smul_def, mul_assoc]
end semiring
-- TODO (semimodule linear maps): once we have them, port next section to semirings
section ring
variables [comm_ring R] [ring A] [algebra R A]
/-- Creating an algebra from a subring. This is the dual of ring extension. -/
instance of_subring (S : set R) [is_subring S] : algebra S R :=
ring_hom.to_algebra ⟨coe, rfl, λ _ _, rfl, rfl, λ _ _, rfl⟩
variables (R A)
/-- The multiplication in an algebra is a bilinear map. -/
def lmul : A →ₗ A →ₗ A :=
linear_map.mk₂ R (*)
(λ x y z, add_mul x y z)
(λ c x y, by rw [smul_def, smul_def, mul_assoc _ x y])
(λ x y z, mul_add x y z)
(λ c x y, by rw [smul_def, smul_def, left_comm])
/-- The multiplication on the left in an algebra is a linear map. -/
def lmul_left (r : A) : A →ₗ A :=
lmul R A r
/-- The multiplication on the right in an algebra is a linear map. -/
def lmul_right (r : A) : A →ₗ A :=
(lmul R A).flip r
variables {R A}
@[simp] lemma lmul_apply (p q : A) : lmul R A p q = p * q := rfl
@[simp] lemma lmul_left_apply (p q : A) : lmul_left R A p q = p * q := rfl
@[simp] lemma lmul_right_apply (p q : A) : lmul_right R A p q = q * p := rfl
end ring
end algebra
instance module.endomorphism_algebra (R : Type u) (M : Type v)
[comm_ring R] [add_comm_group M] [module R M] : algebra R (M →ₗ[R] M) :=
{ to_fun := λ r, r • linear_map.id,
map_one' := one_smul _ _,
map_zero' := zero_smul _ _,
map_add' := λ r₁ r₂, add_smul _ _ _,
map_mul' := λ r₁ r₂, by { ext x, simp [mul_smul] },
commutes' := by { intros, ext, simp },
smul_def' := by { intros, ext, simp } }
instance matrix_algebra (n : Type u) (R : Type v)
[fintype n] [decidable_eq n] [comm_semiring R] : algebra R (matrix n n R) :=
{ to_fun := λ r, r • 1,
map_one' := one_smul _ _,
map_mul' := λ r₁ r₂, by { ext, simp [mul_assoc] },
map_zero' := zero_smul _ _,
map_add' := λ _ _, add_smul _ _ _,
commutes' := by { intros, simp },
smul_def' := by { intros, simp } }
set_option old_structure_cmd true
/-- Defining the homomorphism in the category R-Alg. -/
@[nolint has_inhabited_instance]
structure alg_hom (R : Type u) (A : Type v) (B : Type w)
[comm_semiring R] [semiring A] [semiring B] [algebra R A] [algebra R B] extends ring_hom A B :=
(commutes' : ∀ r : R, to_fun (algebra_map R A r) = algebra_map R B r)
run_cmd tactic.add_doc_string `alg_hom.to_ring_hom "Reinterpret an `alg_hom` as a `ring_hom`"
infixr ` →ₐ `:25 := alg_hom _
notation A ` →ₐ[`:25 R `] ` B := alg_hom R A B
namespace alg_hom
variables {R : Type u} {A : Type v} {B : Type w} {C : Type u₁} {D : Type v₁}
section semiring
variables [comm_semiring R] [semiring A] [semiring B] [semiring C] [semiring D]
variables [algebra R A] [algebra R B] [algebra R C] [algebra R D]
instance : has_coe_to_fun (A →ₐ[R] B) := ⟨_, λ f, f.to_fun⟩
instance coe_ring_hom : has_coe (A →ₐ[R] B) (A →+* B) := ⟨alg_hom.to_ring_hom⟩
instance coe_monoid_hom : has_coe (A →ₐ[R] B) (A →* B) := ⟨λ f, ↑(f : A →+* B)⟩
instance coe_add_monoid_hom : has_coe (A →ₐ[R] B) (A →+ B) := ⟨λ f, ↑(f : A →+* B)⟩
@[simp, norm_cast] lemma coe_mk {f : A → B} (h₁ h₂ h₃ h₄ h₅) :
⇑(⟨f, h₁, h₂, h₃, h₄, h₅⟩ : A →ₐ[R] B) = f := rfl
@[simp, norm_cast] lemma coe_to_ring_hom (f : A →ₐ[R] B) : ⇑(f : A →+* B) = f := rfl
-- as `simp` can already prove this lemma, it is not tagged with the `simp` attribute.
@[norm_cast] lemma coe_to_monoid_hom (f : A →ₐ[R] B) : ⇑(f : A →* B) = f := rfl
-- as `simp` can already prove this lemma, it is not tagged with the `simp` attribute.
@[norm_cast] lemma coe_to_add_monoid_hom (f : A →ₐ[R] B) : ⇑(f : A →+ B) = f := rfl
variables (φ : A →ₐ[R] B)
theorem coe_fn_inj ⦃φ₁ φ₂ : A →ₐ[R] B⦄ (H : ⇑φ₁ = φ₂) : φ₁ = φ₂ :=
by { cases φ₁, cases φ₂, congr, exact H }
theorem coe_ring_hom_inj : function.injective (coe : (A →ₐ[R] B) → (A →+* B)) :=
λ φ₁ φ₂ H, coe_fn_inj $ show ((φ₁ : (A →+* B)) : A → B) = ((φ₂ : (A →+* B)) : A → B),
from congr_arg _ H
theorem coe_monoid_hom_inj : function.injective (coe : (A →ₐ[R] B) → (A →* B)) :=
ring_hom.coe_monoid_hom_inj.comp coe_ring_hom_inj
theorem coe_add_monoid_hom_inj : function.injective (coe : (A →ₐ[R] B) → (A →+ B)) :=
ring_hom.coe_add_monoid_hom_inj.comp coe_ring_hom_inj
@[ext]
theorem ext {φ₁ φ₂ : A →ₐ[R] B} (H : ∀ x, φ₁ x = φ₂ x) : φ₁ = φ₂ :=
coe_fn_inj $ funext H
theorem ext_iff {φ₁ φ₂ : A →ₐ[R] B} : φ₁ = φ₂ ↔ ∀ x, φ₁ x = φ₂ x :=
⟨by { rintro rfl x, refl }, ext⟩
@[simp]
theorem commutes (r : R) : φ (algebra_map R A r) = algebra_map R B r := φ.commutes' r
theorem comp_algebra_map : φ.to_ring_hom.comp (algebra_map R A) = algebra_map R B :=
ring_hom.ext $ φ.commutes
@[simp] lemma map_add (r s : A) : φ (r + s) = φ r + φ s :=
φ.to_ring_hom.map_add r s
@[simp] lemma map_zero : φ 0 = 0 :=
φ.to_ring_hom.map_zero
@[simp] lemma map_mul (x y) : φ (x * y) = φ x * φ y :=
φ.to_ring_hom.map_mul x y
@[simp] lemma map_one : φ 1 = 1 :=
φ.to_ring_hom.map_one
@[simp] lemma map_smul (r : R) (x : A) : φ (r • x) = r • φ x :=
by simp only [algebra.smul_def, map_mul, commutes]
@[simp] lemma map_pow (x : A) (n : ℕ) : φ (x ^ n) = (φ x) ^ n :=
φ.to_ring_hom.map_pow x n
lemma map_sum {ι : Type*} (f : ι → A) (s : finset ι) :
φ (∑ x in s, f x) = ∑ x in s, φ (f x) :=
φ.to_ring_hom.map_sum f s
section
variables (R A)
/-- Identity map as an `alg_hom`. -/
protected def id : A →ₐ[R] A :=
{ commutes' := λ _, rfl,
..ring_hom.id A }
end
@[simp] lemma id_apply (p : A) : alg_hom.id R A p = p := rfl
/-- Composition of algebra homeomorphisms. -/
def comp (φ₁ : B →ₐ[R] C) (φ₂ : A →ₐ[R] B) : A →ₐ[R] C :=
{ commutes' := λ r : R, by rw [← φ₁.commutes, ← φ₂.commutes]; refl,
.. φ₁.to_ring_hom.comp ↑φ₂ }
@[simp] lemma comp_apply (φ₁ : B →ₐ[R] C) (φ₂ : A →ₐ[R] B) (p : A) :
φ₁.comp φ₂ p = φ₁ (φ₂ p) := rfl
@[simp] theorem comp_id : φ.comp (alg_hom.id R A) = φ :=
ext $ λ x, rfl
@[simp] theorem id_comp : (alg_hom.id R B).comp φ = φ :=
ext $ λ x, rfl
theorem comp_assoc (φ₁ : C →ₐ[R] D) (φ₂ : B →ₐ[R] C) (φ₃ : A →ₐ[R] B) :
(φ₁.comp φ₂).comp φ₃ = φ₁.comp (φ₂.comp φ₃) :=
ext $ λ x, rfl
end semiring
section comm_semiring
variables [comm_semiring R] [comm_semiring A] [comm_semiring B]
variables [algebra R A] [algebra R B]
variables (φ : A →ₐ[R] B)
lemma map_prod {ι : Type*} (f : ι → A) (s : finset ι) :
φ (∏ x in s, f x) = ∏ x in s, φ (f x) :=
φ.to_ring_hom.map_prod f s
end comm_semiring
variables [comm_ring R] [ring A] [ring B] [ring C]
variables [algebra R A] [algebra R B] [algebra R C] (φ : A →ₐ[R] B)
@[simp] lemma map_neg (x) : φ (-x) = -φ x :=
φ.to_ring_hom.map_neg x
@[simp] lemma map_sub (x y) : φ (x - y) = φ x - φ y :=
φ.to_ring_hom.map_sub x y
/-- R-Alg ⥤ R-Mod -/
def to_linear_map : A →ₗ B :=
{ to_fun := φ,
map_add' := φ.map_add,
map_smul' := φ.map_smul }
@[simp] lemma to_linear_map_apply (p : A) : φ.to_linear_map p = φ p := rfl
theorem to_linear_map_inj {φ₁ φ₂ : A →ₐ[R] B} (H : φ₁.to_linear_map = φ₂.to_linear_map) : φ₁ = φ₂ :=
ext $ λ x, show φ₁.to_linear_map x = φ₂.to_linear_map x, by rw H
@[simp] lemma comp_to_linear_map (f : A →ₐ[R] B) (g : B →ₐ[R] C) :
(g.comp f).to_linear_map = g.to_linear_map.comp f.to_linear_map := rfl
end alg_hom
set_option old_structure_cmd true
/-- An equivalence of algebras is an equivalence of rings commuting with the actions of scalars. -/
structure alg_equiv (R : Type u) (A : Type v) (B : Type w)
[comm_semiring R] [semiring A] [semiring B] [algebra R A] [algebra R B]
extends A ≃ B, A ≃* B, A ≃+ B, A ≃+* B :=
(commutes' : ∀ r : R, to_fun (algebra_map R A r) = algebra_map R B r)
attribute [nolint doc_blame] alg_equiv.to_ring_equiv
attribute [nolint doc_blame] alg_equiv.to_equiv
attribute [nolint doc_blame] alg_equiv.to_add_equiv
attribute [nolint doc_blame] alg_equiv.to_mul_equiv
notation A ` ≃ₐ[`:50 R `] ` A' := alg_equiv R A A'
namespace alg_equiv
variables {R : Type u} {A₁ : Type v} {A₂ : Type w} {A₃ : Type u₁}
variables [comm_semiring R] [semiring A₁] [semiring A₂] [semiring A₃]
variables [algebra R A₁] [algebra R A₂] [algebra R A₃]
instance : has_coe_to_fun (A₁ ≃ₐ[R] A₂) := ⟨_, alg_equiv.to_fun⟩
@[ext]
lemma ext {f g : A₁ ≃ₐ[R] A₂} (h : ∀ a, f a = g a) : f = g :=
begin
have h₁ : f.to_equiv = g.to_equiv := equiv.ext h,
cases f, cases g, congr,
{ exact (funext h) },
{ exact congr_arg equiv.inv_fun h₁ }
end
lemma coe_fun_injective : @function.injective (A₁ ≃ₐ[R] A₂) (A₁ → A₂) (λ e, (e : A₁ → A₂)) :=
begin
intros f g w,
ext,
exact congr_fun w a,
end
instance has_coe_to_ring_equiv : has_coe (A₁ ≃ₐ[R] A₂) (A₁ ≃+* A₂) := ⟨alg_equiv.to_ring_equiv⟩
@[simp] lemma mk_apply {to_fun inv_fun left_inv right_inv map_mul map_add commutes a} :
(⟨to_fun, inv_fun, left_inv, right_inv, map_mul, map_add, commutes⟩ : A₁ ≃ₐ[R] A₂) a = to_fun a :=
rfl
@[simp] lemma to_fun_apply {e : A₁ ≃ₐ[R] A₂} {a : A₁} : e.to_fun a = e a := rfl
@[simp, norm_cast] lemma coe_ring_equiv (e : A₁ ≃ₐ[R] A₂) : ((e : A₁ ≃+* A₂) : A₁ → A₂) = e := rfl
lemma coe_ring_equiv_injective : function.injective (λ e : A₁ ≃ₐ[R] A₂, (e : A₁ ≃+* A₂)) :=
begin
intros f g w,
ext,
replace w : ((f : A₁ ≃+* A₂) : A₁ → A₂) = ((g : A₁ ≃+* A₂) : A₁ → A₂) :=
congr_arg (λ e : A₁ ≃+* A₂, (e : A₁ → A₂)) w,
exact congr_fun w a,
end
@[simp] lemma map_add (e : A₁ ≃ₐ[R] A₂) : ∀ x y, e (x + y) = e x + e y := e.to_add_equiv.map_add
@[simp] lemma map_zero (e : A₁ ≃ₐ[R] A₂) : e 0 = 0 := e.to_add_equiv.map_zero
@[simp] lemma map_mul (e : A₁ ≃ₐ[R] A₂) : ∀ x y, e (x * y) = (e x) * (e y) := e.to_mul_equiv.map_mul
@[simp] lemma map_one (e : A₁ ≃ₐ[R] A₂) : e 1 = 1 := e.to_mul_equiv.map_one
@[simp] lemma commutes (e : A₁ ≃ₐ[R] A₂) : ∀ (r : R), e (algebra_map R A₁ r) = algebra_map R A₂ r :=
e.commutes'
@[simp] lemma map_neg {A₁ : Type v} {A₂ : Type w}
[ring A₁] [ring A₂] [algebra R A₁] [algebra R A₂] (e : A₁ ≃ₐ[R] A₂) :
∀ x, e (-x) = -(e x) := e.to_add_equiv.map_neg
@[simp] lemma map_sub {A₁ : Type v} {A₂ : Type w}
[ring A₁] [ring A₂] [algebra R A₁] [algebra R A₂] (e : A₁ ≃ₐ[R] A₂) :
∀ x y, e (x - y) = e x - e y := e.to_add_equiv.map_sub
instance has_coe_to_alg_hom : has_coe (A₁ ≃ₐ[R] A₂) (A₁ →ₐ[R] A₂) :=
⟨λ e, { map_one' := e.map_one, map_zero' := e.map_zero, ..e }⟩
@[simp, norm_cast] lemma coe_alg_hom (e : A₁ ≃ₐ[R] A₂) : ((e : A₁ →ₐ[R] A₂) : A₁ → A₂) = e :=
rfl
lemma injective (e : A₁ ≃ₐ[R] A₂) : function.injective e := e.to_equiv.injective
lemma surjective (e : A₁ ≃ₐ[R] A₂) : function.surjective e := e.to_equiv.surjective
lemma bijective (e : A₁ ≃ₐ[R] A₂) : function.bijective e := e.to_equiv.bijective
instance : has_one (A₁ ≃ₐ[R] A₁) := ⟨{commutes' := λ r, rfl, ..(1 : A₁ ≃+* A₁)}⟩
instance : inhabited (A₁ ≃ₐ[R] A₁) := ⟨1⟩
/-- Algebra equivalences are reflexive. -/
@[refl]
def refl : A₁ ≃ₐ[R] A₁ := 1
@[simp] lemma coe_refl : (@refl R A₁ _ _ _ : A₁ →ₐ[R] A₁) = alg_hom.id R A₁ :=
alg_hom.ext (λ x, rfl)
/-- Algebra equivalences are symmetric. -/
@[symm]
def symm (e : A₁ ≃ₐ[R] A₂) : A₂ ≃ₐ[R] A₁ :=
{ commutes' := λ r, by { rw ←e.to_ring_equiv.symm_apply_apply (algebra_map R A₁ r), congr,
change _ = e _, rw e.commutes, },
..e.to_ring_equiv.symm, }
@[simp] lemma inv_fun_apply {e : A₁ ≃ₐ[R] A₂} {a : A₂} : e.inv_fun a = e.symm a := rfl
/-- Algebra equivalences are transitive. -/
@[trans]
def trans (e₁ : A₁ ≃ₐ[R] A₂) (e₂ : A₂ ≃ₐ[R] A₃) : A₁ ≃ₐ[R] A₃ :=
{ commutes' := λ r, show e₂.to_fun (e₁.to_fun _) = _, by rw [e₁.commutes', e₂.commutes'],
..(e₁.to_ring_equiv.trans e₂.to_ring_equiv), }
@[simp] lemma apply_symm_apply (e : A₁ ≃ₐ[R] A₂) : ∀ x, e (e.symm x) = x :=
e.to_equiv.apply_symm_apply
@[simp] lemma symm_apply_apply (e : A₁ ≃ₐ[R] A₂) : ∀ x, e.symm (e x) = x :=
e.to_equiv.symm_apply_apply
@[simp] lemma comp_symm (e : A₁ ≃ₐ[R] A₂) :
alg_hom.comp (e : A₁ →ₐ[R] A₂) ↑e.symm = alg_hom.id R A₂ :=
by { ext, simp }
@[simp] lemma symm_comp (e : A₁ ≃ₐ[R] A₂) :
alg_hom.comp ↑e.symm (e : A₁ →ₐ[R] A₂) = alg_hom.id R A₁ :=
by { ext, simp }
/-- If an algebra morphism has an inverse, it is a algebra isomorphism. -/
def of_alg_hom (f : A₁ →ₐ[R] A₂) (g : A₂ →ₐ[R] A₁) (h₁ : f.comp g = alg_hom.id R A₂) (h₂ : g.comp f = alg_hom.id R A₁) : A₁ ≃ₐ[R] A₂ :=
{ inv_fun := g,
left_inv := alg_hom.ext_iff.1 h₂,
right_inv := alg_hom.ext_iff.1 h₁,
..f }
end alg_equiv
namespace algebra
variables (R : Type u) (S : Type v) (A : Type w)
include R S A
/-- `comap R S A` is a type alias for `A`, and has an R-algebra structure defined on it
when `algebra R S` and `algebra S A`. -/
/- This is done to avoid a type class search with meta-variables `algebra R ?m_1` and
`algebra ?m_1 A -/
/- The `nolint` attribute is added because it has unused arguments `R` and `S`, but these are necessary for synthesizing the
appropriate type classes -/
@[nolint unused_arguments]
def comap : Type w := A
instance comap.inhabited [h : inhabited A] : inhabited (comap R S A) := h
instance comap.semiring [h : semiring A] : semiring (comap R S A) := h
instance comap.ring [h : ring A] : ring (comap R S A) := h
instance comap.comm_semiring [h : comm_semiring A] : comm_semiring (comap R S A) := h
instance comap.comm_ring [h : comm_ring A] : comm_ring (comap R S A) := h
instance comap.algebra' [comm_semiring S] [semiring A] [h : algebra S A] :
algebra S (comap R S A) := h
/-- Identity homomorphism `A →ₐ[S] comap R S A`. -/
def comap.to_comap [comm_semiring S] [semiring A] [algebra S A] :
A →ₐ[S] comap R S A := alg_hom.id S A
/-- Identity homomorphism `comap R S A →ₐ[S] A`. -/
def comap.of_comap [comm_semiring S] [semiring A] [algebra S A] :
comap R S A →ₐ[S] A := alg_hom.id S A
variables [comm_semiring R] [comm_semiring S] [semiring A] [algebra R S] [algebra S A]
/-- `R ⟶ S` induces `S-Alg ⥤ R-Alg` -/
instance comap.algebra : algebra R (comap R S A) :=
{ smul := λ r x, (algebra_map R S r • x : A),
commutes' := λ r x, algebra.commutes _ _,
smul_def' := λ _ _, algebra.smul_def _ _,
.. (algebra_map S A).comp (algebra_map R S) }
/-- Embedding of `S` into `comap R S A`. -/
def to_comap : S →ₐ[R] comap R S A :=
{ commutes' := λ r, rfl,
.. algebra_map S A }
theorem to_comap_apply (x) : to_comap R S A x = algebra_map S A x := rfl
end algebra
namespace alg_hom
variables {R : Type u} {S : Type v} {A : Type w} {B : Type u₁}
variables [comm_semiring R] [comm_semiring S] [semiring A] [semiring B]
variables [algebra R S] [algebra S A] [algebra S B] (φ : A →ₐ[S] B)
include R
/-- R ⟶ S induces S-Alg ⥤ R-Alg -/
def comap : algebra.comap R S A →ₐ[R] algebra.comap R S B :=
{ commutes' := λ r, φ.commutes (algebra_map R S r)
..φ }
end alg_hom
namespace rat
instance algebra_rat {α} [division_ring α] [char_zero α] : algebra ℚ α :=
(rat.cast_hom α).to_algebra' $ λ r x, r.cast_commute x
end rat
/-- A subalgebra is a subring that includes the range of `algebra_map`. -/
structure subalgebra (R : Type u) (A : Type v)
[comm_ring R] [ring A] [algebra R A] : Type v :=
(carrier : set A) [subring : is_subring carrier]
(range_le' : set.range (algebra_map R A) ≤ carrier)
namespace subalgebra
variables {R : Type u} {A : Type v}
variables [comm_ring R] [ring A] [algebra R A]
include R
instance : has_coe (subalgebra R A) (set A) :=
⟨λ S, S.carrier⟩
lemma range_le (S : subalgebra R A) : set.range (algebra_map R A) ≤ S := S.range_le'
instance : has_mem A (subalgebra R A) :=
⟨λ x S, x ∈ (S : set A)⟩
variables {A}
theorem mem_coe {x : A} {s : subalgebra R A} : x ∈ (s : set A) ↔ x ∈ s :=
iff.rfl
@[ext] theorem ext {S T : subalgebra R A}
(h : ∀ x : A, x ∈ S ↔ x ∈ T) : S = T :=
by cases S; cases T; congr; ext x; exact h x
theorem ext_iff {S T : subalgebra R A} : S = T ↔ ∀ x : A, x ∈ S ↔ x ∈ T :=
⟨λ h x, by rw h, ext⟩
variables (S : subalgebra R A)
instance : is_subring (S : set A) := S.subring
instance : ring S := @@subtype.ring _ S.is_subring
instance : inhabited S := ⟨0⟩
instance (R : Type u) (A : Type v) [comm_ring R] [comm_ring A]
[algebra R A] (S : subalgebra R A) : comm_ring S := @@subtype.comm_ring _ S.is_subring
instance algebra : algebra R S :=
{ smul := λ (c:R) x, ⟨c • x.1,
by rw algebra.smul_def; exact @@is_submonoid.mul_mem _ S.2.2 (S.3 ⟨c, rfl⟩) x.2⟩,
commutes' := λ c x, subtype.eq $ algebra.commutes _ _,
smul_def' := λ c x, subtype.eq $ algebra.smul_def _ _,
.. (algebra_map R A).cod_restrict S $ λ x, S.range_le ⟨x, rfl⟩ }
instance to_algebra (R : Type u) (A : Type v) [comm_ring R] [comm_ring A]
[algebra R A] (S : subalgebra R A) : algebra S A :=
algebra.of_subring _
/-- Embedding of a subalgebra into the algebra. -/
def val : S →ₐ[R] A :=
by refine_struct { to_fun := subtype.val }; intros; refl
/-- Convert a `subalgebra` to `submodule` -/
def to_submodule : submodule R A :=
{ carrier := S,
zero_mem' := (0:S).2,
add_mem' := λ x y hx hy, (⟨x, hx⟩ + ⟨y, hy⟩ : S).2,
smul_mem' := λ c x hx, (algebra.smul_def c x).symm ▸
(⟨algebra_map R A c, S.range_le ⟨c, rfl⟩⟩ * ⟨x, hx⟩:S).2 }
instance coe_to_submodule : has_coe (subalgebra R A) (submodule R A) :=
⟨to_submodule⟩
instance to_submodule.is_subring : is_subring ((S : submodule R A) : set A) := S.2
instance : partial_order (subalgebra R A) :=
{ le := λ S T, (S : set A) ≤ (T : set A),
le_refl := λ _, le_refl _,
le_trans := λ _ _ _, le_trans,
le_antisymm := λ S T hst hts, ext $ λ x, ⟨@hst x, @hts x⟩ }
/-- Reinterpret an `S`-subalgebra as an `R`-subalgebra in `comap R S A`. -/
def comap {R : Type u} {S : Type v} {A : Type w}
[comm_ring R] [comm_ring S] [ring A] [algebra R S] [algebra S A]
(iSB : subalgebra S A) : subalgebra R (algebra.comap R S A) :=
{ carrier := (iSB : set A),
subring := iSB.is_subring,
range_le' := λ a ⟨r, hr⟩, hr ▸ iSB.range_le ⟨_, rfl⟩ }
/-- If `S` is an `R`-subalgebra of `A` and `T` is an `S`-subalgebra of `A`,
then `T` is an `R`-subalgebra of `A`. -/
def under {R : Type u} {A : Type v} [comm_ring R] [comm_ring A]
{i : algebra R A} (S : subalgebra R A)
(T : subalgebra S A) : subalgebra R A :=
{ carrier := T,
range_le' := (λ a ⟨r, hr⟩, hr ▸ T.range_le ⟨⟨algebra_map R A r, S.range_le ⟨r, rfl⟩⟩, rfl⟩) }
lemma mul_mem (A' : subalgebra R A) (x y : A) :
x ∈ A' → y ∈ A' → x * y ∈ A' := @is_submonoid.mul_mem A _ A' _ x y
end subalgebra
namespace alg_hom
variables {R : Type u} {A : Type v} {B : Type w}
variables [comm_ring R] [ring A] [ring B] [algebra R A] [algebra R B]
variables (φ : A →ₐ[R] B)
/-- Range of an `alg_hom` as a subalgebra. -/
protected def range (φ : A →ₐ[R] B) : subalgebra R B :=
begin
haveI : is_subring (set.range φ) := show is_subring (set.range φ.to_ring_hom), by apply_instance,
exact ⟨set.range φ, λ y ⟨r, hr⟩, ⟨algebra_map R A r, hr ▸ φ.commutes r⟩⟩
end
end alg_hom
namespace algebra
variables (R : Type u) (A : Type v)
variables [comm_semiring R] [semiring A] [algebra R A]
instance id : algebra R R := (ring_hom.id R).to_algebra
namespace id
@[simp] lemma map_eq_self (x : R) : algebra_map R R x = x := rfl
@[simp] lemma smul_eq_mul (x y : R) : x • y = x * y := rfl
end id
/-- `algebra_map` as an `alg_hom`. -/
def of_id : R →ₐ[R] A :=
{ commutes' := λ _, rfl, .. algebra_map R A }
variables {R}
theorem of_id_apply (r) : of_id R A r = algebra_map R A r := rfl
end algebra
namespace algebra
variables (R : Type u) {A : Type v} [comm_ring R] [ring A] [algebra R A]
/-- The minimal subalgebra that includes `s`. -/
def adjoin (s : set A) : subalgebra R A :=
{ carrier := ring.closure (set.range (algebra_map R A) ∪ s),
range_le' := le_trans (set.subset_union_left _ _) ring.subset_closure }
variables {R}
protected lemma gc : galois_connection (adjoin R : set A → subalgebra R A) coe :=
λ s S, ⟨λ H, le_trans (le_trans (set.subset_union_right _ _) ring.subset_closure) H,
λ H, ring.closure_subset $ set.union_subset S.range_le H⟩
/-- Galois insertion between `adjoin` and `coe`. -/
protected def gi : galois_insertion (adjoin R : set A → subalgebra R A) coe :=
{ choice := λ s hs, adjoin R s,
gc := algebra.gc,
le_l_u := λ S, (algebra.gc (S : set A) (adjoin R S)).1 $ le_refl _,
choice_eq := λ _ _, rfl }
instance : complete_lattice (subalgebra R A) :=
galois_insertion.lift_complete_lattice algebra.gi
instance : inhabited (subalgebra R A) := ⟨⊥⟩
theorem mem_bot {x : A} : x ∈ (⊥ : subalgebra R A) ↔ x ∈ set.range (algebra_map R A) :=
suffices (⊥ : subalgebra R A) = (of_id R A).range, by rw this; refl,
le_antisymm bot_le $ subalgebra.range_le _
theorem mem_top {x : A} : x ∈ (⊤ : subalgebra R A) :=
ring.mem_closure $ or.inr trivial
theorem eq_top_iff {S : subalgebra R A} :
S = ⊤ ↔ ∀ x : A, x ∈ S :=
⟨λ h x, by rw h; exact mem_top, λ h, by ext x; exact ⟨λ _, mem_top, λ _, h x⟩⟩
/-- `alg_hom` to `⊤ : subalgebra R A`. -/
def to_top : A →ₐ[R] (⊤ : subalgebra R A) :=
by refine_struct { to_fun := λ x, (⟨x, mem_top⟩ : (⊤ : subalgebra R A)) }; intros; refl
end algebra
section int
variables (R : Type*) [ring R]
/-- Reinterpret a `ring_hom` as a `ℤ`-algebra homomorphism. -/
def alg_hom_int
{R : Type u} [comm_ring R] [algebra ℤ R]
{S : Type v} [comm_ring S] [algebra ℤ S]
(f : R →+* S) : R →ₐ[ℤ] S :=
{ commutes' := λ i, show f _ = _, by simp, .. f }
/-- CRing ⥤ ℤ-Alg -/
instance algebra_int : algebra ℤ R :=
{ commutes' := int.cast_commute,
smul_def' := λ _ _, gsmul_eq_mul _ _,
.. int.cast_ring_hom R }
variables {R}
/-- A subring is a `ℤ`-subalgebra. -/
def subalgebra_of_subring (S : set R) [is_subring S] : subalgebra ℤ R :=
{ carrier := S,
range_le' := by { rintros _ ⟨i, rfl⟩, rw [ring_hom.eq_int_cast, ← gsmul_one],
exact is_add_subgroup.gsmul_mem is_submonoid.one_mem } }
@[simp] lemma mem_subalgebra_of_subring {x : R} {S : set R} [is_subring S] :
x ∈ subalgebra_of_subring S ↔ x ∈ S :=
iff.rfl
section span_int
open submodule
lemma span_int_eq_add_group_closure (s : set R) :
(span ℤ s).to_add_subgroup = add_subgroup.closure s :=
eq.symm $ add_subgroup.closure_eq_of_le _ subset_span $ λ x hx, span_induction hx
(λ x hx, add_subgroup.subset_closure hx) (add_subgroup.zero_mem _)
(λ _ _, add_subgroup.add_mem _) (λ _ _ _, add_subgroup.gsmul_mem _ ‹_› _)
@[simp] lemma span_int_eq (s : add_subgroup R) : (span ℤ (s : set R)).to_add_subgroup = s :=
by rw [span_int_eq_add_group_closure, s.closure_eq]
end span_int
end int
section restrict_scalars
/- In this section, we describe restriction of scalars: if `S` is an algebra over `R`, then
`S`-modules are also `R`-modules. -/
variables (R : Type*) [comm_ring R] (S : Type*) [ring S] [algebra R S]
variables (E : Type*) [add_comm_group E] [module S E] {F : Type*} [add_comm_group F] [module S F]
/--
When `E` is a module over a ring `S`, and `S` is an algebra over `R`, then `E` inherits a
module structure over `R`, called `module.restrict_scalars' R S E`.
We do not register this as an instance as `S` can not be inferred.
-/
def module.restrict_scalars' : module R E :=
{ smul := λ c x, (algebra_map R S c) • x,
one_smul := by simp,
mul_smul := by simp [mul_smul],
smul_add := by simp [smul_add],
smul_zero := by simp [smul_zero],
add_smul := by simp [add_smul],
zero_smul := by simp [zero_smul] }
/--
When `E` is a module over a ring `S`, and `S` is an algebra over `R`, then `E` inherits a
module structure over `R`, provided as a type synonym `module.restrict_scalars R S E := E`.
-/
@[nolint unused_arguments]
def module.restrict_scalars (R : Type*) (S : Type*) (E : Type*) : Type* := E
instance (R : Type*) (S : Type*) (E : Type*) [I : inhabited E] :
inhabited (module.restrict_scalars R S E) := I
instance (R : Type*) (S : Type*) (E : Type*) [I : add_comm_group E] :
add_comm_group (module.restrict_scalars R S E) := I
instance : module R (module.restrict_scalars R S E) :=
(module.restrict_scalars' R S E : module R E)
lemma module.restrict_scalars_smul_def (c : R) (x : module.restrict_scalars R S E) :
c • x = ((algebra_map R S c) • x : E) := rfl
/--
`module.restrict_scalars R S S` is `R`-linearly equivalent to the original algebra `S`.
Unfortunately these structures are not generally definitionally equal:
the `R`-module structure on `S` is part of the data of `S`,
while the `R`-module structure on `module.restrict_scalars R S S`
comes from the ring homomorphism `R →+* S`, which is a separate part of the data of `S`.
The field `algebra.smul_def'` gives the equation we need here.
-/
def algebra.restrict_scalars_equiv :
(module.restrict_scalars R S S) ≃ₗ[R] S :=
{ to_fun := λ s, s,
inv_fun := λ s, s,
left_inv := λ s, rfl,
right_inv := λ s, rfl,
map_add' := λ x y, rfl,
map_smul' := λ c x, (algebra.smul_def' _ _).symm, }
@[simp]
lemma algebra.restrict_scalars_equiv_apply (s : S) :
algebra.restrict_scalars_equiv R S s = s := rfl
@[simp]
lemma algebra.restrict_scalars_equiv_symm_apply (s : S) :
(algebra.restrict_scalars_equiv R S).symm s = s := rfl
variables {S E}
open module
/--
`V.restrict_scalars R` is the `R`-submodule of the `R`-module given by restriction of scalars,
corresponding to `V`, an `S`-submodule of the original `S`-module.
-/
@[simps]
def submodule.restrict_scalars (V : submodule S E) : submodule R (restrict_scalars R S E) :=
{ carrier := V.carrier,
zero_mem' := V.zero_mem,
smul_mem' := λ c e h, V.smul_mem _ h,
add_mem' := λ x y hx hy, V.add_mem hx hy, }
@[simp]
lemma submodule.restrict_scalars_mem (V : submodule S E) (e : E) :
e ∈ V.restrict_scalars R ↔ e ∈ V :=
iff.refl _
@[simp]
lemma submodule.restrict_scalars_bot :
submodule.restrict_scalars R (⊥ : submodule S E) = ⊥ :=
rfl
@[simp]
lemma submodule.restrict_scalars_top :
submodule.restrict_scalars R (⊤ : submodule S E) = ⊤ :=
rfl
/-- The `R`-linear map induced by an `S`-linear map when `S` is an algebra over `R`. -/
def linear_map.restrict_scalars (f : E →ₗ[S] F) :
(restrict_scalars R S E) →ₗ[R] (restrict_scalars R S F) :=
{ to_fun := f.to_fun,
map_add' := λx y, f.map_add x y,
map_smul' := λc x, f.map_smul (algebra_map R S c) x }
@[simp, norm_cast squash] lemma linear_map.coe_restrict_scalars_eq_coe (f : E →ₗ[S] F) :
(f.restrict_scalars R : E → F) = f := rfl
@[simp]
lemma restrict_scalars_ker (f : E →ₗ[S] F) :
(f.restrict_scalars R).ker = submodule.restrict_scalars R f.ker :=
rfl
variables (𝕜 : Type*) [field 𝕜] (𝕜' : Type*) [field 𝕜'] [algebra 𝕜 𝕜']
variables (W : Type*) [add_comm_group W] [vector_space 𝕜' W]
/--
`V.restrict_scalars 𝕜` is the `𝕜`-subspace of the `𝕜`-vector space given by restriction of scalars,
corresponding to `V`, a `𝕜'`-subspace of the original `𝕜'`-vector space.
-/
def subspace.restrict_scalars (V : subspace 𝕜' W) : subspace 𝕜 (restrict_scalars 𝕜 𝕜' W) :=
{ ..submodule.restrict_scalars 𝕜 (V : submodule 𝕜' W) }
end restrict_scalars
/-!
When `V` and `W` are `S`-modules, for some `R`-algebra `S`,
the collection of `S`-linear maps from `V` to `W` forms an `R`-module.
(But not generally an `S`-module, because `S` may be non-commutative.)
-/
section module_of_linear_maps
variables (R : Type*) [comm_ring R] (S : Type*) [ring S] [algebra R S]
(V : Type*) [add_comm_group V] [module S V]
(W : Type*) [add_comm_group W] [module S W]
/--
For `r : R`, and `f : V →ₗ[S] W` (where `S` is an `R`-algebra) we define
`(r • f) v = f (r • v)`.
-/
def linear_map_algebra_has_scalar : has_scalar R (V →ₗ[S] W) :=
{ smul := λ r f,
{ to_fun := λ v, f ((algebra_map R S r) • v),
map_add' := λ x y, by simp [smul_add],
map_smul' := λ s v, by simp [smul_smul, algebra.commutes], } }
local attribute [instance] linear_map_algebra_has_scalar
/-- The `R`-module structure on `S`-linear maps, for `S` an `R`-algebra. -/
def linear_map_algebra_module : module R (V →ₗ[S] W) :=
{ one_smul := λ f, begin ext v, dsimp [(•)], simp, end,
mul_smul := λ r r' f,
begin
ext v, dsimp [(•)],
rw [linear_map.map_smul, linear_map.map_smul, linear_map.map_smul, ring_hom.map_mul,
smul_smul, algebra.commutes],
end,
smul_zero := λ r, by { ext v, dsimp [(•)], refl, },
smul_add := λ r f g, by { ext v, dsimp [(•)], simp [linear_map.map_add], },
zero_smul := λ f, by { ext v, dsimp [(•)], simp, },
add_smul := λ r r' f, by { ext v, dsimp [(•)], simp [add_smul], }, }
local attribute [instance] linear_map_algebra_module
variables {R S V W}
@[simp]
lemma linear_map_algebra_module.smul_apply (c : R) (f : V →ₗ[S] W) (v : V) :
(c • f) v = (c • (f v) : module.restrict_scalars R S W) :=
begin
erw [linear_map.map_smul],
refl,
end
end module_of_linear_maps
|
0140907e90cd03745b2036756340774e29485853 | 9dd3f3912f7321eb58ee9aa8f21778ad6221f87c | /tests/lean/run/declare_axiom.lean | cc8443f84b8c5cd07f64b080bcf04294ef8d9c9e | [
"Apache-2.0"
] | permissive | bre7k30/lean | de893411bcfa7b3c5572e61b9e1c52951b310aa4 | 5a924699d076dab1bd5af23a8f910b433e598d7a | refs/heads/master | 1,610,900,145,817 | 1,488,006,845,000 | 1,488,006,845,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 108 | lean | open tactic
run_command (do
e ← to_expr `(false),
add_decl $ declaration.ax `useful_assumption [] e)
|
265680ab65c10d0528151aaac3e02ef5cebd1cf5 | 3f7026ea8bef0825ca0339a275c03b911baef64d | /src/category_theory/comma.lean | de25cb5b293c2cc5acf378b028237cba8c1f3cb8 | [
"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 | 11,430 | lean | /-
Copyright (c) 2018 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Johan Commelin
-/
import category_theory.isomorphism
import category_theory.punit
namespace category_theory
universes v₁ v₂ v₃ u₁ u₂ u₃ -- declare the `v`'s first; see `category_theory.category` for an explanation
variables {A : Type u₁} [𝒜 : category.{v₁} A]
variables {B : Type u₂} [ℬ : category.{v₂} B]
variables {T : Type u₃} [𝒯 : category.{v₃} T]
include 𝒜 ℬ 𝒯
structure comma (L : A ⥤ T) (R : B ⥤ T) : Type (max u₁ u₂ v₃) :=
(left : A . obviously)
(right : B . obviously)
(hom : L.obj left ⟶ R.obj right)
variables {L : A ⥤ T} {R : B ⥤ T}
structure comma_morphism (X Y : comma L R) :=
(left : X.left ⟶ Y.left . obviously)
(right : X.right ⟶ Y.right . obviously)
(w' : L.map left ≫ Y.hom = X.hom ≫ R.map right . obviously)
restate_axiom comma_morphism.w'
attribute [simp] comma_morphism.w
namespace comma_morphism
@[extensionality] lemma ext
{X Y : comma L R} {f g : comma_morphism X Y}
(l : f.left = g.left) (r : f.right = g.right) : f = g :=
begin
cases f, cases g,
congr; assumption
end
end comma_morphism
instance comma_category : category (comma L R) :=
{ hom := comma_morphism,
id := λ X,
{ left := 𝟙 X.left,
right := 𝟙 X.right },
comp := λ X Y Z f g,
{ left := f.left ≫ g.left,
right := f.right ≫ g.right,
w' :=
begin
rw [functor.map_comp,
category.assoc,
g.w,
←category.assoc,
f.w,
functor.map_comp,
category.assoc],
end }}
namespace comma
section
variables {X Y Z : comma L R} {f : X ⟶ Y} {g : Y ⟶ Z}
@[simp] lemma comp_left : (f ≫ g).left = f.left ≫ g.left := rfl
@[simp] lemma comp_right : (f ≫ g).right = f.right ≫ g.right := rfl
end
variables (L) (R)
def fst : comma L R ⥤ A :=
{ obj := λ X, X.left,
map := λ _ _ f, f.left }
def snd : comma L R ⥤ B :=
{ obj := λ X, X.right,
map := λ _ _ f, f.right }
@[simp] lemma fst_obj {X : comma L R} : (fst L R).obj X = X.left := rfl
@[simp] lemma snd_obj {X : comma L R} : (snd L R).obj X = X.right := rfl
@[simp] lemma fst_map {X Y : comma L R} {f : X ⟶ Y} : (fst L R).map f = f.left := rfl
@[simp] lemma snd_map {X Y : comma L R} {f : X ⟶ Y} : (snd L R).map f = f.right := rfl
def nat_trans : fst L R ⋙ L ⟶ snd L R ⋙ R :=
{ app := λ X, X.hom }
section
variables {L₁ L₂ L₃ : A ⥤ T} {R₁ R₂ R₃ : B ⥤ T}
def map_left (l : L₁ ⟶ L₂) : comma L₂ R ⥤ comma L₁ R :=
{ obj := λ X,
{ left := X.left,
right := X.right,
hom := l.app X.left ≫ X.hom },
map := λ X Y f,
{ left := f.left,
right := f.right,
w' := by tidy; rw [←category.assoc, l.naturality f.left, category.assoc]; tidy } }
section
variables {X Y : comma L₂ R} {f : X ⟶ Y} {l : L₁ ⟶ L₂}
@[simp] lemma map_left_obj_left : ((map_left R l).obj X).left = X.left := rfl
@[simp] lemma map_left_obj_right : ((map_left R l).obj X).right = X.right := rfl
@[simp] lemma map_left_obj_hom : ((map_left R l).obj X).hom = l.app X.left ≫ X.hom := rfl
@[simp] lemma map_left_map_left : ((map_left R l).map f).left = f.left := rfl
@[simp] lemma map_left_map_right : ((map_left R l).map f).right = f.right := rfl
end
def map_left_id : map_left R (𝟙 L) ≅ 𝟭 _ :=
{ hom :=
{ app := λ X, { left := 𝟙 _, right := 𝟙 _ } },
inv :=
{ app := λ X, { left := 𝟙 _, right := 𝟙 _ } } }
section
variables {X : comma L R}
@[simp] lemma map_left_id_hom_app_left : (((map_left_id L R).hom).app X).left = 𝟙 (X.left) := rfl
@[simp] lemma map_left_id_hom_app_right : (((map_left_id L R).hom).app X).right = 𝟙 (X.right) := rfl
@[simp] lemma map_left_id_inv_app_left : (((map_left_id L R).inv).app X).left = 𝟙 (X.left) := rfl
@[simp] lemma map_left_id_inv_app_right : (((map_left_id L R).inv).app X).right = 𝟙 (X.right) := rfl
end
def map_left_comp (l : L₁ ⟶ L₂) (l' : L₂ ⟶ L₃) :
(map_left R (l ≫ l')) ≅ (map_left R l') ⋙ (map_left R l) :=
{ hom :=
{ app := λ X, { left := 𝟙 _, right := 𝟙 _ } },
inv :=
{ app := λ X, { left := 𝟙 _, right := 𝟙 _ } } }
section
variables {X : comma L₃ R} {l : L₁ ⟶ L₂} {l' : L₂ ⟶ L₃}
@[simp] lemma map_left_comp_hom_app_left : (((map_left_comp R l l').hom).app X).left = 𝟙 (X.left) := rfl
@[simp] lemma map_left_comp_hom_app_right : (((map_left_comp R l l').hom).app X).right = 𝟙 (X.right) := rfl
@[simp] lemma map_left_comp_inv_app_left : (((map_left_comp R l l').inv).app X).left = 𝟙 (X.left) := rfl
@[simp] lemma map_left_comp_inv_app_right : (((map_left_comp R l l').inv).app X).right = 𝟙 (X.right) := rfl
end
def map_right (r : R₁ ⟶ R₂) : comma L R₁ ⥤ comma L R₂ :=
{ obj := λ X,
{ left := X.left,
right := X.right,
hom := X.hom ≫ r.app X.right },
map := λ X Y f,
{ left := f.left,
right := f.right,
w' := by tidy; rw [←r.naturality f.right, ←category.assoc]; tidy } }
section
variables {X Y : comma L R₁} {f : X ⟶ Y} {r : R₁ ⟶ R₂}
@[simp] lemma map_right_obj_left : ((map_right L r).obj X).left = X.left := rfl
@[simp] lemma map_right_obj_right : ((map_right L r).obj X).right = X.right := rfl
@[simp] lemma map_right_obj_hom : ((map_right L r).obj X).hom = X.hom ≫ r.app X.right := rfl
@[simp] lemma map_right_map_left : ((map_right L r).map f).left = f.left := rfl
@[simp] lemma map_right_map_right : ((map_right L r).map f).right = f.right := rfl
end
def map_right_id : map_right L (𝟙 R) ≅ 𝟭 _ :=
{ hom :=
{ app := λ X, { left := 𝟙 _, right := 𝟙 _ } },
inv :=
{ app := λ X, { left := 𝟙 _, right := 𝟙 _ } } }
section
variables {X : comma L R}
@[simp] lemma map_right_id_hom_app_left : (((map_right_id L R).hom).app X).left = 𝟙 (X.left) := rfl
@[simp] lemma map_right_id_hom_app_right : (((map_right_id L R).hom).app X).right = 𝟙 (X.right) := rfl
@[simp] lemma map_right_id_inv_app_left : (((map_right_id L R).inv).app X).left = 𝟙 (X.left) := rfl
@[simp] lemma map_right_id_inv_app_right : (((map_right_id L R).inv).app X).right = 𝟙 (X.right) := rfl
end
def map_right_comp (r : R₁ ⟶ R₂) (r' : R₂ ⟶ R₃) : (map_right L (r ≫ r')) ≅ (map_right L r) ⋙ (map_right L r') :=
{ hom :=
{ app := λ X, { left := 𝟙 _, right := 𝟙 _ } },
inv :=
{ app := λ X, { left := 𝟙 _, right := 𝟙 _ } } }
section
variables {X : comma L R₁} {r : R₁ ⟶ R₂} {r' : R₂ ⟶ R₃}
@[simp] lemma map_right_comp_hom_app_left : (((map_right_comp L r r').hom).app X).left = 𝟙 (X.left) := rfl
@[simp] lemma map_right_comp_hom_app_right : (((map_right_comp L r r').hom).app X).right = 𝟙 (X.right) := rfl
@[simp] lemma map_right_comp_inv_app_left : (((map_right_comp L r r').inv).app X).left = 𝟙 (X.left) := rfl
@[simp] lemma map_right_comp_inv_app_right : (((map_right_comp L r r').inv).app X).right = 𝟙 (X.right) := rfl
end
end
end comma
omit 𝒜 ℬ
def over (X : T) := comma.{v₃ 1 v₃} (𝟭 T) (functor.of.obj X)
namespace over
variables {X : T}
instance category : category (over X) := by delta over; apply_instance
@[extensionality] lemma over_morphism.ext {X : T} {U V : over X} {f g : U ⟶ V}
(h : f.left = g.left) : f = g :=
by tidy
@[simp] lemma over_right (U : over X) : U.right = punit.star := by tidy
@[simp] lemma over_morphism_right {U V : over X} (f : U ⟶ V) : f.right = 𝟙 punit.star := by tidy
@[simp] lemma id_left (U : over X) : comma_morphism.left (𝟙 U) = 𝟙 U.left := rfl
@[simp] lemma comp_left (a b c : over X) (f : a ⟶ b) (g : b ⟶ c) :
(f ≫ g).left = f.left ≫ g.left := rfl
@[simp] lemma w {A B : over X} (f : A ⟶ B) : f.left ≫ B.hom = A.hom :=
by have := f.w; tidy
def mk {X Y : T} (f : Y ⟶ X) : over X :=
{ left := Y, hom := f }
@[simp] lemma mk_left {X Y : T} (f : Y ⟶ X) : (mk f).left = Y := rfl
@[simp] lemma mk_hom {X Y : T} (f : Y ⟶ X) : (mk f).hom = f := rfl
def hom_mk {U V : over X} (f : U.left ⟶ V.left) (w : f ≫ V.hom = U.hom . obviously) :
U ⟶ V :=
{ left := f }
@[simp] lemma hom_mk_left {U V : over X} (f : U.left ⟶ V.left) (w : f ≫ V.hom = U.hom) :
(hom_mk f).left = f :=
rfl
def forget : (over X) ⥤ T := comma.fst _ _
@[simp] lemma forget_obj {U : over X} : forget.obj U = U.left := rfl
@[simp] lemma forget_map {U V : over X} {f : U ⟶ V} : forget.map f = f.left := rfl
def map {Y : T} (f : X ⟶ Y) : over X ⥤ over Y := comma.map_right _ $ functor.of.map f
section
variables {Y : T} {f : X ⟶ Y} {U V : over X} {g : U ⟶ V}
@[simp] lemma map_obj_left : ((map f).obj U).left = U.left := rfl
@[simp] lemma map_obj_hom : ((map f).obj U).hom = U.hom ≫ f := rfl
@[simp] lemma map_map_left : ((map f).map g).left = g.left := rfl
end
section
variables {D : Type u₃} [𝒟 : category.{v₃} D]
include 𝒟
def post (F : T ⥤ D) : over X ⥤ over (F.obj X) :=
{ obj := λ Y, mk $ F.map Y.hom,
map := λ Y₁ Y₂ f,
{ left := F.map f.left,
w' := by tidy; erw [← F.map_comp, w] } }
end
end over
def under (X : T) := comma.{1 v₃ v₃} (functor.of.obj X) (𝟭 T)
namespace under
variables {X : T}
instance : category (under X) := by delta under; apply_instance
@[extensionality] lemma under_morphism.ext {X : T} {U V : under X} {f g : U ⟶ V}
(h : f.right = g.right) : f = g :=
by tidy
@[simp] lemma under_left (U : under X) : U.left = punit.star := by tidy
@[simp] lemma under_morphism_left {U V : under X} (f : U ⟶ V) : f.left = 𝟙 punit.star := by tidy
@[simp] lemma id_right (U : under X) : comma_morphism.right (𝟙 U) = 𝟙 U.right := rfl
@[simp] lemma comp_right (a b c : under X) (f : a ⟶ b) (g : b ⟶ c) :
(f ≫ g).right = f.right ≫ g.right := rfl
@[simp] lemma w {A B : under X} (f : A ⟶ B) : A.hom ≫ f.right = B.hom :=
by have := f.w; tidy
def mk {X Y : T} (f : X ⟶ Y) : under X :=
{ right := Y, hom := f }
@[simp] lemma mk_right {X Y : T} (f : X ⟶ Y) : (mk f).right = Y := rfl
@[simp] lemma mk_hom {X Y : T} (f : X ⟶ Y) : (mk f).hom = f := rfl
def hom_mk {U V : under X} (f : U.right ⟶ V.right) (w : U.hom ≫ f = V.hom . obviously) :
U ⟶ V :=
{ right := f }
@[simp] lemma hom_mk_right {U V : under X} (f : U.right ⟶ V.right) (w : U.hom ≫ f = V.hom) :
(hom_mk f).right = f :=
rfl
def forget : (under X) ⥤ T := comma.snd _ _
@[simp] lemma forget_obj {U : under X} : forget.obj U = U.right := rfl
@[simp] lemma forget_map {U V : under X} {f : U ⟶ V} : forget.map f = f.right := rfl
def map {Y : T} (f : X ⟶ Y) : under Y ⥤ under X := comma.map_left _ $ functor.of.map f
section
variables {Y : T} {f : X ⟶ Y} {U V : under Y} {g : U ⟶ V}
@[simp] lemma map_obj_right : ((map f).obj U).right = U.right := rfl
@[simp] lemma map_obj_hom : ((map f).obj U).hom = f ≫ U.hom := rfl
@[simp] lemma map_map_right : ((map f).map g).right = g.right := rfl
end
section
variables {D : Type u₃} [𝒟 : category.{v₃} D]
include 𝒟
def post {X : T} (F : T ⥤ D) : under X ⥤ under (F.obj X) :=
{ obj := λ Y, mk $ F.map Y.hom,
map := λ Y₁ Y₂ f,
{ right := F.map f.right,
w' := by tidy; erw [← F.map_comp, w] } }
end
end under
end category_theory
|
1249333c91bb5e8f8c7a7980bd4fa889de8f07fd | c777c32c8e484e195053731103c5e52af26a25d1 | /src/number_theory/l_series.lean | 0e6db230439817f3b241bd62207a31ff685a36c7 | [
"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 | 4,966 | lean | /-
Copyright (c) 2021 Aaron Anderson. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Aaron Anderson
-/
import analysis.normed_space.finite_dimension
import analysis.p_series
import number_theory.arithmetic_function
import topology.algebra.infinite_sum.basic
/-!
# L-series
Given an arithmetic function, we define the corresponding L-series.
## Main Definitions
* `nat.arithmetic_function.l_series` is the `l_series` with a given arithmetic function as its
coefficients. This is not the analytic continuation, just the infinite series.
* `nat.arithmetic_function.l_series_summable` indicates that the `l_series`
converges at a given point.
## Main Results
* `nat.arithmetic_function.l_series_summable_of_bounded_of_one_lt_re`: the `l_series` of a bounded
arithmetic function converges when `1 < z.re`.
* `nat.arithmetic_function.zeta_l_series_summable_iff_one_lt_re`: the `l_series` of `ζ`
(whose analytic continuation is the Riemann ζ) converges iff `1 < z.re`.
-/
noncomputable theory
open_locale big_operators
namespace nat
namespace arithmetic_function
/-- The L-series of an `arithmetic_function`. -/
def l_series (f : arithmetic_function ℂ) (z : ℂ) : ℂ := ∑'n, (f n) / (n ^ z)
/-- `f.l_series_summable z` indicates that the L-series of `f` converges at `z`. -/
def l_series_summable (f : arithmetic_function ℂ) (z : ℂ) : Prop := summable (λ n, (f n) / (n ^ z))
lemma l_series_eq_zero_of_not_l_series_summable (f : arithmetic_function ℂ) (z : ℂ) :
¬ f.l_series_summable z → f.l_series z = 0 :=
tsum_eq_zero_of_not_summable
@[simp]
lemma l_series_summable_zero {z : ℂ} : l_series_summable 0 z :=
by simp [l_series_summable, summable_zero]
theorem l_series_summable_of_bounded_of_one_lt_real {f : arithmetic_function ℂ} {m : ℝ}
(h : ∀ (n : ℕ), complex.abs (f n) ≤ m) {z : ℝ} (hz : 1 < z) :
f.l_series_summable z :=
begin
by_cases h0 : m = 0,
{ subst h0,
have hf : f = 0 := arithmetic_function.ext (λ n, complex.abs.eq_zero.1
(le_antisymm (h n) (complex.abs.nonneg _))),
simp [hf] },
refine summable_of_norm_bounded (λ (n : ℕ), m / (n ^ z)) _ _,
{ simp_rw [div_eq_mul_inv],
exact (summable_mul_left_iff h0).2 (real.summable_nat_rpow_inv.2 hz) },
{ intro n,
have hm : 0 ≤ m := le_trans (complex.abs.nonneg _) (h 0),
cases n,
{ simp [hm, real.zero_rpow (ne_of_gt (lt_trans real.zero_lt_one hz))] },
simp only [map_div₀, complex.norm_eq_abs],
apply div_le_div hm (h _) (real.rpow_pos_of_pos (nat.cast_pos.2 n.succ_pos) _) (le_of_eq _),
rw [complex.abs_cpow_real, complex.abs_cast_nat] }
end
theorem l_series_summable_iff_of_re_eq_re {f : arithmetic_function ℂ} {w z : ℂ} (h : w.re = z.re) :
f.l_series_summable w ↔ f.l_series_summable z :=
begin
suffices h : ∀ n : ℕ, complex.abs (f n) / complex.abs (↑n ^ w) =
complex.abs (f n) / complex.abs (↑n ^ z),
{ simp [l_series_summable, ← summable_norm_iff, h, complex.norm_eq_abs] },
intro n,
cases n, { simp },
apply congr rfl,
have h0 : (n.succ : ℂ) ≠ 0,
{ rw [ne.def, nat.cast_eq_zero],
apply n.succ_ne_zero },
rw [complex.cpow_def, complex.cpow_def, if_neg h0, if_neg h0, complex.abs_exp_eq_iff_re_eq],
simp only [h, complex.mul_re, mul_eq_mul_left_iff, sub_right_inj],
right,
rw [complex.log_im, ← complex.of_real_nat_cast],
exact complex.arg_of_real_of_nonneg (le_of_lt (cast_pos.2 n.succ_pos)),
end
theorem l_series_summable_of_bounded_of_one_lt_re {f : arithmetic_function ℂ} {m : ℝ}
(h : ∀ (n : ℕ), complex.abs (f n) ≤ m) {z : ℂ} (hz : 1 < z.re) :
f.l_series_summable z :=
begin
rw ← l_series_summable_iff_of_re_eq_re (complex.of_real_re z.re),
apply l_series_summable_of_bounded_of_one_lt_real h,
exact hz,
end
open_locale arithmetic_function
theorem zeta_l_series_summable_iff_one_lt_re {z : ℂ} :
l_series_summable ζ z ↔ 1 < z.re :=
begin
rw [← l_series_summable_iff_of_re_eq_re (complex.of_real_re z.re), l_series_summable,
← summable_norm_iff, ← real.summable_one_div_nat_rpow, iff_iff_eq],
by_cases h0 : z.re = 0,
{ rw [h0, ← summable_nat_add_iff 1],
swap, { apply_instance },
apply congr rfl,
ext n,
simp [n.succ_ne_zero] },
{ apply congr rfl,
ext ⟨-|n⟩,
{ simp [h0] },
simp only [cast_zero, nat_coe_apply, zeta_apply, succ_ne_zero, if_false, cast_succ, one_div,
complex.norm_eq_abs, map_inv₀, complex.abs_cpow_real, inv_inj, zero_add],
rw [←cast_one, ←cast_add, complex.abs_of_nat, cast_add, cast_one] }
end
@[simp] theorem l_series_add {f g : arithmetic_function ℂ} {z : ℂ}
(hf : f.l_series_summable z) (hg : g.l_series_summable z) :
(f + g).l_series z = f.l_series z + g.l_series z :=
begin
simp only [l_series, add_apply],
rw ← tsum_add hf hg,
apply congr rfl (funext (λ n, _)),
apply _root_.add_div,
end
end arithmetic_function
end nat
|
ad611a5bce3c52269fefafcdf851af2bf1158df9 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/algebra/order/group/prod.lean | 3ca4fe7d767ee96bddc2e315928a69cb3aa75a36 | [
"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 | 701 | 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.order.group.instances
import algebra.order.monoid.prod
/-!
# Products of ordered commutative groups.
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
-/
variable {α : Type*}
namespace prod
variables {G H : Type*}
@[to_additive]
instance [ordered_comm_group G] [ordered_comm_group H] :
ordered_comm_group (G × H) :=
{ .. prod.comm_group, .. prod.partial_order G H, .. prod.ordered_cancel_comm_monoid }
end prod
|
1e339225a420206380a17a9de1c27fa33042cf49 | 4efff1f47634ff19e2f786deadd394270a59ecd2 | /src/geometry/euclidean/monge_point.lean | 0924cde26162805f10d46823145785835301b50e | [
"Apache-2.0"
] | permissive | agjftucker/mathlib | d634cd0d5256b6325e3c55bb7fb2403548371707 | 87fe50de17b00af533f72a102d0adefe4a2285e8 | refs/heads/master | 1,625,378,131,941 | 1,599,166,526,000 | 1,599,166,526,000 | 160,748,509 | 0 | 0 | Apache-2.0 | 1,544,141,789,000 | 1,544,141,789,000 | null | UTF-8 | Lean | false | false | 18,782 | lean | /-
Copyright (c) 2020 Joseph Myers. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Joseph Myers.
-/
import geometry.euclidean.circumcenter
noncomputable theory
open_locale big_operators
open_locale classical
open_locale real
/-!
# Monge point and orthocenter
This file defines the orthocenter of a triangle, via its n-dimensional
generalization, the Monge point of a simplex.
## Main definitions
* `monge_point` is the Monge point of a simplex, defined in terms of
its position on the Euler line and then shown to be the point of
concurrence of the Monge planes.
* `monge_plane` is a Monge plane of an (n+2)-simplex, which is the
(n+1)-dimensional affine subspace of the subspace spanned by the
simplex that passes through the centroid of an n-dimensional face
and is orthogonal to the opposite edge (in 2 dimensions, this is the
same as an altitude).
* `altitude` is the line that passes through a vertex of a simplex and
is orthogonal to the opposite face.
* `orthocenter` is defined, for the case of a triangle, to be the same
as its Monge point, then shown to be the point of concurrence of the
altitudes.
## References
* https://en.wikipedia.org/wiki/Altitude_(triangle)
* https://en.wikipedia.org/wiki/Monge_point
* Małgorzata Buba-Brzozowa, [The Monge Point and the 3(n+1) Point
Sphere of an
n-Simplex](https://pdfs.semanticscholar.org/6f8b/0f623459c76dac2e49255737f8f0f4725d16.pdf)
-/
namespace affine
namespace simplex
open finset affine_subspace euclidean_geometry points_with_circumcenter_index
variables {V : Type*} {P : Type*} [inner_product_space V] [metric_space P]
[normed_add_torsor V P]
include V
/-- The Monge point of a simplex (in 2 or more dimensions) is a
generalization of the orthocenter of a triangle. It is defined to be
the intersection of the Monge planes, where a Monge plane is the
(n-1)-dimensional affine subspace of the subspace spanned by the
simplex that passes through the centroid of an (n-2)-dimensional face
and is orthogonal to the opposite edge (in 2 dimensions, this is the
same as an altitude). The circumcenter O, centroid G and Monge point
M are collinear in that order on the Euler line, with OG : GM = (n-1)
: 2. Here, we use that ratio to define the Monge point (so resulting
in a point that equals the centroid in 0 or 1 dimensions), and then
show in subsequent lemmas that the point so defined lies in the Monge
planes and is their unique point of intersection. -/
def monge_point {n : ℕ} (s : simplex ℝ P n) : P :=
(((n + 1 : ℕ) : ℝ) / (((n - 1) : ℕ) : ℝ)) •
((univ : finset (fin (n + 1))).centroid ℝ s.points -ᵥ s.circumcenter) +ᵥ
s.circumcenter
/-- The position of the Monge point in relation to the circumcenter
and centroid. -/
lemma monge_point_eq_smul_vsub_vadd_circumcenter {n : ℕ} (s : simplex ℝ P n) :
s.monge_point = (((n + 1 : ℕ) : ℝ) / (((n - 1) : ℕ) : ℝ)) •
((univ : finset (fin (n + 1))).centroid ℝ s.points -ᵥ s.circumcenter) +ᵥ
s.circumcenter :=
rfl
/-- The Monge point lies in the affine span. -/
lemma monge_point_mem_affine_span {n : ℕ} (s : simplex ℝ P n) :
s.monge_point ∈ affine_span ℝ (set.range s.points) :=
smul_vsub_vadd_mem _ _
(centroid_mem_affine_span_of_card_eq_add_one ℝ _ (card_fin (n + 1)))
s.circumcenter_mem_affine_span
s.circumcenter_mem_affine_span
omit V
/-- The weights for the Monge point of an (n+2)-simplex, in terms of
`points_with_circumcenter`. -/
def monge_point_weights_with_circumcenter (n : ℕ) : points_with_circumcenter_index (n + 2) → ℝ
| (point_index i) := (((n + 1) : ℕ) : ℝ)⁻¹
| circumcenter_index := (-2 / (((n + 1) : ℕ) : ℝ))
/-- `monge_point_weights_with_circumcenter` sums to 1. -/
@[simp] lemma sum_monge_point_weights_with_circumcenter (n : ℕ) :
∑ i, monge_point_weights_with_circumcenter n i = 1 :=
begin
simp_rw [sum_points_with_circumcenter, monge_point_weights_with_circumcenter, sum_const,
card_fin, nsmul_eq_mul],
have hn1 : (n + 1 : ℝ) ≠ 0,
{ exact_mod_cast nat.succ_ne_zero _ },
field_simp [hn1],
ring
end
include V
/-- The Monge point of an (n+2)-simplex, in terms of
`points_with_circumcenter`. -/
lemma monge_point_eq_affine_combination_of_points_with_circumcenter {n : ℕ}
(s : simplex ℝ P (n + 2)) :
s.monge_point = (univ : finset (points_with_circumcenter_index (n + 2))).affine_combination
s.points_with_circumcenter (monge_point_weights_with_circumcenter n) :=
begin
rw [monge_point_eq_smul_vsub_vadd_circumcenter,
centroid_eq_affine_combination_of_points_with_circumcenter,
circumcenter_eq_affine_combination_of_points_with_circumcenter,
affine_combination_vsub, ←linear_map.map_smul,
weighted_vsub_vadd_affine_combination],
congr' with i,
rw [pi.add_apply, pi.smul_apply, smul_eq_mul, pi.sub_apply],
have hn1 : (n + 1 : ℝ) ≠ 0,
{ exact_mod_cast nat.succ_ne_zero _ },
cases i;
simp_rw [centroid_weights_with_circumcenter, circumcenter_weights_with_circumcenter,
monge_point_weights_with_circumcenter];
rw [nat.add_sub_assoc (dec_trivial : 1 ≤ 2), (dec_trivial : 2 - 1 = 1)],
{ rw [if_pos (mem_univ _), sub_zero, add_zero, card_fin],
have hn3 : (n + 2 + 1 : ℝ) ≠ 0,
{ exact_mod_cast nat.succ_ne_zero _ },
field_simp [hn1, hn3],
ring },
{ field_simp [hn1],
ring }
end
omit V
/-- The weights for the Monge point of an (n+2)-simplex, minus the
centroid of an n-dimensional face, in terms of
`points_with_circumcenter`. This definition is only valid when `i₁ ≠ i₂`. -/
def monge_point_vsub_face_centroid_weights_with_circumcenter {n : ℕ} (i₁ i₂ : fin (n + 3)) :
points_with_circumcenter_index (n + 2) → ℝ
| (point_index i) := if i = i₁ ∨ i = i₂ then (((n + 1) : ℕ) : ℝ)⁻¹ else 0
| circumcenter_index := (-2 / (((n + 1) : ℕ) : ℝ))
/-- `monge_point_vsub_face_centroid_weights_with_circumcenter` is the
result of subtracting `centroid_weights_with_circumcenter` from
`monge_point_weights_with_circumcenter`. -/
lemma monge_point_vsub_face_centroid_weights_with_circumcenter_eq_sub {n : ℕ}
{i₁ i₂ : fin (n + 3)} (h : i₁ ≠ i₂) :
monge_point_vsub_face_centroid_weights_with_circumcenter i₁ i₂ =
monge_point_weights_with_circumcenter n -
centroid_weights_with_circumcenter ({i₁, i₂}ᶜ) :=
begin
ext i,
cases i,
{ rw [pi.sub_apply, monge_point_weights_with_circumcenter, centroid_weights_with_circumcenter,
monge_point_vsub_face_centroid_weights_with_circumcenter],
have hu : card ({i₁, i₂}ᶜ : finset (fin (n + 3))) = n + 1,
{ simp [card_compl, fintype.card_fin, h] },
rw hu,
by_cases hi : i = i₁ ∨ i = i₂;
simp [compl_eq_univ_sdiff, hi] },
{ simp [monge_point_weights_with_circumcenter, centroid_weights_with_circumcenter,
monge_point_vsub_face_centroid_weights_with_circumcenter] }
end
/-- `monge_point_vsub_face_centroid_weights_with_circumcenter` sums to 0. -/
@[simp] lemma sum_monge_point_vsub_face_centroid_weights_with_circumcenter {n : ℕ}
{i₁ i₂ : fin (n + 3)} (h : i₁ ≠ i₂) :
∑ i, monge_point_vsub_face_centroid_weights_with_circumcenter i₁ i₂ i = 0 :=
begin
rw monge_point_vsub_face_centroid_weights_with_circumcenter_eq_sub h,
simp_rw [pi.sub_apply, sum_sub_distrib, sum_monge_point_weights_with_circumcenter],
rw [sum_centroid_weights_with_circumcenter, sub_self],
simp [←card_pos, card_compl, h]
end
include V
/-- The Monge point of an (n+2)-simplex, minus the centroid of an
n-dimensional face, in terms of `points_with_circumcenter`. -/
lemma monge_point_vsub_face_centroid_eq_weighted_vsub_of_points_with_circumcenter {n : ℕ}
(s : simplex ℝ P (n + 2)) {i₁ i₂ : fin (n + 3)} (h : i₁ ≠ i₂) :
s.monge_point -ᵥ ({i₁, i₂}ᶜ : finset (fin (n + 3))).centroid ℝ s.points =
(univ : finset (points_with_circumcenter_index (n + 2))).weighted_vsub
s.points_with_circumcenter (monge_point_vsub_face_centroid_weights_with_circumcenter i₁ i₂) :=
by simp_rw [monge_point_eq_affine_combination_of_points_with_circumcenter,
centroid_eq_affine_combination_of_points_with_circumcenter,
affine_combination_vsub,
monge_point_vsub_face_centroid_weights_with_circumcenter_eq_sub h]
/-- The Monge point of an (n+2)-simplex, minus the centroid of an
n-dimensional face, is orthogonal to the difference of the two
vertices not in that face. -/
lemma inner_monge_point_vsub_face_centroid_vsub {n : ℕ} (s : simplex ℝ P (n + 2))
{i₁ i₂ : fin (n + 3)} (h : i₁ ≠ i₂) :
inner (s.monge_point -ᵥ ({i₁, i₂}ᶜ : finset (fin (n + 3))).centroid ℝ s.points)
(s.points i₁ -ᵥ s.points i₂) = 0 :=
begin
simp_rw [monge_point_vsub_face_centroid_eq_weighted_vsub_of_points_with_circumcenter s h,
point_eq_affine_combination_of_points_with_circumcenter,
affine_combination_vsub],
have hs : ∑ i, (point_weights_with_circumcenter i₁ - point_weights_with_circumcenter i₂) i = 0,
{ simp },
rw [inner_weighted_vsub _ (sum_monge_point_vsub_face_centroid_weights_with_circumcenter h) _ hs,
sum_points_with_circumcenter, points_with_circumcenter_eq_circumcenter],
simp only [monge_point_vsub_face_centroid_weights_with_circumcenter,
points_with_circumcenter_point],
let fs : finset (fin (n + 3)) := {i₁, i₂},
have hfs : ∀ i : fin (n + 3),
i ∉ fs → (i ≠ i₁ ∧ i ≠ i₂),
{ intros i hi,
split ; { intro hj, simpa [←hj] using hi } },
rw ←sum_subset fs.subset_univ _,
{ simp_rw [sum_points_with_circumcenter, points_with_circumcenter_eq_circumcenter,
points_with_circumcenter_point, pi.sub_apply, point_weights_with_circumcenter],
rw [←sum_subset fs.subset_univ _],
{ simp_rw [sum_insert (not_mem_singleton.2 h), sum_singleton],
repeat { rw ←sum_subset fs.subset_univ _ },
{ simp_rw [sum_insert (not_mem_singleton.2 h), sum_singleton],
simp [h, h.symm, dist_comm (s.points i₁)] },
all_goals { intros i hu hi, simp [hfs i hi] } },
{ intros i hu hi,
simp [hfs i hi, point_weights_with_circumcenter] } },
{ intros i hu hi,
simp [hfs i hi] }
end
/-- A Monge plane of an (n+2)-simplex is the (n+1)-dimensional affine
subspace of the subspace spanned by the simplex that passes through
the centroid of an n-dimensional face and is orthogonal to the
opposite edge (in 2 dimensions, this is the same as an altitude).
This definition is only intended to be used when `i₁ ≠ i₂`. -/
def monge_plane {n : ℕ} (s : simplex ℝ P (n + 2)) (i₁ i₂ : fin (n + 3)) :
affine_subspace ℝ P :=
mk' (({i₁, i₂}ᶜ : finset (fin (n + 3))).centroid ℝ s.points) (submodule.span ℝ {s.points i₁ -ᵥ s.points i₂}).orthogonal ⊓
affine_span ℝ (set.range s.points)
/-- The definition of a Monge plane. -/
lemma monge_plane_def {n : ℕ} (s : simplex ℝ P (n + 2)) (i₁ i₂ : fin (n + 3)) :
s.monge_plane i₁ i₂ = mk' (({i₁, i₂}ᶜ : finset (fin (n + 3))).centroid ℝ s.points)
(submodule.span ℝ {s.points i₁ -ᵥ s.points i₂}).orthogonal ⊓
affine_span ℝ (set.range s.points) :=
rfl
/-- The Monge plane associated with vertices `i₁` and `i₂` equals that
associated with `i₂` and `i₁`. -/
lemma monge_plane_comm {n : ℕ} (s : simplex ℝ P (n + 2)) (i₁ i₂ : fin (n + 3)) :
s.monge_plane i₁ i₂ = s.monge_plane i₂ i₁ :=
begin
simp_rw monge_plane_def,
congr' 3,
{ congr' 1,
exact insert_singleton_comm _ _ },
{ ext,
simp_rw submodule.mem_span_singleton,
split,
all_goals { rintros ⟨r, rfl⟩, use -r, rw [neg_smul, ←smul_neg, neg_vsub_eq_vsub_rev] } }
end
/-- The Monge point lies in the Monge planes. -/
lemma monge_point_mem_monge_plane {n : ℕ} (s : simplex ℝ P (n + 2)) {i₁ i₂ : fin (n + 3)}
(h : i₁ ≠ i₂) : s.monge_point ∈ s.monge_plane i₁ i₂ :=
begin
rw [monge_plane_def, mem_inf_iff, ←vsub_right_mem_direction_iff_mem (self_mem_mk' _ _),
direction_mk', submodule.mem_orthogonal'],
refine ⟨_, s.monge_point_mem_affine_span⟩,
intros v hv,
rcases submodule.mem_span_singleton.mp hv with ⟨r, rfl⟩,
rw [inner_smul_right, s.inner_monge_point_vsub_face_centroid_vsub h, mul_zero]
end
-- This doesn't actually need the `i₁ ≠ i₂` hypothesis, but it's
-- convenient for the proof and `monge_plane` isn't intended to be
-- useful without that hypothesis.
/-- The direction of a Monge plane. -/
lemma direction_monge_plane {n : ℕ} (s : simplex ℝ P (n + 2)) {i₁ i₂ : fin (n + 3)} (h : i₁ ≠ i₂) :
(s.monge_plane i₁ i₂).direction = (submodule.span ℝ {s.points i₁ -ᵥ s.points i₂}).orthogonal ⊓
vector_span ℝ (set.range s.points) :=
by rw [monge_plane_def, direction_inf_of_mem_inf (s.monge_point_mem_monge_plane h), direction_mk',
direction_affine_span]
/-- The Monge point is the only point in all the Monge planes from any
one vertex. -/
lemma eq_monge_point_of_forall_mem_monge_plane {n : ℕ} {s : simplex ℝ P (n + 2)}
{i₁ : fin (n + 3)} {p : P} (h : ∀ i₂, i₁ ≠ i₂ → p ∈ s.monge_plane i₁ i₂) :
p = s.monge_point :=
begin
rw ←@vsub_eq_zero_iff_eq V,
have h' : ∀ i₂, i₁ ≠ i₂ → p -ᵥ s.monge_point ∈
(submodule.span ℝ {s.points i₁ -ᵥ s.points i₂}).orthogonal ⊓ vector_span ℝ (set.range s.points),
{ intros i₂ hne,
rw [←s.direction_monge_plane hne,
vsub_right_mem_direction_iff_mem (s.monge_point_mem_monge_plane hne)],
exact h i₂ hne },
have hi : p -ᵥ s.monge_point ∈ ⨅ (i₂ : {i // i₁ ≠ i}),
(submodule.span ℝ ({s.points i₁ -ᵥ s.points i₂}: set V)).orthogonal,
{ rw submodule.mem_infi,
exact λ i, (submodule.mem_inf.1 (h' i i.property)).1 },
rw [submodule.infi_orthogonal, ←submodule.span_Union] at hi,
have hu : (⋃ (i : {i // i₁ ≠ i}), ({s.points i₁ -ᵥ s.points i} : set V)) =
(-ᵥ) (s.points i₁) '' (s.points '' (set.univ \ {i₁})),
{ rw [set.image_image],
ext x,
simp_rw [set.mem_Union, set.mem_image, set.mem_singleton_iff, set.mem_diff_singleton],
split,
{ rintros ⟨i, rfl⟩,
use [i, ⟨set.mem_univ _, i.property.symm⟩] },
{ rintros ⟨i, ⟨hiu, hi⟩, rfl⟩,
use [⟨i, hi.symm⟩, rfl] } },
rw [hu, ←vector_span_image_eq_span_vsub_set_left_ne ℝ _ (set.mem_univ _),
set.image_univ] at hi,
have hv : p -ᵥ s.monge_point ∈ vector_span ℝ (set.range s.points),
{ let s₁ : finset (fin (n + 3)) := univ.erase i₁,
obtain ⟨i₂, h₂⟩ :=
card_pos.1 (show 0 < card s₁, by simp [card_erase_of_mem]),
have h₁₂ : i₁ ≠ i₂ := (ne_of_mem_erase h₂).symm,
exact (submodule.mem_inf.1 (h' i₂ h₁₂)).2 },
exact submodule.disjoint_def.1 ((vector_span ℝ (set.range s.points)).orthogonal_disjoint)
_ hv hi,
end
/-- An altitude of a simplex is the line that passes through a vertex
and is orthogonal to the opposite face. -/
def altitude {n : ℕ} (s : simplex ℝ P (n + 1)) (i : fin (n + 2)) : affine_subspace ℝ P :=
mk' (s.points i) (affine_span ℝ (s.points '' ↑(univ.erase i))).direction.orthogonal ⊓
affine_span ℝ (set.range s.points)
/-- The definition of an altitude. -/
lemma altitude_def {n : ℕ} (s : simplex ℝ P (n + 1)) (i : fin (n + 2)) :
s.altitude i = mk' (s.points i)
(affine_span ℝ (s.points '' ↑(univ.erase i))).direction.orthogonal ⊓
affine_span ℝ (set.range s.points) :=
rfl
end simplex
namespace triangle
open finset simplex
variables {V : Type*} {P : Type*} [inner_product_space V] [metric_space P]
[normed_add_torsor V P]
include V
/-- The orthocenter of a triangle is the intersection of its
altitudes. It is defined here as the 2-dimensional case of the
Monge point. -/
def orthocenter (t : triangle ℝ P) : P := t.monge_point
/-- The orthocenter equals the Monge point. -/
lemma orthocenter_eq_monge_point (t : triangle ℝ P) : t.orthocenter = t.monge_point := rfl
/-- The position of the orthocenter in relation to the circumcenter
and centroid. -/
lemma orthocenter_eq_smul_vsub_vadd_circumcenter (t : triangle ℝ P) :
t.orthocenter = (3 : ℝ) •
((univ : finset (fin 3)).centroid ℝ t.points -ᵥ t.circumcenter : V) +ᵥ t.circumcenter :=
begin
rw [orthocenter_eq_monge_point, monge_point_eq_smul_vsub_vadd_circumcenter],
norm_num
end
/-- The orthocenter lies in the affine span. -/
lemma orthocenter_mem_affine_span (t : triangle ℝ P) :
t.orthocenter ∈ affine_span ℝ (set.range t.points) :=
t.monge_point_mem_affine_span
/-- In the case of a triangle, altitudes are the same thing as Monge
planes. -/
lemma altitude_eq_monge_plane (t : triangle ℝ P) {i₁ i₂ i₃ : fin 3} (h₁₂ : i₁ ≠ i₂)
(h₁₃ : i₁ ≠ i₃) (h₂₃ : i₂ ≠ i₃) : t.altitude i₁ = t.monge_plane i₂ i₃ :=
begin
have hs : ({i₂, i₃}ᶜ : finset (fin 3)) = {i₁}, by dec_trivial!,
have he : univ.erase i₁ = {i₂, i₃}, by dec_trivial!,
rw [monge_plane_def, altitude_def, direction_affine_span, hs, he, centroid_singleton,
coe_insert, coe_singleton,
vector_span_image_eq_span_vsub_set_left_ne ℝ _ (set.mem_insert i₂ _)],
simp [h₂₃, submodule.span_insert_eq_span]
end
/-- The orthocenter lies in the altitudes. -/
lemma orthocenter_mem_altitude (t : triangle ℝ P) {i₁ : fin 3} :
t.orthocenter ∈ t.altitude i₁ :=
begin
obtain ⟨i₂, i₃, h₁₂, h₂₃, h₁₃⟩ : ∃ i₂ i₃, i₁ ≠ i₂ ∧ i₂ ≠ i₃ ∧ i₁ ≠ i₃, by dec_trivial!,
rw [orthocenter_eq_monge_point, t.altitude_eq_monge_plane h₁₂ h₁₃ h₂₃],
exact t.monge_point_mem_monge_plane h₂₃
end
/-- The orthocenter is the only point lying in any two of the
altitudes. -/
lemma eq_orthocenter_of_forall_mem_altitude {t : triangle ℝ P} {i₁ i₂ : fin 3} {p : P}
(h₁₂ : i₁ ≠ i₂) (h₁ : p ∈ t.altitude i₁) (h₂ : p ∈ t.altitude i₂) : p = t.orthocenter :=
begin
obtain ⟨i₃, h₂₃, h₁₃⟩ : ∃ i₃, i₂ ≠ i₃ ∧ i₁ ≠ i₃, { clear h₁ h₂, dec_trivial! },
rw t.altitude_eq_monge_plane h₁₃ h₁₂ h₂₃.symm at h₁,
rw t.altitude_eq_monge_plane h₂₃ h₁₂.symm h₁₃.symm at h₂,
rw orthocenter_eq_monge_point,
have ha : ∀ i, i₃ ≠ i → p ∈ t.monge_plane i₃ i,
{ intros i hi,
have hi₁₂ : i₁ = i ∨ i₂ = i, { clear h₁ h₂, dec_trivial! },
cases hi₁₂,
{ exact hi₁₂ ▸ h₂ },
{ exact hi₁₂ ▸ h₁ } },
exact eq_monge_point_of_forall_mem_monge_plane ha
end
end triangle
end affine
|
2d447f63dde37c5a546ff9ee3953220d37d6fade | bb31430994044506fa42fd667e2d556327e18dfe | /src/ring_theory/polynomial/basic.lean | 6a863a33dcc0f793154090b8c74176505a7462d5 | [
"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 | 50,401 | lean | /-
Copyright (c) 2019 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau
-/
import algebra.char_p.basic
import algebra.geom_sum
import data.mv_polynomial.comm_ring
import data.mv_polynomial.equiv
import ring_theory.polynomial.content
import ring_theory.unique_factorization_domain
/-!
# Ring-theoretic supplement of data.polynomial.
## Main results
* `mv_polynomial.is_domain`:
If a ring is an integral domain, then so is its polynomial ring over finitely many variables.
* `polynomial.is_noetherian_ring`:
Hilbert basis theorem, that if a ring is noetherian then so is its polynomial ring.
* `polynomial.wf_dvd_monoid`:
If an integral domain is a `wf_dvd_monoid`, then so is its polynomial ring.
* `polynomial.unique_factorization_monoid`, `mv_polynomial.unique_factorization_monoid`:
If an integral domain is a `unique_factorization_monoid`, then so is its polynomial ring (of any
number of variables).
-/
noncomputable theory
open_locale classical big_operators polynomial
open finset
universes u v w
variables {R : Type u} {S : Type*}
namespace polynomial
section semiring
variables [semiring R]
instance (p : ℕ) [h : char_p R p] : char_p R[X] p :=
let ⟨h⟩ := h in ⟨λ n, by rw [← map_nat_cast C, ← C_0, C_inj, h]⟩
variables (R)
/-- The `R`-submodule of `R[X]` consisting of polynomials of degree ≤ `n`. -/
def degree_le (n : with_bot ℕ) : submodule R R[X] :=
⨅ k : ℕ, ⨅ h : ↑k > n, (lcoeff R k).ker
/-- The `R`-submodule of `R[X]` consisting of polynomials of degree < `n`. -/
def degree_lt (n : ℕ) : submodule R R[X] :=
⨅ k : ℕ, ⨅ h : k ≥ n, (lcoeff R k).ker
variable {R}
theorem mem_degree_le {n : with_bot ℕ} {f : R[X]} :
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
@[mono] 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 : R[X])^n)) :=
begin
apply le_antisymm,
{ intros p hp, replace hp := mem_degree_le.1 hp,
rw [← polynomial.sum_monomial_eq p, polynomial.sum],
refine submodule.sum_mem _ (λ k hk, _),
show monomial _ _ ∈ _,
have := with_bot.coe_le_coe.1 (finset.sup_le_iff.1 hp k hk),
rw [← C_mul_X_pow_eq_monomial, 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,
exact (degree_X_pow_le _).trans
(with_bot.coe_le_coe.2 $ nat.le_of_lt_succ $ finset.mem_range.1 hk)
end
theorem mem_degree_lt {n : ℕ} {f : R[X]} :
f ∈ degree_lt R n ↔ degree f < n :=
by { simp_rw [degree_lt, submodule.mem_infi, linear_map.mem_ker, degree, finset.max_eq_sup_coe,
finset.sup_lt_iff (with_bot.bot_lt_coe n), mem_support_iff,
with_bot.coe_lt_coe, lt_iff_not_le, ne, not_imp_not], refl }
@[mono] theorem degree_lt_mono {m n : ℕ} (H : m ≤ n) :
degree_lt R m ≤ degree_lt R n :=
λ f hf, mem_degree_lt.2 (lt_of_lt_of_le (mem_degree_lt.1 hf) $ with_bot.coe_le_coe.2 H)
theorem degree_lt_eq_span_X_pow {n : ℕ} :
degree_lt R n = submodule.span R ↑((finset.range n).image (λ n, X^n) : finset R[X]) :=
begin
apply le_antisymm,
{ intros p hp, replace hp := mem_degree_lt.1 hp,
rw [← polynomial.sum_monomial_eq p, polynomial.sum],
refine submodule.sum_mem _ (λ k hk, _),
show monomial _ _ ∈ _,
have := with_bot.coe_lt_coe.1 ((finset.sup_lt_iff $ with_bot.bot_lt_coe n).1 hp k hk),
rw [← C_mul_X_pow_eq_monomial, C_mul'],
refine submodule.smul_mem _ _ (submodule.subset_span $ finset.mem_coe.2 $
finset.mem_image.2 ⟨_, finset.mem_range.2 this, rfl⟩) },
rw [submodule.span_le, finset.coe_image, set.image_subset_iff],
intros k hk, apply mem_degree_lt.2,
exact lt_of_le_of_lt (degree_X_pow_le _) (with_bot.coe_lt_coe.2 $ finset.mem_range.1 hk)
end
/-- The first `n` coefficients on `degree_lt n` form a linear equivalence with `fin n → R`. -/
def degree_lt_equiv (R) [semiring R] (n : ℕ) : degree_lt R n ≃ₗ[R] (fin n → R) :=
{ to_fun := λ p n, (↑p : R[X]).coeff n,
inv_fun := λ f, ⟨∑ i : fin n, monomial i (f i),
(degree_lt R n).sum_mem (λ i _, mem_degree_lt.mpr (lt_of_le_of_lt
(degree_monomial_le i (f i)) (with_bot.coe_lt_coe.mpr i.is_lt)))⟩,
map_add' := λ p q, by { ext, rw [submodule.coe_add, coeff_add], refl },
map_smul' := λ x p, by { ext, rw [submodule.coe_smul, coeff_smul], refl },
left_inv :=
begin
rintro ⟨p, hp⟩, ext1,
simp only [submodule.coe_mk],
by_cases hp0 : p = 0,
{ subst hp0, simp only [coeff_zero, linear_map.map_zero, finset.sum_const_zero] },
rw [mem_degree_lt, degree_eq_nat_degree hp0, with_bot.coe_lt_coe] at hp,
conv_rhs { rw [p.as_sum_range' n hp, ← fin.sum_univ_eq_sum_range] },
end,
right_inv :=
begin
intro f, ext i,
simp only [finset_sum_coeff, submodule.coe_mk],
rw [finset.sum_eq_single i, coeff_monomial, if_pos rfl],
{ rintro j - hji, rw [coeff_monomial, if_neg], rwa [← fin.ext_iff] },
{ intro h, exact (h (finset.mem_univ _)).elim }
end }
@[simp] theorem degree_lt_equiv_eq_zero_iff_eq_zero {n : ℕ} {p : R[X]} (hp : p ∈ degree_lt R n) :
degree_lt_equiv _ _ ⟨p, hp⟩ = 0 ↔ p = 0 :=
by rw [linear_equiv.map_eq_zero_iff, submodule.mk_eq_zero]
theorem eval_eq_sum_degree_lt_equiv {n : ℕ} {p : R[X]} (hp : p ∈ degree_lt R n) (x : R) :
p.eval x = ∑ i, degree_lt_equiv _ _ ⟨p, hp⟩ i * (x ^ (i : ℕ)) :=
begin
simp_rw [eval_eq_sum],
exact (sum_fin _ (by simp_rw [zero_mul, forall_const]) (mem_degree_lt.mp hp)).symm
end
/-- The finset of nonzero coefficients of a polynomial. -/
def frange (p : R[X]) : finset R :=
finset.image (λ n, p.coeff n) p.support
lemma frange_zero : frange (0 : R[X]) = ∅ :=
rfl
lemma mem_frange_iff {p : R[X]} {c : R} :
c ∈ p.frange ↔ ∃ n ∈ p.support, c = p.coeff n :=
by simp [frange, eq_comm]
lemma frange_one : frange (1 : R[X]) ⊆ {1} :=
begin
simp [frange, finset.image_subset_iff],
simp only [← C_1, coeff_C],
assume n hn,
simp only [exists_prop, ite_eq_right_iff, not_forall] at hn,
simp [hn],
end
lemma coeff_mem_frange (p : R[X]) (n : ℕ) (h : p.coeff n ≠ 0) :
p.coeff n ∈ p.frange :=
begin
simp only [frange, exists_prop, mem_support_iff, finset.mem_image, ne.def],
exact ⟨n, h, rfl⟩,
end
lemma geom_sum_X_comp_X_add_one_eq_sum (n : ℕ) :
(∑ i in range n, (X : R[X]) ^ i).comp (X + 1) =
(finset.range n).sum (λ (i : ℕ), (n.choose (i + 1) : R[X]) * X ^ i) :=
begin
ext i,
transitivity (n.choose (i + 1) : R), swap,
{ simp only [finset_sum_coeff, ← C_eq_nat_cast, coeff_C_mul_X_pow],
rw [finset.sum_eq_single i, if_pos rfl],
{ simp only [@eq_comm _ i, if_false, eq_self_iff_true, implies_true_iff] {contextual := tt}, },
{ simp only [nat.lt_add_one_iff, nat.choose_eq_zero_of_lt, nat.cast_zero, finset.mem_range,
not_lt, eq_self_iff_true, if_true, implies_true_iff] {contextual := tt}, } },
induction n with n ih generalizing i,
{ simp only [geom_sum_zero, zero_comp, coeff_zero, nat.choose_zero_succ, nat.cast_zero], },
simp only [geom_sum_succ', ih, add_comp, X_pow_comp, coeff_add, nat.choose_succ_succ,
nat.cast_add, coeff_X_add_one_pow],
end
lemma monic.geom_sum {P : R[X]}
(hP : P.monic) (hdeg : 0 < P.nat_degree) {n : ℕ} (hn : n ≠ 0) : (∑ i in range n, P ^ i).monic :=
begin
nontriviality R,
cases n, { exact (hn rfl).elim },
rw [geom_sum_succ'],
refine (hP.pow _).add_of_left _,
refine lt_of_le_of_lt (degree_sum_le _ _) _,
rw [finset.sup_lt_iff],
{ simp only [finset.mem_range, degree_eq_nat_degree (hP.pow _).ne_zero,
with_bot.coe_lt_coe, hP.nat_degree_pow],
intro k, exact nsmul_lt_nsmul hdeg },
{ rw [bot_lt_iff_ne_bot, ne.def, degree_eq_bot],
exact (hP.pow _).ne_zero }
end
lemma monic.geom_sum' {P : R[X]}
(hP : P.monic) (hdeg : 0 < P.degree) {n : ℕ} (hn : n ≠ 0) : (∑ i in range n, P ^ i).monic :=
hP.geom_sum (nat_degree_pos_iff_degree_pos.2 hdeg) hn
lemma monic_geom_sum_X {n : ℕ} (hn : n ≠ 0) :
(∑ i in range n, (X : R[X]) ^ i).monic :=
begin
nontriviality R,
apply monic_X.geom_sum _ hn,
simpa only [nat_degree_X] using zero_lt_one
end
end semiring
section ring
variables [ring R]
/-- Given a polynomial, return the polynomial whose coefficients are in
the ring closure of the original coefficients. -/
def restriction (p : R[X]) : polynomial (subring.closure (↑p.frange : set R)) :=
∑ i in p.support, monomial i (⟨p.coeff i,
if H : p.coeff i = 0 then H.symm ▸ (subring.closure _).zero_mem
else subring.subset_closure (p.coeff_mem_frange _ H)⟩ : (subring.closure (↑p.frange : set R)))
@[simp] theorem coeff_restriction {p : R[X]} {n : ℕ} :
↑(coeff (restriction p) n) = coeff p n :=
begin
simp only [restriction, coeff_monomial, finset_sum_coeff, mem_support_iff, finset.sum_ite_eq',
ne.def, ite_not],
split_ifs,
{ rw h, refl },
{ refl }
end
@[simp] theorem coeff_restriction' {p : R[X]} {n : ℕ} :
(coeff (restriction p) n).1 = coeff p n :=
coeff_restriction
@[simp] lemma support_restriction (p : R[X]) :
support (restriction p) = support p :=
begin
ext i,
simp only [mem_support_iff, not_iff_not, ne.def],
conv_rhs { rw [← coeff_restriction] },
exact ⟨λ H, by { rw H, refl }, λ H, subtype.coe_injective H⟩
end
@[simp] theorem map_restriction {R : Type u} [comm_ring R]
(p : R[X]) : p.restriction.map (algebra_map _ _) = p :=
ext $ λ n, by rw [coeff_map, algebra.algebra_map_of_subring_apply, coeff_restriction]
@[simp] theorem degree_restriction {p : R[X]} : (restriction p).degree = p.degree :=
by simp [degree]
@[simp] theorem nat_degree_restriction {p : R[X]} :
(restriction p).nat_degree = p.nat_degree :=
by simp [nat_degree]
@[simp] theorem monic_restriction {p : R[X]} : monic (restriction p) ↔ monic p :=
begin
simp only [monic, leading_coeff, nat_degree_restriction],
rw [←@coeff_restriction _ _ p],
exact ⟨λ H, by { rw H, refl }, λ H, subtype.coe_injective H⟩
end
@[simp] theorem restriction_zero : restriction (0 : R[X]) = 0 :=
by simp only [restriction, finset.sum_empty, support_zero]
@[simp] theorem restriction_one : restriction (1 : R[X]) = 1 :=
ext $ λ i, subtype.eq $ by rw [coeff_restriction', coeff_one, coeff_one]; split_ifs; refl
variables [semiring S] {f : R →+* S} {x : S}
theorem eval₂_restriction {p : R[X]} :
eval₂ f x p =
eval₂ (f.comp (subring.subtype (subring.closure (p.frange : set R)))) x p.restriction :=
begin
simp only [eval₂_eq_sum, sum, support_restriction, ←@coeff_restriction _ _ p],
refl,
end
section to_subring
variables (p : R[X]) (T : subring R)
/-- 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 : set R) ⊆ T) : T[X] :=
∑ i in p.support, monomial i (⟨p.coeff i,
if H : p.coeff i = 0 then H.symm ▸ T.zero_mem
else hp (p.coeff_mem_frange _ H)⟩ : T)
variables (hp : (↑p.frange : set R) ⊆ T)
include hp
@[simp] theorem coeff_to_subring {n : ℕ} : ↑(coeff (to_subring p T hp) n) = coeff p n :=
begin
simp only [to_subring, coeff_monomial, finset_sum_coeff, mem_support_iff, finset.sum_ite_eq',
ne.def, ite_not],
split_ifs,
{ rw h, refl },
{ refl }
end
@[simp] theorem coeff_to_subring' {n : ℕ} : (coeff (to_subring p T hp) n).1 = coeff p n :=
coeff_to_subring _ _ hp
@[simp] lemma support_to_subring :
support (to_subring p T hp) = support p :=
begin
ext i,
simp only [mem_support_iff, not_iff_not, ne.def],
conv_rhs { rw [← coeff_to_subring p T hp] },
exact ⟨λ H, by { rw H, refl }, λ H, subtype.coe_injective H⟩
end
@[simp] theorem degree_to_subring : (to_subring p T hp).degree = p.degree :=
by simp [degree]
@[simp] theorem nat_degree_to_subring : (to_subring p T hp).nat_degree = p.nat_degree :=
by simp [nat_degree]
@[simp] theorem monic_to_subring : monic (to_subring p T hp) ↔ monic p :=
begin
simp_rw [monic, leading_coeff, nat_degree_to_subring, ← coeff_to_subring p T hp],
exact ⟨λ H, by { rw H, refl }, λ H, subtype.coe_injective H⟩
end
omit hp
@[simp] theorem to_subring_zero : to_subring (0 : R[X]) T (by simp [frange_zero]) = 0 :=
by { ext i, simp }
@[simp] theorem to_subring_one : to_subring (1 : R[X]) T
(set.subset.trans frange_one $finset.singleton_subset_set_iff.2 T.one_mem) = 1 :=
ext $ λ i, subtype.eq $ by rw [coeff_to_subring', coeff_one, coeff_one]; split_ifs; refl
@[simp] theorem map_to_subring : (p.to_subring T hp).map (subring.subtype T) = p :=
by { ext n, simp [coeff_map] }
end to_subring
variables (T : subring R)
/-- Given a polynomial whose coefficients are in some subring, return
the corresponding polynomial whose coefficients are in the ambient ring. -/
def of_subring (p : T[X]) : R[X] :=
∑ i in p.support, monomial i (p.coeff i : R)
lemma coeff_of_subring (p : T[X]) (n : ℕ) :
coeff (of_subring T p) n = (coeff p n : T) :=
begin
simp only [of_subring, coeff_monomial, finset_sum_coeff, mem_support_iff, finset.sum_ite_eq',
ite_eq_right_iff, ne.def, ite_not, not_not, ite_eq_left_iff],
assume h,
rw h,
refl
end
@[simp] theorem frange_of_subring {p : T[X]} :
(↑(p.of_subring T).frange : set R) ⊆ T :=
begin
assume i hi,
simp only [frange, set.mem_image, mem_support_iff, ne.def, finset.mem_coe, finset.coe_image]
at hi,
rcases hi with ⟨n, hn, h'n⟩,
rw [← h'n, coeff_of_subring],
exact subtype.mem (coeff p n : T)
end
end ring
section comm_ring
variables [comm_ring R]
section mod_by_monic
variables {q : R[X]}
lemma mem_ker_mod_by_monic (hq : q.monic) {p : R[X]} :
p ∈ (mod_by_monic_hom q).ker ↔ q ∣ p :=
linear_map.mem_ker.trans (dvd_iff_mod_by_monic_eq_zero hq)
@[simp] lemma ker_mod_by_monic_hom (hq : q.monic) :
(polynomial.mod_by_monic_hom q).ker = (ideal.span {q}).restrict_scalars R :=
submodule.ext (λ f, (mem_ker_mod_by_monic hq).trans ideal.mem_span_singleton.symm)
end mod_by_monic
end comm_ring
end polynomial
namespace ideal
open polynomial
section semiring
variables [semiring R]
/-- Transport an ideal of `R[X]` to an `R`-submodule of `R[X]`. -/
def of_polynomial (I : ideal R[X]) : submodule R R[X] :=
{ carrier := I.carrier,
zero_mem' := I.zero_mem,
add_mem' := λ _ _, I.add_mem,
smul_mem' := λ c x H, by { rw [← C_mul'], exact I.mul_mem_left _ H } }
variables {I : ideal R[X]}
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 R[X] :=
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
/-- 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
end semiring
section comm_semiring
variables [comm_semiring R] [semiring S]
/-- If every coefficient of a polynomial is in an ideal `I`, then so is the polynomial itself -/
lemma polynomial_mem_ideal_of_coeff_mem_ideal (I : ideal R[X]) (p : R[X])
(hp : ∀ (n : ℕ), (p.coeff n) ∈ I.comap (C : R →+* R[X])) : p ∈ I :=
sum_C_mul_X_pow_eq p ▸ submodule.sum_mem I (λ n hn, I.mul_mem_right _ (hp n))
/-- The push-forward of an ideal `I` of `R` to `R[X]` via inclusion
is exactly the set of polynomials whose coefficients are in `I` -/
theorem mem_map_C_iff {I : ideal R} {f : R[X]} :
f ∈ (ideal.map (C : R →+* R[X]) I : ideal R[X]) ↔ ∀ n : ℕ, f.coeff n ∈ I :=
begin
split,
{ intros hf,
apply submodule.span_induction hf,
{ intros f hf n,
cases (set.mem_image _ _ _).mp hf with x hx,
rw [← hx.right, coeff_C],
by_cases (n = 0),
{ simpa [h] using hx.left },
{ simp [h] } },
{ simp },
{ exact λ f g hf hg n, by simp [I.add_mem (hf n) (hg n)] },
{ refine λ f g hg n, _,
rw [smul_eq_mul, coeff_mul],
exact I.sum_mem (λ c hc, I.mul_mem_left (f.coeff c.fst) (hg c.snd)) } },
{ intros hf,
rw ← sum_monomial_eq f,
refine (I.map C : ideal R[X]).sum_mem (λ n hn, _),
simp [← C_mul_X_pow_eq_monomial],
rw mul_comm,
exact (I.map C : ideal R[X]).mul_mem_left _ (mem_map_of_mem _ (hf n)) }
end
lemma _root_.polynomial.ker_map_ring_hom (f : R →+* S) :
(polynomial.map_ring_hom f).ker = f.ker.map (C : R →+* R[X]) :=
begin
ext,
rw [mem_map_C_iff, ring_hom.mem_ker, polynomial.ext_iff],
simp_rw [coe_map_ring_hom, coeff_map, coeff_zero, ring_hom.mem_ker],
end
variable (I : ideal R[X])
theorem mem_leading_coeff_nth (n : ℕ) (x) :
x ∈ I.leading_coeff_nth n ↔ ∃ p ∈ I, degree p ≤ n ∧ p.leading_coeff = 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, bot_le, _⟩,
rw [leading_coeff_zero, eq_comm],
exact coeff_eq_zero_of_degree_lt hpdeg },
{ refine ⟨p, hpI, le_of_eq hpdeg, _⟩,
rw [polynomial.leading_coeff, nat_degree, hpdeg], refl } },
{ rintro ⟨p, hpI, hpdeg, rfl⟩,
have : nat_degree p + (n - nat_degree p) = n,
{ exact add_tsub_cancel_of_le (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_rfl },
{ rw [polynomial.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, polynomial.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 [set_like.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, add_tsub_cancel_of_le H],
exact le_rfl
end
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⟩ },
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
/--
If `I` is an ideal, and `pᵢ` is a finite family of polynomials each satisfying
`∀ k, (pᵢ)ₖ ∈ Iⁿⁱ⁻ᵏ` for some `nᵢ`, then `p = ∏ pᵢ` also satisfies `∀ k, pₖ ∈ Iⁿ⁻ᵏ` with `n = ∑ nᵢ`.
-/
lemma _root_.polynomial.coeff_prod_mem_ideal_pow_tsub {ι : Type*} (s : finset ι) (f : ι → R[X])
(I : ideal R) (n : ι → ℕ) (h : ∀ (i ∈ s) k, (f i).coeff k ∈ I ^ (n i - k)) (k : ℕ) :
(s.prod f).coeff k ∈ I ^ (s.sum n - k) :=
begin
classical,
induction s using finset.induction with a s ha hs generalizing k,
{ rw [sum_empty, prod_empty, coeff_one, zero_tsub, pow_zero, ideal.one_eq_top],
exact submodule.mem_top },
{ rw [sum_insert ha, prod_insert ha, coeff_mul],
apply sum_mem,
rintro ⟨i, j⟩ e,
obtain rfl : i + j = k := nat.mem_antidiagonal.mp e,
apply ideal.pow_le_pow add_tsub_add_le_tsub_add_tsub,
rw pow_add,
exact ideal.mul_mem_mul (h _ (finset.mem_insert.mpr $ or.inl rfl) _)
(hs (λ i hi k, h _ (finset.mem_insert.mpr $ or.inr hi) _) j) }
end
end comm_semiring
section ring
variables [ring R]
/-- `R[X]` is never a field for any ring `R`. -/
lemma polynomial_not_is_field : ¬ is_field R[X] :=
begin
nontriviality R,
intro hR,
obtain ⟨p, hp⟩ := hR.mul_inv_cancel X_ne_zero,
have hp0 : p ≠ 0,
{ rintro rfl,
rw [mul_zero] at hp,
exact zero_ne_one hp },
have := degree_lt_degree_mul_X hp0,
rw [←X_mul, congr_arg degree hp, degree_one, nat.with_bot.lt_zero_iff, degree_eq_bot] at this,
exact hp0 this,
end
/-- The only constant in a maximal ideal over a field is `0`. -/
lemma eq_zero_of_constant_mem_of_maximal (hR : is_field R)
(I : ideal R[X]) [hI : I.is_maximal] (x : R) (hx : C x ∈ I) : x = 0 :=
begin
refine classical.by_contradiction (λ hx0, hI.ne_top ((eq_top_iff_one I).2 _)),
obtain ⟨y, hy⟩ := hR.mul_inv_cancel hx0,
convert I.mul_mem_left (C y) hx,
rw [← C.map_mul, hR.mul_comm y x, hy, ring_hom.map_one],
end
end ring
section comm_ring
variables [comm_ring R]
lemma quotient_map_C_eq_zero {I : ideal R} :
∀ a ∈ I, ((quotient.mk (map (C : R →+* R[X]) I : ideal R[X])).comp C) a = 0 :=
begin
intros a ha,
rw [ring_hom.comp_apply, quotient.eq_zero_iff_mem],
exact mem_map_of_mem _ ha,
end
lemma eval₂_C_mk_eq_zero {I : ideal R} :
∀ f ∈ (map (C : R →+* R[X]) I : ideal R[X]), eval₂_ring_hom (C.comp (quotient.mk I)) X f = 0 :=
begin
intros a ha,
rw ← sum_monomial_eq a,
dsimp,
rw eval₂_sum,
refine finset.sum_eq_zero (λ n hn, _),
dsimp,
rw eval₂_monomial (C.comp (quotient.mk I)) X,
refine mul_eq_zero_of_left (polynomial.ext (λ m, _)) (X ^ n),
erw coeff_C,
by_cases h : m = 0,
{ simpa [h] using quotient.eq_zero_iff_mem.2 ((mem_map_C_iff.1 ha) n) },
{ simp [h] }
end
/-- If `I` is an ideal of `R`, then the ring polynomials over the quotient ring `I.quotient` is
isomorphic to the quotient of `R[X]` by the ideal `map C I`,
where `map C I` contains exactly the polynomials whose coefficients all lie in `I` -/
def polynomial_quotient_equiv_quotient_polynomial (I : ideal R) :
(R ⧸ I)[X] ≃+* R[X] ⧸ (map C I : ideal R[X]) :=
{ to_fun := eval₂_ring_hom
(quotient.lift I ((quotient.mk (map C I : ideal R[X])).comp C) quotient_map_C_eq_zero)
((quotient.mk (map C I : ideal R[X]) X)),
inv_fun := quotient.lift (map C I : ideal R[X])
(eval₂_ring_hom (C.comp (quotient.mk I)) X) eval₂_C_mk_eq_zero,
map_mul' := λ f g, by simp only [coe_eval₂_ring_hom, eval₂_mul],
map_add' := λ f g, by simp only [eval₂_add, coe_eval₂_ring_hom],
left_inv := begin
intro f,
apply polynomial.induction_on' f,
{ intros p q hp hq,
simp only [coe_eval₂_ring_hom] at hp,
simp only [coe_eval₂_ring_hom] at hq,
simp only [coe_eval₂_ring_hom, hp, hq, ring_hom.map_add] },
{ rintros n ⟨x⟩,
simp only [← smul_X_eq_monomial, C_mul', quotient.lift_mk, submodule.quotient.quot_mk_eq_mk,
quotient.mk_eq_mk, eval₂_X_pow, eval₂_smul, coe_eval₂_ring_hom, ring_hom.map_pow,
eval₂_C, ring_hom.coe_comp, ring_hom.map_mul, eval₂_X] }
end,
right_inv := begin
rintro ⟨f⟩,
apply polynomial.induction_on' f,
{ simp_intros p q hp hq,
rw [hp, hq] },
{ intros n a,
simp only [← smul_X_eq_monomial, ← C_mul' a (X ^ n), quotient.lift_mk,
submodule.quotient.quot_mk_eq_mk, quotient.mk_eq_mk, eval₂_X_pow,
eval₂_smul, coe_eval₂_ring_hom, ring_hom.map_pow, eval₂_C, ring_hom.coe_comp,
ring_hom.map_mul, eval₂_X] },
end, }
@[simp]
lemma polynomial_quotient_equiv_quotient_polynomial_symm_mk (I : ideal R) (f : R[X]) :
I.polynomial_quotient_equiv_quotient_polynomial.symm (quotient.mk _ f) = f.map (quotient.mk I) :=
by rw [polynomial_quotient_equiv_quotient_polynomial, ring_equiv.symm_mk, ring_equiv.coe_mk,
ideal.quotient.lift_mk, coe_eval₂_ring_hom, eval₂_eq_eval_map, ←polynomial.map_map,
←eval₂_eq_eval_map, polynomial.eval₂_C_X]
@[simp]
lemma polynomial_quotient_equiv_quotient_polynomial_map_mk (I : ideal R) (f : R[X]) :
I.polynomial_quotient_equiv_quotient_polynomial (f.map I^.quotient.mk) = quotient.mk _ f :=
begin
apply (polynomial_quotient_equiv_quotient_polynomial I).symm.injective,
rw [ring_equiv.symm_apply_apply, polynomial_quotient_equiv_quotient_polynomial_symm_mk],
end
/-- If `P` is a prime ideal of `R`, then `R[x]/(P)` is an integral domain. -/
lemma is_domain_map_C_quotient {P : ideal R} (H : is_prime P) :
is_domain (R[X] ⧸ (map (C : R →+* R[X]) P : ideal R[X])) :=
ring_equiv.is_domain (polynomial (R ⧸ P))
(polynomial_quotient_equiv_quotient_polynomial P).symm
/-- If `P` is a prime ideal of `R`, then `P.R[x]` is a prime ideal of `R[x]`. -/
lemma is_prime_map_C_of_is_prime {P : ideal R} (H : is_prime P) :
is_prime (map (C : R →+* R[X]) P : ideal R[X]) :=
(quotient.is_domain_iff_prime (map C P : ideal R[X])).mp
(is_domain_map_C_quotient H)
/-- Given any ring `R` and an ideal `I` of `R[X]`, we get a map `R → R[x] → R[x]/I`.
If we let `R` be the image of `R` in `R[x]/I` then we also have a map `R[x] → R'[x]`.
In particular we can map `I` across this map, to get `I'` and a new map `R' → R'[x] → R'[x]/I`.
This theorem shows `I'` will not contain any non-zero constant polynomials
-/
lemma eq_zero_of_polynomial_mem_map_range (I : ideal R[X])
(x : ((quotient.mk I).comp C).range)
(hx : C x ∈ (I.map (polynomial.map_ring_hom ((quotient.mk I).comp C).range_restrict))) :
x = 0 :=
begin
let i := ((quotient.mk I).comp C).range_restrict,
have hi' : (polynomial.map_ring_hom i).ker ≤ I,
{ refine λ f hf, polynomial_mem_ideal_of_coeff_mem_ideal I f (λ n, _),
rw [mem_comap, ← quotient.eq_zero_iff_mem, ← ring_hom.comp_apply],
rw [ring_hom.mem_ker, coe_map_ring_hom] at hf,
replace hf := congr_arg (λ (f : polynomial _), f.coeff n) hf,
simp only [coeff_map, coeff_zero] at hf,
rwa [subtype.ext_iff, ring_hom.coe_range_restrict] at hf },
obtain ⟨x, hx'⟩ := x,
obtain ⟨y, rfl⟩ := (ring_hom.mem_range).1 hx',
refine subtype.eq _,
simp only [ring_hom.comp_apply, quotient.eq_zero_iff_mem, zero_mem_class.coe_zero,
subtype.val_eq_coe],
suffices : C (i y) ∈ (I.map (polynomial.map_ring_hom i)),
{ obtain ⟨f, hf⟩ := mem_image_of_mem_map_of_surjective (polynomial.map_ring_hom i)
(polynomial.map_surjective _ (((quotient.mk I).comp C).range_restrict_surjective)) this,
refine sub_add_cancel (C y) f ▸ I.add_mem (hi' _ : (C y - f) ∈ I) hf.1,
rw [ring_hom.mem_ker, ring_hom.map_sub, hf.2, sub_eq_zero, coe_map_ring_hom, map_C] },
exact hx,
end
theorem is_fg_degree_le [is_noetherian_ring R] (I : ideal R[X]) (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 comm_ring
end ideal
variables {σ : Type v} {M : Type w}
variables [comm_ring R] [comm_ring S] [add_comm_group M] [module R M]
section prime
variables (σ) {r : R}
namespace polynomial
lemma prime_C_iff : prime (C r) ↔ prime r :=
⟨ comap_prime C (eval_ring_hom (0 : R)) (λ r, eval_C),
λ hr, by { have := hr.1,
rw ← ideal.span_singleton_prime at hr ⊢,
{ convert ideal.is_prime_map_C_of_is_prime hr using 1,
rw [ideal.map_span, set.image_singleton] },
exacts [λ h, this (C_eq_zero.1 h), this] } ⟩
end polynomial
namespace mv_polynomial
private lemma prime_C_iff_of_fintype [fintype σ] : prime (C r : mv_polynomial σ R) ↔ prime r :=
begin
rw (rename_equiv R (fintype.equiv_fin σ)).to_mul_equiv.prime_iff,
convert_to prime (C r) ↔ _, { congr, apply rename_C },
{ symmetry, induction fintype.card σ with d hd,
{ exact (is_empty_alg_equiv R (fin 0)).to_mul_equiv.symm.prime_iff },
{ rw [hd, ← polynomial.prime_C_iff],
convert (fin_succ_equiv R d).to_mul_equiv.symm.prime_iff,
rw ← fin_succ_equiv_comp_C_eq_C, refl } },
end
lemma prime_C_iff : prime (C r : mv_polynomial σ R) ↔ prime r :=
⟨ comap_prime C constant_coeff (constant_coeff_C _),
λ hr, ⟨ λ h, hr.1 $ by { rw [← C_inj, h], simp },
λ h, hr.2.1 $ by { rw ← constant_coeff_C _ r, exact h.map _ },
λ a b hd, begin
obtain ⟨s,a',b',rfl,rfl⟩ := exists_finset_rename₂ a b,
rw ← algebra_map_eq at hd, have : algebra_map R _ r ∣ a' * b',
{ convert (kill_compl subtype.coe_injective).to_ring_hom.map_dvd hd, simpa, simp },
rw ← rename_C (coe : s → σ), let f := (rename (coe : s → σ)).to_ring_hom,
exact (((prime_C_iff_of_fintype s).2 hr).2.2 a' b' this).imp f.map_dvd f.map_dvd,
end ⟩ ⟩
variable {σ}
lemma prime_rename_iff (s : set σ) {p : mv_polynomial s R} :
prime (rename (coe : s → σ) p) ↔ prime p :=
begin
classical, symmetry, let eqv := (sum_alg_equiv R _ _).symm.trans
(rename_equiv R $ (equiv.sum_comm ↥sᶜ s).trans $ equiv.set.sum_compl s),
rw [← prime_C_iff ↥sᶜ, eqv.to_mul_equiv.prime_iff], convert iff.rfl,
suffices : (rename coe).to_ring_hom = eqv.to_alg_hom.to_ring_hom.comp C,
{ apply ring_hom.congr_fun this },
{ apply ring_hom_ext,
{ intro, dsimp [eqv], erw [iter_to_sum_C_C, rename_C, rename_C] },
{ intro, dsimp [eqv], erw [iter_to_sum_C_X, rename_X, rename_X], refl } },
end
end mv_polynomial
end prime
namespace polynomial
@[priority 100]
instance {R : Type*} [comm_ring R] [is_domain R] [wf_dvd_monoid R] :
wf_dvd_monoid R[X] :=
{ well_founded_dvd_not_unit := begin
classical,
refine rel_hom_class.well_founded (⟨λ (p : R[X]),
((if p = 0 then ⊤ else ↑p.degree : with_top (with_bot ℕ)), p.leading_coeff), _⟩ :
dvd_not_unit →r prod.lex (<) dvd_not_unit)
(prod.lex_wf (with_top.well_founded_lt $ with_bot.well_founded_lt nat.lt_wf)
‹wf_dvd_monoid R›.well_founded_dvd_not_unit),
rintros a b ⟨ane0, ⟨c, ⟨not_unit_c, rfl⟩⟩⟩,
rw [polynomial.degree_mul, if_neg ane0],
split_ifs with hac,
{ rw [hac, polynomial.leading_coeff_zero],
apply prod.lex.left,
exact lt_of_le_of_ne le_top with_top.coe_ne_top },
have cne0 : c ≠ 0 := right_ne_zero_of_mul hac,
simp only [cne0, ane0, polynomial.leading_coeff_mul],
by_cases hdeg : c.degree = 0,
{ simp only [hdeg, add_zero],
refine prod.lex.right _ ⟨_, ⟨c.leading_coeff, (λ unit_c, not_unit_c _), rfl⟩⟩,
{ rwa [ne, polynomial.leading_coeff_eq_zero] },
rw [polynomial.is_unit_iff, polynomial.eq_C_of_degree_eq_zero hdeg],
use [c.leading_coeff, unit_c],
rw [polynomial.leading_coeff, polynomial.nat_degree_eq_of_degree_eq_some hdeg] },
{ apply prod.lex.left,
rw polynomial.degree_eq_nat_degree cne0 at *,
rw [with_top.coe_lt_coe, polynomial.degree_eq_nat_degree ane0,
← with_bot.coe_add, with_bot.coe_lt_coe],
exact lt_add_of_pos_right _ (nat.pos_of_ne_zero (λ h, hdeg (h.symm ▸ with_bot.coe_zero))) },
end }
end polynomial
/-- Hilbert basis theorem: a polynomial ring over a noetherian ring is a noetherian ring. -/
protected theorem polynomial.is_noetherian_ring [is_noetherian_ring R] :
is_noetherian_ring R[X] :=
is_noetherian_ring_iff.2 ⟨assume I : ideal R[X],
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 R[X]),
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
have : submodule.span R[X] ↑s = ideal.span ↑s, by refl,
rw this,
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 },
haveI : nontrivial R := ⟨⟨0, 1, this⟩⟩,
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', polynomial.degree_X_pow],
rw [polynomial.degree_eq_nat_degree hp0, polynomial.degree_eq_nat_degree hq0],
rw [← with_bot.coe_add, add_tsub_cancel_of_le, 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⟩⟩
attribute [instance] polynomial.is_noetherian_ring
namespace polynomial
theorem exists_irreducible_of_degree_pos
{R : Type u} [comm_ring R] [is_domain R] [wf_dvd_monoid R]
{f : R[X]} (hf : 0 < f.degree) : ∃ g, irreducible g ∧ g ∣ f :=
wf_dvd_monoid.exists_irreducible_factor
(λ huf, ne_of_gt hf $ degree_eq_zero_of_is_unit huf)
(λ hf0, not_lt_of_lt hf $ hf0.symm ▸ (@degree_zero R _).symm ▸ with_bot.bot_lt_coe _)
theorem exists_irreducible_of_nat_degree_pos
{R : Type u} [comm_ring R] [is_domain R] [wf_dvd_monoid R]
{f : R[X]} (hf : 0 < f.nat_degree) : ∃ g, irreducible g ∧ g ∣ f :=
exists_irreducible_of_degree_pos $ by { contrapose! hf, exact nat_degree_le_of_degree_le hf }
theorem exists_irreducible_of_nat_degree_ne_zero
{R : Type u} [comm_ring R] [is_domain R] [wf_dvd_monoid R]
{f : R[X]} (hf : f.nat_degree ≠ 0) : ∃ g, irreducible g ∧ g ∣ f :=
exists_irreducible_of_nat_degree_pos $ nat.pos_of_ne_zero hf
lemma linear_independent_powers_iff_aeval
(f : M →ₗ[R] M) (v : M) :
linear_independent R (λ n : ℕ, (f ^ n) v)
↔ ∀ (p : R[X]), aeval f p v = 0 → p = 0 :=
begin
rw linear_independent_iff,
simp only [finsupp.total_apply, aeval_endomorphism, forall_iff_forall_finsupp, sum, support,
coeff, of_finsupp_eq_zero],
exact iff.rfl,
end
lemma disjoint_ker_aeval_of_coprime
(f : M →ₗ[R] M) {p q : R[X]} (hpq : is_coprime p q) :
disjoint (aeval f p).ker (aeval f q).ker :=
begin
rw disjoint_iff_inf_le,
intros v hv,
rcases hpq with ⟨p', q', hpq'⟩,
simpa [linear_map.mem_ker.1 (submodule.mem_inf.1 hv).1,
linear_map.mem_ker.1 (submodule.mem_inf.1 hv).2]
using congr_arg (λ p : R[X], aeval f p v) hpq'.symm,
end
lemma sup_aeval_range_eq_top_of_coprime
(f : M →ₗ[R] M) {p q : R[X]} (hpq : is_coprime p q) :
(aeval f p).range ⊔ (aeval f q).range = ⊤ :=
begin
rw eq_top_iff,
intros v hv,
rw submodule.mem_sup,
rcases hpq with ⟨p', q', hpq'⟩,
use aeval f (p * p') v,
use linear_map.mem_range.2 ⟨aeval f p' v, by simp only [linear_map.mul_apply, aeval_mul]⟩,
use aeval f (q * q') v,
use linear_map.mem_range.2 ⟨aeval f q' v, by simp only [linear_map.mul_apply, aeval_mul]⟩,
simpa only [mul_comm p p', mul_comm q q', aeval_one, aeval_add]
using congr_arg (λ p : R[X], aeval f p v) hpq'
end
lemma sup_ker_aeval_le_ker_aeval_mul {f : M →ₗ[R] M} {p q : R[X]} :
(aeval f p).ker ⊔ (aeval f q).ker ≤ (aeval f (p * q)).ker :=
begin
intros v hv,
rcases submodule.mem_sup.1 hv with ⟨x, hx, y, hy, hxy⟩,
have h_eval_x : aeval f (p * q) x = 0,
{ rw [mul_comm, aeval_mul, linear_map.mul_apply, linear_map.mem_ker.1 hx, linear_map.map_zero] },
have h_eval_y : aeval f (p * q) y = 0,
{ rw [aeval_mul, linear_map.mul_apply, linear_map.mem_ker.1 hy, linear_map.map_zero] },
rw [linear_map.mem_ker, ←hxy, linear_map.map_add, h_eval_x, h_eval_y, add_zero],
end
lemma sup_ker_aeval_eq_ker_aeval_mul_of_coprime
(f : M →ₗ[R] M) {p q : R[X]} (hpq : is_coprime p q) :
(aeval f p).ker ⊔ (aeval f q).ker = (aeval f (p * q)).ker :=
begin
apply le_antisymm sup_ker_aeval_le_ker_aeval_mul,
intros v hv,
rw submodule.mem_sup,
rcases hpq with ⟨p', q', hpq'⟩,
have h_eval₂_qpp' := calc
aeval f (q * (p * p')) v = aeval f (p' * (p * q)) v :
by rw [mul_comm, mul_assoc, mul_comm, mul_assoc, mul_comm q p]
... = 0 :
by rw [aeval_mul, linear_map.mul_apply, linear_map.mem_ker.1 hv, linear_map.map_zero],
have h_eval₂_pqq' := calc
aeval f (p * (q * q')) v = aeval f (q' * (p * q)) v :
by rw [←mul_assoc, mul_comm]
... = 0 :
by rw [aeval_mul, linear_map.mul_apply, linear_map.mem_ker.1 hv, linear_map.map_zero],
rw aeval_mul at h_eval₂_qpp' h_eval₂_pqq',
refine ⟨aeval f (q * q') v, linear_map.mem_ker.1 h_eval₂_pqq',
aeval f (p * p') v, linear_map.mem_ker.1 h_eval₂_qpp', _⟩,
rw [add_comm, mul_comm p p', mul_comm q q'],
simpa using congr_arg (λ p : R[X], aeval f p v) hpq'
end
end polynomial
namespace mv_polynomial
lemma is_noetherian_ring_fin_0 [is_noetherian_ring R] :
is_noetherian_ring (mv_polynomial (fin 0) R) :=
is_noetherian_ring_of_ring_equiv R
((mv_polynomial.is_empty_ring_equiv R pempty).symm.trans
(rename_equiv R fin_zero_equiv'.symm).to_ring_equiv)
theorem is_noetherian_ring_fin [is_noetherian_ring R] :
∀ {n : ℕ}, is_noetherian_ring (mv_polynomial (fin n) R)
| 0 := is_noetherian_ring_fin_0
| (n+1) :=
@is_noetherian_ring_of_ring_equiv (polynomial (mv_polynomial (fin n) R)) _ _ _
(mv_polynomial.fin_succ_equiv _ n).to_ring_equiv.symm
(@polynomial.is_noetherian_ring (mv_polynomial (fin n) R) _ (is_noetherian_ring_fin))
/-- The multivariate polynomial ring in finitely many variables over a noetherian ring
is itself a noetherian ring. -/
instance is_noetherian_ring [finite σ] [is_noetherian_ring R] :
is_noetherian_ring (mv_polynomial σ R) :=
by casesI nonempty_fintype σ; exact
@is_noetherian_ring_of_ring_equiv (mv_polynomial (fin (fintype.card σ)) R) _ _ _
(rename_equiv R (fintype.equiv_fin σ).symm).to_ring_equiv is_noetherian_ring_fin
/-- Auxiliary lemma:
Multivariate polynomials over an integral domain
with variables indexed by `fin n` form an integral domain.
This fact is proven inductively,
and then used to prove the general case without any finiteness hypotheses.
See `mv_polynomial.no_zero_divisors` for the general case. -/
lemma no_zero_divisors_fin (R : Type u) [comm_semiring R] [no_zero_divisors R] :
∀ (n : ℕ), no_zero_divisors (mv_polynomial (fin n) R)
| 0 := (mv_polynomial.is_empty_alg_equiv R _).injective.no_zero_divisors _ (map_zero _) (map_mul _)
| (n+1) := begin
haveI := no_zero_divisors_fin n,
exact (mv_polynomial.fin_succ_equiv R n).injective.no_zero_divisors _ (map_zero _) (map_mul _)
end
/-- Auxiliary definition:
Multivariate polynomials in finitely many variables over an integral domain form an integral domain.
This fact is proven by transport of structure from the `mv_polynomial.no_zero_divisors_fin`,
and then used to prove the general case without finiteness hypotheses.
See `mv_polynomial.no_zero_divisors` for the general case. -/
lemma no_zero_divisors_of_finite (R : Type u) (σ : Type v) [comm_semiring R] [finite σ]
[no_zero_divisors R] : no_zero_divisors (mv_polynomial σ R) :=
begin
casesI nonempty_fintype σ,
haveI := no_zero_divisors_fin R (fintype.card σ),
exact (rename_equiv R (fintype.equiv_fin σ)).injective.no_zero_divisors _ (map_zero _) (map_mul _)
end
instance {R : Type u} [comm_semiring R] [no_zero_divisors R] {σ : Type v} :
no_zero_divisors (mv_polynomial σ R) :=
⟨λ p q h, begin
obtain ⟨s, p, rfl⟩ := exists_finset_rename p,
obtain ⟨t, q, rfl⟩ := exists_finset_rename q,
have :
rename (subtype.map id (finset.subset_union_left s t) : {x // x ∈ s} → {x // x ∈ s ∪ t}) p *
rename (subtype.map id (finset.subset_union_right s t) : {x // x ∈ t} → {x // x ∈ s ∪ t}) q = 0,
{ apply rename_injective _ subtype.val_injective, simpa using h },
letI := mv_polynomial.no_zero_divisors_of_finite R {x // x ∈ (s ∪ t)},
rw mul_eq_zero at this,
cases this; [left, right],
all_goals { simpa using congr_arg (rename subtype.val) this }
end⟩
/-- The multivariate polynomial ring over an integral domain is an integral domain. -/
instance {R : Type u} {σ : Type v} [comm_ring R] [is_domain R] : is_domain (mv_polynomial σ R) :=
begin
apply no_zero_divisors.to_is_domain _,
exact add_monoid_algebra.nontrivial,
exact mv_polynomial.no_zero_divisors
end
lemma map_mv_polynomial_eq_eval₂ {S : Type*} [comm_ring S] [finite σ]
(ϕ : mv_polynomial σ R →+* S) (p : mv_polynomial σ R) :
ϕ p = mv_polynomial.eval₂ (ϕ.comp mv_polynomial.C) (λ s, ϕ (mv_polynomial.X s)) p :=
begin
casesI nonempty_fintype σ,
refine trans (congr_arg ϕ (mv_polynomial.as_sum p)) _,
rw [mv_polynomial.eval₂_eq', ϕ.map_sum],
congr,
ext,
simp only [monomial_eq, ϕ.map_pow, ϕ.map_prod, ϕ.comp_apply, ϕ.map_mul, finsupp.prod_pow],
end
lemma quotient_map_C_eq_zero {I : ideal R} {i : R} (hi : i ∈ I) :
(ideal.quotient.mk (ideal.map (C : R →+* mv_polynomial σ R) I :
ideal (mv_polynomial σ R))).comp C i = 0 :=
begin
simp only [function.comp_app, ring_hom.coe_comp, ideal.quotient.eq_zero_iff_mem],
exact ideal.mem_map_of_mem _ hi
end
/-- If every coefficient of a polynomial is in an ideal `I`, then so is the polynomial itself,
multivariate version. -/
lemma mem_ideal_of_coeff_mem_ideal (I : ideal (mv_polynomial σ R)) (p : mv_polynomial σ R)
(hcoe : ∀ (m : σ →₀ ℕ), p.coeff m ∈ I.comap (C : R →+* mv_polynomial σ R)) : p ∈ I :=
begin
rw as_sum p,
suffices : ∀ m ∈ p.support, monomial m (mv_polynomial.coeff m p) ∈ I,
{ exact submodule.sum_mem I this },
intros m hm,
rw [← mul_one (coeff m p), ← C_mul_monomial],
suffices : C (coeff m p) ∈ I,
{ exact I.mul_mem_right (monomial m 1) this },
simpa [ideal.mem_comap] using hcoe m
end
/-- The push-forward of an ideal `I` of `R` to `mv_polynomial σ R` via inclusion
is exactly the set of polynomials whose coefficients are in `I` -/
theorem mem_map_C_iff {I : ideal R} {f : mv_polynomial σ R} :
f ∈ (ideal.map (C : R →+* mv_polynomial σ R) I :
ideal (mv_polynomial σ R)) ↔ ∀ (m : σ →₀ ℕ), f.coeff m ∈ I :=
begin
split,
{ intros hf,
apply submodule.span_induction hf,
{ intros f hf n,
cases (set.mem_image _ _ _).mp hf with x hx,
rw [← hx.right, coeff_C],
by_cases (n = 0),
{ simpa [h] using hx.left },
{ simp [ne.symm h] } },
{ simp },
{ exact λ f g hf hg n, by simp [I.add_mem (hf n) (hg n)] },
{ refine λ f g hg n, _,
rw [smul_eq_mul, coeff_mul],
exact I.sum_mem (λ c hc, I.mul_mem_left (f.coeff c.fst) (hg c.snd)) } },
{ intros hf,
rw as_sum f,
suffices : ∀ m ∈ f.support, monomial m (coeff m f) ∈
(ideal.map C I : ideal (mv_polynomial σ R)),
{ exact submodule.sum_mem _ this },
intros m hm,
rw [← mul_one (coeff m f), ← C_mul_monomial],
suffices : C (coeff m f) ∈ (ideal.map C I : ideal (mv_polynomial σ R)),
{ exact ideal.mul_mem_right _ _ this },
apply ideal.mem_map_of_mem _,
exact hf m }
end
lemma ker_map (f : R →+* S) :
(map f : mv_polynomial σ R →+* mv_polynomial σ S).ker = f.ker.map (C : R →+* mv_polynomial σ R) :=
begin
ext,
rw [mv_polynomial.mem_map_C_iff, ring_hom.mem_ker, mv_polynomial.ext_iff],
simp_rw [coeff_map, coeff_zero, ring_hom.mem_ker],
end
lemma eval₂_C_mk_eq_zero {I : ideal R} {a : mv_polynomial σ R}
(ha : a ∈ (ideal.map (C : R →+* mv_polynomial σ R) I : ideal (mv_polynomial σ R))) :
eval₂_hom (C.comp (ideal.quotient.mk I)) X a = 0 :=
begin
rw as_sum a,
rw [coe_eval₂_hom, eval₂_sum],
refine finset.sum_eq_zero (λ n hn, _),
simp only [eval₂_monomial, function.comp_app, ring_hom.coe_comp],
refine mul_eq_zero_of_left _ _,
suffices : coeff n a ∈ I,
{ rw [← @ideal.mk_ker R _ I, ring_hom.mem_ker] at this,
simp only [this, C_0] },
exact mem_map_C_iff.1 ha n
end
/-- If `I` is an ideal of `R`, then the ring `mv_polynomial σ I.quotient` is isomorphic as an
`R`-algebra to the quotient of `mv_polynomial σ R` by the ideal generated by `I`. -/
def quotient_equiv_quotient_mv_polynomial (I : ideal R) :
mv_polynomial σ (R ⧸ I) ≃ₐ[R]
mv_polynomial σ R ⧸ (ideal.map C I : ideal (mv_polynomial σ R)) :=
{ to_fun := eval₂_hom (ideal.quotient.lift I ((ideal.quotient.mk (ideal.map C I : ideal
(mv_polynomial σ R))).comp C) (λ i hi, quotient_map_C_eq_zero hi))
(λ i, ideal.quotient.mk (ideal.map C I : ideal (mv_polynomial σ R)) (X i)),
inv_fun := ideal.quotient.lift (ideal.map C I : ideal (mv_polynomial σ R))
(eval₂_hom (C.comp (ideal.quotient.mk I)) X) (λ a ha, eval₂_C_mk_eq_zero ha),
map_mul' := ring_hom.map_mul _,
map_add' := ring_hom.map_add _,
left_inv := begin
intro f,
apply induction_on f,
{ rintro ⟨r⟩,
rw [coe_eval₂_hom, eval₂_C],
simp only [eval₂_hom_eq_bind₂, submodule.quotient.quot_mk_eq_mk, ideal.quotient.lift_mk,
ideal.quotient.mk_eq_mk, bind₂_C_right, ring_hom.coe_comp] },
{ simp_intros p q hp hq only [ring_hom.map_add, mv_polynomial.coe_eval₂_hom, coe_eval₂_hom,
mv_polynomial.eval₂_add, mv_polynomial.eval₂_hom_eq_bind₂, eval₂_hom_eq_bind₂],
rw [hp, hq] },
{ simp_intros p i hp only [eval₂_hom_eq_bind₂, coe_eval₂_hom],
simp only [hp, eval₂_hom_eq_bind₂, coe_eval₂_hom, ideal.quotient.lift_mk, bind₂_X_right,
eval₂_mul, ring_hom.map_mul, eval₂_X] }
end,
right_inv := begin
rintro ⟨f⟩,
apply induction_on f,
{ intros r,
simp only [submodule.quotient.quot_mk_eq_mk, ideal.quotient.lift_mk, ideal.quotient.mk_eq_mk,
ring_hom.coe_comp, eval₂_hom_C] },
{ simp_intros p q hp hq only [eval₂_hom_eq_bind₂, submodule.quotient.quot_mk_eq_mk, eval₂_add,
ring_hom.map_add, coe_eval₂_hom, ideal.quotient.lift_mk, ideal.quotient.mk_eq_mk],
rw [hp, hq] },
{ simp_intros p i hp only [eval₂_hom_eq_bind₂, submodule.quotient.quot_mk_eq_mk, coe_eval₂_hom,
ideal.quotient.lift_mk, ideal.quotient.mk_eq_mk, bind₂_X_right, eval₂_mul, ring_hom.map_mul,
eval₂_X],
simp only [hp] }
end,
commutes' := λ r, eval₂_hom_C _ _ (ideal.quotient.mk I r) }
end mv_polynomial
section unique_factorization_domain
variables {D : Type u} [comm_ring D] [is_domain D] [unique_factorization_monoid D] (σ)
open unique_factorization_monoid
namespace polynomial
@[priority 100]
instance unique_factorization_monoid : unique_factorization_monoid D[X] :=
begin
haveI := arbitrary (normalization_monoid D),
haveI := to_normalized_gcd_monoid D,
exact ufm_of_gcd_of_wf_dvd_monoid
end
end polynomial
namespace mv_polynomial
private lemma unique_factorization_monoid_of_fintype [fintype σ] :
unique_factorization_monoid (mv_polynomial σ D) :=
(rename_equiv D (fintype.equiv_fin σ)).to_mul_equiv.symm.unique_factorization_monoid $
begin
induction fintype.card σ with d hd,
{ apply (is_empty_alg_equiv D (fin 0)).to_mul_equiv.symm.unique_factorization_monoid,
apply_instance },
{ apply (fin_succ_equiv D d).to_mul_equiv.symm.unique_factorization_monoid,
exactI polynomial.unique_factorization_monoid },
end
@[priority 100]
instance : unique_factorization_monoid (mv_polynomial σ D) :=
begin
rw iff_exists_prime_factors,
intros a ha, obtain ⟨s,a',rfl⟩ := exists_finset_rename a,
obtain ⟨w,h,u,hw⟩ := iff_exists_prime_factors.1
(unique_factorization_monoid_of_fintype s) a' (λ h, ha $ by simp [h]),
exact ⟨ w.map (rename coe),
λ b hb, let ⟨b',hb',he⟩ := multiset.mem_map.1 hb in he ▸ (prime_rename_iff ↑s).2 (h b' hb'),
units.map (@rename s σ D _ coe).to_ring_hom.to_monoid_hom u,
by erw [multiset.prod_hom, ← map_mul, hw] ⟩,
end
end mv_polynomial
end unique_factorization_domain
|
18c5093b29a6a5ba12f3c2be657fd53adadd4406 | 22e97a5d648fc451e25a06c668dc03ac7ed7bc25 | /src/ring_theory/multiplicity.lean | 9a3de78c8ca89cef2cfdfd7bd2db260fcf7afe55 | [
"Apache-2.0"
] | permissive | keeferrowan/mathlib | f2818da875dbc7780830d09bd4c526b0764a4e50 | aad2dfc40e8e6a7e258287a7c1580318e865817e | refs/heads/master | 1,661,736,426,952 | 1,590,438,032,000 | 1,590,438,032,000 | 266,892,663 | 0 | 0 | Apache-2.0 | 1,590,445,835,000 | 1,590,445,835,000 | null | UTF-8 | Lean | false | false | 17,355 | lean | /-
Copyright (c) 2018 Robert Y. Lewis. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Robert Y. Lewis, Chris Hughes
-/
import algebra.associated
import data.int.gcd
import algebra.big_operators
variables {α : Type*}
open nat roption
theorem nat.find_le {p q : ℕ → Prop} [decidable_pred p] [decidable_pred q]
(h : ∀ n, q n → p n) (hp : ∃ n, p n) (hq : ∃ n, q n) :
nat.find hp ≤ nat.find hq :=
nat.find_min' _ ((h _) (nat.find_spec hq))
/-- `multiplicity a b` returns the largest natural number `n` such that
`a ^ n ∣ b`, as an `enat` or natural with infinity. If `∀ n, a ^ n ∣ b`,
then it returns `⊤`-/
def multiplicity [comm_semiring α] [decidable_rel ((∣) : α → α → Prop)] (a b : α) : enat :=
⟨∃ n : ℕ, ¬a ^ (n + 1) ∣ b, λ h, nat.find h⟩
namespace multiplicity
section comm_semiring
variables [comm_semiring α]
@[reducible] def finite (a b : α) : Prop := ∃ n : ℕ, ¬a ^ (n + 1) ∣ b
lemma finite_iff_dom [decidable_rel ((∣) : α → α → Prop)] {a b : α} :
finite a b ↔ (multiplicity a b).dom := iff.rfl
lemma finite_def {a b : α} : finite a b ↔ ∃ n : ℕ, ¬a ^ (n + 1) ∣ b := iff.rfl
@[norm_cast]
theorem int.coe_nat_multiplicity (a b : ℕ) :
multiplicity (a : ℤ) (b : ℤ) = multiplicity a b :=
begin
apply roption.ext',
{ repeat {rw [← finite_iff_dom, finite_def]},
norm_cast, simp },
{ intros h1 h2,
apply _root_.le_antisymm; { apply nat.find_le, norm_cast, simp }}
end
lemma not_finite_iff_forall {a b : α} : (¬ finite a b) ↔ ∀ n : ℕ, a ^ n ∣ b :=
⟨λ h n, nat.cases_on n (one_dvd _) (by simpa [finite, classical.not_not] using h),
by simp [finite, multiplicity, classical.not_not]; tauto⟩
lemma not_unit_of_finite {a b : α} (h : finite a b) : ¬is_unit a :=
let ⟨n, hn⟩ := h in mt (is_unit_iff_forall_dvd.1 ∘ is_unit_pow (n + 1)) $
λ h, hn (h b)
lemma ne_zero_of_finite {a b : α} (h : finite a b) : b ≠ 0 :=
let ⟨n, hn⟩ := h in λ hb, by simpa [hb] using hn
lemma finite_of_finite_mul_left {a b c : α} : finite a (b * c) → finite a c :=
λ ⟨n, hn⟩, ⟨n, λ h, hn (dvd.trans h (by simp [_root_.mul_pow]))⟩
lemma finite_of_finite_mul_right {a b c : α} : finite a (b * c) → finite a b :=
by rw mul_comm; exact finite_of_finite_mul_left
variable [decidable_rel ((∣) : α → α → Prop)]
lemma pow_dvd_of_le_multiplicity {a b : α} {k : ℕ} : (k : enat) ≤ multiplicity a b → a ^ k ∣ b :=
nat.cases_on k (λ _, one_dvd _)
(λ k ⟨h₁, h₂⟩, by_contradiction (λ hk, (nat.find_min _ (lt_of_succ_le (h₂ ⟨k, hk⟩)) hk)))
lemma pow_multiplicity_dvd {a b : α} (h : finite a b) : a ^ get (multiplicity a b) h ∣ b :=
pow_dvd_of_le_multiplicity (by rw enat.coe_get)
lemma is_greatest {a b : α} {m : ℕ} (hm : multiplicity a b < m) : ¬a ^ m ∣ b :=
λ h, have finite a b, from enat.dom_of_le_some (le_of_lt hm),
by rw [← enat.coe_get (finite_iff_dom.1 this), enat.coe_lt_coe] at hm;
exact nat.find_spec this (dvd.trans (pow_dvd_pow _ hm) h)
lemma is_greatest' {a b : α} {m : ℕ} (h : finite a b) (hm : get (multiplicity a b) h < m) :
¬a ^ m ∣ b :=
is_greatest (by rwa [← enat.coe_lt_coe, enat.coe_get] at hm)
lemma unique {a b : α} {k : ℕ} (hk : a ^ k ∣ b) (hsucc : ¬a ^ (k + 1) ∣ b) :
(k : enat) = multiplicity a b :=
le_antisymm (le_of_not_gt (λ hk', is_greatest hk' hk)) $
have finite a b, from ⟨k, hsucc⟩,
by rw [← enat.coe_get (finite_iff_dom.1 this), enat.coe_le_coe];
exact nat.find_min' _ hsucc
lemma unique' {a b : α} {k : ℕ} (hk : a ^ k ∣ b) (hsucc : ¬ a ^ (k + 1) ∣ b) :
k = get (multiplicity a b) ⟨k, hsucc⟩ :=
by rw [← enat.coe_inj, enat.coe_get, unique hk hsucc]
lemma le_multiplicity_of_pow_dvd {a b : α}
{k : ℕ} (hk : a ^ k ∣ b) : (k : enat) ≤ multiplicity a b :=
le_of_not_gt $ λ hk', is_greatest hk' hk
lemma pow_dvd_iff_le_multiplicity {a b : α}
{k : ℕ} : a ^ k ∣ b ↔ (k : enat) ≤ multiplicity a b :=
⟨le_multiplicity_of_pow_dvd, pow_dvd_of_le_multiplicity⟩
lemma multiplicity_lt_iff_neg_dvd {a b : α} {k : ℕ} :
multiplicity a b < (k : enat) ↔ ¬ a ^ k ∣ b :=
by { rw [pow_dvd_iff_le_multiplicity, not_le] }
lemma eq_some_iff {a b : α} {n : ℕ} :
multiplicity a b = (n : enat) ↔ a ^ n ∣ b ∧ ¬a ^ (n + 1) ∣ b :=
⟨λ h, let ⟨h₁, h₂⟩ := eq_some_iff.1 h in
h₂ ▸ ⟨pow_multiplicity_dvd _, is_greatest
(by conv_lhs {rw ← enat.coe_get h₁ }; rw [enat.coe_lt_coe]; exact lt_succ_self _)⟩,
λ h, eq_some_iff.2 ⟨⟨n, h.2⟩, eq.symm $ unique' h.1 h.2⟩⟩
lemma eq_top_iff {a b : α} :
multiplicity a b = ⊤ ↔ ∀ n : ℕ, a ^ n ∣ b :=
⟨λ h n, nat.cases_on n (one_dvd _)
(λ n, by_contradiction (not_exists.1 (eq_none_iff'.1 h) n : _)),
λ h, eq_none_iff.2 (λ n ⟨⟨_, h₁⟩, _⟩, h₁ (h _))⟩
@[simp] protected lemma zero (a : α) : multiplicity a 0 = ⊤ :=
roption.eq_none_iff.2 (λ n ⟨⟨k, hk⟩, _⟩, hk (dvd_zero _))
lemma one_right {a : α} (ha : ¬is_unit a) : multiplicity a 1 = 0 :=
eq_some_iff.2 ⟨dvd_refl _, mt is_unit_iff_dvd_one.2 $ by simpa⟩
@[simp] lemma get_one_right {a : α} (ha : finite a 1) : get (multiplicity a 1) ha = 0 :=
get_eq_iff_eq_some.2 (eq_some_iff.2 ⟨dvd_refl _,
by simpa [is_unit_iff_dvd_one.symm] using not_unit_of_finite ha⟩)
@[simp] lemma multiplicity_unit {a : α} (b : α) (ha : is_unit a) : multiplicity a b = ⊤ :=
eq_top_iff.2 (λ _, is_unit_iff_forall_dvd.1 (is_unit_pow _ ha) _)
@[simp] lemma one_left (b : α) : multiplicity 1 b = ⊤ := by simp [eq_top_iff]
lemma multiplicity_eq_zero_of_not_dvd {a b : α} (ha : ¬a ∣ b) : multiplicity a b = 0 :=
eq_some_iff.2 (by simpa)
lemma eq_top_iff_not_finite {a b : α} : multiplicity a b = ⊤ ↔ ¬ finite a b :=
roption.eq_none_iff'
open_locale classical
lemma multiplicity_le_multiplicity_iff {a b c d : α} : multiplicity a b ≤ multiplicity c d ↔
(∀ n : ℕ, a ^ n ∣ b → c ^ n ∣ d) :=
⟨λ h n hab, (pow_dvd_of_le_multiplicity (le_trans (le_multiplicity_of_pow_dvd hab) h)),
λ h, if hab : finite a b
then by rw [← enat.coe_get (finite_iff_dom.1 hab)]; exact le_multiplicity_of_pow_dvd (h _ (pow_multiplicity_dvd _))
else
have ∀ n : ℕ, c ^ n ∣ d, from λ n, h n (not_finite_iff_forall.1 hab _),
by rw [eq_top_iff_not_finite.2 hab, eq_top_iff_not_finite.2
(not_finite_iff_forall.2 this)]⟩
lemma min_le_multiplicity_add {p a b : α} :
min (multiplicity p a) (multiplicity p b) ≤ multiplicity p (a + b) :=
(le_total (multiplicity p a) (multiplicity p b)).elim
(λ h, by rw [min_eq_left h, multiplicity_le_multiplicity_iff];
exact λ n hn, dvd_add hn (multiplicity_le_multiplicity_iff.1 h n hn))
(λ h, by rw [min_eq_right h, multiplicity_le_multiplicity_iff];
exact λ n hn, dvd_add (multiplicity_le_multiplicity_iff.1 h n hn) hn)
lemma dvd_of_multiplicity_pos {a b : α} (h : (0 : enat) < multiplicity a b) : a ∣ b :=
by rw [← _root_.pow_one a]; exact pow_dvd_of_le_multiplicity (enat.pos_iff_one_le.1 h)
lemma finite_nat_iff {a b : ℕ} : finite a b ↔ (a ≠ 1 ∧ 0 < b) :=
begin
rw [← not_iff_not, not_finite_iff_forall, not_and_distrib, ne.def,
not_not, not_lt, nat.le_zero_iff],
exact ⟨λ h, or_iff_not_imp_right.2 (λ hb,
have ha : a ≠ 0, from λ ha, by simpa [ha] using h 1,
by_contradiction (λ ha1 : a ≠ 1,
have ha_gt_one : 1 < a, from
have ∀ a : ℕ, a ≤ 1 → a ≠ 0 → a ≠ 1 → false, from dec_trivial,
lt_of_not_ge (λ ha', this a ha' ha ha1),
not_lt_of_ge (le_of_dvd (nat.pos_of_ne_zero hb) (h b))
(by simp only [nat.pow_eq_pow]; exact lt_pow_self ha_gt_one b))),
λ h, by cases h; simp *⟩
end
lemma finite_int_iff_nat_abs_finite {a b : ℤ} : finite a b ↔ finite a.nat_abs b.nat_abs :=
begin
rw [finite_def, finite_def],
conv in (a ^ _ ∣ b)
{ rw [← int.nat_abs_dvd_abs_iff, int.nat_abs_pow, ← pow_eq_pow] }
end
lemma finite_int_iff {a b : ℤ} : finite a b ↔ (a.nat_abs ≠ 1 ∧ b ≠ 0) :=
begin
have := int.nat_abs_eq a,
have := @int.nat_abs_ne_zero_of_ne_zero b,
rw [finite_int_iff_nat_abs_finite, finite_nat_iff, nat.pos_iff_ne_zero],
split; finish
end
instance decidable_nat : decidable_rel (λ a b : ℕ, (multiplicity a b).dom) :=
λ a b, decidable_of_iff _ finite_nat_iff.symm
instance decidable_int : decidable_rel (λ a b : ℤ, (multiplicity a b).dom) :=
λ a b, decidable_of_iff _ finite_int_iff.symm
end comm_semiring
section comm_ring
variables [comm_ring α] [decidable_rel ((∣) : α → α → Prop)]
open_locale classical
@[simp] protected lemma neg (a b : α) : multiplicity a (-b) = multiplicity a b :=
roption.ext' (by simp only [multiplicity]; conv in (_ ∣ - _) {rw dvd_neg})
(λ h₁ h₂, enat.coe_inj.1 (by rw [enat.coe_get]; exact
eq.symm (unique ((dvd_neg _ _).2 (pow_multiplicity_dvd _))
(mt (dvd_neg _ _).1 (is_greatest' _ (lt_succ_self _))))))
lemma multiplicity_add_of_gt {p a b : α} (h : multiplicity p b < multiplicity p a) :
multiplicity p (a + b) = multiplicity p b :=
begin
apply le_antisymm,
{ apply enat.le_of_lt_add_one,
cases enat.ne_top_iff.mp (enat.ne_top_of_lt h) with k hk,
rw [hk], rw_mod_cast [multiplicity_lt_iff_neg_dvd], intro h_dvd,
rw [← dvd_add_iff_right] at h_dvd,
apply multiplicity.is_greatest _ h_dvd, rw [hk], apply_mod_cast nat.lt_succ_self,
rw [pow_dvd_iff_le_multiplicity, enat.coe_add, ← hk], exact enat.add_one_le_of_lt h },
{ convert min_le_multiplicity_add, rw [min_eq_right (le_of_lt h)] }
end
lemma multiplicity_sub_of_gt {p a b : α} (h : multiplicity p b < multiplicity p a) :
multiplicity p (a - b) = multiplicity p b :=
by { rw [sub_eq_add_neg, multiplicity_add_of_gt]; rwa [multiplicity.neg] }
lemma multiplicity_add_eq_min {p a b : α} (h : multiplicity p a ≠ multiplicity p b) :
multiplicity p (a + b) = min (multiplicity p a) (multiplicity p b) :=
begin
rcases lt_trichotomy (multiplicity p a) (multiplicity p b) with hab|hab|hab,
{ rw [add_comm, multiplicity_add_of_gt hab, min_eq_left], exact le_of_lt hab },
{ contradiction },
{ rw [multiplicity_add_of_gt hab, min_eq_right], exact le_of_lt hab},
end
end comm_ring
section integral_domain
variables [integral_domain α]
lemma finite_mul_aux {p : α} (hp : prime p) : ∀ {n m : ℕ} {a b : α},
¬p ^ (n + 1) ∣ a → ¬p ^ (m + 1) ∣ b → ¬p ^ (n + m + 1) ∣ a * b
| n m := λ a b ha hb ⟨s, hs⟩,
have p ∣ a * b, from ⟨p ^ (n + m) * s,
by simp [hs, _root_.pow_add, mul_comm, mul_assoc, mul_left_comm]⟩,
(hp.2.2 a b this).elim
(λ ⟨x, hx⟩, have hn0 : 0 < n,
from nat.pos_of_ne_zero (λ hn0, by clear _fun_match _fun_match; simpa [hx, hn0] using ha),
have wf : (n - 1) < n, from nat.sub_lt_self hn0 dec_trivial,
have hpx : ¬ p ^ (n - 1 + 1) ∣ x,
from λ ⟨y, hy⟩, ha (hx.symm ▸ ⟨y, (domain.mul_left_inj hp.1).1
$ by rw [nat.sub_add_cancel hn0] at hy;
simp [hy, _root_.pow_add, mul_comm, mul_assoc, mul_left_comm]⟩),
have 1 ≤ n + m, from le_trans hn0 (le_add_right n m),
finite_mul_aux hpx hb ⟨s, (domain.mul_left_inj hp.1).1 begin
rw [← nat.sub_add_comm hn0, nat.sub_add_cancel this],
clear _fun_match _fun_match finite_mul_aux,
simp [*, mul_comm, mul_assoc, mul_left_comm, _root_.pow_add] at *
end⟩)
(λ ⟨x, hx⟩, have hm0 : 0 < m,
from nat.pos_of_ne_zero (λ hm0, by clear _fun_match _fun_match; simpa [hx, hm0] using hb),
have wf : (m - 1) < m, from nat.sub_lt_self hm0 dec_trivial,
have hpx : ¬ p ^ (m - 1 + 1) ∣ x,
from λ ⟨y, hy⟩, hb (hx.symm ▸ ⟨y, (domain.mul_left_inj hp.1).1
$ by rw [nat.sub_add_cancel hm0] at hy;
simp [hy, _root_.pow_add, mul_comm, mul_assoc, mul_left_comm]⟩),
finite_mul_aux ha hpx ⟨s, (domain.mul_left_inj hp.1).1 begin
rw [add_assoc, nat.sub_add_cancel hm0],
clear _fun_match _fun_match finite_mul_aux,
simp [*, mul_comm, mul_assoc, mul_left_comm, _root_.pow_add] at *
end⟩)
lemma finite_mul {p a b : α} (hp : prime p) : finite p a → finite p b → finite p (a * b) :=
λ ⟨n, hn⟩ ⟨m, hm⟩, ⟨n + m, finite_mul_aux hp hn hm⟩
lemma finite_mul_iff {p a b : α} (hp : prime p) : finite p (a * b) ↔ finite p a ∧ finite p b :=
⟨λ h, ⟨finite_of_finite_mul_right h, finite_of_finite_mul_left h⟩,
λ h, finite_mul hp h.1 h.2⟩
lemma finite_pow {p a : α} (hp : prime p) : Π {k : ℕ} (ha : finite p a), finite p (a ^ k)
| 0 ha := ⟨0, by simp [mt is_unit_iff_dvd_one.2 hp.2.1]⟩
| (k+1) ha := by rw [_root_.pow_succ]; exact finite_mul hp ha (finite_pow ha)
variable [decidable_rel ((∣) : α → α → Prop)]
@[simp] lemma multiplicity_self {a : α} (ha : ¬is_unit a) (ha0 : a ≠ 0) :
multiplicity a a = 1 :=
eq_some_iff.2 ⟨by simp, λ ⟨b, hb⟩, ha (is_unit_iff_dvd_one.2
⟨b, (domain.mul_right_inj ha0).1 $ by clear _fun_match;
simpa [_root_.pow_succ, mul_assoc] using hb⟩)⟩
@[simp] lemma get_multiplicity_self {a : α} (ha : finite a a) :
get (multiplicity a a) ha = 1 :=
roption.get_eq_iff_eq_some.2 (eq_some_iff.2
⟨by simp, λ ⟨b, hb⟩,
by rw [← mul_one a, _root_.pow_add, _root_.pow_one, mul_assoc, mul_assoc,
domain.mul_right_inj (ne_zero_of_finite ha)] at hb;
exact mt is_unit_iff_dvd_one.2 (not_unit_of_finite ha)
⟨b, by clear _fun_match; simp * at *⟩⟩)
protected lemma mul' {p a b : α} (hp : prime p)
(h : (multiplicity p (a * b)).dom) :
get (multiplicity p (a * b)) h =
get (multiplicity p a) ((finite_mul_iff hp).1 h).1 +
get (multiplicity p b) ((finite_mul_iff hp).1 h).2 :=
have hdiva : p ^ get (multiplicity p a) ((finite_mul_iff hp).1 h).1 ∣ a, from pow_multiplicity_dvd _,
have hdivb : p ^ get (multiplicity p b) ((finite_mul_iff hp).1 h).2 ∣ b, from pow_multiplicity_dvd _,
have hpoweq : p ^ (get (multiplicity p a) ((finite_mul_iff hp).1 h).1 +
get (multiplicity p b) ((finite_mul_iff hp).1 h).2) =
p ^ get (multiplicity p a) ((finite_mul_iff hp).1 h).1 *
p ^ get (multiplicity p b) ((finite_mul_iff hp).1 h).2,
by simp [_root_.pow_add],
have hdiv : p ^ (get (multiplicity p a) ((finite_mul_iff hp).1 h).1 +
get (multiplicity p b) ((finite_mul_iff hp).1 h).2) ∣ a * b,
by rw [hpoweq]; apply mul_dvd_mul; assumption,
have hsucc : ¬p ^ ((get (multiplicity p a) ((finite_mul_iff hp).1 h).1 +
get (multiplicity p b) ((finite_mul_iff hp).1 h).2) + 1) ∣ a * b,
from λ h, not_or (is_greatest' _ (lt_succ_self _)) (is_greatest' _ (lt_succ_self _))
(succ_dvd_or_succ_dvd_of_succ_sum_dvd_mul hp (by convert hdiva)
(by convert hdivb) h),
by rw [← enat.coe_inj, enat.coe_get, eq_some_iff];
exact ⟨hdiv, hsucc⟩
open_locale classical
protected lemma mul {p a b : α} (hp : prime p) :
multiplicity p (a * b) = multiplicity p a + multiplicity p b :=
if h : finite p a ∧ finite p b then
by rw [← enat.coe_get (finite_iff_dom.1 h.1), ← enat.coe_get (finite_iff_dom.1 h.2),
← enat.coe_get (finite_iff_dom.1 (finite_mul hp h.1 h.2)),
← enat.coe_add, enat.coe_inj, multiplicity.mul' hp]; refl
else begin
rw [eq_top_iff_not_finite.2 (mt (finite_mul_iff hp).1 h)],
cases not_and_distrib.1 h with h h;
simp [eq_top_iff_not_finite.2 h]
end
lemma finset.prod {β : Type*} {p : α} (hp : prime p) (s : finset β) (f : β → α) :
multiplicity p (s.prod f) = s.sum (λ x, multiplicity p (f x)) :=
begin
classical,
induction s using finset.induction with a s has ih h,
{ simp only [finset.sum_empty, finset.prod_empty],
convert one_right hp.not_unit },
{ simp [has, ← ih],
convert multiplicity.mul hp }
end
protected lemma pow' {p a : α} (hp : prime p) (ha : finite p a) : ∀ {k : ℕ},
get (multiplicity p (a ^ k)) (finite_pow hp ha) = k * get (multiplicity p a) ha
| 0 := by dsimp [_root_.pow_zero]; simp [one_right hp.not_unit]; refl
| (k+1) := by dsimp only [_root_.pow_succ];
erw [multiplicity.mul' hp, pow', add_mul, one_mul, add_comm]
lemma pow {p a : α} (hp : prime p) : ∀ {k : ℕ},
multiplicity p (a ^ k) = add_monoid.smul k (multiplicity p a)
| 0 := by simp [one_right hp.not_unit]
| (succ k) := by simp [_root_.pow_succ, succ_smul, pow, multiplicity.mul hp]
lemma multiplicity_pow_self {p : α} (h0 : p ≠ 0) (hu : ¬ is_unit p) (n : ℕ) :
multiplicity p (p ^ n) = n :=
by { rw [eq_some_iff], use dvd_refl _, rw [pow_dvd_pow_iff h0 hu], apply nat.not_succ_le_self }
lemma multiplicity_pow_self_of_prime {p : α} (hp : prime p) (n : ℕ) :
multiplicity p (p ^ n) = n :=
multiplicity_pow_self hp.ne_zero hp.not_unit n
end integral_domain
end multiplicity
section nat
open multiplicity
lemma multiplicity_eq_zero_of_coprime {p a b : ℕ} (hp : p ≠ 1)
(hle : multiplicity p a ≤ multiplicity p b)
(hab : nat.coprime a b) : multiplicity p a = 0 :=
begin
rw [multiplicity_le_multiplicity_iff] at hle,
rw [← le_zero_iff_eq, ← not_lt, enat.pos_iff_one_le, ← enat.coe_one,
← pow_dvd_iff_le_multiplicity],
assume h,
have := nat.dvd_gcd h (hle _ h),
rw [coprime.gcd_eq_one hab, nat.dvd_one, _root_.pow_one] at this,
exact hp this
end
end nat
|
2a56630f35a7abb8f9f004bb045ac1a59479272f | 3dd1b66af77106badae6edb1c4dea91a146ead30 | /tests/lean/t8.lean | 5b962db504cb559f4b476285666c1c3846296416 | [
"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 | 70 | lean | #setline 1000
prit "ok"
#setline 33
fo
print "ok"
check
print "done" |
feb602c9777f3371188ff84ff531ad041cbc7132 | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/algebraic_geometry/morphisms/basic.lean | 31d5de9befe6623e42b9fd0d2669e1c8e424fb65 | [
"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 | 20,776 | lean | /-
Copyright (c) 2022 Andrew Yang. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Andrew Yang
-/
import algebraic_geometry.AffineScheme
import algebraic_geometry.pullbacks
import category_theory.morphism_property
/-!
# Properties of morphisms between Schemes
We provide the basic framework for talking about properties of morphisms between Schemes.
A `morphism_property Scheme` is a predicate on morphisms between schemes, and an
`affine_target_morphism_property` is a predicate on morphisms into affine schemes. Given a
`P : affine_target_morphism_property`, we may construct a `morphism_property` called
`target_affine_locally P` that holds for `f : X ⟶ Y` whenever `P` holds for the
restriction of `f` on every affine open subset of `Y`.
## Main definitions
- `algebraic_geometry.affine_target_morphism_property.is_local`: We say that `P.is_local` if `P`
satisfies the assumptions of the affine communication lemma
(`algebraic_geometry.of_affine_open_cover`). That is,
1. `P` respects isomorphisms.
2. If `P` holds for `f : X ⟶ Y`, then `P` holds for `f ∣_ Y.basic_open r` for any
global section `r`.
3. If `P` holds for `f ∣_ Y.basic_open r` for all `r` in a spanning set of the global sections,
then `P` holds for `f`.
- `algebraic_geometry.property_is_local_at_target`: We say that `property_is_local_at_target P` for
`P : morphism_property Scheme` if
1. `P` respects isomorphisms.
2. If `P` holds for `f : X ⟶ Y`, then `P` holds for `f ∣_ U` for any `U`.
3. If `P` holds for `f ∣_ U` for an open cover `U` of `Y`, then `P` holds for `f`.
## Main results
- `algebraic_geometry.affine_target_morphism_property.is_local.affine_open_cover_tfae`:
If `P.is_local`, then `target_affine_locally P f` iff there exists an affine cover `{ Uᵢ }` of `Y`
such that `P` holds for `f ∣_ Uᵢ`.
- `algebraic_geometry.affine_target_morphism_property.is_local_of_open_cover_imply`:
If the existance of an affine cover `{ Uᵢ }` of `Y` such that `P` holds for `f ∣_ Uᵢ` implies
`target_affine_locally P f`, then `P.is_local`.
- `algebraic_geometry.affine_target_morphism_property.is_local.affine_target_iff`:
If `Y` is affine and `f : X ⟶ Y`, then `target_affine_locally P f ↔ P f` provided `P.is_local`.
- `algebraic_geometry.affine_target_morphism_property.is_local.target_affine_locally_is_local` :
If `P.is_local`, then `property_is_local_at_target (target_affine_locally P)`.
- `algebraic_geometry.property_is_local_at_target.open_cover_tfae`:
If `property_is_local_at_target P`, then `P f` iff there exists an open cover `{ Uᵢ }` of `Y`
such that `P` holds for `f ∣_ Uᵢ`.
These results should not be used directly, and should be ported to each property that is local.
-/
universe u
open topological_space category_theory category_theory.limits opposite
noncomputable theory
namespace algebraic_geometry
/-- An `affine_target_morphism_property` is a class of morphisms from an arbitrary scheme into an
affine scheme. -/
def affine_target_morphism_property := ∀ ⦃X Y : Scheme⦄ (f : X ⟶ Y) [is_affine Y], Prop
/-- `is_iso` as a `morphism_property`. -/
protected def Scheme.is_iso : morphism_property Scheme := @is_iso Scheme _
/-- `is_iso` as an `affine_morphism_property`. -/
protected def Scheme.affine_target_is_iso : affine_target_morphism_property :=
λ X Y f H, is_iso f
instance : inhabited affine_target_morphism_property := ⟨Scheme.affine_target_is_iso⟩
/-- A `affine_target_morphism_property` can be extended to a `morphism_property` such that it
*never* holds when the target is not affine -/
def affine_target_morphism_property.to_property (P : affine_target_morphism_property) :
morphism_property Scheme :=
λ X Y f, ∃ h, @@P f h
lemma affine_target_morphism_property.to_property_apply (P : affine_target_morphism_property)
{X Y : Scheme} (f : X ⟶ Y) [is_affine Y] :
P.to_property f ↔ P f := by { delta affine_target_morphism_property.to_property, simp [*] }
lemma affine_cancel_left_is_iso {P : affine_target_morphism_property}
(hP : P.to_property.respects_iso) {X Y Z : Scheme} (f : X ⟶ Y)
(g : Y ⟶ Z) [is_iso f] [is_affine Z] : P (f ≫ g) ↔ P g :=
by rw [← P.to_property_apply, ← P.to_property_apply, hP.cancel_left_is_iso]
lemma affine_cancel_right_is_iso
{P : affine_target_morphism_property} (hP : P.to_property.respects_iso) {X Y Z : Scheme}
(f : X ⟶ Y) (g : Y ⟶ Z) [is_iso g] [is_affine Z] [is_affine Y] : P (f ≫ g) ↔ P f :=
by rw [← P.to_property_apply, ← P.to_property_apply, hP.cancel_right_is_iso]
lemma affine_target_morphism_property.respects_iso_mk {P : affine_target_morphism_property}
(h₁ : ∀ {X Y Z} (e : X ≅ Y) (f : Y ⟶ Z) [is_affine Z], by exactI P f → P (e.hom ≫ f))
(h₂ : ∀ {X Y Z} (e : Y ≅ Z) (f : X ⟶ Y) [h : is_affine Y],
by exactI P f → @@P (f ≫ e.hom) (is_affine_of_iso e.inv)) : P.to_property.respects_iso :=
begin
split,
{ rintros X Y Z e f ⟨a, h⟩, exactI ⟨a, h₁ e f h⟩ },
{ rintros X Y Z e f ⟨a, h⟩, exactI ⟨is_affine_of_iso e.inv, h₂ e f h⟩ },
end
/-- For a `P : affine_target_morphism_property`, `target_affine_locally P` holds for
`f : X ⟶ Y` whenever `P` holds for the restriction of `f` on every affine open subset of `Y`. -/
def target_affine_locally (P : affine_target_morphism_property) : morphism_property Scheme :=
λ {X Y : Scheme} (f : X ⟶ Y), ∀ (U : Y.affine_opens), @@P (f ∣_ U) U.prop
lemma is_affine_open.map_is_iso {X Y : Scheme} {U : opens Y.carrier} (hU : is_affine_open U)
(f : X ⟶ Y) [is_iso f] : is_affine_open ((opens.map f.1.base).obj U) :=
begin
haveI : is_affine _ := hU,
exact is_affine_of_iso (f ∣_ U),
end
lemma target_affine_locally_respects_iso {P : affine_target_morphism_property}
(hP : P.to_property.respects_iso) : (target_affine_locally P).respects_iso :=
begin
split,
{ introv H U,
rw [morphism_restrict_comp, affine_cancel_left_is_iso hP],
exact H U },
{ introv H,
rintro ⟨U, hU : is_affine_open U⟩, dsimp,
haveI : is_affine _ := hU,
haveI : is_affine _ := hU.map_is_iso e.hom,
rw [morphism_restrict_comp, affine_cancel_right_is_iso hP],
exact H ⟨(opens.map e.hom.val.base).obj U, hU.map_is_iso e.hom⟩ }
end
/--
We say that `P : affine_target_morphism_property` is a local property if
1. `P` respects isomorphisms.
2. If `P` holds for `f : X ⟶ Y`, then `P` holds for `f ∣_ Y.basic_open r` for any
global section `r`.
3. If `P` holds for `f ∣_ Y.basic_open r` for all `r` in a spanning set of the global sections,
then `P` holds for `f`.
-/
structure affine_target_morphism_property.is_local (P : affine_target_morphism_property) : Prop :=
(respects_iso : P.to_property.respects_iso)
(to_basic_open : ∀ {X Y : Scheme} [is_affine Y] (f : X ⟶ Y) (r : Y.presheaf.obj $ op ⊤),
by exactI P f →
@@P (f ∣_ (Y.basic_open r)) ((top_is_affine_open Y).basic_open_is_affine _))
(of_basic_open_cover : ∀ {X Y : Scheme} [is_affine Y] (f : X ⟶ Y)
(s : finset (Y.presheaf.obj $ op ⊤)) (hs : ideal.span (s : set (Y.presheaf.obj $ op ⊤)) = ⊤),
by exactI (∀ (r : s), @@P (f ∣_ (Y.basic_open r.1))
((top_is_affine_open Y).basic_open_is_affine _)) → P f)
lemma target_affine_locally_of_open_cover {P : affine_target_morphism_property}
(hP : P.is_local)
{X Y : Scheme} (f : X ⟶ Y) (𝒰 : Y.open_cover) [∀ i, is_affine (𝒰.obj i)]
(h𝒰 : ∀ i, P (pullback.snd : (𝒰.pullback_cover f).obj i ⟶ 𝒰.obj i)) :
target_affine_locally P f :=
begin
classical,
let S := λ i, (⟨⟨set.range (𝒰.map i).1.base, (𝒰.is_open i).base_open.open_range⟩,
range_is_affine_open_of_open_immersion (𝒰.map i)⟩ : Y.affine_opens),
intro U,
apply of_affine_open_cover U (set.range S),
{ intros U r h,
haveI : is_affine _ := U.2,
have := hP.2 (f ∣_ U.1),
replace this := this (Y.presheaf.map (eq_to_hom U.1.open_embedding_obj_top).op r) h,
rw ← P.to_property_apply at this ⊢,
exact (hP.1.arrow_mk_iso_iff (morphism_restrict_restrict_basic_open f _ r)).mp this },
{ intros U s hs H,
haveI : is_affine _ := U.2,
apply hP.3 (f ∣_ U.1) (s.image (Y.presheaf.map (eq_to_hom U.1.open_embedding_obj_top).op)),
{ apply_fun ideal.comap (Y.presheaf.map (eq_to_hom U.1.open_embedding_obj_top.symm).op) at hs,
rw ideal.comap_top at hs,
rw ← hs,
simp only [eq_to_hom_op, eq_to_hom_map, finset.coe_image],
have : ∀ {R S : CommRing} (e : S = R) (s : set S),
(by exactI ideal.span (eq_to_hom e '' s) = ideal.comap (eq_to_hom e.symm) (ideal.span s)),
{ intros, subst e, simpa },
apply this },
{ rintro ⟨r, hr⟩,
obtain ⟨r, hr', rfl⟩ := finset.mem_image.mp hr,
simp_rw ← P.to_property_apply at ⊢ H,
exact
(hP.1.arrow_mk_iso_iff (morphism_restrict_restrict_basic_open f _ r)).mpr (H ⟨r, hr'⟩) } },
{ rw set.eq_univ_iff_forall,
simp only [set.mem_Union],
intro x,
exact ⟨⟨_, ⟨𝒰.f x, rfl⟩⟩, 𝒰.covers x⟩ },
{ rintro ⟨_, i, rfl⟩,
simp_rw ← P.to_property_apply at ⊢ h𝒰,
exact (hP.1.arrow_mk_iso_iff (morphism_restrict_opens_range f _)).mpr (h𝒰 i) },
end
lemma affine_target_morphism_property.is_local.affine_open_cover_tfae
{P : affine_target_morphism_property}
(hP : P.is_local) {X Y : Scheme.{u}} (f : X ⟶ Y) :
tfae [target_affine_locally P f,
∃ (𝒰 : Scheme.open_cover.{u} Y) [∀ i, is_affine (𝒰.obj i)], ∀ (i : 𝒰.J),
by exactI P (pullback.snd : (𝒰.pullback_cover f).obj i ⟶ 𝒰.obj i),
∀ (𝒰 : Scheme.open_cover.{u} Y) [∀ i, is_affine (𝒰.obj i)] (i : 𝒰.J),
by exactI P (pullback.snd : (𝒰.pullback_cover f).obj i ⟶ 𝒰.obj i),
∀ {U : Scheme} (g : U ⟶ Y) [is_affine U] [is_open_immersion g],
by exactI P (pullback.snd : pullback f g ⟶ U),
∃ {ι : Type u} (U : ι → opens Y.carrier) (hU : supr U = ⊤) (hU' : ∀ i, is_affine_open (U i)),
∀ i, @@P (f ∣_ (U i)) (hU' i)] :=
begin
tfae_have : 1 → 4,
{ intros H U g h₁ h₂,
resetI,
replace H := H ⟨⟨_, h₂.base_open.open_range⟩,
range_is_affine_open_of_open_immersion g⟩,
rw ← P.to_property_apply at H ⊢,
rwa ← hP.1.arrow_mk_iso_iff (morphism_restrict_opens_range f _) },
tfae_have : 4 → 3,
{ intros H 𝒰 h𝒰 i,
resetI,
apply H },
tfae_have : 3 → 2,
{ exact λ H, ⟨Y.affine_cover, infer_instance, H Y.affine_cover⟩ },
tfae_have : 2 → 1,
{ rintro ⟨𝒰, h𝒰, H⟩, exactI target_affine_locally_of_open_cover hP f 𝒰 H },
tfae_have : 5 → 2,
{ rintro ⟨ι, U, hU, hU', H⟩,
refine ⟨Y.open_cover_of_supr_eq_top U hU, hU', _⟩,
intro i,
specialize H i,
rw [← P.to_property_apply, ← hP.1.arrow_mk_iso_iff (morphism_restrict_opens_range f _)],
rw ← P.to_property_apply at H,
convert H,
all_goals { ext1, exact subtype.range_coe } },
tfae_have : 1 → 5,
{ intro H,
refine ⟨Y.carrier, λ x, (Y.affine_cover.map x).opens_range, _,
λ i, range_is_affine_open_of_open_immersion _, _⟩,
{ rw eq_top_iff, intros x _, erw opens.mem_supr, exact⟨x, Y.affine_cover.covers x⟩ },
{ intro i, exact H ⟨_, range_is_affine_open_of_open_immersion _⟩ } },
tfae_finish
end
lemma affine_target_morphism_property.is_local_of_open_cover_imply
(P : affine_target_morphism_property) (hP : P.to_property.respects_iso)
(H : ∀ {X Y : Scheme.{u}} (f : X ⟶ Y),
(∃ (𝒰 : Scheme.open_cover.{u} Y) [∀ i, is_affine (𝒰.obj i)], ∀ (i : 𝒰.J),
by exactI P (pullback.snd : (𝒰.pullback_cover f).obj i ⟶ 𝒰.obj i)) →
(∀ {U : Scheme} (g : U ⟶ Y) [is_affine U] [is_open_immersion g],
by exactI P (pullback.snd : pullback f g ⟶ U))) : P.is_local :=
begin
refine ⟨hP, _, _⟩,
{ introv h,
resetI,
haveI : is_affine _ := (top_is_affine_open Y).basic_open_is_affine r,
delta morphism_restrict,
rw affine_cancel_left_is_iso hP,
refine @@H f ⟨Scheme.open_cover_of_is_iso (𝟙 Y), _, _⟩ (Y.of_restrict _) _inst _,
{ intro i, dsimp, apply_instance },
{ intro i, dsimp,
rwa [← category.comp_id pullback.snd, ← pullback.condition, affine_cancel_left_is_iso hP] } },
{ introv hs hs',
resetI,
replace hs := ((top_is_affine_open Y).basic_open_union_eq_self_iff _).mpr hs,
have := H f ⟨Y.open_cover_of_supr_eq_top _ hs, _, _⟩ (𝟙 _),
rwa [← category.comp_id pullback.snd, ← pullback.condition,
affine_cancel_left_is_iso hP] at this,
{ intro i, exact (top_is_affine_open Y).basic_open_is_affine _ },
{ rintro (i : s),
specialize hs' i,
haveI : is_affine _ := (top_is_affine_open Y).basic_open_is_affine i.1,
delta morphism_restrict at hs',
rwa affine_cancel_left_is_iso hP at hs' } }
end
lemma affine_target_morphism_property.is_local.affine_open_cover_iff
{P : affine_target_morphism_property} (hP : P.is_local)
{X Y : Scheme.{u}} (f : X ⟶ Y) (𝒰 : Scheme.open_cover.{u} Y) [h𝒰 : ∀ i, is_affine (𝒰.obj i)] :
target_affine_locally P f ↔ ∀ i, @@P (pullback.snd : pullback f (𝒰.map i) ⟶ _) (h𝒰 i) :=
⟨λ H, let h := ((hP.affine_open_cover_tfae f).out 0 2).mp H in h 𝒰,
λ H, let h := ((hP.affine_open_cover_tfae f).out 1 0).mp in h ⟨𝒰, infer_instance, H⟩⟩
lemma affine_target_morphism_property.is_local.affine_target_iff
{P : affine_target_morphism_property} (hP : P.is_local)
{X Y : Scheme.{u}} (f : X ⟶ Y) [is_affine Y] :
target_affine_locally P f ↔ P f :=
begin
rw hP.affine_open_cover_iff f _,
swap, { exact Scheme.open_cover_of_is_iso (𝟙 Y) },
swap, { intro _, dsimp, apply_instance },
transitivity (P (pullback.snd : pullback f (𝟙 _) ⟶ _)),
{ exact ⟨λ H, H punit.star, λ H _, H⟩ },
rw [← category.comp_id pullback.snd, ← pullback.condition, affine_cancel_left_is_iso hP.1],
end
/--
We say that `P : morphism_property Scheme` is local at the target if
1. `P` respects isomorphisms.
2. If `P` holds for `f : X ⟶ Y`, then `P` holds for `f ∣_ U` for any `U`.
3. If `P` holds for `f ∣_ U` for an open cover `U` of `Y`, then `P` holds for `f`.
-/
structure property_is_local_at_target (P : morphism_property Scheme) : Prop :=
(respects_iso : P.respects_iso)
(restrict : ∀ {X Y : Scheme} (f : X ⟶ Y) (U : opens Y.carrier), P f → P (f ∣_ U))
(of_open_cover : ∀ {X Y : Scheme.{u}} (f : X ⟶ Y) (𝒰 : Scheme.open_cover.{u} Y),
(∀ (i : 𝒰.J), P (pullback.snd : (𝒰.pullback_cover f).obj i ⟶ 𝒰.obj i)) → P f)
lemma affine_target_morphism_property.is_local.target_affine_locally_is_local
{P : affine_target_morphism_property} (hP : P.is_local) :
property_is_local_at_target (target_affine_locally P) :=
begin
constructor,
{ exact target_affine_locally_respects_iso hP.1 },
{ intros X Y f U H V,
rw [← P.to_property_apply, hP.1.arrow_mk_iso_iff (morphism_restrict_restrict f _ _)],
convert H ⟨_, is_affine_open.image_is_open_immersion V.2 (Y.of_restrict _)⟩,
rw ← P.to_property_apply,
refl },
{ rintros X Y f 𝒰 h𝒰,
rw (hP.affine_open_cover_tfae f).out 0 1,
refine ⟨𝒰.bind (λ _, Scheme.affine_cover _), _, _⟩,
{ intro i, dsimp [Scheme.open_cover.bind], apply_instance },
{ intro i,
specialize h𝒰 i.1,
rw (hP.affine_open_cover_tfae (pullback.snd : pullback f (𝒰.map i.fst) ⟶ _)).out 0 2
at h𝒰,
specialize h𝒰 (Scheme.affine_cover _) i.2,
let e : pullback f ((𝒰.obj i.fst).affine_cover.map i.snd ≫ 𝒰.map i.fst) ⟶
pullback (pullback.snd : pullback f (𝒰.map i.fst) ⟶ _)
((𝒰.obj i.fst).affine_cover.map i.snd),
{ refine (pullback_symmetry _ _).hom ≫ _,
refine (pullback_right_pullback_fst_iso _ _ _).inv ≫ _,
refine (pullback_symmetry _ _).hom ≫ _,
refine pullback.map _ _ _ _ (pullback_symmetry _ _).hom (𝟙 _) (𝟙 _) _ _;
simp only [category.comp_id, category.id_comp, pullback_symmetry_hom_comp_snd] },
rw ← affine_cancel_left_is_iso hP.1 e at h𝒰,
convert h𝒰,
simp } },
end
lemma property_is_local_at_target.open_cover_tfae
{P : morphism_property Scheme}
(hP : property_is_local_at_target P)
{X Y : Scheme.{u}} (f : X ⟶ Y) :
tfae [P f,
∃ (𝒰 : Scheme.open_cover.{u} Y), ∀ (i : 𝒰.J),
P (pullback.snd : (𝒰.pullback_cover f).obj i ⟶ 𝒰.obj i),
∀ (𝒰 : Scheme.open_cover.{u} Y) (i : 𝒰.J),
P (pullback.snd : (𝒰.pullback_cover f).obj i ⟶ 𝒰.obj i),
∀ (U : opens Y.carrier), P (f ∣_ U),
∀ {U : Scheme} (g : U ⟶ Y) [is_open_immersion g],
P (pullback.snd : pullback f g ⟶ U),
∃ {ι : Type u} (U : ι → opens Y.carrier) (hU : supr U = ⊤), (∀ i, P (f ∣_ (U i)))] :=
begin
tfae_have : 2 → 1,
{ rintro ⟨𝒰, H⟩, exact hP.3 f 𝒰 H },
tfae_have : 1 → 4,
{ intros H U, exact hP.2 f U H },
tfae_have : 4 → 3,
{ intros H 𝒰 i,
rw ← hP.1.arrow_mk_iso_iff (morphism_restrict_opens_range f _),
exact H (𝒰.map i).opens_range },
tfae_have : 3 → 2,
{ exact λ H, ⟨Y.affine_cover, H Y.affine_cover⟩ },
tfae_have : 4 → 5,
{ intros H U g hg,
resetI,
rw ← hP.1.arrow_mk_iso_iff (morphism_restrict_opens_range f _),
apply H },
tfae_have : 5 → 4,
{ intros H U,
erw hP.1.cancel_left_is_iso,
apply H },
tfae_have : 4 → 6,
{ intro H, exact ⟨punit, λ _, ⊤, csupr_const, λ _, H _⟩ },
tfae_have : 6 → 2,
{ rintro ⟨ι, U, hU, H⟩,
refine ⟨Y.open_cover_of_supr_eq_top U hU, _⟩,
intro i,
rw ← hP.1.arrow_mk_iso_iff (morphism_restrict_opens_range f _),
convert H i,
all_goals { ext1, exact subtype.range_coe } },
tfae_finish
end
lemma property_is_local_at_target.open_cover_iff
{P : morphism_property Scheme} (hP : property_is_local_at_target P)
{X Y : Scheme.{u}} (f : X ⟶ Y) (𝒰 : Scheme.open_cover.{u} Y) :
P f ↔ ∀ i, P (pullback.snd : pullback f (𝒰.map i) ⟶ _) :=
⟨λ H, let h := ((hP.open_cover_tfae f).out 0 2).mp H in h 𝒰,
λ H, let h := ((hP.open_cover_tfae f).out 1 0).mp in h ⟨𝒰, H⟩⟩
namespace affine_target_morphism_property
/-- A `P : affine_target_morphism_property` is stable under base change if `P` holds for `Y ⟶ S`
implies that `P` holds for `X ×ₛ Y ⟶ X` with `X` and `S` affine schemes. -/
def stable_under_base_change
(P : affine_target_morphism_property) : Prop :=
∀ ⦃X Y S : Scheme⦄ [is_affine S] [is_affine X] (f : X ⟶ S) (g : Y ⟶ S),
by exactI P g → P (pullback.fst : pullback f g ⟶ X)
lemma is_local.target_affine_locally_pullback_fst_of_right_of_stable_under_base_change
{P : affine_target_morphism_property} (hP : P.is_local) (hP' : P.stable_under_base_change)
{X Y S : Scheme} (f : X ⟶ S) (g : Y ⟶ S) [is_affine S] (H : P g) :
target_affine_locally P (pullback.fst : pullback f g ⟶ X) :=
begin
rw (hP.affine_open_cover_tfae (pullback.fst : pullback f g ⟶ X)).out 0 1,
use [X.affine_cover, infer_instance],
intro i,
let e := pullback_symmetry _ _ ≪≫ pullback_right_pullback_fst_iso f g (X.affine_cover.map i),
have : e.hom ≫ pullback.fst = pullback.snd := by simp,
rw [← this, affine_cancel_left_is_iso hP.1],
apply hP'; assumption,
end
lemma is_local.stable_under_base_change
{P : affine_target_morphism_property} (hP : P.is_local) (hP' : P.stable_under_base_change) :
(target_affine_locally P).stable_under_base_change :=
morphism_property.stable_under_base_change.mk (target_affine_locally_respects_iso hP.respects_iso)
begin
intros X Y S f g H,
rw (hP.target_affine_locally_is_local.open_cover_tfae (pullback.fst : pullback f g ⟶ X)).out 0 1,
use S.affine_cover.pullback_cover f,
intro i,
rw (hP.affine_open_cover_tfae g).out 0 3 at H,
let e : pullback (pullback.fst : pullback f g ⟶ _) ((S.affine_cover.pullback_cover f).map i) ≅ _,
{ refine pullback_symmetry _ _ ≪≫ pullback_right_pullback_fst_iso f g _ ≪≫ _ ≪≫
(pullback_right_pullback_fst_iso (S.affine_cover.map i) g
(pullback.snd : pullback f (S.affine_cover.map i) ⟶ _)).symm,
exact as_iso (pullback.map _ _ _ _ (𝟙 _) (𝟙 _) (𝟙 _)
(by simpa using pullback.condition) (by simp)) },
have : e.hom ≫ pullback.fst = pullback.snd := by simp,
rw [← this, (target_affine_locally_respects_iso hP.1).cancel_left_is_iso],
apply hP.target_affine_locally_pullback_fst_of_right_of_stable_under_base_change hP',
rw [← pullback_symmetry_hom_comp_snd, affine_cancel_left_is_iso hP.1],
apply H
end
end affine_target_morphism_property
end algebraic_geometry
|
1fe49cf74dd0fb748dc3353efd93b54300b324b8 | 7282d49021d38dacd06c4ce45a48d09627687fe0 | /tests/lean/simp28.lean | 1e4693e4f21d9be3cb353a1f98f315e2ff9993bb | [
"Apache-2.0"
] | permissive | steveluc/lean | 5a0b4431acefaf77f15b25bbb49294c2449923ad | 92ba4e8b2d040a799eda7deb8d2a7cdd3e69c496 | refs/heads/master | 1,611,332,256,930 | 1,391,013,244,000 | 1,391,013,244,000 | 16,361,079 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,362 | lean | import cast
variable vec : Nat → Type
variable concat {n m : Nat} (v : vec n) (w : vec m) : vec (n + m)
infixl 65 ; : concat
axiom concat_assoc {n1 n2 n3 : Nat} (v1 : vec n1) (v2 : vec n2) (v3 : vec n3) :
(v1 ; v2) ; v3 = cast (congr2 vec (symm (Nat::add_assoc n1 n2 n3)))
(v1 ; (v2 ; v3))
variable empty : vec 0
axiom concat_empty {n : Nat} (v : vec n) :
v ; empty = cast (congr2 vec (symm (Nat::add_zeror n)))
v
rewrite_set simple
add_rewrite Nat::add_assoc Nat::add_zeror eq_id : simple
add_rewrite concat_assoc concat_empty Nat::add_assoc Nat::add_zeror : simple
(*
local t = parse_lean('λ n : Nat, λ v : vec n, v ; empty')
local t2, pr = simplify(t, "simple")
print(t2)
-- print(pr)
get_environment():type_check(pr)
*)
variable f {A : Type} : A → A
(*
local t = parse_lean('λ n : Nat, λ v : vec (n + 0), (f v) ; empty')
local t2, pr = simplify(t, "simple")
print(t2)
-- print(pr)
get_environment():type_check(pr)
*)
print ""
variable lheq {A B : TypeM} : A → B → Bool
infixl 50 === : lheq
(*
local t = parse_lean('λ val : Nat, (λ n : Nat, λ v : vec (n + 0), (f v) ; empty) val === (λ n : Nat, λ v : vec (n + 0), v) val')
print(t)
print("=====>")
local t2, pr = simplify(t, "simple")
print(t2)
-- print(pr)
get_environment():type_check(pr)
*)
|
d16132c2ecccc79aaacd9fe2280b9aaf53a4cd9e | a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91 | /tests/lean/run/751.lean | 3e5fbe431acac629ea4705df35766d772aac92fe | [
"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 | 94 | lean | exit
inductive foo (A : Type) :=
| intro : foo A → foo A
with bar : Type :=
| intro : bar A
|
0db1b90d09202d69f916071c6dbe4e3d363ecf51 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/category_theory/sites/limits.lean | af9b4b93f99ca91df620622cba3bb1e054dc2189 | [
"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 | 8,051 | lean | /-
Copyright (c) 2021 Adam Topaz. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Adam Topaz
-/
import category_theory.limits.creates
import category_theory.sites.sheafification
/-!
# Limits and colimits of sheaves
## Limits
We prove that the forgetful functor from `Sheaf J D` to presheaves creates limits.
If the target category `D` has limits (of a certain shape),
this then implies that `Sheaf J D` has limits of the same shape and that the forgetful
functor preserves these limits.
## Colimits
Given a diagram `F : K ⥤ Sheaf J D` of sheaves, and a colimit cocone on the level of presheaves,
we show that the cocone obtained by sheafifying the cocone point is a colimit cocone of sheaves.
This allows us to show that `Sheaf J D` has colimits (of a certain shape) as soon as `D` does.
-/
namespace category_theory
namespace Sheaf
open category_theory.limits
open opposite
section limits
universes w v u
variables {C : Type (max v u)} [category.{v} C] {J : grothendieck_topology C}
variables {D : Type w} [category.{max v u} D]
variables {K : Type (max v u)} [small_category K]
noncomputable theory
section
/-- An auxiliary definition to be used below.
Whenever `E` is a cone of shape `K` of sheaves, and `S` is the multifork associated to a
covering `W` of an object `X`, with respect to the cone point `E.X`, this provides a cone of
shape `K` of objects in `D`, with cone point `S.X`.
See `is_limit_multifork_of_is_limit` for more on how this definition is used.
-/
def multifork_evaluation_cone (F : K ⥤ Sheaf J D)
(E : cone (F ⋙ Sheaf_to_presheaf J D)) (X : C) (W : J.cover X) (S : multifork (W.index E.X)) :
cone (F ⋙ Sheaf_to_presheaf J D ⋙ (evaluation Cᵒᵖ D).obj (op X)) :=
{ X := S.X,
π :=
{ app := λ k, (presheaf.is_limit_of_is_sheaf J (F.obj k).1 W (F.obj k).2).lift $
multifork.of_ι _ S.X (λ i, S.ι i ≫ (E.π.app k).app (op i.Y)) begin
intros i,
simp only [category.assoc],
erw [← (E.π.app k).naturality, ← (E.π.app k).naturality],
dsimp,
simp only [← category.assoc],
congr' 1,
apply S.condition,
end,
naturality' := begin
intros i j f,
dsimp [presheaf.is_limit_of_is_sheaf],
rw [category.id_comp],
apply presheaf.is_sheaf.hom_ext (F.obj j).2 W,
intros ii,
rw [presheaf.is_sheaf.amalgamate_map, category.assoc, ← (F.map f).val.naturality,
← category.assoc, presheaf.is_sheaf.amalgamate_map],
dsimp [multifork.of_ι],
erw [category.assoc, ← E.w f],
tidy,
end } }
variables [has_limits_of_shape K D]
/-- If `E` is a cone of shape `K` of sheaves, which is a limit on the level of presheves,
this definition shows that the limit presheaf satisfies the multifork variant of the sheaf
condition, at a given covering `W`.
This is used below in `is_sheaf_of_is_limit` to show that the limit presheaf is indeed a sheaf.
-/
def is_limit_multifork_of_is_limit (F : K ⥤ Sheaf J D)
(E : cone (F ⋙ Sheaf_to_presheaf J D))
(hE : is_limit E) (X : C) (W : J.cover X) : is_limit (W.multifork E.X) :=
multifork.is_limit.mk _
(λ S, (is_limit_of_preserves ((evaluation Cᵒᵖ D).obj (op X)) hE).lift $
multifork_evaluation_cone F E X W S)
begin
intros S i,
apply (is_limit_of_preserves ((evaluation Cᵒᵖ D).obj (op i.Y)) hE).hom_ext,
intros k,
dsimp [multifork.of_ι],
erw [category.assoc, (E.π.app k).naturality],
dsimp,
rw ← category.assoc,
erw (is_limit_of_preserves ((evaluation Cᵒᵖ D).obj (op X)) hE).fac
(multifork_evaluation_cone F E X W S),
dsimp [multifork_evaluation_cone, presheaf.is_limit_of_is_sheaf],
erw presheaf.is_sheaf.amalgamate_map,
refl,
end
begin
intros S m hm,
apply (is_limit_of_preserves ((evaluation Cᵒᵖ D).obj (op X)) hE).hom_ext,
intros k,
dsimp,
erw (is_limit_of_preserves ((evaluation Cᵒᵖ D).obj (op X)) hE).fac,
apply presheaf.is_sheaf.hom_ext (F.obj k).2 W,
intros i,
erw presheaf.is_sheaf.amalgamate_map,
dsimp [multifork.of_ι],
change _ = S.ι i ≫ _,
erw [← hm, category.assoc, ← (E.π.app k).naturality, category.assoc],
refl,
end
/-- If `E` is a cone which is a limit on the level of presheaves,
then the limit presheaf is again a sheaf.
This is used to show that the forgetful functor from sheaves to presheaves creates limits.
-/
lemma is_sheaf_of_is_limit (F : K ⥤ Sheaf J D) (E : cone (F ⋙ Sheaf_to_presheaf J D))
(hE : is_limit E) : presheaf.is_sheaf J E.X :=
begin
rw presheaf.is_sheaf_iff_multifork,
intros X S,
exact ⟨is_limit_multifork_of_is_limit _ _ hE _ _⟩,
end
instance (F : K ⥤ Sheaf J D) : creates_limit F (Sheaf_to_presheaf J D) :=
creates_limit_of_reflects_iso $ λ E hE,
{ lifted_cone := ⟨⟨E.X, is_sheaf_of_is_limit _ _ hE⟩,
⟨λ t, ⟨E.π.app _⟩, λ u v e, Sheaf.hom.ext _ _ $ E.π.naturality _⟩⟩,
valid_lift := cones.ext (eq_to_iso rfl) $ λ j, by { dsimp, simp },
makes_limit :=
{ lift := λ S, ⟨hE.lift ((Sheaf_to_presheaf J D).map_cone S)⟩,
fac' := λ S j, by { ext1, apply hE.fac ((Sheaf_to_presheaf J D).map_cone S) j },
uniq' := λ S m hm, begin
ext1,
exact hE.uniq ((Sheaf_to_presheaf J D).map_cone S) m.val (λ j, congr_arg hom.val (hm j)),
end } }
instance : creates_limits_of_shape K (Sheaf_to_presheaf J D) := {}
instance : has_limits_of_shape K (Sheaf J D) :=
has_limits_of_shape_of_has_limits_of_shape_creates_limits_of_shape (Sheaf_to_presheaf J D)
end
instance [has_limits D] : creates_limits (Sheaf_to_presheaf J D) := ⟨⟩
instance [has_limits D] : has_limits (Sheaf J D) :=
has_limits_of_has_limits_creates_limits (Sheaf_to_presheaf J D)
end limits
section colimits
universes w v u
variables {C : Type (max v u)} [category.{v} C] {J : grothendieck_topology C}
variables {D : Type w} [category.{max v u} D]
variables {K : Type (max v u)} [small_category K]
-- Now we need a handful of instances to obtain sheafification...
variables [concrete_category.{max v u} D]
variables [∀ (P : Cᵒᵖ ⥤ D) (X : C) (S : J.cover X), has_multiequalizer (S.index P)]
variables [preserves_limits (forget D)]
variables [∀ (X : C), has_colimits_of_shape (J.cover X)ᵒᵖ D]
variables [∀ (X : C), preserves_colimits_of_shape (J.cover X)ᵒᵖ (forget D)]
variables [reflects_isomorphisms (forget D)]
/-- Construct a cocone by sheafifying a cocone point of a cocone `E` of presheaves
over a functor which factors through sheaves.
In `is_colimit_sheafify_cocone`, we show that this is a colimit cocone when `E` is a colimit. -/
@[simps]
def sheafify_cocone {F : K ⥤ Sheaf J D} (E : cocone (F ⋙ Sheaf_to_presheaf J D)) : cocone F :=
{ X := ⟨J.sheafify E.X, grothendieck_topology.plus.is_sheaf_plus_plus _ _⟩,
ι :=
{ app := λ k, ⟨E.ι.app k ≫ J.to_sheafify E.X⟩,
naturality' := λ i j f, by { ext1, dsimp, erw [category.comp_id, ← category.assoc, E.w f] } } }
/-- If `E` is a colimit cocone of presheaves, over a diagram factoring through sheaves,
then `sheafify_cocone E` is a colimit cocone. -/
@[simps]
def is_colimit_sheafify_cocone {F : K ⥤ Sheaf J D} (E : cocone (F ⋙ Sheaf_to_presheaf J D))
(hE : is_colimit E) :
is_colimit (sheafify_cocone E) :=
{ desc := λ S, ⟨J.sheafify_lift (hE.desc ((Sheaf_to_presheaf J D).map_cocone S)) S.X.2⟩,
fac' := begin
intros S j,
ext1,
dsimp [sheafify_cocone],
erw [category.assoc, J.to_sheafify_sheafify_lift, hE.fac],
refl,
end,
uniq' := begin
intros S m hm,
ext1,
apply J.sheafify_lift_unique,
apply hE.uniq ((Sheaf_to_presheaf J D).map_cocone S),
intros j,
dsimp,
simpa only [← category.assoc, ← hm],
end }
instance [has_colimits_of_shape K D] : has_colimits_of_shape K (Sheaf J D) :=
⟨λ F, has_colimit.mk ⟨sheafify_cocone (colimit.cocone _),
is_colimit_sheafify_cocone _ (colimit.is_colimit _)⟩⟩
instance [has_colimits D] : has_colimits (Sheaf J D) := ⟨infer_instance⟩
end colimits
end Sheaf
end category_theory
|
b644b7305a43d97888d1295c8e583f2dcdae326f | b7f22e51856f4989b970961f794f1c435f9b8f78 | /hott/algebra/category/functor/exponential_laws.hlean | a59822402a8ee2a5533de4a4d37cb4091230f3de | [
"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 | 11,334 | hlean | /-
Copyright (c) 2015-2016 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn, Jakob von Raumer
Exponential laws
-/
import types.unit .equivalence .examples
..constructions.terminal ..constructions.initial ..constructions.product ..constructions.sum
..constructions.discrete
open eq category functor is_trunc nat_trans iso unit prod sum prod.ops bool
namespace category
/- C ^ 0 ≅ 1 -/
definition functor_zero_iso_one [constructor] (C : Precategory) : C ^c 0 ≅c 1 :=
begin
fapply isomorphism.MK,
{ apply terminal_functor},
{ apply point, apply initial_functor},
{ fapply functor_eq: intros; esimp at *,
{ apply eq_of_is_contr},
{ apply nat_trans_eq, intro u, induction u}},
{ fapply functor_eq: intros; esimp at *,
{ induction x, reflexivity},
{ induction f, reflexivity}},
end
/- 0 ^ C ≅ 0 if C is inhabited -/
definition zero_functor_functor_zero [constructor] (C : Precategory) (c : C) : 0 ^c C ⇒ 0 :=
begin
fapply functor.mk: esimp,
{ intro F, exact F c},
{ intro F, eapply empty.elim (F c)},
{ intro F, eapply empty.elim (F c)},
{ intro F, eapply empty.elim (F c)},
end
definition zero_functor_iso_zero [constructor] (C : Precategory) (c : C) : 0 ^c C ≅c 0 :=
begin
fapply isomorphism.MK,
{ exact zero_functor_functor_zero C c},
{ apply initial_functor},
{ fapply functor_eq: esimp,
{ intro F, apply empty.elim (F c)},
{ intro F, apply empty.elim (F c)}},
{ fapply functor_eq: esimp,
{ intro u, apply empty.elim u},
{ apply empty.elim}},
end
/- C ^ 1 ≅ C -/
definition functor_one_iso [constructor] (C : Precategory) : C ^c 1 ≅c C :=
begin
fapply isomorphism.MK,
{ exact !eval_functor star},
{ apply functor_curry, apply pr1_functor},
{ fapply functor_eq: esimp,
{ intro F, fapply functor_eq: esimp,
{ intro u, induction u, reflexivity},
{ intro u v f, induction u, induction v, induction f, esimp, rewrite [+id_id,-respect_id]}},
{ intro F G η, apply nat_trans_eq, intro u, esimp,
rewrite [natural_map_hom_of_eq _ u, natural_map_inv_of_eq _ u,▸*,+ap010_functor_eq _ _ u],
induction u, rewrite [▸*, id_leftright]}},
{ fapply functor_eq: esimp,
{ intro c d f, rewrite [▸*, id_leftright]}},
end
/- 1 ^ C ≅ 1 -/
definition one_functor_iso_one [constructor] (C : Precategory) : 1 ^c C ≅c 1 :=
begin
fapply isomorphism.MK,
{ apply terminal_functor},
{ apply functor_curry, apply pr1_functor},
{ fapply functor_eq: esimp,
{ intro F, fapply functor_eq: esimp,
{ intro c, apply unit.eta},
{ intro c d f, apply unit.eta}},
{ intro F G η, fapply nat_trans_eq, esimp, intro c, apply unit.eta}},
{ fapply functor_eq: esimp,
{ intro u, apply unit.eta},
{ intro u v f, apply unit.eta}},
end
/- C ^ 2 ≅ C × C -/
definition functor_two_right [constructor] (C : Precategory)
: C ^c c2 ⇒ C ×c C :=
begin
fapply functor.mk: esimp,
{ intro F, exact (F ff, F tt)},
{ intro F G η, esimp, exact (η ff, η tt)},
{ intro F, reflexivity},
{ intro F G H η θ, reflexivity}
end
definition functor_two_left [constructor] (C : Precategory)
: C ×c C ⇒ C ^c c2 :=
begin
fapply functor.mk: esimp,
{ intro v, exact c2_functor C v.1 v.2},
{ intro v w f, exact c2_nat_trans f.1 f.2},
{ intro v, apply nat_trans_eq, esimp, intro b, induction b: reflexivity},
{ intro u v w g f, apply nat_trans_eq, esimp, intro b, induction b: reflexivity}
end
definition functor_two_iso [constructor] (C : Precategory)
: C ^c c2 ≅c C ×c C :=
begin
fapply isomorphism.MK: esimp,
{ apply functor_two_right},
{ apply functor_two_left},
{ fapply functor_eq: esimp,
{ intro F, apply c2_functor_eta},
{ intro F G η, fapply nat_trans_eq, intro b, esimp,
rewrite [@natural_map_hom_of_eq _ _ _ G, @natural_map_inv_of_eq _ _ _ F,
↑c2_functor_eta, +@ap010_functor_eq c2 C, ▸*],
induction b: esimp; apply id_leftright}},
{ fapply functor_eq: esimp,
{ intro v, apply prod.eta},
{ intro v w f, induction v, induction w, esimp, apply prod_eq: apply id_leftright}},
end
/- Cᵒᵖ ^ Dᵒᵖ ≅ (C ^ D)ᵒᵖ -/
definition opposite_functor_opposite_iso [constructor] (C D : Precategory)
: Cᵒᵖ ^c Dᵒᵖ ≅c (C ^c D)ᵒᵖ :=
begin
fapply isomorphism.MK: esimp,
{ apply opposite_functor_opposite_right},
{ apply opposite_functor_opposite_left},
{ fapply functor_eq: esimp,
{ exact opposite_rev_opposite_functor},
{ intro F G η, fapply nat_trans_eq, esimp, intro d,
rewrite [@natural_map_hom_of_eq _ _ _ G, @natural_map_inv_of_eq _ _ _ F,
↑opposite_rev_opposite_functor, +@ap010_functor_eq Dᵒᵖ Cᵒᵖ, ▸*],
exact !id_right ⬝ !id_left}},
{ fapply functor_eq: esimp,
{ exact opposite_opposite_rev_functor},
{ intro F G η, fapply nat_trans_eq, esimp, intro d,
rewrite [opposite_hom_of_eq, opposite_inv_of_eq, @natural_map_hom_of_eq _ _ _ F,
@natural_map_inv_of_eq _ _ _ G, ↑opposite_opposite_rev_functor, +@ap010_functor_eq, ▸*],
exact !id_right ⬝ !id_left}},
end
/- C ^ (D + E) ≅ C ^ D × C ^ E -/
definition functor_sum_right [constructor] (C D E : Precategory)
: C ^c (D +c E) ⇒ C ^c D ×c C ^c E :=
begin
apply functor_prod,
{ apply precomposition_functor, apply inl_functor},
{ apply precomposition_functor, apply inr_functor}
end
definition functor_sum_left [constructor] (C D E : Precategory)
: C ^c D ×c C ^c E ⇒ C ^c (D +c E) :=
begin
fapply functor.mk: esimp,
{ intro V, exact V.1 +f V.2},
{ intro V W ν, apply sum_nat_trans, exact ν.1, exact ν.2},
{ intro V, apply nat_trans_eq, intro a, induction a: reflexivity},
{ intro U V W ν μ, apply nat_trans_eq, intro a, induction a: reflexivity}
-- REPORT: cannot abstract
end
definition functor_sum_iso [constructor] (C D E : Precategory)
: C ^c (D +c E) ≅c C ^c D ×c C ^c E :=
begin
fapply isomorphism.MK,
{ apply functor_sum_right},
{ apply functor_sum_left},
{ fapply functor_eq: esimp,
{ exact sum_functor_eta},
{ intro F G η, fapply nat_trans_eq, intro a, esimp,
rewrite [@natural_map_hom_of_eq _ _ _ G _ a, @natural_map_inv_of_eq _ _ _ F _ a,
↑sum_functor_eta,+ap010_functor_eq _ _ a],
induction a: esimp: apply id_leftright}},
{ fapply functor_eq: esimp,
{ intro V, induction V with F G, apply prod_eq: esimp,
apply sum_functor_inl, apply sum_functor_inr},
{ intro V W ν, induction V with F G, induction W with F' G', induction ν with η θ,
apply prod_eq: apply nat_trans_eq,
{ intro d, rewrite [▸*,@pr1_hom_of_eq (C ^c D) (C ^c E), @pr1_inv_of_eq (C ^c D) (C ^c E),
@natural_map_hom_of_eq _ _ _ F' _ d, @natural_map_inv_of_eq _ _ _ F _ d,
↑sum_functor_inl,+ap010_functor_eq _ _ d, ▸*], apply id_leftright},
{ intro e, rewrite [▸*,@pr2_hom_of_eq (C ^c D) (C ^c E), @pr2_inv_of_eq (C ^c D) (C ^c E),
@natural_map_hom_of_eq _ _ _ G' _ e, @natural_map_inv_of_eq _ _ _ G _ e,
↑sum_functor_inr,+ap010_functor_eq _ _ e, ▸*], apply id_leftright}}},
end
/- (C × D) ^ E ≅ C ^ E × D ^ E -/
definition prod_functor_right [constructor] (C D E : Precategory)
: (C ×c D) ^c E ⇒ C ^c E ×c D ^c E :=
begin
apply functor_prod,
{ apply postcomposition_functor, apply pr1_functor},
{ apply postcomposition_functor, apply pr2_functor}
end
definition prod_functor_left [constructor] (C D E : Precategory)
: C ^c E ×c D ^c E ⇒ (C ×c D) ^c E :=
begin
fapply functor.mk: esimp,
{ intro V, exact V.1 ×f V.2},
{ intro V W ν, exact prod_nat_trans ν.1 ν.2},
{ intro V, apply nat_trans_eq, intro e, reflexivity},
{ intro U V W ν μ, apply nat_trans_eq, intro e, reflexivity}
end
definition prod_functor_iso [constructor] (C D E : Precategory)
: (C ×c D) ^c E ≅c C ^c E ×c D ^c E :=
begin
fapply isomorphism.MK,
{ apply prod_functor_right},
{ apply prod_functor_left},
{ fapply functor_eq: esimp,
{ exact prod_functor_eta},
{ intro F G η, fapply nat_trans_eq, intro e, esimp,
rewrite [@natural_map_hom_of_eq _ _ _ G, @natural_map_inv_of_eq _ _ _ F,↑prod_functor_eta,
+ap010_functor_eq, +hom_of_eq_inv, ▸*, pr1_hom_of_eq, pr2_hom_of_eq,
pr1_inv_of_eq, pr2_inv_of_eq, ▸*, +id_leftright, prod.eta]}},
{ fapply functor_eq: esimp,
{ intro V, apply prod_eq: esimp, apply pr1_functor_prod, apply pr2_functor_prod},
{ intro V W ν, rewrite [@pr1_hom_of_eq (C ^c E) (D ^c E), @pr2_hom_of_eq (C ^c E) (D ^c E),
@pr1_inv_of_eq (C ^c E) (D ^c E), @pr2_inv_of_eq (C ^c E) (D ^c E)],
apply prod_eq: apply nat_trans_eq; intro v: esimp,
{ rewrite [@natural_map_hom_of_eq _ _ _ W.1, @natural_map_inv_of_eq _ _ _ V.1, ▸*,
↑pr1_functor_prod,+ap010_functor_eq, ▸*, id_leftright]},
{ rewrite [@natural_map_hom_of_eq _ _ _ W.2, @natural_map_inv_of_eq _ _ _ V.2, ▸*,
↑pr2_functor_prod,+ap010_functor_eq, ▸*, id_leftright]}}},
end
/- (C ^ D) ^ E ≅ C ^ (E × D) -/
definition functor_functor_right [constructor] (C D E : Precategory)
: (C ^c D) ^c E ⇒ C ^c (E ×c D) :=
begin
fapply functor.mk: esimp,
{ exact functor_uncurry},
{ apply @nat_trans_uncurry},
{ intro F, apply nat_trans_eq, intro e, reflexivity},
{ intro F G H η θ, apply nat_trans_eq, intro e, reflexivity}
end
definition functor_functor_left [constructor] (C D E : Precategory)
: C ^c (E ×c D) ⇒ (C ^c D) ^c E :=
begin
fapply functor.mk: esimp,
{ exact functor_curry},
{ apply @nat_trans_curry},
{ intro F, apply nat_trans_eq, intro e, reflexivity},
{ intro F G H η θ, apply nat_trans_eq, intro e, reflexivity}
end
definition functor_functor_iso [constructor] (C D E : Precategory)
: (C ^c D) ^c E ≅c C ^c (E ×c D) :=
begin
fapply isomorphism.MK: esimp,
{ apply functor_functor_right},
{ apply functor_functor_left},
{ fapply functor_eq: esimp,
{ exact functor_curry_functor_uncurry},
{ intro F G η, fapply nat_trans_eq, intro e, esimp,
rewrite [@natural_map_hom_of_eq _ _ _ G, @natural_map_inv_of_eq _ _ _ F,
↑functor_curry_functor_uncurry, +@ap010_functor_eq E (C ^c D)],
apply nat_trans_eq, intro d, rewrite [▸*, hom_of_eq_inv,
@natural_map_hom_of_eq _ _ _ (G e), @natural_map_inv_of_eq _ _ _ (F e),
↑functor_curry_functor_uncurry_ob, +@ap010_functor_eq D C, ▸*, id_leftright]}},
{ fapply functor_eq: esimp,
{ intro F, apply functor_uncurry_functor_curry},
{ intro F G η, fapply nat_trans_eq, esimp, intro v, induction v with c d,
rewrite [@natural_map_hom_of_eq _ _ _ G, @natural_map_inv_of_eq _ _ _ F,
↑functor_uncurry_functor_curry, +@ap010_functor_eq, ▸*], apply id_leftright}},
end
end category
|
913c6e80a77a88f12970d16e75642c846c4a5df0 | 1d265c7dd8cb3d0e1d645a19fd6157a2084c3921 | /src/lessons/lesson7.lean | df142a59ef000f57002a8efc024faada782754fc | [
"MIT"
] | permissive | hanzhi713/lean-proofs | de432372f220d302be09b5ca4227f8986567e4fd | 4d8356a878645b9ba7cb036f87737f3f1e68ede5 | refs/heads/master | 1,585,580,245,658 | 1,553,646,623,000 | 1,553,646,623,000 | 151,342,188 | 0 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 2,763 | lean | open classical
theorem double_neg_elim: ∀ { P }, ¬¬P → P :=
begin
assume P : Prop,
assume pfNotNotP : ¬¬P,
cases em P with pfP pfnP,
show P, from pfP,
have f: false := pfNotNotP pfnP,
show P, from false.elim f
end
theorem double_neg_elim': ∀ {P}, (P ∨ ¬P) → ¬¬P → P :=
begin
assume P: Prop,
assume h: P ∨ ¬P,
assume notnotP: ¬¬P,
cases h with a b,
show P, from a,
show P, from false.elim (notnotP b),
end
theorem p_b_c' : ∀ P: Prop, (¬P → false) → P := λ P nnP, double_neg_elim nnP
theorem proof_by_contrapositive:
∀ P Q : Prop, (¬ Q → ¬ P) → (P → Q) :=
begin
assume P Q: Prop,
assume nqnp: (¬ Q → ¬ P),
assume p : P,
have nnq : ¬ Q → false :=
begin
assume nq : ¬Q,
have np : ¬P := nqnp nq,
show false, from np p
end,
show Q, from double_neg_elim nnq
end
theorem proof_by_contrapositive':
∀ P Q : Prop, (¬ Q → ¬ P) → (P → Q) :=
begin
assume P Q: Prop,
assume nqnp: (¬ Q → ¬ P),
assume p : P,
have nnq : ¬ Q → false :=
λ nq : ¬Q, nqnp nq p,
show Q, from double_neg_elim nnq
end
theorem proof_by_contrapositive''''':
∀ P Q : Prop, (¬ Q → ¬ P) → (P → Q) :=
begin
assume (P Q: Prop) (nqnp: ¬ Q → ¬ P) (p: P),
exact double_neg_elim (λ nq : ¬Q, nqnp nq p)
end
theorem proof_by_contrapositive'''':
∀ P Q : Prop, (¬ Q → ¬ P) → (P → Q) :=
begin
assume P Q: Prop,
assume nqnp: (¬ Q → ¬ P),
assume p : P,
exact double_neg_elim (λ nq : ¬Q, nqnp nq p)
end
theorem proof_by_contrapositive'': ∀ P Q : Prop, (¬ Q → ¬ P) → (P → Q) :=
λ (P Q : Prop) (nqnp : ¬ Q → ¬ P) (p: P),
double_neg_elim (λ nq : ¬ Q, nqnp nq p)
theorem proof_by_contrapositive''': ∀ P Q : Prop, (¬ Q → ¬ P) → (P → Q) :=
λ P Q nqnp p, double_neg_elim (λ nq, nqnp nq p)
theorem z: ∀ { a }, ¬¬a → a :=
λ a b,
begin
cases em a with c d,
exact c,
exact false.elim (b d)
end
variable k: Prop
#check em k
#check or.by_cases
#check or.cases_on
theorem double_neg_elim'': ∀ {a}, ¬¬a → a :=
λ a b, or.cases_on (em a) (λ a, a) (λ c, false.elim (b c))
example {P: Prop} (a: ¬¬P) : double_neg_elim a = double_neg_elim'' a := rfl
theorem j: ∀ a b : Prop, (¬ b → ¬ a) → (a → b) :=
λ a b c d, z (λ e, c e d)
variables P Q: Prop
variable q : Q -- proof of Q
example : P → Q := λ p : P, q
example : P → Q := assume p : P, q
-- def prime (p : ℕ) := p ≥ 2 ∧ ∀ n, n | p → n = 1 ∨ n = p |
39db0fd1f5d92cfb182a937fd2787f25c1f01aa0 | 8b209d7bd1995e5795f3538ecb50643f69fb146f | /src/globular/mutual.lean | 20314b8ee18022ada97c912257b7a22991264173 | [] | no_license | semorrison/lean-globular | 319661251c3c6d0e593d4f81d8a18b1856ffbc20 | cf44b96e2921be4efed4e00c412ae6088687e411 | refs/heads/master | 1,609,627,650,726 | 1,543,965,455,000 | 1,543,965,455,000 | 99,391,087 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,091 | lean | /-
I'm looking for some help learning how to do mutual inductive types. The context here is wanting to
define Vicary's semistrict higher categories <https://arxiv.org/pdf/1610.06908.pdf> in Lean. (Partly
just for the sake of having them as an approach to 2-, 3-, and 4- categories for mathlib, and more
importantly as an essential step of using Globular in Lean.)
The actual mutual inductive type needed is quite complicated (it needs notions of "signature,
diagram, slice, embedding, rewrite, and lift"), but here's a trimmed down start that I still don't
know how to do in Lean.
We have a mutual inductive definition of "signatures" and "diagrams". I'll start with an informal
definition, very closely following the text of their paper, except where they make mistakes...
For $n \geq 0$, an $n$-signature Sigma is a family of sets $(\Sigma_0, ..., \Sigma_n)$, such that when $n > 0$,
* $\Sigma' = (Sigma_0, ..., Sigma_{n-1})$ is an $(n-1)$-signature,
* each $g \in \Sigma_n$ is equipped with a pair of $(n-1)$-diagrams $g.s$ and $g.t$ over the signature $\Sigma'$,
* and if $n > 1$, then $g.s.s = g.t.s$ and $g.s.t = g.t.t$.
For $n \geq 0$, an $n$-diagram $D$ over an $n$-signature $\Sigma$ is a
* if $n > 0$, an $(n-1)$-diagram $D.s$ over $\Sigma'$,
* a list of elements of $Sigma_n$,
* if $n = 0$, this list must have length one, and
* some more data (the embeddings) which I want to omit for this question.
How are we meant to tell Lean about this?
-/
universes u
mutual inductive signature, diagram
with signature : Type (u+1)
| zero : Type u → signature
| succ : Type u → signature → signature
with diagram : Type (u+1)
| empty : Π (s : signature) (source : sorry), diagram
| layer : Π (s : signature) (layers : diagram)
-- mutual inductive lists.equiv, lists'.subset
-- with lists.equiv : lists α → lists α → Prop
-- | refl (l) : lists.equiv l l
-- | antisymm {l₁ l₂ : lists' α tt} :
-- lists'.subset l₁ l₂ → lists'.subset l₂ l₁ → lists.equiv ⟨_, l₁⟩ ⟨_, l₂⟩
-- with lists'.subset : lists' α tt → lists' α tt → Prop |
479f16218e3c440a4aa61ca380e2c6202d2edb46 | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/set_theory/game/winner.lean | e87f868626f76227b0ca891d44fe7a010cf7baaa | [
"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 | 3,404 | lean | /-
Copyright (c) 2020 Fox Thomson. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Fox Thomson
-/
import set_theory.pgame
/-!
# Basic definitions about who has a winning stratergy
We define `G.first_loses`, `G.first_wins`, `G.left_wins` and `G.right_wins` for a pgame `G`, which
means the second, first, left and right players have a winning strategy respectively.
These are defined by inequalities which can be unfolded with `pgame.lt_def` and `pgame.le_def`.
-/
namespace pgame
local infix ` ≈ ` := equiv
/-- The player who goes first loses -/
def first_loses (G : pgame) : Prop := G ≤ 0 ∧ 0 ≤ G
/-- The player who goes first wins -/
def first_wins (G : pgame) : Prop := 0 < G ∧ G < 0
/-- The left player can always win -/
def left_wins (G : pgame) : Prop := 0 < G ∧ 0 ≤ G
/-- The right player can always win -/
def right_wins (G : pgame) : Prop := G ≤ 0 ∧ G < 0
theorem zero_first_loses : first_loses 0 := by tidy
theorem one_left_wins : left_wins 1 :=
⟨by { rw lt_def_le, tidy }, by rw le_def; tidy⟩
theorem star_first_wins : first_wins star := ⟨zero_lt_star, star_lt_zero⟩
theorem omega_left_wins : left_wins omega :=
⟨by { rw lt_def_le, exact or.inl ⟨ulift.up 0, by tidy⟩ }, by rw le_def; tidy⟩
lemma winner_cases (G : pgame) : G.left_wins ∨ G.right_wins ∨ G.first_loses ∨ G.first_wins :=
begin
classical,
by_cases hpos : 0 < G;
by_cases hneg : G < 0;
{ try { rw not_lt at hpos },
try { rw not_lt at hneg },
try { left, exact ⟨hpos, hneg⟩ },
try { right, left, exact ⟨hpos, hneg⟩ },
try { right, right, left, exact ⟨hpos, hneg⟩ },
try { right, right, right, exact ⟨hpos, hneg⟩ } }
end
lemma first_loses_is_zero {G : pgame} : G.first_loses ↔ G ≈ 0 := by refl
lemma first_loses_of_equiv {G H : pgame} (h : G ≈ H) : G.first_loses → H.first_loses :=
λ hGp, ⟨le_of_equiv_of_le h.symm hGp.1, le_of_le_of_equiv hGp.2 h⟩
lemma first_wins_of_equiv {G H : pgame} (h : G ≈ H) : G.first_wins → H.first_wins :=
λ hGn, ⟨lt_of_lt_of_equiv hGn.1 h, lt_of_equiv_of_lt h.symm hGn.2⟩
lemma left_wins_of_equiv {G H : pgame} (h : G ≈ H) : G.left_wins → H.left_wins :=
λ hGl, ⟨lt_of_lt_of_equiv hGl.1 h, le_of_le_of_equiv hGl.2 h⟩
lemma right_wins_of_equiv {G H : pgame} (h : G ≈ H) : G.right_wins → H.right_wins :=
λ hGr, ⟨le_of_equiv_of_le h.symm hGr.1, lt_of_equiv_of_lt h.symm hGr.2⟩
lemma first_loses_of_equiv_iff {G H : pgame} (h : G ≈ H) : G.first_loses ↔ H.first_loses :=
⟨first_loses_of_equiv h, first_loses_of_equiv h.symm⟩
lemma first_wins_of_equiv_iff {G H : pgame} (h : G ≈ H) : G.first_wins ↔ H.first_wins :=
⟨first_wins_of_equiv h, first_wins_of_equiv h.symm⟩
lemma left_wins_of_equiv_iff {G H : pgame} (h : G ≈ H) : G.left_wins ↔ H.left_wins :=
⟨left_wins_of_equiv h, left_wins_of_equiv h.symm⟩
lemma right_wins_of_equiv_iff {G H : pgame} (h : G ≈ H) : G.right_wins ↔ H.right_wins :=
⟨right_wins_of_equiv h, right_wins_of_equiv h.symm⟩
lemma not_first_wins_of_first_loses {G : pgame} : G.first_loses → ¬G.first_wins :=
begin
rw first_loses_is_zero,
rintros h ⟨h₀, -⟩,
exact pgame.lt_irrefl 0 (lt_of_lt_of_equiv h₀ h)
end
lemma not_first_loses_of_first_wins {G : pgame} : G.first_wins → ¬G.first_loses :=
imp_not_comm.1 $ not_first_wins_of_first_loses
end pgame
|
802f22a0dab8d1f65bdf10dceb64809a8b9a27c9 | 8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3 | /src/ring_theory/integral_closure.lean | b07aa74f7c509789c686fe734c473091b26b8ecf | [
"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 | 27,926 | lean | /-
Copyright (c) 2019 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau
-/
import ring_theory.adjoin.basic
import ring_theory.polynomial.scale_roots
import ring_theory.polynomial.tower
/-!
# Integral closure of a subring.
If A is an R-algebra then `a : A` is integral over R if it is a root of a monic polynomial
with coefficients in R. Enough theory is developed to prove that integral elements
form a sub-R-algebra of A.
## Main definitions
Let `R` be a `comm_ring` and let `A` be an R-algebra.
* `ring_hom.is_integral_elem (f : R →+* A) (x : A)` : `x` is integral with respect to the map `f`,
* `is_integral (x : A)` : `x` is integral over `R`, i.e., is a root of a monic polynomial with
coefficients in `R`.
* `integral_closure R A` : the integral closure of `R` in `A`, regarded as a sub-`R`-algebra of `A`.
-/
open_locale classical
open_locale big_operators
open polynomial submodule
section ring
variables {R S A : Type*}
variables [comm_ring R] [ring A] [ring S] (f : R →+* S)
/-- An element `x` of `A` is said to be integral over `R` with respect to `f`
if it is a root of a monic polynomial `p : polynomial R` evaluated under `f` -/
def ring_hom.is_integral_elem (f : R →+* A) (x : A) :=
∃ p : polynomial R, monic p ∧ eval₂ f x p = 0
/-- A ring homomorphism `f : R →+* A` is said to be integral
if every element `A` is integral with respect to the map `f` -/
def ring_hom.is_integral (f : R →+* A) :=
∀ x : A, f.is_integral_elem x
variables [algebra R A] (R)
/-- An element `x` of an algebra `A` over a commutative ring `R` is said to be *integral*,
if it is a root of some monic polynomial `p : polynomial R`.
Equivalently, the element is integral over `R` with respect to the induced `algebra_map` -/
def is_integral (x : A) : Prop :=
(algebra_map R A).is_integral_elem x
variable (A)
/-- An algebra is integral if every element of the extension is integral over the base ring -/
def algebra.is_integral : Prop :=
(algebra_map R A).is_integral
variables {R A}
lemma ring_hom.is_integral_map {x : R} : f.is_integral_elem (f x) :=
⟨X - C x, monic_X_sub_C _, by simp⟩
theorem is_integral_algebra_map {x : R} : is_integral R (algebra_map R A x) :=
(algebra_map R A).is_integral_map
theorem is_integral_of_noetherian (H : is_noetherian R A) (x : A) :
is_integral R x :=
begin
let leval : @linear_map R (polynomial R) A _ _ _ _ _ := (aeval x).to_linear_map,
let D : ℕ → submodule R A := λ n, (degree_le R n).map leval,
let M := well_founded.min (is_noetherian_iff_well_founded.1 H)
(set.range D) ⟨_, ⟨0, rfl⟩⟩,
have HM : M ∈ set.range D := well_founded.min_mem _ _ _,
cases HM with N HN,
have HM : ¬M < D (N+1) := well_founded.not_lt_min
(is_noetherian_iff_well_founded.1 H) (set.range D) _ ⟨N+1, rfl⟩,
rw ← HN at HM,
have HN2 : D (N+1) ≤ D N := classical.by_contradiction (λ H, HM
(lt_of_le_not_le (map_mono (degree_le_mono
(with_bot.coe_le_coe.2 (nat.le_succ N)))) H)),
have HN3 : leval (X^(N+1)) ∈ D N,
{ exact HN2 (mem_map_of_mem (mem_degree_le.2 (degree_X_pow_le _))) },
rcases HN3 with ⟨p, hdp, hpe⟩,
refine ⟨X^(N+1) - p, monic_X_pow_sub (mem_degree_le.1 hdp), _⟩,
show leval (X ^ (N + 1) - p) = 0,
rw [linear_map.map_sub, hpe, sub_self]
end
theorem is_integral_of_submodule_noetherian (S : subalgebra R A)
(H : is_noetherian R S.to_submodule) (x : A) (hx : x ∈ S) :
is_integral R x :=
begin
suffices : is_integral R (show S, from ⟨x, hx⟩),
{ rcases this with ⟨p, hpm, hpx⟩,
replace hpx := congr_arg S.val hpx,
refine ⟨p, hpm, eq.trans _ hpx⟩,
simp only [aeval_def, eval₂, sum_def],
rw S.val.map_sum,
refine finset.sum_congr rfl (λ n hn, _),
rw [S.val.map_mul, S.val.map_pow, S.val.commutes, S.val_apply, subtype.coe_mk], },
refine is_integral_of_noetherian H ⟨x, hx⟩
end
end ring
section
variables {R A B S : Type*}
variables [comm_ring R] [comm_ring A] [comm_ring B] [comm_ring S]
variables [algebra R A] [algebra R B] (f : R →+* S)
theorem is_integral_alg_hom (f : A →ₐ[R] B) {x : A} (hx : is_integral R x) : is_integral R (f x) :=
let ⟨p, hp, hpx⟩ :=
hx in ⟨p, hp, by rw [← aeval_def, aeval_alg_hom_apply, aeval_def, hpx, f.map_zero]⟩
theorem is_integral_of_is_scalar_tower [algebra A B] [is_scalar_tower R A B]
(x : B) (hx : is_integral R x) : is_integral A x :=
let ⟨p, hp, hpx⟩ := hx in
⟨p.map $ algebra_map R A, monic_map _ hp,
by rw [← aeval_def, ← is_scalar_tower.aeval_apply, aeval_def, hpx]⟩
theorem is_integral_of_subring {x : A} (T : subring R)
(hx : is_integral T x) : is_integral R x :=
is_integral_of_is_scalar_tower x hx
lemma is_integral_algebra_map_iff [algebra A B] [is_scalar_tower R A B]
{x : A} (hAB : function.injective (algebra_map A B)) :
is_integral R (algebra_map A B x) ↔ is_integral R x :=
begin
split; rintros ⟨f, hf, hx⟩; use [f, hf],
{ exact is_scalar_tower.aeval_eq_zero_of_aeval_algebra_map_eq_zero R A B hAB hx },
{ rw [is_scalar_tower.algebra_map_eq R A B, ← hom_eval₂, hx, ring_hom.map_zero] }
end
theorem is_integral_iff_is_integral_closure_finite {r : A} :
is_integral R r ↔ ∃ s : set R, s.finite ∧ is_integral (subring.closure s) r :=
begin
split; intro hr,
{ rcases hr with ⟨p, hmp, hpr⟩,
refine ⟨_, set.finite_mem_finset _, p.restriction, monic_restriction.2 hmp, _⟩,
erw [← aeval_def, is_scalar_tower.aeval_apply _ R, map_restriction, aeval_def, hpr] },
rcases hr with ⟨s, hs, hsr⟩,
exact is_integral_of_subring _ hsr
end
theorem fg_adjoin_singleton_of_integral (x : A) (hx : is_integral R x) :
(algebra.adjoin R ({x} : set A)).to_submodule.fg :=
begin
rcases hx with ⟨f, hfm, hfx⟩,
existsi finset.image ((^) x) (finset.range (nat_degree f + 1)),
apply le_antisymm,
{ rw span_le, intros s hs, rw finset.mem_coe at hs,
rcases finset.mem_image.1 hs with ⟨k, hk, rfl⟩, clear hk,
exact (algebra.adjoin R {x}).pow_mem (algebra.subset_adjoin (set.mem_singleton _)) k },
intros r hr, change r ∈ algebra.adjoin R ({x} : set A) at hr,
rw algebra.adjoin_singleton_eq_range at hr,
rcases (aeval x).mem_range.mp hr with ⟨p, rfl⟩,
rw ← mod_by_monic_add_div p hfm,
rw ← aeval_def at hfx,
rw [alg_hom.map_add, alg_hom.map_mul, hfx, zero_mul, add_zero],
have : degree (p %ₘ f) ≤ degree f := degree_mod_by_monic_le p hfm,
generalize_hyp : p %ₘ f = q at this ⊢,
rw [← sum_C_mul_X_eq q, aeval_def, eval₂_sum, sum_def],
refine sum_mem _ (λ k hkq, _),
rw [eval₂_mul, eval₂_C, eval₂_pow, eval₂_X, ← algebra.smul_def],
refine smul_mem _ _ (subset_span _),
rw finset.mem_coe, refine finset.mem_image.2 ⟨_, _, rfl⟩,
rw [finset.mem_range, nat.lt_succ_iff], refine le_of_not_lt (λ hk, _),
rw [degree_le_iff_coeff_zero] at this,
rw [mem_support_iff] at hkq, apply hkq, apply this,
exact lt_of_le_of_lt degree_le_nat_degree (with_bot.coe_lt_coe.2 hk)
end
theorem fg_adjoin_of_finite {s : set A} (hfs : s.finite)
(his : ∀ x ∈ s, is_integral R x) : (algebra.adjoin R s).to_submodule.fg :=
set.finite.induction_on hfs (λ _, ⟨{1}, submodule.ext $ λ x,
by { erw [algebra.adjoin_empty, finset.coe_singleton, ← one_eq_span, one_eq_range,
linear_map.mem_range, algebra.mem_bot], refl }⟩)
(λ a s has hs ih his, by rw [← set.union_singleton, algebra.adjoin_union_coe_submodule]; exact
fg_mul _ _ (ih $ λ i hi, his i $ set.mem_insert_of_mem a hi)
(fg_adjoin_singleton_of_integral _ $ his a $ set.mem_insert a s)) his
/-- If `S` is a sub-`R`-algebra of `A` and `S` is finitely-generated as an `R`-module,
then all elements of `S` are integral over `R`. -/
theorem is_integral_of_mem_of_fg (S : subalgebra R A)
(HS : S.to_submodule.fg) (x : A) (hx : x ∈ S) : is_integral R x :=
begin
-- say `x ∈ S`. We want to prove that `x` is integral over `R`.
-- Say `S` is generated as an `R`-module by the set `y`.
cases HS with y hy,
-- We can write `x` as `∑ rᵢ yᵢ` for `yᵢ ∈ Y`.
obtain ⟨lx, hlx1, hlx2⟩ :
∃ (l : A →₀ R) (H : l ∈ finsupp.supported R R ↑y), (finsupp.total A A R id) l = x,
{ rwa [←(@finsupp.mem_span_image_iff_total A A R _ _ _ id ↑y x), set.image_id ↑y, hy] },
-- Note that `y ⊆ S`.
have hyS : ∀ {p}, p ∈ y → p ∈ S := λ p hp, show p ∈ S.to_submodule,
by { rw ← hy, exact subset_span hp },
-- Now `S` is a subalgebra so the product of two elements of `y` is also in `S`.
have : ∀ (jk : (↑(y.product y) : set (A × A))), jk.1.1 * jk.1.2 ∈ S.to_submodule :=
λ jk, S.mul_mem (hyS (finset.mem_product.1 jk.2).1) (hyS (finset.mem_product.1 jk.2).2),
rw [← hy, ← set.image_id ↑y] at this, simp only [finsupp.mem_span_image_iff_total] at this,
-- Say `yᵢyⱼ = ∑rᵢⱼₖ yₖ`
choose ly hly1 hly2,
-- Now let `S₀` be the subring of `R` generated by the `rᵢ` and the `rᵢⱼₖ`.
let S₀ : subring R :=
subring.closure ↑(lx.frange ∪ finset.bUnion finset.univ (finsupp.frange ∘ ly)),
-- It suffices to prove that `x` is integral over `S₀`.
refine is_integral_of_subring S₀ _,
letI : comm_ring S₀ := subring.to_comm_ring S₀,
letI : algebra S₀ A := algebra.of_subring S₀,
-- Claim: the `S₀`-module span (in `A`) of the set `y ∪ {1}` is closed under
-- multiplication (indeed, this is the motivation for the definition of `S₀`).
have :
span S₀ (insert 1 ↑y : set A) * span S₀ (insert 1 ↑y : set A) ≤ span S₀ (insert 1 ↑y : set A),
{ rw span_mul_span, refine span_le.2 (λ z hz, _),
rcases set.mem_mul.1 hz with ⟨p, q, rfl | hp, hq, rfl⟩,
{ rw one_mul, exact subset_span hq },
rcases hq with rfl | hq,
{ rw mul_one, exact subset_span (or.inr hp) },
erw ← hly2 ⟨(p, q), finset.mem_product.2 ⟨hp, hq⟩⟩,
rw [finsupp.total_apply, finsupp.sum],
refine (span S₀ (insert 1 ↑y : set A)).sum_mem (λ t ht, _),
have : ly ⟨(p, q), finset.mem_product.2 ⟨hp, hq⟩⟩ t ∈ S₀ :=
subring.subset_closure (finset.mem_union_right _ $ finset.mem_bUnion.2
⟨⟨(p, q), finset.mem_product.2 ⟨hp, hq⟩⟩, finset.mem_univ _,
finsupp.mem_frange.2 ⟨finsupp.mem_support_iff.1 ht, _, rfl⟩⟩),
change (⟨_, this⟩ : S₀) • t ∈ _, exact smul_mem _ _ (subset_span $ or.inr $ hly1 _ ht) },
-- Hence this span is a subring. Call this subring `S₁`.
let S₁ : subring A :=
{ carrier := span S₀ (insert 1 ↑y : set A),
one_mem' := subset_span $ or.inl rfl,
mul_mem' := λ p q hp hq, this $ mul_mem_mul hp hq,
zero_mem' := (span S₀ (insert 1 ↑y : set A)).zero_mem,
add_mem' := λ _ _, (span S₀ (insert 1 ↑y : set A)).add_mem,
neg_mem' := λ _, (span S₀ (insert 1 ↑y : set A)).neg_mem },
have : S₁ = (algebra.adjoin S₀ (↑y : set A)).to_subring,
{ ext z,
suffices : z ∈ span ↥S₀ (insert 1 ↑y : set A) ↔
z ∈ (algebra.adjoin ↥S₀ (y : set A)).to_submodule,
{ simpa },
split; intro hz,
{ exact (span_le.2
(set.insert_subset.2 ⟨(algebra.adjoin S₀ ↑y).one_mem, algebra.subset_adjoin⟩)) hz },
{ rw [subalgebra.mem_to_submodule, algebra.mem_adjoin_iff] at hz,
suffices : subring.closure (set.range ⇑(algebra_map ↥S₀ A) ∪ ↑y) ≤ S₁,
{ exact this hz },
refine subring.closure_le.2 (set.union_subset _ (λ t ht, subset_span $ or.inr ht)),
rw set.range_subset_iff,
intro y,
rw algebra.algebra_map_eq_smul_one,
exact smul_mem _ y (subset_span (or.inl rfl)) } },
have foo : ∀ z, z ∈ S₁ ↔ z ∈ algebra.adjoin ↥S₀ (y : set A),
simp [this],
haveI : is_noetherian_ring ↥S₀ := is_noetherian_subring_closure _ (finset.finite_to_set _),
refine is_integral_of_submodule_noetherian (algebra.adjoin S₀ ↑y)
(is_noetherian_of_fg_of_noetherian _ ⟨insert 1 y,
by { rw [finset.coe_insert], ext z, simp [S₁], convert foo z}⟩) _ _,
rw [← hlx2, finsupp.total_apply, finsupp.sum], refine subalgebra.sum_mem _ (λ r hr, _),
have : lx r ∈ S₀ :=
subring.subset_closure (finset.mem_union_left _ (finset.mem_image_of_mem _ hr)),
change (⟨_, this⟩ : S₀) • r ∈ _,
rw finsupp.mem_supported at hlx1,
exact subalgebra.smul_mem _ (algebra.subset_adjoin $ hlx1 hr) _
end
lemma ring_hom.is_integral_of_mem_closure {x y z : S}
(hx : f.is_integral_elem x) (hy : f.is_integral_elem y)
(hz : z ∈ subring.closure ({x, y} : set S)) :
f.is_integral_elem z :=
begin
letI : algebra R S := f.to_algebra,
have := fg_mul _ _ (fg_adjoin_singleton_of_integral x hx) (fg_adjoin_singleton_of_integral y hy),
rw [← algebra.adjoin_union_coe_submodule, set.singleton_union] at this,
exact is_integral_of_mem_of_fg (algebra.adjoin R {x, y}) this z
(algebra.mem_adjoin_iff.2 $ subring.closure_mono (set.subset_union_right _ _) hz),
end
theorem is_integral_of_mem_closure {x y z : A}
(hx : is_integral R x) (hy : is_integral R y)
(hz : z ∈ subring.closure ({x, y} : set A)) :
is_integral R z :=
(algebra_map R A).is_integral_of_mem_closure hx hy hz
lemma ring_hom.is_integral_zero : f.is_integral_elem 0 :=
f.map_zero ▸ f.is_integral_map
theorem is_integral_zero : is_integral R (0:A) :=
(algebra_map R A).is_integral_zero
lemma ring_hom.is_integral_one : f.is_integral_elem 1 :=
f.map_one ▸ f.is_integral_map
theorem is_integral_one : is_integral R (1:A) :=
(algebra_map R A).is_integral_one
lemma ring_hom.is_integral_add {x y : S}
(hx : f.is_integral_elem x) (hy : f.is_integral_elem y) :
f.is_integral_elem (x + y) :=
f.is_integral_of_mem_closure hx hy $ subring.add_mem _
(subring.subset_closure (or.inl rfl)) (subring.subset_closure (or.inr rfl))
theorem is_integral_add {x y : A}
(hx : is_integral R x) (hy : is_integral R y) :
is_integral R (x + y) :=
(algebra_map R A).is_integral_add hx hy
lemma ring_hom.is_integral_neg {x : S}
(hx : f.is_integral_elem x) : f.is_integral_elem (-x) :=
f.is_integral_of_mem_closure hx hx (subring.neg_mem _ (subring.subset_closure (or.inl rfl)))
theorem is_integral_neg {x : A}
(hx : is_integral R x) : is_integral R (-x) :=
(algebra_map R A).is_integral_neg hx
lemma ring_hom.is_integral_sub {x y : S}
(hx : f.is_integral_elem x) (hy : f.is_integral_elem y) : f.is_integral_elem (x - y) :=
by simpa only [sub_eq_add_neg] using f.is_integral_add hx (f.is_integral_neg hy)
theorem is_integral_sub {x y : A}
(hx : is_integral R x) (hy : is_integral R y) : is_integral R (x - y) :=
(algebra_map R A).is_integral_sub hx hy
lemma ring_hom.is_integral_mul {x y : S}
(hx : f.is_integral_elem x) (hy : f.is_integral_elem y) : f.is_integral_elem (x * y) :=
f.is_integral_of_mem_closure hx hy (subring.mul_mem _
(subring.subset_closure (or.inl rfl)) (subring.subset_closure (or.inr rfl)))
theorem is_integral_mul {x y : A}
(hx : is_integral R x) (hy : is_integral R y) : is_integral R (x * y) :=
(algebra_map R A).is_integral_mul hx hy
variables (R A)
/-- The integral closure of R in an R-algebra A. -/
def integral_closure : subalgebra R A :=
{ carrier := { r | is_integral R r },
zero_mem' := is_integral_zero,
one_mem' := is_integral_one,
add_mem' := λ _ _, is_integral_add,
mul_mem' := λ _ _, is_integral_mul,
algebra_map_mem' := λ x, is_integral_algebra_map }
theorem mem_integral_closure_iff_mem_fg {r : A} :
r ∈ integral_closure R A ↔ ∃ M : subalgebra R A, M.to_submodule.fg ∧ r ∈ M :=
⟨λ hr, ⟨algebra.adjoin R {r}, fg_adjoin_singleton_of_integral _ hr, algebra.subset_adjoin rfl⟩,
λ ⟨M, Hf, hrM⟩, is_integral_of_mem_of_fg M Hf _ hrM⟩
variables {R} {A}
/-- Mapping an integral closure along an `alg_equiv` gives the integral closure. -/
lemma integral_closure_map_alg_equiv (f : A ≃ₐ[R] B) :
(integral_closure R A).map (f : A →ₐ[R] B) = integral_closure R B :=
begin
ext y,
rw subalgebra.mem_map,
split,
{ rintros ⟨x, hx, rfl⟩,
exact is_integral_alg_hom f hx },
{ intro hy,
use [f.symm y, is_integral_alg_hom (f.symm : B →ₐ[R] A) hy],
simp }
end
lemma integral_closure.is_integral (x : integral_closure R A) : is_integral R x :=
let ⟨p, hpm, hpx⟩ := x.2 in ⟨p, hpm, subtype.eq $
by rwa [← aeval_def, subtype.val_eq_coe, ← subalgebra.val_apply, aeval_alg_hom_apply] at hpx⟩
lemma ring_hom.is_integral_of_is_integral_mul_unit (x y : S) (r : R) (hr : f r * y = 1)
(hx : f.is_integral_elem (x * y)) : f.is_integral_elem x :=
begin
obtain ⟨p, ⟨p_monic, hp⟩⟩ := hx,
refine ⟨scale_roots p r, ⟨(monic_scale_roots_iff r).2 p_monic, _⟩⟩,
convert scale_roots_eval₂_eq_zero f hp,
rw [mul_comm x y, ← mul_assoc, hr, one_mul],
end
theorem is_integral_of_is_integral_mul_unit {x y : A} {r : R} (hr : algebra_map R A r * y = 1)
(hx : is_integral R (x * y)) : is_integral R x :=
(algebra_map R A).is_integral_of_is_integral_mul_unit x y r hr hx
/-- Generalization of `is_integral_of_mem_closure` bootstrapped up from that lemma -/
lemma is_integral_of_mem_closure' (G : set A) (hG : ∀ x ∈ G, is_integral R x) :
∀ x ∈ (subring.closure G), is_integral R x :=
λ x hx, subring.closure_induction hx hG is_integral_zero is_integral_one
(λ _ _, is_integral_add) (λ _, is_integral_neg) (λ _ _, is_integral_mul)
lemma is_integral_of_mem_closure'' {S : Type*} [comm_ring S] {f : R →+* S} (G : set S)
(hG : ∀ x ∈ G, f.is_integral_elem x) : ∀ x ∈ (subring.closure G), f.is_integral_elem x :=
λ x hx, @is_integral_of_mem_closure' R S _ _ f.to_algebra G hG x hx
lemma is_integral.pow {x : A} (h : is_integral R x) (n : ℕ) : is_integral R (x ^ n) :=
(integral_closure R A).pow_mem h n
lemma is_integral.nsmul {x : A} (h : is_integral R x) (n : ℕ) : is_integral R (n • x) :=
(integral_closure R A).nsmul_mem h n
lemma is_integral.gsmul {x : A} (h : is_integral R x) (n : ℤ) : is_integral R (n • x) :=
(integral_closure R A).gsmul_mem h n
lemma is_integral.multiset_prod {s : multiset A} (h : ∀ x ∈ s, is_integral R x) :
is_integral R s.prod :=
(integral_closure R A).multiset_prod_mem h
lemma is_integral.multiset_sum {s : multiset A} (h : ∀ x ∈ s, is_integral R x) :
is_integral R s.sum :=
(integral_closure R A).multiset_sum_mem h
lemma is_integral.prod {α : Type*} {s : finset α} (f : α → A) (h : ∀ x ∈ s, is_integral R (f x)) :
is_integral R (∏ x in s, f x) :=
(integral_closure R A).prod_mem h
lemma is_integral.sum {α : Type*} {s : finset α} (f : α → A) (h : ∀ x ∈ s, is_integral R (f x)) :
is_integral R (∑ x in s, f x) :=
(integral_closure R A).sum_mem h
end
section algebra
open algebra
variables {R A B S T : Type*}
variables [comm_ring R] [comm_ring A] [comm_ring B] [comm_ring S] [comm_ring T]
variables [algebra A B] [algebra R B] (f : R →+* S) (g : S →+* T)
lemma is_integral_trans_aux (x : B) {p : polynomial A} (pmonic : monic p) (hp : aeval x p = 0) :
is_integral (adjoin R (↑(p.map $ algebra_map A B).frange : set B)) x :=
begin
generalize hS : (↑(p.map $ algebra_map A B).frange : set B) = S,
have coeffs_mem : ∀ i, (p.map $ algebra_map A B).coeff i ∈ adjoin R S,
{ intro i, by_cases hi : (p.map $ algebra_map A B).coeff i = 0,
{ rw hi, exact subalgebra.zero_mem _ },
rw ← hS,
exact subset_adjoin (coeff_mem_frange _ _ hi) },
obtain ⟨q, hq⟩ : ∃ q : polynomial (adjoin R S), q.map (algebra_map (adjoin R S) B) =
(p.map $ algebra_map A B),
{ rw ← set.mem_range, exact (polynomial.mem_map_range _).2 (λ i, ⟨⟨_, coeffs_mem i⟩, rfl⟩) },
use q,
split,
{ suffices h : (q.map (algebra_map (adjoin R S) B)).monic,
{ refine monic_of_injective _ h,
exact subtype.val_injective },
{ rw hq, exact monic_map _ pmonic } },
{ convert hp using 1,
replace hq := congr_arg (eval x) hq,
convert hq using 1; symmetry; apply eval_map },
end
variables [algebra R A] [is_scalar_tower R A B]
/-- If A is an R-algebra all of whose elements are integral over R,
and x is an element of an A-algebra that is integral over A, then x is integral over R.-/
lemma is_integral_trans (A_int : is_integral R A) (x : B) (hx : is_integral A x) :
is_integral R x :=
begin
rcases hx with ⟨p, pmonic, hp⟩,
let S : set B := ↑(p.map $ algebra_map A B).frange,
refine is_integral_of_mem_of_fg (adjoin R (S ∪ {x})) _ _ (subset_adjoin $ or.inr rfl),
refine fg_trans (fg_adjoin_of_finite (finset.finite_to_set _) (λ x hx, _)) _,
{ rw [finset.mem_coe, frange, finset.mem_image] at hx,
rcases hx with ⟨i, _, rfl⟩,
rw coeff_map,
exact is_integral_alg_hom (is_scalar_tower.to_alg_hom R A B) (A_int _) },
{ apply fg_adjoin_singleton_of_integral,
exact is_integral_trans_aux _ pmonic hp }
end
/-- If A is an R-algebra all of whose elements are integral over R,
and B is an A-algebra all of whose elements are integral over A,
then all elements of B are integral over R.-/
lemma algebra.is_integral_trans (hA : is_integral R A) (hB : is_integral A B) : is_integral R B :=
λ x, is_integral_trans hA x (hB x)
lemma ring_hom.is_integral_trans (hf : f.is_integral) (hg : g.is_integral) :
(g.comp f).is_integral :=
@algebra.is_integral_trans R S T _ _ _ g.to_algebra (g.comp f).to_algebra f.to_algebra
(@is_scalar_tower.of_algebra_map_eq R S T _ _ _ f.to_algebra g.to_algebra (g.comp f).to_algebra
(ring_hom.comp_apply g f)) hf hg
lemma ring_hom.is_integral_of_surjective (hf : function.surjective f) : f.is_integral :=
λ x, (hf x).rec_on (λ y hy, (hy ▸ f.is_integral_map : f.is_integral_elem x))
lemma is_integral_of_surjective (h : function.surjective (algebra_map R A)) : is_integral R A :=
(algebra_map R A).is_integral_of_surjective h
/-- If `R → A → B` is an algebra tower with `A → B` injective,
then if the entire tower is an integral extension so is `R → A` -/
lemma is_integral_tower_bot_of_is_integral (H : function.injective (algebra_map A B))
{x : A} (h : is_integral R (algebra_map A B x)) : is_integral R x :=
begin
rcases h with ⟨p, ⟨hp, hp'⟩⟩,
refine ⟨p, ⟨hp, _⟩⟩,
rw [is_scalar_tower.algebra_map_eq R A B, ← eval₂_map,
eval₂_hom, ← ring_hom.map_zero (algebra_map A B)] at hp',
rw [eval₂_eq_eval_map],
exact H hp',
end
lemma ring_hom.is_integral_tower_bot_of_is_integral (hg : function.injective g)
(hfg : (g.comp f).is_integral) : f.is_integral :=
λ x,
@is_integral_tower_bot_of_is_integral R S T _ _ _ g.to_algebra (g.comp f).to_algebra f.to_algebra
(@is_scalar_tower.of_algebra_map_eq R S T _ _ _ f.to_algebra g.to_algebra (g.comp f).to_algebra
(ring_hom.comp_apply g f)) hg x (hfg (g x))
lemma is_integral_tower_bot_of_is_integral_field {R A B : Type*} [comm_ring R] [field A]
[comm_ring B] [nontrivial B] [algebra R A] [algebra A B] [algebra R B] [is_scalar_tower R A B]
{x : A} (h : is_integral R (algebra_map A B x)) : is_integral R x :=
is_integral_tower_bot_of_is_integral (algebra_map A B).injective h
lemma ring_hom.is_integral_elem_of_is_integral_elem_comp {x : T}
(h : (g.comp f).is_integral_elem x) : g.is_integral_elem x :=
let ⟨p, ⟨hp, hp'⟩⟩ := h in ⟨p.map f, monic_map f hp, by rwa ← eval₂_map at hp'⟩
lemma ring_hom.is_integral_tower_top_of_is_integral (h : (g.comp f).is_integral) : g.is_integral :=
λ x, ring_hom.is_integral_elem_of_is_integral_elem_comp f g (h x)
/-- If `R → A → B` is an algebra tower,
then if the entire tower is an integral extension so is `A → B`. -/
lemma is_integral_tower_top_of_is_integral {x : B} (h : is_integral R x) : is_integral A x :=
begin
rcases h with ⟨p, ⟨hp, hp'⟩⟩,
refine ⟨p.map (algebra_map R A), ⟨monic_map (algebra_map R A) hp, _⟩⟩,
rw [is_scalar_tower.algebra_map_eq R A B, ← eval₂_map] at hp',
exact hp',
end
lemma ring_hom.is_integral_quotient_of_is_integral {I : ideal S} (hf : f.is_integral) :
(ideal.quotient_map I f le_rfl).is_integral :=
begin
rintros ⟨x⟩,
obtain ⟨p, ⟨p_monic, hpx⟩⟩ := hf x,
refine ⟨p.map (ideal.quotient.mk _), ⟨monic_map _ p_monic, _⟩⟩,
simpa only [hom_eval₂, eval₂_map] using congr_arg (ideal.quotient.mk I) hpx
end
lemma is_integral_quotient_of_is_integral {I : ideal A} (hRA : is_integral R A) :
is_integral (I.comap (algebra_map R A)).quotient I.quotient :=
(algebra_map R A).is_integral_quotient_of_is_integral hRA
lemma is_integral_quotient_map_iff {I : ideal S} :
(ideal.quotient_map I f le_rfl).is_integral ↔
((ideal.quotient.mk I).comp f : R →+* I.quotient).is_integral :=
begin
let g := ideal.quotient.mk (I.comap f),
have := ideal.quotient_map_comp_mk le_rfl,
refine ⟨λ h, _, λ h, ring_hom.is_integral_tower_top_of_is_integral g _ (this ▸ h)⟩,
refine this ▸ ring_hom.is_integral_trans g (ideal.quotient_map I f le_rfl) _ h,
exact ring_hom.is_integral_of_surjective g ideal.quotient.mk_surjective,
end
/-- If the integral extension `R → S` is injective, and `S` is a field, then `R` is also a field. -/
lemma is_field_of_is_integral_of_is_field {R S : Type*} [integral_domain R] [integral_domain S]
[algebra R S] (H : is_integral R S) (hRS : function.injective (algebra_map R S))
(hS : is_field S) : is_field R :=
begin
refine ⟨⟨0, 1, zero_ne_one⟩, mul_comm, λ a ha, _⟩,
-- Let `a_inv` be the inverse of `algebra_map R S a`,
-- then we need to show that `a_inv` is of the form `algebra_map R S b`.
obtain ⟨a_inv, ha_inv⟩ := hS.mul_inv_cancel (λ h, ha (hRS (trans h (ring_hom.map_zero _).symm))),
-- Let `p : polynomial R` be monic with root `a_inv`,
-- and `q` be `p` with coefficients reversed (so `q(a) = q'(a) * a + 1`).
-- We claim that `q(a) = 0`, so `-q'(a)` is the inverse of `a`.
obtain ⟨p, p_monic, hp⟩ := H a_inv,
use -∑ (i : ℕ) in finset.range p.nat_degree, (p.coeff i) * a ^ (p.nat_degree - i - 1),
-- `q(a) = 0`, because multiplying everything with `a_inv^n` gives `p(a_inv) = 0`.
-- TODO: this could be a lemma for `polynomial.reverse`.
have hq : ∑ (i : ℕ) in finset.range (p.nat_degree + 1), (p.coeff i) * a ^ (p.nat_degree - i) = 0,
{ apply (algebra_map R S).injective_iff.mp hRS,
have a_inv_ne_zero : a_inv ≠ 0 := right_ne_zero_of_mul (mt ha_inv.symm.trans one_ne_zero),
refine (mul_eq_zero.mp _).resolve_right (pow_ne_zero p.nat_degree a_inv_ne_zero),
rw [eval₂_eq_sum_range] at hp,
rw [ring_hom.map_sum, finset.sum_mul],
refine (finset.sum_congr rfl (λ i hi, _)).trans hp,
rw [ring_hom.map_mul, mul_assoc],
congr,
have : a_inv ^ p.nat_degree = a_inv ^ (p.nat_degree - i) * a_inv ^ i,
{ rw [← pow_add a_inv, nat.sub_add_cancel (nat.le_of_lt_succ (finset.mem_range.mp hi))] },
rw [ring_hom.map_pow, this, ← mul_assoc, ← mul_pow, ha_inv, one_pow, one_mul] },
-- Since `q(a) = 0` and `q(a) = q'(a) * a + 1`, we have `a * -q'(a) = 1`.
-- TODO: we could use a lemma for `polynomial.div_X` here.
rw [finset.sum_range_succ_comm, p_monic.coeff_nat_degree, one_mul, nat.sub_self, pow_zero,
add_eq_zero_iff_eq_neg, eq_comm] at hq,
rw [mul_comm, ← neg_mul_eq_neg_mul, finset.sum_mul],
convert hq using 2,
refine finset.sum_congr rfl (λ i hi, _),
have : 1 ≤ p.nat_degree - i := nat.le_sub_left_of_add_le (finset.mem_range.mp hi),
rw [mul_assoc, ← pow_succ', nat.sub_add_cancel this]
end
end algebra
theorem integral_closure_idem {R : Type*} {A : Type*} [comm_ring R] [comm_ring A] [algebra R A] :
integral_closure (integral_closure R A : set A) A = ⊥ :=
eq_bot_iff.2 $ λ x hx, algebra.mem_bot.2
⟨⟨x, @is_integral_trans _ _ _ _ _ _ _ _ (integral_closure R A).algebra
_ integral_closure.is_integral x hx⟩, rfl⟩
section integral_domain
variables {R S : Type*} [comm_ring R] [integral_domain S] [algebra R S]
instance : integral_domain (integral_closure R S) :=
infer_instance
end integral_domain
|
320e3e32e5e9f86e3aa44b6263229d9295c03169 | a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91 | /tests/lean/unify_tac1.lean | efc82fb936c626662cad12acbf02ef04457655b6 | [
"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 | 572 | lean | open tactic
example (A : Type) (a : A) (p : A → Prop) (H : p a) : ∃ x, p x :=
by do
constructor,
tgt ← target,
t ← get_local `H >>= infer_type,
unify tgt t, -- Succeeds unifying p a =?= p ?m_1
assumption
example (A : Type) (a : A) (p : A → Prop) (H : p a) : ∃ x, p x :=
by do
constructor,
tgt ← target,
t ← get_local `H >>= infer_type,
is_def_eq tgt t, -- Fails at p a =?= p ?m_1
assumption
example (a : nat) : true :=
by do
t1 ← to_expr `(nat.succ a),
t2 ← to_expr `(a + 1),
is_def_eq t1 t2, -- Succeeds
constructor
|
1cb9eab9619311f3b45582d74c992182d6cf1e9b | 1a61aba1b67cddccce19532a9596efe44be4285f | /hott/types/sum.hlean | 1d08ea8f2ae25758a7d4e8d3468f0a281df80996 | [
"Apache-2.0"
] | permissive | eigengrau/lean | 07986a0f2548688c13ba36231f6cdbee82abf4c6 | f8a773be1112015e2d232661ce616d23f12874d0 | refs/heads/master | 1,610,939,198,566 | 1,441,352,386,000 | 1,441,352,494,000 | 41,903,576 | 0 | 0 | null | 1,441,352,210,000 | 1,441,352,210,000 | null | UTF-8 | Lean | false | false | 9,678 | hlean | /-
Copyright (c) 2015 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Floris van Doorn
Theorems about sums/coproducts/disjoint unions
-/
import .pi
open lift eq is_equiv equiv equiv.ops prod prod.ops is_trunc sigma bool
namespace sum
universe variables u v u' v'
variables {A : Type.{u}} {B : Type.{v}} (z z' : A + B) {P : A → Type.{u'}} {Q : A → Type.{v'}}
protected definition eta : sum.rec inl inr z = z :=
by induction z; all_goals reflexivity
protected definition code [unfold 3 4] : A + B → A + B → Type.{max u v}
| code (inl a) (inl a') := lift (a = a')
| code (inr b) (inr b') := lift (b = b')
| code _ _ := lift empty
protected definition decode [unfold 3 4] : Π(z z' : A + B), sum.code z z' → z = z'
| decode (inl a) (inl a') := λc, ap inl (down c)
| decode (inl a) (inr b') := λc, empty.elim (down c) _
| decode (inr b) (inl a') := λc, empty.elim (down c) _
| decode (inr b) (inr b') := λc, ap inr (down c)
variables {z z'}
protected definition encode [unfold 3 4 5] (p : z = z') : sum.code z z' :=
by induction p; induction z; all_goals exact up idp
variables (z z')
definition sum_eq_equiv [constructor] : (z = z') ≃ sum.code z z' :=
equiv.MK sum.encode
!sum.decode
abstract begin
intro c, induction z with a b, all_goals induction z' with a' b',
all_goals (esimp at *; induction c with c),
all_goals induction c, -- c either has type empty or a path
all_goals reflexivity
end end
abstract begin
intro p, induction p, induction z, all_goals reflexivity
end end
section
variables {a a' : A} {b b' : B}
definition eq_of_inl_eq_inl [unfold 5] (p : inl a = inl a' :> A + B) : a = a' :=
down (sum.encode p)
definition eq_of_inr_eq_inr [unfold 5] (p : inr b = inr b' :> A + B) : b = b' :=
down (sum.encode p)
definition empty_of_inl_eq_inr (p : inl a = inr b) : empty := down (sum.encode p)
definition empty_of_inr_eq_inl (p : inr b = inl a) : empty := down (sum.encode p)
/- Transport -/
definition sum_transport (p : a = a') (z : P a + Q a)
: p ▸ z = sum.rec (λa, inl (p ▸ a)) (λb, inr (p ▸ b)) z :=
by induction p; induction z; all_goals reflexivity
/- Pathovers -/
definition etao (p : a = a') (z : P a + Q a)
: z =[p] sum.rec (λa, inl (p ▸ a)) (λb, inr (p ▸ b)) z :=
by induction p; induction z; all_goals constructor
protected definition codeo (p : a = a') : P a + Q a → P a' + Q a' → Type.{max u' v'}
| codeo (inl x) (inl x') := lift.{u' v'} (x =[p] x')
| codeo (inr y) (inr y') := lift.{v' u'} (y =[p] y')
| codeo _ _ := lift empty
protected definition decodeo (p : a = a') : Π(z : P a + Q a) (z' : P a' + Q a'),
sum.codeo p z z' → z =[p] z'
| decodeo (inl x) (inl x') := λc, apo (λa, inl) (down c)
| decodeo (inl x) (inr y') := λc, empty.elim (down c) _
| decodeo (inr y) (inl x') := λc, empty.elim (down c) _
| decodeo (inr y) (inr y') := λc, apo (λa, inr) (down c)
variables {z z'}
protected definition encodeo {p : a = a'} {z : P a + Q a} {z' : P a' + Q a'} (q : z =[p] z')
: sum.codeo p z z' :=
by induction q; induction z; all_goals exact up idpo
variables (z z')
definition sum_pathover_equiv [constructor] (p : a = a') (z : P a + Q a) (z' : P a' + Q a')
: (z =[p] z') ≃ sum.codeo p z z' :=
equiv.MK sum.encodeo
!sum.decodeo
abstract begin
intro c, induction z with a b, all_goals induction z' with a' b',
all_goals (esimp at *; induction c with c),
all_goals induction c, -- c either has type empty or a pathover
all_goals reflexivity
end end
abstract begin
intro q, induction q, induction z, all_goals reflexivity
end end
end
/- Functorial action -/
variables {A' B' : Type} (f : A → A') (g : B → B')
definition sum_functor [unfold 7] : A + B → A' + B'
| sum_functor (inl a) := inl (f a)
| sum_functor (inr b) := inr (g b)
/- Equivalences -/
definition is_equiv_sum_functor [constructor] [Hf : is_equiv f] [Hg : is_equiv g]
: is_equiv (sum_functor f g) :=
adjointify (sum_functor f g)
(sum_functor f⁻¹ g⁻¹)
abstract begin
intro z, induction z,
all_goals (esimp; (apply ap inl | apply ap inr); apply right_inv)
end end
abstract begin
intro z, induction z,
all_goals (esimp; (apply ap inl | apply ap inr); apply right_inv)
end end
definition sum_equiv_sum_of_is_equiv [constructor] [Hf : is_equiv f] [Hg : is_equiv g]
: A + B ≃ A' + B' :=
equiv.mk _ (is_equiv_sum_functor f g)
definition sum_equiv_sum [constructor] (f : A ≃ A') (g : B ≃ B') : A + B ≃ A' + B' :=
equiv.mk _ (is_equiv_sum_functor f g)
definition sum_equiv_sum_left [constructor] (g : B ≃ B') : A + B ≃ A + B' :=
sum_equiv_sum equiv.refl g
definition sum_equiv_sum_right [constructor] (f : A ≃ A') : A + B ≃ A' + B :=
sum_equiv_sum f equiv.refl
definition flip [unfold 3] : A + B → B + A
| flip (inl a) := inr a
| flip (inr b) := inl b
definition sum_comm_equiv [constructor] (A B : Type) : A + B ≃ B + A :=
begin
fapply equiv.MK,
exact flip,
exact flip,
all_goals (intro z; induction z; all_goals reflexivity)
end
definition sum_assoc_equiv (A B C : Type) : A + (B + C) ≃ (A + B) + C :=
begin
fapply equiv.MK,
all_goals try (intro z; induction z with u v;
all_goals try induction u; all_goals try induction v),
all_goals try (repeat append (append (apply inl) (apply inr)) assumption; now),
all_goals reflexivity
end
definition sum_empty_equiv [constructor] (A : Type) : A + empty ≃ A :=
begin
fapply equiv.MK,
{ intro z, induction z, assumption, contradiction},
{ exact inl},
{ intro a, reflexivity},
{ intro z, induction z, reflexivity, contradiction}
end
definition empty_sum_equiv (A : Type) : empty + A ≃ A :=
!sum_comm_equiv ⬝e !sum_empty_equiv
/- universal property -/
definition sum_rec_unc {P : A + B → Type} (fg : (Πa, P (inl a)) × (Πb, P (inr b))) : Πz, P z :=
sum.rec fg.1 fg.2
definition is_equiv_sum_rec [constructor] (P : A + B → Type)
: is_equiv (sum_rec_unc : (Πa, P (inl a)) × (Πb, P (inr b)) → Πz, P z) :=
begin
apply adjointify sum_rec_unc (λf, (λa, f (inl a), λb, f (inr b))),
intro f, apply eq_of_homotopy, intro z, focus (induction z; all_goals reflexivity),
intro h, induction h with f g, reflexivity
end
definition equiv_sum_rec [constructor] (P : A + B → Type)
: (Πa, P (inl a)) × (Πb, P (inr b)) ≃ Πz, P z :=
equiv.mk _ !is_equiv_sum_rec
definition imp_prod_imp_equiv_sum_imp [constructor] (A B C : Type)
: (A → C) × (B → C) ≃ (A + B → C) :=
!equiv_sum_rec
/- truncatedness -/
variables (A B)
definition is_trunc_sum (n : trunc_index) [HA : is_trunc (n.+2) A] [HB : is_trunc (n.+2) B]
: is_trunc (n.+2) (A + B) :=
begin
apply is_trunc_succ_intro, intro z z',
apply is_trunc_equiv_closed_rev, apply sum_eq_equiv,
induction z with a b, all_goals induction z' with a' b', all_goals esimp,
all_goals exact _,
end
definition is_trunc_sum_excluded (n : trunc_index) [HA : is_trunc n A] [HB : is_trunc n B]
(H : A → B → empty) : is_trunc n (A + B) :=
begin
induction n with n IH,
{ exfalso, exact H !center !center},
{ clear IH, induction n with n IH,
{ apply is_hprop.mk, intros x y,
induction x, all_goals induction y, all_goals esimp,
all_goals try (exfalso;apply H;assumption;assumption), all_goals apply ap _ !is_hprop.elim},
{ apply is_trunc_sum}}
end
variable {B}
definition is_contr_sum_left [HA : is_contr A] (H : ¬B) : is_contr (A + B) :=
is_contr.mk (inl !center)
(λx, sum.rec_on x (λa, ap inl !center_eq) (λb, empty.elim (H b)))
/-
Sums are equivalent to dependent sigmas where the first component is a bool.
The current construction only works for A and B in the same universe.
If we need it for A and B in different universes, we need to insert some lifts.
-/
definition sum_of_sigma_bool {A B : Type.{u}} (v : Σ(b : bool), bool.rec A B b) : A + B :=
by induction v with b x; induction b; exact inl x; exact inr x
definition sigma_bool_of_sum {A B : Type.{u}} (z : A + B) : Σ(b : bool), bool.rec A B b :=
by induction z with a b; exact ⟨ff, a⟩; exact ⟨tt, b⟩
definition sum_equiv_sigma_bool [constructor] (A B : Type.{u})
: A + B ≃ Σ(b : bool), bool.rec A B b :=
equiv.MK sigma_bool_of_sum
sum_of_sigma_bool
begin intro v, induction v with b x, induction b, all_goals reflexivity end
begin intro z, induction z with a b, all_goals reflexivity end
end sum
namespace decidable
open sum pi
definition decidable_equiv (A : Type) : decidable A ≃ A + ¬A :=
begin
fapply equiv.MK:intro a;induction a:try (constructor;assumption;now),
all_goals reflexivity
end
definition is_trunc_decidable (A : Type) (n : trunc_index) [H : is_trunc n A] :
is_trunc n (decidable A) :=
begin
apply is_trunc_equiv_closed_rev,
apply decidable_equiv,
induction n with n IH,
{ apply is_contr_sum_left, exact λna, na !center},
{ apply is_trunc_sum_excluded, exact λa na, na a}
end
end decidable
|
db94824272ba6cdf7cdc0e39ff0e080b85509072 | a047a4718edfa935d17231e9e6ecec8c7b701e05 | /src/topology/algebra/infinite_sum.lean | cd38408987a5b1de71facf9c9009d9c53cce037e | [
"Apache-2.0"
] | permissive | utensil-contrib/mathlib | bae0c9fafe5e2bdb516efc89d6f8c1502ecc9767 | b91909e77e219098a2f8cc031f89d595fe274bd2 | refs/heads/master | 1,668,048,976,965 | 1,592,442,701,000 | 1,592,442,701,000 | 273,197,855 | 0 | 0 | null | 1,592,472,812,000 | 1,592,472,811,000 | null | UTF-8 | Lean | false | false | 35,744 | 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
Infinite sum over a topological monoid
This sum is known as unconditionally convergent, as it sums to the same value under all possible
permutations. For Euclidean spaces (finite dimensional Banach spaces) this is equivalent to absolute
convergence.
Note: There are summable sequences which are not unconditionally convergent! The other way holds
generally, see `has_sum.tendsto_sum_nat`.
Reference:
* Bourbaki: General Topology (1995), Chapter 3 §5 (Infinite sums in commutative groups)
-/
import topology.instances.real
noncomputable theory
open finset filter function classical
open_locale topological_space classical big_operators
variables {α : Type*} {β : Type*} {γ : Type*}
section has_sum
variables [add_comm_monoid α] [topological_space α]
/-- Infinite sum on a topological monoid
The `at_top` filter on `finset α` is the limit of all finite sets towards the entire type. So we sum
up bigger and bigger sets. This sum operation is still invariant under reordering, and a absolute
sum operator.
This is based on Mario Carneiro's infinite sum in Metamath.
For the definition or many statements, α does not need to be a topological monoid. We only add
this assumption later, for the lemmas where it is relevant.
-/
def has_sum (f : β → α) (a : α) : Prop := tendsto (λs:finset β, ∑ b in s, f b) at_top (𝓝 a)
/-- `summable f` means that `f` has some (infinite) sum. Use `tsum` to get the value. -/
def summable (f : β → α) : Prop := ∃a, has_sum f a
/-- `tsum f` is the sum of `f` it exists, or 0 otherwise -/
def tsum (f : β → α) := if h : summable f then classical.some h else 0
notation `∑'` binders `, ` r:(scoped f, tsum f) := r
variables {f g : β → α} {a b : α} {s : finset β}
lemma summable.has_sum (ha : summable f) : has_sum f (∑'b, f b) :=
by simp [ha, tsum]; exact some_spec ha
lemma has_sum.summable (h : has_sum f a) : summable f := ⟨a, h⟩
/-- Constant zero function has sum `0` -/
lemma has_sum_zero : has_sum (λb, 0 : β → α) 0 :=
by simp [has_sum, tendsto_const_nhds]
lemma summable_zero : summable (λb, 0 : β → α) := has_sum_zero.summable
lemma tsum_eq_zero_of_not_summable (h : ¬ summable f) : (∑'b, f b) = 0 :=
by simp [tsum, h]
/-- If a function `f` vanishes outside of a finite set `s`, then it `has_sum` `∑ b in s, f b`. -/
lemma has_sum_sum_of_ne_finset_zero (hf : ∀b∉s, f b = 0) : has_sum f (∑ b in s, f b) :=
tendsto_infi' s $ tendsto.congr'
(assume t (ht : s ⊆ t), show ∑ b in s, f b = ∑ b in t, f b, from sum_subset ht $ assume x _, hf _)
tendsto_const_nhds
lemma has_sum_fintype [fintype β] (f : β → α) : has_sum f (∑ b, f b) :=
has_sum_sum_of_ne_finset_zero $ λ a h, h.elim (mem_univ _)
lemma summable_sum_of_ne_finset_zero (hf : ∀b∉s, f b = 0) : summable f :=
(has_sum_sum_of_ne_finset_zero hf).summable
lemma has_sum_single {f : β → α} (b : β) (hf : ∀b' ≠ b, f b' = 0) :
has_sum f (f b) :=
suffices has_sum f (∑ b' in {b}, f b'),
by simpa using this,
has_sum_sum_of_ne_finset_zero $ by simpa [hf]
lemma has_sum_ite_eq (b : β) (a : α) : has_sum (λb', if b' = b then a else 0) a :=
begin
convert has_sum_single b _,
{ exact (if_pos rfl).symm },
assume b' hb',
exact if_neg hb'
end
lemma has_sum_of_iso {j : γ → β} {i : β → γ}
(hf : has_sum f a) (h₁ : ∀x, i (j x) = x) (h₂ : ∀x, j (i x) = x) : has_sum (f ∘ j) a :=
have ∀x y, j x = j y → x = y,
from assume x y h,
have i (j x) = i (j y), by rw [h],
by rwa [h₁, h₁] at this,
have (λs:finset γ, ∑ x in s, f (j x)) = (λs:finset β, ∑ b in s, f b) ∘ (λs:finset γ, s.image j),
from funext $ assume s, (sum_image $ assume x _ y _, this x y).symm,
show tendsto (λs:finset γ, ∑ x in s, f (j x)) at_top (𝓝 a),
by rw [this]; apply hf.comp (tendsto_finset_image_at_top_at_top h₂)
lemma has_sum_iff_has_sum_of_iso {j : γ → β} (i : β → γ)
(h₁ : ∀x, i (j x) = x) (h₂ : ∀x, j (i x) = x) :
has_sum (f ∘ j) a ↔ has_sum f a :=
iff.intro
(assume hfj,
have has_sum ((f ∘ j) ∘ i) a, from has_sum_of_iso hfj h₂ h₁,
by simp [(∘), h₂] at this; assumption)
(assume hf, has_sum_of_iso hf h₁ h₂)
lemma equiv.has_sum_iff (e : γ ≃ β) :
has_sum (f ∘ e) a ↔ has_sum f a :=
has_sum_iff_has_sum_of_iso e.symm e.left_inv e.right_inv
lemma equiv.summable_iff (e : γ ≃ β) :
summable (f ∘ e) ↔ summable f :=
⟨λ H, (e.has_sum_iff.1 H.has_sum).summable, λ H, (e.has_sum_iff.2 H.has_sum).summable⟩
lemma has_sum_hom (g : α → γ) [add_comm_monoid γ] [topological_space γ]
[is_add_monoid_hom g] (h₃ : continuous g) (hf : has_sum f a) :
has_sum (g ∘ f) (g a) :=
have (λs:finset β, ∑ b in s, g (f b)) = g ∘ (λs:finset β, ∑ b in s, f b),
from funext $ assume s, s.sum_hom g,
show tendsto (λs:finset β, ∑ b in s, g (f b)) at_top (𝓝 (g a)),
by rw [this]; exact tendsto.comp (continuous_iff_continuous_at.mp h₃ a) hf
/-- If `f : ℕ → α` has sum `a`, then the partial sums `∑_{i=0}^{n-1} f i` converge to `a`. -/
lemma has_sum.tendsto_sum_nat {f : ℕ → α} (h : has_sum f a) :
tendsto (λn:ℕ, ∑ i in range n, f i) at_top (𝓝 a) :=
@tendsto.comp _ _ _ finset.range (λ s : finset ℕ, ∑ n in s, f n) _ _ _ h tendsto_finset_range
lemma has_sum_unique {a₁ a₂ : α} [t2_space α] : has_sum f a₁ → has_sum f a₂ → a₁ = a₂ :=
tendsto_nhds_unique at_top_ne_bot
lemma has_sum_iff_tendsto_nat_of_summable [t2_space α] {f : ℕ → α} {a : α} (hf : summable f) :
has_sum f a ↔ tendsto (λn:ℕ, ∑ i in range n, f i) at_top (𝓝 a) :=
begin
refine ⟨λ h, h.tendsto_sum_nat, λ h, _⟩,
rw tendsto_nhds_unique at_top_ne_bot h hf.has_sum.tendsto_sum_nat,
exact hf.has_sum
end
variable [topological_add_monoid α]
lemma has_sum.add (hf : has_sum f a) (hg : has_sum g b) : has_sum (λb, f b + g b) (a + b) :=
by simp [has_sum, sum_add_distrib]; exact hf.add hg
lemma summable.add (hf : summable f) (hg : summable g) : summable (λb, f b + g b) :=
(hf.has_sum.add hg.has_sum).summable
lemma has_sum_sum {f : γ → β → α} {a : γ → α} {s : finset γ} :
(∀i∈s, has_sum (f i) (a i)) → has_sum (λb, ∑ i in s, f i b) (∑ i in s, a i) :=
finset.induction_on s (by simp [has_sum_zero]) (by simp [has_sum.add] {contextual := tt})
lemma summable_sum {f : γ → β → α} {s : finset γ} (hf : ∀i∈s, summable (f i)) :
summable (λb, ∑ i in s, f i b) :=
(has_sum_sum $ assume i hi, (hf i hi).has_sum).summable
lemma has_sum.sigma [regular_space α] {γ : β → Type*} {f : (Σ b:β, γ b) → α} {g : β → α} {a : α}
(ha : has_sum f a) (hf : ∀b, has_sum (λc, f ⟨b, c⟩) (g b)) : has_sum g a :=
assume s' hs',
let
⟨s, hs, hss', hsc⟩ := nhds_is_closed hs',
⟨u, hu⟩ := mem_at_top_sets.mp $ ha hs,
fsts := u.image sigma.fst,
snds := λb, u.bind (λp, (if h : p.1 = b then {cast (congr_arg γ h) p.2} else ∅ : finset (γ b)))
in
have u_subset : u ⊆ fsts.sigma snds,
from subset_iff.mpr $ assume ⟨b, c⟩ hu,
have hb : b ∈ fsts, from finset.mem_image.mpr ⟨_, hu, rfl⟩,
have hc : c ∈ snds b, from mem_bind.mpr ⟨_, hu, by simp; refl⟩,
by simp [mem_sigma, hb, hc] ,
mem_at_top_sets.mpr $ exists.intro fsts $ assume bs (hbs : fsts ⊆ bs),
have h : ∀cs : Π b ∈ bs, finset (γ b),
((⋂b (hb : b ∈ bs), (λp:Πb, finset (γ b), p b) ⁻¹' {cs' | cs b hb ⊆ cs' }) ∩
(λp, ∑ b in bs, ∑ c in p b, f ⟨b, c⟩) ⁻¹' s).nonempty,
from assume cs,
let cs' := λb, (if h : b ∈ bs then cs b h else ∅) ∪ snds b in
have sum_eq : ∑ b in bs, ∑ c in cs' b, f ⟨b, c⟩ = ∑ x in bs.sigma cs', f x,
from sum_sigma.symm,
have ∑ x in bs.sigma cs', f x ∈ s,
from hu _ $ finset.subset.trans u_subset $ sigma_mono hbs $
assume b, @finset.subset_union_right (γ b) _ _ _,
exists.intro cs' $
by simp [sum_eq, this]; { intros b hb, simp [cs', hb, finset.subset_union_left] },
have tendsto (λp:(Πb:β, finset (γ b)), ∑ b in bs, ∑ c in p b, f ⟨b, c⟩)
(⨅b (h : b ∈ bs), at_top.comap (λp, p b)) (𝓝 (∑ b in bs, g b)),
from tendsto_finset_sum bs $
assume c hc, tendsto_infi' c $ tendsto_infi' hc $ by apply tendsto.comp (hf c) tendsto_comap,
have ∑ b in bs, g b ∈ s,
from mem_of_closed_of_tendsto' this hsc $ forall_sets_nonempty_iff_ne_bot.mp $
begin
simp only [mem_inf_sets, exists_imp_distrib, forall_and_distrib, and_imp,
filter.mem_infi_sets_finset, mem_comap_sets, mem_at_top_sets, and_comm,
mem_principal_sets, set.preimage_subset_iff, exists_prop, skolem],
intros s₁ s₂ s₃ hs₁ hs₃ p hs₂ p' hp cs hp',
have : (⋂b (h : b ∈ bs), (λp:(Πb, finset (γ b)), p b) ⁻¹' {cs' | cs b h ⊆ cs' }) ≤ (⨅b∈bs, p b),
from (infi_le_infi $ assume b, infi_le_infi $ assume hb,
le_trans (set.preimage_mono $ hp' b hb) (hp b hb)),
exact (h _).mono (set.subset.trans (set.inter_subset_inter (le_trans this hs₂) hs₃) hs₁)
end,
hss' this
lemma summable.sigma [regular_space α] {γ : β → Type*} {f : (Σb:β, γ b) → α}
(ha : summable f) (hf : ∀b, summable (λc, f ⟨b, c⟩)) : summable (λb, ∑'c, f ⟨b, c⟩) :=
(ha.has_sum.sigma (assume b, (hf b).has_sum)).summable
lemma has_sum.sigma_of_has_sum [regular_space α] {γ : β → Type*} {f : (Σ b:β, γ b) → α} {g : β → α} {a : α}
(ha : has_sum g a) (hf : ∀b, has_sum (λc, f ⟨b, c⟩) (g b)) (hf' : summable f) : has_sum f a :=
by simpa [has_sum_unique (hf'.has_sum.sigma hf) ha] using hf'.has_sum
end has_sum
section has_sum_iff_has_sum_of_iso_ne_zero
variables [add_comm_monoid α] [topological_space α]
variables {f : β → α} {g : γ → α} {a : α}
lemma has_sum.has_sum_of_sum_eq
(h_eq : ∀u:finset γ, ∃v:finset β, ∀v', v ⊆ v' → ∃u', u ⊆ u' ∧ ∑ x in u', g x = ∑ b in v', f b)
(hf : has_sum g a) : has_sum f a :=
suffices at_top.map (λs:finset β, ∑ b in s, f b) ≤ at_top.map (λs:finset γ, ∑ x in s, g x),
from le_trans this hf,
by rw [map_at_top_eq, map_at_top_eq];
from (le_infi $ assume b, let ⟨v, hv⟩ := h_eq b in infi_le_of_le v $
by simp [set.image_subset_iff]; exact hv)
lemma has_sum_iff_has_sum
(h₁ : ∀u:finset γ, ∃v:finset β, ∀v', v ⊆ v' → ∃u', u ⊆ u' ∧ ∑ x in u', g x = ∑ b in v', f b)
(h₂ : ∀v:finset β, ∃u:finset γ, ∀u', u ⊆ u' → ∃v', v ⊆ v' ∧ ∑ b in v', f b = ∑ x in u', g x) :
has_sum f a ↔ has_sum g a :=
⟨has_sum.has_sum_of_sum_eq h₂, has_sum.has_sum_of_sum_eq h₁⟩
variables
(i : Π⦃c⦄, g c ≠ 0 → β) (hi : ∀⦃c⦄ (h : g c ≠ 0), f (i h) ≠ 0)
(j : Π⦃b⦄, f b ≠ 0 → γ) (hj : ∀⦃b⦄ (h : f b ≠ 0), g (j h) ≠ 0)
(hji : ∀⦃c⦄ (h : g c ≠ 0), j (hi h) = c)
(hij : ∀⦃b⦄ (h : f b ≠ 0), i (hj h) = b)
(hgj : ∀⦃b⦄ (h : f b ≠ 0), g (j h) = f b)
include hi hj hji hij hgj
-- FIXME this causes a deterministic timeout with `-T50000`
lemma has_sum.has_sum_ne_zero : has_sum g a → has_sum f a :=
have j_inj : ∀x y (hx : f x ≠ 0) (hy : f y ≠ 0), (j hx = j hy ↔ x = y),
from assume x y hx hy,
⟨assume h,
have i (hj hx) = i (hj hy), by simp [h],
by rwa [hij, hij] at this; assumption,
by simp {contextual := tt}⟩,
let ii : finset γ → finset β := λu, u.bind $ λc, if h : g c = 0 then ∅ else {i h} in
let jj : finset β → finset γ := λv, v.bind $ λb, if h : f b = 0 then ∅ else {j h} in
has_sum.has_sum_of_sum_eq $ assume u, exists.intro (ii u) $
assume v hv, exists.intro (u ∪ jj v) $ and.intro (subset_union_left _ _) $
have ∀c:γ, c ∈ u ∪ jj v → c ∉ jj v → g c = 0,
from assume c hc hnc, classical.by_contradiction $ assume h : g c ≠ 0,
have c ∈ u,
from (finset.mem_union.1 hc).resolve_right hnc,
have i h ∈ v,
from hv $ by simp [mem_bind]; existsi c; simp [h, this],
have j (hi h) ∈ jj v,
by simp [mem_bind]; existsi i h; simp [h, hi, this],
by rw [hji h] at this; exact hnc this,
calc ∑ x in u ∪ jj v, g x = ∑ x in jj v, g x : (sum_subset (subset_union_right _ _) this).symm
... = ∑ x in v, _ : sum_bind $ by intros x _ y _ _; by_cases f x = 0; by_cases f y = 0; simp [*]; cc
... = ∑ x in v, f x : sum_congr rfl $ by intros x hx; by_cases f x = 0; simp [*]
lemma has_sum_iff_has_sum_of_ne_zero : has_sum f a ↔ has_sum g a :=
iff.intro
(has_sum.has_sum_ne_zero j hj i hi hij hji $ assume b hb, by rw [←hgj (hi _), hji])
(has_sum.has_sum_ne_zero i hi j hj hji hij hgj)
lemma summable_iff_summable_ne_zero : summable g ↔ summable f :=
exists_congr $
assume a, has_sum_iff_has_sum_of_ne_zero j hj i hi hij hji $
assume b hb, by rw [←hgj (hi _), hji]
end has_sum_iff_has_sum_of_iso_ne_zero
section has_sum_iff_has_sum_of_bij_ne_zero
variables [add_comm_monoid α] [topological_space α]
variables {f : β → α} {g : γ → α} {a : α}
(i : Π⦃c⦄, g c ≠ 0 → β)
(h₁ : ∀⦃c₁ c₂⦄ (h₁ : g c₁ ≠ 0) (h₂ : g c₂ ≠ 0), i h₁ = i h₂ → c₁ = c₂)
(h₂ : ∀⦃b⦄, f b ≠ 0 → ∃c (h : g c ≠ 0), i h = b)
(h₃ : ∀⦃c⦄ (h : g c ≠ 0), f (i h) = g c)
include i h₁ h₂ h₃
lemma has_sum_iff_has_sum_of_ne_zero_bij : has_sum f a ↔ has_sum g a :=
have hi : ∀⦃c⦄ (h : g c ≠ 0), f (i h) ≠ 0,
from assume c h, by simp [h₃, h],
let j : Π⦃b⦄, f b ≠ 0 → γ := λb h, some $ h₂ h in
have hj : ∀⦃b⦄ (h : f b ≠ 0), ∃(h : g (j h) ≠ 0), i h = b,
from assume b h, some_spec $ h₂ h,
have hj₁ : ∀⦃b⦄ (h : f b ≠ 0), g (j h) ≠ 0,
from assume b h, let ⟨h₁, _⟩ := hj h in h₁,
have hj₂ : ∀⦃b⦄ (h : f b ≠ 0), i (hj₁ h) = b,
from assume b h, let ⟨h₁, h₂⟩ := hj h in h₂,
has_sum_iff_has_sum_of_ne_zero i hi j hj₁
(assume c h, h₁ (hj₁ _) h $ hj₂ _) hj₂ (assume b h, by rw [←h₃ (hj₁ _), hj₂])
lemma summable_iff_summable_ne_zero_bij : summable f ↔ summable g :=
exists_congr $
assume a, has_sum_iff_has_sum_of_ne_zero_bij @i h₁ h₂ h₃
end has_sum_iff_has_sum_of_bij_ne_zero
section subtype
variables [add_comm_monoid α] [topological_space α] {s : finset β} {f : β → α} {a : α}
lemma has_sum_subtype_iff_of_eq_zero (h : ∀ x ∈ s, f x = 0) :
has_sum (λ b : {b // b ∉ s}, f b) a ↔ has_sum f a :=
begin
symmetry,
apply has_sum_iff_has_sum_of_ne_zero_bij (λ (b : {b // b ∉ s}) hb, (b : β)),
{ exact λ c₁ c₂ h₁ h₂ H, subtype.eq H },
{ assume b hb,
have : b ∉ s := λ H, hb (h b H),
exact ⟨⟨b, this⟩, hb, rfl⟩ },
{ dsimp, simp }
end
end subtype
section tsum
variables [add_comm_monoid α] [topological_space α] [t2_space α]
variables {f g : β → α} {a a₁ a₂ : α}
lemma tsum_eq_has_sum (ha : has_sum f a) : (∑'b, f b) = a :=
has_sum_unique (summable.has_sum ⟨a, ha⟩) ha
lemma summable.has_sum_iff (h : summable f) : has_sum f a ↔ (∑'b, f b) = a :=
iff.intro tsum_eq_has_sum (assume eq, eq ▸ h.has_sum)
@[simp] lemma tsum_zero : (∑'b:β, 0:α) = 0 := tsum_eq_has_sum has_sum_zero
lemma tsum_eq_sum {f : β → α} {s : finset β} (hf : ∀b∉s, f b = 0) :
(∑'b, f b) = ∑ b in s, f b :=
tsum_eq_has_sum $ has_sum_sum_of_ne_finset_zero hf
lemma tsum_fintype [fintype β] (f : β → α) : (∑'b, f b) = ∑ b, f b :=
tsum_eq_has_sum $ has_sum_fintype f
lemma tsum_eq_single {f : β → α} (b : β) (hf : ∀b' ≠ b, f b' = 0) :
(∑'b, f b) = f b :=
tsum_eq_has_sum $ has_sum_single b hf
@[simp] lemma tsum_ite_eq (b : β) (a : α) : (∑'b', if b' = b then a else 0) = a :=
tsum_eq_has_sum (has_sum_ite_eq b a)
lemma tsum_eq_tsum_of_has_sum_iff_has_sum {f : β → α} {g : γ → α}
(h : ∀{a}, has_sum f a ↔ has_sum g a) : (∑'b, f b) = (∑'c, g c) :=
by_cases
(assume : ∃a, has_sum f a,
let ⟨a, hfa⟩ := this in
have hga : has_sum g a, from h.mp hfa,
by rw [tsum_eq_has_sum hfa, tsum_eq_has_sum hga])
(assume hf : ¬ summable f,
have hg : ¬ summable g, from assume ⟨a, hga⟩, hf ⟨a, h.mpr hga⟩,
by simp [tsum, hf, hg])
lemma tsum_eq_tsum_of_ne_zero {f : β → α} {g : γ → α}
(i : Π⦃c⦄, g c ≠ 0 → β) (hi : ∀⦃c⦄ (h : g c ≠ 0), f (i h) ≠ 0)
(j : Π⦃b⦄, f b ≠ 0 → γ) (hj : ∀⦃b⦄ (h : f b ≠ 0), g (j h) ≠ 0)
(hji : ∀⦃c⦄ (h : g c ≠ 0), j (hi h) = c)
(hij : ∀⦃b⦄ (h : f b ≠ 0), i (hj h) = b)
(hgj : ∀⦃b⦄ (h : f b ≠ 0), g (j h) = f b) :
(∑'i, f i) = (∑'j, g j) :=
tsum_eq_tsum_of_has_sum_iff_has_sum $ assume a, has_sum_iff_has_sum_of_ne_zero i hi j hj hji hij hgj
lemma tsum_eq_tsum_of_ne_zero_bij {f : β → α} {g : γ → α}
(i : Π⦃c⦄, g c ≠ 0 → β)
(h₁ : ∀⦃c₁ c₂⦄ (h₁ : g c₁ ≠ 0) (h₂ : g c₂ ≠ 0), i h₁ = i h₂ → c₁ = c₂)
(h₂ : ∀⦃b⦄, f b ≠ 0 → ∃c (h : g c ≠ 0), i h = b)
(h₃ : ∀⦃c⦄ (h : g c ≠ 0), f (i h) = g c) :
(∑'i, f i) = (∑'j, g j) :=
tsum_eq_tsum_of_has_sum_iff_has_sum $ assume a, has_sum_iff_has_sum_of_ne_zero_bij i h₁ h₂ h₃
lemma tsum_eq_tsum_of_iso (j : γ → β) (i : β → γ)
(h₁ : ∀x, i (j x) = x) (h₂ : ∀x, j (i x) = x) :
(∑'c, f (j c)) = (∑'b, f b) :=
tsum_eq_tsum_of_has_sum_iff_has_sum $ assume a, has_sum_iff_has_sum_of_iso i h₁ h₂
lemma tsum_equiv (j : γ ≃ β) : (∑'c, f (j c)) = (∑'b, f b) :=
tsum_eq_tsum_of_iso j j.symm (by simp) (by simp)
variable [topological_add_monoid α]
lemma tsum_add (hf : summable f) (hg : summable g) : (∑'b, f b + g b) = (∑'b, f b) + (∑'b, g b) :=
tsum_eq_has_sum $ hf.has_sum.add hg.has_sum
lemma tsum_sum {f : γ → β → α} {s : finset γ} (hf : ∀i∈s, summable (f i)) :
(∑'b, ∑ i in s, f i b) = ∑ i in s, ∑'b, f i b :=
tsum_eq_has_sum $ has_sum_sum $ assume i hi, (hf i hi).has_sum
lemma tsum_sigma [regular_space α] {γ : β → Type*} {f : (Σb:β, γ b) → α}
(h₁ : ∀b, summable (λc, f ⟨b, c⟩)) (h₂ : summable f) : (∑'p, f p) = (∑'b c, f ⟨b, c⟩) :=
(tsum_eq_has_sum $ h₂.has_sum.sigma (assume b, (h₁ b).has_sum)).symm
end tsum
section topological_group
variables [add_comm_group α] [topological_space α] [topological_add_group α]
variables {f g : β → α} {a a₁ a₂ : α}
lemma has_sum.neg : has_sum f a → has_sum (λb, - f b) (- a) :=
has_sum_hom has_neg.neg continuous_neg
lemma summable.neg (hf : summable f) : summable (λb, - f b) :=
hf.has_sum.neg.summable
lemma has_sum.sub (hf : has_sum f a₁) (hg : has_sum g a₂) : has_sum (λb, f b - g b) (a₁ - a₂) :=
by { simp [sub_eq_add_neg], exact hf.add hg.neg }
lemma summable.sub (hf : summable f) (hg : summable g) : summable (λb, f b - g b) :=
(hf.has_sum.sub hg.has_sum).summable
section tsum
variables [t2_space α]
lemma tsum_neg (hf : summable f) : (∑'b, - f b) = - (∑'b, f b) :=
tsum_eq_has_sum $ hf.has_sum.neg
lemma tsum_sub (hf : summable f) (hg : summable g) : (∑'b, f b - g b) = (∑'b, f b) - (∑'b, g b) :=
tsum_eq_has_sum $ hf.has_sum.sub hg.has_sum
lemma tsum_eq_zero_add {f : ℕ → α} (hf : summable f) : (∑'b, f b) = f 0 + (∑'b, f (b + 1)) :=
begin
let f₁ : ℕ → α := λ n, nat.rec (f 0) (λ _ _, 0) n,
let f₂ : ℕ → α := λ n, nat.rec 0 (λ k _, f (k+1)) n,
have : f = λ n, f₁ n + f₂ n, { ext n, symmetry, cases n, apply add_zero, apply zero_add },
have hf₁ : summable f₁,
{ fapply summable_sum_of_ne_finset_zero,
{ exact {0} },
{ rintros (_ | n) hn,
{ exfalso,
apply hn,
apply finset.mem_singleton_self },
{ refl } } },
have hf₂ : summable f₂,
{ have : f₂ = λ n, f n - f₁ n, ext, rw [eq_sub_iff_add_eq', this],
rw [this], apply hf.sub hf₁ },
conv_lhs { rw [this] },
rw [tsum_add hf₁ hf₂, tsum_eq_single 0],
{ congr' 1,
fapply tsum_eq_tsum_of_ne_zero_bij (λ n _, n + 1),
{ intros _ _ _ _, exact nat.succ_inj },
{ rintros (_ | n) h,
{ contradiction },
{ exact ⟨n, h, rfl⟩ } },
{ intros, refl },
{ apply_instance } },
{ rintros (_ | n) hn,
{ contradiction },
{ refl } },
{ apply_instance }
end
end tsum
/-!
### Sums on subtypes
If `s` is a finset of `α`, we show that the summability of `f` in the whole space and on the subtype
`univ - s` are equivalent, and relate their sums. For a function defined on `ℕ`, we deduce the
formula `(∑ i in range k, f i) + (∑' i, f (i + k)) = (∑' i, f i)`, in `sum_add_tsum_nat_add`.
-/
section subtype
variables {s : finset β}
lemma has_sum_subtype_iff :
has_sum (λ b : {b // b ∉ s}, f b) a ↔ has_sum f (a + ∑ b in s, f b) :=
begin
let gs := λ b, if b ∈ s then f b else 0,
let g := λ b, if b ∉ s then f b else 0,
have f_sum_iff : has_sum f (a + ∑ b in s, f b) = has_sum (λ b, g b + gs b) (a + ∑ b in s, f b),
{ congr,
ext i,
simp [gs, g],
split_ifs;
simp },
have g_zero : ∀ b ∈ s, g b = 0,
{ assume b hb,
dsimp [g],
split_ifs,
refl },
have gs_sum : has_sum gs (∑ b in s, f b),
{ have : (∑ b in s, f b) = (∑ b in s, gs b),
{ apply sum_congr rfl (λ b hb, _),
dsimp [gs],
split_ifs,
{ refl },
{ exact false.elim (h hb) } },
rw this,
apply has_sum_sum_of_ne_finset_zero (λ b hb, _),
dsimp [gs],
split_ifs,
{ exact false.elim (hb h) },
{ refl } },
have : (λ b : {b // b ∉ s}, f b) = (λ b : {b // b ∉ s}, g b),
{ ext i,
simp [g],
split_ifs,
{ exact false.elim (i.2 h) },
{ refl } },
rw [this, has_sum_subtype_iff_of_eq_zero g_zero, f_sum_iff],
exact ⟨λ H, H.add gs_sum, λ H, by simpa using H.sub gs_sum⟩,
end
lemma has_sum_subtype_iff' :
has_sum (λ b : {b // b ∉ s}, f b) (a - ∑ b in s, f b) ↔ has_sum f a :=
by simp [has_sum_subtype_iff]
lemma summable_subtype_iff (s : finset β):
summable (λ b : {b // b ∉ s}, f b) ↔ summable f :=
⟨λ H, (has_sum_subtype_iff.1 H.has_sum).summable, λ H, (has_sum_subtype_iff'.2 H.has_sum).summable⟩
lemma sum_add_tsum_subtype [t2_space α] (s : finset β) (h : summable f) :
(∑ b in s, f b) + (∑' (b : {b // b ∉ s}), f b) = (∑' b, f b) :=
by simpa [add_comm] using
has_sum_unique (has_sum_subtype_iff.1 ((summable_subtype_iff s).2 h).has_sum) h.has_sum
lemma summable_nat_add_iff {f : ℕ → α} (k : ℕ) : summable (λ n, f (n + k)) ↔ summable f :=
begin
refine iff.trans _ (summable_subtype_iff (range k)),
rw [← (not_mem_range_equiv k).symm.summable_iff],
refl
end
lemma has_sum_nat_add_iff {f : ℕ → α} (k : ℕ) {a : α} :
has_sum (λ n, f (n + k)) a ↔ has_sum f (a + ∑ i in range k, f i) :=
begin
refine iff.trans _ has_sum_subtype_iff,
rw [← (not_mem_range_equiv k).symm.has_sum_iff],
refl
end
lemma has_sum_nat_add_iff' {f : ℕ → α} (k : ℕ) {a : α} :
has_sum (λ n, f (n + k)) (a - ∑ i in range k, f i) ↔ has_sum f a :=
by simp [has_sum_nat_add_iff]
lemma sum_add_tsum_nat_add [t2_space α] {f : ℕ → α} (k : ℕ) (h : summable f) :
(∑ i in range k, f i) + (∑' i, f (i + k)) = (∑' i, f i) :=
by simpa [add_comm] using
has_sum_unique ((has_sum_nat_add_iff k).1 ((summable_nat_add_iff k).2 h).has_sum) h.has_sum
end subtype
end topological_group
section topological_semiring
variables [semiring α] [topological_space α] [topological_semiring α]
variables {f g : β → α} {a a₁ a₂ : α}
lemma has_sum.mul_left (a₂) : has_sum f a₁ → has_sum (λb, a₂ * f b) (a₂ * a₁) :=
has_sum_hom _ (continuous_const.mul continuous_id)
lemma has_sum.mul_right (a₂) (hf : has_sum f a₁) : has_sum (λb, f b * a₂) (a₁ * a₂) :=
@has_sum_hom _ _ _ _ _ f a₁ (λa, a * a₂) _ _ _
(continuous_id.mul continuous_const) hf
lemma summable.mul_left (a) (hf : summable f) : summable (λb, a * f b) :=
(hf.has_sum.mul_left _).summable
lemma summable.mul_right (a) (hf : summable f) : summable (λb, f b * a) :=
(hf.has_sum.mul_right _).summable
section tsum
variables [t2_space α]
lemma tsum_mul_left (a) (hf : summable f) : (∑'b, a * f b) = a * (∑'b, f b) :=
tsum_eq_has_sum $ hf.has_sum.mul_left _
lemma tsum_mul_right (a) (hf : summable f) : (∑'b, f b * a) = (∑'b, f b) * a :=
tsum_eq_has_sum $ hf.has_sum.mul_right _
end tsum
end topological_semiring
section field
variables [field α] [topological_space α] [topological_semiring α]
{f g : β → α} {a a₁ a₂ : α}
lemma has_sum_mul_left_iff (h : a₂ ≠ 0) : has_sum f a₁ ↔ has_sum (λb, a₂ * f b) (a₂ * a₁) :=
⟨has_sum.mul_left _, λ H, by simpa [← mul_assoc, inv_mul_cancel h] using H.mul_left a₂⁻¹⟩
lemma has_sum_mul_right_iff (h : a₂ ≠ 0) : has_sum f a₁ ↔ has_sum (λb, f b * a₂) (a₁ * a₂) :=
by { simp only [mul_comm _ a₂], exact has_sum_mul_left_iff h }
lemma summable_mul_left_iff (h : a ≠ 0) : summable f ↔ summable (λb, a * f b) :=
⟨λ H, H.mul_left _, λ H, by simpa [← mul_assoc, inv_mul_cancel h] using H.mul_left a⁻¹⟩
lemma summable_mul_right_iff (h : a ≠ 0) : summable f ↔ summable (λb, f b * a) :=
by { simp only [mul_comm _ a], exact summable_mul_left_iff h }
end field
section order_topology
variables [ordered_add_comm_monoid α] [topological_space α] [order_closed_topology α]
variables {f g : β → α} {a a₁ a₂ : α}
lemma has_sum_le (h : ∀b, f b ≤ g b) (hf : has_sum f a₁) (hg : has_sum g a₂) : a₁ ≤ a₂ :=
le_of_tendsto_of_tendsto' at_top_ne_bot hf hg $ assume s, sum_le_sum $ assume b _, h b
lemma has_sum_le_inj {g : γ → α} (i : β → γ) (hi : injective i) (hs : ∀c∉set.range i, 0 ≤ g c)
(h : ∀b, f b ≤ g (i b)) (hf : has_sum f a₁) (hg : has_sum g a₂) : a₁ ≤ a₂ :=
have has_sum (λc, (partial_inv i c).cases_on' 0 f) a₁,
begin
refine (has_sum_iff_has_sum_of_ne_zero_bij (λb _, i b) _ _ _).2 hf,
{ assume c₁ c₂ h₁ h₂ eq, exact hi eq },
{ assume c hc,
cases eq : partial_inv i c with b; rw eq at hc,
{ contradiction },
{ rw [partial_inv_of_injective hi] at eq,
exact ⟨b, hc, eq⟩ } },
{ assume c hc, rw [partial_inv_left hi, option.cases_on'] }
end,
begin
refine has_sum_le (assume c, _) this hg,
by_cases c ∈ set.range i,
{ rcases h with ⟨b, rfl⟩,
rw [partial_inv_left hi, option.cases_on'],
exact h _ },
{ have : partial_inv i c = none := dif_neg h,
rw [this, option.cases_on'],
exact hs _ h }
end
lemma tsum_le_tsum_of_inj {g : γ → α} (i : β → γ) (hi : injective i) (hs : ∀c∉set.range i, 0 ≤ g c)
(h : ∀b, f b ≤ g (i b)) (hf : summable f) (hg : summable g) : tsum f ≤ tsum g :=
has_sum_le_inj i hi hs h hf.has_sum hg.has_sum
lemma sum_le_has_sum {f : β → α} (s : finset β) (hs : ∀ b∉s, 0 ≤ f b) (hf : has_sum f a) :
∑ b in s, f b ≤ a :=
ge_of_tendsto at_top_ne_bot hf (eventually_at_top.2 ⟨s, λ t hst,
sum_le_sum_of_subset_of_nonneg hst $ λ b hbt hbs, hs b hbs⟩)
lemma sum_le_tsum {f : β → α} (s : finset β) (hs : ∀ b∉s, 0 ≤ f b) (hf : summable f) :
∑ b in s, f b ≤ tsum f :=
sum_le_has_sum s hs hf.has_sum
lemma tsum_le_tsum (h : ∀b, f b ≤ g b) (hf : summable f) (hg : summable g) : (∑'b, f b) ≤ (∑'b, g b) :=
has_sum_le h hf.has_sum hg.has_sum
lemma tsum_nonneg (h : ∀ b, 0 ≤ g b) : 0 ≤ (∑'b, g b) :=
begin
by_cases hg : summable g,
{ simpa using tsum_le_tsum h summable_zero hg },
{ simp [tsum_eq_zero_of_not_summable hg] }
end
lemma tsum_nonpos (h : ∀ b, f b ≤ 0) : (∑'b, f b) ≤ 0 :=
begin
by_cases hf : summable f,
{ simpa using tsum_le_tsum h hf summable_zero},
{ simp [tsum_eq_zero_of_not_summable hf] }
end
end order_topology
section uniform_group
variables [add_comm_group α] [uniform_space α]
variables {f g : β → α} {a a₁ a₂ : α}
lemma summable_iff_cauchy_seq_finset [complete_space α] :
summable f ↔ cauchy_seq (λ (s : finset β), ∑ b in s, f b) :=
(cauchy_map_iff_exists_tendsto at_top_ne_bot).symm
variable [uniform_add_group α]
lemma cauchy_seq_finset_iff_vanishing :
cauchy_seq (λ (s : finset β), ∑ b in s, f b)
↔ ∀ e ∈ 𝓝 (0:α), (∃s:finset β, ∀t, disjoint t s → ∑ b in t, f b ∈ e) :=
begin
simp only [cauchy_seq, cauchy_map_iff, and_iff_right at_top_ne_bot,
prod_at_top_at_top_eq, uniformity_eq_comap_nhds_zero α, tendsto_comap_iff, (∘)],
rw [tendsto_at_top' (_ : finset β × finset β → α)],
split,
{ assume h e he,
rcases h e he with ⟨⟨s₁, s₂⟩, h⟩,
use [s₁ ∪ s₂],
assume t ht,
specialize h (s₁ ∪ s₂, (s₁ ∪ s₂) ∪ t) ⟨le_sup_left, le_sup_left_of_le le_sup_right⟩,
simpa only [finset.sum_union ht.symm, add_sub_cancel'] using h },
{ assume h e he,
rcases exists_nhds_half_neg he with ⟨d, hd, hde⟩,
rcases h d hd with ⟨s, h⟩,
use [(s, s)],
rintros ⟨t₁, t₂⟩ ⟨ht₁, ht₂⟩,
have : ∑ b in t₂, f b - ∑ b in t₁, f b = ∑ b in t₂ \ s, f b - ∑ b in t₁ \ s, f b,
{ simp only [(finset.sum_sdiff ht₁).symm, (finset.sum_sdiff ht₂).symm,
add_sub_add_right_eq_sub] },
simp only [this],
exact hde _ _ (h _ finset.sdiff_disjoint) (h _ finset.sdiff_disjoint) }
end
variable [complete_space α]
lemma summable_iff_vanishing :
summable f ↔ ∀ e ∈ 𝓝 (0:α), (∃s:finset β, ∀t, disjoint t s → ∑ b in t, f b ∈ e) :=
by rw [summable_iff_cauchy_seq_finset, cauchy_seq_finset_iff_vanishing]
/- TODO: generalize to monoid with a uniform continuous subtraction operator: `(a + b) - b = a` -/
lemma summable.summable_of_eq_zero_or_self (hf : summable f) (h : ∀b, g b = 0 ∨ g b = f b) : summable g :=
summable_iff_vanishing.2 $
assume e he,
let ⟨s, hs⟩ := summable_iff_vanishing.1 hf e he in
⟨s, assume t ht,
have eq : ∑ b in t.filter (λb, g b = f b), f b = ∑ b in t, g b :=
calc ∑ b in t.filter (λb, g b = f b), f b = ∑ b in t.filter (λb, g b = f b), g b :
finset.sum_congr rfl (assume b hb, (finset.mem_filter.1 hb).2.symm)
... = ∑ b in t, g b :
begin
refine finset.sum_subset (finset.filter_subset _) _,
assume b hbt hb,
simp only [(∉), finset.mem_filter, and_iff_right hbt] at hb,
exact (h b).resolve_right hb
end,
eq ▸ hs _ $ finset.disjoint_of_subset_left (finset.filter_subset _) ht⟩
lemma summable.summable_comp_of_injective {i : γ → β} (hf : summable f) (hi : injective i) :
summable (f ∘ i) :=
suffices summable (λb, if b ∈ set.range i then f b else 0),
begin
refine (summable_iff_summable_ne_zero_bij (λc _, i c) _ _ _).1 this,
{ assume c₁ c₂ hc₁ hc₂ eq, exact hi eq },
{ assume b hb,
split_ifs at hb,
{ rcases h with ⟨c, rfl⟩,
exact ⟨c, hb, rfl⟩ },
{ contradiction } },
{ assume c hc, exact if_pos (set.mem_range_self _) }
end,
hf.summable_of_eq_zero_or_self $ assume b, by by_cases b ∈ set.range i; simp [h]
lemma summable.sigma_factor {γ : β → Type*} {f : (Σb:β, γ b) → α}
(ha : summable f) (b : β) : summable (λc, f ⟨b, c⟩) :=
ha.summable_comp_of_injective (λ x y hxy, by simpa using hxy)
lemma summable.sigma' [regular_space α] {γ : β → Type*} {f : (Σb:β, γ b) → α}
(ha : summable f) : summable (λb, ∑'c, f ⟨b, c⟩) :=
ha.sigma (λ b, ha.sigma_factor b)
lemma tsum_sigma' [regular_space α] {γ : β → Type*} {f : (Σb:β, γ b) → α}
(ha : summable f) : (∑'p, f p) = (∑'b c, f ⟨b, c⟩) :=
tsum_sigma (λ b, ha.sigma_factor b) ha
end uniform_group
section cauchy_seq
open finset.Ico filter
/-- If the extended distance between consequent points of a sequence is estimated
by a summable series of `nnreal`s, then the original sequence is a Cauchy sequence. -/
lemma cauchy_seq_of_edist_le_of_summable [emetric_space α] {f : ℕ → α} (d : ℕ → nnreal)
(hf : ∀ n, edist (f n) (f n.succ) ≤ d n) (hd : summable d) : cauchy_seq f :=
begin
refine emetric.cauchy_seq_iff_nnreal.2 (λ ε εpos, _),
-- Actually we need partial sums of `d` to be a Cauchy sequence
replace hd : cauchy_seq (λ (n : ℕ), ∑ x in range n, d x) :=
let ⟨_, H⟩ := hd in cauchy_seq_of_tendsto_nhds _ H.tendsto_sum_nat,
-- Now we take the same `N` as in one of the definitions of a Cauchy sequence
refine (metric.cauchy_seq_iff'.1 hd ε (nnreal.coe_pos.2 εpos)).imp (λ N hN n hn, _),
have hsum := hN n hn,
-- We simplify the known inequality
rw [dist_nndist, nnreal.nndist_eq, ← sum_range_add_sum_Ico _ hn, nnreal.add_sub_cancel'] at hsum,
norm_cast at hsum,
replace hsum := lt_of_le_of_lt (le_max_left _ _) hsum,
rw edist_comm,
-- Then use `hf` to simplify the goal to the same form
apply lt_of_le_of_lt (edist_le_Ico_sum_of_edist_le hn (λ k _ _, hf k)),
assumption_mod_cast
end
/-- If the distance between consequent points of a sequence is estimated by a summable series,
then the original sequence is a Cauchy sequence. -/
lemma cauchy_seq_of_dist_le_of_summable [metric_space α] {f : ℕ → α} (d : ℕ → ℝ)
(hf : ∀ n, dist (f n) (f n.succ) ≤ d n) (hd : summable d) : cauchy_seq f :=
begin
refine metric.cauchy_seq_iff'.2 (λε εpos, _),
replace hd : cauchy_seq (λ (n : ℕ), ∑ x in range n, d x) :=
let ⟨_, H⟩ := hd in cauchy_seq_of_tendsto_nhds _ H.tendsto_sum_nat,
refine (metric.cauchy_seq_iff'.1 hd ε εpos).imp (λ N hN n hn, _),
have hsum := hN n hn,
rw [real.dist_eq, ← sum_Ico_eq_sub _ hn] at hsum,
calc dist (f n) (f N) = dist (f N) (f n) : dist_comm _ _
... ≤ ∑ x in Ico N n, d x : dist_le_Ico_sum_of_dist_le hn (λ k _ _, hf k)
... ≤ abs (∑ x in Ico N n, d x) : le_abs_self _
... < ε : hsum
end
lemma cauchy_seq_of_summable_dist [metric_space α] {f : ℕ → α}
(h : summable (λn, dist (f n) (f n.succ))) : cauchy_seq f :=
cauchy_seq_of_dist_le_of_summable _ (λ _, le_refl _) h
lemma dist_le_tsum_of_dist_le_of_tendsto [metric_space α] {f : ℕ → α} (d : ℕ → ℝ)
(hf : ∀ n, dist (f n) (f n.succ) ≤ d n) (hd : summable d) {a : α} (ha : tendsto f at_top (𝓝 a))
(n : ℕ) :
dist (f n) a ≤ ∑' m, d (n + m) :=
begin
refine le_of_tendsto at_top_ne_bot (tendsto_const_nhds.dist ha)
(eventually_at_top.2 ⟨n, λ m hnm, _⟩),
refine le_trans (dist_le_Ico_sum_of_dist_le hnm (λ k _ _, hf k)) _,
rw [sum_Ico_eq_sum_range],
refine sum_le_tsum (range _) (λ _ _, le_trans dist_nonneg (hf _)) _,
exact hd.summable_comp_of_injective (add_right_injective n)
end
lemma dist_le_tsum_of_dist_le_of_tendsto₀ [metric_space α] {f : ℕ → α} (d : ℕ → ℝ)
(hf : ∀ n, dist (f n) (f n.succ) ≤ d n) (hd : summable d) {a : α} (ha : tendsto f at_top (𝓝 a)) :
dist (f 0) a ≤ tsum d :=
by simpa only [zero_add] using dist_le_tsum_of_dist_le_of_tendsto d hf hd ha 0
lemma dist_le_tsum_dist_of_tendsto [metric_space α] {f : ℕ → α}
(h : summable (λn, dist (f n) (f n.succ))) {a : α} (ha : tendsto f at_top (𝓝 a)) (n) :
dist (f n) a ≤ ∑' m, dist (f (n+m)) (f (n+m).succ) :=
show dist (f n) a ≤ ∑' m, (λx, dist (f x) (f x.succ)) (n + m), from
dist_le_tsum_of_dist_le_of_tendsto (λ n, dist (f n) (f n.succ)) (λ _, le_refl _) h ha n
lemma dist_le_tsum_dist_of_tendsto₀ [metric_space α] {f : ℕ → α}
(h : summable (λn, dist (f n) (f n.succ))) {a : α} (ha : tendsto f at_top (𝓝 a)) :
dist (f 0) a ≤ ∑' n, dist (f n) (f n.succ) :=
by simpa only [zero_add] using dist_le_tsum_dist_of_tendsto h ha 0
end cauchy_seq
|
052c7b5faa8e21cfebf17992aa24c3f3d3efc1d9 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/algebra/category/Module/adjunctions.lean | 0a2b07344bc183920cf612d86f264c0ebdcb58c2 | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 11,190 | lean | /-
Copyright (c) 2021 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Johan Commelin
-/
import algebra.category.Module.monoidal.basic
import category_theory.monoidal.functorial
import category_theory.monoidal.types.basic
import linear_algebra.direct_sum.finsupp
import category_theory.linear.linear_functor
/-!
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
The functor of forming finitely supported functions on a type with values in a `[ring R]`
is the left adjoint of
the forgetful functor from `R`-modules to types.
-/
noncomputable theory
open category_theory
namespace Module
universe u
open_locale classical
variables (R : Type u)
section
variables [ring R]
/--
The free functor `Type u ⥤ Module R` sending a type `X` to the
free `R`-module with generators `x : X`, implemented as the type `X →₀ R`.
-/
@[simps]
def free : Type u ⥤ Module R :=
{ obj := λ X, Module.of R (X →₀ R),
map := λ X Y f, finsupp.lmap_domain _ _ f,
map_id' := by { intros, exact finsupp.lmap_domain_id _ _ },
map_comp' := by { intros, exact finsupp.lmap_domain_comp _ _ _ _, } }
/--
The free-forgetful adjunction for R-modules.
-/
def adj : free R ⊣ forget (Module.{u} R) :=
adjunction.mk_of_hom_equiv
{ hom_equiv := λ X M, (finsupp.lift M R X).to_equiv.symm,
hom_equiv_naturality_left_symm' := λ _ _ M f g,
finsupp.lhom_ext' (λ x, linear_map.ext_ring
(finsupp.sum_map_domain_index_add_monoid_hom (λ y, ((smul_add_hom R M).flip) (g y))).symm) }
instance : is_right_adjoint (forget (Module.{u} R)) := ⟨_, adj R⟩
end
namespace free
variables [comm_ring R]
local attribute [ext] tensor_product.ext
/-- (Implementation detail) The unitor for `free R`. -/
def ε : 𝟙_ (Module.{u} R) ⟶ (free R).obj (𝟙_ (Type u)) :=
finsupp.lsingle punit.star
@[simp] lemma ε_apply (r : R) : ε R r = finsupp.single punit.star r := rfl
/-- (Implementation detail) The tensorator for `free R`. -/
def μ (α β : Type u) : (free R).obj α ⊗ (free R).obj β ≅ (free R).obj (α ⊗ β) :=
(finsupp_tensor_finsupp' R α β).to_Module_iso
lemma μ_natural {X Y X' Y' : Type u} (f : X ⟶ Y) (g : X' ⟶ Y') :
((free R).map f ⊗ (free R).map g) ≫ (μ R Y Y').hom =
(μ R X X').hom ≫ (free R).map (f ⊗ g) :=
begin
intros,
ext x x' ⟨y, y'⟩,
dsimp [μ],
simp_rw [finsupp.map_domain_single, finsupp_tensor_finsupp'_single_tmul_single, mul_one,
finsupp.map_domain_single, category_theory.tensor_apply],
end
lemma left_unitality (X : Type u) :
(λ_ ((free R).obj X)).hom =
(ε R ⊗ 𝟙 ((free R).obj X)) ≫ (μ R (𝟙_ (Type u)) X).hom ≫ map (free R).obj (λ_ X).hom :=
begin
intros,
ext,
dsimp [ε, μ],
simp_rw [finsupp_tensor_finsupp'_single_tmul_single,
Module.monoidal_category.left_unitor_hom_apply, finsupp.smul_single', mul_one,
finsupp.map_domain_single, category_theory.left_unitor_hom_apply],
end
lemma right_unitality (X : Type u) :
(ρ_ ((free R).obj X)).hom =
(𝟙 ((free R).obj X) ⊗ ε R) ≫ (μ R X (𝟙_ (Type u))).hom ≫ map (free R).obj (ρ_ X).hom :=
begin
intros,
ext,
dsimp [ε, μ],
simp_rw [finsupp_tensor_finsupp'_single_tmul_single,
Module.monoidal_category.right_unitor_hom_apply, finsupp.smul_single', mul_one,
finsupp.map_domain_single, category_theory.right_unitor_hom_apply],
end
lemma associativity (X Y Z : Type u) :
((μ R X Y).hom ⊗ 𝟙 ((free R).obj Z)) ≫ (μ R (X ⊗ Y) Z).hom ≫ map (free R).obj (α_ X Y Z).hom =
(α_ ((free R).obj X) ((free R).obj Y) ((free R).obj Z)).hom ≫
(𝟙 ((free R).obj X) ⊗ (μ R Y Z).hom) ≫ (μ R X (Y ⊗ Z)).hom :=
begin
intros,
ext,
dsimp [μ],
simp_rw [finsupp_tensor_finsupp'_single_tmul_single, finsupp.map_domain_single, mul_one,
category_theory.associator_hom_apply],
end
/-- The free R-module functor is lax monoidal. -/
-- In fact, it's strong monoidal, but we don't yet have a typeclass for that.
@[simps]
instance : lax_monoidal.{u} (free R).obj :=
{ -- Send `R` to `punit →₀ R`
ε := ε R,
-- Send `(α →₀ R) ⊗ (β →₀ R)` to `α × β →₀ R`
μ := λ X Y, (μ R X Y).hom,
μ_natural' := λ X Y X' Y' f g, μ_natural R f g,
left_unitality' := left_unitality R,
right_unitality' := right_unitality R,
associativity' := associativity R, }
instance : is_iso (lax_monoidal.ε (free R).obj) :=
⟨⟨finsupp.lapply punit.star, ⟨by { ext, simp, }, by { ext ⟨⟩ ⟨⟩, simp, }⟩⟩⟩
end free
variables [comm_ring R]
/-- The free functor `Type u ⥤ Module R`, as a monoidal functor. -/
def monoidal_free : monoidal_functor (Type u) (Module.{u} R) :=
{ ε_is_iso := by { dsimp, apply_instance, },
μ_is_iso := λ X Y, by { dsimp, apply_instance, },
..lax_monoidal_functor.of (free R).obj }
example (X Y : Type u) : (free R).obj (X × Y) ≅ (free R).obj X ⊗ (free R).obj Y :=
((monoidal_free R).μ_iso X Y).symm
end Module
namespace category_theory
universes v u
/--
`Free R C` is a type synonym for `C`, which, given `[comm_ring R]` and `[category C]`,
we will equip with a category structure where the morphisms are formal `R`-linear combinations
of the morphisms in `C`.
-/
@[nolint unused_arguments has_nonempty_instance]
def Free (R : Type*) (C : Type u) := C
/--
Consider an object of `C` as an object of the `R`-linear completion.
It may be preferable to use `(Free.embedding R C).obj X` instead;
this functor can also be used to lift morphisms.
-/
def Free.of (R : Type*) {C : Type u} (X : C) : Free R C := X
variables (R : Type*) [comm_ring R] (C : Type u) [category.{v} C]
open finsupp
-- Conceptually, it would be nice to construct this via "transport of enrichment",
-- using the fact that `Module.free R : Type ⥤ Module R` and `Module.forget` are both lax monoidal.
-- This still seems difficult, so we just do it by hand.
instance category_Free : category (Free R C) :=
{ hom := λ (X Y : C), (X ⟶ Y) →₀ R,
id := λ (X : C), finsupp.single (𝟙 X) 1,
comp := λ (X Y Z : C) f g, f.sum (λ f' s, g.sum (λ g' t, finsupp.single (f' ≫ g') (s * t))),
assoc' := λ W X Y Z f g h,
begin
dsimp,
-- This imitates the proof of associativity for `monoid_algebra`.
simp only [sum_sum_index, sum_single_index,
single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff,
add_mul, mul_add, category.assoc, mul_assoc, zero_mul, mul_zero, sum_zero, sum_add],
end }.
namespace Free
section
local attribute [reducible] category_theory.category_Free
instance : preadditive (Free R C) :=
{ hom_group := λ X Y, finsupp.add_comm_group,
add_comp' := λ X Y Z f f' g, begin
dsimp,
rw [finsupp.sum_add_index'];
{ simp [add_mul], }
end,
comp_add' := λ X Y Z f g g', begin
dsimp,
rw ← finsupp.sum_add,
congr, ext r h,
rw [finsupp.sum_add_index'];
{ simp [mul_add], },
end, }
instance : linear R (Free R C) :=
{ hom_module := λ X Y, finsupp.module (X ⟶ Y) R,
smul_comp' := λ X Y Z r f g, begin
dsimp,
rw [finsupp.sum_smul_index];
simp [finsupp.smul_sum, mul_assoc],
end,
comp_smul' := λ X Y Z f r g, begin
dsimp,
simp_rw [finsupp.smul_sum],
congr, ext h s,
rw [finsupp.sum_smul_index];
simp [finsupp.smul_sum, mul_left_comm],
end, }
lemma single_comp_single {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z) (r s : R) :
(single f r ≫ single g s : (Free.of R X) ⟶ (Free.of R Z)) = single (f ≫ g) (r * s) :=
by { dsimp, simp, }
end
local attribute [simp] single_comp_single
/--
A category embeds into its `R`-linear completion.
-/
@[simps]
def embedding : C ⥤ Free R C :=
{ obj := λ X, X,
map := λ X Y f, finsupp.single f 1,
map_id' := λ X, rfl,
map_comp' := λ X Y Z f g, by simp, }
variables (R) {C} {D : Type u} [category.{v} D] [preadditive D] [linear R D]
open preadditive linear
/--
A functor to an `R`-linear category lifts to a functor from its `R`-linear completion.
-/
@[simps]
def lift (F : C ⥤ D) : Free R C ⥤ D :=
{ obj := λ X, F.obj X,
map := λ X Y f, f.sum (λ f' r, r • (F.map f')),
map_id' := by { dsimp [category_theory.category_Free], simp },
map_comp' := λ X Y Z f g, begin
apply finsupp.induction_linear f,
{ simp only [limits.zero_comp, sum_zero_index] },
{ intros f₁ f₂ w₁ w₂,
rw add_comp,
rw [finsupp.sum_add_index', finsupp.sum_add_index'],
{ simp only [w₁, w₂, add_comp] },
{ intros, rw zero_smul },
{ intros, simp only [add_smul], },
{ intros, rw zero_smul },
{ intros, simp only [add_smul], }, },
{ intros f' r,
apply finsupp.induction_linear g,
{ simp only [limits.comp_zero, sum_zero_index] },
{ intros f₁ f₂ w₁ w₂,
rw comp_add,
rw [finsupp.sum_add_index', finsupp.sum_add_index'],
{ simp only [w₁, w₂, comp_add], },
{ intros, rw zero_smul },
{ intros, simp only [add_smul], },
{ intros, rw zero_smul },
{ intros, simp only [add_smul], }, },
{ intros g' s,
erw single_comp_single,
simp [mul_comm r s, mul_smul] } }
end, }
@[simp]
lemma lift_map_single (F : C ⥤ D) {X Y : C} (f : X ⟶ Y) (r : R) :
(lift R F).map (single f r) = r • F.map f :=
by simp
instance lift_additive (F : C ⥤ D) : (lift R F).additive :=
{ map_add' := λ X Y f g, begin
dsimp,
rw finsupp.sum_add_index'; simp [add_smul]
end, }
instance lift_linear (F : C ⥤ D) : (lift R F).linear R :=
{ map_smul' := λ X Y f r, begin
dsimp,
rw finsupp.sum_smul_index;
simp [finsupp.smul_sum, mul_smul],
end, }
/--
The embedding into the `R`-linear completion, followed by the lift,
is isomorphic to the original functor.
-/
def embedding_lift_iso (F : C ⥤ D) : embedding R C ⋙ lift R F ≅ F :=
nat_iso.of_components
(λ X, iso.refl _)
(by tidy)
/--
Two `R`-linear functors out of the `R`-linear completion are isomorphic iff their
compositions with the embedding functor are isomorphic.
-/
@[ext]
def ext {F G : Free R C ⥤ D} [F.additive] [F.linear R] [G.additive] [G.linear R]
(α : embedding R C ⋙ F ≅ embedding R C ⋙ G) : F ≅ G :=
nat_iso.of_components
(λ X, α.app X)
begin
intros X Y f,
apply finsupp.induction_linear f,
{ simp, },
{ intros f₁ f₂ w₁ w₂,
simp only [F.map_add, G.map_add, add_comp, comp_add, w₁, w₂], },
{ intros f' r,
rw [iso.app_hom, iso.app_hom, ←smul_single_one, F.map_smul, G.map_smul, smul_comp, comp_smul],
change r • (embedding R C ⋙ F).map f' ≫ _ = r • _ ≫ (embedding R C ⋙ G).map f',
rw α.hom.naturality f',
apply_instance, -- Why are these not picked up automatically when we rewrite?
apply_instance, }
end
/--
`Free.lift` is unique amongst `R`-linear functors `Free R C ⥤ D`
which compose with `embedding ℤ C` to give the original functor.
-/
def lift_unique (F : C ⥤ D) (L : Free R C ⥤ D) [L.additive] [L.linear R]
(α : embedding R C ⋙ L ≅ F) :
L ≅ lift R F :=
ext R (α.trans (embedding_lift_iso R F).symm)
end Free
end category_theory
|
194fc0ccebb303ad3fa6ec5709fdd8247b782891 | 5ae26df177f810c5006841e9c73dc56e01b978d7 | /src/algebra/CommRing/limits.lean | 6115d0c6734846119fec13791b971c8d7635d3a1 | [
"Apache-2.0"
] | permissive | ChrisHughes24/mathlib | 98322577c460bc6b1fe5c21f42ce33ad1c3e5558 | a2a867e827c2a6702beb9efc2b9282bd801d5f9a | refs/heads/master | 1,583,848,251,477 | 1,565,164,247,000 | 1,565,164,247,000 | 129,409,993 | 0 | 1 | Apache-2.0 | 1,565,164,817,000 | 1,523,628,059,000 | Lean | UTF-8 | Lean | false | false | 4,031 | lean | /-
Copyright (c) 2019 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import algebra.CommRing.basic
import category_theory.limits.types
import category_theory.limits.preserves
import ring_theory.subring
import algebra.pi_instances
open category_theory
open category_theory.limits
universe u
namespace CommRing
variables {J : Type u} [small_category J]
instance comm_ring_obj (F : J ⥤ CommRing.{u}) (j) : comm_ring ((F ⋙ CommRing.forget).obj j) :=
by { dsimp, apply_instance }
instance is_ring_hom_map (F : J ⥤ CommRing.{u}) (j j') (f : j ⟶ j') : is_ring_hom ((F ⋙ CommRing.forget).map f) :=
by { dsimp, apply_instance }
instance sections_submonoid (F : J ⥤ CommRing.{u}) : is_submonoid (F ⋙ forget).sections :=
{ one_mem := λ j j' f,
begin
simp only [functor.comp_map],
erw is_ring_hom.map_one (CommRing.forget.map (F.map f)),
refl,
end,
mul_mem := λ a b ah bh j j' f,
begin
simp only [functor.comp_map],
erw is_ring_hom.map_mul (CommRing.forget.map (F.map f)),
dsimp [functor.sections] at ah,
rw ah f,
dsimp [functor.sections] at bh,
rw bh f,
refl,
end }
instance sections_add_submonoid (F : J ⥤ CommRing.{u}) : is_add_submonoid (F ⋙ forget).sections :=
{ zero_mem := λ j j' f,
begin
simp only [functor.comp_map],
erw is_ring_hom.map_zero (CommRing.forget.map (F.map f)),
refl,
end,
add_mem := λ a b ah bh j j' f,
begin
simp only [functor.comp_map],
erw is_ring_hom.map_add (CommRing.forget.map (F.map f)),
dsimp [functor.sections] at ah,
rw ah f,
dsimp [functor.sections] at bh,
rw bh f,
refl,
end }
instance sections_add_subgroup (F : J ⥤ CommRing.{u}) : is_add_subgroup (F ⋙ forget).sections :=
{ neg_mem := λ a ah j j' f,
begin
simp only [functor.comp_map],
erw is_ring_hom.map_neg (CommRing.forget.map (F.map f)),
dsimp [functor.sections] at ah,
rw ah f,
refl,
end,
..(CommRing.sections_add_submonoid F) }
instance sections_subring (F : J ⥤ CommRing.{u}) : is_subring (F ⋙ forget).sections :=
{ ..(CommRing.sections_submonoid F),
..(CommRing.sections_add_subgroup F) }
instance limit_comm_ring (F : J ⥤ CommRing.{u}) : comm_ring (limit (F ⋙ forget)) :=
@subtype.comm_ring ((Π (j : J), (F ⋙ forget).obj j)) (by apply_instance) _
(by convert (CommRing.sections_subring F))
instance limit_π_is_ring_hom (F : J ⥤ CommRing.{u}) (j) : is_ring_hom (limit.π (F ⋙ CommRing.forget) j) :=
{ map_one := by { simp only [types.types_limit_π], refl },
map_mul := λ x y, by { simp only [types.types_limit_π], refl },
map_add := λ x y, by { simp only [types.types_limit_π], refl } }
def limit (F : J ⥤ CommRing.{u}) : cone F :=
{ X := ⟨limit (F ⋙ forget), by apply_instance⟩,
π :=
{ app := λ j, ⟨limit.π (F ⋙ forget) j, by apply_instance⟩,
naturality' := λ j j' f, subtype.eq ((limit.cone (F ⋙ forget)).π.naturality f) } }
def limit_is_limit (F : J ⥤ CommRing.{u}) : is_limit (limit F) :=
begin
refine is_limit.of_faithful forget (limit.is_limit _) (λ s, ⟨_, _⟩) (λ s, rfl),
dsimp, split,
{ apply subtype.eq, funext, dsimp,
erw is_ring_hom.map_one (CommRing.forget.map (s.π.app j)), refl },
{ intros x y, apply subtype.eq, funext, dsimp,
erw is_ring_hom.map_mul (CommRing.forget.map (s.π.app j)), refl },
{ intros x y, apply subtype.eq, funext, dsimp,
erw is_ring_hom.map_add (CommRing.forget.map (s.π.app j)), refl },
end
instance CommRing_has_limits : has_limits.{u} CommRing.{u} :=
{ has_limits_of_shape := λ J 𝒥,
{ has_limit := λ F, by exactI { cone := limit F, is_limit := limit_is_limit F } } }
instance forget_preserves_limits : preserves_limits (forget : CommRing.{u} ⥤ Type u) :=
{ preserves_limits_of_shape := λ J 𝒥,
{ preserves_limit := λ F,
by exactI preserves_limit_of_preserves_limit_cone
(limit.is_limit F) (limit.is_limit (F ⋙ forget)) } }
end CommRing
|
d7397b6546404a001ccb93313d8c57d54b536654 | e953c38599905267210b87fb5d82dcc3e52a4214 | /hott/init/logic.hlean | 801665681753283289e5172fad061251632284fb | [
"Apache-2.0"
] | permissive | c-cube/lean | 563c1020bff98441c4f8ba60111fef6f6b46e31b | 0fb52a9a139f720be418dafac35104468e293b66 | refs/heads/master | 1,610,753,294,113 | 1,440,451,356,000 | 1,440,499,588,000 | 41,748,334 | 0 | 0 | null | 1,441,122,656,000 | 1,441,122,656,000 | null | UTF-8 | Lean | false | false | 13,699 | hlean | /-
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
import init.reserved_notation
/- not -/
definition not (a : Type) := a → empty
prefix `¬` := not
definition absurd {a b : Type} (H₁ : a) (H₂ : ¬a) : b :=
empty.rec (λ e, b) (H₂ H₁)
definition mt {a b : Type} (H₁ : a → b) (H₂ : ¬b) : ¬a :=
assume Ha : a, absurd (H₁ Ha) H₂
protected definition not_empty : ¬ empty :=
assume H : empty, H
definition not_not_intro {a : Type} (Ha : a) : ¬¬a :=
assume Hna : ¬a, absurd Ha Hna
definition not.elim {a : Type} (H₁ : ¬a) (H₂ : a) : empty := H₁ H₂
definition not.intro {a : Type} (H : a → empty) : ¬a := H
definition not_not_of_not_implies {a b : Type} (H : ¬(a → b)) : ¬¬a :=
assume Hna : ¬a, absurd (assume Ha : a, absurd Ha Hna) H
definition not_of_not_implies {a b : Type} (H : ¬(a → b)) : ¬b :=
assume Hb : b, absurd (assume Ha : a, Hb) H
/- eq -/
notation a = b := eq a b
definition rfl {A : Type} {a : A} := eq.refl a
namespace eq
variables {A : Type} {a b c : A}
definition subst [unfold 5] {P : A → Type} (H₁ : a = b) (H₂ : P a) : P b :=
eq.rec H₂ H₁
definition trans [unfold 5] (H₁ : a = b) (H₂ : b = c) : a = c :=
subst H₂ H₁
definition symm [unfold 4] (H : a = b) : b = a :=
subst H (refl a)
namespace ops
notation H `⁻¹` := symm H --input with \sy or \-1 or \inv
notation H1 ⬝ H2 := trans H1 H2
notation H1 ▸ H2 := subst H1 H2
end ops
end eq
definition congr {A B : Type} {f₁ f₂ : A → B} {a₁ a₂ : A} (H₁ : f₁ = f₂) (H₂ : a₁ = a₂) : f₁ a₁ = f₂ a₂ :=
eq.subst H₁ (eq.subst H₂ rfl)
theorem congr_arg {A B : Type} (a a' : A) (f : A → B) (Ha : a = a') : f a = f a' :=
eq.subst Ha rfl
theorem congr_arg2 {A B C : Type} (a a' : A) (b b' : B) (f : A → B → C) (Ha : a = a') (Hb : b = b') : f a b = f a' b' :=
eq.subst Ha (eq.subst Hb rfl)
section
variables {A : Type} {a b c: A}
open eq.ops
definition trans_rel_left (R : A → A → Type) (H₁ : R a b) (H₂ : b = c) : R a c :=
H₂ ▸ H₁
definition trans_rel_right (R : A → A → Type) (H₁ : a = b) (H₂ : R b c) : R a c :=
H₁⁻¹ ▸ H₂
end
attribute eq.subst [subst]
attribute eq.refl [refl]
attribute eq.trans [trans]
attribute eq.symm [symm]
namespace lift
definition down_up.{l₁ l₂} {A : Type.{l₁}} (a : A) : down (up.{l₁ l₂} a) = a :=
rfl
definition up_down.{l₁ l₂} {A : Type.{l₁}} (a : lift.{l₁ l₂} A) : up (down a) = a :=
lift.rec_on a (λ d, rfl)
end lift
/- ne -/
definition ne {A : Type} (a b : A) := ¬(a = b)
notation a ≠ b := ne a b
namespace ne
open eq.ops
variable {A : Type}
variables {a b : A}
definition intro : (a = b → empty) → a ≠ b :=
assume H, H
definition elim : a ≠ b → a = b → empty :=
assume H₁ H₂, H₁ H₂
definition irrefl : a ≠ a → empty :=
assume H, H rfl
definition symm : a ≠ b → b ≠ a :=
assume (H : a ≠ b) (H₁ : b = a), H H₁⁻¹
end ne
section
open eq.ops
variables {A : Type} {a b c : A}
definition false.of_ne : a ≠ a → empty :=
assume H, H rfl
definition ne.of_eq_of_ne : a = b → b ≠ c → a ≠ c :=
assume H₁ H₂, H₁⁻¹ ▸ H₂
definition ne.of_ne_of_eq : a ≠ b → b = c → a ≠ c :=
assume H₁ H₂, H₂ ▸ H₁
end
/- iff -/
definition iff (a b : Type) := prod (a → b) (b → a)
notation a <-> b := iff a b
notation a ↔ b := iff a b
namespace iff
variables {a b c : Type}
definition def : (a ↔ b) = (prod (a → b) (b → a)) :=
rfl
definition intro (H₁ : a → b) (H₂ : b → a) : a ↔ b :=
prod.mk H₁ H₂
definition elim (H₁ : (a → b) → (b → a) → c) (H₂ : a ↔ b) : c :=
prod.rec H₁ H₂
definition elim_left (H : a ↔ b) : a → b :=
elim (assume H₁ H₂, H₁) H
definition mp := @elim_left
definition elim_right (H : a ↔ b) : b → a :=
elim (assume H₁ H₂, H₂) H
definition mp' := @elim_right
definition flip_sign (H₁ : a ↔ b) : ¬a ↔ ¬b :=
intro
(assume Hna, mt (elim_right H₁) Hna)
(assume Hnb, mt (elim_left H₁) Hnb)
definition refl (a : Type) : a ↔ a :=
intro (assume H, H) (assume H, H)
definition rfl {a : Type} : a ↔ a :=
refl a
definition trans (H₁ : a ↔ b) (H₂ : b ↔ c) : a ↔ c :=
intro
(assume Ha, elim_left H₂ (elim_left H₁ Ha))
(assume Hc, elim_right H₁ (elim_right H₂ Hc))
definition symm (H : a ↔ b) : b ↔ a :=
intro
(assume Hb, elim_right H Hb)
(assume Ha, elim_left H Ha)
definition true_elim (H : a ↔ unit) : a :=
mp (symm H) unit.star
definition false_elim (H : a ↔ empty) : ¬a :=
assume Ha : a, mp H Ha
open eq.ops
definition of_eq {a b : Type} (H : a = b) : a ↔ b :=
iff.intro (λ Ha, H ▸ Ha) (λ Hb, H⁻¹ ▸ Hb)
definition pi_iff_pi {A : Type} {P Q : A → Type} (H : Πa, (P a ↔ Q a)) : (Πa, P a) ↔ Πa, Q a :=
iff.intro (λp a, iff.elim_left (H a) (p a)) (λq a, iff.elim_right (H a) (q a))
theorem imp_iff {P : Type} (Q : Type) (p : P) : (P → Q) ↔ Q :=
iff.intro (λf, f p) (λq p, q)
end iff
attribute iff.refl [refl]
attribute iff.trans [trans]
attribute iff.symm [symm]
/- inhabited -/
inductive inhabited [class] (A : Type) : Type :=
mk : A → inhabited A
namespace inhabited
protected definition destruct {A : Type} {B : Type} (H1 : inhabited A) (H2 : A → B) : B :=
inhabited.rec H2 H1
definition inhabited_fun [instance] (A : Type) {B : Type} [H : inhabited B] : inhabited (A → B) :=
inhabited.destruct H (λb, mk (λa, b))
definition inhabited_Pi [instance] (A : Type) {B : A → Type} [H : Πx, inhabited (B x)] :
inhabited (Πx, B x) :=
mk (λa, inhabited.destruct (H a) (λb, b))
definition default (A : Type) [H : inhabited A] : A := inhabited.destruct H (take a, a)
end inhabited
/- decidable -/
inductive decidable.{l} [class] (p : Type.{l}) : Type.{l} :=
| inl : p → decidable p
| inr : ¬p → decidable p
namespace decidable
variables {p q : Type}
definition pos_witness [C : decidable p] (H : p) : p :=
decidable.rec_on C (λ Hp, Hp) (λ Hnp, absurd H Hnp)
definition neg_witness [C : decidable p] (H : ¬ p) : ¬ p :=
decidable.rec_on C (λ Hp, absurd Hp H) (λ Hnp, Hnp)
definition by_cases {q : Type} [C : decidable p] (Hpq : p → q) (Hnpq : ¬p → q) : q :=
decidable.rec_on C (assume Hp, Hpq Hp) (assume Hnp, Hnpq Hnp)
definition em (p : Type) [H : decidable p] : sum p ¬p :=
by_cases (λ Hp, sum.inl Hp) (λ Hnp, sum.inr Hnp)
definition by_contradiction [Hp : decidable p] (H : ¬p → empty) : p :=
by_cases
(assume H₁ : p, H₁)
(assume H₁ : ¬p, empty.rec (λ e, p) (H H₁))
definition decidable_iff_equiv (Hp : decidable p) (H : p ↔ q) : decidable q :=
decidable.rec_on Hp
(assume Hp : p, inl (iff.elim_left H Hp))
(assume Hnp : ¬p, inr (iff.elim_left (iff.flip_sign H) Hnp))
definition decidable_eq_equiv.{l} {p q : Type.{l}} (Hp : decidable p) (H : p = q) : decidable q :=
decidable_iff_equiv Hp (iff.of_eq H)
end decidable
section
variables {p q : Type}
open decidable (rec_on inl inr)
definition decidable_unit [instance] : decidable unit :=
inl unit.star
definition decidable_empty [instance] : decidable empty :=
inr not_empty
definition decidable_prod [instance] [Hp : decidable p] [Hq : decidable q] : decidable (prod p q) :=
rec_on Hp
(assume Hp : p, rec_on Hq
(assume Hq : q, inl (prod.mk Hp Hq))
(assume Hnq : ¬q, inr (λ H : prod p q, prod.rec_on H (λ Hp Hq, absurd Hq Hnq))))
(assume Hnp : ¬p, inr (λ H : prod p q, prod.rec_on H (λ Hp Hq, absurd Hp Hnp)))
definition decidable_sum [instance] [Hp : decidable p] [Hq : decidable q] : decidable (sum p q) :=
rec_on Hp
(assume Hp : p, inl (sum.inl Hp))
(assume Hnp : ¬p, rec_on Hq
(assume Hq : q, inl (sum.inr Hq))
(assume Hnq : ¬q, inr (λ H : sum p q, sum.rec_on H (λ Hp, absurd Hp Hnp) (λ Hq, absurd Hq Hnq))))
definition decidable_not [instance] [Hp : decidable p] : decidable (¬p) :=
rec_on Hp
(assume Hp, inr (not_not_intro Hp))
(assume Hnp, inl Hnp)
definition decidable_implies [instance] [Hp : decidable p] [Hq : decidable q] : decidable (p → q) :=
rec_on Hp
(assume Hp : p, rec_on Hq
(assume Hq : q, inl (assume H, Hq))
(assume Hnq : ¬q, inr (assume H : p → q, absurd (H Hp) Hnq)))
(assume Hnp : ¬p, inl (assume Hp, absurd Hp Hnp))
definition decidable_if [instance] [Hp : decidable p] [Hq : decidable q] : decidable (p ↔ q) :=
show decidable (prod (p → q) (q → p)), from _
end
definition decidable_pred [reducible] {A : Type} (R : A → Type) := Π (a : A), decidable (R a)
definition decidable_rel [reducible] {A : Type} (R : A → A → Type) := Π (a b : A), decidable (R a b)
definition decidable_eq [reducible] (A : Type) := decidable_rel (@eq A)
definition decidable_ne [instance] {A : Type} [H : decidable_eq A] : decidable_rel (@ne A) :=
show Π x y : A, decidable (x = y → empty), from _
definition ite (c : Type) [H : decidable c] {A : Type} (t e : A) : A :=
decidable.rec_on H (λ Hc, t) (λ Hnc, e)
definition if_pos {c : Type} [H : decidable c] (Hc : c) {A : Type} {t e : A} : (if c then t else e) = t :=
decidable.rec
(λ Hc : c, eq.refl (@ite c (decidable.inl Hc) A t e))
(λ Hnc : ¬c, absurd Hc Hnc)
H
definition if_neg {c : Type} [H : decidable c] (Hnc : ¬c) {A : Type} {t e : A} : (if c then t else e) = e :=
decidable.rec
(λ Hc : c, absurd Hc Hnc)
(λ Hnc : ¬c, eq.refl (@ite c (decidable.inr Hnc) A t e))
H
definition if_t_t (c : Type) [H : decidable c] {A : Type} (t : A) : (if c then t else t) = t :=
decidable.rec
(λ Hc : c, eq.refl (@ite c (decidable.inl Hc) A t t))
(λ Hnc : ¬c, eq.refl (@ite c (decidable.inr Hnc) A t t))
H
definition if_unit {A : Type} (t e : A) : (if unit then t else e) = t :=
if_pos unit.star
definition if_empty {A : Type} (t e : A) : (if empty then t else e) = e :=
if_neg not_empty
section
open eq.ops
definition if_cond_congr {c₁ c₂ : Type} [H₁ : decidable c₁] [H₂ : decidable c₂] (Heq : c₁ ↔ c₂) {A : Type} (t e : A)
: (if c₁ then t else e) = (if c₂ then t else e) :=
decidable.rec_on H₁
(λ Hc₁ : c₁, decidable.rec_on H₂
(λ Hc₂ : c₂, if_pos Hc₁ ⬝ (if_pos Hc₂)⁻¹)
(λ Hnc₂ : ¬c₂, absurd (iff.elim_left Heq Hc₁) Hnc₂))
(λ Hnc₁ : ¬c₁, decidable.rec_on H₂
(λ Hc₂ : c₂, absurd (iff.elim_right Heq Hc₂) Hnc₁)
(λ Hnc₂ : ¬c₂, if_neg Hnc₁ ⬝ (if_neg Hnc₂)⁻¹))
definition if_congr_aux {c₁ c₂ : Type} [H₁ : decidable c₁] [H₂ : decidable c₂] {A : Type} {t₁ t₂ e₁ e₂ : A}
(Hc : c₁ ↔ c₂) (Ht : t₁ = t₂) (He : e₁ = e₂) :
(if c₁ then t₁ else e₁) = (if c₂ then t₂ else e₂) :=
Ht ▸ He ▸ (if_cond_congr Hc t₁ e₁)
definition if_congr {c₁ c₂ : Type} [H₁ : decidable c₁] {A : Type} {t₁ t₂ e₁ e₂ : A} (Hc : c₁ ↔ c₂) (Ht : t₁ = t₂) (He : e₁ = e₂) :
(if c₁ then t₁ else e₁) = (@ite c₂ (decidable.decidable_iff_equiv H₁ Hc) A t₂ e₂) :=
have H2 [visible] : decidable c₂, from (decidable.decidable_iff_equiv H₁ Hc),
if_congr_aux Hc Ht He
theorem implies_of_if_pos {c t e : Type} [H : decidable c] (h : if c then t else e) : c → t :=
assume Hc, eq.rec_on (if_pos Hc) h
theorem implies_of_if_neg {c t e : Type} [H : decidable c] (h : if c then t else e) : ¬c → e :=
assume Hnc, eq.rec_on (if_neg Hnc) h
-- We use "dependent" if-then-else to be able to communicate the if-then-else condition
-- to the branches
definition dite (c : Type) [H : decidable c] {A : Type} (t : c → A) (e : ¬ c → A) : A :=
decidable.rec_on H (λ Hc, t Hc) (λ Hnc, e Hnc)
definition dif_pos {c : Type} [H : decidable c] (Hc : c) {A : Type} {t : c → A} {e : ¬ c → A} : (if H : c then t H else e H) = t (decidable.pos_witness Hc) :=
decidable.rec
(λ Hc : c, eq.refl (@dite c (decidable.inl Hc) A t e))
(λ Hnc : ¬c, absurd Hc Hnc)
H
definition dif_neg {c : Type} [H : decidable c] (Hnc : ¬c) {A : Type} {t : c → A} {e : ¬ c → A} : (if H : c then t H else e H) = e (decidable.neg_witness Hnc) :=
decidable.rec
(λ Hc : c, absurd Hc Hnc)
(λ Hnc : ¬c, eq.refl (@dite c (decidable.inr Hnc) A t e))
H
-- Remark: dite and ite are "definitionally equal" when we ignore the proofs.
definition dite_ite_eq (c : Type) [H : decidable c] {A : Type} (t : A) (e : A) : dite c (λh, t) (λh, e) = ite c t e :=
rfl
end
open eq.ops unit
definition is_unit (c : Type) [H : decidable c] : Type₀ :=
if c then unit else empty
definition is_empty (c : Type) [H : decidable c] : Type₀ :=
if c then empty else unit
theorem of_is_unit {c : Type} [H₁ : decidable c] (H₂ : is_unit c) : c :=
decidable.rec_on H₁ (λ Hc, Hc) (λ Hnc, empty.rec _ (if_neg Hnc ▸ H₂))
notation `dec_trivial` := of_is_unit star
theorem not_of_not_is_unit {c : Type} [H₁ : decidable c] (H₂ : ¬ is_unit c) : ¬ c :=
decidable.rec_on H₁ (λ Hc, absurd star (if_pos Hc ▸ H₂)) (λ Hnc, Hnc)
theorem not_of_is_empty {c : Type} [H₁ : decidable c] (H₂ : is_empty c) : ¬ c :=
decidable.rec_on H₁ (λ Hc, empty.rec _ (if_pos Hc ▸ H₂)) (λ Hnc, Hnc)
theorem of_not_is_empty {c : Type} [H₁ : decidable c] (H₂ : ¬ is_empty c) : c :=
decidable.rec_on H₁ (λ Hc, Hc) (λ Hnc, absurd star (if_neg Hnc ▸ H₂))
|
61dcea4e9d7df5317a17b1ad451d7937e80561ed | 4727251e0cd73359b15b664c3170e5d754078599 | /src/topology/instances/real.lean | 515ecfb0f9c1b7ea9de9f286fdd595c9c5841f99 | [
"Apache-2.0"
] | permissive | Vierkantor/mathlib | 0ea59ac32a3a43c93c44d70f441c4ee810ccceca | 83bc3b9ce9b13910b57bda6b56222495ebd31c2f | refs/heads/master | 1,658,323,012,449 | 1,652,256,003,000 | 1,652,256,003,000 | 209,296,341 | 0 | 1 | Apache-2.0 | 1,568,807,655,000 | 1,568,807,655,000 | null | UTF-8 | Lean | false | false | 11,282 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Mario Carneiro
-/
import topology.metric_space.basic
import topology.algebra.uniform_group
import topology.algebra.ring
import topology.algebra.star
import ring_theory.subring.basic
import group_theory.archimedean
import algebra.periodic
import order.filter.archimedean
import topology.instances.int
/-!
# Topological properties of ℝ
-/
noncomputable theory
open classical filter int metric set topological_space
open_locale classical topological_space filter uniformity interval
universes u v w
variables {α : Type u} {β : Type v} {γ : Type w}
instance : noncompact_space ℝ := int.closed_embedding_coe_real.noncompact_space
theorem real.uniform_continuous_add : uniform_continuous (λp : ℝ × ℝ, p.1 + p.2) :=
metric.uniform_continuous_iff.2 $ λ ε ε0,
let ⟨δ, δ0, Hδ⟩ := rat_add_continuous_lemma abs ε0 in
⟨δ, δ0, λ a b h, let ⟨h₁, h₂⟩ := max_lt_iff.1 h in Hδ h₁ h₂⟩
theorem real.uniform_continuous_neg : uniform_continuous (@has_neg.neg ℝ _) :=
metric.uniform_continuous_iff.2 $ λ ε ε0, ⟨_, ε0, λ a b h,
by rw dist_comm at h; simpa [real.dist_eq] using h⟩
instance : has_continuous_star ℝ := ⟨continuous_id⟩
instance : uniform_add_group ℝ :=
uniform_add_group.mk' real.uniform_continuous_add real.uniform_continuous_neg
-- short-circuit type class inference
instance : topological_add_group ℝ := by apply_instance
instance : proper_space ℝ :=
{ is_compact_closed_ball := λx r, by { rw real.closed_ball_eq_Icc, apply is_compact_Icc } }
instance : second_countable_topology ℝ := second_countable_of_proper
lemma real.is_topological_basis_Ioo_rat :
@is_topological_basis ℝ _ (⋃(a b : ℚ) (h : a < b), {Ioo a b}) :=
is_topological_basis_of_open_of_nhds
(by simp [is_open_Ioo] {contextual:=tt})
(assume a v hav hv,
let ⟨l, u, ⟨hl, hu⟩, h⟩ := mem_nhds_iff_exists_Ioo_subset.mp (is_open.mem_nhds hv hav),
⟨q, hlq, hqa⟩ := exists_rat_btwn hl,
⟨p, hap, hpu⟩ := exists_rat_btwn hu in
⟨Ioo q p,
by { simp only [mem_Union], exact ⟨q, p, rat.cast_lt.1 $ hqa.trans hap, rfl⟩ },
⟨hqa, hap⟩, assume a' ⟨hqa', ha'p⟩, h ⟨hlq.trans hqa', ha'p.trans hpu⟩⟩)
@[simp] lemma real.cocompact_eq : cocompact ℝ = at_bot ⊔ at_top :=
by simp only [← comap_dist_right_at_top_eq_cocompact (0 : ℝ), real.dist_eq, sub_zero,
comap_abs_at_top]
/- TODO(Mario): Prove that these are uniform isomorphisms instead of uniform embeddings
lemma uniform_embedding_add_rat {r : ℚ} : uniform_embedding (λp:ℚ, p + r) :=
_
lemma uniform_embedding_mul_rat {q : ℚ} (hq : q ≠ 0) : uniform_embedding ((*) q) :=
_ -/
lemma real.mem_closure_iff {s : set ℝ} {x : ℝ} :
x ∈ closure s ↔ ∀ ε > 0, ∃ y ∈ s, |y - x| < ε :=
by simp [mem_closure_iff_nhds_basis nhds_basis_ball, real.dist_eq]
lemma real.uniform_continuous_inv (s : set ℝ) {r : ℝ} (r0 : 0 < r) (H : ∀ x ∈ s, r ≤ |x|) :
uniform_continuous (λp:s, p.1⁻¹) :=
metric.uniform_continuous_iff.2 $ λ ε ε0,
let ⟨δ, δ0, Hδ⟩ := rat_inv_continuous_lemma abs ε0 r0 in
⟨δ, δ0, λ a b h, Hδ (H _ a.2) (H _ b.2) h⟩
lemma real.uniform_continuous_abs : uniform_continuous (abs : ℝ → ℝ) :=
metric.uniform_continuous_iff.2 $ λ ε ε0,
⟨ε, ε0, λ a b, lt_of_le_of_lt (abs_abs_sub_abs_le_abs_sub _ _)⟩
lemma real.tendsto_inv {r : ℝ} (r0 : r ≠ 0) : tendsto (λq, q⁻¹) (𝓝 r) (𝓝 r⁻¹) :=
by rw ← abs_pos at r0; exact
tendsto_of_uniform_continuous_subtype
(real.uniform_continuous_inv {x | |r| / 2 < |x|} (half_pos r0) (λ x h, le_of_lt h))
(is_open.mem_nhds ((is_open_lt' (|r| / 2)).preimage continuous_abs) (half_lt_self r0))
lemma real.continuous_inv : continuous (λa:{r:ℝ // r ≠ 0}, a.val⁻¹) :=
continuous_iff_continuous_at.mpr $ assume ⟨r, hr⟩,
tendsto.comp (real.tendsto_inv hr) (continuous_iff_continuous_at.mp continuous_subtype_val _)
lemma real.continuous.inv [topological_space α] {f : α → ℝ} (h : ∀a, f a ≠ 0) (hf : continuous f) :
continuous (λa, (f a)⁻¹) :=
show continuous ((has_inv.inv ∘ @subtype.val ℝ (λr, r ≠ 0)) ∘ λa, ⟨f a, h a⟩),
from real.continuous_inv.comp (continuous_subtype_mk _ hf)
lemma real.uniform_continuous_mul_const {x : ℝ} : uniform_continuous ((*) x) :=
metric.uniform_continuous_iff.2 $ λ ε ε0, begin
cases exists_gt (|x|) with y xy,
have y0 := lt_of_le_of_lt (abs_nonneg _) xy,
refine ⟨_, div_pos ε0 y0, λ a b h, _⟩,
rw [real.dist_eq, ← mul_sub, abs_mul, ← mul_div_cancel' ε (ne_of_gt y0)],
exact mul_lt_mul' (le_of_lt xy) h (abs_nonneg _) y0
end
lemma real.uniform_continuous_mul (s : set (ℝ × ℝ))
{r₁ r₂ : ℝ} (H : ∀ x ∈ s, |(x : ℝ × ℝ).1| < r₁ ∧ |x.2| < r₂) :
uniform_continuous (λp:s, p.1.1 * p.1.2) :=
metric.uniform_continuous_iff.2 $ λ ε ε0,
let ⟨δ, δ0, Hδ⟩ := rat_mul_continuous_lemma abs ε0 in
⟨δ, δ0, λ a b h,
let ⟨h₁, h₂⟩ := max_lt_iff.1 h in Hδ (H _ a.2).1 (H _ b.2).2 h₁ h₂⟩
protected lemma real.continuous_mul : continuous (λp : ℝ × ℝ, p.1 * p.2) :=
continuous_iff_continuous_at.2 $ λ ⟨a₁, a₂⟩,
tendsto_of_uniform_continuous_subtype
(real.uniform_continuous_mul
({x | |x| < |a₁| + 1} ×ˢ {x | |x| < |a₂| + 1})
(λ x, id))
(is_open.mem_nhds
(((is_open_gt' (|a₁| + 1)).preimage continuous_abs).prod
((is_open_gt' (|a₂| + 1)).preimage continuous_abs ))
⟨lt_add_one (|a₁|), lt_add_one (|a₂|)⟩)
instance : topological_ring ℝ :=
{ continuous_mul := real.continuous_mul, ..real.topological_add_group }
instance : complete_space ℝ :=
begin
apply complete_of_cauchy_seq_tendsto,
intros u hu,
let c : cau_seq ℝ abs := ⟨u, metric.cauchy_seq_iff'.1 hu⟩,
refine ⟨c.lim, λ s h, _⟩,
rcases metric.mem_nhds_iff.1 h with ⟨ε, ε0, hε⟩,
have := c.equiv_lim ε ε0,
simp only [mem_map, mem_at_top_sets, mem_set_of_eq],
refine this.imp (λ N hN n hn, hε (hN n hn))
end
lemma real.totally_bounded_ball (x ε : ℝ) : totally_bounded (ball x ε) :=
by rw real.ball_eq_Ioo; apply totally_bounded_Ioo
section
lemma closure_of_rat_image_lt {q : ℚ} : closure ((coe:ℚ → ℝ) '' {x | q < x}) = {r | ↑q ≤ r} :=
subset.antisymm
((is_closed_ge' _).closure_subset_iff.2
(image_subset_iff.2 $ λ p h, le_of_lt $ (@rat.cast_lt ℝ _ _ _).2 h)) $
λ x hx, mem_closure_iff_nhds.2 $ λ t ht,
let ⟨ε, ε0, hε⟩ := metric.mem_nhds_iff.1 ht in
let ⟨p, h₁, h₂⟩ := exists_rat_btwn ((lt_add_iff_pos_right x).2 ε0) in
⟨_, hε (show abs _ < _,
by rwa [abs_of_nonneg (le_of_lt $ sub_pos.2 h₁), sub_lt_iff_lt_add']),
p, rat.cast_lt.1 (@lt_of_le_of_lt ℝ _ _ _ _ hx h₁), rfl⟩
/- TODO(Mario): Put these back only if needed later
lemma closure_of_rat_image_le_eq {q : ℚ} : closure ((coe:ℚ → ℝ) '' {x | q ≤ x}) = {r | ↑q ≤ r} :=
_
lemma closure_of_rat_image_le_le_eq {a b : ℚ} (hab : a ≤ b) :
closure (of_rat '' {q:ℚ | a ≤ q ∧ q ≤ b}) = {r:ℝ | of_rat a ≤ r ∧ r ≤ of_rat b} :=
_-/
lemma real.bounded_iff_bdd_below_bdd_above {s : set ℝ} : bounded s ↔ bdd_below s ∧ bdd_above s :=
⟨begin
assume bdd,
rcases (bounded_iff_subset_ball 0).1 bdd with ⟨r, hr⟩, -- hr : s ⊆ closed_ball 0 r
rw real.closed_ball_eq_Icc at hr, -- hr : s ⊆ Icc (0 - r) (0 + r)
exact ⟨bdd_below_Icc.mono hr, bdd_above_Icc.mono hr⟩
end,
λ h, bounded_of_bdd_above_of_bdd_below h.2 h.1⟩
lemma real.subset_Icc_Inf_Sup_of_bounded {s : set ℝ} (h : bounded s) :
s ⊆ Icc (Inf s) (Sup s) :=
subset_Icc_cInf_cSup (real.bounded_iff_bdd_below_bdd_above.1 h).1
(real.bounded_iff_bdd_below_bdd_above.1 h).2
end
section periodic
namespace function
lemma periodic.compact_of_continuous' [topological_space α] {f : ℝ → α} {c : ℝ}
(hp : periodic f c) (hc : 0 < c) (hf : continuous f) :
is_compact (range f) :=
begin
convert is_compact_Icc.image hf,
ext x,
refine ⟨_, mem_range_of_mem_image f (Icc 0 c)⟩,
rintros ⟨y, h1⟩,
obtain ⟨z, hz, h2⟩ := hp.exists_mem_Ico₀ hc y,
exact ⟨z, mem_Icc_of_Ico hz, h2.symm.trans h1⟩,
end
/-- A continuous, periodic function has compact range. -/
lemma periodic.compact_of_continuous [topological_space α] {f : ℝ → α} {c : ℝ}
(hp : periodic f c) (hc : c ≠ 0) (hf : continuous f) :
is_compact (range f) :=
begin
cases lt_or_gt_of_ne hc with hneg hpos,
exacts [hp.neg.compact_of_continuous' (neg_pos.mpr hneg) hf, hp.compact_of_continuous' hpos hf],
end
/-- A continuous, periodic function is bounded. -/
lemma periodic.bounded_of_continuous [pseudo_metric_space α] {f : ℝ → α} {c : ℝ}
(hp : periodic f c) (hc : c ≠ 0) (hf : continuous f) :
bounded (range f) :=
(hp.compact_of_continuous hc hf).bounded
end function
end periodic
section subgroups
/-- Given a nontrivial subgroup `G ⊆ ℝ`, if `G ∩ ℝ_{>0}` has no minimum then `G` is dense. -/
lemma real.subgroup_dense_of_no_min {G : add_subgroup ℝ} {g₀ : ℝ} (g₀_in : g₀ ∈ G) (g₀_ne : g₀ ≠ 0)
(H' : ¬ ∃ a : ℝ, is_least {g : ℝ | g ∈ G ∧ 0 < g} a) :
dense (G : set ℝ) :=
begin
let G_pos := {g : ℝ | g ∈ G ∧ 0 < g},
push_neg at H',
intros x,
suffices : ∀ ε > (0 : ℝ), ∃ g ∈ G, |x - g| < ε,
by simpa only [real.mem_closure_iff, abs_sub_comm],
intros ε ε_pos,
obtain ⟨g₁, g₁_in, g₁_pos⟩ : ∃ g₁ : ℝ, g₁ ∈ G ∧ 0 < g₁,
{ cases lt_or_gt_of_ne g₀_ne with Hg₀ Hg₀,
{ exact ⟨-g₀, G.neg_mem g₀_in, neg_pos.mpr Hg₀⟩ },
{ exact ⟨g₀, g₀_in, Hg₀⟩ } },
obtain ⟨a, ha⟩ : ∃ a, is_glb G_pos a :=
⟨Inf G_pos, is_glb_cInf ⟨g₁, g₁_in, g₁_pos⟩ ⟨0, λ _ hx, le_of_lt hx.2⟩⟩,
have a_notin : a ∉ G_pos,
{ intros H,
exact H' a ⟨H, ha.1⟩ },
obtain ⟨g₂, g₂_in, g₂_pos, g₂_lt⟩ : ∃ g₂ : ℝ, g₂ ∈ G ∧ 0 < g₂ ∧ g₂ < ε,
{ obtain ⟨b, hb, hb', hb''⟩ := ha.exists_between_self_add' a_notin ε_pos,
obtain ⟨c, hc, hc', hc''⟩ := ha.exists_between_self_add' a_notin (sub_pos.2 hb'),
refine ⟨b - c, G.sub_mem hb.1 hc.1, _, _⟩ ;
linarith },
refine ⟨floor (x/g₂) * g₂, _, _⟩,
{ exact add_subgroup.int_mul_mem _ g₂_in },
{ rw abs_of_nonneg (sub_floor_div_mul_nonneg x g₂_pos),
linarith [sub_floor_div_mul_lt x g₂_pos] }
end
/-- Subgroups of `ℝ` are either dense or cyclic. See `real.subgroup_dense_of_no_min` and
`subgroup_cyclic_of_min` for more precise statements. -/
lemma real.subgroup_dense_or_cyclic (G : add_subgroup ℝ) :
dense (G : set ℝ) ∨ ∃ a : ℝ, G = add_subgroup.closure {a} :=
begin
cases add_subgroup.bot_or_exists_ne_zero G with H H,
{ right,
use 0,
rw [H, add_subgroup.closure_singleton_zero] },
{ let G_pos := {g : ℝ | g ∈ G ∧ 0 < g},
by_cases H' : ∃ a, is_least G_pos a,
{ right,
rcases H' with ⟨a, ha⟩,
exact ⟨a, add_subgroup.cyclic_of_min ha⟩ },
{ left,
rcases H with ⟨g₀, g₀_in, g₀_ne⟩,
exact real.subgroup_dense_of_no_min g₀_in g₀_ne H' } }
end
end subgroups
|
bb1dfec389524dec6b40cd2f841240f3f6eee929 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/category_theory/limits/preserves/shapes/products.lean | b52cd36376a5920758cd369dd45052a168eef46c | [
"Apache-2.0"
] | permissive | alreadydone/mathlib | dc0be621c6c8208c581f5170a8216c5ba6721927 | c982179ec21091d3e102d8a5d9f5fe06c8fafb73 | refs/heads/master | 1,685,523,275,196 | 1,670,184,141,000 | 1,670,184,141,000 | 287,574,545 | 0 | 0 | Apache-2.0 | 1,670,290,714,000 | 1,597,421,623,000 | Lean | UTF-8 | Lean | false | false | 6,538 | lean | /-
Copyright (c) 2020 Scott Morrison, Bhavik Mehta. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Bhavik Mehta
-/
import category_theory.limits.shapes.products
import category_theory.limits.preserves.basic
/-!
# Preserving products
Constructions to relate the notions of preserving products and reflecting products
to concrete fans.
In particular, we show that `pi_comparison G f` is an isomorphism iff `G` preserves
the limit of `f`.
-/
noncomputable theory
universes w v₁ v₂ u₁ u₂
open category_theory category_theory.category category_theory.limits
variables {C : Type u₁} [category.{v₁} C]
variables {D : Type u₂} [category.{v₂} D]
variables (G : C ⥤ D)
namespace category_theory.limits
variables {J : Type w} (f : J → C)
/--
The map of a fan is a limit iff the fan consisting of the mapped morphisms is a limit. This
essentially lets us commute `fan.mk` with `functor.map_cone`.
-/
def is_limit_map_cone_fan_mk_equiv {P : C} (g : Π j, P ⟶ f j) :
is_limit (G.map_cone (fan.mk P g)) ≃
is_limit (fan.mk _ (λ j, G.map (g j)) : fan (λ j, G.obj (f j))) :=
begin
refine (is_limit.postcompose_hom_equiv _ _).symm.trans (is_limit.equiv_iso_limit _),
refine discrete.nat_iso (λ j, iso.refl (G.obj (f j.as))),
refine cones.ext (iso.refl _) (λ j, by { discrete_cases, dsimp, simp }),
end
/-- The property of preserving products expressed in terms of fans. -/
def is_limit_fan_mk_obj_of_is_limit [preserves_limit (discrete.functor f) G]
{P : C} (g : Π j, P ⟶ f j) (t : is_limit (fan.mk _ g)) :
is_limit (fan.mk (G.obj P) (λ j, G.map (g j)) : fan (λ j, G.obj (f j))) :=
is_limit_map_cone_fan_mk_equiv _ _ _ (preserves_limit.preserves t)
/-- The property of reflecting products expressed in terms of fans. -/
def is_limit_of_is_limit_fan_mk_obj [reflects_limit (discrete.functor f) G]
{P : C} (g : Π j, P ⟶ f j) (t : is_limit (fan.mk _ (λ j, G.map (g j)) : fan (λ j, G.obj (f j)))) :
is_limit (fan.mk P g) :=
reflects_limit.reflects ((is_limit_map_cone_fan_mk_equiv _ _ _).symm t)
section
variables [has_product f]
/--
If `G` preserves products and `C` has them, then the fan constructed of the mapped projection of a
product is a limit.
-/
def is_limit_of_has_product_of_preserves_limit [preserves_limit (discrete.functor f) G] :
is_limit (fan.mk _ (λ (j : J), G.map (pi.π f j)) : fan (λ j, G.obj (f j))) :=
is_limit_fan_mk_obj_of_is_limit G f _ (product_is_product _)
variables [has_product (λ (j : J), G.obj (f j))]
/-- If `pi_comparison G f` is an isomorphism, then `G` preserves the limit of `f`. -/
def preserves_product.of_iso_comparison [i : is_iso (pi_comparison G f)] :
preserves_limit (discrete.functor f) G :=
begin
apply preserves_limit_of_preserves_limit_cone (product_is_product f),
apply (is_limit_map_cone_fan_mk_equiv _ _ _).symm _,
apply is_limit.of_point_iso (limit.is_limit (discrete.functor (λ (j : J), G.obj (f j)))),
apply i,
end
variable [preserves_limit (discrete.functor f) G]
/--
If `G` preserves limits, we have an isomorphism from the image of a product to the product of the
images.
-/
def preserves_product.iso : G.obj (∏ f) ≅ ∏ (λ j, G.obj (f j)) :=
is_limit.cone_point_unique_up_to_iso
(is_limit_of_has_product_of_preserves_limit G f)
(limit.is_limit _)
@[simp]
lemma preserves_product.iso_hom : (preserves_product.iso G f).hom = pi_comparison G f :=
rfl
instance : is_iso (pi_comparison G f) :=
begin
rw ← preserves_product.iso_hom,
apply_instance,
end
end
/--
The map of a cofan is a colimit iff the cofan consisting of the mapped morphisms is a colimit.
This essentially lets us commute `cofan.mk` with `functor.map_cocone`.
-/
def is_colimit_map_cocone_cofan_mk_equiv {P : C} (g : Π j, f j ⟶ P) :
is_colimit (G.map_cocone (cofan.mk P g)) ≃
is_colimit (cofan.mk _ (λ j, G.map (g j)) : cofan (λ j, G.obj (f j))) :=
begin
refine (is_colimit.precompose_hom_equiv _ _).symm.trans (is_colimit.equiv_iso_colimit _),
refine discrete.nat_iso (λ j, iso.refl (G.obj (f j.as))),
refine cocones.ext (iso.refl _) (λ j, by { discrete_cases, dsimp, simp }),
end
/-- The property of preserving coproducts expressed in terms of cofans. -/
def is_colimit_cofan_mk_obj_of_is_colimit [preserves_colimit (discrete.functor f) G]
{P : C} (g : Π j, f j ⟶ P) (t : is_colimit (cofan.mk _ g)) :
is_colimit (cofan.mk (G.obj P) (λ j, G.map (g j)) : cofan (λ j, G.obj (f j))) :=
is_colimit_map_cocone_cofan_mk_equiv _ _ _ (preserves_colimit.preserves t)
/-- The property of reflecting coproducts expressed in terms of cofans. -/
def is_colimit_of_is_colimit_cofan_mk_obj [reflects_colimit (discrete.functor f) G]
{P : C} (g : Π j, f j ⟶ P)
(t : is_colimit (cofan.mk _ (λ j, G.map (g j)) : cofan (λ j, G.obj (f j)))) :
is_colimit (cofan.mk P g) :=
reflects_colimit.reflects ((is_colimit_map_cocone_cofan_mk_equiv _ _ _).symm t)
section
variables [has_coproduct f]
/--
If `G` preserves coproducts and `C` has them,
then the cofan constructed of the mapped inclusion of a coproduct is a colimit.
-/
def is_colimit_of_has_coproduct_of_preserves_colimit [preserves_colimit (discrete.functor f) G] :
is_colimit (cofan.mk _ (λ (j : J), G.map (sigma.ι f j)) : cofan (λ j, G.obj (f j))) :=
is_colimit_cofan_mk_obj_of_is_colimit G f _ (coproduct_is_coproduct _)
variables [has_coproduct (λ (j : J), G.obj (f j))]
/-- If `sigma_comparison G f` is an isomorphism, then `G` preserves the colimit of `f`. -/
def preserves_coproduct.of_iso_comparison [i : is_iso (sigma_comparison G f)] :
preserves_colimit (discrete.functor f) G :=
begin
apply preserves_colimit_of_preserves_colimit_cocone (coproduct_is_coproduct f),
apply (is_colimit_map_cocone_cofan_mk_equiv _ _ _).symm _,
apply is_colimit.of_point_iso (colimit.is_colimit (discrete.functor (λ (j : J), G.obj (f j)))),
apply i,
end
variable [preserves_colimit (discrete.functor f) G]
/--
If `G` preserves colimits,
we have an isomorphism from the image of a coproduct to the coproduct of the images.
-/
def preserves_coproduct.iso : G.obj (∐ f) ≅ ∐ (λ j, G.obj (f j)) :=
is_colimit.cocone_point_unique_up_to_iso
(is_colimit_of_has_coproduct_of_preserves_colimit G f)
(colimit.is_colimit _)
@[simp]
lemma preserves_coproduct.inv_hom : (preserves_coproduct.iso G f).inv = sigma_comparison G f :=
rfl
instance : is_iso (sigma_comparison G f) :=
begin
rw ← preserves_coproduct.inv_hom,
apply_instance,
end
end
end category_theory.limits
|
2a7bfe0090f33bd7dff20cf2c3b29c96cbb50a1a | 48ee71c79a2d430812d2a0c56d69480c8e22fd71 | /InsideOut/Error.lean | f55354e390c9d73b3f305ca8bd3193040ec2ca94 | [
"MIT"
] | permissive | intsuc/inside-out | a49d08c04f992bdc28607345facd63597db28cab | 5f14d511b8cab8703611d071027311f9967ec3a8 | refs/heads/main | 1,691,829,769,552 | 1,633,103,533,000 | 1,633,103,533,000 | 411,958,950 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 678 | lean | import InsideOut.Syntax
inductive Error where
| unknownVariable : (name : String) → Error
| typeMismatch : (expected : String) → (found : String) → Error
| partialInferenceFailure : Exp → (found : String) → Error
| inferenceFailure : Exp → Error
instance : ToString Error where
toString
| Error.unknownVariable name => s!"unknown variable '#{name}'"
| Error.typeMismatch expected found => s!"type expected '{expected}'; found '{found}'"
| Error.partialInferenceFailure e found => s!"partially failed to infer '{e} ∷ {found}'"
| Error.inferenceFailure e => s!"failed to infer '{e}'"
|
a91fb485f1d26aa8ea01bc17f77ef0af8acc1739 | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/data/nat/bits.lean | 91df832d1e5a601b8f13f0f3ec98a33f4f8a5809 | [
"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 | 3,483 | lean | /-
Copyright (c) 2022 Praneeth Kolichala. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Praneeth Kolichala
-/
import tactic.generalize_proofs
import tactic.norm_num
/-!
# Additional properties of binary recursion on `nat`
This file documents additional properties of binary recursion,
which allows us to more easily work with operations which do depend
on the number of leading zeros in the binary representation of `n`.
For example, we can more easily work with `nat.bits` and `nat.size`.
See also: `nat.bitwise`, `nat.pow` (for various lemmas about `size` and `shiftl`/`shiftr`),
and `nat.digits`.
-/
namespace nat
lemma bit_eq_zero_iff {n : ℕ} {b : bool} : bit b n = 0 ↔ n = 0 ∧ b = ff :=
by { split, { cases b; simp [nat.bit], }, rintro ⟨rfl, rfl⟩, refl, }
/-- The same as binary_rec_eq, but that one unfortunately requires `f` to be the identity when
appending `ff` to `0`. Here, we allow you to explicitly say that that case is not happening, i.e.
supplying `n = 0 → b = tt`. -/
lemma binary_rec_eq' {C : ℕ → Sort*} {z : C 0} {f : ∀ b n, C n → C (bit b n)}
(b n) (h : f ff 0 z = z ∨ (n = 0 → b = tt)) :
binary_rec z f (bit b n) = f b n (binary_rec z f n) :=
begin
rw [binary_rec],
split_ifs with h',
{ rcases bit_eq_zero_iff.mp h' with ⟨rfl, rfl⟩,
rw binary_rec_zero,
simp only [imp_false, or_false, eq_self_iff_true, not_true] at h,
exact h.symm },
{ generalize_proofs e, revert e,
rw [bodd_bit, div2_bit],
intros, refl, }
end
/-- The same as `binary_rec`, but the induction step can assume that if `n=0`,
the bit being appended is `tt`-/
@[elab_as_eliminator]
def binary_rec' {C : ℕ → Sort*} (z : C 0) (f : ∀ b n, (n = 0 → b = tt) → C n → C (bit b n)) :
∀ n, C n :=
binary_rec z (λ b n ih, if h : n = 0 → b = tt then f b n h ih else
by { convert z, rw bit_eq_zero_iff, simpa using h, })
/-- The same as `binary_rec`, but special casing both 0 and 1 as base cases -/
@[elab_as_eliminator]
def binary_rec_from_one {C : ℕ → Sort*} (z₀ : C 0) (z₁ : C 1)
(f : ∀ b n, n ≠ 0 → C n → C (bit b n)) : ∀ n, C n :=
binary_rec' z₀ (λ b n h ih, if h' : n = 0 then by { rw [h', h h'], exact z₁ } else f b n h' ih)
@[simp] lemma zero_bits : bits 0 = [] := by simp [nat.bits]
@[simp] lemma bits_append_bit (n : ℕ) (b : bool) (hn : n = 0 → b = tt) :
(bit b n).bits = b :: n.bits :=
by { rw [nat.bits, binary_rec_eq'], simpa, }
@[simp] lemma bit0_bits (n : ℕ) (hn : n ≠ 0) : (bit0 n).bits = ff :: n.bits :=
bits_append_bit n ff (λ hn', absurd hn' hn)
@[simp] lemma bit1_bits (n : ℕ) : (bit1 n).bits = tt :: n.bits :=
bits_append_bit n tt (λ _, rfl)
@[simp] lemma one_bits : nat.bits 1 = [tt] := by { convert bit1_bits 0, simp, }
example : bits 3423 = [tt, tt, tt, tt, tt, ff, tt, ff, tt, ff, tt, tt] := by norm_num
lemma bodd_eq_bits_head (n : ℕ) : n.bodd = n.bits.head :=
begin
induction n using nat.binary_rec' with b n h ih, { simp, },
simp [bodd_bit, bits_append_bit _ _ h],
end
lemma div2_bits_eq_tail (n : ℕ) : n.div2.bits = n.bits.tail :=
begin
induction n using nat.binary_rec' with b n h ih, { simp, },
simp [div2_bit, bits_append_bit _ _ h],
end
lemma size_eq_bits_len (n : ℕ) : n.bits.length = n.size :=
begin
induction n using nat.binary_rec' with b n h ih, { simp, },
rw [size_bit, bits_append_bit _ _ h],
{ simp [ih], },
{ simpa [bit_eq_zero_iff], }
end
end nat
|
f4b048d2e844ab53d5480bb8041e3c43ccd99b7f | 9be442d9ec2fcf442516ed6e9e1660aa9071b7bd | /stage0/src/Lean/Linter/Util.lean | e38e54ddbceb1a107b376f671fd813ca9bcdf60d | [
"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 | 3,443 | lean | import Lean.Data.Options
import Lean.Elab.Command
import Lean.Server.InfoUtils
namespace Lean.Linter
register_builtin_option linter.all : Bool := {
defValue := false
descr := "enable all linters"
}
def getLinterAll (o : Options) (defValue := linter.all.defValue) : Bool := o.get linter.all.name defValue
def getLinterValue (opt : Lean.Option Bool) (o : Options) : Bool := o.get opt.name (getLinterAll o opt.defValue)
open Lean.Elab Lean.Elab.Command
def logLint (linterOption : Lean.Option Bool) (stx : Syntax) (msg : MessageData) : CommandElabM Unit :=
logWarningAt stx (.tagged linterOption.name m!"{msg} [{linterOption.name}]")
/-- Go upwards through the given `tree` starting from the smallest node that
contains the given `range` and collect all `MacroExpansionInfo`s on the way up.
The result is `some []` if no `MacroExpansionInfo` was found on the way and
`none` if no `InfoTree` node was found that covers the given `range`.
Return the result reversed, s.t. the macro expansion that would be applied to
the original syntax first is the first element of the returned list. -/
def collectMacroExpansions? {m} [Monad m] (range : String.Range) (tree : Elab.InfoTree) : m <| Option <| List Elab.MacroExpansionInfo := do
if let .some <| .some result ← go then
return some result.reverse
else
return none
where
go : m <| Option <| Option <| List Elab.MacroExpansionInfo := tree.visitM (postNode := fun _ i _ results => do
let results := results |>.filterMap id |>.filterMap id
-- we expect that at most one InfoTree child returns a result
if let results :: _ := results then
if let .ofMacroExpansionInfo i := i then
return some <| i :: results
else
return some results
else if i.contains range.start && i.contains (includeStop := true) range.stop then
if let .ofMacroExpansionInfo i := i then
return some [i]
else
return some []
else
return none)
/-- List of `Syntax` nodes in which each succeeding element is the parent of
the current. The associated index is the index of the preceding element in the
list of children of the current element. -/
abbrev SyntaxStack := List (Syntax × Nat)
/-- Go upwards through the given `root` syntax starting from `child` and
collect all `Syntax` nodes on the way up.
Return `none` if the `child` is not found in `root`. -/
partial def findSyntaxStack? (root child : Syntax) : Option SyntaxStack := Id.run <| do
let some childRange := child.getRange?
| none
let rec go (stack : SyntaxStack) (stx : Syntax) : Option SyntaxStack := Id.run <| do
let some range := stx.getRange?
| none
if !range.contains childRange.start then
return none
if range == childRange && stx.getKind == child.getKind then
return stack
for i in List.range stx.getNumArgs do
if let some resultStack := go ((stx, i) :: stack) stx[i] then
return resultStack
return none
go [] root
/-- Compare the `SyntaxNodeKind`s in `pattern` to those of the `Syntax`
elements in `stack`. Return `false` if `stack` is shorter than `pattern`. -/
def stackMatches (stack : SyntaxStack) (pattern : List $ Option SyntaxNodeKind) : Bool :=
stack.length >= pattern.length &&
(stack
|>.zipWith (fun (s, _) p => p |>.map (s.isOfKind ·) |>.getD true) pattern
|>.all id)
abbrev IgnoreFunction := Syntax → SyntaxStack → Options → Bool
end Lean.Linter
|
c155e49d6f457074860b5184a329bcb1e90ec616 | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/data/multiset/sections.lean | e5af0ecc6d695dac0a621c3477a789f8f792cbc4 | [
"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 | 2,236 | 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.multiset.basic
/-!
# Sections of a multiset
-/
namespace multiset
variables {α : Type*}
section sections
/--
The sections of a multiset of multisets `s` consists of all those multisets
which can be put in bijection with `s`, so each element is an member of the corresponding multiset.
-/
def sections (s : multiset (multiset α)) : multiset (multiset α) :=
multiset.rec_on s {0} (λs _ c, s.bind $ λa, c.map (multiset.cons a))
(assume a₀ a₁ s pi, by simp [map_bind, bind_bind a₀ a₁, cons_swap])
@[simp] lemma sections_zero : sections (0 : multiset (multiset α)) = 0 ::ₘ 0 :=
rfl
@[simp] lemma sections_cons (s : multiset (multiset α)) (m : multiset α) :
sections (m ::ₘ s) = m.bind (λa, (sections s).map (multiset.cons a)) :=
rec_on_cons m s
lemma coe_sections : ∀(l : list (list α)),
sections ((l.map (λl:list α, (l : multiset α))) : multiset (multiset α)) =
((l.sections.map (λl:list α, (l : multiset α))) : multiset (multiset α))
| [] := rfl
| (a :: l) :=
begin
simp,
rw [← cons_coe, sections_cons, bind_map_comm, coe_sections l],
simp [list.sections, (∘), list.bind]
end
@[simp] lemma sections_add (s t : multiset (multiset α)) :
sections (s + t) = (sections s).bind (λm, (sections t).map ((+) m)) :=
multiset.induction_on s (by simp)
(assume a s ih, by simp [ih, bind_assoc, map_bind, bind_map, -add_comm])
lemma mem_sections {s : multiset (multiset α)} :
∀{a}, a ∈ sections s ↔ s.rel (λs a, a ∈ s) a :=
multiset.induction_on s (by simp)
(assume a s ih a',
by simp [ih, rel_cons_left, -exists_and_distrib_left, exists_and_distrib_left.symm, eq_comm])
lemma card_sections {s : multiset (multiset α)} : card (sections s) = prod (s.map card) :=
multiset.induction_on s (by simp) (by simp {contextual := tt})
lemma prod_map_sum [comm_semiring α] {s : multiset (multiset α)} :
prod (s.map sum) = sum ((sections s).map prod) :=
multiset.induction_on s (by simp)
(assume a s ih, by simp [ih, map_bind, sum_map_mul_left, sum_map_mul_right])
end sections
end multiset
|
d5a0f2cf9762d6be934fb60bcb9401bba078c04b | 36c7a18fd72e5b57229bd8ba36493daf536a19ce | /hott/homotopy/join.hlean | 7b1aecce12dad22438e3f70b815fa08c9d8106b3 | [
"Apache-2.0"
] | permissive | YHVHvx/lean | 732bf0fb7a298cd7fe0f15d82f8e248c11db49e9 | 038369533e0136dd395dc252084d3c1853accbf2 | refs/heads/master | 1,610,701,080,210 | 1,449,128,595,000 | 1,449,128,595,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 15,338 | hlean | /-
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
Declaration of a join as a special case of a pushout
-/
import hit.pushout .susp cubical.cube cubical.squareover
open eq function prod equiv pushout is_trunc bool sigma.ops function
namespace join
section
variables (A B C : Type)
definition join : Type := @pushout (A × B) A B pr1 pr2
definition jglue {A B : Type} (a : A) (b : B) := @glue (A × B) A B pr1 pr2 (a, b)
protected definition is_contr [HA : is_contr A] :
is_contr (join A B) :=
begin
fapply is_contr.mk, exact inl (center A),
intro x, induction x with a b, apply ap inl, apply center_eq,
apply jglue, induction x with a b, apply pathover_of_tr_eq,
apply concat, apply transport_eq_Fr, esimp, rewrite ap_id,
generalize center_eq a, intro p, cases p, apply idp_con,
end
protected definition bool : join bool A ≃ susp A :=
begin
fapply equiv.MK, intro ba, induction ba with b a,
induction b, exact susp.south, exact susp.north, exact susp.north,
induction x with b a, esimp,
induction b, apply inverse, apply susp.merid, exact a, reflexivity,
intro s, induction s with m,
exact inl tt, exact inl ff, exact (jglue tt m) ⬝ (jglue ff m)⁻¹,
intros, induction b with m, do 2 reflexivity, esimp,
apply eq_pathover, apply hconcat, apply hdeg_square, apply concat,
apply ap_compose' (pushout.elim _ _ _), apply concat,
apply ap (ap (pushout.elim _ _ _)), apply susp.elim_merid, apply ap_con,
apply hconcat, apply vconcat, apply hdeg_square, apply elim_glue,
apply hdeg_square, apply ap_inv, esimp,
apply hconcat, apply hdeg_square, apply concat, apply idp_con,
apply concat, apply ap inverse, apply elim_glue, apply inv_inv,
apply hinverse, apply hdeg_square, apply ap_id,
intro x, induction x with b a, induction b, do 2 reflexivity,
esimp, apply jglue, induction x with b a, induction b, esimp,
apply eq_pathover, rewrite ap_id,
apply eq_hconcat, apply concat, apply ap_compose' (susp.elim _ _ _),
apply concat, apply ap (ap _) !elim_glue,
apply concat, apply ap_inv,
apply concat, apply ap inverse !susp.elim_merid,
apply concat, apply con_inv, apply ap (λ x, x ⬝ _) !inv_inv,
apply square_of_eq_top, apply inverse,
apply concat, apply ap (λ x, x ⬝ _) !con.assoc,
rewrite [con.left_inv, con_idp], apply con.right_inv,
esimp, apply eq_pathover, rewrite ap_id,
apply eq_hconcat, apply concat, apply ap_compose' (susp.elim _ _ _),
apply concat, apply ap (ap _) !elim_glue, esimp, reflexivity,
apply square_of_eq_top, rewrite idp_con, apply !con.right_inv⁻¹,
end
protected definition swap : join A B → join B A :=
begin
intro x, induction x with a b, exact inr a, exact inl b,
apply !jglue⁻¹
end
protected definition swap_involutive (x : join A B) :
join.swap B A (join.swap A B x) = x :=
begin
induction x with a b, do 2 reflexivity,
induction x with a b, esimp,
apply eq_pathover, rewrite ap_id,
apply hdeg_square, esimp[join.swap],
apply concat, apply ap_compose' (pushout.elim _ _ _),
krewrite [elim_glue, ap_inv, elim_glue], apply inv_inv,
end
protected definition symm : join A B ≃ join B A :=
by fapply equiv.MK; do 2 apply join.swap; do 2 apply join.swap_involutive
end
/- This proves that the join operator is associative.
The proof is more or less ported from Evan Cavallo's agda version:
https://github.com/HoTT/HoTT-Agda/blob/master/homotopy/JoinAssocCubical.agda -/
section join_switch
private definition massage_sq' {A : Type} {a₀₀ a₂₀ a₀₂ a₂₂ : A}
{p₁₀ : a₀₀ = a₂₀} {p₁₂ : a₀₂ = a₂₂} {p₀₁ : a₀₀ = a₀₂} {p₂₁ : a₂₀ = a₂₂}
(sq : square p₁₀ p₁₂ p₀₁ p₂₁) : square p₁₀⁻¹ p₀₁⁻¹ (p₂₁ ⬝ p₁₂⁻¹) idp :=
by induction sq; exact ids
private definition massage_sq {A : Type} {a₀₀ a₂₀ a₀₂ : A}
{p₁₀ : a₀₀ = a₂₀} {p₁₂ : a₀₂ = a₂₀} {p₀₁ : a₀₀ = a₀₂}
(sq : square p₁₀ p₁₂ p₀₁ idp) : square p₁₀⁻¹ p₀₁⁻¹ p₁₂⁻¹ idp :=
!idp_con⁻¹ ⬝ph (massage_sq' sq)
private definition ap_square_massage {A B : Type} (f : A → B) {a₀₀ a₀₂ a₂₀ : A}
{p₀₁ : a₀₀ = a₀₂} {p₁₀ : a₀₀ = a₂₀} {p₁₁ : a₂₀ = a₀₂} (sq : square p₀₁ p₁₁ p₁₀ idp) :
cube (hdeg_square (ap_inv f p₁₁)) ids
(aps f (massage_sq sq)) (massage_sq (aps f sq))
(hdeg_square !ap_inv) (hdeg_square !ap_inv) :=
by apply rec_on_r sq; apply idc
private definition massage_cube' {A : Type} {a₀₀₀ a₂₀₀ a₀₂₀ a₂₂₀ a₀₀₂ a₂₀₂ a₀₂₂ a₂₂₂ : A}
{p₁₀₀ : a₀₀₀ = a₂₀₀} {p₀₁₀ : a₀₀₀ = a₀₂₀} {p₀₀₁ : a₀₀₀ = a₀₀₂} {p₁₂₀ : a₀₂₀ = a₂₂₀}
{p₂₁₀ : a₂₀₀ = a₂₂₀} {p₂₀₁ : a₂₀₀ = a₂₀₂} {p₁₀₂ : a₀₀₂ = a₂₀₂} {p₀₁₂ : a₀₀₂ = a₀₂₂}
{p₀₂₁ : a₀₂₀ = a₀₂₂} {p₁₂₂ : a₀₂₂ = a₂₂₂} {p₂₁₂ : a₂₀₂ = a₂₂₂} {p₂₂₁ : a₂₂₀ = a₂₂₂}
{s₁₁₀ : square p₀₁₀ p₂₁₀ p₁₀₀ p₁₂₀} {s₁₁₂ : square p₀₁₂ p₂₁₂ p₁₀₂ p₁₂₂}
{s₀₁₁ : square p₀₁₀ p₀₁₂ p₀₀₁ p₀₂₁} {s₂₁₁ : square p₂₁₀ p₂₁₂ p₂₀₁ p₂₂₁}
{s₁₀₁ : square p₁₀₀ p₁₀₂ p₀₀₁ p₂₀₁} {s₁₂₁ : square p₁₂₀ p₁₂₂ p₀₂₁ p₂₂₁}
(c : cube s₀₁₁ s₂₁₁ s₁₀₁ s₁₂₁ s₁₁₀ s₁₁₂) :
cube (s₂₁₁ ⬝v s₁₁₂⁻¹ᵛ) vrfl (massage_sq' s₁₀₁) (massage_sq' s₁₂₁) s₁₁₀⁻¹ᵛ s₀₁₁⁻¹ᵛ :=
by cases c; apply idc
private definition massage_cube {A : Type} {a₀₀₀ a₂₀₀ a₀₂₀ a₂₂₀ a₀₀₂ a₀₂₂ : A}
{p₁₀₀ : a₀₀₀ = a₂₀₀} {p₀₁₀ : a₀₀₀ = a₀₂₀} {p₀₀₁ : a₀₀₀ = a₀₀₂} {p₁₂₀ : a₀₂₀ = a₂₂₀}
{p₂₁₀ : a₂₀₀ = a₂₂₀} {p₁₀₂ : a₀₀₂ = a₂₀₀} {p₀₁₂ : a₀₀₂ = a₀₂₂}
{p₀₂₁ : a₀₂₀ = a₀₂₂} {p₁₂₂ : a₀₂₂ = a₂₂₀}
{s₁₁₀ : square p₀₁₀ _ _ _} {s₁₁₂ : square p₀₁₂ p₂₁₀ p₁₀₂ p₁₂₂}
{s₀₁₁ : square p₀₁₀ p₀₁₂ p₀₀₁ p₀₂₁} --{s₂₁₁ : square p₂₁₀ p₂₁₀ idp idp}
{s₁₀₁ : square p₁₀₀ p₁₀₂ p₀₀₁ idp} {s₁₂₁ : square p₁₂₀ p₁₂₂ p₀₂₁ idp}
(c : cube s₀₁₁ vrfl s₁₀₁ s₁₂₁ s₁₁₀ s₁₁₂) :
cube s₁₁₂⁻¹ᵛ vrfl (massage_sq s₁₀₁) (massage_sq s₁₂₁) s₁₁₀⁻¹ᵛ s₀₁₁⁻¹ᵛ :=
begin
cases p₁₀₀, cases p₁₀₂, cases p₁₂₂, let c' := massage_cube' c, esimp[massage_sq],
krewrite vdeg_v_eq_ph_pv_hp at c', exact c',
end
private definition massage_massage {A : Type} {a₀₀ a₀₂ a₂₀ : A}
{p₀₁ : a₀₀ = a₀₂} {p₁₀ : a₀₀ = a₂₀} {p₁₁ : a₂₀ = a₀₂} (sq : square p₀₁ p₁₁ p₁₀ idp) :
cube (hdeg_square !inv_inv) ids (massage_sq (massage_sq sq))
sq (hdeg_square !inv_inv) (hdeg_square !inv_inv) :=
by apply rec_on_r sq; apply idc
private definition square_Flr_ap_idp_cube {A B : Type} {b : B} {f : A → B}
{p₁ p₂ : Π a, f a = b} (α : Π a, p₁ a = p₂ a) {a₁ a₂ : A} (q : a₁ = a₂) :
cube hrfl hrfl (square_Flr_ap_idp p₁ q) (square_Flr_ap_idp p₂ q)
(hdeg_square (α _)) (hdeg_square (α _)) :=
by cases q; esimp[square_Flr_ap_idp]; apply deg3_cube; esimp
variables {A B C : Type}
private definition switch_left [reducible] : join A B → join (join C B) A :=
begin
intro x, induction x with a b, exact inr a, exact inl (inr b), apply !jglue⁻¹,
end
private definition switch_coh_fill (a : A) (b : B) (c : C) :
Σ sq : square (jglue (inl c) a)⁻¹ (ap inl (jglue c b))⁻¹ (ap switch_left (jglue a b)) idp,
cube (hdeg_square !elim_glue) ids sq (massage_sq !square_Flr_ap_idp) hrfl hrfl :=
by esimp; apply cube_fill101
private definition switch_coh (ab : join A B) (c : C) : switch_left ab = inl (inl c) :=
begin
induction ab with a b, apply !jglue⁻¹, apply (ap inl !jglue)⁻¹, induction x with a b,
apply eq_pathover, refine _ ⬝hp !ap_constant⁻¹,
apply !switch_coh_fill.1,
end
protected definition switch [reducible] : join (join A B) C → join (join C B) A :=
begin
intro x, induction x with ab c, exact switch_left ab, exact inl (inl c),
induction x with ab c, exact switch_coh ab c,
end
private definition switch_inv_left_square (a : A) (b : B) :
square idp idp (ap (!(@join.switch C) ∘ switch_left) (jglue a b)) (ap inl (jglue a b)) :=
begin
refine hdeg_square !ap_compose ⬝h _,
refine aps join.switch (hdeg_square !elim_glue) ⬝h _, esimp,
refine hdeg_square !(ap_inv join.switch) ⬝h _,
refine hrfl⁻¹ʰ⁻¹ᵛ ⬝h _, esimp[join.switch,switch_left,switch_coh],
refine (hdeg_square !elim_glue)⁻¹ᵛ ⬝h _, esimp,
refine hrfl⁻¹ᵛ ⬝h _, apply hdeg_square !inv_inv,
end
private definition switch_inv_coh_left (c : C) (a : A) :
square idp idp (ap !(@join.switch C B) (switch_coh (inl a) c)) (jglue (inl a) c) :=
begin
refine hrfl ⬝h _,
refine aps join.switch hrfl ⬝h _, esimp[switch_coh],
refine hdeg_square !ap_inv ⬝h _,
refine hrfl⁻¹ʰ⁻¹ᵛ ⬝h _, esimp[join.switch,switch_left],
refine (hdeg_square !elim_glue)⁻¹ᵛ ⬝h _,
refine hrfl⁻¹ᵛ ⬝h _, apply hdeg_square !inv_inv,
end
private definition switch_inv_coh_right (c : C) (b : B) :
square idp idp (ap !(@join.switch _ _ A) (switch_coh (inr b) c)) (jglue (inr b) c) :=
begin
refine hrfl ⬝h _,
refine aps join.switch hrfl ⬝h _, esimp[switch_coh],
refine hdeg_square !ap_inv ⬝h _,
refine (hdeg_square !ap_compose)⁻¹ʰ⁻¹ᵛ ⬝h _,
refine hrfl⁻¹ᵛ ⬝h _, esimp[join.switch,switch_left],
refine (hdeg_square !elim_glue)⁻¹ᵛ ⬝h _, apply hdeg_square !inv_inv,
end
private definition switch_inv_left (ab : join A B) :
!(@join.switch C) (join.switch (inl ab)) = inl ab :=
begin
induction ab with a b, do 2 reflexivity,
induction x with a b, apply eq_pathover, exact !switch_inv_left_square,
end
section
variables (a : A) (b : B) (c : C)
private definition switch_inv_cube_aux1 {A B C : Type} {b : B} {f : A → B} (h : B → C)
(g : Π a, f a = b) {x y : A} (p : x = y) :
cube (hdeg_square (ap_compose h f p)) ids (square_Flr_ap_idp (λ a, ap h (g a)) p)
(aps h (square_Flr_ap_idp _ _)) hrfl hrfl :=
by cases p; esimp[square_Flr_ap_idp]; apply deg2_cube; cases (g x); esimp
private definition switch_inv_cube_aux2 {A B : Type} {b : B} {f : A → B}
(g : Π a, f a = b) {x y : A} (p : x = y) {sq : square (g x) (g y) (ap f p) idp}
(q : apdo g p = eq_pathover (sq ⬝hp !ap_constant⁻¹)) : square_Flr_ap_idp _ _ = sq :=
begin
cases p, esimp at *, apply concat, apply inverse, apply vdeg_square_idp,
apply concat, apply ap vdeg_square, exact ap eq_of_pathover_idp q,
krewrite (is_equiv.right_inv (equiv.to_fun !pathover_idp)),
exact is_equiv.left_inv (equiv.to_fun (vdeg_square_equiv _ _)) sq,
end
private definition switch_inv_cube (a : A) (b : B) (c : C) :
cube (switch_inv_left_square a b) ids (square_Flr_ap_idp _ _)
(square_Flr_ap_idp _ _) (switch_inv_coh_left c a) (switch_inv_coh_right c b) :=
begin
esimp [switch_inv_coh_left, switch_inv_coh_right, switch_inv_left_square],
apply cube_concat2, apply switch_inv_cube_aux1,
apply cube_concat2, apply cube_transport101, apply inverse,
apply ap (λ x, aps join.switch x), apply switch_inv_cube_aux2, apply rec_glue,
apply apc, apply (switch_coh_fill a b c).2,
apply cube_concat2, esimp, apply ap_square_massage,
apply cube_concat2, apply massage_cube, apply cube_inverse2, apply switch_inv_cube_aux1,
apply cube_concat2, apply massage_cube, apply square_Flr_ap_idp_cube,
apply cube_concat2, apply massage_cube, apply cube_transport101,
apply inverse, apply switch_inv_cube_aux2,
esimp[switch_coh], apply rec_glue, apply (switch_coh_fill c b a).2,
apply massage_massage,
end
end
private definition pathover_of_triangle_cube {A B : Type} {b₀ b₁ : A → B}
{b : B} {p₀₁ : Π a, b₀ a = b₁ a} {p₀ : Π a, b₀ a = b} {p₁ : Π a, b₁ a = b}
{x y : A} {q : x = y} {sqx : square (p₀₁ x) idp (p₀ x) (p₁ x)}
{sqy : square (p₀₁ y) idp (p₀ y) (p₁ y)}
(c : cube (natural_square_tr _ _) ids (square_Flr_ap_idp p₀ q) (square_Flr_ap_idp p₁ q)
sqx sqy) :
sqx =[q] sqy :=
by cases q; apply pathover_of_eq_tr; apply eq_of_deg12_cube; exact c
private definition pathover_of_ap_ap_square {A : Type} {x y : A} {p : x = y}
(g : B → A) (f : A → B) {u : g (f x) = x} {v : g (f y) = y}
(sq : square (ap g (ap f p)) p u v) : u =[p] v :=
by cases p; apply eq_pathover; apply transpose; exact sq
private definition natural_square_tr_beta {A B : Type} {f₁ f₂ : A → B}
(p : Π a, f₁ a = f₂ a) {x y : A} (q : x = y) {sq : square (p x) (p y) (ap f₁ q) (ap f₂ q)}
(e : apdo p q = eq_pathover sq) :
natural_square_tr p q = sq :=
begin
cases q, esimp at *, apply concat, apply inverse, apply vdeg_square_idp,
apply concat, apply ap vdeg_square, apply ap eq_of_pathover_idp e,
krewrite (is_equiv.right_inv (equiv.to_fun !pathover_idp)),
exact is_equiv.left_inv (equiv.to_fun (vdeg_square_equiv _ _)) sq,
end
private definition switch_inv_coh (c : C) (k : join A B) :
square (switch_inv_left k) idp (ap join.switch (switch_coh k c)) (jglue k c) :=
begin
induction k, apply switch_inv_coh_left, apply switch_inv_coh_right,
refine pathover_of_triangle_cube _,
induction x with [a, b], esimp, apply cube_transport011,
apply inverse, rotate 1, apply switch_inv_cube,
apply natural_square_tr_beta, apply rec_glue,
end
protected definition switch_involutive (x : join (join A B) C) :
join.switch (join.switch x) = x :=
begin
induction x, apply switch_inv_left, reflexivity,
apply pathover_of_ap_ap_square join.switch join.switch,
induction x with [k, c], krewrite elim_glue, esimp,
apply transpose, exact !switch_inv_coh,
end
end join_switch
protected definition switch_equiv (A B C : Type) : join (join A B) C ≃ join (join C B) A :=
by apply equiv.MK; do 2 apply join.switch_involutive
protected definition assoc (A B C : Type) : join (join A B) C ≃ join A (join B C) :=
calc join (join A B) C ≃ join (join C B) A : join.switch_equiv
... ≃ join A (join C B) : join.symm
... ≃ join A (join B C) : join.symm
end join
|
40d9270f5ccde33809c1e9cda27c3946ffdd8c99 | c777c32c8e484e195053731103c5e52af26a25d1 | /src/category_theory/differential_object.lean | 345d26ac9dd25eb3ec70defd4ba43b40a5209638 | [
"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 | 9,017 | 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 data.int.basic
import category_theory.shift.basic
import category_theory.concrete_category.basic
/-!
# Differential objects in a category.
A differential object in a category with zero morphisms and a shift is
an object `X` equipped with
a morphism `d : X ⟶ X⟦1⟧`, such that `d^2 = 0`.
We build the category of differential objects, and some basic constructions
such as the forgetful functor, zero morphisms and zero objects, and the shift functor
on differential objects.
-/
open category_theory.limits
universes v u
namespace category_theory
variables (C : Type u) [category.{v} C]
-- TODO: generalize to `has_shift C A` for an arbitrary `[add_monoid A]` `[has_one A]`.
variables [has_zero_morphisms C] [has_shift C ℤ]
/--
A differential object in a category with zero morphisms and a shift is
an object `X` equipped with
a morphism `d : X ⟶ X⟦1⟧`, such that `d^2 = 0`.
-/
@[nolint has_nonempty_instance]
structure differential_object :=
(X : C)
(d : X ⟶ X⟦1⟧)
(d_squared' : d ≫ d⟦(1:ℤ)⟧' = 0 . obviously)
restate_axiom differential_object.d_squared'
attribute [simp] differential_object.d_squared
variables {C}
namespace differential_object
/--
A morphism of differential objects is a morphism commuting with the differentials.
-/
@[ext, nolint has_nonempty_instance]
structure hom (X Y : differential_object C) :=
(f : X.X ⟶ Y.X)
(comm' : X.d ≫ f⟦1⟧' = f ≫ Y.d . obviously)
restate_axiom hom.comm'
attribute [simp, reassoc] hom.comm
namespace hom
/-- The identity morphism of a differential object. -/
@[simps]
def id (X : differential_object C) : hom X X :=
{ f := 𝟙 X.X }
/-- The composition of morphisms of differential objects. -/
@[simps]
def comp {X Y Z : differential_object C} (f : hom X Y) (g : hom Y Z) : hom X Z :=
{ f := f.f ≫ g.f, }
end hom
instance category_of_differential_objects : category (differential_object C) :=
{ hom := hom,
id := hom.id,
comp := λ X Y Z f g, hom.comp f g, }
@[simp]
lemma id_f (X : differential_object C) : ((𝟙 X) : X ⟶ X).f = 𝟙 (X.X) := rfl
@[simp]
lemma comp_f {X Y Z : differential_object C} (f : X ⟶ Y) (g : Y ⟶ Z) :
(f ≫ g).f = f.f ≫ g.f :=
rfl
@[simp]
lemma eq_to_hom_f {X Y : differential_object C} (h : X = Y) :
hom.f (eq_to_hom h) = eq_to_hom (congr_arg _ h) :=
by { subst h, rw [eq_to_hom_refl, eq_to_hom_refl], refl }
variables (C)
/-- The forgetful functor taking a differential object to its underlying object. -/
def forget : (differential_object C) ⥤ C :=
{ obj := λ X, X.X,
map := λ X Y f, f.f, }
instance forget_faithful : faithful (forget C) :=
{ }
instance has_zero_morphisms : has_zero_morphisms (differential_object C) :=
{ has_zero := λ X Y,
⟨{ f := 0 }⟩}
variables {C}
@[simp]
lemma zero_f (P Q : differential_object C) : (0 : P ⟶ Q).f = 0 := rfl
/--
An isomorphism of differential objects gives an isomorphism of the underlying objects.
-/
@[simps] def iso_app {X Y : differential_object C} (f : X ≅ Y) : X.X ≅ Y.X :=
⟨f.hom.f, f.inv.f, by { dsimp, rw [← comp_f, iso.hom_inv_id, id_f] },
by { dsimp, rw [← comp_f, iso.inv_hom_id, id_f] }⟩
@[simp] lemma iso_app_refl (X : differential_object C) : iso_app (iso.refl X) = iso.refl X.X := rfl
@[simp] lemma iso_app_symm {X Y : differential_object C} (f : X ≅ Y) :
iso_app f.symm = (iso_app f).symm := rfl
@[simp] lemma iso_app_trans {X Y Z : differential_object C} (f : X ≅ Y) (g : Y ≅ Z) :
iso_app (f ≪≫ g) = iso_app f ≪≫ iso_app g := rfl
/-- An isomorphism of differential objects can be constructed
from an isomorphism of the underlying objects that commutes with the differentials. -/
@[simps] def mk_iso {X Y : differential_object C}
(f : X.X ≅ Y.X) (hf : X.d ≫ f.hom⟦1⟧' = f.hom ≫ Y.d) : X ≅ Y :=
{ hom := ⟨f.hom, hf⟩,
inv := ⟨f.inv, by { dsimp, rw [← functor.map_iso_inv, iso.comp_inv_eq, category.assoc,
iso.eq_inv_comp, functor.map_iso_hom, hf] }⟩,
hom_inv_id' := by { ext1, dsimp, exact f.hom_inv_id },
inv_hom_id' := by { ext1, dsimp, exact f.inv_hom_id } }
end differential_object
namespace functor
universes v' u'
variables (D : Type u') [category.{v'} D]
variables [has_zero_morphisms D] [has_shift D ℤ]
/--
A functor `F : C ⥤ D` which commutes with shift functors on `C` and `D` and preserves zero morphisms
can be lifted to a functor `differential_object C ⥤ differential_object D`.
-/
@[simps]
def map_differential_object (F : C ⥤ D)
(η : (shift_functor C (1:ℤ)).comp F ⟶ F.comp (shift_functor D (1:ℤ)))
(hF : ∀ c c', F.map (0 : c ⟶ c') = 0) :
differential_object C ⥤ differential_object D :=
{ obj := λ X, { X := F.obj X.X,
d := F.map X.d ≫ η.app X.X,
d_squared' := begin
rw [functor.map_comp, ← functor.comp_map F (shift_functor D (1:ℤ))],
slice_lhs 2 3 { rw [← η.naturality X.d] },
rw [functor.comp_map],
slice_lhs 1 2 { rw [← F.map_comp, X.d_squared, hF] },
rw [zero_comp, zero_comp],
end },
map := λ X Y f, { f := F.map f.f,
comm' := begin
dsimp,
slice_lhs 2 3 { rw [← functor.comp_map F (shift_functor D (1:ℤ)), ← η.naturality f.f] },
slice_lhs 1 2 { rw [functor.comp_map, ← F.map_comp, f.comm, F.map_comp] },
rw [category.assoc]
end },
map_id' := by { intros, ext, simp },
map_comp' := by { intros, ext, simp }, }
end functor
end category_theory
namespace category_theory
namespace differential_object
variables (C : Type u) [category.{v} C]
variables [has_zero_object C] [has_zero_morphisms C] [has_shift C ℤ]
open_locale zero_object
instance has_zero_object : has_zero_object (differential_object C) :=
by { refine ⟨⟨⟨0, 0⟩, λ X, ⟨⟨⟨⟨0⟩⟩, λ f, _⟩⟩, λ X, ⟨⟨⟨⟨0⟩⟩, λ f, _⟩⟩⟩⟩; ext, }
end differential_object
namespace differential_object
variables (C : Type (u+1)) [large_category C] [concrete_category C]
[has_zero_morphisms C] [has_shift C ℤ]
instance concrete_category_of_differential_objects :
concrete_category (differential_object C) :=
{ forget := forget C ⋙ category_theory.forget C }
instance : has_forget₂ (differential_object C) C :=
{ forget₂ := forget C }
end differential_object
/-! The category of differential objects itself has a shift functor. -/
namespace differential_object
variables (C : Type u) [category.{v} C]
variables [has_zero_morphisms C] [has_shift C ℤ]
noncomputable theory
/-- The shift functor on `differential_object C`. -/
@[simps]
def shift_functor (n : ℤ) : differential_object C ⥤ differential_object C :=
{ obj := λ X,
{ X := X.X⟦n⟧,
d := X.d⟦n⟧' ≫ (shift_comm _ _ _).hom,
d_squared' := by rw [functor.map_comp, category.assoc, shift_comm_hom_comp_assoc,
←functor.map_comp_assoc, X.d_squared, functor.map_zero, zero_comp] },
map := λ X Y f,
{ f := f.f⟦n⟧',
comm' := begin
dsimp,
erw [category.assoc, shift_comm_hom_comp, ← functor.map_comp_assoc, f.comm,
functor.map_comp_assoc],
refl,
end, },
map_id' := by { intros X, ext1, dsimp, rw functor.map_id },
map_comp' := by { intros X Y Z f g, ext1, dsimp, rw functor.map_comp } }
/-- The shift functor on `differential_object C` is additive. -/
@[simps] def shift_functor_add (m n : ℤ) :
shift_functor C (m + n) ≅ shift_functor C m ⋙ shift_functor C n :=
begin
refine nat_iso.of_components (λ X, mk_iso (shift_add X.X _ _) _) _,
{ dsimp,
rw [← cancel_epi ((shift_functor_add C m n).inv.app X.X)],
simp only [category.assoc, iso.inv_hom_id_app_assoc],
erw [← nat_trans.naturality_assoc],
dsimp,
simp only [functor.map_comp, category.assoc,
shift_functor_comm_hom_app_comp_shift_shift_functor_add_hom_app 1 m n X.X,
iso.inv_hom_id_app_assoc], },
{ intros X Y f, ext, dsimp, exact nat_trans.naturality _ _ }
end
section
/-- The shift by zero is naturally isomorphic to the identity. -/
@[simps]
def shift_zero : shift_functor C 0 ≅ 𝟭 (differential_object C) :=
begin
refine nat_iso.of_components (λ X, mk_iso ((shift_functor_zero C ℤ).app X.X) _) _,
{ erw [← nat_trans.naturality],
dsimp,
simp only [shift_functor_zero_hom_app_shift, category.assoc], },
{ tidy, },
end
end
instance : has_shift (differential_object C) ℤ :=
has_shift_mk _ _
{ F := shift_functor C,
zero := shift_zero C,
add := shift_functor_add C,
assoc_hom_app := λ m₁ m₂ m₃ X, begin
ext1,
convert shift_functor_add_assoc_hom_app m₁ m₂ m₃ X.X,
dsimp [shift_functor_add'],
simpa,
end,
zero_add_hom_app := λ n X, begin
ext1,
convert shift_functor_add_zero_add_hom_app n X.X,
simpa,
end,
add_zero_hom_app := λ n X, begin
ext1,
convert shift_functor_add_add_zero_hom_app n X.X,
simpa,
end, }
end differential_object
end category_theory
|
bcff6fb5dd80c51b5c016fd5cbe80372b635108d | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/combinatorics/derangements/finite.lean | f9aa9e995306a257d8129ec5da05ccf187195658 | [
"Apache-2.0"
] | permissive | alreadydone/mathlib | dc0be621c6c8208c581f5170a8216c5ba6721927 | c982179ec21091d3e102d8a5d9f5fe06c8fafb73 | refs/heads/master | 1,685,523,275,196 | 1,670,184,141,000 | 1,670,184,141,000 | 287,574,545 | 0 | 0 | Apache-2.0 | 1,670,290,714,000 | 1,597,421,623,000 | Lean | UTF-8 | Lean | false | false | 4,741 | lean | /-
Copyright (c) 2021 Henry Swanson. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Henry Swanson
-/
import combinatorics.derangements.basic
import data.fintype.big_operators
import tactic.delta_instance
import tactic.ring
/-!
# Derangements on fintypes
This file contains lemmas that describe the cardinality of `derangements α` when `α` is a fintype.
# Main definitions
* `card_derangements_invariant`: A lemma stating that the number of derangements on a type `α`
depends only on the cardinality of `α`.
* `num_derangements n`: The number of derangements on an n-element set, defined in a computation-
friendly way.
* `card_derangements_eq_num_derangements`: Proof that `num_derangements` really does compute the
number of derangements.
* `num_derangements_sum`: A lemma giving an expression for `num_derangements n` in terms of
factorials.
-/
open derangements equiv fintype
open_locale big_operators
variables {α : Type*} [decidable_eq α] [fintype α]
instance : decidable_pred (derangements α) := λ _, fintype.decidable_forall_fintype
instance : fintype (derangements α) := by delta_instance derangements
lemma card_derangements_invariant {α β : Type*} [fintype α] [decidable_eq α]
[fintype β] [decidable_eq β] (h : card α = card β) :
card (derangements α) = card (derangements β) :=
fintype.card_congr (equiv.derangements_congr $ equiv_of_card_eq h)
lemma card_derangements_fin_add_two (n : ℕ) :
card (derangements (fin (n+2))) = (n+1) * card (derangements (fin n)) +
(n+1) * card (derangements (fin (n+1))) :=
begin
-- get some basic results about the size of fin (n+1) plus or minus an element
have h1 : ∀ a : fin (n+1), card ({a}ᶜ : set (fin (n+1))) = card (fin n),
{ intro a,
simp only [fintype.card_fin, finset.card_fin, fintype.card_of_finset, finset.filter_ne' _ a,
set.mem_compl_singleton_iff, finset.card_erase_of_mem (finset.mem_univ a),
add_tsub_cancel_right] },
have h2 : card (fin (n+2)) = card (option (fin (n+1))),
{ simp only [card_fin, card_option] },
-- rewrite the LHS and substitute in our fintype-level equivalence
simp only [card_derangements_invariant h2,
card_congr (@derangements_recursion_equiv (fin (n+1)) _),
-- push the cardinality through the Σ and ⊕ so that we can use `card_n`
card_sigma, card_sum, card_derangements_invariant (h1 _), finset.sum_const, nsmul_eq_mul,
finset.card_fin, mul_add, nat.cast_id],
end
/-- The number of derangements of an `n`-element set. -/
def num_derangements : ℕ → ℕ
| 0 := 1
| 1 := 0
| (n + 2) := (n + 1) * (num_derangements n + num_derangements (n+1))
@[simp] lemma num_derangements_zero : num_derangements 0 = 1 := rfl
@[simp] lemma num_derangements_one : num_derangements 1 = 0 := rfl
lemma num_derangements_add_two (n : ℕ) :
num_derangements (n+2) = (n+1) * (num_derangements n + num_derangements (n+1)) := rfl
lemma num_derangements_succ (n : ℕ) :
(num_derangements (n+1) : ℤ) = (n + 1) * (num_derangements n : ℤ) - (-1)^n :=
begin
induction n with n hn,
{ refl },
{ simp only [num_derangements_add_two, hn, pow_succ,
int.coe_nat_mul, int.coe_nat_add, int.coe_nat_succ],
ring }
end
lemma card_derangements_fin_eq_num_derangements {n : ℕ} :
card (derangements (fin n)) = num_derangements n :=
begin
induction n using nat.strong_induction_on with n hyp,
obtain (_|_|n) := n, { refl }, { refl }, -- knock out cases 0 and 1
-- now we have n ≥ 2. rewrite everything in terms of card_derangements, so that we can use
-- `card_derangements_fin_add_two`
rw [num_derangements_add_two, card_derangements_fin_add_two, mul_add,
hyp _ (nat.lt_add_of_pos_right zero_lt_two), hyp _ (lt_add_one _)],
end
lemma card_derangements_eq_num_derangements (α : Type*) [fintype α] [decidable_eq α] :
card (derangements α) = num_derangements (card α) :=
begin
rw ←card_derangements_invariant (card_fin _),
exact card_derangements_fin_eq_num_derangements,
end
theorem num_derangements_sum (n : ℕ) :
(num_derangements n : ℤ) = ∑ k in finset.range (n + 1), (-1:ℤ)^k * nat.asc_factorial k (n - k) :=
begin
induction n with n hn, { refl },
rw [finset.sum_range_succ, num_derangements_succ, hn, finset.mul_sum, tsub_self,
nat.asc_factorial_zero, int.coe_nat_one, mul_one, pow_succ, neg_one_mul, sub_eq_add_neg,
add_left_inj, finset.sum_congr rfl],
-- show that (n + 1) * (-1)^x * asc_fac x (n - x) = (-1)^x * asc_fac x (n.succ - x)
intros x hx,
have h_le : x ≤ n := finset.mem_range_succ_iff.mp hx,
rw [nat.succ_sub h_le, nat.asc_factorial_succ, add_tsub_cancel_of_le h_le,
int.coe_nat_mul, int.coe_nat_succ, mul_left_comm],
end
|
9e04186e2a0b2504125ea33caa5e0af7600d56f8 | 2c41ae31b2b771ad5646ad880201393f5269a7f0 | /Lean/Qualities/Secure.lean | bb9df8501b659384d2e3f234504f820310ecee99 | [] | no_license | kevinsullivan/Boehm | 926f25bc6f1a8b6bd47d333d936fdfc278228312 | 55208395bff20d48a598b7fa33a4d55a2447a9cf | refs/heads/master | 1,586,127,134,302 | 1,488,252,326,000 | 1,488,252,326,000 | 32,836,930 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 640 | lean | -- Secure
/-
[Secure] is parameterized by an instance of type [SystemType], and it's a sub-attribute to [Dependable].
An instance of type [SystemType] is deemed [Secure] if and only if all the requirements are satisfied.
-/
import SystemModel.System
inductive Secure (sys_type: SystemType): Prop
| intro : (exists secure: sys_type ^.Contexts -> sys_type ^.Phases -> sys_type ^.Stakeholders -> @SystemInstance sys_type -> Prop,
forall c: sys_type ^.Contexts, forall p: sys_type ^.Phases,
forall s: sys_type ^.Stakeholders, forall st: @SystemInstance sys_type, secure c p s st) ->
Secure
|
1a7cdd65a6d277187f811de4522603fad9610b3f | b561a44b48979a98df50ade0789a21c79ee31288 | /src/Lean/Elab/Binders.lean | 5f1f654c8a9273e8170d8e4e2a837e252ad45bce | [
"Apache-2.0"
] | permissive | 3401ijk/lean4 | 97659c475ebd33a034fed515cb83a85f75ccfb06 | a5b1b8de4f4b038ff752b9e607b721f15a9a4351 | refs/heads/master | 1,693,933,007,651 | 1,636,424,845,000 | 1,636,424,845,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 26,953 | lean | /-
Copyright (c) 2019 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Elab.Quotation.Precheck
import Lean.Elab.Term
import Lean.Elab.BindersUtil
import Lean.Parser.Term
namespace Lean.Elab.Term
open Meta
open Lean.Parser.Term
/--
Given syntax of the forms
a) (`:` term)?
b) `:` term
return `term` if it is present, or a hole if not. -/
private def expandBinderType (ref : Syntax) (stx : Syntax) : Syntax :=
if stx.getNumArgs == 0 then
mkHole ref
else
stx[1]
/-- Given syntax of the form `ident <|> hole`, return `ident`. If `hole`, then we create a new anonymous name. -/
private def expandBinderIdent (stx : Syntax) : TermElabM Syntax :=
match stx with
| `(_) => mkFreshIdent stx
| _ => pure stx
/-- Given syntax of the form `(ident >> " : ")?`, return `ident`, or a new instance name. -/
private def expandOptIdent (stx : Syntax) : TermElabM Syntax := do
if stx.isNone then
let id ← withFreshMacroScope <| MonadQuotation.addMacroScope `inst
return mkIdentFrom stx id
else
return stx[0]
structure BinderView where
id : Syntax
type : Syntax
bi : BinderInfo
partial def quoteAutoTactic : Syntax → TermElabM Syntax
| stx@(Syntax.ident _ _ _ _) => throwErrorAt stx "invalid auto tactic, identifier is not allowed"
| stx@(Syntax.node _ k args) => do
if stx.isAntiquot then
throwErrorAt stx "invalid auto tactic, antiquotation is not allowed"
else
let mut quotedArgs ← `(Array.empty)
for arg in args do
if k == nullKind && (arg.isAntiquotSuffixSplice || arg.isAntiquotSplice) then
throwErrorAt arg "invalid auto tactic, antiquotation is not allowed"
else
let quotedArg ← quoteAutoTactic arg
quotedArgs ← `(Array.push $quotedArgs $quotedArg)
`(Syntax.node SourceInfo.none $(quote k) $quotedArgs)
| Syntax.atom info val => `(mkAtom $(quote val))
| Syntax.missing => throwError "invalid auto tactic, tactic is missing"
def declareTacticSyntax (tactic : Syntax) : TermElabM Name :=
withFreshMacroScope do
let name ← MonadQuotation.addMacroScope `_auto
let type := Lean.mkConst `Lean.Syntax
let tactic ← quoteAutoTactic tactic
let val ← elabTerm tactic type
let val ← instantiateMVars val
trace[Elab.autoParam] val
let decl := Declaration.defnDecl { name := name, levelParams := [], type := type, value := val, hints := ReducibilityHints.opaque,
safety := DefinitionSafety.safe }
addDecl decl
compileDecl decl
return name
/-
Expand `optional (binderTactic <|> binderDefault)`
def binderTactic := leading_parser " := " >> " by " >> tacticParser
def binderDefault := leading_parser " := " >> termParser
-/
private def expandBinderModifier (type : Syntax) (optBinderModifier : Syntax) : TermElabM Syntax := do
if optBinderModifier.isNone then
return type
else
let modifier := optBinderModifier[0]
let kind := modifier.getKind
if kind == `Lean.Parser.Term.binderDefault then
let defaultVal := modifier[1]
`(optParam $type $defaultVal)
else if kind == `Lean.Parser.Term.binderTactic then
let tac := modifier[2]
let name ← declareTacticSyntax tac
`(autoParam $type $(mkIdentFrom tac name))
else
throwUnsupportedSyntax
private def getBinderIds (ids : Syntax) : TermElabM (Array Syntax) :=
ids.getArgs.mapM fun id =>
let k := id.getKind
if k == identKind || k == `Lean.Parser.Term.hole then
return id
else
throwErrorAt id "identifier or `_` expected"
private def matchBinder (stx : Syntax) : TermElabM (Array BinderView) := do
let k := stx.getKind
if k == ``Lean.Parser.Term.simpleBinder then
-- binderIdent+ >> optType
let ids ← getBinderIds stx[0]
let optType := stx[1]
ids.mapM fun id => do pure { id := (← expandBinderIdent id), type := expandOptType id optType, bi := BinderInfo.default }
else if k == ``Lean.Parser.Term.explicitBinder then
-- `(` binderIdent+ binderType (binderDefault <|> binderTactic)? `)`
let ids ← getBinderIds stx[1]
let type := stx[2]
let optModifier := stx[3]
ids.mapM fun id => do pure { id := (← expandBinderIdent id), type := (← expandBinderModifier (expandBinderType id type) optModifier), bi := BinderInfo.default }
else if k == ``Lean.Parser.Term.implicitBinder then
-- `{` binderIdent+ binderType `}`
let ids ← getBinderIds stx[1]
let type := stx[2]
ids.mapM fun id => do pure { id := (← expandBinderIdent id), type := expandBinderType id type, bi := BinderInfo.implicit }
else if k == ``Lean.Parser.Term.strictImplicitBinder then
-- `⦃` binderIdent+ binderType `⦄`
let ids ← getBinderIds stx[1]
let type := stx[2]
ids.mapM fun id => do pure { id := (← expandBinderIdent id), type := expandBinderType id type, bi := BinderInfo.strictImplicit }
else if k == ``Lean.Parser.Term.instBinder then
-- `[` optIdent type `]`
let id ← expandOptIdent stx[1]
let type := stx[2]
pure #[ { id := id, type := type, bi := BinderInfo.instImplicit } ]
else
throwUnsupportedSyntax
private def registerFailedToInferBinderTypeInfo (type : Expr) (ref : Syntax) : TermElabM Unit :=
registerCustomErrorIfMVar type ref "failed to infer binder type"
private def addLocalVarInfo (stx : Syntax) (fvar : Expr) : TermElabM Unit := do
addTermInfo (isBinder := true) stx fvar
private def ensureAtomicBinderName (binderView : BinderView) : TermElabM Unit :=
let n := binderView.id.getId.eraseMacroScopes
unless n.isAtomic do
throwErrorAt binderView.id "invalid binder name '{n}', it must be atomic"
register_builtin_option checkBinderAnnotations : Bool := {
defValue := true
descr := "check whether type is a class instance whenever the binder annotation `[...]` is used"
}
private partial def elabBinderViews {α} (binderViews : Array BinderView) (fvars : Array Expr) (k : Array Expr → TermElabM α)
: TermElabM α :=
let rec loop (i : Nat) (fvars : Array Expr) : TermElabM α := do
if h : i < binderViews.size then
let binderView := binderViews.get ⟨i, h⟩
ensureAtomicBinderName binderView
let type ← elabType binderView.type
registerFailedToInferBinderTypeInfo type binderView.type
if binderView.bi.isInstImplicit && checkBinderAnnotations.get (← getOptions) then
unless (← isClass? type).isSome do
throwErrorAt binderView.type "invalid binder annotation, type is not a class instance{indentExpr type}\nuse the command `set_option checkBinderAnnotations false` to disable the check"
withLocalDecl binderView.id.getId binderView.bi type fun fvar => do
addLocalVarInfo binderView.id fvar
loop (i+1) (fvars.push fvar)
else
k fvars
loop 0 fvars
private partial def elabBindersAux {α} (binders : Array Syntax) (k : Array Expr → TermElabM α) : TermElabM α :=
let rec loop (i : Nat) (fvars : Array Expr) : TermElabM α := do
if h : i < binders.size then
let binderViews ← matchBinder (binders.get ⟨i, h⟩)
elabBinderViews binderViews fvars <| loop (i+1)
else
k fvars
loop 0 #[]
/--
Elaborate the given binders (i.e., `Syntax` objects for `simpleBinder <|> bracketedBinder`),
update the local context, set of local instances, reset instance chache (if needed), and then
execute `x` with the updated context. -/
def elabBinders {α} (binders : Array Syntax) (k : Array Expr → TermElabM α) : TermElabM α :=
withoutPostponingUniverseConstraints do
if binders.isEmpty then
k #[]
else
elabBindersAux binders k
def elabBinder {α} (binder : Syntax) (x : Expr → TermElabM α) : TermElabM α :=
elabBinders #[binder] fun fvars => x fvars[0]
@[builtinTermElab «forall»] def elabForall : TermElab := fun stx _ =>
match stx with
| `(forall $binders*, $term) =>
elabBinders binders fun xs => do
let e ← elabType term
mkForallFVars xs e
| _ => throwUnsupportedSyntax
@[builtinTermElab arrow] def elabArrow : TermElab := fun stx _ =>
match stx with
| `($dom:term -> $rng) => do
-- elaborate independently from each other
let dom ← elabType dom
let rng ← elabType rng
mkForall (← MonadQuotation.addMacroScope `a) BinderInfo.default dom rng
| _ => throwUnsupportedSyntax
@[builtinTermElab depArrow] def elabDepArrow : TermElab := fun stx _ =>
-- bracketedBinder `->` term
let binder := stx[0]
let term := stx[2]
elabBinders #[binder] fun xs => do
mkForallFVars xs (← elabType term)
/--
Auxiliary functions for converting `id_1 ... id_n` application into `#[id_1, ..., id_m]`
It is used at `expandFunBinders`. -/
private partial def getFunBinderIds? (stx : Syntax) : OptionT MacroM (Array Syntax) :=
let convertElem (stx : Syntax) : OptionT MacroM Syntax :=
match stx with
| `(_) => do let ident ← mkFreshIdent stx; pure ident
| `($id:ident) => return id
| _ => failure
match stx with
| `($f $args*) => do
let mut acc := #[].push (← convertElem f)
for arg in args do
acc := acc.push (← convertElem arg)
return acc
| _ =>
return #[].push (← convertElem stx)
/--
Auxiliary function for expanding `fun` notation binders. Recall that `fun` parser is defined as
```
def funBinder : Parser := implicitBinder <|> instBinder <|> termParser maxPrec
leading_parser unicodeSymbol "λ" "fun" >> many1 funBinder >> "=>" >> termParser
```
to allow notation such as `fun (a, b) => a + b`, where `(a, b)` should be treated as a pattern.
The result is a pair `(explicitBinders, newBody)`, where `explicitBinders` is syntax of the form
```
`(` ident `:` term `)`
```
which can be elaborated using `elabBinders`, and `newBody` is the updated `body` syntax.
We update the `body` syntax when expanding the pattern notation.
Example: `fun (a, b) => a + b` expands into `fun _a_1 => match _a_1 with | (a, b) => a + b`.
See local function `processAsPattern` at `expandFunBindersAux`.
The resulting `Bool` is true if a pattern was found. We use it "mark" a macro expansion. -/
partial def expandFunBinders (binders : Array Syntax) (body : Syntax) : MacroM (Array Syntax × Syntax × Bool) :=
let rec loop (body : Syntax) (i : Nat) (newBinders : Array Syntax) := do
if h : i < binders.size then
let binder := binders.get ⟨i, h⟩
let processAsPattern : Unit → MacroM (Array Syntax × Syntax × Bool) := fun _ => do
let pattern := binder
let major ← mkFreshIdent binder
let (binders, newBody, _) ← loop body (i+1) (newBinders.push $ mkExplicitBinder major (mkHole binder))
let newBody ← `(match $major:ident with | $pattern => $newBody)
pure (binders, newBody, true)
match binder with
| Syntax.node _ ``Lean.Parser.Term.implicitBinder _ => loop body (i+1) (newBinders.push binder)
| Syntax.node _ ``Lean.Parser.Term.strictImplicitBinder _ => loop body (i+1) (newBinders.push binder)
| Syntax.node _ ``Lean.Parser.Term.instBinder _ => loop body (i+1) (newBinders.push binder)
| Syntax.node _ ``Lean.Parser.Term.explicitBinder _ => loop body (i+1) (newBinders.push binder)
| Syntax.node _ ``Lean.Parser.Term.simpleBinder _ => loop body (i+1) (newBinders.push binder)
| Syntax.node _ ``Lean.Parser.Term.hole _ =>
let ident ← mkFreshIdent binder
let type := binder
loop body (i+1) (newBinders.push <| mkExplicitBinder ident type)
| Syntax.node _ ``Lean.Parser.Term.paren args =>
-- `(` (termParser >> parenSpecial)? `)`
-- parenSpecial := (tupleTail <|> typeAscription)?
let binderBody := binder[1]
if binderBody.isNone then
processAsPattern ()
else
let idents := binderBody[0]
let special := binderBody[1]
if special.isNone then
processAsPattern ()
else if special[0].getKind != `Lean.Parser.Term.typeAscription then
processAsPattern ()
else
-- typeAscription := `:` term
let type := special[0][1]
match (← getFunBinderIds? idents) with
| some idents => loop body (i+1) (newBinders ++ idents.map (fun ident => mkExplicitBinder ident type))
| none => processAsPattern ()
| Syntax.ident .. =>
let type := mkHole binder
loop body (i+1) (newBinders.push <| mkExplicitBinder binder type)
| _ => processAsPattern ()
else
pure (newBinders, body, false)
loop body 0 #[]
namespace FunBinders
structure State where
fvars : Array Expr := #[]
lctx : LocalContext
localInsts : LocalInstances
expectedType? : Option Expr := none
private def propagateExpectedType (fvar : Expr) (fvarType : Expr) (s : State) : TermElabM State := do
match s.expectedType? with
| none => pure s
| some expectedType =>
let expectedType ← whnfForall expectedType
match expectedType with
| Expr.forallE _ d b _ =>
discard <| isDefEq fvarType d
let b := b.instantiate1 fvar
pure { s with expectedType? := some b }
| _ =>
pure { s with expectedType? := none }
private partial def elabFunBinderViews (binderViews : Array BinderView) (i : Nat) (s : State) : TermElabM State := do
if h : i < binderViews.size then
let binderView := binderViews.get ⟨i, h⟩
ensureAtomicBinderName binderView
withRef binderView.type <| withLCtx s.lctx s.localInsts do
let type ← elabType binderView.type
registerFailedToInferBinderTypeInfo type binderView.type
let fvarId ← mkFreshFVarId
let fvar := mkFVar fvarId
let s := { s with fvars := s.fvars.push fvar }
-- dbgTrace (toString binderView.id.getId ++ " : " ++ toString type)
/-
We do **not** want to support default and auto arguments in lambda abstractions.
Example: `fun (x : Nat := 10) => x+1`.
We do not believe this is an useful feature, and it would complicate the logic here.
-/
let lctx := s.lctx.mkLocalDecl fvarId binderView.id.getId type binderView.bi
addTermInfo (lctx? := some lctx) (isBinder := true) binderView.id fvar
let s ← withRef binderView.id <| propagateExpectedType fvar type s
let s := { s with lctx := lctx }
match (← isClass? type) with
| none => elabFunBinderViews binderViews (i+1) s
| some className =>
resettingSynthInstanceCache do
let localInsts := s.localInsts.push { className := className, fvar := mkFVar fvarId }
elabFunBinderViews binderViews (i+1) { s with localInsts := localInsts }
else
pure s
partial def elabFunBindersAux (binders : Array Syntax) (i : Nat) (s : State) : TermElabM State := do
if h : i < binders.size then
let binderViews ← matchBinder (binders.get ⟨i, h⟩)
let s ← elabFunBinderViews binderViews 0 s
elabFunBindersAux binders (i+1) s
else
pure s
end FunBinders
def elabFunBinders {α} (binders : Array Syntax) (expectedType? : Option Expr) (x : Array Expr → Option Expr → TermElabM α) : TermElabM α :=
if binders.isEmpty then
x #[] expectedType?
else do
let lctx ← getLCtx
let localInsts ← getLocalInstances
let s ← FunBinders.elabFunBindersAux binders 0 { lctx := lctx, localInsts := localInsts, expectedType? := expectedType? }
resettingSynthInstanceCacheWhen (s.localInsts.size > localInsts.size) <| withLCtx s.lctx s.localInsts <|
x s.fvars s.expectedType?
/- Helper function for `expandEqnsIntoMatch` -/
private def getMatchAltsNumPatterns (matchAlts : Syntax) : Nat :=
let alt0 := matchAlts[0][0]
let pats := alt0[1].getSepArgs
pats.size
def expandWhereDecls (whereDecls : Syntax) (body : Syntax) : MacroM Syntax :=
match whereDecls with
| `(whereDecls|where $[$decls:letRecDecl $[;]?]*) => `(let rec $decls:letRecDecl,*; $body)
| _ => Macro.throwUnsupported
def expandWhereDeclsOpt (whereDeclsOpt : Syntax) (body : Syntax) : MacroM Syntax :=
if whereDeclsOpt.isNone then
body
else
expandWhereDecls whereDeclsOpt[0] body
/- Helper function for `expandMatchAltsIntoMatch` -/
private def expandMatchAltsIntoMatchAux (matchAlts : Syntax) (matchTactic : Bool) : Nat → Array Syntax → MacroM Syntax
| 0, discrs => do
if matchTactic then
`(tactic|match $[$discrs:term],* with $matchAlts:matchAlts)
else
`(match $[$discrs:term],* with $matchAlts:matchAlts)
| n+1, discrs => withFreshMacroScope do
let x ← `(x)
let d ← `(@$x:ident) -- See comment below
let body ← expandMatchAltsIntoMatchAux matchAlts matchTactic n (discrs.push d)
if matchTactic then
`(tactic| intro $x:term; $body:tactic)
else
`(@fun $x => $body)
/--
Expand `matchAlts` syntax into a full `match`-expression.
Example
```
| 0, true => alt_1
| i, _ => alt_2
```
expands into (for tactic == false)
```
fun x_1 x_2 =>
match @x_1, @x_2 with
| 0, true => alt_1
| i, _ => alt_2
```
and (for tactic == true)
```
intro x_1; intro x_2;
match @x_1, @x_2 with
| 0, true => alt_1
| i, _ => alt_2
```
Remark: we add `@` to make sure we don't consume implicit arguments, and to make the behavior consistent with `fun`.
Example:
```
inductive T : Type 1 :=
| mkT : (forall {a : Type}, a -> a) -> T
def makeT (f : forall {a : Type}, a -> a) : T :=
mkT f
def makeT' : (forall {a : Type}, a -> a) -> T
| f => mkT f
```
The two definitions should be elaborated without errors and be equivalent.
-/
def expandMatchAltsIntoMatch (ref : Syntax) (matchAlts : Syntax) (tactic := false) : MacroM Syntax :=
withRef ref <| expandMatchAltsIntoMatchAux matchAlts tactic (getMatchAltsNumPatterns matchAlts) #[]
def expandMatchAltsIntoMatchTactic (ref : Syntax) (matchAlts : Syntax) : MacroM Syntax :=
withRef ref <| expandMatchAltsIntoMatchAux matchAlts true (getMatchAltsNumPatterns matchAlts) #[]
/--
Similar to `expandMatchAltsIntoMatch`, but supports an optional `where` clause.
Expand `matchAltsWhereDecls` into `let rec` + `match`-expression.
Example
```
| 0, true => ... f 0 ...
| i, _ => ... f i + g i ...
where
f x := g x + 1
g : Nat → Nat
| 0 => 1
| x+1 => f x
```
expands into
```
fux x_1 x_2 =>
let rec
f x := g x + 1,
g : Nat → Nat
| 0 => 1
| x+1 => f x
match x_1, x_2 with
| 0, true => ... f 0 ...
| i, _ => ... f i + g i ...
```
-/
def expandMatchAltsWhereDecls (matchAltsWhereDecls : Syntax) : MacroM Syntax :=
let matchAlts := matchAltsWhereDecls[0]
let whereDeclsOpt := matchAltsWhereDecls[1]
let rec loop (i : Nat) (discrs : Array Syntax) : MacroM Syntax :=
match i with
| 0 => do
let matchStx ← `(match $[$discrs:term],* with $matchAlts:matchAlts)
if whereDeclsOpt.isNone then
return matchStx
else
expandWhereDeclsOpt whereDeclsOpt matchStx
| n+1 => withFreshMacroScope do
let d ← `(@x) -- See comment at `expandMatchAltsIntoMatch`
let body ← loop n (discrs.push d)
`(@fun x => $body)
loop (getMatchAltsNumPatterns matchAlts) #[]
@[builtinMacro Lean.Parser.Term.fun] partial def expandFun : Macro
| `(fun $binders* => $body) => do
let (binders, body, expandedPattern) ← expandFunBinders binders body
if expandedPattern then
`(fun $binders* => $body)
else
Macro.throwUnsupported
| stx@`(fun $m:matchAlts) => expandMatchAltsIntoMatch stx m
| _ => Macro.throwUnsupported
open Lean.Elab.Term.Quotation in
@[builtinQuotPrecheck Lean.Parser.Term.fun] def precheckFun : Precheck
| `(fun $binders* => $body) => do
let (binders, body, expandedPattern) ← liftMacroM <| expandFunBinders binders body
let mut ids := #[]
for b in binders do
for v in ← matchBinder b do
Quotation.withNewLocals ids <| precheck v.type
ids := ids.push v.id.getId
Quotation.withNewLocals ids <| precheck body
| _ => throwUnsupportedSyntax
@[builtinTermElab «fun»] partial def elabFun : TermElab := fun stx expectedType? =>
match stx with
| `(fun $binders* => $body) => do
-- We can assume all `match` binders have been iteratively expanded by the above macro here, though
-- we still need to call `expandFunBinders` once to obtain `binders` in a normal form
-- expected by `elabFunBinder`.
let (binders, body, expandedPattern) ← liftMacroM <| expandFunBinders binders body
elabFunBinders binders expectedType? fun xs expectedType? => do
/- We ensure the expectedType here since it will force coercions to be applied if needed.
If we just use `elabTerm`, then we will need to a coercion `Coe (α → β) (α → δ)` whenever there is a coercion `Coe β δ`,
and another instance for the dependent version. -/
let e ← elabTermEnsuringType body expectedType?
mkLambdaFVars xs e
| _ => throwUnsupportedSyntax
/- If `useLetExpr` is true, then a kernel let-expression `let x : type := val; body` is created.
Otherwise, we create a term of the form `(fun (x : type) => body) val`
The default elaboration order is `binders`, `typeStx`, `valStx`, and `body`.
If `elabBodyFirst == true`, then we use the order `binders`, `typeStx`, `body`, and `valStx`. -/
def elabLetDeclAux (id : Syntax) (binders : Array Syntax) (typeStx : Syntax) (valStx : Syntax) (body : Syntax)
(expectedType? : Option Expr) (useLetExpr : Bool) (elabBodyFirst : Bool) (usedLetOnly : Bool) : TermElabM Expr := do
let (type, val, arity) ← elabBinders binders fun xs => do
let type ← elabType typeStx
registerCustomErrorIfMVar type typeStx "failed to infer 'let' declaration type"
if elabBodyFirst then
let type ← mkForallFVars xs type
let val ← mkFreshExprMVar type
pure (type, val, xs.size)
else
let val ← elabTermEnsuringType valStx type
let type ← mkForallFVars xs type
/- By default `mkLambdaFVars` and `mkLetFVars` create binders only for let-declarations that are actually used
in the body. This generates counterintuitive behavior in the elaborator since users will not be notified
about holes such as
```
def ex : Nat :=
let x := _
42
```
-/
let val ← mkLambdaFVars xs val (usedLetOnly := false)
pure (type, val, xs.size)
trace[Elab.let.decl] "{id.getId} : {type} := {val}"
let result ←
if useLetExpr then
withLetDecl id.getId type val fun x => do
addLocalVarInfo id x
let body ← elabTermEnsuringType body expectedType?
let body ← instantiateMVars body
mkLetFVars #[x] body (usedLetOnly := usedLetOnly)
else
let f ← withLocalDecl id.getId BinderInfo.default type fun x => do
addLocalVarInfo id x
let body ← elabTermEnsuringType body expectedType?
let body ← instantiateMVars body
mkLambdaFVars #[x] body (usedLetOnly := false)
pure <| mkLetFunAnnotation (mkApp f val)
if elabBodyFirst then
forallBoundedTelescope type arity fun xs type => do
let valResult ← elabTermEnsuringType valStx type
let valResult ← mkLambdaFVars xs valResult (usedLetOnly := false)
unless (← isDefEq val valResult) do
throwError "unexpected error when elaborating 'let'"
pure result
structure LetIdDeclView where
id : Syntax
binders : Array Syntax
type : Syntax
value : Syntax
def mkLetIdDeclView (letIdDecl : Syntax) : LetIdDeclView :=
-- `letIdDecl` is of the form `ident >> many bracketedBinder >> optType >> " := " >> termParser
let id := letIdDecl[0]
let binders := letIdDecl[1].getArgs
let optType := letIdDecl[2]
let type := expandOptType id optType
let value := letIdDecl[4]
{ id := id, binders := binders, type := type, value := value }
def expandLetEqnsDecl (letDecl : Syntax) : MacroM Syntax := do
let ref := letDecl
let matchAlts := letDecl[3]
let val ← expandMatchAltsIntoMatch ref matchAlts
return mkNode `Lean.Parser.Term.letIdDecl #[letDecl[0], letDecl[1], letDecl[2], mkAtomFrom ref " := ", val]
def elabLetDeclCore (stx : Syntax) (expectedType? : Option Expr) (useLetExpr : Bool) (elabBodyFirst : Bool) (usedLetOnly : Bool) : TermElabM Expr := do
let ref := stx
let letDecl := stx[1][0]
let body := stx[3]
if letDecl.getKind == `Lean.Parser.Term.letIdDecl then
let { id := id, binders := binders, type := type, value := val } := mkLetIdDeclView letDecl
elabLetDeclAux id binders type val body expectedType? useLetExpr elabBodyFirst usedLetOnly
else if letDecl.getKind == `Lean.Parser.Term.letPatDecl then
-- node `Lean.Parser.Term.letPatDecl $ try (termParser >> pushNone >> optType >> " := ") >> termParser
let pat := letDecl[0]
let optType := letDecl[2]
let type := expandOptType pat optType
let val := letDecl[4]
let stxNew ← `(let x : $type := $val; match x with | $pat => $body)
let stxNew := match useLetExpr, elabBodyFirst with
| true, false => stxNew
| true, true => stxNew.setKind `Lean.Parser.Term.«let_delayed»
| false, false => stxNew.setKind `Lean.Parser.Term.«let_fun»
| false, true => unreachable!
withMacroExpansion stx stxNew <| elabTerm stxNew expectedType?
else if letDecl.getKind == `Lean.Parser.Term.letEqnsDecl then
let letDeclIdNew ← liftMacroM <| expandLetEqnsDecl letDecl
let declNew := stx[1].setArg 0 letDeclIdNew
let stxNew := stx.setArg 1 declNew
withMacroExpansion stx stxNew <| elabTerm stxNew expectedType?
else
throwUnsupportedSyntax
@[builtinTermElab «let»] def elabLetDecl : TermElab :=
fun stx expectedType? => elabLetDeclCore stx expectedType? (useLetExpr := true) (elabBodyFirst := false) (usedLetOnly := false)
@[builtinTermElab «let_fun»] def elabLetFunDecl : TermElab :=
fun stx expectedType? => elabLetDeclCore stx expectedType? (useLetExpr := false) (elabBodyFirst := false) (usedLetOnly := false)
@[builtinTermElab «let_delayed»] def elabLetDelayedDecl : TermElab :=
fun stx expectedType? => elabLetDeclCore stx expectedType? (useLetExpr := true) (elabBodyFirst := true) (usedLetOnly := false)
@[builtinTermElab «let_tmp»] def elabLetTmpDecl : TermElab :=
fun stx expectedType? => elabLetDeclCore stx expectedType? (useLetExpr := true) (elabBodyFirst := false) (usedLetOnly := true)
builtin_initialize registerTraceClass `Elab.let
end Lean.Elab.Term
|
4facf83a538b594660107176e86e1945040645d2 | a9e33f9c83301c461f3c3ebc6799d9de1f6d4d20 | /assignments/hw6_prop_logic_and_satifiability.lean | 3706a9a2c822372152606433eaaf6a4601575be2 | [] | no_license | yl4df/Discrete-Mathematics | f1c9a6cf8cfb4686fb617637f69a481e1522f0c2 | c93ce9f6a6e36d194e350d9fa0a0360191e97fa0 | refs/heads/master | 1,598,714,938,443 | 1,572,275,647,000 | 1,572,275,647,000 | 218,074,726 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 8,601 | lean | namespace prop_logic
/-
This assignment has five problems. The first
is to extend our propositional logic syntax
and semantics to support the three additional
connectives, exclusive or (⊕), implies (which
we will write as ⇒), and if and only iff (↔).
We first give you the definitions developed
in class. You are to extend/modify them to
support expressions with the new connectives.
The remaining problems use this definition of
our language of expressions in propositional
logic.
-/
/-
1. Extend our syntax and semantics
for propositional logic to support
the xor, implies, and iff and only
iff connectives/operators.
A. Add support for the exclusive or
connective/operator. Define the symbol,
⊕, as an infix notation.
Here are specific steps to take for the
exclusive or connective, as an example.
1. Add new binary connective, xorOp
2. Add pXor as shorthand for binOpExp xorOp
3. Add ⊕ as an infix notation for pXor
4. Specify the interpretation of ⊕ to be bxor
5. Extend interpBinOp to handle the new case
Then add support for the implies connective,
using the symbol, ⇒, as an infix operator.
We can't use → because it's reserved by Lean
and cannot be overloaded. Lean does not have
a Boolean implies operator (analogous to bor),
so you will have to define one. Call it bimpl.
Finally add support for if and only iff. Use
the symbol ↔ as an infix notation. You will
have to define a Boolean function as Lean
does not provide one for iff. Call it biff.
Here is the code as developed in class.
Now review the step-by-step instructions,
and proceed to read and midify this logic
as required. We've bracketed areas where
new material will have to be added.
-/
/- *** SYNTAX *** -/
inductive var : Type
| mkVar : ℕ → var
inductive unOp : Type
| notOp
inductive binOp : Type
| andOp
| orOp
| xorOp
| implOp
| iffOp
inductive pExp : Type
| litExp : bool → pExp
| varExp : var → pExp
| unOpExp : unOp → pExp → pExp
| binOpExp : binOp → pExp → pExp → pExp
open var
open pExp
open unOp
open binOp
-- Shorthand notations
def pTrue := litExp tt
def pFalse := litExp ff
def pNot := unOpExp notOp
def pAnd := binOpExp andOp
def pOr := binOpExp orOp
def pXor := binOpExp xorOp
def pImpl := binOpExp implOp
def pIff := binOpExp iffOp
-- conventional notation
notation e1 ∧ e2 := pAnd e1 e2
notation e1 ∨ e2 := pOr e1 e2
notation ¬ e := pNot e
notation e1 ⊕ e2 := pXor e1 e2
notation e1 ⇒ e2 := pImpl e1 e2
notation e1 ↔ e2 := pIff e1 e2
/-
*****************
*** SEMANTICS ***
*****************
-/
def interpUnOp : unOp → (bool → bool)
| notOp := bnot
def bimpl: bool → bool → bool
| tt tt:= tt
| tt ff:= ff
| ff tt:= tt
| ff ff:= tt
def biff: bool → bool → bool
| tt tt:= tt
| tt ff:= ff
| ff tt:= ff
| ff ff:= tt
def interpBinOp : binOp → (bool → bool → bool)
| andOp := band
| orOp := bor
| xorOp := bxor
| implOp := bimpl
| iffOp := biff
/- *** SEMANTICS *** -/
/-
Given a pExp and an interpretation
for the variables, compute and return
the Boolean value of the expression.
-/
def pEval : pExp → (var → bool) → bool
| (litExp b) i := b
| (varExp v) i := i v
| (unOpExp op e) i :=
(interpUnOp op) (pEval e i)
| (binOpExp op e1 e2) i :=
(interpBinOp op)
(pEval e1 i)
(pEval e2 i)
/-
Note: You are free to use pEval, if you
wish to, to check answers to some of the
questions below. It is not mandatory and
you will not be marked down for not doing
this.
-/
/-
#2. Define X, Y, and Z to be variable
expressions bound to a different variable
expression terms. Hint: Look at the
prop_logic_test.lean file to remind
yourself how we did this in class.
-/
def X : pExp := varExp (mkVar 0)
def Y : pExp := varExp (mkVar 1)
def Z : pExp := varExp (mkVar 2)
/-
#3. Here are some English language
sentences that you are to re-express
in propositional logic. Here's one
example.
-/
/-
EXAMPLE:
Formalize the following proposition,
as a formula in propositional logic:
If it's raining then it's raining.
-/
-- Use R to represent "it's raining"
def R : pExp := varExp (mkVar 4)
-- Solution here
def p1 : pExp := pImpl R R
/-
Explanation: We first choose to represent
the smaller proposition, "it's raining",
by the variable expression, R. We then
formalize the overall natural language
expression, if R then R, as the formula,
R ⇒ R.
Note: R ⇒ R can be pronounced as any of:
- if R is true then R is true
- if R then R
- the truth of R implies the truth of R
- R implies R
The second and fourth pronounciations
are the two that we prefer to use.
-/
/-
For the remaining problems, use the
variables expressions, X, Y, and Z,
as already defined. Use parentheses
if needed to group sub-expressions.
-/
/-
A.
If it's raining and the streets are
wet then it's raining.
-/
def p2 : pExp := (X ∧ Y) ⇒ X
/-
B. If it's raining and the streets
are wet, then the streets are wet
and it's raining.
-/
def p3 := (X ∧ Y) ⇒ (Y ∧ X)
/-
C. If it's raining then if the
streets are wet then it's raining
and the streets are wet.
-/
def p4 := X ⇒ (Y ⇒ (X ∧ Y))
/-
D. If it's raining then it's
raining or the moon is made of
green cheese.
-/
def p5 := X ⇒ (X ∨ Z)
/-
E. If it's raining, then if it's
raining implies that the streets
are wet, then the streets are wet.
-/
def p6 := X ⇒ ((X ⇒ Y) ⇒ Y)
/-
#4. For each of the propositional
logic expressions below, write a truth
table and based on your result, state
whether the expression is unsatisfiable,
satisfiable but not valid, or valid.
Here's an example solution for the
expression, (X ∧ Y) ⇒ Y.
X Y X ∧ Y (X ∧ Y) ⇒ Y
- - ----- -----------
T T T T
T F F T
F T F T
F F F T
The proposition is valid.
-/
/-
A. After each "#check" give your
answer for the specified proposition.
That is, write a truth table in a
comment and then say whether given the
proposition is valid, satisfiable but
not valid, or unsatisifiable.
Note: This expression reqires that
you have properly specified ¬ and
⇒ as notations in our pExp language.
The errors indicated in many of the
following lines will go away once
you have these notations properly
defined.
-/
#check (X ⇒ Y) ⇒ (¬ X ⇒ ¬ Y)
/-
X Y ¬X ¬Y X⇒Y ¬X⇒¬Y (X⇒Y)⇒(¬X⇒¬Y)
- - -- -- --- ----- -------------
T T F F T T T
T F F T F T T
F T T F T F F
F F T T T T T
The proposition is satisfiable but not valid
-/
/-
B.
-/
#check ((X ⇒ Y) ∧ (Y ⇒ X)) ⇒ (X ⇒ Z)
/-
X Y Z X⇒Y Y⇒X (X⇒Y)∧(Y⇒X) (X⇒Z) ((X⇒Y)∧(Y⇒X))⇒(X⇒Z)
- - - --- --- ----------- ----- -------------------
T T T T T T T T
T T F T T T F F
T F T F T F T T
T F F F T F F T
F T T T F F T T
F T F T F F T T
F F T T T T T T
F F F T T T T T
The proposition is satisfiable but not valid
-/
/-
C.
-/
#check pFalse ⇒ (X ∧ ¬ X)
/-
pFalse X ¬X X∧¬X pFalse⇒(X∧¬X)
------ - -- ---- -------------
F T F F T
F F T F T
The proposition is valid
-/
/-
D.
-/
#check pTrue ⇒ (X ∧ ¬ X)
/-
pTrue X ¬X X∧¬X pTrue⇒(X∧¬X)
------ - -- ---- -------------
T T F F F
T F T F F
The proposition is unsatisifiable
-/
/-
E.
-/
#check (X ∨ Y) ∧ X ⇒ ¬ Y
/-
X Y ¬Y X∨Y (X∨Y)∧X (X∨Y)∧X⇒¬Y
- - -- --- ------- ----------
T T F T T F
T F T T T T
F T F T F T
F F T F F T
The proposition is satisfiable but not valid
-/
/-
#5.
A. Find and present an interpretation
that causes the following proposition
to be satisfied (to evaluate to true).
(X ∨ Y) ∧ (¬ Y ∨ Z)
Answer:
Let X be True, Y be True and Z be True. Then X ∨ Y is True and
¬ Y ∨ Z is also True. It follows that (X ∨ Y) ∧ (¬ Y ∨ Z) is True.
B. Count and state how many of the
possible interpretations satisfy the
formula.
Answer:
X Y Z X∨Y ¬Y ¬Y∨Z (X∨Y)∧(¬Y∨Z)
- - - --- -- ---- ------------
T T T T F T T
T T F T F F F
T F T T T T T
T F F T T T T
F T T T F T T
F T F T F F F
F F T F T T F
F F F F T T F
There are 4 interpretations satisfying the formula.
-/
end prop_logic
|
551b011b520fb12c14d36a93852d65b725fb8c9e | 367134ba5a65885e863bdc4507601606690974c1 | /src/analysis/normed_space/banach.lean | 9fd0d1f84f2ed2d62162f51423d7de37df087803 | [
"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 | 14,965 | lean | /-
Copyright (c) 2019 Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sébastien Gouëzel
-/
import topology.metric_space.baire
import analysis.normed_space.operator_norm
/-!
# Banach open mapping theorem
This file contains the Banach open mapping theorem, i.e., the fact that a bijective
bounded linear map between Banach spaces has a bounded inverse.
-/
open function metric set filter finset
open_locale classical topological_space big_operators nnreal
variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜]
{E : Type*} [normed_group E] [normed_space 𝕜 E]
{F : Type*} [normed_group F] [normed_space 𝕜 F]
(f : E →L[𝕜] F)
include 𝕜
namespace continuous_linear_map
/-- A (possibly nonlinear) right inverse to a continuous linear map, which doesn't have to be
linear itself but which satisfies a bound `∥inverse x∥ ≤ C * ∥x∥`. A surjective continuous linear
map doesn't always have a continuous linear right inverse, but it always has a nonlinear inverse
in this sense, by Banach's open mapping theorem. -/
structure nonlinear_right_inverse :=
(to_fun : F → E)
(nnnorm : ℝ≥0)
(bound' : ∀ y, ∥to_fun y∥ ≤ nnnorm * ∥y∥)
(right_inv' : ∀ y, f (to_fun y) = y)
instance : has_coe_to_fun (nonlinear_right_inverse f) := ⟨_, λ fsymm, fsymm.to_fun⟩
@[simp] lemma nonlinear_right_inverse.right_inv {f : E →L[𝕜] F} (fsymm : nonlinear_right_inverse f)
(y : F) : f (fsymm y) = y :=
fsymm.right_inv' y
lemma nonlinear_right_inverse.bound {f : E →L[𝕜] F} (fsymm : nonlinear_right_inverse f) (y : F) :
∥fsymm y∥ ≤ fsymm.nnnorm * ∥y∥ :=
fsymm.bound' y
end continuous_linear_map
/-- Given a continuous linear equivalence, the inverse is in particular an instance of
`nonlinear_right_inverse` (which turns out to be linear). -/
noncomputable def continuous_linear_equiv.to_nonlinear_right_inverse (f : E ≃L[𝕜] F) :
continuous_linear_map.nonlinear_right_inverse (f : E →L[𝕜] F) :=
{ to_fun := f.inv_fun,
nnnorm := nnnorm (f.symm : F →L[𝕜] E),
bound' := λ y, continuous_linear_map.le_op_norm (f.symm : F →L[𝕜] E) _,
right_inv' := f.apply_symm_apply }
noncomputable instance (f : E ≃L[𝕜] F) :
inhabited (continuous_linear_map.nonlinear_right_inverse (f : E →L[𝕜] F)) :=
⟨f.to_nonlinear_right_inverse⟩
/-! ### Proof of the Banach open mapping theorem -/
variable [complete_space F]
/--
First step of the proof of the Banach open mapping theorem (using completeness of `F`):
by Baire's theorem, there exists a ball in `E` whose image closure has nonempty interior.
Rescaling everything, it follows that any `y ∈ F` is arbitrarily well approached by
images of elements of norm at most `C * ∥y∥`.
For further use, we will only need such an element whose image
is within distance `∥y∥/2` of `y`, to apply an iterative process. -/
lemma exists_approx_preimage_norm_le (surj : surjective f) :
∃C ≥ 0, ∀y, ∃x, dist (f x) y ≤ 1/2 * ∥y∥ ∧ ∥x∥ ≤ C * ∥y∥ :=
begin
have A : (⋃n:ℕ, closure (f '' (ball 0 n))) = univ,
{ refine subset.antisymm (subset_univ _) (λy hy, _),
rcases surj y with ⟨x, hx⟩,
rcases exists_nat_gt (∥x∥) with ⟨n, hn⟩,
refine mem_Union.2 ⟨n, subset_closure _⟩,
refine (mem_image _ _ _).2 ⟨x, ⟨_, hx⟩⟩,
rwa [mem_ball, dist_eq_norm, sub_zero] },
have : ∃ (n : ℕ) x, x ∈ interior (closure (f '' (ball 0 n))) :=
nonempty_interior_of_Union_of_closed (λn, is_closed_closure) A,
simp only [mem_interior_iff_mem_nhds, mem_nhds_iff] at this,
rcases this with ⟨n, a, ε, ⟨εpos, H⟩⟩,
rcases normed_field.exists_one_lt_norm 𝕜 with ⟨c, hc⟩,
refine ⟨(ε/2)⁻¹ * ∥c∥ * 2 * n, _, λy, _⟩,
{ refine mul_nonneg (mul_nonneg (mul_nonneg _ (norm_nonneg _)) (by norm_num)) _,
exacts [inv_nonneg.2 (div_nonneg (le_of_lt εpos) (by norm_num)), n.cast_nonneg] },
{ by_cases hy : y = 0,
{ use 0, simp [hy] },
{ rcases rescale_to_shell hc (half_pos εpos) hy with ⟨d, hd, ydlt, leyd, dinv⟩,
let δ := ∥d∥ * ∥y∥/4,
have δpos : 0 < δ :=
div_pos (mul_pos (norm_pos_iff.2 hd) (norm_pos_iff.2 hy)) (by norm_num),
have : a + d • y ∈ ball a ε,
by simp [dist_eq_norm, lt_of_le_of_lt ydlt.le (half_lt_self εpos)],
rcases metric.mem_closure_iff.1 (H this) _ δpos with ⟨z₁, z₁im, h₁⟩,
rcases (mem_image _ _ _).1 z₁im with ⟨x₁, hx₁, xz₁⟩,
rw ← xz₁ at h₁,
rw [mem_ball, dist_eq_norm, sub_zero] at hx₁,
have : a ∈ ball a ε, by { simp, exact εpos },
rcases metric.mem_closure_iff.1 (H this) _ δpos with ⟨z₂, z₂im, h₂⟩,
rcases (mem_image _ _ _).1 z₂im with ⟨x₂, hx₂, xz₂⟩,
rw ← xz₂ at h₂,
rw [mem_ball, dist_eq_norm, sub_zero] at hx₂,
let x := x₁ - x₂,
have I : ∥f x - d • y∥ ≤ 2 * δ := calc
∥f x - d • y∥ = ∥f x₁ - (a + d • y) - (f x₂ - a)∥ :
by { congr' 1, simp only [x, f.map_sub], abel }
... ≤ ∥f x₁ - (a + d • y)∥ + ∥f x₂ - a∥ :
norm_sub_le _ _
... ≤ δ + δ : begin
apply add_le_add,
{ rw [← dist_eq_norm, dist_comm], exact le_of_lt h₁ },
{ rw [← dist_eq_norm, dist_comm], exact le_of_lt h₂ }
end
... = 2 * δ : (two_mul _).symm,
have J : ∥f (d⁻¹ • x) - y∥ ≤ 1/2 * ∥y∥ := calc
∥f (d⁻¹ • x) - y∥ = ∥d⁻¹ • f x - (d⁻¹ * d) • y∥ :
by rwa [f.map_smul _, inv_mul_cancel, one_smul]
... = ∥d⁻¹ • (f x - d • y)∥ : by rw [mul_smul, smul_sub]
... = ∥d∥⁻¹ * ∥f x - d • y∥ : by rw [norm_smul, normed_field.norm_inv]
... ≤ ∥d∥⁻¹ * (2 * δ) : begin
apply mul_le_mul_of_nonneg_left I,
rw inv_nonneg,
exact norm_nonneg _
end
... = (∥d∥⁻¹ * ∥d∥) * ∥y∥ /2 : by { simp only [δ], ring }
... = ∥y∥/2 : by { rw [inv_mul_cancel, one_mul], simp [norm_eq_zero, hd] }
... = (1/2) * ∥y∥ : by ring,
rw ← dist_eq_norm at J,
have K : ∥d⁻¹ • x∥ ≤ (ε / 2)⁻¹ * ∥c∥ * 2 * ↑n * ∥y∥ := calc
∥d⁻¹ • x∥ = ∥d∥⁻¹ * ∥x₁ - x₂∥ : by rw [norm_smul, normed_field.norm_inv]
... ≤ ((ε / 2)⁻¹ * ∥c∥ * ∥y∥) * (n + n) : begin
refine mul_le_mul dinv _ (norm_nonneg _) _,
{ exact le_trans (norm_sub_le _ _) (add_le_add (le_of_lt hx₁) (le_of_lt hx₂)) },
{ apply mul_nonneg (mul_nonneg _ (norm_nonneg _)) (norm_nonneg _),
exact inv_nonneg.2 (le_of_lt (half_pos εpos)) }
end
... = (ε / 2)⁻¹ * ∥c∥ * 2 * ↑n * ∥y∥ : by ring,
exact ⟨d⁻¹ • x, J, K⟩ } },
end
variable [complete_space E]
/-- The Banach open mapping theorem: if a bounded linear map between Banach spaces is onto, then
any point has a preimage with controlled norm. -/
theorem exists_preimage_norm_le (surj : surjective f) :
∃C > 0, ∀y, ∃x, f x = y ∧ ∥x∥ ≤ C * ∥y∥ :=
begin
obtain ⟨C, C0, hC⟩ := exists_approx_preimage_norm_le f surj,
/- Second step of the proof: starting from `y`, we want an exact preimage of `y`. Let `g y` be
the approximate preimage of `y` given by the first step, and `h y = y - f(g y)` the part that
has no preimage yet. We will iterate this process, taking the approximate preimage of `h y`,
leaving only `h^2 y` without preimage yet, and so on. Let `u n` be the approximate preimage
of `h^n y`. Then `u` is a converging series, and by design the sum of the series is a
preimage of `y`. This uses completeness of `E`. -/
choose g hg using hC,
let h := λy, y - f (g y),
have hle : ∀y, ∥h y∥ ≤ (1/2) * ∥y∥,
{ assume y,
rw [← dist_eq_norm, dist_comm],
exact (hg y).1 },
refine ⟨2 * C + 1, by linarith, λy, _⟩,
have hnle : ∀n:ℕ, ∥(h^[n]) y∥ ≤ (1/2)^n * ∥y∥,
{ assume n,
induction n with n IH,
{ simp only [one_div, nat.nat_zero_eq_zero, one_mul, iterate_zero_apply,
pow_zero] },
{ rw [iterate_succ'],
apply le_trans (hle _) _,
rw [pow_succ, mul_assoc],
apply mul_le_mul_of_nonneg_left IH,
norm_num } },
let u := λn, g((h^[n]) y),
have ule : ∀n, ∥u n∥ ≤ (1/2)^n * (C * ∥y∥),
{ assume n,
apply le_trans (hg _).2 _,
calc C * ∥(h^[n]) y∥ ≤ C * ((1/2)^n * ∥y∥) : mul_le_mul_of_nonneg_left (hnle n) C0
... = (1 / 2) ^ n * (C * ∥y∥) : by ring },
have sNu : summable (λn, ∥u n∥),
{ refine summable_of_nonneg_of_le (λn, norm_nonneg _) ule _,
exact summable.mul_right _ (summable_geometric_of_lt_1 (by norm_num) (by norm_num)) },
have su : summable u := summable_of_summable_norm sNu,
let x := tsum u,
have x_ineq : ∥x∥ ≤ (2 * C + 1) * ∥y∥ := calc
∥x∥ ≤ ∑'n, ∥u n∥ : norm_tsum_le_tsum_norm sNu
... ≤ ∑'n, (1/2)^n * (C * ∥y∥) :
tsum_le_tsum ule sNu (summable.mul_right _ summable_geometric_two)
... = (∑'n, (1/2)^n) * (C * ∥y∥) : tsum_mul_right
... = 2 * C * ∥y∥ : by rw [tsum_geometric_two, mul_assoc]
... ≤ 2 * C * ∥y∥ + ∥y∥ : le_add_of_nonneg_right (norm_nonneg y)
... = (2 * C + 1) * ∥y∥ : by ring,
have fsumeq : ∀n:ℕ, f (∑ i in finset.range n, u i) = y - (h^[n]) y,
{ assume n,
induction n with n IH,
{ simp [f.map_zero] },
{ rw [sum_range_succ, f.map_add, IH, iterate_succ'],
simp [u, h, sub_eq_add_neg, add_comm, add_left_comm] } },
have : tendsto (λn, ∑ i in range n, u i) at_top (𝓝 x) :=
su.has_sum.tendsto_sum_nat,
have L₁ : tendsto (λn, f(∑ i in range n, u i)) at_top (𝓝 (f x)) :=
(f.continuous.tendsto _).comp this,
simp only [fsumeq] at L₁,
have L₂ : tendsto (λn, y - (h^[n]) y) at_top (𝓝 (y - 0)),
{ refine tendsto_const_nhds.sub _,
rw tendsto_iff_norm_tendsto_zero,
simp only [sub_zero],
refine squeeze_zero (λ_, norm_nonneg _) hnle _,
rw [← zero_mul ∥y∥],
refine (tendsto_pow_at_top_nhds_0_of_lt_1 _ _).mul tendsto_const_nhds; norm_num },
have feq : f x = y - 0 := tendsto_nhds_unique L₁ L₂,
rw sub_zero at feq,
exact ⟨x, feq, x_ineq⟩
end
/-- The Banach open mapping theorem: a surjective bounded linear map between Banach spaces is
open. -/
theorem open_mapping (surj : surjective f) : is_open_map f :=
begin
assume s hs,
rcases exists_preimage_norm_le f surj with ⟨C, Cpos, hC⟩,
refine is_open_iff.2 (λy yfs, _),
rcases mem_image_iff_bex.1 yfs with ⟨x, xs, fxy⟩,
rcases is_open_iff.1 hs x xs with ⟨ε, εpos, hε⟩,
refine ⟨ε/C, div_pos εpos Cpos, λz hz, _⟩,
rcases hC (z-y) with ⟨w, wim, wnorm⟩,
have : f (x + w) = z, by { rw [f.map_add, wim, fxy, add_sub_cancel'_right] },
rw ← this,
have : x + w ∈ ball x ε := calc
dist (x+w) x = ∥w∥ : by { rw dist_eq_norm, simp }
... ≤ C * ∥z - y∥ : wnorm
... < C * (ε/C) : begin
apply mul_lt_mul_of_pos_left _ Cpos,
rwa [mem_ball, dist_eq_norm] at hz,
end
... = ε : mul_div_cancel' _ (ne_of_gt Cpos),
exact set.mem_image_of_mem _ (hε this)
end
/-! ### Applications of the Banach open mapping theorem -/
namespace continuous_linear_map
lemma exists_nonlinear_right_inverse_of_surjective (f : E →L[𝕜] F) (hsurj : f.range = ⊤) :
∃ (fsymm : nonlinear_right_inverse f), 0 < fsymm.nnnorm :=
begin
choose C hC fsymm h using exists_preimage_norm_le _ (linear_map.range_eq_top.mp hsurj),
use { to_fun := fsymm,
nnnorm := ⟨C, hC.lt.le⟩,
bound' := λ y, (h y).2,
right_inv' := λ y, (h y).1 },
exact hC
end
/-- A surjective continuous linear map between Banach spaces admits a (possibly nonlinear)
controlled right inverse. In general, it is not possible to ensure that such a right inverse
is linear (take for instance the map from `E` to `E/F` where `F` is a closed subspace of `E`
without a closed complement. Then it doesn't have a continuous linear right inverse.) -/
@[irreducible] noncomputable def nonlinear_right_inverse_of_surjective
(f : E →L[𝕜] F) (hsurj : f.range = ⊤) : nonlinear_right_inverse f :=
classical.some (exists_nonlinear_right_inverse_of_surjective f hsurj)
lemma nonlinear_right_inverse_of_surjective_nnnorm_pos (f : E →L[𝕜] F) (hsurj : f.range = ⊤) :
0 < (nonlinear_right_inverse_of_surjective f hsurj).nnnorm :=
begin
rw nonlinear_right_inverse_of_surjective,
exact classical.some_spec (exists_nonlinear_right_inverse_of_surjective f hsurj)
end
end continuous_linear_map
namespace linear_equiv
/-- If a bounded linear map is a bijection, then its inverse is also a bounded linear map. -/
@[continuity]
theorem continuous_symm (e : E ≃ₗ[𝕜] F) (h : continuous e) :
continuous e.symm :=
begin
rw continuous_def,
intros s hs,
rw [← e.image_eq_preimage],
rw [← e.coe_coe] at h ⊢,
exact open_mapping ⟨↑e, h⟩ e.surjective s hs
end
/-- Associating to a linear equivalence between Banach spaces a continuous linear equivalence when
the direct map is continuous, thanks to the Banach open mapping theorem that ensures that the
inverse map is also continuous. -/
def to_continuous_linear_equiv_of_continuous (e : E ≃ₗ[𝕜] F) (h : continuous e) :
E ≃L[𝕜] F :=
{ continuous_to_fun := h,
continuous_inv_fun := e.continuous_symm h,
..e }
@[simp] lemma coe_fn_to_continuous_linear_equiv_of_continuous (e : E ≃ₗ[𝕜] F) (h : continuous e) :
⇑(e.to_continuous_linear_equiv_of_continuous h) = e := rfl
@[simp] lemma coe_fn_to_continuous_linear_equiv_of_continuous_symm (e : E ≃ₗ[𝕜] F)
(h : continuous e) :
⇑(e.to_continuous_linear_equiv_of_continuous h).symm = e.symm := rfl
end linear_equiv
namespace continuous_linear_equiv
/-- Convert a bijective continuous linear map `f : E →L[𝕜] F` between two Banach spaces
to a continuous linear equivalence. -/
noncomputable def of_bijective (f : E →L[𝕜] F) (hinj : f.ker = ⊥) (hsurj : f.range = ⊤) :
E ≃L[𝕜] F :=
(linear_equiv.of_bijective ↑f hinj hsurj).to_continuous_linear_equiv_of_continuous f.continuous
@[simp] lemma coe_fn_of_bijective (f : E →L[𝕜] F) (hinj : f.ker = ⊥) (hsurj : f.range = ⊤) :
⇑(of_bijective f hinj hsurj) = f := rfl
@[simp] lemma of_bijective_symm_apply_apply (f : E →L[𝕜] F) (hinj : f.ker = ⊥)
(hsurj : f.range = ⊤) (x : E) :
(of_bijective f hinj hsurj).symm (f x) = x :=
(of_bijective f hinj hsurj).symm_apply_apply x
@[simp] lemma of_bijective_apply_symm_apply (f : E →L[𝕜] F) (hinj : f.ker = ⊥)
(hsurj : f.range = ⊤) (y : F) :
f ((of_bijective f hinj hsurj).symm y) = y :=
(of_bijective f hinj hsurj).apply_symm_apply y
end continuous_linear_equiv
|
96f1320cba5e1ac97038b9a0a45fb515c7464546 | 9be442d9ec2fcf442516ed6e9e1660aa9071b7bd | /tests/compiler/lazylist.lean | e361440f85ba9008dc81e6edb75660d6cb457627 | [
"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 | 4,046 | lean | /-
Copyright (c) 2019 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
-/
universe u v w
inductive LazyList (α : Type u) where
| nil : LazyList α
| cons (hd : α) (tl : LazyList α) : LazyList α
| delayed (t : Thunk $ LazyList α) : LazyList α
@[extern c inline "#2"]
def List.toLazy {α : Type u} : List α → LazyList α
| [] => LazyList.nil
| h::t => LazyList.cons h (toLazy t)
namespace LazyList
variable {α : Type u} {β : Type v} {δ : Type w}
instance : Inhabited (LazyList α) :=
⟨nil⟩
@[inline] def pure : α → LazyList α
| a => cons a nil
partial def isEmpty : LazyList α → Bool
| nil => true
| cons _ _ => false
| delayed as => isEmpty as.get
partial def toList : LazyList α → List α
| nil => []
| cons a as => a :: toList as
| delayed as => toList as.get
partial def head [Inhabited α] : LazyList α → α
| nil => default
| cons a as => a
| delayed as => head as.get
partial def tail : LazyList α → LazyList α
| nil => nil
| cons a as => as
| delayed as => tail as.get
partial def append : LazyList α → (Unit → LazyList α) → LazyList α
| nil, bs => bs ()
| cons a as, bs => delayed (cons a (append as bs))
| delayed as, bs => delayed (append as.get bs)
instance : Append (LazyList α) where
append a b := LazyList.append a (fun _ => b)
partial def interleave : LazyList α → LazyList α → LazyList α
| nil, bs => bs
| cons a as, bs => delayed (cons a (interleave bs as))
| delayed as, bs => delayed (interleave as.get bs)
partial def map (f : α → β) : LazyList α → LazyList β
| nil => nil
| cons a as => delayed (cons (f a) (map f as))
| delayed as => delayed (map f as.get)
partial def map₂ (f : α → β → δ) : LazyList α → LazyList β → LazyList δ
| nil, _ => nil
| _, nil => nil
| cons a as, cons b bs => delayed (cons (f a b) (map₂ f as bs))
| delayed as, bs => delayed (map₂ f as.get bs)
| as, delayed bs => delayed (map₂ f as bs.get)
@[inline] def zip : LazyList α → LazyList β → LazyList (α × β) :=
map₂ Prod.mk
partial def join : LazyList (LazyList α) → LazyList α
| nil => nil
| cons a as => delayed (append a fun _ => join as)
| delayed as => delayed (join as.get)
@[inline] partial def bind (x : LazyList α) (f : α → LazyList β) : LazyList β :=
join (x.map f)
instance isMonad : Monad LazyList where
pure := LazyList.pure
bind := LazyList.bind
map := LazyList.map
instance : Alternative LazyList where
failure := nil
orElse := LazyList.append
partial def approx : Nat → LazyList α → List α
| 0, as => []
| _, nil => []
| i+1, cons a as => a :: approx i as
| i+1, delayed as => approx (i+1) as.get
partial def iterate (f : α → α) : α → LazyList α
| x => cons x (delayed (iterate f (f x)))
partial def iterate₂ (f : α → α → α) : α → α → LazyList α
| x, y => cons x (delayed (iterate₂ f y (f x y)))
partial def filter (p : α → Bool) : LazyList α → LazyList α
| nil => nil
| cons a as => delayed (if p a then cons a (filter p as) else filter p as)
| delayed as => delayed (filter p as.get)
end LazyList
def fib : LazyList Nat :=
LazyList.iterate₂ (· + ·) 0 1
def iota (i : Nat := 0) : LazyList Nat :=
LazyList.iterate Nat.succ i
def tst : LazyList String := do
let x ← [1, 2, 3].toLazy
let y ← [2, 3, 4].toLazy
guard (x + y > 5)
pure s!"{x} + {y} = {x+y}"
def main : IO Unit := do
let n := 40
IO.println tst.isEmpty
IO.println tst.head
IO.println <| fib.interleave (iota.map (· + 100)) |>.approx n
IO.println <| iota.map (· + 10) |>.filter (· % 2 == 0) |>.approx n
|
75a190724cdd52f9dc26ad57bca0cff3b4427145 | 54deab7025df5d2df4573383df7e1e5497b7a2c2 | /data/list/comb.lean | 9a8ddacb9777584e6d90569960be28196724addd | [
"Apache-2.0"
] | permissive | HGldJ1966/mathlib | f8daac93a5b4ae805cfb0ecebac21a9ce9469009 | c5c5b504b918a6c5e91e372ee29ed754b0513e85 | refs/heads/master | 1,611,340,395,683 | 1,503,040,489,000 | 1,503,040,489,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 21,155 | lean | /-
Copyright (c) 2015 Leonardo de Moura. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura, Haitao Zhang, Floris van Doorn
List combinators.
-/
import data.list.basic data.bool
universes u v w
namespace list
open nat
variables {α : Type u} {β : Type v} {φ : Type w}
@[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 : β → α → β) :
∀ (b : β) (l₁ l₂ : list α), foldl f b (l₁++l₂) = foldl f (foldl f b l₁) l₂
| b [] l₂ := rfl
| b (a::l₁) l₂ := by simp [foldl_append]
@[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 [foldr_append]
theorem foldl_reverse (f : α → β → α) (a : α) (l : list β) : foldl f a (reverse l) = foldr (λx y, f y x) a l :=
by induction l; simp [*, foldl, 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
end list
-- TODO(Leo): uncomment data.equiv after refactoring
open nat prod decidable function
namespace list
universe variables uu vv ww
variables {α : Type uu} {β : Type vv} {γ : Type ww}
-- TODO(Jeremy): this file is a good testing ground for super and auto
section replicate
-- 'replicate n a' returns the list that contains n copies of a.
def replicate : ℕ → α → list α
| 0 a := []
| (succ n) a := a :: replicate n a
@[simp] theorem length_replicate : ∀ (i : ℕ) (a : α), length (replicate i a) = i
| 0 a := rfl
| (succ i) a := congr_arg succ (length_replicate i a)
end replicate
/- map -/
attribute [simp] map_cons
/-def map₂ (f : α → β → γ) : list α → list β → list γ
| [] _ := []
| _ [] := []
| (x::xs) (y::ys) := f x y :: map₂ xs ys-/
theorem map₂_nil1 (f : α → β → γ) : ∀ (l : list β), map₂ f [] l = []
| [] := rfl
| (a::y) := rfl
theorem map₂_nil2 (f : α → β → γ) : ∀ (l : list α), map₂ f l [] = []
| [] := rfl
| (a::y) := rfl
/- TODO(Jeremy): there is an overload ambiguity between min and nat.min -/
/-theorem length_map₂ : ∀ (f : α → β → γ) x y, length (map₂ f x y) = _root_.min (length x) (length y)
| f [] [] := rfl
| f (xh::xr) [] := rfl
| f [] (yh::yr) := rfl
| f (xh::xr) (yh::yr) := calc
length (map₂ f (xh::xr) (yh::yr))
= length (map₂ f xr yr) + 1 : rfl
... = _root_.min (length xr) (length yr) + 1 : by rw length_map₂
... = _root_.min (succ (length xr)) (succ (length yr))
: begin rw min_succ_succ, reflexivity end
... = _root_.min (length (xh::xr)) (length (yh::yr)) : rfl-/
section foldl_eq_foldr
-- foldl and foldr coincide when f is commutative and associative
variables {f : α → α → α} (hcomm : ∀ a b, f a b = f b a) (hassoc : ∀ a b c, f (f a b) c = f a (f b c))
include hcomm hassoc
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) :=
begin
change foldl f (f (f a b) c) l = f b (foldl f (f a c) l),
rw ←foldl_eq_of_comm_of_assoc,
change foldl f (f (f a b) c) l = foldl f (f (f a c) b) l,
have h₁ : f (f a b) c = f (f a c) b, { rw [hassoc, hassoc, hcomm b c] },
rw h₁
end
theorem foldl_eq_foldr : ∀ a l, foldl f a l = foldr f a l
| a nil := rfl
| a (b :: l) :=
begin
simp [foldl_eq_of_comm_of_assoc hcomm hassoc],
change f b (foldl f a l) = f b (foldr f a l),
rw (foldl_eq_foldr a l)
end
end foldl_eq_foldr
/- all & any -/
@[simp] theorem all_nil (p : α → bool) : all [] p = tt := rfl
@[simp] theorem all_cons (p : α → bool) (a : α) (l : list α) : all (a::l) p = (p a && all l p) := rfl
theorem all_eq_tt_of_forall {p : α → bool} : ∀ {l : list α}, (∀ a ∈ l, p a = tt) → all l p = tt
| [] h := all_nil p
| (a::l) h := begin
simp [all_cons, h a],
rw all_eq_tt_of_forall,
intros a ha, simp [h a, ha] end
theorem forall_mem_eq_tt_of_all_eq_tt {p : α → bool} :
∀ {l : list α}, all l p = tt → ∀ a ∈ l, p a = tt
| [] h := assume a h, absurd h (not_mem_nil a)
| (b::l) h := assume a, assume : a ∈ b::l,
begin
simp [bool.band_eq_tt] at h, cases h with h₁ h₂,
simp at this, cases this with h' h',
simp [*],
exact forall_mem_eq_tt_of_all_eq_tt h₂ _ h'
end
theorem all_eq_tt_iff {p : α → bool} {l : list α} : all l p = tt ↔ ∀ a ∈ l, p a = tt :=
iff.intro forall_mem_eq_tt_of_all_eq_tt all_eq_tt_of_forall
@[simp] theorem any_nil (p : α → bool) : any [] p = ff := rfl
@[simp] theorem any_cons (p : α → bool) (a : α) (l : list α) : any (a::l) p = (p a || any l p) := rfl
theorem any_of_mem {p : α → bool} {a : α} : ∀ {l : list α}, a ∈ l → p a = tt → any l p = tt
| [] i h := absurd i (not_mem_nil a)
| (b::l) i h :=
or.elim (eq_or_mem_of_mem_cons i)
(assume : a = b, begin simp [this.symm, bool.bor_eq_tt], exact (or.inl h) end)
(assume : a ∈ l, begin
cases (eq_or_mem_of_mem_cons i) with h' h',
{ simp [h'.symm, h] },
simp [bool.bor_eq_tt, any_of_mem h', h]
end)
theorem exists_of_any_eq_tt {p : α → bool} : ∀ {l : list α}, any l p = tt → ∃ a : α, a ∈ l ∧ p a
| [] h := begin simp at h, contradiction end
| (b::l) h := begin
simp [bool.bor_eq_tt] at h, cases h with h h,
{ existsi b, simp [h]},
cases (exists_of_any_eq_tt h) with a ha,
existsi a, apply (and.intro (or.inr ha.left) ha.right)
end
theorem any_eq_tt_iff {p : α → bool} {l : list α} : any l p = tt ↔ ∃ a : α, a ∈ l ∧ p a = tt :=
iff.intro exists_of_any_eq_tt (begin intro h, cases h with a ha, apply any_of_mem ha.left ha.right end)
/- bounded quantifiers over lists -/
theorem forall_mem_nil (p : α → Prop) : ∀ x ∈ @nil α, p x :=
assume x xnil, absurd xnil (not_mem_nil x)
theorem forall_mem_cons {p : α → Prop} {a : α} {l : list α} (pa : p a) (h : ∀ x ∈ l, p x) :
∀ x ∈ a :: l, p x :=
assume x xal, or.elim (eq_or_mem_of_mem_cons xal)
(assume : x = a, by simp [this, pa])
(assume : x ∈ l, by simp [this, h])
theorem of_forall_mem_cons {p : α → Prop} {a : α} {l : list α} (h : ∀ x ∈ a :: l, p x) : p a :=
h a (by simp)
theorem forall_mem_of_forall_mem_cons {p : α → Prop} {a : α} {l : list α}
(h : ∀ x ∈ a :: l, p x) :
∀ x ∈ l, p x :=
assume x xl, h x (by simp [xl])
@[simp] theorem forall_mem_cons_iff (p : α → Prop) (a : α) (l : list α) :
(∀ x ∈ a :: l, p x) ↔ p a ∧ ∀ x ∈ l, p x :=
iff.intro
(λ h, ⟨of_forall_mem_cons h, forall_mem_of_forall_mem_cons h⟩)
(λ h, forall_mem_cons h.left h.right)
theorem not_exists_mem_nil (p : α → Prop) : ¬ ∃ x ∈ @nil α, p x :=
assume h, bexists.elim h (λ a anil, absurd anil (not_mem_nil a))
theorem exists_mem_cons_of {p : α → Prop} {a : α} (l : list α) (h : p a) :
∃ x ∈ a :: l, p x :=
bexists.intro a (by simp) h
theorem exists_mem_cons_of_exists {p : α → Prop} {a : α} {l : list α} (h : ∃ x ∈ l, p x) :
∃ x ∈ a :: l, p x :=
bexists.elim h (λ x xl px, bexists.intro x (by simp [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 :=
bexists.elim h (λ x xal px,
or.elim (eq_or_mem_of_mem_cons xal)
(assume : x = a, begin rw ←this, simp [px] end)
(assume : x ∈ l, or.inr (bexists.intro x this px)))
@[simp] 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)
instance decidable_forall_mem {p : α → Prop} [h : decidable_pred p] :
∀ l : list α, decidable (∀ x ∈ l, p x)
| [] := is_true (forall_mem_nil p)
| (a :: l) := decidable_of_decidable_of_iff
(@and.decidable _ _ _ (decidable_forall_mem l))
(forall_mem_cons_iff p a l).symm
instance decidable_exists_mem {p : α → Prop} [h : decidable_pred p] :
∀ l : list α, decidable (∃ x ∈ l, p x)
| [] := is_false (not_exists_mem_nil p)
| (a :: l) := decidable_of_decidable_of_iff
(@or.decidable _ _ _ (decidable_exists_mem l))
(exists_mem_cons_iff p a l).symm
/- zip & unzip -/
@[simp] theorem zip_cons_cons (a : α) (b : β) (l₁ : list α) (l₂ : list β) :
zip (a :: l₁) (b :: l₂) = (a, b) :: zip l₁ l₂ := rfl
@[simp] theorem zip_nil_left (l : list α) : zip ([] : list β) l = [] := rfl
@[simp] theorem zip_nil_right (l : list α) : zip l ([] : list β) = [] :=
by cases l; refl
@[simp] theorem unzip_nil : unzip (@nil (α × β)) = ([], []) := rfl
theorem unzip_cons' (a : α) (b : β) (l : list (α × β)) :
unzip ((a, b) :: l) = match (unzip l) with (la, lb) := (a :: la, b :: lb) end := rfl
-- TODO(Jeremy): it seems this version is better for the simplifier
@[simp] theorem unzip_cons (a : α) (b : β) (l : list (α × β)) :
unzip ((a, b) :: l) = let p := unzip l in (a :: p.1, b :: p.2) :=
by rw unzip_cons'; cases unzip l; refl
theorem zip_unzip : ∀ (l : list (α × β)), zip (unzip l).1 (unzip l).2 = l
| [] := rfl
| ((a, b) :: l) := begin simp [zip_unzip l] end
-- TODO(Jeremy): this is as far as I got
section mapAccumR
variable {S : Type}
-- This runs a function over a list returning the intermediate results and a
-- a final result.
def mapAccumR : (α → S → S × β) → list α → S → (S × list β)
| f [] c := (c, [])
| f (y::yr) c :=
let r := mapAccumR f yr c in
let z := f y r.1 in
(z.1, z.2 :: r.2)
theorem length_mapAccumR :
∀ (f : α → S → S × β) (x : list α) (s : S),
length (mapAccumR f x s).2 = length x
| f (a::x) s := calc
length (snd (mapAccumR f (a::x) s))
= length x + 1 : begin rw ←(length_mapAccumR f x s), reflexivity end
... = length (a::x) : rfl
| f [] s := calc length (snd (mapAccumR f [] s)) = 0 : by reflexivity
end mapAccumR
section mapAccumR₂
variable {S : Type uu}
-- This runs a function over two lists returning the intermediate results and a
-- a final result.
def mapAccumR₂
: (α → β → S → S × γ) → list α → list β → S → S × list γ
| f [] _ c := (c,[])
| f _ [] c := (c,[])
| f (x::xr) (y::yr) c :=
let r := mapAccumR₂ f xr yr c in
let q := f x y r.1 in
(q.1, q.2 :: r.2)
-- TODO(Jeremy) : again the "min" overload
theorem length_mapAccumR₂ : ∀ (f : α → β → S → S × γ) (x : list α) (y : list β) (c : S),
length (mapAccumR₂ f x y c).2 = _root_.min (length x) (length y)
| f (a::x) (b::y) c := calc
length (snd (mapAccumR₂ f (a::x) (b::y) c))
= length (snd (mapAccumR₂ f x y c)) + 1 : rfl
... = _root_.min (length x) (length y) + 1 : by rw (length_mapAccumR₂ f x y c)
... = _root_.min (succ (length x)) (succ (length y)) : begin rw min_succ_succ end
... = _root_.min (length (a::x)) (length (b::y)) : rfl
| f (a::x) [] c := rfl
| f [] (b::y) c := rfl
| f [] [] c := rfl
end mapAccumR₂
/- flat -/
def flat (l : list (list α)) : list α :=
foldl append nil l
/- product -/
section product
def product : list α → list β → list (α × β)
| [] l₂ := []
| (a::l₁) l₂ := map (λ b, (a, b)) l₂ ++ product l₁ l₂
theorem nil_product (l : list β) : product (@nil α) l = [] := rfl
theorem product_cons (a : α) (l₁ : list α) (l₂ : list β)
: product (a::l₁) l₂ = map (λ b, (a, b)) l₂ ++ product l₁ l₂ := rfl
theorem product_nil : ∀ (l : list α), product l (@nil β) = []
| [] := rfl
| (a::l) := begin rw [product_cons, product_nil], reflexivity end
theorem eq_of_mem_map_pair₁ {a₁ a : α} {b₁ : β} {l : list β} :
(a₁, b₁) ∈ map (λ b, (a, b)) l → a₁ = a :=
assume ain,
have fst (a₁, b₁) ∈ map fst (map (λ b, (a, b)) l), from mem_map fst ain,
have a₁ ∈ map (λb, a) l, begin revert this, rw [map_map], intro this, assumption end,
eq_of_map_const this
theorem mem_of_mem_map_pair₁ {a₁ a : α} {b₁ : β} {l : list β} :
(a₁, b₁) ∈ map (λ b, (a, b)) l → b₁ ∈ l :=
assume ain,
have snd (a₁, b₁) ∈ map snd (map (λ b, (a, b)) l), from mem_map snd ain,
have b₁ ∈ map id l, begin rw [map_map] at this, exact this end,
begin rw [map_id] at this, exact this end
theorem mem_product {a : α} {b : β} : ∀ {l₁ l₂}, a ∈ l₁ → b ∈ l₂ → (a, b) ∈ @product α β l₁ l₂
| [] l₂ h₁ h₂ := absurd h₁ (not_mem_nil _)
| (x::l₁) l₂ h₁ h₂ :=
or.elim (eq_or_mem_of_mem_cons h₁)
(assume aeqx : a = x,
have (a, b) ∈ map (λ b, (a, b)) l₂, from mem_map _ h₂,
begin rw [←aeqx, product_cons], exact mem_append_left _ this end)
(assume ainl₁ : a ∈ l₁,
have (a, b) ∈ product l₁ l₂, from mem_product ainl₁ h₂,
begin rw [product_cons], exact mem_append_right _ this end)
theorem mem_of_mem_product_left {a : α} {b : β} : ∀ {l₁ l₂}, (a, b) ∈ @product α β l₁ l₂ → a ∈ l₁
| [] l₂ h := absurd h (not_mem_nil _)
| (x::l₁) l₂ h :=
or.elim (mem_append.1 h)
(assume : (a, b) ∈ map (λ b, (x, b)) l₂,
have a = x, from eq_of_mem_map_pair₁ this,
begin rw this, apply mem_cons_self end)
(assume : (a, b) ∈ product l₁ l₂,
have a ∈ l₁, from mem_of_mem_product_left this,
mem_cons_of_mem _ this)
theorem mem_of_mem_product_right {a : α} {b : β} : ∀ {l₁ l₂}, (a, b) ∈ @product α β l₁ l₂ → b ∈ l₂
| [] l₂ h := absurd h (not_mem_nil ((a, b)))
| (x::l₁) l₂ h :=
or.elim (mem_append.1 h)
(assume : (a, b) ∈ map (λ b, (x, b)) l₂,
mem_of_mem_map_pair₁ this)
(assume : (a, b) ∈ product l₁ l₂,
mem_of_mem_product_right this)
theorem length_product :
∀ (l₁ : list α) (l₂ : list β), length (product l₁ l₂) = length l₁ * length l₂
| [] l₂ := begin simp, reflexivity end
| (x::l₁) l₂ :=
have length (product l₁ l₂) = length l₁ * length l₂, from length_product l₁ l₂,
by rw [product_cons, length_append, length_cons,
length_map, this, right_distrib, one_mul, add_comm]
end product
-- new for list/comb dependent map theory
def dinj₁ (p : α → Prop) (f : Π a, p a → β) := ∀ ⦃a1 a2⦄ (h1 : p a1) (h2 : p a2), a1 ≠ a2 → (f a1 h1) ≠ (f a2 h2)
def dinj (p : α → Prop) (f : Π a, p a → β) := ∀ ⦃a1 a2⦄ (h1 : p a1) (h2 : p a2), (f a1 h1) = (f a2 h2) → a1 = a2
def dmap (p : α → Prop) [h : decidable_pred p] (f : Π a, p a → β) : list α → list β
| [] := []
| (a::l) := if P : (p a) then cons (f a P) (dmap l) else (dmap l)
-- properties of dmap
section dmap
variable {p : α → Prop}
variable [h : decidable_pred p]
include h
variable {f : Π a, p a → β}
theorem dmap_nil : dmap p f [] = [] := rfl
theorem dmap_cons_of_pos {a : α} (P : p a) : ∀ l, dmap p f (a::l) = (f a P) :: dmap p f l :=
λ l, dif_pos P
theorem dmap_cons_of_neg {a : α} (P : ¬ p a) : ∀ l, dmap p f (a::l) = dmap p f l :=
λ l, dif_neg P
theorem mem_dmap : ∀ {l : list α} {a} (Pa : p a), a ∈ l → (f a Pa) ∈ dmap p f l
| [] := assume a Pa Pinnil, absurd Pinnil (not_mem_nil _)
| (a::l) := assume b Pb Pbin, or.elim (eq_or_mem_of_mem_cons Pbin)
(assume Pbeqa, begin
rw [eq.symm Pbeqa, dmap_cons_of_pos Pb],
apply mem_cons_self
end)
(assume Pbinl,
if pa : p a then
begin
rw [dmap_cons_of_pos pa],
apply mem_cons_of_mem,
exact mem_dmap Pb Pbinl
end
else
begin
rw [dmap_cons_of_neg pa],
exact mem_dmap Pb Pbinl
end)
theorem exists_of_mem_dmap : ∀ {l : list α} {b : β}, b ∈ dmap p f l → ∃ a P, a ∈ l ∧ b = f a P
| [] := assume b, begin rw dmap_nil, intro h, exact absurd h (not_mem_nil _) end
| (a::l) := assume b,
if Pa : p a then
begin
rw [dmap_cons_of_pos Pa, mem_cons_iff],
intro Pb, cases Pb with Peq Pin,
exact exists.intro a (exists.intro Pa (and.intro (mem_cons_self _ _) Peq)),
have Pex : ∃ (a : α) (P : p a), a ∈ l ∧ b = f a P, exact exists_of_mem_dmap Pin,
cases Pex with a' Pex', cases Pex' with Pa' P',
exact exists.intro a' (exists.intro Pa' (and.intro (mem_cons_of_mem a (and.left P'))
(and.right P')))
end
else
begin
rw [dmap_cons_of_neg Pa],
intro Pin,
have Pex : ∃ (a : α) (P : p a), a ∈ l ∧ b = f a P, exact exists_of_mem_dmap Pin,
cases Pex with a' Pex', cases Pex' with Pa' P',
exact exists.intro a' (exists.intro Pa' (and.intro (mem_cons_of_mem a (and.left P'))
(and.right P')))
end
theorem map_dmap_of_inv_of_pos {g : β → α} (Pinv : ∀ a (Pa : p a), g (f a Pa) = a) :
∀ {l : list α}, (∀ ⦃a⦄, a ∈ l → p a) → map g (dmap p f l) = l
| [] := assume Pl, by rw [dmap_nil]; reflexivity
| (a::l) := assume Pal,
have Pa : p a, from Pal (mem_cons_self _ _),
have Pl : ∀ a, a ∈ l → p a,
from assume x Pxin, Pal (mem_cons_of_mem a Pxin),
by rw [dmap_cons_of_pos Pa, map_cons, Pinv, map_dmap_of_inv_of_pos Pl]
theorem mem_of_dinj_of_mem_dmap (Pdi : dinj p f) :
∀ {l : list α} {a} (Pa : p a), (f a Pa) ∈ dmap p f l → a ∈ l
| [] := assume a Pa Pinnil, absurd Pinnil (not_mem_nil _)
| (b::l) := assume a Pa Pmap,
if Pb : p b then
begin
rw (dmap_cons_of_pos Pb) at Pmap,
rw mem_cons_iff at Pmap,
rw mem_cons_iff,
cases Pmap with h h,
left, apply Pdi Pa Pb h,
right, apply mem_of_dinj_of_mem_dmap Pa h
end
else
begin
rw (dmap_cons_of_neg Pb) at Pmap,
apply mem_cons_of_mem,
exact mem_of_dinj_of_mem_dmap Pa Pmap
end
theorem not_mem_dmap_of_dinj_of_not_mem (Pdi : dinj p f) {l : list α} {a} (Pa : p a) :
a ∉ l → (f a Pa) ∉ dmap p f l :=
mt (mem_of_dinj_of_mem_dmap Pdi Pa)
end dmap
/-
section
open equiv
def list_equiv_of_equiv {α β : Type} : α ≃ β → list α ≃ list β
| (mk f g l r) :=
mk (map f) (map g)
begin intros, rw [map_map, id_of_left_inverse l, map_id], try reflexivity end
begin intros, rw [map_map, id_of_right_inverse r, map_id], try reflexivity end
private def to_nat : list nat → nat
| [] := 0
| (x::xs) := succ (mkpair (to_nat xs) x)
open prod.ops
private def of_nat.F : Π (n : nat), (Π m, m < n → list nat) → list nat
| 0 f := []
| (succ n) f := (unpair n).2 :: f (unpair n).1 (unpair_lt n)
private def of_nat : nat → list nat :=
well_founded.fix of_nat.F
private lemma of_nat_zero : of_nat 0 = [] :=
well_founded.fix_eq of_nat.F 0
private lemma of_nat_succ (n : nat)
: of_nat (succ n) = (unpair n).2 :: of_nat (unpair n).1 :=
well_founded.fix_eq of_nat.F (succ n)
private lemma to_nat_of_nat (n : nat) : to_nat (of_nat n) = n :=
nat.case_strong_induction_on n
_
(λ n ih,
begin
rw of_nat_succ, unfold to_nat,
have to_nat (of_nat (unpair n).1) = (unpair n).1, from ih _ (le_of_lt_succ (unpair_lt n)),
rw this, rw mkpair_unpair
end)
private lemma of_nat_to_nat : ∀ (l : list nat), of_nat (to_nat l) = l
| [] := rfl
| (x::xs) := begin unfold to_nat, rw of_nat_succ, rw *unpair_mkpair, esimp, congruence, apply of_nat_to_nat end
def list_nat_equiv_nat : list nat ≃ nat :=
mk to_nat of_nat of_nat_to_nat to_nat_of_nat
def list_equiv_self_of_equiv_nat {α : Type} : α ≃ nat → list α ≃ α :=
assume : α ≃ nat, calc
list α ≃ list nat : list_equiv_of_equiv this
... ≃ nat : list_nat_equiv_nat
... ≃ α : this
end
-/
end list
|
c31023f197841100e0b9f0304fcbaffbcd156988 | 80162757f50b09d3cad5564907e4c9b00742e045 | /frechet.lean | 577b888f4b058cc90eb8ad28bff272a63c9dd61d | [] | no_license | EdAyers/edlib | cc30d0a54fed347a85b6df6045f68e6b48bc71a3 | 78b8c5d91f023f939c102837d748868e2f3ed27d | refs/heads/master | 1,586,459,758,216 | 1,571,322,179,000 | 1,571,322,179,000 | 160,538,917 | 2 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,287 | lean | import order.filter analysis.metric_space
open filter set
/- Non standard analysis -/
/-- The filter of sets of natural numbers with finite complement. -/
def frechet : filter ℕ :=
{ sets := {X : set ℕ | set.finite (- X)}
, univ_sets := begin simp end
, sets_of_superset := λ A B h₁ h₂, finite_subset h₁ $ λ x h₃ h₄, h₃ $ h₂ h₄
, inter_sets := begin intros, simp, rw [compl_inter], apply finite_union, repeat {assumption} end
}
/- [TODO] show it's an ultrafilter. -/
/-- A sequence of reals. Think of them as 'schedules' -/
def rs := ℕ → ℝ
instance feq (ℱ : filter ℕ := frechet) : setoid rs :=
{ r := λ σ ρ, {n : ℕ| σ n = ρ n} ∈ ℱ.sets
, iseqv :=
⟨ λ σ, begin simp, apply ℱ.univ_sets end
, λ σ₁ σ₂ p , have h : {n : ℕ | σ₂ n = σ₁ n} = {n : ℕ | σ₁ n = σ₂ n}, from set.ext $ λ x, ⟨eq.symm,eq.symm⟩ , by simp [h]; apply p
, λ σ₁ σ₂ σ₃ p₁₂ p₂₃, ℱ.sets_of_superset (ℱ.inter_sets p₁₂ p₂₃) (λ n ⟨a,b⟩, eq.trans a b)
⟩
}
def hyperreal := @quotient rs feq
def of_real : real → hyperreal := λ x, @quotient.mk _ feq (λ n, x)
/-- An infinitesimal? -/
noncomputable def ε : hyperreal := @quotient.mk _ feq $ λ n, 1 / (n + 1)
/- [TODO] show it's a field. -/ |
8b142ff3530eb74e9458e89d196d0ff30dd8c545 | 8e2026ac8a0660b5a490dfb895599fb445bb77a0 | /tests/lean/run/record1.lean | 47a4795cd0bec17d87938f06aa8ca93a3c4683c5 | [
"Apache-2.0"
] | permissive | pcmoritz/lean | 6a8575115a724af933678d829b4f791a0cb55beb | 35eba0107e4cc8a52778259bb5392300267bfc29 | refs/heads/master | 1,607,896,326,092 | 1,490,752,175,000 | 1,490,752,175,000 | 86,612,290 | 0 | 0 | null | 1,490,809,641,000 | 1,490,809,641,000 | null | UTF-8 | Lean | false | false | 770 | lean | structure point (A : Type) (B : Type) :=
mk :: (x : A) (y : B)
#check point
#check @ point.rec
#check point.mk
#check point.x
#check point.y
#check point.rec_on
inductive color
| red | green | blue
structure color_point (A : Type) (B : Type) extends point A B :=
mk :: (c : color)
#check @color_point.rec_on
#check color_point.rec_on
#check color_point.to_point
section
variables a b : num
example : point.x (point.mk a b) = a :=
rfl
example : point.y (point.mk a b) = b :=
rfl
variables cc : color
example : color_point.x (color_point.mk a b cc) = a :=
rfl
example : color_point.y (color_point.mk a b cc) = b :=
rfl
example : color_point.c (color_point.mk a b cc) = cc :=
rfl
example : color_point.to_point (color_point.mk a b cc) = point.mk a b :=
rfl
end
|
501aa95dd0d6e7bde82fba3b8a21e33f79e078b6 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/category_theory/closed/monoidal.lean | 5016a124b2b3202cbe79ea3bff6d5dc61305f058 | [
"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 | 10,946 | lean | /-
Copyright (c) 2020 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Bhavik Mehta
-/
import category_theory.monoidal.functor
import category_theory.adjunction.limits
import category_theory.adjunction.mates
import category_theory.functor.inv_isos
/-!
# Closed monoidal categories
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
Define (right) closed objects and (right) closed monoidal categories.
## TODO
Some of the theorems proved about cartesian closed categories
should be generalised and moved to this file.
-/
universes v u u₂ v₂
namespace category_theory
open category monoidal_category
/-- An object `X` is (right) closed if `(X ⊗ -)` is a left adjoint. -/
-- Note that this class carries a particular choice of right adjoint,
-- (which is only unique up to isomorphism),
-- not merely the existence of such, and
-- so definitional properties of instances may be important.
class closed {C : Type u} [category.{v} C] [monoidal_category.{v} C] (X : C) :=
(is_adj : is_left_adjoint (tensor_left X))
/-- A monoidal category `C` is (right) monoidal closed if every object is (right) closed. -/
class monoidal_closed (C : Type u) [category.{v} C] [monoidal_category.{v} C] :=
(closed' : Π (X : C), closed X)
attribute [instance, priority 100] monoidal_closed.closed'
variables {C : Type u} [category.{v} C] [monoidal_category.{v} C]
/--
If `X` and `Y` are closed then `X ⊗ Y` is.
This isn't an instance because it's not usually how we want to construct internal homs,
we'll usually prove all objects are closed uniformly.
-/
def tensor_closed {X Y : C}
(hX : closed X) (hY : closed Y) : closed (X ⊗ Y) :=
{ is_adj :=
begin
haveI := hX.is_adj,
haveI := hY.is_adj,
exact adjunction.left_adjoint_of_nat_iso (monoidal_category.tensor_left_tensor _ _).symm
end }
/--
The unit object is always closed.
This isn't an instance because most of the time we'll prove closedness for all objects at once,
rather than just for this one.
-/
def unit_closed : closed (𝟙_ C) :=
{ is_adj :=
{ right := 𝟭 C,
adj := adjunction.mk_of_hom_equiv
{ hom_equiv := λ X _,
{ to_fun := λ a, (left_unitor X).inv ≫ a,
inv_fun := λ a, (left_unitor X).hom ≫ a,
left_inv := by tidy,
right_inv := by tidy },
hom_equiv_naturality_left_symm' := λ X' X Y f g,
by { dsimp, rw left_unitor_naturality_assoc } } } }
variables (A B : C) {X X' Y Y' Z : C}
variables [closed A]
/--
This is the internal hom `A ⟶[C] -`.
-/
def ihom : C ⥤ C :=
(@closed.is_adj _ _ _ A _).right
namespace ihom
/-- The adjunction between `A ⊗ -` and `A ⟹ -`. -/
def adjunction : tensor_left A ⊣ ihom A :=
closed.is_adj.adj
/-- The evaluation natural transformation. -/
def ev : ihom A ⋙ tensor_left A ⟶ 𝟭 C :=
(ihom.adjunction A).counit
/-- The coevaluation natural transformation. -/
def coev : 𝟭 C ⟶ tensor_left A ⋙ ihom A :=
(ihom.adjunction A).unit
@[simp] lemma ihom_adjunction_counit : (ihom.adjunction A).counit = ev A := rfl
@[simp] lemma ihom_adjunction_unit : (ihom.adjunction A).unit = coev A := rfl
@[simp, reassoc]
lemma ev_naturality {X Y : C} (f : X ⟶ Y) :
((𝟙 A) ⊗ ((ihom A).map f)) ≫ (ev A).app Y = (ev A).app X ≫ f :=
(ev A).naturality f
@[simp, reassoc]
lemma coev_naturality {X Y : C} (f : X ⟶ Y) :
f ≫ (coev A).app Y = (coev A).app X ≫ (ihom A).map ((𝟙 A) ⊗ f) :=
(coev A).naturality f
notation (name := ihom) A ` ⟶[`C`] ` B:10 := (@ihom C _ _ A _).obj B
@[simp, reassoc] lemma ev_coev :
((𝟙 A) ⊗ ((coev A).app B)) ≫ (ev A).app (A ⊗ B) = 𝟙 (A ⊗ B) :=
adjunction.left_triangle_components (ihom.adjunction A)
@[simp, reassoc] lemma coev_ev :
(coev A).app (A ⟶[C] B) ≫ (ihom A).map ((ev A).app B) = 𝟙 (A ⟶[C] B) :=
adjunction.right_triangle_components (ihom.adjunction A)
end ihom
open category_theory.limits
instance : preserves_colimits (tensor_left A) :=
(ihom.adjunction A).left_adjoint_preserves_colimits
variables {A}
-- Wrap these in a namespace so we don't clash with the core versions.
namespace monoidal_closed
/-- Currying in a monoidal closed category. -/
def curry : (A ⊗ Y ⟶ X) → (Y ⟶ (A ⟶[C] X)) :=
(ihom.adjunction A).hom_equiv _ _
/-- Uncurrying in a monoidal closed category. -/
def uncurry : (Y ⟶ (A ⟶[C] X)) → (A ⊗ Y ⟶ X) :=
((ihom.adjunction A).hom_equiv _ _).symm
@[simp] lemma hom_equiv_apply_eq (f : A ⊗ Y ⟶ X) :
(ihom.adjunction A).hom_equiv _ _ f = curry f := rfl
@[simp] lemma hom_equiv_symm_apply_eq (f : Y ⟶ (A ⟶[C] X)) :
((ihom.adjunction A).hom_equiv _ _).symm f = uncurry f := rfl
@[reassoc]
lemma curry_natural_left (f : X ⟶ X') (g : A ⊗ X' ⟶ Y) :
curry (((𝟙 _) ⊗ f) ≫ g) = f ≫ curry g :=
adjunction.hom_equiv_naturality_left _ _ _
@[reassoc]
lemma curry_natural_right (f : A ⊗ X ⟶ Y) (g : Y ⟶ Y') :
curry (f ≫ g) = curry f ≫ (ihom _).map g :=
adjunction.hom_equiv_naturality_right _ _ _
@[reassoc]
lemma uncurry_natural_right (f : X ⟶ (A ⟶[C] Y)) (g : Y ⟶ Y') :
uncurry (f ≫ (ihom _).map g) = uncurry f ≫ g :=
adjunction.hom_equiv_naturality_right_symm _ _ _
@[reassoc]
lemma uncurry_natural_left (f : X ⟶ X') (g : X' ⟶ (A ⟶[C] Y)) :
uncurry (f ≫ g) = ((𝟙 _) ⊗ f) ≫ uncurry g :=
adjunction.hom_equiv_naturality_left_symm _ _ _
@[simp]
lemma uncurry_curry (f : A ⊗ X ⟶ Y) : uncurry (curry f) = f :=
(closed.is_adj.adj.hom_equiv _ _).left_inv f
@[simp]
lemma curry_uncurry (f : X ⟶ (A ⟶[C] Y)) : curry (uncurry f) = f :=
(closed.is_adj.adj.hom_equiv _ _).right_inv f
lemma curry_eq_iff (f : A ⊗ Y ⟶ X) (g : Y ⟶ (A ⟶[C] X)) :
curry f = g ↔ f = uncurry g :=
adjunction.hom_equiv_apply_eq _ f g
lemma eq_curry_iff (f : A ⊗ Y ⟶ X) (g : Y ⟶ (A ⟶[C] X)) :
g = curry f ↔ uncurry g = f :=
adjunction.eq_hom_equiv_apply _ f g
-- I don't think these two should be simp.
lemma uncurry_eq (g : Y ⟶ (A ⟶[C] X)) : uncurry g = ((𝟙 A) ⊗ g) ≫ (ihom.ev A).app X :=
adjunction.hom_equiv_counit _
lemma curry_eq (g : A ⊗ Y ⟶ X) : curry g = (ihom.coev A).app Y ≫ (ihom A).map g :=
adjunction.hom_equiv_unit _
lemma curry_injective : function.injective (curry : (A ⊗ Y ⟶ X) → (Y ⟶ (A ⟶[C] X))) :=
(closed.is_adj.adj.hom_equiv _ _).injective
lemma uncurry_injective : function.injective (uncurry : (Y ⟶ (A ⟶[C] X)) → (A ⊗ Y ⟶ X)) :=
(closed.is_adj.adj.hom_equiv _ _).symm.injective
variables (A X)
lemma uncurry_id_eq_ev : uncurry (𝟙 (A ⟶[C] X)) = (ihom.ev A).app X :=
by rw [uncurry_eq, tensor_id, id_comp]
lemma curry_id_eq_coev : curry (𝟙 _) = (ihom.coev A).app X :=
by { rw [curry_eq, (ihom A).map_id (A ⊗ _)], apply comp_id }
section pre
variables {A B} [closed B]
/-- Pre-compose an internal hom with an external hom. -/
def pre (f : B ⟶ A) : ihom A ⟶ ihom B :=
transfer_nat_trans_self (ihom.adjunction _) (ihom.adjunction _) ((tensoring_left C).map f)
@[simp, reassoc]
lemma id_tensor_pre_app_comp_ev (f : B ⟶ A) (X : C) :
(𝟙 B ⊗ ((pre f).app X)) ≫ (ihom.ev B).app X =
(f ⊗ (𝟙 (A ⟶[C] X))) ≫ (ihom.ev A).app X :=
transfer_nat_trans_self_counit _ _ ((tensoring_left C).map f) X
@[simp]
lemma uncurry_pre (f : B ⟶ A) (X : C) :
monoidal_closed.uncurry ((pre f).app X) = (f ⊗ 𝟙 _) ≫ (ihom.ev A).app X :=
by rw [uncurry_eq, id_tensor_pre_app_comp_ev]
@[simp, reassoc]
lemma coev_app_comp_pre_app (f : B ⟶ A) :
(ihom.coev A).app X ≫ (pre f).app (A ⊗ X) =
(ihom.coev B).app X ≫ (ihom B).map (f ⊗ (𝟙 _)) :=
unit_transfer_nat_trans_self _ _ ((tensoring_left C).map f) X
@[simp]
lemma pre_id (A : C) [closed A] : pre (𝟙 A) = 𝟙 _ :=
by { simp only [pre, functor.map_id], dsimp, simp, }
@[simp]
lemma pre_map {A₁ A₂ A₃ : C} [closed A₁] [closed A₂] [closed A₃]
(f : A₁ ⟶ A₂) (g : A₂ ⟶ A₃) :
pre (f ≫ g) = pre g ≫ pre f :=
by rw [pre, pre, pre, transfer_nat_trans_self_comp, (tensoring_left C).map_comp]
lemma pre_comm_ihom_map {W X Y Z : C} [closed W] [closed X]
(f : W ⟶ X) (g : Y ⟶ Z) :
(pre f).app Y ≫ (ihom W).map g = (ihom X).map g ≫ (pre f).app Z := by simp
end pre
/-- The internal hom functor given by the monoidal closed structure. -/
@[simps]
def internal_hom [monoidal_closed C] : Cᵒᵖ ⥤ C ⥤ C :=
{ obj := λ X, ihom X.unop,
map := λ X Y f, pre f.unop }
section of_equiv
variables {D : Type u₂} [category.{v₂} D] [monoidal_category.{v₂} D]
/-- Transport the property of being monoidal closed across a monoidal equivalence of categories -/
noncomputable
def of_equiv (F : monoidal_functor C D) [is_equivalence F.to_functor] [h : monoidal_closed D] :
monoidal_closed C :=
{ closed' := λ X,
{ is_adj := begin
haveI q : closed (F.to_functor.obj X) := infer_instance,
haveI : is_left_adjoint (tensor_left (F.to_functor.obj X)) := q.is_adj,
have i := comp_inv_iso (monoidal_functor.comm_tensor_left F X),
exact adjunction.left_adjoint_of_nat_iso i,
end } }
/-- Suppose we have a monoidal equivalence `F : C ≌ D`, with `D` monoidal closed. We can pull the
monoidal closed instance back along the equivalence. For `X, Y, Z : C`, this lemma describes the
resulting currying map `Hom(X ⊗ Y, Z) → Hom(Y, (X ⟶[C] Z))`. (`X ⟶[C] Z` is defined to be
`F⁻¹(F(X) ⟶[D] F(Z))`, so currying in `C` is given by essentially conjugating currying in
`D` by `F.`) -/
lemma of_equiv_curry_def (F : monoidal_functor C D) [is_equivalence F.to_functor]
[h : monoidal_closed D] {X Y Z : C} (f : X ⊗ Y ⟶ Z) :
@monoidal_closed.curry _ _ _ _ _ _ ((monoidal_closed.of_equiv F).1 _) f =
(F.1.1.adjunction.hom_equiv Y ((ihom _).obj _)) (monoidal_closed.curry
(F.1.1.inv.adjunction.hom_equiv (F.1.1.obj X ⊗ F.1.1.obj Y) Z
((comp_inv_iso (F.comm_tensor_left X)).hom.app Y ≫ f))) := rfl
/-- Suppose we have a monoidal equivalence `F : C ≌ D`, with `D` monoidal closed. We can pull the
monoidal closed instance back along the equivalence. For `X, Y, Z : C`, this lemma describes the
resulting uncurrying map `Hom(Y, (X ⟶[C] Z)) → Hom(X ⊗ Y ⟶ Z)`. (`X ⟶[C] Z` is
defined to be `F⁻¹(F(X) ⟶[D] F(Z))`, so uncurrying in `C` is given by essentially conjugating
uncurrying in `D` by `F.`) -/
lemma of_equiv_uncurry_def
(F : monoidal_functor C D) [is_equivalence F.to_functor] [h : monoidal_closed D] {X Y Z : C}
(f : Y ⟶ (@ihom _ _ _ X $ (monoidal_closed.of_equiv F).1 X).obj Z) :
@monoidal_closed.uncurry _ _ _ _ _ _ ((monoidal_closed.of_equiv F).1 _) f =
(comp_inv_iso (F.comm_tensor_left X)).inv.app Y ≫ (F.1.1.inv.adjunction.hom_equiv
(F.1.1.obj X ⊗ F.1.1.obj Y) Z).symm (monoidal_closed.uncurry
((F.1.1.adjunction.hom_equiv Y ((ihom (F.1.1.obj X)).obj (F.1.1.obj Z))).symm f)) :=
rfl
end of_equiv
end monoidal_closed
end category_theory
|
51daf97d322a12d075b9ecdbe9e1b86e790904f0 | dd0f5513e11c52db157d2fcc8456d9401a6cd9da | /06_Inductive_Types.org.34.lean | fa6fb03dd5e679c5cd77f39e47223224ddeb0a40 | [] | no_license | cjmazey/lean-tutorial | ba559a49f82aa6c5848b9bf17b7389bf7f4ba645 | 381f61c9fcac56d01d959ae0fa6e376f2c4e3b34 | refs/heads/master | 1,610,286,098,832 | 1,447,124,923,000 | 1,447,124,923,000 | 43,082,433 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 474 | lean | /- page 88 -/
import standard
namespace hide
-- BEGIN
inductive list (A : Type) : Type :=
| nil {} : list A
| cons : A → list A → list A
namespace list
variable {A : Type}
notation h :: t := cons h t
definition append (s t : list A) : list A :=
list.rec t (λ x l u, x::u) s
notation s ++ t := append s t
theorem nil_append (t : list A) : nil ++ t = t := rfl
theorem cons_append (x : A) (s t : list A) : x::s ++ t = x::(s ++ t) := rfl
end list
-- END
end hide
|
b3fb0737e0f5457e47a02755e4ff0009c2de4f76 | b00eb947a9c4141624aa8919e94ce6dcd249ed70 | /stage0/src/Init/Data/Ord.lean | 36bbbf1f392adb6daa4636262d7a511ee1337a90 | [
"Apache-2.0"
] | permissive | gebner/lean4-old | a4129a041af2d4d12afb3a8d4deedabde727719b | ee51cdfaf63ee313c914d83264f91f414a0e3b6e | refs/heads/master | 1,683,628,606,745 | 1,622,651,300,000 | 1,622,654,405,000 | 142,608,821 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,155 | lean | /-
Copyright (c) 2021 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Dany Fabian
-/
prelude
import Init.Data.Int
import Init.Data.String
inductive Ordering where
| lt | eq | gt
deriving Inhabited, BEq
class Ord (α : Type u) where
compare : α → α → Ordering
export Ord (compare)
@[inline] def compareOfLessAndEq {α} (x y : α) [LT α] [Decidable (x < y)] [DecidableEq α] : Ordering :=
if x < y then Ordering.lt
else if x = y then Ordering.eq
else Ordering.gt
instance : Ord Nat where
compare x y := compareOfLessAndEq x y
instance : Ord Int where
compare x y := compareOfLessAndEq x y
instance : Ord Bool where
compare
| false, true => Ordering.lt
| true, false => Ordering.gt
| _, _ => Ordering.eq
instance : Ord String where
compare x y := compareOfLessAndEq x y
instance (n : Nat) : Ord (Fin n) where
compare x y := compare x.val y.val
def USize.cmp (a b : USize) : Ordering := compare a.val b.val
instance : Ord USize where
compare x y := compareOfLessAndEq x y
instance : Ord Char where
compare x y := compareOfLessAndEq x y
|
788c015a20c2f4e4989d8e040bf7845f2dc453ee | 4727251e0cd73359b15b664c3170e5d754078599 | /src/order/modular_lattice.lean | ce193145da41cc5a085cf7045b5d0e5fa979e363 | [
"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 | 9,591 | lean | /-
Copyright (c) 2020 Aaron Anderson. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Aaron Anderson
-/
import order.rel_iso
import order.lattice_intervals
import order.galois_connection
/-!
# Modular Lattices
This file defines Modular Lattices, a kind of lattice useful in algebra.
For examples, look to the subobject lattices of abelian groups, submodules, and ideals, or consider
any distributive lattice.
## Main Definitions
- `is_modular_lattice` defines a modular lattice to be one such that
`x ≤ z → (x ⊔ y) ⊓ z ≤ x ⊔ (y ⊓ z)`
- `inf_Icc_order_iso_Icc_sup` gives an order isomorphism between the intervals
`[a ⊓ b, a]` and `[b, a ⊔ b]`.
This corresponds to the diamond (or second) isomorphism theorems of algebra.
## Main Results
- `is_modular_lattice_iff_inf_sup_inf_assoc`:
Modularity is equivalent to the `inf_sup_inf_assoc`: `(x ⊓ z) ⊔ (y ⊓ z) = ((x ⊓ z) ⊔ y) ⊓ z`
- `distrib_lattice.is_modular_lattice`: Distributive lattices are modular.
## To do
- Relate atoms and coatoms in modular lattices
-/
variable {α : Type*}
/-- A modular lattice is one with a limited associativity between `⊓` and `⊔`. -/
class is_modular_lattice α [lattice α] : Prop :=
(sup_inf_le_assoc_of_le : ∀ {x : α} (y : α) {z : α}, x ≤ z → (x ⊔ y) ⊓ z ≤ x ⊔ (y ⊓ z))
section is_modular_lattice
variables [lattice α] [is_modular_lattice α]
theorem sup_inf_assoc_of_le {x : α} (y : α) {z : α} (h : x ≤ z) :
(x ⊔ y) ⊓ z = x ⊔ (y ⊓ z) :=
le_antisymm (is_modular_lattice.sup_inf_le_assoc_of_le y h)
(le_inf (sup_le_sup_left inf_le_left _) (sup_le h inf_le_right))
theorem is_modular_lattice.inf_sup_inf_assoc {x y z : α} :
(x ⊓ z) ⊔ (y ⊓ z) = ((x ⊓ z) ⊔ y) ⊓ z :=
(sup_inf_assoc_of_le y inf_le_right).symm
lemma inf_sup_assoc_of_le {x : α} (y : α) {z : α} (h : z ≤ x) :
(x ⊓ y) ⊔ z = x ⊓ (y ⊔ z) :=
by rw [inf_comm, sup_comm, ← sup_inf_assoc_of_le y h, inf_comm, sup_comm]
instance : is_modular_lattice αᵒᵈ :=
⟨λ x y z xz, le_of_eq (by { rw [inf_comm, sup_comm, eq_comm, inf_comm, sup_comm],
convert sup_inf_assoc_of_le (order_dual.of_dual y) (order_dual.dual_le.2 xz) })⟩
variables {x y z : α}
theorem is_modular_lattice.sup_inf_sup_assoc :
(x ⊔ z) ⊓ (y ⊔ z) = ((x ⊔ z) ⊓ y) ⊔ z :=
@is_modular_lattice.inf_sup_inf_assoc αᵒᵈ _ _ _ _ _
theorem eq_of_le_of_inf_le_of_sup_le (hxy : x ≤ y) (hinf : y ⊓ z ≤ x ⊓ z) (hsup : y ⊔ z ≤ x ⊔ z) :
x = y :=
le_antisymm hxy $
have h : y ≤ x ⊔ z,
from calc y ≤ y ⊔ z : le_sup_left
... ≤ x ⊔ z : hsup,
calc y ≤ (x ⊔ z) ⊓ y : le_inf h le_rfl
... = x ⊔ (z ⊓ y) : sup_inf_assoc_of_le _ hxy
... ≤ x ⊔ (z ⊓ x) : sup_le_sup_left
(by rw [inf_comm, @inf_comm _ _ z]; exact hinf) _
... ≤ x : sup_le le_rfl inf_le_right
theorem sup_lt_sup_of_lt_of_inf_le_inf (hxy : x < y) (hinf : y ⊓ z ≤ x ⊓ z) : x ⊔ z < y ⊔ z :=
lt_of_le_of_ne
(sup_le_sup_right (le_of_lt hxy) _)
(λ hsup, ne_of_lt hxy $ eq_of_le_of_inf_le_of_sup_le (le_of_lt hxy) hinf
(le_of_eq hsup.symm))
theorem inf_lt_inf_of_lt_of_sup_le_sup (hxy : x < y) (hinf : y ⊔ z ≤ x ⊔ z) : x ⊓ z < y ⊓ z :=
@sup_lt_sup_of_lt_of_inf_le_inf αᵒᵈ _ _ _ _ _ hxy hinf
/-- A generalization of the theorem that if `N` is a submodule of `M` and
`N` and `M / N` are both Artinian, then `M` is Artinian. -/
theorem well_founded_lt_exact_sequence
{β γ : Type*} [partial_order β] [preorder γ]
(h₁ : well_founded ((<) : β → β → Prop))
(h₂ : well_founded ((<) : γ → γ → Prop))
(K : α) (f₁ : β → α) (f₂ : α → β) (g₁ : γ → α) (g₂ : α → γ)
(gci : galois_coinsertion f₁ f₂)
(gi : galois_insertion g₂ g₁)
(hf : ∀ a, f₁ (f₂ a) = a ⊓ K)
(hg : ∀ a, g₁ (g₂ a) = a ⊔ K) :
well_founded ((<) : α → α → Prop) :=
subrelation.wf
(λ A B hAB, show prod.lex (<) (<) (f₂ A, g₂ A) (f₂ B, g₂ B),
begin
simp only [prod.lex_def, lt_iff_le_not_le, ← gci.l_le_l_iff,
← gi.u_le_u_iff, hf, hg, le_antisymm_iff],
simp only [gci.l_le_l_iff, gi.u_le_u_iff, ← lt_iff_le_not_le, ← le_antisymm_iff],
cases lt_or_eq_of_le (inf_le_inf_right K (le_of_lt hAB)) with h h,
{ exact or.inl h },
{ exact or.inr ⟨h, sup_lt_sup_of_lt_of_inf_le_inf hAB (le_of_eq h.symm)⟩ }
end)
(inv_image.wf _ (prod.lex_wf h₁ h₂))
/-- A generalization of the theorem that if `N` is a submodule of `M` and
`N` and `M / N` are both Noetherian, then `M` is Noetherian. -/
theorem well_founded_gt_exact_sequence
{β γ : Type*} [preorder β] [partial_order γ]
(h₁ : well_founded ((>) : β → β → Prop))
(h₂ : well_founded ((>) : γ → γ → Prop))
(K : α) (f₁ : β → α) (f₂ : α → β) (g₁ : γ → α) (g₂ : α → γ)
(gci : galois_coinsertion f₁ f₂)
(gi : galois_insertion g₂ g₁)
(hf : ∀ a, f₁ (f₂ a) = a ⊓ K)
(hg : ∀ a, g₁ (g₂ a) = a ⊔ K) :
well_founded ((>) : α → α → Prop) :=
@well_founded_lt_exact_sequence αᵒᵈ _ _ γᵒᵈ βᵒᵈ _ _ h₂ h₁ K g₁ g₂ f₁ f₂ gi.dual gci.dual hg hf
/-- The diamond isomorphism between the intervals `[a ⊓ b, a]` and `[b, a ⊔ b]` -/
def inf_Icc_order_iso_Icc_sup (a b : α) : set.Icc (a ⊓ b) a ≃o set.Icc b (a ⊔ b) :=
{ to_fun := λ x, ⟨x ⊔ b, ⟨le_sup_right, sup_le_sup_right x.prop.2 b⟩⟩,
inv_fun := λ x, ⟨a ⊓ x, ⟨inf_le_inf_left a x.prop.1, inf_le_left⟩⟩,
left_inv := λ x, subtype.ext (by { change a ⊓ (↑x ⊔ b) = ↑x,
rw [sup_comm, ← inf_sup_assoc_of_le _ x.prop.2, sup_eq_right.2 x.prop.1] }),
right_inv := λ x, subtype.ext (by { change a ⊓ ↑x ⊔ b = ↑x,
rw [inf_comm, inf_sup_assoc_of_le _ x.prop.1, inf_eq_left.2 x.prop.2] }),
map_rel_iff' := λ x y, begin
simp only [subtype.mk_le_mk, equiv.coe_fn_mk, and_true, le_sup_right],
rw [← subtype.coe_le_coe],
refine ⟨λ h, _, λ h, sup_le_sup_right h _⟩,
rw [← sup_eq_right.2 x.prop.1, inf_sup_assoc_of_le _ x.prop.2, sup_comm,
← sup_eq_right.2 y.prop.1, inf_sup_assoc_of_le _ y.prop.2, @sup_comm _ _ b],
exact inf_le_inf_left _ h
end }
end is_modular_lattice
namespace is_compl
variables [lattice α] [bounded_order α] [is_modular_lattice α]
/-- The diamond isomorphism between the intervals `set.Iic a` and `set.Ici b`. -/
def Iic_order_iso_Ici {a b : α} (h : is_compl a b) : set.Iic a ≃o set.Ici b :=
(order_iso.set_congr (set.Iic a) (set.Icc (a ⊓ b) a) (h.inf_eq_bot.symm ▸ set.Icc_bot.symm)).trans $
(inf_Icc_order_iso_Icc_sup a b).trans
(order_iso.set_congr (set.Icc b (a ⊔ b)) (set.Ici b) (h.sup_eq_top.symm ▸ set.Icc_top))
end is_compl
theorem is_modular_lattice_iff_inf_sup_inf_assoc [lattice α] :
is_modular_lattice α ↔ ∀ (x y z : α), (x ⊓ z) ⊔ (y ⊓ z) = ((x ⊓ z) ⊔ y) ⊓ z :=
⟨λ h, @is_modular_lattice.inf_sup_inf_assoc _ _ h, λ h, ⟨λ x y z xz, by rw [← inf_eq_left.2 xz, h]⟩⟩
namespace distrib_lattice
@[priority 100]
instance [distrib_lattice α] : is_modular_lattice α :=
⟨λ x y z xz, by rw [inf_sup_right, inf_eq_left.2 xz]⟩
end distrib_lattice
theorem disjoint.disjoint_sup_right_of_disjoint_sup_left
[lattice α] [order_bot α] [is_modular_lattice α] {a b c : α}
(h : disjoint a b) (hsup : disjoint (a ⊔ b) c) :
disjoint a (b ⊔ c) :=
begin
rw [disjoint, ← h.eq_bot, sup_comm],
apply le_inf inf_le_left,
apply (inf_le_inf_right (c ⊔ b) le_sup_right).trans,
rw [sup_comm, is_modular_lattice.sup_inf_sup_assoc, hsup.eq_bot, bot_sup_eq]
end
theorem disjoint.disjoint_sup_left_of_disjoint_sup_right
[lattice α] [order_bot α] [is_modular_lattice α] {a b c : α}
(h : disjoint b c) (hsup : disjoint a (b ⊔ c)) :
disjoint (a ⊔ b) c :=
begin
rw [disjoint.comm, sup_comm],
apply disjoint.disjoint_sup_right_of_disjoint_sup_left h.symm,
rwa [sup_comm, disjoint.comm] at hsup,
end
namespace is_modular_lattice
variables [lattice α] [is_modular_lattice α] {a : α}
instance is_modular_lattice_Iic : is_modular_lattice (set.Iic a) :=
⟨λ x y z xz, (sup_inf_le_assoc_of_le (y : α) xz : (↑x ⊔ ↑y) ⊓ ↑z ≤ ↑x ⊔ ↑y ⊓ ↑z)⟩
instance is_modular_lattice_Ici : is_modular_lattice (set.Ici a) :=
⟨λ x y z xz, (sup_inf_le_assoc_of_le (y : α) xz : (↑x ⊔ ↑y) ⊓ ↑z ≤ ↑x ⊔ ↑y ⊓ ↑z)⟩
section is_complemented
variables [bounded_order α] [is_complemented α]
instance is_complemented_Iic : is_complemented (set.Iic a) :=
⟨λ ⟨x, hx⟩, let ⟨y, hy⟩ := exists_is_compl x in
⟨⟨y ⊓ a, set.mem_Iic.2 inf_le_right⟩, begin
split,
{ change x ⊓ (y ⊓ a) ≤ ⊥, -- improve lattice subtype API
rw ← inf_assoc,
exact le_trans inf_le_left hy.1 },
{ change a ≤ x ⊔ (y ⊓ a), -- improve lattice subtype API
rw [← sup_inf_assoc_of_le _ (set.mem_Iic.1 hx), top_le_iff.1 hy.2, top_inf_eq] }
end⟩⟩
instance is_complemented_Ici : is_complemented (set.Ici a) :=
⟨λ ⟨x, hx⟩, let ⟨y, hy⟩ := exists_is_compl x in
⟨⟨y ⊔ a, set.mem_Ici.2 le_sup_right⟩, begin
split,
{ change x ⊓ (y ⊔ a) ≤ a, -- improve lattice subtype API
rw [← inf_sup_assoc_of_le _ (set.mem_Ici.1 hx), le_bot_iff.1 hy.1, bot_sup_eq] },
{ change ⊤ ≤ x ⊔ (y ⊔ a), -- improve lattice subtype API
rw ← sup_assoc,
exact le_trans hy.2 le_sup_left }
end⟩⟩
end is_complemented
end is_modular_lattice
|
1f5be65a04dbc3ef318b5ceca20705da05a7a99d | f7315930643edc12e76c229a742d5446dad77097 | /library/data/fintype.lean | 1dd5d3126699cab9608443387650a8cfd09ee071 | [
"Apache-2.0"
] | permissive | bmalehorn/lean | 8f77b762a76c59afff7b7403f9eb5fc2c3ce70c1 | 53653c352643751c4b62ff63ec5e555f11dae8eb | refs/heads/master | 1,610,945,684,489 | 1,429,681,220,000 | 1,429,681,449,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 8,233 | lean | /-
Copyright (c) 2015 Leonardo de Moura. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
Finite type (type class)
-/
import data.list data.bool
open list bool unit decidable option function
structure fintype [class] (A : Type) : Type :=
(elems : list A) (unique : nodup elems) (complete : ∀ a, a ∈ elems)
definition elements_of (A : Type) [h : fintype A] : list A :=
@fintype.elems A h
definition fintype_unit [instance] : fintype unit :=
fintype.mk [star] dec_trivial (λ u, match u with star := dec_trivial end)
definition fintype_bool [instance] : fintype bool :=
fintype.mk [ff, tt]
dec_trivial
(λ b, match b with | tt := dec_trivial | ff := dec_trivial end)
definition fintype_product [instance] {A B : Type} : Π [h₁ : fintype A] [h₂ : fintype B], fintype (A × B)
| (fintype.mk e₁ u₁ c₁) (fintype.mk e₂ u₂ c₂) :=
fintype.mk
(cross_product e₁ e₂)
(nodup_cross_product u₁ u₂)
(λ p,
match p with
(a, b) := mem_cross_product (c₁ a) (c₂ b)
end)
/- auxiliary function for finding 'a' s.t. f a ≠ g a -/
section find_discr
variables {A B : Type}
variable [h : decidable_eq B]
include h
definition find_discr (f g : A → B) : list A → option A
| [] := none
| (a::l) := if f a = g a then find_discr l else some a
theorem find_discr_nil (f g : A → B) : find_discr f g [] = none :=
rfl
theorem find_discr_cons_of_ne {f g : A → B} {a : A} (l : list A) : f a ≠ g a → find_discr f g (a::l) = some a :=
assume ne, if_neg ne
theorem find_discr_cons_of_eq {f g : A → B} {a : A} (l : list A) : f a = g a → find_discr f g (a::l) = find_discr f g l :=
assume eq, if_pos eq
theorem ne_of_find_discr_eq_some {f g : A → B} {a : A} : ∀ {l}, find_discr f g l = some a → f a ≠ g a
| [] e := option.no_confusion e
| (x::l) e := by_cases
(λ h : f x = g x,
have aux : find_discr f g l = some a, by rewrite [find_discr_cons_of_eq l h at e]; exact e,
ne_of_find_discr_eq_some aux)
(λ h : f x ≠ g x,
have aux : some x = some a, by rewrite [find_discr_cons_of_ne l h at e]; exact e,
option.no_confusion aux (λ xeqa : x = a, eq.rec_on xeqa h))
theorem all_eq_of_find_discr_eq_none {f g : A → B} : ∀ {l}, find_discr f g l = none → ∀ a, a ∈ l → f a = g a
| [] e a i := absurd i !not_mem_nil
| (x::l) e a i := by_cases
(λ fx_eq_gx : f x = g x,
have aux : find_discr f g l = none, by rewrite [find_discr_cons_of_eq l fx_eq_gx at e]; exact e,
or.elim (eq_or_mem_of_mem_cons i)
(λ aeqx : a = x, by rewrite [-aeqx at fx_eq_gx]; exact fx_eq_gx)
(λ ainl : a ∈ l, all_eq_of_find_discr_eq_none aux a ainl))
(λ fx_ne_gx : f x ≠ g x,
have aux : some x = none, by rewrite [find_discr_cons_of_ne l fx_ne_gx at e]; exact e,
option.no_confusion aux)
end find_discr
definition decidable_eq_fun [instance] {A B : Type} [h₁ : fintype A] [h₂ : decidable_eq B] : decidable_eq (A → B) :=
λ f g,
match h₁ with
| fintype.mk e u c :=
match find_discr f g e with
| some a := λ h : find_discr f g e = some a, inr (λ f_eq_g : f = g, absurd (by rewrite f_eq_g) (ne_of_find_discr_eq_some h))
| none := λ h : find_discr f g e = none, inl (show f = g, from funext (λ a : A, all_eq_of_find_discr_eq_none h a (c a)))
end rfl
end
section check_pred
variables {A : Type}
definition check_pred (p : A → Prop) [h : decidable_pred p] : list A → bool
| [] := tt
| (a::l) := if p a then check_pred l else ff
theorem check_pred_cons_of_pos {p : A → Prop} [h : decidable_pred p] {a : A} (l : list A) : p a → check_pred p (a::l) = check_pred p l :=
assume pa, if_pos pa
theorem check_pred_cons_of_neg {p : A → Prop} [h : decidable_pred p] {a : A} (l : list A) : ¬ p a → check_pred p (a::l) = ff :=
assume npa, if_neg npa
theorem all_of_check_pred_eq_tt {p : A → Prop} [h : decidable_pred p] : ∀ {l : list A}, check_pred p l = tt → ∀ {a}, a ∈ l → p a
| [] eqtt a ainl := absurd ainl !not_mem_nil
| (b::l) eqtt a ainbl := by_cases
(λ pb : p b, or.elim (eq_or_mem_of_mem_cons ainbl)
(λ aeqb : a = b, by rewrite [aeqb]; exact pb)
(λ ainl : a ∈ l,
have eqtt₁ : check_pred p l = tt, by rewrite [check_pred_cons_of_pos _ pb at eqtt]; exact eqtt,
all_of_check_pred_eq_tt eqtt₁ ainl))
(λ npb : ¬ p b,
by rewrite [check_pred_cons_of_neg _ npb at eqtt]; exact (bool.no_confusion eqtt))
theorem ex_of_check_pred_eq_ff {p : A → Prop} [h : decidable_pred p] : ∀ {l : list A}, check_pred p l = ff → ∃ w, ¬ p w
| [] eqtt := bool.no_confusion eqtt
| (a::l) eqtt := by_cases
(λ pa : p a,
have eqtt₁ : check_pred p l = ff, by rewrite [check_pred_cons_of_pos _ pa at eqtt]; exact eqtt,
ex_of_check_pred_eq_ff eqtt₁)
(λ npa : ¬ p a, exists.intro a npa)
end check_pred
definition decidable_finite_pred [instance] {A : Type} {p : A → Prop} [h₁ : fintype A] [h₂ : decidable_pred p]
: decidable (∀ x : A, p x) :=
match h₁ with
| fintype.mk e u c :=
match check_pred p e with
| tt := λ h : check_pred p e = tt, inl (λ a : A, all_of_check_pred_eq_tt h (c a))
| ff := λ h : check_pred p e = ff,
inr (λ n : (∀ x, p x),
obtain (a : A) (w : ¬ p a), from ex_of_check_pred_eq_ff h,
absurd (n a) w)
end rfl
end
open list.as_type
-- Auxiliary function for returning a list with all elements of the type: (list.as_type l)
-- Remark ⟪s⟫ is notation for (list.as_type l)
-- We use this function to define the instance for (fintype ⟪s⟫)
private definition ltype_elems {A : Type} {s : list A} : Π {l : list A}, l ⊆ s → list ⟪s⟫
| [] h := []
| (a::l) h := lval a (h a !mem_cons) :: ltype_elems (sub_of_cons_sub h)
private theorem mem_of_mem_ltype_elems {A : Type} {a : A} {s : list A}
: Π {l : list A} {h : l ⊆ s} {m : a ∈ s}, mk a m ∈ ltype_elems h → a ∈ l
| [] h m lin := absurd lin !not_mem_nil
| (b::l) h m lin := or.elim (eq_or_mem_of_mem_cons lin)
(λ leq : mk a m = mk b (h b (mem_cons b l)),
as_type.no_confusion leq (λ aeqb em, by rewrite [aeqb]; exact !mem_cons))
(λ linl : mk a m ∈ ltype_elems (sub_of_cons_sub h),
have ainl : a ∈ l, from mem_of_mem_ltype_elems linl,
mem_cons_of_mem _ ainl)
private theorem nodup_ltype_elems {A : Type} {s : list A} : Π {l : list A} (d : nodup l) (h : l ⊆ s), nodup (ltype_elems h)
| [] d h := nodup_nil
| (a::l) d h :=
have d₁ : nodup l, from nodup_of_nodup_cons d,
have nainl : a ∉ l, from not_mem_of_nodup_cons d,
let h₁ : l ⊆ s := sub_of_cons_sub h in
have d₂ : nodup (ltype_elems h₁), from nodup_ltype_elems d₁ h₁,
have nin : mk a (h a (mem_cons a l)) ∉ ltype_elems h₁, from
assume ab, absurd (mem_of_mem_ltype_elems ab) nainl,
nodup_cons nin d₂
private theorem mem_ltype_elems {A : Type} {s : list A} {a : ⟪s⟫}
: Π {l : list A} (h : l ⊆ s), value a ∈ l → a ∈ ltype_elems h
| [] h vainl := absurd vainl !not_mem_nil
| (b::l) h vainbl := or.elim (eq_or_mem_of_mem_cons vainbl)
(λ vaeqb : value a = b,
begin
reverts [vaeqb, h],
-- TODO(Leo): check why 'cases a with [va, ma]' produces an incorrect proof
apply (as_type.cases_on a),
intros [va, ma, vaeqb],
rewrite -vaeqb, intro h,
apply mem_cons
end)
(λ vainl : value a ∈ l,
have s₁ : l ⊆ s, from sub_of_cons_sub h,
have aux : a ∈ ltype_elems (sub_of_cons_sub h), from mem_ltype_elems s₁ vainl,
mem_cons_of_mem _ aux)
definition fintype_list_as_type [instance] {A : Type} [h : decidable_eq A] {s : list A} : fintype ⟪s⟫ :=
let nds : list A := erase_dup s in
have sub₁ : nds ⊆ s, from erase_dup_sub s,
have sub₂ : s ⊆ nds, from sub_erase_dup s,
have dnds : nodup nds, from nodup_erase_dup s,
let e : list ⟪s⟫ := ltype_elems sub₁ in
fintype.mk
e
(nodup_ltype_elems dnds sub₁)
(λ a : ⟪s⟫, show a ∈ e, from
have vains : value a ∈ s, from is_member a,
have vainnds : value a ∈ nds, from sub₂ vains,
mem_ltype_elems sub₁ vainnds)
|
6332eb180b787af1ecff7338929bcdcfa3f5c5da | 80cc5bf14c8ea85ff340d1d747a127dcadeb966f | /src/algebra/midpoint.lean | 0efe9e365296d2b38c84688504b6a32f822f64ce | [
"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 | 5,311 | 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.module.basic
import algebra.invertible
/-!
# Midpoint of a segment
## Main definitions
* `midpoint R x y`: midpoint of the segment `[x, y]`. We define it for `x` and `y`
in a module over a ring `R` with invertible `2`.
* `add_monoid_hom.of_map_midpoint`: construct an `add_monoid_hom` given a map `f` such that
`f` sends zero to zero and midpoints to midpoints.
## Main theorems
* `midpoint_eq_iff`: `z` is the midpoint of `[x, y]` if and only if `x + y = z + z`,
* `midpoint_unique`: `midpoint R x y` does not depend on `R`;
* `midpoint x y` is linear both in `x` and `y`;
* `point_reflection_midpoint_left`, `point_reflection_midpoint_right`:
`equiv.point_reflection (midpoint R x y)` swaps `x` and `y`.
We do not mark most lemmas as `@[simp]` because it is hard to tell which side is simpler.
## Tags
midpoint, add_monoid_hom
-/
variables (R : Type*) {E : Type*}
section monoid
variables [semiring R] [invertible (2:R)] [add_comm_monoid E] [semimodule R E]
/-- `midpoint x y` is the midpoint of the segment `[x, y]`. -/
def midpoint (x y : E) : E := (⅟2:R) • (x + y)
lemma midpoint_eq_iff {x y z : E} : midpoint R x y = z ↔ x + y = z + z :=
⟨λ h, h ▸ calc x + y = (2 * ⅟2:R) • (x + y) : by rw [mul_inv_of_self, one_smul]
... = midpoint R x y + midpoint R x y : by rw [two_mul, add_smul, midpoint],
λ h, by rw [midpoint, h, ← two_smul R z, smul_smul, inv_of_mul_self, one_smul]⟩
@[simp] lemma midpoint_add_self (x y : E) : midpoint R x y + midpoint R x y = x + y :=
((midpoint_eq_iff R).1 rfl).symm
/-- `midpoint` does not depend on the ring `R`. -/
lemma midpoint_unique (R' : Type*) [semiring R'] [invertible (2:R')] [semimodule R' E] (x y : E) :
midpoint R x y = midpoint R' x y :=
(midpoint_eq_iff R).2 $ (midpoint_eq_iff R').1 rfl
@[simp] lemma midpoint_self (x : E) : midpoint R x x = x :=
by rw [midpoint, smul_add, ← two_smul R, smul_smul, mul_inv_of_self, one_smul]
variable {R}
lemma midpoint_def (x y : E) : midpoint R x y = (⅟2:R) • (x + y) := rfl
lemma midpoint_comm (x y : E) : midpoint R x y = midpoint R y x :=
by simp only [midpoint_def, add_comm]
lemma midpoint_zero_add (x y : E) : midpoint R 0 (x + y) = midpoint R x y :=
(midpoint_eq_iff R).2 $ (zero_add (x + y)).symm ▸ (midpoint_eq_iff R).1 rfl
lemma midpoint_add_add (x y x' y' : E) :
midpoint R (x + x') (y + y') = midpoint R x y + midpoint R x' y' :=
by { simp only [midpoint_def, ← smul_add, add_assoc, add_left_comm x'] }
lemma midpoint_add_right (x y z : E) : midpoint R (x + z) (y + z) = midpoint R x y + z :=
by rw [midpoint_add_add, midpoint_self]
lemma midpoint_add_left (x y z : E) : midpoint R (x + y) (x + z) = x + midpoint R y z :=
by rw [midpoint_add_add, midpoint_self]
lemma midpoint_smul_smul (c : R) (x y : E) : midpoint R (c • x) (c • y) = c • midpoint R x y :=
(midpoint_eq_iff R).2 $ by rw [← smul_add, ← smul_add, (midpoint_eq_iff R).1 rfl]
end monoid
section group
variables [ring R] [invertible (2:R)] [add_comm_group E] [module R E]
lemma midpoint_neg_neg (x y : E) : midpoint R (-x) (-y) = -midpoint R x y :=
by simpa only [neg_one_smul] using midpoint_smul_smul (-1:R) x y
lemma midpoint_sub_sub (x y x' y' : E) :
midpoint R (x - x') (y - y') = midpoint R x y - midpoint R x' y' :=
by simp only [sub_eq_add_neg, midpoint_add_add, midpoint_neg_neg]
lemma midpoint_sub_right (x y z : E) : midpoint R (x - z) (y - z) = midpoint R x y - z :=
by rw [midpoint_sub_sub, midpoint_self]
lemma midpoint_sub_left (x y z : E) : midpoint R (x - y) (x - z) = x - midpoint R y z :=
by rw [midpoint_sub_sub, midpoint_self]
end group
namespace add_monoid_hom
variables (R) (R' : Type*) {F : Type*}
[semiring R] [invertible (2:R)] [add_comm_monoid E] [semimodule R E]
[semiring R'] [invertible (2:R')] [add_comm_monoid F] [semimodule R' F]
/-- A map `f : E → F` sending zero to zero and midpoints to midpoints is an `add_monoid_hom`. -/
def of_map_midpoint (f : E → F) (h0 : f 0 = 0)
(hm : ∀ x y, f (midpoint R x y) = midpoint R' (f x) (f y)) :
E →+ F :=
{ to_fun := f,
map_zero' := h0,
map_add' := λ x y, -- by rw [← midpoint_self R (x + y), ← midpoint_zero_add, hm, h0]
calc f (x + y) = f 0 + f (x + y) : by rw [h0, zero_add]
... = midpoint R' (f 0) (f (x + y)) + midpoint R' (f 0) (f (x + y)) :
(midpoint_add_self _ _ _).symm
... = f (midpoint R x y) + f (midpoint R x y) : by rw [← hm, midpoint_zero_add]
... = f x + f y : by rw [hm, midpoint_add_self] }
@[simp] lemma coe_of_map_midpoint (f : E → F) (h0 : f 0 = 0)
(hm : ∀ x y, f (midpoint R x y) = midpoint R' (f x) (f y)) :
⇑(of_map_midpoint R R' f h0 hm) = f := rfl
end add_monoid_hom
namespace equiv
variables [ring R] [invertible (2:R)] [add_comm_group E] [module R E]
@[simp] lemma point_reflection_midpoint_left (x y : E) :
(point_reflection (midpoint R x y) : E → E) x = y :=
by rw [point_reflection_apply, midpoint_add_self, add_sub_cancel']
@[simp] lemma point_reflection_midpoint_right (x y : E) :
(point_reflection (midpoint R x y) : E → E) y = x :=
by rw [point_reflection_apply, midpoint_add_self, add_sub_cancel]
end equiv
|
0a75fca213c6b13bd0e80e09a0ae10d5f0665621 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/category_theory/subobject/lattice.lean | 319f87ae235ef3623bba4fa253070c9778670bb5 | [
"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 | 24,099 | lean | /-
Copyright (c) 2020 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Bhavik Mehta, Scott Morrison
-/
import category_theory.subobject.factor_thru
import category_theory.subobject.well_powered
/-!
# The lattice of subobjects
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
We provide the `semilattice_inf` with `order_top (subobject X)` instance when `[has_pullback C]`,
and the `semilattice_sup (subobject X)` instance when `[has_images C] [has_binary_coproducts C]`.
-/
universes v₁ v₂ u₁ u₂
noncomputable theory
open category_theory category_theory.category category_theory.limits
variables {C : Type u₁} [category.{v₁} C] {X Y Z : C}
variables {D : Type u₂} [category.{v₂} D]
namespace category_theory
namespace mono_over
section has_top
instance {X : C} : has_top (mono_over X) :=
{ top := mk' (𝟙 _) }
instance {X : C} : inhabited (mono_over X) := ⟨⊤⟩
/-- The morphism to the top object in `mono_over X`. -/
def le_top (f : mono_over X) : f ⟶ ⊤ :=
hom_mk f.arrow (comp_id _)
@[simp] lemma top_left (X : C) : ((⊤ : mono_over X) : C) = X := rfl
@[simp] lemma top_arrow (X : C) : (⊤ : mono_over X).arrow = 𝟙 X := rfl
/-- `map f` sends `⊤ : mono_over X` to `⟨X, f⟩ : mono_over Y`. -/
def map_top (f : X ⟶ Y) [mono f] : (map f).obj ⊤ ≅ mk' f :=
iso_of_both_ways (hom_mk (𝟙 _) rfl) (hom_mk (𝟙 _) (by simp [id_comp f]))
section
variable [has_pullbacks C]
/-- The pullback of the top object in `mono_over Y`
is (isomorphic to) the top object in `mono_over X`. -/
def pullback_top (f : X ⟶ Y) : (pullback f).obj ⊤ ≅ ⊤ :=
iso_of_both_ways (le_top _) (hom_mk (pullback.lift f (𝟙 _) (by tidy)) (pullback.lift_snd _ _ _))
/-- There is a morphism from `⊤ : mono_over A` to the pullback of a monomorphism along itself;
as the category is thin this is an isomorphism. -/
def top_le_pullback_self {A B : C} (f : A ⟶ B) [mono f] :
(⊤ : mono_over A) ⟶ (pullback f).obj (mk' f) :=
hom_mk _ (pullback.lift_snd _ _ rfl)
/-- The pullback of a monomorphism along itself is isomorphic to the top object. -/
def pullback_self {A B : C} (f : A ⟶ B) [mono f] :
(pullback f).obj (mk' f) ≅ ⊤ :=
iso_of_both_ways (le_top _) (top_le_pullback_self _)
end
end has_top
section has_bot
variables [has_initial C] [initial_mono_class C]
instance {X : C} : has_bot (mono_over X) :=
{ bot := mk' (initial.to X) }
@[simp] lemma bot_left (X : C) : ((⊥ : mono_over X) : C) = ⊥_ C := rfl
@[simp] lemma bot_arrow {X : C} : (⊥ : mono_over X).arrow = initial.to X := rfl
/-- The (unique) morphism from `⊥ : mono_over X` to any other `f : mono_over X`. -/
def bot_le {X : C} (f : mono_over X) : ⊥ ⟶ f :=
hom_mk (initial.to _) (by simp)
/-- `map f` sends `⊥ : mono_over X` to `⊥ : mono_over Y`. -/
def map_bot (f : X ⟶ Y) [mono f] : (map f).obj ⊥ ≅ ⊥ :=
iso_of_both_ways (hom_mk (initial.to _) (by simp)) (hom_mk (𝟙 _) (by simp))
end has_bot
section zero_order_bot
variables [has_zero_object C]
open_locale zero_object
/-- The object underlying `⊥ : subobject B` is (up to isomorphism) the zero object. -/
def bot_coe_iso_zero {B : C} : ((⊥ : mono_over B) : C) ≅ 0 :=
initial_is_initial.unique_up_to_iso has_zero_object.zero_is_initial
@[simp] lemma bot_arrow_eq_zero [has_zero_morphisms C] {B : C} : (⊥ : mono_over B).arrow = 0 :=
zero_of_source_iso_zero _ bot_coe_iso_zero
end zero_order_bot
section inf
variables [has_pullbacks C]
/--
When `[has_pullbacks C]`, `mono_over A` has "intersections", functorial in both arguments.
As `mono_over A` is only a preorder, this doesn't satisfy the axioms of `semilattice_inf`,
but we reuse all the names from `semilattice_inf` because they will be used to construct
`semilattice_inf (subobject A)` shortly.
-/
@[simps]
def inf {A : C} : mono_over A ⥤ mono_over A ⥤ mono_over A :=
{ obj := λ f, pullback f.arrow ⋙ map f.arrow,
map := λ f₁ f₂ k,
{ app := λ g,
begin
apply hom_mk _ _,
apply pullback.lift pullback.fst (pullback.snd ≫ k.left) _,
rw [pullback.condition, assoc, w k],
dsimp,
rw [pullback.lift_snd_assoc, assoc, w k],
end } }.
/-- A morphism from the "infimum" of two objects in `mono_over A` to the first object. -/
def inf_le_left {A : C} (f g : mono_over A) :
(inf.obj f).obj g ⟶ f :=
hom_mk _ rfl
/-- A morphism from the "infimum" of two objects in `mono_over A` to the second object. -/
def inf_le_right {A : C} (f g : mono_over A) :
(inf.obj f).obj g ⟶ g :=
hom_mk _ pullback.condition
/-- A morphism version of the `le_inf` axiom. -/
def le_inf {A : C} (f g h : mono_over A) :
(h ⟶ f) → (h ⟶ g) → (h ⟶ (inf.obj f).obj g) :=
begin
intros k₁ k₂,
refine hom_mk (pullback.lift k₂.left k₁.left _) _,
rw [w k₁, w k₂],
erw [pullback.lift_snd_assoc, w k₁],
end
end inf
section sup
variables [has_images C] [has_binary_coproducts C]
/-- When `[has_images C] [has_binary_coproducts C]`, `mono_over A` has a `sup` construction,
which is functorial in both arguments,
and which on `subobject A` will induce a `semilattice_sup`. -/
def sup {A : C} : mono_over A ⥤ mono_over A ⥤ mono_over A :=
curry_obj ((forget A).prod (forget A) ⋙ uncurry.obj over.coprod ⋙ image)
/-- A morphism version of `le_sup_left`. -/
def le_sup_left {A : C} (f g : mono_over A) :
f ⟶ (sup.obj f).obj g :=
begin
refine hom_mk (coprod.inl ≫ factor_thru_image _) _,
erw [category.assoc, image.fac, coprod.inl_desc],
refl,
end
/-- A morphism version of `le_sup_right`. -/
def le_sup_right {A : C} (f g : mono_over A) :
g ⟶ (sup.obj f).obj g :=
begin
refine hom_mk (coprod.inr ≫ factor_thru_image _) _,
erw [category.assoc, image.fac, coprod.inr_desc],
refl,
end
/-- A morphism version of `sup_le`. -/
def sup_le {A : C} (f g h : mono_over A) :
(f ⟶ h) → (g ⟶ h) → ((sup.obj f).obj g ⟶ h) :=
begin
intros k₁ k₂,
refine hom_mk _ _,
apply image.lift ⟨_, h.arrow, coprod.desc k₁.left k₂.left, _⟩,
{ dsimp,
ext1,
{ simp [w k₁] },
{ simp [w k₂] } },
{ apply image.lift_fac }
end
end sup
end mono_over
namespace subobject
section order_top
instance order_top {X : C} : order_top (subobject X) :=
{ top := quotient.mk' ⊤,
le_top :=
begin
refine quotient.ind' (λ f, _),
exact ⟨mono_over.le_top f⟩,
end }
instance {X : C} : inhabited (subobject X) := ⟨⊤⟩
lemma top_eq_id (B : C) : (⊤ : subobject B) = subobject.mk (𝟙 B) := rfl
lemma underlying_iso_top_hom {B : C} :
(underlying_iso (𝟙 B)).hom = (⊤ : subobject B).arrow :=
by { convert underlying_iso_hom_comp_eq_mk (𝟙 B), simp only [comp_id], }
instance top_arrow_is_iso {B : C} : is_iso ((⊤ : subobject B).arrow) :=
by { rw ←underlying_iso_top_hom, apply_instance, }
@[simp, reassoc]
lemma underlying_iso_inv_top_arrow {B : C} :
(underlying_iso _).inv ≫ (⊤ : subobject B).arrow = 𝟙 B :=
underlying_iso_arrow _
@[simp]
lemma map_top (f : X ⟶ Y) [mono f] : (map f).obj ⊤ = subobject.mk f :=
quotient.sound' ⟨mono_over.map_top f⟩
lemma top_factors {A B : C} (f : A ⟶ B) : (⊤ : subobject B).factors f :=
⟨f, comp_id _⟩
lemma is_iso_iff_mk_eq_top {X Y : C} (f : X ⟶ Y) [mono f] : is_iso f ↔ mk f = ⊤ :=
⟨λ _, by exactI mk_eq_mk_of_comm _ _ (as_iso f) (category.comp_id _), λ h,
by { rw [←of_mk_le_mk_comp h.le, category.comp_id], exact is_iso.of_iso (iso_of_mk_eq_mk _ _ h) }⟩
lemma is_iso_arrow_iff_eq_top {Y : C} (P : subobject Y) : is_iso P.arrow ↔ P = ⊤ :=
by rw [is_iso_iff_mk_eq_top, mk_arrow]
instance is_iso_top_arrow {Y : C} : is_iso (⊤ : subobject Y).arrow :=
by rw is_iso_arrow_iff_eq_top
lemma mk_eq_top_of_is_iso {X Y : C} (f : X ⟶ Y) [is_iso f] : mk f = ⊤ :=
(is_iso_iff_mk_eq_top f).mp infer_instance
lemma eq_top_of_is_iso_arrow {Y : C} (P : subobject Y) [is_iso P.arrow] : P = ⊤ :=
(is_iso_arrow_iff_eq_top P).mp infer_instance
section
variables [has_pullbacks C]
lemma pullback_top (f : X ⟶ Y) : (pullback f).obj ⊤ = ⊤ :=
quotient.sound' ⟨mono_over.pullback_top f⟩
lemma pullback_self {A B : C} (f : A ⟶ B) [mono f] :
(pullback f).obj (mk f) = ⊤ :=
quotient.sound' ⟨mono_over.pullback_self f⟩
end
end order_top
section order_bot
variables [has_initial C] [initial_mono_class C]
instance order_bot {X : C} : order_bot (subobject X) :=
{ bot := quotient.mk' ⊥,
bot_le :=
begin
refine quotient.ind' (λ f, _),
exact ⟨mono_over.bot_le f⟩,
end }
lemma bot_eq_initial_to {B : C} : (⊥ : subobject B) = subobject.mk (initial.to B) := rfl
/-- The object underlying `⊥ : subobject B` is (up to isomorphism) the initial object. -/
def bot_coe_iso_initial {B : C} : ((⊥ : subobject B) : C) ≅ ⊥_ C := underlying_iso _
lemma map_bot (f : X ⟶ Y) [mono f] : (map f).obj ⊥ = ⊥ :=
quotient.sound' ⟨mono_over.map_bot f⟩
end order_bot
section zero_order_bot
variables [has_zero_object C]
open_locale zero_object
/-- The object underlying `⊥ : subobject B` is (up to isomorphism) the zero object. -/
def bot_coe_iso_zero {B : C} : ((⊥ : subobject B) : C) ≅ 0 :=
bot_coe_iso_initial ≪≫ initial_is_initial.unique_up_to_iso has_zero_object.zero_is_initial
variables [has_zero_morphisms C]
lemma bot_eq_zero {B : C} : (⊥ : subobject B) = subobject.mk (0 : 0 ⟶ B) :=
mk_eq_mk_of_comm _ _ (initial_is_initial.unique_up_to_iso has_zero_object.zero_is_initial) (by simp)
@[simp] lemma bot_arrow {B : C} : (⊥ : subobject B).arrow = 0 :=
zero_of_source_iso_zero _ bot_coe_iso_zero
lemma bot_factors_iff_zero {A B : C} (f : A ⟶ B) : (⊥ : subobject B).factors f ↔ f = 0 :=
⟨by { rintro ⟨h, rfl⟩, simp }, by { rintro rfl, exact ⟨0, by simp⟩, }⟩
lemma mk_eq_bot_iff_zero {f : X ⟶ Y} [mono f] : subobject.mk f = ⊥ ↔ f = 0 :=
⟨λ h, by simpa [h, bot_factors_iff_zero] using mk_factors_self f,
λ h, mk_eq_mk_of_comm _ _ ((iso_zero_of_mono_eq_zero h).trans has_zero_object.zero_iso_initial)
(by simp [h])⟩
end zero_order_bot
section functor
variable (C)
/-- Sending `X : C` to `subobject X` is a contravariant functor `Cᵒᵖ ⥤ Type`. -/
@[simps]
def functor [has_pullbacks C] : Cᵒᵖ ⥤ Type (max u₁ v₁) :=
{ obj := λ X, subobject X.unop,
map := λ X Y f, (pullback f.unop).obj,
map_id' := λ X, funext pullback_id,
map_comp' := λ X Y Z f g, funext (pullback_comp _ _) }
end functor
section semilattice_inf_top
variables [has_pullbacks C]
/-- The functorial infimum on `mono_over A` descends to an infimum on `subobject A`. -/
def inf {A : C} : subobject A ⥤ subobject A ⥤ subobject A :=
thin_skeleton.map₂ mono_over.inf
lemma inf_le_left {A : C} (f g : subobject A) :
(inf.obj f).obj g ≤ f :=
quotient.induction_on₂' f g (λ a b, ⟨mono_over.inf_le_left _ _⟩)
lemma inf_le_right {A : C} (f g : subobject A) :
(inf.obj f).obj g ≤ g :=
quotient.induction_on₂' f g (λ a b, ⟨mono_over.inf_le_right _ _⟩)
lemma le_inf {A : C} (h f g : subobject A) :
h ≤ f → h ≤ g → h ≤ (inf.obj f).obj g :=
quotient.induction_on₃' h f g
begin
rintros f g h ⟨k⟩ ⟨l⟩,
exact ⟨mono_over.le_inf _ _ _ k l⟩,
end
instance {B : C} : semilattice_inf (subobject B) :=
{ inf := λ m n, (inf.obj m).obj n,
inf_le_left := inf_le_left,
inf_le_right := inf_le_right,
le_inf := le_inf,
..subobject.partial_order _ }
lemma factors_left_of_inf_factors {A B : C} {X Y : subobject B} {f : A ⟶ B}
(h : (X ⊓ Y).factors f) : X.factors f :=
factors_of_le _ (inf_le_left _ _) h
lemma factors_right_of_inf_factors {A B : C} {X Y : subobject B} {f : A ⟶ B}
(h : (X ⊓ Y).factors f) : Y.factors f :=
factors_of_le _ (inf_le_right _ _) h
@[simp]
lemma inf_factors {A B : C} {X Y : subobject B} (f : A ⟶ B) :
(X ⊓ Y).factors f ↔ X.factors f ∧ Y.factors f :=
⟨λ h, ⟨factors_left_of_inf_factors h, factors_right_of_inf_factors h⟩,
begin
revert X Y,
refine quotient.ind₂' _,
rintro X Y ⟨⟨g₁, rfl⟩, ⟨g₂, hg₂⟩⟩,
exact ⟨_, pullback.lift_snd_assoc _ _ hg₂ _⟩,
end⟩
lemma inf_arrow_factors_left {B : C} (X Y : subobject B) : X.factors (X ⊓ Y).arrow :=
(factors_iff _ _).mpr ⟨of_le (X ⊓ Y) X (inf_le_left X Y), by simp⟩
lemma inf_arrow_factors_right {B : C} (X Y : subobject B) : Y.factors (X ⊓ Y).arrow :=
(factors_iff _ _).mpr ⟨of_le (X ⊓ Y) Y (inf_le_right X Y), by simp⟩
@[simp]
lemma finset_inf_factors {I : Type*} {A B : C} {s : finset I} {P : I → subobject B}
(f : A ⟶ B) :
(s.inf P).factors f ↔ ∀ i ∈ s, (P i).factors f :=
begin
classical,
apply finset.induction_on s,
{ simp [top_factors] },
{ intros i s nm ih, simp [ih] },
end
-- `i` is explicit here because often we'd like to defer a proof of `m`
lemma finset_inf_arrow_factors {I : Type*} {B : C} (s : finset I) (P : I → subobject B)
(i : I) (m : i ∈ s) : (P i).factors (s.inf P).arrow :=
begin
revert i m,
classical,
apply finset.induction_on s,
{ rintro _ ⟨⟩, },
{ intros i s nm ih j m,
rw [finset.inf_insert],
simp only [finset.mem_insert] at m, rcases m with (rfl|m),
{ rw ←factor_thru_arrow _ _ (inf_arrow_factors_left _ _),
exact factors_comp_arrow _, },
{ rw ←factor_thru_arrow _ _ (inf_arrow_factors_right _ _),
apply factors_of_factors_right,
exact ih _ m, } },
end
lemma inf_eq_map_pullback' {A : C} (f₁ : mono_over A) (f₂ : subobject A) :
(subobject.inf.obj (quotient.mk' f₁)).obj f₂ =
(subobject.map f₁.arrow).obj ((subobject.pullback f₁.arrow).obj f₂) :=
begin
apply quotient.induction_on' f₂,
intro f₂,
refl,
end
lemma inf_eq_map_pullback {A : C} (f₁ : mono_over A) (f₂ : subobject A) :
(quotient.mk' f₁ ⊓ f₂ : subobject A) = (map f₁.arrow).obj ((pullback f₁.arrow).obj f₂) :=
inf_eq_map_pullback' f₁ f₂
lemma prod_eq_inf {A : C} {f₁ f₂ : subobject A} [has_binary_product f₁ f₂] :
(f₁ ⨯ f₂) = f₁ ⊓ f₂ :=
le_antisymm
(_root_.le_inf (limits.prod.fst).le (limits.prod.snd).le)
((prod.lift (_root_.inf_le_left.hom) (_root_.inf_le_right.hom))).le
lemma inf_def {B : C} (m m' : subobject B) :
m ⊓ m' = (inf.obj m).obj m' := rfl
/-- `⊓` commutes with pullback. -/
lemma inf_pullback {X Y : C} (g : X ⟶ Y) (f₁ f₂) :
(pullback g).obj (f₁ ⊓ f₂) = (pullback g).obj f₁ ⊓ (pullback g).obj f₂ :=
begin
revert f₁,
apply quotient.ind',
intro f₁,
erw [inf_def, inf_def, inf_eq_map_pullback', inf_eq_map_pullback', ← pullback_comp,
← map_pullback pullback.condition (pullback_is_pullback f₁.arrow g),
← pullback_comp, pullback.condition],
refl,
end
/-- `⊓` commutes with map. -/
lemma inf_map {X Y : C} (g : Y ⟶ X) [mono g] (f₁ f₂) :
(map g).obj (f₁ ⊓ f₂) = (map g).obj f₁ ⊓ (map g).obj f₂ :=
begin
revert f₁,
apply quotient.ind',
intro f₁,
erw [inf_def, inf_def, inf_eq_map_pullback',
inf_eq_map_pullback', ← map_comp],
dsimp,
rw [pullback_comp, pullback_map_self],
end
end semilattice_inf_top
section semilattice_sup
variables [has_images C] [has_binary_coproducts C]
/-- The functorial supremum on `mono_over A` descends to an supremum on `subobject A`. -/
def sup {A : C} : subobject A ⥤ subobject A ⥤ subobject A :=
thin_skeleton.map₂ mono_over.sup
instance {B : C} : semilattice_sup (subobject B) :=
{ sup := λ m n, (sup.obj m).obj n,
le_sup_left := λ m n, quotient.induction_on₂' m n (λ a b, ⟨mono_over.le_sup_left _ _⟩),
le_sup_right := λ m n, quotient.induction_on₂' m n (λ a b, ⟨mono_over.le_sup_right _ _⟩),
sup_le := λ m n k, quotient.induction_on₃' m n k (λ a b c ⟨i⟩ ⟨j⟩, ⟨mono_over.sup_le _ _ _ i j⟩),
..subobject.partial_order B }
lemma sup_factors_of_factors_left {A B : C} {X Y : subobject B} {f : A ⟶ B} (P : X.factors f) :
(X ⊔ Y).factors f :=
factors_of_le f le_sup_left P
lemma sup_factors_of_factors_right {A B : C} {X Y : subobject B} {f : A ⟶ B} (P : Y.factors f) :
(X ⊔ Y).factors f :=
factors_of_le f le_sup_right P
variables [has_initial C] [initial_mono_class C]
lemma finset_sup_factors {I : Type*} {A B : C} {s : finset I} {P : I → subobject B}
{f : A ⟶ B} (h : ∃ i ∈ s, (P i).factors f) :
(s.sup P).factors f :=
begin
classical,
revert h,
apply finset.induction_on s,
{ rintro ⟨_, ⟨⟨⟩, _⟩⟩, },
{ rintros i s nm ih ⟨j, ⟨m, h⟩⟩,
simp only [finset.sup_insert],
simp at m, rcases m with (rfl|m),
{ exact sup_factors_of_factors_left h, },
{ exact sup_factors_of_factors_right (ih ⟨j, ⟨m, h⟩⟩), }, },
end
end semilattice_sup
section lattice
instance [has_initial C] [initial_mono_class C] {B : C} : bounded_order (subobject B) :=
{ ..subobject.order_top,
..subobject.order_bot }
variables [has_pullbacks C] [has_images C] [has_binary_coproducts C]
instance {B : C} : lattice (subobject B) :=
{ ..subobject.semilattice_inf,
..subobject.semilattice_sup }
end lattice
section Inf
variables [well_powered C]
/--
The "wide cospan" diagram, with a small indexing type, constructed from a set of subobjects.
(This is just the diagram of all the subobjects pasted together, but using `well_powered C`
to make the diagram small.)
-/
def wide_cospan {A : C} (s : set (subobject A)) :
wide_pullback_shape (equiv_shrink _ '' s) ⥤ C :=
wide_pullback_shape.wide_cospan A
(λ j : equiv_shrink _ '' s, (((equiv_shrink (subobject A)).symm j) : C))
(λ j, ((equiv_shrink (subobject A)).symm j).arrow)
@[simp] lemma wide_cospan_map_term {A : C} (s : set (subobject A)) (j) :
(wide_cospan s).map (wide_pullback_shape.hom.term j) =
((equiv_shrink (subobject A)).symm j).arrow :=
rfl
/-- Auxiliary construction of a cone for `le_Inf`. -/
def le_Inf_cone {A : C} (s : set (subobject A)) (f : subobject A) (k : Π (g ∈ s), f ≤ g) :
cone (wide_cospan s) :=
wide_pullback_shape.mk_cone f.arrow
(λ j, underlying.map (hom_of_le (k _ (by { rcases j with ⟨-, ⟨g, ⟨m, rfl⟩⟩⟩, simpa using m, }))))
(by tidy)
@[simp] lemma le_Inf_cone_π_app_none
{A : C} (s : set (subobject A)) (f : subobject A) (k : Π (g ∈ s), f ≤ g) :
(le_Inf_cone s f k).π.app none = f.arrow :=
rfl
variables [has_wide_pullbacks.{v₁} C]
/--
The limit of `wide_cospan s`. (This will be the supremum of the set of subobjects.)
-/
def wide_pullback {A : C} (s : set (subobject A)) : C :=
limits.limit (wide_cospan s)
/--
The inclusion map from `wide_pullback s` to `A`
-/
def wide_pullback_ι {A : C} (s : set (subobject A)) :
wide_pullback s ⟶ A :=
limits.limit.π (wide_cospan s) none
instance wide_pullback_ι_mono {A : C} (s : set (subobject A)) :
mono (wide_pullback_ι s) :=
⟨λ W u v h, limit.hom_ext (λ j, begin
cases j,
{ exact h, },
{ apply (cancel_mono ((equiv_shrink (subobject A)).symm j).arrow).1,
rw [assoc, assoc],
erw limit.w (wide_cospan s) (wide_pullback_shape.hom.term j),
exact h, },
end)⟩
/--
When `[well_powered C]` and `[has_wide_pullbacks C]`, `subobject A` has arbitrary infimums.
-/
def Inf {A : C} (s : set (subobject A)) : subobject A :=
subobject.mk (wide_pullback_ι s)
lemma Inf_le {A : C} (s : set (subobject A)) (f ∈ s) :
Inf s ≤ f :=
begin
fapply le_of_comm,
{ refine (underlying_iso _).hom ≫
(limits.limit.π
(wide_cospan s)
(some ⟨equiv_shrink _ f, set.mem_image_of_mem (equiv_shrink (subobject A)) H⟩)) ≫ _,
apply eq_to_hom,
apply (congr_arg (λ X : subobject A, (X : C))),
exact (equiv.symm_apply_apply _ _), },
{ dsimp [Inf],
simp only [category.comp_id, category.assoc, ←underlying_iso_hom_comp_eq_mk,
subobject.arrow_congr, congr_arg_mpr_hom_left, iso.cancel_iso_hom_left],
convert limit.w (wide_cospan s) (wide_pullback_shape.hom.term _), },
end.
lemma le_Inf {A : C} (s : set (subobject A)) (f : subobject A) (k : Π (g ∈ s), f ≤ g) :
f ≤ Inf s :=
begin
fapply le_of_comm,
{ exact limits.limit.lift _ (le_Inf_cone s f k) ≫ (underlying_iso _).inv, },
{ dsimp [Inf, wide_pullback_ι],
simp, },
end
instance {B : C} : complete_semilattice_Inf (subobject B) :=
{ Inf := Inf,
Inf_le := Inf_le,
le_Inf := le_Inf,
..subobject.partial_order B }
end Inf
section Sup
variables [well_powered C] [has_coproducts.{v₁} C]
/--
The univesal morphism out of the coproduct of a set of subobjects,
after using `[well_powered C]` to reindex by a small type.
-/
def small_coproduct_desc {A : C} (s : set (subobject A)) : _ ⟶ A :=
limits.sigma.desc (λ j : equiv_shrink _ '' s, ((equiv_shrink (subobject A)).symm j).arrow)
variables [has_images C]
/-- When `[well_powered C] [has_images C] [has_coproducts C]`,
`subobject A` has arbitrary supremums. -/
def Sup {A : C} (s : set (subobject A)) : subobject A :=
subobject.mk (image.ι (small_coproduct_desc s))
lemma le_Sup {A : C} (s : set (subobject A)) (f ∈ s) :
f ≤ Sup s :=
begin
fapply le_of_comm,
{ dsimp [Sup],
refine _ ≫ factor_thru_image _ ≫ (underlying_iso _).inv,
refine _ ≫ sigma.ι _ ⟨equiv_shrink _ f, (by simpa [set.mem_image] using H)⟩,
exact eq_to_hom (congr_arg (λ X : subobject A, (X : C)) (equiv.symm_apply_apply _ _).symm), },
{ dsimp [Sup, small_coproduct_desc],
simp, dsimp, simp, },
end
lemma symm_apply_mem_iff_mem_image {α β : Type*} (e : α ≃ β) (s : set α) (x : β) :
e.symm x ∈ s ↔ x ∈ e '' s :=
⟨λ h, ⟨e.symm x, h, by simp⟩, by { rintro ⟨a, m, rfl⟩, simpa using m, }⟩
lemma Sup_le {A : C} (s : set (subobject A)) (f : subobject A) (k : Π (g ∈ s), g ≤ f) :
Sup s ≤ f :=
begin
fapply le_of_comm,
{ dsimp [Sup],
refine (underlying_iso _).hom ≫ image.lift ⟨_, f.arrow, _, _⟩,
{ refine sigma.desc _,
rintro ⟨g, m⟩,
refine underlying.map (hom_of_le (k _ _)),
simpa [symm_apply_mem_iff_mem_image] using m, },
{ ext j, rcases j with ⟨j, m⟩, dsimp [small_coproduct_desc], simp, dsimp, simp, }, },
{ dsimp [Sup],
simp, },
end
instance {B : C} : complete_semilattice_Sup (subobject B) :=
{ Sup := Sup,
le_Sup := le_Sup,
Sup_le := Sup_le,
..subobject.partial_order B }
end Sup
section complete_lattice
variables [well_powered C] [has_wide_pullbacks.{v₁} C] [has_images C] [has_coproducts.{v₁} C]
[initial_mono_class C]
local attribute [instance] has_smallest_coproducts_of_has_coproducts
instance {B : C} : complete_lattice (subobject B) :=
{ ..subobject.semilattice_inf,
..subobject.semilattice_sup,
..subobject.bounded_order,
..subobject.complete_semilattice_Inf,
..subobject.complete_semilattice_Sup, }
end complete_lattice
section zero_object
variables [has_zero_morphisms C] [has_zero_object C]
open_locale zero_object
/-- A nonzero object has nontrivial subobject lattice. -/
lemma nontrivial_of_not_is_zero {X : C} (h : ¬ is_zero X) : nontrivial (subobject X) :=
⟨⟨mk (0 : 0 ⟶ X), mk (𝟙 X), λ w, h (is_zero.of_iso (is_zero_zero C) (iso_of_mk_eq_mk _ _ w).symm)⟩⟩
end zero_object
section subobject_subobject
/-- The subobject lattice of a subobject `Y` is order isomorphic to the interval `set.Iic Y`. -/
def subobject_order_iso {X : C} (Y : subobject X) : subobject (Y : C) ≃o set.Iic Y :=
{ to_fun := λ Z, ⟨subobject.mk (Z.arrow ≫ Y.arrow),
set.mem_Iic.mpr (le_of_comm ((underlying_iso _).hom ≫ Z.arrow) (by simp))⟩,
inv_fun := λ Z, subobject.mk (of_le _ _ Z.2),
left_inv := λ Z, mk_eq_of_comm _ (underlying_iso _) (by { ext, simp, }),
right_inv := λ Z, subtype.ext (mk_eq_of_comm _ (underlying_iso _)
(by { dsimp, simp [←iso.eq_inv_comp], })),
map_rel_iff' := λ W Z,
⟨λ h, le_of_comm
((underlying_iso _).inv ≫ of_le _ _ (subtype.mk_le_mk.mp h) ≫ (underlying_iso _).hom)
(by { ext, simp, }),
λ h, subtype.mk_le_mk.mpr
(le_of_comm ((underlying_iso _).hom ≫ of_le _ _ h ≫ (underlying_iso _).inv) (by simp))⟩, }
end subobject_subobject
end subobject
end category_theory
|
145f8109aa06bff0a98bd55d8ceebdb1627f9e4a | 4727251e0cd73359b15b664c3170e5d754078599 | /src/data/list/nodup.lean | ec4766e578d31251d590facd0a72a68717ebece5 | [
"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 | 13,981 | lean | /-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro, Kenny Lau
-/
import data.list.lattice
import data.list.pairwise
import data.list.forall2
/-!
# Lists with no duplicates
`list.nodup` is defined in `data/list/defs`. In this file we prove various properties of this
predicate.
-/
universes u v
open nat function
variables {α : Type u} {β : Type v} {l l₁ l₂ : list α} {a b : α}
namespace list
@[simp] theorem forall_mem_ne {a : α} {l : list α} : (∀ (a' : α), a' ∈ l → ¬a = a') ↔ a ∉ l :=
⟨λ h m, h _ m rfl, λ h a' m e, h (e.symm ▸ m)⟩
@[simp] theorem nodup_nil : @nodup α [] := pairwise.nil
@[simp] theorem nodup_cons {a : α} {l : list α} : nodup (a::l) ↔ a ∉ l ∧ nodup l :=
by simp only [nodup, pairwise_cons, forall_mem_ne]
protected lemma pairwise.nodup {l : list α} {r : α → α → Prop} [is_irrefl α r] (h : pairwise r l) :
nodup l :=
h.imp $ λ a b, ne_of_irrefl
lemma rel_nodup {r : α → β → Prop} (hr : relator.bi_unique r) : (forall₂ r ⇒ (↔)) nodup nodup
| _ _ forall₂.nil := by simp only [nodup_nil]
| _ _ (forall₂.cons hab h) :=
by simpa only [nodup_cons] using relator.rel_and (relator.rel_not (rel_mem hr hab h))
(rel_nodup h)
protected lemma nodup.cons (ha : a ∉ l) (hl : nodup l) : nodup (a :: l) := nodup_cons.2 ⟨ha, hl⟩
lemma nodup_singleton (a : α) : nodup [a] := pairwise_singleton _ _
lemma nodup.of_cons (h : nodup (a :: l)) : nodup l := (nodup_cons.1 h).2
lemma nodup.not_mem (h : (a :: l).nodup) : a ∉ l := (nodup_cons.1 h).1
lemma not_nodup_cons_of_mem : a ∈ l → ¬ nodup (a :: l) := imp_not_comm.1 nodup.not_mem
protected lemma nodup.sublist : l₁ <+ l₂ → nodup l₂ → nodup l₁ := pairwise.sublist
theorem not_nodup_pair (a : α) : ¬ nodup [a, a] :=
not_nodup_cons_of_mem $ mem_singleton_self _
theorem nodup_iff_sublist {l : list α} : nodup l ↔ ∀ a, ¬ [a, a] <+ l :=
⟨λ d a h, not_nodup_pair a (d.sublist h), begin
induction l with a l IH; intro h, {exact nodup_nil},
exact (IH $ λ a s, h a $ sublist_cons_of_sublist _ s).cons
(λ al, h a $ (singleton_sublist.2 al).cons_cons _)
end⟩
theorem nodup_iff_nth_le_inj {l : list α} :
nodup l ↔ ∀ i j h₁ h₂, nth_le l i h₁ = nth_le l j h₂ → i = j :=
pairwise_iff_nth_le.trans
⟨λ H i j h₁ h₂ h, ((lt_trichotomy _ _)
.resolve_left (λ h', H _ _ h₂ h' h))
.resolve_right (λ h', H _ _ h₁ h' h.symm),
λ H i j h₁ h₂ h, ne_of_lt h₂ (H _ _ _ _ h)⟩
theorem nodup.nth_le_inj_iff {α : Type*} {l : list α} (h : nodup l)
{i j : ℕ} (hi : i < l.length) (hj : j < l.length) :
l.nth_le i hi = l.nth_le j hj ↔ i = j :=
⟨nodup_iff_nth_le_inj.mp h _ _ _ _, by simp {contextual := tt}⟩
lemma nodup.ne_singleton_iff {l : list α} (h : nodup l) (x : α) :
l ≠ [x] ↔ l = [] ∨ ∃ y ∈ l, y ≠ x :=
begin
induction l with hd tl hl,
{ simp },
{ specialize hl h.of_cons,
by_cases hx : tl = [x],
{ simpa [hx, and.comm, and_or_distrib_left] using h },
{ rw [←ne.def, hl] at hx,
rcases hx with rfl | ⟨y, hy, hx⟩,
{ simp },
{ have : tl ≠ [] := ne_nil_of_mem hy,
suffices : ∃ (y : α) (H : y ∈ hd :: tl), y ≠ x,
{ simpa [ne_nil_of_mem hy] },
exact ⟨y, mem_cons_of_mem _ hy, hx⟩ } } }
end
lemma nth_le_eq_of_ne_imp_not_nodup (xs : list α) (n m : ℕ) (hn : n < xs.length)
(hm : m < xs.length) (h : xs.nth_le n hn = xs.nth_le m hm) (hne : n ≠ m) :
¬ nodup xs :=
begin
rw nodup_iff_nth_le_inj,
simp only [exists_prop, exists_and_distrib_right, not_forall],
exact ⟨n, m, ⟨hn, hm, h⟩, hne⟩
end
@[simp] theorem nth_le_index_of [decidable_eq α] {l : list α} (H : nodup l) (n h) :
index_of (nth_le l n h) l = n :=
nodup_iff_nth_le_inj.1 H _ _ _ h $
index_of_nth_le $ index_of_lt_length.2 $ nth_le_mem _ _ _
theorem nodup_iff_count_le_one [decidable_eq α] {l : list α} : nodup l ↔ ∀ a, count a l ≤ 1 :=
nodup_iff_sublist.trans $ forall_congr $ λ a,
have [a, a] <+ l ↔ 1 < count a l, from (@le_count_iff_repeat_sublist _ _ a l 2).symm,
(not_congr this).trans not_lt
theorem nodup_repeat (a : α) : ∀ {n : ℕ}, nodup (repeat a n) ↔ n ≤ 1
| 0 := by simp [nat.zero_le]
| 1 := by simp
| (n+2) := iff_of_false
(λ H, nodup_iff_sublist.1 H a ((repeat_sublist_repeat _).2 (nat.le_add_left 2 n)))
(not_le_of_lt $ nat.le_add_left 2 n)
@[simp] theorem count_eq_one_of_mem [decidable_eq α] {a : α} {l : list α}
(d : nodup l) (h : a ∈ l) : count a l = 1 :=
le_antisymm (nodup_iff_count_le_one.1 d a) (count_pos.2 h)
lemma nodup.of_append_left : nodup (l₁ ++ l₂) → nodup l₁ :=
nodup.sublist (sublist_append_left l₁ l₂)
lemma nodup.of_append_right : nodup (l₁ ++ l₂) → nodup l₂ :=
nodup.sublist (sublist_append_right l₁ l₂)
theorem nodup_append {l₁ l₂ : list α} : nodup (l₁++l₂) ↔ nodup l₁ ∧ nodup l₂ ∧ disjoint l₁ l₂ :=
by simp only [nodup, pairwise_append, disjoint_iff_ne]
theorem disjoint_of_nodup_append {l₁ l₂ : list α} (d : nodup (l₁++l₂)) : disjoint l₁ l₂ :=
(nodup_append.1 d).2.2
lemma nodup.append (d₁ : nodup l₁) (d₂ : nodup l₂) (dj : disjoint l₁ l₂) : nodup (l₁ ++ l₂) :=
nodup_append.2 ⟨d₁, d₂, dj⟩
theorem nodup_append_comm {l₁ l₂ : list α} : nodup (l₁++l₂) ↔ nodup (l₂++l₁) :=
by simp only [nodup_append, and.left_comm, disjoint_comm]
theorem nodup_middle {a : α} {l₁ l₂ : list α} : nodup (l₁ ++ a::l₂) ↔ nodup (a::(l₁++l₂)) :=
by simp only [nodup_append, not_or_distrib, and.left_comm, and_assoc, nodup_cons, mem_append,
disjoint_cons_right]
lemma nodup.of_map (f : α → β) {l : list α} : nodup (map f l) → nodup l :=
pairwise.of_map f $ λ a b, mt $ congr_arg f
lemma nodup.map_on {f : α → β} (H : ∀ x ∈ l, ∀ y ∈ l, f x = f y → x = y) (d : nodup l) :
(map f l).nodup :=
pairwise.map _ (by exact λ a b ⟨ma, mb, n⟩ e, n (H a ma b mb e)) (pairwise.and_mem.1 d)
theorem inj_on_of_nodup_map {f : α → β} {l : list α} (d : nodup (map f l)) :
∀ ⦃x⦄, x ∈ l → ∀ ⦃y⦄, y ∈ l → f x = f y → x = y :=
begin
induction l with hd tl ih,
{ simp },
{ simp only [map, nodup_cons, mem_map, not_exists, not_and, ←ne.def] at d,
rintro _ (rfl | h₁) _ (rfl | h₂) h₃,
{ refl },
{ apply (d.1 _ h₂ h₃.symm).elim },
{ apply (d.1 _ h₁ h₃).elim },
{ apply ih d.2 h₁ h₂ h₃ } }
end
theorem nodup_map_iff_inj_on {f : α → β} {l : list α} (d : nodup l) :
nodup (map f l) ↔ (∀ (x ∈ l) (y ∈ l), f x = f y → x = y) :=
⟨inj_on_of_nodup_map, λ h, d.map_on h⟩
protected lemma nodup.map {f : α → β} (hf : injective f) : nodup l → nodup (map f l) :=
nodup.map_on (assume x _ y _ h, hf h)
theorem nodup_map_iff {f : α → β} {l : list α} (hf : injective f) : nodup (map f l) ↔ nodup l :=
⟨nodup.of_map _, nodup.map hf⟩
@[simp] theorem nodup_attach {l : list α} : nodup (attach l) ↔ nodup l :=
⟨λ h, attach_map_val l ▸ h.map (λ a b, subtype.eq),
λ h, nodup.of_map subtype.val ((attach_map_val l).symm ▸ h)⟩
alias nodup_attach ↔ list.nodup.of_attach list.nodup.attach
attribute [protected] nodup.attach
lemma nodup.pmap {p : α → Prop} {f : Π a, p a → β} {l : list α} {H}
(hf : ∀ a ha b hb, f a ha = f b hb → a = b) (h : nodup l) : nodup (pmap f l H) :=
by rw [pmap_eq_map_attach]; exact h.attach.map
(λ ⟨a, ha⟩ ⟨b, hb⟩ h, by congr; exact hf a (H _ ha) b (H _ hb) h)
lemma nodup.filter (p : α → Prop) [decidable_pred p] {l} : nodup l → nodup (filter p l) :=
pairwise.filter p
@[simp] theorem nodup_reverse {l : list α} : nodup (reverse l) ↔ nodup l :=
pairwise_reverse.trans $ by simp only [nodup, ne.def, eq_comm]
lemma nodup.erase_eq_filter [decidable_eq α] {l} (d : nodup l) (a : α) :
l.erase a = filter (≠ a) l :=
begin
induction d with b l m d IH, {refl},
by_cases b = a,
{ subst h, rw [erase_cons_head, filter_cons_of_neg],
symmetry, rw filter_eq_self, simpa only [ne.def, eq_comm] using m, exact not_not_intro rfl },
{ rw [erase_cons_tail _ h, filter_cons_of_pos, IH], exact h }
end
lemma nodup.erase [decidable_eq α] (a : α) : nodup l → nodup (l.erase a) :=
nodup.sublist $ erase_sublist _ _
lemma nodup.diff [decidable_eq α] : l₁.nodup → (l₁.diff l₂).nodup :=
nodup.sublist $ diff_sublist _ _
lemma nodup.mem_erase_iff [decidable_eq α] (d : nodup l) : a ∈ l.erase b ↔ a ≠ b ∧ a ∈ l :=
by rw [d.erase_eq_filter, mem_filter, and_comm]
lemma nodup.not_mem_erase [decidable_eq α] (h : nodup l) : a ∉ l.erase a :=
λ H, (h.mem_erase_iff.1 H).1 rfl
theorem nodup_join {L : list (list α)} :
nodup (join L) ↔ (∀ l ∈ L, nodup l) ∧ pairwise disjoint L :=
by simp only [nodup, pairwise_join, disjoint_left.symm, forall_mem_ne]
theorem nodup_bind {l₁ : list α} {f : α → list β} : nodup (l₁.bind f) ↔
(∀ x ∈ l₁, nodup (f x)) ∧ pairwise (λ (a b : α), disjoint (f a) (f b)) l₁ :=
by simp only [list.bind, nodup_join, pairwise_map, and_comm, and.left_comm, mem_map,
exists_imp_distrib, and_imp];
rw [show (∀ (l : list β) (x : α), f x = l → x ∈ l₁ → nodup l) ↔
(∀ (x : α), x ∈ l₁ → nodup (f x)),
from forall_swap.trans $ forall_congr $ λ_, forall_eq']
protected lemma nodup.product {l₂ : list β} (d₁ : l₁.nodup) (d₂ : l₂.nodup) :
(l₁.product l₂).nodup :=
nodup_bind.2
⟨λ a ma, d₂.map $ left_inverse.injective $ λ b, (rfl : (a,b).2 = b),
d₁.imp $ λ a₁ a₂ n x h₁ h₂, begin
rcases mem_map.1 h₁ with ⟨b₁, mb₁, rfl⟩,
rcases mem_map.1 h₂ with ⟨b₂, mb₂, ⟨⟩⟩,
exact n rfl
end⟩
lemma nodup.sigma {σ : α → Type*} {l₂ : Π a, list (σ a)} (d₁ : nodup l₁) (d₂ : ∀ a, nodup (l₂ a)) :
(l₁.sigma l₂).nodup :=
nodup_bind.2
⟨λ a ma, (d₂ a).map (λ b b' h, by injection h with _ h; exact eq_of_heq h),
d₁.imp $ λ a₁ a₂ n x h₁ h₂, begin
rcases mem_map.1 h₁ with ⟨b₁, mb₁, rfl⟩,
rcases mem_map.1 h₂ with ⟨b₂, mb₂, ⟨⟩⟩,
exact n rfl
end⟩
protected lemma nodup.filter_map {f : α → option β} (h : ∀ a a' b, b ∈ f a → b ∈ f a' → a = a') :
nodup l → nodup (filter_map f l) :=
pairwise.filter_map f $ λ a a' n b bm b' bm' e, n $ h a a' b' (e ▸ bm) bm'
protected lemma nodup.concat (h : a ∉ l) (h' : l.nodup) : (l.concat a).nodup :=
by rw concat_eq_append; exact h'.append (nodup_singleton _) (disjoint_singleton.2 h)
lemma nodup.insert [decidable_eq α] (h : l.nodup) : (insert a l).nodup :=
if h' : a ∈ l then by rw [insert_of_mem h']; exact h
else by rw [insert_of_not_mem h', nodup_cons]; split; assumption
lemma nodup.union [decidable_eq α] (l₁ : list α) (h : nodup l₂) : (l₁ ∪ l₂).nodup :=
begin
induction l₁ with a l₁ ih generalizing l₂,
{ exact h },
{ exact (ih h).insert }
end
lemma nodup.inter [decidable_eq α] (l₂ : list α) : nodup l₁ → nodup (l₁ ∩ l₂) := nodup.filter _
@[simp] theorem nodup_sublists {l : list α} : nodup (sublists l) ↔ nodup l :=
⟨λ h, (h.sublist (map_ret_sublist_sublists _)).of_map _,
λ h, (pairwise_sublists h).imp (λ _ _ h, mt reverse_inj.2 h.to_ne)⟩
@[simp] theorem nodup_sublists' {l : list α} : nodup (sublists' l) ↔ nodup l :=
by rw [sublists'_eq_sublists, nodup_map_iff reverse_injective,
nodup_sublists, nodup_reverse]
alias nodup_sublists ↔ list.nodup.of_sublists list.nodup.sublists
alias nodup_sublists' ↔ list.nodup.of_sublists' list.nodup.sublists'
attribute [protected] nodup.sublists nodup.sublists'
lemma nodup_sublists_len (n : ℕ) (h : nodup l) : (sublists_len n l).nodup :=
h.sublists'.sublist $ sublists_len_sublist_sublists' _ _
lemma nodup.diff_eq_filter [decidable_eq α] :
∀ {l₁ l₂ : list α} (hl₁ : l₁.nodup), l₁.diff l₂ = l₁.filter (∉ l₂)
| l₁ [] hl₁ := by simp
| l₁ (a::l₂) hl₁ :=
begin
rw [diff_cons, (hl₁.erase _).diff_eq_filter, hl₁.erase_eq_filter, filter_filter],
simp only [mem_cons_iff, not_or_distrib, and.comm]
end
lemma nodup.mem_diff_iff [decidable_eq α] (hl₁ : l₁.nodup) : a ∈ l₁.diff l₂ ↔ a ∈ l₁ ∧ a ∉ l₂ :=
by rw [hl₁.diff_eq_filter, mem_filter]
protected lemma nodup.update_nth : ∀ {l : list α} {n : ℕ} {a : α} (hl : l.nodup) (ha : a ∉ l),
(l.update_nth n a).nodup
| [] n a hl ha := nodup_nil
| (b::l) 0 a hl ha := nodup_cons.2 ⟨mt (mem_cons_of_mem _) ha, (nodup_cons.1 hl).2⟩
| (b::l) (n+1) a hl ha := nodup_cons.2
⟨λ h, (mem_or_eq_of_mem_update_nth h).elim
(nodup_cons.1 hl).1
(λ hba, ha (hba ▸ mem_cons_self _ _)), hl.of_cons.update_nth (mt (mem_cons_of_mem _) ha)⟩
lemma nodup.map_update [decidable_eq α] {l : list α} (hl : l.nodup) (f : α → β) (x : α) (y : β) :
l.map (function.update f x y) =
if x ∈ l then (l.map f).update_nth (l.index_of x) y else l.map f :=
begin
induction l with hd tl ihl, { simp },
rw [nodup_cons] at hl,
simp only [mem_cons_iff, map, ihl hl.2],
by_cases H : hd = x,
{ subst hd,
simp [update_nth, hl.1] },
{ simp [ne.symm H, H, update_nth, ← apply_ite (cons (f hd))] }
end
lemma nodup.pairwise_of_forall_ne {l : list α} {r : α → α → Prop}
(hl : l.nodup) (h : ∀ (a ∈ l) (b ∈ l), a ≠ b → r a b) : l.pairwise r :=
begin
classical,
refine pairwise_of_reflexive_on_dupl_of_forall_ne _ h,
intros x hx,
rw nodup_iff_count_le_one at hl,
exact absurd (hl x) hx.not_le
end
lemma nodup.pairwise_of_set_pairwise {l : list α} {r : α → α → Prop}
(hl : l.nodup) (h : {x | x ∈ l}.pairwise r) : l.pairwise r :=
hl.pairwise_of_forall_ne h
end list
theorem option.to_list_nodup {α} : ∀ o : option α, o.to_list.nodup
| none := list.nodup_nil
| (some x) := list.nodup_singleton x
|
b5a37f20794636bdff6469c0385e191ffda1e50f | 3b15c7b0b62d8ada1399c112ad88a529e6bfa115 | /stage0/src/Lean/Syntax.lean | 7b4822bf9257d5f7955d1a56c7df8212e2995f7d | [
"Apache-2.0"
] | permissive | stephenbrady/lean4 | 74bf5cae8a433e9c815708ce96c9e54a5caf2115 | b1bd3fc304d0f7bc6810ec78bfa4c51476d263f9 | refs/heads/master | 1,692,621,473,161 | 1,634,308,743,000 | 1,634,310,749,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 18,099 | lean | /-
Copyright (c) 2019 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Sebastian Ullrich, Leonardo de Moura
-/
import Lean.Data.Name
import Lean.Data.Format
namespace Lean
def SourceInfo.updateTrailing (trailing : Substring) : SourceInfo → SourceInfo
| SourceInfo.original leading pos _ endPos => SourceInfo.original leading pos trailing endPos
| info => info
/- Syntax AST -/
inductive IsNode : Syntax → Prop where
| mk (kind : SyntaxNodeKind) (args : Array Syntax) : IsNode (Syntax.node kind args)
def SyntaxNode : Type := {s : Syntax // IsNode s }
def unreachIsNodeMissing {β} (h : IsNode Syntax.missing) : β := False.elim (nomatch h)
def unreachIsNodeAtom {β} {info val} (h : IsNode (Syntax.atom info val)) : β := False.elim (nomatch h)
def unreachIsNodeIdent {β info rawVal val preresolved} (h : IsNode (Syntax.ident info rawVal val preresolved)) : β := False.elim (nomatch h)
namespace SyntaxNode
@[inline] def getKind (n : SyntaxNode) : SyntaxNodeKind :=
match n with
| ⟨Syntax.node k args, _⟩ => k
| ⟨Syntax.missing, h⟩ => unreachIsNodeMissing h
| ⟨Syntax.atom .., h⟩ => unreachIsNodeAtom h
| ⟨Syntax.ident .., h⟩ => unreachIsNodeIdent h
@[inline] def withArgs {β} (n : SyntaxNode) (fn : Array Syntax → β) : β :=
match n with
| ⟨Syntax.node _ args, _⟩ => fn args
| ⟨Syntax.missing, h⟩ => unreachIsNodeMissing h
| ⟨Syntax.atom _ _, h⟩ => unreachIsNodeAtom h
| ⟨Syntax.ident _ _ _ _, h⟩ => unreachIsNodeIdent h
@[inline] def getNumArgs (n : SyntaxNode) : Nat :=
withArgs n $ fun args => args.size
@[inline] def getArg (n : SyntaxNode) (i : Nat) : Syntax :=
withArgs n $ fun args => args.get! i
@[inline] def getArgs (n : SyntaxNode) : Array Syntax :=
withArgs n $ fun args => args
@[inline] def modifyArgs (n : SyntaxNode) (fn : Array Syntax → Array Syntax) : Syntax :=
match n with
| ⟨Syntax.node kind args, _⟩ => Syntax.node kind (fn args)
| ⟨Syntax.missing, h⟩ => unreachIsNodeMissing h
| ⟨Syntax.atom _ _, h⟩ => unreachIsNodeAtom h
| ⟨Syntax.ident _ _ _ _, h⟩ => unreachIsNodeIdent h
end SyntaxNode
namespace Syntax
def getAtomVal! : Syntax → String
| atom _ val => val
| _ => panic! "getAtomVal!: not an atom"
def setAtomVal : Syntax → String → Syntax
| atom info _, v => (atom info v)
| stx, _ => stx
@[inline] def ifNode {β} (stx : Syntax) (hyes : SyntaxNode → β) (hno : Unit → β) : β :=
match stx with
| Syntax.node k args => hyes ⟨Syntax.node k args, IsNode.mk k args⟩
| _ => hno ()
@[inline] def ifNodeKind {β} (stx : Syntax) (kind : SyntaxNodeKind) (hyes : SyntaxNode → β) (hno : Unit → β) : β :=
match stx with
| Syntax.node k args => if k == kind then hyes ⟨Syntax.node k args, IsNode.mk k args⟩ else hno ()
| _ => hno ()
def asNode : Syntax → SyntaxNode
| Syntax.node kind args => ⟨Syntax.node kind args, IsNode.mk kind args⟩
| _ => ⟨Syntax.node nullKind #[], IsNode.mk nullKind #[]⟩
def getIdAt (stx : Syntax) (i : Nat) : Name :=
(stx.getArg i).getId
@[inline] def modifyArgs (stx : Syntax) (fn : Array Syntax → Array Syntax) : Syntax :=
match stx with
| node k args => node k (fn args)
| stx => stx
@[inline] def modifyArg (stx : Syntax) (i : Nat) (fn : Syntax → Syntax) : Syntax :=
match stx with
| node k args => node k (args.modify i fn)
| stx => stx
@[specialize] partial def replaceM {m : Type → Type} [Monad m] (fn : Syntax → m (Option Syntax)) : Syntax → m (Syntax)
| stx@(node kind args) => do
match (← fn stx) with
| some stx => return stx
| none => return node kind (← args.mapM (replaceM fn))
| stx => do
let o ← fn stx
return o.getD stx
@[specialize] partial def rewriteBottomUpM {m : Type → Type} [Monad m] (fn : Syntax → m (Syntax)) : Syntax → m (Syntax)
| node kind args => do
let args ← args.mapM (rewriteBottomUpM fn)
fn (node kind args)
| stx => fn stx
@[inline] def rewriteBottomUp (fn : Syntax → Syntax) (stx : Syntax) : Syntax :=
Id.run $ stx.rewriteBottomUpM fn
private def updateInfo : SourceInfo → String.Pos → String.Pos → SourceInfo
| SourceInfo.original lead pos trail endPos, leadStart, trailStop =>
SourceInfo.original { lead with startPos := leadStart } pos { trail with stopPos := trailStop } endPos
| info, _, _ => info
private def chooseNiceTrailStop (trail : Substring) : String.Pos :=
trail.startPos + trail.posOf '\n'
/- Remark: the State `String.Pos` is the `SourceInfo.trailing.stopPos` of the previous token,
or the beginning of the String. -/
@[inline]
private def updateLeadingAux : Syntax → StateM String.Pos (Option Syntax)
| atom info@(SourceInfo.original lead _ trail _) val => do
let trailStop := chooseNiceTrailStop trail
let newInfo := updateInfo info (← get) trailStop
set trailStop
pure $ some (atom newInfo val)
| ident info@(SourceInfo.original lead _ trail _) rawVal val pre => do
let trailStop := chooseNiceTrailStop trail
let newInfo := updateInfo info (← get) trailStop
set trailStop
pure $ some (ident newInfo rawVal val pre)
| _ => pure none
/-- Set `SourceInfo.leading` according to the trailing stop of the preceding token.
The result is a round-tripping syntax tree IF, in the input syntax tree,
* all leading stops, atom contents, and trailing starts are correct
* trailing stops are between the trailing start and the next leading stop.
Remark: after parsing, all `SourceInfo.leading` fields are empty.
The `Syntax` argument is the output produced by the parser for `source`.
This function "fixes" the `source.leading` field.
Additionally, we try to choose "nicer" splits between leading and trailing stops
according to some heuristics so that e.g. comments are associated to the (intuitively)
correct token.
Note that the `SourceInfo.trailing` fields must be correct.
The implementation of this Function relies on this property. -/
def updateLeading : Syntax → Syntax :=
fun stx => (replaceM updateLeadingAux stx).run' 0
partial def updateTrailing (trailing : Substring) : Syntax → Syntax
| Syntax.atom info val => Syntax.atom (info.updateTrailing trailing) val
| Syntax.ident info rawVal val pre => Syntax.ident (info.updateTrailing trailing) rawVal val pre
| n@(Syntax.node k args) =>
if args.size == 0 then n
else
let i := args.size - 1
let last := updateTrailing trailing args[i]
let args := args.set! i last;
Syntax.node k args
| s => s
partial def getTailWithPos : Syntax → Option Syntax
| stx@(atom info _) => info.getPos?.map fun _ => stx
| stx@(ident info ..) => info.getPos?.map fun _ => stx
| node _ args => args.findSomeRev? getTailWithPos
| _ => none
open SourceInfo in
/-- Split an `ident` into its dot-separated components while preserving source info.
Macro scopes are first erased. For example, `` `foo.bla.boo._@._hyg.4 `` ↦ `` [`foo, `bla, `boo] ``.
If `nFields` is set, we take that many fields from the end and keep the remaining components
as one name. For example, `` `foo.bla.boo `` with `(nFields := 1)` ↦ `` [`foo.bla, `boo] ``. -/
def identComponents (stx : Syntax) (nFields? : Option Nat := none) : List Syntax :=
match stx with
| ident (SourceInfo.original lead pos trail _) rawStr val _ =>
let val := val.eraseMacroScopes
-- With original info, we assume that `rawStr` represents `val`.
let nameComps := nameComps val nFields?
let rawComps := splitNameLit rawStr
let rawComps :=
if let some nFields := nFields? then
let nPrefix := rawComps.length - nFields
let prefixSz := rawComps.take nPrefix |>.foldl (init := 0) fun acc (ss : Substring) => acc + ss.bsize + 1
let prefixSz := prefixSz - 1 -- The last component has no dot
rawStr.extract 0 prefixSz :: rawComps.drop nPrefix
else
rawComps
assert! nameComps.length == rawComps.length
nameComps.zip rawComps |>.map fun (id, ss) =>
let off := ss.startPos - rawStr.startPos
let lead := if off == 0 then lead else "".toSubstring
let trail := if ss.stopPos == rawStr.stopPos then trail else "".toSubstring
let info := original lead (pos + off) trail (pos + off + ss.bsize)
ident info ss id []
| ident si _ val _ =>
let val := val.eraseMacroScopes
/- With non-original info:
- `rawStr` can take all kinds of forms so we only use `val`.
- there is no source extent to offset, so we pass it as-is. -/
nameComps val nFields? |>.map fun n => ident si n.toString.toSubstring n []
| _ => unreachable!
where
nameComps (n : Name) (nFields? : Option Nat) : List Name :=
if let some nFields := nFields? then
let nameComps := n.components
let nPrefix := nameComps.length - nFields
let namePrefix := nameComps.take nPrefix |>.foldl (init := Name.anonymous) fun acc n => acc ++ n
namePrefix :: nameComps.drop nPrefix
else
n.components
structure TopDown where
firstChoiceOnly : Bool
stx : Syntax
/--
`for _ in stx.topDown` iterates through each node and leaf in `stx` top-down, left-to-right.
If `firstChoiceOnly` is `true`, only visit the first argument of each choice node.
-/
def topDown (stx : Syntax) (firstChoiceOnly := false) : TopDown := ⟨firstChoiceOnly, stx⟩
partial instance : ForIn m TopDown Syntax where
forIn := fun ⟨firstChoiceOnly, stx⟩ init f => do
let rec @[specialize] loop stx b [Inhabited (type_of% b)] := do
match (← f stx b) with
| ForInStep.yield b' =>
let mut b := b'
if let Syntax.node k args := stx then
if firstChoiceOnly && k == choiceKind then
return ← loop args[0] b
else
for arg in args do
match (← loop arg b) with
| ForInStep.yield b' => b := b'
| ForInStep.done b' => return ForInStep.done b'
return ForInStep.yield b
| ForInStep.done b => return ForInStep.done b
match (← @loop stx init ⟨init⟩) with
| ForInStep.yield b => return b
| ForInStep.done b => return b
partial def reprint (stx : Syntax) : Option String :=
OptionM.run do
let mut s := ""
for stx in stx.topDown (firstChoiceOnly := true) do
match stx with
| atom info val => s := s ++ reprintLeaf info val
| ident info rawVal _ _ => s := s ++ reprintLeaf info rawVal.toString
| node kind args =>
if kind == choiceKind then
-- this visit the first arg twice, but that should hardly be a problem
-- given that choice nodes are quite rare and small
let s0 ← reprint args[0]
for arg in args[1:] do
let s' ← reprint arg
guard (s0 == s')
| _ => pure ()
return s
where
reprintLeaf (info : SourceInfo) (val : String) : String :=
match info with
| SourceInfo.original lead _ trail _ => s!"{lead}{val}{trail}"
-- no source info => add gracious amounts of whitespace to definitely separate tokens
-- Note that the proper pretty printer does not use this function.
-- The parser as well always produces source info, so round-tripping is still
-- guaranteed.
| _ => s!" {val} "
def hasMissing (stx : Syntax) : Bool := do
for stx in stx.topDown do
if stx.isMissing then
return true
return false
/--
Represents a cursor into a syntax tree that can be read, written, and advanced down/up/left/right.
Indices are allowed to be out-of-bound, in which case `cur` is `Syntax.missing`.
If the `Traverser` is used linearly, updates are linear in the `Syntax` object as well.
-/
structure Traverser where
cur : Syntax
parents : Array Syntax
idxs : Array Nat
namespace Traverser
def fromSyntax (stx : Syntax) : Traverser :=
⟨stx, #[], #[]⟩
def setCur (t : Traverser) (stx : Syntax) : Traverser :=
{ t with cur := stx }
/-- Advance to the `idx`-th child of the current node. -/
def down (t : Traverser) (idx : Nat) : Traverser :=
if idx < t.cur.getNumArgs then
{ cur := t.cur.getArg idx, parents := t.parents.push $ t.cur.setArg idx arbitrary, idxs := t.idxs.push idx }
else
{ cur := Syntax.missing, parents := t.parents.push t.cur, idxs := t.idxs.push idx }
/-- Advance to the parent of the current node, if any. -/
def up (t : Traverser) : Traverser :=
if t.parents.size > 0 then
let cur := if t.idxs.back < t.parents.back.getNumArgs then t.parents.back.setArg t.idxs.back t.cur else t.parents.back
{ cur := cur, parents := t.parents.pop, idxs := t.idxs.pop }
else
t
/-- Advance to the left sibling of the current node, if any. -/
def left (t : Traverser) : Traverser :=
if t.parents.size > 0 then
t.up.down (t.idxs.back - 1)
else
t
/-- Advance to the right sibling of the current node, if any. -/
def right (t : Traverser) : Traverser :=
if t.parents.size > 0 then
t.up.down (t.idxs.back + 1)
else
t
end Traverser
/-- Monad class that gives read/write access to a `Traverser`. -/
class MonadTraverser (m : Type → Type) where
st : MonadState Traverser m
namespace MonadTraverser
variable {m : Type → Type} [Monad m] [t : MonadTraverser m]
def getCur : m Syntax := Traverser.cur <$> t.st.get
def setCur (stx : Syntax) : m Unit := @modify _ _ t.st (fun t => t.setCur stx)
def goDown (idx : Nat) : m Unit := @modify _ _ t.st (fun t => t.down idx)
def goUp : m Unit := @modify _ _ t.st (fun t => t.up)
def goLeft : m Unit := @modify _ _ t.st (fun t => t.left)
def goRight : m Unit := @modify _ _ t.st (fun t => t.right)
def getIdx : m Nat := do
let st ← t.st.get
st.idxs.back?.getD 0
end MonadTraverser
end Syntax
namespace SyntaxNode
@[inline] def getIdAt (n : SyntaxNode) (i : Nat) : Name :=
(n.getArg i).getId
end SyntaxNode
def mkListNode (args : Array Syntax) : Syntax :=
Syntax.node nullKind args
namespace Syntax
-- quotation node kinds are formed from a unique quotation name plus "quot"
def isQuot : Syntax → Bool
| Syntax.node (Name.str _ "quot" _) _ => true
| Syntax.node `Lean.Parser.Term.dynamicQuot _ => true
| _ => false
def getQuotContent (stx : Syntax) : Syntax :=
if stx.isOfKind `Lean.Parser.Term.dynamicQuot then
stx[3]
else
stx[1]
-- antiquotation node kinds are formed from the original node kind (if any) plus "antiquot"
def isAntiquot : Syntax → Bool
| Syntax.node (Name.str _ "antiquot" _) _ => true
| _ => false
def mkAntiquotNode (term : Syntax) (nesting := 0) (name : Option String := none) (kind := Name.anonymous) : Syntax :=
let nesting := mkNullNode (mkArray nesting (mkAtom "$"))
let term := match term.isIdent with
| true => term
| false => mkNode `antiquotNestedExpr #[mkAtom "(", term, mkAtom ")"]
let name := match name with
| some name => mkNode `antiquotName #[mkAtom ":", mkAtom name]
| none => mkNullNode
mkNode (kind ++ `antiquot) #[mkAtom "$", nesting, term, name]
-- Antiquotations can be escaped as in `$$x`, which is useful for nesting macros. Also works for antiquotation splices.
def isEscapedAntiquot (stx : Syntax) : Bool :=
!stx[1].getArgs.isEmpty
-- Also works for antiquotation splices.
def unescapeAntiquot (stx : Syntax) : Syntax :=
if isAntiquot stx then
stx.setArg 1 $ mkNullNode stx[1].getArgs.pop
else
stx
-- Also works for token antiquotations.
def getAntiquotTerm (stx : Syntax) : Syntax :=
let e := if stx.isAntiquot then stx[2] else stx[3]
if e.isIdent then e
else
-- `e` is from `"(" >> termParser >> ")"`
e[1]
def antiquotKind? : Syntax → Option SyntaxNodeKind
| Syntax.node (Name.str k "antiquot" _) args =>
if args[3].isOfKind `antiquotName then some k
else
-- we treat all antiquotations where the kind was left implicit (`$e`) the same (see `elimAntiquotChoices`)
some Name.anonymous
| _ => none
-- An "antiquotation splice" is something like `$[...]?` or `$[...]*`.
def antiquotSpliceKind? : Syntax → Option SyntaxNodeKind
| Syntax.node (Name.str k "antiquot_scope" _) args => some k
| _ => none
def isAntiquotSplice (stx : Syntax) : Bool :=
antiquotSpliceKind? stx |>.isSome
def getAntiquotSpliceContents (stx : Syntax) : Array Syntax :=
stx[3].getArgs
-- `$[..],*` or `$x,*` ~> `,*`
def getAntiquotSpliceSuffix (stx : Syntax) : Syntax :=
if stx.isAntiquotSplice then
stx[5]
else
stx[1]
def mkAntiquotSpliceNode (kind : SyntaxNodeKind) (contents : Array Syntax) (suffix : String) (nesting := 0) : Syntax :=
let nesting := mkNullNode (mkArray nesting (mkAtom "$"))
mkNode (kind ++ `antiquot_splice) #[mkAtom "$", nesting, mkAtom "[", mkNullNode contents, mkAtom "]", mkAtom suffix]
-- `$x,*` etc.
def antiquotSuffixSplice? : Syntax → Option SyntaxNodeKind
| Syntax.node (Name.str k "antiquot_suffix_splice" _) args => some k
| _ => none
def isAntiquotSuffixSplice (stx : Syntax) : Bool :=
antiquotSuffixSplice? stx |>.isSome
-- `$x` in the example above
def getAntiquotSuffixSpliceInner (stx : Syntax) : Syntax :=
stx[0]
def mkAntiquotSuffixSpliceNode (kind : SyntaxNodeKind) (inner : Syntax) (suffix : String) : Syntax :=
mkNode (kind ++ `antiquot_suffix_splice) #[inner, mkAtom suffix]
def isTokenAntiquot (stx : Syntax) : Bool :=
stx.isOfKind `token_antiquot
def isAnyAntiquot (stx : Syntax) : Bool :=
stx.isAntiquot || stx.isAntiquotSplice || stx.isAntiquotSuffixSplice || stx.isTokenAntiquot
end Syntax
end Lean
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.