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
a25f35fa9b97e53161b6291b515ad871c68795e3
bce603343785d07c32cb8b35950aba0b4d9415ae
/lean4/Crypto/Util.lean
2a074fa9b0d10e6a4a0886c93faddf35bf2e62d3
[]
no_license
fpvandoorn/cryptomorphism
db00cf0b12c8fd561a6200eee6a6ff2a023f1228
d486419ecced54de3db759dae81110be44b7c28b
refs/heads/master
1,693,198,883,854
1,636,562,038,000
1,636,562,038,000
363,219,330
0
0
null
null
null
null
UTF-8
Lean
false
false
7,734
lean
import Lean namespace List universe u v w x y z variable {α : Type u} {β : Type v} {γ : Type w} {δ : Type x} {ε : Type y} {ζ : Type z} def zipWith3 (f : α → β → γ → δ) : List α → List β → List γ → List δ | x::xs, y::ys, z::zs => f x y z :: zipWith3 f xs ys zs | _, _, _ => [] def zipWith4 (f : α → β → γ → δ → ε) : List α → List β → List γ → List δ → List ε | x::xs, y::ys, z::zs, u::us => f x y z u :: zipWith4 f xs ys zs us | _, _, _, _ => [] def zipWith5 (f : α → β → γ → δ → ε → ζ) : List α → List β → List γ → List δ → List ε → List ζ | x::xs, y::ys, z::zs, u::us, v::vs => f x y z u v :: zipWith5 f xs ys zs us vs | _, _, _, _, _ => [] end List namespace Array universe u v w x y z variable {α : Type u} {β : Type v} {γ : Type w} {δ : Type x} {ε : Type y} {ζ : Type z} def zipWith3 (f : α → β → γ → δ) : Array α → Array β → Array γ → Array δ | as, bs, cs => zipWith (zipWith as bs f) cs id def zipWith4 (f : α → β → γ → δ → ε) : Array α → Array β → Array γ → Array δ → Array ε | as, bs, cs, ds => zipWith (zipWith (zipWith as bs f) cs id) ds id def zipWith5 (f : α → β → γ → δ → ε → ζ) : Array α → Array β → Array γ → Array δ → Array ε → Array ζ | as, bs, cs, ds, es => zipWith (zipWith (zipWith (zipWith as bs f) cs id) ds id) es id end Array namespace Lean namespace BinderInfo protected def «open» : BinderInfo → Char | default => '(' | implicit => '{' | strictImplicit => '⦃' | instImplicit => '[' | auxDecl => '(' protected def close : BinderInfo → Char | default => ')' | implicit => '}' | strictImplicit => '⦄' | instImplicit => ']' | auxDecl => ')' end BinderInfo namespace LocalDecl instance : ToString LocalDecl := ⟨λ d => match d with | cdecl i f n t bi => s!"{bi.open}{n} : {t}{bi.close}" | ldecl i f n t v b => s!"({n} : {t} := {v})"⟩ end LocalDecl namespace Name def updateSuffix (f : String → String) : Name → Name | str n s _ => mkStr n (f s) | n => n def head! : Name → String | str anonymous s _ => s | str n s _ => head! n | num anonymous s _ => toString s | num n s _ => head! n | anonymous => panic! "Name.anonymous doesn't have a head" end Name namespace Level /-- Is a level equal to a succ, when all (meta)variables are instantiated with 0? -/ def isASucc : Level → Bool | succ _ _ => true | max u v _ => isASucc u || isASucc v | imax u v _ => isASucc v | _ => false end Level namespace Expr /-- Warning: very slow performance! -/ def foldAtomicAux {α : Type u} : Expr → (Expr → Nat → α → α) → Nat → α → α | app fn e d, f, l, x => foldAtomicAux e f l $ foldAtomicAux fn f l x | lam v t e d, f, l, x => foldAtomicAux e f (l+1) $ foldAtomicAux t f l x | forallE v t e d, f, l, x => foldAtomicAux e f (l+1) $ foldAtomicAux t f l x | letE v t a e d, f, l, x => foldAtomicAux e f (l+1) $ foldAtomicAux a f l $ foldAtomicAux t f l x | mdata m e d, f, l, x => foldAtomicAux e f l x | proj n k e d, f, l, x => foldAtomicAux e f l x | e, f, l, x => f e l x /-- `foldAtomic e x f` folds `f` over all atomic subexpressions of `e`. The `Nat` passed to `f` is the binder depth. Warning: very slow performance! -/ def foldAtomic {α : Type u} : Expr → α → (Expr → Nat → α → α) → α | e, x, f => foldAtomicAux e f 0 x /-- Warning: very slow performance! -/ def foldAux {α : Type u} : Expr → (Expr → Nat → α → α) → Nat → α → α | b@(app fn e d), f, l, x => foldAux e f l $ foldAux fn f l $ f b l x | b@(lam v t e d), f, l, x => foldAux e f (l+1) $ foldAux t f l $ f b l x | b@(forallE v t e d), f, l, x => foldAux e f (l+1) $ foldAux t f l $ f b l x | b@(letE v t a e d), f, l, x => foldAux e f (l+1) $ foldAux a f l $ foldAux t f l $ f b l x | b@(mdata m e d), f, l, x => foldAux e f l $ f b l x | b@(proj n k e d), f, l, x => foldAux e f l $ f b l x | e, f, l, x => f e l x /-- `fold e x f` folds `f` over all subexpressions of `e`. The `nat` passed to `f` is the binder depth. Warning: very slow performance! -/ def fold {α : Type u} : Expr → α → (Expr → Nat → α → α) → α | e, x, f => foldAux e f 0 x /-- Warning: very slow performance! -/ def foldSomeAux {α : Type u} : Expr → (Expr → Nat → α → Option α) → Nat → α → α | b@(app fn e d), f, l, x => (f b l x).getD $ foldSomeAux e f l $ foldSomeAux fn f l x | b@(lam v t e d), f, l, x => (f b l x).getD $ foldSomeAux e f (l+1) $ foldSomeAux t f l x | b@(forallE v t e d), f, l, x => (f b l x).getD $ foldSomeAux e f (l+1) $ foldSomeAux t f l x | b@(letE v t a e d), f, l, x => (f b l x).getD $ foldSomeAux e f (l+1) $ foldSomeAux a f l $ foldSomeAux t f l x | b@(mdata m e d), f, l, x => (f b l x).getD $ foldSomeAux e f l x | b@(proj n k e d), f, l, x => (f b l x).getD $ foldSomeAux e f l x | e, f, l, x => (f e l x).getD x /-- `fold e x f` folds `f` over all subexpressions of `e`. If `f` returns `none` then the subexpressions of `f` are traversed. The `Nat` passed to `f` is the binder depth. Warning: very slow performance! -/ def foldSome {α : Type u} : Expr → α → (Expr → Nat → α → Option α) → α | e, x, f => foldSomeAux e f 0 x /-- Replace `nm args` for `nm` in `nms` by the expression in the `NameMap`. -/ def replaceConst (e : Expr) (nms : NameMap Expr) (args : Array Expr) : Expr := e.replace $ λ subterm => let data := nms.find? subterm.getAppFn.constName! if data.isSome && subterm.getAppArgs == args then data.get! else none /-- Returns the pretty Names of all local constants in an expression. -/ def ListFvarIds (e : Expr) : NameSet := e.foldAtomic NameSet.empty $ λ e' _ es => if e'.isFVar then es.insert e'.fvarId!.name else es /-- Replace occurrences of the free variables `fvars` in `e` with `vs` -/ def instantiateFVars (e : Expr) (fvars : Array (Expr × Expr)) : Expr := e.replaceFVars (fvars.map (·.fst)) $ fvars.map (·.snd) def level! : Expr → Level | sort u _ => u | _ => arbitrary end Expr namespace NameSet /-- Erase a Name from a NameSet -/ @[inline] def erase (t : NameSet) (a : Name) : NameSet := Std.RBTree.erase t a /-- Erase a List of names from a NameSet -/ def eraseList (ns : NameSet) (nms : List Name) : NameSet := nms.foldl NameSet.erase ns end NameSet namespace Meta instance : Inhabited CasesSubgoal := ⟨arbitrary, Name.anonymous⟩ /-- Convert the given goal `Ctx |- target` into `Ctx, name : type |- target` and `Ctx |- type`. It assumes `val` has type `type`. Similar to `assert`, but uses a metavariable for `val`. -/ def assertm (mvarId : MVarId) (name : Name) (type : Expr) : MetaM (FVarId × MVarId × MVarId) := withMVarContext mvarId do checkNotAssigned mvarId `assert let tag ← getMVarTag mvarId let target ← getMVarType mvarId let newType := mkForall name BinderInfo.default type target let newMVarFn ← mkFreshExprSyntheticOpaqueMVar newType tag let newMVarB ← mkFreshExprSyntheticOpaqueMVar type tag assignExprMVar mvarId (mkApp newMVarFn newMVarB) let (newFVarId, newMVarId) ← intro1P newMVarFn.mvarId! pure (newFVarId, newMVarId, newMVarB.mvarId!) /-- Convert the given goal `Ctx |- target` into `Ctx, name : type |- target`. It assumes `val` has type `type` -/ def asserti (mvarId : MVarId) (name : Name) (type : Expr) (val : Expr) : MetaM (FVarId × MVarId) := do let m ← assert mvarId name type val intro1P m end Meta end Lean
35abc2bfeaee7df6d481a0a73d58773d037c4069
4727251e0cd73359b15b664c3170e5d754078599
/src/ring_theory/trace.lean
a00ca29fbb2419448f1fe18a324e72bd2e0e3cfa
[ "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
21,557
lean
/- Copyright (c) 2020 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen -/ import linear_algebra.matrix.bilinear_form import linear_algebra.matrix.charpoly.minpoly import linear_algebra.determinant import linear_algebra.vandermonde import linear_algebra.trace import field_theory.is_alg_closed.algebraic_closure import field_theory.primitive_element import ring_theory.power_basis /-! # Trace for (finite) ring extensions. Suppose we have an `R`-algebra `S` with a finite basis. For each `s : S`, the trace of the linear map given by multiplying by `s` gives information about the roots of the minimal polynomial of `s` over `R`. ## Main definitions * `algebra.trace R S x`: the trace of an element `s` of an `R`-algebra `S` * `algebra.trace_form R S`: bilinear form sending `x`, `y` to the trace of `x * y` * `algebra.trace_matrix R b`: the matrix whose `(i j)`-th element is the trace of `b i * b j`. * `algebra.embeddings_matrix A C b : matrix κ (B →ₐ[A] C) C` is the matrix whose `(i, σ)` coefficient is `σ (b i)`. * `algebra.embeddings_matrix_reindex A C b e : matrix κ κ C` is the matrix whose `(i, j)` coefficient is `σⱼ (b i)`, where `σⱼ : B →ₐ[A] C` is the embedding corresponding to `j : κ` given by a bijection `e : κ ≃ (B →ₐ[A] C)`. ## Main results * `trace_algebra_map_of_basis`, `trace_algebra_map`: if `x : K`, then `Tr_{L/K} x = [L : K] x` * `trace_trace_of_basis`, `trace_trace`: `Tr_{L/K} (Tr_{F/L} x) = Tr_{F/K} x` * `trace_eq_sum_roots`: the trace of `x : K(x)` is the sum of all conjugate roots of `x` * `trace_eq_sum_embeddings`: the trace of `x : K(x)` is the sum of all embeddings of `x` into an algebraically closed field * `trace_form_nondegenerate`: the trace form over a separable extension is a nondegenerate bilinear form ## Implementation notes Typically, the trace is defined specifically for finite field extensions. The definition is as general as possible and the assumption that we have fields or that the extension is finite is added to the lemmas as needed. We only define the trace for left multiplication (`algebra.left_mul_matrix`, i.e. `algebra.lmul_left`). For now, the definitions assume `S` is commutative, so the choice doesn't matter anyway. ## References * https://en.wikipedia.org/wiki/Field_trace -/ universes u v w z variables {R S T : Type*} [comm_ring R] [comm_ring S] [comm_ring T] variables [algebra R S] [algebra R T] variables {K L : Type*} [field K] [field L] [algebra K L] variables {ι κ : Type w} [fintype ι] open finite_dimensional open linear_map open matrix open_locale big_operators open_locale matrix namespace algebra variables (b : basis ι R S) variables (R S) /-- The trace of an element `s` of an `R`-algebra is the trace of `(*) s`, as an `R`-linear map. -/ noncomputable def trace : S →ₗ[R] R := (linear_map.trace R S).comp (lmul R S).to_linear_map variables {S} -- Not a `simp` lemma since there are more interesting ways to rewrite `trace R S x`, -- for example `trace_trace` lemma trace_apply (x) : trace R S x = linear_map.trace R S (lmul R S x) := rfl lemma trace_eq_zero_of_not_exists_basis (h : ¬ ∃ (s : finset S), nonempty (basis s R S)) : trace R S = 0 := by { ext s, simp [trace_apply, linear_map.trace, h] } include b variables {R} -- Can't be a `simp` lemma because it depends on a choice of basis lemma trace_eq_matrix_trace [decidable_eq ι] (b : basis ι R S) (s : S) : trace R S s = matrix.trace (algebra.left_mul_matrix b s) := by rw [trace_apply, linear_map.trace_eq_matrix_trace _ b, to_matrix_lmul_eq] /-- If `x` is in the base field `K`, then the trace is `[L : K] * x`. -/ lemma trace_algebra_map_of_basis (x : R) : trace R S (algebra_map R S x) = fintype.card ι • x := begin haveI := classical.dec_eq ι, rw [trace_apply, linear_map.trace_eq_matrix_trace R b, matrix.trace], convert finset.sum_const _, ext i, simp, end omit b /-- If `x` is in the base field `K`, then the trace is `[L : K] * x`. (If `L` is not finite-dimensional over `K`, then `trace` and `finrank` return `0`.) -/ @[simp] lemma trace_algebra_map (x : K) : trace K L (algebra_map K L x) = finrank K L • x := begin by_cases H : ∃ (s : finset L), nonempty (basis s K L), { rw [trace_algebra_map_of_basis H.some_spec.some, finrank_eq_card_basis H.some_spec.some] }, { simp [trace_eq_zero_of_not_exists_basis K H, finrank_eq_zero_of_not_exists_basis_finset H] } end lemma trace_trace_of_basis [algebra S T] [is_scalar_tower R S T] {ι κ : Type*} [fintype ι] [fintype κ] (b : basis ι R S) (c : basis κ S T) (x : T) : trace R S (trace S T x) = trace R T x := begin haveI := classical.dec_eq ι, haveI := classical.dec_eq κ, rw [trace_eq_matrix_trace (b.smul c), trace_eq_matrix_trace b, trace_eq_matrix_trace c, matrix.trace, matrix.trace, matrix.trace, ← finset.univ_product_univ, finset.sum_product], refine finset.sum_congr rfl (λ i _, _), simp only [alg_hom.map_sum, smul_left_mul_matrix, finset.sum_apply, matrix.diag, -- The unifier is not smart enough to apply this one by itself: finset.sum_apply i _ (λ y, left_mul_matrix b (left_mul_matrix c x y y))] end lemma trace_comp_trace_of_basis [algebra S T] [is_scalar_tower R S T] {ι κ : Type*} [fintype ι] [fintype κ] (b : basis ι R S) (c : basis κ S T) : (trace R S).comp ((trace S T).restrict_scalars R) = trace R T := by { ext, rw [linear_map.comp_apply, linear_map.restrict_scalars_apply, trace_trace_of_basis b c] } @[simp] lemma trace_trace [algebra K T] [algebra L T] [is_scalar_tower K L T] [finite_dimensional K L] [finite_dimensional L T] (x : T) : trace K L (trace L T x) = trace K T x := trace_trace_of_basis (basis.of_vector_space K L) (basis.of_vector_space L T) x @[simp] lemma trace_comp_trace [algebra K T] [algebra L T] [is_scalar_tower K L T] [finite_dimensional K L] [finite_dimensional L T] : (trace K L).comp ((trace L T).restrict_scalars K) = trace K T := by { ext, rw [linear_map.comp_apply, linear_map.restrict_scalars_apply, trace_trace] } section trace_form variables (R S) /-- The `trace_form` maps `x y : S` to the trace of `x * y`. It is a symmetric bilinear form and is nondegenerate if the extension is separable. -/ noncomputable def trace_form : bilin_form R S := (linear_map.compr₂ (lmul R S).to_linear_map (trace R S)).to_bilin variables {S} -- This is a nicer lemma than the one produced by `@[simps] def trace_form`. @[simp] lemma trace_form_apply (x y : S) : trace_form R S x y = trace R S (x * y) := rfl lemma trace_form_is_symm : (trace_form R S).is_symm := λ x y, congr_arg (trace R S) (mul_comm _ _) lemma trace_form_to_matrix [decidable_eq ι] (i j) : bilin_form.to_matrix b (trace_form R S) i j = trace R S (b i * b j) := by rw [bilin_form.to_matrix_apply, trace_form_apply] lemma trace_form_to_matrix_power_basis (h : power_basis R S) : bilin_form.to_matrix h.basis (trace_form R S) = λ i j, (trace R S (h.gen ^ (i + j : ℕ))) := by { ext, rw [trace_form_to_matrix, pow_add, h.basis_eq_pow, h.basis_eq_pow] } end trace_form end algebra section eq_sum_roots open algebra polynomial variables {F : Type*} [field F] variables [algebra K S] [algebra K F] /-- Given `pb : power_basis K S`, the trace of `pb.gen` is `-(minpoly K pb.gen).next_coeff`. -/ lemma power_basis.trace_gen_eq_next_coeff_minpoly [nontrivial S] (pb : power_basis K S) : algebra.trace K S pb.gen = -(minpoly K pb.gen).next_coeff := begin have d_pos : 0 < pb.dim := power_basis.dim_pos pb, have d_pos' : 0 < (minpoly K pb.gen).nat_degree, { simpa }, haveI : nonempty (fin pb.dim) := ⟨⟨0, d_pos⟩⟩, rw [trace_eq_matrix_trace pb.basis, trace_eq_neg_charpoly_coeff, charpoly_left_mul_matrix, ← pb.nat_degree_minpoly, fintype.card_fin, ← next_coeff_of_pos_nat_degree _ d_pos'] end /-- Given `pb : power_basis K S`, then the trace of `pb.gen` is `((minpoly K pb.gen).map (algebra_map K F)).roots.sum`. -/ lemma power_basis.trace_gen_eq_sum_roots [nontrivial S] (pb : power_basis K S) (hf : (minpoly K pb.gen).splits (algebra_map K F)) : algebra_map K F (trace K S pb.gen) = ((minpoly K pb.gen).map (algebra_map K F)).roots.sum := begin rw [power_basis.trace_gen_eq_next_coeff_minpoly, ring_hom.map_neg, ← next_coeff_map (algebra_map K F).injective, sum_roots_eq_next_coeff_of_monic_of_split ((minpoly.monic (power_basis.is_integral_gen _)).map _) ((splits_id_iff_splits _).2 hf), neg_neg] end namespace intermediate_field.adjoin_simple open intermediate_field lemma trace_gen_eq_zero {x : L} (hx : ¬ is_integral K x) : algebra.trace K K⟮x⟯ (adjoin_simple.gen K x) = 0 := begin rw [trace_eq_zero_of_not_exists_basis, linear_map.zero_apply], contrapose! hx, obtain ⟨s, ⟨b⟩⟩ := hx, refine is_integral_of_mem_of_fg (K⟮x⟯).to_subalgebra _ x _, { exact (submodule.fg_iff_finite_dimensional _).mpr (finite_dimensional.of_finset_basis b) }, { exact subset_adjoin K _ (set.mem_singleton x) } end lemma trace_gen_eq_sum_roots (x : L) (hf : (minpoly K x).splits (algebra_map K F)) : algebra_map K F (trace K K⟮x⟯ (adjoin_simple.gen K x)) = ((minpoly K x).map (algebra_map K F)).roots.sum := begin have injKxL := (algebra_map K⟮x⟯ L).injective, by_cases hx : is_integral K x, swap, { simp [minpoly.eq_zero hx, trace_gen_eq_zero hx], }, have hx' : is_integral K (adjoin_simple.gen K x), { rwa [← is_integral_algebra_map_iff injKxL, adjoin_simple.algebra_map_gen], apply_instance }, rw [← adjoin.power_basis_gen hx, (adjoin.power_basis hx).trace_gen_eq_sum_roots]; rw [adjoin.power_basis_gen hx, minpoly.eq_of_algebra_map_eq injKxL hx']; try { simp only [adjoin_simple.algebra_map_gen _ _] }, exact hf end end intermediate_field.adjoin_simple open intermediate_field variables (K) lemma trace_eq_trace_adjoin [finite_dimensional K L] (x : L) : algebra.trace K L x = finrank K⟮x⟯ L • trace K K⟮x⟯ (adjoin_simple.gen K x) := begin rw ← @trace_trace _ _ K K⟮x⟯ _ _ _ _ _ _ _ _ x, conv in x { rw ← intermediate_field.adjoin_simple.algebra_map_gen K x }, rw [trace_algebra_map, linear_map.map_smul_of_tower], end variables {K} lemma trace_eq_sum_roots [finite_dimensional K L] {x : L} (hF : (minpoly K x).splits (algebra_map K F)) : algebra_map K F (algebra.trace K L x) = finrank K⟮x⟯ L • ((minpoly K x).map (algebra_map K _)).roots.sum := by rw [trace_eq_trace_adjoin K x, algebra.smul_def, ring_hom.map_mul, ← algebra.smul_def, intermediate_field.adjoin_simple.trace_gen_eq_sum_roots _ hF, is_scalar_tower.algebra_map_smul] end eq_sum_roots variables {F : Type*} [field F] variables [algebra R L] [algebra L F] [algebra R F] [is_scalar_tower R L F] open polynomial lemma algebra.is_integral_trace [finite_dimensional L F] {x : F} (hx : _root_.is_integral R x) : _root_.is_integral R (algebra.trace L F x) := begin have hx' : _root_.is_integral L x := is_integral_of_is_scalar_tower _ hx, rw [← is_integral_algebra_map_iff (algebra_map L (algebraic_closure F)).injective, trace_eq_sum_roots], { refine (is_integral.multiset_sum _).nsmul _, intros y hy, rw mem_roots_map (minpoly.ne_zero hx') at hy, use [minpoly R x, minpoly.monic hx], rw ← aeval_def at ⊢ hy, exact minpoly.aeval_of_is_scalar_tower R x y hy }, { apply is_alg_closed.splits_codomain }, { apply_instance } end section eq_sum_embeddings variables [algebra K F] [is_scalar_tower K L F] open algebra intermediate_field variables (F) (E : Type*) [field E] [algebra K E] lemma trace_eq_sum_embeddings_gen (pb : power_basis K L) (hE : (minpoly K pb.gen).splits (algebra_map K E)) (hfx : (minpoly K pb.gen).separable) : algebra_map K E (algebra.trace K L pb.gen) = (@@finset.univ (power_basis.alg_hom.fintype pb)).sum (λ σ, σ pb.gen) := begin letI := classical.dec_eq E, rw [pb.trace_gen_eq_sum_roots hE, fintype.sum_equiv pb.lift_equiv', finset.sum_mem_multiset, finset.sum_eq_multiset_sum, multiset.to_finset_val, multiset.dedup_eq_self.mpr _, multiset.map_id], { exact nodup_roots ((separable_map _).mpr hfx) }, { intro x, refl }, { intro σ, rw [power_basis.lift_equiv'_apply_coe, id.def] } end variables [is_alg_closed E] lemma sum_embeddings_eq_finrank_mul [finite_dimensional K F] [is_separable K F] (pb : power_basis K L) : ∑ σ : F →ₐ[K] E, σ (algebra_map L F pb.gen) = finrank L F • (@@finset.univ (power_basis.alg_hom.fintype pb)).sum (λ σ : L →ₐ[K] E, σ pb.gen) := begin haveI : finite_dimensional L F := finite_dimensional.right K L F, haveI : is_separable L F := is_separable_tower_top_of_is_separable K L F, letI : fintype (L →ₐ[K] E) := power_basis.alg_hom.fintype pb, letI : ∀ (f : L →ₐ[K] E), fintype (@@alg_hom L F E _ _ _ _ f.to_ring_hom.to_algebra) := _, -- will be solved by unification rw [fintype.sum_equiv alg_hom_equiv_sigma (λ (σ : F →ₐ[K] E), _) (λ σ, σ.1 pb.gen), ← finset.univ_sigma_univ, finset.sum_sigma, ← finset.sum_nsmul], refine finset.sum_congr rfl (λ σ _, _), { letI : algebra L E := σ.to_ring_hom.to_algebra, simp only [finset.sum_const, finset.card_univ], rw alg_hom.card L F E }, { intros σ, simp only [alg_hom_equiv_sigma, equiv.coe_fn_mk, alg_hom.restrict_domain, alg_hom.comp_apply, is_scalar_tower.coe_to_alg_hom'] } end lemma trace_eq_sum_embeddings [finite_dimensional K L] [is_separable K L] {x : L} : algebra_map K E (algebra.trace K L x) = ∑ σ : L →ₐ[K] E, σ x := begin have hx := is_separable.is_integral K x, rw [trace_eq_trace_adjoin K x, algebra.smul_def, ring_hom.map_mul, ← adjoin.power_basis_gen hx, trace_eq_sum_embeddings_gen E (adjoin.power_basis hx) (is_alg_closed.splits_codomain _), ← algebra.smul_def, algebra_map_smul], { exact (sum_embeddings_eq_finrank_mul L E (adjoin.power_basis hx)).symm }, { haveI := is_separable_tower_bot_of_is_separable K K⟮x⟯ L, exact is_separable.separable K _ } end end eq_sum_embeddings section det_ne_zero namespace algebra variables (A : Type u) {B : Type v} (C : Type z) variables [comm_ring A] [comm_ring B] [algebra A B] [comm_ring C] [algebra A C] open finset /-- Given an `A`-algebra `B` and `b`, an `κ`-indexed family of elements of `B`, we define `trace_matrix A b` as the matrix whose `(i j)`-th element is the trace of `b i * b j`. -/ @[simp] noncomputable def trace_matrix (b : κ → B) : matrix κ κ A | i j := trace_form A B (b i) (b j) lemma trace_matrix_def (b : κ → B) : trace_matrix A b = λ i j, trace_form A B (b i) (b j) := rfl lemma trace_matrix_reindex {κ' : Type*} (b : basis κ A B) (f : κ ≃ κ') : trace_matrix A (b.reindex f) = reindex f f (trace_matrix A b) := by {ext x y, simp} variables {A} lemma trace_matrix_of_matrix_vec_mul [fintype κ] (b : κ → B) (P : matrix κ κ A) : trace_matrix A ((P.map (algebra_map A B)).vec_mul b) = Pᵀ ⬝ (trace_matrix A b) ⬝ P := begin ext α β, rw [trace_matrix, vec_mul, dot_product, vec_mul, dot_product, matrix.mul_apply, bilin_form.sum_left, fintype.sum_congr _ _ (λ (i : κ), @bilin_form.sum_right _ _ _ _ _ _ _ _ (b i * P.map (algebra_map A B) i α) (λ (y : κ), b y * P.map (algebra_map A B) y β)), sum_comm], congr, ext x, rw [matrix.mul_apply, sum_mul], congr, ext y, rw [map_apply, trace_form_apply, mul_comm (b y), ← smul_def], simp only [id.smul_eq_mul, ring_hom.id_apply, map_apply, transpose_apply, linear_map.map_smulₛₗ, trace_form_apply, algebra.smul_mul_assoc], rw [mul_comm (b x), ← smul_def], ring_nf, simp, end lemma trace_matrix_of_matrix_mul_vec [fintype κ] (b : κ → B) (P : matrix κ κ A) : trace_matrix A ((P.map (algebra_map A B)).mul_vec b) = P ⬝ (trace_matrix A b) ⬝ Pᵀ := begin refine add_equiv.injective transpose_add_equiv _, rw [transpose_add_equiv_apply, transpose_add_equiv_apply, ← vec_mul_transpose, ← transpose_map, trace_matrix_of_matrix_vec_mul, transpose_transpose, transpose_mul, transpose_transpose, transpose_mul] end lemma trace_matrix_of_basis [fintype κ] [decidable_eq κ] (b : basis κ A B) : trace_matrix A b = bilin_form.to_matrix b (trace_form A B) := begin ext i j, rw [trace_matrix, trace_form_apply, trace_form_to_matrix] end lemma trace_matrix_of_basis_mul_vec (b : basis ι A B) (z : B) : (trace_matrix A b).mul_vec (b.equiv_fun z) = (λ i, trace A B (z * (b i))) := begin ext i, rw [← col_apply ((trace_matrix A b).mul_vec (b.equiv_fun z)) i unit.star, col_mul_vec, matrix.mul_apply, trace_matrix_def], simp only [col_apply, trace_form_apply], conv_lhs { congr, skip, funext, rw [mul_comm _ (b.equiv_fun z _), ← smul_eq_mul, ← linear_map.map_smul] }, rw [← linear_map.map_sum], congr, conv_lhs { congr, skip, funext, rw [← mul_smul_comm] }, rw [← finset.mul_sum, mul_comm z], congr, rw [b.sum_equiv_fun ] end variable (A) /-- `embeddings_matrix A C b : matrix κ (B →ₐ[A] C) C` is the matrix whose `(i, σ)` coefficient is `σ (b i)`. It is mostly useful for fields when `fintype.card κ = finrank A B` and `C` is algebraically closed. -/ @[simp] def embeddings_matrix (b : κ → B) : matrix κ (B →ₐ[A] C) C | i σ := σ (b i) /-- `embeddings_matrix_reindex A C b e : matrix κ κ C` is the matrix whose `(i, j)` coefficient is `σⱼ (b i)`, where `σⱼ : B →ₐ[A] C` is the embedding corresponding to `j : κ` given by a bijection `e : κ ≃ (B →ₐ[A] C)`. It is mostly useful for fields and `C` is algebraically closed. In this case, in presence of `h : fintype.card κ = finrank A B`, one can take `e := equiv_of_card_eq ((alg_hom.card A B C).trans h.symm)`. -/ def embeddings_matrix_reindex (b : κ → B) (e : κ ≃ (B →ₐ[A] C)) := reindex (equiv.refl κ) e.symm (embeddings_matrix A C b) variable {A} lemma embeddings_matrix_reindex_eq_vandermonde (pb : power_basis A B) (e : fin pb.dim ≃ (B →ₐ[A] C)) : embeddings_matrix_reindex A C pb.basis e = (vandermonde (λ i, e i pb.gen))ᵀ := by { ext i j, simp [embeddings_matrix_reindex, embeddings_matrix] } section field variables (K) {L} (E : Type z) [field E] variables [algebra K E] variables [module.finite K L] [is_separable K L] [is_alg_closed E] variables (b : κ → L) (pb : power_basis K L) lemma trace_matrix_eq_embeddings_matrix_mul_trans : (trace_matrix K b).map (algebra_map K E) = (embeddings_matrix K E b) ⬝ (embeddings_matrix K E b)ᵀ := by { ext i j, simp [trace_eq_sum_embeddings, embeddings_matrix, matrix.mul_apply] } lemma trace_matrix_eq_embeddings_matrix_reindex_mul_trans [fintype κ] (e : κ ≃ (L →ₐ[K] E)) : (trace_matrix K b).map (algebra_map K E) = (embeddings_matrix_reindex K E b e) ⬝ (embeddings_matrix_reindex K E b e)ᵀ := by rw [trace_matrix_eq_embeddings_matrix_mul_trans, embeddings_matrix_reindex, reindex_apply, transpose_minor, ← minor_mul_transpose_minor, ← equiv.coe_refl, equiv.refl_symm] end field end algebra open algebra variables (pb : power_basis K L) lemma det_trace_matrix_ne_zero' [is_separable K L] : det (trace_matrix K pb.basis) ≠ 0 := begin suffices : algebra_map K (algebraic_closure L) (det (trace_matrix K pb.basis)) ≠ 0, { refine mt (λ ht, _) this, rw [ht, ring_hom.map_zero] }, haveI : finite_dimensional K L := pb.finite_dimensional, let e : fin pb.dim ≃ (L →ₐ[K] algebraic_closure L) := (fintype.equiv_fin_of_card_eq _).symm, rw [ring_hom.map_det, ring_hom.map_matrix_apply, trace_matrix_eq_embeddings_matrix_reindex_mul_trans K _ _ e, embeddings_matrix_reindex_eq_vandermonde, det_mul, det_transpose], refine mt mul_self_eq_zero.mp _, { simp only [det_vandermonde, finset.prod_eq_zero_iff, not_exists, sub_eq_zero], intros i _ j hij h, exact (finset.mem_filter.mp hij).2.ne' (e.injective $ pb.alg_hom_ext h) }, { rw [alg_hom.card, pb.finrank] } end lemma det_trace_form_ne_zero [is_separable K L] [decidable_eq ι] (b : basis ι K L) : det (bilin_form.to_matrix b (trace_form K L)) ≠ 0 := begin haveI : finite_dimensional K L := finite_dimensional.of_fintype_basis b, let pb : power_basis K L := field.power_basis_of_finite_of_separable _ _, rw [← bilin_form.to_matrix_mul_basis_to_matrix pb.basis b, ← det_comm' (pb.basis.to_matrix_mul_to_matrix_flip b) _, ← matrix.mul_assoc, det_mul], swap, { apply basis.to_matrix_mul_to_matrix_flip }, refine mul_ne_zero (is_unit_of_mul_eq_one _ ((b.to_matrix pb.basis)ᵀ ⬝ b.to_matrix pb.basis).det _).ne_zero _, { calc (pb.basis.to_matrix b ⬝ (pb.basis.to_matrix b)ᵀ).det * ((b.to_matrix pb.basis)ᵀ ⬝ b.to_matrix pb.basis).det = (pb.basis.to_matrix b ⬝ (b.to_matrix pb.basis ⬝ pb.basis.to_matrix b)ᵀ ⬝ b.to_matrix pb.basis).det : by simp only [← det_mul, matrix.mul_assoc, matrix.transpose_mul] ... = 1 : by simp only [basis.to_matrix_mul_to_matrix_flip, matrix.transpose_one, matrix.mul_one, matrix.det_one] }, simpa only [trace_matrix_of_basis] using det_trace_matrix_ne_zero' pb end variables (K L) theorem trace_form_nondegenerate [finite_dimensional K L] [is_separable K L] : (trace_form K L).nondegenerate := bilin_form.nondegenerate_of_det_ne_zero (trace_form K L) _ (det_trace_form_ne_zero (finite_dimensional.fin_basis K L)) end det_ne_zero
2b8a67793fbf4fe88f37831c2e5a9f20bad49386
947b78d97130d56365ae2ec264df196ce769371a
/tests/lean/run/int_to_nat_bug.lean
f0088f095dae867519780b62e7ff7d9ea95bdb7a
[ "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
38
lean
new_frontend #eval (4294967295 : Int)
1a74a08828e18d55a9540bb4af05690030635153
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/private_names.lean
b711390306458e94ce1e281c2a615069df9463d2
[ "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
187
lean
namespace bla section private definition foo : inhabited Prop := inhabited.mk false attribute foo [instance] [priority 1000] example : default Prop = false := rfl end end bla
530d2ec0192792e5cf3f2c18008760808017c9a3
a44280b79dc85615010e3fbda46abf82c6730fa3
/library/init/data/array/basic.lean
8a85dcfccc47313893b26e635afb5e20c422d925
[ "Apache-2.0" ]
permissive
kodyvajjha/lean4
8e1c613248b531d47367ca6e8d97ee1046645aa1
c8a045d69fac152fd5e3a577f718615cecb9c53d
refs/heads/master
1,589,684,450,102
1,555,200,447,000
1,556,139,945,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
8,088
lean
/- Copyright (c) 2018 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.nat.basic init.data.fin.basic init.data.uint import init.data.repr init.data.tostring init.control.id universes u v w /- The Compiler has special support for arrays. They are implemented using dynamic arrays: https://en.wikipedia.org/wiki/Dynamic_array -/ structure Array (α : Type u) := (sz : Nat) (data : Fin sz → α) attribute [extern cpp inline "lean::array_sz(#2)"] Array.sz @[reducible, extern cpp inline "lean::array_get_size(#2)"] def Array.size {α : Type u} (a : @& Array α) : Nat := a.sz namespace Array variables {α : Type u} {β : Type v} /- The parameter `c` is the initial capacity -/ @[extern cpp inline "lean::mk_empty_array(#2)"] def mkEmpty (c : @& Nat) : Array α := { sz := 0, data := λ ⟨x, h⟩, absurd h (Nat.notLtZero x) } @[extern cpp inline "lean::array_push(#2, #3)"] def push (a : Array α) (v : α) : Array α := { sz := Nat.succ a.sz, data := λ ⟨j, h₁⟩, if h₂ : j = a.sz then v else a.data ⟨j, Nat.ltOfLeOfNe (Nat.leOfLtSucc h₁) h₂⟩ } @[extern cpp inline "lean::mk_array(#2, #3)"] def mkArray {α : Type u} (n : Nat) (v : α) : Array α := { sz := n, data := λ _, v} theorem szMkArrayEq {α : Type u} (n : Nat) (v : α) : (mkArray n v).sz = n := rfl def empty : Array α := mkEmpty 0 instance : HasEmptyc (Array α) := ⟨Array.empty⟩ instance : Inhabited (Array α) := ⟨Array.empty⟩ def isEmpty (a : Array α) : Bool := a.size = 0 def singleton (v : α) : Array α := mkArray 1 v @[extern cpp inline "lean::array_index(#2, #3)"] def index (a : @& Array α) (i : @& Fin a.size) : α := a.data i /- Low-level version of `index` which is as fast as a C array read. `Fin` values are represented as tag pointers in the Lean runtime. Thus, `index` may be slightly slower than `idx`. -/ @[extern cpp inline "lean::array_idx(#2, #3)"] def idx (a : @& Array α) (i : USize) (h : i.toNat < a.size) : α := a.index ⟨i.toNat, h⟩ /- "Comfortable" version of `index`. It performs a bound check at runtime. -/ @[extern cpp inline "lean::array_get(#2, #3, #4)"] def get [Inhabited α] (a : @& Array α) (i : @& Nat) : α := if h : i < a.size then a.index ⟨i, h⟩ else default α def back [Inhabited α] (a : Array α) : α := a.get (a.size - 1) def getOpt (a : Array α) (i : Nat) : Option α := if h : i < a.size then some (a.index ⟨i, h⟩) else none @[extern cpp inline "lean::array_update(#2, #3, #4)"] def update (a : Array α) (i : @& Fin a.size) (v : α) : Array α := { sz := a.sz, data := λ j, if h : i = j then v else a.data j } /- Low-level version of `update` which is as fast as a C array update. `Fin` values are represented as tag pointers in the Lean runtime. Thus, `update` may be slightly slower than `updt`. -/ @[extern cpp inline "lean::array_updt(#2, #3, #4)"] def updt (a : Array α) (i : USize) (v : α) (h : i.toNat < a.size) : Array α := a.update ⟨i.toNat, h⟩ v /- "Comfortable" version of `update`. It performs a bound check at runtime. -/ @[extern cpp inline "lean::array_set(#2, #3, #4)"] def set (a : Array α) (i : @& Nat) (v : α) : Array α := if h : i < a.size then a.update ⟨i, h⟩ v else a theorem szUpdateEq (a : Array α) (i : Fin a.size) (v : α) : (update a i v).size = a.size := rfl @[extern cpp inline "lean::array_pop(#2)"] def pop (a : Array α) : Array α := { sz := Nat.pred a.size, data := λ ⟨j, h⟩, a.index ⟨j, Nat.ltOfLtOfLe h (Nat.predLe _)⟩ } -- TODO(Leo): justify termination using wf-rec partial def shrink : Array α → Nat → Array α | a n := if n ≥ a.size then a else shrink a.pop n section variables {m : Type v → Type v} [Monad m] local attribute [instance] monadInhabited' -- TODO(Leo): justify termination using wf-rec @[specialize] partial def miterateAux (a : Array α) (f : Π i : Fin a.size, α → β → m β) : Nat → β → m β | i b := if h : i < a.size then let idx : Fin a.size := ⟨i, h⟩ in f idx (a.index idx) b >>= miterateAux (i+1) else pure b @[inline] def miterate (a : Array α) (b : β) (f : Π i : Fin a.size, α → β → m β) : m β := miterateAux a f 0 b @[inline] def mfoldl (a : Array α) (b : β) (f : α → β → m β) : m β := miterate a b (λ _, f) @[inline] def mfoldlFrom (a : Array α) (b : β) (f : α → β → m β) (ini : Nat := 0) : m β := miterateAux a (λ _, f) ini b local attribute [instance] monadInhabited -- TODO(Leo): justify termination using wf-rec @[specialize] partial def mfindAux (a : Array α) (f : α → m (Option β)) : Nat → m (Option β) | i := if h : i < a.size then let idx : Fin a.size := ⟨i, h⟩ in do r ← f (a.index idx), (match r with | some v := pure r | none := mfindAux (i+1)) else pure none @[inline] def mfind (a : Array α) (f : α → m (Option β)) : m (Option β) := mfindAux a f 0 end @[inline] def iterate (a : Array α) (b : β) (f : Π i : Fin a.size, α → β → β) : β := Id.run $ miterateAux a f 0 b @[inline] def foldl (a : Array α) (f : α → β → β) (b : β) : β := iterate a b (λ _, f) @[inline] def foldlFrom (a : Array α) (f : α → β → β) (b : β) (ini : Nat := 0) : β := Id.run $ mfoldlFrom a b f ini @[inline] def find (a : Array α) (f : α → Option β) : Option β := Id.run $ mfindAux a f 0 @[specialize] private def revIterateAux (a : Array α) (f : Π i : Fin a.size, α → β → β) : Π (i : Nat), i ≤ a.size → β → β | 0 h b := b | (j+1) h b := let i : Fin a.size := ⟨j, h⟩ in revIterateAux j (Nat.leOfLt h) (f i (a.index i) b) @[inline] def revIterate (a : Array α) (b : β) (f : Π i : Fin a.size, α → β → β) : β := revIterateAux a f a.size (Nat.leRefl _) b @[inline] def revFoldl (a : Array α) (b : β) (f : α → β → β) : β := revIterate a b (λ _, f) def toList (a : Array α) : List α := a.revFoldl [] (::) instance [HasRepr α] : HasRepr (Array α) := ⟨repr ∘ toList⟩ instance [HasToString α] : HasToString (Array α) := ⟨toString ∘ toList⟩ section variables {m : Type u → Type u} [Monad m] @[inline] def mmap {β : Type u} (f : α → m β) (as : Array α) : m (Array β) := as.mfoldl (mkEmpty as.size) (λ a bs, do b ← f a, pure (bs.push b)) end section variables {m : Type u → Type v} [Monad m] [Inhabited α] local attribute [instance] monadInhabited' @[specialize] partial def hmmapAux (f : α → m α) : Nat → Array α → m (Array α) | i a := if h : i < a.size then let idx : Fin a.size := ⟨i, h⟩ in let v : α := a.index idx in let a := a.update idx (default α) in do v ← f v, hmmapAux (i+1) (a.update idx v) else pure a /- Homogeneous `mmap` -/ @[inline] def hmmap (f : α → m α) (a : Array α) : m (Array α) := hmmapAux f 0 a end /- Homogeneous map -/ @[inline] def hmap [Inhabited α] (f : α → α) (a : Array α) : Array α := Id.run $ hmmap f a @[inline] def map (f : α → β) (as : Array α) : Array β := as.foldl (λ a bs, bs.push (f a)) (mkEmpty as.size) -- TODO(Leo): justify termination using wf-rec partial def extractAux (a : Array α) : Nat → Π (e : Nat), e ≤ a.size → Array α → Array α | i e hle r := if hlt : i < e then let idx : Fin a.size := ⟨i, Nat.ltOfLtOfLe hlt hle⟩ in extractAux (i+1) e hle (r.push (a.index idx)) else r def extract (a : Array α) (b e : Nat) : Array α := let r : Array α := mkEmpty (e - b) in if h : e ≤ a.size then extractAux a b e h r else r end Array export Array (mkArray) @[inlineIfReduce] def List.toArrayAux {α : Type u} : List α → Array α → Array α | [] r := r | (a::as) r := List.toArrayAux as (r.push a) @[inlineIfReduce] def List.redLength {α : Type u} : List α → Nat | [] := 0 | (_::as) := as.redLength + 1 @[inline] def List.toArray {α : Type u} (as : List α) : Array α := as.toArrayAux (Array.mkEmpty as.redLength)
ea72b4917dc7e264e7baf213953e745734afe47b
c777c32c8e484e195053731103c5e52af26a25d1
/src/number_theory/modular_forms/congruence_subgroups.lean
18d42bbc9f7fa1f61a45e3ba922910e9b576234f
[ "Apache-2.0" ]
permissive
kbuzzard/mathlib
2ff9e85dfe2a46f4b291927f983afec17e946eb8
58537299e922f9c77df76cb613910914a479c1f7
refs/heads/master
1,685,313,702,744
1,683,974,212,000
1,683,974,212,000
128,185,277
1
0
null
1,522,920,600,000
1,522,920,600,000
null
UTF-8
Lean
false
false
7,973
lean
/- Copyright (c) 2022 Chris Birkbeck. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Birkbeck -/ import data.zmod.basic import group_theory.group_action.conj_act import group_theory.subgroup.pointwise import linear_algebra.matrix.special_linear_group /-! # Congruence subgroups This defines congruence subgroups of `SL(2, ℤ)` such as `Γ(N)`, `Γ₀(N)` and `Γ₁(N)` for `N` a natural number. It also contains basic results about congruence subgroups. -/ local notation `SL(` n `, ` R `)`:= matrix.special_linear_group (fin n) R local attribute [-instance] matrix.special_linear_group.has_coe_to_fun local prefix `↑ₘ`:1024 := @coe _ (matrix (fin 2) (fin 2) _) _ open matrix.special_linear_group matrix variable (N : ℕ) local notation `SLMOD(`N`)` := @matrix.special_linear_group.map (fin 2) _ _ _ _ _ _ (int.cast_ring_hom (zmod N)) @[simp] lemma SL_reduction_mod_hom_val (N : ℕ) (γ : SL(2, ℤ)) : ∀ (i j : fin 2), ((SLMOD(N) γ) : (matrix (fin 2) (fin 2) (zmod N))) i j = (((↑ₘγ i j) : ℤ) : zmod N) := λ i j, rfl /--The full level `N` congruence subgroup of `SL(2, ℤ)` of matrices that reduce to the identity modulo `N`.-/ def Gamma (N : ℕ) : subgroup SL(2, ℤ) := (SLMOD(N)).ker lemma Gamma_mem' (N : ℕ) (γ : SL(2, ℤ)) : γ ∈ Gamma N ↔ SLMOD(N) γ = 1 := iff.rfl @[simp] lemma Gamma_mem (N : ℕ) (γ : SL(2, ℤ)) : γ ∈ Gamma N ↔ (((↑ₘγ 0 0) : ℤ) : zmod N) = 1 ∧ (((↑ₘγ 0 1) : ℤ) : zmod N) = 0 ∧ (((↑ₘγ 1 0) : ℤ) : zmod N) = 0 ∧ (((↑ₘγ 1 1) : ℤ) : zmod N) = 1 := begin rw Gamma_mem', split, { intro h, simp [←(SL_reduction_mod_hom_val N γ), h] }, { intro h, ext, rw SL_reduction_mod_hom_val N γ, fin_cases i; fin_cases j, all_goals {simp_rw h, refl} } end lemma Gamma_normal (N : ℕ) : subgroup.normal (Gamma N) := (SLMOD(N)).normal_ker lemma Gamma_one_top : Gamma 1 = ⊤ := begin ext, simp, end lemma Gamma_zero_bot : Gamma 0 = ⊥ := begin ext, simp only [Gamma_mem, coe_coe, coe_matrix_coe, int.coe_cast_ring_hom, map_apply, int.cast_id, subgroup.mem_bot], split, { intro h, ext, fin_cases i; fin_cases j, any_goals {simp [h]} }, { intro h, simp [h] } end /--The congruence subgroup of `SL(2, ℤ)` of matrices whose lower left-hand entry reduces to zero modulo `N`. -/ def Gamma0 (N : ℕ) : subgroup SL(2, ℤ) := { carrier := { g : SL(2, ℤ) | ((↑ₘg 1 0 : ℤ) : zmod N) = 0 }, one_mem' := by { simp }, mul_mem':= by {intros a b ha hb, simp only [ set.mem_set_of_eq], have h := ((matrix.two_mul_expl a.1 b.1).2.2.1), simp only [coe_coe, coe_matrix_coe, coe_mul, int.coe_cast_ring_hom, map_apply, set.mem_set_of_eq, subtype.val_eq_coe, mul_eq_mul] at *, rw h, simp [ha, hb] }, inv_mem':= by {intros a ha, simp only [ set.mem_set_of_eq, subtype.val_eq_coe], rw (SL2_inv_expl a), simp only [subtype.val_eq_coe, cons_val_zero, cons_val_one, head_cons, coe_coe, coe_matrix_coe, coe_mk, int.coe_cast_ring_hom, map_apply, int.cast_neg, neg_eq_zero, set.mem_set_of_eq] at *, exact ha } } @[simp] lemma Gamma0_mem (N : ℕ) (A: SL(2, ℤ)) : A ∈ Gamma0 N ↔ (((↑ₘA) 1 0 : ℤ) : zmod N) = 0 := iff.rfl lemma Gamma0_det (N : ℕ) (A : Gamma0 N) : (A.1.1.det : zmod N) = 1 := by {simp [A.1.property]} /--The group homomorphism from `Gamma0` to `zmod N` given by mapping a matrix to its lower right-hand entry. -/ def Gamma_0_map (N : ℕ): Gamma0 N →* zmod N := { to_fun := λ g, ((↑ₘg 1 1 : ℤ) : zmod N), map_one' := by { simp, }, map_mul' := by {intros A B, have := (two_mul_expl A.1.1 B.1.1).2.2.2, simp only [coe_coe, subgroup.coe_mul, coe_matrix_coe, coe_mul, int.coe_cast_ring_hom, map_apply, subtype.val_eq_coe, mul_eq_mul] at *, rw this, have ha := A.property, simp only [int.cast_add, int.cast_mul, add_left_eq_self, subtype.val_eq_coe, Gamma0_mem, coe_coe, coe_matrix_coe, int.coe_cast_ring_hom, map_apply] at *, rw ha, simp,} } /--The congruence subgroup `Gamma1` (as a subgroup of `Gamma0`) of matrices whose bottom row is congruent to `(0,1)` modulo `N`.-/ def Gamma1' (N : ℕ) : subgroup (Gamma0 N) := (Gamma_0_map N).ker @[simp] lemma Gamma1_mem' (N : ℕ) (γ : Gamma0 N) : γ ∈ Gamma1' N ↔ (Gamma_0_map N) γ = 1 := iff.rfl lemma Gamma1_to_Gamma0_mem (N : ℕ) (A : Gamma0 N) : A ∈ Gamma1' N ↔ ((↑ₘA 0 0 : ℤ) : zmod N) = 1 ∧ ((↑ₘA 1 1 : ℤ) : zmod N) = 1 ∧ ((↑ₘA 1 0 : ℤ) : zmod N) = 0 := begin split, { intro ha, have hA := A.property, rw Gamma0_mem at hA, have adet := Gamma0_det N A, rw matrix.det_fin_two at adet, simp only [Gamma_0_map, coe_coe, coe_matrix_coe, int.coe_cast_ring_hom, map_apply, Gamma1_mem', monoid_hom.coe_mk, subtype.val_eq_coe, int.cast_sub, int.cast_mul] at *, rw [hA, ha] at adet, simp only [mul_one, mul_zero, sub_zero] at adet, simp only [adet, hA, ha, eq_self_iff_true, and_self]}, { intro ha, simp only [Gamma1_mem', Gamma_0_map, monoid_hom.coe_mk, coe_coe, coe_matrix_coe, int.coe_cast_ring_hom, map_apply], exact ha.2.1,} end /--The congruence subgroup `Gamma1` of `SL(2, ℤ)` consisting of matrices whose bottom row is congruent to `(0,1)` modulo `N`. -/ def Gamma1 (N : ℕ) : subgroup SL(2, ℤ) := subgroup.map (((Gamma0 N).subtype).comp (Gamma1' N).subtype) ⊤ @[simp] lemma Gamma1_mem (N : ℕ) (A : SL(2, ℤ)) : A ∈ Gamma1 N ↔ ((↑ₘA 0 0 : ℤ) : zmod N) = 1 ∧ ((↑ₘA 1 1 : ℤ) : zmod N) = 1 ∧ ((↑ₘA 1 0 : ℤ) : zmod N) = 0 := begin split, { intro ha, simp_rw [Gamma1, subgroup.mem_map] at ha, simp at ha, obtain ⟨⟨x, hx⟩, hxx⟩ := ha, rw Gamma1_to_Gamma0_mem at hx, rw ←hxx, convert hx }, { intro ha, simp_rw [Gamma1, subgroup.mem_map], have hA : A ∈ (Gamma0 N), by {simp [ha.right.right, Gamma0_mem, subtype.val_eq_coe],}, have HA : (⟨A , hA⟩ : Gamma0 N) ∈ Gamma1' N, by {simp only [Gamma1_to_Gamma0_mem, subgroup.coe_mk, coe_coe, coe_matrix_coe, int.coe_cast_ring_hom, map_apply], exact ha,}, refine ⟨(⟨(⟨A , hA⟩ : Gamma0 N), HA ⟩ : (( Gamma1' N ) : subgroup (Gamma0 N))), _⟩, simp } end lemma Gamma1_in_Gamma0 (N : ℕ) : Gamma1 N ≤ Gamma0 N := begin intros x HA, simp only [Gamma0_mem, Gamma1_mem, coe_coe, coe_matrix_coe, int.coe_cast_ring_hom, map_apply] at *, exact HA.2.2, end section congruence_subgroup /--A congruence subgroup is a subgroup of `SL(2, ℤ)` which contains some `Gamma N` for some `(N : ℕ+)`. -/ def is_congruence_subgroup (Γ : subgroup SL(2, ℤ)) : Prop := ∃ (N : ℕ+), Gamma N ≤ Γ lemma is_congruence_subgroup_trans (H K : subgroup SL(2, ℤ)) (h: H ≤ K) (h2 : is_congruence_subgroup H) : is_congruence_subgroup K := begin obtain ⟨N , hN⟩ := h2, refine ⟨N, le_trans hN h⟩, end lemma Gamma_is_cong_sub (N : ℕ+) : is_congruence_subgroup (Gamma N) := ⟨N, by {simp only [le_refl]}⟩ lemma Gamma1_is_congruence (N : ℕ+) : is_congruence_subgroup (Gamma1 N) := begin refine ⟨N, _⟩, intros A hA, simp only [Gamma1_mem, Gamma_mem] at *, simp only [hA, eq_self_iff_true, and_self], end lemma Gamma0_is_congruence (N : ℕ+) : is_congruence_subgroup (Gamma0 N) := is_congruence_subgroup_trans _ _ (Gamma1_in_Gamma0 N) (Gamma1_is_congruence N) end congruence_subgroup section conjugation open_locale pointwise lemma Gamma_cong_eq_self (N : ℕ) (g : conj_act SL(2, ℤ)) : g • (Gamma N) = Gamma N := begin apply subgroup.normal.conj_act (Gamma_normal N), end lemma conj_cong_is_cong (g : conj_act SL(2, ℤ)) (Γ : subgroup SL(2, ℤ)) (h : is_congruence_subgroup Γ) : is_congruence_subgroup (g • Γ) := begin obtain ⟨N, HN⟩ := h, refine ⟨N, _⟩, rw [←Gamma_cong_eq_self N g, subgroup.pointwise_smul_le_pointwise_smul_iff], exact HN, end end conjugation
dad4670866b11c24b89254e3ccf6914b4263344f
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/intros_defeq_canonizer_bug.lean
1983055868d7733269625a99bf89739fcabe78cf
[ "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
368
lean
class ring (α : Type*) extends has_zero α. constant {u} f {α : Type u} : α → α → α → α axiom {u} fax {α : Type u} [ring α] (a b : α) : f a b a = 0 attribute [ematch] fax universe variables u class field (α : Type*) extends ring α, has_add α. lemma ex {α : Type u} [field α] (x y : α) : f (x + y) (y + x) (x + y) = 0 := begin [smt] ematch end
c6b6d5440c914598d0e44de52bcd6c65c6d557aa
4950bf76e5ae40ba9f8491647d0b6f228ddce173
/src/algebra/group/hom.lean
cea98937f03a5259fa217cfb2bfddb0d4175bd50
[ "Apache-2.0" ]
permissive
ntzwq/mathlib
ca50b21079b0a7c6781c34b62199a396dd00cee2
36eec1a98f22df82eaccd354a758ef8576af2a7f
refs/heads/master
1,675,193,391,478
1,607,822,996,000
1,607,822,996,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
34,984
lean
/- Copyright (c) 2018 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot, Kevin Buzzard, Scott Morrison, Johan Commelin, Chris Hughes, Johannes Hölzl, Yury Kudryashov -/ import algebra.group.commute import algebra.group_with_zero.defs /-! # monoid and group homomorphisms This file defines the bundled structures for monoid and group homomorphisms. Namely, we define `monoid_hom` (resp., `add_monoid_hom`) to be bundled homomorphisms between multiplicative (resp., additive) monoids or groups. We also define coercion to a function, and usual operations: composition, identity homomorphism, pointwise multiplication and pointwise inversion. This file also defines the lesser-used (and notation-less) homomorphism types which are used as building blocks for other homomorphisms: * `zero_hom` * `one_hom` * `add_hom` * `mul_hom` * `monoid_with_zero_hom` ## Notations * `→*` for bundled monoid homs (also use for group homs) * `→+` for bundled add_monoid homs (also use for add_group homs) ## implementation notes There's a coercion from bundled homs to fun, and the canonical notation is to use the bundled hom as a function via this coercion. There is no `group_hom` -- the idea is that `monoid_hom` is used. The constructor for `monoid_hom` needs a proof of `map_one` as well as `map_mul`; a separate constructor `monoid_hom.mk'` will construct group homs (i.e. monoid homs between groups) given only a proof that multiplication is preserved, Implicit `{}` brackets are often used instead of type class `[]` brackets. This is done when the instances can be inferred because they are implicit arguments to the type `monoid_hom`. When they can be inferred from the type it is faster to use this method than to use type class inference. Historically this file also included definitions of unbundled homomorphism classes; they were deprecated and moved to `deprecated/group`. ## Tags monoid_hom, add_monoid_hom -/ variables {M : Type*} {N : Type*} {P : Type*} -- monoids {G : Type*} {H : Type*} -- groups -- for easy multiple inheritance set_option old_structure_cmd true /-- Homomorphism that preserves zero -/ structure zero_hom (M : Type*) (N : Type*) [has_zero M] [has_zero N] := (to_fun : M → N) (map_zero' : to_fun 0 = 0) /-- Homomorphism that preserves addition -/ structure add_hom (M : Type*) (N : Type*) [has_add M] [has_add N] := (to_fun : M → N) (map_add' : ∀ x y, to_fun (x + y) = to_fun x + to_fun y) /-- Bundled add_monoid homomorphisms; use this for bundled add_group homomorphisms too. -/ structure add_monoid_hom (M : Type*) (N : Type*) [add_monoid M] [add_monoid N] extends zero_hom M N, add_hom M N attribute [nolint doc_blame] add_monoid_hom.to_add_hom attribute [nolint doc_blame] add_monoid_hom.to_zero_hom infixr ` →+ `:25 := add_monoid_hom /-- Homomorphism that preserves one -/ @[to_additive] structure one_hom (M : Type*) (N : Type*) [has_one M] [has_one N] := (to_fun : M → N) (map_one' : to_fun 1 = 1) /-- Homomorphism that preserves multiplication -/ @[to_additive] structure mul_hom (M : Type*) (N : Type*) [has_mul M] [has_mul N] := (to_fun : M → N) (map_mul' : ∀ x y, to_fun (x * y) = to_fun x * to_fun y) /-- Bundled monoid homomorphisms; use this for bundled group homomorphisms too. -/ @[to_additive] structure monoid_hom (M : Type*) (N : Type*) [monoid M] [monoid N] extends one_hom M N, mul_hom M N /-- Bundled monoid with zero homomorphisms; use this for bundled group with zero homomorphisms too. -/ structure monoid_with_zero_hom (M : Type*) (N : Type*) [monoid_with_zero M] [monoid_with_zero N] extends zero_hom M N, monoid_hom M N attribute [nolint doc_blame, to_additive] monoid_hom.to_mul_hom attribute [nolint doc_blame, to_additive] monoid_hom.to_one_hom attribute [nolint doc_blame] monoid_with_zero_hom.to_monoid_hom attribute [nolint doc_blame] monoid_with_zero_hom.to_zero_hom infixr ` →* `:25 := monoid_hom -- completely uninteresting lemmas about coercion to function, that all homs need section coes /-! Bundled morphisms can be down-cast to weaker bundlings -/ @[to_additive] instance monoid_hom.has_coe_to_one_hom {mM : monoid M} {mN : monoid N} : has_coe (M →* N) (one_hom M N) := ⟨monoid_hom.to_one_hom⟩ @[to_additive] instance monoid_hom.has_coe_to_mul_hom {mM : monoid M} {mN : monoid N} : has_coe (M →* N) (mul_hom M N) := ⟨monoid_hom.to_mul_hom⟩ instance monoid_with_zero_hom.has_coe_to_monoid_hom {mM : monoid_with_zero M} {mN : monoid_with_zero N} : has_coe (monoid_with_zero_hom M N) (M →* N) := ⟨monoid_with_zero_hom.to_monoid_hom⟩ instance monoid_with_zero_hom.has_coe_to_zero_hom {mM : monoid_with_zero M} {mN : monoid_with_zero N} : has_coe (monoid_with_zero_hom M N) (zero_hom M N) := ⟨monoid_with_zero_hom.to_zero_hom⟩ /-! The simp-normal form of morphism coercion is `f.to_..._hom`. This choice is primarily because this is the way things were before the above coercions were introduced. Bundled morphisms defined elsewhere in Mathlib may choose `↑f` as their simp-normal form instead. -/ @[simp, to_additive] lemma monoid_hom.coe_eq_to_one_hom {mM : monoid M} {mN : monoid N} (f : M →* N) : (f : one_hom M N) = f.to_one_hom := rfl @[simp, to_additive] lemma monoid_hom.coe_eq_to_mul_hom {mM : monoid M} {mN : monoid N} (f : M →* N) : (f : mul_hom M N) = f.to_mul_hom := rfl @[simp] lemma monoid_with_zero_hom.coe_eq_to_monoid_hom {mM : monoid_with_zero M} {mN : monoid_with_zero N} (f : monoid_with_zero_hom M N) : (f : M →* N) = f.to_monoid_hom := rfl @[simp] lemma monoid_with_zero_hom.coe_eq_to_zero_hom {mM : monoid_with_zero M} {mN : monoid_with_zero N} (f : monoid_with_zero_hom M N) : (f : zero_hom M N) = f.to_zero_hom := rfl @[to_additive] instance {mM : has_one M} {mN : has_one N} : has_coe_to_fun (one_hom M N) := ⟨_, one_hom.to_fun⟩ @[to_additive] instance {mM : has_mul M} {mN : has_mul N} : has_coe_to_fun (mul_hom M N) := ⟨_, mul_hom.to_fun⟩ @[to_additive] instance {mM : monoid M} {mN : monoid N} : has_coe_to_fun (M →* N) := ⟨_, monoid_hom.to_fun⟩ instance {mM : monoid_with_zero M} {mN : monoid_with_zero N} : has_coe_to_fun (monoid_with_zero_hom M N) := ⟨_, monoid_with_zero_hom.to_fun⟩ -- these must come after the coe_to_fun definitions initialize_simps_projections zero_hom (to_fun → apply) initialize_simps_projections add_hom (to_fun → apply) initialize_simps_projections add_monoid_hom (to_fun → apply) initialize_simps_projections one_hom (to_fun → apply) initialize_simps_projections mul_hom (to_fun → apply) initialize_simps_projections monoid_hom (to_fun → apply) initialize_simps_projections monoid_with_zero_hom (to_fun → apply) @[simp, to_additive] lemma one_hom.to_fun_eq_coe [has_one M] [has_one N] (f : one_hom M N) : f.to_fun = f := rfl @[simp, to_additive] lemma mul_hom.to_fun_eq_coe [has_mul M] [has_mul N] (f : mul_hom M N) : f.to_fun = f := rfl @[simp, to_additive] lemma monoid_hom.to_fun_eq_coe [monoid M] [monoid N] (f : M →* N) : f.to_fun = f := rfl @[simp] lemma monoid_with_zero_hom.to_fun_eq_coe [monoid_with_zero M] [monoid_with_zero N] (f : monoid_with_zero_hom M N) : f.to_fun = f := rfl @[simp, to_additive] lemma one_hom.coe_mk [has_one M] [has_one N] (f : M → N) (h1) : ⇑(one_hom.mk f h1) = f := rfl @[simp, to_additive] lemma mul_hom.coe_mk [has_mul M] [has_mul N] (f : M → N) (hmul) : ⇑(mul_hom.mk f hmul) = f := rfl @[simp, to_additive] lemma monoid_hom.coe_mk [monoid M] [monoid N] (f : M → N) (h1 hmul) : ⇑(monoid_hom.mk f h1 hmul) = f := rfl @[simp] lemma monoid_with_zero_hom.coe_mk [monoid_with_zero M] [monoid_with_zero N] (f : M → N) (h0 h1 hmul) : ⇑(monoid_with_zero_hom.mk f h0 h1 hmul) = f := rfl @[simp, to_additive] lemma monoid_hom.to_one_hom_coe [monoid M] [monoid N] (f : M →* N) : (f.to_one_hom : M → N) = f := rfl @[simp, to_additive] lemma monoid_hom.to_mul_hom_coe [monoid M] [monoid N] (f : M →* N) : (f.to_mul_hom : M → N) = f := rfl @[simp] lemma monoid_with_zero_hom.to_zero_hom_coe [monoid_with_zero M] [monoid_with_zero N] (f : monoid_with_zero_hom M N) : (f.to_zero_hom : M → N) = f := rfl @[simp] lemma monoid_with_zero_hom.to_monoid_hom_coe [monoid_with_zero M] [monoid_with_zero N] (f : monoid_with_zero_hom M N) : (f.to_monoid_hom : M → N) = f := rfl @[to_additive] theorem one_hom.congr_fun [has_one M] [has_one N] {f g : one_hom M N} (h : f = g) (x : M) : f x = g x := congr_arg (λ h : one_hom M N, h x) h @[to_additive] theorem mul_hom.congr_fun [has_mul M] [has_mul N] {f g : mul_hom M N} (h : f = g) (x : M) : f x = g x := congr_arg (λ h : mul_hom M N, h x) h @[to_additive] theorem monoid_hom.congr_fun [monoid M] [monoid N] {f g : M →* N} (h : f = g) (x : M) : f x = g x := congr_arg (λ h : M →* N, h x) h theorem monoid_with_zero_hom.congr_fun [monoid_with_zero M] [monoid_with_zero N] {f g : monoid_with_zero_hom M N} (h : f = g) (x : M) : f x = g x := congr_arg (λ h : monoid_with_zero_hom M N, h x) h @[to_additive] theorem one_hom.congr_arg [has_one M] [has_one N] (f : one_hom M N) {x y : M} (h : x = y) : f x = f y := congr_arg (λ x : M, f x) h @[to_additive] theorem mul_hom.congr_arg [has_mul M] [has_mul N] (f : mul_hom M N) {x y : M} (h : x = y) : f x = f y := congr_arg (λ x : M, f x) h @[to_additive] theorem monoid_hom.congr_arg [monoid M] [monoid N] (f : M →* N) {x y : M} (h : x = y) : f x = f y := congr_arg (λ x : M, f x) h theorem monoid_with_zero_hom.congr_arg [monoid_with_zero M] [monoid_with_zero N] (f : monoid_with_zero_hom M N) {x y : M} (h : x = y) : f x = f y := congr_arg (λ x : M, f x) h @[to_additive] lemma one_hom.coe_inj [has_one M] [has_one N] ⦃f g : one_hom M N⦄ (h : (f : M → N) = g) : f = g := by cases f; cases g; cases h; refl @[to_additive] lemma mul_hom.coe_inj [has_mul M] [has_mul N] ⦃f g : mul_hom M N⦄ (h : (f : M → N) = g) : f = g := by cases f; cases g; cases h; refl @[to_additive] lemma monoid_hom.coe_inj [monoid M] [monoid N] ⦃f g : M →* N⦄ (h : (f : M → N) = g) : f = g := by cases f; cases g; cases h; refl lemma monoid_with_zero_hom.coe_inj [monoid_with_zero M] [monoid_with_zero N] ⦃f g : monoid_with_zero_hom M N⦄ (h : (f : M → N) = g) : f = g := by cases f; cases g; cases h; refl @[ext, to_additive] lemma one_hom.ext [has_one M] [has_one N] ⦃f g : one_hom M N⦄ (h : ∀ x, f x = g x) : f = g := one_hom.coe_inj (funext h) @[ext, to_additive] lemma mul_hom.ext [has_mul M] [has_mul N] ⦃f g : mul_hom M N⦄ (h : ∀ x, f x = g x) : f = g := mul_hom.coe_inj (funext h) @[ext, to_additive] lemma monoid_hom.ext [monoid M] [monoid N] ⦃f g : M →* N⦄ (h : ∀ x, f x = g x) : f = g := monoid_hom.coe_inj (funext h) @[ext] lemma monoid_with_zero_hom.ext [monoid_with_zero M] [monoid_with_zero N] ⦃f g : monoid_with_zero_hom M N⦄ (h : ∀ x, f x = g x) : f = g := monoid_with_zero_hom.coe_inj (funext h) attribute [ext] zero_hom.ext add_hom.ext add_monoid_hom.ext @[to_additive] lemma one_hom.ext_iff [has_one M] [has_one N] {f g : one_hom M N} : f = g ↔ ∀ x, f x = g x := ⟨λ h x, h ▸ rfl, λ h, one_hom.ext h⟩ @[to_additive] lemma mul_hom.ext_iff [has_mul M] [has_mul N] {f g : mul_hom M N} : f = g ↔ ∀ x, f x = g x := ⟨λ h x, h ▸ rfl, λ h, mul_hom.ext h⟩ @[to_additive] lemma monoid_hom.ext_iff [monoid M] [monoid N] {f g : M →* N} : f = g ↔ ∀ x, f x = g x := ⟨λ h x, h ▸ rfl, λ h, monoid_hom.ext h⟩ lemma monoid_with_zero_hom.ext_iff [monoid_with_zero M] [monoid_with_zero N] {f g : monoid_with_zero_hom M N} : f = g ↔ ∀ x, f x = g x := ⟨λ h x, h ▸ rfl, λ h, monoid_with_zero_hom.ext h⟩ end coes @[simp, to_additive] lemma one_hom.map_one [has_one M] [has_one N] (f : one_hom M N) : f 1 = 1 := f.map_one' /-- If `f` is a monoid homomorphism then `f 1 = 1`. -/ @[simp, to_additive] lemma monoid_hom.map_one [monoid M] [monoid N] (f : M →* N) : f 1 = 1 := f.map_one' @[simp] lemma monoid_with_zero_hom.map_one [monoid_with_zero M] [monoid_with_zero N] (f : monoid_with_zero_hom M N) : f 1 = 1 := f.map_one' /-- If `f` is an additive monoid homomorphism then `f 0 = 0`. -/ add_decl_doc add_monoid_hom.map_zero @[simp] lemma monoid_with_zero_hom.map_zero [monoid_with_zero M] [monoid_with_zero N] (f : monoid_with_zero_hom M N) : f 0 = 0 := f.map_zero' @[simp, to_additive] lemma mul_hom.map_mul [has_mul M] [has_mul N] (f : mul_hom M N) (a b : M) : f (a * b) = f a * f b := f.map_mul' a b /-- If `f` is a monoid homomorphism then `f (a * b) = f a * f b`. -/ @[simp, to_additive] lemma monoid_hom.map_mul [monoid M] [monoid N] (f : M →* N) (a b : M) : f (a * b) = f a * f b := f.map_mul' a b @[simp] lemma monoid_with_zero_hom.map_mul [monoid_with_zero M] [monoid_with_zero N] (f : monoid_with_zero_hom M N) (a b : M) : f (a * b) = f a * f b := f.map_mul' a b /-- If `f` is an additive monoid homomorphism then `f (a + b) = f a + f b`. -/ add_decl_doc add_monoid_hom.map_add namespace monoid_hom variables {mM : monoid M} {mN : monoid N} {mP : monoid P} variables [group G] [comm_group H] include mM mN @[to_additive] lemma map_mul_eq_one (f : M →* N) {a b : M} (h : a * b = 1) : f a * f b = 1 := by rw [← f.map_mul, h, f.map_one] /-- Given a monoid homomorphism `f : M →* N` and an element `x : M`, if `x` has a right inverse, then `f x` has a right inverse too. For elements invertible on both sides see `is_unit.map`. -/ @[to_additive "Given an add_monoid homomorphism `f : M →+ N` and an element `x : M`, if `x` has a right inverse, then `f x` has a right inverse too."] lemma map_exists_right_inv (f : M →* N) {x : M} (hx : ∃ y, x * y = 1) : ∃ y, f x * y = 1 := let ⟨y, hy⟩ := hx in ⟨f y, f.map_mul_eq_one hy⟩ /-- Given a monoid homomorphism `f : M →* N` and an element `x : M`, if `x` has a left inverse, then `f x` has a left inverse too. For elements invertible on both sides see `is_unit.map`. -/ @[to_additive "Given an add_monoid homomorphism `f : M →+ N` and an element `x : M`, if `x` has a left inverse, then `f x` has a left inverse too. For elements invertible on both sides see `is_add_unit.map`."] lemma map_exists_left_inv (f : M →* N) {x : M} (hx : ∃ y, y * x = 1) : ∃ y, y * f x = 1 := let ⟨y, hy⟩ := hx in ⟨f y, f.map_mul_eq_one hy⟩ end monoid_hom /-- The identity map from a type with 1 to itself. -/ @[to_additive] def one_hom.id (M : Type*) [has_one M] : one_hom M M := { to_fun := id, map_one' := rfl, } /-- The identity map from a type with multiplication to itself. -/ @[to_additive] def mul_hom.id (M : Type*) [has_mul M] : mul_hom M M := { to_fun := id, map_mul' := λ _ _, rfl, } /-- The identity map from a monoid to itself. -/ @[to_additive] def monoid_hom.id (M : Type*) [monoid M] : M →* M := { to_fun := id, map_one' := rfl, map_mul' := λ _ _, rfl, } /-- The identity map from a monoid_with_zero to itself. -/ def monoid_with_zero_hom.id (M : Type*) [monoid_with_zero M] : monoid_with_zero_hom M M := { to_fun := id, map_zero' := rfl, map_one' := rfl, map_mul' := λ _ _, rfl, } /-- The identity map from an type with zero to itself. -/ add_decl_doc zero_hom.id /-- The identity map from an type with addition to itself. -/ add_decl_doc add_hom.id /-- The identity map from an additive monoid to itself. -/ add_decl_doc add_monoid_hom.id @[simp, to_additive] lemma one_hom.id_apply {M : Type*} [has_one M] (x : M) : one_hom.id M x = x := rfl @[simp, to_additive] lemma mul_hom.id_apply {M : Type*} [has_mul M] (x : M) : mul_hom.id M x = x := rfl @[simp, to_additive] lemma monoid_hom.id_apply {M : Type*} [monoid M] (x : M) : monoid_hom.id M x = x := rfl @[simp] lemma monoid_with_zero_hom.id_apply {M : Type*} [monoid_with_zero M] (x : M) : monoid_with_zero_hom.id M x = x := rfl /-- Composition of `one_hom`s as a `one_hom`. -/ @[to_additive] def one_hom.comp [has_one M] [has_one N] [has_one P] (hnp : one_hom N P) (hmn : one_hom M N) : one_hom M P := { to_fun := hnp ∘ hmn, map_one' := by simp, } /-- Composition of `mul_hom`s as a `mul_hom`. -/ @[to_additive] def mul_hom.comp [has_mul M] [has_mul N] [has_mul P] (hnp : mul_hom N P) (hmn : mul_hom M N) : mul_hom M P := { to_fun := hnp ∘ hmn, map_mul' := by simp, } /-- Composition of monoid morphisms as a monoid morphism. -/ @[to_additive] def monoid_hom.comp [monoid M] [monoid N] [monoid P] (hnp : N →* P) (hmn : M →* N) : M →* P := { to_fun := hnp ∘ hmn, map_one' := by simp, map_mul' := by simp, } /-- Composition of `monoid_with_zero_hom`s as a `monoid_with_zero_hom`. -/ def monoid_with_zero_hom.comp [monoid_with_zero M] [monoid_with_zero N] [monoid_with_zero P] (hnp : monoid_with_zero_hom N P) (hmn : monoid_with_zero_hom M N) : monoid_with_zero_hom M P := { to_fun := hnp ∘ hmn, map_zero' := by simp, map_one' := by simp, map_mul' := by simp, } /-- Composition of `zero_hom`s as a `zero_hom`. -/ add_decl_doc zero_hom.comp /-- Composition of `add_hom`s as a `add_hom`. -/ add_decl_doc add_hom.comp /-- Composition of additive monoid morphisms as an additive monoid morphism. -/ add_decl_doc add_monoid_hom.comp @[simp, to_additive] lemma one_hom.coe_comp [has_one M] [has_one N] [has_one P] (g : one_hom N P) (f : one_hom M N) : ⇑(g.comp f) = g ∘ f := rfl @[simp, to_additive] lemma mul_hom.coe_comp [has_mul M] [has_mul N] [has_mul P] (g : mul_hom N P) (f : mul_hom M N) : ⇑(g.comp f) = g ∘ f := rfl @[simp, to_additive] lemma monoid_hom.coe_comp [monoid M] [monoid N] [monoid P] (g : N →* P) (f : M →* N) : ⇑(g.comp f) = g ∘ f := rfl @[simp] lemma monoid_with_zero_hom.coe_comp [monoid_with_zero M] [monoid_with_zero N] [monoid_with_zero P] (g : monoid_with_zero_hom N P) (f : monoid_with_zero_hom M N) : ⇑(g.comp f) = g ∘ f := rfl @[to_additive] lemma one_hom.comp_apply [has_one M] [has_one N] [has_one P] (g : one_hom N P) (f : one_hom M N) (x : M) : g.comp f x = g (f x) := rfl @[to_additive] lemma mul_hom.comp_apply [has_mul M] [has_mul N] [has_mul P] (g : mul_hom N P) (f : mul_hom M N) (x : M) : g.comp f x = g (f x) := rfl @[to_additive] lemma monoid_hom.comp_apply [monoid M] [monoid N] [monoid P] (g : N →* P) (f : M →* N) (x : M) : g.comp f x = g (f x) := rfl lemma monoid_with_zero_hom.comp_apply [monoid_with_zero M] [monoid_with_zero N] [monoid_with_zero P] (g : monoid_with_zero_hom N P) (f : monoid_with_zero_hom M N) (x : M) : g.comp f x = g (f x) := rfl /-- Composition of monoid homomorphisms is associative. -/ @[to_additive] lemma one_hom.comp_assoc {Q : Type*} [has_one M] [has_one N] [has_one P] [has_one Q] (f : one_hom M N) (g : one_hom N P) (h : one_hom P Q) : (h.comp g).comp f = h.comp (g.comp f) := rfl @[to_additive] lemma mul_hom.comp_assoc {Q : Type*} [has_mul M] [has_mul N] [has_mul P] [has_mul Q] (f : mul_hom M N) (g : mul_hom N P) (h : mul_hom P Q) : (h.comp g).comp f = h.comp (g.comp f) := rfl @[to_additive] lemma monoid_hom.comp_assoc {Q : Type*} [monoid M] [monoid N] [monoid P] [monoid Q] (f : M →* N) (g : N →* P) (h : P →* Q) : (h.comp g).comp f = h.comp (g.comp f) := rfl lemma monoid_with_zero_hom.comp_assoc {Q : Type*} [monoid_with_zero M] [monoid_with_zero N] [monoid_with_zero P] [monoid_with_zero Q] (f : monoid_with_zero_hom M N) (g : monoid_with_zero_hom N P) (h : monoid_with_zero_hom P Q) : (h.comp g).comp f = h.comp (g.comp f) := rfl @[to_additive] lemma one_hom.cancel_right [has_one M] [has_one N] [has_one P] {g₁ g₂ : one_hom N P} {f : one_hom M N} (hf : function.surjective f) : g₁.comp f = g₂.comp f ↔ g₁ = g₂ := ⟨λ h, one_hom.ext $ (forall_iff_forall_surj hf).1 (one_hom.ext_iff.1 h), λ h, h ▸ rfl⟩ @[to_additive] lemma mul_hom.cancel_right [has_mul M] [has_mul N] [has_mul P] {g₁ g₂ : mul_hom N P} {f : mul_hom M N} (hf : function.surjective f) : g₁.comp f = g₂.comp f ↔ g₁ = g₂ := ⟨λ h, mul_hom.ext $ (forall_iff_forall_surj hf).1 (mul_hom.ext_iff.1 h), λ h, h ▸ rfl⟩ @[to_additive] lemma monoid_hom.cancel_right [monoid M] [monoid N] [monoid P] {g₁ g₂ : N →* P} {f : M →* N} (hf : function.surjective f) : g₁.comp f = g₂.comp f ↔ g₁ = g₂ := ⟨λ h, monoid_hom.ext $ (forall_iff_forall_surj hf).1 (monoid_hom.ext_iff.1 h), λ h, h ▸ rfl⟩ lemma monoid_with_zero_hom.cancel_right [monoid_with_zero M] [monoid_with_zero N] [monoid_with_zero P] {g₁ g₂ : monoid_with_zero_hom N P} {f : monoid_with_zero_hom M N} (hf : function.surjective f) : g₁.comp f = g₂.comp f ↔ g₁ = g₂ := ⟨λ h, monoid_with_zero_hom.ext $ (forall_iff_forall_surj hf).1 (monoid_with_zero_hom.ext_iff.1 h), λ h, h ▸ rfl⟩ @[to_additive] lemma one_hom.cancel_left [has_one M] [has_one N] [has_one P] {g : one_hom N P} {f₁ f₂ : one_hom M N} (hg : function.injective g) : g.comp f₁ = g.comp f₂ ↔ f₁ = f₂ := ⟨λ h, one_hom.ext $ λ x, hg $ by rw [← one_hom.comp_apply, h, one_hom.comp_apply], λ h, h ▸ rfl⟩ @[to_additive] lemma mul_hom.cancel_left [has_one M] [has_one N] [has_one P] {g : one_hom N P} {f₁ f₂ : one_hom M N} (hg : function.injective g) : g.comp f₁ = g.comp f₂ ↔ f₁ = f₂ := ⟨λ h, one_hom.ext $ λ x, hg $ by rw [← one_hom.comp_apply, h, one_hom.comp_apply], λ h, h ▸ rfl⟩ @[to_additive] lemma monoid_hom.cancel_left [monoid M] [monoid N] [monoid P] {g : N →* P} {f₁ f₂ : M →* N} (hg : function.injective g) : g.comp f₁ = g.comp f₂ ↔ f₁ = f₂ := ⟨λ h, monoid_hom.ext $ λ x, hg $ by rw [← monoid_hom.comp_apply, h, monoid_hom.comp_apply], λ h, h ▸ rfl⟩ lemma monoid_with_zero_hom.cancel_left [monoid_with_zero M] [monoid_with_zero N] [monoid_with_zero P] {g : monoid_with_zero_hom N P} {f₁ f₂ : monoid_with_zero_hom M N} (hg : function.injective g) : g.comp f₁ = g.comp f₂ ↔ f₁ = f₂ := ⟨λ h, monoid_with_zero_hom.ext $ λ x, hg $ by rw [ ← monoid_with_zero_hom.comp_apply, h, monoid_with_zero_hom.comp_apply], λ h, h ▸ rfl⟩ @[simp, to_additive] lemma one_hom.comp_id [has_one M] [has_one N] (f : one_hom M N) : f.comp (one_hom.id M) = f := one_hom.ext $ λ x, rfl @[simp, to_additive] lemma mul_hom.comp_id [has_mul M] [has_mul N] (f : mul_hom M N) : f.comp (mul_hom.id M) = f := mul_hom.ext $ λ x, rfl @[simp, to_additive] lemma monoid_hom.comp_id [monoid M] [monoid N] (f : M →* N) : f.comp (monoid_hom.id M) = f := monoid_hom.ext $ λ x, rfl @[simp] lemma monoid_with_zero_hom.comp_id [monoid_with_zero M] [monoid_with_zero N] (f : monoid_with_zero_hom M N) : f.comp (monoid_with_zero_hom.id M) = f := monoid_with_zero_hom.ext $ λ x, rfl @[simp, to_additive] lemma one_hom.id_comp [has_one M] [has_one N] (f : one_hom M N) : (one_hom.id N).comp f = f := one_hom.ext $ λ x, rfl @[simp, to_additive] lemma mul_hom.id_comp [has_mul M] [has_mul N] (f : mul_hom M N) : (mul_hom.id N).comp f = f := mul_hom.ext $ λ x, rfl @[simp, to_additive] lemma monoid_hom.id_comp [monoid M] [monoid N] (f : M →* N) : (monoid_hom.id N).comp f = f := monoid_hom.ext $ λ x, rfl @[simp] lemma monoid_with_zero_hom.id_comp [monoid_with_zero M] [monoid_with_zero N] (f : monoid_with_zero_hom M N) : (monoid_with_zero_hom.id N).comp f = f := monoid_with_zero_hom.ext $ λ x, rfl section End namespace monoid variables (M) [monoid M] /-- The monoid of endomorphisms. -/ protected def End := M →* M namespace End instance : monoid (monoid.End M) := { mul := monoid_hom.comp, one := monoid_hom.id M, mul_assoc := λ _ _ _, monoid_hom.comp_assoc _ _ _, mul_one := monoid_hom.comp_id, one_mul := monoid_hom.id_comp } instance : inhabited (monoid.End M) := ⟨1⟩ instance : has_coe_to_fun (monoid.End M) := ⟨_, monoid_hom.to_fun⟩ end End @[simp] lemma coe_one : ((1 : monoid.End M) : M → M) = id := rfl @[simp] lemma coe_mul (f g) : ((f * g : monoid.End M) : M → M) = f ∘ g := rfl end monoid namespace add_monoid variables (A : Type*) [add_monoid A] /-- The monoid of endomorphisms. -/ protected def End := A →+ A namespace End instance : monoid (add_monoid.End A) := { mul := add_monoid_hom.comp, one := add_monoid_hom.id A, mul_assoc := λ _ _ _, add_monoid_hom.comp_assoc _ _ _, mul_one := add_monoid_hom.comp_id, one_mul := add_monoid_hom.id_comp } instance : inhabited (add_monoid.End A) := ⟨1⟩ instance : has_coe_to_fun (add_monoid.End A) := ⟨_, add_monoid_hom.to_fun⟩ end End @[simp] lemma coe_one : ((1 : add_monoid.End A) : A → A) = id := rfl @[simp] lemma coe_mul (f g) : ((f * g : add_monoid.End A) : A → A) = f ∘ g := rfl end add_monoid end End /-- `1` is the homomorphism sending all elements to `1`. -/ @[to_additive] instance [has_one M] [has_one N] : has_one (one_hom M N) := ⟨⟨λ _, 1, rfl⟩⟩ /-- `1` is the multiplicative homomorphism sending all elements to `1`. -/ @[to_additive] instance [has_mul M] [monoid N] : has_one (mul_hom M N) := ⟨⟨λ _, 1, λ _ _, (one_mul 1).symm⟩⟩ /-- `1` is the monoid homomorphism sending all elements to `1`. -/ @[to_additive] instance [monoid M] [monoid N] : has_one (M →* N) := ⟨⟨λ _, 1, rfl, λ _ _, (one_mul 1).symm⟩⟩ /-- `0` is the homomorphism sending all elements to `0`. -/ add_decl_doc zero_hom.has_zero /-- `0` is the additive homomorphism sending all elements to `0`. -/ add_decl_doc add_hom.has_zero /-- `0` is the additive monoid homomorphism sending all elements to `0`. -/ add_decl_doc add_monoid_hom.has_zero @[simp, to_additive] lemma one_hom.one_apply [has_one M] [has_one N] (x : M) : (1 : one_hom M N) x = 1 := rfl @[simp, to_additive] lemma monoid_hom.one_apply [monoid M] [monoid N] (x : M) : (1 : M →* N) x = 1 := rfl @[simp, to_additive] lemma one_hom.one_comp [has_one M] [has_one N] [has_one P] (f : one_hom M N) : (1 : one_hom N P).comp f = 1 := rfl @[simp, to_additive] lemma one_hom.comp_one [has_one M] [has_one N] [has_one P] (f : one_hom N P) : f.comp (1 : one_hom M N) = 1 := by { ext, simp only [one_hom.map_one, one_hom.coe_comp, function.comp_app, one_hom.one_apply] } @[to_additive] instance [has_one M] [has_one N] : inhabited (one_hom M N) := ⟨1⟩ @[to_additive] instance [has_mul M] [monoid N] : inhabited (mul_hom M N) := ⟨1⟩ @[to_additive] instance [monoid M] [monoid N] : inhabited (M →* N) := ⟨1⟩ -- unlike the other homs, `monoid_with_zero_hom` does not have a `1` or `0` instance [monoid_with_zero M] : inhabited (monoid_with_zero_hom M M) := ⟨monoid_with_zero_hom.id M⟩ namespace monoid_hom variables [mM : monoid M] [mN : monoid N] [mP : monoid P] variables [group G] [comm_group H] /-- Given two monoid morphisms `f`, `g` to a commutative monoid, `f * g` is the monoid morphism sending `x` to `f x * g x`. -/ @[to_additive] instance {M N} {mM : monoid M} [comm_monoid N] : has_mul (M →* N) := ⟨λ f g, { to_fun := λ m, f m * g m, map_one' := show f 1 * g 1 = 1, by simp, map_mul' := begin intros, show f (x * y) * g (x * y) = f x * g x * (f y * g y), rw [f.map_mul, g.map_mul, ←mul_assoc, ←mul_assoc, mul_right_comm (f x)], end }⟩ /-- Given two additive monoid morphisms `f`, `g` to an additive commutative monoid, `f + g` is the additive monoid morphism sending `x` to `f x + g x`. -/ add_decl_doc add_monoid_hom.has_add @[simp, to_additive] lemma mul_apply {M N} {mM : monoid M} {mN : comm_monoid N} (f g : M →* N) (x : M) : (f * g) x = f x * g x := rfl @[simp, to_additive] lemma one_comp [monoid M] [monoid N] [monoid P] (f : M →* N) : (1 : N →* P).comp f = 1 := rfl @[simp, to_additive] lemma comp_one [monoid M] [monoid N] [monoid P] (f : N →* P) : f.comp (1 : M →* N) = 1 := by { ext, simp only [map_one, coe_comp, function.comp_app, one_apply] } @[to_additive] lemma mul_comp [monoid M] [comm_monoid N] [comm_monoid P] (g₁ g₂ : N →* P) (f : M →* N) : (g₁ * g₂).comp f = (g₁.comp f) * (g₂.comp f) := rfl @[to_additive] lemma comp_mul [monoid M] [comm_monoid N] [comm_monoid P] (g : N →* P) (f₁ f₂ : M →* N) : g.comp (f₁ * f₂) = (g.comp f₁) * (g.comp f₂) := by { ext, simp only [mul_apply, function.comp_app, map_mul, coe_comp] } /-- (M →* N) is a comm_monoid if N is commutative. -/ @[to_additive] instance {M N} [monoid M] [comm_monoid N] : comm_monoid (M →* N) := { mul := (*), mul_assoc := by intros; ext; apply mul_assoc, one := 1, one_mul := by intros; ext; apply one_mul, mul_one := by intros; ext; apply mul_one, mul_comm := by intros; ext; apply mul_comm } /-- `flip` arguments of `f : M →* N →* P` -/ @[to_additive "`flip` arguments of `f : M →+ N →+ P`"] def flip {mM : monoid M} {mN : monoid N} {mP : comm_monoid P} (f : M →* N →* P) : N →* M →* P := { to_fun := λ y, ⟨λ x, f x y, by rw [f.map_one, one_apply], λ x₁ x₂, by rw [f.map_mul, mul_apply]⟩, map_one' := ext $ λ x, (f x).map_one, map_mul' := λ y₁ y₂, ext $ λ x, (f x).map_mul y₁ y₂ } @[simp, to_additive] lemma flip_apply {mM : monoid M} {mN : monoid N} {mP : comm_monoid P} (f : M →* N →* P) (x : M) (y : N) : f.flip y x = f x y := rfl /-- Evaluation of a `monoid_hom` at a point as a monoid homomorphism. See also `monoid_hom.apply` for the evaluation of any function at a point. -/ @[to_additive "Evaluation of an `add_monoid_hom` at a point as an additive monoid homomorphism. See also `add_monoid_hom.apply` for the evaluation of any function at a point."] def eval [monoid M] [comm_monoid N] : M →* (M →* N) →* N := (monoid_hom.id (M →* N)).flip @[simp, to_additive] lemma eval_apply [monoid M] [comm_monoid N] (x : M) (f : M →* N) : eval x f = f x := rfl /-- Composition of monoid morphisms (`monoid_hom.comp`) as a monoid morphism. -/ @[simps, to_additive "Composition of additive monoid morphisms (`add_monoid_hom.comp`) as an additive monoid morphism."] def comp_hom [monoid M] [comm_monoid N] [comm_monoid P] : (N →* P) →* (M →* N) →* (M →* P) := { to_fun := λ g, { to_fun := g.comp, map_one' := comp_one g, map_mul' := comp_mul g }, map_one' := by { ext1 f, exact one_comp f }, map_mul' := λ g₁ g₂, by { ext1 f, exact mul_comp g₁ g₂ f } } /-- If two homomorphism from a group to a monoid are equal at `x`, then they are equal at `x⁻¹`. -/ @[to_additive "If two homomorphism from an additive group to an additive monoid are equal at `x`, then they are equal at `-x`." ] lemma eq_on_inv {G} [group G] [monoid M] {f g : G →* M} {x : G} (h : f x = g x) : f x⁻¹ = g x⁻¹ := left_inv_eq_right_inv (f.map_mul_eq_one $ inv_mul_self x) $ h.symm ▸ g.map_mul_eq_one $ mul_inv_self x /-- Group homomorphisms preserve inverse. -/ @[simp, to_additive] theorem map_inv {G H} [group G] [group H] (f : G →* H) (g : G) : f g⁻¹ = (f g)⁻¹ := eq_inv_of_mul_eq_one $ f.map_mul_eq_one $ inv_mul_self g /-- Group homomorphisms preserve division. -/ @[simp, to_additive] theorem map_mul_inv {G H} [group G] [group H] (f : G →* H) (g h : G) : f (g * h⁻¹) = (f g) * (f h)⁻¹ := by rw [f.map_mul, f.map_inv] /-- A homomorphism from a group to a monoid is injective iff its kernel is trivial. -/ @[to_additive] lemma injective_iff {G H} [group G] [monoid H] (f : G →* H) : function.injective f ↔ (∀ a, f a = 1 → a = 1) := ⟨λ h x hfx, h $ hfx.trans f.map_one.symm, λ h x y hxy, mul_inv_eq_one.1 $ h _ $ by rw [f.map_mul, hxy, ← f.map_mul, mul_inv_self, f.map_one]⟩ include mM /-- Makes a group homomomorphism from a proof that the map preserves multiplication. -/ @[to_additive] def mk' (f : M → G) (map_mul : ∀ a b : M, f (a * b) = f a * f b) : M →* G := { to_fun := f, map_mul' := map_mul, map_one' := mul_self_iff_eq_one.1 $ by rw [←map_mul, mul_one] } /-- Makes an additive group homomomorphism from a proof that the map preserves multiplication. -/ add_decl_doc add_monoid_hom.mk' @[simp, to_additive] lemma coe_mk' {f : M → G} (map_mul : ∀ a b : M, f (a * b) = f a * f b) : ⇑(mk' f map_mul) = f := rfl omit mM /-- Makes a group homomorphism from a proof that the map preserves right division `λ x y, x * y⁻¹`. -/ @[to_additive "Makes an additive group homomorphism from a proof that the map preserves the operation `λ a b, a + -b`. See also `add_monoid_hom.of_map_sub` for a version using `λ a b, a - b`."] def of_map_mul_inv {H : Type*} [group H] (f : G → H) (map_div : ∀ a b : G, f (a * b⁻¹) = f a * (f b)⁻¹) : G →* H := mk' f $ λ x y, calc f (x * y) = f x * (f $ 1 * 1⁻¹ * y⁻¹)⁻¹ : by simp only [one_mul, one_inv, ← map_div, inv_inv] ... = f x * f y : by { simp only [map_div], simp only [mul_right_inv, one_mul, inv_inv] } @[simp, to_additive] lemma coe_of_map_mul_inv {H : Type*} [group H] (f : G → H) (map_div : ∀ a b : G, f (a * b⁻¹) = f a * (f b)⁻¹) : ⇑(of_map_mul_inv f map_div) = f := rfl /-- If `f` is a monoid homomorphism to a commutative group, then `f⁻¹` is the homomorphism sending `x` to `(f x)⁻¹`. -/ @[to_additive] instance {M G} [monoid M] [comm_group G] : has_inv (M →* G) := ⟨λ f, mk' (λ g, (f g)⁻¹) $ λ a b, by rw [←mul_inv, f.map_mul]⟩ /-- If `f` is an additive monoid homomorphism to an additive commutative group, then `-f` is the homomorphism sending `x` to `-(f x)`. -/ add_decl_doc add_monoid_hom.has_neg @[simp, to_additive] lemma inv_apply {M G} {mM : monoid M} {gG : comm_group G} (f : M →* G) (x : M) : f⁻¹ x = (f x)⁻¹ := rfl /-- If `G` is a commutative group, then `M →* G` a commutative group too. -/ @[to_additive] instance {M G} [monoid M] [comm_group G] : comm_group (M →* G) := { inv := has_inv.inv, mul_left_inv := by intros; ext; apply mul_left_inv, ..monoid_hom.comm_monoid } /-- If `G` is an additive commutative group, then `M →+ G` an additive commutative group too. -/ add_decl_doc add_monoid_hom.add_comm_group end monoid_hom namespace add_monoid_hom variables {A B : Type*} [add_monoid A] [add_comm_group B] [add_group G] [add_group H] /-- Additive group homomorphisms preserve subtraction. -/ @[simp] theorem map_sub (f : G →+ H) (g h : G) : f (g - h) = (f g) - (f h) := by rw [sub_eq_add_neg, sub_eq_add_neg, f.map_add_neg g h] /-- Define a morphism of additive groups given a map which respects difference. -/ def of_map_sub (f : G → H) (hf : ∀ x y, f (x - y) = f x - f y) : G →+ H := of_map_add_neg f (by simpa only [sub_eq_add_neg] using hf) @[simp] lemma coe_of_map_sub (f : G → H) (hf : ∀ x y, f (x - y) = f x - f y) : ⇑(of_map_sub f hf) = f := rfl @[simp] lemma sub_apply (f g : A →+ B) (a : A) : (f - g) a = f a - g a := rfl end add_monoid_hom section commute variables [monoid M] [monoid N] {a x y : M} @[simp, to_additive] protected lemma semiconj_by.map (h : semiconj_by a x y) (f : M →* N) : semiconj_by (f a) (f x) (f y) := by simpa only [semiconj_by, f.map_mul] using congr_arg f h @[simp, to_additive] protected lemma commute.map (h : commute x y) (f : M →* N) : commute (f x) (f y) := h.map f end commute
0a2b126d956eadb8e395b643b92c9f10256e43f8
bf532e3e865883a676110e756f800e0ddeb465be
/order/filter.lean
89f1997078ad712832bc21992bac61482b7b25ec
[ "Apache-2.0" ]
permissive
aqjune/mathlib
da42a97d9e6670d2efaa7d2aa53ed3585dafc289
f7977ff5a6bcf7e5c54eec908364ceb40dafc795
refs/heads/master
1,631,213,225,595
1,521,089,840,000
1,521,089,840,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
70,603
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl Theory of filters on sets. -/ import order.complete_lattice order.galois_connection data.set data.finset order.zorn open lattice set universes u v w x y open set classical local attribute [instance] prop_decidable namespace lattice variables {α : Type u} {ι : Sort v} section variable [complete_lattice α] lemma Inf_eq_finite_sets {s : set α} : Inf s = (⨅ t ∈ { t | finite t ∧ t ⊆ s}, Inf t) := le_antisymm (le_infi $ assume t, le_infi $ assume ⟨_, h⟩, Inf_le_Inf h) (le_Inf $ assume b h, infi_le_of_le {b} $ infi_le_of_le (by simp [h]) $ Inf_le $ by simp) lemma infi_insert_finset {ι : Type v} {s : finset ι} {f : ι → α} {i : ι} : (⨅j∈insert i s, f j) = f i ⊓ (⨅j∈s, f j) := by simp [infi_or, infi_inf_eq] lemma infi_empty_finset {ι : Type v} {f : ι → α} : (⨅j∈(∅ : finset ι), f j) = ⊤ := by simp end lemma inf_left_comm [semilattice_inf α] (a b c : α) : a ⊓ (b ⊓ c) = b ⊓ (a ⊓ c) := by rw [← inf_assoc, ← inf_assoc, @inf_comm α _ a] end lattice namespace set variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type x} {ι : Sort y} theorem monotone_inter [preorder β] {f g : β → set α} (hf : monotone f) (hg : monotone g) : monotone (λx, (f x) ∩ (g x)) := assume a b h x ⟨h₁, h₂⟩, ⟨hf h h₁, hg h h₂⟩ theorem monotone_set_of [preorder α] {p : α → β → Prop} (hp : ∀b, monotone (λa, p a b)) : monotone (λa, {b | p a b}) := assume a a' h b, hp b h end set section order variables {α : Type u} (r : α → α → Prop) local infix `≼` : 50 := r /-- A family of elements of α is directed (with respect to a relation `≼` on α) if there is a member of the family `≼`-above any pair in the family. -/ def directed {ι : Sort v} (f : ι → α) := ∀x y, ∃z, f z ≼ f x ∧ f z ≼ f y /-- A subset of α is directed if there is an element of the set `≼`-above any pair of elements in the set. -/ def directed_on (s : set α) := ∀ (x ∈ s) (y ∈ s), ∃z ∈ s, z ≼ x ∧ z ≼ y lemma directed_on_Union {r} {ι : Sort v} {f : ι → set α} (hd : directed (⊇) f) (h : ∀x, directed_on r (f x)) : directed_on r (⋃x, f x) := by simp [directed_on]; exact assume a₁ b₁ fb₁ a₂ b₂ fb₂, let ⟨z, zb₁, zb₂⟩ := hd b₁ b₂, ⟨x, xf, xa₁, xa₂⟩ := h z a₁ (zb₁ fb₁) a₂ (zb₂ fb₂) in ⟨x, ⟨z, xf⟩, xa₁, xa₂⟩ end order theorem directed_of_chain {α : Type u} {β : Type v} [preorder β] {f : α → β} {c : set α} (h : zorn.chain (λa b, f b ≤ f a) c) : directed (≤) (λx:{a:α // a ∈ c}, f (x.val)) := assume ⟨a, ha⟩ ⟨b, hb⟩, classical.by_cases (assume : a = b, by simp [this]; exact ⟨b, hb, le_refl _⟩) (assume : a ≠ b, have f b ≤ f a ∨ f a ≤ f b, from h a ha b hb this, or.elim this (assume : f b ≤ f a, ⟨⟨b, hb⟩, this, le_refl _⟩) (assume : f a ≤ f b, ⟨⟨a, ha⟩, le_refl _, this⟩)) structure filter (α : Type u) := (sets : set (set α)) (exists_mem_sets : ∃x, x ∈ sets) (upwards_sets : ∀{x y}, x ∈ sets → x ⊆ y → y ∈ sets) (directed_sets : directed_on (⊆) sets) namespace filter variables {α : Type u} {f g : filter α} {s t : set α} lemma filter_eq : ∀{f g : filter α}, f.sets = g.sets → f = g | ⟨a, _, _, _⟩ ⟨._, _, _, _⟩ rfl := rfl lemma filter_eq_iff : f = g ↔ f.sets = g.sets := ⟨congr_arg _, filter_eq⟩ lemma filter.ext : f = g ↔ ∀ s, s ∈ f.sets ↔ s ∈ g.sets := by rw [filter_eq_iff, set_eq_def] lemma univ_mem_sets' (h : ∀ a, a ∈ s): s ∈ f.sets := let ⟨x, x_in_s⟩ := f.exists_mem_sets in f.upwards_sets x_in_s (assume x _, h x) lemma univ_mem_sets : univ ∈ f.sets := univ_mem_sets' mem_univ lemma inter_mem_sets (hs : s ∈ f.sets) (ht : t ∈ f.sets) : s ∩ t ∈ f.sets := let ⟨z, ⟨z_in_s, z_le_x, z_le_y⟩⟩ := f.directed_sets _ hs _ ht in f.upwards_sets z_in_s (subset_inter z_le_x z_le_y) lemma mp_sets (hs : s ∈ f.sets) (h : {x | x ∈ s → x ∈ t} ∈ f.sets) : t ∈ f.sets := f.upwards_sets (inter_mem_sets hs h) $ assume x ⟨h₁, h₂⟩, h₂ h₁ lemma Inter_mem_sets {β : Type v} {s : β → set α} {is : set β} (hf : finite is) : (∀i∈is, s i ∈ f.sets) → (⋂i∈is, s i) ∈ f.sets := finite.induction_on hf (assume hs, by simp [univ_mem_sets]) (assume i is _ hf hi hs, have h₁ : s i ∈ f.sets, from hs i (by simp), have h₂ : (⋂x∈is, s x) ∈ f.sets, from hi $ assume a ha, hs _ $ by simp [ha], by simp [inter_mem_sets h₁ h₂]) lemma exists_sets_subset_iff : (∃t∈f.sets, t ⊆ s) ↔ s ∈ f.sets := ⟨assume ⟨t, ht, ts⟩, f.upwards_sets ht ts, assume hs, ⟨s, hs, subset.refl _⟩⟩ lemma monotone_mem_sets {f : filter α} : monotone (λs, s ∈ f.sets) := assume s t hst h, f.upwards_sets h hst end filter namespace tactic.interactive open tactic interactive /-- `filter [t1, ⋯, tn]` replaces a goal of the form `s ∈ f.sets` and terms `h1 : t1 ∈ f.sets, ⋯, tn ∈ f.sets` with `∀x, x ∈ t1 → ⋯ → x ∈ tn → x ∈ s`. `filter [t1, ⋯, tn] e` is a short form for `{ filter [t1, ⋯, tn], exact e }`. -/ meta def filter_upwards (s : parse types.pexpr_list) (e' : parse $ optional types.texpr) : tactic unit := do s.reverse.mmap (λ e, eapplyc `filter.mp_sets >> eapply e), eapplyc `filter.univ_mem_sets', match e' with | some e := interactive.exact e | none := skip end end tactic.interactive namespace filter variables {α : Type u} {β : Type v} {γ : Type w} {ι : Sort x} section principal /-- The principal filter of `s` is the collection of all supersets of `s`. -/ def principal (s : set α) : filter α := { sets := {t | s ⊆ t}, exists_mem_sets := ⟨s, subset.refl _⟩, upwards_sets := assume x y hx hy, subset.trans hx hy, directed_sets := assume x hx y hy, ⟨s, subset.refl _, hx, hy⟩ } instance : inhabited (filter α) := ⟨principal ∅⟩ @[simp] lemma mem_principal_sets {s t : set α} : s ∈ (principal t).sets ↔ t ⊆ s := iff.rfl lemma mem_principal_self (s : set α) : s ∈ (principal s).sets := subset.refl _ end principal section join /-- The join of a filter of filters is defined by the relation `s ∈ join f ↔ {t | s ∈ t} ∈ f`. -/ def join (f : filter (filter α)) : filter α := { sets := {s | {t : filter α | s ∈ t.sets} ∈ f.sets}, exists_mem_sets := ⟨univ, by simp [univ_mem_sets]; exact univ_mem_sets⟩, upwards_sets := assume x y hx xy, f.upwards_sets hx $ assume a h, a.upwards_sets h xy, directed_sets := assume x hx y hy, ⟨x ∩ y, by filter_upwards [hx, hy] assume z h₁ h₂, inter_mem_sets h₁ h₂, inter_subset_left _ _, inter_subset_right _ _⟩ } @[simp] lemma mem_join_sets {s : set α} {f : filter (filter α)} : s ∈ (join f).sets ↔ {t | s ∈ filter.sets t} ∈ f.sets := iff.rfl end join section lattice instance : partial_order (filter α) := { le := λf g, g.sets ⊆ f.sets, le_antisymm := assume a b h₁ h₂, filter_eq $ subset.antisymm h₂ h₁, le_refl := assume a, subset.refl _, le_trans := assume a b c h₁ h₂, subset.trans h₂ h₁ } theorem le_def {f g : filter α} : f ≤ g ↔ ∀ x ∈ g.sets, x ∈ f.sets := iff.rfl @[simp] lemma le_principal_iff {s : set α} {f : filter α} : f ≤ principal s ↔ s ∈ f.sets := show (∀{t}, s ⊆ t → t ∈ f.sets) ↔ s ∈ f.sets, from ⟨assume h, h (subset.refl s), assume hs t ht, f.upwards_sets hs ht⟩ lemma principal_mono {s t : set α} : principal s ≤ principal t ↔ s ⊆ t := by simp lemma monotone_principal : monotone (principal : set α → filter α) := by simp [monotone, principal_mono]; exact assume a b h, h @[simp] lemma principal_eq_iff_eq {s t : set α} : principal s = principal t ↔ s = t := by simp [le_antisymm_iff]; refl /-- The supremum of filters is the intersection because the ordering of filters is reverse subset. -/ instance : has_sup (filter α) := ⟨λf g : filter α, { sets := f.sets ∩ g.sets, exists_mem_sets := ⟨univ, by simp [univ_mem_sets]; exact univ_mem_sets⟩, upwards_sets := assume x y hx xy, and.imp (assume h, f.upwards_sets h xy) (assume h, g.upwards_sets h xy) hx, directed_sets := assume x ⟨hx₁, hx₂⟩ y ⟨hy₁, hy₂⟩, ⟨x ∩ y, ⟨inter_mem_sets hx₁ hy₁, inter_mem_sets hx₂ hy₂⟩, inter_subset_left _ _, inter_subset_right _ _⟩ }⟩ @[simp] lemma mem_sup_sets {f g : filter α} {s : set α} : s ∈ (f ⊔ g).sets ↔ s ∈ f.sets ∧ s ∈ g.sets := iff.rfl /-- The infimum of filters is the filter generated by intersections of elements of the two filters. -/ instance : has_inf (filter α) := ⟨λf g : filter α, { sets := {s | ∃ (a ∈ f.sets) (b ∈ g.sets), a ∩ b ⊆ s }, exists_mem_sets := ⟨univ, univ, univ_mem_sets, univ, univ_mem_sets, subset_univ _⟩, upwards_sets := assume x y ⟨a, ha, b, hb, h⟩ xy, ⟨a, ha, b, hb, subset.trans h xy⟩, directed_sets := assume x ⟨a₁, ha₁, b₁, hb₁, h₁⟩ y ⟨a₂, ha₂, b₂, hb₂, h₂⟩, ⟨x ∩ y, ⟨_, inter_mem_sets ha₁ ha₂, _, inter_mem_sets hb₁ hb₂, calc (a₁ ⊓ a₂) ⊓ (b₁ ⊓ b₂) = (a₁ ⊓ b₁) ⊓ (a₂ ⊓ b₂) : by ac_refl ... ≤ x ∩ y : inf_le_inf h₁ h₂ ⟩, inter_subset_left _ _, inter_subset_right _ _⟩ }⟩ @[simp] lemma mem_inf_sets {f g : filter α} {s : set α} : s ∈ (f ⊓ g).sets ↔ ∃t₁∈f.sets, ∃t₂∈g.sets, t₁ ∩ t₂ ⊆ s := iff.rfl lemma mem_inf_sets_of_left {f g : filter α} {s : set α} (h : s ∈ f.sets) : s ∈ (f ⊓ g).sets := ⟨s, h, univ, univ_mem_sets, inter_subset_left _ _⟩ lemma mem_inf_sets_of_right {f g : filter α} {s : set α} (h : s ∈ g.sets) : s ∈ (f ⊓ g).sets := ⟨univ, univ_mem_sets, s, h, inter_subset_right _ _⟩ lemma inter_mem_inf_sets {α : Type u} {f g : filter α} {s t : set α} (hs : s ∈ f.sets) (ht : t ∈ g.sets) : s ∩ t ∈ (f ⊓ g).sets := inter_mem_sets (mem_inf_sets_of_left hs) (mem_inf_sets_of_right ht) instance : has_top (filter α) := ⟨principal univ⟩ @[simp] lemma mem_top_sets_iff {s : set α} : s ∈ (⊤ : filter α).sets ↔ s = univ := ⟨assume h, top_unique $ h, assume h, h.symm ▸ univ_mem_sets⟩ instance : has_bot (filter α) := ⟨principal ∅⟩ @[simp] lemma mem_bot_sets {s : set α} : s ∈ (⊥ : filter α).sets := assume x, false.elim instance : has_Sup (filter α) := ⟨join ∘ principal⟩ protected lemma le_Sup {s : set (filter α)} {f : filter α} : f ∈ s → f ≤ Sup s := assume f_in_s t' h, h f_in_s protected lemma Sup_le {s : set (filter α)} {f : filter α} : (∀g∈s, g ≤ f) → Sup s ≤ f := assume h a ha g hg, h g hg ha @[simp] lemma mem_Sup_sets {S : set (filter α)} {s : set α} : s ∈ (Sup S).sets ↔ ∀ f ∈ S, s ∈ (f : filter α).sets := by simp [Sup, has_Sup.Sup]; refl @[simp] lemma join_principal_eq_Sup {s : set (filter α)} : join (principal s) = Sup s := rfl instance complete_lattice_filter : complete_lattice (filter α) := { sup := (⊔), le_sup_left := assume a b, inter_subset_left _ _, le_sup_right := assume a b, inter_subset_right _ _, sup_le := assume a b c h₁ h₂, subset_inter h₁ h₂, inf := (⊓), le_inf := assume f g h fg fh s ⟨a, ha, b, hb, h⟩, by filter_upwards [fg ha, fh hb] assume x ha hb, h ⟨ha, hb⟩, inf_le_left := assume f g s, mem_inf_sets_of_left, inf_le_right := assume f g s, mem_inf_sets_of_right, top := ⊤, le_top := assume a, show a ≤ principal univ, by simp [univ_mem_sets], bot := ⊥, bot_le := assume a, show a.sets ⊆ {x | ∅ ⊆ x}, by simp; apply subset_univ, Sup := Sup, le_Sup := assume s f, filter.le_Sup, Sup_le := assume s f, filter.Sup_le, Inf := λs, Sup {x | ∀y∈s, x ≤ y}, le_Inf := assume s a h, filter.le_Sup h, Inf_le := assume s a ha, filter.Sup_le $ assume b h, h _ ha, ..filter.partial_order } /- lattice equations -/ lemma empty_in_sets_eq_bot {f : filter α} : ∅ ∈ f.sets ↔ f = ⊥ := ⟨assume h, bot_unique $ assume s _, f.upwards_sets h (empty_subset s), assume : f = ⊥, this.symm ▸ mem_bot_sets⟩ lemma inhabited_of_mem_sets {f : filter α} {s : set α} (hf : f ≠ ⊥) (hs : s ∈ f.sets) : ∃x, x ∈ s := have ∅ ∉ f.sets, from assume h, hf $ empty_in_sets_eq_bot.mp h, have s ≠ ∅, from assume h, this (h ▸ hs), exists_mem_of_ne_empty this lemma filter_eq_bot_of_not_nonempty {f : filter α} (ne : ¬ nonempty α) : f = ⊥ := empty_in_sets_eq_bot.mp $ univ_mem_sets' $ assume x, false.elim (ne ⟨x⟩) lemma forall_sets_neq_empty_iff_neq_bot {f : filter α} : (∀ (s : set α), s ∈ f.sets → s ≠ ∅) ↔ f ≠ ⊥ := by simp [(@empty_in_sets_eq_bot α f).symm]; exact ⟨assume h hs, h _ hs rfl, assume h s hs eq, h $ eq ▸ hs⟩ lemma mem_sets_of_neq_bot {f : filter α} {s : set α} (h : f ⊓ principal (-s) = ⊥) : s ∈ f.sets := have ∅ ∈ (f ⊓ principal (- s)).sets, from h.symm ▸ mem_bot_sets, let ⟨s₁, hs₁, s₂, (hs₂ : -s ⊆ s₂), (hs : s₁ ∩ s₂ ⊆ ∅)⟩ := this in by filter_upwards [hs₁] assume a ha, classical.by_contradiction $ assume ha', hs ⟨ha, hs₂ ha'⟩ lemma infi_sets_eq {f : ι → filter α} (h : directed (≤) f) (ne : nonempty ι) : (infi f).sets = (⋃ i, (f i).sets) := let ⟨i⟩ := ne, u := { filter . sets := (⋃ i, (f i).sets), exists_mem_sets := ⟨univ, begin simp, exact ⟨i, univ_mem_sets⟩ end⟩, directed_sets := directed_on_Union (show directed (≤) f, from h) (assume i, (f i).directed_sets), upwards_sets := by simpa using assume x y j xf (xy : x ⊆ y), exists.intro j ((f j).upwards_sets xf xy) } in subset.antisymm (show u ≤ infi f, from le_infi $ assume i, le_supr (λi, (f i).sets) i) (Union_subset $ assume i, infi_le f i) lemma infi_sets_eq' {f : β → filter α} {s : set β} (h : directed_on (λx y, f x ≤ f y) s) (ne : ∃i, i ∈ s) : (⨅ i∈s, f i).sets = (⋃ i ∈ s, (f i).sets) := let ⟨i, hi⟩ := ne in calc (⨅ i ∈ s, f i).sets = (⨅ t : {t // t ∈ s}, (f t.val)).sets : by rw [infi_subtype]; refl ... = (⨆ t : {t // t ∈ s}, (f t.val).sets) : infi_sets_eq (assume ⟨x, hx⟩ ⟨y, hy⟩, match h x hx y hy with ⟨z, h₁, h₂, h₃⟩ := ⟨⟨z, h₁⟩, h₂, h₃⟩ end) ⟨⟨i, hi⟩⟩ ... = (⨆ t ∈ {t | t ∈ s}, (f t).sets) : by rw [supr_subtype]; refl lemma Inf_sets_eq_finite {s : set (filter α)} : (Inf s).sets = (⋃ t ∈ {t | finite t ∧ t ⊆ s}, (Inf t).sets) := calc (Inf s).sets = (⨅ t ∈ { t | finite t ∧ t ⊆ s}, Inf t).sets : by rw [lattice.Inf_eq_finite_sets] ... = (⨆ t ∈ {t | finite t ∧ t ⊆ s}, (Inf t).sets) : infi_sets_eq' (assume x ⟨hx₁, hx₂⟩ y ⟨hy₁, hy₂⟩, ⟨x ∪ y, ⟨finite_union hx₁ hy₁, union_subset hx₂ hy₂⟩, Inf_le_Inf $ subset_union_left _ _, Inf_le_Inf $ subset_union_right _ _⟩) ⟨∅, by simp⟩ lemma supr_sets_eq {f : ι → filter α} : (supr f).sets = (⋂i, (f i).sets) := set.ext $ assume s, show s ∈ (join (principal {a : filter α | ∃i : ι, a = f i})).sets ↔ s ∈ (⋂i, (f i).sets), begin rw [mem_join_sets], simp, rw [forall_swap], exact forall_congr (λ i, by simp) end @[simp] lemma mem_supr_sets {f : ι → filter α} {s : set α} : s ∈ (supr f).sets ↔ ∀ i, s ∈ (f i).sets := by simp [supr_sets_eq] @[simp] lemma sup_join {f₁ f₂ : filter (filter α)} : (join f₁ ⊔ join f₂) = join (f₁ ⊔ f₂) := filter_eq $ set.ext $ assume x, by simp [supr_sets_eq, join] @[simp] lemma supr_join {ι : Sort w} {f : ι → filter (filter α)} : (⨆x, join (f x)) = join (⨆x, f x) := filter_eq $ set.ext $ assume x, by simp [supr_sets_eq, join] instance : bounded_distrib_lattice (filter α) := { le_sup_inf := assume x y z s, begin simp only [and_assoc, mem_inf_sets, mem_sup_sets, exists_prop, exists_imp_distrib, and_imp], intros hs t₁ ht₁ t₂ ht₂ hts, exact ⟨s ∪ t₁, x.upwards_sets hs $ subset_union_left _ _, y.upwards_sets ht₁ $ subset_union_right _ _, s ∪ t₂, x.upwards_sets hs $ subset_union_left _ _, z.upwards_sets ht₂ $ subset_union_right _ _, subset.trans (@le_sup_inf (set α) _ _ _ _) (union_subset (subset.refl _) hts)⟩ end, ..filter.complete_lattice_filter } private lemma infi_finite_distrib {s : set (filter α)} {f : filter α} (h : finite s) : (⨅ a ∈ s, f ⊔ a) = f ⊔ (Inf s) := finite.induction_on h (by simp only [mem_empty_eq, infi_false, infi_top, Inf_empty, sup_top_eq]) (by intros a s hn hs hi; rw [infi_insert, hi, ← sup_inf_left, Inf_insert]) /- the complementary version with ⨆ g∈s, f ⊓ g does not hold! -/ lemma binfi_sup_eq { f : filter α } {s : set (filter α)} : (⨅ g∈s, f ⊔ g) = f ⊔ Inf s := le_antisymm begin intros t h, cases h with h₁ h₂, rw [Inf_sets_eq_finite] at h₂, simp [and_assoc] at h₂, cases h₂ with s' hs', cases hs' with hs' hs'', cases hs'' with hs's ht', have ht : t ∈ (⨅ a ∈ s', f ⊔ a).sets, { rw [infi_finite_distrib], exact ⟨h₁, ht'⟩, exact hs' }, clear h₁ ht', revert ht t, change (⨅ a ∈ s, f ⊔ a) ≤ (⨅ a ∈ s', f ⊔ a), apply infi_le_infi2 _, exact assume i, ⟨i, infi_le_infi2 $ assume h, ⟨hs's h, le_refl _⟩⟩ end (le_infi $ assume g, le_infi $ assume h, sup_le_sup (le_refl f) $ Inf_le h) lemma infi_sup_eq { f : filter α } {g : ι → filter α} : (⨅ x, f ⊔ g x) = f ⊔ infi g := calc (⨅ x, f ⊔ g x) = (⨅ x (h : ∃i, g i = x), f ⊔ x) : by simp; rw [infi_comm]; simp ... = f ⊔ Inf {x | ∃i, g i = x} : binfi_sup_eq ... = f ⊔ infi g : by rw [Inf_eq_infi]; dsimp; simp; rw [infi_comm]; simp lemma mem_infi_sets_finset {s : finset α} {f : α → filter β} : ∀t, t ∈ (⨅a∈s, f a).sets ↔ (∃p:α → set β, (∀a∈s, p a ∈ (f a).sets) ∧ (⋂a∈s, p a) ⊆ t) := show ∀t, t ∈ (⨅a∈s, f a).sets ↔ (∃p:α → set β, (∀a∈s, p a ∈ (f a).sets) ∧ (⨅a∈s, p a) ≤ t), begin refine finset.induction_on s _ _, { simp only [finset.not_mem_empty, false_implies_iff, lattice.infi_empty_finset, top_le_iff, imp_true_iff, mem_top_sets_iff, true_and, exists_const], exact assume _, iff.refl _ }, { intros a s has ih t, simp only [ih, finset.forall_mem_insert, lattice.infi_insert_finset, mem_inf_sets, exists_prop, iff_iff_implies_and_implies, exists_imp_distrib, and_imp, and_assoc] {contextual := tt}, split, { intros t₁ ht₁ t₂ p hp ht₂ ht, existsi function.update p a t₁, have : ∀a'∈s, function.update p a t₁ a' = p a', from assume a' ha', have a' ≠ a, from assume h, has $ h ▸ ha', function.update_noteq this, have eq : (⨅j ∈ s, function.update p a t₁ j) = (⨅j ∈ s, p j), begin congr, funext b, congr, funext h, apply this, assumption end, simp only [this, ht₁, hp, function.update_same, true_and, imp_true_iff, eq] {contextual := tt}, exact subset.trans (inter_subset_inter (subset.refl _) ht₂) ht }, from assume p hpa hp ht, ⟨p a, hpa, (⨅j∈s, p j), ⟨⟨p, hp, le_refl _⟩, ht⟩⟩ } end /- principal equations -/ @[simp] lemma inf_principal {s t : set α} : principal s ⊓ principal t = principal (s ∩ t) := le_antisymm (by simp; exact ⟨s, subset.refl s, t, subset.refl t, by simp⟩) (by simp [le_inf_iff, inter_subset_left, inter_subset_right]) @[simp] lemma sup_principal {s t : set α} : principal s ⊔ principal t = principal (s ∪ t) := filter_eq $ set.ext $ by simp [union_subset_iff] @[simp] lemma supr_principal {ι : Sort w} {s : ι → set α} : (⨆x, principal (s x)) = principal (⋃i, s i) := filter_eq $ set.ext $ assume x, by simp [supr_sets_eq]; exact (@supr_le_iff (set α) _ _ _ _).symm lemma principal_univ : principal (univ : set α) = ⊤ := rfl lemma principal_empty : principal (∅ : set α) = ⊥ := rfl @[simp] lemma principal_eq_bot_iff {s : set α} : principal s = ⊥ ↔ s = ∅ := ⟨assume h, principal_eq_iff_eq.mp $ by simp [principal_empty, h], assume h, by simp [*, principal_empty]⟩ lemma inf_principal_eq_bot {f : filter α} {s : set α} (hs : -s ∈ f.sets) : f ⊓ principal s = ⊥ := empty_in_sets_eq_bot.mp ⟨_, hs, s, mem_principal_self s, assume x ⟨h₁, h₂⟩, h₁ h₂⟩ end lattice section map /-- The forward map of a filter -/ def map (m : α → β) (f : filter α) : filter β := { sets := preimage m ⁻¹' f.sets, exists_mem_sets := ⟨univ, univ_mem_sets⟩, upwards_sets := assume s t hs st, f.upwards_sets hs (assume x h, st h), directed_sets := assume s hs t ht, ⟨s ∩ t, inter_mem_sets hs ht, inter_subset_left _ _, inter_subset_right _ _⟩ } @[simp] lemma map_principal {s : set α} {f : α → β} : map f (principal s) = principal (set.image f s) := filter_eq $ set.ext $ assume a, image_subset_iff.symm variables {f : filter α} {m : α → β} {m' : β → γ} {s : set α} {t : set β} @[simp] lemma mem_map : t ∈ (map m f).sets ↔ {x | m x ∈ t} ∈ f.sets := iff.rfl lemma image_mem_map (hs : s ∈ f.sets) : m '' s ∈ (map m f).sets := f.upwards_sets hs $ assume x hx, ⟨x, hx, rfl⟩ @[simp] lemma map_id : filter.map id f = f := filter_eq $ rfl @[simp] lemma map_compose : filter.map m' ∘ filter.map m = filter.map (m' ∘ m) := funext $ assume _, filter_eq $ rfl @[simp] lemma map_map : filter.map m' (filter.map m f) = filter.map (m' ∘ m) f := congr_fun (@@filter.map_compose m m') f end map section vmap /-- The inverse map of a filter -/ def vmap (m : α → β) (f : filter β) : filter α := { sets := { s | ∃t∈f.sets, m ⁻¹' t ⊆ s }, exists_mem_sets := ⟨univ, univ, univ_mem_sets, by simp⟩, upwards_sets := assume a b ⟨a', ha', ma'a⟩ ab, ⟨a', ha', subset.trans ma'a ab⟩, directed_sets := assume a ⟨a', ha₁, ha₂⟩ b ⟨b', hb₁, hb₂⟩, ⟨preimage m (a' ∩ b'), ⟨a' ∩ b', inter_mem_sets ha₁ hb₁, subset.refl _⟩, subset.trans (preimage_mono $ inter_subset_left _ _) ha₂, subset.trans (preimage_mono $ inter_subset_right _ _) hb₂⟩ } end vmap /-- The cofinite filter is the filter of subsets whose complements are finite. -/ def cofinite : filter α := { sets := {s | finite (- s)}, exists_mem_sets := ⟨univ, by simp⟩, upwards_sets := assume s t, assume hs : finite (-s), assume st: s ⊆ t, finite_subset hs $ @lattice.neg_le_neg (set α) _ _ _ st, directed_sets := assume s, assume hs : finite (-s), assume t, assume ht : finite (-t), ⟨s ∩ t, by simp [compl_inter, finite_union, ht, hs], inter_subset_left _ _, inter_subset_right _ _⟩ } /-- The monadic bind operation on filter is defined the usual way in terms of `map` and `join`. -/ def bind (f : filter α) (m : α → filter β) : filter β := join (map m f) instance monad_filter : monad filter := { bind := @bind, pure := λ(α : Type u) x, principal {x}, map := @filter.map, id_map := assume α f, filter_eq rfl, pure_bind := assume α β a f, by simp [bind, Sup_image], bind_assoc := assume α β γ f m₁ m₂, filter_eq rfl, bind_pure_comp_eq_map := assume α β f x, filter_eq $ by simp [bind, join, map, preimage, principal] } @[simp] lemma pure_def (x : α) : pure x = principal {x} := rfl @[simp] lemma mem_pure {a : α} {s : set α} : a ∈ s → s ∈ (pure a : filter α).sets := by simp; exact id @[simp] lemma map_def {α β} (m : α → β) (f : filter α) : m <$> f = map m f := rfl @[simp] lemma bind_def {α β} (f : filter α) (m : α → filter β) : f >>= m = bind f m := rfl instance : alternative filter := { failure := λα, ⊥, orelse := λα x y, x ⊔ y, ..filter.monad_filter } /- map and vmap equations -/ section map variables {f f₁ f₂ : filter α} {g g₁ g₂ : filter β} {m : α → β} {m' : β → γ} {s : set α} {t : set β} @[simp] theorem mem_vmap_sets : s ∈ (vmap m g).sets ↔ ∃t∈g.sets, m ⁻¹' t ⊆ s := iff.rfl theorem preimage_mem_vmap (ht : t ∈ g.sets) : m ⁻¹' t ∈ (vmap m g).sets := ⟨t, ht, subset.refl _⟩ lemma vmap_id : vmap id f = f := le_antisymm (assume s, preimage_mem_vmap) (assume s ⟨t, ht, hst⟩, f.upwards_sets ht hst) lemma vmap_vmap_comp {m : γ → β} {n : β → α} : vmap m (vmap n f) = vmap (n ∘ m) f := le_antisymm (assume c ⟨b, hb, (h : preimage (n ∘ m) b ⊆ c)⟩, ⟨preimage n b, preimage_mem_vmap hb, h⟩) (assume c ⟨b, ⟨a, ha, (h₁ : preimage n a ⊆ b)⟩, (h₂ : preimage m b ⊆ c)⟩, ⟨a, ha, show preimage m (preimage n a) ⊆ c, from subset.trans (preimage_mono h₁) h₂⟩) @[simp] theorem vmap_principal {t : set β} : vmap m (principal t) = principal (m ⁻¹' t) := filter_eq $ set.ext $ assume s, ⟨assume ⟨u, (hu : t ⊆ u), (b : preimage m u ⊆ s)⟩, subset.trans (preimage_mono hu) b, assume : preimage m t ⊆ s, ⟨t, subset.refl t, this⟩⟩ lemma map_le_iff_le_vmap : map m f ≤ g ↔ f ≤ vmap m g := ⟨assume h s ⟨t, ht, hts⟩, f.upwards_sets (h ht) hts, assume h s ht, h ⟨_, ht, subset.refl _⟩⟩ lemma gc_map_vmap (m : α → β) : galois_connection (map m) (vmap m) := assume f g, map_le_iff_le_vmap lemma map_mono (h : f₁ ≤ f₂) : map m f₁ ≤ map m f₂ := (gc_map_vmap m).monotone_l h lemma monotone_map : monotone (map m) | a b := map_mono lemma vmap_mono (h : g₁ ≤ g₂) : vmap m g₁ ≤ vmap m g₂ := (gc_map_vmap m).monotone_u h lemma monotone_vmap : monotone (vmap m) | a b := vmap_mono @[simp] lemma map_bot : map m ⊥ = ⊥ := (gc_map_vmap m).l_bot @[simp] lemma map_sup : map m (f₁ ⊔ f₂) = map m f₁ ⊔ map m f₂ := (gc_map_vmap m).l_sup @[simp] lemma map_supr {f : ι → filter α} : map m (⨆i, f i) = (⨆i, map m (f i)) := (gc_map_vmap m).l_supr @[simp] lemma vmap_top : vmap m ⊤ = ⊤ := (gc_map_vmap m).u_top @[simp] lemma vmap_inf : vmap m (g₁ ⊓ g₂) = vmap m g₁ ⊓ vmap m g₂ := (gc_map_vmap m).u_inf @[simp] lemma vmap_infi {f : ι → filter β} : vmap m (⨅i, f i) = (⨅i, vmap m (f i)) := (gc_map_vmap m).u_infi lemma map_vmap_le : map m (vmap m g) ≤ g := (gc_map_vmap m).decreasing_l_u _ lemma le_vmap_map : f ≤ vmap m (map m f) := (gc_map_vmap m).increasing_u_l _ @[simp] lemma vmap_bot : vmap m ⊥ = ⊥ := bot_unique $ assume s _, ⟨∅, by simp, by simp⟩ lemma vmap_sup : vmap m (g₁ ⊔ g₂) = vmap m g₁ ⊔ vmap m g₂ := le_antisymm (assume s ⟨⟨t₁, ht₁, hs₁⟩, ⟨t₂, ht₂, hs₂⟩⟩, ⟨t₁ ∪ t₂, ⟨g₁.upwards_sets ht₁ (subset_union_left _ _), g₂.upwards_sets ht₂ (subset_union_right _ _)⟩, union_subset hs₁ hs₂⟩) (sup_le (vmap_mono le_sup_left) (vmap_mono le_sup_right)) lemma le_map_vmap' {f : filter β} {m : α → β} {s : set β} (hs : s ∈ f.sets) (hm : ∀b∈s, ∃a, m a = b) : f ≤ map m (vmap m f) := assume t' ⟨t, ht, (sub : m ⁻¹' t ⊆ m ⁻¹' t')⟩, by filter_upwards [ht, hs] assume x hxt hxs, let ⟨y, hy⟩ := hm x hxs in hy ▸ sub (show m y ∈ t, from hy.symm ▸ hxt) lemma le_map_vmap {f : filter β} {m : α → β} (hm : ∀x, ∃y, m y = x) : f ≤ map m (vmap m f) := le_map_vmap' univ_mem_sets (assume b _, hm b) lemma vmap_map {f : filter α} {m : α → β} (h : ∀ x y, m x = m y → x = y) : vmap m (map m f) = f := have ∀s, preimage m (image m s) = s, from assume s, preimage_image_eq s h, le_antisymm (assume s hs, ⟨ image m s, f.upwards_sets hs $ by simp [this, subset.refl], by simp [this, subset.refl]⟩) le_vmap_map lemma le_of_map_le_map_inj' {f g : filter α} {m : α → β} {s : set α} (hsf : s ∈ f.sets) (hsg : s ∈ g.sets) (hm : ∀x∈s, ∀y∈s, m x = m y → x = y) (h : map m f ≤ map m g) : f ≤ g := assume t ht, by filter_upwards [hsf, h $ image_mem_map (inter_mem_sets hsg ht)] assume a has ⟨b, ⟨hbs, hb⟩, h⟩, have b = a, from hm _ hbs _ has h, this ▸ hb lemma eq_of_map_eq_map_inj' {f g : filter α} {m : α → β} {s : set α} (hsf : s ∈ f.sets) (hsg : s ∈ g.sets) (hm : ∀x∈s, ∀y∈s, m x = m y → x = y) (h : map m f = map m g) : f = g := le_antisymm (le_of_map_le_map_inj' hsf hsg hm $ le_of_eq h) (le_of_map_le_map_inj' hsg hsf hm $ le_of_eq h.symm) lemma map_inj {f g : filter α} {m : α → β} (hm : ∀ x y, m x = m y → x = y) (h : map m f = map m g) : f = g := have vmap m (map m f) = vmap m (map m g), by rw h, by rwa [vmap_map hm, vmap_map hm] at this lemma vmap_neq_bot {f : filter β} {m : α → β} (hm : ∀t∈f.sets, ∃a, m a ∈ t) : vmap m f ≠ ⊥ := forall_sets_neq_empty_iff_neq_bot.mp $ assume s ⟨t, ht, t_s⟩, let ⟨a, (ha : a ∈ preimage m t)⟩ := hm t ht in neq_bot_of_le_neq_bot (ne_empty_of_mem ha) t_s lemma vmap_neq_bot_of_surj {f : filter β} {m : α → β} (hf : f ≠ ⊥) (hm : ∀b, ∃a, m a = b) : vmap m f ≠ ⊥ := vmap_neq_bot $ assume t ht, let ⟨b, (hx : b ∈ t)⟩ := inhabited_of_mem_sets hf ht, ⟨a, (ha : m a = b)⟩ := hm b in ⟨a, ha.symm ▸ hx⟩ @[simp] lemma map_eq_bot_iff : map m f = ⊥ ↔ f = ⊥ := ⟨by rw [←empty_in_sets_eq_bot, ←empty_in_sets_eq_bot]; exact id, assume h, by simp [*]⟩ lemma map_ne_bot (hf : f ≠ ⊥) : map m f ≠ ⊥ := assume h, hf $ by rwa [map_eq_bot_iff] at h end map lemma map_cong {m₁ m₂ : α → β} {f : filter α} (h : {x | m₁ x = m₂ x} ∈ f.sets) : map m₁ f = map m₂ f := have ∀(m₁ m₂ : α → β) (h : {x | m₁ x = m₂ x} ∈ f.sets), map m₁ f ≤ map m₂ f, begin intros m₁ m₂ h s hs, show {x | m₁ x ∈ s} ∈ f.sets, filter_upwards [h, hs], simp [subset_def] {contextual := tt} end, le_antisymm (this m₁ m₂ h) (this m₂ m₁ $ f.upwards_sets h $ assume x, eq.symm) -- this is a generic rule for monotone functions: lemma map_infi_le {f : ι → filter α} {m : α → β} : map m (infi f) ≤ (⨅ i, map m (f i)) := le_infi $ assume i, map_mono $ infi_le _ _ lemma map_infi_eq {f : ι → filter α} {m : α → β} (hf : directed (≤) f) (hι : nonempty ι) : map m (infi f) = (⨅ i, map m (f i)) := le_antisymm map_infi_le (assume s (hs : preimage m s ∈ (infi f).sets), have ∃i, preimage m s ∈ (f i).sets, by simp [infi_sets_eq hf hι] at hs; assumption, let ⟨i, hi⟩ := this in have (⨅ i, map m (f i)) ≤ principal s, from infi_le_of_le i $ by simp; assumption, by simp at this; assumption) lemma map_binfi_eq {ι : Type w} {f : ι → filter α} {m : α → β} {p : ι → Prop} (h : directed_on (λx y, f x ≤ f y) {x | p x}) (ne : ∃i, p i) : map m (⨅i (h : p i), f i) = (⨅i (h: p i), map m (f i)) := let ⟨i, hi⟩ := ne in calc map m (⨅i (h : p i), f i) = map m (⨅i:subtype p, f i.val) : by simp [infi_subtype] ... = (⨅i:subtype p, map m (f i.val)) : map_infi_eq (assume ⟨x, hx⟩ ⟨y, hy⟩, match h x hx y hy with ⟨z, h₁, h₂, h₃⟩ := ⟨⟨z, h₁⟩, h₂, h₃⟩ end) ⟨⟨i, hi⟩⟩ ... = (⨅i (h : p i), map m (f i)) : by simp [infi_subtype] lemma map_inf' {f g : filter α} {m : α → β} {t : set α} (htf : t ∈ f.sets) (htg : t ∈ g.sets) (h : ∀x∈t, ∀y∈t, m x = m y → x = y) : map m (f ⊓ g) = map m f ⊓ map m g := begin refine le_antisymm (le_inf (map_mono inf_le_left) (map_mono inf_le_right)) (assume s hs, _), simp [map, mem_inf_sets] at hs ⊢, rcases hs with ⟨t₁, h₁, t₂, h₂, hs⟩, refine ⟨m '' (t₁ ∩ t), _, m '' (t₂ ∩ t), _, _⟩, { filter_upwards [h₁, htf] assume a h₁ h₂, mem_image_of_mem _ ⟨h₁, h₂⟩ }, { filter_upwards [h₂, htg] assume a h₁ h₂, mem_image_of_mem _ ⟨h₁, h₂⟩ }, { rw [image_inter_on], { refine image_subset_iff.2 _, exact λ x ⟨⟨h₁, _⟩, h₂, _⟩, hs ⟨h₁, h₂⟩ }, { exact λ x ⟨_, hx⟩ y ⟨_, hy⟩, h x hx y hy } } end lemma map_inf {f g : filter α} {m : α → β} (h : ∀ x y, m x = m y → x = y) : map m (f ⊓ g) = map m f ⊓ map m g := map_inf' univ_mem_sets univ_mem_sets (assume x _ y _, h x y) lemma map_eq_vmap_of_inverse {f : filter α} {m : α → β} {n : β → α} (h₁ : m ∘ n = id) (h₂ : n ∘ m = id) : map m f = vmap n f := le_antisymm (assume b ⟨a, ha, (h : preimage n a ⊆ b)⟩, f.upwards_sets ha $ calc a = preimage (n ∘ m) a : by simp [h₂, preimage_id] ... ⊆ preimage m b : preimage_mono h) (assume b (hb : preimage m b ∈ f.sets), ⟨preimage m b, hb, show preimage (m ∘ n) b ⊆ b, by simp [h₁]; apply subset.refl⟩) lemma map_swap_eq_vmap_swap {f : filter (α × β)} : prod.swap <$> f = vmap prod.swap f := map_eq_vmap_of_inverse prod.swap_swap_eq prod.swap_swap_eq /- bind equations -/ @[simp] lemma mem_bind_sets {s : set β} {f : filter α} {m : α → filter β} : s ∈ (bind f m).sets ↔ ∃t ∈ f.sets, ∀x ∈ t, s ∈ (m x).sets := calc s ∈ (bind f m).sets ↔ {a | s ∈ (m a).sets} ∈ f.sets : by simp [bind] ... ↔ (∃t ∈ f.sets, t ⊆ {a | s ∈ (m a).sets}) : exists_sets_subset_iff.symm ... ↔ (∃t ∈ f.sets, ∀x ∈ t, s ∈ (m x).sets) : iff.refl _ lemma bind_mono {f : filter α} {g h : α → filter β} (h₁ : {a | g a ≤ h a} ∈ f.sets) : bind f g ≤ bind f h := assume x h₂, show (_ ∈ f.sets), by filter_upwards [h₁, h₂] assume s gh' h', gh' h' lemma bind_sup {f g : filter α} {h : α → filter β} : bind (f ⊔ g) h = bind f h ⊔ bind g h := by simp [bind] lemma bind_mono2 {f g : filter α} {h : α → filter β} (h₁ : f ≤ g) : bind f h ≤ bind g h := assume s h', h₁ h' lemma principal_bind {s : set α} {f : α → filter β} : (bind (principal s) f) = (⨆x ∈ s, f x) := show join (map f (principal s)) = (⨆x ∈ s, f x), by simp [Sup_image] lemma seq_mono {β : Type u} {f₁ f₂ : filter (α → β)} {g₁ g₂ : filter α} (hf : f₁ ≤ f₂) (hg : g₁ ≤ g₂) : f₁ <*> g₁ ≤ f₂ <*> g₂ := le_trans (bind_mono2 hf) (bind_mono $ univ_mem_sets' $ assume f, map_mono hg) @[simp] lemma mem_pure_sets {a : α} {s : set α} : s ∈ (pure a : filter α).sets ↔ a ∈ s := by simp @[simp] lemma mem_return_sets {a : α} {s : set α} : s ∈ (return a : filter α).sets ↔ a ∈ s := mem_pure_sets lemma infi_neq_bot_of_directed {f : ι → filter α} (hn : nonempty α) (hd : directed (≤) f) (hb : ∀i, f i ≠ ⊥): (infi f) ≠ ⊥ := let ⟨x⟩ := hn in assume h, have he: ∅ ∈ (infi f).sets, from h.symm ▸ mem_bot_sets, classical.by_cases (assume : nonempty ι, have ∃i, ∅ ∈ (f i).sets, by rw [infi_sets_eq hd this] at he; simp at he; assumption, let ⟨i, hi⟩ := this in hb i $ bot_unique $ assume s _, (f i).upwards_sets hi $ empty_subset _) (assume : ¬ nonempty ι, have univ ⊆ (∅ : set α), begin rw [←principal_mono, principal_univ, principal_empty, ←h], exact (le_infi $ assume i, false.elim $ this ⟨i⟩) end, this $ mem_univ x) lemma infi_neq_bot_iff_of_directed {f : ι → filter α} (hn : nonempty α) (hd : directed (≤) f) : (infi f) ≠ ⊥ ↔ (∀i, f i ≠ ⊥) := ⟨assume neq_bot i eq_bot, neq_bot $ bot_unique $ infi_le_of_le i $ eq_bot ▸ le_refl _, infi_neq_bot_of_directed hn hd⟩ lemma mem_infi_sets {f : ι → filter α} (i : ι) : ∀{s}, s ∈ (f i).sets → s ∈ (⨅i, f i).sets := show (⨅i, f i) ≤ f i, from infi_le _ _ @[elab_as_eliminator] lemma infi_sets_induct {f : ι → filter α} {s : set α} (hs : s ∈ (infi f).sets) {p : set α → Prop} (uni : p univ) (ins : ∀{i s₁ s₂}, s₁ ∈ (f i).sets → p s₂ → p (s₁ ∩ s₂)) (upw : ∀{s₁ s₂}, s₁ ⊆ s₂ → p s₁ → p s₂) : p s := begin have hs' : s ∈ (Inf {a : filter α | ∃ (i : ι), a = f i}).sets := hs, rw [Inf_sets_eq_finite] at hs', simp only [mem_Union_eq] at hs', rcases hs' with ⟨is, h, hs⟩, cases h with fin_is his, revert his s, refine finite.induction_on fin_is _ (λ fi is fi_ne_is fin_is ih, _); intros his s hs' hs, { rw [Inf_empty, mem_top_sets_iff] at hs, subst hs, assumption }, { rw [Inf_insert] at hs, rcases hs with ⟨s₁, hs₁, s₂, hs₂, hs⟩, rcases (his (mem_insert _ _) : ∃i, fi = f i) with ⟨i, rfl⟩, have hs₂ : p s₂, from have his : is ⊆ {x | ∃i, x = f i}, from assume i hi, his $ mem_insert_of_mem _ hi, have infi f ≤ Inf is, from Inf_le_Inf his, ih his (this hs₂) hs₂, exact upw hs (ins hs₁ hs₂) } end @[simp] lemma pure_neq_bot {α : Type u} {a : α} : pure a ≠ (⊥ : filter α) := by simp [pure, has_pure.pure] /- tendsto -/ /-- `tendsto` is the generic "limit of a function" predicate. `tendsto f l₁ l₂` asserts that for every `l₂` neighborhood `a`, the `f`-preimage of `a` is an `l₁` neighborhood. -/ def tendsto (f : α → β) (l₁ : filter α) (l₂ : filter β) := l₁.map f ≤ l₂ lemma tendsto_def {f : α → β} {l₁ : filter α} {l₂ : filter β} : tendsto f l₁ l₂ ↔ ∀ s ∈ l₂.sets, f ⁻¹' s ∈ l₁.sets := iff.rfl lemma tendsto_iff_vmap {f : α → β} {l₁ : filter α} {l₂ : filter β} : tendsto f l₁ l₂ ↔ l₁ ≤ l₂.vmap f := map_le_iff_le_vmap lemma tendsto_cong {f₁ f₂ : α → β} {l₁ : filter α} {l₂ : filter β} (h : tendsto f₁ l₁ l₂) (hl : {x | f₁ x = f₂ x} ∈ l₁.sets) : tendsto f₂ l₁ l₂ := by rwa [tendsto, ←map_cong hl] lemma tendsto_id' {x y : filter α} : x ≤ y → tendsto id x y := by simp [tendsto] { contextual := tt } lemma tendsto_id {x : filter α} : tendsto id x x := tendsto_id' $ le_refl x lemma tendsto.comp {f : α → β} {g : β → γ} {x : filter α} {y : filter β} {z : filter γ} (hf : tendsto f x y) (hg : tendsto g y z) : tendsto (g ∘ f) x z := calc map (g ∘ f) x = map g (map f x) : by rw [map_map] ... ≤ map g y : map_mono hf ... ≤ z : hg lemma tendsto_le_left {f : α → β} {x y : filter α} {z : filter β} (h : y ≤ x) : tendsto f x z → tendsto f y z := le_trans (map_mono h) lemma tendsto_le_right {f : α → β} {x : filter α} {y z : filter β} (h₁ : y ≤ z) (h₂ : tendsto f x y) : tendsto f x z := le_trans h₂ h₁ lemma tendsto_map {f : α → β} {x : filter α} : tendsto f x (map f x) := le_refl (map f x) lemma tendsto_map' {f : β → γ} {g : α → β} {x : filter α} {y : filter γ} (h : tendsto (f ∘ g) x y) : tendsto f (map g x) y := by rwa [tendsto, map_map] lemma tendsto_vmap {f : α → β} {x : filter β} : tendsto f (vmap f x) x := map_vmap_le lemma tendsto_vmap_iff {f : α → β} {g : β → γ} {a : filter α} {c : filter γ} : tendsto f a (c.vmap g) ↔ tendsto (g ∘ f) a c := ⟨assume h, h.comp tendsto_vmap, assume h, map_le_iff_le_vmap.mp $ by rwa [map_map]⟩ lemma tendsto_vmap'' {m : α → β} {f : filter α} {g : filter β} (s : set α) {i : γ → α} (hs : s ∈ f.sets) (hi : ∀a∈s, ∃c, i c = a) (h : tendsto (m ∘ i) (vmap i f) g) : tendsto m f g := have tendsto m (map i $ vmap i $ f) g, by rwa [tendsto, ←map_compose] at h, le_trans (map_mono $ le_map_vmap' hs hi) this lemma tendsto_inf {f : α → β} {x : filter α} {y₁ y₂ : filter β} : tendsto f x (y₁ ⊓ y₂) ↔ tendsto f x y₁ ∧ tendsto f x y₂ := by simp [tendsto] lemma tendsto_inf_left {f : α → β} {x₁ x₂ : filter α} {y : filter β} (h : tendsto f x₁ y) : tendsto f (x₁ ⊓ x₂) y := le_trans (map_mono inf_le_left) h lemma tendsto_inf_right {f : α → β} {x₁ x₂ : filter α} {y : filter β} (h : tendsto f x₂ y) : tendsto f (x₁ ⊓ x₂) y := le_trans (map_mono inf_le_right) h lemma tendsto_infi {f : α → β} {x : filter α} {y : ι → filter β} : tendsto f x (⨅i, y i) ↔ ∀i, tendsto f x (y i) := by simp [tendsto] lemma tendsto_infi' {f : α → β} {x : ι → filter α} {y : filter β} (i : ι) : tendsto f (x i) y → tendsto f (⨅i, x i) y := tendsto_le_left (infi_le _ _) lemma tendsto_principal {f : α → β} {a : filter α} {s : set β} : tendsto f a (principal s) ↔ {a | f a ∈ s} ∈ a.sets := by simp [tendsto] lemma tendsto_principal_principal {f : α → β} {s : set α} {t : set β} : tendsto f (principal s) (principal t) ↔ ∀a∈s, f a ∈ t := by simp [tendsto, image_subset_iff]; refl section lift /-- A variant on `bind` using a function `g` taking a set instead of a member of `α`. -/ protected def lift (f : filter α) (g : set α → filter β) := ⨅s ∈ f.sets, g s variables {f f₁ f₂ : filter α} {g g₁ g₂ : set α → filter β} lemma lift_sets_eq (hg : monotone g) : (f.lift g).sets = (⋃t∈f.sets, (g t).sets) := infi_sets_eq' (assume s hs t ht, ⟨s ∩ t, inter_mem_sets hs ht, hg $ inter_subset_left s t, hg $ inter_subset_right s t⟩) ⟨univ, univ_mem_sets⟩ lemma mem_lift {s : set β} {t : set α} (ht : t ∈ f.sets) (hs : s ∈ (g t).sets) : s ∈ (f.lift g).sets := le_principal_iff.mp $ show f.lift g ≤ principal s, from infi_le_of_le t $ infi_le_of_le ht $ le_principal_iff.mpr hs lemma mem_lift_sets (hg : monotone g) {s : set β} : s ∈ (f.lift g).sets ↔ (∃t∈f.sets, s ∈ (g t).sets) := by rw [lift_sets_eq hg]; simp only [mem_Union_eq] lemma lift_le {f : filter α} {g : set α → filter β} {h : filter β} {s : set α} (hs : s ∈ f.sets) (hg : g s ≤ h) : f.lift g ≤ h := infi_le_of_le s $ infi_le_of_le hs $ hg lemma le_lift {f : filter α} {g : set α → filter β} {h : filter β} (hh : ∀s∈f.sets, h ≤ g s) : h ≤ f.lift g := le_infi $ assume s, le_infi $ assume hs, hh s hs lemma lift_mono (hf : f₁ ≤ f₂) (hg : g₁ ≤ g₂) : f₁.lift g₁ ≤ f₂.lift g₂ := infi_le_infi $ assume s, infi_le_infi2 $ assume hs, ⟨hf hs, hg s⟩ lemma lift_mono' (hg : ∀s∈f.sets, g₁ s ≤ g₂ s) : f.lift g₁ ≤ f.lift g₂ := infi_le_infi $ assume s, infi_le_infi $ assume hs, hg s hs lemma map_lift_eq {m : β → γ} (hg : monotone g) : map m (f.lift g) = f.lift (map m ∘ g) := have monotone (map m ∘ g), from monotone_comp hg monotone_map, filter_eq $ set.ext $ by simp [mem_lift_sets, hg, @mem_lift_sets _ _ f _ this] lemma vmap_lift_eq {m : γ → β} (hg : monotone g) : vmap m (f.lift g) = f.lift (vmap m ∘ g) := have monotone (vmap m ∘ g), from monotone_comp hg monotone_vmap, filter_eq $ set.ext begin simp only [hg, @mem_lift_sets _ _ f _ this, vmap, mem_lift_sets, mem_set_of_eq, exists_prop, function.comp_apply], exact λ s, ⟨λ ⟨b, ⟨a, ha, hb⟩, hs⟩, ⟨a, ha, b, hb, hs⟩, λ ⟨a, ha, b, hb, hs⟩, ⟨b, ⟨a, ha, hb⟩, hs⟩⟩ end theorem vmap_lift_eq2 {m : β → α} {g : set β → filter γ} (hg : monotone g) : (vmap m f).lift g = f.lift (g ∘ preimage m) := le_antisymm (le_infi $ assume s, le_infi $ assume hs, infi_le_of_le (preimage m s) $ infi_le _ ⟨s, hs, subset.refl _⟩) (le_infi $ assume s, le_infi $ assume ⟨s', hs', (h_sub : preimage m s' ⊆ s)⟩, infi_le_of_le s' $ infi_le_of_le hs' $ hg h_sub) lemma map_lift_eq2 {g : set β → filter γ} {m : α → β} (hg : monotone g) : (map m f).lift g = f.lift (g ∘ image m) := le_antisymm (infi_le_infi2 $ assume s, ⟨image m s, infi_le_infi2 $ assume hs, ⟨ f.upwards_sets hs $ assume a h, mem_image_of_mem _ h, le_refl _⟩⟩) (infi_le_infi2 $ assume t, ⟨preimage m t, infi_le_infi2 $ assume ht, ⟨ht, hg $ assume x, assume h : x ∈ m '' preimage m t, let ⟨y, hy, h_eq⟩ := h in show x ∈ t, from h_eq ▸ hy⟩⟩) lemma lift_comm {g : filter β} {h : set α → set β → filter γ} : f.lift (λs, g.lift (h s)) = g.lift (λt, f.lift (λs, h s t)) := le_antisymm (le_infi $ assume i, le_infi $ assume hi, le_infi $ assume j, le_infi $ assume hj, infi_le_of_le j $ infi_le_of_le hj $ infi_le_of_le i $ infi_le _ hi) (le_infi $ assume i, le_infi $ assume hi, le_infi $ assume j, le_infi $ assume hj, infi_le_of_le j $ infi_le_of_le hj $ infi_le_of_le i $ infi_le _ hi) lemma lift_assoc {h : set β → filter γ} (hg : monotone g) : (f.lift g).lift h = f.lift (λs, (g s).lift h) := le_antisymm (le_infi $ assume s, le_infi $ assume hs, le_infi $ assume t, le_infi $ assume ht, infi_le_of_le t $ infi_le _ $ (mem_lift_sets hg).mpr ⟨_, hs, ht⟩) (le_infi $ assume t, le_infi $ assume ht, let ⟨s, hs, h'⟩ := (mem_lift_sets hg).mp ht in infi_le_of_le s $ infi_le_of_le hs $ infi_le_of_le t $ infi_le _ h') lemma lift_lift_same_le_lift {g : set α → set α → filter β} : f.lift (λs, f.lift (g s)) ≤ f.lift (λs, g s s) := le_infi $ assume s, le_infi $ assume hs, infi_le_of_le s $ infi_le_of_le hs $ infi_le_of_le s $ infi_le _ hs lemma lift_lift_same_eq_lift {g : set α → set α → filter β} (hg₁ : ∀s, monotone (λt, g s t)) (hg₂ : ∀t, monotone (λs, g s t)): f.lift (λs, f.lift (g s)) = f.lift (λs, g s s) := le_antisymm lift_lift_same_le_lift (le_infi $ assume s, le_infi $ assume hs, le_infi $ assume t, le_infi $ assume ht, infi_le_of_le (s ∩ t) $ infi_le_of_le (inter_mem_sets hs ht) $ calc g (s ∩ t) (s ∩ t) ≤ g s (s ∩ t) : hg₂ (s ∩ t) (inter_subset_left _ _) ... ≤ g s t : hg₁ s (inter_subset_right _ _)) lemma lift_principal {s : set α} (hg : monotone g) : (principal s).lift g = g s := le_antisymm (infi_le_of_le s $ infi_le _ $ subset.refl _) (le_infi $ assume t, le_infi $ assume hi, hg hi) theorem monotone_lift [preorder γ] {f : γ → filter α} {g : γ → set α → filter β} (hf : monotone f) (hg : monotone g) : monotone (λc, (f c).lift (g c)) := assume a b h, lift_mono (hf h) (hg h) lemma lift_neq_bot_iff (hm : monotone g) : (f.lift g ≠ ⊥) ↔ (∀s∈f.sets, g s ≠ ⊥) := classical.by_cases (assume hn : nonempty β, calc f.lift g ≠ ⊥ ↔ (⨅s : { s // s ∈ f.sets}, g s.val) ≠ ⊥ : by simp [filter.lift, infi_subtype] ... ↔ (∀s:{ s // s ∈ f.sets}, g s.val ≠ ⊥) : infi_neq_bot_iff_of_directed hn (assume ⟨a, ha⟩ ⟨b, hb⟩, ⟨⟨a ∩ b, inter_mem_sets ha hb⟩, hm $ inter_subset_left _ _, hm $ inter_subset_right _ _⟩) ... ↔ (∀s∈f.sets, g s ≠ ⊥) : ⟨assume h s hs, h ⟨s, hs⟩, assume h ⟨s, hs⟩, h s hs⟩) (assume hn : ¬ nonempty β, have h₁ : f.lift g = ⊥, from filter_eq_bot_of_not_nonempty hn, have h₂ : ∀s, g s = ⊥, from assume s, filter_eq_bot_of_not_nonempty hn, calc (f.lift g ≠ ⊥) ↔ false : by simp [h₁] ... ↔ (∀s∈f.sets, false) : ⟨false.elim, assume h, h univ univ_mem_sets⟩ ... ↔ (∀s∈f.sets, g s ≠ ⊥) : by simp [h₂]) @[simp] lemma lift_const {f : filter α} {g : filter β} : f.lift (λx, g) = g := le_antisymm (lift_le univ_mem_sets $ le_refl g) (le_lift $ assume s hs, le_refl g) @[simp] lemma lift_inf {f : filter α} {g h : set α → filter β} : f.lift (λx, g x ⊓ h x) = f.lift g ⊓ f.lift h := by simp [filter.lift, infi_inf_eq] @[simp] lemma lift_principal2 {f : filter α} : f.lift principal = f := le_antisymm (assume s hs, mem_lift hs (mem_principal_self s)) (le_infi $ assume s, le_infi $ assume hs, by simp [hs]) lemma lift_infi {f : ι → filter α} {g : set α → filter β} (hι : nonempty ι) (hg : ∀{s t}, g s ⊓ g t = g (s ∩ t)) : (infi f).lift g = (⨅i, (f i).lift g) := le_antisymm (le_infi $ assume i, lift_mono (infi_le _ _) (le_refl _)) (assume s, have g_mono : monotone g, from assume s t h, le_of_inf_eq $ eq.trans hg $ congr_arg g $ inter_eq_self_of_subset_left h, have ∀t∈(infi f).sets, (⨅ (i : ι), filter.lift (f i) g) ≤ g t, from assume t ht, infi_sets_induct ht (let ⟨i⟩ := hι in infi_le_of_le i $ infi_le_of_le univ $ infi_le _ univ_mem_sets) (assume i s₁ s₂ hs₁ hs₂, @hg s₁ s₂ ▸ le_inf (infi_le_of_le i $ infi_le_of_le s₁ $ infi_le _ hs₁) hs₂) (assume s₁ s₂ hs₁ hs₂, le_trans hs₂ $ g_mono hs₁), begin rw [lift_sets_eq g_mono], simp only [mem_Union_eq, exists_imp_distrib], exact assume t ht hs, this t ht hs end) end lift section lift' /-- Specialize `lift` to functions `set α → set β`. This can be viewed as a generalization of `vmap`. -/ protected def lift' (f : filter α) (h : set α → set β) := f.lift (principal ∘ h) variables {f f₁ f₂ : filter α} {h h₁ h₂ : set α → set β} lemma mem_lift' {t : set α} (ht : t ∈ f.sets) : h t ∈ (f.lift' h).sets := le_principal_iff.mp $ show f.lift' h ≤ principal (h t), from infi_le_of_le t $ infi_le_of_le ht $ le_refl _ lemma mem_lift'_sets (hh : monotone h) {s : set β} : s ∈ (f.lift' h).sets ↔ (∃t∈f.sets, h t ⊆ s) := have monotone (principal ∘ h), from assume a b h, principal_mono.mpr $ hh h, by simp [filter.lift', @mem_lift_sets α β f _ this] lemma lift'_le {f : filter α} {g : set α → set β} {h : filter β} {s : set α} (hs : s ∈ f.sets) (hg : principal (g s) ≤ h) : f.lift' g ≤ h := lift_le hs hg lemma lift'_mono (hf : f₁ ≤ f₂) (hh : h₁ ≤ h₂) : f₁.lift' h₁ ≤ f₂.lift' h₂ := lift_mono hf $ assume s, principal_mono.mpr $ hh s lemma lift'_mono' (hh : ∀s∈f.sets, h₁ s ⊆ h₂ s) : f.lift' h₁ ≤ f.lift' h₂ := infi_le_infi $ assume s, infi_le_infi $ assume hs, principal_mono.mpr $ hh s hs lemma lift'_cong (hh : ∀s∈f.sets, h₁ s = h₂ s) : f.lift' h₁ = f.lift' h₂ := le_antisymm (lift'_mono' $ assume s hs, le_of_eq $ hh s hs) (lift'_mono' $ assume s hs, le_of_eq $ (hh s hs).symm) lemma map_lift'_eq {m : β → γ} (hh : monotone h) : map m (f.lift' h) = f.lift' (image m ∘ h) := calc map m (f.lift' h) = f.lift (map m ∘ principal ∘ h) : map_lift_eq $ monotone_comp hh monotone_principal ... = f.lift' (image m ∘ h) : by simp [function.comp, filter.lift'] lemma map_lift'_eq2 {g : set β → set γ} {m : α → β} (hg : monotone g) : (map m f).lift' g = f.lift' (g ∘ image m) := map_lift_eq2 $ monotone_comp hg monotone_principal theorem vmap_lift'_eq {m : γ → β} (hh : monotone h) : vmap m (f.lift' h) = f.lift' (preimage m ∘ h) := calc vmap m (f.lift' h) = f.lift (vmap m ∘ principal ∘ h) : vmap_lift_eq $ monotone_comp hh monotone_principal ... = f.lift' (preimage m ∘ h) : by simp [function.comp, filter.lift'] theorem vmap_lift'_eq2 {m : β → α} {g : set β → set γ} (hg : monotone g) : (vmap m f).lift' g = f.lift' (g ∘ preimage m) := vmap_lift_eq2 $ monotone_comp hg monotone_principal lemma lift'_principal {s : set α} (hh : monotone h) : (principal s).lift' h = principal (h s) := lift_principal $ monotone_comp hh monotone_principal lemma principal_le_lift' {t : set β} (hh : ∀s∈f.sets, t ⊆ h s) : principal t ≤ f.lift' h := le_infi $ assume s, le_infi $ assume hs, principal_mono.mpr (hh s hs) theorem monotone_lift' [preorder γ] {f : γ → filter α} {g : γ → set α → set β} (hf : monotone f) (hg : monotone g) : monotone (λc, (f c).lift' (g c)) := assume a b h, lift'_mono (hf h) (hg h) lemma lift_lift'_assoc {g : set α → set β} {h : set β → filter γ} (hg : monotone g) (hh : monotone h) : (f.lift' g).lift h = f.lift (λs, h (g s)) := calc (f.lift' g).lift h = f.lift (λs, (principal (g s)).lift h) : lift_assoc (monotone_comp hg monotone_principal) ... = f.lift (λs, h (g s)) : by simp [lift_principal, hh] lemma lift'_lift'_assoc {g : set α → set β} {h : set β → set γ} (hg : monotone g) (hh : monotone h) : (f.lift' g).lift' h = f.lift' (λs, h (g s)) := lift_lift'_assoc hg (monotone_comp hh monotone_principal) lemma lift'_lift_assoc {g : set α → filter β} {h : set β → set γ} (hg : monotone g) : (f.lift g).lift' h = f.lift (λs, (g s).lift' h) := lift_assoc hg lemma lift_lift'_same_le_lift' {g : set α → set α → set β} : f.lift (λs, f.lift' (g s)) ≤ f.lift' (λs, g s s) := lift_lift_same_le_lift lemma lift_lift'_same_eq_lift' {g : set α → set α → set β} (hg₁ : ∀s, monotone (λt, g s t)) (hg₂ : ∀t, monotone (λs, g s t)): f.lift (λs, f.lift' (g s)) = f.lift' (λs, g s s) := lift_lift_same_eq_lift (assume s, monotone_comp monotone_id $ monotone_comp (hg₁ s) monotone_principal) (assume t, monotone_comp (hg₂ t) monotone_principal) lemma lift'_inf_principal_eq {h : set α → set β} {s : set β} : f.lift' h ⊓ principal s = f.lift' (λt, h t ∩ s) := le_antisymm (le_infi $ assume t, le_infi $ assume ht, calc filter.lift' f h ⊓ principal s ≤ principal (h t) ⊓ principal s : inf_le_inf (infi_le_of_le t $ infi_le _ ht) (le_refl _) ... = _ : by simp) (le_inf (le_infi $ assume t, le_infi $ assume ht, infi_le_of_le t $ infi_le_of_le ht $ by simp; exact inter_subset_right _ _) (infi_le_of_le univ $ infi_le_of_le univ_mem_sets $ by simp; exact inter_subset_left _ _)) lemma lift'_neq_bot_iff (hh : monotone h) : (f.lift' h ≠ ⊥) ↔ (∀s∈f.sets, h s ≠ ∅) := calc (f.lift' h ≠ ⊥) ↔ (∀s∈f.sets, principal (h s) ≠ ⊥) : lift_neq_bot_iff (monotone_comp hh monotone_principal) ... ↔ (∀s∈f.sets, h s ≠ ∅) : by simp [principal_eq_bot_iff] @[simp] lemma lift'_id {f : filter α} : f.lift' id = f := lift_principal2 lemma le_lift' {f : filter α} {h : set α → set β} {g : filter β} (h_le : ∀s∈f.sets, h s ∈ g.sets) : g ≤ f.lift' h := le_infi $ assume s, le_infi $ assume hs, by simp [h_le]; exact h_le s hs lemma lift_infi' {f : ι → filter α} {g : set α → filter β} (hι : nonempty ι) (hf : directed (≤) f) (hg : monotone g) : (infi f).lift g = (⨅i, (f i).lift g) := le_antisymm (le_infi $ assume i, lift_mono (infi_le _ _) (le_refl _)) (assume s, begin rw [lift_sets_eq hg], simp only [mem_Union_eq, exists_imp_distrib, infi_sets_eq hf hι], exact assume t i ht hs, mem_infi_sets i $ mem_lift ht hs end) lemma lift'_infi {f : ι → filter α} {g : set α → set β} (hι : nonempty ι) (hg : ∀{s t}, g s ∩ g t = g (s ∩ t)) : (infi f).lift' g = (⨅i, (f i).lift' g) := lift_infi hι $ by simp; apply assume s t, hg theorem vmap_eq_lift' {f : filter β} {m : α → β} : vmap m f = f.lift' (preimage m) := filter_eq $ set.ext $ by simp [mem_lift'_sets, monotone_preimage, vmap] end lift' section prod variables {s : set α} {t : set β} {f : filter α} {g : filter β} /- The product filter cannot be defined using the monad structure on filters. For example: F := do {x <- seq, y <- top, return (x, y)} hence: s ∈ F <-> ∃n, [n..∞] × univ ⊆ s G := do {y <- top, x <- seq, return (x, y)} hence: s ∈ G <-> ∀i:ℕ, ∃n, [n..∞] × {i} ⊆ s Now ⋃i, [i..∞] × {i} is in G but not in F. As product filter we want to have F as result. -/ /-- Product of filters. This is the filter generated by cartesian products of elements of the component filters. -/ protected def prod (f : filter α) (g : filter β) : filter (α × β) := f.vmap prod.fst ⊓ g.vmap prod.snd lemma prod_mem_prod {s : set α} {t : set β} {f : filter α} {g : filter β} (hs : s ∈ f.sets) (ht : t ∈ g.sets) : set.prod s t ∈ (filter.prod f g).sets := inter_mem_inf_sets (preimage_mem_vmap hs) (preimage_mem_vmap ht) lemma mem_prod_iff {s : set (α×β)} {f : filter α} {g : filter β} : s ∈ (filter.prod f g).sets ↔ (∃t₁∈f.sets, ∃t₂∈g.sets, set.prod t₁ t₂ ⊆ s) := begin simp [filter.prod], split, exact assume ⟨t₁, ⟨s₁, hs₁, hts₁⟩, t₂, ⟨s₂, hs₂, hts₂⟩, h⟩, ⟨s₁, hs₁, s₂, hs₂, subset.trans (inter_subset_inter hts₁ hts₂) h⟩, exact assume ⟨t₁, ht₁, t₂, ht₂, h⟩, ⟨prod.fst ⁻¹' t₁, ⟨t₁, ht₁, subset.refl _⟩, prod.snd ⁻¹' t₂, ⟨t₂, ht₂, subset.refl _⟩, h⟩ end lemma tendsto_fst {f : filter α} {g : filter β} : tendsto prod.fst (filter.prod f g) f := tendsto_inf_left tendsto_vmap lemma tendsto_snd {f : filter α} {g : filter β} : tendsto prod.snd (filter.prod f g) g := tendsto_inf_right tendsto_vmap lemma tendsto.prod_mk {f : filter α} {g : filter β} {h : filter γ} {m₁ : α → β} {m₂ : α → γ} (h₁ : tendsto m₁ f g) (h₂ : tendsto m₂ f h) : tendsto (λx, (m₁ x, m₂ x)) f (filter.prod g h) := tendsto_inf.2 ⟨tendsto_vmap_iff.2 h₁, tendsto_vmap_iff.2 h₂⟩ lemma prod_infi_left {f : ι → filter α} {g : filter β} (i : ι) : filter.prod (⨅i, f i) g = (⨅i, filter.prod (f i) g) := by rw [filter.prod, vmap_infi, infi_inf i]; simp [filter.prod] lemma prod_infi_right {f : filter α} {g : ι → filter β} (i : ι) : filter.prod f (⨅i, g i) = (⨅i, filter.prod f (g i)) := by rw [filter.prod, vmap_infi, inf_infi i]; simp [filter.prod] lemma prod_mono {f₁ f₂ : filter α} {g₁ g₂ : filter β} (hf : f₁ ≤ f₂) (hg : g₁ ≤ g₂) : filter.prod f₁ g₁ ≤ filter.prod f₂ g₂ := inf_le_inf (vmap_mono hf) (vmap_mono hg) lemma prod_vmap_vmap_eq {α₁ : Type u} {α₂ : Type v} {β₁ : Type w} {β₂ : Type x} {f₁ : filter α₁} {f₂ : filter α₂} {m₁ : β₁ → α₁} {m₂ : β₂ → α₂} : filter.prod (vmap m₁ f₁) (vmap m₂ f₂) = vmap (λp:β₁×β₂, (m₁ p.1, m₂ p.2)) (filter.prod f₁ f₂) := by simp [filter.prod, vmap_vmap_comp] lemma prod_comm' : filter.prod f g = vmap (prod.swap) (filter.prod g f) := by simp [filter.prod, vmap_vmap_comp, function.comp, inf_comm] lemma prod_comm : filter.prod f g = map (λp:β×α, (p.2, p.1)) (filter.prod g f) := by rw [prod_comm', ← map_swap_eq_vmap_swap]; refl lemma prod_map_map_eq {α₁ : Type u} {α₂ : Type v} {β₁ : Type w} {β₂ : Type x} {f₁ : filter α₁} {f₂ : filter α₂} {m₁ : α₁ → β₁} {m₂ : α₂ → β₂} : filter.prod (map m₁ f₁) (map m₂ f₂) = map (λp:α₁×α₂, (m₁ p.1, m₂ p.2)) (filter.prod f₁ f₂) := le_antisymm (assume s hs, let ⟨s₁, hs₁, s₂, hs₂, h⟩ := mem_prod_iff.mp hs in filter.upwards_sets _ (prod_mem_prod (image_mem_map hs₁) (image_mem_map hs₂)) $ calc set.prod (m₁ '' s₁) (m₂ '' s₂) = (λp:α₁×α₂, (m₁ p.1, m₂ p.2)) '' set.prod s₁ s₂ : set.prod_image_image_eq ... ⊆ _ : by rwa [image_subset_iff]) ((tendsto_fst.comp (le_refl _)).prod_mk (tendsto_snd.comp (le_refl _))) lemma prod_inf_prod {f₁ f₂ : filter α} {g₁ g₂ : filter β} : filter.prod f₁ g₁ ⊓ filter.prod f₂ g₂ = filter.prod (f₁ ⊓ f₂) (g₁ ⊓ g₂) := by simp only [filter.prod, vmap_inf, inf_comm, inf_assoc, lattice.inf_left_comm] @[simp] lemma prod_bot1 {f : filter α} : filter.prod f (⊥ : filter β) = ⊥ := by simp [filter.prod] @[simp] lemma prod_bot2 {g : filter β} : filter.prod (⊥ : filter α) g = ⊥ := by simp [filter.prod] @[simp] lemma prod_principal_principal {s : set α} {t : set β} : filter.prod (principal s) (principal t) = principal (set.prod s t) := by simp [filter.prod, vmap_principal]; refl lemma prod_def {f : filter α} {g : filter β} : f.prod g = (f.lift $ λs, g.lift' $ set.prod s) := have ∀(s:set α) (t : set β), principal (set.prod s t) = (principal s).vmap prod.fst ⊓ (principal t).vmap prod.snd, by simp; intros; refl, begin simp [filter.lift', function.comp, this, -vmap_principal, lift_inf], rw [← vmap_lift_eq monotone_principal, ← vmap_lift_eq monotone_principal], simp [filter.prod] end lemma prod_same_eq : filter.prod f f = f.lift' (λt, set.prod t t) := by rw [prod_def]; from lift_lift'_same_eq_lift' (assume s, set.monotone_prod monotone_const monotone_id) (assume t, set.monotone_prod monotone_id monotone_const) lemma mem_prod_same_iff {s : set (α×α)} : s ∈ (filter.prod f f).sets ↔ (∃t∈f.sets, set.prod t t ⊆ s) := by rw [prod_same_eq, mem_lift'_sets]; exact set.monotone_prod monotone_id monotone_id lemma prod_lift_lift {α₁ : Type u} {α₂ : Type v} {β₁ : Type w} {β₂ : Type x} {f₁ : filter α₁} {f₂ : filter α₂} {g₁ : set α₁ → filter β₁} {g₂ : set α₂ → filter β₂} (hg₁ : monotone g₁) (hg₂ : monotone g₂) : filter.prod (f₁.lift g₁) (f₂.lift g₂) = f₁.lift (λs, f₂.lift (λt, filter.prod (g₁ s) (g₂ t))) := begin simp only [prod_def], rw [lift_assoc], apply congr_arg, funext x, rw [lift_comm], apply congr_arg, funext y, rw [lift'_lift_assoc], exact hg₂, exact hg₁ end lemma prod_lift'_lift' {α₁ : Type u} {α₂ : Type v} {β₁ : Type w} {β₂ : Type x} {f₁ : filter α₁} {f₂ : filter α₂} {g₁ : set α₁ → set β₁} {g₂ : set α₂ → set β₂} (hg₁ : monotone g₁) (hg₂ : monotone g₂) : filter.prod (f₁.lift' g₁) (f₂.lift' g₂) = f₁.lift (λs, f₂.lift' (λt, set.prod (g₁ s) (g₂ t))) := begin rw [prod_def, lift_lift'_assoc], apply congr_arg, funext x, rw [lift'_lift'_assoc], exact hg₂, exact set.monotone_prod monotone_const monotone_id, exact hg₁, exact (monotone_lift' monotone_const $ monotone_lam $ assume x, set.monotone_prod monotone_id monotone_const) end lemma prod_neq_bot {f : filter α} {g : filter β} : filter.prod f g ≠ ⊥ ↔ (f ≠ ⊥ ∧ g ≠ ⊥) := calc filter.prod f g ≠ ⊥ ↔ (∀s∈f.sets, g.lift' (set.prod s) ≠ ⊥) : begin rw [prod_def, lift_neq_bot_iff], exact (monotone_lift' monotone_const $ monotone_lam $ assume s, set.monotone_prod monotone_id monotone_const) end ... ↔ (∀s∈f.sets, ∀t∈g.sets, s ≠ ∅ ∧ t ≠ ∅) : begin apply forall_congr, intro s, apply forall_congr, intro hs, rw [lift'_neq_bot_iff], apply forall_congr, intro t, apply forall_congr, intro ht, rw [set.prod_neq_empty_iff], exact set.monotone_prod monotone_const monotone_id end ... ↔ (∀s∈f.sets, s ≠ ∅) ∧ (∀t∈g.sets, t ≠ ∅) : ⟨assume h, ⟨assume s hs, (h s hs univ univ_mem_sets).left, assume t ht, (h univ univ_mem_sets t ht).right⟩, assume ⟨h₁, h₂⟩ s hs t ht, ⟨h₁ s hs, h₂ t ht⟩⟩ ... ↔ _ : by simp only [forall_sets_neq_empty_iff_neq_bot] end prod /- at_top and at_bot -/ /-- `at_top` is the filter representing the limit `→ ∞` on an ordered set. It is generated by the collection of up-sets `{b | a ≤ b}`. (The preorder need not have a top element for this to be well defined, and indeed is trivial when a top element exists.) -/ def at_top [preorder α] : filter α := ⨅ a, principal {b | a ≤ b} /-- `at_bot` is the filter representing the limit `→ -∞` on an ordered set. It is generated by the collection of down-sets `{b | b ≤ a}`. (The preorder need not have a bottom element for this to be well defined, and indeed is trivial when a bottom element exists.) -/ def at_bot [preorder α] : filter α := ⨅ a, principal {b | b ≤ a} lemma mem_at_top [preorder α] (a : α) : {b : α | a ≤ b} ∈ (@at_top α _).sets := mem_infi_sets a $ subset.refl _ @[simp] lemma at_top_ne_bot [inhabited α] [semilattice_sup α] : (at_top : filter α) ≠ ⊥ := infi_neq_bot_of_directed (by apply_instance) (assume a b, ⟨a ⊔ b, by simp {contextual := tt}⟩) (assume a, by simp [principal_eq_bot_iff]; exact ne_empty_of_mem (le_refl a)) @[simp] lemma mem_at_top_sets [inhabited α] [semilattice_sup α] {s : set α} : s ∈ (at_top : filter α).sets ↔ ∃a:α, ∀b≥a, b ∈ s := iff.intro (assume h, infi_sets_induct h ⟨default α, by simp⟩ (assume a s₁ s₂ ha ⟨b, hb⟩, ⟨a ⊔ b, assume c hc, ⟨ha $ le_trans le_sup_left hc, hb _ $ le_trans le_sup_right hc⟩⟩) (assume s₁ s₂ h ⟨a, ha⟩, ⟨a, assume b hb, h $ ha _ hb⟩)) (assume ⟨a, h⟩, mem_infi_sets a $ assume x, h x) lemma map_at_top_eq [inhabited α] [semilattice_sup α] {f : α → β} : at_top.map f = (⨅a, principal $ f '' {a' | a ≤ a'}) := calc map f (⨅a, principal {a' | a ≤ a'}) = (⨅a, map f $ principal {a' | a ≤ a'}) : map_infi_eq (assume a b, ⟨a ⊔ b, by simp {contextual := tt}⟩) ⟨default α⟩ ... = (⨅a, principal $ f '' {a' | a ≤ a'}) : by simp lemma tendsto_finset_image_at_top_at_top {i : β → γ} {j : γ → β} (h : ∀x, j (i x) = x) : tendsto (λs:finset γ, s.image j) at_top at_top := tendsto_infi.2 $ assume s, tendsto_infi' (s.image i) $ tendsto_principal_principal.2 $ assume t (ht : s.image i ⊆ t), calc s = (s.image i).image j : by simp [finset.image_image, (∘), h]; exact finset.image_id.symm ... ⊆ t.image j : finset.image_subset_image ht /- ultrafilter -/ section ultrafilter open zorn variables {f g : filter α} /-- An ultrafilter is a minimal (maximal in the set order) proper filter. -/ def ultrafilter (f : filter α) := f ≠ ⊥ ∧ ∀g, g ≠ ⊥ → g ≤ f → f ≤ g lemma ultrafilter_pure {a : α} : ultrafilter (pure a) := ⟨pure_neq_bot, assume g hg ha, have {a} ∈ g.sets, begin simp at ha, assumption end, show ∀s∈g.sets, {a} ⊆ s, from classical.by_contradiction $ begin simp only [classical.not_forall, not_imp, exists_imp_distrib, singleton_subset_iff], exact assume s ⟨hs, hna⟩, have {a} ∩ s ∈ g.sets, from inter_mem_sets ‹{a} ∈ g.sets› hs, have ∅ ∈ g.sets, from g.upwards_sets this $ assume x ⟨hxa, hxs⟩, begin simp at hxa; simp [hxa] at hxs, exact hna hxs end, have g = ⊥, from empty_in_sets_eq_bot.mp this, hg this end⟩ lemma ultrafilter_unique (hg : ultrafilter g) (hf : f ≠ ⊥) (h : f ≤ g) : f = g := le_antisymm h (hg.right _ hf h) lemma exists_ultrafilter (h : f ≠ ⊥) : ∃u, u ≤ f ∧ ultrafilter u := let τ := {f' // f' ≠ ⊥ ∧ f' ≤ f}, r : τ → τ → Prop := λt₁ t₂, t₂.val ≤ t₁.val, ⟨a, ha⟩ := inhabited_of_mem_sets h univ_mem_sets, top : τ := ⟨f, h, le_refl f⟩, sup : Π(c:set τ), chain r c → τ := λc hc, ⟨⨅a:{a:τ // a ∈ insert top c}, a.val.val, infi_neq_bot_of_directed ⟨a⟩ (directed_of_chain $ chain_insert hc $ assume ⟨b, _, hb⟩ _ _, or.inl hb) (assume ⟨⟨a, ha, _⟩, _⟩, ha), infi_le_of_le ⟨top, mem_insert _ _⟩ (le_refl _)⟩ in have ∀c (hc: chain r c) a (ha : a ∈ c), r a (sup c hc), from assume c hc a ha, infi_le_of_le ⟨a, mem_insert_of_mem _ ha⟩ (le_refl _), have (∃ (u : τ), ∀ (a : τ), r u a → r a u), from zorn (assume c hc, ⟨sup c hc, this c hc⟩) (assume f₁ f₂ f₃ h₁ h₂, le_trans h₂ h₁), let ⟨uτ, hmin⟩ := this in ⟨uτ.val, uτ.property.right, uτ.property.left, assume g hg₁ hg₂, hmin ⟨g, hg₁, le_trans hg₂ uτ.property.right⟩ hg₂⟩ lemma le_of_ultrafilter {g : filter α} (hf : ultrafilter f) (h : f ⊓ g ≠ ⊥) : f ≤ g := le_of_inf_eq $ ultrafilter_unique hf h inf_le_left lemma mem_or_compl_mem_of_ultrafilter (hf : ultrafilter f) (s : set α) : s ∈ f.sets ∨ - s ∈ f.sets := classical.or_iff_not_imp_right.2 $ assume : - s ∉ f.sets, have f ≤ principal s, from le_of_ultrafilter hf $ assume h, this $ mem_sets_of_neq_bot $ by simp [*], by simp at this; assumption lemma mem_or_mem_of_ultrafilter {s t : set α} (hf : ultrafilter f) (h : s ∪ t ∈ f.sets) : s ∈ f.sets ∨ t ∈ f.sets := (mem_or_compl_mem_of_ultrafilter hf s).imp_right (assume : -s ∈ f.sets, by filter_upwards [this, h] assume x hnx hx, hx.resolve_left hnx) lemma mem_of_finite_sUnion_ultrafilter {s : set (set α)} (hf : ultrafilter f) (hs : finite s) : ⋃₀ s ∈ f.sets → ∃t∈s, t ∈ f.sets := finite.induction_on hs (by simp [empty_in_sets_eq_bot, hf.left]) $ λ t s' ht' hs' ih, by simp; exact assume h, (mem_or_mem_of_ultrafilter hf h).elim (assume : t ∈ f.sets, ⟨t, or.inl rfl, this⟩) (assume h, let ⟨t, hts', ht⟩ := ih h in ⟨t, or.inr hts', ht⟩) lemma mem_of_finite_Union_ultrafilter {is : set β} {s : β → set α} (hf : ultrafilter f) (his : finite is) (h : (⋃i∈is, s i) ∈ f.sets) : ∃i∈is, s i ∈ f.sets := have his : finite (image s is), from finite_image s his, have h : (⋃₀ image s is) ∈ f.sets, from by simp [sUnion_image]; assumption, let ⟨t, ⟨i, hi, h_eq⟩, (ht : t ∈ f.sets)⟩ := mem_of_finite_sUnion_ultrafilter hf his h in ⟨i, hi, h_eq.symm ▸ ht⟩ lemma ultrafilter_of_split {f : filter α} (hf : f ≠ ⊥) (h : ∀s, s ∈ f.sets ∨ - s ∈ f.sets) : ultrafilter f := ⟨hf, assume g hg g_le s hs, (h s).elim id $ assume : - s ∈ f.sets, have s ∩ -s ∈ g.sets, from inter_mem_sets hs (g_le this), by simp [empty_in_sets_eq_bot, hg] at this; contradiction⟩ lemma ultrafilter_map {f : filter α} {m : α → β} (h : ultrafilter f) : ultrafilter (map m f) := ultrafilter_of_split (by simp [map_eq_bot_iff, h.left]) $ assume s, show preimage m s ∈ f.sets ∨ - preimage m s ∈ f.sets, from mem_or_compl_mem_of_ultrafilter h (preimage m s) /-- Construct an ultrafilter extending a given filter. The ultrafilter lemma is the assertion that such a filter exists; we use the axiom of choice to pick one. -/ noncomputable def ultrafilter_of (f : filter α) : filter α := if h : f = ⊥ then ⊥ else epsilon (λu, u ≤ f ∧ ultrafilter u) lemma ultrafilter_of_spec (h : f ≠ ⊥) : ultrafilter_of f ≤ f ∧ ultrafilter (ultrafilter_of f) := begin have h' := epsilon_spec (exists_ultrafilter h), simp [ultrafilter_of, dif_neg, h], simp at h', assumption end lemma ultrafilter_of_le : ultrafilter_of f ≤ f := if h : f = ⊥ then by simp [ultrafilter_of, dif_pos, h]; exact le_refl _ else (ultrafilter_of_spec h).left lemma ultrafilter_ultrafilter_of (h : f ≠ ⊥) : ultrafilter (ultrafilter_of f) := (ultrafilter_of_spec h).right lemma ultrafilter_of_ultrafilter (h : ultrafilter f) : ultrafilter_of f = f := ultrafilter_unique h (ultrafilter_ultrafilter_of h.left).left ultrafilter_of_le end ultrafilter end filter
beadea16a7adaa6be21d42e972a9057536c0f9d4
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/data/finset/sym.lean
b023650775f22c71678dbf17684dba9eee892915
[ "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
7,166
lean
/- Copyright (c) 2021 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import data.finset.lattice import data.fintype.prod import data.fintype.vector import data.sym.sym2 /-! # Symmetric powers of a finset This file defines the symmetric powers of a finset as `finset (sym α n)` and `finset (sym2 α)`. ## Main declarations * `finset.sym`: The symmetric power of a finset. `s.sym n` is all the multisets of cardinality `n` whose elements are in `s`. * `finset.sym2`: The symmetric square of a finset. `s.sym2` is all the pairs whose elements are in `s`. ## TODO `finset.sym` forms a Galois connection between `finset α` and `finset (sym α n)`. Similar for `finset.sym2`. -/ namespace finset variables {α : Type*} [decidable_eq α] {s t : finset α} {a b : α} lemma is_diag_mk_of_mem_diag {a : α × α} (h : a ∈ s.diag) : sym2.is_diag ⟦a⟧ := (sym2.is_diag_iff_proj_eq _).2 (mem_diag.1 h).2 lemma not_is_diag_mk_of_mem_off_diag {a : α × α} (h : a ∈ s.off_diag) : ¬ sym2.is_diag ⟦a⟧ := by { rw sym2.is_diag_iff_proj_eq, exact (mem_off_diag.1 h).2.2 } section sym2 variables {m : sym2 α} /-- Lifts a finset to `sym2 α`. `s.sym2` is the finset of all pairs with elements in `s`. -/ protected def sym2 (s : finset α) : finset (sym2 α) := (s ×ˢ s).image quotient.mk @[simp] lemma mem_sym2_iff : m ∈ s.sym2 ↔ ∀ a ∈ m, a ∈ s := begin refine mem_image.trans ⟨_, λ h, ⟨m.out, mem_product.2 ⟨h _ m.out_fst_mem, h _ m.out_snd_mem⟩, m.out_eq⟩⟩, rintro ⟨⟨a, b⟩, h, rfl⟩, rw sym2.ball, rwa mem_product at h, end lemma mk_mem_sym2_iff : ⟦(a, b)⟧ ∈ s.sym2 ↔ a ∈ s ∧ b ∈ s := by rw [mem_sym2_iff, sym2.ball] @[simp] lemma sym2_empty : (∅ : finset α).sym2 = ∅ := rfl @[simp] lemma sym2_eq_empty : s.sym2 = ∅ ↔ s = ∅ := by rw [finset.sym2, image_eq_empty, product_eq_empty, or_self] @[simp] lemma sym2_nonempty : s.sym2.nonempty ↔ s.nonempty := by rw [finset.sym2, nonempty.image_iff, nonempty_product, and_self] alias sym2_nonempty ↔ _ nonempty.sym2 attribute [protected] nonempty.sym2 @[simp] lemma sym2_univ [fintype α] : (univ : finset α).sym2 = univ := rfl @[simp] lemma sym2_singleton (a : α) : ({a} : finset α).sym2 = {sym2.diag a} := by rw [finset.sym2, singleton_product_singleton, image_singleton, sym2.diag] @[simp] lemma diag_mem_sym2_iff : sym2.diag a ∈ s.sym2 ↔ a ∈ s := mk_mem_sym2_iff.trans $ and_self _ @[simp] lemma sym2_mono (h : s ⊆ t) : s.sym2 ⊆ t.sym2 := λ m he, mem_sym2_iff.2 $ λ a ha, h $ mem_sym2_iff.1 he _ ha lemma image_diag_union_image_off_diag : s.diag.image quotient.mk ∪ s.off_diag.image quotient.mk = s.sym2 := by { rw [←image_union, diag_union_off_diag], refl } end sym2 section sym variables {n : ℕ} {m : sym α n} /-- Lifts a finset to `sym α n`. `s.sym n` is the finset of all unordered tuples of cardinality `n` with elements in `s`. -/ protected def sym (s : finset α) : Π n, finset (sym α n) | 0 := {∅} | (n + 1) := s.sup $ λ a, (sym n).image $ _root_.sym.cons a @[simp] lemma sym_zero : s.sym 0 = {∅} := rfl @[simp] lemma sym_succ : s.sym (n + 1) = s.sup (λ a, (s.sym n).image $ sym.cons a) := rfl @[simp] lemma mem_sym_iff : m ∈ s.sym n ↔ ∀ a ∈ m, a ∈ s := begin induction n with n ih, { refine mem_singleton.trans ⟨_, λ _, sym.eq_nil_of_card_zero _⟩, rintro rfl, exact λ a ha, ha.elim }, refine mem_sup.trans ⟨_, λ h, _⟩, { rintro ⟨a, ha, he⟩ b hb, rw mem_image at he, obtain ⟨m, he, rfl⟩ := he, rw sym.mem_cons at hb, obtain rfl | hb := hb, { exact ha }, { exact ih.1 he _ hb } }, { obtain ⟨a, m, rfl⟩ := m.exists_eq_cons_of_succ, exact ⟨a, h _ $ sym.mem_cons_self _ _, mem_image_of_mem _ $ ih.2 $ λ b hb, h _ $ sym.mem_cons_of_mem hb⟩ } end @[simp] lemma sym_empty (n : ℕ) : (∅ : finset α).sym (n + 1) = ∅ := rfl lemma repeat_mem_sym (ha : a ∈ s) (n : ℕ) : sym.repeat a n ∈ s.sym n := mem_sym_iff.2 $ λ b hb, by rwa (sym.mem_repeat.1 hb).2 protected lemma nonempty.sym (h : s.nonempty) (n : ℕ) : (s.sym n).nonempty := let ⟨a, ha⟩ := h in ⟨_, repeat_mem_sym ha n⟩ @[simp] lemma sym_singleton (a : α) (n : ℕ) : ({a} : finset α).sym n = {sym.repeat a n} := eq_singleton_iff_nonempty_unique_mem.2 ⟨(singleton_nonempty _).sym n, λ s hs, sym.eq_repeat_iff.2 $ λ b hb, eq_of_mem_singleton $ mem_sym_iff.1 hs _ hb⟩ lemma eq_empty_of_sym_eq_empty (h : s.sym n = ∅) : s = ∅ := begin rw ←not_nonempty_iff_eq_empty at ⊢ h, exact λ hs, h (hs.sym _), end @[simp] lemma sym_eq_empty : s.sym n = ∅ ↔ n ≠ 0 ∧ s = ∅ := begin cases n, { exact iff_of_false (singleton_ne_empty _) (λ h, (h.1 rfl).elim) }, { refine ⟨λ h, ⟨n.succ_ne_zero, eq_empty_of_sym_eq_empty h⟩, _⟩, rintro ⟨_, rfl⟩, exact sym_empty _ } end @[simp] lemma sym_nonempty : (s.sym n).nonempty ↔ n = 0 ∨ s.nonempty := by simp_rw [nonempty_iff_ne_empty, ne.def, sym_eq_empty, not_and_distrib, not_ne_iff] alias sym2_nonempty ↔ _ nonempty.sym2 attribute [protected] nonempty.sym2 @[simp] lemma sym_univ [fintype α] (n : ℕ) : (univ : finset α).sym n = univ := eq_univ_iff_forall.2 $ λ s, mem_sym_iff.2 $ λ a _, mem_univ _ @[simp] lemma sym_mono (h : s ⊆ t) (n : ℕ): s.sym n ⊆ t.sym n := λ m hm, mem_sym_iff.2 $ λ a ha, h $ mem_sym_iff.1 hm _ ha @[simp] lemma sym_inter (s t : finset α) (n : ℕ) : (s ∩ t).sym n = s.sym n ∩ t.sym n := by { ext m, simp only [mem_inter, mem_sym_iff, imp_and_distrib, forall_and_distrib] } @[simp] lemma sym_union (s t : finset α) (n : ℕ) : s.sym n ∪ t.sym n ⊆ (s ∪ t).sym n := union_subset (sym_mono (subset_union_left s t) n) (sym_mono (subset_union_right s t) n) lemma sym_fill_mem (a : α) {i : fin (n + 1)} {m : sym α (n - i)} (h : m ∈ s.sym (n - i)) : m.fill a i ∈ (insert a s).sym n := mem_sym_iff.2 $ λ b hb, mem_insert.2 $ (sym.mem_fill_iff.1 hb).imp and.right $ mem_sym_iff.1 h b lemma sym_filter_ne_mem (a : α) (h : m ∈ s.sym n) : (m.filter_ne a).2 ∈ (s.erase a).sym (n - (m.filter_ne a).1) := mem_sym_iff.2 $ λ b H, mem_erase.2 $ (multiset.mem_filter.1 H).symm.imp ne.symm $ mem_sym_iff.1 h b /-- If `a` does not belong to the finset `s`, then the `n`th symmetric power of `{a} ∪ s` is in 1-1 correspondence with the disjoint union of the `n - i`th symmetric powers of `s`, for `0 ≤ i ≤ n`. -/ @[simps] def sym_insert_equiv (h : a ∉ s) : (insert a s).sym n ≃ Σ i : fin (n + 1), s.sym (n - i) := { to_fun := λ m, ⟨_, (m.1.filter_ne a).2, by convert sym_filter_ne_mem a m.2; rw erase_insert h⟩, inv_fun := λ m, ⟨m.2.1.fill a m.1, sym_fill_mem a m.2.2⟩, left_inv := λ m, subtype.ext $ m.1.fill_filter_ne a, right_inv := λ ⟨i, m, hm⟩, begin refine (_ : id.injective).sigma_map (λ i, _) _, { exact λ i, sym α (n - i) }, swap, { exact λ _ _, id }, swap, { exact subtype.coe_injective }, refine eq.trans _ (sym.filter_ne_fill a _ _), exacts [rfl, h ∘ mem_sym_iff.1 hm a], end } end sym end finset
60f24ed170ca333d8a7a379ab2756d926b831de9
9dc8cecdf3c4634764a18254e94d43da07142918
/src/group_theory/free_product.lean
89c1f0989563089fd985c1e9b77f449972598417
[ "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
33,835
lean
/- Copyright (c) 2021 David Wärn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: David Wärn, Joachim Breitner -/ import algebra.free_monoid import group_theory.congruence import group_theory.is_free_group import group_theory.subgroup.pointwise import data.list.chain import set_theory.cardinal.ordinal /-! # The free product of groups or monoids Given an `ι`-indexed family `M` of monoids, we define their free product (categorical coproduct) `free_product M`. When `ι` and all `M i` have decidable equality, the free product bijects with the type `word M` of reduced words. This bijection is constructed by defining an action of `free_product M` on `word M`. When `M i` are all groups, `free_product M` is also a group (and the coproduct in the category of groups). ## Main definitions - `free_product M`: the free product, defined as a quotient of a free monoid. - `free_product.of {i} : M i →* free_product M`. - `free_product.lift : (Π {i}, M i →* N) ≃ (free_product M →* N)`: the universal property. - `free_product.word M`: the type of reduced words. - `free_product.word.equiv M : free_product M ≃ word M`. - `free_product.neword M i j`: an inductive description of non-empty words with first letter from `M i` and last letter from `M j`, together with an API (`singleton`, `append`, `head`, `tail`, `to_word`, `prod`, `inv`). Used in the proof of the Ping-Pong-lemma. - `free_product.lift_injective_of_ping_pong`: The Ping-Pong-lemma, proving injectivity of the `lift`. See the documentation of that theorem for more information. ## Remarks There are many answers to the question "what is the free product of a family `M` of monoids?", and they are all equivalent but not obviously equivalent. We provide two answers. The first, almost tautological answer is given by `free_product M`, which is a quotient of the type of words in the alphabet `Σ i, M i`. It's straightforward to define and easy to prove its universal property. But this answer is not completely satisfactory, because it's difficult to tell when two elements `x y : free_product M` are distinct since `free_product M` is defined as a quotient. The second, maximally efficient answer is given by `word M`. An element of `word M` is a word in the alphabet `Σ i, M i`, where the letter `⟨i, 1⟩` doesn't occur and no adjacent letters share an index `i`. Since we only work with reduced words, there is no need for quotienting, and it is easy to tell when two elements are distinct. However it's not obvious that this is even a monoid! We prove that every element of `free_product M` can be represented by a unique reduced word, i.e. `free_product M` and `word M` are equivalent types. This means that `word M` can be given a monoid structure, and it lets us tell when two elements of `free_product M` are distinct. There is also a completely tautological, maximally inefficient answer given by `algebra.category.Mon.colimits`. Whereas `free_product M` at least ensures that (any instance of) associativity holds by reflexivity, in this answer associativity holds because of quotienting. Yet another answer, which is constructively more satisfying, could be obtained by showing that `free_product.rel` is confluent. ## References [van der Waerden, *Free products of groups*][MR25465] -/ open set variables {ι : Type*} (M : Π i : ι, Type*) [Π i, monoid (M i)] /-- A relation on the free monoid on alphabet `Σ i, M i`, relating `⟨i, 1⟩` with `1` and `⟨i, x⟩ * ⟨i, y⟩` with `⟨i, x * y⟩`. -/ inductive free_product.rel : free_monoid (Σ i, M i) → free_monoid (Σ i, M i) → Prop | of_one (i : ι) : free_product.rel (free_monoid.of ⟨i, 1⟩) 1 | of_mul {i : ι} (x y : M i) : free_product.rel (free_monoid.of ⟨i, x⟩ * free_monoid.of ⟨i, y⟩) (free_monoid.of ⟨i, x * y⟩) /-- The free product (categorical coproduct) of an indexed family of monoids. -/ @[derive [monoid, inhabited]] def free_product : Type* := (con_gen (free_product.rel M)).quotient namespace free_product /-- The type of reduced words. A reduced word cannot contain a letter `1`, and no two adjacent letters can come from the same summand. -/ @[ext] structure word := (to_list : list (Σ i, M i)) (ne_one : ∀ l ∈ to_list, sigma.snd l ≠ 1) (chain_ne : to_list.chain' (λ l l', sigma.fst l ≠ sigma.fst l')) variable {M} /-- The inclusion of a summand into the free product. -/ def of {i : ι} : M i →* free_product M := { to_fun := λ x, con.mk' _ (free_monoid.of $ sigma.mk i x), map_one' := (con.eq _).mpr (con_gen.rel.of _ _ (free_product.rel.of_one i)), map_mul' := λ x y, eq.symm $ (con.eq _).mpr (con_gen.rel.of _ _ (free_product.rel.of_mul x y)) } lemma of_apply {i} (m : M i) : of m = con.mk' _ (free_monoid.of $ sigma.mk i m) := rfl variables {N : Type*} [monoid N] /-- See note [partially-applied ext lemmas]. -/ @[ext] lemma ext_hom (f g : free_product M →* N) (h : ∀ i, f.comp (of : M i →* _) = g.comp of) : f = g := (monoid_hom.cancel_right con.mk'_surjective).mp $ free_monoid.hom_eq $ λ ⟨i, x⟩, by rw [monoid_hom.comp_apply, monoid_hom.comp_apply, ←of_apply, ←monoid_hom.comp_apply, ←monoid_hom.comp_apply, h] /-- A map out of the free product corresponds to a family of maps out of the summands. This is the universal property of the free product, charaterizing it as a categorical coproduct. -/ @[simps symm_apply] def lift : (Π i, M i →* N) ≃ (free_product M →* N) := { to_fun := λ fi, con.lift _ (free_monoid.lift $ λ p : Σ i, M i, fi p.fst p.snd) $ con.con_gen_le begin simp_rw [con.rel_eq_coe, con.ker_rel], rintros _ _ (i | ⟨i, x, y⟩), { change free_monoid.lift _ (free_monoid.of _) = free_monoid.lift _ 1, simp only [monoid_hom.map_one, free_monoid.lift_eval_of], }, { change free_monoid.lift _ (free_monoid.of _ * free_monoid.of _) = free_monoid.lift _ (free_monoid.of _), simp only [monoid_hom.map_mul, free_monoid.lift_eval_of], } end, inv_fun := λ f i, f.comp of, left_inv := by { intro fi, ext i x, rw [monoid_hom.comp_apply, of_apply, con.lift_mk', free_monoid.lift_eval_of], }, right_inv := by { intro f, ext i x, simp only [monoid_hom.comp_apply, of_apply, con.lift_mk', free_monoid.lift_eval_of], } } @[simp] lemma lift_of {N} [monoid N] (fi : Π i, M i →* N) {i} (m : M i) : lift fi (of m) = fi i m := by conv_rhs { rw [←lift.symm_apply_apply fi, lift_symm_apply, monoid_hom.comp_apply] } @[elab_as_eliminator] lemma induction_on {C : free_product M → Prop} (m : free_product M) (h_one : C 1) (h_of : ∀ (i) (m : M i), C (of m)) (h_mul : ∀ (x y), C x → C y → C (x * y)) : C m := begin let S : submonoid (free_product M) := submonoid.mk (set_of C) h_mul h_one, convert subtype.prop (lift (λ i, of.cod_restrict S (h_of i)) m), change monoid_hom.id _ m = S.subtype.comp _ m, congr, ext, simp [monoid_hom.cod_restrict], end lemma of_left_inverse [decidable_eq ι] (i : ι) : function.left_inverse (lift $ pi.mul_single i (monoid_hom.id (M i))) of := λ x, by simp only [lift_of, pi.mul_single_eq_same, monoid_hom.id_apply] lemma of_injective (i : ι) : function.injective ⇑(of : M i →* _) := by { classical, exact (of_left_inverse i).injective } lemma lift_mrange_le {N} [monoid N] (f : Π i, M i →* N) {s : submonoid N} (h : ∀ i, (f i).mrange ≤ s) : (lift f).mrange ≤ s := begin rintros _ ⟨x, rfl⟩, induction x using free_product.induction_on with i x x y hx hy, { exact s.one_mem, }, { simp only [lift_of, set_like.mem_coe], exact h i (set.mem_range_self x), }, { simp only [map_mul, set_like.mem_coe], exact s.mul_mem hx hy, }, end lemma mrange_eq_supr {N} [monoid N] (f : Π i, M i →* N) : (lift f).mrange = ⨆ i, (f i).mrange := begin apply le_antisymm (lift_mrange_le f (λ i, le_supr _ i)), apply supr_le _, rintros i _ ⟨x, rfl⟩, exact ⟨of x, by simp only [lift_of]⟩ end section group variables (G : ι → Type*) [Π i, group (G i)] instance : has_inv (free_product G) := { inv := mul_opposite.unop ∘ lift (λ i, (of : G i →* _).op.comp (mul_equiv.inv' (G i)).to_monoid_hom) } lemma inv_def (x : free_product G) : x⁻¹ = mul_opposite.unop (lift (λ i, (of : G i →* _).op.comp (mul_equiv.inv' (G i)).to_monoid_hom) x) := rfl instance : group (free_product G) := { mul_left_inv := begin intro m, rw inv_def, apply m.induction_on, { rw [monoid_hom.map_one, mul_opposite.unop_one, one_mul], }, { intros i m, change of m⁻¹ * of m = 1, rw [←of.map_mul, mul_left_inv, of.map_one], }, { intros x y hx hy, rw [monoid_hom.map_mul, mul_opposite.unop_mul, mul_assoc, ← mul_assoc _ x y, hx, one_mul, hy], }, end, ..free_product.has_inv G, ..free_product.monoid G } lemma lift_range_le {N} [group N] (f : Π i, G i →* N) {s : subgroup N} (h : ∀ i, (f i).range ≤ s) : (lift f).range ≤ s := begin rintros _ ⟨x, rfl⟩, induction x using free_product.induction_on with i x x y hx hy, { exact s.one_mem, }, { simp only [lift_of, set_like.mem_coe], exact h i (set.mem_range_self x), }, { simp only [map_mul, set_like.mem_coe], exact s.mul_mem hx hy, }, end lemma range_eq_supr {N} [group N] (f : Π i, G i →* N) : (lift f).range = ⨆ i, (f i).range := begin apply le_antisymm (lift_range_le _ f (λ i, le_supr _ i)), apply supr_le _, rintros i _ ⟨x, rfl⟩, exact ⟨of x, by simp only [lift_of]⟩ end end group namespace word /-- The empty reduced word. -/ def empty : word M := { to_list := [], ne_one := λ _, false.elim, chain_ne := list.chain'_nil } instance : inhabited (word M) := ⟨empty⟩ /-- A reduced word determines an element of the free product, given by multiplication. -/ def prod (w : word M) : free_product M := list.prod (w.to_list.map $ λ l, of l.snd) @[simp] lemma prod_empty : prod (empty : word M) = 1 := rfl /-- `fst_idx w` is `some i` if the first letter of `w` is `⟨i, m⟩` with `m : M i`. If `w` is empty then it's `none`. -/ def fst_idx (w : word M) : option ι := w.to_list.head'.map sigma.fst lemma fst_idx_ne_iff {w : word M} {i} : fst_idx w ≠ some i ↔ ∀ l ∈ w.to_list.head', i ≠ sigma.fst l := not_iff_not.mp $ by simp [fst_idx] variable (M) /-- Given an index `i : ι`, `pair M i` is the type of pairs `(head, tail)` where `head : M i` and `tail : word M`, subject to the constraint that first letter of `tail` can't be `⟨i, m⟩`. By prepending `head` to `tail`, one obtains a new word. We'll show that any word can be uniquely obtained in this way. -/ @[ext] structure pair (i : ι) := (head : M i) (tail : word M) (fst_idx_ne : fst_idx tail ≠ some i) instance (i : ι) : inhabited (pair M i) := ⟨⟨1, empty, by tauto⟩⟩ variable {M} variables [∀ i, decidable_eq (M i)] /-- Given a pair `(head, tail)`, we can form a word by prepending `head` to `tail`, except if `head` is `1 : M i` then we have to just return `word` since we need the result to be reduced. -/ def rcons {i} (p : pair M i) : word M := if h : p.head = 1 then p.tail else { to_list := ⟨i, p.head⟩ :: p.tail.to_list, ne_one := by { rintros l (rfl | hl), exact h, exact p.tail.ne_one l hl }, chain_ne := p.tail.chain_ne.cons' (fst_idx_ne_iff.mp p.fst_idx_ne) } /-- Given a word of the form `⟨l :: ls, h1, h2⟩`, we can form a word of the form `⟨ls, _, _⟩`, dropping the first letter. -/ private def mk_aux {l} (ls : list (Σ i, M i)) (h1 : ∀ l' ∈ l :: ls, sigma.snd l' ≠ 1) (h2 : (l :: ls).chain' _) : word M := ⟨ls, λ l' hl, h1 _ (list.mem_cons_of_mem _ hl), h2.tail⟩ lemma cons_eq_rcons {i} {m : M i} {ls h1 h2} : word.mk (⟨i, m⟩ :: ls) h1 h2 = rcons ⟨m, mk_aux ls h1 h2, fst_idx_ne_iff.mpr h2.rel_head'⟩ := by { rw [rcons, dif_neg], refl, exact h1 ⟨i, m⟩ (ls.mem_cons_self _) } @[simp] lemma prod_rcons {i} (p : pair M i) : prod (rcons p) = of p.head * prod p.tail := if hm : p.head = 1 then by rw [rcons, dif_pos hm, hm, monoid_hom.map_one, one_mul] else by rw [rcons, dif_neg hm, prod, list.map_cons, list.prod_cons, prod] lemma rcons_inj {i} : function.injective (rcons : pair M i → word M) := begin rintros ⟨m, w, h⟩ ⟨m', w', h'⟩ he, by_cases hm : m = 1; by_cases hm' : m' = 1, { simp only [rcons, dif_pos hm, dif_pos hm'] at he, cc, }, { exfalso, simp only [rcons, dif_pos hm, dif_neg hm'] at he, rw he at h, exact h rfl }, { exfalso, simp only [rcons, dif_pos hm', dif_neg hm] at he, rw ←he at h', exact h' rfl, }, { have : m = m' ∧ w.to_list = w'.to_list, { simpa only [rcons, dif_neg hm, dif_neg hm', true_and, eq_self_iff_true, subtype.mk_eq_mk, heq_iff_eq, ←subtype.ext_iff_val] using he }, rcases this with ⟨rfl, h⟩, congr, exact word.ext _ _ h, } end variable [decidable_eq ι] /-- Given `i : ι`, any reduced word can be decomposed into a pair `p` such that `w = rcons p`. -/ -- This definition is computable but not very nice to look at. Thankfully we don't have to inspect -- it, since `rcons` is known to be injective. private def equiv_pair_aux (i) : Π w : word M, { p : pair M i // rcons p = w } | w@⟨[], _, _⟩ := ⟨⟨1, w, by rintro ⟨⟩⟩, dif_pos rfl⟩ | w@⟨⟨j, m⟩ :: ls, h1, h2⟩ := if ij : i = j then { val := { head := ij.symm.rec m, tail := mk_aux ls h1 h2, fst_idx_ne := by cases ij; exact fst_idx_ne_iff.mpr h2.rel_head' }, property := by cases ij; exact cons_eq_rcons.symm } else ⟨⟨1, w, (option.some_injective _).ne (ne.symm ij)⟩, dif_pos rfl⟩ /-- The equivalence between words and pairs. Given a word, it decomposes it as a pair by removing the first letter if it comes from `M i`. Given a pair, it prepends the head to the tail. -/ def equiv_pair (i) : word M ≃ pair M i := { to_fun := λ w, (equiv_pair_aux i w).val, inv_fun := rcons, left_inv := λ w, (equiv_pair_aux i w).property, right_inv := λ p, rcons_inj (equiv_pair_aux i _).property } lemma equiv_pair_symm (i) (p : pair M i) : (equiv_pair i).symm p = rcons p := rfl lemma equiv_pair_eq_of_fst_idx_ne {i} {w : word M} (h : fst_idx w ≠ some i) : equiv_pair i w = ⟨1, w, h⟩ := (equiv_pair i).apply_eq_iff_eq_symm_apply.mpr $ eq.symm (dif_pos rfl) instance summand_action (i) : mul_action (M i) (word M) := { smul := λ m w, rcons { head := m * (equiv_pair i w).head, ..equiv_pair i w }, one_smul := λ w, by { simp_rw [one_mul], apply (equiv_pair i).symm_apply_eq.mpr, ext; refl }, mul_smul := λ m m' w, by simp only [mul_assoc, ←equiv_pair_symm, equiv.apply_symm_apply], } instance : mul_action (free_product M) (word M) := mul_action.of_End_hom (lift (λ i, mul_action.to_End_hom)) lemma of_smul_def (i) (w : word M) (m : M i) : of m • w = rcons { head := m * (equiv_pair i w).head, ..equiv_pair i w } := rfl lemma cons_eq_smul {i} {m : M i} {ls h1 h2} : word.mk (⟨i, m⟩ :: ls) h1 h2 = of m • mk_aux ls h1 h2 := by rw [cons_eq_rcons, of_smul_def, equiv_pair_eq_of_fst_idx_ne _]; simp only [mul_one] lemma smul_induction {C : word M → Prop} (h_empty : C empty) (h_smul : ∀ i (m : M i) w, C w → C (of m • w)) (w : word M) : C w := begin cases w with ls h1 h2, induction ls with l ls ih, { exact h_empty }, cases l with i m, rw cons_eq_smul, exact h_smul _ _ _ (ih _ _), end @[simp] lemma prod_smul (m) : ∀ w : word M, prod (m • w) = m * prod w := begin apply m.induction_on, { intro, rw [one_smul, one_mul] }, { intros, rw [of_smul_def, prod_rcons, of.map_mul, mul_assoc, ←prod_rcons, ←equiv_pair_symm, equiv.symm_apply_apply] }, { intros x y hx hy w, rw [mul_smul, hx, hy, mul_assoc] }, end /-- Each element of the free product corresponds to a unique reduced word. -/ def equiv : free_product M ≃ word M := { to_fun := λ m, m • empty, inv_fun := λ w, prod w, left_inv := λ m, by dsimp only; rw [prod_smul, prod_empty, mul_one], right_inv := begin apply smul_induction, { dsimp only, rw [prod_empty, one_smul], }, { dsimp only, intros i m w ih, rw [prod_smul, mul_smul, ih], }, end } instance : decidable_eq (word M) := function.injective.decidable_eq word.ext instance : decidable_eq (free_product M) := word.equiv.decidable_eq end word variable (M) /-- A `neword M i j` is a representation of a non-empty reduced words where the first letter comes from `M i` and the last letter comes from `M j`. It can be constructed from singletons and via concatentation, and thus provides a useful induction principle. -/ @[nolint has_nonempty_instance] inductive neword : ι → ι → Type (max u_1 u_2) | singleton : ∀ {i} (x : M i) (hne1 : x ≠ 1), neword i i | append : ∀ {i j k l} (w₁ : neword i j) (hne : j ≠ k) (w₂ : neword k l), neword i l variable {M} namespace neword open word /-- The list represented by a given `neword` -/ @[simp] def to_list : Π {i j} (w : neword M i j), list (Σ i, M i) | i _ (singleton x hne1) := [⟨i, x⟩] | _ _ (append w₁ hne w₂) := w₁.to_list ++ w₂.to_list lemma to_list_ne_nil {i j} (w : neword M i j) : w.to_list ≠ list.nil := by { induction w, { rintros ⟨rfl⟩ }, { apply list.append_ne_nil_of_ne_nil_left, assumption } } /-- The first letter of a `neword` -/ @[simp] def head : Π {i j} (w : neword M i j), M i | i _ (singleton x hne1) := x | _ _ (append w₁ hne w₂) := w₁.head /-- The last letter of a `neword` -/ @[simp] def last : Π {i j} (w : neword M i j), M j | i _ (singleton x hne1) := x | _ _ (append w₁ hne w₂) := w₂.last @[simp] lemma to_list_head' {i j} (w : neword M i j) : w.to_list.head' = option.some ⟨i, w.head⟩ := begin rw ← option.mem_def, induction w, { rw option.mem_def, reflexivity, }, { exact list.head'_append w_ih_w₁, }, end @[simp] lemma to_list_last' {i j} (w : neword M i j) : w.to_list.last' = option.some ⟨j, w.last⟩ := begin rw ← option.mem_def, induction w, { rw option.mem_def, reflexivity, }, { exact list.last'_append w_ih_w₂, }, end /-- The `word M` represented by a `neword M i j` -/ def to_word {i j} (w : neword M i j) : word M := { to_list := w.to_list, ne_one := begin induction w, { rintros ⟨k,x⟩ ⟨rfl, rfl⟩, exact w_hne1, exfalso, apply H, }, { intros l h, simp only [to_list, list.mem_append] at h, cases h, { exact w_ih_w₁ _ h, }, { exact w_ih_w₂ _ h, }, }, end, chain_ne := begin induction w, { exact list.chain'_singleton _, }, { apply list.chain'.append w_ih_w₁ w_ih_w₂, intros x hx y hy, rw [w_w₁.to_list_last', option.mem_some_iff] at hx, rw [w_w₂.to_list_head', option.mem_some_iff] at hy, subst hx, subst hy, exact w_hne, }, end, } /-- Every nonempty `word M` can be constructed as a `neword M i j` -/ lemma of_word (w : word M) (h : w ≠ empty) : ∃ i j (w' : neword M i j), w'.to_word = w := begin rsuffices ⟨i, j, w, h⟩ : ∃ i j (w' : neword M i j), w'.to_word.to_list = w.to_list, { refine ⟨i, j, w, _⟩, ext, rw h, }, cases w with l hnot1 hchain, induction l with x l hi, { contradiction, }, { rw list.forall_mem_cons at hnot1, cases l with y l, { refine ⟨x.1, x.1, singleton x.2 hnot1.1, _ ⟩, simp [to_word], }, { rw list.chain'_cons at hchain, specialize hi hnot1.2 hchain.2 (by rintros ⟨rfl⟩), obtain ⟨i, j, w', hw' : w'.to_list = y :: l⟩ := hi, obtain rfl : y = ⟨i, w'.head⟩, by simpa [hw'] using w'.to_list_head', refine ⟨x.1, j, append (singleton x.2 hnot1.1) hchain.1 w', _⟩, { simpa [to_word] using hw', } } } end /-- A non-empty reduced word determines an element of the free product, given by multiplication. -/ def prod {i j} (w : neword M i j) := w.to_word.prod @[simp] lemma singleton_head {i} (x : M i) (hne_one : x ≠ 1) : (singleton x hne_one).head = x := rfl @[simp] lemma singleton_last {i} (x : M i) (hne_one : x ≠ 1) : (singleton x hne_one).last = x := rfl @[simp] lemma prod_singleton {i} (x : M i) (hne_one : x ≠ 1) : (singleton x hne_one).prod = of x := by simp [to_word, prod, word.prod] @[simp] lemma append_head {i j k l} {w₁ : neword M i j} {hne : j ≠ k} {w₂ : neword M k l} : (append w₁ hne w₂).head = w₁.head := rfl @[simp] lemma append_last {i j k l} {w₁ : neword M i j} {hne : j ≠ k} {w₂ : neword M k l} : (append w₁ hne w₂).last = w₂.last := rfl @[simp] lemma append_prod {i j k l} {w₁ : neword M i j} {hne : j ≠ k} {w₂ : neword M k l} : (append w₁ hne w₂).prod = w₁.prod * w₂.prod := by simp [to_word, prod, word.prod] /-- One can replace the first letter in a non-empty reduced word by an element of the same group -/ def replace_head : Π {i j : ι} (x : M i) (hnotone : x ≠ 1) (w : neword M i j), neword M i j | _ _ x h (singleton _ _) := singleton x h | _ _ x h (append w₁ hne w₂) := append (replace_head x h w₁) hne w₂ @[simp] lemma replace_head_head {i j : ι} (x : M i) (hnotone : x ≠ 1) (w : neword M i j) : (replace_head x hnotone w).head = x := by { induction w, refl, exact w_ih_w₁ _ _, } /-- One can multiply an element from the left to a non-empty reduced word if it does not cancel with the first element in the word. -/ def mul_head {i j : ι} (w : neword M i j) (x : M i) (hnotone : x * w.head ≠ 1) : neword M i j := replace_head (x * w.head) hnotone w @[simp] lemma mul_head_head {i j : ι} (w : neword M i j) (x : M i) (hnotone : x * w.head ≠ 1) : (mul_head w x hnotone).head = x * w.head := by { induction w, refl, exact w_ih_w₁ _ _, } @[simp] lemma mul_head_prod {i j : ι} (w : neword M i j) (x : M i) (hnotone : x * w.head ≠ 1) : (mul_head w x hnotone).prod = of x * w.prod := begin unfold mul_head, induction w, { simp [mul_head, replace_head], }, { specialize w_ih_w₁ _ hnotone, clear w_ih_w₂, simp [replace_head, ← mul_assoc] at *, congr' 1, } end section group variables {G : ι → Type*} [Π i, group (G i)] /-- The inverse of a non-empty reduced word -/ def inv : Π {i j} (w : neword G i j), neword G j i | _ _ (singleton x h) := singleton x⁻¹ (mt inv_eq_one.mp h) | _ _ (append w₁ h w₂) := append w₂.inv h.symm w₁.inv @[simp] lemma inv_prod {i j} (w : neword G i j) : w.inv.prod = w.prod⁻¹ := by induction w; simp [inv, *] @[simp] lemma inv_head {i j} (w : neword G i j) : w.inv.head = w.last⁻¹ := by induction w; simp [inv, *] @[simp] lemma inv_last {i j} (w : neword G i j) : w.inv.last = w.head⁻¹ := by induction w; simp [inv, *] end group end neword section ping_pong_lemma open_locale pointwise open_locale cardinal variables [hnontriv : nontrivial ι] variables {G : Type*} [group G] variables {H : ι → Type*} [∀ i, group (H i)] variables (f : Π i, H i →* G) -- We need many groups or one group with many elements variables (hcard : 3 ≤ # ι ∨ ∃ i, 3 ≤ # (H i)) -- A group action on α, and the ping-pong sets variables {α : Type*} [mul_action G α] variables (X : ι → set α) variables (hXnonempty : ∀ i, (X i).nonempty) variables (hXdisj : pairwise (λ i j, disjoint (X i) (X j))) variables (hpp : pairwise (λ i j, ∀ h : H i, h ≠ 1 → f i h • X j ⊆ X i)) include hpp lemma lift_word_ping_pong {i j k} (w : neword H i j) (hk : j ≠ k) : lift f w.prod • X k ⊆ X i := begin rename [i → i', j → j', k → m, hk → hm], induction w with i x hne_one i j k l w₁ hne w₂ hIw₁ hIw₂ generalizing m; clear i' j', { simpa using hpp _ _ hm _ hne_one, }, { calc lift f (neword.append w₁ hne w₂).prod • X m = lift f w₁.prod • lift f w₂.prod • X m : by simp [mul_action.mul_smul] ... ⊆ lift f w₁.prod • X k : set_smul_subset_set_smul_iff.mpr (hIw₂ hm) ... ⊆ X i : hIw₁ hne }, end include X hXnonempty hXdisj lemma lift_word_prod_nontrivial_of_other_i {i j k} (w : neword H i j) (hhead : k ≠ i) (hlast : k ≠ j) : lift f w.prod ≠ 1 := begin intro heq1, have : X k ⊆ X i, by simpa [heq1] using lift_word_ping_pong f X hpp w hlast.symm, obtain ⟨x, hx⟩ := hXnonempty k, exact hXdisj k i hhead ⟨hx, this hx⟩, end include hnontriv lemma lift_word_prod_nontrivial_of_head_eq_last {i} (w : neword H i i) : lift f w.prod ≠ 1 := begin obtain ⟨k, hk⟩ := exists_ne i, exact lift_word_prod_nontrivial_of_other_i f X hXnonempty hXdisj hpp w hk hk, end lemma lift_word_prod_nontrivial_of_head_card {i j} (w : neword H i j) (hcard : 3 ≤ # (H i)) (hheadtail : i ≠ j) : lift f w.prod ≠ 1 := begin obtain ⟨h, hn1, hnh⟩ := cardinal.three_le hcard 1 (w.head⁻¹), have hnot1 : h * w.head ≠ 1, by { rw ← div_inv_eq_mul, exact div_ne_one_of_ne hnh }, let w' : neword H i i := neword.append (neword.mul_head w h hnot1) hheadtail.symm (neword.singleton h⁻¹ (inv_ne_one.mpr hn1)), have hw' : lift f w'.prod ≠ 1 := lift_word_prod_nontrivial_of_head_eq_last f X hXnonempty hXdisj hpp w', intros heq1, apply hw', simp [w', heq1] end include hcard lemma lift_word_prod_nontrivial_of_not_empty {i j} (w : neword H i j) : lift f w.prod ≠ 1 := begin classical, cases hcard, { obtain ⟨i, h1, h2⟩ := cardinal.three_le hcard i j, exact lift_word_prod_nontrivial_of_other_i f X hXnonempty hXdisj hpp w h1 h2, }, { cases hcard with k hcard, by_cases hh : i = k; by_cases hl : j = k, { subst hh, subst hl, exact lift_word_prod_nontrivial_of_head_eq_last f X hXnonempty hXdisj hpp w, }, { subst hh, change j ≠ i at hl, exact lift_word_prod_nontrivial_of_head_card f X hXnonempty hXdisj hpp w hcard hl.symm, }, { subst hl, change i ≠ j at hh, have : lift f w.inv.prod ≠ 1 := lift_word_prod_nontrivial_of_head_card f X hXnonempty hXdisj hpp w.inv hcard hh.symm, intros heq, apply this, simpa using heq, }, { change i ≠ k at hh, change j ≠ k at hl, obtain ⟨h, hn1, -⟩ := cardinal.three_le hcard 1 1, let w' : neword H k k := neword.append (neword.append (neword.singleton h hn1) hh.symm w) hl (neword.singleton h⁻¹ (inv_ne_one.mpr hn1)) , have hw' : lift f w'.prod ≠ 1 := lift_word_prod_nontrivial_of_head_eq_last f X hXnonempty hXdisj hpp w', intros heq1, apply hw', simp [w', heq1], }, } end lemma empty_of_word_prod_eq_one {w : word H} (h : lift f w.prod = 1) : w = word.empty := begin by_contradiction hnotempty, obtain ⟨i, j, w, rfl⟩ := neword.of_word w hnotempty, exact lift_word_prod_nontrivial_of_not_empty f hcard X hXnonempty hXdisj hpp w h, end /-- The Ping-Pong-Lemma. Given a group action of `G` on `X` so that the `H i` acts in a specific way on disjoint subsets `X i` we can prove that `lift f` is injective, and thus the image of `lift f` is isomorphic to the direct product of the `H i`. Often the Ping-Pong-Lemma is stated with regard to subgroups `H i` that generate the whole group; we generalize to arbitrary group homomorphisms `f i : H i →* G` and do not require the group to be generated by the images. Usually the Ping-Pong-Lemma requires that one group `H i` has at least three elements. This condition is only needed if `# ι = 2`, and we accept `3 ≤ # ι` as an alternative. -/ theorem lift_injective_of_ping_pong: function.injective (lift f) := begin classical, apply (injective_iff_map_eq_one (lift f)).mpr, rw (free_product.word.equiv : _ ≃ word H).forall_congr_left', { intros w Heq, dsimp [word.equiv] at *, { rw empty_of_word_prod_eq_one f hcard X hXnonempty hXdisj hpp Heq, reflexivity, }, }, end end ping_pong_lemma /-- The free product of free groups is itself a free group -/ @[simps] instance {ι : Type*} (G : ι → Type*) [∀ i, group (G i)] [hG : ∀ i, is_free_group (G i)] : is_free_group (free_product G) := { generators := Σ i, is_free_group.generators (G i), mul_equiv := monoid_hom.to_mul_equiv (free_group.lift (λ (x : Σ i, is_free_group.generators (G i)), free_product.of (is_free_group.of x.2 : G x.1))) (free_product.lift (λ (i : ι), (is_free_group.lift (λ (x : is_free_group.generators (G i)), free_group.of (⟨i, x⟩ : Σ i, is_free_group.generators (G i))) : G i →* (free_group (Σ i, is_free_group.generators (G i)))))) (by {ext, simp, }) (by {ext, simp, }) } /-- A free group is a free product of copies of the free_group over one generator. -/ -- NB: One might expect this theorem to be phrased with ℤ, but ℤ is an additive group, -- and using `multiplicative ℤ` runs into diamond issues. @[simps] def _root_.free_group_equiv_free_product {ι : Type u_1} : free_group ι ≃* free_product (λ (_ : ι), free_group unit) := begin refine monoid_hom.to_mul_equiv _ _ _ _, exact free_group.lift (λ i, @free_product.of ι _ _ i (free_group.of unit.star)), exact free_product.lift (λ i, free_group.lift (λ pstar, free_group.of i)), { ext i, refl, }, { ext i a, cases a, refl, }, end section ping_pong_lemma open_locale pointwise cardinal variables [nontrivial ι] variables {G : Type u_1} [group G] (a : ι → G) -- A group action on α, and the ping-pong sets variables {α : Type*} [mul_action G α] variables (X Y : ι → set α) variables (hXnonempty : ∀ i, (X i).nonempty) variables (hXdisj : pairwise (λ i j, disjoint (X i) (X j))) variables (hYdisj : pairwise (λ i j, disjoint (Y i) (Y j))) variables (hXYdisj : ∀ i j, disjoint (X i) (Y j)) variables (hX : ∀ i, a i • (Y i)ᶜ ⊆ X i) variables (hY : ∀ i, a⁻¹ i • (X i)ᶜ ⊆ Y i) include hXnonempty hXdisj hYdisj hXYdisj hX hY /-- The Ping-Pong-Lemma. Given a group action of `G` on `X` so that the generators of the free groups act in specific ways on disjoint subsets `X i` and `Y i` we can prove that `lift f` is injective, and thus the image of `lift f` is isomorphic to the free group. Often the Ping-Pong-Lemma is stated with regard to group elements that generate the whole group; we generalize to arbitrary group homomorphisms from the free group to `G` and do not require the group to be generated by the elements. -/ theorem _root_.free_group.injective_lift_of_ping_pong : function.injective (free_group.lift a) := begin -- Step one: express the free group lift via the free product lift have : free_group.lift a = (free_product.lift (λ i, free_group.lift (λ _, a i))).comp (((@free_group_equiv_free_product ι)).to_monoid_hom), { ext i, simp, }, rw this, clear this, refine function.injective.comp _ (mul_equiv.injective _), -- Step two: Invoke the ping-pong lemma for free products show function.injective (lift (λ (i : ι), free_group.lift (λ _, a i))), -- Prepare to instantiate lift_injective_of_ping_pong let H : ι → Type _ := λ i, free_group unit, let f : Π i, H i →* G := λ i, free_group.lift (λ _, a i), let X' : ι → set α := λ i, X i ∪ Y i, apply lift_injective_of_ping_pong f _ X', show _ ∨ ∃ i, 3 ≤ # (H i), { inhabit ι, right, use arbitrary ι, simp only [H], rw [free_group.free_group_unit_equiv_int.cardinal_eq, cardinal.mk_denumerable], apply le_of_lt, simp }, show ∀ i, (X' i).nonempty, { exact (λ i, set.nonempty.inl (hXnonempty i)), }, show pairwise (λ i j, disjoint (X' i) (X' j)), { intros i j hij, simp only [X'], apply disjoint.union_left; apply disjoint.union_right, { exact hXdisj i j hij, }, { exact hXYdisj i j, }, { exact (hXYdisj j i).symm, }, { exact hYdisj i j hij, }, }, show pairwise (λ i j, ∀ h : H i, h ≠ 1 → f i h • X' j ⊆ X' i), { rintros i j hij, -- use free_group unit ≃ ℤ refine free_group.free_group_unit_equiv_int.forall_congr_left'.mpr _, intros n hne1, change free_group.lift (λ _, a i) (free_group.of () ^ n) • X' j ⊆ X' i, simp only [map_zpow, free_group.lift.of], change a i ^ n • X' j ⊆ X' i, have hnne0 : n ≠ 0, { rintro rfl, apply hne1, simpa, }, clear hne1, simp only [X'], -- Positive and negative powers separately cases (lt_or_gt_of_ne hnne0).swap with hlt hgt, { have h1n : 1 ≤ n := hlt, calc a i ^ n • X' j ⊆ a i ^ n • (Y i)ᶜ : smul_set_mono ((hXYdisj j i).union_left $ hYdisj j i hij.symm).subset_compl_right ... ⊆ X i : begin refine int.le_induction _ _ _ h1n, { rw zpow_one, exact hX i, }, { intros n hle hi, calc (a i ^ (n + 1)) • (Y i)ᶜ = (a i ^ n * a i) • (Y i)ᶜ : by rw [zpow_add, zpow_one] ... = a i ^ n • (a i • (Y i)ᶜ) : mul_action.mul_smul _ _ _ ... ⊆ a i ^ n • X i : smul_set_mono $ hX i ... ⊆ a i ^ n • (Y i)ᶜ : smul_set_mono (hXYdisj i i).subset_compl_right ... ⊆ X i : hi, }, end ... ⊆ X' i : set.subset_union_left _ _, }, { have h1n : n ≤ -1, { apply int.le_of_lt_add_one, simpa using hgt, }, calc a i ^ n • X' j ⊆ a i ^ n • (X i)ᶜ : smul_set_mono ((hXdisj j i hij.symm).union_left (hXYdisj i j).symm).subset_compl_right ... ⊆ Y i : begin refine int.le_induction_down _ _ _ h1n, { rw [zpow_neg, zpow_one], exact hY i, }, { intros n hle hi, calc (a i ^ (n - 1)) • (X i)ᶜ = (a i ^ n * (a i)⁻¹) • (X i)ᶜ : by rw [zpow_sub, zpow_one] ... = a i ^ n • ((a i)⁻¹ • (X i)ᶜ) : mul_action.mul_smul _ _ _ ... ⊆ a i ^ n • Y i : smul_set_mono $ hY i ... ⊆ a i ^ n • (X i)ᶜ : smul_set_mono (hXYdisj i i).symm.subset_compl_right ... ⊆ Y i : hi, }, end ... ⊆ X' i : set.subset_union_right _ _, }, }, end end ping_pong_lemma end free_product
e77fb2f2923e8c453c4fe163cc333c2316fee1d2
cf39355caa609c0f33405126beee2739aa3cb77e
/library/init/data/basic.lean
0e4dadf7b0f136896afbb0bcf735f8acc12f54a3
[ "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
598
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.data.setoid init.data.quot init.data.bool.basic init.data.punit import init.data.nat.basic init.data.prod init.data.sum.basic import init.data.sigma.basic init.data.subtype.basic import init.data.fin.basic init.data.list.basic init.data.char.basic import init.data.string.basic init.data.option.basic init.data.set import init.data.unsigned.basic init.data.ordering.basic init.data.repr import init.data.to_string
7efa34b1f635e123edaf64dc23ec2f000b597273
a4673261e60b025e2c8c825dfa4ab9108246c32e
/tests/lean/run/def7.lean
32a84a3e643e34e775a7a248c8c89e42bb2f3295
[ "Apache-2.0" ]
permissive
jcommelin/lean4
c02dec0cc32c4bccab009285475f265f17d73228
2909313475588cc20ac0436e55548a4502050d0a
refs/heads/master
1,674,129,550,893
1,606,415,348,000
1,606,415,348,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,423
lean
inductive InfTree.{u} (α : Type u) | leaf : α → InfTree α | node : (Nat → InfTree α) → InfTree α open InfTree def szn.{u} {α : Type u} (n : Nat) : InfTree α → InfTree α → Nat | leaf a, t2 => 1 | node c, leaf b => 0 | node c, node d => szn n (c n) (d n) universes u theorem ex1 {α : Type u} (n : Nat) (c : Nat → InfTree α) (d : Nat → InfTree α) : szn n (node c) (node d) = szn n (c n) (d n) := rfl inductive BTree (α : Type u) | leaf : α → BTree α | node : (Bool → Bool → BTree α) → BTree α def BTree.sz {α : Type u} : BTree α → Nat | leaf a => 1 | node c => sz (c true true) + sz (c true false) + sz (c false true) + sz (c false false) + 1 theorem ex2 {α : Type u} (c : Bool → Bool → BTree α) : (BTree.node c).sz = (c true true).sz + (c true false).sz + (c false true).sz + (c false false).sz + 1 := rfl inductive L (α : Type u) | nil : L α | cons : (Unit → α) → (Unit → L α) → L α def L.append {α} : L α → L α → L α | nil, bs => bs | cons a as, bs => cons a (fun _ => append (as ()) bs) theorem L.appendNil {α} : (as : L α) → append as nil = as | nil => rfl | cons a as => show cons a (fun _ => append (as ()) nil) = cons a as from have ih : append (as ()) nil = as () from appendNil $ as () have thunkAux : (fun _ => as ()) = as from funext fun x => by cases x exact rfl by rw [ih, thunkAux]; exact rfl
de97d2d43bac8e7c96c730d1dcaeb07275c39193
94e33a31faa76775069b071adea97e86e218a8ee
/src/linear_algebra/std_basis.lean
1dc5356aa4254ab788c9b31e50188c6104085480
[ "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
10,547
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl -/ import data.matrix.basis import linear_algebra.basis import linear_algebra.pi /-! # The standard basis This file defines the standard basis `pi.basis (s : ∀ j, basis (ι j) R (M j))`, which is the `Σ j, ι j`-indexed basis of Π j, M j`. The basis vectors are given by `pi.basis s ⟨j, i⟩ j' = linear_map.std_basis R M j' (s j) i = if j = j' then s i else 0`. The standard basis on `R^η`, i.e. `η → R` is called `pi.basis_fun`. To give a concrete example, `linear_map.std_basis R (λ (i : fin 3), R) i 1` gives the `i`th unit basis vector in `R³`, and `pi.basis_fun R (fin 3)` proves this is a basis over `fin 3 → R`. ## Main definitions - `linear_map.std_basis R M`: if `x` is a basis vector of `M i`, then `linear_map.std_basis R M i x` is the `i`th standard basis vector of `Π i, M i`. - `pi.basis s`: given a basis `s i` for each `M i`, the standard basis on `Π i, M i` - `pi.basis_fun R η`: the standard basis on `R^η`, i.e. `η → R`, given by `pi.basis_fun R η i j = if i = j then 1 else 0`. - `matrix.std_basis R n m`: the standard basis on `matrix n m R`, given by `matrix.std_basis R n m (i, j) i' j' = if (i, j) = (i', j') then 1 else 0`. -/ open function submodule open_locale big_operators open_locale big_operators namespace linear_map variables (R : Type*) {ι : Type*} [semiring R] (φ : ι → Type*) [Π i, add_comm_monoid (φ i)] [Π i, module R (φ i)] [decidable_eq ι] /-- The standard basis of the product of `φ`. -/ def std_basis : Π (i : ι), φ i →ₗ[R] (Πi, φ i) := single lemma std_basis_apply (i : ι) (b : φ i) : std_basis R φ i b = update 0 i b := rfl lemma coe_std_basis (i : ι) : ⇑(std_basis R φ i) = pi.single i := rfl @[simp] lemma std_basis_same (i : ι) (b : φ i) : std_basis R φ i b i = b := pi.single_eq_same i b lemma std_basis_ne (i j : ι) (h : j ≠ i) (b : φ i) : std_basis R φ i b j = 0 := pi.single_eq_of_ne h b lemma std_basis_eq_pi_diag (i : ι) : std_basis R φ i = pi (diag i) := begin ext x j, convert (update_apply 0 x i j _).symm, refl, end lemma ker_std_basis (i : ι) : ker (std_basis R φ i) = ⊥ := ker_eq_bot_of_injective $ pi.single_injective _ _ lemma proj_comp_std_basis (i j : ι) : (proj i).comp (std_basis R φ j) = diag j i := by rw [std_basis_eq_pi_diag, proj_pi] lemma proj_std_basis_same (i : ι) : (proj i).comp (std_basis R φ i) = id := linear_map.ext $ std_basis_same R φ i lemma proj_std_basis_ne (i j : ι) (h : i ≠ j) : (proj i).comp (std_basis R φ j) = 0 := linear_map.ext $ std_basis_ne R φ _ _ h lemma supr_range_std_basis_le_infi_ker_proj (I J : set ι) (h : disjoint I J) : (⨆i∈I, range (std_basis R φ i)) ≤ (⨅i∈J, ker (proj i)) := begin refine (supr_le $ λ i, supr_le $ λ hi, range_le_iff_comap.2 _), simp only [(ker_comp _ _).symm, eq_top_iff, set_like.le_def, mem_ker, comap_infi, mem_infi], rintro b - j hj, rw [proj_std_basis_ne R φ j i, zero_apply], rintro rfl, exact h ⟨hi, hj⟩ end lemma infi_ker_proj_le_supr_range_std_basis {I : finset ι} {J : set ι} (hu : set.univ ⊆ ↑I ∪ J) : (⨅ i∈J, ker (proj i)) ≤ (⨆i∈I, range (std_basis R φ i)) := set_like.le_def.2 begin assume b hb, simp only [mem_infi, mem_ker, proj_apply] at hb, rw ← show ∑ i in I, std_basis R φ i (b i) = b, { ext i, rw [finset.sum_apply, ← std_basis_same R φ i (b i)], refine finset.sum_eq_single i (assume j hjI ne, std_basis_ne _ _ _ _ ne.symm _) _, assume hiI, rw [std_basis_same], exact hb _ ((hu trivial).resolve_left hiI) }, exact sum_mem_bsupr (λ i hi, (std_basis R φ i).mem_range_self (b i)) end lemma supr_range_std_basis_eq_infi_ker_proj {I J : set ι} (hd : disjoint I J) (hu : set.univ ⊆ I ∪ J) (hI : set.finite I) : (⨆i∈I, range (std_basis R φ i)) = (⨅i∈J, ker (proj i)) := begin refine le_antisymm (supr_range_std_basis_le_infi_ker_proj _ _ _ _ hd) _, have : set.univ ⊆ ↑hI.to_finset ∪ J, { rwa [hI.coe_to_finset] }, refine le_trans (infi_ker_proj_le_supr_range_std_basis R φ this) (supr_mono $ assume i, _), rw [set.finite.mem_to_finset], exact le_rfl end lemma supr_range_std_basis [fintype ι] : (⨆i:ι, range (std_basis R φ i)) = ⊤ := have (set.univ : set ι) ⊆ ↑(finset.univ : finset ι) ∪ ∅ := by rw [finset.coe_univ, set.union_empty], begin apply top_unique, convert (infi_ker_proj_le_supr_range_std_basis R φ this), exact infi_emptyset.symm, exact (funext $ λi, (@supr_pos _ _ _ (λh, range (std_basis R φ i)) $ finset.mem_univ i).symm) end lemma disjoint_std_basis_std_basis (I J : set ι) (h : disjoint I J) : disjoint (⨆i∈I, range (std_basis R φ i)) (⨆i∈J, range (std_basis R φ i)) := begin refine disjoint.mono (supr_range_std_basis_le_infi_ker_proj _ _ _ _ $ disjoint_compl_right) (supr_range_std_basis_le_infi_ker_proj _ _ _ _ $ disjoint_compl_right) _, simp only [disjoint, set_like.le_def, mem_infi, mem_inf, mem_ker, mem_bot, proj_apply, funext_iff], rintros b ⟨hI, hJ⟩ i, classical, by_cases hiI : i ∈ I, { by_cases hiJ : i ∈ J, { exact (h ⟨hiI, hiJ⟩).elim }, { exact hJ i hiJ } }, { exact hI i hiI } end lemma std_basis_eq_single {a : R} : (λ (i : ι), (std_basis R (λ _ : ι, R) i) a) = λ (i : ι), (finsupp.single i a) := funext $ λ i, (finsupp.single_eq_pi_single i a).symm end linear_map namespace pi open linear_map open set variables {R : Type*} section module variables {η : Type*} {ιs : η → Type*} {Ms : η → Type*} lemma linear_independent_std_basis [ring R] [∀i, add_comm_group (Ms i)] [∀i, module R (Ms i)] [decidable_eq η] (v : Πj, ιs j → (Ms j)) (hs : ∀i, linear_independent R (v i)) : linear_independent R (λ (ji : Σ j, ιs j), std_basis R Ms ji.1 (v ji.1 ji.2)) := begin have hs' : ∀j : η, linear_independent R (λ i : ιs j, std_basis R Ms j (v j i)), { intro j, exact (hs j).map' _ (ker_std_basis _ _ _) }, apply linear_independent_Union_finite hs', { assume j J _ hiJ, simp [(set.Union.equations._eqn_1 _).symm, submodule.span_image, submodule.span_Union], have h₀ : ∀ j, span R (range (λ (i : ιs j), std_basis R Ms j (v j i))) ≤ range (std_basis R Ms j), { intro j, rw [span_le, linear_map.range_coe], apply range_comp_subset_range }, have h₁ : span R (range (λ (i : ιs j), std_basis R Ms j (v j i))) ≤ ⨆ i ∈ {j}, range (std_basis R Ms i), { rw @supr_singleton _ _ _ (λ i, linear_map.range (std_basis R (λ (j : η), Ms j) i)), apply h₀ }, have h₂ : (⨆ j ∈ J, span R (range (λ (i : ιs j), std_basis R Ms j (v j i)))) ≤ ⨆ j ∈ J, range (std_basis R (λ (j : η), Ms j) j) := supr₂_mono (λ i _, h₀ i), have h₃ : disjoint (λ (i : η), i ∈ {j}) J, { convert set.disjoint_singleton_left.2 hiJ using 0 }, exact (disjoint_std_basis_std_basis _ _ _ _ h₃).mono h₁ h₂ } end variables [semiring R] [∀i, add_comm_monoid (Ms i)] [∀i, module R (Ms i)] variable [fintype η] section open linear_equiv /-- `pi.basis (s : ∀ j, basis (ιs j) R (Ms j))` is the `Σ j, ιs j`-indexed basis on `Π j, Ms j` given by `s j` on each component. For the standard basis over `R` on the finite-dimensional space `η → R` see `pi.basis_fun`. -/ protected noncomputable def basis (s : ∀ j, basis (ιs j) R (Ms j)) : basis (Σ j, ιs j) R (Π j, Ms j) := -- The `add_comm_monoid (Π j, Ms j)` instance was hard to find. -- Defining this in tactic mode seems to shake up instance search enough that it works by itself. by { refine basis.of_repr (_ ≪≫ₗ (finsupp.sigma_finsupp_lequiv_pi_finsupp R).symm), exact linear_equiv.Pi_congr_right (λ j, (s j).repr) } @[simp] lemma basis_repr_std_basis [decidable_eq η] (s : ∀ j, basis (ιs j) R (Ms j)) (j i) : (pi.basis s).repr (std_basis R _ j (s j i)) = finsupp.single ⟨j, i⟩ 1 := begin ext ⟨j', i'⟩, by_cases hj : j = j', { subst hj, simp only [pi.basis, linear_equiv.trans_apply, basis.repr_self, std_basis_same, linear_equiv.Pi_congr_right_apply, finsupp.sigma_finsupp_lequiv_pi_finsupp_symm_apply], symmetry, exact basis.finsupp.single_apply_left (λ i i' (h : (⟨j, i⟩ : Σ j, ιs j) = ⟨j, i'⟩), eq_of_heq (sigma.mk.inj h).2) _ _ _ }, simp only [pi.basis, linear_equiv.trans_apply, finsupp.sigma_finsupp_lequiv_pi_finsupp_symm_apply, linear_equiv.Pi_congr_right_apply], dsimp, rw [std_basis_ne _ _ _ _ (ne.symm hj), linear_equiv.map_zero, finsupp.zero_apply, finsupp.single_eq_of_ne], rintros ⟨⟩, contradiction end @[simp] lemma basis_apply [decidable_eq η] (s : ∀ j, basis (ιs j) R (Ms j)) (ji) : pi.basis s ji = std_basis R _ ji.1 (s ji.1 ji.2) := basis.apply_eq_iff.mpr (by simp) @[simp] lemma basis_repr (s : ∀ j, basis (ιs j) R (Ms j)) (x) (ji) : (pi.basis s).repr x ji = (s ji.1).repr (x ji.1) ji.2 := rfl end section variables (R η) /-- The basis on `η → R` where the `i`th basis vector is `function.update 0 i 1`. -/ noncomputable def basis_fun : basis η R (Π (j : η), R) := basis.of_equiv_fun (linear_equiv.refl _ _) @[simp] lemma basis_fun_apply [decidable_eq η] (i) : basis_fun R η i = std_basis R (λ (i : η), R) i 1 := by { simp only [basis_fun, basis.coe_of_equiv_fun, linear_equiv.refl_symm, linear_equiv.refl_apply, std_basis_apply], congr /- Get rid of a `decidable_eq` mismatch. -/ } @[simp] lemma basis_fun_repr (x : η → R) (i : η) : (pi.basis_fun R η).repr x i = x i := by simp [basis_fun] end end module end pi namespace matrix variables (R : Type*) (m n : Type*) [fintype m] [fintype n] [semiring R] /-- The standard basis of `matrix m n R`. -/ noncomputable def std_basis : basis (m × n) R (matrix m n R) := basis.reindex (pi.basis (λ (i : m), pi.basis_fun R n)) (equiv.sigma_equiv_prod _ _) variables {n m} lemma std_basis_eq_std_basis_matrix (i : n) (j : m) [decidable_eq n] [decidable_eq m] : std_basis R n m (i, j) = std_basis_matrix i j (1 : R) := begin ext a b, by_cases hi : i = a; by_cases hj : j = b, { simp [std_basis, hi, hj] }, { simp [std_basis, hi, hj, ne.symm hj, linear_map.std_basis_ne] }, { simp [std_basis, hi, hj, ne.symm hi, linear_map.std_basis_ne] }, { simp [std_basis, hi, hj, ne.symm hj, ne.symm hi, linear_map.std_basis_ne] } end end matrix
264f54ec8f7930a393c0c1d4f1cd46f5223786cf
a7eef317ddec01b9fc6cfbb876fe7ac00f205ac7
/src/category_theory/concrete_category/bundled.lean
8a89c9a68c7826757660e29fb94ce96b0403c681
[ "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
1,929
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Johannes Hölzl, Reid Barton, Sean Leather Bundled types. -/ import tactic.doc_commands import tactic.lint /-! `bundled c` provides a uniform structure for bundling a type equipped with a type class. We provide `category` instances for these in `unbundled_hom.lean` (for categories with unbundled homs, e.g. topological spaces) and in `bundled_hom.lean` (for categories with bundled homs, e.g. monoids). -/ universes u v namespace category_theory variables {c d : Type u → Type v} {α : Type u} /-- `bundled` is a type bundled with a type class instance for that type. Only the type class is exposed as a parameter. -/ @[nolint has_inhabited_instance] structure bundled (c : Type u → Type v) : Type (max (u+1) v) := (α : Type u) (str : c α . tactic.apply_instance) namespace bundled /-- A generic function for lifting a type equipped with an instance to a bundled object. -/ -- Usually explicit instances will provide their own version of this, e.g. `Mon.of` and `Top.of`. def of {c : Type u → Type v} (α : Type u) [str : c α] : bundled c := ⟨α, str⟩ instance : has_coe_to_sort (bundled c) := { S := Type u, coe := bundled.α } @[simp] lemma coe_mk (α) (str) : (@bundled.mk c α str : Type u) = α := rfl /- `bundled.map` is reducible so that, if we define a category def Ring : Type (u+1) := induced_category SemiRing (bundled.map @ring.to_semiring) instance search is able to "see" that a morphism R ⟶ S in Ring is really a (semi)ring homomorphism from R.α to S.α, and not merely from `(bundled.map @ring.to_semiring R).α` to `(bundled.map @ring.to_semiring S).α`. -/ /-- Map over the bundled structure -/ @[reducible] def map (f : Π {α}, c α → d α) (b : bundled c) : bundled d := ⟨b, f b.str⟩ end bundled end category_theory
ae2a052ddf298d81b90bee06b8ee9b62e6a023a6
9dd3f3912f7321eb58ee9aa8f21778ad6221f87c
/tests/lean/run/abstract_tac.lean
f7e742bb3cbe9bc7ad8a12e1d9596dbbf2c4bfdf
[ "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
228
lean
def ex (a b c : nat) : a = b → c = b → a = c ∧ b = c := begin intros, split, abstract { transitivity, trace "hello", trace_state, assumption, symmetry, assumption }, abstract { symmetry, assumption } end print ex
8fd69cf1a23a0c408cbc2c98224f005f4e9fe080
9dc8cecdf3c4634764a18254e94d43da07142918
/src/category_theory/preadditive/opposite.lean
9b6aeed3f23ad28c6ebc6074a9de2ff29a7f49b0
[ "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
2,988
lean
/- Copyright (c) 2021 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Adam Topaz, Johan Commelin, Joël Riou -/ import category_theory.preadditive import category_theory.preadditive.additive_functor import logic.equiv.transfer_instance /-! # If `C` is preadditive, `Cᵒᵖ` has a natural preadditive structure. -/ open opposite namespace category_theory variables (C : Type*) [category C] [preadditive C] instance : preadditive Cᵒᵖ := { hom_group := λ X Y, equiv.add_comm_group (op_equiv X Y), add_comp' := λ X Y Z f f' g, congr_arg quiver.hom.op (preadditive.comp_add _ _ _ g.unop f.unop f'.unop), comp_add' := λ X Y Z f g g', congr_arg quiver.hom.op (preadditive.add_comp _ _ _ g.unop g'.unop f.unop), } instance module_End_left {X : Cᵒᵖ} {Y : C} : module (End X) (unop X ⟶ Y) := { smul_add := λ r f g, preadditive.comp_add _ _ _ _ _ _, smul_zero := λ r, limits.comp_zero, add_smul := λ r s f, preadditive.add_comp _ _ _ _ _ _, zero_smul := λ f, limits.zero_comp } @[simp] lemma unop_zero (X Y : Cᵒᵖ) : (0 : X ⟶ Y).unop = 0 := rfl @[simp] lemma unop_add {X Y : Cᵒᵖ} (f g : X ⟶ Y) : (f + g).unop = f.unop + g.unop := rfl @[simp] lemma unop_zsmul {X Y : Cᵒᵖ} (k : ℤ) (f : X ⟶ Y) : (k • f).unop = k • f.unop := rfl @[simp] lemma unop_neg {X Y : Cᵒᵖ}(f : X ⟶ Y) : (-f).unop = -(f.unop) := rfl @[simp] lemma op_zero (X Y : C) : (0 : X ⟶ Y).op = 0 := rfl @[simp] lemma op_add {X Y : C} (f g : X ⟶ Y) : (f + g).op = f.op + g.op := rfl @[simp] lemma op_zsmul {X Y : C} (k : ℤ) (f : X ⟶ Y) : (k • f).op = k • f.op := rfl @[simp] lemma op_neg {X Y : C}(f : X ⟶ Y) : (-f).op = -(f.op) := rfl variable {C} /-- `unop` induces morphisms of monoids on hom groups of a preadditive category -/ @[simps] def unop_hom (X Y : Cᵒᵖ) : (X ⟶ Y) →+ (opposite.unop Y ⟶ opposite.unop X) := add_monoid_hom.mk' (λ f, f.unop) $ λ f g, unop_add _ f g @[simp] lemma unop_sum (X Y : Cᵒᵖ) {ι : Type*} (s : finset ι) (f : ι → (X ⟶ Y)) : (s.sum f).unop = s.sum (λ i, (f i).unop) := (unop_hom X Y).map_sum _ _ /-- `op` induces morphisms of monoids on hom groups of a preadditive category -/ @[simps] def op_hom (X Y : C) : (X ⟶ Y) →+ (opposite.op Y ⟶ opposite.op X) := add_monoid_hom.mk' (λ f, f.op) $ λ f g, op_add _ f g @[simp] lemma op_sum (X Y : C) {ι : Type*} (s : finset ι) (f : ι → (X ⟶ Y)) : (s.sum f).op = s.sum (λ i, (f i).op) := (op_hom X Y).map_sum _ _ variables {D : Type*} [category D] [preadditive D] instance functor.op_additive (F : C ⥤ D) [F.additive] : F.op.additive := {} instance functor.right_op_additive (F : Cᵒᵖ ⥤ D) [F.additive] : F.right_op.additive := {} instance functor.left_op_additive (F : C ⥤ Dᵒᵖ) [F.additive] : F.left_op.additive := {} instance functor.unop_additive (F : Cᵒᵖ ⥤ Dᵒᵖ) [F.additive] : F.unop.additive := {} end category_theory
a088d2d5f0c847d902d1d68f139457130bafe796
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/topology/algebra/order/monotone_continuity.lean
4671266286a4d1eb6b2257dd3fc7a51bb56a6e55
[ "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
17,338
lean
/- Copyright (c) 2021 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury G. Kudryashov, Heather Macbeth -/ import topology.order.basic import topology.homeomorph /-! # Continuity of monotone functions > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file we prove the following fact: if `f` is a monotone function on a neighborhood of `a` and the image of this neighborhood is a neighborhood of `f a`, then `f` is continuous at `a`, see `continuous_at_of_monotone_on_of_image_mem_nhds`, as well as several similar facts. We also prove that an `order_iso` is continuous. ## Tags continuous, monotone -/ open set filter open_locale topology section linear_order variables {α β : Type*} [linear_order α] [topological_space α] [order_topology α] variables [linear_order β] [topological_space β] [order_topology β] /-- If `f` is a function strictly monotone on a right neighborhood of `a` and the image of this neighborhood under `f` meets every interval `(f a, b]`, `b > f a`, then `f` is continuous at `a` from the right. The assumption `hfs : ∀ b > f a, ∃ c ∈ s, f c ∈ Ioc (f a) b` is required because otherwise the function `f : ℝ → ℝ` given by `f x = if x ≤ 0 then x else x + 1` would be a counter-example at `a = 0`. -/ lemma strict_mono_on.continuous_at_right_of_exists_between {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_on f s) (hs : s ∈ 𝓝[≥] a) (hfs : ∀ b > f a, ∃ c ∈ s, f c ∈ Ioc (f a) b) : continuous_within_at f (Ici a) a := begin have ha : a ∈ Ici a := left_mem_Ici, have has : a ∈ s := mem_of_mem_nhds_within ha hs, refine tendsto_order.2 ⟨λ b hb, _, λ b hb, _⟩, { filter_upwards [hs, self_mem_nhds_within] with _ hxs hxa using hb.trans_le ((h_mono.le_iff_le has hxs).2 hxa) }, { rcases hfs b hb with ⟨c, hcs, hac, hcb⟩, rw [h_mono.lt_iff_lt has hcs] at hac, filter_upwards [hs, Ico_mem_nhds_within_Ici (left_mem_Ico.2 hac)], rintros x hx ⟨hax, hxc⟩, exact ((h_mono.lt_iff_lt hx hcs).2 hxc).trans_le hcb } end /-- If `f` is a monotone function on a right neighborhood of `a` and the image of this neighborhood under `f` meets every interval `(f a, b)`, `b > f a`, then `f` is continuous at `a` from the right. The assumption `hfs : ∀ b > f a, ∃ c ∈ s, f c ∈ Ioo (f a) b` cannot be replaced by the weaker assumption `hfs : ∀ b > f a, ∃ c ∈ s, f c ∈ Ioc (f a) b` we use for strictly monotone functions because otherwise the function `ceil : ℝ → ℤ` would be a counter-example at `a = 0`. -/ lemma continuous_at_right_of_monotone_on_of_exists_between {f : α → β} {s : set α} {a : α} (h_mono : monotone_on f s) (hs : s ∈ 𝓝[≥] a) (hfs : ∀ b > f a, ∃ c ∈ s, f c ∈ Ioo (f a) b) : continuous_within_at f (Ici a) a := begin have ha : a ∈ Ici a := left_mem_Ici, have has : a ∈ s := mem_of_mem_nhds_within ha hs, refine tendsto_order.2 ⟨λ b hb, _, λ b hb, _⟩, { filter_upwards [hs, self_mem_nhds_within] with _ hxs hxa using hb.trans_le (h_mono has hxs hxa) }, { rcases hfs b hb with ⟨c, hcs, hac, hcb⟩, have : a < c, from not_le.1 (λ h, hac.not_le $ h_mono hcs has h), filter_upwards [hs, Ico_mem_nhds_within_Ici (left_mem_Ico.2 this)], rintros x hx ⟨hax, hxc⟩, exact (h_mono hx hcs hxc.le).trans_lt hcb } end /-- If a function `f` with a densely ordered codomain is monotone on a right neighborhood of `a` and the closure of the image of this neighborhood under `f` is a right neighborhood of `f a`, then `f` is continuous at `a` from the right. -/ lemma continuous_at_right_of_monotone_on_of_closure_image_mem_nhds_within [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : monotone_on f s) (hs : s ∈ 𝓝[≥] a) (hfs : closure (f '' s) ∈ 𝓝[≥] (f a)) : continuous_within_at f (Ici a) a := begin refine continuous_at_right_of_monotone_on_of_exists_between h_mono hs (λ b hb, _), rcases (mem_nhds_within_Ici_iff_exists_mem_Ioc_Ico_subset hb).1 hfs with ⟨b', ⟨hab', hbb'⟩, hb'⟩, rcases exists_between hab' with ⟨c', hc'⟩, rcases mem_closure_iff.1 (hb' ⟨hc'.1.le, hc'.2⟩) (Ioo (f a) b') is_open_Ioo hc' with ⟨_, hc, ⟨c, hcs, rfl⟩⟩, exact ⟨c, hcs, hc.1, hc.2.trans_le hbb'⟩ end /-- If a function `f` with a densely ordered codomain is monotone on a right neighborhood of `a` and the image of this neighborhood under `f` is a right neighborhood of `f a`, then `f` is continuous at `a` from the right. -/ lemma continuous_at_right_of_monotone_on_of_image_mem_nhds_within [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : monotone_on f s) (hs : s ∈ 𝓝[≥] a) (hfs : f '' s ∈ 𝓝[≥] (f a)) : continuous_within_at f (Ici a) a := continuous_at_right_of_monotone_on_of_closure_image_mem_nhds_within h_mono hs $ mem_of_superset hfs subset_closure /-- If a function `f` with a densely ordered codomain is strictly monotone on a right neighborhood of `a` and the closure of the image of this neighborhood under `f` is a right neighborhood of `f a`, then `f` is continuous at `a` from the right. -/ lemma strict_mono_on.continuous_at_right_of_closure_image_mem_nhds_within [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_on f s) (hs : s ∈ 𝓝[≥] a) (hfs : closure (f '' s) ∈ 𝓝[≥] (f a)) : continuous_within_at f (Ici a) a := continuous_at_right_of_monotone_on_of_closure_image_mem_nhds_within (λ x hx y hy, (h_mono.le_iff_le hx hy).2) hs hfs /-- If a function `f` with a densely ordered codomain is strictly monotone on a right neighborhood of `a` and the image of this neighborhood under `f` is a right neighborhood of `f a`, then `f` is continuous at `a` from the right. -/ lemma strict_mono_on.continuous_at_right_of_image_mem_nhds_within [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_on f s) (hs : s ∈ 𝓝[≥] a) (hfs : f '' s ∈ 𝓝[≥] (f a)) : continuous_within_at f (Ici a) a := h_mono.continuous_at_right_of_closure_image_mem_nhds_within hs (mem_of_superset hfs subset_closure) /-- If a function `f` is strictly monotone on a right neighborhood of `a` and the image of this neighborhood under `f` includes `Ioi (f a)`, then `f` is continuous at `a` from the right. -/ lemma strict_mono_on.continuous_at_right_of_surj_on {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_on f s) (hs : s ∈ 𝓝[≥] a) (hfs : surj_on f s (Ioi (f a))) : continuous_within_at f (Ici a) a := h_mono.continuous_at_right_of_exists_between hs $ λ b hb, let ⟨c, hcs, hcb⟩ := hfs hb in ⟨c, hcs, hcb.symm ▸ hb, hcb.le⟩ /-- If `f` is a strictly monotone function on a left neighborhood of `a` and the image of this neighborhood under `f` meets every interval `[b, f a)`, `b < f a`, then `f` is continuous at `a` from the left. The assumption `hfs : ∀ b < f a, ∃ c ∈ s, f c ∈ Ico b (f a)` is required because otherwise the function `f : ℝ → ℝ` given by `f x = if x < 0 then x else x + 1` would be a counter-example at `a = 0`. -/ lemma strict_mono_on.continuous_at_left_of_exists_between {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_on f s) (hs : s ∈ 𝓝[≤] a) (hfs : ∀ b < f a, ∃ c ∈ s, f c ∈ Ico b (f a)) : continuous_within_at f (Iic a) a := h_mono.dual.continuous_at_right_of_exists_between hs $ λ b hb, let ⟨c, hcs, hcb, hca⟩ := hfs b hb in ⟨c, hcs, hca, hcb⟩ /-- If `f` is a monotone function on a left neighborhood of `a` and the image of this neighborhood under `f` meets every interval `(b, f a)`, `b < f a`, then `f` is continuous at `a` from the left. The assumption `hfs : ∀ b < f a, ∃ c ∈ s, f c ∈ Ioo b (f a)` cannot be replaced by the weaker assumption `hfs : ∀ b < f a, ∃ c ∈ s, f c ∈ Ico b (f a)` we use for strictly monotone functions because otherwise the function `floor : ℝ → ℤ` would be a counter-example at `a = 0`. -/ lemma continuous_at_left_of_monotone_on_of_exists_between {f : α → β} {s : set α} {a : α} (hf : monotone_on f s) (hs : s ∈ 𝓝[≤] a) (hfs : ∀ b < f a, ∃ c ∈ s, f c ∈ Ioo b (f a)) : continuous_within_at f (Iic a) a := @continuous_at_right_of_monotone_on_of_exists_between αᵒᵈ βᵒᵈ _ _ _ _ _ _ f s a hf.dual hs $ λ b hb, let ⟨c, hcs, hcb, hca⟩ := hfs b hb in ⟨c, hcs, hca, hcb⟩ /-- If a function `f` with a densely ordered codomain is monotone on a left neighborhood of `a` and the closure of the image of this neighborhood under `f` is a left neighborhood of `f a`, then `f` is continuous at `a` from the left -/ lemma continuous_at_left_of_monotone_on_of_closure_image_mem_nhds_within [densely_ordered β] {f : α → β} {s : set α} {a : α} (hf : monotone_on f s) (hs : s ∈ 𝓝[≤] a) (hfs : closure (f '' s) ∈ 𝓝[≤] (f a)) : continuous_within_at f (Iic a) a := @continuous_at_right_of_monotone_on_of_closure_image_mem_nhds_within αᵒᵈ βᵒᵈ _ _ _ _ _ _ _ f s a hf.dual hs hfs /-- If a function `f` with a densely ordered codomain is monotone on a left neighborhood of `a` and the image of this neighborhood under `f` is a left neighborhood of `f a`, then `f` is continuous at `a` from the left. -/ lemma continuous_at_left_of_monotone_on_of_image_mem_nhds_within [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : monotone_on f s) (hs : s ∈ 𝓝[≤] a) (hfs : f '' s ∈ 𝓝[≤] (f a)) : continuous_within_at f (Iic a) a := continuous_at_left_of_monotone_on_of_closure_image_mem_nhds_within h_mono hs (mem_of_superset hfs subset_closure) /-- If a function `f` with a densely ordered codomain is strictly monotone on a left neighborhood of `a` and the closure of the image of this neighborhood under `f` is a left neighborhood of `f a`, then `f` is continuous at `a` from the left. -/ lemma strict_mono_on.continuous_at_left_of_closure_image_mem_nhds_within [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_on f s) (hs : s ∈ 𝓝[≤] a) (hfs : closure (f '' s) ∈ 𝓝[≤] (f a)) : continuous_within_at f (Iic a) a := h_mono.dual.continuous_at_right_of_closure_image_mem_nhds_within hs hfs /-- If a function `f` with a densely ordered codomain is strictly monotone on a left neighborhood of `a` and the image of this neighborhood under `f` is a left neighborhood of `f a`, then `f` is continuous at `a` from the left. -/ lemma strict_mono_on.continuous_at_left_of_image_mem_nhds_within [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_on f s) (hs : s ∈ 𝓝[≤] a) (hfs : f '' s ∈ 𝓝[≤] (f a)) : continuous_within_at f (Iic a) a := h_mono.dual.continuous_at_right_of_image_mem_nhds_within hs hfs /-- If a function `f` is strictly monotone on a left neighborhood of `a` and the image of this neighborhood under `f` includes `Iio (f a)`, then `f` is continuous at `a` from the left. -/ lemma strict_mono_on.continuous_at_left_of_surj_on {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_on f s) (hs : s ∈ 𝓝[≤] a) (hfs : surj_on f s (Iio (f a))) : continuous_within_at f (Iic a) a := h_mono.dual.continuous_at_right_of_surj_on hs hfs /-- If a function `f` is strictly monotone on a neighborhood of `a` and the image of this neighborhood under `f` meets every interval `[b, f a)`, `b < f a`, and every interval `(f a, b]`, `b > f a`, then `f` is continuous at `a`. -/ lemma strict_mono_on.continuous_at_of_exists_between {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_on f s) (hs : s ∈ 𝓝 a) (hfs_l : ∀ b < f a, ∃ c ∈ s, f c ∈ Ico b (f a)) (hfs_r : ∀ b > f a, ∃ c ∈ s, f c ∈ Ioc (f a) b) : continuous_at f a := continuous_at_iff_continuous_left_right.2 ⟨h_mono.continuous_at_left_of_exists_between (mem_nhds_within_of_mem_nhds hs) hfs_l, h_mono.continuous_at_right_of_exists_between (mem_nhds_within_of_mem_nhds hs) hfs_r⟩ /-- If a function `f` with a densely ordered codomain is strictly monotone on a neighborhood of `a` and the closure of the image of this neighborhood under `f` is a neighborhood of `f a`, then `f` is continuous at `a`. -/ lemma strict_mono_on.continuous_at_of_closure_image_mem_nhds [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_on f s) (hs : s ∈ 𝓝 a) (hfs : closure (f '' s) ∈ 𝓝 (f a)) : continuous_at f a := continuous_at_iff_continuous_left_right.2 ⟨h_mono.continuous_at_left_of_closure_image_mem_nhds_within (mem_nhds_within_of_mem_nhds hs) (mem_nhds_within_of_mem_nhds hfs), h_mono.continuous_at_right_of_closure_image_mem_nhds_within (mem_nhds_within_of_mem_nhds hs) (mem_nhds_within_of_mem_nhds hfs)⟩ /-- If a function `f` with a densely ordered codomain is strictly monotone on a neighborhood of `a` and the image of this set under `f` is a neighborhood of `f a`, then `f` is continuous at `a`. -/ lemma strict_mono_on.continuous_at_of_image_mem_nhds [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_on f s) (hs : s ∈ 𝓝 a) (hfs : f '' s ∈ 𝓝 (f a)) : continuous_at f a := h_mono.continuous_at_of_closure_image_mem_nhds hs (mem_of_superset hfs subset_closure) /-- If `f` is a monotone function on a neighborhood of `a` and the image of this neighborhood under `f` meets every interval `(b, f a)`, `b < f a`, and every interval `(f a, b)`, `b > f a`, then `f` is continuous at `a`. -/ lemma continuous_at_of_monotone_on_of_exists_between {f : α → β} {s : set α} {a : α} (h_mono : monotone_on f s) (hs : s ∈ 𝓝 a) (hfs_l : ∀ b < f a, ∃ c ∈ s, f c ∈ Ioo b (f a)) (hfs_r : ∀ b > f a, ∃ c ∈ s, f c ∈ Ioo (f a) b) : continuous_at f a := continuous_at_iff_continuous_left_right.2 ⟨continuous_at_left_of_monotone_on_of_exists_between h_mono (mem_nhds_within_of_mem_nhds hs) hfs_l, continuous_at_right_of_monotone_on_of_exists_between h_mono (mem_nhds_within_of_mem_nhds hs) hfs_r⟩ /-- If a function `f` with a densely ordered codomain is monotone on a neighborhood of `a` and the closure of the image of this neighborhood under `f` is a neighborhood of `f a`, then `f` is continuous at `a`. -/ lemma continuous_at_of_monotone_on_of_closure_image_mem_nhds [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : monotone_on f s) (hs : s ∈ 𝓝 a) (hfs : closure (f '' s) ∈ 𝓝 (f a)) : continuous_at f a := continuous_at_iff_continuous_left_right.2 ⟨continuous_at_left_of_monotone_on_of_closure_image_mem_nhds_within h_mono (mem_nhds_within_of_mem_nhds hs) (mem_nhds_within_of_mem_nhds hfs), continuous_at_right_of_monotone_on_of_closure_image_mem_nhds_within h_mono (mem_nhds_within_of_mem_nhds hs) (mem_nhds_within_of_mem_nhds hfs)⟩ /-- If a function `f` with a densely ordered codomain is monotone on a neighborhood of `a` and the image of this neighborhood under `f` is a neighborhood of `f a`, then `f` is continuous at `a`. -/ lemma continuous_at_of_monotone_on_of_image_mem_nhds [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : monotone_on f s) (hs : s ∈ 𝓝 a) (hfs : f '' s ∈ 𝓝 (f a)) : continuous_at f a := continuous_at_of_monotone_on_of_closure_image_mem_nhds h_mono hs (mem_of_superset hfs subset_closure) /-- A monotone function with densely ordered codomain and a dense range is continuous. -/ lemma monotone.continuous_of_dense_range [densely_ordered β] {f : α → β} (h_mono : monotone f) (h_dense : dense_range f) : continuous f := continuous_iff_continuous_at.mpr $ λ a, continuous_at_of_monotone_on_of_closure_image_mem_nhds (λ x hx y hy hxy, h_mono hxy) univ_mem $ by simp only [image_univ, h_dense.closure_eq, univ_mem] /-- A monotone surjective function with a densely ordered codomain is continuous. -/ lemma monotone.continuous_of_surjective [densely_ordered β] {f : α → β} (h_mono : monotone f) (h_surj : function.surjective f) : continuous f := h_mono.continuous_of_dense_range h_surj.dense_range end linear_order /-! ### Continuity of order isomorphisms In this section we prove that an `order_iso` is continuous, hence it is a `homeomorph`. We prove this for an `order_iso` between to partial orders with order topology. -/ namespace order_iso variables {α β : Type*} [partial_order α] [partial_order β] [topological_space α] [topological_space β] [order_topology α] [order_topology β] protected lemma continuous (e : α ≃o β) : continuous e := begin rw [‹order_topology β›.topology_eq_generate_intervals], refine continuous_generated_from (λ s hs, _), rcases hs with ⟨a, rfl|rfl⟩, { rw e.preimage_Ioi, apply is_open_lt' }, { rw e.preimage_Iio, apply is_open_gt' } end /-- An order isomorphism between two linear order `order_topology` spaces is a homeomorphism. -/ def to_homeomorph (e : α ≃o β) : α ≃ₜ β := { continuous_to_fun := e.continuous, continuous_inv_fun := e.symm.continuous, .. e } @[simp] lemma coe_to_homeomorph (e : α ≃o β) : ⇑e.to_homeomorph = e := rfl @[simp] lemma coe_to_homeomorph_symm (e : α ≃o β) : ⇑e.to_homeomorph.symm = e.symm := rfl end order_iso
659f54db50f8c17a946770a8d8de1a40ebcbf06d
63abd62053d479eae5abf4951554e1064a4c45b4
/src/algebraic_geometry/sheafed_space.lean
9367a2be67b360c30bbfb05747d9c55c69307791
[ "Apache-2.0" ]
permissive
Lix0120/mathlib
0020745240315ed0e517cbf32e738d8f9811dd80
e14c37827456fc6707f31b4d1d16f1f3a3205e91
refs/heads/master
1,673,102,855,024
1,604,151,044,000
1,604,151,044,000
308,930,245
0
0
Apache-2.0
1,604,164,710,000
1,604,163,547,000
null
UTF-8
Lean
false
false
4,576
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 algebraic_geometry.presheafed_space import topology.sheaves.sheaf /-! # Sheafed spaces Introduces the category of topological spaces equipped with a sheaf (taking values in an arbitrary target category `C`.) We further describe how to apply functors and natural transformations to the values of the presheaves. -/ universes v u open category_theory open Top open topological_space open opposite open category_theory.limits open category_theory.category category_theory.functor variables (C : Type u) [category.{v} C] [limits.has_products C] local attribute [tidy] tactic.op_induction' namespace algebraic_geometry /-- A `SheafedSpace C` is a topological space equipped with a sheaf of `C`s. -/ structure SheafedSpace extends PresheafedSpace C := (sheaf_condition : presheaf.sheaf_condition) variables {C} namespace SheafedSpace instance coe_carrier : has_coe (SheafedSpace C) Top := { coe := λ X, X.carrier } /-- Extract the `sheaf C (X : Top)` from a `SheafedSpace C`. -/ def sheaf (X : SheafedSpace C) : sheaf C (X : Top.{v}) := ⟨X.presheaf, X.sheaf_condition⟩ @[simp] lemma as_coe (X : SheafedSpace C) : X.carrier = (X : Top.{v}) := rfl @[simp] lemma mk_coe (carrier) (presheaf) (h) : (({ carrier := carrier, presheaf := presheaf, sheaf_condition := h } : SheafedSpace.{v} C) : Top.{v}) = carrier := rfl instance (X : SheafedSpace.{v} C) : topological_space X := X.carrier.str /-- The trivial `punit` valued sheaf on any topological space. -/ noncomputable def punit (X : Top) : SheafedSpace (discrete punit) := { sheaf_condition := presheaf.sheaf_condition_punit _, ..@PresheafedSpace.const (discrete punit) _ X punit.star } noncomputable instance : inhabited (SheafedSpace (discrete _root_.punit)) := ⟨punit (Top.of pempty)⟩ instance : category (SheafedSpace C) := show category (induced_category (PresheafedSpace C) SheafedSpace.to_PresheafedSpace), by apply_instance /-- Forgetting the sheaf condition is a functor from `SheafedSpace C` to `PresheafedSpace C`. -/ def forget_to_PresheafedSpace : (SheafedSpace C) ⥤ (PresheafedSpace C) := induced_functor _ variables {C} section local attribute [simp] id comp @[simp] lemma id_base (X : SheafedSpace C) : ((𝟙 X) : X ⟶ X).base = (𝟙 (X : Top.{v})) := rfl lemma id_c (X : SheafedSpace C) : ((𝟙 X) : X ⟶ X).c = (((functor.left_unitor _).inv) ≫ (whisker_right (nat_trans.op (opens.map_id (X.carrier)).hom) _)) := rfl @[simp] lemma id_c_app (X : SheafedSpace C) (U) : ((𝟙 X) : X ⟶ X).c.app U = eq_to_hom (by { op_induction U, cases U, refl }) := by { op_induction U, cases U, simp only [id_c], dsimp, simp, } @[simp] lemma comp_base {X Y Z : SheafedSpace C} (f : X ⟶ Y) (g : Y ⟶ Z) : (f ≫ g).base = f.base ≫ g.base := rfl @[simp] lemma comp_c_app {X Y Z : SheafedSpace C} (α : X ⟶ Y) (β : Y ⟶ Z) (U) : (α ≫ β).c.app U = (β.c).app U ≫ (α.c).app (op ((opens.map (β.base)).obj (unop U))) ≫ (Top.presheaf.pushforward.comp _ _ _).inv.app U := rfl variables (C) /-- The forgetful functor from `SheafedSpace` to `Top`. -/ def forget : SheafedSpace C ⥤ Top := { obj := λ X, (X : Top.{v}), map := λ X Y f, f.base } end open Top.presheaf /-- The restriction of a sheafed space along an open embedding into the space. -/ noncomputable def restrict {U : Top} (X : SheafedSpace C) (f : U ⟶ (X : Top.{v})) (h : open_embedding f) : SheafedSpace C := { sheaf_condition := λ ι 𝒰, is_limit.of_iso_limit ((is_limit.postcompose_inv_equiv _ _).inv_fun (X.sheaf_condition _)) (sheaf_condition_equalizer_products.fork.iso_of_open_embedding h 𝒰).symm, ..X.to_PresheafedSpace.restrict f h } /-- The global sections, notated Gamma. -/ def Γ : (SheafedSpace C)ᵒᵖ ⥤ C := forget_to_PresheafedSpace.op ⋙ PresheafedSpace.Γ lemma Γ_def : (Γ : _ ⥤ C) = forget_to_PresheafedSpace.op ⋙ PresheafedSpace.Γ := rfl @[simp] lemma Γ_obj (X : (SheafedSpace C)ᵒᵖ) : Γ.obj X = (unop X).presheaf.obj (op ⊤) := rfl lemma Γ_obj_op (X : SheafedSpace C) : Γ.obj (op X) = X.presheaf.obj (op ⊤) := rfl @[simp] lemma Γ_map {X Y : (SheafedSpace C)ᵒᵖ} (f : X ⟶ Y) : Γ.map f = f.unop.c.app (op ⊤) ≫ (unop Y).presheaf.map (opens.le_map_top _ _).op := rfl lemma Γ_map_op {X Y : SheafedSpace C} (f : X ⟶ Y) : Γ.map f.op = f.c.app (op ⊤) ≫ X.presheaf.map (opens.le_map_top _ _).op := rfl end SheafedSpace end algebraic_geometry
b0c2393c5a31c82f4539eb9b845a83060731945a
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/set_theory/cardinal/continuum.lean
550db497dc7e74c9e48667c79d66fe95896f05f7
[ "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
3,417
lean
/- Copyright (c) 2021 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import set_theory.cardinal.ordinal /-! # Cardinality of continuum In this file we define `cardinal.continuum` (notation: `𝔠`, localized in `cardinal`) to be `2 ^ ℵ₀`. We also prove some `simp` lemmas about cardinal arithmetic involving `𝔠`. ## Notation - `𝔠` : notation for `cardinal.continuum` in locale `cardinal`. -/ namespace cardinal universes u v open_locale cardinal /-- Cardinality of continuum. -/ def continuum : cardinal.{u} := 2 ^ aleph_0.{u} localized "notation `𝔠` := cardinal.continuum" in cardinal @[simp] lemma two_power_aleph_0 : 2 ^ aleph_0.{u} = continuum.{u} := rfl @[simp] lemma lift_continuum : lift.{v} 𝔠 = 𝔠 := by rw [←two_power_aleph_0, lift_two_power, lift_aleph_0, two_power_aleph_0] /-! ### Inequalities -/ lemma aleph_0_lt_continuum : ℵ₀ < 𝔠 := cantor ℵ₀ lemma aleph_0_le_continuum : ℵ₀ ≤ 𝔠 := aleph_0_lt_continuum.le @[simp] lemma beth_one : beth 1 = 𝔠 := by simpa using beth_succ 0 lemma nat_lt_continuum (n : ℕ) : ↑n < 𝔠 := (nat_lt_aleph_0 n).trans aleph_0_lt_continuum lemma mk_set_nat : #(set ℕ) = 𝔠 := by simp lemma continuum_pos : 0 < 𝔠 := nat_lt_continuum 0 lemma continuum_ne_zero : 𝔠 ≠ 0 := continuum_pos.ne' lemma aleph_one_le_continuum : aleph 1 ≤ 𝔠 := by { rw ←succ_aleph_0, exact order.succ_le_of_lt aleph_0_lt_continuum } @[simp] theorem continuum_to_nat : continuum.to_nat = 0 := to_nat_apply_of_aleph_0_le aleph_0_le_continuum @[simp] theorem continuum_to_part_enat : continuum.to_part_enat = ⊤ := to_part_enat_apply_of_aleph_0_le aleph_0_le_continuum /-! ### Addition -/ @[simp] lemma aleph_0_add_continuum : ℵ₀ + 𝔠 = 𝔠 := add_eq_right aleph_0_le_continuum aleph_0_le_continuum @[simp] lemma continuum_add_aleph_0 : 𝔠 + ℵ₀ = 𝔠 := (add_comm _ _).trans aleph_0_add_continuum @[simp] lemma continuum_add_self : 𝔠 + 𝔠 = 𝔠 := add_eq_right aleph_0_le_continuum le_rfl @[simp] lemma nat_add_continuum (n : ℕ) : ↑n + 𝔠 = 𝔠 := add_eq_right aleph_0_le_continuum (nat_lt_continuum n).le @[simp] lemma continuum_add_nat (n : ℕ) : 𝔠 + n = 𝔠 := (add_comm _ _).trans (nat_add_continuum n) /-! ### Multiplication -/ @[simp] lemma continuum_mul_self : 𝔠 * 𝔠 = 𝔠 := mul_eq_left aleph_0_le_continuum le_rfl continuum_ne_zero @[simp] lemma continuum_mul_aleph_0 : 𝔠 * ℵ₀ = 𝔠 := mul_eq_left aleph_0_le_continuum aleph_0_le_continuum aleph_0_ne_zero @[simp] lemma aleph_0_mul_continuum : ℵ₀ * 𝔠 = 𝔠 := (mul_comm _ _).trans continuum_mul_aleph_0 @[simp] lemma nat_mul_continuum {n : ℕ} (hn : n ≠ 0) : ↑n * 𝔠 = 𝔠 := mul_eq_right aleph_0_le_continuum (nat_lt_continuum n).le (nat.cast_ne_zero.2 hn) @[simp] lemma continuum_mul_nat {n : ℕ} (hn : n ≠ 0) : 𝔠 * n = 𝔠 := (mul_comm _ _).trans (nat_mul_continuum hn) /-! ### Power -/ @[simp] lemma aleph_0_power_aleph_0 : aleph_0.{u} ^ aleph_0.{u} = 𝔠 := power_self_eq le_rfl @[simp] lemma nat_power_aleph_0 {n : ℕ} (hn : 2 ≤ n) : (n ^ aleph_0.{u} : cardinal.{u}) = 𝔠 := nat_power_eq le_rfl hn @[simp] lemma continuum_power_aleph_0 : continuum.{u} ^ aleph_0.{u} = 𝔠 := by rw [←two_power_aleph_0, ←power_mul, mul_eq_left le_rfl le_rfl aleph_0_ne_zero] end cardinal
2ec7460c7a5ce09118520feebe77cdbc5a4dc645
7b66d83f3b69dae0a3dfb684d7ebe5e9e3f3c913
/src/exercises_sources/thursday/afternoon/linear_algebra.lean
e061950bb334852d4f8b2730ab6aeab312e9a23e
[]
permissive
dpochekutov/lftcm2020
58a09e10f0e638075b97884d3c2612eb90296adb
cdfbf1ac089f21058e523db73f2476c9c98ed16a
refs/heads/master
1,669,226,265,076
1,594,629,725,000
1,594,629,725,000
279,213,346
1
0
MIT
1,594,622,757,000
1,594,615,843,000
null
UTF-8
Lean
false
false
7,013
lean
import analysis.normed_space.real_inner_product import data.matrix.notation import linear_algebra.bilinear_form import linear_algebra.matrix import tactic universes u v section exercise1 namespace semimodule variables (R M : Type*) [comm_semiring R] [add_comm_monoid M] [semimodule R M] /- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Exercise 1: defining modules and submodules - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -/ /-- The endomorphisms of an `R`-semimodule `M` are the `R`-linear maps from `M` to `M`. -/ def End := M →ₗ[R] M /-- The following line tells Lean we can apply `f : End R M` as if it was a function. -/ instance : has_coe_to_fun (End R M) := { F := λ _, M → M, coe := linear_map.to_fun } /-- Endomorphisms inherit the pointwise addition operator from linear maps. -/ instance : add_comm_monoid (End R M) := linear_map.add_comm_monoid /- Define the identity endomorphism `id`. -/ def End.id : End R M := sorry /- Show that the endomorphisms of `M` form a semimodule over `R`. Hint: we can re-use the scalar multiplication of linear maps using the `refine` tactic: ``` refine { smul := linear_map.has_scalar.smul, .. }, ``` This will fill in the `smul` field of the `semimodule` structure with the given value. The remaining fields become goals that you can fill in yourself. Hint: Prove the equalities using the semimodule structure on `M`. If `f` and `g` are linear maps, the `ext` tactic turns the goal `f = g` into `∀ x, f x = g x`. -/ instance : semimodule R (End R M) := begin sorry end variables {R M} /- Bonus exercise: define the submodule of `End R M` consisting of the scalar multiplications. That is, `f ∈ homothety R M` iff `f` is of the form `λ (x : M), s • x` for some `s : R`. Hints: * You could specify the carrier subset and show it is closed under the operations. * You could instead use library functions: try `submodule.map` or `linear_map.range`. -/ def homothety : submodule R (End R M) := sorry end semimodule end exercise1 section exercise2 namespace matrix variables {m n R M : Type} [fintype m] [fintype n] [comm_ring R] [add_comm_group M] [module R M] /- The following line allows us to write `⬝` (`\cdot`) and `ᵀ` (`\^T`) for matrix multiplication and transpose. -/ open_locale matrix /- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Exercise 2: working with matrices - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -/ /-- Prove the following four lemmas, that were missing from `mathlib`. Hints: * Look up the definition of `vec_mul` and `mul_vec`. * Search the library for useful lemmas about the function used in that definition. -/ @[simp] lemma add_vec_mul (v w : m → R) (M : matrix m n R) : vec_mul (v + w) M = vec_mul v M + vec_mul w M := sorry @[simp] lemma smul_vec_mul (x : R) (v : m → R) (M : matrix m n R) : vec_mul (x • v) M = x • vec_mul v M := sorry @[simp] lemma mul_vec_add (M : matrix m n R) (v w : n → R) : mul_vec M (v + w) = mul_vec M v + mul_vec M w := sorry @[simp] lemma mul_vec_smul (M : matrix m n R) (x : R) (v : n → R) : mul_vec M (x • v) = x • mul_vec M v := sorry /- Define the canonical map from bilinear forms to matrices. We assume `R` has a basis `v` indexed by `ι`. Hint: Follow your nose, the types will guide you. A matrix `A : matrix ι ι R` is not much more than a function `ι → ι → R`, and a bilinear form is not much more than a function `M → M → R`. -/ def bilin_form_to_matrix {ι : Type*} [fintype ι] (v : ι → M) (B : bilin_form R M) : matrix ι ι R := sorry /-- Define the canonical map from matrices to bilinear forms. For a matrix `A`, `to_bilin_form A` should take two vectors `v`, `w` and multiply `A` by `v` on the left and `v` on the right. -/ def matrix_to_bilin_form (A : matrix n n R) : bilin_form R (n → R) := sorry /- Can you define a bilinear form directly that is equivalent to this matrix `A`? Don't use `bilin_form_to_matrix`, give the map explicitly in the form `λ v w, _`. Check your definition by putting your cursor on the lines starting with `#eval`. Hints: * Use the `simp` tactic to simplify `(x + y) i` to `x i + y i` and `(s • x) i` to `s * x i`. * To deal with equalities containing many `+` and `*` symbols, use the `ring` tactic. -/ def A : matrix (fin 2) (fin 2) R := ![![1, 0], ![-2, 1]] def your_bilin_form : bilin_form R (fin 2 → R) := sorry /- Check your definition here: -/ def v : fin 2 → ℤ := ![1, 3] def w : fin 2 → ℤ := ![2, 4] #eval matrix_to_bilin_form A v w #eval your_bilin_form v w end matrix end exercise2 section exercise3 namespace pi variables {n : Type*} [fintype n] open matrix /- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Exercise 3: inner product spaces - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -/ /- Use the `dot_product` function to put an inner product on `n → R`. Hints: * Try the lemmas `finset.sum_nonneg`, `finset.sum_eq_zero_iff_of_nonneg`, `mul_self_nonneg` and `mul_self_eq_zero`. -/ noncomputable instance : inner_product_space (n → ℝ) := inner_product_space.of_core sorry end pi end exercise3 section exercise4 namespace pi variables (R n : Type) [comm_ring R] [fintype n] [decidable_eq n] /- Enable sum and product notation with `∑` and `∏`. -/ open_locale big_operators /- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Exercise 4: basis and dimension - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -/ /-- The `i`'th vector in the standard basis of `n → R` is `1` at the `i`th entry and `0` otherwise. -/ def std_basis (i : n) : (n → R) := λ j, if i = j then 1 else 0 /- Bonus exercise: Show the standard basis of `n → R` is a basis. This is a difficult exercise, so feel free to skip some parts. Hints for showing linear independence: * Try using the lemma `linear_independent_iff` or `linear_independent_iff'`. * To derive `f x = 0` from `h : f = 0`, use a tactic `have := congr_fun h x`. * Take a term out of a sum by combining `finset.insert_erase` and `finset.sum_insert`. Hints for showing it spans the whole module: * To show equality of set-like terms, apply the `ext` tactic. * First show `x = ∑ i, x i • std_basis R n i`, then rewrite with this equality. -/ lemma std_basis_is_basis : is_basis R (std_basis R n) := sorry variables {K : Type} [field K] /- Conclude `n → K` is a finite dimensional vector space for each field `K` and the dimension of `n → K` over `K` is the cardinality of `n`. You don't need to complete `std_basis_is_basis` to prove these two lemmas. Hint: search the library for appropriate lemmas. -/ lemma finite_dimensional : finite_dimensional K (n → K) := sorry lemma findim_eq : finite_dimensional.findim K (n → K) = fintype.card n := sorry end pi end exercise4
b50fe2f804388942ed2414a51fa2f78698244008
ba4794a0deca1d2aaa68914cd285d77880907b5c
/src/game/world9/level3.lean
75cf4dc8153fe08ae96f5d516883ac872fb4bffb
[ "Apache-2.0" ]
permissive
ChrisHughes24/natural_number_game
c7c00aa1f6a95004286fd456ed13cf6e113159ce
9d09925424da9f6275e6cfe427c8bcf12bb0944f
refs/heads/master
1,600,715,773,528
1,573,910,462,000
1,573,910,462,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
606
lean
import game.world9.level2 -- hide namespace mynat -- hide /- # Advanced Multiplication World ## Level 3: `mul_eq_zero_iff` Now you have `eq_zero_or_eq_zero_of_mul_eq_zero` this is pretty straightforward. -/ /- Theorem $ab = 0$, if and only if at least one of $a$ or $b$ is equal to zero. -/ theorem mul_eq_zero_iff (a b : mynat): a * b = 0 ↔ a = 0 ∨ b = 0 := begin [less_leaky] split, swap, intro hab, cases hab, rw hab, rw zero_mul, refl, rw hab, rw mul_zero, refl, intro h, exact eq_zero_or_eq_zero_of_mul_eq_zero a b h, end end mynat -- hide
94a075eb73f6b1609de9bbd40201b774d266a825
947b78d97130d56365ae2ec264df196ce769371a
/tests/lean/json.lean
82296ad5e544775f7b6de76cedb14a71f05385b2
[ "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
607
lean
import Lean.Data.Json.Parser import Lean.Data.Json.Printer new_frontend def test (s : String) : String := match Lean.Json.parse s with | Except.ok res => toString res | Except.error err => err #eval test "null" #eval test "false" #eval test "true" #eval test "123.456e-7" #eval test "-0.01e8" #eval test "\"\"" #eval test "\"abc\"" #eval test "[true, 123, \"foo\", []]" #eval test "{\"a\": 1.2, \"b\": \"foo\", \"c\": null, \"d\": {\"foo\": \"bar\"}, \"e\": [{}]}" #eval test "[false_]" #eval test "[" #eval test "]" #eval test "{" #eval test "\"" #eval test "1." #eval test "{foo: 1}" #eval test " "
06a5d7d0b828842da2e769f6bb96acb6066a6268
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/archive/imo/imo2005_q3.lean
2bb48ae6cda387eaf5bbca8fbd728c83ec63feff
[ "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
3,428
lean
/- Copyright (c) 2021 Manuel Candales. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Manuel Candales -/ import data.real.basic /-! # IMO 2005 Q3 Let `x`, `y` and `z` be positive real numbers such that `xyz ≥ 1`. Prove that: `(x^5 - x^2)/(x^5 + y^2 + z^2) + (y^5 - y^2)/(y^5 + z^2 + x^2) + (z^5 - z^2)/(z^5 + x^2 + y^2) ≥ 0` # Solution The solution by Iurie Boreico from Moldova is presented, which won a special prize during the exam. The key insight is that `(x^5-x^2)/(x^5+y^2+z^2) ≥ (x^2-y*z)/(x^2+y^2+z^2)`, which is proven by factoring `(x^5-x^2)/(x^5+y^2+z^2) - (x^5-x^2)/(x^3*(x^2+y^2+z^2))` into a non-negative expression and then making use of `xyz ≥ 1` to show `(x^5-x^2)/(x^3*(x^2+y^2+z^2)) ≥ (x^2-y*z)/(x^2+y^2+z^2)`. -/ lemma key_insight (x y z : ℝ) (hx : x > 0) (hy : y > 0) (hz : z > 0) (h : x*y*z ≥ 1) : (x^5-x^2)/(x^5+y^2+z^2) ≥ (x^2-y*z)/(x^2+y^2+z^2) := begin have h₁ : x^5+y^2+z^2 > 0, linarith [pow_pos hx 5, pow_pos hy 2, pow_pos hz 2], have h₂ : x^3 > 0, exact pow_pos hx 3, have h₃ : x^2+y^2+z^2 > 0, linarith [pow_pos hx 2, pow_pos hy 2, pow_pos hz 2], have h₄ : x^3*(x^2+y^2+z^2) > 0, exact mul_pos h₂ h₃, have key : (x^5-x^2)/(x^5+y^2+z^2) - (x^5-x^2)/(x^3*(x^2+y^2+z^2)) = ((x^3 - 1)^2*x^2*(y^2 + z^2))/((x^5+y^2+z^2)*(x^3*(x^2+y^2+z^2))), calc (x^5-x^2)/(x^5+y^2+z^2) - (x^5-x^2)/(x^3*(x^2+y^2+z^2)) = ((x^5-x^2)*(x^3*(x^2+y^2+z^2))-(x^5+y^2+z^2)*(x^5-x^2))/((x^5+y^2+z^2)*(x^3*(x^2+y^2+z^2))): by exact div_sub_div _ _ (ne_of_gt h₁) (ne_of_gt h₄) -- a/b - c/d = (a*d-b*c)/(b*d) ... = ((x^3 - 1)^2*x^2*(y^2 + z^2))/((x^5+y^2+z^2)*(x^3*(x^2+y^2+z^2))) : by ring, have h₅ : ((x^3 - 1)^2*x^2*(y^2 + z^2))/((x^5+y^2+z^2)*(x^3*(x^2+y^2+z^2))) ≥ 0, { refine div_nonneg _ _, refine mul_nonneg (mul_nonneg (sq_nonneg _) (sq_nonneg _)) _, exact add_nonneg (sq_nonneg _) (sq_nonneg _), exact le_of_lt (mul_pos h₁ h₄) }, calc (x^5-x^2)/(x^5+y^2+z^2) ≥ (x^5-x^2)/(x^3*(x^2+y^2+z^2)) : by linarith [key, h₅] ... ≥ (x^5-x^2*(x*y*z))/(x^3*(x^2+y^2+z^2)) : by { refine (div_le_div_right h₄).mpr _, simp, exact (le_mul_iff_one_le_right (pow_pos hx 2)).mpr h } ... = (x^2-y*z)/(x^2+y^2+z^2) : by { field_simp [ne_of_gt h₂, ne_of_gt h₃], ring }, end theorem imo2005_q3 (x y z : ℝ) (hx : x > 0) (hy : y > 0) (hz : z > 0) (h : x*y*z ≥ 1) : (x^5-x^2)/(x^5+y^2+z^2) + (y^5-y^2)/(y^5+z^2+x^2) + (z^5-z^2)/(z^5+x^2+y^2) ≥ 0 := begin calc (x^5-x^2)/(x^5+y^2+z^2) + (y^5-y^2)/(y^5+z^2+x^2) + (z^5-z^2)/(z^5+x^2+y^2) ≥ (x^2-y*z)/(x^2+y^2+z^2) + (y^2-z*x)/(y^2+z^2+x^2) + (z^2-x*y)/(z^2+x^2+y^2) : by { linarith [key_insight x y z hx hy hz h, key_insight y z x hy hz hx (by linarith [h]), key_insight z x y hz hx hy (by linarith [h])] } ... = (x^2 + y^2 + z^2 - y*z - z*x - x*y) / (x^2+y^2+z^2) : by { have h₁ : y^2+z^2+x^2 = x^2+y^2+z^2, linarith, have h₂ : z^2+x^2+y^2 = x^2+y^2+z^2, linarith, rw [h₁, h₂], ring } ... = 1/2*( (x-y)^2 + (y-z)^2 + (z-x)^2 ) / (x^2+y^2+z^2) : by ring ... ≥ 0 : by { exact div_nonneg (by linarith [sq_nonneg (x-y), sq_nonneg (y-z), sq_nonneg (z-x)]) (by linarith [sq_nonneg x, sq_nonneg y, sq_nonneg z]) }, end
717ee1d34b089decf480e84aa5b7d64f76f44ad6
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/trust10/simplifier_assoc.lean
9b573a4b017a0d6dbc417235c57fd7a17aaf7178
[ "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
816
lean
open tactic constants (A : Type 1) (a b c d e f g h : A) (op : A → A → A) (op_assoc : is_associative op) attribute op_assoc [instance] infixr `%%` := op namespace pat2 constant H : a %% b = f local attribute H [simp] example : a %% b = f := by simp example : c %% a %% b = c %% f := by simp example : a %% b %% c = f %% c := by simp example : c %% a %% b %% d = c %% f %% d := by simp example : c %% d %% a %% b %% c %% d = c %% d %% f %% c %% d := by simp end pat2 namespace pat3 constant H : (a %% b) %% g = f attribute H [simp] example : a %% b %% g = f := by simp example : c %% a %% b %% g = c %% f := by simp example : a %% b %% g %% c = f %% c := by simp example : c %% a %% b %% g %% d = c %% f %% d := by simp example : c %% d %% a %% b %% g %% c %% d = c %% d %% f %% c %% d := by simp end pat3
7c339c463ad1a3ca6e5872c8233f0b8e73b99558
c777c32c8e484e195053731103c5e52af26a25d1
/src/measure_theory/covering/besicovitch_vector_space.lean
9cba2ebdeafdb5a663025779eeea2b6587955e36
[ "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
26,067
lean
/- Copyright (c) 2021 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 measure_theory.measure.haar_lebesgue import measure_theory.covering.besicovitch /-! # Satellite configurations for Besicovitch covering lemma in vector spaces The Besicovitch covering theorem ensures that, in a nice metric space, there exists a number `N` such that, from any family of balls with bounded radii, one can extract `N` families, each made of disjoint balls, covering together all the centers of the initial family. A key tool in the proof of this theorem is the notion of a satellite configuration, i.e., a family of `N + 1` balls, where the first `N` balls all intersect the last one, but none of them contains the center of another one and their radii are controlled. This is a technical notion, but it shows up naturally in the proof of the Besicovitch theorem (which goes through a greedy algorithm): to ensure that in the end one needs at most `N` families of balls, the crucial property of the underlying metric space is that there should be no satellite configuration of `N + 1` points. This file is devoted to the study of this property in vector spaces: we prove the main result of [Füredi and Loeb, On the best constant for the Besicovitch covering theorem][furedi-loeb1994], which shows that the optimal such `N` in a vector space coincides with the maximal number of points one can put inside the unit ball of radius `2` under the condition that their distances are bounded below by `1`. In particular, this number is bounded by `5 ^ dim` by a straightforward measure argument. ## Main definitions and results * `multiplicity E` is the maximal number of points one can put inside the unit ball of radius `2` in the vector space `E`, under the condition that their distances are bounded below by `1`. * `multiplicity_le E` shows that `multiplicity E ≤ 5 ^ (dim E)`. * `good_τ E` is a constant `> 1`, but close enough to `1` that satellite configurations with this parameter `τ` are not worst than for `τ = 1`. * `is_empty_satellite_config_multiplicity` is the main theorem, saying that there are no satellite configurations of `(multiplicity E) + 1` points, for the parameter `good_τ E`. -/ universe u open metric set finite_dimensional measure_theory filter fin open_locale ennreal topology noncomputable theory namespace besicovitch variables {E : Type*} [normed_add_comm_group E] namespace satellite_config variables [normed_space ℝ E] {N : ℕ} {τ : ℝ} (a : satellite_config E N τ) /-- Rescaling a satellite configuration in a vector space, to put the basepoint at `0` and the base radius at `1`. -/ def center_and_rescale : satellite_config E N τ := { c := λ i, (a.r (last N))⁻¹ • (a.c i - a.c (last N)), r := λ i, (a.r (last N))⁻¹ * a.r i, rpos := λ i, mul_pos (inv_pos.2 (a.rpos _)) (a.rpos _), h := λ i j hij, begin rcases a.h i j hij with H|H, { left, split, { rw [dist_eq_norm, ← smul_sub, norm_smul, real.norm_eq_abs, abs_of_nonneg (inv_nonneg.2 ((a.rpos _)).le)], refine mul_le_mul_of_nonneg_left _ (inv_nonneg.2 ((a.rpos _)).le), rw [dist_eq_norm] at H, convert H.1 using 2, abel }, { rw [← mul_assoc, mul_comm τ, mul_assoc], refine mul_le_mul_of_nonneg_left _ (inv_nonneg.2 ((a.rpos _)).le), exact H.2 } }, { right, split, { rw [dist_eq_norm, ← smul_sub, norm_smul, real.norm_eq_abs, abs_of_nonneg (inv_nonneg.2 ((a.rpos _)).le)], refine mul_le_mul_of_nonneg_left _ (inv_nonneg.2 ((a.rpos _)).le), rw [dist_eq_norm] at H, convert H.1 using 2, abel }, { rw [← mul_assoc, mul_comm τ, mul_assoc], refine mul_le_mul_of_nonneg_left _ (inv_nonneg.2 ((a.rpos _)).le), exact H.2 } }, end, hlast := λ i hi, begin have H := a.hlast i hi, split, { rw [dist_eq_norm, ← smul_sub, norm_smul, real.norm_eq_abs, abs_of_nonneg (inv_nonneg.2 ((a.rpos _)).le)], refine mul_le_mul_of_nonneg_left _ (inv_nonneg.2 ((a.rpos _)).le), rw [dist_eq_norm] at H, convert H.1 using 2, abel }, { rw [← mul_assoc, mul_comm τ, mul_assoc], refine mul_le_mul_of_nonneg_left _ (inv_nonneg.2 ((a.rpos _)).le), exact H.2 } end, inter := λ i hi, begin have H := a.inter i hi, rw [dist_eq_norm, ← smul_sub, norm_smul, real.norm_eq_abs, abs_of_nonneg (inv_nonneg.2 ((a.rpos _)).le), ← mul_add], refine mul_le_mul_of_nonneg_left _ (inv_nonneg.2 ((a.rpos _)).le), rw dist_eq_norm at H, convert H using 2, abel end } lemma center_and_rescale_center : a.center_and_rescale.c (last N) = 0 := by simp [satellite_config.center_and_rescale] lemma center_and_rescale_radius {N : ℕ} {τ : ℝ} (a : satellite_config E N τ) : a.center_and_rescale.r (last N) = 1 := by simp [satellite_config.center_and_rescale, inv_mul_cancel (a.rpos _).ne'] end satellite_config /-! ### Disjoint balls of radius close to `1` in the radius `2` ball. -/ /-- The maximum cardinality of a `1`-separated set in the ball of radius `2`. This is also the optimal number of families in the Besicovitch covering theorem. -/ def multiplicity (E : Type*) [normed_add_comm_group E] := Sup {N | ∃ s : finset E, s.card = N ∧ (∀ c ∈ s, ‖c‖ ≤ 2) ∧ (∀ c ∈ s, ∀ d ∈ s, c ≠ d → 1 ≤ ‖c - d‖)} section variables [normed_space ℝ E] [finite_dimensional ℝ E] /-- Any `1`-separated set in the ball of radius `2` has cardinality at most `5 ^ dim`. This is useful to show that the supremum in the definition of `besicovitch.multiplicity E` is well behaved. -/ lemma card_le_of_separated (s : finset E) (hs : ∀ c ∈ s, ‖c‖ ≤ 2) (h : ∀ (c ∈ s) (d ∈ s), c ≠ d → 1 ≤ ‖c - d‖) : s.card ≤ 5 ^ (finrank ℝ E) := begin /- We consider balls of radius `1/2` around the points in `s`. They are disjoint, and all contained in the ball of radius `5/2`. A volume argument gives `s.card * (1/2)^dim ≤ (5/2)^dim`, i.e., `s.card ≤ 5^dim`. -/ borelize E, let μ : measure E := measure.add_haar, let δ : ℝ := (1 : ℝ)/2, let ρ : ℝ := (5 : ℝ)/2, have ρpos : 0 < ρ := by norm_num [ρ], set A := ⋃ (c ∈ s), ball (c : E) δ with hA, have D : set.pairwise (s : set E) (disjoint on (λ c, ball (c : E) δ)), { rintros c hc d hd hcd, apply ball_disjoint_ball, rw dist_eq_norm, convert h c hc d hd hcd, norm_num }, have A_subset : A ⊆ ball (0 : E) ρ, { refine Union₂_subset (λ x hx, _), apply ball_subset_ball', calc δ + dist x 0 ≤ δ + 2 : by { rw dist_zero_right, exact add_le_add le_rfl (hs x hx) } ... = 5 / 2 : by norm_num [δ] }, have I : (s.card : ℝ≥0∞) * ennreal.of_real (δ ^ (finrank ℝ E)) * μ (ball 0 1) ≤ ennreal.of_real (ρ ^ (finrank ℝ E)) * μ (ball 0 1) := calc (s.card : ℝ≥0∞) * ennreal.of_real (δ ^ (finrank ℝ E)) * μ (ball 0 1) = μ A : begin rw [hA, measure_bUnion_finset D (λ c hc, measurable_set_ball)], have I : 0 < δ, by norm_num [δ], simp only [μ.add_haar_ball_of_pos _ I, one_div, one_pow, finset.sum_const, nsmul_eq_mul, div_pow, mul_assoc] end ... ≤ μ (ball (0 : E) ρ) : measure_mono A_subset ... = ennreal.of_real (ρ ^ (finrank ℝ E)) * μ (ball 0 1) : by simp only [μ.add_haar_ball_of_pos _ ρpos], have J : (s.card : ℝ≥0∞) * ennreal.of_real (δ ^ (finrank ℝ E)) ≤ ennreal.of_real (ρ ^ (finrank ℝ E)) := (ennreal.mul_le_mul_right (measure_ball_pos _ _ zero_lt_one).ne' measure_ball_lt_top.ne).1 I, have K : (s.card : ℝ) ≤ (5 : ℝ) ^ finrank ℝ E, by simpa [ennreal.to_real_mul, div_eq_mul_inv] using ennreal.to_real_le_of_le_of_real (pow_nonneg ρpos.le _) J, exact_mod_cast K, end lemma multiplicity_le : multiplicity E ≤ 5 ^ (finrank ℝ E) := begin apply cSup_le, { refine ⟨0, ⟨∅, by simp⟩⟩ }, { rintros _ ⟨s, ⟨rfl, h⟩⟩, exact besicovitch.card_le_of_separated s h.1 h.2 } end lemma card_le_multiplicity {s : finset E} (hs : ∀ c ∈ s, ‖c‖ ≤ 2) (h's : ∀ (c ∈ s) (d ∈ s), c ≠ d → 1 ≤ ‖c - d‖) : s.card ≤ multiplicity E := begin apply le_cSup, { refine ⟨5 ^ (finrank ℝ E), _⟩, rintros _ ⟨s, ⟨rfl, h⟩⟩, exact besicovitch.card_le_of_separated s h.1 h.2 }, { simp only [mem_set_of_eq, ne.def], exact ⟨s, rfl, hs, h's⟩ } end variable (E) /-- If `δ` is small enough, a `(1-δ)`-separated set in the ball of radius `2` also has cardinality at most `multiplicity E`. -/ lemma exists_good_δ : ∃ (δ : ℝ), 0 < δ ∧ δ < 1 ∧ ∀ (s : finset E), (∀ c ∈ s, ‖c‖ ≤ 2) → (∀ (c ∈ s) (d ∈ s), c ≠ d → 1 - δ ≤ ‖c - d‖) → s.card ≤ multiplicity E := begin /- This follows from a compactness argument: otherwise, one could extract a converging subsequence, to obtain a `1`-separated set in the ball of radius `2` with cardinality `N = multiplicity E + 1`. To formalize this, we work with functions `fin N → E`. -/ classical, by_contra' h, set N := multiplicity E + 1 with hN, have : ∀ (δ : ℝ), 0 < δ → ∃ f : fin N → E, (∀ (i : fin N), ‖f i‖ ≤ 2) ∧ (∀ i j, i ≠ j → 1 - δ ≤ ‖f i - f j‖), { assume δ hδ, rcases lt_or_le δ 1 with hδ'|hδ', { rcases h δ hδ hδ' with ⟨s, hs, h's, s_card⟩, obtain ⟨f, f_inj, hfs⟩ : ∃ (f : fin N → E), function.injective f ∧ range f ⊆ ↑s, { have : fintype.card (fin N) ≤ s.card, { simp only [fintype.card_fin], exact s_card }, rcases function.embedding.exists_of_card_le_finset this with ⟨f, hf⟩, exact ⟨f, f.injective, hf⟩ }, simp only [range_subset_iff, finset.mem_coe] at hfs, refine ⟨f, λ i, hs _ (hfs i), λ i j hij, h's _ (hfs i) _ (hfs j) (f_inj.ne hij)⟩ }, { exact ⟨λ i, 0, λ i, by simp, λ i j hij, by simpa only [norm_zero, sub_nonpos, sub_self]⟩ } }, -- For `δ > 0`, `F δ` is a function from `fin N` to the ball of radius `2` for which two points -- in the image are separated by `1 - δ`. choose! F hF using this, -- Choose a converging subsequence when `δ → 0`. have : ∃ f : fin N → E, (∀ (i : fin N), ‖f i‖ ≤ 2) ∧ (∀ i j, i ≠ j → 1 ≤ ‖f i - f j‖), { obtain ⟨u, u_mono, zero_lt_u, hu⟩ : ∃ (u : ℕ → ℝ), (∀ (m n : ℕ), m < n → u n < u m) ∧ (∀ (n : ℕ), 0 < u n) ∧ filter.tendsto u filter.at_top (𝓝 0) := exists_seq_strict_anti_tendsto (0 : ℝ), have A : ∀ n, F (u n) ∈ closed_ball (0 : fin N → E) 2, { assume n, simp only [pi_norm_le_iff_of_nonneg zero_le_two, mem_closed_ball, dist_zero_right, (hF (u n) (zero_lt_u n)).left, forall_const], }, obtain ⟨f, fmem, φ, φ_mono, hf⟩ : ∃ (f ∈ closed_ball (0 : fin N → E) 2) (φ : ℕ → ℕ), strict_mono φ ∧ tendsto ((F ∘ u) ∘ φ) at_top (𝓝 f) := is_compact.tendsto_subseq (is_compact_closed_ball _ _) A, refine ⟨f, λ i, _, λ i j hij, _⟩, { simp only [pi_norm_le_iff_of_nonneg zero_le_two, mem_closed_ball, dist_zero_right] at fmem, exact fmem i }, { have A : tendsto (λ n, ‖F (u (φ n)) i - F (u (φ n)) j‖) at_top (𝓝 (‖f i - f j‖)) := ((hf.apply i).sub (hf.apply j)).norm, have B : tendsto (λ n, 1 - u (φ n)) at_top (𝓝 (1 - 0)) := tendsto_const_nhds.sub (hu.comp φ_mono.tendsto_at_top), rw sub_zero at B, exact le_of_tendsto_of_tendsto' B A (λ n, (hF (u (φ n)) (zero_lt_u _)).2 i j hij) } }, rcases this with ⟨f, hf, h'f⟩, -- the range of `f` contradicts the definition of `multiplicity E`. have finj : function.injective f, { assume i j hij, by_contra, have : 1 ≤ ‖f i - f j‖ := h'f i j h, simp only [hij, norm_zero, sub_self] at this, exact lt_irrefl _ (this.trans_lt zero_lt_one) }, let s := finset.image f finset.univ, have s_card : s.card = N, by { rw finset.card_image_of_injective _ finj, exact finset.card_fin N }, have hs : ∀ c ∈ s, ‖c‖ ≤ 2, by simp only [hf, forall_apply_eq_imp_iff', forall_const, forall_exists_index, finset.mem_univ, finset.mem_image], have h's : ∀ (c ∈ s) (d ∈ s), c ≠ d → 1 ≤ ‖c - d‖, { simp only [s, forall_apply_eq_imp_iff', forall_exists_index, finset.mem_univ, finset.mem_image, ne.def, exists_true_left, forall_apply_eq_imp_iff', forall_true_left], assume i j hij, have : i ≠ j := λ h, by { rw h at hij, exact hij rfl }, exact h'f i j this }, have : s.card ≤ multiplicity E := card_le_multiplicity hs h's, rw [s_card, hN] at this, exact lt_irrefl _ ((nat.lt_succ_self (multiplicity E)).trans_le this), end /-- A small positive number such that any `1 - δ`-separated set in the ball of radius `2` has cardinality at most `besicovitch.multiplicity E`. -/ def good_δ : ℝ := (exists_good_δ E).some lemma good_δ_lt_one : good_δ E < 1 := (exists_good_δ E).some_spec.2.1 /-- A number `τ > 1`, but chosen close enough to `1` so that the construction in the Besicovitch covering theorem using this parameter `τ` will give the smallest possible number of covering families. -/ def good_τ : ℝ := 1 + (good_δ E) / 4 lemma one_lt_good_τ : 1 < good_τ E := by { dsimp [good_τ, good_δ], linarith [(exists_good_δ E).some_spec.1] } variable {E} lemma card_le_multiplicity_of_δ {s : finset E} (hs : ∀ c ∈ s, ‖c‖ ≤ 2) (h's : ∀ (c ∈ s) (d ∈ s), c ≠ d → 1 - good_δ E ≤ ‖c - d‖) : s.card ≤ multiplicity E := (classical.some_spec (exists_good_δ E)).2.2 s hs h's lemma le_multiplicity_of_δ_of_fin {n : ℕ} (f : fin n → E) (h : ∀ i, ‖f i‖ ≤ 2) (h' : ∀ i j, i ≠ j → 1 - good_δ E ≤ ‖f i - f j‖) : n ≤ multiplicity E := begin classical, have finj : function.injective f, { assume i j hij, by_contra, have : 1 - good_δ E ≤ ‖f i - f j‖ := h' i j h, simp only [hij, norm_zero, sub_self] at this, linarith [good_δ_lt_one E] }, let s := finset.image f finset.univ, have s_card : s.card = n, by { rw finset.card_image_of_injective _ finj, exact finset.card_fin n }, have hs : ∀ c ∈ s, ‖c‖ ≤ 2, by simp only [h, forall_apply_eq_imp_iff', forall_const, forall_exists_index, finset.mem_univ, finset.mem_image, implies_true_iff], have h's : ∀ (c ∈ s) (d ∈ s), c ≠ d → 1 - good_δ E ≤ ‖c - d‖, { simp only [s, forall_apply_eq_imp_iff', forall_exists_index, finset.mem_univ, finset.mem_image, ne.def, exists_true_left, forall_apply_eq_imp_iff', forall_true_left], assume i j hij, have : i ≠ j := λ h, by { rw h at hij, exact hij rfl }, exact h' i j this }, have : s.card ≤ multiplicity E := card_le_multiplicity_of_δ hs h's, rwa [s_card] at this, end end namespace satellite_config /-! ### Relating satellite configurations to separated points in the ball of radius `2`. We prove that the number of points in a satellite configuration is bounded by the maximal number of `1`-separated points in the ball of radius `2`. For this, start from a satellite congifuration `c`. Without loss of generality, one can assume that the last ball is centered at `0` and of radius `1`. Define `c' i = c i` if `‖c i‖ ≤ 2`, and `c' i = (2/‖c i‖) • c i` if `‖c i‖ > 2`. It turns out that these points are `1 - δ`-separated, where `δ` is arbitrarily small if `τ` is close enough to `1`. The number of such configurations is bounded by `multiplicity E` if `δ` is suitably small. To check that the points `c' i` are `1 - δ`-separated, one treats separately the cases where both `‖c i‖` and `‖c j‖` are `≤ 2`, where one of them is `≤ 2` and the other one is `> 2`, and where both of them are `> 2`. -/ lemma exists_normalized_aux1 {N : ℕ} {τ : ℝ} (a : satellite_config E N τ) (lastr : a.r (last N) = 1) (hτ : 1 ≤ τ) (δ : ℝ) (hδ1 : τ ≤ 1 + δ / 4) (hδ2 : δ ≤ 1) (i j : fin N.succ) (inej : i ≠ j) : 1 - δ ≤ ‖a.c i - a.c j‖ := begin have ah : ∀ i j, i ≠ j → (a.r i ≤ ‖a.c i - a.c j‖ ∧ a.r j ≤ τ * a.r i) ∨ (a.r j ≤ ‖a.c j - a.c i‖ ∧ a.r i ≤ τ * a.r j), by simpa only [dist_eq_norm] using a.h, have δnonneg : 0 ≤ δ := by linarith only [hτ, hδ1], have D : 0 ≤ 1 - δ / 4, by linarith only [hδ2], have τpos : 0 < τ := _root_.zero_lt_one.trans_le hτ, have I : (1 - δ / 4) * τ ≤ 1 := calc (1 - δ / 4) * τ ≤ (1 - δ / 4) * (1 + δ / 4) : mul_le_mul_of_nonneg_left hδ1 D ... = 1 - δ^2 / 16 : by ring ... ≤ 1 : (by linarith only [sq_nonneg δ]), have J : 1 - δ ≤ 1 - δ / 4, by linarith only [δnonneg], have K : 1 - δ / 4 ≤ τ⁻¹, by { rw [inv_eq_one_div, le_div_iff τpos], exact I }, suffices L : τ⁻¹ ≤ ‖a.c i - a.c j‖, by linarith only [J, K, L], have hτ' : ∀ k, τ⁻¹ ≤ a.r k, { assume k, rw [inv_eq_one_div, div_le_iff τpos, ← lastr, mul_comm], exact a.hlast' k hτ }, rcases ah i j inej with H|H, { apply le_trans _ H.1, exact hτ' i }, { rw norm_sub_rev, apply le_trans _ H.1, exact hτ' j } end variable [normed_space ℝ E] lemma exists_normalized_aux2 {N : ℕ} {τ : ℝ} (a : satellite_config E N τ) (lastc : a.c (last N) = 0) (lastr : a.r (last N) = 1) (hτ : 1 ≤ τ) (δ : ℝ) (hδ1 : τ ≤ 1 + δ / 4) (hδ2 : δ ≤ 1) (i j : fin N.succ) (inej : i ≠ j) (hi : ‖a.c i‖ ≤ 2) (hj : 2 < ‖a.c j‖) : 1 - δ ≤ ‖a.c i - (2 / ‖a.c j‖) • a.c j‖ := begin have ah : ∀ i j, i ≠ j → (a.r i ≤ ‖a.c i - a.c j‖ ∧ a.r j ≤ τ * a.r i) ∨ (a.r j ≤ ‖a.c j - a.c i‖ ∧ a.r i ≤ τ * a.r j), by simpa only [dist_eq_norm] using a.h, have δnonneg : 0 ≤ δ := by linarith only [hτ, hδ1], have D : 0 ≤ 1 - δ / 4, by linarith only [hδ2], have τpos : 0 < τ := _root_.zero_lt_one.trans_le hτ, have hcrj : ‖a.c j‖ ≤ a.r j + 1, by simpa only [lastc, lastr, dist_zero_right] using a.inter' j, have I : a.r i ≤ 2, { rcases lt_or_le i (last N) with H|H, { apply (a.hlast i H).1.trans, simpa only [dist_eq_norm, lastc, sub_zero] using hi }, { have : i = last N := top_le_iff.1 H, rw [this, lastr], exact one_le_two } }, have J : (1 - δ / 4) * τ ≤ 1 := calc (1 - δ / 4) * τ ≤ (1 - δ / 4) * (1 + δ / 4) : mul_le_mul_of_nonneg_left hδ1 D ... = 1 - δ^2 / 16 : by ring ... ≤ 1 : (by linarith only [sq_nonneg δ]), have A : a.r j - δ ≤ ‖a.c i - a.c j‖, { rcases ah j i inej.symm with H|H, { rw norm_sub_rev, linarith [H.1] }, have C : a.r j ≤ 4 := calc a.r j ≤ τ * a.r i : H.2 ... ≤ τ * 2 : mul_le_mul_of_nonneg_left I τpos.le ... ≤ (5/4) * 2 : mul_le_mul_of_nonneg_right (by linarith only [hδ1, hδ2]) zero_le_two ... ≤ 4 : by norm_num, calc a.r j - δ ≤ a.r j - (a.r j / 4) * δ : begin refine sub_le_sub le_rfl _, refine mul_le_of_le_one_left δnonneg _, linarith only [C], end ... = (1 - δ / 4) * a.r j : by ring ... ≤ (1 - δ / 4) * (τ * a.r i) : mul_le_mul_of_nonneg_left (H.2) D ... ≤ 1 * a.r i : by { rw [← mul_assoc], apply mul_le_mul_of_nonneg_right J (a.rpos _).le } ... ≤ ‖a.c i - a.c j‖ : by { rw [one_mul], exact H.1 } }, set d := (2 / ‖a.c j‖) • a.c j with hd, have : a.r j - δ ≤ ‖a.c i - d‖ + (a.r j - 1) := calc a.r j - δ ≤ ‖a.c i - a.c j‖ : A ... ≤ ‖a.c i - d‖ + ‖d - a.c j‖ : by simp only [← dist_eq_norm, dist_triangle] ... ≤ ‖a.c i - d‖ + (a.r j - 1) : begin apply add_le_add_left, have A : 0 ≤ 1 - 2 / ‖a.c j‖, by simpa [div_le_iff (zero_le_two.trans_lt hj)] using hj.le, rw [← one_smul ℝ (a.c j), hd, ← sub_smul, norm_smul, norm_sub_rev, real.norm_eq_abs, abs_of_nonneg A, sub_mul], field_simp [(zero_le_two.trans_lt hj).ne'], linarith only [hcrj] end, linarith only [this] end lemma exists_normalized_aux3 {N : ℕ} {τ : ℝ} (a : satellite_config E N τ) (lastc : a.c (last N) = 0) (lastr : a.r (last N) = 1) (hτ : 1 ≤ τ) (δ : ℝ) (hδ1 : τ ≤ 1 + δ / 4) (i j : fin N.succ) (inej : i ≠ j) (hi : 2 < ‖a.c i‖) (hij : ‖a.c i‖ ≤ ‖a.c j‖) : 1 - δ ≤ ‖(2 / ‖a.c i‖) • a.c i - (2 / ‖a.c j‖) • a.c j‖ := begin have ah : ∀ i j, i ≠ j → (a.r i ≤ ‖a.c i - a.c j‖ ∧ a.r j ≤ τ * a.r i) ∨ (a.r j ≤ ‖a.c j - a.c i‖ ∧ a.r i ≤ τ * a.r j), by simpa only [dist_eq_norm] using a.h, have δnonneg : 0 ≤ δ := by linarith only [hτ, hδ1], have τpos : 0 < τ := _root_.zero_lt_one.trans_le hτ, have hcrj : ‖a.c j‖ ≤ a.r j + 1, by simpa only [lastc, lastr, dist_zero_right] using a.inter' j, have A : a.r i ≤ ‖a.c i‖, { have : i < last N, { apply lt_top_iff_ne_top.2, assume iN, change i = last N at iN, rw [iN, lastc, norm_zero] at hi, exact lt_irrefl _ (zero_le_two.trans_lt hi) }, convert (a.hlast i this).1, rw [dist_eq_norm, lastc, sub_zero] }, have hj : 2 < ‖a.c j‖ := hi.trans_le hij, set s := ‖a.c i‖ with hs, have spos : 0 < s := zero_lt_two.trans hi, set d := (s/‖a.c j‖) • a.c j with hd, have I : ‖a.c j - a.c i‖ ≤ ‖a.c j‖ - s + ‖d - a.c i‖ := calc ‖a.c j - a.c i‖ ≤ ‖a.c j - d‖ + ‖d - a.c i‖ : by simp [← dist_eq_norm, dist_triangle] ... = ‖a.c j‖ - ‖a.c i‖ + ‖d - a.c i‖ : begin nth_rewrite 0 ← one_smul ℝ (a.c j), rw [add_left_inj, hd, ← sub_smul, norm_smul, real.norm_eq_abs, abs_of_nonneg, sub_mul, one_mul, div_mul_cancel _ (zero_le_two.trans_lt hj).ne'], rwa [sub_nonneg, div_le_iff (zero_lt_two.trans hj), one_mul], end, have J : a.r j - ‖a.c j - a.c i‖ ≤ s / 2 * δ := calc a.r j - ‖a.c j - a.c i‖ ≤ s * (τ - 1) : begin rcases ah j i inej.symm with H|H, { calc a.r j - ‖a.c j - a.c i‖ ≤ 0 : sub_nonpos.2 H.1 ... ≤ s * (τ - 1) : mul_nonneg spos.le (sub_nonneg.2 hτ) }, { rw norm_sub_rev at H, calc a.r j - ‖a.c j - a.c i‖ ≤ τ * a.r i - a.r i : sub_le_sub H.2 H.1 ... = a.r i * (τ - 1) : by ring ... ≤ s * (τ - 1) : mul_le_mul_of_nonneg_right A (sub_nonneg.2 hτ) } end ... ≤ s * (δ / 2) : mul_le_mul_of_nonneg_left (by linarith only [δnonneg, hδ1]) spos.le ... = s / 2 * δ : by ring, have invs_nonneg : 0 ≤ 2 / s := (div_nonneg zero_le_two (zero_le_two.trans hi.le)), calc 1 - δ = (2 / s) * (s / 2 - (s / 2) * δ) : by { field_simp [spos.ne'], ring } ... ≤ (2 / s) * ‖d - a.c i‖ : mul_le_mul_of_nonneg_left (by linarith only [hcrj, I, J, hi]) invs_nonneg ... = ‖(2 / s) • a.c i - (2 / ‖a.c j‖) • a.c j‖ : begin conv_lhs { rw [norm_sub_rev, ← abs_of_nonneg invs_nonneg] }, rw [← real.norm_eq_abs, ← norm_smul, smul_sub, hd, smul_smul], congr' 3, field_simp [spos.ne'], end end lemma exists_normalized {N : ℕ} {τ : ℝ} (a : satellite_config E N τ) (lastc : a.c (last N) = 0) (lastr : a.r (last N) = 1) (hτ : 1 ≤ τ) (δ : ℝ) (hδ1 : τ ≤ 1 + δ / 4) (hδ2 : δ ≤ 1) : ∃ (c' : fin N.succ → E), (∀ n, ‖c' n‖ ≤ 2) ∧ (∀ i j, i ≠ j → 1 - δ ≤ ‖c' i - c' j‖) := begin let c' : fin N.succ → E := λ i, if ‖a.c i‖ ≤ 2 then a.c i else (2 / ‖a.c i‖) • a.c i, have norm_c'_le : ∀ i, ‖c' i‖ ≤ 2, { assume i, simp only [c'], split_ifs, { exact h }, by_cases hi : ‖a.c i‖ = 0; field_simp [norm_smul, hi] }, refine ⟨c', λ n, norm_c'_le n, λ i j inej, _⟩, -- up to exchanging `i` and `j`, one can assume `∥c i∥ ≤ ∥c j∥`. wlog hij : ‖a.c i‖ ≤ ‖a.c j‖ generalizing i j, { rw norm_sub_rev, exact this j i inej.symm (le_of_not_le hij) }, rcases le_or_lt (‖a.c j‖) 2 with Hj|Hj, -- case `∥c j∥ ≤ 2` (and therefore also `∥c i∥ ≤ 2`) { simp_rw [c', Hj, hij.trans Hj, if_true], exact exists_normalized_aux1 a lastr hτ δ hδ1 hδ2 i j inej }, -- case `2 < ‖c j‖` { have H'j : (‖a.c j‖ ≤ 2) ↔ false, by simpa only [not_le, iff_false] using Hj, rcases le_or_lt (‖a.c i‖) 2 with Hi|Hi, { -- case `‖c i‖ ≤ 2` simp_rw [c', Hi, if_true, H'j, if_false], exact exists_normalized_aux2 a lastc lastr hτ δ hδ1 hδ2 i j inej Hi Hj }, { -- case `2 < ‖c i‖` have H'i : (‖a.c i‖ ≤ 2) ↔ false, by simpa only [not_le, iff_false] using Hi, simp_rw [c', H'i, if_false, H'j, if_false], exact exists_normalized_aux3 a lastc lastr hτ δ hδ1 i j inej Hi hij } } end end satellite_config variables (E) [normed_space ℝ E] [finite_dimensional ℝ E] /-- In a normed vector space `E`, there can be no satellite configuration with `multiplicity E + 1` points and the parameter `good_τ E`. This will ensure that in the inductive construction to get the Besicovitch covering families, there will never be more than `multiplicity E` nonempty families. -/ theorem is_empty_satellite_config_multiplicity : is_empty (satellite_config E (multiplicity E) (good_τ E)) := ⟨begin assume a, let b := a.center_and_rescale, rcases b.exists_normalized (a.center_and_rescale_center) (a.center_and_rescale_radius) (one_lt_good_τ E).le (good_δ E) le_rfl (good_δ_lt_one E).le with ⟨c', c'_le_two, hc'⟩, exact lt_irrefl _ ((nat.lt_succ_self _).trans_le (le_multiplicity_of_δ_of_fin c' c'_le_two hc')) end⟩ @[priority 100] instance : has_besicovitch_covering E := ⟨⟨multiplicity E, good_τ E, one_lt_good_τ E, is_empty_satellite_config_multiplicity E⟩⟩ end besicovitch
1dab2457e426f8266b4324b3c7d1b644563b8f8f
9cb9db9d79fad57d80ca53543dc07efb7c4f3838
/src/pseudo_normed_group/profinitely_filtered.lean
ba28853c54eb5ed69c7a11f105172f77bab97a5e
[]
no_license
mr-infty/lean-liquid
3ff89d1f66244b434654c59bdbd6b77cb7de0109
a8db559073d2101173775ccbd85729d3a4f1ed4d
refs/heads/master
1,678,465,145,334
1,614,565,310,000
1,614,565,310,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
17,737
lean
import algebra.punit_instances import topology.algebra.group import pseudo_normed_group.basic import hacks_and_tricks.type_pow import facts open pseudo_normed_group open_locale nnreal big_operators local attribute [instance] type_pow /-- A *profinitely filtered pseudo normed topological group* is * an abelian group `M` with an increasing filtration `filtration M c, c : ℝ≥0` such that * `filtration M c` is a profinite set * `M` is pseudo normed, so `0 ∈ filtration M c`, `-(filtration M c = filtration M c`, and `x₁ ∈ filtration M c₁, x₂ ∈ filtration M c₂ → (x₁ + x₂) ∈ filtration M (c₁ + c₂)` * (bounded) addition and negation are continuous. Morphisms are continuous and bounded homomorphisms. -/ class profinitely_filtered_pseudo_normed_group (M : Type*) extends pseudo_normed_group M := [topology : ∀ c, topological_space (filtration c)] [t2 : ∀ c, t2_space (filtration c)] [td : ∀ c, totally_disconnected_space (filtration c)] [compact : ∀ c, compact_space (filtration c)] (continuous_add' : ∀ (c₁ c₂), continuous (add' : filtration c₁ × filtration c₂ → filtration (c₁ + c₂))) (continuous_neg' : ∀ c, continuous (neg' : filtration c → filtration c)) (continuous_cast_le : ∀ (c₁ c₂) [h : fact (c₁ ≤ c₂)], continuous (cast_le : filtration c₁ → filtration c₂)) namespace profinitely_filtered_pseudo_normed_group variables {M M₁ M₂ M₃ : Type*} variables [profinitely_filtered_pseudo_normed_group M] variables [profinitely_filtered_pseudo_normed_group M₁] variables [profinitely_filtered_pseudo_normed_group M₂] variables [profinitely_filtered_pseudo_normed_group M₃] instance (c : ℝ≥0) : topological_space (filtration M c) := topology c instance (c : ℝ≥0) : t2_space (filtration M c) := t2 c instance (c : ℝ≥0) : totally_disconnected_space (filtration M c) := td c instance (c : ℝ≥0) : compact_space (filtration M c) := compact c lemma is_closed_map_cast_le (c₁ c₂) [h : fact (c₁ ≤ c₂)] : is_closed_map (@pseudo_normed_group.cast_le M _ _ _ h) := (continuous_cast_le c₁ c₂).is_closed_map lemma closed_embedding_cast_le (c₁ c₂) [h : fact (c₁ ≤ c₂)] : closed_embedding (@pseudo_normed_group.cast_le M _ _ _ h) := closed_embedding_of_continuous_injective_closed (continuous_cast_le c₁ c₂) (injective_cast_le c₁ c₂) (is_closed_map_cast_le c₁ c₂) lemma embedding_cast_le (c₁ c₂) [h : fact (c₁ ≤ c₂)] : embedding (@pseudo_normed_group.cast_le M _ _ _ h) := (closed_embedding_cast_le c₁ c₂).to_embedding end profinitely_filtered_pseudo_normed_group section set_option old_structure_cmd true structure profinitely_filtered_pseudo_normed_group_hom (M₁ M₂ : Type*) [profinitely_filtered_pseudo_normed_group M₁] [profinitely_filtered_pseudo_normed_group M₂] extends M₁ →+ M₂ := (bound' : ∃ C, ∀ c x, x ∈ filtration M₁ c → to_fun x ∈ filtration M₂ (C * c)) (continuous' : ∀ ⦃c₁ c₂⦄ (f₀ : filtration M₁ c₁ → filtration M₂ c₂) (h : ∀ x, to_fun ↑x = f₀ x), continuous f₀) end attribute [nolint doc_blame] profinitely_filtered_pseudo_normed_group_hom.mk profinitely_filtered_pseudo_normed_group_hom.to_add_monoid_hom namespace profinitely_filtered_pseudo_normed_group_hom open profinitely_filtered_pseudo_normed_group variables {M M₁ M₂ M₃ : Type*} variables [profinitely_filtered_pseudo_normed_group M] variables [profinitely_filtered_pseudo_normed_group M₁] variables [profinitely_filtered_pseudo_normed_group M₂] variables [profinitely_filtered_pseudo_normed_group M₃] variables (f g : profinitely_filtered_pseudo_normed_group_hom M₁ M₂) instance : has_coe_to_fun (profinitely_filtered_pseudo_normed_group_hom M₁ M₂) := ⟨_, profinitely_filtered_pseudo_normed_group_hom.to_fun⟩ @[simp] lemma coe_mk (f) (h₁) (h₂) (h₃) (h₄) : ⇑(⟨f, h₁, h₂, h₃, h₄⟩ : profinitely_filtered_pseudo_normed_group_hom M₁ M₂) = f := rfl @[simp] lemma mk_to_monoid_hom (f) (h₁) (h₂) (h₃) (h₄) : (⟨f, h₁, h₂, h₃, h₄⟩ : profinitely_filtered_pseudo_normed_group_hom M₁ M₂).to_add_monoid_hom = ⟨f, h₁, h₂⟩ := rfl @[simp] lemma map_zero : f 0 = 0 := f.to_add_monoid_hom.map_zero @[simp] lemma map_add (x y) : f (x + y) = f x + f y := f.to_add_monoid_hom.map_add _ _ @[simp] lemma map_sum {ι : Type*} (x : ι → M₁) (s : finset ι) : f (∑ i in s, x i) = ∑ i in s, f (x i) := f.to_add_monoid_hom.map_sum _ _ @[simp] lemma map_sub (x y) : f (x - y) = f x - f y := f.to_add_monoid_hom.map_sub _ _ @[simp] lemma map_neg (x) : f (-x) = -(f x) := f.to_add_monoid_hom.map_neg _ /-- Make a profinitely filtered pseudo normed group hom from a group hom and a proof that it is bounded and continuous. -/ def mk' (f : M₁ →+ M₂) (h : ∃ C, ∀ c, ∃ (H : ∀ x, x ∈ filtration M₁ c → f x ∈ filtration M₂ (C * c)), @continuous (filtration M₁ c) (filtration M₂ (C * c)) _ _ (λ x, ⟨f x, H x x.2⟩)) : profinitely_filtered_pseudo_normed_group_hom M₁ M₂ := { bound' := by { obtain ⟨C, hC⟩ := h, refine ⟨C, λ c, (hC c).some⟩ }, continuous' := λ c₁ c₂ f₀ hf₀, begin obtain ⟨C, hC⟩ := h, obtain ⟨_, H⟩ := hC c₁, haveI : fact ((C * c₁) ≤ max (C * c₁) c₂) := le_max_left _ _, haveI : fact (c₂ ≤ max (C * c₁) c₂) := le_max_right _ _, rw (embedding_cast_le c₂ (max (C * c₁) c₂)).continuous_iff, rw (embedding_cast_le (C * c₁) (max (C * c₁) c₂)).continuous_iff at H, convert H using 1, ext, dsimp, rw ← hf₀, refl end, .. f } @[simp] lemma coe_mk' (f : M₁ →+ M₂) (h) : ⇑(mk' f h) = f := rfl lemma bound : ∃ C, ∀ ⦃c x⦄, x ∈ filtration M₁ c → f x ∈ filtration M₂ (C * c) := f.bound' protected lemma continuous ⦃c₁ c₂⦄ (f₀ : filtration M₁ c₁ → filtration M₂ c₂) (h : ∀ x, f ↑x = f₀ x) : continuous f₀ := f.continuous' f₀ h -- /-- `f.level c` is the function `filtration M₁ c → filtration M₂ c` -- induced by a `profinitely_filtered_pseudo_normed_group_hom M₁ M₂`. -/ -- @[simps] def level (c : ℝ≥0) (x : filtration M₁ c) : filtration M₂ c := ⟨f x, f.strict x.2⟩ -- lemma level_continuous (c : ℝ≥0) : continuous (f.level c) := f.continuous' c variables {f g} @[ext] theorem ext (H : ∀ x, f x = g x) : f = g := by cases f; cases g; congr'; exact funext H instance : has_zero (profinitely_filtered_pseudo_normed_group_hom M₁ M₂) := ⟨mk' (0 : M₁ →+ M₂) ⟨0, λ c, ⟨λ _ _, zero_mem_filtration _, @continuous_const _ _ _ _ 0⟩⟩⟩ instance : inhabited (profinitely_filtered_pseudo_normed_group_hom M₁ M₂) := ⟨0⟩ lemma coe_inj ⦃f g : profinitely_filtered_pseudo_normed_group_hom M₁ M₂⦄ (h : (f : M₁ → M₂) = g) : f = g := by cases f; cases g; cases h; refl /-- The identity function as `profinitely_filtered_pseudo_normed_group_hom`. -/ @[simps] def id : profinitely_filtered_pseudo_normed_group_hom M M := mk' (add_monoid_hom.id _) $ begin refine ⟨1, λ c, ⟨_, _⟩⟩, { intros, rwa one_mul }, haveI : fact (1 * c ≤ c) := by { apply le_of_eq, rw one_mul }, rw (embedding_cast_le (1 * c) c).continuous_iff, convert continuous_id, ext, refl end /-- The composition of `profinitely_filtered_pseudo_normed_group_hom`s. -/ @[simps] def comp (g : profinitely_filtered_pseudo_normed_group_hom M₂ M₃) (f : profinitely_filtered_pseudo_normed_group_hom M₁ M₂) : profinitely_filtered_pseudo_normed_group_hom M₁ M₃ := mk' (g.to_add_monoid_hom.comp f.to_add_monoid_hom) $ begin obtain ⟨Cf, hCf⟩ := f.bound, obtain ⟨Cg, hCg⟩ := g.bound, refine ⟨Cg * Cf, λ c, ⟨_, _⟩⟩, { intros x hx, rw mul_assoc, exact hCg (hCf hx) }, let f₀ : filtration M₁ c → filtration M₂ (Cf * c) := λ x, ⟨f x, hCf x.2⟩, have hf₀ : continuous f₀ := f.continuous _ (λ x, rfl), let g₀ : filtration M₂ (Cf * c) → filtration M₃ (Cg * (Cf * c)) := λ x, ⟨g x, hCg x.2⟩, have hg₀ : continuous g₀ := g.continuous _ (λ x, rfl), haveI : fact (Cg * Cf * c ≤ Cg * (Cf * c)) := by { apply le_of_eq, rw mul_assoc }, rw (embedding_cast_le (Cg * Cf * c) (Cg * (Cf * c))).continuous_iff, exact hg₀.comp hf₀ end end profinitely_filtered_pseudo_normed_group_hom namespace punit -- move this instance (X : Type*) [subsingleton X] (p : X → Prop) : subsingleton (subtype p) := ⟨λ x y, subtype.ext $ subsingleton.elim _ _⟩ instance : profinitely_filtered_pseudo_normed_group punit := { filtration := λ _, set.univ, filtration_mono := λ _ _ _, set.subset_univ _, zero_mem_filtration := λ _, set.mem_univ _, neg_mem_filtration := λ _ _ _, set.mem_univ _, add_mem_filtration := λ _ _ _ _ _ _, set.mem_univ _, continuous_add' := λ _ _, continuous_of_discrete_topology, continuous_neg' := λ _, continuous_of_discrete_topology, continuous_cast_le := λ _ _ _, continuous_of_discrete_topology } end punit section continuity variables {M M₁ M₂ M₃ : Type*} namespace pseudo_normed_group /-- Helper function for pseudo normed groups. `pow_incl` is the natural inclusion function `(filtration M c)^n → M^n`. Note that `(filtration M c)^n` is not the same type as `filtration (M^n) c`, although they are naturally equivalent. -/ def pow_incl {n : ℕ} {c : ℝ≥0} [pseudo_normed_group M] : (filtration M c : Type*)^n → M^n := λ x j, x j lemma pow_incl_injective {n : ℕ} {c : ℝ≥0} [pseudo_normed_group M] : function.injective (@pow_incl M n c _) := λ x y h, funext $ λ j, subtype.coe_injective $ congr_fun h j @[simp] lemma pow_incl_apply {n : ℕ} {c : ℝ≥0} [pseudo_normed_group M] (x : (filtration M c : Type*)^n) (j : fin n) : pow_incl x j = x j := rfl end pseudo_normed_group open pseudo_normed_group profinitely_filtered_pseudo_normed_group variables [profinitely_filtered_pseudo_normed_group M] variables [profinitely_filtered_pseudo_normed_group M₁] variables [profinitely_filtered_pseudo_normed_group M₂] variables [profinitely_filtered_pseudo_normed_group M₃] /-- A function `f : M₁ → M₂` between profinitely filtered pseudo normed groups is continuous if it is continuous when restricted to the filtration sets. Implementation detail: to avoid diamonds of topologies on `filtration M c` we avoid `topological_space M`. We therefore give a hands on definition of continuity. -/ def pfpng_ctu (f : M₁ → M₂) : Prop := ∀ ⦃c₁ c₂⦄ (f₀ : filtration M₁ c₁ → filtration M₂ c₂) (h : ∀ x, f ↑x = f₀ x), continuous f₀ section pfpng_ctu lemma pfpng_ctu_const (y : M₂) : pfpng_ctu (λ x : M₁, y) := begin intros c₁ c₂ f₀ h, suffices : f₀ = λ x, f₀ ⟨0, zero_mem_filtration _⟩, { rw this, exact continuous_const }, ext1 x, apply subtype.coe_injective, rw [← h, ← h] end lemma pfpng_ctu.neg {f : M₁ → M₂} (hf : pfpng_ctu f) : pfpng_ctu (-f) := begin intros c₁ c₂ f₀ h, let g := neg' ∘ f₀, have hg : f₀ = neg' ∘ g, { ext, simp [neg_neg] }, rw hg, refine (continuous_neg' c₂).comp (hf g _), intro x, specialize h x, simp only [g, ← h, neg_neg, pi.neg_apply, neg'_eq] end lemma pfpng_ctu.add {f g : M₁ → M₂} (hf : pfpng_ctu f) (hg : pfpng_ctu g) (H : ∀ c₁, ∃ c₂, ∀ x : filtration M₁ c₁, f x ∈ filtration M₂ c₂) : pfpng_ctu (f + g) := begin intros c₁ c₂ fg₀ hfg₀, obtain ⟨cf, hcf⟩ := H c₁, let f₀ : filtration M₁ c₁ → filtration M₂ cf := λ x, ⟨f x, hcf x⟩, have hf₀ : ∀ x, f ↑x = f₀ x := λ x, rfl, have f₀_ctu : continuous f₀ := hf f₀ hf₀, let cg := cf + c₂, haveI : fact (c₂ ≤ cf + cg) := calc c₂ ≤ cf + c₂ : self_le_add_left _ _ ... ≤ cf + (cf + c₂) : self_le_add_left _ _, have hcg : ∀ x : filtration M₁ c₁, g x ∈ filtration M₂ cg, { intros x, have : g x = -(f x) + (f + g) x, { simp only [pi.add_apply, neg_add_cancel_left] }, rw this, refine add_mem_filtration (neg_mem_filtration $ hcf x) _, rw hfg₀, exact (fg₀ x).2 }, let g₀ : filtration M₁ c₁ → filtration M₂ cg := λ x, ⟨g x, hcg x⟩, have hg₀ : ∀ x, g ↑x = g₀ x := λ x, rfl, have g₀_ctu : continuous g₀ := hg g₀ hg₀, have aux := (f₀_ctu.prod_mk g₀_ctu), rw (embedding_cast_le c₂ (cf + cg)).continuous_iff, convert (continuous_add' cf cg).comp aux using 1, ext, dsimp, rw [← hfg₀, pi.add_apply] end variables (M) lemma pfpng_ctu_id : pfpng_ctu (@id M) := begin intros c₁ c₂ f₀ h, haveI : fact (c₁ ≤ max c₁ c₂) := le_max_left _ _, haveI : fact (c₂ ≤ max c₁ c₂) := le_max_right _ _, have : @cast_le M _ c₂ (max c₁ c₂) _ ∘ f₀ = cast_le, { ext, dsimp, rw ← h, refl }, rw [(embedding_cast_le c₂ (max c₁ c₂)).continuous_iff, this], exact (embedding_cast_le _ _).continuous end lemma pfpng_ctu_smul_nat : ∀ (n : ℕ), pfpng_ctu (λ x : M, n • x) | 0 := pfpng_ctu_const 0 | (n+1) := (pfpng_ctu_id M).add (pfpng_ctu_smul_nat n) (λ c, ⟨c, λ x, x.2⟩) lemma pfpng_ctu_smul_int : ∀ (n : ℤ), pfpng_ctu (λ x : M, n • x) | (n:ℕ) := pfpng_ctu_smul_nat M n | -[1+n] := (pfpng_ctu_smul_nat M (n+1)).neg end pfpng_ctu /-- A function `f : M₁^m → M₂^n` between powers of profinitely filtered pseudo normed groups is continuous if it is continuous when restricted to the filtration sets. Implementation details: * To avoid diamonds of topologies on `filtration M c` we avoid `topological_space M`. * This definitions attempts to avoid moving between `(filtration M c)^n` and `filtration (M^n) c`. It is therefore particularly ad hoc. -/ def pfpng_ctu' {m n : ℕ} (f : M₁^m → M₂^n) : Prop := ∀ ⦃c₁ c₂⦄ (f₀ : (filtration M₁ c₁ : Type*)^m → (filtration M₂ c₂ : Type*)^n) (h : ∀ x, f (pow_incl x) = pow_incl (f₀ x)), continuous f₀ section pfpng_ctu' variables {m n : ℕ} lemma pfpng_ctu'_const (y : M₂^n) : pfpng_ctu' (λ x : M₁^m, y) := begin intros c₁ c₂ f₀ h, suffices : f₀ = λ x, f₀ (λ i, ⟨0, zero_mem_filtration _⟩), { rw this, exact continuous_const }, ext1 x, apply pow_incl_injective, rw [← h, ← h] end lemma pfpng_ctu'_of_pfpng_ctu (i : fin m) (f : M₁ → M₂^n) (h : ∀ j, pfpng_ctu (λ x, f x j)) : pfpng_ctu' (λ x, f (x i)) := begin intros c₁ c₂ f₀ h₀, apply continuous_pi, intro j, have aux : ∀ (x : filtration M₁ c₁), f x j ∈ filtration M₂ c₂, { intro x, specialize h₀ (λ i, x), dsimp at h₀, simp only [h₀, pow_incl_apply], exact (f₀ (λ i, x) j).2 }, let g : filtration M₁ c₁ → filtration M₂ c₂ := λ x, ⟨f x j, aux x⟩, have hg : ∀ x, f₀ x j = g (x i), { intro x, apply subtype.coe_injective, exact (congr_fun (h₀ x) j).symm }, simp only [hg], exact (h j g (λ x, rfl)).comp (continuous_apply _), end -- -- we don't need this -- lemma pfpng_ctu'_iff_pfpng_ctu (i : fin m) (f : M₁ → M₂^n) : -- pfpng_ctu' (λ x, f (x i)) ↔ (∀ j, pfpng_ctu (λ x, f x j)) := -- admit lemma pfpng_ctu'.add {f g : M₁^m → M₂^n} (hf : pfpng_ctu' f) (hg : pfpng_ctu' g) (H : ∀ c₁, ∃ c₂, ∀ (x : (filtration M₁ c₁ : Type*)^m) j, f (pow_incl x) j ∈ filtration M₂ c₂) : pfpng_ctu' (f + g) := begin intros c₁ c₂ fg₀ hfg₀, obtain ⟨cf, hcf⟩ := H c₁, let f₀ : (filtration M₁ c₁ : Type*)^m → (filtration M₂ cf : Type*)^n := λ x j, ⟨f (pow_incl x) j, hcf x j⟩, have hf₀ : ∀ x, f (pow_incl x) = pow_incl (f₀ x) := λ x, rfl, have f₀_ctu : continuous f₀ := hf f₀ hf₀, let cg := cf + c₂, haveI : fact (c₂ ≤ cf + cg) := calc c₂ ≤ cf + c₂ : self_le_add_left _ _ ... ≤ cf + (cf + c₂) : self_le_add_left _ _, have hcg : ∀ (x : (filtration M₁ c₁ : Type*)^m) j, g (pow_incl x) j ∈ filtration M₂ cg, { intros x j, have : g (pow_incl x) j = -(f (pow_incl x) j) + (f + g) (pow_incl x) j, { simp only [pi.add_apply, neg_add_cancel_left] }, rw this, refine add_mem_filtration (neg_mem_filtration $ hcf x j) _, rw hfg₀, exact (fg₀ x j).2 }, let g₀ : (filtration M₁ c₁ : Type*)^m → (filtration M₂ cg : Type*)^n := λ x j, ⟨g (pow_incl x) j, hcg x j⟩, have hg₀ : ∀ x, g (pow_incl x) = pow_incl (g₀ x) := λ x, rfl, have g₀_ctu : continuous g₀ := hg g₀ hg₀, have aux := f₀_ctu.prod_mk g₀_ctu, apply continuous_pi, intro j, have aux' := ((continuous_apply j).prod_map (continuous_apply j)).comp aux, dsimp [function.comp] at aux', rw (embedding_cast_le c₂ (cf + cg)).continuous_iff, convert (continuous_add' cf cg).comp aux' using 1, ext x, replace hfg₀ := congr_fun (hfg₀ x) j, dsimp at hfg₀ ⊢, rw [← hfg₀], refl end lemma pfpng_ctu'_sum {ι : Type*} (s : finset ι) (f : ι → M₁^m → M₂^n) (h : ∀ i ∈ s, pfpng_ctu' (f i)) (H : ∀ i ∈ s, ∀ c₁, ∃ c₂, ∀ (x : (filtration M₁ c₁ : Type*)^m) j, f i (pow_incl x) j ∈ filtration M₂ c₂) : pfpng_ctu' (∑ i in s, f i) := begin classical, revert h H, apply finset.induction_on s; clear s, { simp only [finset.sum_empty], intros, exact pfpng_ctu'_const 0 }, intros i s his IH h H, simp [his, IH, h, finset.sum_insert], apply pfpng_ctu'.add, { exact h _ (finset.mem_insert_self _ _) }, { apply IH, { intros i' hi', exact h _ (finset.mem_insert_of_mem hi') }, { intros i' hi', exact H _ (finset.mem_insert_of_mem hi') } }, { exact H _ (finset.mem_insert_self _ _) } end end pfpng_ctu' end continuity
f12a6a2fe027c2f30573ffb9fc93d5c76f3f9c97
a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940
/stage0/src/Lean/Util/Recognizers.lean
a3fd604b0b559fa74de2808bd92017992106d6b2
[ "Apache-2.0" ]
permissive
WojciechKarpiel/lean4
7f89706b8e3c1f942b83a2c91a3a00b05da0e65b
f6e1314fa08293dea66a329e05b6c196a0189163
refs/heads/master
1,686,633,402,214
1,625,821,189,000
1,625,821,258,000
384,640,886
0
0
Apache-2.0
1,625,903,617,000
1,625,903,026,000
null
UTF-8
Lean
false
false
3,820
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.Environment namespace Lean namespace Expr @[inline] def const? (e : Expr) : Option (Name × List Level) := match e with | Expr.const n us _ => some (n, us) | _ => none @[inline] def app1? (e : Expr) (fName : Name) : Option Expr := if e.isAppOfArity fName 1 then some e.appArg! else none @[inline] def app2? (e : Expr) (fName : Name) : Option (Expr × Expr) := if e.isAppOfArity fName 2 then some (e.appFn!.appArg!, e.appArg!) else none @[inline] def app3? (e : Expr) (fName : Name) : Option (Expr × Expr × Expr) := if e.isAppOfArity fName 3 then some (e.appFn!.appFn!.appArg!, e.appFn!.appArg!, e.appArg!) else none @[inline] def app4? (e : Expr) (fName : Name) : Option (Expr × Expr × Expr × Expr) := if e.isAppOfArity fName 4 then some (e.appFn!.appFn!.appFn!.appArg!, e.appFn!.appFn!.appArg!, e.appFn!.appArg!, e.appArg!) else none @[inline] def eq? (p : Expr) : Option (Expr × Expr × Expr) := p.app3? ``Eq @[inline] def ne? (p : Expr) : Option (Expr × Expr × Expr) := p.app3? ``Ne @[inline] def iff? (p : Expr) : Option (Expr × Expr) := p.app2? ``Iff @[inline] def not? (p : Expr) : Option Expr := p.app1? ``Not @[inline] def and? (p : Expr) : Option (Expr × Expr) := p.app2? ``And @[inline] def heq? (p : Expr) : Option (Expr × Expr × Expr × Expr) := p.app4? ``HEq def natAdd? (e : Expr) : Option (Expr × Expr) := e.app2? ``Nat.add @[inline] def arrow? : Expr → Option (Expr × Expr) | Expr.forallE _ α β _ => if β.hasLooseBVars then none else some (α, β) | _ => none def isEq (e : Expr) := e.isAppOfArity ``Eq 3 def isHEq (e : Expr) := e.isAppOfArity ``HEq 4 partial def listLit? (e : Expr) : Option (Expr × List Expr) := let rec loop (e : Expr) (acc : List Expr) := if e.isAppOfArity ``List.nil 1 then some (e.appArg!, acc.reverse) else if e.isAppOfArity ``List.cons 3 then loop e.appArg! (e.appFn!.appArg! :: acc) else none loop e [] def arrayLit? (e : Expr) : Option (Expr × List Expr) := match e.app2? ``List.toArray with | some (_, e) => e.listLit? | none => none /-- Recognize `α × β` -/ def prod? (e : Expr) : Option (Expr × Expr) := e.app2? ``Prod private def getConstructorVal? (env : Environment) (ctorName : Name) : Option ConstructorVal := do match env.find? ctorName with | some (ConstantInfo.ctorInfo v) => v | _ => none def isConstructorApp? (env : Environment) (e : Expr) : Option ConstructorVal := match e with | Expr.lit (Literal.natVal n) _ => if n == 0 then getConstructorVal? env `Nat.zero else getConstructorVal? env `Nat.succ | _ => match e.getAppFn with | Expr.const n _ _ => match getConstructorVal? env n with | some v => if v.numParams + v.numFields == e.getAppNumArgs then some v else none | none => none | _ => none def isConstructorApp (env : Environment) (e : Expr) : Bool := e.isConstructorApp? env |>.isSome def constructorApp? (env : Environment) (e : Expr) : Option (ConstructorVal × Array Expr) := OptionM.run do match e with | Expr.lit (Literal.natVal n) _ => if n == 0 then do let v ← getConstructorVal? env `Nat.zero pure (v, #[]) else do let v ← getConstructorVal? env `Nat.succ pure (v, #[mkNatLit (n-1)]) | _ => match e.getAppFn with | Expr.const n _ _ => do let v ← getConstructorVal? env n if v.numParams + v.numFields == e.getAppNumArgs then pure (v, e.getAppArgs) else none | _ => none end Lean.Expr
cd57b545739239eafeb38748a0aa19089c809e50
4fa118f6209450d4e8d058790e2967337811b2b5
/src/Frobenius.lean
2342c6af3d12e7b351c6f7b40f961a1de9d90982
[ "Apache-2.0" ]
permissive
leanprover-community/lean-perfectoid-spaces
16ab697a220ed3669bf76311daa8c466382207f7
95a6520ce578b30a80b4c36e36ab2d559a842690
refs/heads/master
1,639,557,829,139
1,638,797,866,000
1,638,797,866,000
135,769,296
96
10
Apache-2.0
1,638,797,866,000
1,527,892,754,000
Lean
UTF-8
Lean
false
false
470
lean
import algebra.char_p import for_mathlib.primes /-! The main purpose of this file is to introduce notation that is not available in mathlib, and that we don't want to set up in the main file. -/ /--The Frobenius endomorphism of a semiring-/ noncomputable def Frobenius (α : Type*) [semiring α] : α → α := λ x, x^(ring_char α) notation `Frob` R `∕` x := Frobenius (ideal.quotient (ideal.span ({x} : set R))) notation x `∣` y `in` R := (x : R) ∣ (y : R)
d9dbe64088023a85c68989bcd6a7485602c224f8
9dc8cecdf3c4634764a18254e94d43da07142918
/src/measure_theory/measure/null_measurable.lean
b55766e7ac003873a1dd0f21a6af92e7fa965993
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
18,585
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Yury Kudryashov -/ import measure_theory.measure.ae_disjoint /-! # Null measurable sets and complete measures ## Main definitions ### Null measurable sets and functions A set `s : set α` is called *null measurable* (`measure_theory.null_measurable_set`) if it satisfies any of the following equivalent conditions: * there exists a measurable set `t` such that `s =ᵐ[μ] t` (this is used as a definition); * `measure_theory.to_measurable μ s =ᵐ[μ] s`; * there exists a measurable subset `t ⊆ s` such that `t =ᵐ[μ] s` (in this case the latter equality means that `μ (s \ t) = 0`); * `s` can be represented as a union of a measurable set and a set of measure zero; * `s` can be represented as a difference of a measurable set and a set of measure zero. Null measurable sets form a σ-algebra that is registered as a `measurable_space` instance on `measure_theory.null_measurable_space α μ`. We also say that `f : α → β` is `measure_theory.null_measurable` if the preimage of a measurable set is a null measurable set. In other words, `f : α → β` is null measurable if it is measurable as a function `measure_theory.null_measurable_space α μ → β`. ### Complete measures We say that a measure `μ` is complete w.r.t. the `measurable_space α` σ-algebra (or the σ-algebra is complete w.r.t measure `μ`) if every set of measure zero is measurable. In this case all null measurable sets and functions are measurable. For each measure `μ`, we define `measure_theory.measure.completion μ` to be the same measure interpreted as a measure on `measure_theory.null_measurable_space α μ` and prove that this is a complete measure. ## Implementation notes We define `measure_theory.null_measurable_set` as `@measurable_set (null_measurable_space α μ) _` so that theorems about `measurable_set`s like `measurable_set.union` can be applied to `null_measurable_set`s. However, these lemmas output terms of the same form `@measurable_set (null_measurable_space α μ) _ _`. While this is definitionally equal to the expected output `null_measurable_set s μ`, it looks different and may be misleading. So we copy all standard lemmas about measurable sets to the `measure_theory.null_measurable_set` namespace and fix the output type. ## Tags measurable, measure, null measurable, completion -/ open filter set encodable variables {ι α β γ : Type*} namespace measure_theory /-- A type tag for `α` with `measurable_set` given by `null_measurable_set`. -/ @[nolint unused_arguments] def null_measurable_space (α : Type*) [measurable_space α] (μ : measure α . volume_tac) : Type* := α section variables {m0 : measurable_space α} {μ : measure α} {s t : set α} instance [h : inhabited α] : inhabited (null_measurable_space α μ) := h instance [h : subsingleton α] : subsingleton (null_measurable_space α μ) := h instance : measurable_space (null_measurable_space α μ) := { measurable_set' := λ s, ∃ t, measurable_set t ∧ s =ᵐ[μ] t, measurable_set_empty := ⟨∅, measurable_set.empty, ae_eq_refl _⟩, measurable_set_compl := λ s ⟨t, htm, hts⟩, ⟨tᶜ, htm.compl, hts.compl⟩, measurable_set_Union := λ s hs, by { choose t htm hts using hs, exact ⟨⋃ i, t i, measurable_set.Union htm, eventually_eq.countable_Union hts⟩ } } /-- A set is called `null_measurable_set` if it can be approximated by a measurable set up to a set of null measure. -/ def null_measurable_set [measurable_space α] (s : set α) (μ : measure α . volume_tac) : Prop := @measurable_set (null_measurable_space α μ) _ s @[simp] lemma _root_.measurable_set.null_measurable_set (h : measurable_set s) : null_measurable_set s μ := ⟨s, h, ae_eq_refl _⟩ @[simp] lemma null_measurable_set_empty : null_measurable_set ∅ μ := measurable_set.empty @[simp] lemma null_measurable_set_univ : null_measurable_set univ μ := measurable_set.univ namespace null_measurable_set lemma of_null (h : μ s = 0) : null_measurable_set s μ := ⟨∅, measurable_set.empty, ae_eq_empty.2 h⟩ lemma compl (h : null_measurable_set s μ) : null_measurable_set sᶜ μ := h.compl lemma of_compl (h : null_measurable_set sᶜ μ) : null_measurable_set s μ := h.of_compl @[simp] lemma compl_iff : null_measurable_set sᶜ μ ↔ null_measurable_set s μ := measurable_set.compl_iff @[nontriviality] lemma of_subsingleton [subsingleton α] : null_measurable_set s μ := subsingleton.measurable_set protected lemma congr (hs : null_measurable_set s μ) (h : s =ᵐ[μ] t) : null_measurable_set t μ := let ⟨s', hm, hs'⟩ := hs in ⟨s', hm, h.symm.trans hs'⟩ protected lemma Union {ι : Sort*} [countable ι] {s : ι → set α} (h : ∀ i, null_measurable_set (s i) μ) : null_measurable_set (⋃ i, s i) μ := measurable_set.Union h protected lemma bUnion_decode₂ [encodable ι] ⦃f : ι → set α⦄ (h : ∀ i, null_measurable_set (f i) μ) (n : ℕ) : null_measurable_set (⋃ b ∈ encodable.decode₂ ι n, f b) μ := measurable_set.bUnion_decode₂ h n protected lemma bUnion {f : ι → set α} {s : set ι} (hs : s.countable) (h : ∀ b ∈ s, null_measurable_set (f b) μ) : null_measurable_set (⋃ b ∈ s, f b) μ := measurable_set.bUnion hs h protected lemma sUnion {s : set (set α)} (hs : s.countable) (h : ∀ t ∈ s, null_measurable_set t μ) : null_measurable_set (⋃₀ s) μ := by { rw sUnion_eq_bUnion, exact measurable_set.bUnion hs h } protected lemma Inter {ι : Sort*} [countable ι] {f : ι → set α} (h : ∀ i, null_measurable_set (f i) μ) : null_measurable_set (⋂ i, f i) μ := measurable_set.Inter h protected lemma bInter {f : β → set α} {s : set β} (hs : s.countable) (h : ∀ b ∈ s, null_measurable_set (f b) μ) : null_measurable_set (⋂ b ∈ s, f b) μ := measurable_set.bInter hs h protected lemma sInter {s : set (set α)} (hs : s.countable) (h : ∀ t ∈ s, null_measurable_set t μ) : null_measurable_set (⋂₀ s) μ := measurable_set.sInter hs h @[simp] protected lemma union (hs : null_measurable_set s μ) (ht : null_measurable_set t μ) : null_measurable_set (s ∪ t) μ := hs.union ht protected lemma union_null (hs : null_measurable_set s μ) (ht : μ t = 0) : null_measurable_set (s ∪ t) μ := hs.union (of_null ht) @[simp] protected lemma inter (hs : null_measurable_set s μ) (ht : null_measurable_set t μ) : null_measurable_set (s ∩ t) μ := hs.inter ht @[simp] protected lemma diff (hs : null_measurable_set s μ) (ht : null_measurable_set t μ) : null_measurable_set (s \ t) μ := hs.diff ht @[simp] protected lemma disjointed {f : ℕ → set α} (h : ∀ i, null_measurable_set (f i) μ) (n) : null_measurable_set (disjointed f n) μ := measurable_set.disjointed h n @[simp] protected lemma const (p : Prop) : null_measurable_set {a : α | p} μ := measurable_set.const p instance [measurable_singleton_class α] : measurable_singleton_class (null_measurable_space α μ) := ⟨λ x, (@measurable_set_singleton α _ _ x).null_measurable_set⟩ protected lemma insert [measurable_singleton_class (null_measurable_space α μ)] (hs : null_measurable_set s μ) (a : α) : null_measurable_set (insert a s) μ := hs.insert a lemma exists_measurable_superset_ae_eq (h : null_measurable_set s μ) : ∃ t ⊇ s, measurable_set t ∧ t =ᵐ[μ] s := begin rcases h with ⟨t, htm, hst⟩, refine ⟨t ∪ to_measurable μ (s \ t), _, htm.union (measurable_set_to_measurable _ _), _⟩, { exact diff_subset_iff.1 (subset_to_measurable _ _) }, { have : to_measurable μ (s \ t) =ᵐ[μ] (∅ : set α), by simp [ae_le_set.1 hst.le], simpa only [union_empty] using hst.symm.union this } end lemma to_measurable_ae_eq (h : null_measurable_set s μ) : to_measurable μ s =ᵐ[μ] s := begin rw [to_measurable, dif_pos], exact h.exists_measurable_superset_ae_eq.some_spec.snd.2 end lemma compl_to_measurable_compl_ae_eq (h : null_measurable_set s μ) : (to_measurable μ sᶜ)ᶜ =ᵐ[μ] s := by simpa only [compl_compl] using h.compl.to_measurable_ae_eq.compl lemma exists_measurable_subset_ae_eq (h : null_measurable_set s μ) : ∃ t ⊆ s, measurable_set t ∧ t =ᵐ[μ] s := ⟨(to_measurable μ sᶜ)ᶜ, compl_subset_comm.2 $ subset_to_measurable _ _, (measurable_set_to_measurable _ _).compl, h.compl_to_measurable_compl_ae_eq⟩ end null_measurable_set /-- If `sᵢ` is a countable family of (null) measurable pairwise `μ`-a.e. disjoint sets, then there exists a subordinate family `tᵢ ⊆ sᵢ` of measurable pairwise disjoint sets such that `tᵢ =ᵐ[μ] sᵢ`. -/ lemma exists_subordinate_pairwise_disjoint [countable ι] {s : ι → set α} (h : ∀ i, null_measurable_set (s i) μ) (hd : pairwise (ae_disjoint μ on s)) : ∃ t : ι → set α, (∀ i, t i ⊆ s i) ∧ (∀ i, s i =ᵐ[μ] t i) ∧ (∀ i, measurable_set (t i)) ∧ pairwise (disjoint on t) := begin choose t ht_sub htm ht_eq using λ i, (h i).exists_measurable_subset_ae_eq, rcases exists_null_pairwise_disjoint_diff hd with ⟨u, hum, hu₀, hud⟩, exact ⟨λ i, t i \ u i, λ i, (diff_subset _ _).trans (ht_sub _), λ i, (ht_eq _).symm.trans (diff_null_ae_eq_self (hu₀ i)).symm, λ i, (htm i).diff (hum i), hud.mono $ λ i j h, h.mono (diff_subset_diff_left (ht_sub i)) (diff_subset_diff_left (ht_sub j))⟩ end lemma measure_Union {m0 : measurable_space α} {μ : measure α} [countable ι] {f : ι → set α} (hn : pairwise (disjoint on f)) (h : ∀ i, measurable_set (f i)) : μ (⋃ i, f i) = ∑' i, μ (f i) := begin rw [measure_eq_extend (measurable_set.Union h), extend_Union measurable_set.empty _ measurable_set.Union _ hn h], { simp [measure_eq_extend, h] }, { exact μ.empty }, { exact μ.m_Union } end lemma measure_Union₀ [countable ι] {f : ι → set α} (hd : pairwise (ae_disjoint μ on f)) (h : ∀ i, null_measurable_set (f i) μ) : μ (⋃ i, f i) = ∑' i, μ (f i) := begin rcases exists_subordinate_pairwise_disjoint h hd with ⟨t, ht_sub, ht_eq, htm, htd⟩, calc μ (⋃ i, f i) = μ (⋃ i, t i) : measure_congr (eventually_eq.countable_Union ht_eq) ... = ∑' i, μ (t i) : measure_Union htd htm ... = ∑' i, μ (f i) : tsum_congr (λ i, measure_congr (ht_eq _).symm) end lemma measure_union₀_aux (hs : null_measurable_set s μ) (ht : null_measurable_set t μ) (hd : ae_disjoint μ s t) : μ (s ∪ t) = μ s + μ t := begin rw [union_eq_Union, measure_Union₀, tsum_fintype, fintype.sum_bool, cond, cond], exacts [(pairwise_on_bool ae_disjoint.symmetric).2 hd, λ b, bool.cases_on b ht hs] end /-- A null measurable set `t` is Carathéodory measurable: for any `s`, we have `μ (s ∩ t) + μ (s \ t) = μ s`. -/ lemma measure_inter_add_diff₀ (s : set α) (ht : null_measurable_set t μ) : μ (s ∩ t) + μ (s \ t) = μ s := begin refine le_antisymm _ _, { rcases exists_measurable_superset μ s with ⟨s', hsub, hs'm, hs'⟩, replace hs'm : null_measurable_set s' μ := hs'm.null_measurable_set, calc μ (s ∩ t) + μ (s \ t) ≤ μ (s' ∩ t) + μ (s' \ t) : add_le_add (measure_mono $ inter_subset_inter_left _ hsub) (measure_mono $ diff_subset_diff_left hsub) ... = μ (s' ∩ t ∪ s' \ t) : (measure_union₀_aux (hs'm.inter ht) (hs'm.diff ht) $ (@disjoint_inf_sdiff _ s' t _).ae_disjoint).symm ... = μ s' : congr_arg μ (inter_union_diff _ _) ... = μ s : hs' }, { calc μ s = μ (s ∩ t ∪ s \ t) : by rw inter_union_diff ... ≤ μ (s ∩ t) + μ (s \ t) : measure_union_le _ _ } end lemma measure_union_add_inter₀ (s : set α) (ht : null_measurable_set t μ) : μ (s ∪ t) + μ (s ∩ t) = μ s + μ t := by rw [← measure_inter_add_diff₀ (s ∪ t) ht, union_inter_cancel_right, union_diff_right, ← measure_inter_add_diff₀ s ht, add_comm, ← add_assoc, add_right_comm] lemma measure_union_add_inter₀' (hs : null_measurable_set s μ) (t : set α) : μ (s ∪ t) + μ (s ∩ t) = μ s + μ t := by rw [union_comm, inter_comm, measure_union_add_inter₀ t hs, add_comm] lemma measure_union₀ (ht : null_measurable_set t μ) (hd : ae_disjoint μ s t) : μ (s ∪ t) = μ s + μ t := by rw [← measure_union_add_inter₀ s ht, hd.eq, add_zero] lemma measure_union₀' (hs : null_measurable_set s μ) (hd : ae_disjoint μ s t) : μ (s ∪ t) = μ s + μ t := by rw [union_comm, measure_union₀ hs hd.symm, add_comm] section measurable_singleton_class variable [measurable_singleton_class (null_measurable_space α μ)] lemma null_measurable_set_singleton (x : α) : null_measurable_set {x} μ := measurable_set_singleton x @[simp] lemma null_measurable_set_insert {a : α} {s : set α} : null_measurable_set (insert a s) μ ↔ null_measurable_set s μ := measurable_set_insert lemma null_measurable_set_eq {a : α} : null_measurable_set {x | x = a} μ := null_measurable_set_singleton a protected lemma _root_.set.finite.null_measurable_set (hs : s.finite) : null_measurable_set s μ := finite.measurable_set hs protected lemma _root_.finset.null_measurable_set (s : finset α) : null_measurable_set ↑s μ := finset.measurable_set s end measurable_singleton_class lemma _root_.set.finite.null_measurable_set_bUnion {f : ι → set α} {s : set ι} (hs : s.finite) (h : ∀ b ∈ s, null_measurable_set (f b) μ) : null_measurable_set (⋃ b ∈ s, f b) μ := finite.measurable_set_bUnion hs h lemma _root_.finset.null_measurable_set_bUnion {f : ι → set α} (s : finset ι) (h : ∀ b ∈ s, null_measurable_set (f b) μ) : null_measurable_set (⋃ b ∈ s, f b) μ := finset.measurable_set_bUnion s h lemma _root_.set.finite.null_measurable_set_sUnion {s : set (set α)} (hs : s.finite) (h : ∀ t ∈ s, null_measurable_set t μ) : null_measurable_set (⋃₀ s) μ := finite.measurable_set_sUnion hs h lemma _root_.set.finite.null_measurable_set_bInter {f : ι → set α} {s : set ι} (hs : s.finite) (h : ∀ b ∈ s, null_measurable_set (f b) μ) : null_measurable_set (⋂ b ∈ s, f b) μ := finite.measurable_set_bInter hs h lemma _root_.finset.null_measurable_set_bInter {f : ι → set α} (s : finset ι) (h : ∀ b ∈ s, null_measurable_set (f b) μ) : null_measurable_set (⋂ b ∈ s, f b) μ := s.finite_to_set.null_measurable_set_bInter h lemma _root_.set.finite.null_measurable_set_sInter {s : set (set α)} (hs : s.finite) (h : ∀ t ∈ s, null_measurable_set t μ) : null_measurable_set (⋂₀ s) μ := null_measurable_set.sInter hs.countable h lemma null_measurable_set_to_measurable : null_measurable_set (to_measurable μ s) μ := (measurable_set_to_measurable _ _).null_measurable_set end section null_measurable variables [measurable_space α] [measurable_space β] [measurable_space γ] {f : α → β} {μ : measure α} /-- A function `f : α → β` is null measurable if the preimage of a measurable set is a null measurable set. -/ def null_measurable (f : α → β) (μ : measure α . volume_tac) : Prop := ∀ ⦃s : set β⦄, measurable_set s → null_measurable_set (f ⁻¹' s) μ protected lemma _root_.measurable.null_measurable (h : measurable f) : null_measurable f μ := λ s hs, (h hs).null_measurable_set protected lemma null_measurable.measurable' (h : null_measurable f μ) : @measurable (null_measurable_space α μ) β _ _ f := h lemma measurable.comp_null_measurable {g : β → γ} (hg : measurable g) (hf : null_measurable f μ) : null_measurable (g ∘ f) μ := hg.comp hf lemma null_measurable.congr {g : α → β} (hf : null_measurable f μ) (hg : f =ᵐ[μ] g) : null_measurable g μ := λ s hs, (hf hs).congr $ eventually_eq_set.2 $ hg.mono $ λ x hx, by rw [mem_preimage, mem_preimage, hx] end null_measurable section is_complete /-- A measure is complete if every null set is also measurable. A null set is a subset of a measurable set with measure `0`. Since every measure is defined as a special case of an outer measure, we can more simply state that a set `s` is null if `μ s = 0`. -/ class measure.is_complete {_ : measurable_space α} (μ : measure α) : Prop := (out' : ∀ s, μ s = 0 → measurable_set s) variables {m0 : measurable_space α} {μ : measure α} {s t : set α} theorem measure.is_complete_iff : μ.is_complete ↔ ∀ s, μ s = 0 → measurable_set s := ⟨λ h, h.1, λ h, ⟨h⟩⟩ theorem measure.is_complete.out (h : μ.is_complete) : ∀ s, μ s = 0 → measurable_set s := h.1 theorem measurable_set_of_null [μ.is_complete] (hs : μ s = 0) : measurable_set s := measure_theory.measure.is_complete.out' s hs theorem null_measurable_set.measurable_of_complete (hs : null_measurable_set s μ) [μ.is_complete] : measurable_set s := diff_diff_cancel_left (subset_to_measurable μ s) ▸ (measurable_set_to_measurable _ _).diff (measurable_set_of_null (ae_le_set.1 hs.to_measurable_ae_eq.le)) theorem null_measurable.measurable_of_complete [μ.is_complete] {m1 : measurable_space β} {f : α → β} (hf : null_measurable f μ) : measurable f := λ s hs, (hf hs).measurable_of_complete lemma _root_.measurable.congr_ae {α β} [measurable_space α] [measurable_space β] {μ : measure α} [hμ : μ.is_complete] {f g : α → β} (hf : measurable f) (hfg : f =ᵐ[μ] g) : measurable g := (hf.null_measurable.congr hfg).measurable_of_complete namespace measure /-- Given a measure we can complete it to a (complete) measure on all null measurable sets. -/ def completion {_ : measurable_space α} (μ : measure α) : @measure_theory.measure (null_measurable_space α μ) _ := { to_outer_measure := μ.to_outer_measure, m_Union := λ s hs hd, measure_Union₀ (hd.mono $ λ i j h, h.ae_disjoint) hs, trimmed := begin refine le_antisymm (λ s, _) (outer_measure.le_trim _), rw outer_measure.trim_eq_infi, simp only [to_outer_measure_apply], refine (infi₂_mono _).trans_eq (measure_eq_infi _).symm, exact λ t ht, infi_mono' (λ h, ⟨h.null_measurable_set, le_rfl⟩) end } instance completion.is_complete {m : measurable_space α} (μ : measure α) : μ.completion.is_complete := ⟨λ z hz, null_measurable_set.of_null hz⟩ @[simp] lemma coe_completion {_ : measurable_space α} (μ : measure α) : ⇑μ.completion = μ := rfl lemma completion_apply {_ : measurable_space α} (μ : measure α) (s : set α) : μ.completion s = μ s := rfl end measure end is_complete end measure_theory
7a5d6e0092757c2d1d556e1deec44cce14d1a931
367134ba5a65885e863bdc4507601606690974c1
/src/category_theory/limits/cones.lean
dbdea4a4a988cbc943c3661908bbe0845dd8039e
[ "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
29,430
lean
/- Copyright (c) 2017 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Stephen Morgan, Scott Morrison, Floris van Doorn -/ import category_theory.const import category_theory.discrete_category import category_theory.yoneda import category_theory.reflects_isomorphisms universes v u u' -- declare the `v`'s first; see `category_theory.category` for an explanation open category_theory variables {J : Type v} [small_category J] variables {C : Type u} [category.{v} C] open category_theory open category_theory.category open category_theory.functor open opposite namespace category_theory namespace functor variables {J C} (F : J ⥤ C) /-- `F.cones` is the functor assigning to an object `X` the type of natural transformations from the constant functor with value `X` to `F`. An object representing this functor is a limit of `F`. -/ @[simps] def cones : Cᵒᵖ ⥤ Type v := (const J).op ⋙ (yoneda.obj F) /-- `F.cocones` is the functor assigning to an object `X` the type of natural transformations from `F` to the constant functor with value `X`. An object corepresenting this functor is a colimit of `F`. -/ @[simps] def cocones : C ⥤ Type v := const J ⋙ coyoneda.obj (op F) end functor section variables (J C) /-- Functorially associated to each functor `J ⥤ C`, we have the `C`-presheaf consisting of cones with a given cone point. -/ @[simps] def cones : (J ⥤ C) ⥤ (Cᵒᵖ ⥤ Type v) := { obj := functor.cones, map := λ F G f, whisker_left (const J).op (yoneda.map f) } /-- Contravariantly associated to each functor `J ⥤ C`, we have the `C`-copresheaf consisting of cocones with a given cocone point. -/ @[simps] def cocones : (J ⥤ C)ᵒᵖ ⥤ (C ⥤ Type v) := { obj := λ F, functor.cocones (unop F), map := λ F G f, whisker_left (const J) (coyoneda.map f) } end namespace limits /-- A `c : cone F` is: * an object `c.X` and * a natural transformation `c.π : c.X ⟶ F` from the constant `c.X` functor to `F`. `cone F` is equivalent, via `cone.equiv` below, to `Σ X, F.cones.obj X`. -/ structure cone (F : J ⥤ C) := (X : C) (π : (const J).obj X ⟶ F) instance inhabited_cone (F : discrete punit ⥤ C) : inhabited (cone F) := ⟨{ X := F.obj punit.star, π := { app := λ X, match X with | punit.star := 𝟙 _ end } }⟩ @[simp, reassoc] lemma cone.w {F : J ⥤ C} (c : cone F) {j j' : J} (f : j ⟶ j') : c.π.app j ≫ F.map f = c.π.app j' := by { rw ← (c.π.naturality f), apply id_comp } /-- A `c : cocone F` is * an object `c.X` and * a natural transformation `c.ι : F ⟶ c.X` from `F` to the constant `c.X` functor. `cocone F` is equivalent, via `cone.equiv` below, to `Σ X, F.cocones.obj X`. -/ structure cocone (F : J ⥤ C) := (X : C) (ι : F ⟶ (const J).obj X) instance inhabited_cocone (F : discrete punit ⥤ C) : inhabited (cocone F) := ⟨{ X := F.obj punit.star, ι := { app := λ X, match X with | punit.star := 𝟙 _ end } }⟩ @[simp, reassoc] lemma cocone.w {F : J ⥤ C} (c : cocone F) {j j' : J} (f : j ⟶ j') : F.map f ≫ c.ι.app j' = c.ι.app j := by { rw (c.ι.naturality f), apply comp_id } variables {F : J ⥤ C} namespace cone /-- The isomorphism between a cone on `F` and an element of the functor `F.cones`. -/ def equiv (F : J ⥤ C) : cone F ≅ Σ X, F.cones.obj X := { hom := λ c, ⟨op c.X, c.π⟩, inv := λ c, { X := unop c.1, π := c.2 }, hom_inv_id' := begin ext1, cases x, refl, end, inv_hom_id' := begin ext1, cases x, refl, end } /-- A map to the vertex of a cone naturally induces a cone by composition. -/ @[simp] def extensions (c : cone F) : yoneda.obj c.X ⟶ F.cones := { app := λ X f, (const J).map f ≫ c.π } /-- A map to the vertex of a cone induces a cone by composition. -/ @[simp] def extend (c : cone F) {X : C} (f : X ⟶ c.X) : cone F := { X := X, π := c.extensions.app (op X) f } @[simp] lemma extend_π (c : cone F) {X : Cᵒᵖ} (f : unop X ⟶ c.X) : (extend c f).π = c.extensions.app X f := rfl /-- Whisker a cone by precomposition of a functor. -/ @[simps] def whisker {K : Type v} [small_category K] (E : K ⥤ J) (c : cone F) : cone (E ⋙ F) := { X := c.X, π := whisker_left E c.π } end cone namespace cocone /-- The isomorphism between a cocone on `F` and an element of the functor `F.cocones`. -/ def equiv (F : J ⥤ C) : cocone F ≅ Σ X, F.cocones.obj X := { hom := λ c, ⟨c.X, c.ι⟩, inv := λ c, { X := c.1, ι := c.2 }, hom_inv_id' := begin ext1, cases x, refl, end, inv_hom_id' := begin ext1, cases x, refl, end } /-- A map from the vertex of a cocone naturally induces a cocone by composition. -/ @[simp] def extensions (c : cocone F) : coyoneda.obj (op c.X) ⟶ F.cocones := { app := λ X f, c.ι ≫ (const J).map f } /-- A map from the vertex of a cocone induces a cocone by composition. -/ @[simp] def extend (c : cocone F) {X : C} (f : c.X ⟶ X) : cocone F := { X := X, ι := c.extensions.app X f } @[simp] lemma extend_ι (c : cocone F) {X : C} (f : c.X ⟶ X) : (extend c f).ι = c.extensions.app X f := rfl /-- Whisker a cocone by precomposition of a functor. See `whiskering` for a functorial version. -/ @[simps] def whisker {K : Type v} [small_category K] (E : K ⥤ J) (c : cocone F) : cocone (E ⋙ F) := { X := c.X, ι := whisker_left E c.ι } end cocone /-- A cone morphism between two cones for the same diagram is a morphism of the cone points which commutes with the cone legs. -/ @[ext] structure cone_morphism (A B : cone F) := (hom : A.X ⟶ B.X) (w' : ∀ j : J, hom ≫ B.π.app j = A.π.app j . obviously) restate_axiom cone_morphism.w' attribute [simp, reassoc] cone_morphism.w instance inhabited_cone_morphism (A : cone F) : inhabited (cone_morphism A A) := ⟨{ hom := 𝟙 _}⟩ /-- The category of cones on a given diagram. -/ @[simps] instance cone.category : category.{v} (cone F) := { hom := λ A B, cone_morphism A B, comp := λ X Y Z f g, { hom := f.hom ≫ g.hom }, id := λ B, { hom := 𝟙 B.X } } namespace cones /-- To give an isomorphism between cones, it suffices to give an isomorphism between their vertices which commutes with the cone maps. -/ @[ext, simps] def ext {c c' : cone F} (φ : c.X ≅ c'.X) (w : ∀ j, c.π.app j = φ.hom ≫ c'.π.app j) : c ≅ c' := { hom := { hom := φ.hom }, inv := { hom := φ.inv, w' := λ j, φ.inv_comp_eq.mpr (w j) } } /-- Given a cone morphism whose object part is an isomorphism, produce an isomorphism of cones. -/ def cone_iso_of_hom_iso {K : J ⥤ C} {c d : cone K} (f : c ⟶ d) [i : is_iso f.hom] : is_iso f := { inv := { hom := i.inv, w' := λ j, (as_iso f.hom).inv_comp_eq.2 (f.w j).symm } } /-- Functorially postcompose a cone for `F` by a natural transformation `F ⟶ G` to give a cone for `G`. -/ @[simps] def postcompose {G : J ⥤ C} (α : F ⟶ G) : cone F ⥤ cone G := { obj := λ c, { X := c.X, π := c.π ≫ α }, map := λ c₁ c₂ f, { hom := f.hom, w' := by intro; erw ← category.assoc; simp [-category.assoc] } } /-- Postcomposing a cone by the composite natural transformation `α ≫ β` is the same as postcomposing by `α` and then by `β`. -/ def postcompose_comp {G H : J ⥤ C} (α : F ⟶ G) (β : G ⟶ H) : postcompose (α ≫ β) ≅ postcompose α ⋙ postcompose β := nat_iso.of_components (λ s, cones.ext (iso.refl _) (by tidy)) (by tidy) /-- Postcomposing by the identity does not change the cone up to isomorphism. -/ def postcompose_id : postcompose (𝟙 F) ≅ 𝟭 (cone F) := nat_iso.of_components (λ s, cones.ext (iso.refl _) (by tidy)) (by tidy) /-- If `F` and `G` are naturally isomorphic functors, then they have equivalent categories of cones. -/ @[simps] def postcompose_equivalence {G : J ⥤ C} (α : F ≅ G) : cone F ≌ cone G := { functor := postcompose α.hom, inverse := postcompose α.inv, unit_iso := nat_iso.of_components (λ s, cones.ext (iso.refl _) (by tidy)) (by tidy), counit_iso := nat_iso.of_components (λ s, cones.ext (iso.refl _) (by tidy)) (by tidy) } /-- Whiskering on the left by `E : K ⥤ J` gives a functor from `cone F` to `cone (E ⋙ F)`. -/ @[simps] def whiskering {K : Type v} [small_category K] (E : K ⥤ J) : cone F ⥤ cone (E ⋙ F) := { obj := λ c, c.whisker E, map := λ c c' f, { hom := f.hom, } } /-- Whiskering by an equivalence gives an equivalence between categories of cones. -/ @[simps] def whiskering_equivalence {K : Type v} [small_category K] (e : K ≌ J) : cone F ≌ cone (e.functor ⋙ F) := { functor := whiskering e.functor, inverse := whiskering e.inverse ⋙ postcompose ((functor.associator _ _ _).inv ≫ (whisker_right (e.counit_iso).hom F) ≫ (functor.left_unitor F).hom), unit_iso := nat_iso.of_components (λ s, cones.ext (iso.refl _) (by tidy)) (by tidy), counit_iso := nat_iso.of_components (λ s, cones.ext (iso.refl _) (begin intro k, have t := s.π.naturality (e.unit_inv.app k), dsimp at t, simp only [←e.counit_app_functor k, id_comp] at t, dsimp, simp [t], end)) (by tidy), } /-- The categories of cones over `F` and `G` are equivalent if `F` and `G` are naturally isomorphic (possibly after changing the indexing category by an equivalence). -/ @[simps functor_obj] def equivalence_of_reindexing {K : Type v} [small_category K] {G : K ⥤ C} (e : K ≌ J) (α : e.functor ⋙ F ≅ G) : cone F ≌ cone G := (whiskering_equivalence e).trans (postcompose_equivalence α) section variable (F) /-- Forget the cone structure and obtain just the cone point. -/ @[simps] def forget : cone F ⥤ C := { obj := λ t, t.X, map := λ s t f, f.hom } variables {D : Type u'} [category.{v} D] (G : C ⥤ D) /-- A functor `G : C ⥤ D` sends cones over `F` to cones over `F ⋙ G` functorially. -/ @[simps] def functoriality : cone F ⥤ cone (F ⋙ G) := { obj := λ A, { X := G.obj A.X, π := { app := λ j, G.map (A.π.app j), naturality' := by intros; erw ←G.map_comp; tidy } }, map := λ X Y f, { hom := G.map f.hom, w' := by intros; rw [←functor.map_comp, f.w] } } instance functoriality_full [full G] [faithful G] : full (functoriality F G) := { preimage := λ X Y t, { hom := G.preimage t.hom, w' := λ j, G.map_injective (by simpa using t.w j) } } instance functoriality_faithful [faithful G] : faithful (cones.functoriality F G) := { map_injective' := λ X Y f g e, by { ext1, injection e, apply G.map_injective h_1 } } /-- If `e : C ≌ D` is an equivalence of categories, then `functoriality F e.functor` induces an equivalence between cones over `F` and cones over `F ⋙ e.functor`. -/ @[simps] def functoriality_equivalence (e : C ≌ D) : cone F ≌ cone (F ⋙ e.functor) := let f : (F ⋙ e.functor) ⋙ e.inverse ≅ F := functor.associator _ _ _ ≪≫ iso_whisker_left _ (e.unit_iso).symm ≪≫ functor.right_unitor _ in { functor := functoriality F e.functor, inverse := (functoriality (F ⋙ e.functor) e.inverse) ⋙ (postcompose_equivalence f).functor, unit_iso := nat_iso.of_components (λ c, cones.ext (e.unit_iso.app _) (by tidy)) (by tidy), counit_iso := nat_iso.of_components (λ c, cones.ext (e.counit_iso.app _) (by tidy)) (by tidy), } /-- If `F` reflects isomorphisms, then `cones.functoriality F` reflects isomorphisms as well. -/ instance reflects_cone_isomorphism (F : C ⥤ D) [reflects_isomorphisms F] (K : J ⥤ C) : reflects_isomorphisms (cones.functoriality K F) := begin constructor, introsI, haveI : is_iso (F.map f.hom) := (cones.forget (K ⋙ F)).map_is_iso ((cones.functoriality K F).map f), haveI := reflects_isomorphisms.reflects F f.hom, apply cone_iso_of_hom_iso end end end cones /-- A cocone morphism between two cocones for the same diagram is a morphism of the cocone points which commutes with the cocone legs. -/ @[ext] structure cocone_morphism (A B : cocone F) := (hom : A.X ⟶ B.X) (w' : ∀ j : J, A.ι.app j ≫ hom = B.ι.app j . obviously) instance inhabited_cocone_morphism (A : cocone F) : inhabited (cocone_morphism A A) := ⟨{ hom := 𝟙 _ }⟩ restate_axiom cocone_morphism.w' attribute [simp, reassoc] cocone_morphism.w @[simps] instance cocone.category : category.{v} (cocone F) := { hom := λ A B, cocone_morphism A B, comp := λ _ _ _ f g, { hom := f.hom ≫ g.hom }, id := λ B, { hom := 𝟙 B.X } } namespace cocones /-- To give an isomorphism between cocones, it suffices to give an isomorphism between their vertices which commutes with the cocone maps. -/ @[ext, simps] def ext {c c' : cocone F} (φ : c.X ≅ c'.X) (w : ∀ j, c.ι.app j ≫ φ.hom = c'.ι.app j) : c ≅ c' := { hom := { hom := φ.hom }, inv := { hom := φ.inv, w' := λ j, φ.comp_inv_eq.mpr (w j).symm } } /-- Given a cocone morphism whose object part is an isomorphism, produce an isomorphism of cocones. -/ def cocone_iso_of_hom_iso {K : J ⥤ C} {c d : cocone K} (f : c ⟶ d) [i : is_iso f.hom] : is_iso f := { inv := { hom := i.inv, w' := λ j, (as_iso f.hom).comp_inv_eq.2 (f.w j).symm } } /-- Functorially precompose a cocone for `F` by a natural transformation `G ⟶ F` to give a cocone for `G`. -/ @[simps] def precompose {G : J ⥤ C} (α : G ⟶ F) : cocone F ⥤ cocone G := { obj := λ c, { X := c.X, ι := α ≫ c.ι }, map := λ c₁ c₂ f, { hom := f.hom } } /-- Precomposing a cocone by the composite natural transformation `α ≫ β` is the same as precomposing by `β` and then by `α`. -/ def precompose_comp {G H : J ⥤ C} (α : F ⟶ G) (β : G ⟶ H) : precompose (α ≫ β) ≅ precompose β ⋙ precompose α := nat_iso.of_components (λ s, cocones.ext (iso.refl _) (by tidy)) (by tidy) /-- Precomposing by the identity does not change the cocone up to isomorphism. -/ def precompose_id : precompose (𝟙 F) ≅ 𝟭 (cocone F) := nat_iso.of_components (λ s, cocones.ext (iso.refl _) (by tidy)) (by tidy) /-- If `F` and `G` are naturally isomorphic functors, then they have equivalent categories of cocones. -/ @[simps] def precompose_equivalence {G : J ⥤ C} (α : G ≅ F) : cocone F ≌ cocone G := { functor := precompose α.hom, inverse := precompose α.inv, unit_iso := nat_iso.of_components (λ s, cocones.ext (iso.refl _) (by tidy)) (by tidy), counit_iso := nat_iso.of_components (λ s, cocones.ext (iso.refl _) (by tidy)) (by tidy) } /-- Whiskering on the left by `E : K ⥤ J` gives a functor from `cocone F` to `cocone (E ⋙ F)`. -/ @[simps] def whiskering {K : Type v} [small_category K] (E : K ⥤ J) : cocone F ⥤ cocone (E ⋙ F) := { obj := λ c, c.whisker E, map := λ c c' f, { hom := f.hom, } } /-- Whiskering by an equivalence gives an equivalence between categories of cones. -/ @[simps] def whiskering_equivalence {K : Type v} [small_category K] (e : K ≌ J) : cocone F ≌ cocone (e.functor ⋙ F) := { functor := whiskering e.functor, inverse := whiskering e.inverse ⋙ precompose ((functor.left_unitor F).inv ≫ (whisker_right (e.counit_iso).inv F) ≫ (functor.associator _ _ _).inv), unit_iso := nat_iso.of_components (λ s, cocones.ext (iso.refl _) (by tidy)) (by tidy), counit_iso := nat_iso.of_components (λ s, cocones.ext (iso.refl _) (begin intro k, have t := s.ι.naturality (e.unit.app k), dsimp at t, simp only [←e.counit_inv_app_functor k, comp_id] at t, dsimp, simp [t], end)) (by tidy), } /-- The categories of cocones over `F` and `G` are equivalent if `F` and `G` are naturally isomorphic (possibly after changing the indexing category by an equivalence). -/ @[simps functor_obj] def equivalence_of_reindexing {K : Type v} [small_category K] {G : K ⥤ C} (e : K ≌ J) (α : e.functor ⋙ F ≅ G) : cocone F ≌ cocone G := (whiskering_equivalence e).trans (precompose_equivalence α.symm) section variable (F) /-- Forget the cocone structure and obtain just the cocone point. -/ @[simps] def forget : cocone F ⥤ C := { obj := λ t, t.X, map := λ s t f, f.hom } variables {D : Type u'} [category.{v} D] (G : C ⥤ D) /-- A functor `G : C ⥤ D` sends cocones over `F` to cocones over `F ⋙ G` functorially. -/ @[simps] def functoriality : cocone F ⥤ cocone (F ⋙ G) := { obj := λ A, { X := G.obj A.X, ι := { app := λ j, G.map (A.ι.app j), naturality' := by intros; erw ←G.map_comp; tidy } }, map := λ _ _ f, { hom := G.map f.hom, w' := by intros; rw [←functor.map_comp, cocone_morphism.w] } } instance functoriality_full [full G] [faithful G] : full (functoriality F G) := { preimage := λ X Y t, { hom := G.preimage t.hom, w' := λ j, G.map_injective (by simpa using t.w j) } } instance functoriality_faithful [faithful G] : faithful (functoriality F G) := { map_injective' := λ X Y f g e, by { ext1, injection e, apply G.map_injective h_1 } } /-- If `e : C ≌ D` is an equivalence of categories, then `functoriality F e.functor` induces an equivalence between cocones over `F` and cocones over `F ⋙ e.functor`. -/ @[simps] def functoriality_equivalence (e : C ≌ D) : cocone F ≌ cocone (F ⋙ e.functor) := let f : (F ⋙ e.functor) ⋙ e.inverse ≅ F := functor.associator _ _ _ ≪≫ iso_whisker_left _ (e.unit_iso).symm ≪≫ functor.right_unitor _ in { functor := functoriality F e.functor, inverse := (functoriality (F ⋙ e.functor) e.inverse) ⋙ (precompose_equivalence f.symm).functor, unit_iso := nat_iso.of_components (λ c, cocones.ext (e.unit_iso.app _) (by tidy)) (by tidy), counit_iso := nat_iso.of_components (λ c, cocones.ext (e.counit_iso.app _) begin -- Unfortunately this doesn't work by `tidy`. -- In this configuration `simp` reaches a dead-end and needs help. intros j, dsimp, simp only [←equivalence.counit_inv_app_functor, iso.inv_hom_id_app, map_comp, equivalence.fun_inv_map, assoc, id_comp, iso.inv_hom_id_app_assoc], dsimp, simp, -- See note [dsimp, simp]. end) (by tidy), } /-- If `F` reflects isomorphisms, then `cocones.functoriality F` reflects isomorphisms as well. -/ instance reflects_cocone_isomorphism (F : C ⥤ D) [reflects_isomorphisms F] (K : J ⥤ C) : reflects_isomorphisms (cocones.functoriality K F) := begin constructor, introsI, haveI : is_iso (F.map f.hom) := (cocones.forget (K ⋙ F)).map_is_iso ((cocones.functoriality K F).map f), haveI := reflects_isomorphisms.reflects F f.hom, apply cocone_iso_of_hom_iso end end end cocones end limits namespace functor variables {D : Type u'} [category.{v} D] variables {F : J ⥤ C} {G : J ⥤ C} (H : C ⥤ D) open category_theory.limits /-- The image of a cone in C under a functor G : C ⥤ D is a cone in D. -/ @[simps] def map_cone (c : cone F) : cone (F ⋙ H) := (cones.functoriality F H).obj c /-- The image of a cocone in C under a functor G : C ⥤ D is a cocone in D. -/ @[simps] def map_cocone (c : cocone F) : cocone (F ⋙ H) := (cocones.functoriality F H).obj c /-- Given a cone morphism `c ⟶ c'`, construct a cone morphism on the mapped cones functorially. -/ def map_cone_morphism {c c' : cone F} (f : c ⟶ c') : H.map_cone c ⟶ H.map_cone c' := (cones.functoriality F H).map f /-- Given a cocone morphism `c ⟶ c'`, construct a cocone morphism on the mapped cocones functorially. -/ def map_cocone_morphism {c c' : cocone F} (f : c ⟶ c') : H.map_cocone c ⟶ H.map_cocone c' := (cocones.functoriality F H).map f /-- If `H` is an equivalence, we invert `H.map_cone` and get a cone for `F` from a cone for `F ⋙ H`.-/ def map_cone_inv [is_equivalence H] (c : cone (F ⋙ H)) : cone F := (limits.cones.functoriality_equivalence F (as_equivalence H)).inverse.obj c /-- `map_cone` is the left inverse to `map_cone_inv`. -/ def map_cone_map_cone_inv {F : J ⥤ D} (H : D ⥤ C) [is_equivalence H] (c : cone (F ⋙ H)) : map_cone H (map_cone_inv H c) ≅ c := (limits.cones.functoriality_equivalence F (as_equivalence H)).counit_iso.app c /-- `map_cone` is the right inverse to `map_cone_inv`. -/ def map_cone_inv_map_cone {F : J ⥤ D} (H : D ⥤ C) [is_equivalence H] (c : cone F) : map_cone_inv H (map_cone H c) ≅ c := (limits.cones.functoriality_equivalence F (as_equivalence H)).unit_iso.symm.app c /-- If `H` is an equivalence, we invert `H.map_cone` and get a cone for `F` from a cone for `F ⋙ H`.-/ def map_cocone_inv [is_equivalence H] (c : cocone (F ⋙ H)) : cocone F := (limits.cocones.functoriality_equivalence F (as_equivalence H)).inverse.obj c /-- `map_cocone` is the left inverse to `map_cocone_inv`. -/ def map_cocone_map_cocone_inv {F : J ⥤ D} (H : D ⥤ C) [is_equivalence H] (c : cocone (F ⋙ H)) : map_cocone H (map_cocone_inv H c) ≅ c := (limits.cocones.functoriality_equivalence F (as_equivalence H)).counit_iso.app c /-- `map_cocone` is the right inverse to `map_cocone_inv`. -/ def map_cocone_inv_map_cocone {F : J ⥤ D} (H : D ⥤ C) [is_equivalence H] (c : cocone F) : map_cocone_inv H (map_cocone H c) ≅ c := (limits.cocones.functoriality_equivalence F (as_equivalence H)).unit_iso.symm.app c /-- `functoriality F _ ⋙ postcompose (whisker_left F _)` simplifies to `functoriality F _`. -/ @[simps] def functoriality_comp_postcompose {H H' : C ⥤ D} (α : H ≅ H') : cones.functoriality F H ⋙ cones.postcompose (whisker_left F α.hom) ≅ cones.functoriality F H' := nat_iso.of_components (λ c, cones.ext (α.app _) (by tidy)) (by tidy) /-- For `F : J ⥤ C`, given a cone `c : cone F`, and a natural isomorphism `α : H ≅ H'` for functors `H H' : C ⥤ D`, the postcomposition of the cone `H.map_cone` using the isomorphism `α` is isomorphic to the cone `H'.map_cone`. -/ @[simps] def postcompose_whisker_left_map_cone {H H' : C ⥤ D} (α : H ≅ H') (c : cone F) : (cones.postcompose (whisker_left F α.hom : _)).obj (H.map_cone c) ≅ H'.map_cone c := (functoriality_comp_postcompose α).app c /-- `map_cone` commutes with `postcompose`. In particular, for `F : J ⥤ C`, given a cone `c : cone F`, a natural transformation `α : F ⟶ G` and a functor `H : C ⥤ D`, we have two obvious ways of producing a cone over `G ⋙ H`, and they are both isomorphic. -/ @[simps] def map_cone_postcompose {α : F ⟶ G} {c} : H.map_cone ((cones.postcompose α).obj c) ≅ (cones.postcompose (whisker_right α H : _)).obj (H.map_cone c) := cones.ext (iso.refl _) (by tidy) /-- `map_cone` commutes with `postcompose_equivalence` -/ @[simps] def map_cone_postcompose_equivalence_functor {α : F ≅ G} {c} : H.map_cone ((cones.postcompose_equivalence α).functor.obj c) ≅ (cones.postcompose_equivalence (iso_whisker_right α H : _)).functor.obj (H.map_cone c) := cones.ext (iso.refl _) (by tidy) /-- `functoriality F _ ⋙ precompose (whisker_left F _)` simplifies to `functoriality F _`. -/ @[simps] def functoriality_comp_precompose {H H' : C ⥤ D} (α : H ≅ H') : cocones.functoriality F H ⋙ cocones.precompose (whisker_left F α.inv) ≅ cocones.functoriality F H' := nat_iso.of_components (λ c, cocones.ext (α.app _) (by tidy)) (by tidy) /-- For `F : J ⥤ C`, given a cocone `c : cocone F`, and a natural isomorphism `α : H ≅ H'` for functors `H H' : C ⥤ D`, the precomposition of the cocone `H.map_cocone` using the isomorphism `α` is isomorphic to the cocone `H'.map_cocone`. -/ @[simps] def precompose_whisker_left_map_cocone {H H' : C ⥤ D} (α : H ≅ H') (c : cocone F) : (cocones.precompose (whisker_left F α.inv : _)).obj (H.map_cocone c) ≅ H'.map_cocone c := (functoriality_comp_precompose α).app c /-- `map_cocone` commutes with `precompose`. In particular, for `F : J ⥤ C`, given a cocone `c : cocone F`, a natural transformation `α : F ⟶ G` and a functor `H : C ⥤ D`, we have two obvious ways of producing a cocone over `G ⋙ H`, and they are both isomorphic. -/ @[simps] def map_cocone_precompose {α : F ⟶ G} {c} : H.map_cocone ((cocones.precompose α).obj c) ≅ (cocones.precompose (whisker_right α H : _)).obj (H.map_cocone c) := cocones.ext (iso.refl _) (by tidy) /-- `map_cocone` commutes with `precompose_equivalence` -/ @[simps] def map_cocone_precompose_equivalence_functor {α : F ≅ G} {c} : H.map_cocone ((cocones.precompose_equivalence α).functor.obj c) ≅ (cocones.precompose_equivalence (iso_whisker_right α H : _)).functor.obj (H.map_cocone c) := cocones.ext (iso.refl _) (by tidy) variables {K : Type v} [small_category K] /-- `map_cone` commutes with `whisker` -/ @[simps] def map_cone_whisker {E : K ⥤ J} {c : cone F} : H.map_cone (c.whisker E) ≅ (H.map_cone c).whisker E := cones.ext (iso.refl _) (by tidy) /-- `map_cocone` commutes with `whisker` -/ @[simps] def map_cocone_whisker {E : K ⥤ J} {c : cocone F} : H.map_cocone (c.whisker E) ≅ (H.map_cocone c).whisker E := cocones.ext (iso.refl _) (by tidy) end functor end category_theory namespace category_theory.limits section variables {F : J ⥤ C} /-- Change a `cocone F` into a `cone F.op`. -/ @[simps] def cocone.op (c : cocone F) : cone F.op := { X := op c.X, π := { app := λ j, (c.ι.app (unop j)).op, naturality' := λ j j' f, has_hom.hom.unop_inj (by tidy) } } /-- Change a `cone F` into a `cocone F.op`. -/ @[simps] def cone.op (c : cone F) : cocone F.op := { X := op c.X, ι := { app := λ j, (c.π.app (unop j)).op, naturality' := λ j j' f, has_hom.hom.unop_inj (by tidy) } } /-- Change a `cocone F.op` into a `cone F`. -/ @[simps] def cocone.unop (c : cocone F.op) : cone F := { X := unop c.X, π := { app := λ j, (c.ι.app (op j)).unop, naturality' := λ j j' f, has_hom.hom.op_inj begin dsimp, simp only [comp_id], exact (c.w f.op).symm, end } } /-- Change a `cone F.op` into a `cocone F`. -/ @[simps] def cone.unop (c : cone F.op) : cocone F := { X := unop c.X, ι := { app := λ j, (c.π.app (op j)).unop, naturality' := λ j j' f, has_hom.hom.op_inj begin dsimp, simp only [id_comp], exact (c.w f.op), end } } variables (F) /-- The category of cocones on `F` is equivalent to the opposite category of the category of cones on the opposite of `F`. -/ @[simps] def cocone_equivalence_op_cone_op : cocone F ≌ (cone F.op)ᵒᵖ := { functor := { obj := λ c, op (cocone.op c), map := λ X Y f, has_hom.hom.op { hom := f.hom.op, w' := λ j, by { apply has_hom.hom.unop_inj, dsimp, simp, }, } }, inverse := { obj := λ c, cone.unop (unop c), map := λ X Y f, { hom := f.unop.hom.unop, w' := λ j, by { apply has_hom.hom.op_inj, dsimp, simp, }, } }, unit_iso := nat_iso.of_components (λ c, cocones.ext (iso.refl _) (by tidy)) (by tidy), counit_iso := nat_iso.of_components (λ c, by { op_induction c, dsimp, apply iso.op, exact cones.ext (iso.refl _) (by tidy), }) begin intros, have hX : X = op (unop X) := rfl, revert hX, generalize : unop X = X', rintro rfl, have hY : Y = op (unop Y) := rfl, revert hY, generalize : unop Y = Y', rintro rfl, apply has_hom.hom.unop_inj, apply cone_morphism.ext, dsimp, simp, end, functor_unit_iso_comp' := λ c, begin apply has_hom.hom.unop_inj, ext, dsimp, simp, end } end section variables {F : J ⥤ Cᵒᵖ} /-- Change a cocone on `F.left_op : Jᵒᵖ ⥤ C` to a cocone on `F : J ⥤ Cᵒᵖ`. -/ -- Here and below we only automatically generate the `@[simp]` lemma for the `X` field, -- as we can write a simpler `rfl` lemma for the components of the natural transformation by hand. @[simps {rhs_md := semireducible, simp_rhs := tt}] def cone_of_cocone_left_op (c : cocone F.left_op) : cone F := { X := op c.X, π := nat_trans.remove_left_op (c.ι ≫ (const.op_obj_unop (op c.X)).hom) } /-- Change a cone on `F : J ⥤ Cᵒᵖ` to a cocone on `F.left_op : Jᵒᵖ ⥤ C`. -/ @[simps {rhs_md := semireducible, simp_rhs := tt}] def cocone_left_op_of_cone (c : cone F) : cocone (F.left_op) := { X := unop c.X, ι := nat_trans.left_op c.π } /-- Change a cone on `F.left_op : Jᵒᵖ ⥤ C` to a cocone on `F : J ⥤ Cᵒᵖ`. -/ /- When trying use `@[simps]` to generate the `ι_app` field of this definition, `@[simps]` tries to reduce the RHS using `expr.dsimp` and `expr.simp`, but for some reason the expression is not being simplified properly. -/ @[simps X] def cocone_of_cone_left_op (c : cone F.left_op) : cocone F := { X := op c.X, ι := nat_trans.remove_left_op ((const.op_obj_unop (op c.X)).hom ≫ c.π) } @[simp] lemma cocone_of_cone_left_op_ι_app (c : cone F.left_op) (j) : (cocone_of_cone_left_op c).ι.app j = (c.π.app (op j)).op := by { dsimp [cocone_of_cone_left_op], simp } /-- Change a cocone on `F : J ⥤ Cᵒᵖ` to a cone on `F.left_op : Jᵒᵖ ⥤ C`. -/ @[simps {rhs_md := semireducible, simp_rhs := tt}] def cone_left_op_of_cocone (c : cocone F) : cone (F.left_op) := { X := unop c.X, π := nat_trans.left_op c.ι } end end category_theory.limits namespace category_theory.functor open category_theory.limits variables {F : J ⥤ C} variables {D : Type u'} [category.{v} D] section variables (G : C ⥤ D) /-- The opposite cocone of the image of a cone is the image of the opposite cocone. -/ def map_cone_op (t : cone F) : (G.map_cone t).op ≅ (G.op.map_cocone t.op) := cocones.ext (iso.refl _) (by tidy) /-- The opposite cone of the image of a cocone is the image of the opposite cone. -/ def map_cocone_op {t : cocone F} : (G.map_cocone t).op ≅ (G.op.map_cone t.op) := cones.ext (iso.refl _) (by tidy) end end category_theory.functor
a580def80a9f335a7b640f06c625db82dbb4c2a4
3dc4623269159d02a444fe898d33e8c7e7e9461b
/.github/workflows/geo/src/pullbacks.lean
33b34a6b1a16fcb048d8cef864d09ec98a3b6d6f
[]
no_license
Or7ando/lean
cc003e6c41048eae7c34aa6bada51c9e9add9e66
d41169cf4e416a0d42092fb6bdc14131cee9dd15
refs/heads/master
1,650,600,589,722
1,587,262,906,000
1,587,262,906,000
255,387,160
0
0
null
null
null
null
UTF-8
Lean
false
false
20,699
lean
/- Copyright (c) 2020 Bhavik Mehta, Edward Ayers. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta, Edward Ayers -/ import category_theory.limits.shapes import category_theory.limits.preserves import .comma /-! # Pullbacks Many, many lemmas to work with pullbacks. -/ open category_theory category_theory.category category_theory.limits universes u v variables {C : Type u} [𝒞 : category.{v} C] variables {J : Type v} [small_category J] include 𝒞 variables {X Y Z : C} {f : X ⟶ Z} {g : Y ⟶ Z} @[simp] lemma pullback_cone.simp_left {L : C} {lx : L ⟶ X} {ly : L ⟶ Y} {e : lx ≫ f = ly ≫ g} : ((pullback_cone.mk lx ly e).π).app walking_cospan.left = lx := rfl @[simp] lemma pullback_cone.simp_right {L : C} {lx : L ⟶ X} {ly : L ⟶ Y} {e : lx ≫ f = ly ≫ g} : ((pullback_cone.mk lx ly e).π).app walking_cospan.right = ly := rfl lemma pi_app {W : C} {h : X ⟶ Z} {k : Y ⟶ Z} {c₁ c₂ : cone (cospan h k)} {f : W ⟶ c₁.X} {g : W ⟶ c₂.X} (h1 : f ≫ pullback_cone.fst c₁ = g ≫ pullback_cone.fst c₂) (h2 : f ≫ pullback_cone.snd c₁ = g ≫ pullback_cone.snd c₂) : ∀ (j : walking_cospan), f ≫ c₁.π.app j = g ≫ c₂.π.app j := begin intro j, cases j, exact h1, exact h2, rw ← cone.w c₂ walking_cospan.hom.inl, rw ← cone.w c₁ walking_cospan.hom.inl, rw ← assoc, rw ← assoc, rw h1 end /-- This is often useful in proving we have a limit for a pullback. -/ lemma pi_app_left {h : X ⟶ Z} {k : Y ⟶ Z} (c₁ c₂ : cone (cospan h k)) (f : c₂.X ⟶ c₁.X) (h1 : f ≫ pullback_cone.fst c₁ = pullback_cone.fst c₂) (h2 : f ≫ pullback_cone.snd c₁ = pullback_cone.snd c₂) : ∀ (j : walking_cospan), f ≫ c₁.π.app j = c₂.π.app j := begin convert @pi_app C _ _ _ _ _ _ _ c₁ c₂ f (𝟙 _) _ _, simp, simpa, simpa end lemma pullback_cone.hom_ext {t : pullback_cone f g} (h : is_limit t) {W : C} {f₁ f₂ : W ⟶ t.X} (h1 : f₁ ≫ pullback_cone.fst t = f₂ ≫ pullback_cone.fst t) (h2 : f₁ ≫ pullback_cone.snd t = f₂ ≫ pullback_cone.snd t) : f₁ = f₂ := is_limit.hom_ext h (pi_app h1 h2) lemma pullback.hom_ext {X Y Z A : C} {f : X ⟶ Z} {g : Y ⟶ Z} [has_limit (cospan f g)] (a b : A ⟶ pullback f g) (h1 : a ≫ pullback.fst = b ≫ pullback.fst) (h2 : a ≫ pullback.snd = b ≫ pullback.snd) : a = b := pullback_cone.hom_ext (limit.is_limit _) h1 h2 @[simp] lemma pullback.lift_self_id {X Y Z : C} {f : X ⟶ Z} {g : Y ⟶ Z} [has_limit (cospan f g)] : pullback.lift pullback.fst pullback.snd pullback.condition = 𝟙 (pullback f g) := begin apply pullback.hom_ext, rw limit.lift_π, rw id_comp, refl, rw limit.lift_π, rw id_comp, refl end def iso_apex_of_iso_cone {F : J ⥤ C} {c₁ c₂ : cone F} (h : c₁ ≅ c₂) : c₁.X ≅ c₂.X := { hom := h.hom.hom, inv := h.inv.hom, hom_inv_id' := begin show (h.hom ≫ h.inv).hom = 𝟙 (c₁.X), have: h.hom ≫ h.inv = 𝟙 c₁ := h.hom_inv_id', rw this, refl end, inv_hom_id' := begin show (h.inv ≫ h.hom).hom = 𝟙 (c₂.X), have: h.inv ≫ h.hom = 𝟙 c₂ := h.inv_hom_id', rw this, refl end, } -- The pasting lemma for pullbacks. lemma pasting {C : Type u} [𝒞 : category.{v} C] {U V W X Y Z : C} (f : U ⟶ V) (g : V ⟶ W) (h : U ⟶ X) (k : V ⟶ Y) (l : W ⟶ Z) (m : X ⟶ Y) (n : Y ⟶ Z) (left_comm : f ≫ k = h ≫ m) (right_comm : g ≫ l = k ≫ n) (right : is_limit (pullback_cone.mk g k right_comm)) : is_limit (pullback_cone.mk (f ≫ g) h (begin rw assoc, rw right_comm, rw ← assoc, rw left_comm, rw assoc end)) ≅ is_limit (pullback_cone.mk f h left_comm) := { hom := begin intro entire, refine ⟨λ c, _, _, _⟩, { have new_cone_comm: (pullback_cone.fst c ≫ g) ≫ l = pullback_cone.snd c ≫ m ≫ n, rw assoc, rw ← pullback_cone.condition_assoc, rw right_comm, exact entire.lift (pullback_cone.mk (pullback_cone.fst c ≫ g) (pullback_cone.snd c) new_cone_comm) }, { intro c, have new_cone_comm: (pullback_cone.fst c ≫ g) ≫ l = pullback_cone.snd c ≫ m ≫ n, rw assoc, rw ← pullback_cone.condition_assoc, rw right_comm, set new_cone := pullback_cone.mk (pullback_cone.fst c ≫ g) (pullback_cone.snd c) new_cone_comm, have coned := entire.fac new_cone, apply pi_app_left (pullback_cone.mk f h left_comm), { apply pullback_cone.hom_ext right, { rw assoc, exact coned walking_cospan.left }, { rw assoc, conv_lhs {congr, skip, erw left_comm}, rw ← assoc, erw [pullback_cone.condition c, coned walking_cospan.right], refl } }, { exact coned walking_cospan.right }}, { intros c r j, have new_cone_comm: (pullback_cone.fst c ≫ g) ≫ l = pullback_cone.snd c ≫ m ≫ n, rw assoc, rw ← pullback_cone.condition_assoc, rw right_comm, set new_cone := pullback_cone.mk (pullback_cone.fst c ≫ g) (pullback_cone.snd c) new_cone_comm, apply entire.uniq new_cone r, -- BM: here apply pi_app_left (pullback_cone.mk (f ≫ g) h _) new_cone _, { show r ≫ f ≫ g = _ ≫ g, rw ← assoc, congr, exact j walking_cospan.left }, { show r ≫ h = (new_cone.π).app walking_cospan.right, exact j walking_cospan.right }, } end, inv := begin intro left, refine ⟨λ c, _, λ c, _, λ c, _⟩, { have new_cone_comm: pullback_cone.fst c ≫ l = (pullback_cone.snd c ≫ m) ≫ n, rw assoc, rw pullback_cone.condition, have new_cone2_comm: (right.lift (pullback_cone.mk _ _ new_cone_comm)) ≫ k = (pullback_cone.snd c : c.X ⟶ X) ≫ m := right.fac (pullback_cone.mk _ _ new_cone_comm) walking_cospan.right, exact left.lift (pullback_cone.mk _ _ new_cone2_comm) }, { set π₁ : c.X ⟶ W := pullback_cone.fst c, set π₂ : c.X ⟶ X := pullback_cone.snd c, have new_cone_comm: π₁ ≫ l = (π₂ ≫ m) ≫ n, rw assoc, rw pullback_cone.condition, have new_cone2_comm: (right.lift (pullback_cone.mk _ _ new_cone_comm)) ≫ k = π₂ ≫ m := right.fac (pullback_cone.mk _ _ new_cone_comm) walking_cospan.right, set new_cone := pullback_cone.mk _ _ new_cone_comm, set new_cone2 := pullback_cone.mk _ _ new_cone2_comm, apply pi_app_left (pullback_cone.mk (f ≫ g) h _) c, erw [← assoc, left.fac' new_cone2 walking_cospan.left, right.fac' new_cone walking_cospan.left], refl, exact left.fac' new_cone2 walking_cospan.right }, { set π₁ : c.X ⟶ W := pullback_cone.fst c, set π₂ : c.X ⟶ X := pullback_cone.snd c, have new_cone_comm: π₁ ≫ l = (π₂ ≫ m) ≫ n, rw assoc, rw pullback_cone.condition, set new_cone := pullback_cone.mk _ _ new_cone_comm, have new_cone2_comm: (right.lift new_cone) ≫ k = π₂ ≫ m := right.fac' new_cone walking_cospan.right, set new_cone2 := pullback_cone.mk _ _ new_cone2_comm, intros r J, show r = left.lift new_cone2, have Jr: r ≫ h = π₂ := J walking_cospan.right, apply left.uniq new_cone2, -- BM: here apply pi_app_left (pullback_cone.mk f h left_comm) new_cone2 _ _ Jr, { apply right.uniq new_cone, -- BM: here apply pi_app_left (pullback_cone.mk g k right_comm) new_cone, { rw assoc, exact J walking_cospan.left}, { rw assoc, show r ≫ f ≫ k = π₂ ≫ m, rw ← Jr, conv_rhs {rw assoc}, congr, exact left_comm} } } end , hom_inv_id' := subsingleton.elim _ _ , inv_hom_id' := subsingleton.elim _ _ } def pullback.with_id_r' {X Y : C} (f : X ⟶ Y) : is_limit (pullback_cone.mk f (𝟙 X) (by simp) : pullback_cone (𝟙 Y) f) := { lift := λ c, (c.π).app walking_cospan.right, fac' := λ c j, begin cases j, -- BM: triple case { erw ← pullback_cone.condition c, simp }, { erw comp_id }, show _ ≫ f ≫ 𝟙 Y = _, erw [comp_id, ← c.π.naturality walking_cospan.hom.inr, id_comp], end, uniq' := λ _ _ J, by erw ← J walking_cospan.right; exact (comp_id _ _).symm } @[reducible] def cospan_cone.flip {f : X ⟶ Z} {g : Y ⟶ Z} (c : cone (cospan f g)) : cone (cospan g f) := pullback_cone.mk (pullback_cone.snd c) (pullback_cone.fst c) (pullback_cone.condition c).symm def flip_mk {X Y Z W : C} {f : X ⟶ Y} {g : X ⟶ Z} {h : Y ⟶ W} {k : Z ⟶ W} (comm : f ≫ h = g ≫ k) : cospan_cone.flip (pullback_cone.mk f g comm) ≅ pullback_cone.mk g f comm.symm := by apply cones.ext (iso.refl _) (λ j, _); erw id_comp def flip_twice {f : X ⟶ Z} {g : Y ⟶ Z} (c : cone (cospan f g)) : cospan_cone.flip (cospan_cone.flip c) ≅ c := begin apply cones.ext _ _, exact iso.refl _, intros j, erw id_comp, cases j, -- BM: triple case refl, refl, apply cone.w c walking_cospan.hom.inl end def flip_hom {f : X ⟶ Z} {g : Y ⟶ Z} {c₁ c₂ : cone (cospan f g)} (h : c₁ ⟶ c₂) : cospan_cone.flip c₁ ⟶ cospan_cone.flip c₂ := { hom := h.hom, w' := begin rintro (_ | _ | _), apply h.w, apply h.w, erw [← assoc, h.w], refl end} -- BM: triple case def pullback.flip {Y Z W : C} {h : Y ⟶ W} {k : Z ⟶ W} {c : cone (cospan h k)} (z : is_limit c) : is_limit (cospan_cone.flip c) := { lift := λ s, z.lift (cospan_cone.flip s), fac' := λ s j, walking_cospan.cases_on j (z.fac' (cospan_cone.flip s) walking_cospan.right) (z.fac' (cospan_cone.flip s) walking_cospan.left) (begin show _ ≫ _ ≫ _ = _, rw ← cone.w s walking_cospan.hom.inr, rw ← pullback_cone.condition c, rw ← assoc, erw z.fac', refl end), -- BM: triple case uniq' := λ s m J, begin apply z.uniq (cospan_cone.flip s), apply pi_app_left c (cospan_cone.flip s), erw J walking_cospan.right, refl, erw J walking_cospan.left, refl, end } def pullback.flip'' {Y Z W : C} {h : Y ⟶ W} {k : Z ⟶ W} {c : cone (cospan h k)} : is_limit c ≅ is_limit (cospan_cone.flip c) := { hom := pullback.flip, inv := pullback.flip ≫ (λ l, is_limit.of_iso_limit l (flip_twice _))} def flip_limit_cone [@has_pullbacks C 𝒞] (f : X ⟶ Z) (g : Y ⟶ Z) : cospan_cone.flip (limit.cone (cospan g f)) ≅ limit.cone (cospan f g) := { hom := limit.cone_morphism _, inv := ((flip_twice _).inv ≫ flip_hom (limit.cone_morphism _)), hom_inv_id' := begin ext, simp, dunfold flip_hom flip_twice cones.ext, erw [id_comp, limit.lift_π], { erw limit.lift_π, refl }, { simp, erw limit.lift_π, dunfold flip_twice cospan_cone.flip, simp, erw [id_comp, limit.lift_π], refl } end, inv_hom_id' := is_limit.uniq_cone_morphism (limit.is_limit _) } def pullback.flip' [@has_pullbacks C 𝒞] (f : X ⟶ Z) (g : Y ⟶ Z) : pullback f g ≅ pullback g f := iso_apex_of_iso_cone (flip_limit_cone f g).symm def pullback.with_id_l' {X Y : C} (f : X ⟶ Y) : is_limit (pullback_cone.mk (𝟙 X) f (show (𝟙 X) ≫ f = f ≫ (𝟙 Y), by simp)) := is_limit.of_iso_limit (pullback.flip (pullback.with_id_r' f)) (flip_mk _) def identify_limit_apex {F : J ⥤ C} [has_limit F] {a : cone F} (t : is_limit a) : (limit.cone F).X ≅ a.X := iso_apex_of_iso_cone (is_limit.unique_up_to_iso (limit.is_limit _) t) /- Note that we need `has_pullbacks` even though this particular pullback always exists, because here we are showing that the constructive limit derived using has_pullbacks has to be iso to this simple definition. -/ def pullback.with_id_r [@has_pullbacks C 𝒞] {X Y : C} (f : X ⟶ Y) : pullback (𝟙 Y) f ≅ X := identify_limit_apex (pullback.with_id_r' f) def pullback.with_id_l [@has_pullbacks C 𝒞] {X Y : C} (f : X ⟶ Y) : pullback f (𝟙 Y) ≅ X := pullback.flip' _ _ ≪≫ pullback.with_id_r f lemma make_pullback [has_limit (cospan f g)] : pullback_cone.mk pullback.fst pullback.snd pullback.condition ≅ limit.cone (cospan f g) := begin apply cones.ext _ (λ j, _), refl, erw id_comp, cases j, refl, refl, apply (limit.cone (cospan f g)).w walking_cospan.hom.inl end -- todo: use pasting here lemma pullback.comp_l {W X Y Z : C} {xz : X ⟶ Z} {yz : Y ⟶ Z} {wx : W ⟶ X} [@has_pullbacks C 𝒞]: pullback (wx ≫ xz) yz ≅ pullback wx (@pullback.fst _ _ _ _ _ xz yz _) := begin apply iso.mk _ _ _ _, { refine pullback.lift pullback.fst (pullback.lift (pullback.fst ≫ wx) pullback.snd _) _, simp, rw pullback.condition, simp}, { refine pullback.lift pullback.fst (pullback.snd ≫ pullback.snd) _, rw ← category.assoc, rw pullback.condition, simp, rw pullback.condition }, {apply pullback.hom_ext, simp, simp }, {apply pullback.hom_ext, simp, simp, apply pullback.hom_ext, simp, apply pullback.condition, simp}, end lemma test [has_pullbacks.{v} C] {X Y Z : C} {xz : X ⟶ Z} {yz : Y ⟶ Z} : is_limit (pullback_cone.mk pullback.fst pullback.snd pullback.condition : pullback_cone yz xz) := (limit.is_limit _).of_iso_limit make_pullback.symm lemma pullback.comp_r {W X Y Z : C} {xz : X ⟶ Z} {yz : Y ⟶ Z} {wx : W ⟶ X} [@has_pullbacks C 𝒞]: pullback yz (wx ≫ xz) ≅ pullback (@pullback.snd _ _ _ _ _ yz xz _) wx := identify_limit_apex ((pasting _ _ _ _ _ _ _ _ _ test).inv test) ≪≫ iso_apex_of_iso_cone make_pullback -- Show -- D × A ⟶ B × A -- | | -- v v -- D ⟶ B -- is a pullback (needed in over/exponentiable_in_slice) def pullback_prod (xy : X ⟶ Y) (Z : C) [has_binary_products.{v} C] : is_limit (pullback_cone.mk limits.prod.fst (limits.prod.map xy (𝟙 Z)) (by simp) : pullback_cone xy limits.prod.fst) := { lift := λ s, prod.lift (pullback_cone.fst s) (pullback_cone.snd s ≫ limits.prod.snd), fac' := λ s, begin apply pi_app_left (pullback_cone.mk limits.prod.fst (limits.prod.map xy (𝟙 Z)) _) s, dsimp, dunfold pullback_cone.fst, simp, -- this should have been just simp apply limit.hom_ext, intro j, cases j, simp, dsimp, -- this should be easy. dunfold pullback_cone.snd, rw pullback_cone.simp_right, simp, exact pullback_cone.condition s, simp, dunfold pullback_cone.snd, simp, dsimp, simp -- look here ed end, uniq' := λ s m J, begin ext, cases j, simp, apply J walking_cospan.left, simp, dunfold pullback_cone.snd, erw ← J walking_cospan.right, simp, dsimp, simp end } def pullback_prod' (xy : X ⟶ Y) (Z : C) [has_binary_products.{v} C] : is_limit (pullback_cone.mk limits.prod.snd (limits.prod.map (𝟙 Z) xy) (by simp) : pullback_cone xy limits.prod.snd) := { lift := λ s, prod.lift (pullback_cone.snd s ≫ limits.prod.fst) (pullback_cone.fst s), fac' := λ s, begin apply pi_app_left (pullback_cone.mk limits.prod.snd (limits.prod.map (𝟙 Z) xy) _) s, dsimp, dunfold pullback_cone.fst, simp, apply limit.hom_ext, intro j, cases j, simp, dsimp, dunfold pullback_cone.snd, rw pullback_cone.simp_right, simp, dsimp, simp, simp, dunfold pullback_cone.snd, simp, dsimp, rw pullback_cone.condition s, end, uniq' := λ s m J, begin ext, cases j, simp, dunfold pullback_cone.snd, erw ← J walking_cospan.right, simp, dsimp, simp, simp, dsimp, dunfold pullback_cone.fst, erw ← J walking_cospan.left, simp, end } @[reducible] def pullback_of_iso {U V W X : C} {f : U ⟶ X} {g : V ⟶ X} {h : W ⟶ X} (z : V ≅ W) (hyp : z.hom ≫ h = g) (c : pullback_cone f g) : pullback_cone f h := pullback_cone.mk c.fst (c.snd ≫ z.hom) (by rw [pullback_cone.condition c, assoc, hyp]) set_option pp.implicit false lemma pullback_of_iso_is_limit {U V W X : C} (f : U ⟶ X) {g : V ⟶ X} {h : W ⟶ X} (z : V ≅ W) (hyp : z.hom ≫ h = g) (c : pullback_cone f g) : is_limit c ≅ is_limit (pullback_of_iso z hyp c) := { hom := λ t, { lift := begin intro s, apply t.lift (pullback_of_iso z.symm _ s), rw [iso.symm_hom, iso.inv_comp_eq, hyp], end, fac' := begin intro s, apply pi_app_left (pullback_of_iso z hyp c) s, apply t.fac, erw ← assoc, rw t.fac, erw assoc, simp end, uniq' := begin intros s m J, apply t.uniq (pullback_of_iso z.symm _ s), apply pi_app_left c (pullback_of_iso _ _ _), erw J walking_cospan.left, refl, erw ← iso.comp_inv_eq, rw assoc, exact J walking_cospan.right end }, inv := λ t, { lift := λ s, t.lift (pullback_of_iso z hyp s), fac' := begin intro s, apply pi_app_left c s, exact t.fac (pullback_of_iso z hyp s) walking_cospan.left, have := t.fac (pullback_of_iso z hyp s) walking_cospan.right, simp at this, rw ← assoc at this, rw cancel_mono at this, assumption end, uniq' := λ s m J, begin apply t.uniq (pullback_of_iso z hyp s), apply pi_app_left (pullback_of_iso z hyp c) (pullback_of_iso z hyp s), apply J walking_cospan.left, erw ← assoc, erw J walking_cospan.right, refl end}, hom_inv_id' := subsingleton.elim _ _, inv_hom_id' := subsingleton.elim _ _} /-- If V and W are isomorphic, and g : V ⟶ X, h : W ⟶ X respect the isomorphism, then the pullback of f along g is isomorphic to the pullback of f along h -/ lemma pullback_of_iso_apex [has_pullbacks.{v} C] {U V W X : C} {f : U ⟶ X} {g : V ⟶ X} {h : W ⟶ X} (z : V ≅ W) (hyp : z.hom ≫ h = g) : pullback f g ≅ pullback f h := (identify_limit_apex ((pullback_of_iso_is_limit f z hyp (limit.cone _)).hom (limit.is_limit _))).symm lemma pullback.comp_l' {W X Y Z : C} {xz : X ⟶ Z} {yz : Y ⟶ Z} {wx : W ⟶ X} [@has_pullbacks C 𝒞]: pullback (wx ≫ xz) yz ≅ pullback wx (@pullback.fst _ _ _ _ _ xz yz _) := pullback.flip' _ _ ≪≫ pullback.comp_r ≪≫ pullback.flip' _ _ ≪≫ begin show pullback wx (@pullback.snd _ _ _ _ _ yz xz _ : pullback yz xz ⟶ X) ≅ pullback wx (@pullback.fst _ _ _ _ _ xz yz _ : pullback xz yz ⟶ X), apply pullback_of_iso_apex (pullback.flip' _ _), -- XXX: this goal should probably be its own lemma dunfold pullback.flip' iso_apex_of_iso_cone flip_limit_cone flip_twice flip_hom, show (𝟙 _ ≫ _) ≫ _ = _, erw id_comp, erw [limit.lift_π], refl end -- [todo] comp_r; I was hoping there would be a cool way of lifting the isomorphism `(cospan f g).cones ≅ (cospan g f).cones` but can't see it. /-- Pullback of a monic is monic. -/ lemma pullback.preserve_mono [@has_pullbacks C 𝒞] {X Y Z : C} {f : X ⟶ Z} {g : Y ⟶ Z} (hm : mono f) : @mono _ _ (pullback f g) _ pullback.snd := begin split, intros A a b e, have c : pullback.fst ≫ f = pullback.snd ≫ g, apply pullback.condition, apply pullback.hom_ext, show a ≫ pullback.fst = b ≫ pullback.fst, apply hm.1, simp, rw c, rw ← category.assoc, rw e, simp, show a ≫ pullback.snd = b ≫ pullback.snd, assumption, end def over.pullback [@has_pullbacks C 𝒞] {X Y : C} (f : X ⟶ Y) (g : over Y) : over X := over.mk (@pullback.fst _ _ _ _ _ f g.hom _) @[simp] lemma over_pullback_def [@has_pullbacks C 𝒞] {X Y : C} (f : X ⟶ Y) (g : over Y) : (over.pullback f g).hom = pullback.fst := rfl lemma mono_of_pullback (X Y : C) (f : X ⟶ Y) (hl : is_limit (pullback_cone.mk (𝟙 X) (𝟙 X) (by simp) : pullback_cone f f)) : mono f := begin split, intros, set new_cone : pullback_cone f f := pullback_cone.mk g h w, exact (hl.fac new_cone walking_cospan.left).symm.trans (hl.fac new_cone walking_cospan.right), end lemma pullback_of_mono (X Y : C) (f : X ⟶ Y) (hf : mono f) : is_limit (pullback_cone.mk (𝟙 X) (𝟙 X) (by simp) : pullback_cone f f) := { lift := λ s, pullback_cone.fst s, fac' := λ s, begin apply pi_app_left (pullback_cone.mk (𝟙 X) (𝟙 X) _) s, erw comp_id, erw comp_id, rw ← cancel_mono f, exact pullback_cone.condition s end, uniq' := λ s m J, (comp_id _ m).symm.trans (J walking_cospan.left) } universe u₂ lemma cospan_comp {D : Type u₂} [category.{v} D] (F : C ⥤ D) : cospan (F.map f) (F.map g) = cospan f g ⋙ F := begin apply category_theory.functor.ext, intros, cases f_1, simp, simp, simp, dsimp, simp, intro j, cases j, simp, simp, simp end lemma preserves_mono_of_preserves_pullback {D : Type u₂} [category.{v} D] (F : C ⥤ D) (hF : preserves_limits_of_shape walking_cospan F) (X Y : C) (f : X ⟶ Y) (hf : mono f) : mono (F.map f) := begin apply mono_of_pullback, have that: is_limit _ := preserves_limit.preserves F (pullback_of_mono _ _ f hf), have: cospan (F.map f) (F.map f) = cospan f f ⋙ F := cospan_comp _, convert that, dsimp [functor.map_cone, cones.functoriality, pullback_cone.mk], congr, assumption, assumption, refine function.hfunext rfl _, intros, tactic.case_bash, simp, simp, simp, apply proof_irrel_heq end
8333a9053bd1133880e250b002d554acb5bac145
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/set_theory/ordinal/basic.lean
87aae5edf6b63763ddad3621ec706b4cf1b58cbe
[ "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
47,051
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Floris van Doorn -/ import data.sum.order import order.initial_seg import set_theory.cardinal.basic /-! # Ordinals Ordinals are defined as equivalences of well-ordered sets under order isomorphism. They are endowed with a total order, where an ordinal is smaller than another one if it embeds into it as an initial segment (or, equivalently, in any way). This total order is well founded. ## Main definitions * `ordinal`: the type of ordinals (in a given universe) * `ordinal.type r`: given a well-founded order `r`, this is the corresponding ordinal * `ordinal.typein r a`: given a well-founded order `r` on a type `α`, and `a : α`, the ordinal corresponding to all elements smaller than `a`. * `enum r o h`: given a well-order `r` on a type `α`, and an ordinal `o` strictly smaller than the ordinal corresponding to `r` (this is the assumption `h`), returns the `o`-th element of `α`. In other words, the elements of `α` can be enumerated using ordinals up to `type r`. * `ordinal.card o`: the cardinality of an ordinal `o`. * `ordinal.lift` lifts an ordinal in universe `u` to an ordinal in universe `max u v`. For a version registering additionally that this is an initial segment embedding, see `ordinal.lift.initial_seg`. For a version regiserting that it is a principal segment embedding if `u < v`, see `ordinal.lift.principal_seg`. * `ordinal.omega` or `ω` is the order type of `ℕ`. This definition is universe polymorphic: `ordinal.omega.{u} : ordinal.{u}` (contrast with `ℕ : Type`, which lives in a specific universe). In some cases the universe level has to be given explicitly. * `o₁ + o₂` is the order on the disjoint union of `o₁` and `o₂` obtained by declaring that every element of `o₁` is smaller than every element of `o₂`. The main properties of addition (and the other operations on ordinals) are stated and proved in `ordinal_arithmetic.lean`. Here, we only introduce it and prove its basic properties to deduce the fact that the order on ordinals is total (and well founded). * `succ o` is the successor of the ordinal `o`. * `cardinal.ord c`: when `c` is a cardinal, `ord c` is the smallest ordinal with this cardinality. It is the canonical way to represent a cardinal with an ordinal. A conditionally complete linear order with bot structure is registered on ordinals, where `⊥` is `0`, the ordinal corresponding to the empty type, and `Inf` is the minimum for nonempty sets and `0` for the empty set by convention. ## Notations * `ω` is a notation for the first infinite ordinal in the locale `ordinal`. -/ noncomputable theory open function cardinal set equiv order open_locale classical cardinal initial_seg universes u v w variables {α : Type*} {β : Type*} {γ : Type*} {r : α → α → Prop} {s : β → β → Prop} {t : γ → γ → Prop} /-! ### Well order on an arbitrary type -/ section well_ordering_thm parameter {σ : Type u} open function theorem nonempty_embedding_to_cardinal : nonempty (σ ↪ cardinal.{u}) := (embedding.total _ _).resolve_left $ λ ⟨⟨f, hf⟩⟩, let g : σ → cardinal.{u} := inv_fun f in let ⟨x, (hx : g x = 2 ^ sum g)⟩ := inv_fun_surjective hf (2 ^ sum g) in have g x ≤ sum g, from le_sum.{u u} g x, not_le_of_gt (by rw hx; exact cantor _) this /-- An embedding of any type to the set of cardinals. -/ def embedding_to_cardinal : σ ↪ cardinal.{u} := classical.choice nonempty_embedding_to_cardinal /-- Any type can be endowed with a well order, obtained by pulling back the well order over cardinals by some embedding. -/ def well_ordering_rel : σ → σ → Prop := embedding_to_cardinal ⁻¹'o (<) instance well_ordering_rel.is_well_order : is_well_order σ well_ordering_rel := (rel_embedding.preimage _ _).is_well_order instance is_well_order.subtype_nonempty : nonempty {r // is_well_order σ r} := ⟨⟨well_ordering_rel, infer_instance⟩⟩ end well_ordering_thm /-! ### Definition of ordinals -/ /-- Bundled structure registering a well order on a type. Ordinals will be defined as a quotient of this type. -/ structure Well_order : Type (u+1) := (α : Type u) (r : α → α → Prop) (wo : is_well_order α r) attribute [instance] Well_order.wo namespace Well_order instance : inhabited Well_order := ⟨⟨pempty, _, empty_relation.is_well_order⟩⟩ end Well_order /-- Equivalence relation on well orders on arbitrary types in universe `u`, given by order isomorphism. -/ instance ordinal.is_equivalent : setoid Well_order := { r := λ ⟨α, r, wo⟩ ⟨β, s, wo'⟩, nonempty (r ≃r s), iseqv := ⟨λ ⟨α, r, _⟩, ⟨rel_iso.refl _⟩, λ ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨e⟩, ⟨e.symm⟩, λ ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨γ, t, _⟩ ⟨e₁⟩ ⟨e₂⟩, ⟨e₁.trans e₂⟩⟩ } /-- `ordinal.{u}` is the type of well orders in `Type u`, up to order isomorphism. -/ def ordinal : Type (u + 1) := quotient ordinal.is_equivalent instance has_well_founded_out (o : ordinal) : has_well_founded o.out.α := ⟨o.out.r, o.out.wo.wf⟩ instance linear_order_out (o : ordinal) : linear_order o.out.α := is_well_order.linear_order o.out.r instance is_well_order_out_lt (o : ordinal) : is_well_order o.out.α (<) := o.out.wo namespace ordinal /-- The order type of a well order is an ordinal. -/ def type (r : α → α → Prop) [wo : is_well_order α r] : ordinal := ⟦⟨α, r, wo⟩⟧ /-- The order type of an element inside a well order. For the embedding as a principal segment, see `typein.principal_seg`. -/ def typein (r : α → α → Prop) [is_well_order α r] (a : α) : ordinal := type (subrel r {b | r b a}) @[simp] theorem type_def' (w : Well_order) : ⟦w⟧ = type w.r := by { cases w, refl } @[simp] theorem type_def (r) [wo : is_well_order α r] : (⟦⟨α, r, wo⟩⟧ : ordinal) = type r := rfl theorem type_eq {α β} {r : α → α → Prop} {s : β → β → Prop} [is_well_order α r] [is_well_order β s] : type r = type s ↔ nonempty (r ≃r s) := quotient.eq theorem _root_.rel_iso.ordinal_type_eq {α β} {r : α → α → Prop} {s : β → β → Prop} [is_well_order α r] [is_well_order β s] (h : r ≃r s) : type r = type s := type_eq.2 ⟨h⟩ @[simp] theorem type_lt (o : ordinal) : type ((<) : o.out.α → o.out.α → Prop) = o := (type_def' _).symm.trans $ quotient.out_eq o @[simp] theorem type_preimage {α β : Type u} (r : α → α → Prop) [is_well_order α r] (f : β ≃ α) : type (f ⁻¹'o r) = type r := (rel_iso.preimage f r).ordinal_type_eq @[elab_as_eliminator] theorem induction_on {C : ordinal → Prop} (o : ordinal) (H : ∀ α r [is_well_order α r], by exactI C (type r)) : C o := quot.induction_on o $ λ ⟨α, r, wo⟩, @H α r wo /-! ### The order on ordinals -/ instance : partial_order ordinal := { le := λ a b, quotient.lift_on₂ a b (λ ⟨α, r, wo⟩ ⟨β, s, wo'⟩, nonempty (r ≼i s)) $ λ ⟨α₁, r₁, o₁⟩ ⟨α₂, r₂, o₂⟩ ⟨β₁, s₁, p₁⟩ ⟨β₂, s₂, p₂⟩ ⟨f⟩ ⟨g⟩, propext ⟨ λ ⟨h⟩, ⟨(initial_seg.of_iso f.symm).trans $ h.trans (initial_seg.of_iso g)⟩, λ ⟨h⟩, ⟨(initial_seg.of_iso f).trans $ h.trans (initial_seg.of_iso g.symm)⟩⟩, lt := λ a b, quotient.lift_on₂ a b (λ ⟨α, r, wo⟩ ⟨β, s, wo'⟩, nonempty (r ≺i s)) $ λ ⟨α₁, r₁, o₁⟩ ⟨α₂, r₂, o₂⟩ ⟨β₁, s₁, p₁⟩ ⟨β₂, s₂, p₂⟩ ⟨f⟩ ⟨g⟩, by exactI propext ⟨ λ ⟨h⟩, ⟨principal_seg.equiv_lt f.symm $ h.lt_le (initial_seg.of_iso g)⟩, λ ⟨h⟩, ⟨principal_seg.equiv_lt f $ h.lt_le (initial_seg.of_iso g.symm)⟩⟩, le_refl := quot.ind $ by exact λ ⟨α, r, wo⟩, ⟨initial_seg.refl _⟩, le_trans := λ a b c, quotient.induction_on₃ a b c $ λ ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨γ, t, _⟩ ⟨f⟩ ⟨g⟩, ⟨f.trans g⟩, lt_iff_le_not_le := λ a b, quotient.induction_on₂ a b $ λ ⟨α, r, _⟩ ⟨β, s, _⟩, by exactI ⟨λ ⟨f⟩, ⟨⟨f⟩, λ ⟨g⟩, (f.lt_le g).irrefl⟩, λ ⟨⟨f⟩, h⟩, sum.rec_on f.lt_or_eq (λ g, ⟨g⟩) (λ g, (h ⟨initial_seg.of_iso g.symm⟩).elim)⟩, le_antisymm := λ a b, quotient.induction_on₂ a b $ λ ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨h₁⟩ ⟨h₂⟩, by exactI quot.sound ⟨initial_seg.antisymm h₁ h₂⟩ } /-- Ordinal less-equal is defined such that well orders `r` and `s` satisfy `type r ≤ type s` if there exists a function embedding `r` as an initial segment of `s`. -/ add_decl_doc ordinal.partial_order.le /-- Ordinal less-than is defined such that well orders `r` and `s` satisfy `type r < type s` if there exists a function embedding `r` as a principal segment of `s`. -/ add_decl_doc ordinal.partial_order.lt theorem type_le_iff {α β} {r : α → α → Prop} {s : β → β → Prop} [is_well_order α r] [is_well_order β s] : type r ≤ type s ↔ nonempty (r ≼i s) := iff.rfl theorem type_le_iff' {α β} {r : α → α → Prop} {s : β → β → Prop} [is_well_order α r] [is_well_order β s] : type r ≤ type s ↔ nonempty (r ↪r s) := ⟨λ ⟨f⟩, ⟨f⟩, λ ⟨f⟩, ⟨f.collapse⟩⟩ theorem _root_.initial_seg.ordinal_type_le {α β} {r : α → α → Prop} {s : β → β → Prop} [is_well_order α r] [is_well_order β s] (h : r ≼i s) : type r ≤ type s := ⟨h⟩ theorem _root_.rel_embedding.ordinal_type_le {α β} {r : α → α → Prop} {s : β → β → Prop} [is_well_order α r] [is_well_order β s] (h : r ↪r s) : type r ≤ type s := ⟨h.collapse⟩ @[simp] theorem type_lt_iff {α β} {r : α → α → Prop} {s : β → β → Prop} [is_well_order α r] [is_well_order β s] : type r < type s ↔ nonempty (r ≺i s) := iff.rfl theorem _root_.principal_seg.ordinal_type_lt {α β} {r : α → α → Prop} {s : β → β → Prop} [is_well_order α r] [is_well_order β s] (h : r ≺i s) : type r < type s := ⟨h⟩ /-- Given two ordinals `α ≤ β`, then `initial_seg_out α β` is the initial segment embedding of `α` to `β`, as map from a model type for `α` to a model type for `β`. -/ def initial_seg_out {α β : ordinal} (h : α ≤ β) : initial_seg ((<) : α.out.α → α.out.α → Prop) ((<) : β.out.α → β.out.α → Prop) := begin change α.out.r ≼i β.out.r, rw [←quotient.out_eq α, ←quotient.out_eq β] at h, revert h, cases quotient.out α, cases quotient.out β, exact classical.choice end /-- Given two ordinals `α < β`, then `principal_seg_out α β` is the principal segment embedding of `α` to `β`, as map from a model type for `α` to a model type for `β`. -/ def principal_seg_out {α β : ordinal} (h : α < β) : principal_seg ((<) : α.out.α → α.out.α → Prop) ((<) : β.out.α → β.out.α → Prop) := begin change α.out.r ≺i β.out.r, rw [←quotient.out_eq α, ←quotient.out_eq β] at h, revert h, cases quotient.out α, cases quotient.out β, exact classical.choice end theorem typein_lt_type (r : α → α → Prop) [is_well_order α r] (a : α) : typein r a < type r := ⟨principal_seg.of_element _ _⟩ theorem typein_lt_self {o : ordinal} (i : o.out.α) : typein (<) i < o := by { simp_rw ←type_lt o, apply typein_lt_type } @[simp] theorem typein_top {α β} {r : α → α → Prop} {s : β → β → Prop} [is_well_order α r] [is_well_order β s] (f : r ≺i s) : typein s f.top = type r := eq.symm $ quot.sound ⟨rel_iso.of_surjective (rel_embedding.cod_restrict _ f f.lt_top) (λ ⟨a, h⟩, by rcases f.down.1 h with ⟨b, rfl⟩; exact ⟨b, rfl⟩)⟩ @[simp] theorem typein_apply {α β} {r : α → α → Prop} {s : β → β → Prop} [is_well_order α r] [is_well_order β s] (f : r ≼i s) (a : α) : ordinal.typein s (f a) = ordinal.typein r a := eq.symm $ quotient.sound ⟨rel_iso.of_surjective (rel_embedding.cod_restrict _ ((subrel.rel_embedding _ _).trans f) (λ ⟨x, h⟩, by rw [rel_embedding.trans_apply]; exact f.to_rel_embedding.map_rel_iff.2 h)) (λ ⟨y, h⟩, by rcases f.init' h with ⟨a, rfl⟩; exact ⟨⟨a, f.to_rel_embedding.map_rel_iff.1 h⟩, subtype.eq $ rel_embedding.trans_apply _ _ _⟩)⟩ @[simp] theorem typein_lt_typein (r : α → α → Prop) [is_well_order α r] {a b : α} : typein r a < typein r b ↔ r a b := ⟨λ ⟨f⟩, begin have : f.top.1 = a, { let f' := principal_seg.of_element r a, let g' := f.trans (principal_seg.of_element r b), have : g'.top = f'.top, {rw subsingleton.elim f' g'}, exact this }, rw ← this, exact f.top.2 end, λ h, ⟨principal_seg.cod_restrict _ (principal_seg.of_element r a) (λ x, @trans _ r _ _ _ _ x.2 h) h⟩⟩ theorem typein_surj (r : α → α → Prop) [is_well_order α r] {o} (h : o < type r) : ∃ a, typein r a = o := induction_on o (λ β s _ ⟨f⟩, by exactI ⟨f.top, typein_top _⟩) h lemma typein_injective (r : α → α → Prop) [is_well_order α r] : injective (typein r) := injective_of_increasing r (<) (typein r) (λ x y, (typein_lt_typein r).2) @[simp] theorem typein_inj (r : α → α → Prop) [is_well_order α r] {a b} : typein r a = typein r b ↔ a = b := (typein_injective r).eq_iff /-! ### Enumerating elements in a well-order with ordinals. -/ /-- `enum r o h` is the `o`-th element of `α` ordered by `r`. That is, `enum` maps an initial segment of the ordinals, those less than the order type of `r`, to the elements of `α`. -/ def enum (r : α → α → Prop) [is_well_order α r] (o) : o < type r → α := quot.rec_on o (λ ⟨β, s, _⟩ h, (classical.choice h).top) $ λ ⟨β, s, _⟩ ⟨γ, t, _⟩ ⟨h⟩, begin resetI, refine funext (λ (H₂ : type t < type r), _), have H₁ : type s < type r, {rwa type_eq.2 ⟨h⟩}, have : ∀ {o e} (H : o < type r), @@eq.rec (λ (o : ordinal), o < type r → α) (λ (h : type s < type r), (classical.choice h).top) e H = (classical.choice H₁).top, {intros, subst e}, exact (this H₂).trans (principal_seg.top_eq h (classical.choice H₁) (classical.choice H₂)) end theorem enum_type {α β} {r : α → α → Prop} {s : β → β → Prop} [is_well_order α r] [is_well_order β s] (f : s ≺i r) {h : type s < type r} : enum r (type s) h = f.top := principal_seg.top_eq (rel_iso.refl _) _ _ @[simp] theorem enum_typein (r : α → α → Prop) [is_well_order α r] (a : α) : enum r (typein r a) (typein_lt_type r a) = a := enum_type (principal_seg.of_element r a) @[simp] theorem typein_enum (r : α → α → Prop) [is_well_order α r] {o} (h : o < type r) : typein r (enum r o h) = o := let ⟨a, e⟩ := typein_surj r h in by clear _let_match; subst e; rw enum_typein theorem enum_lt_enum {r : α → α → Prop} [is_well_order α r] {o₁ o₂ : ordinal} (h₁ : o₁ < type r) (h₂ : o₂ < type r) : r (enum r o₁ h₁) (enum r o₂ h₂) ↔ o₁ < o₂ := by rw [← typein_lt_typein r, typein_enum, typein_enum] lemma rel_iso_enum' {α β : Type u} {r : α → α → Prop} {s : β → β → Prop} [is_well_order α r] [is_well_order β s] (f : r ≃r s) (o : ordinal) : ∀(hr : o < type r) (hs : o < type s), f (enum r o hr) = enum s o hs := begin refine induction_on o _, rintros γ t wo ⟨g⟩ ⟨h⟩, resetI, rw [enum_type g, enum_type (principal_seg.lt_equiv g f)], refl end lemma rel_iso_enum {α β : Type u} {r : α → α → Prop} {s : β → β → Prop} [is_well_order α r] [is_well_order β s] (f : r ≃r s) (o : ordinal) (hr : o < type r) : f (enum r o hr) = enum s o (by {convert hr using 1, apply quotient.sound, exact ⟨f.symm⟩ }) := rel_iso_enum' _ _ _ _ theorem lt_wf : @well_founded ordinal (<) := ⟨λ a, induction_on a $ λ α r wo, by exactI suffices ∀ a, acc (<) (typein r a), from ⟨_, λ o h, let ⟨a, e⟩ := typein_surj r h in e ▸ this a⟩, λ a, acc.rec_on (wo.wf.apply a) $ λ x H IH, ⟨_, λ o h, begin rcases typein_surj r (lt_trans h (typein_lt_type r _)) with ⟨b, rfl⟩, exact IH _ ((typein_lt_typein r).1 h) end⟩⟩ instance : has_well_founded ordinal := ⟨(<), lt_wf⟩ /-- Reformulation of well founded induction on ordinals as a lemma that works with the `induction` tactic, as in `induction i using ordinal.induction with i IH`. -/ lemma induction {p : ordinal.{u} → Prop} (i : ordinal.{u}) (h : ∀ j, (∀ k, k < j → p k) → p j) : p i := lt_wf.induction i h /-- Principal segment version of the `typein` function, embedding a well order into ordinals as a principal segment. -/ def typein.principal_seg {α : Type u} (r : α → α → Prop) [is_well_order α r] : @principal_seg α ordinal.{u} r (<) := ⟨rel_embedding.of_monotone (typein r) (λ a b, (typein_lt_typein r).2), type r, λ b, ⟨λ h, ⟨enum r _ h, typein_enum r h⟩, λ ⟨a, e⟩, e ▸ typein_lt_type _ _⟩⟩ @[simp] theorem typein.principal_seg_coe (r : α → α → Prop) [is_well_order α r] : (typein.principal_seg r : α → ordinal) = typein r := rfl /-! ### Cardinality of ordinals -/ /-- The cardinal of an ordinal is the cardinality of any type on which a relation with that order type is defined. -/ def card : ordinal → cardinal := quotient.map Well_order.α $ λ ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨e⟩, ⟨e.to_equiv⟩ @[simp] theorem card_type (r : α → α → Prop) [is_well_order α r] : card (type r) = #α := rfl @[simp] lemma card_typein {r : α → α → Prop} [wo : is_well_order α r] (x : α) : #{y // r y x} = (typein r x).card := rfl theorem card_le_card {o₁ o₂ : ordinal} : o₁ ≤ o₂ → card o₁ ≤ card o₂ := induction_on o₁ $ λ α r _, induction_on o₂ $ λ β s _ ⟨⟨⟨f, _⟩, _⟩⟩, ⟨f⟩ instance : has_zero ordinal := ⟨type $ @empty_relation pempty⟩ instance : inhabited ordinal := ⟨0⟩ theorem type_eq_zero_of_empty (r) [is_well_order α r] [is_empty α] : type r = 0 := (rel_iso.rel_iso_of_is_empty r _).ordinal_type_eq @[simp] theorem type_eq_zero_iff_is_empty [is_well_order α r] : type r = 0 ↔ is_empty α := ⟨λ h, let ⟨s⟩ := type_eq.1 h in s.to_equiv.is_empty, @type_eq_zero_of_empty α r _⟩ theorem type_ne_zero_iff_nonempty [is_well_order α r] : type r ≠ 0 ↔ nonempty α := by simp theorem type_ne_zero_of_nonempty (r) [is_well_order α r] [h : nonempty α] : type r ≠ 0 := type_ne_zero_iff_nonempty.2 h theorem type_pempty : type (@empty_relation pempty) = 0 := rfl theorem type_empty : type (@empty_relation empty) = 0 := type_eq_zero_of_empty _ @[simp] theorem card_zero : card 0 = 0 := rfl protected theorem zero_le (o : ordinal) : 0 ≤ o := induction_on o $ λ α r _, ⟨⟨⟨embedding.of_is_empty, is_empty_elim⟩, is_empty_elim⟩⟩ instance : order_bot ordinal := ⟨0, ordinal.zero_le⟩ @[simp] protected theorem le_zero {o : ordinal} : o ≤ 0 ↔ o = 0 := le_bot_iff protected theorem pos_iff_ne_zero {o : ordinal} : 0 < o ↔ o ≠ 0 := bot_lt_iff_ne_bot @[simp] theorem out_empty_iff_eq_zero {o : ordinal} : is_empty o.out.α ↔ o = 0 := by rw [←@type_eq_zero_iff_is_empty o.out.α (<), type_lt] lemma eq_zero_of_out_empty (o : ordinal) [h : is_empty o.out.α] : o = 0 := out_empty_iff_eq_zero.1 h instance is_empty_out_zero : is_empty (0 : ordinal).out.α := out_empty_iff_eq_zero.2 rfl @[simp] theorem out_nonempty_iff_ne_zero {o : ordinal} : nonempty o.out.α ↔ o ≠ 0 := by rw [←@type_ne_zero_iff_nonempty o.out.α (<), type_lt] lemma ne_zero_of_out_nonempty (o : ordinal) [h : nonempty o.out.α] : o ≠ 0 := out_nonempty_iff_ne_zero.1 h instance : has_one ordinal := ⟨type $ @empty_relation punit⟩ theorem type_eq_one_of_unique (r) [is_well_order α r] [unique α] : type r = 1 := (rel_iso.rel_iso_of_unique_of_irrefl r _).ordinal_type_eq @[simp] theorem type_eq_one_iff_unique [is_well_order α r] : type r = 1 ↔ nonempty (unique α) := ⟨λ h, let ⟨s⟩ := type_eq.1 h in ⟨s.to_equiv.unique⟩, λ ⟨h⟩, @type_eq_one_of_unique α r _ h⟩ theorem type_punit : type (@empty_relation punit) = 1 := rfl theorem type_unit : type (@empty_relation unit) = 1 := rfl @[simp] theorem card_one : card 1 = 1 := rfl /-! ### Lifting ordinals to a higher universe -/ /-- The universe lift operation for ordinals, which embeds `ordinal.{u}` as a proper initial segment of `ordinal.{v}` for `v > u`. For the initial segment version, see `lift.initial_seg`. -/ def lift (o : ordinal.{v}) : ordinal.{max v u} := quotient.lift_on o (λ w, type $ ulift.down ⁻¹'o w.r) $ λ ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨f⟩, quot.sound ⟨(rel_iso.preimage equiv.ulift r).trans $ f.trans (rel_iso.preimage equiv.ulift s).symm⟩ @[simp] theorem type_ulift (r : α → α → Prop) [is_well_order α r] : type (ulift.down ⁻¹'o r) = lift.{v} (type r) := rfl theorem _root_.rel_iso.ordinal_lift_type_eq {α : Type u} {β : Type v} {r : α → α → Prop} {s : β → β → Prop} [is_well_order α r] [is_well_order β s] (f : r ≃r s) : lift.{v} (type r) = lift.{u} (type s) := ((rel_iso.preimage equiv.ulift r).trans $ f.trans (rel_iso.preimage equiv.ulift s).symm).ordinal_type_eq @[simp] theorem type_lift_preimage {α : Type u} {β : Type v} (r : α → α → Prop) [is_well_order α r] (f : β ≃ α) : lift.{u} (type (f ⁻¹'o r)) = lift.{v} (type r) := (rel_iso.preimage f r).ordinal_lift_type_eq /-- `lift.{(max u v) u}` equals `lift.{v u}`. Using `set_option pp.universes true` will make it much easier to understand what's happening when using this lemma. -/ @[simp] theorem lift_umax : lift.{(max u v) u} = lift.{v u} := funext $ λ a, induction_on a $ λ α r _, quotient.sound ⟨(rel_iso.preimage equiv.ulift r).trans (rel_iso.preimage equiv.ulift r).symm⟩ /-- `lift.{(max v u) u}` equals `lift.{v u}`. Using `set_option pp.universes true` will make it much easier to understand what's happening when using this lemma. -/ @[simp] theorem lift_umax' : lift.{(max v u) u} = lift.{v u} := lift_umax /-- An ordinal lifted to a lower or equal universe equals itself. -/ @[simp] theorem lift_id' (a : ordinal) : lift a = a := induction_on a $ λ α r _, quotient.sound ⟨rel_iso.preimage equiv.ulift r⟩ /-- An ordinal lifted to the same universe equals itself. -/ @[simp] theorem lift_id : ∀ a, lift.{u u} a = a := lift_id'.{u u} /-- An ordinal lifted to the zero universe equals itself. -/ @[simp] theorem lift_uzero (a : ordinal.{u}) : lift.{0} a = a := lift_id'.{0 u} a @[simp] theorem lift_lift (a : ordinal) : lift.{w} (lift.{v} a) = lift.{max v w} a := induction_on a $ λ α r _, quotient.sound ⟨(rel_iso.preimage equiv.ulift _).trans $ (rel_iso.preimage equiv.ulift _).trans (rel_iso.preimage equiv.ulift _).symm⟩ theorem lift_type_le {α : Type u} {β : Type v} {r s} [is_well_order α r] [is_well_order β s] : lift.{max v w} (type r) ≤ lift.{max u w} (type s) ↔ nonempty (r ≼i s) := ⟨λ ⟨f⟩, ⟨(initial_seg.of_iso (rel_iso.preimage equiv.ulift r).symm).trans $ f.trans (initial_seg.of_iso (rel_iso.preimage equiv.ulift s))⟩, λ ⟨f⟩, ⟨(initial_seg.of_iso (rel_iso.preimage equiv.ulift r)).trans $ f.trans (initial_seg.of_iso (rel_iso.preimage equiv.ulift s).symm)⟩⟩ theorem lift_type_eq {α : Type u} {β : Type v} {r s} [is_well_order α r] [is_well_order β s] : lift.{max v w} (type r) = lift.{max u w} (type s) ↔ nonempty (r ≃r s) := quotient.eq.trans ⟨λ ⟨f⟩, ⟨(rel_iso.preimage equiv.ulift r).symm.trans $ f.trans (rel_iso.preimage equiv.ulift s)⟩, λ ⟨f⟩, ⟨(rel_iso.preimage equiv.ulift r).trans $ f.trans (rel_iso.preimage equiv.ulift s).symm⟩⟩ theorem lift_type_lt {α : Type u} {β : Type v} {r s} [is_well_order α r] [is_well_order β s] : lift.{max v w} (type r) < lift.{max u w} (type s) ↔ nonempty (r ≺i s) := by haveI := @rel_embedding.is_well_order _ _ (@equiv.ulift.{max v w} α ⁻¹'o r) r (rel_iso.preimage equiv.ulift.{max v w} r) _; haveI := @rel_embedding.is_well_order _ _ (@equiv.ulift.{max u w} β ⁻¹'o s) s (rel_iso.preimage equiv.ulift.{max u w} s) _; exact ⟨λ ⟨f⟩, ⟨(f.equiv_lt (rel_iso.preimage equiv.ulift r).symm).lt_le (initial_seg.of_iso (rel_iso.preimage equiv.ulift s))⟩, λ ⟨f⟩, ⟨(f.equiv_lt (rel_iso.preimage equiv.ulift r)).lt_le (initial_seg.of_iso (rel_iso.preimage equiv.ulift s).symm)⟩⟩ @[simp] theorem lift_le {a b : ordinal} : lift.{u v} a ≤ lift b ↔ a ≤ b := induction_on a $ λ α r _, induction_on b $ λ β s _, by { rw ← lift_umax, exactI lift_type_le } @[simp] theorem lift_inj {a b : ordinal} : lift a = lift b ↔ a = b := by simp only [le_antisymm_iff, lift_le] @[simp] theorem lift_lt {a b : ordinal} : lift a < lift b ↔ a < b := by simp only [lt_iff_le_not_le, lift_le] @[simp] theorem lift_zero : lift 0 = 0 := type_eq_zero_of_empty _ @[simp] theorem lift_one : lift 1 = 1 := type_eq_one_of_unique _ @[simp] theorem lift_card (a) : (card a).lift = card (lift a) := induction_on a $ λ α r _, rfl theorem lift_down' {a : cardinal.{u}} {b : ordinal.{max u v}} (h : card b ≤ a.lift) : ∃ a', lift a' = b := let ⟨c, e⟩ := cardinal.lift_down h in cardinal.induction_on c (λ α, induction_on b $ λ β s _ e', begin resetI, rw [card_type, ← cardinal.lift_id'.{(max u v) u} (#β), ← cardinal.lift_umax.{u v}, lift_mk_eq.{u (max u v) (max u v)}] at e', cases e' with f, have g := rel_iso.preimage f s, haveI := (g : ⇑f ⁻¹'o s ↪r s).is_well_order, have := lift_type_eq.{u (max u v) (max u v)}.2 ⟨g⟩, rw [lift_id, lift_umax.{u v}] at this, exact ⟨_, this⟩ end) e theorem lift_down {a : ordinal.{u}} {b : ordinal.{max u v}} (h : b ≤ lift a) : ∃ a', lift a' = b := @lift_down' (card a) _ (by rw lift_card; exact card_le_card h) theorem le_lift_iff {a : ordinal.{u}} {b : ordinal.{max u v}} : b ≤ lift a ↔ ∃ a', lift a' = b ∧ a' ≤ a := ⟨λ h, let ⟨a', e⟩ := lift_down h in ⟨a', e, lift_le.1 $ e.symm ▸ h⟩, λ ⟨a', e, h⟩, e ▸ lift_le.2 h⟩ theorem lt_lift_iff {a : ordinal.{u}} {b : ordinal.{max u v}} : b < lift a ↔ ∃ a', lift a' = b ∧ a' < a := ⟨λ h, let ⟨a', e⟩ := lift_down (le_of_lt h) in ⟨a', e, lift_lt.1 $ e.symm ▸ h⟩, λ ⟨a', e, h⟩, e ▸ lift_lt.2 h⟩ /-- Initial segment version of the lift operation on ordinals, embedding `ordinal.{u}` in `ordinal.{v}` as an initial segment when `u ≤ v`. -/ def lift.initial_seg : @initial_seg ordinal.{u} ordinal.{max u v} (<) (<) := ⟨⟨⟨lift.{v}, λ a b, lift_inj.1⟩, λ a b, lift_lt⟩, λ a b h, lift_down (le_of_lt h)⟩ @[simp] theorem lift.initial_seg_coe : (lift.initial_seg : ordinal → ordinal) = lift := rfl /-! ### The first infinite ordinal `omega` -/ /-- `ω` is the first infinite ordinal, defined as the order type of `ℕ`. -/ def omega : ordinal.{u} := lift $ @type ℕ (<) _ localized "notation `ω` := ordinal.omega" in ordinal /-- Note that the presence of this lemma makes `simp [omega]` form a loop. -/ @[simp] theorem type_nat_lt : @type ℕ (<) _ = ω := (lift_id _).symm @[simp] theorem card_omega : card ω = ℵ₀ := rfl @[simp] theorem lift_omega : lift ω = ω := lift_lift _ /-! ### Definition and first properties of addition on ordinals In this paragraph, we introduce the addition on ordinals, and prove just enough properties to deduce that the order on ordinals is total (and therefore well-founded). Further properties of the addition, together with properties of the other operations, are proved in `ordinal_arithmetic.lean`. -/ /-- `o₁ + o₂` is the order on the disjoint union of `o₁` and `o₂` obtained by declaring that every element of `o₁` is smaller than every element of `o₂`. -/ instance : has_add ordinal.{u} := ⟨λ o₁ o₂, quotient.lift_on₂ o₁ o₂ (λ ⟨α, r, wo⟩ ⟨β, s, wo'⟩, by exactI type (sum.lex r s)) $ λ ⟨α₁, r₁, o₁⟩ ⟨α₂, r₂, o₂⟩ ⟨β₁, s₁, p₁⟩ ⟨β₂, s₂, p₂⟩ ⟨f⟩ ⟨g⟩, quot.sound ⟨rel_iso.sum_lex_congr f g⟩⟩ instance : add_monoid_with_one ordinal.{u} := { add := (+), zero := 0, one := 1, zero_add := λ o, induction_on o $ λ α r _, eq.symm $ quotient.sound ⟨⟨(empty_sum pempty α).symm, λ a b, sum.lex_inr_inr⟩⟩, add_zero := λ o, induction_on o $ λ α r _, eq.symm $ quotient.sound ⟨⟨(sum_empty α pempty).symm, λ a b, sum.lex_inl_inl⟩⟩, add_assoc := λ o₁ o₂ o₃, quotient.induction_on₃ o₁ o₂ o₃ $ λ ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨γ, t, _⟩, quot.sound ⟨⟨sum_assoc _ _ _, λ a b, begin rcases a with ⟨a|a⟩|a; rcases b with ⟨b|b⟩|b; simp only [sum_assoc_apply_inl_inl, sum_assoc_apply_inl_inr, sum_assoc_apply_inr, sum.lex_inl_inl, sum.lex_inr_inr, sum.lex.sep, sum.lex_inr_inl] end⟩⟩ } @[simp] theorem card_add (o₁ o₂ : ordinal) : card (o₁ + o₂) = card o₁ + card o₂ := induction_on o₁ $ λ α r _, induction_on o₂ $ λ β s _, rfl @[simp] theorem type_sum_lex {α β : Type u} (r : α → α → Prop) (s : β → β → Prop) [is_well_order α r] [is_well_order β s] : type (sum.lex r s) = type r + type s := rfl @[simp] theorem card_nat (n : ℕ) : card.{u} n = n := by induction n; [refl, simp only [card_add, card_one, nat.cast_succ, *]] instance add_covariant_class_le : covariant_class ordinal.{u} ordinal.{u} (+) (≤) := ⟨λ c a b h, begin revert h c, exact ( induction_on a $ λ α₁ r₁ _, induction_on b $ λ α₂ r₂ _ ⟨⟨⟨f, fo⟩, fi⟩⟩ c, induction_on c $ λ β s _, ⟨⟨⟨(embedding.refl _).sum_map f, λ a b, match a, b with | sum.inl a, sum.inl b := sum.lex_inl_inl.trans sum.lex_inl_inl.symm | sum.inl a, sum.inr b := by apply iff_of_true; apply sum.lex.sep | sum.inr a, sum.inl b := by apply iff_of_false; exact sum.lex_inr_inl | sum.inr a, sum.inr b := sum.lex_inr_inr.trans $ fo.trans sum.lex_inr_inr.symm end⟩, λ a b H, match a, b, H with | _, sum.inl b, _ := ⟨sum.inl b, rfl⟩ | sum.inl a, sum.inr b, H := (sum.lex_inr_inl H).elim | sum.inr a, sum.inr b, H := let ⟨w, h⟩ := fi _ _ (sum.lex_inr_inr.1 H) in ⟨sum.inr w, congr_arg sum.inr h⟩ end⟩⟩) end⟩ instance add_swap_covariant_class_le : covariant_class ordinal.{u} ordinal.{u} (swap (+)) (≤) := ⟨λ c a b h, begin revert h c, exact ( induction_on a $ λ α₁ r₁ hr₁, induction_on b $ λ α₂ r₂ hr₂ ⟨⟨⟨f, fo⟩, fi⟩⟩ c, induction_on c $ λ β s hs, by exactI @rel_embedding.ordinal_type_le _ _ (sum.lex r₁ s) (sum.lex r₂ s) _ _ ⟨f.sum_map (embedding.refl _), λ a b, begin split; intro H, { cases a with a a; cases b with b b; cases H; constructor; [rwa ← fo, assumption] }, { cases H; constructor; [rwa fo, assumption] } end⟩) end⟩ theorem le_add_right (a b : ordinal) : a ≤ a + b := by simpa only [add_zero] using add_le_add_left (ordinal.zero_le b) a theorem le_add_left (a b : ordinal) : a ≤ b + a := by simpa only [zero_add] using add_le_add_right (ordinal.zero_le b) a instance : linear_order ordinal := { le_total := λ a b, match lt_or_eq_of_le (le_add_left b a), lt_or_eq_of_le (le_add_right a b) with | or.inr h, _ := by rw h; exact or.inl (le_add_right _ _) | _, or.inr h := by rw h; exact or.inr (le_add_left _ _) | or.inl h₁, or.inl h₂ := induction_on a (λ α₁ r₁ _, induction_on b $ λ α₂ r₂ _ ⟨f⟩ ⟨g⟩, begin resetI, rw [← typein_top f, ← typein_top g, le_iff_lt_or_eq, le_iff_lt_or_eq, typein_lt_typein, typein_lt_typein], rcases trichotomous_of (sum.lex r₁ r₂) g.top f.top with h|h|h; [exact or.inl (or.inl h), {left, right, rw h}, exact or.inr (or.inl h)] end) h₁ h₂ end, decidable_le := classical.dec_rel _, ..ordinal.partial_order } instance : well_founded_lt ordinal := ⟨lt_wf⟩ instance : is_well_order ordinal (<) := { } instance : conditionally_complete_linear_order_bot ordinal := is_well_order.conditionally_complete_linear_order_bot _ @[simp] lemma bot_eq_zero : (⊥ : ordinal) = 0 := rfl @[simp] lemma max_zero_left : ∀ a : ordinal, max 0 a = a := max_bot_left @[simp] lemma max_zero_right : ∀ a : ordinal, max a 0 = a := max_bot_right @[simp] lemma max_eq_zero {a b : ordinal} : max a b = 0 ↔ a = 0 ∧ b = 0 := max_eq_bot protected theorem not_lt_zero (o : ordinal) : ¬ o < 0 := not_lt_bot theorem eq_zero_or_pos : ∀ a : ordinal, a = 0 ∨ 0 < a := eq_bot_or_bot_lt @[simp] theorem Inf_empty : Inf (∅ : set ordinal) = 0 := dif_neg not_nonempty_empty private theorem succ_le_iff' {a b : ordinal} : a + 1 ≤ b ↔ a < b := ⟨lt_of_lt_of_le (induction_on a $ λ α r _, ⟨⟨⟨⟨λ x, sum.inl x, λ _ _, sum.inl.inj⟩, λ _ _, sum.lex_inl_inl⟩, sum.inr punit.star, λ b, sum.rec_on b (λ x, ⟨λ _, ⟨x, rfl⟩, λ _, sum.lex.sep _ _⟩) (λ x, sum.lex_inr_inr.trans ⟨false.elim, λ ⟨x, H⟩, sum.inl_ne_inr H⟩)⟩⟩), induction_on a $ λ α r hr, induction_on b $ λ β s hs ⟨⟨f, t, hf⟩⟩, begin haveI := hs, refine ⟨⟨@rel_embedding.of_monotone (α ⊕ punit) β _ _ _ _ (sum.rec _ _) (λ a b, _), λ a b, _⟩⟩, { exact f }, { exact λ _, t }, { rcases a with a|_; rcases b with b|_, { simpa only [sum.lex_inl_inl] using f.map_rel_iff.2 }, { intro _, rw hf, exact ⟨_, rfl⟩ }, { exact false.elim ∘ sum.lex_inr_inl }, { exact false.elim ∘ sum.lex_inr_inr.1 } }, { rcases a with a|_, { intro h, have := @principal_seg.init _ _ _ _ _ ⟨f, t, hf⟩ _ _ h, cases this with w h, exact ⟨sum.inl w, h⟩ }, { intro h, cases (hf b).1 h with w h, exact ⟨sum.inl w, h⟩ } } end⟩ instance : no_max_order ordinal := ⟨λ a, ⟨_, succ_le_iff'.1 le_rfl⟩⟩ instance : succ_order ordinal.{u} := succ_order.of_succ_le_iff (λ o, o + 1) (λ a b, succ_le_iff') @[simp] theorem add_one_eq_succ (o : ordinal) : o + 1 = succ o := rfl @[simp] lemma typein_le_typein (r : α → α → Prop) [is_well_order α r] {x x' : α} : typein r x ≤ typein r x' ↔ ¬r x' x := by rw [←not_lt, typein_lt_typein] @[simp] lemma typein_le_typein' (o : ordinal) {x x' : o.out.α} : typein (<) x ≤ typein (<) x' ↔ x ≤ x' := by { rw typein_le_typein, exact not_lt } @[simp] lemma enum_le_enum (r : α → α → Prop) [is_well_order α r] {o o' : ordinal} (ho : o < type r) (ho' : o' < type r) : ¬r (enum r o' ho') (enum r o ho) ↔ o ≤ o' := by rw [←@not_lt _ _ o' o, enum_lt_enum ho'] @[simp] lemma enum_le_enum' (a : ordinal) {o o' : ordinal} (ho : o < type (<)) (ho' : o' < type (<)) : enum (<) o ho ≤ @enum a.out.α (<) _ o' ho' ↔ o ≤ o' := by rw [←enum_le_enum (<), ←not_lt] theorem enum_zero_le {r : α → α → Prop} [is_well_order α r] (h0 : 0 < type r) (a : α) : ¬ r a (enum r 0 h0) := by { rw [←enum_typein r a, enum_le_enum r], apply ordinal.zero_le } theorem enum_zero_le' {o : ordinal} (h0 : 0 < o) (a : o.out.α) : @enum o.out.α (<) _ 0 (by rwa type_lt) ≤ a := by { rw ←not_lt, apply enum_zero_le } theorem le_enum_succ {o : ordinal} (a : (succ o).out.α) : a ≤ @enum (succ o).out.α (<) _ o (by { rw type_lt, exact lt_succ o }) := by { rw [←enum_typein (<) a, enum_le_enum', ←lt_succ_iff], apply typein_lt_self } @[simp] theorem enum_inj {r : α → α → Prop} [is_well_order α r] {o₁ o₂ : ordinal} (h₁ : o₁ < type r) (h₂ : o₂ < type r) : enum r o₁ h₁ = enum r o₂ h₂ ↔ o₁ = o₂ := ⟨λ h, begin by_contra hne, cases lt_or_gt_of_ne hne with hlt hlt; apply (is_well_order.is_irrefl r).1, { rwa [←@enum_lt_enum α r _ o₁ o₂ h₁ h₂, h] at hlt }, { change _ < _ at hlt, rwa [←@enum_lt_enum α r _ o₂ o₁ h₂ h₁, h] at hlt } end, λ h, by simp_rw h⟩ /-- A well order `r` is order isomorphic to the set of ordinals smaller than `type r`. -/ @[simps] def enum_iso (r : α → α → Prop) [is_well_order α r] : subrel (<) (< type r) ≃r r := { to_fun := λ x, enum r x.1 x.2, inv_fun := λ x, ⟨typein r x, typein_lt_type r x⟩, left_inv := λ ⟨o, h⟩, subtype.ext_val (typein_enum _ _), right_inv := λ h, enum_typein _ _, map_rel_iff' := by { rintros ⟨a, _⟩ ⟨b, _⟩, apply enum_lt_enum } } /-- The order isomorphism between ordinals less than `o` and `o.out.α`. -/ @[simps] noncomputable def enum_iso_out (o : ordinal) : set.Iio o ≃o o.out.α := { to_fun := λ x, enum (<) x.1 $ by { rw type_lt, exact x.2 }, inv_fun := λ x, ⟨typein (<) x, typein_lt_self x⟩, left_inv := λ ⟨o', h⟩, subtype.ext_val (typein_enum _ _), right_inv := λ h, enum_typein _ _, map_rel_iff' := by { rintros ⟨a, _⟩ ⟨b, _⟩, apply enum_le_enum' } } /-- `o.out.α` is an `order_bot` whenever `0 < o`. -/ def out_order_bot_of_pos {o : ordinal} (ho : 0 < o) : order_bot o.out.α := ⟨_, enum_zero_le' ho⟩ theorem enum_zero_eq_bot {o : ordinal} (ho : 0 < o) : enum (<) 0 (by rwa type_lt) = by { haveI H := out_order_bot_of_pos ho, exact ⊥ } := rfl /-- `univ.{u v}` is the order type of the ordinals of `Type u` as a member of `ordinal.{v}` (when `u < v`). It is an inaccessible cardinal. -/ @[nolint check_univs] -- intended to be used with explicit universe parameters def univ : ordinal.{max (u + 1) v} := lift.{v (u+1)} (@type ordinal (<) _) theorem univ_id : univ.{u (u+1)} = @type ordinal (<) _ := lift_id _ @[simp] theorem lift_univ : lift.{w} univ.{u v} = univ.{u (max v w)} := lift_lift _ theorem univ_umax : univ.{u (max (u+1) v)} = univ.{u v} := congr_fun lift_umax _ /-- Principal segment version of the lift operation on ordinals, embedding `ordinal.{u}` in `ordinal.{v}` as a principal segment when `u < v`. -/ def lift.principal_seg : @principal_seg ordinal.{u} ordinal.{max (u+1) v} (<) (<) := ⟨↑lift.initial_seg.{u (max (u+1) v)}, univ.{u v}, begin refine λ b, induction_on b _, introsI β s _, rw [univ, ← lift_umax], split; intro h, { rw ← lift_id (type s) at h ⊢, cases lift_type_lt.1 h with f, cases f with f a hf, existsi a, revert hf, apply induction_on a, introsI α r _ hf, refine lift_type_eq.{u (max (u+1) v) (max (u+1) v)}.2 ⟨(rel_iso.of_surjective (rel_embedding.of_monotone _ _) _).symm⟩, { exact λ b, enum r (f b) ((hf _).2 ⟨_, rfl⟩) }, { refine λ a b h, (typein_lt_typein r).1 _, rw [typein_enum, typein_enum], exact f.map_rel_iff.2 h }, { intro a', cases (hf _).1 (typein_lt_type _ a') with b e, existsi b, simp, simp [e] } }, { cases h with a e, rw [← e], apply induction_on a, introsI α r _, exact lift_type_lt.{u (u+1) (max (u+1) v)}.2 ⟨typein.principal_seg r⟩ } end⟩ @[simp] theorem lift.principal_seg_coe : (lift.principal_seg.{u v} : ordinal → ordinal) = lift.{max (u+1) v} := rfl @[simp] theorem lift.principal_seg_top : lift.principal_seg.top = univ := rfl theorem lift.principal_seg_top' : lift.principal_seg.{u (u+1)}.top = @type ordinal (<) _ := by simp only [lift.principal_seg_top, univ_id] end ordinal /-! ### Representing a cardinal with an ordinal -/ namespace cardinal open ordinal @[simp] theorem mk_ordinal_out (o : ordinal) : #(o.out.α) = o.card := (ordinal.card_type _).symm.trans $ by rw ordinal.type_lt /-- The ordinal corresponding to a cardinal `c` is the least ordinal whose cardinal is `c`. For the order-embedding version, see `ord.order_embedding`. -/ def ord (c : cardinal) : ordinal := let F := λ α : Type u, ⨅ r : {r // is_well_order α r}, @type α r.1 r.2 in quot.lift_on c F begin suffices : ∀ {α β}, α ≈ β → F α ≤ F β, from λ α β h, (this h).antisymm (this (setoid.symm h)), rintros α β ⟨f⟩, refine le_cinfi_iff'.2 (λ i, _), haveI := @rel_embedding.is_well_order _ _ (f ⁻¹'o i.1) _ ↑(rel_iso.preimage f i.1) i.2, exact (cinfi_le' _ (subtype.mk (⇑f ⁻¹'o i.val) (@rel_embedding.is_well_order _ _ _ _ ↑(rel_iso.preimage f i.1) i.2))).trans_eq (quot.sound ⟨rel_iso.preimage f i.1⟩) end lemma ord_eq_Inf (α : Type u) : ord (#α) = ⨅ r : {r // is_well_order α r}, @type α r.1 r.2 := rfl theorem ord_eq (α) : ∃ (r : α → α → Prop) [wo : is_well_order α r], ord (#α) = @type α r wo := let ⟨r, wo⟩ := infi_mem (λ r : {r // is_well_order α r}, @type α r.1 r.2) in ⟨r.1, r.2, wo.symm⟩ theorem ord_le_type (r : α → α → Prop) [h : is_well_order α r] : ord (#α) ≤ type r := cinfi_le' _ (subtype.mk r h) theorem ord_le {c o} : ord c ≤ o ↔ c ≤ o.card := induction_on c $ λ α, ordinal.induction_on o $ λ β s _, let ⟨r, _, e⟩ := ord_eq α in begin resetI, simp only [card_type], split; intro h, { rw e at h, exact let ⟨f⟩ := h in ⟨f.to_embedding⟩ }, { cases h with f, have g := rel_embedding.preimage f s, haveI := rel_embedding.is_well_order g, exact le_trans (ord_le_type _) g.ordinal_type_le } end theorem gc_ord_card : galois_connection ord card := λ _ _, ord_le theorem lt_ord {c o} : o < ord c ↔ o.card < c := gc_ord_card.lt_iff_lt @[simp] theorem card_ord (c) : (ord c).card = c := quotient.induction_on c $ λ α, let ⟨r, _, e⟩ := ord_eq α in by simp only [mk_def, e, card_type] /-- Galois coinsertion between `cardinal.ord` and `ordinal.card`. -/ def gci_ord_card : galois_coinsertion ord card := gc_ord_card.to_galois_coinsertion $ λ c, c.card_ord.le theorem ord_card_le (o : ordinal) : o.card.ord ≤ o := gc_ord_card.l_u_le _ lemma lt_ord_succ_card (o : ordinal) : o < (succ o.card).ord := lt_ord.2 $ lt_succ _ @[mono] theorem ord_strict_mono : strict_mono ord := gci_ord_card.strict_mono_l @[mono] theorem ord_mono : monotone ord := gc_ord_card.monotone_l @[simp] theorem ord_le_ord {c₁ c₂} : ord c₁ ≤ ord c₂ ↔ c₁ ≤ c₂ := gci_ord_card.l_le_l_iff @[simp] theorem ord_lt_ord {c₁ c₂} : ord c₁ < ord c₂ ↔ c₁ < c₂ := ord_strict_mono.lt_iff_lt @[simp] theorem ord_zero : ord 0 = 0 := gc_ord_card.l_bot @[simp] theorem ord_nat (n : ℕ) : ord n = n := (ord_le.2 (card_nat n).ge).antisymm begin induction n with n IH, { apply ordinal.zero_le }, { exact succ_le_of_lt (IH.trans_lt $ ord_lt_ord.2 $ nat_cast_lt.2 (nat.lt_succ_self n)) } end @[simp] theorem ord_one : ord 1 = 1 := by simpa using ord_nat 1 @[simp] theorem lift_ord (c) : (ord c).lift = ord (lift c) := begin refine le_antisymm (le_of_forall_lt (λ a ha, _)) _, { rcases ordinal.lt_lift_iff.1 ha with ⟨a, rfl, h⟩, rwa [lt_ord, ← lift_card, lift_lt, ← lt_ord, ← ordinal.lift_lt] }, { rw [ord_le, ← lift_card, card_ord] } end lemma mk_ord_out (c : cardinal) : #c.ord.out.α = c := by simp lemma card_typein_lt (r : α → α → Prop) [is_well_order α r] (x : α) (h : ord (#α) = type r) : card (typein r x) < #α := by { rw [←lt_ord, h], apply typein_lt_type } lemma card_typein_out_lt (c : cardinal) (x : c.ord.out.α) : card (typein (<) x) < c := by { rw ←lt_ord, apply typein_lt_self } lemma ord_injective : injective ord := by { intros c c' h, rw [←card_ord c, ←card_ord c', h] } /-- The ordinal corresponding to a cardinal `c` is the least ordinal whose cardinal is `c`. This is the order-embedding version. For the regular function, see `ord`. -/ def ord.order_embedding : cardinal ↪o ordinal := rel_embedding.order_embedding_of_lt_embedding (rel_embedding.of_monotone cardinal.ord $ λ a b, cardinal.ord_lt_ord.2) @[simp] theorem ord.order_embedding_coe : (ord.order_embedding : cardinal → ordinal) = ord := rfl /-- The cardinal `univ` is the cardinality of ordinal `univ`, or equivalently the cardinal of `ordinal.{u}`, or `cardinal.{u}`, as an element of `cardinal.{v}` (when `u < v`). -/ @[nolint check_univs] -- intended to be used with explicit universe parameters def univ := lift.{v (u+1)} (#ordinal) theorem univ_id : univ.{u (u+1)} = #ordinal := lift_id _ @[simp] theorem lift_univ : lift.{w} univ.{u v} = univ.{u (max v w)} := lift_lift _ theorem univ_umax : univ.{u (max (u+1) v)} = univ.{u v} := congr_fun lift_umax _ theorem lift_lt_univ (c : cardinal) : lift.{(u+1) u} c < univ.{u (u+1)} := by simpa only [lift.principal_seg_coe, lift_ord, lift_succ, ord_le, succ_le_iff] using le_of_lt (lift.principal_seg.{u (u+1)}.lt_top (succ c).ord) theorem lift_lt_univ' (c : cardinal) : lift.{(max (u+1) v) u} c < univ.{u v} := by simpa only [lift_lift, lift_univ, univ_umax] using lift_lt.{_ (max (u+1) v)}.2 (lift_lt_univ c) @[simp] theorem ord_univ : ord univ.{u v} = ordinal.univ.{u v} := le_antisymm (ord_card_le _) $ le_of_forall_lt $ λ o h, lt_ord.2 begin rcases lift.principal_seg.{u v}.down.1 (by simpa only [lift.principal_seg_coe] using h) with ⟨o', rfl⟩, simp only [lift.principal_seg_coe], rw [← lift_card], apply lift_lt_univ' end theorem lt_univ {c} : c < univ.{u (u+1)} ↔ ∃ c', c = lift.{(u+1) u} c' := ⟨λ h, begin have := ord_lt_ord.2 h, rw ord_univ at this, cases lift.principal_seg.{u (u+1)}.down.1 (by simpa only [lift.principal_seg_top]) with o e, have := card_ord c, rw [← e, lift.principal_seg_coe, ← lift_card] at this, exact ⟨_, this.symm⟩ end, λ ⟨c', e⟩, e.symm ▸ lift_lt_univ _⟩ theorem lt_univ' {c} : c < univ.{u v} ↔ ∃ c', c = lift.{(max (u+1) v) u} c' := ⟨λ h, let ⟨a, e, h'⟩ := lt_lift_iff.1 h in begin rw [← univ_id] at h', rcases lt_univ.{u}.1 h' with ⟨c', rfl⟩, exact ⟨c', by simp only [e.symm, lift_lift]⟩ end, λ ⟨c', e⟩, e.symm ▸ lift_lt_univ' _⟩ theorem small_iff_lift_mk_lt_univ {α : Type u} : small.{v} α ↔ cardinal.lift (#α) < univ.{v (max u (v + 1))} := begin rw lt_univ', split, { rintro ⟨β, e⟩, exact ⟨#β, lift_mk_eq.{u _ (v + 1)}.2 e⟩ }, { rintro ⟨c, hc⟩, exact ⟨⟨c.out, lift_mk_eq.{u _ (v + 1)}.1 (hc.trans (congr rfl c.mk_out.symm))⟩⟩ } end end cardinal namespace ordinal @[simp] theorem card_univ : card univ = cardinal.univ := rfl @[simp] theorem nat_le_card {o} {n : ℕ} : (n : cardinal) ≤ card o ↔ (n : ordinal) ≤ o := by rw [← cardinal.ord_le, cardinal.ord_nat] @[simp] theorem nat_lt_card {o} {n : ℕ} : (n : cardinal) < card o ↔ (n : ordinal) < o := by { rw [←succ_le_iff, ←succ_le_iff, ←nat_succ, nat_le_card], refl } @[simp] theorem card_lt_nat {o} {n : ℕ} : card o < n ↔ o < n := lt_iff_lt_of_le_iff_le nat_le_card @[simp] theorem card_le_nat {o} {n : ℕ} : card o ≤ n ↔ o ≤ n := le_iff_le_iff_lt_iff_lt.2 nat_lt_card @[simp] theorem card_eq_nat {o} {n : ℕ} : card o = n ↔ o = n := by simp only [le_antisymm_iff, card_le_nat, nat_le_card] @[simp] theorem type_fintype (r : α → α → Prop) [is_well_order α r] [fintype α] : type r = fintype.card α := by rw [←card_eq_nat, card_type, mk_fintype] theorem type_fin (n : ℕ) : @type (fin n) (<) _ = n := by simp end ordinal
f3ed867b50535a01699e81c1f9f2ab5b9f356dfa
4727251e0cd73359b15b664c3170e5d754078599
/src/data/polynomial/cancel_leads.lean
377db80acaa7913bf44069629190b6151c8da181
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
3,006
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 data.polynomial.degree.definitions /-! # Cancel the leading terms of two polynomials ## Definition * `cancel_leads p q`: the polynomial formed by multiplying `p` and `q` by monomials so that they have the same leading term, and then subtracting. ## Main Results The degree of `cancel_leads` is less than that of the larger of the two polynomials being cancelled. Thus it is useful for induction or minimal-degree arguments. -/ namespace polynomial noncomputable theory open_locale polynomial variables {R : Type*} section comm_ring variables [ring R] (p q : R[X]) /-- `cancel_leads p q` is formed by multiplying `p` and `q` by monomials so that they have the same leading term, and then subtracting. -/ def cancel_leads : R[X] := C p.leading_coeff * X ^ (p.nat_degree - q.nat_degree) * q - C q.leading_coeff * X ^ (q.nat_degree - p.nat_degree) * p variables {p q} @[simp] lemma neg_cancel_leads : - p.cancel_leads q = q.cancel_leads p := neg_sub _ _ end comm_ring section comm_ring variables [comm_ring R] {p q : R[X]} lemma dvd_cancel_leads_of_dvd_of_dvd {r : R[X]} (pq : p ∣ q) (pr : p ∣ r) : p ∣ q.cancel_leads r := dvd_sub (pr.trans (dvd.intro_left _ rfl)) (pq.trans (dvd.intro_left _ rfl)) end comm_ring lemma nat_degree_cancel_leads_lt_of_nat_degree_le_nat_degree [comm_ring R] [is_domain R] {p q : R[X]} (h : p.nat_degree ≤ q.nat_degree) (hq : 0 < q.nat_degree) : (p.cancel_leads q).nat_degree < q.nat_degree := begin by_cases hp : p = 0, { convert hq, simp [hp, cancel_leads], }, rw [cancel_leads, sub_eq_add_neg, tsub_eq_zero_iff_le.mpr h, pow_zero, mul_one], by_cases h0 : C p.leading_coeff * q + -(C q.leading_coeff * X ^ (q.nat_degree - p.nat_degree) * p) = 0, { convert hq, simp only [h0, nat_degree_zero], }, have hq0 : ¬ q = 0, { contrapose! hq, simp [hq] }, apply lt_of_le_of_ne, { rw [← with_bot.coe_le_coe, ← degree_eq_nat_degree h0, ← degree_eq_nat_degree hq0], apply le_trans (degree_add_le _ _), rw ← leading_coeff_eq_zero at hp hq0, simp only [max_le_iff, degree_C hp, degree_C hq0, le_refl q.degree, true_and, nat.cast_with_bot, nsmul_one, degree_neg, degree_mul, zero_add, degree_X, degree_pow], rw leading_coeff_eq_zero at hp hq0, rw [degree_eq_nat_degree hp, degree_eq_nat_degree hq0, ← with_bot.coe_add, with_bot.coe_le_coe, tsub_add_cancel_of_le h], }, { contrapose! h0, rw [← leading_coeff_eq_zero, leading_coeff, h0, mul_assoc, mul_comm _ p, ← tsub_add_cancel_of_le h, add_comm _ p.nat_degree], simp only [coeff_mul_X_pow, coeff_neg, coeff_C_mul, add_tsub_cancel_left, coeff_add], rw [add_comm p.nat_degree, tsub_add_cancel_of_le h, ← leading_coeff, ← leading_coeff, mul_comm _ q.leading_coeff, ← sub_eq_add_neg, ← mul_sub, sub_self, mul_zero] } end end polynomial
1b65e915187c8393f7522e5333508bf985ea405e
94637389e03c919023691dcd05bd4411b1034aa5
/src/inClassNotes/type_library/empty.lean
1823e56b0337436a13da1b7a6f06d87089b28b2b
[]
no_license
kevinsullivan/complogic-s21
7c4eef2105abad899e46502270d9829d913e8afc
99039501b770248c8ceb39890be5dfe129dc1082
refs/heads/master
1,682,985,669,944
1,621,126,241,000
1,621,126,241,000
335,706,272
0
38
null
1,618,325,669,000
1,612,374,118,000
Lean
UTF-8
Lean
false
false
694
lean
namespace hidden /- The empty data type has no values/constructors at all. This fact becomes interesting and useful when one performs a case analysis on values of this type, as there are no cases to consider. -/ inductive empty : Type -- uninhabited /- Exercise: Show that you can implement a function, e2n, that takes (assumes it's given) an argument, e, of type empty and that then uses match/with/end to "eliminate" e and to return without returning any particular value of type nat. -/ def bool_not' : bool → bool | tt := ff | ff := tt def bool_not : bool → bool := λ b, match b with | tt := ff | ff := tt end def e2n (e : empty) : nat := match e with end end hidden
89e06d6ce59591b66a7f2ffe3fbc3ac85e6556f1
c777c32c8e484e195053731103c5e52af26a25d1
/src/analysis/normed_space/extend.lean
a9080f14c606e3c4239e0d55be814d74a4f11973
[ "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
6,703
lean
/- Copyright (c) 2020 Ruben Van de Velde. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Ruben Van de Velde -/ import analysis.normed_space.operator_norm import algebra.algebra.restrict_scalars import data.is_R_or_C.basic /-! # Extending a continuous `ℝ`-linear map to a continuous `𝕜`-linear map In this file we provide a way to extend a continuous `ℝ`-linear map to a continuous `𝕜`-linear map in a way that bounds the norm by the norm of the original map, when `𝕜` is either `ℝ` (the extension is trivial) or `ℂ`. We formulate the extension uniformly, by assuming `is_R_or_C 𝕜`. We motivate the form of the extension as follows. Note that `fc : F →ₗ[𝕜] 𝕜` is determined fully by `Re fc`: for all `x : F`, `fc (I • x) = I * fc x`, so `Im (fc x) = -Re (fc (I • x))`. Therefore, given an `fr : F →ₗ[ℝ] ℝ`, we define `fc x = fr x - fr (I • x) * I`. ## Main definitions * `linear_map.extend_to_𝕜` * `continuous_linear_map.extend_to_𝕜` ## Implementation details For convenience, the main definitions above operate in terms of `restrict_scalars ℝ 𝕜 F`. Alternate forms which operate on `[is_scalar_tower ℝ 𝕜 F]` instead are provided with a primed name. -/ open is_R_or_C open_locale complex_conjugate variables {𝕜 : Type*} [is_R_or_C 𝕜] {F : Type*} [seminormed_add_comm_group F] [normed_space 𝕜 F] namespace linear_map variables [module ℝ F] [is_scalar_tower ℝ 𝕜 F] /-- Extend `fr : F →ₗ[ℝ] ℝ` to `F →ₗ[𝕜] 𝕜` in a way that will also be continuous and have its norm bounded by `‖fr‖` if `fr` is continuous. -/ noncomputable def extend_to_𝕜' (fr : F →ₗ[ℝ] ℝ) : F →ₗ[𝕜] 𝕜 := begin let fc : F → 𝕜 := λ x, (fr x : 𝕜) - (I : 𝕜) * (fr ((I : 𝕜) • x)), have add : ∀ x y : F, fc (x + y) = fc x + fc y, { assume x y, simp only [fc], simp only [smul_add, linear_map.map_add, of_real_add], rw mul_add, abel, }, have A : ∀ (c : ℝ) (x : F), (fr ((c : 𝕜) • x) : 𝕜) = (c : 𝕜) * (fr x : 𝕜), { assume c x, rw [← of_real_mul], congr' 1, rw [is_R_or_C.of_real_alg, smul_assoc, fr.map_smul, algebra.id.smul_eq_mul, one_smul] }, have smul_ℝ : ∀ (c : ℝ) (x : F), fc ((c : 𝕜) • x) = (c : 𝕜) * fc x, { assume c x, simp only [fc, A], rw A c x, rw [smul_smul, mul_comm I (c : 𝕜), ← smul_smul, A, mul_sub], ring }, have smul_I : ∀ x : F, fc ((I : 𝕜) • x) = (I : 𝕜) * fc x, { assume x, simp only [fc], cases @I_mul_I_ax 𝕜 _ with h h, { simp [h] }, rw [mul_sub, ← mul_assoc, smul_smul, h], simp only [neg_mul, linear_map.map_neg, one_mul, one_smul, mul_neg, of_real_neg, neg_smul, sub_neg_eq_add, add_comm] }, have smul_𝕜 : ∀ (c : 𝕜) (x : F), fc (c • x) = c • fc x, { assume c x, rw [← re_add_im c, add_smul, add_smul, add, smul_ℝ, ← smul_smul, smul_ℝ, smul_I, ← mul_assoc], refl }, exact { to_fun := fc, map_add' := add, map_smul' := smul_𝕜 } end lemma extend_to_𝕜'_apply (fr : F →ₗ[ℝ] ℝ) (x : F) : fr.extend_to_𝕜' x = (fr x : 𝕜) - (I : 𝕜) * fr ((I : 𝕜) • x) := rfl @[simp] lemma extend_to_𝕜'_apply_re (fr : F →ₗ[ℝ] ℝ) (x : F) : re (fr.extend_to_𝕜' x : 𝕜) = fr x := by simp only [extend_to_𝕜'_apply, map_sub, zero_mul, mul_zero, sub_zero] with is_R_or_C_simps lemma norm_extend_to_𝕜'_apply_sq (f : F →ₗ[ℝ] ℝ) (x : F) : ‖(f.extend_to_𝕜' x : 𝕜)‖ ^ 2 = f (conj (f.extend_to_𝕜' x : 𝕜) • x) := calc ‖(f.extend_to_𝕜' x : 𝕜)‖ ^ 2 = re (conj (f.extend_to_𝕜' x) * f.extend_to_𝕜' x : 𝕜) : by rw [is_R_or_C.conj_mul, norm_sq_eq_def', of_real_re] ... = f (conj (f.extend_to_𝕜' x : 𝕜) • x) : by rw [← smul_eq_mul, ← map_smul, extend_to_𝕜'_apply_re] end linear_map namespace continuous_linear_map variables [normed_space ℝ F] [is_scalar_tower ℝ 𝕜 F] /-- The norm of the extension is bounded by `‖fr‖`. -/ lemma norm_extend_to_𝕜'_bound (fr : F →L[ℝ] ℝ) (x : F) : ‖(fr.to_linear_map.extend_to_𝕜' x : 𝕜)‖ ≤ ‖fr‖ * ‖x‖ := begin set lm : F →ₗ[𝕜] 𝕜 := fr.to_linear_map.extend_to_𝕜', classical, by_cases h : lm x = 0, { rw [h, norm_zero], apply mul_nonneg; exact norm_nonneg _ }, rw [← mul_le_mul_left (norm_pos_iff.2 h), ← sq], calc ‖lm x‖ ^ 2 = fr (conj (lm x : 𝕜) • x) : fr.to_linear_map.norm_extend_to_𝕜'_apply_sq x ... ≤ ‖fr (conj (lm x : 𝕜) • x)‖ : le_abs_self _ ... ≤ ‖fr‖ * ‖conj (lm x : 𝕜) • x‖ : le_op_norm _ _ ... = ‖(lm x : 𝕜)‖ * (‖fr‖ * ‖x‖) : by rw [norm_smul, norm_conj, mul_left_comm] end /-- Extend `fr : F →L[ℝ] ℝ` to `F →L[𝕜] 𝕜`. -/ noncomputable def extend_to_𝕜' (fr : F →L[ℝ] ℝ) : F →L[𝕜] 𝕜 := linear_map.mk_continuous _ (‖fr‖) fr.norm_extend_to_𝕜'_bound lemma extend_to_𝕜'_apply (fr : F →L[ℝ] ℝ) (x : F) : fr.extend_to_𝕜' x = (fr x : 𝕜) - (I : 𝕜) * fr ((I : 𝕜) • x) := rfl @[simp] lemma norm_extend_to_𝕜' (fr : F →L[ℝ] ℝ) : ‖(fr.extend_to_𝕜' : F →L[𝕜] 𝕜)‖ = ‖fr‖ := le_antisymm (linear_map.mk_continuous_norm_le _ (norm_nonneg _) _) $ op_norm_le_bound _ (norm_nonneg _) $ λ x, calc ‖fr x‖ = ‖re (fr.extend_to_𝕜' x : 𝕜)‖ : congr_arg norm (fr.extend_to_𝕜'_apply_re x).symm ... ≤ ‖(fr.extend_to_𝕜' x : 𝕜)‖ : abs_re_le_norm _ ... ≤ ‖(fr.extend_to_𝕜' : F →L[𝕜] 𝕜)‖ * ‖x‖ : le_op_norm _ _ end continuous_linear_map /-- Extend `fr : restrict_scalars ℝ 𝕜 F →ₗ[ℝ] ℝ` to `F →ₗ[𝕜] 𝕜`. -/ noncomputable def linear_map.extend_to_𝕜 (fr : (restrict_scalars ℝ 𝕜 F) →ₗ[ℝ] ℝ) : F →ₗ[𝕜] 𝕜 := fr.extend_to_𝕜' lemma linear_map.extend_to_𝕜_apply (fr : (restrict_scalars ℝ 𝕜 F) →ₗ[ℝ] ℝ) (x : F) : fr.extend_to_𝕜 x = (fr x : 𝕜) - (I : 𝕜) * fr ((I : 𝕜) • x : _) := rfl /-- Extend `fr : restrict_scalars ℝ 𝕜 F →L[ℝ] ℝ` to `F →L[𝕜] 𝕜`. -/ noncomputable def continuous_linear_map.extend_to_𝕜 (fr : (restrict_scalars ℝ 𝕜 F) →L[ℝ] ℝ) : F →L[𝕜] 𝕜 := fr.extend_to_𝕜' lemma continuous_linear_map.extend_to_𝕜_apply (fr : (restrict_scalars ℝ 𝕜 F) →L[ℝ] ℝ) (x : F) : fr.extend_to_𝕜 x = (fr x : 𝕜) - (I : 𝕜) * fr ((I : 𝕜) • x : _) := rfl @[simp] lemma continuous_linear_map.norm_extend_to_𝕜 (fr : (restrict_scalars ℝ 𝕜 F) →L[ℝ] ℝ) : ‖fr.extend_to_𝕜‖ = ‖fr‖ := fr.norm_extend_to_𝕜'
8afee0796da52d276b7d86ea35a9d08fc830e571
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/category_theory/monad/products.lean
b2359bb522dfae6768fc43d59cc2ba329a93fb24
[ "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,061
lean
/- Copyright (c) 2021 Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta -/ import category_theory.over import category_theory.limits.preserves.basic import category_theory.limits.creates import category_theory.limits.shapes.binary_products import category_theory.monad.algebra /-! # Algebras for the coproduct monad The functor `Y ↦ X ⨿ Y` forms a monad, whose category of monads is equivalent to the under category of `X`. Similarly, `Y ↦ X ⨯ Y` forms a comonad, whose category of comonads is equivalent to the over category of `X`. ## TODO Show that `over.forget X : over X ⥤ C` is a comonadic left adjoint and `under.forget : under X ⥤ C` is a monadic right adjoint. -/ noncomputable theory universes v u -- morphism levels before object levels. See note [category_theory universes]. namespace category_theory open category limits variables {C : Type u} [category.{v} C] (X : C) section open comonad variable [has_binary_products C] /-- `X ⨯ -` has a comonad structure. This is sometimes called the writer comonad. -/ @[simps] def prod_comonad : comonad C := { to_functor := prod.functor.obj X, ε' := { app := λ Y, limits.prod.snd }, δ' := { app := λ Y, prod.lift limits.prod.fst (𝟙 _) } } /-- The forward direction of the equivalence from coalgebras for the product comonad to the over category. -/ @[simps] def coalgebra_to_over : coalgebra (prod_comonad X) ⥤ over X := { obj := λ A, over.mk (A.a ≫ limits.prod.fst), map := λ A₁ A₂ f, over.hom_mk f.f (by { rw [over.mk_hom, ← f.h_assoc], dsimp, simp }) } /-- The backward direction of the equivalence from coalgebras for the product comonad to the over category. -/ @[simps] def over_to_coalgebra : over X ⥤ coalgebra (prod_comonad X) := { obj := λ f, { A := f.left, a := prod.lift f.hom (𝟙 _) }, map := λ f₁ f₂ g, { f := g.left } } /-- The equivalence from coalgebras for the product comonad to the over category. -/ @[simps] def coalgebra_equiv_over : coalgebra (prod_comonad X) ≌ over X := { functor := coalgebra_to_over X, inverse := over_to_coalgebra X, unit_iso := nat_iso.of_components (λ A, coalgebra.iso_mk (iso.refl _) (prod.hom_ext (by { dsimp, simp }) (by { dsimp, simpa using A.counit }))) (λ A₁ A₂ f, by { ext, simp }), counit_iso := nat_iso.of_components (λ f, over.iso_mk (iso.refl _)) (λ f g k, by tidy) }. end section open monad variable [has_binary_coproducts C] /-- `X ⨿ -` has a monad structure. This is sometimes called the either monad. -/ @[simps] def coprod_monad : monad C := { to_functor := coprod.functor.obj X, η' := { app := λ Y, coprod.inr }, μ' := { app := λ Y, coprod.desc coprod.inl (𝟙 _) } } /-- The forward direction of the equivalence from algebras for the coproduct monad to the under category. -/ @[simps] def algebra_to_under : monad.algebra (coprod_monad X) ⥤ under X := { obj := λ A, under.mk (coprod.inl ≫ A.a), map := λ A₁ A₂ f, under.hom_mk f.f (by { rw [under.mk_hom, assoc, ←f.h], dsimp, simp }) } /-- The backward direction of the equivalence from algebras for the coproduct monad to the under category. -/ @[simps] def under_to_algebra : under X ⥤ monad.algebra (coprod_monad X) := { obj := λ f, { A := f.right, a := coprod.desc f.hom (𝟙 _) }, map := λ f₁ f₂ g, { f := g.right } } /-- The equivalence from algebras for the coproduct monad to the under category. -/ @[simps] def algebra_equiv_under : monad.algebra (coprod_monad X) ≌ under X := { functor := algebra_to_under X, inverse := under_to_algebra X, unit_iso := nat_iso.of_components (λ A, monad.algebra.iso_mk (iso.refl _) (coprod.hom_ext (by tidy) (by { dsimp, simpa using A.unit.symm }))) (λ A₁ A₂ f, by { ext, simp }), counit_iso := nat_iso.of_components (λ f, under.iso_mk (iso.refl _) (by tidy)) (λ f g k, by tidy) }. end end category_theory
46a7fd33e3f36af2521d7bcc429bfc060cbc4a4b
57aec6ee746bc7e3a3dd5e767e53bd95beb82f6d
/stage0/src/Lean/ReducibilityAttrs.lean
6545f7c83e9db49dff52ee69faf1d8a2cfcb8834
[ "Apache-2.0" ]
permissive
collares/lean4
861a9269c4592bce49b71059e232ff0bfe4594cc
52a4f535d853a2c7c7eea5fee8a4fa04c682c1ee
refs/heads/master
1,691,419,031,324
1,618,678,138,000
1,618,678,138,000
358,989,750
0
0
Apache-2.0
1,618,696,333,000
1,618,696,333,000
null
UTF-8
Lean
false
false
1,887
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.Attributes namespace Lean inductive ReducibilityStatus where | reducible | semireducible | irreducible deriving Inhabited, Repr builtin_initialize reducibilityAttrs : EnumAttributes ReducibilityStatus ← registerEnumAttributes `reducibility [(`reducible, "reducible", ReducibilityStatus.reducible), (`semireducible, "semireducible", ReducibilityStatus.semireducible), (`irreducible, "irreducible", ReducibilityStatus.irreducible)] @[export lean_get_reducibility_status] def getReducibilityStatusImp (env : Environment) (declName : Name) : ReducibilityStatus := match reducibilityAttrs.getValue env declName with | some s => s | none => ReducibilityStatus.semireducible @[export lean_set_reducibility_status] def setReducibilityStatusImp (env : Environment) (declName : Name) (s : ReducibilityStatus) : Environment := match reducibilityAttrs.setValue env declName s with | Except.ok env => env | _ => env -- TODO(Leo): we should extend EnumAttributes.setValue in the future and ensure it never fails def getReducibilityStatus {m} [Monad m] [MonadEnv m] (declName : Name) : m ReducibilityStatus := do return getReducibilityStatusImp (← getEnv) declName def setReducibilityStatus {m} [Monad m] [MonadEnv m] (declName : Name) (s : ReducibilityStatus) : m Unit := do modifyEnv fun env => setReducibilityStatusImp env declName s def setReducibleAttribute {m} [Monad m] [MonadEnv m] (declName : Name) : m Unit := do setReducibilityStatus declName ReducibilityStatus.reducible def isReducible {m} [Monad m] [MonadEnv m] (declName : Name) : m Bool := do match ← getReducibilityStatus declName with | ReducibilityStatus.reducible => true | _ => false end Lean
00ba47a0953f14ad6b2e0e72308798910a6179fb
1fbca480c1574e809ae95a3eda58188ff42a5e41
/src/util/data/stream.lean
6533fde0cff5ab3afa5f196123a2b61c0966b8b9
[]
no_license
unitb/lean-lib
560eea0acf02b1fd4bcaac9986d3d7f1a4290e7e
439b80e606b4ebe4909a08b1d77f4f5c0ee3dee9
refs/heads/master
1,610,706,025,400
1,570,144,245,000
1,570,144,245,000
99,579,229
5
0
null
null
null
null
UTF-8
Lean
false
false
3,886
lean
import data.list import data.stream import util.classical universe variables u namespace stream open nat lemma map_app {α β} (f : α → β) (s : stream α) (i : ℕ) : stream.map f s i = f (s i) := rfl def sprefix {α} : ℕ → stream α → list α | 0 _ := [] | (succ n) x := x 0 :: sprefix n (stream.tail x) def sums (x : stream ℕ) : stream ℕ | 0 := 0 | (succ n) := x n + sums n def range : ℕ → list ℕ | 0 := [] | (succ n) := n :: range n def prepend {α} : list α → stream α → stream α | [] xs := xs | (list.cons x xs) ys := stream.cons x (prepend xs ys) def coinduct {α β} (f : α → (β × α)) (x : α) : stream β := stream.corec (prod.fst ∘ f) (prod.snd ∘ f) x def rounds.f : ℕ × ℕ → ℕ × ℕ × ℕ | (n, 0) := (0, succ n, succ n) | (n, succ p) := (succ p, n, p) lemma stream.head_corec {α β} (f : α → α) (g : α → β) (x : α) : stream.head (stream.corec g f x) = g x := begin refl end section s variables {α β : Type u} (f : α → α) (g : α → β) (x : α) variables i : ℕ variables s : stream α lemma stream.nth_tail : stream.nth i (stream.tail s) = stream.nth (succ i) s := rfl lemma stream.tail_corec : stream.tail (stream.corec g f x) = stream.corec g f (f x) := begin apply stream.ext, intro i, unfold stream.corec, rw [stream.map_iterate,stream.tail_map,stream.tail_iterate], rw [stream.map_iterate], end theorem head_drop : head (drop i s) = nth i s := begin change nth 0 (drop i s) = _, rw nth_drop 0 i s, simp end end s universe variables u₀ u₁ variables {α : Type u₀} variables {β : Type u₁} def zip' (x : stream α) (y : stream β) : stream (α × β) := λ i, (x i, y i) lemma fst_comp_zip' (x : stream α) (y : stream β) : prod.fst ∘ zip' x y = x := rfl lemma fst_zip' (x : stream α) (y : stream β) (i : ℕ) : prod.fst (zip' x y i) = x i := rfl lemma snd_zip' (x : stream α) (y : stream β) (i : ℕ) : prod.snd (zip' x y i) = y i := rfl lemma length_approx (i : ℕ) (s : stream α) : list.length (approx i s) = i := begin revert s, induction i with i IH ; intro s, { refl }, { simp [approx_succ,IH], } end lemma approx_succ_eq_append (i : ℕ) (s : stream α) : approx (succ i) s = approx i s ++ [s i] := begin revert s, induction i with i IH ; intros s, { refl }, { have H : tail s i = s (succ i), { refl }, rw [approx_succ,IH,approx_succ,list.cons_append,H], } end lemma approx_succ_eq_concat (i : ℕ) (s : stream α) : approx (succ i) s = list.concat (approx i s) (s i) := by rw [list.concat_eq_append,approx_succ_eq_append] section next parameter {p : ℕ → Prop} parameter Hp : ∀ i, ∃ j, i ≤ j ∧ p j noncomputable def solutions : stream ℕ | 0 := classical.some (Hp 0) | (succ n) := classical.some $ Hp $ succ $ solutions n lemma solutions_spec (i : ℕ) : p (solutions i) := begin cases i with i, { apply classical.some_spec', intro, apply and.right }, { apply classical.some_spec', intro, apply and.right }, end lemma solutions_increases {i j : ℕ} (H : i < j) : solutions i < solutions j := begin rw ← add_sub_of_le H, clear H, generalize : (j - succ i) = k, clear j, rw [succ_add], induction k, case zero { dsimp [solutions], simp, apply_some_spec, }, case succ : j { unfold solutions, apply classical.some_spec', intros x Hx, transitivity solutions Hp (succ (i + j)), { apply k_ih }, { simp [stream.solutions], apply Hx.left } }, end lemma solutions_injective : function.injective solutions := begin intros i j H, apply classical.by_contradiction, intros H', revert H, apply (@ne_iff_lt_or_gt ℕ _ _ _).mpr, cases (@ne_iff_lt_or_gt ℕ _ _ _).mp H' with H₁ H₁, { left, apply solutions_increases _ H₁, }, { right, apply solutions_increases _ H₁, }, end end next end stream
003401f34cc8d4eca5688a64628b7f3162e51b09
9dd3f3912f7321eb58ee9aa8f21778ad6221f87c
/tests/lean/run/default_param2.lean
db1a8955a2a1b9d01c2a56ccd66d3437468ff908
[ "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
1,075
lean
universe variable u def f (a : nat) (o : nat := 5) := a + o example : f 1 = f 1 5 := rfl check f 1 structure config := (v1 := 10) (v2 := 20) (flag := tt) (ps := ["hello", "world"]) def g (a : nat) (c : config := {}) : nat := if c^.flag then a + c^.v1 else a + c^.v2 example : g 1 = 11 := rfl example : g 1 {flag := ff} = 21 := rfl example : g 1 {v1 := 100} = 101 := rfl def h (a : nat) (c : config := {v1 := a}) : nat := g a c example : h 2 = 4 := rfl example : h 3 = 6 := rfl example : h 2 {flag := ff} = 22 := rfl def boo (a : nat) (b : nat := a) (c : bool := ff) (d : config := {v2 := b, flag := c}) := g a d check boo 2 example : boo 2 = 4 := rfl example : boo 2 20 = 22 := rfl example : boo 2 0 tt = 12 := rfl open tactic set_option pp.all true meta def check_expr (p : pexpr) (t : expr) : tactic unit := do e ← to_expr p, guard (t = e) run_command do e ← to_expr `(boo 2), check_expr `(boo 2 (2:nat) ff {v1 := config.v1._default, v2 := 2, flag := ff, ps := config.ps._default}) e, e ← to_expr `(f 1), check_expr `(f 1 (5:nat)) e
e44258e7127e0107f590f3aa40aa028c34954694
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/def3.lean
dc669dbab564e98f781d160ede3831faf7318027
[ "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
89
lean
abbrev N := Nat def f : N → Nat | 0 => 1 | n+1 => n theorem ex1 : f 0 = 1 := rfl
1ac0e86162eb22ff017e2b6fe4a8e8b3d810a3e3
86f6f4f8d827a196a32bfc646234b73328aeb306
/examples/basics/unnamed_775.lean
bca7ac3dd100afe9124d1a54151e36e291a5c2a1
[]
no_license
jamescheuk91/mathematics_in_lean
09f1f87d2b0dce53464ff0cbe592c568ff59cf5e
4452499264e2975bca2f42565c0925506ba5dda3
refs/heads/master
1,679,716,410,967
1,613,957,947,000
1,613,957,947,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
386
lean
import algebra.ring namespace my_ring variables {R : Type*} [ring R] -- BEGIN theorem neg_eq_of_add_eq_zero {a b : R} (h : a + b = 0) : -a = b := sorry theorem eq_neg_of_add_eq_zero {a b : R} (h : a + b = 0) : a = -b := sorry theorem neg_zero : (-0 : R) = 0 := begin apply neg_eq_of_add_eq_zero, rw add_zero end theorem neg_neg (a : R) : -(-a) = a := sorry -- END end my_ring
4a30194f02c8e797c272516b1082bbd52b43cfc5
ebb7367fa8ab324601b5abf705720fd4cc0e8598
/algebra/graded.hlean
36f84cfd5519c3af21ffa998c5d9ed4f15c9c79e
[ "Apache-2.0" ]
permissive
radams78/Spectral
3e34916d9bbd0939ee6a629e36744827ff27bfc2
c8145341046cfa2b4960ef3cc5a1117d12c43f63
refs/heads/master
1,610,421,583,830
1,481,232,014,000
1,481,232,014,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
177
hlean
/- Copyright (c) 2016 Egbert Rijke. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Egbert Rijke Graded modules and rings. -/
1167a76d65b13e777e48145b36ec2fde2d134c27
471bedbd023d35c9d078c2f936dd577ace7f5813
/library/init/classical.lean
23c31664050adc4b6b735641c04bbe6850f069d5
[ "Apache-2.0" ]
permissive
lambdaxymox/lean
e06f0fa503666df827edd9867d7f49ca017aae64
fc13c8c72a15dab71a2c2b31410c2cadc3526bd7
refs/heads/master
1,666,785,407,985
1,666,153,673,000
1,666,153,673,000
310,165,986
0
0
Apache-2.0
1,604,542,096,000
1,604,542,095,000
null
UTF-8
Lean
false
false
5,556
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, Jeremy Avigad, Mario Carneiro -/ prelude import init.data.subtype.basic init.funext namespace classical universes u v /- the axiom -/ axiom choice {α : Sort u} : nonempty α → α @[irreducible] noncomputable def indefinite_description {α : Sort u} (p : α → Prop) (h : ∃ x, p x) : {x // p x} := choice $ let ⟨x, px⟩ := h in ⟨⟨x, px⟩⟩ noncomputable def some {α : Sort u} {p : α → Prop} (h : ∃ x, p x) : α := (indefinite_description p h).val theorem some_spec {α : Sort u} {p : α → Prop} (h : ∃ x, p x) : p (some h) := (indefinite_description p h).property /- Diaconescu's theorem: using function extensionality and propositional extensionality, we can get excluded middle from this. -/ section diaconescu parameter p : Prop private def U (x : Prop) : Prop := x = true ∨ p private def V (x : Prop) : Prop := x = false ∨ p private lemma exU : ∃ x, U x := ⟨true, or.inl rfl⟩ private lemma exV : ∃ x, V x := ⟨false, or.inl rfl⟩ private def u : Prop := some exU private def v : Prop := some exV private lemma u_def : U u := some_spec exU private lemma v_def : V v := some_spec exV private lemma not_uv_or_p : u ≠ v ∨ p := or.elim u_def (assume hut : u = true, or.elim v_def (assume hvf : v = false, have hne : u ≠ v, from hvf.symm ▸ hut.symm ▸ true_ne_false, or.inl hne) or.inr) or.inr private lemma p_implies_uv (hp : p) : u = v := have hpred : U = V, from funext (assume x : Prop, have hl : (x = true ∨ p) → (x = false ∨ p), from assume a, or.inr hp, have hr : (x = false ∨ p) → (x = true ∨ p), from assume a, or.inr hp, show (x = true ∨ p) = (x = false ∨ p), from propext (iff.intro hl hr)), have h₀ : ∀ exU exV, @some _ U exU = @some _ V exV, from hpred ▸ λ exU exV, rfl, show u = v, from h₀ _ _ theorem em : p ∨ ¬p := or.elim not_uv_or_p (assume hne : u ≠ v, or.inr (mt p_implies_uv hne)) or.inl end diaconescu theorem exists_true_of_nonempty {α : Sort u} : nonempty α → ∃ x : α, true | ⟨x⟩ := ⟨x, trivial⟩ noncomputable def inhabited_of_nonempty {α : Sort u} (h : nonempty α) : inhabited α := ⟨choice h⟩ noncomputable def inhabited_of_exists {α : Sort u} {p : α → Prop} (h : ∃ x, p x) : inhabited α := inhabited_of_nonempty (exists.elim h (λ w hw, ⟨w⟩)) /- all propositions are decidable -/ noncomputable def prop_decidable (a : Prop) : decidable a := choice $ or.elim (em a) (assume ha, ⟨is_true ha⟩) (assume hna, ⟨is_false hna⟩) local attribute [instance] prop_decidable noncomputable def decidable_inhabited (a : Prop) : inhabited (decidable a) := ⟨prop_decidable a⟩ local attribute [instance] decidable_inhabited noncomputable def type_decidable_eq (α : Sort u) : decidable_eq α := λ x y, prop_decidable (x = y) noncomputable def type_decidable (α : Sort u) : psum α (α → false) := match (prop_decidable (nonempty α)) with | (is_true hp) := psum.inl (@inhabited.default _ (inhabited_of_nonempty hp)) | (is_false hn) := psum.inr (λ a, absurd (nonempty.intro a) hn) end @[irreducible] noncomputable def strong_indefinite_description {α : Sort u} (p : α → Prop) (h : nonempty α) : {x : α // (∃ y : α, p y) → p x} := if hp : ∃ x : α, p x then let xp := indefinite_description _ hp in ⟨xp.val, λ h', xp.property⟩ else ⟨choice h, λ h, absurd h hp⟩ /- the Hilbert epsilon function -/ noncomputable def epsilon {α : Sort u} [h : nonempty α] (p : α → Prop) : α := (strong_indefinite_description p h).val theorem epsilon_spec_aux {α : Sort u} (h : nonempty α) (p : α → Prop) : (∃ y, p y) → p (@epsilon α h p) := (strong_indefinite_description p h).property theorem epsilon_spec {α : Sort u} {p : α → Prop} (hex : ∃ y, p y) : p (@epsilon α (nonempty_of_exists hex) p) := epsilon_spec_aux (nonempty_of_exists hex) p hex theorem epsilon_singleton {α : Sort u} (x : α) : @epsilon α ⟨x⟩ (λ y, y = x) = x := @epsilon_spec α (λ y, y = x) ⟨x, rfl⟩ /- the axiom of choice -/ theorem axiom_of_choice {α : Sort u} {β : α → Sort v} {r : Π x, β x → Prop} (h : ∀ x, ∃ y, r x y) : ∃ (f : Π x, β x), ∀ x, r x (f x) := ⟨_, λ x, some_spec (h x)⟩ theorem skolem {α : Sort u} {b : α → Sort v} {p : Π x, b x → Prop} : (∀ x, ∃ y, p x y) ↔ ∃ (f : Π x, b x), ∀ x, p x (f x) := ⟨axiom_of_choice, λ ⟨f, hw⟩ x, ⟨f x, hw x⟩⟩ theorem prop_complete (a : Prop) : a = true ∨ a = false := or.elim (em a) (λ t, or.inl (eq_true_intro t)) (λ f, or.inr (eq_false_intro f)) section aux attribute [elab_as_eliminator] theorem cases_true_false (p : Prop → Prop) (h1 : p true) (h2 : p false) (a : Prop) : p a := or.elim (prop_complete a) (assume ht : a = true, ht.symm ▸ h1) (assume hf : a = false, hf.symm ▸ h2) theorem cases_on (a : Prop) {p : Prop → Prop} (h1 : p true) (h2 : p false) : p a := cases_true_false p h1 h2 a -- this supercedes by_cases in decidable lemma by_cases {p q : Prop} (hpq : p → q) (hnpq : ¬p → q) : q := decidable.by_cases hpq hnpq -- this supercedes by_contradiction in decidable theorem by_contradiction {p : Prop} (h : ¬p → false) : p := decidable.by_contradiction h theorem eq_false_or_eq_true (a : Prop) : a = false ∨ a = true := (prop_complete a).symm end aux end classical
4c1e080f8c0dde3fe427d3db164200b88ffb10a7
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/topology/dense_embedding_auto.lean
2b3f9d86454618366c5a19032a050e54aca6c8cb
[]
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
13,412
lean
/- Copyright (c) 2019 Reid Barton. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Patrick Massot -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.topology.separation import Mathlib.topology.bases import Mathlib.PostPort universes u_1 u_2 l u_3 u_4 namespace Mathlib /-! # Dense embeddings This file defines three properties of functions: * `dense_range f` means `f` has dense image; * `dense_inducing i` means `i` is also `inducing`; * `dense_embedding e` means `e` is also an `embedding`. The main theorem `continuous_extend` gives a criterion for a function `f : X → Z` to a regular (T₃) space Z to extend along a dense embedding `i : X → Y` to a continuous function `g : Y → Z`. Actually `i` only has to be `dense_inducing` (not necessarily injective). -/ /-- `i : α → β` is "dense inducing" if it has dense range and the topology on `α` is the one induced by `i` from the topology on `β`. -/ structure dense_inducing {α : Type u_1} {β : Type u_2} [topological_space α] [topological_space β] (i : α → β) extends inducing i where dense : dense_range i namespace dense_inducing theorem nhds_eq_comap {α : Type u_1} {β : Type u_2} [topological_space α] [topological_space β] {i : α → β} (di : dense_inducing i) (a : α) : nhds a = filter.comap i (nhds (i a)) := inducing.nhds_eq_comap (to_inducing di) protected theorem continuous {α : Type u_1} {β : Type u_2} [topological_space α] [topological_space β] {i : α → β} (di : dense_inducing i) : continuous i := inducing.continuous (to_inducing di) theorem closure_range {α : Type u_1} {β : Type u_2} [topological_space α] [topological_space β] {i : α → β} (di : dense_inducing i) : closure (set.range i) = set.univ := dense_range.closure_range (dense di) theorem self_sub_closure_image_preimage_of_open {α : Type u_1} {β : Type u_2} [topological_space α] [topological_space β] {i : α → β} {s : set β} (di : dense_inducing i) : is_open s → s ⊆ closure (i '' (i ⁻¹' s)) := sorry theorem closure_image_nhds_of_nhds {α : Type u_1} {β : Type u_2} [topological_space α] [topological_space β] {i : α → β} {s : set α} {a : α} (di : dense_inducing i) : s ∈ nhds a → closure (i '' s) ∈ nhds (i a) := sorry /-- The product of two dense inducings is a dense inducing -/ protected theorem prod {α : Type u_1} {β : Type u_2} {γ : Type u_3} {δ : Type u_4} [topological_space α] [topological_space β] [topological_space γ] [topological_space δ] {e₁ : α → β} {e₂ : γ → δ} (de₁ : dense_inducing e₁) (de₂ : dense_inducing e₂) : dense_inducing fun (p : α × γ) => (e₁ (prod.fst p), e₂ (prod.snd p)) := mk (inducing.mk (inducing.induced (inducing.prod_mk (to_inducing de₁) (to_inducing de₂)))) (dense_range.prod_map (dense de₁) (dense de₂)) /-- If the domain of a `dense_inducing` map is a separable space, then so is the codomain. -/ protected theorem separable_space {α : Type u_1} {β : Type u_2} [topological_space α] [topological_space β] {i : α → β} (di : dense_inducing i) [topological_space.separable_space α] : topological_space.separable_space β := dense_range.separable_space (dense di) (dense_inducing.continuous di) /-- γ -f→ α g↓ ↓e δ -h→ β -/ theorem tendsto_comap_nhds_nhds {α : Type u_1} {β : Type u_2} {γ : Type u_3} {δ : Type u_4} [topological_space α] [topological_space β] {i : α → β} [topological_space δ] {f : γ → α} {g : γ → δ} {h : δ → β} {d : δ} {a : α} (di : dense_inducing i) (H : filter.tendsto h (nhds d) (nhds (i a))) (comm : h ∘ g = i ∘ f) : filter.tendsto f (filter.comap g (nhds d)) (nhds a) := sorry protected theorem nhds_within_ne_bot {α : Type u_1} {β : Type u_2} [topological_space α] [topological_space β] {i : α → β} (di : dense_inducing i) (b : β) : filter.ne_bot (nhds_within b (set.range i)) := dense_range.nhds_within_ne_bot (dense di) b theorem comap_nhds_ne_bot {α : Type u_1} {β : Type u_2} [topological_space α] [topological_space β] {i : α → β} (di : dense_inducing i) (b : β) : filter.ne_bot (filter.comap i (nhds b)) := sorry /-- If `i : α → β` is a dense inducing, then any function `f : α → γ` "extends" to a function `g = extend di f : β → γ`. If `γ` is Hausdorff and `f` has a continuous extension, then `g` is the unique such extension. In general, `g` might not be continuous or even extend `f`. -/ def extend {α : Type u_1} {β : Type u_2} {γ : Type u_3} [topological_space α] [topological_space β] {i : α → β} [topological_space γ] (di : dense_inducing i) (f : α → γ) (b : β) : γ := lim (filter.comap i (nhds b)) f theorem extend_eq_of_tendsto {α : Type u_1} {β : Type u_2} {γ : Type u_3} [topological_space α] [topological_space β] {i : α → β} (di : dense_inducing i) [topological_space γ] [t2_space γ] {b : β} {c : γ} {f : α → γ} (hf : filter.tendsto f (filter.comap i (nhds b)) (nhds c)) : extend di f b = c := filter.tendsto.lim_eq hf theorem extend_eq_at {α : Type u_1} {β : Type u_2} {γ : Type u_3} [topological_space α] [topological_space β] {i : α → β} (di : dense_inducing i) [topological_space γ] [t2_space γ] {f : α → γ} (a : α) (hf : continuous_at f a) : extend di f (i a) = f a := extend_eq_of_tendsto di (nhds_eq_comap di a ▸ hf) theorem extend_eq {α : Type u_1} {β : Type u_2} {γ : Type u_3} [topological_space α] [topological_space β] {i : α → β} (di : dense_inducing i) [topological_space γ] [t2_space γ] {f : α → γ} (hf : continuous f) (a : α) : extend di f (i a) = f a := extend_eq_at di a (continuous.continuous_at hf) theorem extend_unique_at {α : Type u_1} {β : Type u_2} {γ : Type u_3} [topological_space α] [topological_space β] {i : α → β} [topological_space γ] [t2_space γ] {b : β} {f : α → γ} {g : β → γ} (di : dense_inducing i) (hf : filter.eventually (fun (x : α) => g (i x) = f x) (filter.comap i (nhds b))) (hg : continuous_at g b) : extend di f b = g b := sorry theorem extend_unique {α : Type u_1} {β : Type u_2} {γ : Type u_3} [topological_space α] [topological_space β] {i : α → β} [topological_space γ] [t2_space γ] {f : α → γ} {g : β → γ} (di : dense_inducing i) (hf : ∀ (x : α), g (i x) = f x) (hg : continuous g) : extend di f = g := funext fun (b : β) => extend_unique_at di (filter.eventually_of_forall hf) (continuous.continuous_at hg) theorem continuous_at_extend {α : Type u_1} {β : Type u_2} {γ : Type u_3} [topological_space α] [topological_space β] {i : α → β} [topological_space γ] [regular_space γ] {b : β} {f : α → γ} (di : dense_inducing i) (hf : filter.eventually (fun (x : β) => ∃ (c : γ), filter.tendsto f (filter.comap i (nhds x)) (nhds c)) (nhds b)) : continuous_at (extend di f) b := sorry theorem continuous_extend {α : Type u_1} {β : Type u_2} {γ : Type u_3} [topological_space α] [topological_space β] {i : α → β} [topological_space γ] [regular_space γ] {f : α → γ} (di : dense_inducing i) (hf : ∀ (b : β), ∃ (c : γ), filter.tendsto f (filter.comap i (nhds b)) (nhds c)) : continuous (extend di f) := iff.mpr continuous_iff_continuous_at fun (b : β) => continuous_at_extend di (filter.univ_mem_sets' hf) theorem mk' {α : Type u_1} {β : Type u_2} [topological_space α] [topological_space β] (i : α → β) (c : continuous i) (dense : ∀ (x : β), x ∈ closure (set.range i)) (H : ∀ (a : α) (s : set α) (H : s ∈ nhds a), ∃ (t : set β), ∃ (H : t ∈ nhds (i a)), ∀ (b : α), i b ∈ t → b ∈ s) : dense_inducing i := sorry end dense_inducing /-- A dense embedding is an embedding with dense image. -/ structure dense_embedding {α : Type u_1} {β : Type u_2} [topological_space α] [topological_space β] (e : α → β) extends dense_inducing e where inj : function.injective e theorem dense_embedding.mk' {α : Type u_1} {β : Type u_2} [topological_space α] [topological_space β] (e : α → β) (c : continuous e) (dense : dense_range e) (inj : function.injective e) (H : ∀ (a : α) (s : set α) (H : s ∈ nhds a), ∃ (t : set β), ∃ (H : t ∈ nhds (e a)), ∀ (b : α), e b ∈ t → b ∈ s) : dense_embedding e := sorry namespace dense_embedding theorem inj_iff {α : Type u_1} {β : Type u_2} [topological_space α] [topological_space β] {e : α → β} (de : dense_embedding e) {x : α} {y : α} : e x = e y ↔ x = y := function.injective.eq_iff (inj de) theorem to_embedding {α : Type u_1} {β : Type u_2} [topological_space α] [topological_space β] {e : α → β} (de : dense_embedding e) : embedding e := embedding.mk (inducing.mk (inducing.induced (dense_inducing.to_inducing (to_dense_inducing de)))) (inj de) /-- If the domain of a `dense_embedding` is a separable space, then so is its codomain. -/ protected theorem separable_space {α : Type u_1} {β : Type u_2} [topological_space α] [topological_space β] {e : α → β} (de : dense_embedding e) [topological_space.separable_space α] : topological_space.separable_space β := dense_inducing.separable_space (to_dense_inducing de) /-- The product of two dense embeddings is a dense embedding. -/ protected theorem prod {α : Type u_1} {β : Type u_2} {γ : Type u_3} {δ : Type u_4} [topological_space α] [topological_space β] [topological_space γ] [topological_space δ] {e₁ : α → β} {e₂ : γ → δ} (de₁ : dense_embedding e₁) (de₂ : dense_embedding e₂) : dense_embedding fun (p : α × γ) => (e₁ (prod.fst p), e₂ (prod.snd p)) := sorry /-- The dense embedding of a subtype inside its closure. -/ def subtype_emb {β : Type u_2} [topological_space β] {α : Type u_1} (p : α → Prop) (e : α → β) (x : Subtype fun (x : α) => p x) : Subtype fun (x : β) => x ∈ closure (e '' set_of fun (x : α) => p x) := { val := e ↑x, property := sorry } protected theorem subtype {α : Type u_1} {β : Type u_2} [topological_space α] [topological_space β] {e : α → β} (de : dense_embedding e) (p : α → Prop) : dense_embedding (subtype_emb p e) := sorry end dense_embedding theorem is_closed_property {α : Type u_1} {β : Type u_2} [topological_space β] {e : α → β} {p : β → Prop} (he : dense_range e) (hp : is_closed (set_of fun (x : β) => p x)) (h : ∀ (a : α), p (e a)) (b : β) : p b := sorry theorem is_closed_property2 {α : Type u_1} {β : Type u_2} [topological_space β] {e : α → β} {p : β → β → Prop} (he : dense_range e) (hp : is_closed (set_of fun (q : β × β) => p (prod.fst q) (prod.snd q))) (h : ∀ (a₁ a₂ : α), p (e a₁) (e a₂)) (b₁ : β) (b₂ : β) : p b₁ b₂ := (fun (this : ∀ (q : β × β), p (prod.fst q) (prod.snd q)) (b₁ b₂ : β) => this (b₁, b₂)) (is_closed_property (dense_range.prod_map he he) hp fun (_x : α × α) => h (prod.fst _x) (prod.snd _x)) theorem is_closed_property3 {α : Type u_1} {β : Type u_2} [topological_space β] {e : α → β} {p : β → β → β → Prop} (he : dense_range e) (hp : is_closed (set_of fun (q : β × β × β) => p (prod.fst q) (prod.fst (prod.snd q)) (prod.snd (prod.snd q)))) (h : ∀ (a₁ a₂ a₃ : α), p (e a₁) (e a₂) (e a₃)) (b₁ : β) (b₂ : β) (b₃ : β) : p b₁ b₂ b₃ := sorry theorem dense_range.induction_on {α : Type u_1} {β : Type u_2} [topological_space β] {e : α → β} (he : dense_range e) {p : β → Prop} (b₀ : β) (hp : is_closed (set_of fun (b : β) => p b)) (ih : ∀ (a : α), p (e a)) : p b₀ := is_closed_property he hp ih b₀ theorem dense_range.induction_on₂ {α : Type u_1} {β : Type u_2} [topological_space β] {e : α → β} {p : β → β → Prop} (he : dense_range e) (hp : is_closed (set_of fun (q : β × β) => p (prod.fst q) (prod.snd q))) (h : ∀ (a₁ a₂ : α), p (e a₁) (e a₂)) (b₁ : β) (b₂ : β) : p b₁ b₂ := is_closed_property2 he hp h b₁ b₂ theorem dense_range.induction_on₃ {α : Type u_1} {β : Type u_2} [topological_space β] {e : α → β} {p : β → β → β → Prop} (he : dense_range e) (hp : is_closed (set_of fun (q : β × β × β) => p (prod.fst q) (prod.fst (prod.snd q)) (prod.snd (prod.snd q)))) (h : ∀ (a₁ a₂ a₃ : α), p (e a₁) (e a₂) (e a₃)) (b₁ : β) (b₂ : β) (b₃ : β) : p b₁ b₂ b₃ := is_closed_property3 he hp h b₁ b₂ b₃ /-- Two continuous functions to a t2-space that agree on the dense range of a function are equal. -/ theorem dense_range.equalizer {α : Type u_1} {β : Type u_2} {γ : Type u_3} [topological_space β] [topological_space γ] [t2_space γ] {f : α → β} (hfd : dense_range f) {g : β → γ} {h : β → γ} (hg : continuous g) (hh : continuous h) (H : g ∘ f = h ∘ f) : g = h := funext fun (y : β) => dense_range.induction_on hfd y (is_closed_eq hg hh) (congr_fun H) end Mathlib
637fdab3da42db453d4325fb947fa7a02736528a
63abd62053d479eae5abf4951554e1064a4c45b4
/src/data/qpf/multivariate/constructions/cofix.lean
1034d790f87bc09b2852847c1814b5d2764e182a
[ "Apache-2.0" ]
permissive
Lix0120/mathlib
0020745240315ed0e517cbf32e738d8f9811dd80
e14c37827456fc6707f31b4d1d16f1f3a3205e91
refs/heads/master
1,673,102,855,024
1,604,151,044,000
1,604,151,044,000
308,930,245
0
0
Apache-2.0
1,604,164,710,000
1,604,163,547,000
null
UTF-8
Lean
false
false
19,003
lean
/- Copyright (c) 2018 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jeremy Avigad, Simon Hudon -/ import control.functor.multivariate import data.pfunctor.multivariate.basic import data.pfunctor.multivariate.M import data.qpf.multivariate.basic /-! # The final co-algebra of a multivariate qpf is again a qpf. For a `(n+1)`-ary QPF `F (α₀,..,αₙ)`, we take the least fixed point of `F` with regards to its last argument `αₙ`. The result is a `n`-ary functor: `fix F (α₀,..,αₙ₋₁)`. Making `fix F` into a functor allows us to take the fixed point, compose with other functors and take a fixed point again. ## Main definitions * `cofix.mk` - constructor * `cofix.dest - destructor * `cofix.corec` - corecursor: useful for formulating infinite, productive computations * `cofix.bisim` - bisimulation: proof technique to show the equality of possibly infinite values of `cofix F α` ## Implementation notes For `F` a QPF`, we define `cofix F α` in terms of the M-type of the polynomial functor `P` of `F`. We define the relation `Mcongr` and take its quotient as the definition of `cofix F α`. `Mcongr` is taken as the weakest bisimulation on M-type. See [avigad-carneiro-hudon2019] for more details. ## Reference * [Jeremy Avigad, Mario M. Carneiro and Simon Hudon, *Data Types as Quotients of Polynomial Functors*][avigad-carneiro-hudon2019] -/ universe u open_locale mvfunctor namespace mvqpf open typevec mvpfunctor open mvfunctor (liftp liftr) variables {n : ℕ} {F : typevec.{u} (n+1) → Type u} [mvfunctor F] [q : mvqpf F] include q /-- `corecF` is used as a basis for defining the corecursor of `cofix F α`. `corecF` uses corecursion to construct the M-type generated by `q.P` and uses function on `F` as a corecursive step -/ def corecF {α : typevec n} {β : Type*} (g : β → F (α.append1 β)) : β → q.P.M α := M.corec _ (λ x, repr (g x)) theorem corecF_eq {α : typevec n} {β : Type*} (g : β → F (α.append1 β)) (x : β) : M.dest q.P (corecF g x) = append_fun id (corecF g) <$$> repr (g x) := by rw [corecF, M.dest_corec] /-- Characterization of desirable equivalence relations on M-types -/ def is_precongr {α : typevec n} (r : q.P.M α → q.P.M α → Prop) : Prop := ∀ ⦃x y⦄, r x y → abs (append_fun id (quot.mk r) <$$> M.dest q.P x) = abs (append_fun id (quot.mk r) <$$> M.dest q.P y) /-- Equivalence relation on M-types representing a value of type `cofix F` -/ def Mcongr {α : typevec n} (x y : q.P.M α) : Prop := ∃ r, is_precongr r ∧ r x y /-- Greatest fixed point of functor F. The result is a functor with one fewer parameters than the input. For `F a b c` a ternary functor, fix F is a binary functor such that ```lean cofix F a b = F a b (cofix F a b) ``` -/ def cofix (F : typevec (n + 1) → Type u) [mvfunctor F] [q : mvqpf F] (α : typevec n) := quot (@Mcongr _ F _ q α) instance {α : typevec n} [inhabited q.P.A] [Π (i : fin2 n), inhabited (α i)] : inhabited (cofix F α) := ⟨ quot.mk _ (default _) ⟩ /-- maps every element of the W type to a canonical representative -/ def Mrepr {α : typevec n} : q.P.M α → q.P.M α := corecF (abs ∘ M.dest q.P) /-- the map function for the functor `cofix F` -/ def cofix.map {α β : typevec n} (g : α ⟹ β) : cofix F α → cofix F β := quot.lift (λ x : q.P.M α, quot.mk Mcongr (g <$$> x)) begin rintros aa₁ aa₂ ⟨r, pr, ra₁a₂⟩, apply quot.sound, let r' := λ b₁ b₂, ∃ a₁ a₂ : q.P.M α, r a₁ a₂ ∧ b₁ = g <$$> a₁ ∧ b₂ = g <$$> a₂, use r', split, { show is_precongr r', rintros b₁ b₂ ⟨a₁, a₂, ra₁a₂, b₁eq, b₂eq⟩, let u : quot r → quot r' := quot.lift (λ x : q.P.M α, quot.mk r' (g <$$> x)) (by { intros a₁ a₂ ra₁a₂, apply quot.sound, exact ⟨a₁, a₂, ra₁a₂, rfl, rfl⟩ }), have hu : (quot.mk r' ∘ λ x : q.P.M α, g <$$> x) = u ∘ quot.mk r, { ext x, refl }, rw [b₁eq, b₂eq, M.dest_map, M.dest_map, ←q.P.comp_map, ←q.P.comp_map], rw [←append_fun_comp, id_comp, hu, hu, ←comp_id g, append_fun_comp], rw [q.P.comp_map, q.P.comp_map, abs_map, pr ra₁a₂, ←abs_map] }, show r' (g <$$> aa₁) (g <$$> aa₂), from ⟨aa₁, aa₂, ra₁a₂, rfl, rfl⟩ end instance cofix.mvfunctor : mvfunctor (cofix F) := { map := @cofix.map _ _ _ _} /-- Corecursor for `cofix F` -/ def cofix.corec {α : typevec n} {β : Type u} (g : β → F (α.append1 β)) : β → cofix F α := λ x, quot.mk _ (corecF g x) /-- Destructor for `cofix F` -/ def cofix.dest {α : typevec n} : cofix F α → F (α.append1 (cofix F α)) := quot.lift (λ x, append_fun id (quot.mk Mcongr) <$$> (abs (M.dest q.P x))) begin rintros x y ⟨r, pr, rxy⟩, dsimp, have : ∀ x y, r x y → Mcongr x y, { intros x y h, exact ⟨r, pr, h⟩ }, rw [←quot.factor_mk_eq _ _ this], dsimp, conv { to_lhs, rw [append_fun_comp_id, comp_map, ←abs_map, pr rxy, abs_map, ←comp_map, ←append_fun_comp_id] } end /-- Abstraction function for `cofix F α` -/ def cofix.abs {α} : q.P.M α → cofix F α := quot.mk _ /-- Representation function for `cofix F α` -/ def cofix.repr {α} : cofix F α → q.P.M α := M.corec _ $ repr ∘ cofix.dest /-- Corecursor for `cofix F` -/ def cofix.corec'₁ {α : typevec n} {β : Type u} (g : Π {X}, (β → X) → F (α.append1 X)) (x : β) : cofix F α := cofix.corec (λ x, g id) x /-- More flexible corecursor for `cofix F`. Allows the return of a fully formed value instead of making a recursive call -/ def cofix.corec' {α : typevec n} {β : Type u} (g : β → F (α.append1 (cofix F α ⊕ β))) (x : β) : cofix F α := let f : α ::: cofix F α ⟹ α ::: (cofix F α ⊕ β) := id ::: sum.inl in cofix.corec (sum.elim (mvfunctor.map f ∘ cofix.dest) g) (sum.inr x : cofix F α ⊕ β) /-- Corecursor for `cofix F`. The shape allows recursive calls to look like recursive calls. -/ def cofix.corec₁ {α : typevec n} {β : Type u} (g : Π {X}, (cofix F α → X) → (β → X) → β → F (α ::: X)) (x : β) : cofix F α := cofix.corec' (λ x, g sum.inl sum.inr x) x theorem cofix.dest_corec {α : typevec n} {β : Type u} (g : β → F (α.append1 β)) (x : β) : cofix.dest (cofix.corec g x) = append_fun id (cofix.corec g) <$$> g x := begin conv { to_lhs, rw [cofix.dest, cofix.corec] }, dsimp, rw [corecF_eq, abs_map, abs_repr, ←comp_map, ←append_fun_comp], reflexivity end /-- constructor for `cofix F` -/ def cofix.mk {α : typevec n} : F (α.append1 $ cofix F α) → cofix F α := cofix.corec (λ x, append_fun id (λ i : cofix F α, cofix.dest.{u} i) <$$> x) /-! ## Bisimulation principles for `cofix F` The following theorems are bisimulation principles. The general idea is to use a bisimulation relation to prove the equality between specific values of type `cofix F α`. A bisimulation relation `R` for values `x y : cofix F α`: * holds for `x y`: `R x y` * for any values `x y` that satisfy `R`, their root has the same shape and their children can be paired in such a way that they satisfy `R`. -/ private theorem cofix.bisim_aux {α : typevec n} (r : cofix F α → cofix F α → Prop) (h' : ∀ x, r x x) (h : ∀ x y, r x y → append_fun id (quot.mk r) <$$> cofix.dest x = append_fun id (quot.mk r) <$$> cofix.dest y) : ∀ x y, r x y → x = y := begin intro x, apply quot.induction_on x, clear x, intros x y, apply quot.induction_on y, clear y, intros y rxy, apply quot.sound, let r' := λ x y, r (quot.mk _ x) (quot.mk _ y), have : is_precongr r', { intros a b r'ab, have h₀ : append_fun id (quot.mk r ∘ quot.mk Mcongr) <$$> abs (M.dest q.P a) = append_fun id (quot.mk r ∘ quot.mk Mcongr) <$$> abs (M.dest q.P b) := by rw [append_fun_comp_id, comp_map, comp_map]; exact h _ _ r'ab, have h₁ : ∀ u v : q.P.M α, Mcongr u v → quot.mk r' u = quot.mk r' v, { intros u v cuv, apply quot.sound, dsimp [r'], rw quot.sound cuv, apply h' }, let f : quot r → quot r' := quot.lift (quot.lift (quot.mk r') h₁) begin intro c, apply quot.induction_on c, clear c, intros c d, apply quot.induction_on d, clear d, intros d rcd, apply quot.sound, apply rcd end, have : f ∘ quot.mk r ∘ quot.mk Mcongr = quot.mk r' := rfl, rw [←this, append_fun_comp_id, q.P.comp_map, q.P.comp_map, abs_map, abs_map, abs_map, abs_map, h₀] }, refine ⟨r', this, rxy⟩ end /-- Bisimulation principle using `map` and `quot.mk` to match and relate children of two trees. -/ theorem cofix.bisim_rel {α : typevec n} (r : cofix F α → cofix F α → Prop) (h : ∀ x y, r x y → append_fun id (quot.mk r) <$$> cofix.dest x = append_fun id (quot.mk r) <$$> cofix.dest y) : ∀ x y, r x y → x = y := let r' x y := x = y ∨ r x y in begin intros x y rxy, apply cofix.bisim_aux r', { intro x, left, reflexivity }, { intros x y r'xy, cases r'xy, { rw r'xy }, have : ∀ x y, r x y → r' x y := λ x y h, or.inr h, rw ←quot.factor_mk_eq _ _ this, dsimp, rw [append_fun_comp_id, append_fun_comp_id], rw [@comp_map _ _ _ q _ _ _ (append_fun id (quot.mk r)), @comp_map _ _ _ q _ _ _ (append_fun id (quot.mk r))], rw h _ _ r'xy }, right, exact rxy end /-- Bisimulation principle using `liftr` to match and relate children of two trees. -/ theorem cofix.bisim {α : typevec n} (r : cofix F α → cofix F α → Prop) (h : ∀ x y, r x y → liftr (rel_last α r) (cofix.dest x) (cofix.dest y)) : ∀ x y, r x y → x = y := begin apply cofix.bisim_rel, intros x y rxy, rcases (liftr_iff (rel_last α r) _ _).mp (h x y rxy) with ⟨a, f₀, f₁, dxeq, dyeq, h'⟩, rw [dxeq, dyeq, ←abs_map, ←abs_map, mvpfunctor.map_eq, mvpfunctor.map_eq], rw [←split_drop_fun_last_fun f₀, ←split_drop_fun_last_fun f₁], rw [append_fun_comp_split_fun, append_fun_comp_split_fun], rw [id_comp, id_comp], congr' 2 with i j, cases i with _ i; dsimp, { apply quot.sound, apply h' _ j }, { change f₀ _ j = f₁ _ j, apply h' _ j }, end open mvfunctor /-- Bisimulation principle using `liftr'` to match and relate children of two trees. -/ theorem cofix.bisim₂ {α : typevec n} (r : cofix F α → cofix F α → Prop) (h : ∀ x y, r x y → liftr' (rel_last' α r) (cofix.dest x) (cofix.dest y)) : ∀ x y, r x y → x = y := cofix.bisim _ $ by intros; rw ← liftr_last_rel_iff; apply h; assumption /-- Bisimulation principle the values `⟨a,f⟩` of the polynomial functor representing `cofix F α` as well as an invariant `Q : β → Prop` and a state `β` generating the left-hand side and right-hand side of the equality through functions `u v : β → cofix F α` -/ theorem cofix.bisim' {α : typevec n} {β : Type*} (Q : β → Prop) (u v : β → cofix F α) (h : ∀ x, Q x → ∃ a f' f₀ f₁, cofix.dest (u x) = abs ⟨a, q.P.append_contents f' f₀⟩ ∧ cofix.dest (v x) = abs ⟨a, q.P.append_contents f' f₁⟩ ∧ ∀ i, ∃ x', Q x' ∧ f₀ i = u x' ∧ f₁ i = v x') : ∀ x, Q x → u x = v x := λ x Qx, let R := λ w z : cofix F α, ∃ x', Q x' ∧ w = u x' ∧ z = v x' in cofix.bisim R (λ x y ⟨x', Qx', xeq, yeq⟩, begin rcases h x' Qx' with ⟨a, f', f₀, f₁, ux'eq, vx'eq, h'⟩, rw liftr_iff, refine ⟨a, q.P.append_contents f' f₀, q.P.append_contents f' f₁, xeq.symm ▸ ux'eq, yeq.symm ▸ vx'eq, _⟩, intro i, cases i, { apply h' }, { intro j, apply eq.refl }, end) _ _ ⟨x, Qx, rfl, rfl⟩ lemma cofix.mk_dest {α : typevec n} (x : cofix F α) : cofix.mk (cofix.dest x) = x := begin apply cofix.bisim_rel (λ x y : cofix F α, x = cofix.mk (cofix.dest y)) _ _ _ rfl, dsimp, intros x y h, rw h, conv { to_lhs, congr, skip, rw [cofix.mk], rw cofix.dest_corec}, rw [←comp_map, ←append_fun_comp, id_comp], rw [←comp_map, ←append_fun_comp, id_comp, ←cofix.mk], congr' 2 with u, apply quot.sound, refl end lemma cofix.dest_mk {α : typevec n} (x : F (α.append1 $ cofix F α)) : cofix.dest (cofix.mk x) = x := begin have : cofix.mk ∘ cofix.dest = @_root_.id (cofix F α) := funext cofix.mk_dest, rw [cofix.mk, cofix.dest_corec, ←comp_map, ←cofix.mk, ← append_fun_comp, this, id_comp, append_fun_id_id, mvfunctor.id_map] end lemma cofix.ext {α : typevec n} (x y : cofix F α) (h : x.dest = y.dest) : x = y := by rw [← cofix.mk_dest x,h,cofix.mk_dest] lemma cofix.ext_mk {α : typevec n} (x y : F (α ::: cofix F α)) (h : cofix.mk x = cofix.mk y) : x = y := by rw [← cofix.dest_mk x,h,cofix.dest_mk] /-! `liftr_map`, `liftr_map_last` and `liftr_map_last'` are useful for reasoning about the induction step in bisimulation proofs. -/ section liftr_map omit q theorem liftr_map {α β : typevec n} {F' : typevec n → Type u} [mvfunctor F'] [is_lawful_mvfunctor F'] (R : β ⊗ β ⟹ repeat n Prop) (x : F' α) (f g : α ⟹ β) (h : α ⟹ subtype_ R) (hh : subtype_val _ ⊚ h = (f ⊗' g) ⊚ prod.diag) : liftr' R (f <$$> x) (g <$$> x) := begin rw liftr_def, existsi h <$$> x, rw [mvfunctor.map_map,comp_assoc,hh,← comp_assoc,fst_prod_mk,comp_assoc,fst_diag], rw [mvfunctor.map_map,comp_assoc,hh,← comp_assoc,snd_prod_mk,comp_assoc,snd_diag], dsimp [liftr'], split; refl, end open function theorem liftr_map_last [is_lawful_mvfunctor F] {α : typevec n} {ι ι'} (R : ι' → ι' → Prop) (x : F (α ::: ι)) (f g : ι → ι') (hh : ∀ x : ι, R (f x) (g x)) : liftr' (rel_last' _ R) ((id ::: f) <$$> x) ((id ::: g) <$$> x) := let h : ι → { x : ι' × ι' // uncurry R x } := λ x, ⟨ (f x,g x), hh x ⟩ in let b : α ::: ι ⟹ _ := @diag_sub n α ::: h, c : subtype_ α.repeat_eq ::: {x // uncurry R x} ⟹ (λ (i : fin2 (n)), {x // of_repeat (α.rel_last' R i.fs x)}) ::: subtype (uncurry R) := of_subtype _ ::: id in have hh : subtype_val _ ⊚ to_subtype _ ⊚ from_append1_drop_last ⊚ c ⊚ b = (id ::: f ⊗' id ::: g) ⊚ prod.diag, by { dsimp [c,b], apply eq_of_drop_last_eq, { dsimp, simp only [prod_map_id, drop_fun_prod, drop_fun_append_fun, drop_fun_diag, id_comp, drop_fun_to_subtype], erw [to_subtype_of_subtype_assoc,id_comp], clear_except, ext i x : 2, induction i, refl, apply i_ih, }, simp only [h, last_fun_from_append1_drop_last, last_fun_to_subtype, last_fun_append_fun, last_fun_subtype_val, comp.left_id, last_fun_comp, last_fun_prod], dsimp, ext1, refl }, liftr_map _ _ _ _ (to_subtype _ ⊚ from_append1_drop_last ⊚ c ⊚ b) hh theorem liftr_map_last' [is_lawful_mvfunctor F] {α : typevec n} {ι} (R : ι → ι → Prop) (x : F (α ::: ι)) (f : ι → ι) (hh : ∀ x : ι, R (f x) x) : liftr' (rel_last' _ R) ((id ::: f) <$$> x) x := begin have := liftr_map_last R x f id hh, rwa [append_fun_id_id,mvfunctor.id_map] at this, end end liftr_map lemma cofix.abs_repr {α} (x : cofix F α) : quot.mk _ (cofix.repr x) = x := begin let R := λ x y : cofix F α, cofix.abs (cofix.repr y) = x, refine cofix.bisim₂ R _ _ _ rfl, clear x, rintros x y h, dsimp [R] at h, subst h, dsimp [cofix.dest,cofix.abs], induction y using quot.ind, simp only [cofix.repr, M.dest_corec, abs_map, abs_repr], conv { congr, skip, rw cofix.dest }, dsimp, rw [mvfunctor.map_map,mvfunctor.map_map,← append_fun_comp_id,← append_fun_comp_id], let f : α ::: (P F).M α ⟹ subtype_ (α.rel_last' R) := split_fun diag_sub (λ x, ⟨(cofix.abs (cofix.abs x).repr, cofix.abs x),_⟩), refine liftr_map _ _ _ _ f _, { simp only [←append_prod_append_fun, prod_map_id], apply eq_of_drop_last_eq, { dsimp, simp only [drop_fun_diag], erw subtype_val_diag_sub, }, ext1, simp only [cofix.abs, prod.mk.inj_iff, prod_map, function.comp_app, last_fun_append_fun, last_fun_subtype_val, last_fun_comp, last_fun_split_fun], dsimp [drop_fun_rel_last,last_fun,prod.diag], split; refl, }, dsimp [rel_last',split_fun,function.uncurry,R], refl, end section tactic setup_tactic_parser open tactic omit q /-- tactic for proof by bisimulation -/ meta def mv_bisim (e : parse texpr) (ids : parse with_ident_list) : tactic unit := do e ← to_expr e, (expr.pi n bi d b) ← retrieve $ do { generalize e, target }, `(@eq %%t %%l %%r) ← pure b, x ← mk_local_def `n d, v₀ ← mk_local_def `a t, v₁ ← mk_local_def `b t, x₀ ← mk_app ``eq [v₀,l.instantiate_var x], x₁ ← mk_app ``eq [v₁,r.instantiate_var x], xx ← mk_app ``and [x₀,x₁], ex ← lambdas [x] xx, ex ← mk_app ``Exists [ex] >>= lambdas [v₀,v₁], R ← pose `R none ex, refine ``(cofix.bisim₂ %%R _ _ _ ⟨_,rfl,rfl⟩), let f (a b : name) : name := if a = `_ then b else a, let ids := (ids ++ list.repeat `_ 5).zip_with f [`a,`b,`x,`Ha,`Hb], (ids₀,w::ids₁) ← pure $ list.split_at 2 ids, intro_lst ids₀, h ← intro1, [(_,[w,h],_)] ← cases_core h [w], cases h ids₁, pure () run_cmd add_interactive [``mv_bisim] end tactic theorem corec_roll {α : typevec n} {X Y} {x₀ : X} (f : X → Y) (g : Y → F (α ::: X)) : cofix.corec (g ∘ f) x₀ = cofix.corec (mvfunctor.map (id ::: f) ∘ g) (f x₀) := begin mv_bisim x₀, rw [Ha,Hb,cofix.dest_corec,cofix.dest_corec], rw [mvfunctor.map_map,← append_fun_comp_id], refine liftr_map_last _ _ _ _ _, intros a, refine ⟨a,rfl,rfl⟩ end theorem cofix.dest_corec' {α : typevec n} {β : Type u} (g : β → F (α.append1 (cofix F α ⊕ β))) (x : β) : cofix.dest (cofix.corec' g x) = append_fun id (sum.elim id (cofix.corec' g)) <$$> g x := begin rw [cofix.corec',cofix.dest_corec], dsimp, congr' with (i|i); rw corec_roll; dsimp [cofix.corec'], { mv_bisim i, rw [Ha,Hb,cofix.dest_corec], dsimp [(∘)], repeat { rw [mvfunctor.map_map,← append_fun_comp_id] }, apply liftr_map_last', dsimp [(∘),R], intros, exact ⟨_,rfl,rfl⟩ }, { congr' with y, erw [append_fun_id_id], simp [mvfunctor.id_map] }, end theorem cofix.dest_corec₁ {α : typevec n} {β : Type u} (g : Π {X}, (cofix F α → X) → (β → X) → β → F (α.append1 X)) (x : β) (h : ∀ X Y (f : cofix F α → X) (f' : β → X) (k : X → Y), g (k ∘ f) (k ∘ f') x = (id ::: k) <$$> g f f' x) : cofix.dest (cofix.corec₁ @g x) = g id (cofix.corec₁ @g) x := by rw [cofix.corec₁,cofix.dest_corec',← h]; refl instance mvqpf_cofix : mvqpf (cofix F) := { P := q.P.Mp, abs := λ α, quot.mk Mcongr, repr := λ α, cofix.repr, abs_repr := λ α, cofix.abs_repr, abs_map := λ α β g x, rfl } end mvqpf
08702df1144026f1ed114241d1d1b88afd072cf3
36938939954e91f23dec66a02728db08a7acfcf9
/lean4/deps/x86_semantics/src/X86Semantics/BackendAPI.lean
d9f1e77e0690e9e573a8ac5171d84107905d0d6a
[]
no_license
pnwamk/reopt-vcg
f8b56dd0279392a5e1c6aee721be8138e6b558d3
c9f9f185fbefc25c36c4b506bbc85fd1a03c3b6d
refs/heads/master
1,631,145,017,772
1,593,549,019,000
1,593,549,143,000
254,191,418
0
0
null
1,586,377,077,000
1,586,377,077,000
null
UTF-8
Lean
false
false
3,828
lean
import X86Semantics.Common namespace x86 open mc_semantics open mc_semantics.type structure Backend : Type 1 := -- Symbolic types (s_bv : Nat -> Type) (s_bool : Type) -- injections (s_bv_imm (n : Nat) : Nat -> s_bv n) (s_bool_imm : Bool -> s_bool) -- Underlying monad. (monad : Type -> Type) [Monad_backend : Monad monad] [MonadExcept_backend : MonadExcept String monad] -- Memory operations (store_word (n : Nat) (addr : s_bv 64) (b : s_bv (8 * n)) : monad Unit) -- Option here as reading can fail, but this might be total for the smt backend (read_word (addr : s_bv 64) (n : Nat) : monad (s_bv (8 * n))) -- Register operations (get_gpreg : Fin 16 -> monad (s_bv 64)) (set_gpreg : Fin 16 -> s_bv 64 -> monad Unit) (get_flag : Fin 32 -> monad s_bool) (set_flag : Fin 32 -> s_bool -> monad Unit) -- Value operations -- FIXME: should these reside inside a monad? -- Symbolic bool operations -- monomorphic to avoid s_ite b (21 : Nat) (32 : Nat) (s_mux_bool : s_bool -> s_bool -> s_bool -> s_bool) (s_mux_bv {n : Nat} : s_bool -> s_bv n -> s_bv n -> s_bv n) -- Mainly for cond_set, useful for cond jump as well -- FIXME: replace by mux_set_reg etc. (s_mux_m : s_bool -> monad Unit -> monad Unit -> monad Unit) (s_not : s_bool -> s_bool) (s_or : s_bool -> s_bool -> s_bool) (s_and : s_bool -> s_bool -> s_bool) (s_xor : s_bool -> s_bool -> s_bool) -- Symbolic bv operations -- - Comparison (s_bveq {n : Nat} : s_bv n -> s_bv n -> s_bool) (s_bvult {n : Nat} : s_bv n -> s_bv n -> s_bool) (s_bvslt {n : Nat} : s_bv n -> s_bv n -> s_bool) -- - Arithmetic (s_bvneg (n : Nat) : s_bv n -> s_bv n) (s_bvnot (n : Nat) : s_bv n -> s_bv n) (s_bvadd (n : Nat) : s_bv n -> s_bv n -> s_bv n) (s_bvsub (n : Nat) : s_bv n -> s_bv n -> s_bv n) (s_bvmul (n : Nat) : s_bv n -> s_bv n -> s_bv n) (s_bvudiv (n : Nat) : s_bv n -> s_bv n -> s_bv n) (s_bvurem (n : Nat) : s_bv n -> s_bv n -> s_bv n) -- FIXME: add proof args? (s_bvextract (n i j : Nat ) : s_bv n -> s_bv (i + 1 - j)) (s_sext (n m : Nat) : s_bv n -> s_bv m) (s_uext (n m : Nat) : s_bv n -> s_bv m) (s_trunc (n m : Nat) : s_bv n -> s_bv m) (s_bvappend {n : Nat} {m : Nat} : s_bv n -> s_bv m -> s_bv (n + m)) (s_bvgetbits {n : Nat} (off m : Nat) : s_bv n -> s_bv m) (s_bvsetbits {n m : Nat} (off : Nat) : s_bv n -> s_bv m -> s_bv n) (s_bvand (n : Nat) : s_bv n -> s_bv n -> s_bv n) (s_bvor (n : Nat) : s_bv n -> s_bv n -> s_bv n) (s_bvxor (n : Nat) : s_bv n -> s_bv n -> s_bv n) (s_bvshl (n : Nat) : s_bv n -> s_bv n -> s_bv n) (s_bvmsb (n : Nat) : s_bv n -> s_bool) -- unsigned (s_bvlshr {n : Nat} : s_bv n -> s_bv n -> s_bv n) -- signed (s_bvsshr {n : Nat} : s_bv n -> s_bv n -> s_bv n) (s_parity {n : Nat} : s_bv n -> s_bool) (s_bit_test {wr wi : Nat} : s_bv wr -> s_bv wi -> s_bool) -- System operations (s_os_transition : monad Unit) (s_get_ip : monad (s_bv 64)) (s_set_ip : s_bv 64 -> monad Unit) (s_cond_set_ip : s_bool -> s_bv 64 -> monad Unit) -- We could combine this with the above. -- (s_cond_set_ip : s_bool -> s_bv 64 -> monad Unit) (s_read_cpuid : monad Unit) namespace Backend def s_bvzero (be : Backend) (n : Nat) : be.s_bv n := be.s_bv_imm n 0 def machine_word (be : Backend) : Type := be.s_bv 64 def s_bool_eq (be : Backend) (b1 : be.s_bool) (b2 : be.s_bool) := be.s_mux_bool b1 b2 (be.s_not b2) def false (be : Backend) : be.s_bool := be.s_bool_imm false def true (be : Backend) : be.s_bool := be.s_bool_imm true def bit_to_bitvec (be : Backend) (n : Nat) (b : be.s_bool) : be.s_bv n := be.s_mux_bv b (be.s_bv_imm n 1) (be.s_bv_imm n 0) end Backend end x86
a8692358fc3e9429bd5e71679a0d2e664a7cc151
a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940
/stage0/src/Lean/Meta/DiscrTreeTypes.lean
460991be479ce7ad0b3e343ad88f4231fff69823
[ "Apache-2.0" ]
permissive
WojciechKarpiel/lean4
7f89706b8e3c1f942b83a2c91a3a00b05da0e65b
f6e1314fa08293dea66a329e05b6c196a0189163
refs/heads/master
1,686,633,402,214
1,625,821,189,000
1,625,821,258,000
384,640,886
0
0
Apache-2.0
1,625,903,617,000
1,625,903,026,000
null
UTF-8
Lean
false
false
1,089
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 namespace Lean.Meta /- See file `DiscrTree.lean` for the actual implementation and documentation. -/ namespace DiscrTree inductive Key where | const : Name → Nat → Key | fvar : FVarId → Nat → Key | lit : Literal → Key | star : Key | other : Key | arrow : Key deriving Inhabited, BEq protected def Key.hash : Key → UInt64 | Key.const n a => mixHash 5237 $ mixHash (hash n) (hash a) | Key.fvar n a => mixHash 3541 $ mixHash (hash n) (hash a) | Key.lit v => mixHash 1879 $ hash v | Key.star => 7883 | Key.other => 2411 | Key.arrow => 17 instance : Hashable Key := ⟨Key.hash⟩ inductive Trie (α : Type) where | node (vs : Array α) (children : Array (Key × Trie α)) : Trie α end DiscrTree open DiscrTree open Std (PersistentHashMap) structure DiscrTree (α : Type) where root : PersistentHashMap Key (Trie α) := {} end Lean.Meta
63cdcd4cd5a9428fd91ee9d667bc66eebacd46d7
f618aea02cb4104ad34ecf3b9713065cc0d06103
/src/measure_theory/probability_mass_function.lean
4bd6ff08ea280e2e38bcff1a6a2f6dd7b4a01ee8
[ "Apache-2.0" ]
permissive
joehendrix/mathlib
84b6603f6be88a7e4d62f5b1b0cbb523bb82b9a5
c15eab34ad754f9ecd738525cb8b5a870e834ddc
refs/heads/master
1,589,606,591,630
1,555,946,393,000
1,555,946,393,000
182,813,854
0
0
null
1,555,946,309,000
1,555,946,308,000
null
UTF-8
Lean
false
false
4,866
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Johannes Hölzl Probability mass function -- discrete probability measures -/ import topology.instances.nnreal topology.instances.ennreal topology.algebra.infinite_sum noncomputable theory variables {α : Type*} {β : Type*} {γ : Type*} local attribute [instance] classical.prop_decidable local attribute [instance, priority 0] nat.cast_coe /-- Probability mass functions, i.e. discrete probability measures -/ def {u} pmf (α : Type u) : Type u := { f : α → nnreal // has_sum f 1 } namespace pmf instance : has_coe_to_fun (pmf α) := ⟨λp, α → nnreal, λp a, p.1 a⟩ @[extensionality] protected lemma ext : ∀{p q : pmf α}, (∀a, p a = q a) → p = q | ⟨f, hf⟩ ⟨g, hg⟩ eq := subtype.eq $ funext eq lemma has_sum_coe_one (p : pmf α) : has_sum p 1 := p.2 lemma summable_coe (p : pmf α) : summable p := summable_spec p.has_sum_coe_one @[simp] lemma tsum_coe (p : pmf α) : (∑a, p a) = 1 := tsum_eq_has_sum p.has_sum_coe_one def support (p : pmf α) : set α := {a | p.1 a ≠ 0} def pure (a : α) : pmf α := ⟨λa', if a' = a then 1 else 0, has_sum_ite_eq _ _⟩ @[simp] lemma pure_apply (a a' : α) : pure a a' = (if a' = a then 1 else 0) := rfl instance [inhabited α] : inhabited (pmf α) := ⟨pure (default α)⟩ lemma coe_le_one (p : pmf α) (a : α) : p a ≤ 1 := has_sum_le (by intro b; split_ifs; simp [h]; exact le_refl _) (has_sum_ite_eq a (p a)) p.2 protected lemma bind.summable (p : pmf α) (f : α → pmf β) (b : β) : summable (λa:α, p a * f a b) := begin refine nnreal.summable_of_le (assume a, _) p.summable_coe, suffices : p a * f a b ≤ p a * 1, { simpa }, exact mul_le_mul_of_nonneg_left ((f a).coe_le_one _) (p a).2 end def bind (p : pmf α) (f : α → pmf β) : pmf β := ⟨λb, (∑a, p a * f a b), begin simp [ennreal.has_sum_coe.symm, (ennreal.tsum_coe (bind.summable p f _)).symm], rw [has_sum_iff_of_summable ennreal.summable, ennreal.tsum_comm], simp [ennreal.mul_tsum, (ennreal.tsum_coe (f _).summable_coe), ennreal.tsum_coe p.summable_coe] end⟩ @[simp] lemma bind_apply (p : pmf α) (f : α → pmf β) (b : β) : p.bind f b = (∑a, p a * f a b) := rfl lemma coe_bind_apply (p : pmf α) (f : α → pmf β) (b : β) : (p.bind f b : ennreal) = (∑a, p a * f a b) := eq.trans (ennreal.tsum_coe $ bind.summable p f b).symm $ by simp @[simp] lemma pure_bind (a : α) (f : α → pmf β) : (pure a).bind f = f a := have ∀b a', ite (a' = a) 1 0 * f a' b = ite (a' = a) (f a b) 0, from assume b a', by split_ifs; simp; subst h; simp, by ext b; simp [this] @[simp] lemma bind_pure (p : pmf α) : p.bind pure = p := have ∀a a', (p a * ite (a' = a) 1 0) = ite (a = a') (p a') 0, from assume a a', begin split_ifs; try { subst a }; try { subst a' }; simp * at * end, by ext b; simp [this] @[simp] lemma bind_bind (p : pmf α) (f : α → pmf β) (g : β → pmf γ) : (p.bind f).bind g = p.bind (λa, (f a).bind g) := begin ext b, simp only [ennreal.coe_eq_coe.symm, coe_bind_apply, ennreal.mul_tsum.symm, ennreal.tsum_mul.symm], rw [ennreal.tsum_comm], simp [mul_assoc, mul_left_comm, mul_comm] end lemma bind_comm (p : pmf α) (q : pmf β) (f : α → β → pmf γ) : p.bind (λa, q.bind (f a)) = q.bind (λb, p.bind (λa, f a b)) := begin ext b, simp only [ennreal.coe_eq_coe.symm, coe_bind_apply, ennreal.mul_tsum.symm, ennreal.tsum_mul.symm], rw [ennreal.tsum_comm], simp [mul_assoc, mul_left_comm, mul_comm] end def map (f : α → β) (p : pmf α) : pmf β := bind p (pure ∘ f) lemma bind_pure_comp (f : α → β) (p : pmf α) : bind p (pure ∘ f) = map f p := rfl lemma map_id (p : pmf α) : map id p = p := by simp [map] lemma map_comp (p : pmf α) (f : α → β) (g : β → γ) : (p.map f).map g = p.map (g ∘ f) := by simp [map] lemma pure_map (a : α) (f : α → β) : (pure a).map f = pure (f a) := by simp [map] def seq (f : pmf (α → β)) (p : pmf α) : pmf β := f.bind (λm, p.bind $ λa, pure (m a)) def of_multiset (s : multiset α) (hs : s ≠ 0) : pmf α := ⟨λa, s.count a / s.card, have s.to_finset.sum (λa, (s.count a : ℝ) / s.card) = 1, by simp [div_eq_inv_mul, finset.mul_sum.symm, (finset.sum_nat_cast _ _).symm, hs], have s.to_finset.sum (λa, (s.count a : nnreal) / s.card) = 1, by rw [← nnreal.eq_iff, nnreal.coe_one, ← this, nnreal.sum_coe]; simp, begin rw ← this, apply has_sum_sum_of_ne_finset_zero, simp {contextual := tt}, end⟩ def of_fintype [fintype α] (f : α → nnreal) (h : finset.univ.sum f = 1) : pmf α := ⟨f, h ▸ has_sum_sum_of_ne_finset_zero (by simp)⟩ def bernoulli (p : nnreal) (h : p ≤ 1) : pmf bool := of_fintype (λb, cond b p (1 - p)) (nnreal.eq $ by simp [h]) end pmf
919315b7afe25e4dd0098da53eb25139e28aafd4
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/algebraic_geometry/locally_ringed_space_auto.lean
0c65ea965b17898846bbacabecba1cc8307c12d5
[]
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
7,665
lean
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.algebraic_geometry.sheafed_space import Mathlib.algebra.category.CommRing.limits import Mathlib.algebra.category.CommRing.colimits import Mathlib.algebraic_geometry.stalks import Mathlib.ring_theory.ideal.basic import Mathlib.PostPort universes u_1 l u namespace Mathlib /-! # The category of locally ringed spaces We define (bundled) locally ringed spaces (as `SheafedSpace CommRing` along with the fact that the stalks are local rings), and morphisms between these (morphisms in `SheafedSpace` with `is_local_ring_hom` on the stalk maps). ## Future work * Define the restriction along an open embedding -/ namespace algebraic_geometry /-- A `LocallyRingedSpace` is a topological space equipped with a sheaf of commutative rings such that all the stalks are local rings. A morphism of locally ringed spaces is a morphism of ringed spaces such that the morphims induced on stalks are local ring homomorphisms. -/ structure LocallyRingedSpace extends SheafedSpace CommRing where local_ring : ∀ (x : ↥(PresheafedSpace.carrier (SheafedSpace.to_PresheafedSpace _to_SheafedSpace))), local_ring ↥(Top.presheaf.stalk (PresheafedSpace.presheaf (SheafedSpace.to_PresheafedSpace _to_SheafedSpace)) x) namespace LocallyRingedSpace /-- The underlying topological space of a locally ringed space. -/ def to_Top (X : LocallyRingedSpace) : Top := PresheafedSpace.carrier (SheafedSpace.to_PresheafedSpace (to_SheafedSpace X)) protected instance has_coe_to_sort : has_coe_to_sort LocallyRingedSpace := has_coe_to_sort.mk (Type u) fun (X : LocallyRingedSpace) => ↥(to_Top X) -- PROJECT: how about a typeclass "has_structure_sheaf" to mediate the 𝒪 notation, rather -- than defining it over and over for PresheafedSpace, LRS, Scheme, etc. /-- The structure sheaf of a locally ringed space. -/ def 𝒪 (X : LocallyRingedSpace) : Top.sheaf CommRing (to_Top X) := SheafedSpace.sheaf (to_SheafedSpace X) /-- A morphism of locally ringed spaces is a morphism of ringed spaces such that the morphims induced on stalks are local ring homomorphisms. -/ def hom (X : LocallyRingedSpace) (Y : LocallyRingedSpace) := Subtype fun (f : to_SheafedSpace X ⟶ to_SheafedSpace Y) => ∀ (x : ↥(SheafedSpace.to_PresheafedSpace (to_SheafedSpace X))), is_local_ring_hom (PresheafedSpace.stalk_map f x) protected instance category_theory.has_hom : category_theory.has_hom LocallyRingedSpace := category_theory.has_hom.mk hom theorem hom_ext {X : LocallyRingedSpace} {Y : LocallyRingedSpace} (f : hom X Y) (g : hom X Y) (w : subtype.val f = subtype.val g) : f = g := subtype.eq w /-- The stalk of a locally ringed space, just as a `CommRing`. -/ -- TODO perhaps we should make a bundled `LocalRing` and return one here? -- TODO define `sheaf.stalk` so we can write `X.𝒪.stalk` here? def stalk (X : LocallyRingedSpace) (x : ↥X) : CommRing := Top.presheaf.stalk (PresheafedSpace.presheaf (SheafedSpace.to_PresheafedSpace (to_SheafedSpace X))) x /-- A morphism of locally ringed spaces `f : X ⟶ Y` induces a local ring homomorphism from `Y.stalk (f x)` to `X.stalk x` for any `x : X`. -/ def stalk_map {X : LocallyRingedSpace} {Y : LocallyRingedSpace} (f : X ⟶ Y) (x : ↥X) : stalk Y (coe_fn (PresheafedSpace.hom.base (subtype.val f)) x) ⟶ stalk X x := PresheafedSpace.stalk_map (subtype.val f) x protected instance stalk_map.is_local_ring_hom {X : LocallyRingedSpace} {Y : LocallyRingedSpace} (f : X ⟶ Y) (x : ↥X) : is_local_ring_hom (stalk_map f x) := subtype.property f x /-- The identity morphism on a locally ringed space. -/ def id (X : LocallyRingedSpace) : hom X X := { val := 𝟙, property := sorry } protected instance hom.inhabited (X : LocallyRingedSpace) : Inhabited (hom X X) := { default := id X } /-- Composition of morphisms of locally ringed spaces. -/ def comp {X : LocallyRingedSpace} {Y : LocallyRingedSpace} {Z : LocallyRingedSpace} (f : hom X Y) (g : hom Y Z) : hom X Z := { val := subtype.val f ≫ subtype.val g, property := sorry } /-- The category of locally ringed spaces. -/ protected instance category_theory.category : category_theory.category LocallyRingedSpace := category_theory.category.mk /-- The forgetful functor from `LocallyRingedSpace` to `SheafedSpace CommRing`. -/ def forget_to_SheafedSpace : LocallyRingedSpace ⥤ SheafedSpace CommRing := category_theory.functor.mk (fun (X : LocallyRingedSpace) => to_SheafedSpace X) fun (X Y : LocallyRingedSpace) (f : X ⟶ Y) => subtype.val f protected instance forget_to_SheafedSpace.category_theory.faithful : category_theory.faithful forget_to_SheafedSpace := category_theory.faithful.mk -- PROJECT: once we have `PresheafedSpace.restrict_stalk_iso` -- (that restriction doesn't change stalks) we can uncomment this. /- def restrict {U : Top} (X : LocallyRingedSpace) (f : U ⟶ X.to_Top) (h : open_embedding f) : LocallyRingedSpace := { local_ring := begin intro x, dsimp at *, -- We show that the stalk of the restriction is isomorphic to the original stalk, have := X.to_SheafedSpace.to_PresheafedSpace.restrict_stalk_iso f h x, -- and then transfer `local_ring` across the ring equivalence. apply (this.CommRing_iso_to_ring_equiv).local_ring, -- import data.equiv.transfer_instance apply X.local_ring, end, .. X.to_SheafedSpace.restrict _ f h } -/ /-- The global sections, notated Gamma. -/ def Γ : LocallyRingedSpaceᵒᵖ ⥤ CommRing := category_theory.functor.op forget_to_SheafedSpace ⋙ SheafedSpace.Γ theorem Γ_def : Γ = category_theory.functor.op forget_to_SheafedSpace ⋙ SheafedSpace.Γ := rfl @[simp] theorem Γ_obj (X : LocallyRingedSpaceᵒᵖ) : category_theory.functor.obj Γ X = category_theory.functor.obj (PresheafedSpace.presheaf (SheafedSpace.to_PresheafedSpace (to_SheafedSpace (opposite.unop X)))) (opposite.op ⊤) := rfl theorem Γ_obj_op (X : LocallyRingedSpace) : category_theory.functor.obj Γ (opposite.op X) = category_theory.functor.obj (PresheafedSpace.presheaf (SheafedSpace.to_PresheafedSpace (to_SheafedSpace X))) (opposite.op ⊤) := rfl @[simp] theorem Γ_map {X : LocallyRingedSpaceᵒᵖ} {Y : LocallyRingedSpaceᵒᵖ} (f : X ⟶ Y) : category_theory.functor.map Γ f = category_theory.nat_trans.app (PresheafedSpace.hom.c (subtype.val (category_theory.has_hom.hom.unop f))) (opposite.op ⊤) ≫ category_theory.functor.map (PresheafedSpace.presheaf (SheafedSpace.to_PresheafedSpace (to_SheafedSpace (opposite.unop Y)))) (category_theory.has_hom.hom.op (topological_space.opens.le_map_top (PresheafedSpace.hom.base (subtype.val (category_theory.has_hom.hom.unop f))) ⊤)) := rfl theorem Γ_map_op {X : LocallyRingedSpace} {Y : LocallyRingedSpace} (f : X ⟶ Y) : category_theory.functor.map Γ (category_theory.has_hom.hom.op f) = category_theory.nat_trans.app (PresheafedSpace.hom.c (subtype.val f)) (opposite.op ⊤) ≫ category_theory.functor.map (PresheafedSpace.presheaf (SheafedSpace.to_PresheafedSpace (to_SheafedSpace X))) (category_theory.has_hom.hom.op (topological_space.opens.le_map_top (PresheafedSpace.hom.base (subtype.val f)) ⊤)) := rfl end Mathlib
c972dac3c18b6e703cd4acb1c674fd6293d0c029
63abd62053d479eae5abf4951554e1064a4c45b4
/src/analysis/normed_space/add_torsor.lean
aacaca317c67c1bfd8eda5601c24d4370d838bba
[ "Apache-2.0" ]
permissive
Lix0120/mathlib
0020745240315ed0e517cbf32e738d8f9811dd80
e14c37827456fc6707f31b4d1d16f1f3a3205e91
refs/heads/master
1,673,102,855,024
1,604,151,044,000
1,604,151,044,000
308,930,245
0
0
Apache-2.0
1,604,164,710,000
1,604,163,547,000
null
UTF-8
Lean
false
false
13,929
lean
/- Copyright (c) 2020 Joseph Myers. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joseph Myers, Yury Kudryashov. -/ import linear_algebra.affine_space.midpoint import topology.metric_space.isometry import topology.instances.real_vector_space /-! # Torsors of additive normed group actions. This file defines torsors of additive normed group actions, with a metric space structure. The motivating case is Euclidean affine spaces. -/ noncomputable theory open_locale nnreal topological_space open filter /-- A `normed_add_torsor V P` is a torsor of an additive normed group action by a `normed_group V` on points `P`. We bundle the metric space structure and require the distance to be the same as results from the norm (which in fact implies the distance yields a metric space, but bundling just the distance and using an instance for the metric space results in type class problems). -/ class normed_add_torsor (V : out_param $ Type*) (P : Type*) [out_param $ normed_group V] [metric_space P] extends add_torsor V P := (dist_eq_norm' : ∀ (x y : P), dist x y = ∥(x -ᵥ y : V)∥) variables {α V P : Type*} [normed_group V] [metric_space P] [normed_add_torsor V P] include V section variable (V) /-- The distance equals the norm of subtracting two points. In this lemma, it is necessary to have `V` as an explicit argument; otherwise `rw dist_eq_norm_vsub` sometimes doesn't work. -/ lemma dist_eq_norm_vsub (x y : P) : dist x y = ∥(x -ᵥ y)∥ := normed_add_torsor.dist_eq_norm' x y /-- A `normed_group` is a `normed_add_torsor` over itself. -/ @[priority 100] instance normed_group.normed_add_torsor : normed_add_torsor V V := { dist_eq_norm' := dist_eq_norm } end @[simp] lemma dist_vadd_cancel_left (v : V) (x y : P) : dist (v +ᵥ x) (v +ᵥ y) = dist x y := by rw [dist_eq_norm_vsub V, dist_eq_norm_vsub V, vadd_vsub_vadd_cancel_left] @[simp] lemma dist_vadd_cancel_right (v₁ v₂ : V) (x : P) : dist (v₁ +ᵥ x) (v₂ +ᵥ x) = dist v₁ v₂ := by rw [dist_eq_norm_vsub V, dist_eq_norm, vadd_vsub_vadd_cancel_right] @[simp] lemma dist_vadd_left (v : V) (x : P) : dist (v +ᵥ x) x = ∥v∥ := by simp [dist_eq_norm_vsub V _ x] @[simp] lemma dist_vadd_right (v : V) (x : P) : dist x (v +ᵥ x) = ∥v∥ := by rw [dist_comm, dist_vadd_left] @[simp] lemma dist_vsub_cancel_left (x y z : P) : dist (x -ᵥ y) (x -ᵥ z) = dist y z := by rw [dist_eq_norm, vsub_sub_vsub_cancel_left, dist_comm, dist_eq_norm_vsub V] @[simp] lemma dist_vsub_cancel_right (x y z : P) : dist (x -ᵥ z) (y -ᵥ z) = dist x y := by rw [dist_eq_norm, vsub_sub_vsub_cancel_right, dist_eq_norm_vsub V] lemma dist_vadd_vadd_le (v v' : V) (p p' : P) : dist (v +ᵥ p) (v' +ᵥ p') ≤ dist v v' + dist p p' := by simpa using dist_triangle (v +ᵥ p) (v' +ᵥ p) (v' +ᵥ p') lemma dist_vsub_vsub_le (p₁ p₂ p₃ p₄ : P) : dist (p₁ -ᵥ p₂) (p₃ -ᵥ p₄) ≤ dist p₁ p₃ + dist p₂ p₄ := by { rw [dist_eq_norm, vsub_sub_vsub_comm, dist_eq_norm_vsub V, dist_eq_norm_vsub V], exact norm_sub_le _ _ } lemma nndist_vadd_vadd_le (v v' : V) (p p' : P) : nndist (v +ᵥ p) (v' +ᵥ p') ≤ nndist v v' + nndist p p' := by simp only [← nnreal.coe_le_coe, nnreal.coe_add, ← dist_nndist, dist_vadd_vadd_le] lemma nndist_vsub_vsub_le (p₁ p₂ p₃ p₄ : P) : nndist (p₁ -ᵥ p₂) (p₃ -ᵥ p₄) ≤ nndist p₁ p₃ + nndist p₂ p₄ := by simp only [← nnreal.coe_le_coe, nnreal.coe_add, ← dist_nndist, dist_vsub_vsub_le] lemma edist_vadd_vadd_le (v v' : V) (p p' : P) : edist (v +ᵥ p) (v' +ᵥ p') ≤ edist v v' + edist p p' := by { simp only [edist_nndist], apply_mod_cast nndist_vadd_vadd_le } lemma edist_vsub_vsub_le (p₁ p₂ p₃ p₄ : P) : edist (p₁ -ᵥ p₂) (p₃ -ᵥ p₄) ≤ edist p₁ p₃ + edist p₂ p₄ := by { simp only [edist_nndist], apply_mod_cast nndist_vsub_vsub_le } omit V /-- The distance defines a metric space structure on the torsor. This is not an instance because it depends on `V` to define a `metric_space P`. -/ def metric_space_of_normed_group_of_add_torsor (V P : Type*) [normed_group V] [add_torsor V P] : metric_space P := { dist := λ x y, ∥(x -ᵥ y : V)∥, dist_self := λ x, by simp, eq_of_dist_eq_zero := λ x y h, by simpa using h, dist_comm := λ x y, by simp only [←neg_vsub_eq_vsub_rev y x, norm_neg], dist_triangle := begin intros x y z, change ∥x -ᵥ z∥ ≤ ∥x -ᵥ y∥ + ∥y -ᵥ z∥, rw ←vsub_add_vsub_cancel, apply norm_add_le end } include V namespace isometric /-- The map `v ↦ v +ᵥ p` as an isometric equivalence between `V` and `P`. -/ def vadd_const (p : P) : V ≃ᵢ P := ⟨equiv.vadd_const p, isometry_emetric_iff_metric.2 $ λ x₁ x₂, dist_vadd_cancel_right x₁ x₂ p⟩ @[simp] lemma coe_vadd_const (p : P) : ⇑(vadd_const p) = λ v, v +ᵥ p := rfl @[simp] lemma coe_vadd_const_symm (p : P) : ⇑(vadd_const p).symm = λ p', p' -ᵥ p := rfl @[simp] lemma vadd_const_to_equiv (p : P) : (vadd_const p).to_equiv = equiv.vadd_const p := rfl /-- `p' ↦ p -ᵥ p'` as an equivalence. -/ def const_vsub (p : P) : P ≃ᵢ V := ⟨equiv.const_vsub p, isometry_emetric_iff_metric.2 $ λ p₁ p₂, dist_vsub_cancel_left _ _ _⟩ @[simp] lemma coe_const_vsub (p : P) : ⇑(const_vsub p) = (-ᵥ) p := rfl @[simp] lemma coe_const_vsub_symm (p : P) : ⇑(const_vsub p).symm = λ v, -v +ᵥ p := rfl variables (P) /-- The map `p ↦ v +ᵥ p` as an isometric automorphism of `P`. -/ def const_vadd (v : V) : P ≃ᵢ P := ⟨equiv.const_vadd P v, isometry_emetric_iff_metric.2 $ dist_vadd_cancel_left v⟩ @[simp] lemma coe_const_vadd (v : V) : ⇑(const_vadd P v) = (+ᵥ) v := rfl variable (V) @[simp] lemma const_vadd_zero : const_vadd P (0:V) = isometric.refl P := isometric.to_equiv_inj $ equiv.const_vadd_zero V P variables {P V} /-- Point reflection in `x` as an `isometric` homeomorphism. -/ def point_reflection (x : P) : P ≃ᵢ P := (const_vsub x).trans (vadd_const x) lemma point_reflection_apply (x y : P) : point_reflection x y = x -ᵥ y +ᵥ x := rfl @[simp] lemma point_reflection_to_equiv (x : P) : (point_reflection x).to_equiv = equiv.point_reflection x := rfl @[simp] lemma point_reflection_self (x : P) : point_reflection x x = x := equiv.point_reflection_self x lemma point_reflection_involutive (x : P) : function.involutive (point_reflection x : P → P) := equiv.point_reflection_involutive x @[simp] lemma point_reflection_symm (x : P) : (point_reflection x).symm = point_reflection x := to_equiv_inj $ equiv.point_reflection_symm x @[simp] lemma dist_point_reflection_fixed (x y : P) : dist (point_reflection x y) x = dist y x := by rw [← (point_reflection x).dist_eq y x, point_reflection_self] lemma dist_point_reflection_self' (x y : P) : dist (point_reflection x y) y = ∥bit0 (x -ᵥ y)∥ := by rw [point_reflection_apply, dist_eq_norm_vsub V, vadd_vsub_assoc, bit0] lemma dist_point_reflection_self (𝕜 : Type*) [normed_field 𝕜] [normed_space 𝕜 V] (x y : P) : dist (point_reflection x y) y = ∥(2:𝕜)∥ * dist x y := by rw [dist_point_reflection_self', ← two_smul' 𝕜 (x -ᵥ y), norm_smul, ← dist_eq_norm_vsub V] lemma point_reflection_fixed_iff (𝕜 : Type*) [normed_field 𝕜] [normed_space 𝕜 V] [invertible (2:𝕜)] {x y : P} : point_reflection x y = y ↔ y = x := affine_equiv.point_reflection_fixed_iff_of_module 𝕜 variables [normed_space ℝ V] lemma dist_point_reflection_self_real (x y : P) : dist (point_reflection x y) y = 2 * dist x y := by { rw [dist_point_reflection_self ℝ, real.norm_two], apply_instance } @[simp] lemma point_reflection_midpoint_left (x y : P) : point_reflection (midpoint ℝ x y) x = y := affine_equiv.point_reflection_midpoint_left x y @[simp] lemma point_reflection_midpoint_right (x y : P) : point_reflection (midpoint ℝ x y) y = x := affine_equiv.point_reflection_midpoint_right x y end isometric lemma lipschitz_with.vadd [emetric_space α] {f : α → V} {g : α → P} {Kf Kg : ℝ≥0} (hf : lipschitz_with Kf f) (hg : lipschitz_with Kg g) : lipschitz_with (Kf + Kg) (f +ᵥ g) := λ x y, calc edist (f x +ᵥ g x) (f y +ᵥ g y) ≤ edist (f x) (f y) + edist (g x) (g y) : edist_vadd_vadd_le _ _ _ _ ... ≤ Kf * edist x y + Kg * edist x y : add_le_add (hf x y) (hg x y) ... = (Kf + Kg) * edist x y : (add_mul _ _ _).symm lemma lipschitz_with.vsub [emetric_space α] {f g : α → P} {Kf Kg : ℝ≥0} (hf : lipschitz_with Kf f) (hg : lipschitz_with Kg g) : lipschitz_with (Kf + Kg) (f -ᵥ g) := λ x y, calc edist (f x -ᵥ g x) (f y -ᵥ g y) ≤ edist (f x) (f y) + edist (g x) (g y) : edist_vsub_vsub_le _ _ _ _ ... ≤ Kf * edist x y + Kg * edist x y : add_le_add (hf x y) (hg x y) ... = (Kf + Kg) * edist x y : (add_mul _ _ _).symm lemma uniform_continuous_vadd : uniform_continuous (λ x : V × P, x.1 +ᵥ x.2) := (lipschitz_with.prod_fst.vadd lipschitz_with.prod_snd).uniform_continuous lemma uniform_continuous_vsub : uniform_continuous (λ x : P × P, x.1 -ᵥ x.2) := (lipschitz_with.prod_fst.vsub lipschitz_with.prod_snd).uniform_continuous lemma continuous_vadd : continuous (λ x : V × P, x.1 +ᵥ x.2) := uniform_continuous_vadd.continuous lemma continuous_vsub : continuous (λ x : P × P, x.1 -ᵥ x.2) := uniform_continuous_vsub.continuous lemma filter.tendsto.vadd {l : filter α} {f : α → V} {g : α → P} {v : V} {p : P} (hf : tendsto f l (𝓝 v)) (hg : tendsto g l (𝓝 p)) : tendsto (f +ᵥ g) l (𝓝 (v +ᵥ p)) := (continuous_vadd.tendsto (v, p)).comp (hf.prod_mk_nhds hg) lemma filter.tendsto.vsub {l : filter α} {f g : α → P} {x y : P} (hf : tendsto f l (𝓝 x)) (hg : tendsto g l (𝓝 y)) : tendsto (f -ᵥ g) l (𝓝 (x -ᵥ y)) := (continuous_vsub.tendsto (x, y)).comp (hf.prod_mk_nhds hg) section variables [topological_space α] lemma continuous.vadd {f : α → V} {g : α → P} (hf : continuous f) (hg : continuous g) : continuous (f +ᵥ g) := continuous_vadd.comp (hf.prod_mk hg) lemma continuous.vsub {f g : α → P} (hf : continuous f) (hg : continuous g) : continuous (f -ᵥ g) := continuous_vsub.comp (hf.prod_mk hg) lemma continuous_at.vadd {f : α → V} {g : α → P} {x : α} (hf : continuous_at f x) (hg : continuous_at g x) : continuous_at (f +ᵥ g) x := hf.vadd hg lemma continuous_at.vsub {f g : α → P} {x : α} (hf : continuous_at f x) (hg : continuous_at g x) : continuous_at (f -ᵥ g) x := hf.vsub hg lemma continuous_within_at.vadd {f : α → V} {g : α → P} {x : α} {s : set α} (hf : continuous_within_at f s x) (hg : continuous_within_at g s x) : continuous_within_at (f +ᵥ g) s x := hf.vadd hg lemma continuous_within_at.vsub {f g : α → P} {x : α} {s : set α} (hf : continuous_within_at f s x) (hg : continuous_within_at g s x) : continuous_within_at (f -ᵥ g) s x := hf.vsub hg end variables {V' : Type*} {P' : Type*} [normed_group V'] [metric_space P'] [normed_add_torsor V' P'] /-- The map `g` from `V1` to `V2` corresponding to a map `f` from `P1` to `P2`, at a base point `p`, is an isometry if `f` is one. -/ lemma isometry.vadd_vsub {f : P → P'} (hf : isometry f) {p : P} {g : V → V'} (hg : ∀ v, g v = f (v +ᵥ p) -ᵥ f p) : isometry g := begin convert (isometric.vadd_const (f p)).symm.isometry.comp (hf.comp (isometric.vadd_const p).isometry), exact funext hg end section normed_space variables {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 V] open affine_map @[simp] lemma dist_center_homothety (p₁ p₂ : P) (c : 𝕜) : dist p₁ (homothety p₁ c p₂) = ∥c∥ * dist p₁ p₂ := by simp [homothety_def, norm_smul, ← dist_eq_norm_vsub, dist_comm] @[simp] lemma dist_homothety_center (p₁ p₂ : P) (c : 𝕜) : dist (homothety p₁ c p₂) p₁ = ∥c∥ * dist p₁ p₂ := by rw [dist_comm, dist_center_homothety] @[simp] lemma dist_homothety_self (p₁ p₂ : P) (c : 𝕜) : dist (homothety p₁ c p₂) p₂ = ∥1 - c∥ * dist p₁ p₂ := by rw [homothety_eq_line_map, ← line_map_apply_one_sub, ← homothety_eq_line_map, dist_homothety_center, dist_comm] @[simp] lemma dist_self_homothety (p₁ p₂ : P) (c : 𝕜) : dist p₂ (homothety p₁ c p₂) = ∥1 - c∥ * dist p₁ p₂ := by rw [dist_comm, dist_homothety_self] variables [invertible (2:𝕜)] @[simp] lemma dist_left_midpoint (p₁ p₂ : P) : dist p₁ (midpoint 𝕜 p₁ p₂) = ∥(2:𝕜)∥⁻¹ * dist p₁ p₂ := by rw [midpoint, ← homothety_eq_line_map, dist_center_homothety, inv_of_eq_inv, ← normed_field.norm_inv] @[simp] lemma dist_midpoint_left (p₁ p₂ : P) : dist (midpoint 𝕜 p₁ p₂) p₁ = ∥(2:𝕜)∥⁻¹ * dist p₁ p₂ := by rw [dist_comm, dist_left_midpoint] @[simp] lemma dist_midpoint_right (p₁ p₂ : P) : dist (midpoint 𝕜 p₁ p₂) p₂ = ∥(2:𝕜)∥⁻¹ * dist p₁ p₂ := by rw [midpoint_comm, dist_midpoint_left, dist_comm] @[simp] lemma dist_right_midpoint (p₁ p₂ : P) : dist p₂ (midpoint 𝕜 p₁ p₂) = ∥(2:𝕜)∥⁻¹ * dist p₁ p₂ := by rw [dist_comm, dist_midpoint_right] end normed_space variables [normed_space ℝ V] [normed_space ℝ V'] include V' /-- A continuous map between two normed affine spaces is an affine map provided that it sends midpoints to midpoints. -/ def affine_map.of_map_midpoint (f : P → P') (h : ∀ x y, f (midpoint ℝ x y) = midpoint ℝ (f x) (f y)) (hfc : continuous f) : P →ᵃ[ℝ] P' := affine_map.mk' f ↑((add_monoid_hom.of_map_midpoint ℝ ℝ ((affine_equiv.vadd_const ℝ (f $ classical.arbitrary P)).symm ∘ f ∘ (affine_equiv.vadd_const ℝ (classical.arbitrary P))) (by simp) (λ x y, by simp [h])).to_real_linear_map $ by apply_rules [continuous.vadd, continuous.vsub, continuous_const, hfc.comp, continuous_id]) (classical.arbitrary P) (λ p, by simp)
9e8b67e48a48f4fa453c7077ad3c7d5d867ebe4c
69d4931b605e11ca61881fc4f66db50a0a875e39
/src/field_theory/fixed.lean
6ad1c8792f8e18704de5751dae80108f090e14ae
[ "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
12,497
lean
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import algebra.polynomial.group_ring_action import deprecated.subfield import field_theory.normal import field_theory.separable import field_theory.tower import ring_theory.polynomial /-! # Fixed field under a group action. This is the basis of the Fundamental Theorem of Galois Theory. Given a (finite) group `G` that acts on a field `F`, we define `fixed_points G F`, the subfield consisting of elements of `F` fixed_points by every element of `G`. This subfield is then normal and separable, and in addition (TODO) if `G` acts faithfully on `F` then `finrank (fixed_points G F) F = fintype.card G`. ## Main Definitions - `fixed_points G F`, the subfield consisting of elements of `F` fixed_points by every element of `G`, where `G` is a group that acts on `F`. -/ noncomputable theory open_locale classical big_operators open mul_action finset finite_dimensional universes u v w variables (G : Type u) [group G] (F : Type v) [field F] [mul_semiring_action G F] (g : G) instance fixed_by.is_subfield : is_subfield (fixed_by G F g) := { zero_mem := smul_zero g, add_mem := λ x y hx hy, (smul_add g x y).trans $ congr_arg2 _ hx hy, neg_mem := λ x hx, (smul_neg g x).trans $ congr_arg _ hx, one_mem := smul_one g, mul_mem := λ x y hx hy, (smul_mul' g x y).trans $ congr_arg2 _ hx hy, inv_mem := λ x hx, (smul_inv' F g x).trans $ congr_arg _ hx } namespace fixed_points instance : is_subfield (fixed_points G F) := by convert @is_subfield.Inter F _ G (fixed_by G F) _; rw fixed_eq_Inter_fixed_by instance : is_invariant_subring G (fixed_points G F) := { smul_mem := λ g x hx g', by rw [hx, hx] } @[simp] theorem smul (g : G) (x : fixed_points G F) : g • x = x := subtype.eq $ x.2 g -- Why is this so slow? @[simp] theorem smul_polynomial (g : G) (p : polynomial (fixed_points G F)) : g • p = p := polynomial.induction_on p (λ x, by rw [polynomial.smul_C, smul]) (λ p q ihp ihq, by rw [smul_add, ihp, ihq]) (λ n x ih, by rw [smul_mul', polynomial.smul_C, smul, smul_pow, polynomial.smul_X]) instance : algebra (fixed_points G F) F := algebra.of_is_subring _ theorem coe_algebra_map : algebra_map (fixed_points G F) F = is_subring.subtype (fixed_points G F) := rfl lemma linear_independent_smul_of_linear_independent {s : finset F} : linear_independent (fixed_points G F) (λ i : (↑s : set F), (i : F)) → linear_independent F (λ i : (↑s : set F), mul_action.to_fun G F i) := begin refine finset.induction_on s (λ _, linear_independent_empty_type $ λ ⟨x⟩, x.2) (λ a s has ih hs, _), rw coe_insert at hs ⊢, rw linear_independent_insert (mt mem_coe.1 has) at hs, rw linear_independent_insert' (mt mem_coe.1 has), refine ⟨ih hs.1, λ ha, _⟩, rw finsupp.mem_span_iff_total at ha, rcases ha with ⟨l, hl, hla⟩, rw [finsupp.total_apply_of_mem_supported F hl] at hla, suffices : ∀ i ∈ s, l i ∈ fixed_points G F, { replace hla := (sum_apply _ _ (λ i, l i • to_fun G F i)).symm.trans (congr_fun hla 1), simp_rw [pi.smul_apply, to_fun_apply, one_smul] at hla, refine hs.2 (hla ▸ submodule.sum_mem _ (λ c hcs, _)), change (⟨l c, this c hcs⟩ : fixed_points G F) • c ∈ _, exact submodule.smul_mem _ _ (submodule.subset_span $ mem_coe.2 hcs) }, intros i his g, refine eq_of_sub_eq_zero (linear_independent_iff'.1 (ih hs.1) s.attach (λ i, g • l i - l i) _ ⟨i, his⟩ (mem_attach _ _) : _), refine (@sum_attach _ _ s _ (λ i, (g • l i - l i) • (to_fun G F) i)).trans _, ext g', dsimp only, conv_lhs { rw sum_apply, congr, skip, funext, rw [pi.smul_apply, sub_smul, smul_eq_mul] }, rw [sum_sub_distrib, pi.zero_apply, sub_eq_zero], conv_lhs { congr, skip, funext, rw [to_fun_apply, ← mul_inv_cancel_left g g', mul_smul, ← smul_mul', ← to_fun_apply _ x] }, show ∑ x in s, g • (λ y, l y • to_fun G F y) x (g⁻¹ * g') = ∑ x in s, (λ y, l y • to_fun G F y) x g', rw [← smul_sum, ← sum_apply _ _ (λ y, l y • to_fun G F y), ← sum_apply _ _ (λ y, l y • to_fun G F y)], dsimp only, rw [hla, to_fun_apply, to_fun_apply, smul_smul, mul_inv_cancel_left] end variables [fintype G] (x : F) /-- `minpoly G F x` is the minimal polynomial of `(x : F)` over `fixed_points G F`. -/ def minpoly : polynomial (fixed_points G F) := (prod_X_sub_smul G F x).to_subring _ $ λ c hc g, let ⟨n, hc0, hn⟩ := polynomial.mem_frange_iff.1 hc in hn.symm ▸ prod_X_sub_smul.coeff G F x g n namespace minpoly theorem monic : (minpoly G F x).monic := by { simp only [minpoly, polynomial.monic_to_subring], exact prod_X_sub_smul.monic G F x } theorem eval₂ : polynomial.eval₂ (is_subring.subtype $ fixed_points G F) x (minpoly G F x) = 0 := begin rw [← prod_X_sub_smul.eval G F x, polynomial.eval₂_eq_eval_map], simp [minpoly], end theorem ne_one : minpoly G F x ≠ (1 : polynomial (fixed_points G F)) := λ H, have _ := eval₂ G F x, (one_ne_zero : (1 : F) ≠ 0) $ by rwa [H, polynomial.eval₂_one] at this theorem of_eval₂ (f : polynomial (fixed_points G F)) (hf : polynomial.eval₂ (is_subring.subtype $ fixed_points G F) x f = 0) : minpoly G F x ∣ f := begin rw [← polynomial.map_dvd_map' (is_subring.subtype $ fixed_points G F), minpoly, polynomial.map_to_subring, prod_X_sub_smul], refine fintype.prod_dvd_of_coprime (polynomial.pairwise_coprime_X_sub $ mul_action.injective_of_quotient_stabilizer G x) (λ y, quotient_group.induction_on y $ λ g, _), rw [polynomial.dvd_iff_is_root, polynomial.is_root.def, mul_action.of_quotient_stabilizer_mk, polynomial.eval_smul', ← is_invariant_subring.coe_subtype_hom' G (fixed_points G F), ← mul_semiring_action_hom.coe_polynomial, ← mul_semiring_action_hom.map_smul, smul_polynomial, mul_semiring_action_hom.coe_polynomial, is_invariant_subring.coe_subtype_hom', polynomial.eval_map, hf, smul_zero] end /- Why is this so slow? -/ theorem irreducible_aux (f g : polynomial (fixed_points G F)) (hf : f.monic) (hg : g.monic) (hfg : f * g = minpoly G F x) : f = 1 ∨ g = 1 := begin have hf2 : f ∣ minpoly G F x, { rw ← hfg, exact dvd_mul_right _ _ }, have hg2 : g ∣ minpoly G F x, { rw ← hfg, exact dvd_mul_left _ _ }, have := eval₂ G F x, rw [← hfg, polynomial.eval₂_mul, mul_eq_zero] at this, cases this, { right, have hf3 : f = minpoly G F x, { exact polynomial.eq_of_monic_of_associated hf (monic G F x) (associated_of_dvd_dvd hf2 $ @of_eval₂ G _ F _ _ _ x f this) }, rwa [← mul_one (minpoly G F x), hf3, mul_right_inj' (monic G F x).ne_zero] at hfg }, { left, have hg3 : g = minpoly G F x, { exact polynomial.eq_of_monic_of_associated hg (monic G F x) (associated_of_dvd_dvd hg2 $ @of_eval₂ G _ F _ _ _ x g this) }, rwa [← one_mul (minpoly G F x), hg3, mul_left_inj' (monic G F x).ne_zero] at hfg } end theorem irreducible : irreducible (minpoly G F x) := (polynomial.irreducible_of_monic (monic G F x) (ne_one G F x)).2 (irreducible_aux G F x) end minpoly theorem is_integral : is_integral (fixed_points G F) x := ⟨minpoly G F x, minpoly.monic G F x, minpoly.eval₂ G F x⟩ theorem minpoly_eq_minpoly : minpoly G F x = _root_.minpoly (fixed_points G F) x := minpoly.unique' (minpoly.irreducible G F x) (minpoly.eval₂ G F x) (minpoly.monic G F x) instance normal : normal (fixed_points G F) F := ⟨λ x, is_integral G F x, λ x, (polynomial.splits_id_iff_splits _).1 $ by { rw [← minpoly_eq_minpoly, minpoly, coe_algebra_map, polynomial.map_to_subring, prod_X_sub_smul], exact polynomial.splits_prod _ (λ _ _, polynomial.splits_X_sub_C _) }⟩ instance separable : is_separable (fixed_points G F) F := ⟨λ x, is_integral G F x, λ x, by { rw [← minpoly_eq_minpoly, ← polynomial.separable_map (is_subring.subtype (fixed_points G F)), minpoly, polynomial.map_to_subring], exact polynomial.separable_prod_X_sub_C_iff.2 (injective_of_quotient_stabilizer G x) }⟩ lemma dim_le_card : module.rank (fixed_points G F) F ≤ fintype.card G := begin refine dim_le (λ s hs, cardinal.nat_cast_le.1 _), rw [← @dim_fun' F G, ← cardinal.lift_nat_cast.{v (max u v)}, cardinal.finset_card, ← cardinal.lift_id (module.rank F (G → F))], exact linear_independent_le_dim'.{_ _ _ (max u v)} (linear_independent_smul_of_linear_independent G F hs) end instance : finite_dimensional (fixed_points G F) F := is_noetherian.iff_dim_lt_omega.2 $ lt_of_le_of_lt (dim_le_card G F) (cardinal.nat_lt_omega _) lemma finrank_le_card : finrank (fixed_points G F) F ≤ fintype.card G := by exact_mod_cast trans_rel_right (≤) (finrank_eq_dim _ _) (dim_le_card G F) end fixed_points lemma linear_independent_to_linear_map (R : Type u) (A : Type v) (B : Type w) [comm_semiring R] [integral_domain A] [algebra R A] [integral_domain B] [algebra R B] : linear_independent B (alg_hom.to_linear_map : (A →ₐ[R] B) → (A →ₗ[R] B)) := have linear_independent B (linear_map.lto_fun R A B ∘ alg_hom.to_linear_map), from ((linear_independent_monoid_hom A B).comp (coe : (A →ₐ[R] B) → (A →* B)) (λ f g hfg, alg_hom.ext $ monoid_hom.ext_iff.1 hfg) : _), this.of_comp _ lemma cardinal_mk_alg_hom (K : Type u) (V : Type v) (W : Type w) [field K] [field V] [algebra K V] [finite_dimensional K V] [field W] [algebra K W] [finite_dimensional K W] : cardinal.mk (V →ₐ[K] W) ≤ finrank W (V →ₗ[K] W) := cardinal_mk_le_finrank_of_linear_independent $ linear_independent_to_linear_map K V W noncomputable instance alg_hom.fintype (K : Type u) (V : Type v) (W : Type w) [field K] [field V] [algebra K V] [finite_dimensional K V] [field W] [algebra K W] [finite_dimensional K W] : fintype (V →ₐ[K] W) := classical.choice $ cardinal.lt_omega_iff_fintype.1 $ lt_of_le_of_lt (cardinal_mk_alg_hom K V W) (cardinal.nat_lt_omega _) noncomputable instance alg_equiv.fintype (K : Type u) (V : Type v) [field K] [field V] [algebra K V] [finite_dimensional K V] : fintype (V ≃ₐ[K] V) := fintype.of_equiv (V →ₐ[K] V) (alg_equiv_equiv_alg_hom K V).symm lemma finrank_alg_hom (K : Type u) (V : Type v) [field K] [field V] [algebra K V] [finite_dimensional K V] : fintype.card (V →ₐ[K] V) ≤ finrank V (V →ₗ[K] V) := fintype_card_le_finrank_of_linear_independent $ linear_independent_to_linear_map K V V namespace fixed_points /-- Embedding produced from a faithful action. -/ @[simps apply {fully_applied := ff}] def to_alg_hom (G : Type u) (F : Type v) [group G] [field F] [faithful_mul_semiring_action G F] : G ↪ (F →ₐ[fixed_points G F] F) := { to_fun := λ g, { commutes' := λ x, x.2 g, .. mul_semiring_action.to_semiring_hom G F g }, inj' := λ g₁ g₂ hg, to_semiring_hom_injective G F $ ring_hom.ext $ λ x, alg_hom.ext_iff.1 hg x, } lemma to_alg_hom_apply_apply {G : Type u} {F : Type v} [group G] [field F] [faithful_mul_semiring_action G F] (g : G) (x : F) : to_alg_hom G F g x = g • x := rfl theorem finrank_eq_card (G : Type u) (F : Type v) [group G] [field F] [fintype G] [faithful_mul_semiring_action G F] : finrank (fixed_points G F) F = fintype.card G := le_antisymm (fixed_points.finrank_le_card G F) $ calc fintype.card G ≤ fintype.card (F →ₐ[fixed_points G F] F) : fintype.card_le_of_injective _ (to_alg_hom G F).2 ... ≤ finrank F (F →ₗ[fixed_points G F] F) : finrank_alg_hom (fixed_points G F) F ... = finrank (fixed_points G F) F : finrank_linear_map' _ _ _ theorem to_alg_hom_bijective (G : Type u) (F : Type v) [group G] [field F] [fintype G] [faithful_mul_semiring_action G F] : function.bijective (to_alg_hom G F) := begin rw fintype.bijective_iff_injective_and_card, split, { exact (to_alg_hom G F).injective }, { apply le_antisymm, { exact fintype.card_le_of_injective _ (to_alg_hom G F).injective }, { rw ← finrank_eq_card G F, exact has_le.le.trans_eq (finrank_alg_hom _ F) (finrank_linear_map' _ _ _) } }, end /-- Bijection between G and algebra homomorphisms that fix the fixed points -/ def to_alg_hom_equiv (G : Type u) (F : Type v) [group G] [field F] [fintype G] [faithful_mul_semiring_action G F] : G ≃ (F →ₐ[fixed_points G F] F) := function.embedding.equiv_of_surjective (to_alg_hom G F) (to_alg_hom_bijective G F).2 end fixed_points
e2d133ae949dcc174e7b5640f38a9652fad708a2
a047a4718edfa935d17231e9e6ecec8c7b701e05
/src/algebra/module.lean
25e51932742f590f6a7dff9a131bf3ac3818b9bb
[ "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
29,026
lean
/- Copyright (c) 2015 Nathaniel Thomas. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Nathaniel Thomas, Jeremy Avigad, Johannes Hölzl, Mario Carneiro -/ import group_theory.group_action /-! # Modules over a ring In this file we define * `semimodule R M` : an additive commutative monoid `M` is a `semimodule` over a `semiring` `R` if for `r : R` and `x : M` their "scalar multiplication `r • x : M` is defined, and the operation `•` satisfies some natural associativity and distributivity axioms similar to those on a ring. * `module R M` : same as `semimodule R M` but assumes that `R` is a `ring` and `M` is an additive commutative group. * `vector_space k M` : same as `semimodule k M` and `module k M` but assumes that `k` is a `field` and `M` is an additive commutative group. * `linear_map R M M₂`, `M →ₗ[R] M₂` : a linear map between two R-`semimodule`s. * `is_linear_map R f` : predicate saying that `f : M → M₂` is a linear map. * `submodule R M` : a subset of `M` that contains zero and is closed with respect to addition and scalar multiplication. * `subspace k M` : an abbreviation for `submodule` assuming that `k` is a `field`. ## Implementation notes * `vector_space` and `module` are abbreviations for `semimodule R M`. ## TODO * `submodule R M` was written before bundled `submonoid`s, so it does not extend it. ## Tags semimodule, module, vector space, submodule, subspace, linear map -/ open function open_locale big_operators universes u u' v w x y z variables {R : Type u} {k : Type u'} {S : Type v} {M : Type w} {M₂ : Type x} {M₃ : Type y} {ι : Type z} section prio set_option default_priority 100 -- see Note [default priority] /-- A semimodule is a generalization of vector spaces to a scalar semiring. It consists of a scalar semiring `R` and an additive monoid of "vectors" `M`, connected by a "scalar multiplication" operation `r • x : M` (where `r : R` and `x : M`) with some natural associativity and distributivity axioms similar to those on a ring. -/ @[protect_proj] class semimodule (R : Type u) (M : Type v) [semiring R] [add_comm_monoid M] extends distrib_mul_action R M := (add_smul : ∀(r s : R) (x : M), (r + s) • x = r • x + s • x) (zero_smul : ∀x : M, (0 : R) • x = 0) end prio section add_comm_monoid variables [semiring R] [add_comm_monoid M] [semimodule R M] (r s : R) (x y : M) theorem add_smul : (r + s) • x = r • x + s • x := semimodule.add_smul r s x variables (R) @[simp] theorem zero_smul : (0 : R) • x = 0 := semimodule.zero_smul x theorem two_smul : (2 : R) • x = x + x := by rw [bit0, add_smul, one_smul] variable (M) /-- `(•)` as an `add_monoid_hom`. -/ def smul_add_hom : R →+ M →+ M := { to_fun := const_smul_hom M, map_zero' := add_monoid_hom.ext $ λ r, by simp, map_add' := λ x y, add_monoid_hom.ext $ λ r, by simp [add_smul] } variables {R M} @[simp] lemma smul_add_hom_apply (r : R) (x : M) : smul_add_hom R M r x = r • x := rfl lemma semimodule.eq_zero_of_zero_eq_one (zero_eq_one : (0 : R) = 1) : x = 0 := by rw [←one_smul R x, ←zero_eq_one, zero_smul] lemma list.sum_smul {l : list R} {x : M} : l.sum • x = (l.map (λ r, r • x)).sum := ((smul_add_hom R M).flip x).map_list_sum l lemma multiset.sum_smul {l : multiset R} {x : M} : l.sum • x = (l.map (λ r, r • x)).sum := ((smul_add_hom R M).flip x).map_multiset_sum l lemma finset.sum_smul {f : ι → R} {s : finset ι} {x : M} : (∑ i in s, f i) • x = (∑ i in s, (f i) • x) := ((smul_add_hom R M).flip x).map_sum f s end add_comm_monoid section add_comm_group variables (R M) [semiring R] [add_comm_group M] /-- A structure containing most informations as in a semimodule, except the fields `zero_smul` and `smul_zero`. As these fields can be deduced from the other ones when `M` is an `add_comm_group`, this provides a way to construct a semimodule structure by checking less properties, in `semimodule.of_core`. -/ @[nolint has_inhabited_instance] structure semimodule.core extends has_scalar R M := (smul_add : ∀(r : R) (x y : M), r • (x + y) = r • x + r • y) (add_smul : ∀(r s : R) (x : M), (r + s) • x = r • x + s • x) (mul_smul : ∀(r s : R) (x : M), (r * s) • x = r • s • x) (one_smul : ∀x : M, (1 : R) • x = x) variables {R M} /-- Define `semimodule` without proving `zero_smul` and `smul_zero` by using an auxiliary structure `semimodule.core`, when the underlying space is an `add_comm_group`. -/ def semimodule.of_core (H : semimodule.core R M) : semimodule R M := by letI := H.to_has_scalar; exact { zero_smul := λ x, (add_monoid_hom.mk' (λ r : R, r • x) (λ r s, H.add_smul r s x)).map_zero, smul_zero := λ r, (add_monoid_hom.mk' ((•) r) (H.smul_add r)).map_zero, ..H } variable [semimodule R M] @[simp] theorem smul_neg (r : R) (x : M) : r • (-x) = -(r • x) := eq_neg_of_add_eq_zero (by simp [← smul_add]) theorem smul_sub (r : R) (x y : M) : r • (x - y) = r • x - r • y := by simp [smul_add, sub_eq_add_neg]; rw smul_neg end add_comm_group /-- Modules are defined as an `abbreviation` for semimodules, if the base semiring is a ring. (A previous definition made `module` a structure defined to be `semimodule`.) This has as advantage that modules are completely transparent for type class inference, which means that all instances for semimodules are immediately picked up for modules as well. A cosmetic disadvantage is that one can not extend modules as such, in definitions such as `normed_space`. The solution is to extend `semimodule` instead. -/ library_note "module definition" /-- A module is the same as a semimodule, except the scalar semiring is actually a ring. This is the traditional generalization of spaces like `ℤ^n`, which have a natural addition operation and a way to multiply them by elements of a ring, but no multiplication operation between vectors. -/ abbreviation module (R : Type u) (M : Type v) [ring R] [add_comm_group M] := semimodule R M /-- To prove two module structures on a fixed `add_comm_group` agree, it suffices to check the scalar multiplications agree. -/ -- We'll later use this to show `module ℤ M` is a subsingleton. @[ext] lemma module_ext {R : Type*} [ring R] {M : Type*} [add_comm_group M] (P Q : module R M) (w : ∀ (r : R) (m : M), by { haveI := P, exact r • m } = by { haveI := Q, exact r • m }) : P = Q := begin resetI, rcases P with ⟨⟨⟨⟨P⟩⟩⟩⟩, rcases Q with ⟨⟨⟨⟨Q⟩⟩⟩⟩, congr, funext r m, exact w r m, all_goals { apply proof_irrel_heq }, end section module variables [ring R] [add_comm_group M] [module R M] (r s : R) (x y : M) @[simp] theorem neg_smul : -r • x = - (r • x) := eq_neg_of_add_eq_zero (by rw [← add_smul, add_left_neg, zero_smul]) variables (R) theorem neg_one_smul (x : M) : (-1 : R) • x = -x := by simp variables {R} theorem sub_smul (r s : R) (y : M) : (r - s) • y = r • y - s • y := by simp [add_smul, sub_eq_add_neg] theorem smul_eq_zero {R E : Type*} [division_ring R] [add_comm_group E] [module R E] {c : R} {x : E} : c • x = 0 ↔ c = 0 ∨ x = 0 := ⟨λ h, classical.or_iff_not_imp_left.2 $ λ hc, (units.mk0 c hc).smul_eq_zero.1 h, λ h, h.elim (λ hc, hc.symm ▸ zero_smul R x) (λ hx, hx.symm ▸ smul_zero c)⟩ end module section set_option default_priority 910 instance semiring.to_semimodule [semiring R] : semimodule R R := { smul := (*), smul_add := mul_add, add_smul := add_mul, mul_smul := mul_assoc, one_smul := one_mul, zero_smul := zero_mul, smul_zero := mul_zero } end @[simp] lemma smul_eq_mul [semiring R] {a a' : R} : a • a' = a * a' := rfl /-- A ring homomorphism `f : R →+* M` defines a module structure by `r • x = f r * x`. -/ def ring_hom.to_semimodule [semiring R] [semiring S] (f : R →+* S) : semimodule R S := { smul := λ r x, f r * x, smul_add := λ r x y, by unfold has_scalar.smul; rw [mul_add], add_smul := λ r s x, by unfold has_scalar.smul; rw [f.map_add, add_mul], mul_smul := λ r s x, by unfold has_scalar.smul; rw [f.map_mul, mul_assoc], one_smul := λ x, show f 1 * x = _, by rw [f.map_one, one_mul], zero_smul := λ x, show f 0 * x = 0, by rw [f.map_zero, zero_mul], smul_zero := λ r, mul_zero (f r) } /-- A map `f` between semimodules over a semiring is linear if it satisfies the two properties `f (x + y) = f x + f y` and `f (c • x) = c • f x`. The predicate `is_linear_map R f` asserts this property. A bundled version is available with `linear_map`, and should be favored over `is_linear_map` most of the time. -/ structure is_linear_map (R : Type u) {M : Type v} {M₂ : Type w} [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [semimodule R M] [semimodule R M₂] (f : M → M₂) : Prop := (map_add : ∀ x y, f (x + y) = f x + f y) (map_smul : ∀ (c : R) x, f (c • x) = c • f x) /-- A map `f` between semimodules over a semiring is linear if it satisfies the two properties `f (x + y) = f x + f y` and `f (c • x) = c • f x`. Elements of `linear_map R M M₂` (available under the notation `M →ₗ[R] M₂`) are bundled versions of such maps. An unbundled version is available with the predicate `is_linear_map`, but it should be avoided most of the time. -/ structure linear_map (R : Type u) (M : Type v) (M₂ : Type w) [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [semimodule R M] [semimodule R M₂] := (to_fun : M → M₂) (map_add' : ∀x y, to_fun (x + y) = to_fun x + to_fun y) (map_smul' : ∀(c : R) x, to_fun (c • x) = c • to_fun x) infixr ` →ₗ `:25 := linear_map _ notation M ` →ₗ[`:25 R:25 `] `:0 M₂:0 := linear_map R M M₂ namespace linear_map section add_comm_monoid variables [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] section variables [semimodule R M] [semimodule R M₂] instance : has_coe_to_fun (M →ₗ[R] M₂) := ⟨_, to_fun⟩ @[simp] lemma coe_mk (f : M → M₂) (h₁ h₂) : ((linear_map.mk f h₁ h₂ : M →ₗ[R] M₂) : M → M₂) = f := rfl /-- Identity map as a `linear_map` -/ def id : M →ₗ[R] M := ⟨id, λ _ _, rfl, λ _ _, rfl⟩ @[simp] lemma id_apply (x : M) : @id R M _ _ _ x = x := rfl end section -- We can infer the module structure implicitly from the linear maps, -- rather than via typeclass resolution. variables {semimodule_M : semimodule R M} {semimodule_M₂ : semimodule R M₂} variables (f g : M →ₗ[R] M₂) @[simp] lemma to_fun_eq_coe : f.to_fun = ⇑f := rfl theorem is_linear : is_linear_map R f := ⟨f.2, f.3⟩ variables {f g} @[ext] theorem ext (H : ∀ x, f x = g x) : f = g := by cases f; cases g; congr'; exact funext H lemma coe_fn_congr : Π {x x' : M}, x = x' → f x = f x' | _ _ rfl := rfl theorem ext_iff : f = g ↔ ∀ x, f x = g x := ⟨by { rintro rfl x, refl } , ext⟩ variables (f g) @[simp] lemma map_add (x y : M) : f (x + y) = f x + f y := f.map_add' x y @[simp] lemma map_smul (c : R) (x : M) : f (c • x) = c • f x := f.map_smul' c x @[simp] lemma map_zero : f 0 = 0 := by rw [← zero_smul R, map_smul f 0 0, zero_smul] instance : is_add_monoid_hom f := { map_add := map_add f, map_zero := map_zero f } /-- convert a linear map to an additive map -/ def to_add_monoid_hom : M →+ M₂ := { to_fun := f, map_zero' := f.map_zero, map_add' := f.map_add } @[simp] lemma to_add_monoid_hom_coe : ((f.to_add_monoid_hom) : M → M₂) = f := rfl @[simp] lemma map_sum {ι} {t : finset ι} {g : ι → M} : f (∑ i in t, g i) = (∑ i in t, f (g i)) := f.to_add_monoid_hom.map_sum _ _ end section variables {semimodule_M : semimodule R M} {semimodule_M₂ : semimodule R M₂} {semimodule_M₃ : semimodule R M₃} variables (f : M₂ →ₗ[R] M₃) (g : M →ₗ[R] M₂) /-- Composition of two linear maps is a linear map -/ def comp : M →ₗ[R] M₃ := ⟨f ∘ g, by simp, by simp⟩ @[simp] lemma comp_apply (x : M) : f.comp g x = f (g x) := rfl end end add_comm_monoid section add_comm_group variables [semiring R] [add_comm_group M] [add_comm_group M₂] variables {semimodule_M : semimodule R M} {semimodule_M₂ : semimodule R M₂} variables (f : M →ₗ[R] M₂) @[simp] lemma map_neg (x : M) : f (- x) = - f x := f.to_add_monoid_hom.map_neg x @[simp] lemma map_sub (x y : M) : f (x - y) = f x - f y := f.to_add_monoid_hom.map_sub x y instance : is_add_group_hom f := { map_add := map_add f} end add_comm_group end linear_map namespace is_linear_map section add_comm_monoid variables [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] variables [semimodule R M] [semimodule R M₂] include R /-- Convert an `is_linear_map` predicate to a `linear_map` -/ def mk' (f : M → M₂) (H : is_linear_map R f) : M →ₗ M₂ := ⟨f, H.1, H.2⟩ @[simp] theorem mk'_apply {f : M → M₂} (H : is_linear_map R f) (x : M) : mk' f H x = f x := rfl lemma is_linear_map_smul {R M : Type*} [comm_semiring R] [add_comm_monoid M] [semimodule R M] (c : R) : is_linear_map R (λ (z : M), c • z) := begin refine is_linear_map.mk (smul_add c) _, intros _ _, simp only [smul_smul, mul_comm] end --TODO: move lemma is_linear_map_smul' {R M : Type*} [semiring R] [add_comm_monoid M] [semimodule R M] (a : M) : is_linear_map R (λ (c : R), c • a) := is_linear_map.mk (λ x y, add_smul x y a) (λ x y, mul_smul x y a) variables {f : M → M₂} (lin : is_linear_map R f) include M M₂ lin lemma map_zero : f (0 : M) = (0 : M₂) := (lin.mk' f).map_zero end add_comm_monoid section add_comm_group variables [semiring R] [add_comm_group M] [add_comm_group M₂] variables [semimodule R M] [semimodule R M₂] include R lemma is_linear_map_neg : is_linear_map R (λ (z : M), -z) := is_linear_map.mk neg_add (λ x y, (smul_neg x y).symm) variables {f : M → M₂} (lin : is_linear_map R f) include M M₂ lin lemma map_neg (x : M) : f (- x) = - f x := (lin.mk' f).map_neg x lemma map_sub (x y) : f (x - y) = f x - f y := (lin.mk' f).map_sub x y end add_comm_group end is_linear_map /-- Ring of linear endomorphismsms of a module. -/ abbreviation module.End (R : Type u) (M : Type v) [semiring R] [add_comm_monoid M] [semimodule R M] := M →ₗ[R] M set_option old_structure_cmd true /-- A submodule of a module is one which is closed under vector operations. This is a sufficient condition for the subset of vectors in the submodule to themselves form a module. -/ structure submodule (R : Type u) (M : Type v) [semiring R] [add_comm_monoid M] [semimodule R M] extends add_submonoid M : Type v := (smul_mem' : ∀ (c:R) {x}, x ∈ carrier → c • x ∈ carrier) /-- Reinterpret a `submodule` as an `add_submonoid`. -/ add_decl_doc submodule.to_add_submonoid namespace submodule variables [semiring R] [add_comm_monoid M] [semimodule R M] instance : has_coe_t (submodule R M) (set M) := ⟨λ s, s.carrier⟩ instance : has_mem M (submodule R M) := ⟨λ x p, x ∈ (p : set M)⟩ instance : has_coe_to_sort (submodule R M) := ⟨_, λ p, {x : M // x ∈ p}⟩ variables (p q : submodule R M) @[simp, norm_cast] theorem coe_sort_coe : ↥(p : set M) = p := rfl variables {p q} protected theorem «exists» {q : p → Prop} : (∃ x, q x) ↔ (∃ x ∈ p, q ⟨x, ‹_›⟩) := set_coe.exists protected theorem «forall» {q : p → Prop} : (∀ x, q x) ↔ (∀ x ∈ p, q ⟨x, ‹_›⟩) := set_coe.forall theorem ext' : injective (coe : submodule R M → set M) := λ p q h, by cases p; cases q; congr' @[simp, norm_cast] theorem coe_set_eq : (p : set M) = q ↔ p = q := ext'.eq_iff theorem ext'_iff : p = q ↔ (p : set M) = q := coe_set_eq.symm @[ext] theorem ext (h : ∀ x, x ∈ p ↔ x ∈ q) : p = q := ext' $ set.ext h theorem to_add_submonoid_injective : injective (to_add_submonoid : submodule R M → add_submonoid M) := λ p q h, ext'_iff.2 $ add_submonoid.ext'_iff.1 h @[simp] theorem to_add_submonoid_eq : p.to_add_submonoid = q.to_add_submonoid ↔ p = q := to_add_submonoid_injective.eq_iff end submodule namespace submodule section add_comm_monoid variables [semiring R] [add_comm_monoid M] -- We can infer the module structure implicitly from the bundled submodule, -- rather than via typeclass resolution. variables {semimodule_M : semimodule R M} variables {p q : submodule R M} variables {r : R} {x y : M} variables (p) @[simp] theorem mem_coe : x ∈ (p : set M) ↔ x ∈ p := iff.rfl @[simp] lemma zero_mem : (0 : M) ∈ p := p.zero_mem' lemma add_mem (h₁ : x ∈ p) (h₂ : y ∈ p) : x + y ∈ p := p.add_mem' h₁ h₂ lemma smul_mem (r : R) (h : x ∈ p) : r • x ∈ p := p.smul_mem' r h lemma sum_mem {t : finset ι} {f : ι → M} : (∀c∈t, f c ∈ p) → (∑ i in t, f i) ∈ p := p.to_add_submonoid.sum_mem @[simp] lemma smul_mem_iff' (u : units R) : (u:R) • x ∈ p ↔ x ∈ p := ⟨λ h, by simpa only [smul_smul, u.inv_mul, one_smul] using p.smul_mem ↑u⁻¹ h, p.smul_mem u⟩ instance : has_add p := ⟨λx y, ⟨x.1 + y.1, add_mem _ x.2 y.2⟩⟩ instance : has_zero p := ⟨⟨0, zero_mem _⟩⟩ instance : inhabited p := ⟨0⟩ instance : has_scalar R p := ⟨λ c x, ⟨c • x.1, smul_mem _ c x.2⟩⟩ @[simp] lemma mk_eq_zero {x} (h : x ∈ p) : (⟨x, h⟩ : p) = 0 ↔ x = 0 := subtype.ext variables {p} @[simp, norm_cast] lemma coe_eq_coe {x y : p} : (x : M) = y ↔ x = y := subtype.ext.symm @[simp, norm_cast] lemma coe_eq_zero {x : p} : (x : M) = 0 ↔ x = 0 := @coe_eq_coe _ _ _ _ _ _ x 0 @[simp, norm_cast] lemma coe_add (x y : p) : (↑(x + y) : M) = ↑x + ↑y := rfl @[simp, norm_cast] lemma coe_zero : ((0 : p) : M) = 0 := rfl @[simp, norm_cast] lemma coe_smul (r : R) (x : p) : ((r • x : p) : M) = r • ↑x := rfl @[simp, norm_cast] lemma coe_mk (x : M) (hx : x ∈ p) : ((⟨x, hx⟩ : p) : M) = x := rfl @[simp] lemma coe_mem (x : p) : (x : M) ∈ p := x.2 @[simp] protected lemma eta (x : p) (hx : (x : M) ∈ p) : (⟨x, hx⟩ : p) = x := subtype.eta x hx variables (p) instance : add_comm_monoid p := { add := (+), zero := 0, .. p.to_add_submonoid.to_add_comm_monoid } instance : semimodule R p := by refine {smul := (•), ..}; { intros, apply set_coe.ext, simp [smul_add, add_smul, mul_smul] } /-- Embedding of a submodule `p` to the ambient space `M`. -/ protected def subtype : p →ₗ[R] M := by refine {to_fun := coe, ..}; simp [coe_smul] @[simp] theorem subtype_apply (x : p) : p.subtype x = x := rfl lemma subtype_eq_val : ((submodule.subtype p) : p → M) = subtype.val := rfl end add_comm_monoid section add_comm_group variables [ring R] [add_comm_group M] variables {semimodule_M : semimodule R M} variables (p p' : submodule R M) variables {r : R} {x y : M} lemma neg_mem (hx : x ∈ p) : -x ∈ p := by rw ← neg_one_smul R; exact p.smul_mem _ hx /-- Reinterpret a submodule as an additive subgroup. -/ def to_add_subgroup : add_subgroup M := { neg_mem' := λ _, p.neg_mem , .. p.to_add_submonoid } @[simp] lemma coe_to_add_subgroup : (p.to_add_subgroup : set M) = p := rfl lemma sub_mem (hx : x ∈ p) (hy : y ∈ p) : x - y ∈ p := p.add_mem hx (p.neg_mem hy) @[simp] lemma neg_mem_iff : -x ∈ p ↔ x ∈ p := p.to_add_subgroup.neg_mem_iff lemma add_mem_iff_left (h₁ : y ∈ p) : x + y ∈ p ↔ x ∈ p := ⟨λ h₂, by simpa using sub_mem _ h₂ h₁, λ h₂, add_mem _ h₂ h₁⟩ lemma add_mem_iff_right (h₁ : x ∈ p) : x + y ∈ p ↔ y ∈ p := ⟨λ h₂, by simpa using sub_mem _ h₂ h₁, add_mem _ h₁⟩ instance : has_neg p := ⟨λx, ⟨-x.1, neg_mem _ x.2⟩⟩ @[simp, norm_cast] lemma coe_neg (x : p) : ((-x : p) : M) = -x := rfl instance : add_comm_group p := { add := (+), zero := 0, neg := has_neg.neg, ..p.to_add_subgroup.to_add_comm_group } @[simp, norm_cast] lemma coe_sub (x y : p) : (↑(x - y) : M) = ↑x - ↑y := rfl end add_comm_group end submodule -- TODO: Do we want one-sided ideals? /-- Ideal in a commutative ring is an additive subgroup `s` such that `a * b ∈ s` whenever `b ∈ s`. We define `ideal R` as `submodule R R`. -/ @[reducible] def ideal (R : Type u) [comm_ring R] := submodule R R namespace ideal variables [comm_ring R] (I : ideal R) {a b : R} protected lemma zero_mem : (0 : R) ∈ I := I.zero_mem protected lemma add_mem : a ∈ I → b ∈ I → a + b ∈ I := I.add_mem lemma neg_mem_iff : -a ∈ I ↔ a ∈ I := I.neg_mem_iff lemma add_mem_iff_left : b ∈ I → (a + b ∈ I ↔ a ∈ I) := I.add_mem_iff_left lemma add_mem_iff_right : a ∈ I → (a + b ∈ I ↔ b ∈ I) := I.add_mem_iff_right protected lemma sub_mem : a ∈ I → b ∈ I → a - b ∈ I := I.sub_mem lemma mul_mem_left : b ∈ I → a * b ∈ I := I.smul_mem _ lemma mul_mem_right (h : a ∈ I) : a * b ∈ I := mul_comm b a ▸ I.mul_mem_left h end ideal /-- Vector spaces are defined as an `abbreviation` for semimodules, if the base ring is a field. (A previous definition made `vector_space` a structure defined to be `module`.) This has as advantage that vector spaces are completely transparent for type class inference, which means that all instances for semimodules are immediately picked up for vector spaces as well. A cosmetic disadvantage is that one can not extend vector spaces as such, in definitions such as `normed_space`. The solution is to extend `semimodule` instead. -/ library_note "vector space definition" /-- A vector space is the same as a module, except the scalar ring is actually a field. (This adds commutativity of the multiplication and existence of inverses.) This is the traditional generalization of spaces like `ℝ^n`, which have a natural addition operation and a way to multiply them by real numbers, but no multiplication operation between vectors. -/ abbreviation vector_space (R : Type u) (M : Type v) [field R] [add_comm_group M] := semimodule R M /-- Subspace of a vector space. Defined to equal `submodule`. -/ abbreviation subspace (R : Type u) (M : Type v) [field R] [add_comm_group M] [vector_space R M] := submodule R M namespace submodule variables [division_ring R] [add_comm_group M] [module R M] variables (p : submodule R M) {r : R} {x y : M} theorem smul_mem_iff (r0 : r ≠ 0) : r • x ∈ p ↔ x ∈ p := p.smul_mem_iff' (units.mk0 r r0) end submodule namespace add_comm_monoid open add_monoid variables [add_comm_monoid M] /-- The natural ℕ-semimodule structure on any `add_comm_monoid`. -/ -- We don't make this a global instance, as it results in too many instances, -- and confusing ambiguity in the notation `n • x` when `n : ℕ`. def nat_semimodule : semimodule ℕ M := { smul := nsmul, smul_add := λ _ _ _, nsmul_add _ _ _, add_smul := λ _ _ _, add_nsmul _ _ _, mul_smul := λ _ _ _, mul_nsmul _ _ _, one_smul := one_nsmul, zero_smul := zero_nsmul, smul_zero := nsmul_zero } end add_comm_monoid namespace add_comm_group variables [add_comm_group M] /-- The natural ℤ-module structure on any `add_comm_group`. -/ -- We don't immediately make this a global instance, as it results in too many instances, -- and confusing ambiguity in the notation `n • x` when `n : ℤ`. -- We do turn it into a global instance, but only at the end of this file, -- and I remain dubious whether this is a good idea. def int_module : module ℤ M := { smul := gsmul, smul_add := λ _ _ _, gsmul_add _ _ _, add_smul := λ _ _ _, add_gsmul _ _ _, mul_smul := λ _ _ _, gsmul_mul _ _ _, one_smul := one_gsmul, zero_smul := zero_gsmul, smul_zero := gsmul_zero } instance : subsingleton (module ℤ M) := begin split, intros P Q, ext, -- isn't that lovely: `r • m = r • m` have one_smul : by { haveI := P, exact (1 : ℤ) • m } = by { haveI := Q, exact (1 : ℤ) • m }, begin rw [@one_smul ℤ _ _ (by { haveI := P, apply_instance, }) m], rw [@one_smul ℤ _ _ (by { haveI := Q, apply_instance, }) m], end, have nat_smul : ∀ n : ℕ, by { haveI := P, exact (n : ℤ) • m } = by { haveI := Q, exact (n : ℤ) • m }, begin intro n, induction n with n ih, { erw [zero_smul, zero_smul], }, { rw [int.coe_nat_succ, add_smul, add_smul], erw ih, rw [one_smul], } end, cases r, { rw [int.of_nat_eq_coe, nat_smul], }, { rw [int.neg_succ_of_nat_coe, neg_smul, neg_smul, nat_smul], } end end add_comm_group section local attribute [instance] add_comm_monoid.nat_semimodule lemma semimodule.smul_eq_smul (R : Type*) [semiring R] {M : Type*} [add_comm_monoid M] [semimodule R M] (n : ℕ) (b : M) : n • b = (n : R) • b := begin induction n with n ih, { rw [nat.cast_zero, zero_smul, zero_smul] }, { change (n + 1) • b = (n + 1 : R) • b, rw [add_smul, add_smul, one_smul, ih, one_smul] } end lemma semimodule.nsmul_eq_smul (R : Type*) [semiring R] {M : Type*} [add_comm_monoid M] [semimodule R M] (n : ℕ) (b : M) : n •ℕ b = (n : R) • b := semimodule.smul_eq_smul R n b lemma nat.smul_def {M : Type*} [add_comm_monoid M] (n : ℕ) (x : M) : n • x = n •ℕ x := rfl end section local attribute [instance] add_comm_group.int_module lemma gsmul_eq_smul {M : Type*} [add_comm_group M] (n : ℤ) (x : M) : gsmul n x = n • x := rfl lemma module.gsmul_eq_smul_cast (R : Type*) [ring R] {M : Type*} [add_comm_group M] [module R M] (n : ℤ) (b : M) : gsmul n b = (n : R) • b := begin cases n, { apply semimodule.nsmul_eq_smul, }, { dsimp, rw semimodule.nsmul_eq_smul R, push_cast, rw neg_smul, } end lemma module.gsmul_eq_smul {M : Type*} [add_comm_group M] [module ℤ M] (n : ℤ) (b : M) : gsmul n b = n • b := by rw [module.gsmul_eq_smul_cast ℤ, int.cast_id] end -- We prove this without using the `add_comm_group.int_module` instance, so the `•`s here -- come from whatever the local `module ℤ` structure actually is. lemma add_monoid_hom.map_int_module_smul [add_comm_group M] [add_comm_group M₂] [module ℤ M] [module ℤ M₂] (f : M →+ M₂) (x : ℤ) (a : M) : f (x • a) = x • f a := by simp only [← module.gsmul_eq_smul, f.map_gsmul] lemma add_monoid_hom.map_int_cast_smul [ring R] [add_comm_group M] [add_comm_group M₂] [module R M] [module R M₂] (f : M →+ M₂) (x : ℤ) (a : M) : f ((x : R) • a) = (x : R) • f a := by simp only [← module.gsmul_eq_smul_cast, f.map_gsmul] lemma add_monoid_hom.map_nat_cast_smul [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [semimodule R M] [semimodule R M₂] (f : M →+ M₂) (x : ℕ) (a : M) : f ((x : R) • a) = (x : R) • f a := by simp only [← semimodule.nsmul_eq_smul, f.map_nsmul] lemma add_monoid_hom.map_rat_cast_smul {R : Type*} [division_ring R] [char_zero R] {E : Type*} [add_comm_group E] [module R E] {F : Type*} [add_comm_group F] [module R F] (f : E →+ F) (c : ℚ) (x : E) : f ((c : R) • x) = (c : R) • f x := begin have : ∀ (x : E) (n : ℕ), 0 < n → f (((n⁻¹ : ℚ) : R) • x) = ((n⁻¹ : ℚ) : R) • f x, { intros x n hn, replace hn : (n : R) ≠ 0 := nat.cast_ne_zero.2 (ne_of_gt hn), conv_rhs { congr, skip, rw [← one_smul R x, ← mul_inv_cancel hn, mul_smul] }, rw [f.map_nat_cast_smul, smul_smul, rat.cast_inv, rat.cast_coe_nat, inv_mul_cancel hn, one_smul] }, refine c.num_denom_cases_on (λ m n hn hmn, _), rw [rat.mk_eq_div, div_eq_mul_inv, rat.cast_mul, int.cast_coe_nat, mul_smul, mul_smul, rat.cast_coe_int, f.map_int_cast_smul, this _ n hn] end lemma add_monoid_hom.map_rat_module_smul {E : Type*} [add_comm_group E] [vector_space ℚ E] {F : Type*} [add_comm_group F] [module ℚ F] (f : E →+ F) (c : ℚ) (x : E) : f (c • x) = c • f x := rat.cast_id c ▸ f.map_rat_cast_smul c x -- We finally turn on these instances globally: attribute [instance] add_comm_monoid.nat_semimodule add_comm_group.int_module /-- Reinterpret an additive homomorphism as a `ℤ`-linear map. -/ def add_monoid_hom.to_int_linear_map [add_comm_group M] [add_comm_group M₂] (f : M →+ M₂) : M →ₗ[ℤ] M₂ := ⟨f, f.map_add, f.map_int_module_smul⟩ /-- Reinterpret an additive homomorphism as a `ℚ`-linear map. -/ def add_monoid_hom.to_rat_linear_map [add_comm_group M] [vector_space ℚ M] [add_comm_group M₂] [vector_space ℚ M₂] (f : M →+ M₂) : M →ₗ[ℚ] M₂ := ⟨f, f.map_add, f.map_rat_module_smul⟩ namespace finset variable (R) lemma sum_const' [semiring R] [add_comm_monoid M] [semimodule R M] {s : finset ι} (b : M) : (∑ i in s, b) = (finset.card s : R) • b := by rw [finset.sum_const, ← semimodule.smul_eq_smul]; refl variables {R} [decidable_linear_ordered_cancel_add_comm_monoid M] {s : finset ι} (f : ι → M) theorem exists_card_smul_le_sum (hs : s.nonempty) : ∃ i ∈ s, s.card • f i ≤ (∑ i in s, f i) := exists_le_of_sum_le hs $ by rw [sum_const, ← nat.smul_def, smul_sum] theorem exists_card_smul_ge_sum (hs : s.nonempty) : ∃ i ∈ s, (∑ i in s, f i) ≤ s.card • f i := exists_le_of_sum_le hs $ by rw [sum_const, ← nat.smul_def, smul_sum] end finset
dd45890fd04a3ec3e3ba994f9875df4e114639e3
957a80ea22c5abb4f4670b250d55534d9db99108
/tests/lean/run/dep_coe_to_fn.lean
95a69eaa360a2129007b8cffd1a5c7192c93745d
[ "Apache-2.0" ]
permissive
GaloisInc/lean
aa1e64d604051e602fcf4610061314b9a37ab8cd
f1ec117a24459b59c6ff9e56a1d09d9e9e60a6c0
refs/heads/master
1,592,202,909,807
1,504,624,387,000
1,504,624,387,000
75,319,626
2
1
Apache-2.0
1,539,290,164,000
1,480,616,104,000
C++
UTF-8
Lean
false
false
253
lean
universe variables u v structure Func := (A : Type u) (B : Type v) (fn : A → B → A) instance F_to_fn : has_coe_to_fun Func := { F := λ f, f^.A → f^.B → f^.A, coe := λ f, f^.fn } variables (f : Func) (a : f^.A) (b : f^.B) #check (f a b)
f7c4637673f2bccc303b1d23e33c999b6b796a69
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/category_theory/sigma/basic.lean
95ddbc6910bd198380f64a992446d11bb8b1aa48
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
11,615
lean
/- Copyright (c) 2020 Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.category_theory.natural_isomorphism import Mathlib.category_theory.eq_to_hom import Mathlib.data.sigma.basic import Mathlib.category_theory.pi.basic import Mathlib.PostPort universes w₁ v₁ u₁ l u₂ v₂ w₂ w₃ namespace Mathlib /-! # Disjoint union of categories We define the category structure on a sigma-type (disjoint union) of categories. -/ namespace category_theory namespace sigma /-- The type of morphisms of a disjoint union of categories: for `X : C i` and `Y : C j`, a morphism `(i, X) ⟶ (j, Y)` if `i = j` is just a morphism `X ⟶ Y`, and if `i ≠ j` there are no such morphisms. -/ inductive sigma_hom {I : Type w₁} {C : I → Type u₁} [(i : I) → category (C i)] : (sigma fun (i : I) => C i) → (sigma fun (i : I) => C i) → Type (max w₁ v₁ u₁) where | mk : {i : I} → {X Y : C i} → (X ⟶ Y) → sigma_hom (sigma.mk i X) (sigma.mk i Y) namespace sigma_hom /-- The identity morphism on an object. -/ def id {I : Type w₁} {C : I → Type u₁} [(i : I) → category (C i)] (X : sigma fun (i : I) => C i) : sigma_hom X X := sorry protected instance inhabited {I : Type w₁} {C : I → Type u₁} [(i : I) → category (C i)] (X : sigma fun (i : I) => C i) : Inhabited (sigma_hom X X) := { default := id X } /-- Composition of sigma homomorphisms. -/ def comp {I : Type w₁} {C : I → Type u₁} [(i : I) → category (C i)] {X : sigma fun (i : I) => C i} {Y : sigma fun (i : I) => C i} {Z : sigma fun (i : I) => C i} : sigma_hom X Y → sigma_hom Y Z → sigma_hom X Z := sorry protected instance sigma.category_theory.category_struct {I : Type w₁} {C : I → Type u₁} [(i : I) → category (C i)] : category_struct (sigma fun (i : I) => C i) := category_struct.mk id fun (X Y Z : sigma fun (i : I) => C i) (f : X ⟶ Y) (g : Y ⟶ Z) => comp f g @[simp] theorem comp_def {I : Type w₁} {C : I → Type u₁} [(i : I) → category (C i)] (i : I) (X : C i) (Y : C i) (Z : C i) (f : X ⟶ Y) (g : Y ⟶ Z) : comp (mk f) (mk g) = mk (f ≫ g) := rfl theorem assoc {I : Type w₁} {C : I → Type u₁} [(i : I) → category (C i)] (X : sigma fun (i : I) => C i) (Y : sigma fun (i : I) => C i) (Z : sigma fun (i : I) => C i) (W : sigma fun (i : I) => C i) (f : X ⟶ Y) (g : Y ⟶ Z) (h : Z ⟶ W) : (f ≫ g) ≫ h = f ≫ g ≫ h := sorry theorem id_comp {I : Type w₁} {C : I → Type u₁} [(i : I) → category (C i)] (X : sigma fun (i : I) => C i) (Y : sigma fun (i : I) => C i) (f : X ⟶ Y) : 𝟙 ≫ f = f := sorry theorem comp_id {I : Type w₁} {C : I → Type u₁} [(i : I) → category (C i)] (X : sigma fun (i : I) => C i) (Y : sigma fun (i : I) => C i) (f : X ⟶ Y) : f ≫ 𝟙 = f := sorry end sigma_hom protected instance sigma {I : Type w₁} {C : I → Type u₁} [(i : I) → category (C i)] : category (sigma fun (i : I) => C i) := category.mk /-- The inclusion functor into the disjoint union of categories. -/ @[simp] theorem incl_map {I : Type w₁} {C : I → Type u₁} [(i : I) → category (C i)] (i : I) (X : C i) (Y : C i) : ∀ (ᾰ : X ⟶ Y), functor.map (incl i) ᾰ = sigma_hom.mk ᾰ := fun (ᾰ : X ⟶ Y) => Eq.refl (functor.map (incl i) ᾰ) @[simp] theorem incl_obj {I : Type w₁} {C : I → Type u₁} [(i : I) → category (C i)] {i : I} (X : C i) : functor.obj (incl i) X = sigma.mk i X := rfl protected instance incl.category_theory.full {I : Type w₁} {C : I → Type u₁} [(i : I) → category (C i)] (i : I) : full (incl i) := full.mk fun (X Y : C i) (_x : functor.obj (incl i) X ⟶ functor.obj (incl i) Y) => sorry protected instance incl.category_theory.faithful {I : Type w₁} {C : I → Type u₁} [(i : I) → category (C i)] (i : I) : faithful (incl i) := faithful.mk /-- To build a natural transformation over the sigma category, it suffices to specify it restricted to each subcategory. -/ def nat_trans {I : Type w₁} {C : I → Type u₁} [(i : I) → category (C i)] {D : Type u₂} [category D] {F : (sigma fun (i : I) => C i) ⥤ D} {G : (sigma fun (i : I) => C i) ⥤ D} (h : (i : I) → incl i ⋙ F ⟶ incl i ⋙ G) : F ⟶ G := nat_trans.mk fun (_x : sigma fun (i : I) => C i) => sorry @[simp] theorem nat_trans_app {I : Type w₁} {C : I → Type u₁} [(i : I) → category (C i)] {D : Type u₂} [category D] {F : (sigma fun (i : I) => C i) ⥤ D} {G : (sigma fun (i : I) => C i) ⥤ D} (h : (i : I) → incl i ⋙ F ⟶ incl i ⋙ G) (i : I) (X : C i) : nat_trans.app (nat_trans h) (sigma.mk i X) = nat_trans.app (h i) X := rfl /-- (Implementation). An auxiliary definition to build the functor `desc`. -/ def desc_map {I : Type w₁} {C : I → Type u₁} [(i : I) → category (C i)] {D : Type u₂} [category D] (F : (i : I) → C i ⥤ D) (X : sigma fun (i : I) => C i) (Y : sigma fun (i : I) => C i) : (X ⟶ Y) → (functor.obj (F (sigma.fst X)) (sigma.snd X) ⟶ functor.obj (F (sigma.fst Y)) (sigma.snd Y)) := sorry /-- Given a collection of functors `F i : C i ⥤ D`, we can produce a functor `(Σ i, C i) ⥤ D`. The produced functor `desc F` satisfies: `incl i ⋙ desc F ≅ F i`, i.e. restricted to just the subcategory `C i`, `desc F` agrees with `F i`, and it is unique (up to natural isomorphism) with this property. This witnesses that the sigma-type is the coproduct in Cat. -/ @[simp] theorem desc_obj {I : Type w₁} {C : I → Type u₁} [(i : I) → category (C i)] {D : Type u₂} [category D] (F : (i : I) → C i ⥤ D) (X : sigma fun (i : I) => C i) : functor.obj (desc F) X = functor.obj (F (sigma.fst X)) (sigma.snd X) := Eq.refl (functor.obj (desc F) X) @[simp] theorem desc_map_mk {I : Type w₁} {C : I → Type u₁} [(i : I) → category (C i)] {D : Type u₂} [category D] (F : (i : I) → C i ⥤ D) {i : I} (X : C i) (Y : C i) (f : X ⟶ Y) : functor.map (desc F) (sigma_hom.mk f) = functor.map (F i) f := rfl /-- This shows that when `desc F` is restricted to just the subcategory `C i`, `desc F` agrees with `F i`. -/ -- We hand-generate the simp lemmas about this since they come out cleaner. def incl_desc {I : Type w₁} {C : I → Type u₁} [(i : I) → category (C i)] {D : Type u₂} [category D] (F : (i : I) → C i ⥤ D) (i : I) : incl i ⋙ desc F ≅ F i := nat_iso.of_components (fun (X : C i) => iso.refl (functor.obj (incl i ⋙ desc F) X)) sorry @[simp] theorem incl_desc_hom_app {I : Type w₁} {C : I → Type u₁} [(i : I) → category (C i)] {D : Type u₂} [category D] (F : (i : I) → C i ⥤ D) (i : I) (X : C i) : nat_trans.app (iso.hom (incl_desc F i)) X = 𝟙 := rfl @[simp] theorem incl_desc_inv_app {I : Type w₁} {C : I → Type u₁} [(i : I) → category (C i)] {D : Type u₂} [category D] (F : (i : I) → C i ⥤ D) (i : I) (X : C i) : nat_trans.app (iso.inv (incl_desc F i)) X = 𝟙 := rfl /-- If `q` when restricted to each subcategory `C i` agrees with `F i`, then `q` is isomorphic to `desc F`. -/ def desc_uniq {I : Type w₁} {C : I → Type u₁} [(i : I) → category (C i)] {D : Type u₂} [category D] (F : (i : I) → C i ⥤ D) (q : (sigma fun (i : I) => C i) ⥤ D) (h : (i : I) → incl i ⋙ q ≅ F i) : q ≅ desc F := nat_iso.of_components (fun (_x : sigma fun (i : I) => C i) => sorry) sorry @[simp] theorem desc_uniq_hom_app {I : Type w₁} {C : I → Type u₁} [(i : I) → category (C i)] {D : Type u₂} [category D] (F : (i : I) → C i ⥤ D) (q : (sigma fun (i : I) => C i) ⥤ D) (h : (i : I) → incl i ⋙ q ≅ F i) (i : I) (X : C i) : nat_trans.app (iso.hom (desc_uniq F q h)) (sigma.mk i X) = nat_trans.app (iso.hom (h i)) X := rfl @[simp] theorem desc_uniq_inv_app {I : Type w₁} {C : I → Type u₁} [(i : I) → category (C i)] {D : Type u₂} [category D] (F : (i : I) → C i ⥤ D) (q : (sigma fun (i : I) => C i) ⥤ D) (h : (i : I) → incl i ⋙ q ≅ F i) (i : I) (X : C i) : nat_trans.app (iso.inv (desc_uniq F q h)) (sigma.mk i X) = nat_trans.app (iso.inv (h i)) X := rfl /-- If `q₁` and `q₂` when restricted to each subcategory `C i` agree, then `q₁` and `q₂` are isomorphic. -/ @[simp] theorem nat_iso_inv {I : Type w₁} {C : I → Type u₁} [(i : I) → category (C i)] {D : Type u₂} [category D] {q₁ : (sigma fun (i : I) => C i) ⥤ D} {q₂ : (sigma fun (i : I) => C i) ⥤ D} (h : (i : I) → incl i ⋙ q₁ ≅ incl i ⋙ q₂) : iso.inv (nat_iso h) = nat_trans fun (i : I) => iso.inv (h i) := Eq.refl (iso.inv (nat_iso h)) /-- A function `J → I` induces a functor `Σ j, C (g j) ⥤ Σ i, C i`. -/ def map {I : Type w₁} (C : I → Type u₁) [(i : I) → category (C i)] {J : Type w₂} (g : J → I) : (sigma fun (j : J) => C (g j)) ⥤ sigma fun (i : I) => C i := desc fun (j : J) => incl (g j) @[simp] theorem map_obj {I : Type w₁} (C : I → Type u₁) [(i : I) → category (C i)] {J : Type w₂} (g : J → I) (j : J) (X : C (g j)) : functor.obj (map C g) (sigma.mk j X) = sigma.mk (g j) X := rfl @[simp] theorem map_map {I : Type w₁} (C : I → Type u₁) [(i : I) → category (C i)] {J : Type w₂} (g : J → I) {j : J} {X : C (g j)} {Y : C (g j)} (f : X ⟶ Y) : functor.map (map C g) (sigma_hom.mk f) = sigma_hom.mk f := rfl /-- The functor `sigma.map C g` restricted to the subcategory `C j` acts as the inclusion of `g j`. -/ @[simp] theorem incl_comp_map_hom_app {I : Type w₁} (C : I → Type u₁) [(i : I) → category (C i)] {J : Type w₂} (g : J → I) (j : J) (X : C (g j)) : nat_trans.app (iso.hom (incl_comp_map C g j)) X = 𝟙 := Eq.refl 𝟙 /-- The functor `sigma.map` applied to the identity function is just the identity functor. -/ @[simp] theorem map_id_hom_app (I : Type w₁) (C : I → Type u₁) [(i : I) → category (C i)] (_x : sigma fun (i : I) => (fun (i : I) => (fun (i : I) => C (id i)) i) i) : nat_trans.app (iso.hom (map_id I C)) _x = nat_trans._match_1 (fun (i : I) => iso.hom (nat_iso.of_components (fun (X : C i) => iso.refl (sigma.mk i X)) (map_id._proof_1 I C i))) _x := sorry /-- The functor `sigma.map` applied to a composition is a composition of functors. -/ @[simp] theorem map_comp_hom_app {I : Type w₁} (C : I → Type u₁) [(i : I) → category (C i)] {J : Type w₂} {K : Type w₃} (f : K → J) (g : J → I) (X : sigma fun (i : K) => (fun (j : K) => function.comp C g (f j)) i) : nat_trans.app (iso.hom (map_comp C f g)) X = iso.hom (desc_uniq._match_1 (fun (j : K) => incl (g (f j))) (map (C ∘ g) f ⋙ map C g) (fun (k : K) => iso_whisker_right (incl_comp_map (C ∘ g) f k) (map C g) ≪≫ incl_comp_map C g (f k)) X) := sorry namespace functor /-- Assemble an `I`-indexed family of functors into a functor between the sigma types. -/ def sigma {I : Type w₁} {C : I → Type u₁} [(i : I) → category (C i)] {D : I → Type u₁} [(i : I) → category (D i)] (F : (i : I) → C i ⥤ D i) : (sigma fun (i : I) => C i) ⥤ sigma fun (i : I) => D i := desc fun (i : I) => F i ⋙ incl i end functor namespace nat_trans /-- Assemble an `I`-indexed family of natural transformations into a single natural transformation. -/ def sigma {I : Type w₁} {C : I → Type u₁} [(i : I) → category (C i)] {D : I → Type u₁} [(i : I) → category (D i)] {F : (i : I) → C i ⥤ D i} {G : (i : I) → C i ⥤ D i} (α : (i : I) → F i ⟶ G i) : functor.sigma F ⟶ functor.sigma G := nat_trans.mk fun (f : sigma fun (i : I) => C i) => sigma_hom.mk (nat_trans.app (α (sigma.fst f)) (sigma.snd f))
9c9224906e84df13d4586226616a1f6128ce4611
e00ea76a720126cf9f6d732ad6216b5b824d20a7
/src/tactic/lift.lean
f4726642c49cf20257d65595d010f6ece544bd70
[ "Apache-2.0" ]
permissive
vaibhavkarve/mathlib
a574aaf68c0a431a47fa82ce0637f0f769826bfe
17f8340912468f49bdc30acdb9a9fa02eeb0473a
refs/heads/master
1,621,263,802,637
1,585,399,588,000
1,585,399,588,000
250,833,447
0
0
Apache-2.0
1,585,410,341,000
1,585,410,341,000
null
UTF-8
Lean
false
false
8,865
lean
/- Copyright (c) 2019 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import tactic.rcases /-! # lift tactic This file defines the lift tactic, allowing the user to lift elements from one type to another under a specified condition. ## Tags lift, tactic -/ universe variables u v w /-- A class specifying that you can lift elements from `α` to `β` assuming `cond` is true. Used by the tactic `lift`. -/ class can_lift (α : Type u) (β : Type v) : Type (max u v) := (coe : β → α) (cond : α → Prop) (prf : ∀(x : α), cond x → ∃(y : β), coe y = x) open tactic @[user_attribute] meta def can_lift_attr : user_attribute (list name) := { name := "_can_lift", descr := "internal attribute used by the lift tactic", cache_cfg := { mk_cache := λ _, do { ls ← attribute.get_instances `instance, ls.mfilter $ λ l, do { (_,t) ← mk_const l >>= infer_type >>= mk_local_pis, return $ t.is_app_of `can_lift } }, dependencies := [`instance] } } instance : can_lift ℤ ℕ := ⟨coe, λ n, 0 ≤ n, λ n hn, ⟨n.nat_abs, int.nat_abs_of_nonneg hn⟩⟩ /-- Enable automatic handling of pi types in `can_lift`. -/ instance pi.can_lift (ι : Type u) (α : Π i : ι, Type v) (β : Π i : ι, Type w) [Π i : ι, can_lift (α i) (β i)] : can_lift (Π i : ι, α i) (Π i : ι, β i) := { coe := λ f i, can_lift.coe (α i) (f i), cond := λ f, ∀ i, can_lift.cond (β i) (f i), prf := λ f hf, ⟨λ i, classical.some (can_lift.prf (f i) (hf i)), funext $ λ i, classical.some_spec (can_lift.prf (f i) (hf i))⟩ } namespace tactic /- Construct the proof of `cond x` in the lift tactic. `e` is the expression being lifted and `h` is the specified proof of `can_lift.cond e`. `old_tp` and `new_tp` are the arguments to `can_lift` and `inst` is the `can_lift`-instance. `s` and `to_unfold` contain the information of the simp set used to simplify. If the proof was specified, we check whether it has the correct type. If it doesn't have the correct type, we display an error message (but first call dsimp on the expression in the message). If the proof was not specified, we create assert it as a local constant. (The name of this local constant doesn't matter, since `lift` will remove it from the context) -/ meta def get_lift_prf (h : option pexpr) (old_tp new_tp inst e : expr) (s : simp_lemmas) (to_unfold : list name) : tactic expr := if h_some : h.is_some then (do prf ← i_to_expr (option.get h_some), prf_ty ← infer_type prf, expected_prf_ty ← mk_app `can_lift.cond [old_tp, new_tp, inst, e], unify prf_ty expected_prf_ty <|> (do expected_prf_ty2 ← s.dsimplify to_unfold expected_prf_ty, pformat!"lift tactic failed. The type of\n {prf}\nis\n {prf_ty}\nbut it is expected to be\n {expected_prf_ty2}" >>= fail), return prf) else (do prf_nm ← get_unused_name, prf ← mk_app `can_lift.cond [old_tp, new_tp, inst, e] >>= assert prf_nm, dsimp_target s to_unfold {}, swap, return prf) /-- Lift the expression `p` to the type `t`, with proof obligation given by `h`. The list `n` is used for the two newly generated names, and to specify whether `h` should remain in the local context. See the doc string of `tactic.interactive.lift` for more information. -/ meta def lift (p : pexpr) (t : pexpr) (h : option pexpr) (n : list name) : tactic unit := do propositional_goal <|> fail "lift tactic failed. Tactic is only applicable when the target is a proposition.", e ← i_to_expr p, old_tp ← infer_type e, new_tp ← i_to_expr t, inst_type ← mk_app ``can_lift [old_tp, new_tp], inst ← mk_instance inst_type <|> pformat!"Failed to find a lift from {old_tp} to {new_tp}. Provide an instance of\n {inst_type}" >>= fail, /- make the simp set to get rid of `can_lift` projections -/ can_lift_instances ← can_lift_attr.get_cache >>= λ l, l.mmap resolve_name, (s, to_unfold) ← mk_simp_set tt [] $ can_lift_instances.map simp_arg_type.expr, prf_cond ← get_lift_prf h old_tp new_tp inst e s to_unfold, let prf_nm := if prf_cond.is_local_constant then some prf_cond.local_pp_name else none, /- We use mk_mapp to apply `can_lift.prf` to all but one argument, and then just use expr.app for the last argument. For some reason we get an error when applying mk_mapp it to all arguments. -/ prf_ex0 ← mk_mapp `can_lift.prf [old_tp, new_tp, inst, e], let prf_ex := prf_ex0 prf_cond, /- Find the name of the new variable -/ new_nm ← if n ≠ [] then return n.head else if e.is_local_constant then return e.local_pp_name else get_unused_name, /- Find the name of the proof of the equation -/ eq_nm ← if hn : 1 < n.length then return (n.nth_le 1 hn) else if e.is_local_constant then return `rfl else get_unused_name `h, /- We add the proof of the existential statement to the context and then apply `dsimp` to it, unfolding all `can_lift` instances. -/ temp_nm ← get_unused_name, temp_e ← note temp_nm none prf_ex, dsimp_hyp temp_e s to_unfold {}, /- We case on the existential. We use `rcases` because `eq_nm` could be `rfl`. -/ rcases none (pexpr.of_expr temp_e) [[rcases_patt.one new_nm, rcases_patt.one eq_nm]], /- If the lifted variable is not a local constant, try to rewrite it away using the new equality-/ when (¬ e.is_local_constant) (get_local eq_nm >>= λ e, interactive.rw ⟨[⟨⟨0, 0⟩, tt, (pexpr.of_expr e)⟩], none⟩ interactive.loc.wildcard), /- If the proof `prf_cond` is a local constant, remove it from the context, unless `n` specifies to keep it. -/ if h_prf_nm : prf_nm.is_some ∧ n.nth 2 ≠ prf_nm then get_local (option.get h_prf_nm.1) >>= clear else skip open lean.parser interactive interactive.types local postfix `?`:9001 := optional meta def using_texpr := (tk "using" *> texpr)? reserve notation `to` meta def to_texpr := (tk "to" *> texpr) namespace interactive /-- Lift an expression to another type. Lift an expression to another type. * Usage: `'lift' expr 'to' expr ('using' expr)? ('with' id (id id?)?)?`. * If `n : ℤ` and `hn : n ≥ 0` then the tactic `lift n to ℕ using hn` creates a new constant of type `ℕ`, also named `n` and replaces all occurrences of the old variable `(n : ℤ)` with `↑n` (where `n` in the new variable). It will remove `n` and `hn` from the context. + So for example the tactic `lift n to ℕ using hn` transforms the goal `n : ℤ, hn : n ≥ 0, h : P n ⊢ n = 3` to `n : ℕ, h : P ↑n ⊢ ↑n = 3` (here `P` is some term of type `ℤ → Prop`). * The argument `using hn` is optional, the tactic `lift n to ℕ` does the same, but also creates a new subgoal that `n ≥ 0` (where `n` is the old variable). + So for example the tactic `lift n to ℕ` transforms the goal `n : ℤ, h : P n ⊢ n = 3` to two goals `n : ℕ, h : P ↑n ⊢ ↑n = 3` and `n : ℤ, h : P n ⊢ n ≥ 0`. * You can also use `lift n to ℕ using e` where `e` is any expression of type `n ≥ 0`. * Use `lift n to ℕ with k` to specify the name of the new variable. * Use `lift n to ℕ with k hk` to also specify the name of the equality `↑k = n`. In this case, `n` will remain in the context. You can use `rfl` for the name of `hk` to substitute `n` away (i.e. the default behavior). * You can also use `lift e to ℕ with k hk` where `e` is any expression of type `ℤ`. In this case, the `hk` will always stay in the context, but it will be used to rewrite `e` in all hypotheses and the target. + So for example the tactic `lift n + 3 to ℕ using hn with k hk` transforms the goal `n : ℤ, hn : n + 3 ≥ 0, h : P (n + 3) ⊢ n + 3 = 2 * n` to the goal `n : ℤ, k : ℕ, hk : ↑k = n + 3, h : P ↑k ⊢ ↑k = 2 * n`. * The tactic `lift n to ℕ using h` will remove `h` from the context. If you want to keep it, specify it again as the third argument to `with`, like this: `lift n to ℕ using h with n rfl h`. * More generally, this can lift an expression from `α` to `β` assuming that there is an instance of `can_lift α β`. In this case the proof obligation is specified by `can_lift.cond`. * Given an instance `can_lift β γ`, it can also lift `α → β` to `α → γ`; more generally, given `β : Π a : α, Type*`, `γ : Π a : α, Type*`, and `[Π a : α, can_lift (β a) (γ a)]`, it automatically generates an instance `can_lift (Π a, β a) (Π a, γ a)`. -/ meta def lift (p : parse texpr) (t : parse to_texpr) (h : parse using_texpr) (n : parse with_ident_list) : tactic unit := tactic.lift p t h n add_tactic_doc { name := "lift", category := doc_category.tactic, decl_names := [`tactic.interactive.lift], tags := ["coercions"] } end interactive end tactic
a03d94c61eab96551167718d82bd4fd60a04617b
491068d2ad28831e7dade8d6dff871c3e49d9431
/hott/types/sigma.hlean
9798bf34e6b62c2bc948fbad8e646f880008a106
[ "Apache-2.0" ]
permissive
davidmueller13/lean
65a3ed141b4088cd0a268e4de80eb6778b21a0e9
c626e2e3c6f3771e07c32e82ee5b9e030de5b050
refs/heads/master
1,611,278,313,401
1,444,021,177,000
1,444,021,177,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
17,911
hlean
/- Copyright (c) 2014-15 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Floris van Doorn Partially ported from Coq HoTT Theorems about sigma-types (dependent sums) -/ import types.prod open eq sigma sigma.ops equiv is_equiv function is_trunc sum unit namespace sigma variables {A A' : Type} {B : A → Type} {B' : A' → Type} {C : Πa, B a → Type} {D : Πa b, C a b → Type} {a a' a'' : A} {b b₁ b₂ : B a} {b' : B a'} {b'' : B a''} {u v w : Σa, B a} definition destruct := @sigma.cases_on /- Paths in a sigma-type -/ protected definition eta : Π (u : Σa, B a), ⟨u.1 , u.2⟩ = u | eta ⟨u₁, u₂⟩ := idp definition eta2 : Π (u : Σa b, C a b), ⟨u.1, u.2.1, u.2.2⟩ = u | eta2 ⟨u₁, u₂, u₃⟩ := idp definition eta3 : Π (u : Σa b c, D a b c), ⟨u.1, u.2.1, u.2.2.1, u.2.2.2⟩ = u | eta3 ⟨u₁, u₂, u₃, u₄⟩ := idp definition dpair_eq_dpair (p : a = a') (q : b =[p] b') : ⟨a, b⟩ = ⟨a', b'⟩ := by induction q; reflexivity definition sigma_eq (p : u.1 = v.1) (q : u.2 =[p] v.2) : u = v := by induction u; induction v; exact (dpair_eq_dpair p q) definition eq_pr1 [unfold 5] (p : u = v) : u.1 = v.1 := ap pr1 p postfix `..1`:(max+1) := eq_pr1 definition eq_pr2 (p : u = v) : u.2 =[p..1] v.2 := by induction p; exact idpo postfix `..2`:(max+1) := eq_pr2 definition dpair_sigma_eq (p : u.1 = v.1) (q : u.2 =[p] v.2) : ⟨(sigma_eq p q)..1, (sigma_eq p q)..2⟩ = ⟨p, q⟩ := by induction u; induction v;esimp at *;induction q;esimp definition sigma_eq_pr1 (p : u.1 = v.1) (q : u.2 =[p] v.2) : (sigma_eq p q)..1 = p := (dpair_sigma_eq p q)..1 definition sigma_eq_pr2 (p : u.1 = v.1) (q : u.2 =[p] v.2) : (sigma_eq p q)..2 =[sigma_eq_pr1 p q] q := (dpair_sigma_eq p q)..2 definition sigma_eq_eta (p : u = v) : sigma_eq (p..1) (p..2) = p := by induction p; induction u; reflexivity definition tr_pr1_sigma_eq {B' : A → Type} (p : u.1 = v.1) (q : u.2 =[p] v.2) : transport (λx, B' x.1) (sigma_eq p q) = transport B' p := by induction u; induction v; esimp at *;induction q; reflexivity /- the uncurried version of sigma_eq. We will prove that this is an equivalence -/ definition sigma_eq_unc : Π (pq : Σ(p : u.1 = v.1), u.2 =[p] v.2), u = v | sigma_eq_unc ⟨pq₁, pq₂⟩ := sigma_eq pq₁ pq₂ definition dpair_sigma_eq_unc : Π (pq : Σ(p : u.1 = v.1), u.2 =[p] v.2), ⟨(sigma_eq_unc pq)..1, (sigma_eq_unc pq)..2⟩ = pq | dpair_sigma_eq_unc ⟨pq₁, pq₂⟩ := dpair_sigma_eq pq₁ pq₂ definition sigma_eq_pr1_unc (pq : Σ(p : u.1 = v.1), u.2 =[p] v.2) : (sigma_eq_unc pq)..1 = pq.1 := (dpair_sigma_eq_unc pq)..1 definition sigma_eq_pr2_unc (pq : Σ(p : u.1 = v.1), u.2 =[p] v.2) : (sigma_eq_unc pq)..2 =[sigma_eq_pr1_unc pq] pq.2 := (dpair_sigma_eq_unc pq)..2 definition sigma_eq_eta_unc (p : u = v) : sigma_eq_unc ⟨p..1, p..2⟩ = p := sigma_eq_eta p definition tr_sigma_eq_pr1_unc {B' : A → Type} (pq : Σ(p : u.1 = v.1), u.2 =[p] v.2) : transport (λx, B' x.1) (@sigma_eq_unc A B u v pq) = transport B' pq.1 := destruct pq tr_pr1_sigma_eq definition is_equiv_sigma_eq [instance] (u v : Σa, B a) : is_equiv (@sigma_eq_unc A B u v) := adjointify sigma_eq_unc (λp, ⟨p..1, p..2⟩) sigma_eq_eta_unc dpair_sigma_eq_unc definition sigma_eq_equiv (u v : Σa, B a) : (u = v) ≃ (Σ(p : u.1 = v.1), u.2 =[p] v.2) := (equiv.mk sigma_eq_unc _)⁻¹ᵉ definition dpair_eq_dpair_con (p1 : a = a' ) (q1 : b =[p1] b' ) (p2 : a' = a'') (q2 : b' =[p2] b'') : dpair_eq_dpair (p1 ⬝ p2) (q1 ⬝o q2) = dpair_eq_dpair p1 q1 ⬝ dpair_eq_dpair p2 q2 := by induction q1; induction q2; reflexivity definition sigma_eq_con (p1 : u.1 = v.1) (q1 : u.2 =[p1] v.2) (p2 : v.1 = w.1) (q2 : v.2 =[p2] w.2) : sigma_eq (p1 ⬝ p2) (q1 ⬝o q2) = sigma_eq p1 q1 ⬝ sigma_eq p2 q2 := by induction u; induction v; induction w; apply dpair_eq_dpair_con local attribute dpair_eq_dpair [reducible] definition dpair_eq_dpair_con_idp (p : a = a') (q : b =[p] b') : dpair_eq_dpair p q = dpair_eq_dpair p !pathover_tr ⬝ dpair_eq_dpair idp (pathover_idp_of_eq (tr_eq_of_pathover q)) := by induction q; reflexivity /- eq_pr1 commutes with the groupoid structure. -/ definition eq_pr1_idp (u : Σa, B a) : (refl u) ..1 = refl (u.1) := idp definition eq_pr1_con (p : u = v) (q : v = w) : (p ⬝ q) ..1 = (p..1) ⬝ (q..1) := !ap_con definition eq_pr1_inv (p : u = v) : p⁻¹ ..1 = (p..1)⁻¹ := !ap_inv /- Applying dpair to one argument is the same as dpair_eq_dpair with reflexivity in the first place. -/ definition ap_dpair (q : b₁ = b₂) : ap (sigma.mk a) q = dpair_eq_dpair idp (pathover_idp_of_eq q) := by induction q; reflexivity /- Dependent transport is the same as transport along a sigma_eq. -/ definition transportD_eq_transport (p : a = a') (c : C a b) : p ▸D c = transport (λu, C (u.1) (u.2)) (dpair_eq_dpair p !pathover_tr) c := by induction p; reflexivity definition sigma_eq_eq_sigma_eq {p1 q1 : a = a'} {p2 : b =[p1] b'} {q2 : b =[q1] b'} (r : p1 = q1) (s : p2 =[r] q2) : sigma_eq p1 p2 = sigma_eq q1 q2 := by induction s; reflexivity /- A path between paths in a total space is commonly shown component wise. -/ definition sigma_eq2 {p q : u = v} (r : p..1 = q..1) (s : p..2 =[r] q..2) : p = q := begin revert q r s, induction p, induction u with u1 u2, intro q r s, transitivity sigma_eq q..1 q..2, apply sigma_eq_eq_sigma_eq r s, apply sigma_eq_eta, end definition sigma_eq2_unc {p q : u = v} (rs : Σ(r : p..1 = q..1), p..2 =[r] q..2) : p = q := destruct rs sigma_eq2 /- Transport -/ /- The concrete description of transport in sigmas (and also pis) is rather trickier than in the other types. In particular, these cannot be described just in terms of transport in simpler types; they require also the dependent transport [transportD]. In particular, this indicates why `transport` alone cannot be fully defined by induction on the structure of types, although Id-elim/transportD can be (cf. Observational Type Theory). A more thorough set of lemmas, along the lines of the present ones but dealing with Id-elim rather than just transport, might be nice to have eventually? -/ definition sigma_transport (p : a = a') (bc : Σ(b : B a), C a b) : p ▸ bc = ⟨p ▸ bc.1, p ▸D bc.2⟩ := by induction p; induction bc; reflexivity /- The special case when the second variable doesn't depend on the first is simpler. -/ definition sigma_transport_nondep {B : Type} {C : A → B → Type} (p : a = a') (bc : Σ(b : B), C a b) : p ▸ bc = ⟨bc.1, p ▸ bc.2⟩ := by induction p; induction bc; reflexivity /- Or if the second variable contains a first component that doesn't depend on the first. -/ definition sigma_transport2_nondep {C : A → Type} {D : Π a:A, B a → C a → Type} (p : a = a') (bcd : Σ(b : B a) (c : C a), D a b c) : p ▸ bcd = ⟨p ▸ bcd.1, p ▸ bcd.2.1, p ▸D2 bcd.2.2⟩ := begin induction p, induction bcd with b cd, induction cd, reflexivity end /- Pathovers -/ definition etao (p : a = a') (bc : Σ(b : B a), C a b) : bc =[p] ⟨p ▸ bc.1, p ▸D bc.2⟩ := by induction p; induction bc; apply idpo definition sigma_pathover (p : a = a') (u : Σ(b : B a), C a b) (v : Σ(b : B a'), C a' b) (r : u.1 =[p] v.1) (s : u.2 =[apo011 C p r] v.2) : u =[p] v := begin induction u, induction v, esimp at *, induction r, esimp [apo011] at s, induction s using idp_rec_on, apply idpo end /- TODO: * define the projections from the type u =[p] v * show that the uncurried version of sigma_pathover is an equivalence -/ /- Functorial action -/ variables (f : A → A') (g : Πa, B a → B' (f a)) definition sigma_functor [unfold 7] (u : Σa, B a) : Σa', B' a' := ⟨f u.1, g u.1 u.2⟩ definition total [reducible] [unfold 5] {B' : A → Type} (g : Πa, B a → B' a) (u : Σa, B a) : Σa', B' a' := sigma_functor id g u /- Equivalences -/ definition is_equiv_sigma_functor [H1 : is_equiv f] [H2 : Π a, is_equiv (g a)] : is_equiv (sigma_functor f g) := adjointify (sigma_functor f g) (sigma_functor f⁻¹ (λ(a' : A') (b' : B' a'), ((g (f⁻¹ a'))⁻¹ (transport B' (right_inv f a')⁻¹ b')))) begin intro u', induction u' with a' b', apply sigma_eq (right_inv f a'), rewrite [▸*,right_inv (g (f⁻¹ a')),▸*], apply tr_pathover end begin intro u, induction u with a b, apply (sigma_eq (left_inv f a)), apply pathover_of_tr_eq, rewrite [▸*,adj f,-(fn_tr_eq_tr_fn (left_inv f a) (λ a, (g a)⁻¹)), ▸*,tr_compose B' f,tr_inv_tr,left_inv] end definition sigma_equiv_sigma_of_is_equiv [constructor] [H1 : is_equiv f] [H2 : Π a, is_equiv (g a)] : (Σa, B a) ≃ (Σa', B' a') := equiv.mk (sigma_functor f g) !is_equiv_sigma_functor definition sigma_equiv_sigma [constructor] (Hf : A ≃ A') (Hg : Π a, B a ≃ B' (to_fun Hf a)) : (Σa, B a) ≃ (Σa', B' a') := sigma_equiv_sigma_of_is_equiv (to_fun Hf) (λ a, to_fun (Hg a)) definition sigma_equiv_sigma_id [constructor] {B' : A → Type} (Hg : Π a, B a ≃ B' a) : (Σa, B a) ≃ Σa, B' a := sigma_equiv_sigma equiv.refl Hg definition ap_sigma_functor_eq_dpair (p : a = a') (q : b =[p] b') : ap (sigma_functor f g) (sigma_eq p q) = sigma_eq (ap f p) (pathover.rec_on q idpo) := by induction q; reflexivity -- definition ap_sigma_functor_eq (p : u.1 = v.1) (q : u.2 =[p] v.2) -- : ap (sigma_functor f g) (sigma_eq p q) = -- sigma_eq (ap f p) -- ((tr_compose B' f p (g u.1 u.2))⁻¹ ⬝ (fn_tr_eq_tr_fn p g u.2)⁻¹ ⬝ ap (g v.1) q) := -- by induction u; induction v; apply ap_sigma_functor_eq_dpair /- definition 3.11.9(i): Summing up a contractible family of types does nothing. -/ definition is_equiv_pr1 [instance] [constructor] (B : A → Type) [H : Π a, is_contr (B a)] : is_equiv (@pr1 A B) := adjointify pr1 (λa, ⟨a, !center⟩) (λa, idp) (λu, sigma_eq idp (pathover_idp_of_eq !center_eq)) definition sigma_equiv_of_is_contr_right [constructor] [H : Π a, is_contr (B a)] : (Σa, B a) ≃ A := equiv.mk pr1 _ /- definition 3.11.9(ii): Dually, summing up over a contractible type does nothing. -/ definition sigma_equiv_of_is_contr_left [constructor] (B : A → Type) [H : is_contr A] : (Σa, B a) ≃ B (center A) := equiv.MK (λu, (center_eq u.1)⁻¹ ▸ u.2) (λb, ⟨!center, b⟩) (λb, ap (λx, x ▸ b) !hprop_eq_of_is_contr) (λu, sigma_eq !center_eq !tr_pathover) /- Associativity -/ --this proof is harder than in Coq because we don't have eta definitionally for sigma definition sigma_assoc_equiv [constructor] (C : (Σa, B a) → Type) : (Σa b, C ⟨a, b⟩) ≃ (Σu, C u) := equiv.mk _ (adjointify (λav, ⟨⟨av.1, av.2.1⟩, av.2.2⟩) (λuc, ⟨uc.1.1, uc.1.2, !sigma.eta⁻¹ ▸ uc.2⟩) begin intro uc, induction uc with u c, induction u, reflexivity end begin intro av, induction av with a v, induction v, reflexivity end) open prod prod.ops definition assoc_equiv_prod [constructor] (C : (A × A') → Type) : (Σa a', C (a,a')) ≃ (Σu, C u) := equiv.mk _ (adjointify (λav, ⟨(av.1, av.2.1), av.2.2⟩) (λuc, ⟨pr₁ (uc.1), pr₂ (uc.1), !prod.eta⁻¹ ▸ uc.2⟩) proof (λuc, destruct uc (λu, prod.destruct u (λa b c, idp))) qed proof (λav, destruct av (λa v, destruct v (λb c, idp))) qed) /- Symmetry -/ definition comm_equiv_unc (C : A × A' → Type) : (Σa a', C (a, a')) ≃ (Σa' a, C (a, a')) := calc (Σa a', C (a, a')) ≃ Σu, C u : assoc_equiv_prod ... ≃ Σv, C (flip v) : sigma_equiv_sigma !prod_comm_equiv (λu, prod.destruct u (λa a', equiv.refl)) ... ≃ Σa' a, C (a, a') : assoc_equiv_prod definition sigma_comm_equiv [constructor] (C : A → A' → Type) : (Σa a', C a a') ≃ (Σa' a, C a a') := comm_equiv_unc (λu, C (prod.pr1 u) (prod.pr2 u)) definition equiv_prod [constructor] (A B : Type) : (Σ(a : A), B) ≃ A × B := equiv.mk _ (adjointify (λs, (s.1, s.2)) (λp, ⟨pr₁ p, pr₂ p⟩) proof (λp, prod.destruct p (λa b, idp)) qed proof (λs, destruct s (λa b, idp)) qed) definition comm_equiv_nondep (A B : Type) : (Σ(a : A), B) ≃ Σ(b : B), A := calc (Σ(a : A), B) ≃ A × B : equiv_prod ... ≃ B × A : prod_comm_equiv ... ≃ Σ(b : B), A : equiv_prod /- Interaction with other type constructors -/ definition sigma_empty_left [constructor] (B : empty → Type) : (Σx, B x) ≃ empty := begin fapply equiv.MK, { intro v, induction v, contradiction}, { intro x, contradiction}, { intro x, contradiction}, { intro v, induction v, contradiction}, end definition sigma_empty_right [constructor] (A : Type) : (Σ(a : A), empty) ≃ empty := begin fapply equiv.MK, { intro v, induction v, contradiction}, { intro x, contradiction}, { intro x, contradiction}, { intro v, induction v, contradiction}, end definition sigma_unit_left [constructor] (B : unit → Type) : (Σx, B x) ≃ B star := !sigma_equiv_of_is_contr_left definition sigma_unit_right [constructor] (A : Type) : (Σ(a : A), unit) ≃ A := !sigma_equiv_of_is_contr_right definition sigma_sum_left [constructor] (B : A + A' → Type) : (Σp, B p) ≃ (Σa, B (inl a)) + (Σa, B (inr a)) := begin fapply equiv.MK, { intro v, induction v with p b, induction p: append (apply inl) (apply inr); constructor; assumption }, { intro p, induction p with v v: induction v; constructor; assumption}, { intro p, induction p with v v: induction v; reflexivity}, { intro v, induction v with p b, induction p: reflexivity}, end definition sigma_sum_right [constructor] (B C : A → Type) : (Σa, B a + C a) ≃ (Σa, B a) + (Σa, C a) := begin fapply equiv.MK, { intro v, induction v with a p, induction p: append (apply inl) (apply inr); constructor; assumption}, { intro p, induction p with v v: induction v; constructor; append (apply inl) (apply inr); assumption}, { intro p, induction p with v v: induction v; reflexivity}, { intro v, induction v with a p, induction p: reflexivity}, end /- ** Universal mapping properties -/ /- *** The positive universal property. -/ section definition is_equiv_sigma_rec [instance] (C : (Σa, B a) → Type) : is_equiv (sigma.rec : (Πa b, C ⟨a, b⟩) → Πab, C ab) := adjointify _ (λ g a b, g ⟨a, b⟩) (λ g, proof eq_of_homotopy (λu, destruct u (λa b, idp)) qed) (λ f, refl f) definition equiv_sigma_rec (C : (Σa, B a) → Type) : (Π(a : A) (b: B a), C ⟨a, b⟩) ≃ (Πxy, C xy) := equiv.mk sigma.rec _ /- *** The negative universal property. -/ protected definition coind_unc (fg : Σ(f : Πa, B a), Πa, C a (f a)) (a : A) : Σ(b : B a), C a b := ⟨fg.1 a, fg.2 a⟩ protected definition coind (f : Π a, B a) (g : Π a, C a (f a)) (a : A) : Σ(b : B a), C a b := sigma.coind_unc ⟨f, g⟩ a --is the instance below dangerous? --in Coq this can be done without function extensionality definition is_equiv_coind [instance] (C : Πa, B a → Type) : is_equiv (@sigma.coind_unc _ _ C) := adjointify _ (λ h, ⟨λa, (h a).1, λa, (h a).2⟩) (λ h, proof eq_of_homotopy (λu, !sigma.eta) qed) (λfg, destruct fg (λ(f : Π (a : A), B a) (g : Π (x : A), C x (f x)), proof idp qed)) definition sigma_pi_equiv_pi_sigma : (Σ(f : Πa, B a), Πa, C a (f a)) ≃ (Πa, Σb, C a b) := equiv.mk sigma.coind_unc _ end /- Subtypes (sigma types whose second components are hprops) -/ definition subtype [reducible] {A : Type} (P : A → Type) [H : Πa, is_hprop (P a)] := Σ(a : A), P a notation [parsing-only] `{` binder `|` r:(scoped:1 P, subtype P) `}` := r /- To prove equality in a subtype, we only need equality of the first component. -/ definition subtype_eq [H : Πa, is_hprop (B a)] (u v : {a | B a}) : u.1 = v.1 → u = v := sigma_eq_unc ∘ inv pr1 definition is_equiv_subtype_eq [H : Πa, is_hprop (B a)] (u v : {a | B a}) : is_equiv (subtype_eq u v) := !is_equiv_compose local attribute is_equiv_subtype_eq [instance] definition equiv_subtype [H : Πa, is_hprop (B a)] (u v : {a | B a}) : (u.1 = v.1) ≃ (u = v) := equiv.mk !subtype_eq _ definition subtype_eq_inv {A : Type} {B : A → Type} [H : Πa, is_hprop (B a)] (u v : Σa, B a) : u = v → u.1 = v.1 := (subtype_eq u v)⁻¹ᶠ local attribute subtype_eq_inv [reducible] definition is_equiv_subtype_eq_inv {A : Type} {B : A → Type} [H : Πa, is_hprop (B a)] (u v : Σa, B a) : is_equiv (subtype_eq_inv u v) := _ /- truncatedness -/ theorem is_trunc_sigma (B : A → Type) (n : trunc_index) [HA : is_trunc n A] [HB : Πa, is_trunc n (B a)] : is_trunc n (Σa, B a) := begin revert A B HA HB, induction n with n IH, { intro A B HA HB, fapply is_trunc_equiv_closed_rev, apply sigma_equiv_of_is_contr_left}, { intro A B HA HB, apply is_trunc_succ_intro, intro u v, apply is_trunc_equiv_closed_rev, apply sigma_eq_equiv, exact IH _ _ _ _} end end sigma attribute sigma.is_trunc_sigma [instance] [priority 1490]
9bfbea1b33bf7e25173364665f161c13c65cec17
94e33a31faa76775069b071adea97e86e218a8ee
/src/category_theory/preadditive/biproducts.lean
d397c0813732aa03d5c22b07e4b7603ef693c02f
[ "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,164
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 tactic.abel import category_theory.limits.shapes.biproducts import category_theory.preadditive /-! # Basic facts about morphisms between biproducts in preadditive categories. * In any category (with zero morphisms), if `biprod.map f g` is an isomorphism, then both `f` and `g` are isomorphisms. The remaining lemmas hold in any preadditive category. * If `f` is a morphism `X₁ ⊞ X₂ ⟶ Y₁ ⊞ Y₂` whose `X₁ ⟶ Y₁` entry is an isomorphism, then we can construct isomorphisms `L : X₁ ⊞ X₂ ≅ X₁ ⊞ X₂` and `R : Y₁ ⊞ Y₂ ≅ Y₁ ⊞ Y₂` so that `L.hom ≫ g ≫ R.hom` is diagonal (with `X₁ ⟶ Y₁` component still `f`), via Gaussian elimination. * As a corollary of the previous two facts, if we have an isomorphism `X₁ ⊞ X₂ ≅ Y₁ ⊞ Y₂` whose `X₁ ⟶ Y₁` entry is an isomorphism, we can construct an isomorphism `X₂ ≅ Y₂`. * If `f : W ⊞ X ⟶ Y ⊞ Z` is an isomorphism, either `𝟙 W = 0`, or at least one of the component maps `W ⟶ Y` and `W ⟶ Z` is nonzero. * If `f : ⨁ S ⟶ ⨁ T` is an isomorphism, then every column (corresponding to a nonzero summand in the domain) has some nonzero matrix entry. -/ open category_theory open category_theory.preadditive open category_theory.limits universes v u noncomputable theory namespace category_theory variables {C : Type u} [category.{v} C] section variables [has_zero_morphisms.{v} C] [has_binary_biproducts.{v} C] /-- If ``` (f 0) (0 g) ``` is invertible, then `f` is invertible. -/ lemma is_iso_left_of_is_iso_biprod_map {W X Y Z : C} (f : W ⟶ Y) (g : X ⟶ Z) [is_iso (biprod.map f g)] : is_iso f := ⟨⟨biprod.inl ≫ inv (biprod.map f g) ≫ biprod.fst, ⟨begin have t := congr_arg (λ p : W ⊞ X ⟶ W ⊞ X, biprod.inl ≫ p ≫ biprod.fst) (is_iso.hom_inv_id (biprod.map f g)), simp only [category.id_comp, category.assoc, biprod.inl_map_assoc] at t, simp [t], end, begin have t := congr_arg (λ p : Y ⊞ Z ⟶ Y ⊞ Z, biprod.inl ≫ p ≫ biprod.fst) (is_iso.inv_hom_id (biprod.map f g)), simp only [category.id_comp, category.assoc, biprod.map_fst] at t, simp only [category.assoc], simp [t], end⟩⟩⟩ /-- If ``` (f 0) (0 g) ``` is invertible, then `g` is invertible. -/ lemma is_iso_right_of_is_iso_biprod_map {W X Y Z : C} (f : W ⟶ Y) (g : X ⟶ Z) [is_iso (biprod.map f g)] : is_iso g := begin letI : is_iso (biprod.map g f) := by { rw [←biprod.braiding_map_braiding], apply_instance, }, exact is_iso_left_of_is_iso_biprod_map g f, end end section variables [preadditive.{v} C] [has_binary_biproducts.{v} C] variables {X₁ X₂ Y₁ Y₂ : C} variables (f₁₁ : X₁ ⟶ Y₁) (f₁₂ : X₁ ⟶ Y₂) (f₂₁ : X₂ ⟶ Y₁) (f₂₂ : X₂ ⟶ Y₂) /-- The "matrix" morphism `X₁ ⊞ X₂ ⟶ Y₁ ⊞ Y₂` with specified components. -/ def biprod.of_components : X₁ ⊞ X₂ ⟶ Y₁ ⊞ Y₂ := biprod.fst ≫ f₁₁ ≫ biprod.inl + biprod.fst ≫ f₁₂ ≫ biprod.inr + biprod.snd ≫ f₂₁ ≫ biprod.inl + biprod.snd ≫ f₂₂ ≫ biprod.inr @[simp] lemma biprod.inl_of_components : biprod.inl ≫ biprod.of_components f₁₁ f₁₂ f₂₁ f₂₂ = f₁₁ ≫ biprod.inl + f₁₂ ≫ biprod.inr := by simp [biprod.of_components] @[simp] lemma biprod.inr_of_components : biprod.inr ≫ biprod.of_components f₁₁ f₁₂ f₂₁ f₂₂ = f₂₁ ≫ biprod.inl + f₂₂ ≫ biprod.inr := by simp [biprod.of_components] @[simp] lemma biprod.of_components_fst : biprod.of_components f₁₁ f₁₂ f₂₁ f₂₂ ≫ biprod.fst = biprod.fst ≫ f₁₁ + biprod.snd ≫ f₂₁ := by simp [biprod.of_components] @[simp] lemma biprod.of_components_snd : biprod.of_components f₁₁ f₁₂ f₂₁ f₂₂ ≫ biprod.snd = biprod.fst ≫ f₁₂ + biprod.snd ≫ f₂₂ := by simp [biprod.of_components] @[simp] lemma biprod.of_components_eq (f : X₁ ⊞ X₂ ⟶ Y₁ ⊞ Y₂) : biprod.of_components (biprod.inl ≫ f ≫ biprod.fst) (biprod.inl ≫ f ≫ biprod.snd) (biprod.inr ≫ f ≫ biprod.fst) (biprod.inr ≫ f ≫ biprod.snd) = f := begin ext; simp, end @[simp] lemma biprod.of_components_comp {X₁ X₂ Y₁ Y₂ Z₁ Z₂ : C} (f₁₁ : X₁ ⟶ Y₁) (f₁₂ : X₁ ⟶ Y₂) (f₂₁ : X₂ ⟶ Y₁) (f₂₂ : X₂ ⟶ Y₂) (g₁₁ : Y₁ ⟶ Z₁) (g₁₂ : Y₁ ⟶ Z₂) (g₂₁ : Y₂ ⟶ Z₁) (g₂₂ : Y₂ ⟶ Z₂) : biprod.of_components f₁₁ f₁₂ f₂₁ f₂₂ ≫ biprod.of_components g₁₁ g₁₂ g₂₁ g₂₂ = biprod.of_components (f₁₁ ≫ g₁₁ + f₁₂ ≫ g₂₁) (f₁₁ ≫ g₁₂ + f₁₂ ≫ g₂₂) (f₂₁ ≫ g₁₁ + f₂₂ ≫ g₂₁) (f₂₁ ≫ g₁₂ + f₂₂ ≫ g₂₂) := begin dsimp [biprod.of_components], apply biprod.hom_ext; apply biprod.hom_ext'; simp only [add_comp, comp_add, add_comp_assoc, add_zero, zero_add, biprod.inl_fst, biprod.inl_snd, biprod.inr_fst, biprod.inr_snd, biprod.inl_fst_assoc, biprod.inl_snd_assoc, biprod.inr_fst_assoc, biprod.inr_snd_assoc, comp_zero, zero_comp, category.comp_id, category.assoc], end /-- The unipotent upper triangular matrix ``` (1 r) (0 1) ``` as an isomorphism. -/ @[simps] def biprod.unipotent_upper {X₁ X₂ : C} (r : X₁ ⟶ X₂) : X₁ ⊞ X₂ ≅ X₁ ⊞ X₂ := { hom := biprod.of_components (𝟙 _) r 0 (𝟙 _), inv := biprod.of_components (𝟙 _) (-r) 0 (𝟙 _), } /-- The unipotent lower triangular matrix ``` (1 0) (r 1) ``` as an isomorphism. -/ @[simps] def biprod.unipotent_lower {X₁ X₂ : C} (r : X₂ ⟶ X₁) : X₁ ⊞ X₂ ≅ X₁ ⊞ X₂ := { hom := biprod.of_components (𝟙 _) 0 r (𝟙 _), inv := biprod.of_components (𝟙 _) 0 (-r) (𝟙 _), } /-- If `f` is a morphism `X₁ ⊞ X₂ ⟶ Y₁ ⊞ Y₂` whose `X₁ ⟶ Y₁` entry is an isomorphism, then we can construct isomorphisms `L : X₁ ⊞ X₂ ≅ X₁ ⊞ X₂` and `R : Y₁ ⊞ Y₂ ≅ Y₁ ⊞ Y₂` so that `L.hom ≫ g ≫ R.hom` is diagonal (with `X₁ ⟶ Y₁` component still `f`), via Gaussian elimination. (This is the version of `biprod.gaussian` written in terms of components.) -/ def biprod.gaussian' [is_iso f₁₁] : Σ' (L : X₁ ⊞ X₂ ≅ X₁ ⊞ X₂) (R : Y₁ ⊞ Y₂ ≅ Y₁ ⊞ Y₂) (g₂₂ : X₂ ⟶ Y₂), L.hom ≫ (biprod.of_components f₁₁ f₁₂ f₂₁ f₂₂) ≫ R.hom = biprod.map f₁₁ g₂₂ := ⟨biprod.unipotent_lower (-(f₂₁ ≫ inv f₁₁)), biprod.unipotent_upper (-(inv f₁₁ ≫ f₁₂)), f₂₂ - f₂₁ ≫ (inv f₁₁) ≫ f₁₂, by ext; simp; abel⟩ /-- If `f` is a morphism `X₁ ⊞ X₂ ⟶ Y₁ ⊞ Y₂` whose `X₁ ⟶ Y₁` entry is an isomorphism, then we can construct isomorphisms `L : X₁ ⊞ X₂ ≅ X₁ ⊞ X₂` and `R : Y₁ ⊞ Y₂ ≅ Y₁ ⊞ Y₂` so that `L.hom ≫ g ≫ R.hom` is diagonal (with `X₁ ⟶ Y₁` component still `f`), via Gaussian elimination. -/ def biprod.gaussian (f : X₁ ⊞ X₂ ⟶ Y₁ ⊞ Y₂) [is_iso (biprod.inl ≫ f ≫ biprod.fst)] : Σ' (L : X₁ ⊞ X₂ ≅ X₁ ⊞ X₂) (R : Y₁ ⊞ Y₂ ≅ Y₁ ⊞ Y₂) (g₂₂ : X₂ ⟶ Y₂), L.hom ≫ f ≫ R.hom = biprod.map (biprod.inl ≫ f ≫ biprod.fst) g₂₂ := begin let := biprod.gaussian' (biprod.inl ≫ f ≫ biprod.fst) (biprod.inl ≫ f ≫ biprod.snd) (biprod.inr ≫ f ≫ biprod.fst) (biprod.inr ≫ f ≫ biprod.snd), simpa [biprod.of_components_eq], end /-- If `X₁ ⊞ X₂ ≅ Y₁ ⊞ Y₂` via a two-by-two matrix whose `X₁ ⟶ Y₁` entry is an isomorphism, then we can construct an isomorphism `X₂ ≅ Y₂`, via Gaussian elimination. -/ def biprod.iso_elim' [is_iso f₁₁] [is_iso (biprod.of_components f₁₁ f₁₂ f₂₁ f₂₂)] : X₂ ≅ Y₂ := begin obtain ⟨L, R, g, w⟩ := biprod.gaussian' f₁₁ f₁₂ f₂₁ f₂₂, letI : is_iso (biprod.map f₁₁ g) := by { rw ←w, apply_instance, }, letI : is_iso g := (is_iso_right_of_is_iso_biprod_map f₁₁ g), exact as_iso g, end /-- If `f` is an isomorphism `X₁ ⊞ X₂ ≅ Y₁ ⊞ Y₂` whose `X₁ ⟶ Y₁` entry is an isomorphism, then we can construct an isomorphism `X₂ ≅ Y₂`, via Gaussian elimination. -/ def biprod.iso_elim (f : X₁ ⊞ X₂ ≅ Y₁ ⊞ Y₂) [is_iso (biprod.inl ≫ f.hom ≫ biprod.fst)] : X₂ ≅ Y₂ := begin letI : is_iso (biprod.of_components (biprod.inl ≫ f.hom ≫ biprod.fst) (biprod.inl ≫ f.hom ≫ biprod.snd) (biprod.inr ≫ f.hom ≫ biprod.fst) (biprod.inr ≫ f.hom ≫ biprod.snd)) := by { simp only [biprod.of_components_eq], apply_instance, }, exact biprod.iso_elim' (biprod.inl ≫ f.hom ≫ biprod.fst) (biprod.inl ≫ f.hom ≫ biprod.snd) (biprod.inr ≫ f.hom ≫ biprod.fst) (biprod.inr ≫ f.hom ≫ biprod.snd) end lemma biprod.column_nonzero_of_iso {W X Y Z : C} (f : W ⊞ X ⟶ Y ⊞ Z) [is_iso f] : 𝟙 W = 0 ∨ biprod.inl ≫ f ≫ biprod.fst ≠ 0 ∨ biprod.inl ≫ f ≫ biprod.snd ≠ 0 := begin by_contra' h, rcases h with ⟨nz, a₁, a₂⟩, set x := biprod.inl ≫ f ≫ inv f ≫ biprod.fst, have h₁ : x = 𝟙 W, by simp [x], have h₀ : x = 0, { dsimp [x], rw [←category.id_comp (inv f), category.assoc, ←biprod.total], conv_lhs { slice 2 3, rw [comp_add], }, simp only [category.assoc], rw [comp_add_assoc, add_comp], conv_lhs { congr, skip, slice 1 3, rw a₂, }, simp only [zero_comp, add_zero], conv_lhs { slice 1 3, rw a₁, }, simp only [zero_comp], }, exact nz (h₁.symm.trans h₀), end end variables [preadditive.{v} C] lemma biproduct.column_nonzero_of_iso' {σ τ : Type} [fintype τ] {S : σ → C} [has_biproduct S] {T : τ → C} [has_biproduct T] (s : σ) (f : ⨁ S ⟶ ⨁ T) [is_iso f] : (∀ t : τ, biproduct.ι S s ≫ f ≫ biproduct.π T t = 0) → 𝟙 (S s) = 0 := begin intro z, set x := biproduct.ι S s ≫ f ≫ inv f ≫ biproduct.π S s, have h₁ : x = 𝟙 (S s), by simp [x], have h₀ : x = 0, { dsimp [x], rw [←category.id_comp (inv f), category.assoc, ←biproduct.total], simp only [comp_sum_assoc], conv_lhs { congr, apply_congr, skip, simp only [reassoc_of z], }, simp, }, exact h₁.symm.trans h₀, end /-- If `f : ⨁ S ⟶ ⨁ T` is an isomorphism, and `s` is a non-trivial summand of the source, then there is some `t` in the target so that the `s, t` matrix entry of `f` is nonzero. -/ def biproduct.column_nonzero_of_iso {σ τ : Type} [fintype τ] {S : σ → C} [has_biproduct S] {T : τ → C} [has_biproduct T] (s : σ) (nz : 𝟙 (S s) ≠ 0) (f : ⨁ S ⟶ ⨁ T) [is_iso f] : trunc (Σ' t : τ, biproduct.ι S s ≫ f ≫ biproduct.π T t ≠ 0) := begin classical, apply trunc_sigma_of_exists, have t := biproduct.column_nonzero_of_iso'.{v} s f, by_contradiction h, simp only [not_exists_not] at h, exact nz (t h) end end category_theory
5136ac403df2d5fc45fcb5b676203e829053974c
037dba89703a79cd4a4aec5e959818147f97635d
/src/2020/relations/random_reln_transitive2.lean
1dd1b358dd73abfa836bf2f3fb7fbd3eef8f754d
[]
no_license
ImperialCollegeLondon/M40001_lean
3a6a09298da395ab51bc220a535035d45bbe919b
62a76fa92654c855af2b2fc2bef8e60acd16ccec
refs/heads/master
1,666,750,403,259
1,665,771,117,000
1,665,771,117,000
209,141,835
115
12
null
1,640,270,596,000
1,568,749,174,000
Lean
UTF-8
Lean
false
false
640
lean
import tactic -- Let X be the set {A,B,C} inductive X | A : X | B : X | C : X open X def R (x y : X) : Prop := (x = A ∧ y = B) ∨ (x = A ∧ y = C) -- R(A,B) and R(A,C) both true, everything else false theorem R_fst {x y : X} : R x y → x = A := begin intro h, cases h, cases h, assumption, cases h, assumption, end example : transitive R := begin intros x y z, intro hxy, intro hyz, have h1 := R_fst hxy, have h2 := R_fst hyz, subst h1, subst h2, assumption, end example (P Q : Prop) : (P → Q) → (¬ Q → ¬ P) := begin intro hPQ, intro hnQ, intro hP, apply hnQ, exact hPQ hP, end
36b20bf50a619f7d00c4cf575559d71589a46af9
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/hcongr.lean
d066ed2a7c2230145a1e94beffdadf87a40e9bda
[ "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
685
lean
import Lean inductive Vec (α : Type u) : Nat → Type u | nil : Vec α 0 | cons : α → Vec α n → Vec α (n+1) def Vec.map (f : α → β) : Vec α n → Vec β n | nil => nil | cons a as => cons (f a) (map f as) open Lean open Lean.Meta def tstHCongr (f : Expr) : MetaM Unit := do let result ← mkHCongr f check result.proof IO.println (← ppExpr result.type) IO.println (← ppExpr result.proof) unless (← isDefEq result.type (← inferType result.proof)) do throwError "invalid proof" #eval tstHCongr (mkConst ``Vec.map [levelZero, levelZero]) #eval tstHCongr (mkApp2 (mkConst ``Vec.map [levelZero, levelZero]) (mkConst ``Nat) (mkConst ``Nat))
44d266e477c0d302f070565a36e4783b87649980
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/tests/lean/run/elabIte.lean
88c619a3b5f933293fbc63db8455fc7c28ae7e62
[ "Apache-2.0" ]
permissive
mhuisi/lean4
28d35a4febc2e251c7f05492e13f3b05d6f9b7af
dda44bc47f3e5d024508060dac2bcb59fd12e4c0
refs/heads/master
1,621,225,489,283
1,585,142,689,000
1,585,142,689,000
250,590,438
0
2
Apache-2.0
1,602,443,220,000
1,585,327,814,000
C
UTF-8
Lean
false
false
74
lean
new_frontend def f (x y : Nat) : Option Nat := if x > y then x else none
71e411a53ba538155209ebb8b8a734e12b505e18
957a80ea22c5abb4f4670b250d55534d9db99108
/library/init/meta/contradiction_tactic.lean
329279d5d03dcb76609ecd751bfb258109f84923
[ "Apache-2.0" ]
permissive
GaloisInc/lean
aa1e64d604051e602fcf4610061314b9a37ab8cd
f1ec117a24459b59c6ff9e56a1d09d9e9e60a6c0
refs/heads/master
1,592,202,909,807
1,504,624,387,000
1,504,624,387,000
75,319,626
2
1
Apache-2.0
1,539,290,164,000
1,480,616,104,000
C++
UTF-8
Lean
false
false
2,627
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.meta.tactic init.function namespace tactic open expr tactic decidable environment private meta def contra_p_not_p : list expr → list expr → tactic unit | [] Hs := failed | (H1 :: Rs) Hs := do t ← infer_type H1 >>= whnf, (do a ← match_not t, H2 ← find_same_type a Hs, tgt ← target, pr ← mk_app `absurd [tgt, H2, H1], exact pr) <|> contra_p_not_p Rs Hs private meta def contra_false : list expr → tactic unit | [] := failed | (H :: Hs) := do t ← infer_type H, if is_false t then do tgt ← target, pr ← mk_app `false.rec [tgt, H], exact pr else contra_false Hs private meta def contra_not_a_refl_rel_a : list expr → tactic unit | [] := failed | (H :: Hs) := do t ← infer_type H >>= head_beta, (do (lhs, rhs) ← match_ne t, unify lhs rhs, tgt ← target, refl_pr ← mk_app `eq.refl [lhs], mk_app `absurd [tgt, refl_pr, H] >>= exact) <|> (do p ← match_not t, (refl_lemma, lhs, rhs) ← match_refl_app p, unify lhs rhs, tgt ← target, refl_pr ← mk_app refl_lemma [lhs], mk_app `absurd [tgt, refl_pr, H] >>= exact) <|> contra_not_a_refl_rel_a Hs private meta def contra_constructor_eq : list expr → tactic unit | [] := failed | (H :: Hs) := do t ← infer_type H >>= whnf, match t with | `((%%lhs_0 : %%α) = %%rhs_0) := do env ← get_env, lhs ← whnf lhs_0, rhs ← whnf rhs_0, if is_constructor_app env lhs ∧ is_constructor_app env rhs ∧ const_name (get_app_fn lhs) ≠ const_name (get_app_fn rhs) then do tgt ← target, I_name ← return $ name.get_prefix (const_name (get_app_fn lhs)), pr ← mk_app (I_name <.> "no_confusion") [tgt, lhs, rhs, H], exact pr else contra_constructor_eq Hs | _ := contra_constructor_eq Hs end meta def contradiction : tactic unit := do try intro1, ctx ← local_context, (contra_false ctx <|> contra_not_a_refl_rel_a ctx <|> contra_p_not_p ctx ctx <|> contra_constructor_eq ctx <|> fail "contradiction tactic failed") meta def exfalso : tactic unit := do fail_if_no_goals, assert `Hfalse (expr.const `false []), swap, contradiction end tactic
60654678a1f317e6b0569474d2b5dbde55ae884c
f4bff2062c030df03d65e8b69c88f79b63a359d8
/src/kb_real_defs.lean
b9894fff29bd52b0df2ef540dbdb1933767d85fb
[ "Apache-2.0" ]
permissive
adastra7470/real-number-game
776606961f52db0eb824555ed2f8e16f92216ea3
f9dcb7d9255a79b57e62038228a23346c2dc301b
refs/heads/master
1,669,221,575,893
1,594,669,800,000
1,594,669,800,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
117
lean
import data.real.basic open set lemma mem_Icc_iff {x a b : ℝ} : x ∈ Icc a b ↔ a ≤ x ∧ x ≤ b := iff.rfl
8bac6f990bdfcd3279dfd5d340004531bb0a8f8a
e94d3f31e48d06d252ee7307fe71efe1d500f274
/library/algebra/order.lean
03adaa6f7aaaaa1bdd91575579fcf2ff1e088174
[ "Apache-2.0" ]
permissive
GallagherCommaJack/lean
e4471240a069d82f97cb361d2bf1a029de3f4256
226f8bafeb9baaa5a2ac58000c83d6beb29991e2
refs/heads/master
1,610,725,100,482
1,459,194,829,000
1,459,195,377,000
55,377,224
0
0
null
1,459,731,701,000
1,459,731,700,000
null
UTF-8
Lean
false
false
17,557
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jeremy Avigad Weak orders "≤", strict orders "<", and structures that include both. -/ import logic.eq logic.connectives algebra.binary algebra.priority open eq eq.ops function variables {A : Type} /- weak orders -/ structure weak_order [class] (A : Type) extends has_le A := (le_refl : ∀a, le a a) (le_trans : ∀a b c, le a b → le b c → le a c) (le_antisymm : ∀a b, le a b → le b a → a = b) section variables [weak_order A] theorem le.refl [refl] (a : A) : a ≤ a := !weak_order.le_refl theorem le_of_eq {a b : A} (H : a = b) : a ≤ b := H ▸ le.refl a theorem le.trans [trans] {a b c : A} : a ≤ b → b ≤ c → a ≤ c := !weak_order.le_trans theorem ge.trans [trans] {a b c : A} (H1 : a ≥ b) (H2: b ≥ c) : a ≥ c := le.trans H2 H1 theorem le.antisymm {a b : A} : a ≤ b → b ≤ a → a = b := !weak_order.le_antisymm -- Alternate syntax. (Abbreviations do not migrate well.) theorem eq_of_le_of_ge {a b : A} : a ≤ b → b ≤ a → a = b := !le.antisymm end structure linear_weak_order [class] (A : Type) extends weak_order A := (le_total : ∀a b, le a b ∨ le b a) section variables [linear_weak_order A] theorem le.total (a b : A) : a ≤ b ∨ b ≤ a := !linear_weak_order.le_total theorem le_of_not_ge {a b : A} (H : ¬ a ≥ b) : a ≤ b := or.resolve_left !le.total H end /- strict orders -/ structure strict_order [class] (A : Type) extends has_lt A := (lt_irrefl : ∀a, ¬ lt a a) (lt_trans : ∀a b c, lt a b → lt b c → lt a c) section variable [strict_order A] theorem lt.irrefl (a : A) : ¬ a < a := !strict_order.lt_irrefl theorem not_lt_self (a : A) : ¬ a < a := !lt.irrefl -- alternate syntax theorem lt_self_iff_false (a : A) : a < a ↔ false := iff_false_intro (lt.irrefl a) theorem lt.trans [trans] {a b c : A} : a < b → b < c → a < c := !strict_order.lt_trans theorem gt.trans [trans] {a b c : A} (H1 : a > b) (H2: b > c) : a > c := lt.trans H2 H1 theorem ne_of_lt {a b : A} (lt_ab : a < b) : a ≠ b := assume eq_ab : a = b, show false, from lt.irrefl b (eq_ab ▸ lt_ab) theorem ne_of_gt {a b : A} (gt_ab : a > b) : a ≠ b := ne.symm (ne_of_lt gt_ab) theorem lt.asymm {a b : A} (H : a < b) : ¬ b < a := assume H1 : b < a, lt.irrefl _ (lt.trans H H1) theorem not_lt_of_gt {a b : A} (H : a > b) : ¬ a < b := !lt.asymm H -- alternate syntax end /- well-founded orders -/ structure wf_strict_order [class] (A : Type) extends strict_order A := (wf_rec : ∀P : A → Type, (∀x, (∀y, lt y x → P y) → P x) → ∀x, P x) definition wf.rec_on {A : Type} [s : wf_strict_order A] {P : A → Type} (x : A) (H : ∀x, (∀y, wf_strict_order.lt y x → P y) → P x) : P x := wf_strict_order.wf_rec P H x theorem wf.ind_on.{u v} {A : Type.{u}} [s : wf_strict_order.{u 0} A] {P : A → Prop} (x : A) (H : ∀x, (∀y, wf_strict_order.lt y x → P y) → P x) : P x := wf.rec_on x H /- structures with a weak and a strict order -/ structure order_pair [class] (A : Type) extends weak_order A, has_lt A := (le_of_lt : ∀ a b, lt a b → le a b) (lt_of_lt_of_le : ∀ a b c, lt a b → le b c → lt a c) (lt_of_le_of_lt : ∀ a b c, le a b → lt b c → lt a c) (lt_irrefl : ∀ a, ¬ lt a a) section variable [s : order_pair A] variables {a b c : A} include s theorem le_of_lt : a < b → a ≤ b := !order_pair.le_of_lt theorem lt_of_lt_of_le [trans] : a < b → b ≤ c → a < c := !order_pair.lt_of_lt_of_le theorem lt_of_le_of_lt [trans] : a ≤ b → b < c → a < c := !order_pair.lt_of_le_of_lt private theorem lt_irrefl (s' : order_pair A) (a : A) : ¬ a < a := !order_pair.lt_irrefl private theorem lt_trans (s' : order_pair A) (a b c: A) (lt_ab : a < b) (lt_bc : b < c) : a < c := lt_of_lt_of_le lt_ab (le_of_lt lt_bc) definition order_pair.to_strict_order [trans_instance] : strict_order A := ⦃ strict_order, s, lt_irrefl := lt_irrefl s, lt_trans := lt_trans s ⦄ theorem gt_of_gt_of_ge [trans] (H1 : a > b) (H2 : b ≥ c) : a > c := lt_of_le_of_lt H2 H1 theorem gt_of_ge_of_gt [trans] (H1 : a ≥ b) (H2 : b > c) : a > c := lt_of_lt_of_le H2 H1 theorem not_le_of_gt (H : a > b) : ¬ a ≤ b := assume H1 : a ≤ b, lt.irrefl _ (lt_of_lt_of_le H H1) theorem not_lt_of_ge (H : a ≥ b) : ¬ a < b := assume H1 : a < b, lt.irrefl _ (lt_of_le_of_lt H H1) end structure strong_order_pair [class] (A : Type) extends weak_order A, has_lt A := (le_iff_lt_or_eq : ∀a b, le a b ↔ lt a b ∨ a = b) (lt_irrefl : ∀ a, ¬ lt a a) section strong_order_pair variable [strong_order_pair A] theorem le_iff_lt_or_eq {a b : A} : a ≤ b ↔ a < b ∨ a = b := !strong_order_pair.le_iff_lt_or_eq theorem lt_or_eq_of_le {a b : A} (le_ab : a ≤ b) : a < b ∨ a = b := iff.mp le_iff_lt_or_eq le_ab theorem le_of_lt_or_eq {a b : A} (lt_or_eq : a < b ∨ a = b) : a ≤ b := iff.mpr le_iff_lt_or_eq lt_or_eq private theorem lt_irrefl' (a : A) : ¬ a < a := !strong_order_pair.lt_irrefl private theorem le_of_lt' (a b : A) : a < b → a ≤ b := take Hlt, le_of_lt_or_eq (or.inl Hlt) private theorem lt_iff_le_and_ne {a b : A} : a < b ↔ (a ≤ b ∧ a ≠ b) := iff.intro (take Hlt, and.intro (le_of_lt_or_eq (or.inl Hlt)) (take Hab, absurd (Hab ▸ Hlt) !lt_irrefl')) (take Hand, have Hor : a < b ∨ a = b, from lt_or_eq_of_le (and.left Hand), or_resolve_left Hor (and.right Hand)) theorem lt_of_le_of_ne {a b : A} : a ≤ b → a ≠ b → a < b := take H1 H2, iff.mpr lt_iff_le_and_ne (and.intro H1 H2) private theorem ne_of_lt' {a b : A} (H : a < b) : a ≠ b := and.right ((iff.mp lt_iff_le_and_ne) H) private theorem lt_of_lt_of_le' (a b c : A) : a < b → b ≤ c → a < c := assume lt_ab : a < b, assume le_bc : b ≤ c, have le_ac : a ≤ c, from le.trans (le_of_lt' _ _ lt_ab) le_bc, have ne_ac : a ≠ c, from assume eq_ac : a = c, have le_ba : b ≤ a, from eq_ac⁻¹ ▸ le_bc, have eq_ab : a = b, from le.antisymm (le_of_lt' _ _ lt_ab) le_ba, show false, from ne_of_lt' lt_ab eq_ab, show a < c, from iff.mpr (lt_iff_le_and_ne) (and.intro le_ac ne_ac) theorem lt_of_le_of_lt' (a b c : A) : a ≤ b → b < c → a < c := assume le_ab : a ≤ b, assume lt_bc : b < c, have le_ac : a ≤ c, from le.trans le_ab (le_of_lt' _ _ lt_bc), have ne_ac : a ≠ c, from assume eq_ac : a = c, have le_cb : c ≤ b, from eq_ac ▸ le_ab, have eq_bc : b = c, from le.antisymm (le_of_lt' _ _ lt_bc) le_cb, show false, from ne_of_lt' lt_bc eq_bc, show a < c, from iff.mpr (lt_iff_le_and_ne) (and.intro le_ac ne_ac) end strong_order_pair definition strong_order_pair.to_order_pair [trans_instance] [s : strong_order_pair A] : order_pair A := ⦃ order_pair, s, lt_irrefl := lt_irrefl', le_of_lt := le_of_lt', lt_of_le_of_lt := lt_of_le_of_lt', lt_of_lt_of_le := lt_of_lt_of_le' ⦄ /- linear orders -/ structure linear_order_pair [class] (A : Type) extends order_pair A, linear_weak_order A structure linear_strong_order_pair [class] (A : Type) extends strong_order_pair A, linear_weak_order A definition linear_strong_order_pair.to_linear_order_pair [trans_instance] [s : linear_strong_order_pair A] : linear_order_pair A := ⦃ linear_order_pair, s, strong_order_pair.to_order_pair ⦄ section variable [linear_strong_order_pair A] variables (a b c : A) theorem lt.trichotomy : a < b ∨ a = b ∨ b < a := or.elim (le.total a b) (assume H : a ≤ b, or.elim (iff.mp !le_iff_lt_or_eq H) (assume H1, or.inl H1) (assume H1, or.inr (or.inl H1))) (assume H : b ≤ a, or.elim (iff.mp !le_iff_lt_or_eq H) (assume H1, or.inr (or.inr H1)) (assume H1, or.inr (or.inl (H1⁻¹)))) theorem lt.by_cases {a b : A} {P : Prop} (H1 : a < b → P) (H2 : a = b → P) (H3 : b < a → P) : P := or.elim !lt.trichotomy (assume H, H1 H) (assume H, or.elim H (assume H', H2 H') (assume H', H3 H')) definition lt_ge_by_cases {a b : A} {P : Prop} (H1 : a < b → P) (H2 : a ≥ b → P) : P := lt.by_cases H1 (λH, H2 (H ▸ le.refl a)) (λH, H2 (le_of_lt H)) theorem le_of_not_gt {a b : A} (H : ¬ a > b) : a ≤ b := lt.by_cases (assume H', absurd H' H) (assume H', H' ▸ !le.refl) (assume H', le_of_lt H') theorem lt_of_not_ge {a b : A} (H : ¬ a ≥ b) : a < b := lt.by_cases (assume H', absurd (le_of_lt H') H) (assume H', absurd (H' ▸ !le.refl) H) (assume H', H') theorem lt_or_ge : a < b ∨ a ≥ b := lt.by_cases (assume H1 : a < b, or.inl H1) (assume H1 : a = b, or.inr (H1 ▸ le.refl a)) (assume H1 : a > b, or.inr (le_of_lt H1)) theorem le_or_gt : a ≤ b ∨ a > b := !or.swap (lt_or_ge b a) theorem lt_or_gt_of_ne {a b : A} (H : a ≠ b) : a < b ∨ a > b := lt.by_cases (assume H1, or.inl H1) (assume H1, absurd H1 H) (assume H1, or.inr H1) end open decidable structure decidable_linear_order [class] (A : Type) extends linear_strong_order_pair A := (decidable_lt : decidable_rel lt) section variable [s : decidable_linear_order A] variables {a b c d : A} include s open decidable definition decidable_lt [instance] : decidable (a < b) := @decidable_linear_order.decidable_lt _ _ _ _ definition decidable_le [instance] : decidable (a ≤ b) := by_cases (assume H : a < b, inl (le_of_lt H)) (assume H : ¬ a < b, have H1 : b ≤ a, from le_of_not_gt H, by_cases (assume H2 : b < a, inr (not_le_of_gt H2)) (assume H2 : ¬ b < a, inl (le_of_not_gt H2))) definition has_decidable_eq [instance] : decidable (a = b) := by_cases (assume H : a ≤ b, by_cases (assume H1 : b ≤ a, inl (le.antisymm H H1)) (assume H1 : ¬ b ≤ a, inr (assume H2 : a = b, H1 (H2 ▸ le.refl a)))) (assume H : ¬ a ≤ b, (inr (assume H1 : a = b, H (H1 ▸ !le.refl)))) theorem eq_or_lt_of_not_lt {a b : A} (H : ¬ a < b) : a = b ∨ b < a := if Heq : a = b then or.inl Heq else or.inr (lt_of_not_ge (λ Hge, H (lt_of_le_of_ne Hge Heq))) theorem eq_or_lt_of_le {a b : A} (H : a ≤ b) : a = b ∨ a < b := begin cases eq_or_lt_of_not_lt (not_lt_of_ge H), exact or.inl a_1⁻¹, exact or.inr a_1 end -- testing equality first may result in more definitional equalities definition lt.cases {B : Type} (a b : A) (t_lt t_eq t_gt : B) : B := if a = b then t_eq else (if a < b then t_lt else t_gt) theorem lt.cases_of_eq {B : Type} {a b : A} {t_lt t_eq t_gt : B} (H : a = b) : lt.cases a b t_lt t_eq t_gt = t_eq := if_pos H theorem lt.cases_of_lt {B : Type} {a b : A} {t_lt t_eq t_gt : B} (H : a < b) : lt.cases a b t_lt t_eq t_gt = t_lt := if_neg (ne_of_lt H) ⬝ if_pos H theorem lt.cases_of_gt {B : Type} {a b : A} {t_lt t_eq t_gt : B} (H : a > b) : lt.cases a b t_lt t_eq t_gt = t_gt := if_neg (ne.symm (ne_of_lt H)) ⬝ if_neg (lt.asymm H) definition min (a b : A) : A := if a ≤ b then a else b definition max (a b : A) : A := if a ≤ b then b else a /- these show min and max form a lattice -/ theorem min_le_left (a b : A) : min a b ≤ a := by_cases (assume H : a ≤ b, by rewrite [↑min, if_pos H]) (assume H : ¬ a ≤ b, by rewrite [↑min, if_neg H]; apply le_of_lt (lt_of_not_ge H)) theorem min_le_right (a b : A) : min a b ≤ b := by_cases (assume H : a ≤ b, by rewrite [↑min, if_pos H]; apply H) (assume H : ¬ a ≤ b, by rewrite [↑min, if_neg H]) theorem le_min {a b c : A} (H₁ : c ≤ a) (H₂ : c ≤ b) : c ≤ min a b := by_cases (assume H : a ≤ b, by rewrite [↑min, if_pos H]; apply H₁) (assume H : ¬ a ≤ b, by rewrite [↑min, if_neg H]; apply H₂) theorem le_max_left (a b : A) : a ≤ max a b := by_cases (assume H : a ≤ b, by rewrite [↑max, if_pos H]; apply H) (assume H : ¬ a ≤ b, by rewrite [↑max, if_neg H]) theorem le_max_right (a b : A) : b ≤ max a b := by_cases (assume H : a ≤ b, by rewrite [↑max, if_pos H]) (assume H : ¬ a ≤ b, by rewrite [↑max, if_neg H]; apply le_of_lt (lt_of_not_ge H)) theorem max_le {a b c : A} (H₁ : a ≤ c) (H₂ : b ≤ c) : max a b ≤ c := by_cases (assume H : a ≤ b, by rewrite [↑max, if_pos H]; apply H₂) (assume H : ¬ a ≤ b, by rewrite [↑max, if_neg H]; apply H₁) theorem le_max_left_iff_true (a b : A) : a ≤ max a b ↔ true := iff_true_intro (le_max_left a b) theorem le_max_right_iff_true (a b : A) : b ≤ max a b ↔ true := iff_true_intro (le_max_right a b) /- these are also proved for lattices, but with inf and sup in place of min and max -/ theorem eq_min {a b c : A} (H₁ : c ≤ a) (H₂ : c ≤ b) (H₃ : ∀{d}, d ≤ a → d ≤ b → d ≤ c) : c = min a b := le.antisymm (le_min H₁ H₂) (H₃ !min_le_left !min_le_right) theorem min.comm (a b : A) : min a b = min b a := eq_min !min_le_right !min_le_left (λ c H₁ H₂, le_min H₂ H₁) theorem min.assoc (a b c : A) : min (min a b) c = min a (min b c) := begin apply eq_min, { apply le.trans, apply min_le_left, apply min_le_left }, { apply le_min, apply le.trans, apply min_le_left, apply min_le_right, apply min_le_right }, { intros [d, H₁, H₂], apply le_min, apply le_min H₁, apply le.trans H₂, apply min_le_left, apply le.trans H₂, apply min_le_right } end theorem min.left_comm (a b c : A) : min a (min b c) = min b (min a c) := binary.left_comm (@min.comm A s) (@min.assoc A s) a b c theorem min.right_comm (a b c : A) : min (min a b) c = min (min a c) b := binary.right_comm (@min.comm A s) (@min.assoc A s) a b c theorem min_self (a : A) : min a a = a := by apply eq.symm; apply eq_min (le.refl a) !le.refl; intros; assumption theorem min_eq_left {a b : A} (H : a ≤ b) : min a b = a := by apply eq.symm; apply eq_min !le.refl H; intros; assumption theorem min_eq_right {a b : A} (H : b ≤ a) : min a b = b := eq.subst !min.comm (min_eq_left H) theorem eq_max {a b c : A} (H₁ : a ≤ c) (H₂ : b ≤ c) (H₃ : ∀{d}, a ≤ d → b ≤ d → c ≤ d) : c = max a b := le.antisymm (H₃ !le_max_left !le_max_right) (max_le H₁ H₂) theorem max.comm (a b : A) : max a b = max b a := eq_max !le_max_right !le_max_left (λ c H₁ H₂, max_le H₂ H₁) theorem max.assoc (a b c : A) : max (max a b) c = max a (max b c) := begin apply eq_max, { apply le.trans, apply le_max_left a b, apply le_max_left }, { apply max_le, apply le.trans, apply le_max_right a b, apply le_max_left, apply le_max_right }, { intros [d, H₁, H₂], apply max_le, apply max_le H₁, apply le.trans !le_max_left H₂, apply le.trans !le_max_right H₂} end theorem max.left_comm (a b c : A) : max a (max b c) = max b (max a c) := binary.left_comm (@max.comm A s) (@max.assoc A s) a b c theorem max.right_comm (a b c : A) : max (max a b) c = max (max a c) b := binary.right_comm (@max.comm A s) (@max.assoc A s) a b c theorem max_self (a : A) : max a a = a := by apply eq.symm; apply eq_max (le.refl a) !le.refl; intros; assumption theorem max_eq_left {a b : A} (H : b ≤ a) : max a b = a := by apply eq.symm; apply eq_max !le.refl H; intros; assumption theorem max_eq_right {a b : A} (H : a ≤ b) : max a b = b := eq.subst !max.comm (max_eq_left H) /- these rely on lt_of_lt -/ theorem min_eq_left_of_lt {a b : A} (H : a < b) : min a b = a := min_eq_left (le_of_lt H) theorem min_eq_right_of_lt {a b : A} (H : b < a) : min a b = b := min_eq_right (le_of_lt H) theorem max_eq_left_of_lt {a b : A} (H : b < a) : max a b = a := max_eq_left (le_of_lt H) theorem max_eq_right_of_lt {a b : A} (H : a < b) : max a b = b := max_eq_right (le_of_lt H) /- these use the fact that it is a linear ordering -/ theorem lt_min {a b c : A} (H₁ : a < b) (H₂ : a < c) : a < min b c := or.elim !le_or_gt (assume H : b ≤ c, by rewrite (min_eq_left H); apply H₁) (assume H : b > c, by rewrite (min_eq_right_of_lt H); apply H₂) theorem max_lt {a b c : A} (H₁ : a < c) (H₂ : b < c) : max a b < c := or.elim !le_or_gt (assume H : a ≤ b, by rewrite (max_eq_right H); apply H₂) (assume H : a > b, by rewrite (max_eq_left_of_lt H); apply H₁) end /- order instances -/ definition weak_order_Prop [instance] : weak_order Prop := ⦃ weak_order, le := λx y, x → y, le_refl := λx, id, le_trans := λa b c H1 H2 x, H2 (H1 x), le_antisymm := λf g H1 H2, propext (and.intro H1 H2) ⦄ definition weak_order_fun [instance] (A B : Type) [weak_order B] : weak_order (A → B) := ⦃ weak_order, le := λx y, ∀b, x b ≤ y b, le_refl := λf b, !le.refl, le_trans := λf g h H1 H2 b, !le.trans (H1 b) (H2 b), le_antisymm := λf g H1 H2, funext (λb, !le.antisymm (H1 b) (H2 b)) ⦄ definition weak_order_dual {A : Type} (wo : weak_order A) : weak_order A := ⦃ weak_order, le := λx y, y ≤ x, le_refl := le.refl, le_trans := take a b c `b ≤ a` `c ≤ b`, le.trans `c ≤ b` `b ≤ a`, le_antisymm := take a b `b ≤ a` `a ≤ b`, le.antisymm `a ≤ b` `b ≤ a` ⦄ lemma le_dual_eq_le {A : Type} (wo : weak_order A) (a b : A) : @le _ (@weak_order.to_has_le _ (weak_order_dual wo)) a b = @le _ (@weak_order.to_has_le _ wo) b a := rfl -- what to do with the strict variants?
50a82d7351a9a9bfc9cc914befc07989a89614cd
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/category_theory/limits/shapes/strict_initial.lean
3e64e95e5c077e979ef481e3bb4f98a38d8407d0
[ "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
8,376
lean
/- Copyright (c) 2021 Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta -/ import category_theory.limits.shapes.terminal import category_theory.limits.shapes.binary_products /-! # Strict initial objects > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file sets up the basic theory of strict initial objects: initial objects where every morphism to it is an isomorphism. This generalises a property of the empty set in the category of sets: namely that the only function to the empty set is from itself. We say `C` has strict initial objects if every initial object is strict, ie given any morphism `f : A ⟶ I` where `I` is initial, then `f` is an isomorphism. Strictly speaking, this says that *any* initial object must be strict, rather than that strict initial objects exist, which turns out to be a more useful notion to formalise. If the binary product of `X` with a strict initial object exists, it is also initial. To show a category `C` with an initial object has strict initial objects, the most convenient way is to show any morphism to the (chosen) initial object is an isomorphism and use `has_strict_initial_objects_of_initial_is_strict`. The dual notion (strict terminal objects) occurs much less frequently in practice so is ignored. ## TODO * Construct examples of this: `Type*`, `Top`, `Groupoid`, simplicial types, posets. * Construct the bottom element of the subobject lattice given strict initials. * Show cartesian closed categories have strict initials ## References * https://ncatlab.org/nlab/show/strict+initial+object -/ universes v u namespace category_theory namespace limits open category variables (C : Type u) [category.{v} C] section strict_initial /-- We say `C` has strict initial objects if every initial object is strict, ie given any morphism `f : A ⟶ I` where `I` is initial, then `f` is an isomorphism. Strictly speaking, this says that *any* initial object must be strict, rather than that strict initial objects exist. -/ class has_strict_initial_objects : Prop := (out : ∀ {I A : C} (f : A ⟶ I), is_initial I → is_iso f) variables {C} section variables [has_strict_initial_objects C] {I : C} lemma is_initial.is_iso_to (hI : is_initial I) {A : C} (f : A ⟶ I) : is_iso f := has_strict_initial_objects.out f hI lemma is_initial.strict_hom_ext (hI : is_initial I) {A : C} (f g : A ⟶ I) : f = g := begin haveI := hI.is_iso_to f, haveI := hI.is_iso_to g, exact eq_of_inv_eq_inv (hI.hom_ext (inv f) (inv g)), end lemma is_initial.subsingleton_to (hI : is_initial I) {A : C} : subsingleton (A ⟶ I) := ⟨hI.strict_hom_ext⟩ @[priority 100] instance initial_mono_of_strict_initial_objects : initial_mono_class C := { is_initial_mono_from := λ I A hI, { right_cancellation := λ B g h i, hI.strict_hom_ext _ _ } } /-- If `I` is initial, then `X ⨯ I` is isomorphic to it. -/ @[simps hom] noncomputable def mul_is_initial (X : C) [has_binary_product X I] (hI : is_initial I) : X ⨯ I ≅ I := @@as_iso _ prod.snd (hI.is_iso_to _) @[simp] lemma mul_is_initial_inv (X : C) [has_binary_product X I] (hI : is_initial I) : (mul_is_initial X hI).inv = hI.to _ := hI.hom_ext _ _ /-- If `I` is initial, then `I ⨯ X` is isomorphic to it. -/ @[simps hom] noncomputable def is_initial_mul (X : C) [has_binary_product I X] (hI : is_initial I) : I ⨯ X ≅ I := @@as_iso _ prod.fst (hI.is_iso_to _) @[simp] lemma is_initial_mul_inv (X : C) [has_binary_product I X] (hI : is_initial I) : (is_initial_mul X hI).inv = hI.to _ := hI.hom_ext _ _ variable [has_initial C] instance initial_is_iso_to {A : C} (f : A ⟶ ⊥_ C) : is_iso f := initial_is_initial.is_iso_to _ @[ext] lemma initial.hom_ext {A : C} (f g : A ⟶ ⊥_ C) : f = g := initial_is_initial.strict_hom_ext _ _ lemma initial.subsingleton_to {A : C} : subsingleton (A ⟶ ⊥_ C) := initial_is_initial.subsingleton_to /-- The product of `X` with an initial object in a category with strict initial objects is itself initial. This is the generalisation of the fact that `X × empty ≃ empty` for types (or `n * 0 = 0`). -/ @[simps hom] noncomputable def mul_initial (X : C) [has_binary_product X ⊥_ C] : X ⨯ ⊥_ C ≅ ⊥_ C := mul_is_initial _ initial_is_initial @[simp] lemma mul_initial_inv (X : C) [has_binary_product X ⊥_ C] : (mul_initial X).inv = initial.to _ := subsingleton.elim _ _ /-- The product of `X` with an initial object in a category with strict initial objects is itself initial. This is the generalisation of the fact that `empty × X ≃ empty` for types (or `0 * n = 0`). -/ @[simps hom] noncomputable def initial_mul (X : C) [has_binary_product (⊥_ C) X] : ⊥_ C ⨯ X ≅ ⊥_ C := is_initial_mul _ initial_is_initial @[simp] lemma initial_mul_inv (X : C) [has_binary_product (⊥_ C) X] : (initial_mul X).inv = initial.to _ := subsingleton.elim _ _ end /-- If `C` has an initial object such that every morphism *to* it is an isomorphism, then `C` has strict initial objects. -/ lemma has_strict_initial_objects_of_initial_is_strict [has_initial C] (h : ∀ A (f : A ⟶ ⊥_ C), is_iso f) : has_strict_initial_objects C := { out := λ I A f hI, begin haveI := h A (f ≫ hI.to _), exact ⟨⟨hI.to _ ≫ inv (f ≫ hI.to ⊥_ C), by rw [←assoc, is_iso.hom_inv_id], hI.hom_ext _ _⟩⟩, end } end strict_initial section strict_terminal /-- We say `C` has strict terminal objects if every terminal object is strict, ie given any morphism `f : I ⟶ A` where `I` is terminal, then `f` is an isomorphism. Strictly speaking, this says that *any* terminal object must be strict, rather than that strict terminal objects exist. -/ class has_strict_terminal_objects : Prop := (out : ∀ {I A : C} (f : I ⟶ A), is_terminal I → is_iso f) variables {C} section variables [has_strict_terminal_objects C] {I : C} lemma is_terminal.is_iso_from (hI : is_terminal I) {A : C} (f : I ⟶ A) : is_iso f := has_strict_terminal_objects.out f hI lemma is_terminal.strict_hom_ext (hI : is_terminal I) {A : C} (f g : I ⟶ A) : f = g := begin haveI := hI.is_iso_from f, haveI := hI.is_iso_from g, exact eq_of_inv_eq_inv (hI.hom_ext (inv f) (inv g)), end lemma is_terminal.subsingleton_to (hI : is_terminal I) {A : C} : subsingleton (I ⟶ A) := ⟨hI.strict_hom_ext⟩ variables {J : Type v} [small_category J] /-- If all but one object in a diagram is strict terminal, the the limit is isomorphic to the said object via `limit.π`. -/ lemma limit_π_is_iso_of_is_strict_terminal (F : J ⥤ C) [has_limit F] (i : J) (H : ∀ j ≠ i, is_terminal (F.obj j)) [subsingleton (i ⟶ i)] : is_iso (limit.π F i) := begin classical, refine ⟨⟨limit.lift _ ⟨_,⟨_,_⟩⟩,_,_⟩⟩, { exact λ j, dite (j = i) (λ h, eq_to_hom (by { cases h, refl })) (λ h, (H _ h).from _) }, { intros j k f, split_ifs, { cases h, cases h_1, obtain rfl : f = 𝟙 _ := subsingleton.elim _ _, simpa }, { cases h, erw category.comp_id, haveI : is_iso (F.map f) := (H _ h_1).is_iso_from _, rw ← is_iso.comp_inv_eq, apply (H _ h_1).hom_ext }, { cases h_1, apply (H _ h).hom_ext }, { apply (H _ h).hom_ext } }, { ext, rw [assoc, limit.lift_π], dsimp only, split_ifs, { cases h, rw [id_comp, eq_to_hom_refl], exact comp_id _ }, { apply (H _ h).hom_ext } }, { rw limit.lift_π, simpa } end variable [has_terminal C] instance terminal_is_iso_from {A : C} (f : ⊤_ C ⟶ A) : is_iso f := terminal_is_terminal.is_iso_from _ @[ext] lemma terminal.hom_ext {A : C} (f g : ⊤_ C ⟶ A) : f = g := terminal_is_terminal.strict_hom_ext _ _ lemma terminal.subsingleton_to {A : C} : subsingleton (⊤_ C ⟶ A) := terminal_is_terminal.subsingleton_to end /-- If `C` has an object such that every morphism *from* it is an isomorphism, then `C` has strict terminal objects. -/ lemma has_strict_terminal_objects_of_terminal_is_strict (I : C) (h : ∀ A (f : I ⟶ A), is_iso f) : has_strict_terminal_objects C := { out := λ I' A f hI', begin haveI := h A (hI'.from _ ≫ f), exact ⟨⟨inv (hI'.from I ≫ f) ≫ hI'.from I, hI'.hom_ext _ _, by rw [assoc, is_iso.inv_hom_id]⟩⟩, end } end strict_terminal end limits end category_theory
b0f978fccc90c068f4a752f52fad57914913e65c
05f637fa14ac28031cb1ea92086a0f4eb23ff2b1
/tests/lean/simp16.lean
a1e09887111b5d68057f803a44e0cc65c31c0025
[ "Apache-2.0" ]
permissive
codyroux/lean0.1
1ce92751d664aacff0529e139083304a7bbc8a71
0dc6fb974aa85ed6f305a2f4b10a53a44ee5f0ef
refs/heads/master
1,610,830,535,062
1,402,150,480,000
1,402,150,480,000
19,588,851
2
0
null
null
null
null
UTF-8
Lean
false
false
382
lean
rewrite_set simple add_rewrite and_truer and_truel and_falser and_falsel or_falsel : simple (* add_congr_theorem("simple", "and_congr") add_congr_theorem("simple", "or_congr") *) variables a b c : Nat (* local t = parse_lean([[a = 1 ∧ ((¬ b = 0 ∨ c ≠ b) ∨ b + c > a)]]) local s, pr = simplify(t, "simple") print(s) print(pr) print(get_environment():type_check(pr)) *)
766a00aff1b388ec7bb3d0357012b0c03bb72861
57aec6ee746bc7e3a3dd5e767e53bd95beb82f6d
/src/Lean/Parser.lean
5e28081eccb8ad686d7c1776fc5aca204b035943
[ "Apache-2.0" ]
permissive
collares/lean4
861a9269c4592bce49b71059e232ff0bfe4594cc
52a4f535d853a2c7c7eea5fee8a4fa04c682c1ee
refs/heads/master
1,691,419,031,324
1,618,678,138,000
1,618,678,138,000
358,989,750
0
0
Apache-2.0
1,618,696,333,000
1,618,696,333,000
null
UTF-8
Lean
false
false
6,233
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Sebastian Ullrich -/ import Lean.Parser.Basic import Lean.Parser.Level import Lean.Parser.Term import Lean.Parser.Tactic import Lean.Parser.Command import Lean.Parser.Module import Lean.Parser.Syntax import Lean.Parser.Do namespace Lean namespace Parser builtin_initialize registerAlias "ws" checkWsBefore registerAlias "noWs" checkNoWsBefore registerAlias "linebreak" checkLinebreakBefore registerAlias "num" numLit registerAlias "str" strLit registerAlias "char" charLit registerAlias "name" nameLit registerAlias "ident" ident registerAlias "colGt" checkColGt registerAlias "colGe" checkColGe registerAlias "lookahead" lookahead registerAlias "atomic" atomic registerAlias "many" many registerAlias "many1" many1 registerAlias "notFollowedBy" (notFollowedBy · "element") registerAlias "optional" optional registerAlias "withPosition" withPosition registerAlias "interpolatedStr" interpolatedStr registerAlias "orelse" orelse registerAlias "andthen" andthen end Parser namespace PrettyPrinter namespace Parenthesizer -- Close the mutual recursion loop; see corresponding `[extern]` in the parenthesizer. @[export lean_mk_antiquot_parenthesizer] def mkAntiquot.parenthesizer (name : String) (kind : Option SyntaxNodeKind) (anonymous := true) : Parenthesizer := Parser.mkAntiquot.parenthesizer name kind anonymous -- The parenthesizer auto-generated these instances correctly, but tagged them with the wrong kind, since the actual kind -- (e.g. `ident`) is not equal to the parser name `Lean.Parser.Term.ident`. @[builtinParenthesizer ident] def ident.parenthesizer : Parenthesizer := Parser.Term.ident.parenthesizer @[builtinParenthesizer numLit] def numLit.parenthesizer : Parenthesizer := Parser.Term.num.parenthesizer @[builtinParenthesizer scientificLit] def scientificLit.parenthesizer : Parenthesizer := Parser.Term.scientific.parenthesizer @[builtinParenthesizer charLit] def charLit.parenthesizer : Parenthesizer := Parser.Term.char.parenthesizer @[builtinParenthesizer strLit] def strLit.parenthesizer : Parenthesizer := Parser.Term.str.parenthesizer open Lean.Parser @[export lean_pretty_printer_parenthesizer_interpret_parser_descr] unsafe def interpretParserDescr : ParserDescr → CoreM Parenthesizer | ParserDescr.const n => getConstAlias parenthesizerAliasesRef n | ParserDescr.unary n d => return (← getUnaryAlias parenthesizerAliasesRef n) (← interpretParserDescr d) | ParserDescr.binary n d₁ d₂ => return (← getBinaryAlias parenthesizerAliasesRef n) (← interpretParserDescr d₁) (← interpretParserDescr d₂) | ParserDescr.node k prec d => return leadingNode.parenthesizer k prec (← interpretParserDescr d) | ParserDescr.nodeWithAntiquot _ k d => return node.parenthesizer k (← interpretParserDescr d) | ParserDescr.sepBy p sep psep trail => return sepBy.parenthesizer (← interpretParserDescr p) sep (← interpretParserDescr psep) trail | ParserDescr.sepBy1 p sep psep trail => return sepBy1.parenthesizer (← interpretParserDescr p) sep (← interpretParserDescr psep) trail | ParserDescr.trailingNode k prec lhsPrec d => return trailingNode.parenthesizer k prec lhsPrec (← interpretParserDescr d) | ParserDescr.symbol tk => return symbol.parenthesizer tk | ParserDescr.nonReservedSymbol tk includeIdent => return nonReservedSymbol.parenthesizer tk includeIdent | ParserDescr.parser constName => combinatorParenthesizerAttribute.runDeclFor constName | ParserDescr.cat catName prec => return categoryParser.parenthesizer catName prec end Parenthesizer namespace Formatter @[export lean_mk_antiquot_formatter] def mkAntiquot.formatter (name : String) (kind : Option SyntaxNodeKind) (anonymous := true) : Formatter := Parser.mkAntiquot.formatter name kind anonymous @[builtinFormatter ident] def ident.formatter : Formatter := Parser.Term.ident.formatter @[builtinFormatter numLit] def numLit.formatter : Formatter := Parser.Term.num.formatter @[builtinFormatter scientificLit] def scientificLit.formatter : Formatter := Parser.Term.scientific.formatter @[builtinFormatter charLit] def charLit.formatter : Formatter := Parser.Term.char.formatter @[builtinFormatter strLit] def strLit.formatter : Formatter := Parser.Term.str.formatter open Lean.Parser @[export lean_pretty_printer_formatter_interpret_parser_descr] unsafe def interpretParserDescr : ParserDescr → CoreM Formatter | ParserDescr.const n => getConstAlias formatterAliasesRef n | ParserDescr.unary n d => return (← getUnaryAlias formatterAliasesRef n) (← interpretParserDescr d) | ParserDescr.binary n d₁ d₂ => return (← getBinaryAlias formatterAliasesRef n) (← interpretParserDescr d₁) (← interpretParserDescr d₂) | ParserDescr.node k prec d => return node.formatter k (← interpretParserDescr d) | ParserDescr.nodeWithAntiquot _ k d => return node.formatter k (← interpretParserDescr d) | ParserDescr.sepBy p sep psep trail => return sepBy.formatter (← interpretParserDescr p) sep (← interpretParserDescr psep) trail | ParserDescr.sepBy1 p sep psep trail => return sepBy1.formatter (← interpretParserDescr p) sep (← interpretParserDescr psep) trail | ParserDescr.trailingNode k prec lhsPrec d => return trailingNode.formatter k prec lhsPrec (← interpretParserDescr d) | ParserDescr.symbol tk => return symbol.formatter tk | ParserDescr.nonReservedSymbol tk includeIdent => return nonReservedSymbol.formatter tk | ParserDescr.parser constName => combinatorFormatterAttribute.runDeclFor constName | ParserDescr.cat catName prec => return categoryParser.formatter catName end Formatter end PrettyPrinter end Lean
fbc87abd6e3b0bde1d2244b38c4657c1112fda85
f2fbd9ce3f46053c664b74a5294d7d2f584e72d3
/src/for_mathlib/ring_completion.lean
204fe9ad9a49425d70b4fa8f0aa3356a1acab583
[ "Apache-2.0" ]
permissive
jcommelin/lean-perfectoid-spaces
c656ae26a2338ee7a0072dab63baf577f079ca12
d5ed816bcc116fd4cde5ce9aaf03905d00ee391c
refs/heads/master
1,584,610,432,107
1,538,491,594,000
1,538,491,594,000
136,299,168
0
0
null
1,528,274,452,000
1,528,274,452,000
null
UTF-8
Lean
false
false
1,010
lean
import tactic.ring import for_mathlib.topological_structures import for_mathlib.group_completion noncomputable theory open filter uniform_space function section topological_ring universe u variables {A : Type u} [topological_space A] [comm_ring A] [topological_ring A] def uncurry_mul : completion A × completion A → completion A := completion.map₂ (*) instance completion_mul : has_mul (completion A) := ⟨curry (uncurry_mul)⟩ instance : ring (completion A) := begin refine_struct { one := ((1 : A) : completion A), mul := (*), ..completion_group_str}, repeat { sorry } end instance : uniform_space (completion A) := topological_add_group.to_uniform_space (completion A) instance completion_ring_top : topological_ring (completion A) := sorry variables {B : Type u} [topological_space B] [comm_ring B] [topological_ring B] instance completion_map_ring_hom {f : A → B} [is_add_group_hom f] (h : continuous f) : is_ring_hom (completion.map f) := sorry end topological_ring
8bd02598ab82753d0e872438d6276197ea1f768e
cf39355caa609c0f33405126beee2739aa3cb77e
/library/init/data/ordering/lemmas.lean
f227b5ef43a31e91a6405f7ea87005d6c5720861
[ "Apache-2.0" ]
permissive
leanprover-community/lean
12b87f69d92e614daea8bcc9d4de9a9ace089d0e
cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0
refs/heads/master
1,687,508,156,644
1,684,951,104,000
1,684,951,104,000
169,960,991
457
107
Apache-2.0
1,686,744,372,000
1,549,790,268,000
C++
UTF-8
Lean
false
false
1,593
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.data.ordering.basic init.meta init.algebra.classes init.ite_simp set_option default_priority 100 universes u namespace ordering @[simp] theorem ite_eq_lt_distrib (c : Prop) [decidable c] (a b : ordering) : ((if c then a else b) = ordering.lt) = (if c then a = ordering.lt else b = ordering.lt) := by by_cases c; simp [*] @[simp] theorem ite_eq_eq_distrib (c : Prop) [decidable c] (a b : ordering) : ((if c then a else b) = ordering.eq) = (if c then a = ordering.eq else b = ordering.eq) := by by_cases c; simp [*] @[simp] theorem ite_eq_gt_distrib (c : Prop) [decidable c] (a b : ordering) : ((if c then a else b) = ordering.gt) = (if c then a = ordering.gt else b = ordering.gt) := by by_cases c; simp [*] /- ------------------------------------------------------------------ -/ end ordering section variables {α : Type u} {lt : α → α → Prop} [decidable_rel lt] local attribute [simp] cmp_using @[simp] lemma cmp_using_eq_lt (a b : α) : (cmp_using lt a b = ordering.lt) = lt a b := by simp @[simp] lemma cmp_using_eq_gt [is_strict_order α lt] (a b : α) : (cmp_using lt a b = ordering.gt) = lt b a := begin simp, apply propext, apply iff.intro, { exact λ h, h.2 }, { intro hba, split, { intro hab, exact absurd (trans hab hba) (irrefl a) }, { assumption } } end @[simp] lemma cmp_using_eq_eq (a b : α) : (cmp_using lt a b = ordering.eq) = (¬ lt a b ∧ ¬ lt b a) := by simp end
b4c2716745ee3c9d8cde880cc42b0fda80ac0601
9028d228ac200bbefe3a711342514dd4e4458bff
/src/data/rat/order.lean
b3d6db2a8efb028411cb136efa5f74bcba82e474
[ "Apache-2.0" ]
permissive
mcncm/mathlib
8d25099344d9d2bee62822cb9ed43aa3e09fa05e
fde3d78cadeec5ef827b16ae55664ef115e66f57
refs/heads/master
1,672,743,316,277
1,602,618,514,000
1,602,618,514,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
8,743
lean
/- Copyright (c) 2019 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro -/ import data.rat.basic /-! # Order for Rational Numbers ## Summary We define the order on `ℚ`, prove that `ℚ` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Notations - `/.` is infix notation for `rat.mk`. ## Tags rat, rationals, field, ℚ, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace rat variables (a b c : ℚ) open_locale rat protected def nonneg : ℚ → Prop | ⟨n, d, h, c⟩ := 0 ≤ n @[simp] theorem mk_nonneg (a : ℤ) {b : ℤ} (h : 0 < b) : (a /. b).nonneg ↔ 0 ≤ a := begin generalize ha : a /. b = x, cases x with n₁ d₁ h₁ c₁, rw num_denom' at ha, simp [rat.nonneg], have d0 := int.coe_nat_lt.2 h₁, have := (mk_eq (ne_of_gt h) (ne_of_gt d0)).1 ha, constructor; intro h₂, { apply nonneg_of_mul_nonneg_right _ d0, rw this, exact mul_nonneg h₂ (le_of_lt h) }, { apply nonneg_of_mul_nonneg_right _ h, rw ← this, exact mul_nonneg h₂ (int.coe_zero_le _) }, end protected lemma nonneg_add {a b} : rat.nonneg a → rat.nonneg b → rat.nonneg (a + b) := num_denom_cases_on' a $ λ n₁ d₁ h₁, num_denom_cases_on' b $ λ n₂ d₂ h₂, begin have d₁0 : 0 < (d₁:ℤ) := int.coe_nat_pos.2 (nat.pos_of_ne_zero h₁), have d₂0 : 0 < (d₂:ℤ) := int.coe_nat_pos.2 (nat.pos_of_ne_zero h₂), simp [d₁0, d₂0, h₁, h₂, mul_pos d₁0 d₂0], intros n₁0 n₂0, apply add_nonneg; apply mul_nonneg; {assumption <|> apply int.coe_zero_le}, end protected lemma nonneg_mul {a b} : rat.nonneg a → rat.nonneg b → rat.nonneg (a * b) := num_denom_cases_on' a $ λ n₁ d₁ h₁, num_denom_cases_on' b $ λ n₂ d₂ h₂, begin have d₁0 : 0 < (d₁:ℤ) := int.coe_nat_pos.2 (nat.pos_of_ne_zero h₁), have d₂0 : 0 < (d₂:ℤ) := int.coe_nat_pos.2 (nat.pos_of_ne_zero h₂), simp [d₁0, d₂0, h₁, h₂, mul_pos d₁0 d₂0, mul_nonneg] { contextual := tt } end protected lemma nonneg_antisymm {a} : rat.nonneg a → rat.nonneg (-a) → a = 0 := num_denom_cases_on' a $ λ n d h, begin have d0 : 0 < (d:ℤ) := int.coe_nat_pos.2 (nat.pos_of_ne_zero h), simp [d0, h], exact λ h₁ h₂, le_antisymm h₂ h₁ end protected lemma nonneg_total : rat.nonneg a ∨ rat.nonneg (-a) := by cases a with n; exact or.imp_right neg_nonneg_of_nonpos (le_total 0 n) instance decidable_nonneg : decidable (rat.nonneg a) := by cases a; unfold rat.nonneg; apply_instance protected def le (a b : ℚ) := rat.nonneg (b - a) instance : has_le ℚ := ⟨rat.le⟩ instance decidable_le : decidable_rel ((≤) : ℚ → ℚ → Prop) | a b := show decidable (rat.nonneg (b - a)), by apply_instance protected theorem le_def {a b c d : ℤ} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≤ c /. d ↔ a * d ≤ c * b := begin show rat.nonneg _ ↔ _, rw ← sub_nonneg, simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] end protected theorem le_refl : a ≤ a := show rat.nonneg (a - a), by rw sub_self; exact le_refl (0 : ℤ) protected theorem le_total : a ≤ b ∨ b ≤ a := by have := rat.nonneg_total (b - a); rwa neg_sub at this protected theorem le_antisymm {a b : ℚ} (hab : a ≤ b) (hba : b ≤ a) : a = b := by have := eq_neg_of_add_eq_zero (rat.nonneg_antisymm hba $ by simpa); rwa neg_neg at this protected theorem le_trans {a b c : ℚ} (hab : a ≤ b) (hbc : b ≤ c) : a ≤ c := have rat.nonneg (b - a + (c - b)), from rat.nonneg_add hab hbc, by simpa [sub_eq_add_neg, add_comm, add_left_comm] instance : decidable_linear_order ℚ := { le := rat.le, le_refl := rat.le_refl, le_trans := @rat.le_trans, le_antisymm := @rat.le_antisymm, le_total := rat.le_total, decidable_eq := by apply_instance, decidable_le := assume a b, rat.decidable_nonneg (b - a) } /- Extra instances to short-circuit type class resolution -/ instance : has_lt ℚ := by apply_instance instance : distrib_lattice ℚ := by apply_instance instance : lattice ℚ := by apply_instance instance : semilattice_inf ℚ := by apply_instance instance : semilattice_sup ℚ := by apply_instance instance : has_inf ℚ := by apply_instance instance : has_sup ℚ := by apply_instance instance : linear_order ℚ := by apply_instance instance : partial_order ℚ := by apply_instance instance : preorder ℚ := by apply_instance protected lemma le_def' {p q : ℚ} : p ≤ q ↔ p.num * q.denom ≤ q.num * p.denom := begin rw [←(@num_denom q), ←(@num_denom p)], conv_rhs { simp only [num_denom] }, exact rat.le_def (by exact_mod_cast p.pos) (by exact_mod_cast q.pos) end protected lemma lt_def {p q : ℚ} : p < q ↔ p.num * q.denom < q.num * p.denom := begin rw [lt_iff_le_and_ne, rat.le_def'], suffices : p ≠ q ↔ p.num * q.denom ≠ q.num * p.denom, by { split; intro h, { exact lt_iff_le_and_ne.elim_right ⟨h.left, (this.elim_left h.right)⟩ }, { have tmp := lt_iff_le_and_ne.elim_left h, exact ⟨tmp.left, this.elim_right tmp.right⟩ }}, exact (not_iff_not.elim_right eq_iff_mul_eq_mul) end theorem nonneg_iff_zero_le {a} : rat.nonneg a ↔ 0 ≤ a := show rat.nonneg a ↔ rat.nonneg (a - 0), by simp theorem num_nonneg_iff_zero_le : ∀ {a : ℚ}, 0 ≤ a.num ↔ 0 ≤ a | ⟨n, d, h, c⟩ := @nonneg_iff_zero_le ⟨n, d, h, c⟩ protected theorem add_le_add_left {a b c : ℚ} : c + a ≤ c + b ↔ a ≤ b := by unfold has_le.le rat.le; rw add_sub_add_left_eq_sub protected theorem mul_nonneg {a b : ℚ} (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ a * b := by rw ← nonneg_iff_zero_le at ha hb ⊢; exact rat.nonneg_mul ha hb instance : discrete_linear_ordered_field ℚ := { zero_lt_one := dec_trivial, add_le_add_left := assume a b ab c, rat.add_le_add_left.2 ab, mul_pos := assume a b ha hb, lt_of_le_of_ne (rat.mul_nonneg (le_of_lt ha) (le_of_lt hb)) (mul_ne_zero (ne_of_lt ha).symm (ne_of_lt hb).symm).symm, ..rat.field, ..rat.decidable_linear_order, ..rat.semiring } /- Extra instances to short-circuit type class resolution -/ instance : linear_ordered_field ℚ := by apply_instance instance : decidable_linear_ordered_comm_ring ℚ := by apply_instance instance : linear_ordered_comm_ring ℚ := by apply_instance instance : linear_ordered_ring ℚ := by apply_instance instance : ordered_ring ℚ := by apply_instance instance : decidable_linear_ordered_semiring ℚ := by apply_instance instance : linear_ordered_semiring ℚ := by apply_instance instance : ordered_semiring ℚ := by apply_instance instance : decidable_linear_ordered_add_comm_group ℚ := by apply_instance instance : ordered_add_comm_group ℚ := by apply_instance instance : ordered_cancel_add_comm_monoid ℚ := by apply_instance instance : ordered_add_comm_monoid ℚ := by apply_instance attribute [irreducible] rat.le theorem num_pos_iff_pos {a : ℚ} : 0 < a.num ↔ 0 < a := lt_iff_lt_of_le_iff_le $ by simpa [(by cases a; refl : (-a).num = -a.num)] using @num_nonneg_iff_zero_le (-a) lemma div_lt_div_iff_mul_lt_mul {a b c d : ℤ} (b_pos : 0 < b) (d_pos : 0 < d) : (a : ℚ) / b < c / d ↔ a * d < c * b := begin simp only [lt_iff_le_not_le], apply and_congr, { simp [div_num_denom, (rat.le_def b_pos d_pos)] }, { apply not_iff_not_of_iff, simp [div_num_denom, (rat.le_def d_pos b_pos)] } end lemma lt_one_iff_num_lt_denom {q : ℚ} : q < 1 ↔ q.num < q.denom := begin cases decidable.em (0 < q) with q_pos q_nonpos, { simp [rat.lt_def] }, { replace q_nonpos : q ≤ 0, from not_lt.elim_left q_nonpos, have : q.num < q.denom, by { have : ¬0 < q.num ↔ ¬0 < q, from not_iff_not.elim_right num_pos_iff_pos, simp only [not_lt] at this, exact lt_of_le_of_lt (this.elim_right q_nonpos) (by exact_mod_cast q.pos) }, simp only [this, (lt_of_le_of_lt q_nonpos zero_lt_one)] } end theorem abs_def (q : ℚ) : abs q = q.num.nat_abs /. q.denom := begin have hz : (0:ℚ) = 0 /. 1 := rfl, cases le_total q 0 with hq hq, { rw [abs_of_nonpos hq], rw [←(@num_denom q), hz, rat.le_def (int.coe_nat_pos.2 q.pos) zero_lt_one, mul_one, zero_mul] at hq, rw [int.of_nat_nat_abs_of_nonpos hq, ← neg_def, num_denom] }, { rw [abs_of_nonneg hq], rw [←(@num_denom q), hz, rat.le_def zero_lt_one (int.coe_nat_pos.2 q.pos), mul_one, zero_mul] at hq, rw [int.nat_abs_of_nonneg hq, num_denom] } end end rat
86d0768ef0ff60c92206c143e8c96f3bfaf4c86e
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/tests/lean/run/tacticExtOverlap.lean
befde5144656393c1f220e39da37e7e8cff0e644
[ "Apache-2.0" ]
permissive
mhuisi/lean4
28d35a4febc2e251c7f05492e13f3b05d6f9b7af
dda44bc47f3e5d024508060dac2bcb59fd12e4c0
refs/heads/master
1,621,225,489,283
1,585,142,689,000
1,585,142,689,000
250,590,438
0
2
Apache-2.0
1,602,443,220,000
1,585,327,814,000
C
UTF-8
Lean
false
false
577
lean
new_frontend open Lean syntax [myintro] "intros" (sepBy ident ",") : tactic macro_rules [myintro] | `(tactic| intros $x*) => pure $ Syntax.node `Lean.Parser.Tactic.intros #[Syntax.atom none "intros", mkNullNode x.getSepElems] theorem tst1 {p q : Prop} : p → q → p := begin intros h1, h2; assumption end theorem tst2 {p q : Prop} : p → q → p := begin intros h1; -- the builtin and myintro overlap here. intros h2; -- the builtin and myintro overlap here. assumption end theorem tst3 {p q : Prop} : p → q → p := begin intros h1 h2; assumption end
fd7180dc8ba5d0d0a4a5d4f889208957094a0cd5
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/geometry/euclidean/circumcenter.lean
a65c0db65410bf8545857f96aeb7bbc8f6617273
[ "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
44,379
lean
/- Copyright (c) 2020 Joseph Myers. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joseph Myers -/ import geometry.euclidean.sphere.basic import linear_algebra.affine_space.finite_dimensional import tactic.derive_fintype /-! # Circumcenter and circumradius > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file proves some lemmas on points equidistant from a set of points, and defines the circumradius and circumcenter of a simplex. There are also some definitions for use in calculations where it is convenient to work with affine combinations of vertices together with the circumcenter. ## Main definitions * `circumcenter` and `circumradius` are the circumcenter and circumradius of a simplex. ## References * https://en.wikipedia.org/wiki/Circumscribed_circle -/ noncomputable theory open_locale big_operators open_locale classical open_locale real_inner_product_space namespace euclidean_geometry variables {V : Type*} {P : Type*} [normed_add_comm_group V] [inner_product_space ℝ V] [metric_space P] [normed_add_torsor V P] include V open affine_subspace /-- `p` is equidistant from two points in `s` if and only if its `orthogonal_projection` is. -/ lemma dist_eq_iff_dist_orthogonal_projection_eq {s : affine_subspace ℝ P} [nonempty s] [complete_space s.direction] {p1 p2 : P} (p3 : P) (hp1 : p1 ∈ s) (hp2 : p2 ∈ s) : dist p1 p3 = dist p2 p3 ↔ dist p1 (orthogonal_projection s p3) = dist p2 (orthogonal_projection s p3) := begin rw [←mul_self_inj_of_nonneg dist_nonneg dist_nonneg, ←mul_self_inj_of_nonneg dist_nonneg dist_nonneg, dist_sq_eq_dist_orthogonal_projection_sq_add_dist_orthogonal_projection_sq p3 hp1, dist_sq_eq_dist_orthogonal_projection_sq_add_dist_orthogonal_projection_sq p3 hp2], simp end /-- `p` is equidistant from a set of points in `s` if and only if its `orthogonal_projection` is. -/ lemma dist_set_eq_iff_dist_orthogonal_projection_eq {s : affine_subspace ℝ P} [nonempty s] [complete_space s.direction] {ps : set P} (hps : ps ⊆ s) (p : P) : set.pairwise ps (λ p1 p2, dist p1 p = dist p2 p) ↔ (set.pairwise ps (λ p1 p2, dist p1 (orthogonal_projection s p) = dist p2 (orthogonal_projection s p))) := ⟨λ h p1 hp1 p2 hp2 hne, (dist_eq_iff_dist_orthogonal_projection_eq p (hps hp1) (hps hp2)).1 (h hp1 hp2 hne), λ h p1 hp1 p2 hp2 hne, (dist_eq_iff_dist_orthogonal_projection_eq p (hps hp1) (hps hp2)).2 (h hp1 hp2 hne)⟩ /-- There exists `r` such that `p` has distance `r` from all the points of a set of points in `s` if and only if there exists (possibly different) `r` such that its `orthogonal_projection` has that distance from all the points in that set. -/ lemma exists_dist_eq_iff_exists_dist_orthogonal_projection_eq {s : affine_subspace ℝ P} [nonempty s] [complete_space s.direction] {ps : set P} (hps : ps ⊆ s) (p : P) : (∃ r, ∀ p1 ∈ ps, dist p1 p = r) ↔ ∃ r, ∀ p1 ∈ ps, dist p1 ↑(orthogonal_projection s p) = r := begin have h := dist_set_eq_iff_dist_orthogonal_projection_eq hps p, simp_rw set.pairwise_eq_iff_exists_eq at h, exact h end /-- The induction step for the existence and uniqueness of the circumcenter. Given a nonempty set of points in a nonempty affine subspace whose direction is complete, such that there is a unique (circumcenter, circumradius) pair for those points in that subspace, and a point `p` not in that subspace, there is a unique (circumcenter, circumradius) pair for the set with `p` added, in the span of the subspace with `p` added. -/ lemma exists_unique_dist_eq_of_insert {s : affine_subspace ℝ P} [complete_space s.direction] {ps : set P} (hnps : ps.nonempty) {p : P} (hps : ps ⊆ s) (hp : p ∉ s) (hu : ∃! cs : sphere P, cs.center ∈ s ∧ ps ⊆ (cs : set P)) : ∃! cs₂ : sphere P, cs₂.center ∈ affine_span ℝ (insert p (s : set P)) ∧ (insert p ps) ⊆ (cs₂ : set P) := begin haveI : nonempty s := set.nonempty.to_subtype (hnps.mono hps), rcases hu with ⟨⟨cc, cr⟩, ⟨hcc, hcr⟩, hcccru⟩, simp only at hcc hcr hcccru, let x := dist cc (orthogonal_projection s p), let y := dist p (orthogonal_projection s p), have hy0 : y ≠ 0 := dist_orthogonal_projection_ne_zero_of_not_mem hp, let ycc₂ := (x * x + y * y - cr * cr) / (2 * y), let cc₂ := (ycc₂ / y) • (p -ᵥ orthogonal_projection s p : V) +ᵥ cc, let cr₂ := real.sqrt (cr * cr + ycc₂ * ycc₂), use ⟨cc₂, cr₂⟩, simp only, have hpo : p = (1 : ℝ) • (p -ᵥ orthogonal_projection s p : V) +ᵥ orthogonal_projection s p, { simp }, split, { split, { refine vadd_mem_of_mem_direction _ (mem_affine_span ℝ (set.mem_insert_of_mem _ hcc)), rw direction_affine_span, exact submodule.smul_mem _ _ (vsub_mem_vector_span ℝ (set.mem_insert _ _) (set.mem_insert_of_mem _ (orthogonal_projection_mem _))) }, { intros p1 hp1, rw [sphere.mem_coe, mem_sphere, ←mul_self_inj_of_nonneg dist_nonneg (real.sqrt_nonneg _), real.mul_self_sqrt (add_nonneg (mul_self_nonneg _) (mul_self_nonneg _))], cases hp1, { rw hp1, rw [hpo, dist_sq_smul_orthogonal_vadd_smul_orthogonal_vadd (orthogonal_projection_mem p) hcc _ _ (vsub_orthogonal_projection_mem_direction_orthogonal s p), ←dist_eq_norm_vsub V p, dist_comm _ cc], field_simp [hy0], ring }, { rw [dist_sq_eq_dist_orthogonal_projection_sq_add_dist_orthogonal_projection_sq _ (hps hp1), orthogonal_projection_vadd_smul_vsub_orthogonal_projection _ _ hcc, subtype.coe_mk, dist_of_mem_subset_mk_sphere hp1 hcr, dist_eq_norm_vsub V cc₂ cc, vadd_vsub, norm_smul, ←dist_eq_norm_vsub V, real.norm_eq_abs, abs_div, abs_of_nonneg dist_nonneg, div_mul_cancel _ hy0, abs_mul_abs_self] } } }, { rintros ⟨cc₃, cr₃⟩ ⟨hcc₃, hcr₃⟩, simp only at hcc₃ hcr₃, obtain ⟨t₃, cc₃', hcc₃', hcc₃''⟩ : ∃ (r : ℝ) (p0 : P) (hp0 : p0 ∈ s), cc₃ = r • (p -ᵥ ↑((orthogonal_projection s) p)) +ᵥ p0, { rwa mem_affine_span_insert_iff (orthogonal_projection_mem p) at hcc₃ }, have hcr₃' : ∃ r, ∀ p1 ∈ ps, dist p1 cc₃ = r := ⟨cr₃, λ p1 hp1, dist_of_mem_subset_mk_sphere (set.mem_insert_of_mem _ hp1) hcr₃⟩, rw [exists_dist_eq_iff_exists_dist_orthogonal_projection_eq hps cc₃, hcc₃'', orthogonal_projection_vadd_smul_vsub_orthogonal_projection _ _ hcc₃'] at hcr₃', cases hcr₃' with cr₃' hcr₃', have hu := hcccru ⟨cc₃', cr₃'⟩, simp only at hu, replace hu := hu ⟨hcc₃', hcr₃'⟩, cases hu with hucc hucr, substs hucc hucr, have hcr₃val : cr₃ = real.sqrt (cr₃' * cr₃' + (t₃ * y) * (t₃ * y)), { cases hnps with p0 hp0, have h' : ↑(⟨cc₃', hcc₃'⟩ : s) = cc₃' := rfl, rw [←dist_of_mem_subset_mk_sphere (set.mem_insert_of_mem _ hp0) hcr₃, hcc₃'', ←mul_self_inj_of_nonneg dist_nonneg (real.sqrt_nonneg _), real.mul_self_sqrt (add_nonneg (mul_self_nonneg _) (mul_self_nonneg _)), dist_sq_eq_dist_orthogonal_projection_sq_add_dist_orthogonal_projection_sq _ (hps hp0), orthogonal_projection_vadd_smul_vsub_orthogonal_projection _ _ hcc₃', h', dist_of_mem_subset_mk_sphere hp0 hcr, dist_eq_norm_vsub V _ cc₃', vadd_vsub, norm_smul, ←dist_eq_norm_vsub V p, real.norm_eq_abs, ←mul_assoc, mul_comm _ (|t₃|), ←mul_assoc, abs_mul_abs_self], ring }, replace hcr₃ := dist_of_mem_subset_mk_sphere (set.mem_insert _ _) hcr₃, rw [hpo, hcc₃'', hcr₃val, ←mul_self_inj_of_nonneg dist_nonneg (real.sqrt_nonneg _), dist_sq_smul_orthogonal_vadd_smul_orthogonal_vadd (orthogonal_projection_mem p) hcc₃' _ _ (vsub_orthogonal_projection_mem_direction_orthogonal s p), dist_comm, ←dist_eq_norm_vsub V p, real.mul_self_sqrt (add_nonneg (mul_self_nonneg _) (mul_self_nonneg _))] at hcr₃, change x * x + _ * (y * y) = _ at hcr₃, rw [(show x * x + (1 - t₃) * (1 - t₃) * (y * y) = x * x + y * y - 2 * y * (t₃ * y) + t₃ * y * (t₃ * y), by ring), add_left_inj] at hcr₃, have ht₃ : t₃ = ycc₂ / y, { field_simp [←hcr₃, hy0], ring }, subst ht₃, change cc₃ = cc₂ at hcc₃'', congr', rw hcr₃val, congr' 2, field_simp [hy0], ring } end /-- Given a finite nonempty affinely independent family of points, there is a unique (circumcenter, circumradius) pair for those points in the affine subspace they span. -/ lemma _root_.affine_independent.exists_unique_dist_eq {ι : Type*} [hne : nonempty ι] [finite ι] {p : ι → P} (ha : affine_independent ℝ p) : ∃! cs : sphere P, cs.center ∈ affine_span ℝ (set.range p) ∧ set.range p ⊆ (cs : set P) := begin casesI nonempty_fintype ι, unfreezingI { induction hn : fintype.card ι with m hm generalizing ι }, { exfalso, have h := fintype.card_pos_iff.2 hne, rw hn at h, exact lt_irrefl 0 h }, { cases m, { rw fintype.card_eq_one_iff at hn, cases hn with i hi, haveI : unique ι := ⟨⟨i⟩, hi⟩, use ⟨p i, 0⟩, simp only [set.range_unique, affine_subspace.mem_affine_span_singleton], split, { simp_rw [hi default, set.singleton_subset_iff, sphere.mem_coe, mem_sphere, dist_self], exact ⟨rfl, rfl⟩ }, { rintros ⟨cc, cr⟩, simp only, rintros ⟨rfl, hdist⟩, simp_rw [set.singleton_subset_iff, sphere.mem_coe, mem_sphere, dist_self] at hdist, rw [hi default, hdist], exact ⟨rfl, rfl⟩ } }, { have i := hne.some, let ι2 := {x // x ≠ i}, have hc : fintype.card ι2 = m + 1, { rw fintype.card_of_subtype (finset.univ.filter (λ x, x ≠ i)), { rw finset.filter_not, simp_rw eq_comm, rw [finset.filter_eq, if_pos (finset.mem_univ _), finset.card_sdiff (finset.subset_univ _), finset.card_singleton, finset.card_univ, hn], simp }, { simp } }, haveI : nonempty ι2 := fintype.card_pos_iff.1 (hc.symm ▸ nat.zero_lt_succ _), have ha2 : affine_independent ℝ (λ i2 : ι2, p i2) := ha.subtype _, replace hm := hm ha2 _ hc, have hr : set.range p = insert (p i) (set.range (λ i2 : ι2, p i2)), { change _ = insert _ (set.range (λ i2 : {x | x ≠ i}, p i2)), rw [←set.image_eq_range, ←set.image_univ, ←set.image_insert_eq], congr' with j, simp [classical.em] }, rw [hr, ←affine_span_insert_affine_span], refine exists_unique_dist_eq_of_insert (set.range_nonempty _) (subset_span_points ℝ _) _ hm, convert ha.not_mem_affine_span_diff i set.univ, change set.range (λ i2 : {x | x ≠ i}, p i2) = _, rw ←set.image_eq_range, congr' with j, simp, refl } } end end euclidean_geometry namespace affine namespace simplex open finset affine_subspace euclidean_geometry variables {V : Type*} {P : Type*} [normed_add_comm_group V] [inner_product_space ℝ V] [metric_space P] [normed_add_torsor V P] include V /-- The circumsphere of a simplex. -/ def circumsphere {n : ℕ} (s : simplex ℝ P n) : sphere P := s.independent.exists_unique_dist_eq.some /-- The property satisfied by the circumsphere. -/ lemma circumsphere_unique_dist_eq {n : ℕ} (s : simplex ℝ P n) : (s.circumsphere.center ∈ affine_span ℝ (set.range s.points) ∧ set.range s.points ⊆ s.circumsphere) ∧ (∀ cs : sphere P, (cs.center ∈ affine_span ℝ (set.range s.points) ∧ set.range s.points ⊆ cs → cs = s.circumsphere)) := s.independent.exists_unique_dist_eq.some_spec /-- The circumcenter of a simplex. -/ def circumcenter {n : ℕ} (s : simplex ℝ P n) : P := s.circumsphere.center /-- The circumradius of a simplex. -/ def circumradius {n : ℕ} (s : simplex ℝ P n) : ℝ := s.circumsphere.radius /-- The center of the circumsphere is the circumcenter. -/ @[simp] lemma circumsphere_center {n : ℕ} (s : simplex ℝ P n) : s.circumsphere.center = s.circumcenter := rfl /-- The radius of the circumsphere is the circumradius. -/ @[simp] lemma circumsphere_radius {n : ℕ} (s : simplex ℝ P n) : s.circumsphere.radius = s.circumradius := rfl /-- The circumcenter lies in the affine span. -/ lemma circumcenter_mem_affine_span {n : ℕ} (s : simplex ℝ P n) : s.circumcenter ∈ affine_span ℝ (set.range s.points) := s.circumsphere_unique_dist_eq.1.1 /-- All points have distance from the circumcenter equal to the circumradius. -/ @[simp] lemma dist_circumcenter_eq_circumradius {n : ℕ} (s : simplex ℝ P n) (i : fin (n + 1)) : dist (s.points i) s.circumcenter = s.circumradius := dist_of_mem_subset_sphere (set.mem_range_self _) s.circumsphere_unique_dist_eq.1.2 /-- All points lie in the circumsphere. -/ lemma mem_circumsphere {n : ℕ} (s : simplex ℝ P n) (i : fin (n + 1)) : s.points i ∈ s.circumsphere := s.dist_circumcenter_eq_circumradius i /-- All points have distance to the circumcenter equal to the circumradius. -/ @[simp] lemma dist_circumcenter_eq_circumradius' {n : ℕ} (s : simplex ℝ P n) : ∀ i, dist s.circumcenter (s.points i) = s.circumradius := begin intro i, rw dist_comm, exact dist_circumcenter_eq_circumradius _ _ end /-- Given a point in the affine span from which all the points are equidistant, that point is the circumcenter. -/ lemma eq_circumcenter_of_dist_eq {n : ℕ} (s : simplex ℝ P n) {p : P} (hp : p ∈ affine_span ℝ (set.range s.points)) {r : ℝ} (hr : ∀ i, dist (s.points i) p = r) : p = s.circumcenter := begin have h := s.circumsphere_unique_dist_eq.2 ⟨p, r⟩, simp only [hp, hr, forall_const, eq_self_iff_true, subset_sphere, sphere.ext_iff, set.forall_range_iff, mem_sphere, true_and] at h, exact h.1 end /-- Given a point in the affine span from which all the points are equidistant, that distance is the circumradius. -/ lemma eq_circumradius_of_dist_eq {n : ℕ} (s : simplex ℝ P n) {p : P} (hp : p ∈ affine_span ℝ (set.range s.points)) {r : ℝ} (hr : ∀ i, dist (s.points i) p = r) : r = s.circumradius := begin have h := s.circumsphere_unique_dist_eq.2 ⟨p, r⟩, simp only [hp, hr, forall_const, eq_self_iff_true, subset_sphere, sphere.ext_iff, set.forall_range_iff, mem_sphere, true_and] at h, exact h.2 end /-- The circumradius is non-negative. -/ lemma circumradius_nonneg {n : ℕ} (s : simplex ℝ P n) : 0 ≤ s.circumradius := s.dist_circumcenter_eq_circumradius 0 ▸ dist_nonneg /-- The circumradius of a simplex with at least two points is positive. -/ lemma circumradius_pos {n : ℕ} (s : simplex ℝ P (n + 1)) : 0 < s.circumradius := begin refine lt_of_le_of_ne s.circumradius_nonneg _, intro h, have hr := s.dist_circumcenter_eq_circumradius, simp_rw [←h, dist_eq_zero] at hr, have h01 := s.independent.injective.ne (dec_trivial : (0 : fin (n + 2)) ≠ 1), simpa [hr] using h01 end /-- The circumcenter of a 0-simplex equals its unique point. -/ lemma circumcenter_eq_point (s : simplex ℝ P 0) (i : fin 1) : s.circumcenter = s.points i := begin have h := s.circumcenter_mem_affine_span, rw [set.range_unique, mem_affine_span_singleton] at h, rw h, congr end /-- The circumcenter of a 1-simplex equals its centroid. -/ lemma circumcenter_eq_centroid (s : simplex ℝ P 1) : s.circumcenter = finset.univ.centroid ℝ s.points := begin have hr : set.pairwise set.univ (λ i j : fin 2, dist (s.points i) (finset.univ.centroid ℝ s.points) = dist (s.points j) (finset.univ.centroid ℝ s.points)), { intros i hi j hj hij, rw [finset.centroid_pair_fin, dist_eq_norm_vsub V (s.points i), dist_eq_norm_vsub V (s.points j), vsub_vadd_eq_vsub_sub, vsub_vadd_eq_vsub_sub, ←one_smul ℝ (s.points i -ᵥ s.points 0), ←one_smul ℝ (s.points j -ᵥ s.points 0)], fin_cases i; fin_cases j; simp [-one_smul, ←sub_smul]; norm_num }, rw set.pairwise_eq_iff_exists_eq at hr, cases hr with r hr, exact (s.eq_circumcenter_of_dist_eq (centroid_mem_affine_span_of_card_eq_add_one ℝ _ (finset.card_fin 2)) (λ i, hr i (set.mem_univ _))).symm end /-- Reindexing a simplex along an `equiv` of index types does not change the circumsphere. -/ @[simp] lemma circumsphere_reindex {m n : ℕ} (s : simplex ℝ P m) (e : fin (m + 1) ≃ fin (n + 1)) : (s.reindex e).circumsphere = s.circumsphere := begin refine s.circumsphere_unique_dist_eq.2 _ ⟨_, _⟩; rw ←s.reindex_range_points e, { exact (s.reindex e).circumsphere_unique_dist_eq.1.1 }, { exact (s.reindex e).circumsphere_unique_dist_eq.1.2 } end /-- Reindexing a simplex along an `equiv` of index types does not change the circumcenter. -/ @[simp] lemma circumcenter_reindex {m n : ℕ} (s : simplex ℝ P m) (e : fin (m + 1) ≃ fin (n + 1)) : (s.reindex e).circumcenter = s.circumcenter := by simp_rw [←circumcenter, circumsphere_reindex] /-- Reindexing a simplex along an `equiv` of index types does not change the circumradius. -/ @[simp] lemma circumradius_reindex {m n : ℕ} (s : simplex ℝ P m) (e : fin (m + 1) ≃ fin (n + 1)) : (s.reindex e).circumradius = s.circumradius := by simp_rw [←circumradius, circumsphere_reindex] local attribute [instance] affine_subspace.to_add_torsor /-- The orthogonal projection of a point `p` onto the hyperplane spanned by the simplex's points. -/ def orthogonal_projection_span {n : ℕ} (s : simplex ℝ P n) : P →ᵃ[ℝ] affine_span ℝ (set.range s.points) := orthogonal_projection (affine_span ℝ (set.range s.points)) /-- Adding a vector to a point in the given subspace, then taking the orthogonal projection, produces the original point if the vector is a multiple of the result of subtracting a point's orthogonal projection from that point. -/ lemma orthogonal_projection_vadd_smul_vsub_orthogonal_projection {n : ℕ} (s : simplex ℝ P n) {p1 : P} (p2 : P) (r : ℝ) (hp : p1 ∈ affine_span ℝ (set.range s.points)) : s.orthogonal_projection_span (r • (p2 -ᵥ s.orthogonal_projection_span p2 : V) +ᵥ p1) = ⟨p1, hp⟩ := orthogonal_projection_vadd_smul_vsub_orthogonal_projection _ _ _ lemma coe_orthogonal_projection_vadd_smul_vsub_orthogonal_projection {n : ℕ} {r₁ : ℝ} (s : simplex ℝ P n) {p p₁o : P} (hp₁o : p₁o ∈ affine_span ℝ (set.range s.points)) : ↑(s.orthogonal_projection_span (r₁ • (p -ᵥ ↑(s.orthogonal_projection_span p)) +ᵥ p₁o)) = p₁o := congr_arg coe (orthogonal_projection_vadd_smul_vsub_orthogonal_projection _ _ _ hp₁o) lemma dist_sq_eq_dist_orthogonal_projection_sq_add_dist_orthogonal_projection_sq {n : ℕ} (s : simplex ℝ P n) {p1 : P} (p2 : P) (hp1 : p1 ∈ affine_span ℝ (set.range s.points)) : dist p1 p2 * dist p1 p2 = dist p1 (s.orthogonal_projection_span p2) * dist p1 (s.orthogonal_projection_span p2) + dist p2 (s.orthogonal_projection_span p2) * dist p2 (s.orthogonal_projection_span p2) := begin rw [pseudo_metric_space.dist_comm p2 _, dist_eq_norm_vsub V p1 _, dist_eq_norm_vsub V p1 _, dist_eq_norm_vsub V _ p2, ← vsub_add_vsub_cancel p1 (s.orthogonal_projection_span p2) p2, norm_add_sq_eq_norm_sq_add_norm_sq_iff_real_inner_eq_zero], exact submodule.inner_right_of_mem_orthogonal (vsub_orthogonal_projection_mem_direction p2 hp1) (orthogonal_projection_vsub_mem_direction_orthogonal _ p2), end lemma dist_circumcenter_sq_eq_sq_sub_circumradius {n : ℕ} {r : ℝ} (s : simplex ℝ P n) {p₁ : P} (h₁ : ∀ (i : fin (n + 1)), dist (s.points i) p₁ = r) (h₁' : ↑((s.orthogonal_projection_span) p₁) = s.circumcenter) (h : s.points 0 ∈ affine_span ℝ (set.range s.points)) : dist p₁ s.circumcenter * dist p₁ s.circumcenter = r * r - s.circumradius * s.circumradius := begin rw [dist_comm, ←h₁ 0, s.dist_sq_eq_dist_orthogonal_projection_sq_add_dist_orthogonal_projection_sq p₁ h], simp only [h₁', dist_comm p₁, add_sub_cancel', simplex.dist_circumcenter_eq_circumradius], end /-- If there exists a distance that a point has from all vertices of a simplex, the orthogonal projection of that point onto the subspace spanned by that simplex is its circumcenter. -/ lemma orthogonal_projection_eq_circumcenter_of_exists_dist_eq {n : ℕ} (s : simplex ℝ P n) {p : P} (hr : ∃ r, ∀ i, dist (s.points i) p = r) : ↑(s.orthogonal_projection_span p) = s.circumcenter := begin change ∃ r : ℝ, ∀ i, (λ x, dist x p = r) (s.points i) at hr, conv at hr { congr, funext, rw ←set.forall_range_iff }, rw exists_dist_eq_iff_exists_dist_orthogonal_projection_eq (subset_affine_span ℝ _) p at hr, cases hr with r hr, exact s.eq_circumcenter_of_dist_eq (orthogonal_projection_mem p) (λ i, hr _ (set.mem_range_self i)), end /-- If a point has the same distance from all vertices of a simplex, the orthogonal projection of that point onto the subspace spanned by that simplex is its circumcenter. -/ lemma orthogonal_projection_eq_circumcenter_of_dist_eq {n : ℕ} (s : simplex ℝ P n) {p : P} {r : ℝ} (hr : ∀ i, dist (s.points i) p = r) : ↑(s.orthogonal_projection_span p) = s.circumcenter := s.orthogonal_projection_eq_circumcenter_of_exists_dist_eq ⟨r, hr⟩ /-- The orthogonal projection of the circumcenter onto a face is the circumcenter of that face. -/ lemma orthogonal_projection_circumcenter {n : ℕ} (s : simplex ℝ P n) {fs : finset (fin (n + 1))} {m : ℕ} (h : fs.card = m + 1) : ↑((s.face h).orthogonal_projection_span s.circumcenter) = (s.face h).circumcenter := begin have hr : ∃ r, ∀ i, dist ((s.face h).points i) s.circumcenter = r, { use s.circumradius, simp [face_points] }, exact orthogonal_projection_eq_circumcenter_of_exists_dist_eq _ hr end /-- Two simplices with the same points have the same circumcenter. -/ lemma circumcenter_eq_of_range_eq {n : ℕ} {s₁ s₂ : simplex ℝ P n} (h : set.range s₁.points = set.range s₂.points) : s₁.circumcenter = s₂.circumcenter := begin have hs : s₁.circumcenter ∈ affine_span ℝ (set.range s₂.points) := h ▸ s₁.circumcenter_mem_affine_span, have hr : ∀ i, dist (s₂.points i) s₁.circumcenter = s₁.circumradius, { intro i, have hi : s₂.points i ∈ set.range s₂.points := set.mem_range_self _, rw [←h, set.mem_range] at hi, rcases hi with ⟨j, hj⟩, rw [←hj, s₁.dist_circumcenter_eq_circumradius j] }, exact s₂.eq_circumcenter_of_dist_eq hs hr end omit V /-- An index type for the vertices of a simplex plus its circumcenter. This is for use in calculations where it is convenient to work with affine combinations of vertices together with the circumcenter. (An equivalent form sometimes used in the literature is placing the circumcenter at the origin and working with vectors for the vertices.) -/ @[derive fintype] inductive points_with_circumcenter_index (n : ℕ) | point_index : fin (n + 1) → points_with_circumcenter_index | circumcenter_index : points_with_circumcenter_index open points_with_circumcenter_index instance points_with_circumcenter_index_inhabited (n : ℕ) : inhabited (points_with_circumcenter_index n) := ⟨circumcenter_index⟩ /-- `point_index` as an embedding. -/ def point_index_embedding (n : ℕ) : fin (n + 1) ↪ points_with_circumcenter_index n := ⟨λ i, point_index i, λ _ _ h, by injection h⟩ /-- The sum of a function over `points_with_circumcenter_index`. -/ lemma sum_points_with_circumcenter {α : Type*} [add_comm_monoid α] {n : ℕ} (f : points_with_circumcenter_index n → α) : ∑ i, f i = (∑ (i : fin (n + 1)), f (point_index i)) + f circumcenter_index := begin have h : univ = insert circumcenter_index (univ.map (point_index_embedding n)), { ext x, refine ⟨λ h, _, λ _, mem_univ _⟩, cases x with i, { exact mem_insert_of_mem (mem_map_of_mem _ (mem_univ i)) }, { exact mem_insert_self _ _ } }, change _ = ∑ i, f (point_index_embedding n i) + _, rw [add_comm, h, ←sum_map, sum_insert], simp_rw [finset.mem_map, not_exists], intros x hx h, injection h end include V /-- The vertices of a simplex plus its circumcenter. -/ def points_with_circumcenter {n : ℕ} (s : simplex ℝ P n) : points_with_circumcenter_index n → P | (point_index i) := s.points i | circumcenter_index := s.circumcenter /-- `points_with_circumcenter`, applied to a `point_index` value, equals `points` applied to that value. -/ @[simp] lemma points_with_circumcenter_point {n : ℕ} (s : simplex ℝ P n) (i : fin (n + 1)) : s.points_with_circumcenter (point_index i) = s.points i := rfl /-- `points_with_circumcenter`, applied to `circumcenter_index`, equals the circumcenter. -/ @[simp] lemma points_with_circumcenter_eq_circumcenter {n : ℕ} (s : simplex ℝ P n) : s.points_with_circumcenter circumcenter_index = s.circumcenter := rfl omit V /-- The weights for a single vertex of a simplex, in terms of `points_with_circumcenter`. -/ def point_weights_with_circumcenter {n : ℕ} (i : fin (n + 1)) : points_with_circumcenter_index n → ℝ | (point_index j) := if j = i then 1 else 0 | circumcenter_index := 0 /-- `point_weights_with_circumcenter` sums to 1. -/ @[simp] lemma sum_point_weights_with_circumcenter {n : ℕ} (i : fin (n + 1)) : ∑ j, point_weights_with_circumcenter i j = 1 := begin convert sum_ite_eq' univ (point_index i) (function.const _ (1 : ℝ)), { ext j, cases j ; simp [point_weights_with_circumcenter] }, { simp } end include V /-- A single vertex, in terms of `points_with_circumcenter`. -/ lemma point_eq_affine_combination_of_points_with_circumcenter {n : ℕ} (s : simplex ℝ P n) (i : fin (n + 1)) : s.points i = (univ : finset (points_with_circumcenter_index n)).affine_combination ℝ s.points_with_circumcenter (point_weights_with_circumcenter i) := begin rw ←points_with_circumcenter_point, symmetry, refine affine_combination_of_eq_one_of_eq_zero _ _ _ (mem_univ _) (by simp [point_weights_with_circumcenter]) _, intros i hi hn, cases i, { have h : i_1 ≠ i := λ h, hn (h ▸ rfl), simp [point_weights_with_circumcenter, h] }, { refl } end omit V /-- The weights for the centroid of some vertices of a simplex, in terms of `points_with_circumcenter`. -/ def centroid_weights_with_circumcenter {n : ℕ} (fs : finset (fin (n + 1))) : points_with_circumcenter_index n → ℝ | (point_index i) := if i ∈ fs then ((card fs : ℝ) ⁻¹) else 0 | circumcenter_index := 0 /-- `centroid_weights_with_circumcenter` sums to 1, if the `finset` is nonempty. -/ @[simp] lemma sum_centroid_weights_with_circumcenter {n : ℕ} {fs : finset (fin (n + 1))} (h : fs.nonempty) : ∑ i, centroid_weights_with_circumcenter fs i = 1 := begin simp_rw [sum_points_with_circumcenter, centroid_weights_with_circumcenter, add_zero, ←fs.sum_centroid_weights_eq_one_of_nonempty ℝ h, set.sum_indicator_subset _ fs.subset_univ], rcongr end include V /-- The centroid of some vertices of a simplex, in terms of `points_with_circumcenter`. -/ lemma centroid_eq_affine_combination_of_points_with_circumcenter {n : ℕ} (s : simplex ℝ P n) (fs : finset (fin (n + 1))) : fs.centroid ℝ s.points = (univ : finset (points_with_circumcenter_index n)).affine_combination ℝ s.points_with_circumcenter (centroid_weights_with_circumcenter fs) := begin simp_rw [centroid_def, affine_combination_apply, weighted_vsub_of_point_apply, sum_points_with_circumcenter, centroid_weights_with_circumcenter, points_with_circumcenter_point, zero_smul, add_zero, centroid_weights, set.sum_indicator_subset_of_eq_zero (function.const (fin (n + 1)) ((card fs : ℝ)⁻¹)) (λ i wi, wi • (s.points i -ᵥ classical.choice add_torsor.nonempty)) fs.subset_univ (λ i, zero_smul ℝ _), set.indicator_apply], congr, end omit V /-- The weights for the circumcenter of a simplex, in terms of `points_with_circumcenter`. -/ def circumcenter_weights_with_circumcenter (n : ℕ) : points_with_circumcenter_index n → ℝ | (point_index i) := 0 | circumcenter_index := 1 /-- `circumcenter_weights_with_circumcenter` sums to 1. -/ @[simp] lemma sum_circumcenter_weights_with_circumcenter (n : ℕ) : ∑ i, circumcenter_weights_with_circumcenter n i = 1 := begin convert sum_ite_eq' univ circumcenter_index (function.const _ (1 : ℝ)), { ext ⟨j⟩ ; simp [circumcenter_weights_with_circumcenter] }, { simp } end include V /-- The circumcenter of a simplex, in terms of `points_with_circumcenter`. -/ lemma circumcenter_eq_affine_combination_of_points_with_circumcenter {n : ℕ} (s : simplex ℝ P n) : s.circumcenter = (univ : finset (points_with_circumcenter_index n)).affine_combination ℝ s.points_with_circumcenter (circumcenter_weights_with_circumcenter n) := begin rw ←points_with_circumcenter_eq_circumcenter, symmetry, refine affine_combination_of_eq_one_of_eq_zero _ _ _ (mem_univ _) rfl _, rintros ⟨i⟩ hi hn ; tauto end omit V /-- The weights for the reflection of the circumcenter in an edge of a simplex. This definition is only valid with `i₁ ≠ i₂`. -/ def reflection_circumcenter_weights_with_circumcenter {n : ℕ} (i₁ i₂ : fin (n + 1)) : points_with_circumcenter_index n → ℝ | (point_index i) := if i = i₁ ∨ i = i₂ then 1 else 0 | circumcenter_index := -1 /-- `reflection_circumcenter_weights_with_circumcenter` sums to 1. -/ @[simp] lemma sum_reflection_circumcenter_weights_with_circumcenter {n : ℕ} {i₁ i₂ : fin (n + 1)} (h : i₁ ≠ i₂) : ∑ i, reflection_circumcenter_weights_with_circumcenter i₁ i₂ i = 1 := begin simp_rw [sum_points_with_circumcenter, reflection_circumcenter_weights_with_circumcenter, sum_ite, sum_const, filter_or, filter_eq'], rw card_union_eq, { simp }, { simpa only [if_true, mem_univ, disjoint_singleton] using h } end include V /-- The reflection of the circumcenter of a simplex in an edge, in terms of `points_with_circumcenter`. -/ lemma reflection_circumcenter_eq_affine_combination_of_points_with_circumcenter {n : ℕ} (s : simplex ℝ P n) {i₁ i₂ : fin (n + 1)} (h : i₁ ≠ i₂) : reflection (affine_span ℝ (s.points '' {i₁, i₂})) s.circumcenter = (univ : finset (points_with_circumcenter_index n)).affine_combination ℝ s.points_with_circumcenter (reflection_circumcenter_weights_with_circumcenter i₁ i₂) := begin have hc : card ({i₁, i₂} : finset (fin (n + 1))) = 2, { simp [h] }, -- Making the next line a separate definition helps the elaborator: set W : affine_subspace ℝ P := affine_span ℝ (s.points '' {i₁, i₂}) with W_def, have h_faces : ↑(orthogonal_projection W s.circumcenter) = ↑((s.face hc).orthogonal_projection_span s.circumcenter), { apply eq_orthogonal_projection_of_eq_subspace, simp }, rw [euclidean_geometry.reflection_apply, h_faces, s.orthogonal_projection_circumcenter hc, circumcenter_eq_centroid, s.face_centroid_eq_centroid hc, centroid_eq_affine_combination_of_points_with_circumcenter, circumcenter_eq_affine_combination_of_points_with_circumcenter, ←@vsub_eq_zero_iff_eq V, affine_combination_vsub, weighted_vsub_vadd_affine_combination, affine_combination_vsub, weighted_vsub_apply, sum_points_with_circumcenter], simp_rw [pi.sub_apply, pi.add_apply, pi.sub_apply, sub_smul, add_smul, sub_smul, centroid_weights_with_circumcenter, circumcenter_weights_with_circumcenter, reflection_circumcenter_weights_with_circumcenter, ite_smul, zero_smul, sub_zero, apply_ite2 (+), add_zero, ←add_smul, hc, zero_sub, neg_smul, sub_self, add_zero], convert sum_const_zero, norm_num end end simplex end affine namespace euclidean_geometry open affine affine_subspace finite_dimensional variables {V : Type*} {P : Type*} [normed_add_comm_group V] [inner_product_space ℝ V] [metric_space P] [normed_add_torsor V P] include V /-- Given a nonempty affine subspace, whose direction is complete, that contains a set of points, those points are cospherical if and only if they are equidistant from some point in that subspace. -/ lemma cospherical_iff_exists_mem_of_complete {s : affine_subspace ℝ P} {ps : set P} (h : ps ⊆ s) [nonempty s] [complete_space s.direction] : cospherical ps ↔ ∃ (center ∈ s) (radius : ℝ), ∀ p ∈ ps, dist p center = radius := begin split, { rintro ⟨c, hcr⟩, rw exists_dist_eq_iff_exists_dist_orthogonal_projection_eq h c at hcr, exact ⟨orthogonal_projection s c, orthogonal_projection_mem _, hcr⟩ }, { exact λ ⟨c, hc, hd⟩, ⟨c, hd⟩ } end /-- Given a nonempty affine subspace, whose direction is finite-dimensional, that contains a set of points, those points are cospherical if and only if they are equidistant from some point in that subspace. -/ lemma cospherical_iff_exists_mem_of_finite_dimensional {s : affine_subspace ℝ P} {ps : set P} (h : ps ⊆ s) [nonempty s] [finite_dimensional ℝ s.direction] : cospherical ps ↔ ∃ (center ∈ s) (radius : ℝ), ∀ p ∈ ps, dist p center = radius := cospherical_iff_exists_mem_of_complete h /-- All n-simplices among cospherical points in an n-dimensional subspace have the same circumradius. -/ lemma exists_circumradius_eq_of_cospherical_subset {s : affine_subspace ℝ P} {ps : set P} (h : ps ⊆ s) [nonempty s] {n : ℕ} [finite_dimensional ℝ s.direction] (hd : finrank ℝ s.direction = n) (hc : cospherical ps) : ∃ r : ℝ, ∀ sx : simplex ℝ P n, set.range sx.points ⊆ ps → sx.circumradius = r := begin rw cospherical_iff_exists_mem_of_finite_dimensional h at hc, rcases hc with ⟨c, hc, r, hcr⟩, use r, intros sx hsxps, have hsx : affine_span ℝ (set.range sx.points) = s, { refine sx.independent.affine_span_eq_of_le_of_card_eq_finrank_add_one (span_points_subset_coe_of_subset_coe (hsxps.trans h)) _, simp [hd] }, have hc : c ∈ affine_span ℝ (set.range sx.points) := hsx.symm ▸ hc, exact (sx.eq_circumradius_of_dist_eq hc (λ i, hcr (sx.points i) (hsxps (set.mem_range_self i)))).symm end /-- Two n-simplices among cospherical points in an n-dimensional subspace have the same circumradius. -/ lemma circumradius_eq_of_cospherical_subset {s : affine_subspace ℝ P} {ps : set P} (h : ps ⊆ s) [nonempty s] {n : ℕ} [finite_dimensional ℝ s.direction] (hd : finrank ℝ s.direction = n) (hc : cospherical ps) {sx₁ sx₂ : simplex ℝ P n} (hsx₁ : set.range sx₁.points ⊆ ps) (hsx₂ : set.range sx₂.points ⊆ ps) : sx₁.circumradius = sx₂.circumradius := begin rcases exists_circumradius_eq_of_cospherical_subset h hd hc with ⟨r, hr⟩, rw [hr sx₁ hsx₁, hr sx₂ hsx₂] end /-- All n-simplices among cospherical points in n-space have the same circumradius. -/ lemma exists_circumradius_eq_of_cospherical {ps : set P} {n : ℕ} [finite_dimensional ℝ V] (hd : finrank ℝ V = n) (hc : cospherical ps) : ∃ r : ℝ, ∀ sx : simplex ℝ P n, set.range sx.points ⊆ ps → sx.circumradius = r := begin haveI : nonempty (⊤ : affine_subspace ℝ P) := set.univ.nonempty, rw [←finrank_top, ←direction_top ℝ V P] at hd, refine exists_circumradius_eq_of_cospherical_subset _ hd hc, exact set.subset_univ _ end /-- Two n-simplices among cospherical points in n-space have the same circumradius. -/ lemma circumradius_eq_of_cospherical {ps : set P} {n : ℕ} [finite_dimensional ℝ V] (hd : finrank ℝ V = n) (hc : cospherical ps) {sx₁ sx₂ : simplex ℝ P n} (hsx₁ : set.range sx₁.points ⊆ ps) (hsx₂ : set.range sx₂.points ⊆ ps) : sx₁.circumradius = sx₂.circumradius := begin rcases exists_circumradius_eq_of_cospherical hd hc with ⟨r, hr⟩, rw [hr sx₁ hsx₁, hr sx₂ hsx₂] end /-- All n-simplices among cospherical points in an n-dimensional subspace have the same circumcenter. -/ lemma exists_circumcenter_eq_of_cospherical_subset {s : affine_subspace ℝ P} {ps : set P} (h : ps ⊆ s) [nonempty s] {n : ℕ} [finite_dimensional ℝ s.direction] (hd : finrank ℝ s.direction = n) (hc : cospherical ps) : ∃ c : P, ∀ sx : simplex ℝ P n, set.range sx.points ⊆ ps → sx.circumcenter = c := begin rw cospherical_iff_exists_mem_of_finite_dimensional h at hc, rcases hc with ⟨c, hc, r, hcr⟩, use c, intros sx hsxps, have hsx : affine_span ℝ (set.range sx.points) = s, { refine sx.independent.affine_span_eq_of_le_of_card_eq_finrank_add_one (span_points_subset_coe_of_subset_coe (hsxps.trans h)) _, simp [hd] }, have hc : c ∈ affine_span ℝ (set.range sx.points) := hsx.symm ▸ hc, exact (sx.eq_circumcenter_of_dist_eq hc (λ i, hcr (sx.points i) (hsxps (set.mem_range_self i)))).symm end /-- Two n-simplices among cospherical points in an n-dimensional subspace have the same circumcenter. -/ lemma circumcenter_eq_of_cospherical_subset {s : affine_subspace ℝ P} {ps : set P} (h : ps ⊆ s) [nonempty s] {n : ℕ} [finite_dimensional ℝ s.direction] (hd : finrank ℝ s.direction = n) (hc : cospherical ps) {sx₁ sx₂ : simplex ℝ P n} (hsx₁ : set.range sx₁.points ⊆ ps) (hsx₂ : set.range sx₂.points ⊆ ps) : sx₁.circumcenter = sx₂.circumcenter := begin rcases exists_circumcenter_eq_of_cospherical_subset h hd hc with ⟨r, hr⟩, rw [hr sx₁ hsx₁, hr sx₂ hsx₂] end /-- All n-simplices among cospherical points in n-space have the same circumcenter. -/ lemma exists_circumcenter_eq_of_cospherical {ps : set P} {n : ℕ} [finite_dimensional ℝ V] (hd : finrank ℝ V = n) (hc : cospherical ps) : ∃ c : P, ∀ sx : simplex ℝ P n, set.range sx.points ⊆ ps → sx.circumcenter = c := begin haveI : nonempty (⊤ : affine_subspace ℝ P) := set.univ.nonempty, rw [←finrank_top, ←direction_top ℝ V P] at hd, refine exists_circumcenter_eq_of_cospherical_subset _ hd hc, exact set.subset_univ _ end /-- Two n-simplices among cospherical points in n-space have the same circumcenter. -/ lemma circumcenter_eq_of_cospherical {ps : set P} {n : ℕ} [finite_dimensional ℝ V] (hd : finrank ℝ V = n) (hc : cospherical ps) {sx₁ sx₂ : simplex ℝ P n} (hsx₁ : set.range sx₁.points ⊆ ps) (hsx₂ : set.range sx₂.points ⊆ ps) : sx₁.circumcenter = sx₂.circumcenter := begin rcases exists_circumcenter_eq_of_cospherical hd hc with ⟨r, hr⟩, rw [hr sx₁ hsx₁, hr sx₂ hsx₂] end /-- All n-simplices among cospherical points in an n-dimensional subspace have the same circumsphere. -/ lemma exists_circumsphere_eq_of_cospherical_subset {s : affine_subspace ℝ P} {ps : set P} (h : ps ⊆ s) [nonempty s] {n : ℕ} [finite_dimensional ℝ s.direction] (hd : finrank ℝ s.direction = n) (hc : cospherical ps) : ∃ c : sphere P, ∀ sx : simplex ℝ P n, set.range sx.points ⊆ ps → sx.circumsphere = c := begin obtain ⟨r, hr⟩ := exists_circumradius_eq_of_cospherical_subset h hd hc, obtain ⟨c, hc⟩ := exists_circumcenter_eq_of_cospherical_subset h hd hc, exact ⟨⟨c, r⟩, λ sx hsx, sphere.ext _ _ (hc sx hsx) (hr sx hsx)⟩ end /-- Two n-simplices among cospherical points in an n-dimensional subspace have the same circumsphere. -/ lemma circumsphere_eq_of_cospherical_subset {s : affine_subspace ℝ P} {ps : set P} (h : ps ⊆ s) [nonempty s] {n : ℕ} [finite_dimensional ℝ s.direction] (hd : finrank ℝ s.direction = n) (hc : cospherical ps) {sx₁ sx₂ : simplex ℝ P n} (hsx₁ : set.range sx₁.points ⊆ ps) (hsx₂ : set.range sx₂.points ⊆ ps) : sx₁.circumsphere = sx₂.circumsphere := begin rcases exists_circumsphere_eq_of_cospherical_subset h hd hc with ⟨r, hr⟩, rw [hr sx₁ hsx₁, hr sx₂ hsx₂] end /-- All n-simplices among cospherical points in n-space have the same circumsphere. -/ lemma exists_circumsphere_eq_of_cospherical {ps : set P} {n : ℕ} [finite_dimensional ℝ V] (hd : finrank ℝ V = n) (hc : cospherical ps) : ∃ c : sphere P, ∀ sx : simplex ℝ P n, set.range sx.points ⊆ ps → sx.circumsphere = c := begin haveI : nonempty (⊤ : affine_subspace ℝ P) := set.univ.nonempty, rw [←finrank_top, ←direction_top ℝ V P] at hd, refine exists_circumsphere_eq_of_cospherical_subset _ hd hc, exact set.subset_univ _ end /-- Two n-simplices among cospherical points in n-space have the same circumsphere. -/ lemma circumsphere_eq_of_cospherical {ps : set P} {n : ℕ} [finite_dimensional ℝ V] (hd : finrank ℝ V = n) (hc : cospherical ps) {sx₁ sx₂ : simplex ℝ P n} (hsx₁ : set.range sx₁.points ⊆ ps) (hsx₂ : set.range sx₂.points ⊆ ps) : sx₁.circumsphere = sx₂.circumsphere := begin rcases exists_circumsphere_eq_of_cospherical hd hc with ⟨r, hr⟩, rw [hr sx₁ hsx₁, hr sx₂ hsx₂] end /-- Suppose all distances from `p₁` and `p₂` to the points of a simplex are equal, and that `p₁` and `p₂` lie in the affine span of `p` with the vertices of that simplex. Then `p₁` and `p₂` are equal or reflections of each other in the affine span of the vertices of the simplex. -/ lemma eq_or_eq_reflection_of_dist_eq {n : ℕ} {s : simplex ℝ P n} {p p₁ p₂ : P} {r : ℝ} (hp₁ : p₁ ∈ affine_span ℝ (insert p (set.range s.points))) (hp₂ : p₂ ∈ affine_span ℝ (insert p (set.range s.points))) (h₁ : ∀ i, dist (s.points i) p₁ = r) (h₂ : ∀ i, dist (s.points i) p₂ = r) : p₁ = p₂ ∨ p₁ = reflection (affine_span ℝ (set.range s.points)) p₂ := begin let span_s := affine_span ℝ (set.range s.points), have h₁' := s.orthogonal_projection_eq_circumcenter_of_dist_eq h₁, have h₂' := s.orthogonal_projection_eq_circumcenter_of_dist_eq h₂, rw [←affine_span_insert_affine_span, mem_affine_span_insert_iff (orthogonal_projection_mem p)] at hp₁ hp₂, obtain ⟨r₁, p₁o, hp₁o, hp₁⟩ := hp₁, obtain ⟨r₂, p₂o, hp₂o, hp₂⟩ := hp₂, obtain rfl : ↑(s.orthogonal_projection_span p₁) = p₁o, { subst hp₁, exact s.coe_orthogonal_projection_vadd_smul_vsub_orthogonal_projection hp₁o }, rw h₁' at hp₁, obtain rfl : ↑(s.orthogonal_projection_span p₂) = p₂o, { subst hp₂, exact s.coe_orthogonal_projection_vadd_smul_vsub_orthogonal_projection hp₂o }, rw h₂' at hp₂, have h : s.points 0 ∈ span_s := mem_affine_span ℝ (set.mem_range_self _), have hd₁ : dist p₁ s.circumcenter * dist p₁ s.circumcenter = r * r - s.circumradius * s.circumradius := s.dist_circumcenter_sq_eq_sq_sub_circumradius h₁ h₁' h, have hd₂ : dist p₂ s.circumcenter * dist p₂ s.circumcenter = r * r - s.circumradius * s.circumradius := s.dist_circumcenter_sq_eq_sq_sub_circumradius h₂ h₂' h, rw [←hd₂, hp₁, hp₂, dist_eq_norm_vsub V _ s.circumcenter, dist_eq_norm_vsub V _ s.circumcenter, vadd_vsub, vadd_vsub, ←real_inner_self_eq_norm_mul_norm, ←real_inner_self_eq_norm_mul_norm, real_inner_smul_left, real_inner_smul_left, real_inner_smul_right, real_inner_smul_right, ←mul_assoc, ←mul_assoc] at hd₁, by_cases hp : p = s.orthogonal_projection_span p, { rw simplex.orthogonal_projection_span at hp, rw [hp₁, hp₂, ←hp], simp only [true_or, eq_self_iff_true, smul_zero, vsub_self] }, { have hz : ⟪p -ᵥ orthogonal_projection span_s p, p -ᵥ orthogonal_projection span_s p⟫ ≠ 0, by simpa only [ne.def, vsub_eq_zero_iff_eq, inner_self_eq_zero] using hp, rw [mul_left_inj' hz, mul_self_eq_mul_self_iff] at hd₁, rw [hp₁, hp₂], cases hd₁, { left, rw hd₁ }, { right, rw [hd₁, reflection_vadd_smul_vsub_orthogonal_projection p r₂ s.circumcenter_mem_affine_span, neg_smul] } } end end euclidean_geometry
0a9fa8efb3e872e5fe2348eb3c518eba837a1bc8
63abd62053d479eae5abf4951554e1064a4c45b4
/src/category_theory/monoidal/braided.lean
2dcf7a082a68dc15c660e0366c71d72921684b10
[ "Apache-2.0" ]
permissive
Lix0120/mathlib
0020745240315ed0e517cbf32e738d8f9811dd80
e14c37827456fc6707f31b4d1d16f1f3a3205e91
refs/heads/master
1,673,102,855,024
1,604,151,044,000
1,604,151,044,000
308,930,245
0
0
Apache-2.0
1,604,164,710,000
1,604,163,547,000
null
UTF-8
Lean
false
false
11,454
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 category_theory.monoidal.natural_transformation import category_theory.monoidal.discrete /-! # Braided and symmetric monoidal categories The basic definitions of braided monoidal categories, and symmetric monoidal categories, as well as braided functors. ## Implementation note We make `braided_monoidal_category` another typeclass, but then have `symmetric_monoidal_category` extend this. The rationale is that we are not carrying any additional data, just requiring a property. ## Future work * Construct the Drinfeld center of a monoidal category as a braided monoidal category. * Say something about pseudo-natural transformations. -/ open category_theory universes v v₁ v₂ v₃ u u₁ u₂ u₃ namespace category_theory /-- A braided monoidal category is a monoidal category equipped with a braiding isomorphism `β_ X Y : X ⊗ Y ≅ Y ⊗ X` which is natural in both arguments, and also satisfies the two hexagon identities. -/ class braided_category (C : Type u) [category.{v} C] [monoidal_category.{v} C] := -- braiding natural iso: (braiding : Π X Y : C, X ⊗ Y ≅ Y ⊗ X) (braiding_naturality' : ∀ {X X' Y Y' : C} (f : X ⟶ Y) (g : X' ⟶ Y'), (f ⊗ g) ≫ (braiding Y Y').hom = (braiding X X').hom ≫ (g ⊗ f) . obviously) -- hexagon identities: (hexagon_forward' : Π X Y Z : C, (α_ X Y Z).hom ≫ (braiding X (Y ⊗ Z)).hom ≫ (α_ Y Z X).hom = ((braiding X Y).hom ⊗ (𝟙 Z)) ≫ (α_ Y X Z).hom ≫ ((𝟙 Y) ⊗ (braiding X Z).hom) . obviously) (hexagon_reverse' : Π X Y Z : C, (α_ X Y Z).inv ≫ (braiding (X ⊗ Y) Z).hom ≫ (α_ Z X Y).inv = ((𝟙 X) ⊗ (braiding Y Z).hom) ≫ (α_ X Z Y).inv ≫ ((braiding X Z).hom ⊗ (𝟙 Y)) . obviously) restate_axiom braided_category.braiding_naturality' attribute [simp,reassoc] braided_category.braiding_naturality restate_axiom braided_category.hexagon_forward' restate_axiom braided_category.hexagon_reverse' open category open monoidal_category open braided_category notation `β_` := braiding section /-! We now establish how the braiding interacts with the unitors. I couldn't find a detailed proof in print, but this is discussed in: * Proposition 1 of André Joyal and Ross Street, "Braided monoidal categories", Macquarie Math Reports 860081 (1986). * Proposition 2.1 of André Joyal and Ross Street, "Braided tensor categories" , Adv. Math. 102 (1993), 20–78. * Exercise 8.1.6 of Etingof, Gelaki, Nikshych, Ostrik, "Tensor categories", vol 25, Mathematical Surveys and Monographs (2015), AMS. -/ variables (C : Type u₁) [category.{v₁} C] [monoidal_category C] [braided_category C] lemma braiding_left_unitor_aux₁ (X : C) : (α_ (𝟙_ C) (𝟙_ C) X).hom ≫ (𝟙 _ ⊗ (β_ X (𝟙_ C)).inv) ≫ (α_ _ X _).inv ≫ ((λ_ X).hom ⊗ 𝟙 _) = ((λ_ _).hom ⊗ 𝟙 X) ≫ (β_ X _).inv := by { rw [←left_unitor_tensor, left_unitor_naturality], simp, } lemma braiding_left_unitor_aux₂ (X : C) : ((β_ X (𝟙_ C)).hom ⊗ (𝟙 (𝟙_ C))) ≫ ((λ_ X).hom ⊗ (𝟙 (𝟙_ C))) = (ρ_ X).hom ⊗ (𝟙 (𝟙_ C)) := calc ((β_ X (𝟙_ C)).hom ⊗ (𝟙 (𝟙_ C))) ≫ ((λ_ X).hom ⊗ (𝟙 (𝟙_ C))) = ((β_ X (𝟙_ C)).hom ⊗ (𝟙 (𝟙_ C))) ≫ (α_ _ _ _).hom ≫ (α_ _ _ _).inv ≫ ((λ_ X).hom ⊗ (𝟙 (𝟙_ C))) : by simp ... = ((β_ X (𝟙_ C)).hom ⊗ (𝟙 (𝟙_ C))) ≫ (α_ _ _ _).hom ≫ (𝟙 _ ⊗ (β_ X _).hom) ≫ (𝟙 _ ⊗ (β_ X _).inv) ≫ (α_ _ _ _).inv ≫ ((λ_ X).hom ⊗ (𝟙 (𝟙_ C))) : by { slice_rhs 3 4 { rw [←id_tensor_comp, iso.hom_inv_id, tensor_id], }, rw [id_comp], } ... = (α_ _ _ _).hom ≫ (β_ _ _).hom ≫ (α_ _ _ _).hom ≫ (𝟙 _ ⊗ (β_ X _).inv) ≫ (α_ _ _ _).inv ≫ ((λ_ X).hom ⊗ (𝟙 (𝟙_ C))) : by { slice_lhs 1 3 { rw ←hexagon_forward }, simp only [assoc], } ... = (α_ _ _ _).hom ≫ (β_ _ _).hom ≫ ((λ_ _).hom ⊗ 𝟙 X) ≫ (β_ X _).inv : by rw braiding_left_unitor_aux₁ ... = (α_ _ _ _).hom ≫ (𝟙 _ ⊗ (λ_ _).hom) ≫ (β_ _ _).hom ≫ (β_ X _).inv : by { slice_lhs 2 3 { rw [←braiding_naturality] }, simp only [assoc], } ... = (α_ _ _ _).hom ≫ (𝟙 _ ⊗ (λ_ _).hom) : by rw [iso.hom_inv_id, comp_id] ... = (ρ_ X).hom ⊗ (𝟙 (𝟙_ C)) : by rw triangle @[simp] lemma braiding_left_unitor (X : C) : (β_ X (𝟙_ C)).hom ≫ (λ_ X).hom = (ρ_ X).hom := by rw [←tensor_right_iff, comp_tensor_id, braiding_left_unitor_aux₂] lemma braiding_right_unitor_aux₁ (X : C) : (α_ X (𝟙_ C) (𝟙_ C)).inv ≫ ((β_ (𝟙_ C) X).inv ⊗ 𝟙 _) ≫ (α_ _ X _).hom ≫ (𝟙 _ ⊗ (ρ_ X).hom) = (𝟙 X ⊗ (ρ_ _).hom) ≫ (β_ _ X).inv := by { rw [←right_unitor_tensor, right_unitor_naturality], simp, } lemma braiding_right_unitor_aux₂ (X : C) : ((𝟙 (𝟙_ C)) ⊗ (β_ (𝟙_ C) X).hom) ≫ ((𝟙 (𝟙_ C)) ⊗ (ρ_ X).hom) = (𝟙 (𝟙_ C)) ⊗ (λ_ X).hom := calc ((𝟙 (𝟙_ C)) ⊗ (β_ (𝟙_ C) X).hom) ≫ ((𝟙 (𝟙_ C)) ⊗ (ρ_ X).hom) = ((𝟙 (𝟙_ C)) ⊗ (β_ (𝟙_ C) X).hom) ≫ (α_ _ _ _).inv ≫ (α_ _ _ _).hom ≫ ((𝟙 (𝟙_ C)) ⊗ (ρ_ X).hom) : by simp ... = ((𝟙 (𝟙_ C)) ⊗ (β_ (𝟙_ C) X).hom) ≫ (α_ _ _ _).inv ≫ ((β_ _ X).hom ⊗ 𝟙 _) ≫ ((β_ _ X).inv ⊗ 𝟙 _) ≫ (α_ _ _ _).hom ≫ ((𝟙 (𝟙_ C)) ⊗ (ρ_ X).hom) : by { slice_rhs 3 4 { rw [←comp_tensor_id, iso.hom_inv_id, tensor_id], }, rw [id_comp], } ... = (α_ _ _ _).inv ≫ (β_ _ _).hom ≫ (α_ _ _ _).inv ≫ ((β_ _ X).inv ⊗ 𝟙 _) ≫ (α_ _ _ _).hom ≫ ((𝟙 (𝟙_ C)) ⊗ (ρ_ X).hom) : by { slice_lhs 1 3 { rw ←hexagon_reverse }, simp only [assoc], } ... = (α_ _ _ _).inv ≫ (β_ _ _).hom ≫ (𝟙 X ⊗ (ρ_ _).hom) ≫ (β_ _ X).inv : by rw braiding_right_unitor_aux₁ ... = (α_ _ _ _).inv ≫ ((ρ_ _).hom ⊗ 𝟙 _) ≫ (β_ _ X).hom ≫ (β_ _ _).inv : by { slice_lhs 2 3 { rw [←braiding_naturality] }, simp only [assoc], } ... = (α_ _ _ _).inv ≫ ((ρ_ _).hom ⊗ 𝟙 _) : by rw [iso.hom_inv_id, comp_id] ... = (𝟙 (𝟙_ C)) ⊗ (λ_ X).hom : by rw [triangle_assoc_comp_right] @[simp] lemma braiding_right_unitor (X : C) : (β_ (𝟙_ C) X).hom ≫ (ρ_ X).hom = (λ_ X).hom := by rw [←tensor_left_iff, id_tensor_comp, braiding_right_unitor_aux₂] end /-- A symmetric monoidal category is a braided monoidal category for which the braiding is symmetric. See https://stacks.math.columbia.edu/tag/0FFW. -/ class symmetric_category (C : Type u) [category.{v} C] [monoidal_category.{v} C] extends braided_category.{v} C := -- braiding symmetric: (symmetry' : ∀ X Y : C, (β_ X Y).hom ≫ (β_ Y X).hom = 𝟙 (X ⊗ Y) . obviously) restate_axiom symmetric_category.symmetry' attribute [simp,reassoc] symmetric_category.symmetry variables (C : Type u₁) [category.{v₁} C] [monoidal_category C] [braided_category C] variables (D : Type u₂) [category.{v₂} D] [monoidal_category D] [braided_category D] variables (E : Type u₃) [category.{v₃} E] [monoidal_category E] [braided_category E] /-- A lax braided functor between braided monoidal categories is a lax monoidal functor which preserves the braiding. -/ structure lax_braided_functor extends lax_monoidal_functor C D := (braided' : ∀ X Y : C, μ X Y ≫ map (β_ X Y).hom = (β_ (obj X) (obj Y)).hom ≫ μ Y X . obviously) restate_axiom lax_braided_functor.braided' namespace lax_braided_functor /-- The identity lax braided monoidal functor. -/ @[simps] def id : lax_braided_functor C C := { .. monoidal_functor.id C } instance : inhabited (lax_braided_functor C C) := ⟨id C⟩ variables {C D E} /-- The composition of lax braided monoidal functors. -/ @[simps] def comp (F : lax_braided_functor C D) (G : lax_braided_functor D E) : lax_braided_functor C E := { braided' := λ X Y, begin dsimp, slice_lhs 2 3 { rw [←category_theory.functor.map_comp, F.braided, category_theory.functor.map_comp], }, slice_lhs 1 2 { rw [G.braided], }, simp only [category.assoc], end, ..(lax_monoidal_functor.comp F.to_lax_monoidal_functor G.to_lax_monoidal_functor) } instance category_lax_braided_functor : category (lax_braided_functor C D) := induced_category.category lax_braided_functor.to_lax_monoidal_functor @[simp] lemma comp_to_nat_trans {F G H : lax_braided_functor C D} {α : F ⟶ G} {β : G ⟶ H} : (α ≫ β).to_nat_trans = @category_struct.comp (C ⥤ D) _ _ _ _ (α.to_nat_trans) (β.to_nat_trans) := rfl /-- Interpret a natural isomorphism of the underlyling lax monoidal functors as an isomorphism of the lax braided monoidal functors. -/ @[simps] def mk_iso {F G : lax_braided_functor C D} (i : F.to_lax_monoidal_functor ≅ G.to_lax_monoidal_functor) : F ≅ G := { ..i } end lax_braided_functor /-- A braided functor between braided monoidal categories is a monoidal functor which preserves the braiding. -/ structure braided_functor extends monoidal_functor C D := -- Note this is stated different than for `lax_braided_functor`. -- We move the `μ X Y` to the right hand side, -- so that this makes a good `@[simp]` lemma. (braided' : ∀ X Y : C, map (β_ X Y).hom = inv (μ X Y) ≫ (β_ (obj X) (obj Y)).hom ≫ μ Y X . obviously) restate_axiom braided_functor.braided' attribute [simp] braided_functor.braided namespace braided_functor /-- Turn a braided functor into a lax braided functor. -/ @[simps] def to_lax_braided_functor (F : braided_functor C D) : lax_braided_functor C D := { braided' := λ X Y, by { rw F.braided, simp, } .. F } /-- The identity braided monoidal functor. -/ @[simps] def id : braided_functor C C := { .. monoidal_functor.id C } instance : inhabited (braided_functor C C) := ⟨id C⟩ variables {C D E} /-- The composition of braided monoidal functors. -/ @[simps] def comp (F : braided_functor C D) (G : braided_functor D E) : braided_functor C E := { ..(monoidal_functor.comp F.to_monoidal_functor G.to_monoidal_functor) } instance category_braided_functor : category (braided_functor C D) := induced_category.category braided_functor.to_monoidal_functor @[simp] lemma comp_to_nat_trans {F G H : braided_functor C D} {α : F ⟶ G} {β : G ⟶ H} : (α ≫ β).to_nat_trans = @category_struct.comp (C ⥤ D) _ _ _ _ (α.to_nat_trans) (β.to_nat_trans) := rfl /-- Interpret a natural isomorphism of the underlyling monoidal functors as an isomorphism of the braided monoidal functors. -/ @[simps] def mk_iso {F G : braided_functor C D} (i : F.to_monoidal_functor ≅ G.to_monoidal_functor) : F ≅ G := { ..i } end braided_functor section comm_monoid variables (M : Type u) [comm_monoid M] instance comm_monoid_discrete : comm_monoid (discrete M) := by { dsimp [discrete], apply_instance } instance : braided_category (discrete M) := { braiding := λ X Y, eq_to_iso (mul_comm X Y), } variables {M} {N : Type u} [comm_monoid N] /-- A multiplicative morphism between commutative monoids gives a braided functor between the corresponding discrete braided monoidal categories. -/ @[simps] def discrete.braided_functor (F : M →* N) : braided_functor (discrete M) (discrete N) := { ..discrete.monoidal_functor F } end comm_monoid end category_theory
8930ffc8af68060672a6b57b116dbf670cbacd08
02005f45e00c7ecf2c8ca5db60251bd1e9c860b5
/src/linear_algebra/dual.lean
b0501f85e78d891e9dfff4f51adbfa74b664b985
[ "Apache-2.0" ]
permissive
anthony2698/mathlib
03cd69fe5c280b0916f6df2d07c614c8e1efe890
407615e05814e98b24b2ff322b14e8e3eb5e5d67
refs/heads/master
1,678,792,774,873
1,614,371,563,000
1,614,371,563,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
17,966
lean
/- Copyright (c) 2019 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Fabian Glöckle -/ import linear_algebra.finite_dimensional import linear_algebra.projection noncomputable theory /-! # Dual vector spaces The dual space of an R-module M is the R-module of linear maps `M → R`. ## Main definitions * `dual R M` defines the dual space of M over R. * Given a basis for a K-vector space `V`, `is_basis.to_dual` produces a map from `V` to `dual K V`. * Given families of vectors `e` and `ε`, `dual_pair e ε` states that these families have the characteristic properties of a basis and a dual. * `dual_annihilator W` is the submodule of `dual R M` where every element annihilates `W`. ## Main results * `to_dual_equiv` : the dual space is linearly equivalent to the primal space. * `dual_pair.is_basis` and `dual_pair.eq_dual`: if `e` and `ε` form a dual pair, `e` is a basis and `ε` is its dual basis. * `quot_equiv_annihilator`: the quotient by a subspace is isomorphic to its dual annihilator. ## Notation We sometimes use `V'` as local notation for `dual K V`. -/ namespace module variables (R : Type*) (M : Type*) variables [comm_ring R] [add_comm_group M] [module R M] /-- The dual space of an R-module M is the R-module of linear maps `M → R`. -/ @[derive [add_comm_group, module R]] def dual := M →ₗ[R] R namespace dual instance : inhabited (dual R M) := by dunfold dual; apply_instance instance : has_coe_to_fun (dual R M) := ⟨_, linear_map.to_fun⟩ /-- Maps a module M to the dual of the dual of M. See `vector_space.erange_coe` and `vector_space.eval_equiv`. -/ def eval : M →ₗ[R] (dual R (dual R M)) := linear_map.flip linear_map.id @[simp] lemma eval_apply (v : M) (a : dual R M) : eval R M v a = a v := begin dunfold eval, rw [linear_map.flip_apply, linear_map.id_apply] end variables {R M} {M' : Type*} [add_comm_group M'] [module R M'] /-- The transposition of linear maps, as a linear map from `M →ₗ[R] M'` to `dual R M' →ₗ[R] dual R M`. -/ def transpose : (M →ₗ[R] M') →ₗ[R] (dual R M' →ₗ[R] dual R M) := (linear_map.llcomp R M M' R).flip lemma transpose_apply (u : M →ₗ[R] M') (l : dual R M') : transpose u l = l.comp u := rfl variables {M'' : Type*} [add_comm_group M''] [module R M''] lemma transpose_comp (u : M' →ₗ[R] M'') (v : M →ₗ[R] M') : transpose (u.comp v) = (transpose v).comp (transpose u) := rfl end dual end module namespace is_basis universes u v w variables {K : Type u} {V : Type v} {ι : Type w} variables [field K] [add_comm_group V] [vector_space K V] open vector_space module module.dual submodule linear_map cardinal function variables [de : decidable_eq ι] variables (B : ι → V) (h : is_basis K B) include de h /-- The linear map from a vector space equipped with basis to its dual vector space, taking basis elements to corresponding dual basis elements. -/ def to_dual : V →ₗ[K] module.dual K V := h.constr $ λ v, h.constr $ λ w, if w = v then 1 else 0 variable {B} lemma to_dual_apply (i j : ι) : h.to_dual B (B i) (B j) = if i = j then 1 else 0 := by { erw [constr_basis h, constr_basis h], ac_refl } @[simp] lemma to_dual_total_left (f : ι →₀ K) (i : ι) : h.to_dual B (finsupp.total ι V K B f) (B i) = f i := begin rw [finsupp.total_apply, finsupp.sum, linear_map.map_sum, linear_map.sum_apply], simp_rw [linear_map.map_smul, linear_map.smul_apply, to_dual_apply, smul_eq_mul, mul_boole, finset.sum_ite_eq'], split_ifs with h, { refl }, { rw finsupp.not_mem_support_iff.mp h } end @[simp] lemma to_dual_total_right (f : ι →₀ K) (i : ι) : h.to_dual B (B i) (finsupp.total ι V K B f) = f i := begin rw [finsupp.total_apply, finsupp.sum, linear_map.map_sum], simp_rw [linear_map.map_smul, to_dual_apply, smul_eq_mul, mul_boole, finset.sum_ite_eq], split_ifs with h, { refl }, { rw finsupp.not_mem_support_iff.mp h } end lemma to_dual_apply_left (v : V) (i : ι) : h.to_dual B v (B i) = h.repr v i := by rw [← h.to_dual_total_left, h.total_repr] lemma to_dual_apply_right (i : ι) (v : V) : h.to_dual B (B i) v = h.repr v i := by rw [← h.to_dual_total_right, h.total_repr] variable (B) /-- `h.to_dual_flip v` is the linear map sending `w` to `h.to_dual w v`. -/ def to_dual_flip (v : V) : (V →ₗ[K] K) := (h.to_dual B).flip v variable {B} omit de /-- `h.coord_fun i` sends vectors to their `i`'th coordinate with respect to the basis `h`. -/ def coord_fun (i : ι) : (V →ₗ[K] K) := (finsupp.lapply i).comp h.repr lemma coord_fun_eq_repr (v : V) (i : ι) : h.coord_fun i v = h.repr v i := rfl include de -- TODO: this lemma should be called something like `to_dual_flip_apply` lemma to_dual_swap_eq_to_dual (v w : V) : h.to_dual_flip B v w = h.to_dual B w v := rfl lemma to_dual_eq_repr (v : V) (i : ι) : h.to_dual B v (B i) = h.repr v i := h.to_dual_apply_left v i lemma to_dual_eq_equiv_fun [fintype ι] (v : V) (i : ι) : h.to_dual B v (B i) = h.equiv_fun v i := by rw [h.equiv_fun_apply, to_dual_eq_repr] lemma to_dual_inj (v : V) (a : h.to_dual B v = 0) : v = 0 := begin rw [← mem_bot K, ← h.repr_ker, mem_ker], apply finsupp.ext, intro b, rw [←to_dual_eq_repr _ _ _, a], refl end theorem to_dual_ker : (h.to_dual B).ker = ⊥ := ker_eq_bot'.mpr h.to_dual_inj theorem to_dual_range [fin : fintype ι] : (h.to_dual B).range = ⊤ := begin rw eq_top_iff', intro f, rw linear_map.mem_range, let lin_comb : ι →₀ K := finsupp.on_finset fin.elems (λ i, f.to_fun (B i)) _, { use finsupp.total ι V K B lin_comb, apply h.ext, { intros i, rw [h.to_dual_eq_repr _ i, repr_total h], { refl }, { rw [finsupp.mem_supported], exact λ _ _, set.mem_univ _ } } }, { intros a _, apply fin.complete } end /-- Maps a basis for `V` to a basis for the dual space. -/ def dual_basis : ι → dual K V := λ i, h.to_dual B (B i) theorem dual_lin_independent : linear_independent K h.dual_basis := h.1.map' _ (to_dual_ker _) @[simp] lemma dual_basis_apply_self (i j : ι) : h.dual_basis i (B j) = if i = j then 1 else 0 := h.to_dual_apply i j variable (B) /-- A vector space is linearly equivalent to its dual space. -/ def to_dual_equiv [fintype ι] : V ≃ₗ[K] (dual K V) := linear_equiv.of_bijective (h.to_dual B) h.to_dual_ker h.to_dual_range variable {B} theorem dual_basis_is_basis [fintype ι] : is_basis K h.dual_basis := (h.to_dual_equiv B).is_basis h @[simp] lemma total_dual_basis [fintype ι] (f : ι →₀ K) (i : ι) : finsupp.total ι (dual K V) K h.dual_basis f (B i) = f i := begin rw [finsupp.total_apply, finsupp.sum_fintype, linear_map.sum_apply], { simp_rw [smul_apply, smul_eq_mul, dual_basis_apply_self, mul_boole, finset.sum_ite_eq', if_pos (finset.mem_univ i)] }, { intro, rw zero_smul }, end lemma dual_basis_repr [fintype ι] (l : dual K V) (i : ι) : h.dual_basis_is_basis.repr l i = l (B i) := by rw [← total_dual_basis h, is_basis.total_repr h.dual_basis_is_basis l ] lemma dual_basis_equiv_fun [fintype ι] (l : dual K V) (i : ι) : h.dual_basis_is_basis.equiv_fun l i = l (B i) := by rw [is_basis.equiv_fun_apply, dual_basis_repr] lemma dual_basis_apply [fintype ι] (i : ι) (v : V) : h.dual_basis i v = h.equiv_fun v i := h.to_dual_apply_right i v @[simp] lemma to_dual_to_dual [fintype ι] : (h.dual_basis_is_basis.to_dual _).comp (h.to_dual B) = eval K V := begin refine h.ext (λ i, h.dual_basis_is_basis.ext (λ j, _)), suffices : @ite K _ (classical.prop_decidable _) 1 0 = @ite K _ (de j i) 1 0, by simpa [h.dual_basis_is_basis.to_dual_apply_left, h.dual_basis_repr, h.to_dual_apply_right], split_ifs; refl end omit de theorem dual_dim_eq [fintype ι] : cardinal.lift.{v u} (dim K V) = dim K (dual K V) := begin classical, have := linear_equiv.dim_eq_lift (h.to_dual_equiv B), simp only [cardinal.lift_umax] at this, rw [this, ← cardinal.lift_umax], apply cardinal.lift_id, end end is_basis namespace vector_space universes u v variables {K : Type u} {V : Type v} variables [field K] [add_comm_group V] [vector_space K V] open module module.dual submodule linear_map cardinal is_basis finite_dimensional theorem eval_ker : (eval K V).ker = ⊥ := begin classical, rw ker_eq_bot', intros v h, rw linear_map.ext_iff at h, by_contradiction H, rcases exists_subset_is_basis (linear_independent_singleton H) with ⟨b, hv, hb⟩, swap 4, assumption, have hv' : v = (coe : b → V) ⟨v, hv (set.mem_singleton v)⟩ := rfl, let hx := h (hb.to_dual _ v), rw [eval_apply, hv', to_dual_apply, if_pos rfl, zero_apply] at hx, exact one_ne_zero hx end theorem dual_dim_eq [finite_dimensional K V] : cardinal.lift.{v u} (dim K V) = dim K (dual K V) := begin classical, rcases exists_is_basis_fintype (dim_lt_omega K V) with ⟨b, hb, ⟨hf⟩⟩, resetI, exact hb.dual_dim_eq end lemma erange_coe [finite_dimensional K V] : (eval K V).range = ⊤ := begin classical, rcases exists_is_basis_fintype (dim_lt_omega K V) with ⟨b, hb, ⟨hf⟩⟩, unfreezingI { rw [← hb.to_dual_to_dual, range_comp, hb.to_dual_range, map_top, to_dual_range _] }, apply_instance end /-- A vector space is linearly equivalent to the dual of its dual space. -/ def eval_equiv [finite_dimensional K V] : V ≃ₗ[K] dual K (dual K V) := linear_equiv.of_bijective (eval K V) eval_ker (erange_coe) end vector_space section dual_pair open vector_space module module.dual linear_map function universes u v w variables {K : Type u} {V : Type v} {ι : Type w} [decidable_eq ι] variables [field K] [add_comm_group V] [vector_space K V] local notation `V'` := dual K V /-- `e` and `ε` have characteristic properties of a basis and its dual -/ @[nolint has_inhabited_instance] structure dual_pair (e : ι → V) (ε : ι → V') := (eval : ∀ i j : ι, ε i (e j) = if i = j then 1 else 0) (total : ∀ {v : V}, (∀ i, ε i v = 0) → v = 0) [finite : ∀ v : V, fintype {i | ε i v ≠ 0}] end dual_pair namespace dual_pair open vector_space module module.dual linear_map function universes u v w variables {K : Type u} {V : Type v} {ι : Type w} [dι : decidable_eq ι] variables [field K] [add_comm_group V] [vector_space K V] variables {e : ι → V} {ε : ι → dual K V} (h : dual_pair e ε) include h /-- The coefficients of `v` on the basis `e` -/ def coeffs (v : V) : ι →₀ K := { to_fun := λ i, ε i v, support := by { haveI := h.finite v, exact {i : ι | ε i v ≠ 0}.to_finset }, mem_support_to_fun := by {intro i, rw set.mem_to_finset, exact iff.rfl } } @[simp] lemma coeffs_apply (v : V) (i : ι) : h.coeffs v i = ε i v := rfl omit h /-- linear combinations of elements of `e`. This is a convenient abbreviation for `finsupp.total _ V K e l` -/ def lc (e : ι → V) (l : ι →₀ K) : V := l.sum (λ (i : ι) (a : K), a • (e i)) include h lemma dual_lc (l : ι →₀ K) (i : ι) : ε i (dual_pair.lc e l) = l i := begin erw linear_map.map_sum, simp only [h.eval, map_smul, smul_eq_mul], rw finset.sum_eq_single i, { simp }, { intros q q_in q_ne, simp [q_ne.symm] }, { intro p_not_in, simp [finsupp.not_mem_support_iff.1 p_not_in] }, end @[simp] lemma coeffs_lc (l : ι →₀ K) : h.coeffs (dual_pair.lc e l) = l := by { ext i, rw [h.coeffs_apply, h.dual_lc] } /-- For any v : V n, \sum_{p ∈ Q n} (ε p v) • e p = v -/ lemma decomposition (v : V) : dual_pair.lc e (h.coeffs v) = v := begin refine eq_of_sub_eq_zero (h.total _), intros i, simp [-sub_eq_add_neg, linear_map.map_sub, h.dual_lc, sub_eq_zero_iff_eq] end lemma mem_of_mem_span {H : set ι} {x : V} (hmem : x ∈ submodule.span K (e '' H)) : ∀ i : ι, ε i x ≠ 0 → i ∈ H := begin intros i hi, rcases (finsupp.mem_span_iff_total _).mp hmem with ⟨l, supp_l, sum_l⟩, change dual_pair.lc e l = x at sum_l, rw finsupp.mem_supported' at supp_l, apply classical.by_contradiction, intro i_not, apply hi, rw ← sum_l, simpa [h.dual_lc] using supp_l i i_not end lemma is_basis : is_basis K e := begin split, { rw linear_independent_iff, intros l H, change dual_pair.lc e l = 0 at H, ext i, apply_fun ε i at H, simpa [h.dual_lc] using H }, { rw submodule.eq_top_iff', intro v, rw [← set.image_univ, finsupp.mem_span_iff_total], exact ⟨h.coeffs v, by simp, h.decomposition v⟩ }, end lemma eq_dual : ε = is_basis.dual_basis h.is_basis := begin funext i, refine h.is_basis.ext (λ _, _), erw [is_basis.to_dual_apply, h.eval] end end dual_pair namespace submodule universes u v w variables {R : Type u} {M : Type v} [comm_ring R] [add_comm_group M] [module R M] variable {W : submodule R M} /-- The `dual_restrict` of a submodule `W` of `M` is the linear map from the dual of `M` to the dual of `W` such that the domain of each linear map is restricted to `W`. -/ def dual_restrict (W : submodule R M) : module.dual R M →ₗ[R] module.dual R W := linear_map.dom_restrict' W @[simp] lemma dual_restrict_apply (W : submodule R M) (φ : module.dual R M) (x : W) : W.dual_restrict φ x = φ (x : M) := rfl /-- The `dual_annihilator` of a submodule `W` is the set of linear maps `φ` such that `φ w = 0` for all `w ∈ W`. -/ def dual_annihilator {R : Type u} {M : Type v} [comm_ring R] [add_comm_group M] [module R M] (W : submodule R M) : submodule R $ module.dual R M := W.dual_restrict.ker @[simp] lemma mem_dual_annihilator (φ : module.dual R M) : φ ∈ W.dual_annihilator ↔ ∀ w ∈ W, φ w = 0 := begin refine linear_map.mem_ker.trans _, simp_rw [linear_map.ext_iff, dual_restrict_apply], exact ⟨λ h w hw, h ⟨w, hw⟩, λ h w, h w.1 w.2⟩ end lemma dual_restrict_ker_eq_dual_annihilator (W : submodule R M) : W.dual_restrict.ker = W.dual_annihilator := rfl end submodule namespace subspace open submodule linear_map universes u v w -- We work in vector spaces because `exists_is_compl` only hold for vector spaces variables {K : Type u} {V : Type v} [field K] [add_comm_group V] [vector_space K V] /-- Given a subspace `W` of `V` and an element of its dual `φ`, `dual_lift W φ` is the natural extension of `φ` to an element of the dual of `V`. That is, `dual_lift W φ` sends `w ∈ W` to `φ x` and `x` in the complement of `W` to `0`. -/ noncomputable def dual_lift (W : subspace K V) : module.dual K W →ₗ[K] module.dual K V := let h := classical.indefinite_description _ W.exists_is_compl in (linear_map.of_is_compl_prod h.2).comp (linear_map.inl _ _ _) variable {W : subspace K V} @[simp] lemma dual_lift_of_subtype {φ : module.dual K W} (w : W) : W.dual_lift φ (w : V) = φ w := by { erw of_is_compl_left_apply _ w, refl } lemma dual_lift_of_mem {φ : module.dual K W} {w : V} (hw : w ∈ W) : W.dual_lift φ w = φ ⟨w, hw⟩ := dual_lift_of_subtype ⟨w, hw⟩ @[simp] lemma dual_restrict_comp_dual_lift (W : subspace K V) : W.dual_restrict.comp W.dual_lift = 1 := by { ext φ x, simp } lemma dual_restrict_left_inverse (W : subspace K V) : function.left_inverse W.dual_restrict W.dual_lift := λ x, show W.dual_restrict.comp W.dual_lift x = x, by { rw [dual_restrict_comp_dual_lift], refl } lemma dual_lift_right_inverse (W : subspace K V) : function.right_inverse W.dual_lift W.dual_restrict := W.dual_restrict_left_inverse lemma dual_restrict_surjective : function.surjective W.dual_restrict := W.dual_lift_right_inverse.surjective lemma dual_lift_injective : function.injective W.dual_lift := W.dual_restrict_left_inverse.injective /-- The quotient by the `dual_annihilator` of a subspace is isomorphic to the dual of that subspace. -/ noncomputable def quot_annihilator_equiv (W : subspace K V) : W.dual_annihilator.quotient ≃ₗ[K] module.dual K W := (quot_equiv_of_eq _ _ W.dual_restrict_ker_eq_dual_annihilator).symm.trans $ W.dual_restrict.quot_ker_equiv_of_surjective dual_restrict_surjective /-- The natural isomorphism forom the dual of a subspace `W` to `W.dual_lift.range`. -/ noncomputable def dual_equiv_dual (W : subspace K V) : module.dual K W ≃ₗ[K] W.dual_lift.range := linear_equiv.of_injective _ $ ker_eq_bot.2 dual_lift_injective lemma dual_equiv_dual_def (W : subspace K V) : W.dual_equiv_dual.to_linear_map = W.dual_lift.range_restrict := rfl @[simp] lemma dual_equiv_dual_apply (φ : module.dual K W) : W.dual_equiv_dual φ = ⟨W.dual_lift φ, mem_range.2 ⟨φ, rfl⟩⟩ := rfl section open_locale classical open finite_dimensional variables {V₁ : Type*} [add_comm_group V₁] [vector_space K V₁] instance [H : finite_dimensional K V] : finite_dimensional K (module.dual K V) := begin refine @linear_equiv.finite_dimensional _ _ _ _ _ _ _ _ _ H, have hB := classical.some_spec (exists_is_basis_finite K V), haveI := classical.choice hB.2, exact is_basis.to_dual_equiv _ hB.1 end variables [finite_dimensional K V] [finite_dimensional K V₁] /-- The quotient by the dual is isomorphic to its dual annihilator. -/ noncomputable def quot_dual_equiv_annihilator (W : subspace K V) : W.dual_lift.range.quotient ≃ₗ[K] W.dual_annihilator := linear_equiv.quot_equiv_of_quot_equiv $ linear_equiv.trans W.quot_annihilator_equiv W.dual_equiv_dual /-- The quotient by a subspace is isomorphic to its dual annihilator. -/ noncomputable def quot_equiv_annihilator (W : subspace K V) : W.quotient ≃ₗ[K] W.dual_annihilator := begin refine linear_equiv.trans _ W.quot_dual_equiv_annihilator, refine linear_equiv.quot_equiv_of_equiv _ _, { refine linear_equiv.trans _ W.dual_equiv_dual, have hB := classical.some_spec (exists_is_basis_finite K W), haveI := classical.choice hB.2, exact is_basis.to_dual_equiv _ hB.1 }, { have hB := classical.some_spec (exists_is_basis_finite K V), haveI := classical.choice hB.2, exact is_basis.to_dual_equiv _ hB.1 }, end end end subspace
c50e4d42c579a9994dbb6d7fb5122ad715de4862
4f065978c49388d188224610d9984673079f7d91
/tensor_product.lean
8311097911b57cc8ce86aac49a16204e26cdf43f
[]
no_license
kckennylau/Lean
b323103f52706304907adcfaee6f5cb8095d4a33
907d0a4d2bd8f23785abd6142ad53d308c54fdcb
refs/heads/master
1,624,623,720,653
1,563,901,820,000
1,563,901,820,000
109,506,702
3
1
null
null
null
null
UTF-8
Lean
false
false
58,430
lean
import algebra.group_power algebra.linear_algebra.prod_module algebra.module import data.finsupp data.set.basic tactic.ring noncomputable theory universes u u₁ v v₁ w w₁ open classical set function local attribute [instance] decidable_inhabited prop_decidable namespace prod @[simp] lemma prod_add_prod {α : Type u} {β : Type v} {γ : Type w} [comm_ring α] [module α β] [module α γ] (x₁ x₂ : β) (y₁ y₂ : γ) : (x₁, y₁) + (x₂, y₂) = (x₁ + x₂, y₁ + y₂) := rfl @[simp] lemma smul_prod {α : Type u} {β : Type v} {γ : Type w} [comm_ring α] [module α β] [module α γ] (r : α) (x : β) (y : γ) : r • (x, y) = (r • x, r • y) := rfl @[simp] lemma fst_smul {α : Type u} {β : Type v} {γ : Type w} [comm_ring α] [module α β] [module α γ] (r : α) (z : β × γ) : (r • z).fst = r • z.fst := rfl @[simp] lemma snd_smul {α : Type u} {β : Type v} {γ : Type w} [comm_ring α] [module α β] [module α γ] (r : α) (z : β × γ) : (r • z).snd = r • z.snd := rfl end prod class type_singleton (α : Type u) : Type u := (default : α) (unique : ∀ x : α, x = default) namespace type_singleton variables (α : Type u) [type_singleton α] variables (β : Type v) [type_singleton β] def equiv_unit : equiv α unit := { to_fun := λ x, unit.star, inv_fun := λ x, type_singleton.default α, left_inv := λ x, by rw type_singleton.unique x, right_inv := λ x, unit.cases_on x rfl } def equiv_singleton : equiv α β := { to_fun := λ x, type_singleton.default β, inv_fun := λ x, type_singleton.default α, left_inv := λ x, by rw type_singleton.unique x, right_inv := λ x, by rw type_singleton.unique x } end type_singleton @[simp] lemma quotient.lift_beta {α : Sort u} {β : Sort v} [s : setoid α] (f : α → β) (h : ∀ (a b : α), a ≈ b → f a = f b) (x : α): quotient.lift f h (quotient.mk x) = f x := rfl @[simp] lemma quotient.lift_on_beta {α : Sort u} {β : Sort v} [s : setoid α] (f : α → β) (h : ∀ (a b : α), a ≈ b → f a = f b) (x : α): quotient.lift_on (quotient.mk x) f h = f x := rfl section bilinear variables {α : Type u} [comm_ring α] include α variables {β : Type v} {γ : Type w} {α₁ : Type u₁} {β₁ : Type v₁} variables [module α β] [module α γ] [module α α₁] [module α β₁] structure is_bilinear_map {β γ α₁} [module α β] [module α γ] [module α α₁] (f : β → γ → α₁) : Prop := (add_pair : ∀ x y z, f (x + y) z = f x z + f y z) (pair_add : ∀ x y z, f x (y + z) = f x y + f x z) (smul_pair : ∀ r x y, f (r • x) y = r • f x y) (pair_smul : ∀ r x y, f x (r • y) = r • f x y) variables {f : β → γ → α₁} (hf : is_bilinear_map f) include hf theorem is_bilinear_map.zero_pair : ∀ y, f 0 y = 0 := λ y, calc f 0 y = f (0 + 0) y - f 0 y : by rw [hf.add_pair 0 0 y]; simp ... = 0 : by simp theorem is_bilinear_map.pair_zero : ∀ x, f x 0 = 0 := λ x, calc f x 0 = f x (0 + 0) - f x 0 : by rw [hf.pair_add x 0 0]; simp ... = 0 : by simp theorem is_bilinear_map.linear_pair (y : γ) : is_linear_map (λ x, f x y) := { add := λ m n, hf.add_pair m n y, smul := λ r m, hf.smul_pair r m y } theorem is_bilinear_map.pair_linear (x : β) : is_linear_map (λ y, f x y) := { add := λ m n, hf.pair_add x m n, smul := λ r m, hf.pair_smul r x m } variables {g : α₁ → β₁} (hg : is_linear_map g) include hg theorem is_bilinear_map.comp : is_bilinear_map (λ x y, g (f x y)) := { add_pair := λ x y z, by rw [hf.add_pair, hg.add], pair_add := λ x y z, by rw [hf.pair_add, hg.add], smul_pair := λ r x y, by rw [hf.smul_pair, hg.smul], pair_smul := λ r x y, by rw [hf.pair_smul, hg.smul] } omit hf hg variables (β γ) structure module_iso (β γ) [module α β] [module α γ] extends equiv β γ := ( linear : is_linear_map to_fun ) end bilinear infix ` ≃ₘ `:25 := module_iso namespace module_iso variables (α : Type u) [comm_ring α] variables (β : Type v) (γ : Type w) (α₁ : Type u₁) [module α β] [module α γ] [module α α₁] variables {α β γ α₁} include α protected def refl : β ≃ₘ β := { linear := is_linear_map.id ..equiv.refl β } protected def symm (hbc : β ≃ₘ γ) : γ ≃ₘ β := { linear := is_linear_map.inverse hbc.linear hbc.left_inv hbc.right_inv ..equiv.symm hbc.to_equiv } protected def trans : β ≃ₘ γ → γ ≃ₘ α₁ → β ≃ₘ α₁ := λ hbc hca, { linear := is_linear_map.comp hca.linear hbc.linear ..equiv.trans hbc.to_equiv hca.to_equiv } end module_iso theorem gsmul_sub (α : Type u) [add_group α] (a : α) : ∀ i j : int, gsmul a (i - j) = gsmul a i - gsmul a j := begin intros i j, rw sub_eq_add_neg, rw sub_eq_add_neg, rw gsmul_add, rw gsmul_neg end theorem add_gsmul (α : Type u) [add_comm_group α] (a b : α) : ∀ n, gsmul (a + b) n = gsmul a n + gsmul b n := begin intro n, induction n, { rw ← int.coe_nat_eq, rw smul_coe_nat, rw smul_coe_nat, rw smul_coe_nat, rw add_monoid.add_smul }, { rw int.neg_succ_of_nat_eq, rw gsmul_neg, rw gsmul_neg, rw gsmul_neg, rw gsmul_add, rw gsmul_add, rw gsmul_add, rw smul_coe_nat, rw smul_coe_nat, rw smul_coe_nat, rw add_monoid.add_smul, simp } end theorem sub_gsmul (α : Type u) [add_comm_group α] (a b : α) : ∀ n, gsmul (a - b) n = gsmul a n - gsmul b n := begin intro n, rw sub_eq_add_neg, rw sub_eq_add_neg, rw add_gsmul, rw neg_gsmul end theorem gsmul_smul (α : Type u) (β : Type v) [ring α] [module α β] (r : α) (x : β) : ∀ n:ℤ, gsmul (r • x) n = r • gsmul x n := begin intro n, cases n, { induction n with n ih, { simp }, { rw nat.succ_eq_add_one, rw int.of_nat_add, simp [gsmul_add] at ih ⊢, rw ih, rw smul_add } }, { rw int.neg_succ_of_nat_eq, rw gsmul_neg, rw gsmul_neg, rw gsmul_add, induction n with n ih, { simp [gsmul_one] }, { simpa [gsmul_add, ih, smul_add] using ih } } end namespace multiset variable {α : Type u} @[simp] theorem map_id (s : multiset α) : map id s = s := quot.induction_on s $ λ l, congr_arg coe $ list.map_id _ end multiset namespace list theorem map_neg {α : Type u} [add_comm_group α] : ∀ L:list α, (L.map (λ x, -x)).sum = -L.sum | [] := by simp | (h::t) := by simp [map_neg t] theorem sum_singleton {α : Type u} [add_group α] {x : α} : list.sum [x] = x := calc list.sum [x] = x + list.sum [] : list.sum_cons ... = x + 0 : congr_arg _ list.sum_nil ... = x : add_zero x end list namespace finsupp variables {α : Type u} {β : Type v} [add_comm_group β] {a : α} {b b₁ b₂ : β} variables {α₁ : Type u₁} variables {γ : Type w} [add_comm_group γ] @[simp] lemma single_neg : single a (-b) = -single a b := ext $ assume a', begin by_cases h : a = a', { rw [h, neg_apply, single_eq_same, single_eq_same] }, { rw [neg_apply, single_eq_of_ne h, single_eq_of_ne h, neg_zero] } end @[simp] lemma single_sub : single a (b₁ - b₂) = single a b₁ - single a b₂ := by simp theorem sum_neg_index' {g : α →₀ β} {h : α → β → γ}: (∀ (a : α) (b₁ b₂ : β), h a (b₁ - b₂) = h a b₁ - h a b₂) → finsupp.sum (-g) h = -finsupp.sum g h := begin intro H, rw ← zero_sub, rw sum_sub_index H, rw sum_zero_index, rw zero_sub end @[simp] theorem finsum_apply {S : finset α₁} {f : α₁ → α →₀ β} {z : α} : (S.sum f) z = S.sum (λ x, f x z) := eq.symm $ finset.sum_hom (λ g : α →₀ β, g z) rfl (λ x y, rfl) end finsupp namespace tensor_product variables (α : Type u) [comm_ring α] variables (β : Type v) (γ : Type w) (α₁ : Type u₁) (β₁ : Type v₁) (γ₁ : Type w₁) variables [module α β] [module α γ] [module α α₁] [module α β₁] [module α γ₁] def free_abelian_group : Type (max v w) := β × γ →₀ ℤ instance free_abelian_group.has_coe_to_fun : has_coe_to_fun (free_abelian_group β γ) := finsupp.has_coe_to_fun instance free_abelian_group.add_comm_monoid : add_comm_monoid (free_abelian_group β γ) := finsupp.add_comm_monoid instance free_abelian_group.add_comm_group : add_comm_group (free_abelian_group β γ) := finsupp.add_comm_group theorem structural_theorem (f : free_abelian_group β γ) : ∃ S : finset (free_abelian_group β γ), (∀ g ∈ S, ∃ (x : β) (y : γ) (n : ℤ) (H : n ≠ 0), g = finsupp.single (x, y) n) ∧ S.sum id = f := begin rcases f with ⟨f, ⟨hf⟩⟩, cases hf with S hs, existsi S.image (λ z, finsupp.single z.val $ f z.val : {a : β × γ | f a ≠ 0} → free_abelian_group β γ), split, { intros g hg, rw finset.mem_image at hg, rcases hg with ⟨z, hzs, hg⟩, cases z with z hz, cases z with x y, exact ⟨x, y, f (x, y), hz, by simp [hg]⟩ }, { rw finset.sum_image, { apply finsupp.ext, intro z, by_cases hz : f z = 0, { simp, rw ← finset.sum_subset (finset.empty_subset S), { simpa using hz.symm }, { intros x hx hnx, rw id, rw finsupp.single_apply, have h2 : ¬x.val = z := λ hxz, x.property (by rwa hxz), rw if_neg h2 } }, { simp, have h1 : finset.singleton (⟨z, hz⟩ : {a : β × γ | f a ≠ 0}) ⊆ S := λ x hx, hs x, rw ← finset.sum_subset h1, { rw finset.sum_singleton, rw id, rw finsupp.single_apply, rw if_pos rfl, refl }, { intros x hx hnxz, rw finset.mem_singleton at hnxz, rw id, rw finsupp.single_apply, have h2 : ¬x.val = z := λ hxz, hnxz (subtype.eq hxz), rw if_neg h2 } } }, { intros x hx y hy hxy, by_contradiction hnxy, have hxyx : @@coe_fn (free_abelian_group.has_coe_to_fun β γ) (finsupp.single x.val (f x.val)) x.val = @@coe_fn (free_abelian_group.has_coe_to_fun β γ) (finsupp.single y.val (f y.val)) x.val := by rw hxy, rw finsupp.single_apply at hxyx, rw finsupp.single_apply at hxyx, rw if_pos rfl at hxyx, have h1 : ¬y.val = x.val := λ hyx, hnxy (subtype.eq hyx.symm), rw if_neg h1 at hxyx, exact x.property hxyx } } end variables α {β γ} include α namespace relators def pair_add : β → γ → γ → ℤ → free_abelian_group β γ := λ x y₁ y₂ n, finsupp.single (x, y₁) n + finsupp.single (x, y₂) n - finsupp.single (x, y₁ + y₂) n def add_pair : β → β → γ → ℤ → free_abelian_group β γ := λ x₁ x₂ y n, finsupp.single (x₁, y) n + finsupp.single (x₂, y) n - finsupp.single (x₁ + x₂, y) n def smul_trans : α → β → γ → ℤ → free_abelian_group β γ := λ r x y n, finsupp.single (r • x, y) n - finsupp.single (x, r • y) n variables α β γ def smul_aux : α → β × γ → ℤ → free_abelian_group β γ := λ r f y, finsupp.single (r • f.fst, f.snd) y variables {α β γ} theorem pair_add.neg (x : β) (y₁ y₂ : γ) (n : ℤ) : -pair_add α x y₁ y₂ n = pair_add α x y₁ y₂ (-n) := begin unfold pair_add, rw finsupp.single_neg, rw finsupp.single_neg, rw finsupp.single_neg, simp, repeat { rw add_comm, rw add_assoc } end theorem pair_add.smul (r : α) (x : β) (y₁ y₂ : γ) (n : ℤ) : finsupp.sum (pair_add α x y₁ y₂ n) (smul_aux α β γ r) = relators.pair_add α (r • x) y₁ y₂ n := begin unfold pair_add, rw finsupp.sum_sub_index, rw finsupp.sum_add_index, repeat { rw finsupp.sum_single_index }, repeat { intros, simp [smul_aux] }, repeat { refl } end theorem add_pair.neg (x₁ x₂ : β) (y : γ) (n : ℤ) : -add_pair α x₁ x₂ y n = add_pair α x₁ x₂ y (-n) := begin unfold add_pair, rw finsupp.single_neg, rw finsupp.single_neg, rw finsupp.single_neg, simp, repeat { rw add_comm, rw add_assoc } end theorem add_pair.smul (r : α) (x₁ x₂ : β) (y : γ) (n : ℤ) : finsupp.sum (add_pair α x₁ x₂ y n) (smul_aux α β γ r) = relators.add_pair α (r • x₁) (r • x₂) y n := begin unfold add_pair, rw finsupp.sum_sub_index, rw finsupp.sum_add_index, repeat { rw finsupp.sum_single_index }, repeat { intros, simp [smul_aux, smul_add] }, repeat { refl } end theorem smul_trans.neg (r : α) (x : β) (y : γ) (n : ℤ) : -smul_trans α r x y n = smul_trans α r x y (-n) := begin unfold smul_trans, rw finsupp.single_neg, rw finsupp.single_neg, simp end theorem smul_trans.smul (r r' : α) (x : β) (y : γ) (n : ℤ) : finsupp.sum (smul_trans α r' x y n) (smul_aux α β γ r) = relators.smul_trans α r' (r • x) y n := begin unfold smul_trans, rw finsupp.sum_sub_index, repeat { rw finsupp.sum_single_index }, repeat { intros, simp [smul_aux, smul_add, smul_smul, mul_comm] }, repeat { refl }, end end relators variables (α β γ) def relators : set (free_abelian_group β γ) := {f | (∃ x y₁ y₂ n, f = relators.pair_add α x y₁ y₂ n) ∨ (∃ x₁ x₂ y n, f = relators.add_pair α x₁ x₂ y n) ∨ (∃ r x y n, f = relators.smul_trans α r x y n)} theorem relators.zero_mem : (0 : free_abelian_group β γ ) ∈ relators α β γ := or.inl ⟨0, 0, 0, 0, by simpa [relators.pair_add, finsupp.single_zero]⟩ theorem relators.neg_mem : ∀ f, f ∈ relators α β γ → -f ∈ relators α β γ := begin intros f hf, rcases hf with hf | hf | hf; rcases hf with ⟨a, b, c, d, hf⟩, { from or.inl ⟨a, b, c, -d, by rw [hf, relators.pair_add.neg]⟩ }, { from or.inr (or.inl ⟨a, b, c, -d, by rw [hf, relators.add_pair.neg]⟩) }, { from or.inr (or.inr ⟨a, b, c, -d, by rw [hf, relators.smul_trans.neg]⟩) } end def closure : set (free_abelian_group β γ) := {f | ∃ (L : list (free_abelian_group β γ)), (∀ x ∈ L, x ∈ relators α β γ) ∧ L.sum = f} def r : free_abelian_group β γ → free_abelian_group β γ → Prop := λ f g, f - g ∈ closure α β γ local infix ≈ := r α β γ theorem refl (f : free_abelian_group β γ) : f ≈ f := ⟨[], by simp⟩ theorem symm (f g : free_abelian_group β γ) : f ≈ g → g ≈ f := λ ⟨L, hL, hLfg⟩, ⟨L.map (λ x, -x), λ x hx, let ⟨y, hyL, hyx⟩ := list.exists_of_mem_map hx in by rw ← hyx; exact relators.neg_mem α β γ y (hL y hyL), by simp [list.map_neg, hLfg]⟩ theorem trans (f g h : free_abelian_group β γ) : f ≈ g → g ≈ h → f ≈ h := λ ⟨L₁, hL₁, hLfg₁⟩ ⟨L₂, hL₂, hLfg₂⟩, ⟨L₁ ++ L₂, λ x hx, by rw [list.mem_append] at hx; from or.cases_on hx (hL₁ x) (hL₂ x), by simp [hLfg₁, hLfg₂]⟩ instance : setoid (free_abelian_group β γ) := ⟨r α β γ, refl α β γ, symm α β γ, trans α β γ⟩ end tensor_product def tensor_product {α} (β γ) [comm_ring α] [module α β] [module α γ] : Type (max v w) := quotient (tensor_product.setoid α β γ) local infix ` ⊗ `:100 := tensor_product namespace tensor_product variables (α : Type u) [comm_ring α] variables (β : Type v) (γ : Type w) (α₁ : Type u₁) (β₁ : Type v₁) (γ₁ : Type w₁) variables [module α β] [module α γ] [module α α₁] [module α β₁] [module α γ₁] include α def add : β ⊗ γ → β ⊗ γ → β ⊗ γ := quotient.lift₂ (λ f g, ⟦f + g⟧ : free_abelian_group β γ → free_abelian_group β γ → β ⊗ γ) $ λ f₁ f₂ g₁ g₂ ⟨L₁, hL₁, hLfg₁⟩ ⟨L₂, hL₂, hLfg₂⟩, quotient.sound ⟨L₁ ++ L₂, λ x hx, by rw [list.mem_append] at hx; from or.cases_on hx (hL₁ x) (hL₂ x), by rw [list.sum_append, hLfg₁, hLfg₂]; simp⟩ protected theorem add_assoc (f g h : β ⊗ γ) : add α β γ (add α β γ f g) h = add α β γ f (add α β γ g h) := quotient.induction_on₃ f g h $ λ m n k, quotient.sound $ by simp def zero : β ⊗ γ := ⟦0⟧ protected theorem zero_add (f : β ⊗ γ) : add α β γ (zero α β γ) f = f := quotient.induction_on f $ λ m, quotient.sound $ by simp protected theorem add_zero (f : β ⊗ γ) : add α β γ f (zero α β γ) = f := quotient.induction_on f $ λ m, quotient.sound $ by simp def neg : β ⊗ γ → β ⊗ γ := quotient.lift (λ f, ⟦-f⟧ : free_abelian_group β γ → β ⊗ γ) $ λ f g ⟨L, hL, hLfg⟩, quotient.sound ⟨L.map (λ x, -x), λ x hx, let ⟨y, hyL, hyx⟩ := list.exists_of_mem_map hx in by rw ← hyx; exact relators.neg_mem α β γ y (hL y hyL), by simp [list.map_neg, hLfg]⟩ protected theorem add_left_neg (f : β ⊗ γ) : add α β γ (neg α β γ f) f = zero α β γ := quotient.induction_on f $ λ m, quotient.sound $ by simp protected theorem add_comm (f g : β ⊗ γ) : add α β γ f g = add α β γ g f := quotient.induction_on₂ f g $ λ m n, quotient.sound $ by simp instance : add_comm_group (β ⊗ γ) := { add := add α β γ, add_assoc := tensor_product.add_assoc α β γ, zero := zero α β γ, zero_add := tensor_product.zero_add α β γ, add_zero := tensor_product.add_zero α β γ, neg := neg α β γ, add_left_neg := tensor_product.add_left_neg α β γ, add_comm := tensor_product.add_comm α β γ } theorem mem_closure_of_finset {f : free_abelian_group β γ} : (∃ (S : finset (free_abelian_group β γ)) g, S.sum g = f ∧ ∀ x ∈ S, g x ∈ relators α β γ) → f ∈ closure α β γ := λ ⟨S, g, hSf, hSr⟩, begin cases S with ms hms, cases quot.exists_rep ms with L hL, existsi L.map g, split, { intros x hxL, rcases list.exists_of_mem_map hxL with ⟨y, hyL, hyx⟩, have hyms : y ∈ ms, { unfold has_mem.mem, unfold multiset.mem, rw ← hL, rw quot.lift_on, rwa @quot.lift_beta _ (list.perm.setoid (free_abelian_group β γ)).r, exact multiset.mem._proof_1 y }, rw ← hyx, exact hSr y hyms }, { change multiset.sum (multiset.map g ms) = f at hSf, rw ← hL at hSf, rw ← multiset.coe_sum, exact hSf } end private lemma zero_eq_zero : (0 : free_abelian_group β γ) = (0 : β × γ →₀ ℤ) := rfl private lemma sum_zero_index (f : β × γ → ℤ → free_abelian_group β γ) : @finsupp.sum (β × γ) ℤ (free_abelian_group β γ) int.has_zero _ (0 : free_abelian_group β γ) f = 0 := begin rw zero_eq_zero, simp [finsupp.sum], refl end private lemma sum_zero_index' [add_comm_group α₁] (f : β × γ → ℤ → α₁) : @finsupp.sum (β × γ) ℤ α₁ int.has_zero _ (0 : free_abelian_group β γ) f = 0 := begin rw zero_eq_zero, simp [finsupp.sum] end def smul : α → β ⊗ γ → β ⊗ γ := λ r, quotient.lift (λ f, ⟦f.sum (relators.smul_aux α β γ r)⟧ : free_abelian_group β γ → β ⊗ γ) $ λ f g ⟨L, hL, hLfg⟩, quotient.sound begin clear _fun_match, induction L generalizing f g, { existsi [], have : f = g, by simpa [add_neg_eq_zero] using hLfg.symm, simp [this] }, { specialize L_ih (λ z hzt, hL z (or.inr hzt)) L_tl.sum (0:free_abelian_group β γ), specialize L_ih ⟨L_tl, λ z hzt, hL z (or.inr hzt), eq.symm (sub_zero _)⟩, specialize L_ih (eq.symm $ sub_zero _), rcases L_ih with ⟨L', hL', hLfg'⟩, rw [sum_zero_index] at hLfg', simp at hLfg', rcases hL L_hd (or.inl rfl) with h | h | h, { rcases h with ⟨x, y₁, y₂, n, h⟩, existsi list.cons (relators.pair_add α (r • x) y₁ y₂ n) L', split, { exact λ z hz, or.cases_on (list.eq_or_mem_of_mem_cons hz) (λ hzh, or.inl ⟨r • x, y₁, y₂, n, hzh⟩) (λ hzt, hL' z hzt) }, { rw ← finsupp.sum_sub_index, rw ← hLfg, rw list.sum_cons, rw list.sum_cons, rw finsupp.sum_add_index, rw ← hLfg', rw h, rw relators.pair_add.smul, all_goals { intros, simp [relators.smul_aux], try {refl} } } }, { rcases h with ⟨x₁, x₂, y, n, h⟩, existsi list.cons (relators.add_pair α (r • x₁) (r • x₂) y n) L', split, { exact λ z hz, or.cases_on (list.eq_or_mem_of_mem_cons hz) (λ hzh, or.inr $ or.inl ⟨r • x₁, r • x₂, y, n, hzh⟩) (λ hzt, hL' z hzt) }, { rw ← finsupp.sum_sub_index, rw ← hLfg, rw list.sum_cons, rw list.sum_cons, rw finsupp.sum_add_index, rw ← hLfg', rw h, rw relators.add_pair.smul, all_goals { intros, simp [relators.smul_aux], try {refl} } } }, { rcases h with ⟨r', x, y, n, h⟩, existsi list.cons (relators.smul_trans α r' (r • x) y n) L', split, { exact λ z hz, or.cases_on (list.eq_or_mem_of_mem_cons hz) (λ hzh, or.inr $ or.inr ⟨r', r • x, y, n, hzh⟩) (λ hzt, hL' z hzt) }, { rw ← finsupp.sum_sub_index, rw ← hLfg, rw list.sum_cons, rw list.sum_cons, rw finsupp.sum_add_index, rw ← hLfg', rw h, rw relators.smul_trans.smul, all_goals { intros, simp [relators.smul_aux], try {refl} } } } } end protected theorem smul_add (r : α) (f g : β ⊗ γ) : smul α β γ r (add α β γ f g) = add α β γ (smul α β γ r f) (smul α β γ r g) := quotient.induction_on₂ f g $ λ m n, quotient.sound $ by rw [finsupp.sum_add_index]; all_goals { intros, simp [relators.smul_aux], try {refl} } protected theorem add_smul (r₁ r₂ : α) (f : β ⊗ γ) : smul α β γ (r₁ + r₂) f = add α β γ (smul α β γ r₁ f) (smul α β γ r₂ f) := quotient.induction_on f $ λ m, quotient.sound $ begin unfold relators.smul_aux, simp [add_smul], rcases structural_theorem β γ m with ⟨S, hS, hSm⟩, rw ← hSm, apply symm, apply mem_closure_of_finset, existsi S, revert m hS hSm, apply finset.induction_on S, { intros m hS hSm, existsi id, split, { simp, rw sum_zero_index, rw sum_zero_index, rw sum_zero_index, simp }, { intros x hx, exfalso, exact hx } }, { intros g T hgT ih m hS hSm, rcases hS g (finset.mem_insert_self g T) with ⟨x, y, n, H, h⟩, rcases ih (T.sum id) (λ g' hg', hS g' $ finset.mem_insert_of_mem hg') rfl with ⟨φ', hst', hss'⟩, existsi (λ f, if f = g then finsupp.single (r₁ • x, y) n + finsupp.single (r₂ • x, y) n - finsupp.single (r₁ • x + r₂ • x, y) n else φ' f), split, { rw finset.sum_insert, rw finset.sum_insert, rw finsupp.sum_add_index, rw finsupp.sum_add_index, rw finsupp.sum_add_index, rw if_pos rfl, rw h, rw id.def, rw finsupp.sum_single_index, rw finsupp.sum_single_index, rw finsupp.sum_single_index, have h1 : finset.sum T (λ (f : free_abelian_group β γ), ite (f = finsupp.single (x, y) n) (finsupp.single (r₁ • x, y) n + finsupp.single (r₂ • x, y) n - finsupp.single (r₁ • x + r₂ • x, y) n) (φ' f)) = finset.sum T φ', { apply finset.sum_congr, { refl }, { intros g' hg', have h1 : g' ≠ g, { intro hgg', rw hgg' at hg', exact hgT hg' }, rw h at h1, rw if_neg h1 } }, rw h1, rw hst', unfold prod.fst, unfold prod.snd, simp, all_goals { intros, try {rw finsupp.single_zero}, try {rw finsupp.single_zero}, try {rw finsupp.single_add}, try {refl}, try {refl}, try {exact hgT} } }, { intros f' hf', rw finset.mem_insert at hf', cases hf', { dsimp, rw if_pos hf', from or.inr (or.inl ⟨r₁ • x, r₂ • x, y, n, rfl⟩) }, { have h1 : f' ≠ g, { intro hfg', rw hfg' at hf', exact hgT hf' }, dsimp, rw if_neg h1, from hss' f' hf' } } } end protected theorem mul_smul (r₁ r₂ : α) (f : β ⊗ γ) : smul α β γ (r₁ * r₂) f = smul α β γ r₁ (smul α β γ r₂ f) := quotient.induction_on f $ λ m, quotient.sound $ begin unfold relators.smul_aux, simp [mul_smul], rcases structural_theorem β γ m with ⟨S, hS, hSm⟩, rw ← hSm, apply symm, apply mem_closure_of_finset, existsi S, existsi (λ f, 0 : free_abelian_group β γ → free_abelian_group β γ), revert m hS hSm, apply finset.induction_on S, { intros m hS hSm, split, { simp, rw sum_zero_index, rw sum_zero_index, rw sum_zero_index, simp }, { intros x hx, exfalso, exact hx } }, { intros g T hgT ih m hS hSm, rcases hS g (finset.mem_insert_self g T) with ⟨x, y, n, H, h⟩, rcases ih (T.sum id) (λ g' hg', hS g' $ finset.mem_insert_of_mem hg') rfl with ⟨hst', hss'⟩, split, { rw finset.sum_insert, rw finset.sum_insert, rw finset.sum_const_zero, rw finset.sum_const_zero at hst', rw finsupp.sum_add_index, rw finsupp.sum_add_index, rw finsupp.sum_add_index, rw h, rw id.def, rw finsupp.sum_single_index, rw finsupp.sum_single_index, rw finsupp.sum_single_index, unfold prod.fst, unfold prod.snd, rw add_comm_group.zero_add, rw hst', simp, all_goals { intros, try {rw finsupp.single_zero}, try {rw finsupp.single_zero}, try {rw finsupp.single_add}, try {refl}, try {refl}, try {exact hgT} } }, { intros f' hf', dsimp, exact relators.zero_mem α β γ } } end protected theorem one_smul (f : β ⊗ γ) : smul α β γ 1 f = f := quotient.induction_on f $ λ m, quotient.sound $ by simp [relators.smul_aux] instance : module α (β ⊗ γ) := { smul := smul α β γ, smul_add := tensor_product.smul_add α β γ, add_smul := tensor_product.add_smul α β γ, mul_smul := tensor_product.mul_smul α β γ, one_smul := tensor_product.one_smul α β γ } @[simp] lemma add_quot (f g : free_abelian_group β γ) : @has_add.add (β ⊗ γ) _ (⟦f⟧ : β ⊗ γ) ⟦g⟧ = ⟦f + g⟧ := rfl @[simp] lemma neg_quot (f : free_abelian_group β γ) : @has_neg.neg (β ⊗ γ) _ (⟦f⟧ : β ⊗ γ) = ⟦-f⟧ := rfl variables {α β γ} def tprod : β → γ → β ⊗ γ := λ x y, ⟦finsupp.single (x, y) 1⟧ infix ` ⊗ₛ `:100 := tprod variables {r r₁ r₂ : α} {x x₁ x₂ : β} {y y₁ y₂ : γ} theorem tprod_unfold : x ⊗ₛ y = tprod x y := rfl theorem tprod.is_bilinear_map : is_bilinear_map (@tprod α _ β γ _ _) := { add_pair := λ x y z, quotient.sound $ setoid.symm $ ⟨[(finsupp.single (x, z) 1 + finsupp.single (y, z) 1 - finsupp.single (x + y, z) 1 : free_abelian_group β γ)], λ u hu, or.inr $ or.inl ⟨x, y, z, 1, list.eq_of_mem_singleton hu⟩, list.sum_singleton⟩, pair_add := λ x y z, quotient.sound $ setoid.symm $ ⟨[(finsupp.single (x, y) 1 + finsupp.single (x, z) 1 - finsupp.single (x, y + z) 1 : free_abelian_group β γ)], λ u hu, or.inl ⟨x, y, z, 1, list.eq_of_mem_singleton hu⟩, list.sum_singleton⟩, smul_pair := λ r x y, quotient.sound $ setoid.symm $ begin simp [relators.smul_aux], rw finsupp.sum_single_index, exact finsupp.single_zero end, pair_smul := λ r x y, quotient.sound $ setoid.symm $ begin simp [relators.smul_aux], rw finsupp.sum_single_index, unfold prod.fst, unfold prod.snd, existsi ([(finsupp.single (r • x, y) 1 - finsupp.single (x, r • y) 1 : free_abelian_group β γ)]), split, { intros z hz, rw list.mem_singleton at hz, rw hz, from or.inr (or.inr ⟨r, x, y, 1, by simp [relators.smul_trans]⟩) }, simp [list.sum_singleton], { rw finsupp.single_zero, refl } end } @[simp] lemma add_tprod : (x₁ + x₂) ⊗ₛ y = x₁ ⊗ₛ y + x₂ ⊗ₛ y := tprod.is_bilinear_map.add_pair x₁ x₂ y @[simp] lemma tprod_add : x ⊗ₛ (y₁ + y₂) = x ⊗ₛ y₁ + x ⊗ₛ y₂ := tprod.is_bilinear_map.pair_add x y₁ y₂ @[simp] lemma smul_tprod : (r • x) ⊗ₛ y = r • x ⊗ₛ y := tprod.is_bilinear_map.smul_pair r x y @[simp] lemma tprod_smul : x ⊗ₛ (r • y) = r • x ⊗ₛ y := tprod.is_bilinear_map.pair_smul r x y @[simp] lemma zero_tprod : (0:β) ⊗ₛ y = 0 := tprod.is_bilinear_map.zero_pair y @[simp] lemma tprod_zero {x : β} : x ⊗ₛ (0:γ) = 0 := tprod.is_bilinear_map.pair_zero x namespace universal_property variables {β γ α₁} variables {f : β → γ → α₁} (hf : is_bilinear_map f) include β γ α₁ hf def factor_aux : free_abelian_group β γ → α₁ := λ g : free_abelian_group β γ, (g.sum (λ z n, gsmul (f z.fst z.snd) n)) theorem factor_equiv : ∀ g₁ g₂ : free_abelian_group β γ, g₁ ≈ g₂ → factor_aux hf g₁ = factor_aux hf g₂ := λ g₁ g₂ ⟨L, hL, hgL⟩, begin clear _fun_match _x, induction L generalizing hgL hL g₂ g₁, { simp at hgL, replace hgL := hgL.symm, rw add_neg_eq_zero at hgL, rw hgL }, { specialize L_ih L_tl.sum 0, specialize L_ih (λ x hx, hL x (list.mem_cons_of_mem L_hd hx)), specialize L_ih (sub_zero _).symm, rw ← sub_eq_zero, unfold factor_aux at L_ih ⊢, rw ← finsupp.sum_sub_index, rw ← hgL, rw list.sum_cons, rw finsupp.sum_add_index, rw L_ih, rw sum_zero_index', specialize hL L_hd, specialize hL (or.inl rfl), rcases hL with h | h | h, { rcases h with ⟨x, y₁, y₂, n, h⟩, rw h, unfold relators.pair_add, rw finsupp.sum_sub_index, rw finsupp.sum_add_index, rw finsupp.sum_single_index, rw finsupp.sum_single_index, rw finsupp.sum_single_index, rw ← add_gsmul, rw ← sub_gsmul, rw hf.pair_add, simp, { rw gsmul_zero }, { rw gsmul_zero }, { rw gsmul_zero }, { intros, rw gsmul_zero }, { intros, rw gsmul_add }, { intros, rw gsmul_sub } }, { rcases h with ⟨x₁, x₂, y, n, h⟩, rw h, unfold relators.add_pair, rw finsupp.sum_sub_index, rw finsupp.sum_add_index, rw finsupp.sum_single_index, rw finsupp.sum_single_index, rw finsupp.sum_single_index, rw ← add_gsmul, rw ← sub_gsmul, rw hf.add_pair, simp, { rw gsmul_zero }, { rw gsmul_zero }, { rw gsmul_zero }, { intros, rw gsmul_zero }, { intros, rw gsmul_add }, { intros, rw gsmul_sub } }, { rcases h with ⟨r, x, y, n, h⟩, rw h, unfold relators.smul_trans, rw finsupp.sum_sub_index, rw finsupp.sum_single_index, rw finsupp.sum_single_index, rw ← sub_gsmul, rw hf.smul_pair, rw hf.pair_smul, simp, { rw gsmul_zero }, { rw gsmul_zero }, { intros, rw gsmul_sub } }, { intros, rw gsmul_zero }, { intros, rw gsmul_add }, { intros, rw gsmul_sub } } end def factor : β ⊗ γ → α₁ := quotient.lift (factor_aux hf) (factor_equiv hf) theorem factor_add : ∀ g₁ g₂ : β ⊗ γ, factor hf (g₁ + g₂) = factor hf g₁ + factor hf g₂ := λ x y, quotient.induction_on₂ x y begin intros m n, simp [universal_property.factor], simp [universal_property.factor_aux], rw finsupp.sum_add_index, { intros, rw gsmul_zero }, { intros, rw gsmul_add } end theorem factor_smul : ∀ (r : α) (g : β ⊗ γ), factor hf (r • g) = r • factor hf g := λ r x, quotient.induction_on x begin intros m, simp [has_scalar.smul, smul, relators.smul_aux], rcases structural_theorem β γ m with ⟨S, hS, hSm⟩, rw ← hSm, revert m hS hSm, apply finset.induction_on S, { intros m hS hSm, unfold universal_property.factor, unfold universal_property.factor_aux, simp [finsupp.sum_zero_index], rw sum_zero_index, rw sum_zero_index', simp }, { intros n T hnT ih m hS hSm, unfold universal_property.factor, unfold universal_property.factor_aux, simp, specialize ih (finset.sum T id), specialize ih (λ g hg, hS g (finset.mem_insert_of_mem hg)), specialize ih rfl, unfold universal_property.factor at ih, unfold universal_property.factor_aux at ih, simp at ih, rw finset.sum_insert, rw finsupp.sum_add_index, rw finsupp.sum_add_index, rw finsupp.sum_add_index, rw ih, specialize hS n (finset.mem_insert_self n T), rcases hS with ⟨x', y', n', H', hn'⟩, rw hn', rw finsupp.sum_single_index, rw finsupp.sum_single_index, rw finsupp.sum_single_index, rw hf.smul_pair, rw gsmul_smul, rw smul_add, { rw gsmul_zero }, { rw gsmul_zero }, { rw finsupp.single_zero, refl }, { intros, rw gsmul_zero }, { intros, rw gsmul_add }, { intros, rw gsmul_zero }, { intros, rw gsmul_add }, { intros, rw finsupp.single_zero, refl }, { intros, rw finsupp.single_add }, { exact hnT } } end theorem factor_linear : is_linear_map (factor hf) := { add := factor_add hf, smul := factor_smul hf } theorem factor_commutes : ∀ x y, factor hf (x ⊗ₛ y) = f x y := begin intros, simp [function.comp, tprod, factor, factor_aux], simp [finsupp.sum_single_index] end theorem factor_unique (h : β ⊗ γ → α₁) (H : is_linear_map h) (hh : ∀ x y, h (x ⊗ₛ y) = f x y) : h = factor hf := begin apply funext, intro x, apply quotient.induction_on x, intro y, unfold universal_property.factor, unfold universal_property.factor_aux, simp, rcases structural_theorem β γ y with ⟨S, hS, hSy⟩, revert hSy hS y, apply finset.induction_on S, { intros y hS hSy, rw ← hSy, rw finset.sum_empty, rw sum_zero_index', exact H.zero }, { intros n T hnT ih y hS hSy, rw ← hSy, rw finset.sum_insert, rw ← add_quot, rw H.add, rw finsupp.sum_add_index, rw id.def, specialize ih (T.sum id), specialize ih (λ z hz, hS z (finset.mem_insert_of_mem hz)), specialize ih rfl, rw ih, specialize hS n, specialize hS (finset.mem_insert_self n T), rcases hS with ⟨x', y', n', H', hn'⟩, rw hn', rw finsupp.sum_single_index, specialize hh x' y', simp [function.comp, tprod] at hh, clear H' hn', suffices : h ⟦finsupp.single (x', y') n'⟧ = gsmul (f x' y') n', { rw this }, cases n', { induction n' with n' ih', { rw int.of_nat_zero, rw finsupp.single_zero, rw gsmul_zero, exact H.zero }, { rw int.of_nat_succ, rw finsupp.single_add, rw ← add_quot, rw H.add, rw [ih', hh], rw gsmul_add, rw gsmul_one } }, { induction n' with n' ih', { rw int.neg_succ_of_nat_eq, rw finsupp.single_neg, rw gsmul_neg, rw finsupp.single_add, simp, rw ← neg_quot, rw H.neg, rw hh }, { rw int.neg_succ_of_nat_coe, rw int.neg_succ_of_nat_eq at ih', rw int.coe_nat_add, rw finsupp.single_neg at ih' ⊢, rw ← neg_quot at ih' ⊢, rw H.neg at ih' ⊢, rw nat.succ_eq_add_one, rw finsupp.single_add, rw ← add_quot, rw H.add, rw neg_add, rw int.coe_nat_add, rw int.coe_nat_eq 1, rw int.of_nat_one, rw ih', rw hh, simp [gsmul_add, gsmul_neg] } }, { rw gsmul_zero }, { intros, rw gsmul_zero }, { intros, rw gsmul_add }, { exact hnT } } end end universal_property instance universal_property {f : β → γ → α₁} (hf : is_bilinear_map f) : type_singleton { h : β ⊗ γ → α₁ // ∃ (H : is_linear_map h), ∀ x y, h (x ⊗ₛ y) = f x y } := { default := ⟨universal_property.factor hf, universal_property.factor_linear hf, universal_property.factor_commutes hf⟩, unique := λ ⟨h, H, hh⟩, subtype.eq $ universal_property.factor_unique hf h H hh } variables {β γ α₁} protected theorem ext {f g : β ⊗ γ → α₁} (hf : is_linear_map f) (hg : is_linear_map g) (h : ∀ x y, f (x ⊗ₛ y) = g (x ⊗ₛ y)) (z : β ⊗ γ) : f z = g z := have h1 : _ := universal_property.factor_unique (tprod.is_bilinear_map.comp hg) f hf h, have h2 : _ := universal_property.factor_unique (tprod.is_bilinear_map.comp hg) g hg (λ x y, rfl), congr_fun (h1.trans h2.symm) z variables (β γ) protected def id : β ⊗ α ≃ₘ β := let hba1 : β → α → β := λ x y, y • x in have hba2 : is_bilinear_map hba1, by refine {..}; intros; simp [hba1, smul_add, add_smul, smul_smul, mul_comm, mul_left_comm], let hba3 : β ⊗ α → β := universal_property.factor hba2 in have hba4 : _ := universal_property.factor_linear hba2, have hba5 : _ := universal_property.factor_commutes hba2, let hb1 : β → β ⊗ α := λ x, x ⊗ₛ 1 in have hb2 : is_linear_map hb1, from { add := λ x y, add_tprod, smul := λ r x, smul_tprod }, have hbb1 : ∀ (x : β) (y : α), hb1 (hba3 (x ⊗ₛ y)) = x ⊗ₛ y, from λ x y, calc hb1 (hba3 (x ⊗ₛ y)) = (y • x) ⊗ₛ 1 : congr_arg hb1 (hba5 _ _) ... = y • x ⊗ₛ 1 : smul_tprod ... = x ⊗ₛ (y • 1) : eq.symm $ tprod_smul ... = x ⊗ₛ y : by simp, { to_fun := hba3, inv_fun := hb1, left_inv := tensor_product.ext (hb2.comp hba4) is_linear_map.id hbb1, right_inv := λ x, by simp *, linear := hba4 } protected def comm : β ⊗ γ ≃ₘ γ ⊗ β := let hbg1 : β → γ → γ ⊗ β := λ x y, y ⊗ₛ x in have hbg2 : is_bilinear_map hbg1, from { add_pair := λ x y z, tprod_add, pair_add := λ x y z, add_tprod, smul_pair := λ r x y, tprod_smul, pair_smul := λ r x y, smul_tprod }, let hbg3 : β ⊗ γ → γ ⊗ β := universal_property.factor hbg2 in have hbg4 : _ := universal_property.factor_linear hbg2, have hbg5 : _ := universal_property.factor_commutes hbg2, let hgb1 : γ → β → β ⊗ γ := λ y x , x ⊗ₛ y in have hgb2 : is_bilinear_map hgb1, from { add_pair := λ x y z, tprod_add, pair_add := λ x y z, add_tprod, smul_pair := λ r x y, tprod_smul, pair_smul := λ r x y, smul_tprod }, let hgb3 : γ ⊗ β → β ⊗ γ := universal_property.factor hgb2 in have hgb4 : _ := universal_property.factor_linear hgb2, have hgb5 : _ := universal_property.factor_commutes hgb2, have hbb1 : ∀ x y, (hgb3 ∘ hbg3) (x ⊗ₛ y) = x ⊗ₛ y, from λ x y, by simp [function.comp, *], have hbb2 : is_linear_map (hgb3 ∘ hbg3) := hgb4.comp hbg4, have hbb3 : _ := tensor_product.ext hbb2 is_linear_map.id hbb1, have hgg1 : ∀ y x, (hbg3 ∘ hgb3) (y ⊗ₛ x) = y ⊗ₛ x, from λ x y, by simp [function.comp, *], have hgg2 : is_linear_map (hbg3 ∘ hgb3) := hbg4.comp hgb4, have hgg3 : _ := tensor_product.ext hgg2 is_linear_map.id hgg1, { to_fun := hbg3, inv_fun := hgb3, left_inv := hbb3, right_inv := hgg3, linear := hbg4 } protected def prod_tensor : (β × γ) ⊗ α₁ ≃ₘ β ⊗ α₁ × γ ⊗ α₁ := let ha1 : β × γ → α₁ → β ⊗ α₁ × γ ⊗ α₁ := λ z r, (z.fst ⊗ₛ r, z.snd ⊗ₛ r) in have ha2 : is_bilinear_map ha1, from { add_pair := λ x y z, prod.ext.2 ⟨add_tprod, add_tprod⟩, pair_add := λ x y z, prod.ext.2 ⟨tprod_add, tprod_add⟩, smul_pair := λ r x y, prod.ext.2 ⟨smul_tprod, smul_tprod⟩ , pair_smul := λ r x y, prod.ext.2 ⟨tprod_smul, tprod_smul⟩ }, let ha3 : (β × γ) ⊗ α₁ → β ⊗ α₁ × γ ⊗ α₁ := universal_property.factor ha2 in have ha4 : _ := universal_property.factor_linear ha2, have ha5 : _ := universal_property.factor_commutes ha2, let hb1 : β → α₁ → (β × γ) ⊗ α₁ := λ x r, (x, 0) ⊗ₛ r in have hb2 : is_bilinear_map hb1, from { add_pair := λ x y z, calc (x + y, (0:γ)) ⊗ₛ z = (x + y, 0 + 0) ⊗ₛ z : congr_arg (λ r, (x + y, r) ⊗ₛ z) (zero_add 0).symm ... = ((x, 0) + (y, 0)) ⊗ₛ z : rfl ... = (x, 0) ⊗ₛ z + (y, 0) ⊗ₛ z : add_tprod, pair_add := λ x y z, tprod_add, smul_pair := λ r x y, calc (r • x, (0:γ)) ⊗ₛ y = (r • (x, 0)) ⊗ₛ y : by simp only [prod.smul_prod, smul_zero] ... = r • (x, 0) ⊗ₛ y : smul_tprod, pair_smul := λ r x y, tprod_smul }, let hb3 : β ⊗ α₁ → (β × γ) ⊗ α₁ := universal_property.factor hb2 in have hb4 : _ := universal_property.factor_linear hb2, have hb5 : _ := universal_property.factor_commutes hb2, have hb6 : ∀ x, ha3 (hb3 x) = prod.inl x := tensor_product.ext (ha4.comp hb4) prod.is_linear_map_prod_inl $ λ x y, calc ha3 (hb3 (x ⊗ₛ y)) = ha3 ((x, 0) ⊗ₛ y) : congr_arg ha3 (hb5 x y) ... = (x ⊗ₛ y, 0 ⊗ₛ y) : ha5 (x, 0) y ... = (x ⊗ₛ y, 0) : congr_arg (λ z, (x ⊗ₛ y, z)) zero_tprod, let hc1 : γ → α₁ → (β × γ) ⊗ α₁ := λ x r, (0, x) ⊗ₛ r in have hc2 : is_bilinear_map hc1, from { add_pair := λ x y z, calc ((0:β), x + y) ⊗ₛ z = (0 + 0, x + y) ⊗ₛ z : congr_arg (λ r, (r, x + y) ⊗ₛ z) (zero_add 0).symm ... = ((0, x) + (0, y)) ⊗ₛ z : rfl ... = (0, x) ⊗ₛ z + (0, y) ⊗ₛ z : add_tprod, pair_add := λ x y z, tprod_add, smul_pair := λ r x y, calc ((0:β), r • x) ⊗ₛ y = (r • (0, x)) ⊗ₛ y : by simp only [prod.smul_prod, smul_zero] ... = r • (0, x) ⊗ₛ y : smul_tprod, pair_smul := λ r x y, tprod_smul }, let hc3 : γ ⊗ α₁ → (β × γ) ⊗ α₁ := universal_property.factor hc2 in have hc4 : _ := universal_property.factor_linear hc2, have hc5 : _ := universal_property.factor_commutes hc2, have hc6 : ∀ y, ha3 (hc3 y) = prod.inr y := tensor_product.ext (ha4.comp hc4) prod.is_linear_map_prod_inr $ λ x y, calc ha3 (hc3 (x ⊗ₛ y)) = ha3 ((0, x) ⊗ₛ y) : congr_arg ha3 (hc5 x y) ... = (0 ⊗ₛ y, x ⊗ₛ y) : ha5 (0, x) y ... = (0, x ⊗ₛ y) : congr_arg (λ z, (z, x ⊗ₛ y)) zero_tprod, let hd1 : β ⊗ α₁ × γ ⊗ α₁ → (β × γ) ⊗ α₁ := λ z, hb3 z.fst + hc3 z.snd in have hd2 : is_linear_map hd1, from { add := λ x y, calc hb3 (x + y).fst + hc3 (x + y).snd = hb3 (x.fst + y.fst) + hc3 (x.snd + y.snd) : rfl ... = (hb3 x.fst + hb3 y.fst) + hc3 (x.snd + y.snd) : congr_arg (λ z, z + hc3 (x.snd + y.snd)) (hb4.add x.fst y.fst) ... = (hb3 x.fst + hb3 y.fst) + (hc3 x.snd + hc3 y.snd) : congr_arg (λ z, (hb3 x.fst + hb3 y.fst) + z) (hc4.add x.snd y.snd) ... = hb3 x.fst + (hb3 y.fst + (hc3 x.snd + hc3 y.snd)) : add_assoc _ _ _ ... = hb3 x.fst + ((hc3 x.snd + hc3 y.snd) + hb3 y.fst) : congr_arg _ (add_comm _ _) ... = hb3 x.fst + (hc3 x.snd + (hc3 y.snd + hb3 y.fst)) : congr_arg _ (add_assoc _ _ _) ... = hb3 x.fst + (hc3 x.snd + (hb3 y.fst + hc3 y.snd)) : by have := congr_arg (λ z, hb3 x.fst + (hc3 x.snd + z)) (add_comm (hc3 y.snd) (hb3 y.fst)); dsimp at this; exact this ... = (hb3 x.fst + hc3 x.snd) + (hb3 y.fst + hc3 y.snd) : eq.symm $ add_assoc _ _ _, smul := λ r x, calc hb3 (r • x.fst) + hc3 (r • x.snd) = hb3 (r • x.fst) + r • (hc3 x.snd) : congr_arg _ (hc4.smul r x.snd) ... = r • (hb3 x.fst) + r • (hc3 x.snd) : congr_arg (λ z, z + r • (hc3 x.snd)) (hb4.smul r x.fst) ... = r • (hb3 x.fst + hc3 x.snd) : eq.symm $ @smul_add _ _ _ _ r (hb3 x.fst) (hc3 x.snd) ... = r • hd1 x : rfl }, have h1 : is_linear_map (hd1 ∘ ha3), from hd2.comp ha4, have h2 : _ := tensor_product.ext h1 is_linear_map.id (λ x y, by simp [function.comp, *, add_tprod.symm]), have h3 : ∀ z, (ha3 ∘ hd1) z = id z, from λ z, calc ha3 (hd1 z) = ha3 (hb3 z.fst + hc3 z.snd) : rfl ... = ha3 (hb3 z.fst) + ha3 (hc3 z.snd) : ha4.add _ _ ... = prod.inl z.fst + prod.inr z.snd : by rw [hb6, hc6] ... = (z.fst + 0, 0 + z.snd) : rfl ... = z : by cases z; simp [prod.inl, prod.inr], { to_fun := ha3, inv_fun := hd1, left_inv := h2, right_inv := h3, linear := ha4 } protected def assoc : (β ⊗ γ) ⊗ α₁ ≃ₘ β ⊗ (γ ⊗ α₁) := let ha1 (z : α₁) : β → γ → β ⊗ (γ ⊗ α₁) := λ x y, x ⊗ₛ (y ⊗ₛ z) in have ha2 : Π (z : α₁), is_bilinear_map (ha1 z), from λ z, { add_pair := λ m n k, add_tprod, pair_add := λ m n k, calc m ⊗ₛ ((n + k) ⊗ₛ z) = m ⊗ₛ (n ⊗ₛ z + k ⊗ₛ z) : congr_arg (λ b, m ⊗ₛ b) add_tprod ... = m ⊗ₛ (n ⊗ₛ z) + m ⊗ₛ (k ⊗ₛ z) : tprod_add, smul_pair := λ r m n, smul_tprod, pair_smul := λ r m n, calc m ⊗ₛ ((r • n) ⊗ₛ z) = m ⊗ₛ (r • n ⊗ₛ z) : congr_arg _ smul_tprod ... = r • m ⊗ₛ (n ⊗ₛ z) : tprod_smul }, let ha3 : β ⊗ γ → α₁ → β ⊗ (γ ⊗ α₁) := λ xy z, universal_property.factor (ha2 z) xy in have ha4 : _ := λ z, universal_property.factor_linear (ha2 z), have ha5 : _ := λ z, universal_property.factor_commutes (ha2 z), have ha6 : is_bilinear_map ha3, from { add_pair := λ m n k, (ha4 k).add m n, pair_add := λ m n k, (tensor_product.ext (ha4 $ n + k) ((ha4 n).map_add (ha4 k)) $ λ x y, calc ha3 (x ⊗ₛ y) (n + k) = x ⊗ₛ (y ⊗ₛ (n + k)) : ha5 (n + k) x y ... = x ⊗ₛ (y ⊗ₛ n + y ⊗ₛ k) : congr_arg ((⊗ₛ) x) tprod_add ... = x ⊗ₛ (y ⊗ₛ n) + x ⊗ₛ (y ⊗ₛ k) : tprod_add ... = x ⊗ₛ (y ⊗ₛ n) + ha3 (x ⊗ₛ y) k : congr_arg (λ b, x ⊗ₛ (y ⊗ₛ n) + b) (ha5 k _ _).symm ... = ha3 (x ⊗ₛ y) n + ha3 (x ⊗ₛ y) k : congr_arg (λ b, b + ha3 (x ⊗ₛ y) k) (ha5 n _ _).symm) m, smul_pair := λ r x y, (ha4 y).smul r x, pair_smul := λ r x y, (tensor_product.ext (ha4 $ r • y) (ha4 y).map_smul_right $ λ m n, calc ha3 (m ⊗ₛ n) (r • y) = m ⊗ₛ (n ⊗ₛ (r • y)) : ha5 (r • y) m n ... = m ⊗ₛ (r • n ⊗ₛ y) : congr_arg _ tprod_smul ... = r • m ⊗ₛ (n ⊗ₛ y) : tprod_smul ... = r • ha3 (m ⊗ₛ n) y : congr_arg _ (ha5 y m n).symm) x }, let ha7 : (β ⊗ γ) ⊗ α₁ → β ⊗ (γ ⊗ α₁) := universal_property.factor ha6 in have ha8 : _ := universal_property.factor_linear ha6, have ha9 : _ := universal_property.factor_commutes ha6, let hb1 (x : β) : γ → α₁ → (β ⊗ γ) ⊗ α₁ := λ y z, (x ⊗ₛ y) ⊗ₛ z in have hb2 : Π (x : β), is_bilinear_map (hb1 x), from λ x, { add_pair := λ m n k, calc (x ⊗ₛ (m + n)) ⊗ₛ k = (x ⊗ₛ m + x ⊗ₛ n) ⊗ₛ k : congr_arg (λ z, z ⊗ₛ k) tprod_add ... = (x ⊗ₛ m) ⊗ₛ k + (x ⊗ₛ n) ⊗ₛ k : add_tprod, pair_add := λ m n k, tprod_add, smul_pair := λ r m n, calc (x ⊗ₛ (r • m)) ⊗ₛ n = (r • x ⊗ₛ m) ⊗ₛ n : congr_arg (λ z, z ⊗ₛ n) tprod_smul ... = r • (x ⊗ₛ m) ⊗ₛ n : smul_tprod, pair_smul := λ r m n, tprod_smul }, let hb3 : β → γ ⊗ α₁ → (β ⊗ γ) ⊗ α₁ := λ x, universal_property.factor (hb2 x) in have hb4 : _ := λ x, universal_property.factor_linear (hb2 x), have hb5 : _ := λ x, universal_property.factor_commutes (hb2 x), have hb6 : is_bilinear_map hb3, from { add_pair := λ m n k, (tensor_product.ext (hb4 $ m + n) ((hb4 m).map_add (hb4 n)) $ λ x y, calc hb3 (m + n) (x ⊗ₛ y) = ((m + n) ⊗ₛ x) ⊗ₛ y : (hb5 $ m + n) x y ... = (m ⊗ₛ x + n ⊗ₛ x) ⊗ₛ y : congr_arg (λ z, z ⊗ₛ y) add_tprod ... = (m ⊗ₛ x) ⊗ₛ y + (n ⊗ₛ x) ⊗ₛ y : add_tprod ... = (m ⊗ₛ x) ⊗ₛ y + hb3 n (x ⊗ₛ y) : congr_arg _ ((hb5 n) x y).symm ... = hb3 m (x ⊗ₛ y) + hb3 n (x ⊗ₛ y) : congr_arg (λ z, z + hb3 n (x ⊗ₛ y)) ((hb5 m) x y).symm) k, pair_add := λ m n k, (hb4 m).add n k, smul_pair := λ r x y, (tensor_product.ext (hb4 $ r • x) (hb4 x).map_smul_right $ λ m n, calc hb3 (r • x) (m ⊗ₛ n) = ((r • x) ⊗ₛ m) ⊗ₛ n : (hb5 $ r • x) m n ... = (r • x ⊗ₛ m) ⊗ₛ n : congr_arg (λ z, z ⊗ₛ n) smul_tprod ... = r • (x ⊗ₛ m) ⊗ₛ n : smul_tprod ... = r • hb3 x (m ⊗ₛ n) : congr_arg _ ((hb5 $ x) m n).symm) y, pair_smul := λ r x y, (hb4 x).smul r y }, let hb7 : β ⊗ (γ ⊗ α₁) → (β ⊗ γ) ⊗ α₁ := universal_property.factor hb6 in have hb8 : _ := universal_property.factor_linear hb6, have hb9 : _ := universal_property.factor_commutes hb6, have hc1 : _ := λ z, tensor_product.ext (hb8.comp $ ha6.linear_pair z) (tprod.is_bilinear_map.linear_pair z) $ λ x y, calc hb7 (ha3 (x ⊗ₛ y) z) = hb7 (x ⊗ₛ (y ⊗ₛ z)) : congr_arg hb7 (ha5 z x y) ... = hb3 x (y ⊗ₛ z) : hb9 x (y ⊗ₛ z) ... = (x ⊗ₛ y) ⊗ₛ z : hb5 x y z, have hc2 : _ := tensor_product.ext (hb8.comp ha8) is_linear_map.id $ λ xy z, calc hb7 (ha7 (xy ⊗ₛ z)) = hb7 (ha3 xy z) : congr_arg hb7 (ha9 xy z) ... = xy ⊗ₛ z : hc1 z xy, have hd1 : _ := λ x, tensor_product.ext (ha8.comp $ hb6.pair_linear x) (tprod.is_bilinear_map.pair_linear x) $ λ y z, calc ha7 (hb3 x (y ⊗ₛ z)) = ha7 ((x ⊗ₛ y) ⊗ₛ z) : congr_arg ha7 (hb5 x y z) ... = ha3 (x ⊗ₛ y) z : ha9 (x ⊗ₛ y) z ... = x ⊗ₛ (y ⊗ₛ z) : ha5 z x y, have hd2 : _ := tensor_product.ext (ha8.comp hb8) is_linear_map.id $ λ x yz, calc ha7 (hb7 (x ⊗ₛ yz)) = ha7 (hb3 x yz) : congr_arg ha7 (hb9 x yz) ... = x ⊗ₛ yz : hd1 x yz, { to_fun := ha7, inv_fun := hb7, left_inv := hc2, right_inv := hd2, linear := ha8 } variables {α β γ α₁ β₁ γ₁} variables {f : β → γ} (hf : is_linear_map f) variables {g : β₁ → γ₁} (hg : is_linear_map g) include hf hg def tprod_map : β ⊗ β₁ → γ ⊗ γ₁ := let h1 : β → β₁ → γ ⊗ γ₁ := λ x y, f x ⊗ₛ g y in have h2 : is_bilinear_map h1 := { add_pair := λ m n k, by simp [h1]; rw [hf.add, add_tprod], pair_add := λ m n k, by simp [h1]; rw [hg.add, tprod_add], smul_pair := λ r m n, by simp [h1]; rw [hf.smul, smul_tprod], pair_smul := λ r m n, by simp [h1]; rw [hg.smul, tprod_smul] }, universal_property.factor h2 end tensor_product class is_ring_hom {α : Type u} {β : Type v} [comm_ring α] [comm_ring β] (f : α → β) : Prop := (map_add : ∀ {x y}, f (x + y) = f x + f y) (map_mul : ∀ {x y}, f (x * y) = f x * f y) (map_one : f 1 = 1) namespace is_ring_hom variables {α : Type u} {β : Type v} [comm_ring α] [comm_ring β] variables (f : α → β) [is_ring_hom f] {x y : α} lemma map_zero : f 0 = 0 := calc f 0 = f (0 + 0) - f 0 : by rw [map_add f]; simp ... = 0 : by simp lemma map_neg : f (-x) = -f x := calc f (-x) = f (-x + x) - f x : by rw [map_add f]; simp ... = -f x : by simp [map_zero f] lemma map_sub : f (x - y) = f x - f y := by simp [map_add f, map_neg f] end is_ring_hom instance is_ring_hom.to_module {α : Type u} {β : Type v} [comm_ring α] [comm_ring β] (f : α → β) [is_ring_hom f] : module α β := { smul := λ r x, f r * x, smul_add := λ r x y, by simp [mul_add], add_smul := λ r₁ r₂ x, by simp [is_ring_hom.map_add f, add_mul], mul_smul := λ r₁ r₂ x, by simp [is_ring_hom.map_mul f, mul_assoc], one_smul := λ x, by simp [is_ring_hom.map_one f] } section algebra_tensor parameters {α : Type u} {β : Type v} {γ : Type w} parameters [comm_ring α] [comm_ring β] [comm_ring γ] parameters (f : α → β) [is_ring_hom f] parameters (g : α → γ) [is_ring_hom g] variables {r r₁ r₂ : α} {x x₁ x₂ : β} {y y₁ y₂ : γ} include α f g def module_f := is_ring_hom.to_module f def module_g := is_ring_hom.to_module g local attribute [instance] module_f module_g def h1 : β → γ → β → γ → β ⊗ γ := λ x₁ y₁ x₂ y₂, (x₁ * x₂) ⊗ₛ (y₁ * y₂) def δ := @tensor_product α β γ _ (is_ring_hom.to_module f) (is_ring_hom.to_module g) instance η : module α δ := tensor_product.module α β γ theorem h2 : Π (x : β) (y : γ), is_bilinear_map (h1 x y) := λ x y, { add_pair := λ m n k, by simp [h1]; rw [mul_add, tensor_product.add_tprod], pair_add := λ m n k, by simp [h1]; rw [mul_add, tensor_product.tprod_add], smul_pair := λ r m n, calc (x * (f r * m)) ⊗ₛ (y * n) = (f r * (x * m)) ⊗ₛ (y * n) : by rw [mul_left_comm] ... = r • (x * m) ⊗ₛ (y * n) : tensor_product.smul_tprod, pair_smul := λ r m n, calc (x * m) ⊗ₛ (y * (g r * n)) = (x * m) ⊗ₛ (g r * (y * n)) : by rw [mul_left_comm] ... = r • (x * m) ⊗ₛ (y * n) : tensor_product.tprod_smul } def h3 (z : β ⊗ γ) : β → γ → δ := λ x y, tensor_product.universal_property.factor (h2 x y) z def h4 : Π x y, is_linear_map (λ z, h3 z x y) := λ x y, tensor_product.universal_property.factor_linear (h2 x y) theorem h5 : ∀ x₁ y₁ x₂ y₂, h3 (x₂ ⊗ₛ y₂) x₁ y₁ = (x₁ * x₂) ⊗ₛ (y₁ * y₂) := λ x y, tensor_product.universal_property.factor_commutes (h2 x y) theorem h6 : ∀ t, is_bilinear_map (h3 t) := λ t, { add_pair := λ x y z, (tensor_product.ext (h4 (x + y) z) ((h4 x z).map_add (h4 y z)) $ λ m n, calc h3 (m ⊗ₛ n) (x + y) z = ((x + y) * m) ⊗ₛ (z * n) : h5 (x + y) z m n ... = (x * m) ⊗ₛ (z * n) + (y * m) ⊗ₛ (z * n) : by rw [add_mul, tensor_product.add_tprod] ... = (x * m) ⊗ₛ (z * n) + h3 (m ⊗ₛ n) y z : congr_arg _ (h5 y z m n).symm ... = h3 (m ⊗ₛ n) x z + h3 (m ⊗ₛ n) y z : congr_arg (λ b, b + h3 (m ⊗ₛ n) y z) (h5 x z m n).symm) t, pair_add := λ x y z, (tensor_product.ext (h4 x (y + z)) ((h4 x y).map_add (h4 x z)) $ λ m n, calc h3 (m ⊗ₛ n) x (y + z) = (x * m) ⊗ₛ ((y + z) * n) : h5 x (y + z) m n ... = (x * m) ⊗ₛ (y * n) + (x * m) ⊗ₛ (z * n) : by rw [add_mul, tensor_product.tprod_add] ... = (x * m) ⊗ₛ (y * n) + h3 (m ⊗ₛ n) x z : congr_arg _ (h5 x z m n).symm ... = h3 (m ⊗ₛ n) x y + h3 (m ⊗ₛ n) x z : congr_arg (λ b, b + h3 (m ⊗ₛ n) x z) (h5 x y m n).symm) t, smul_pair := λ r x y, (tensor_product.ext (h4 (r • x) y) (h4 x y).map_smul_right $ λ m n, calc h3 (m ⊗ₛ n) (f r * x) y = ((f r * x) * m) ⊗ₛ (y * n) : h5 (r • x) y m n ... = (f r * (x * m)) ⊗ₛ (y * n) : congr_arg (λ z, z ⊗ₛ (y * n)) (mul_assoc _ _ _) ... = r • (x * m) ⊗ₛ (y * n) : tensor_product.smul_tprod ... = r • h3 (m ⊗ₛ n) x y : congr_arg _ (h5 x y m n).symm) t, pair_smul := λ r x y, (tensor_product.ext (h4 x (r • y)) (h4 x y).map_smul_right $ λ m n, calc h3 (m ⊗ₛ n) x (g r * y) = (x * m) ⊗ₛ ((g r * y) * n) : h5 x (r • y) m n ... = (x * m) ⊗ₛ (g r * (y * n)) : congr_arg _ (mul_assoc _ _ _) ... = r • (x * m) ⊗ₛ (y * n) : tensor_product.tprod_smul ... = r • h3 (m ⊗ₛ n) x y : congr_arg _ (h5 x y m n).symm) t } def algebra_tensor.mul : β ⊗ γ → β ⊗ γ → β ⊗ γ := λ x, tensor_product.universal_property.factor (h6 x) theorem h7 : ∀ x, is_linear_map (algebra_tensor.mul x) := λ x, tensor_product.universal_property.factor_linear (h6 x) theorem h8 : ∀ x y z, algebra_tensor.mul x (y ⊗ₛ z) = h3 x y z := λ x, tensor_product.universal_property.factor_commutes (h6 x) theorem h9 : is_bilinear_map algebra_tensor.mul := { add_pair := λ x y, tensor_product.ext (h7 $ x + y) ((h7 x).map_add (h7 y)) $ λ m n, by rw [h8, h8, h8]; exact (h4 m n).add x y, pair_add := λ x, (h7 x).add, smul_pair := λ r x, tensor_product.ext (h7 $ r • x) (h7 x).map_smul_right $ λ m n, by rw [h8, h8]; exact (h4 m n).smul r x, pair_smul := λ r x, (h7 x).smul r } theorem algebra_tensor.mul.comm : ∀ x y, algebra_tensor.mul x y = algebra_tensor.mul y x := λ x, tensor_product.ext (h9.pair_linear x) (h9.linear_pair x) (λ y z, (tensor_product.ext (h9.linear_pair $ y ⊗ₛ z) (h9.pair_linear $ y ⊗ₛ z) (λ m n, by rw [h8, h8, h5, h5]; simp [mul_comm])) x) theorem algebra_tensor.mul.assoc : ∀ x y z, algebra_tensor.mul (algebra_tensor.mul x y) z = algebra_tensor.mul x (algebra_tensor.mul y z) := begin intros x y, apply tensor_product.ext, { exact h9.pair_linear (algebra_tensor.mul x y) }, { exact (h9.pair_linear x).comp (h9.pair_linear y) }, { intros m n, revert y, apply tensor_product.ext, { exact (h9.linear_pair (m ⊗ₛ n)).comp (h9.pair_linear x) }, { exact (h9.pair_linear x).comp (h9.linear_pair (m ⊗ₛ n)) }, { intros c d, revert x, apply tensor_product.ext, { exact (h9.linear_pair (m ⊗ₛ n)).comp (h9.linear_pair (c ⊗ₛ d)) }, { exact h9.linear_pair (algebra_tensor.mul (c ⊗ₛ d) (m ⊗ₛ n)) }, { intros p q, rw [h8, h8, h8, h5, h5, h5, h8, h5], simp only [mul_assoc] } } } end def algebra_tensor.hom : α → (β ⊗ γ) := λ x, f x ⊗ₛ 1 instance algebra_tensor.to_comm_ring : comm_ring (β ⊗ γ) := { module.to_add_comm_group (β ⊗ γ) with mul := algebra_tensor.mul, mul_assoc := algebra_tensor.mul.assoc, one := 1 ⊗ₛ 1, one_mul := tensor_product.ext (h9.pair_linear $ 1 ⊗ₛ 1) is_linear_map.id $ λ x y, by rw [h8, h5, mul_one, mul_one], mul_one := tensor_product.ext (h9.linear_pair $ 1 ⊗ₛ 1) is_linear_map.id $ λ x y, by simp only [(*)]; rw [h8, h5, one_mul, one_mul], left_distrib := h9.pair_add, right_distrib := h9.add_pair, mul_comm := algebra_tensor.mul.comm } instance algebra_tensor.is_ring_hom : is_ring_hom algebra_tensor.hom := { map_add := λ x y, by simp [algebra_tensor.hom, is_ring_hom.map_add f], map_mul := λ x y, calc algebra_tensor.hom (x * y) = f (x * y) ⊗ₛ 1 : rfl ... = (f x * f y) ⊗ₛ 1 : by rw is_ring_hom.map_mul f ... = (f y * f x) ⊗ₛ (1 * 1) : by simp [mul_comm] ... = h3 (f x ⊗ₛ 1) (f y) 1 : (h5 _ _ _ 1).symm ... = (f x ⊗ₛ 1) * (f y ⊗ₛ 1) : (h8 _ _ _).symm ... = algebra_tensor.hom x * algebra_tensor.hom y : rfl, map_one := by simp [algebra_tensor.hom, is_ring_hom.map_one f]; refl } end algebra_tensor
c2ea0041f7751366d5724375c09898e30b3f0cdb
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/category_theory/closed/zero.lean
186a74dc0d0173a6119c5a30304df2465a4456fa
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
2,193
lean
/- Copyright (c) 2020 Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.category_theory.closed.cartesian import Mathlib.category_theory.limits.shapes.zero import Mathlib.category_theory.punit import Mathlib.category_theory.conj import Mathlib.PostPort universes u v namespace Mathlib /-! # A cartesian closed category with zero object is trivial A cartesian closed category with zero object is trivial: it is equivalent to the category with one object and one morphism. ## References * https://mathoverflow.net/a/136480 -/ namespace category_theory /-- If a cartesian closed category has an initial object which is isomorphic to the terminal object, then each homset has exactly one element. -/ def unique_homset_of_initial_iso_terminal {C : Type u} [category C] [limits.has_finite_products C] [cartesian_closed C] [limits.has_initial C] (i : ⊥_C ≅ ⊤_C) (X : C) (Y : C) : unique (X ⟶ Y) := equiv.unique (equiv.trans (equiv.trans (iso.hom_congr (iso.symm (limits.prod.right_unitor X)) (iso.refl Y)) (iso.hom_congr (limits.prod.map_iso (iso.refl X) (iso.symm i)) (iso.refl Y))) (adjunction.hom_equiv (exp.adjunction X) (⊥_C) Y)) /-- If a cartesian closed category has a zero object, each homset has exactly one element. -/ def unique_homset_of_zero {C : Type u} [category C] [limits.has_finite_products C] [cartesian_closed C] [limits.has_zero_object C] (X : C) (Y : C) : unique (X ⟶ Y) := unique_homset_of_initial_iso_terminal (iso.mk Inhabited.default (Inhabited.default ≫ Inhabited.default)) X Y /-- A cartesian closed category with a zero object is equivalent to the category with one object and one morphism. -/ def equiv_punit {C : Type u} [category C] [limits.has_finite_products C] [cartesian_closed C] [limits.has_zero_object C] : C ≌ discrete PUnit := equivalence.mk (functor.star C) (functor.from_punit 0) (nat_iso.of_components (fun (X : C) => iso.mk Inhabited.default Inhabited.default) sorry) (functor.punit_ext (functor.from_punit 0 ⋙ functor.star C) 𝟭)
af1874b98a3fca604f0eed37127a65c76dcc4b89
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/ring_theory/tensor_product.lean
8992834fbbd0e80eebf7bb99fd5ff459c4b1dae0
[ "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
16,585
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 linear_algebra.tensor_product import ring_theory.algebra universes u v₁ v₂ v₃ v₄ /-! The tensor product of R-algebras. We construct the R-algebra structure on `A ⊗[R] B`, when `A` and `B` are both `R`-algebras, and provide the structure isomorphisms * `R ⊗[R] A ≃ₐ[R] A` * `A ⊗[R] R ≃ₐ[R] A` * `A ⊗[R] B ≃ₐ[R] B ⊗[R] A` The code for * `((A ⊗[R] B) ⊗[R] C) ≃ₐ[R] (A ⊗[R] (B ⊗[R] C))` is written and compiles, but takes longer than the `-T100000` time limit, so is currently commented out. -/ namespace algebra open_locale tensor_product open tensor_product namespace tensor_product section semiring variables {R : Type u} [comm_semiring R] variables {A : Type v₁} [semiring A] [algebra R A] variables {B : Type v₂} [semiring B] [algebra R B] /-- (Implementation detail) The multiplication map on `A ⊗[R] B`, for a fixed pure tensor in the first argument, as an `R`-linear map. -/ def mul_aux (a₁ : A) (b₁ : B) : (A ⊗[R] B) →ₗ[R] (A ⊗[R] B) := begin -- Why doesn't `apply tensor_product.lift` work? apply @tensor_product.lift R _ A B (A ⊗[R] B) _ _ _ _ _ _ _, fsplit, intro a₂, fsplit, intro b₂, exact (a₁ * a₂) ⊗ₜ[R] (b₁ * b₂), { intros b₂ b₂', simp [mul_add, tmul_add], }, { intros c b₂, simp [mul_smul, tmul_smul], }, { intros a₂ a₂', ext b₂, simp [mul_add, add_tmul], }, { intros c a₂, ext b₂, simp [mul_smul, smul_tmul], } end @[simp] lemma mul_aux_apply (a₁ a₂ : A) (b₁ b₂ : B) : (mul_aux a₁ b₁) (a₂ ⊗ₜ[R] b₂) = (a₁ * a₂) ⊗ₜ[R] (b₁ * b₂) := rfl /-- (Implementation detail) The multiplication map on `A ⊗[R] B`, as an `R`-bilinear map. -/ def mul : (A ⊗[R] B) →ₗ[R] (A ⊗[R] B) →ₗ[R] (A ⊗[R] B) := begin apply @tensor_product.lift R _ A B ((A ⊗[R] B) →ₗ[R] (A ⊗[R] B)) _ _ _ _ _ _ _, fsplit, intro a₁, fsplit, intro b₁, exact mul_aux a₁ b₁, { intros b₁ b₁', -- Why doesn't just `apply tensor_product.ext`, or indeed `ext` work?! apply @tensor_product.ext R _ A B (A ⊗[R] B) _ _ _ _ _ _, intros a₂ b₂, simp [add_mul, tmul_add], }, { intros c b₁, apply @tensor_product.ext R _ A B (A ⊗[R] B) _ _ _ _ _ _, intros a₂ b₂, simp, }, { intros a₁ a₁', ext1 b₁, apply @tensor_product.ext R _ A B (A ⊗[R] B) _ _ _ _ _ _, intros a₂ b₂, simp [add_mul, add_tmul], }, { intros c a₁, ext1 b₁, apply @tensor_product.ext R _ A B (A ⊗[R] B) _ _ _ _ _ _, intros a₂ b₂, simp [smul_tmul], }, end @[simp] lemma mul_apply (a₁ a₂ : A) (b₁ b₂ : B) : mul (a₁ ⊗ₜ[R] b₁) (a₂ ⊗ₜ[R] b₂) = (a₁ * a₂) ⊗ₜ[R] (b₁ * b₂) := rfl lemma mul_assoc' (mul : (A ⊗[R] B) →ₗ[R] (A ⊗[R] B) →ₗ[R] (A ⊗[R] B)) (h : ∀ (a₁ a₂ a₃ : A) (b₁ b₂ b₃ : B), mul (mul (a₁ ⊗ₜ[R] b₁) (a₂ ⊗ₜ[R] b₂)) (a₃ ⊗ₜ[R] b₃) = mul (a₁ ⊗ₜ[R] b₁) (mul (a₂ ⊗ₜ[R] b₂) (a₃ ⊗ₜ[R] b₃))) : ∀ (x y z : A ⊗[R] B), mul (mul x y) z = mul x (mul y z) := begin intros, apply tensor_product.induction_on x, { simp, }, apply tensor_product.induction_on y, { simp, }, apply tensor_product.induction_on z, { simp, }, { intros, simp [h], }, { intros, simp [linear_map.map_add, *], }, { intros, simp [linear_map.map_add, *], }, { intros, simp [linear_map.map_add, *], }, end lemma mul_assoc (x y z : A ⊗[R] B) : mul (mul x y) z = mul x (mul y z) := mul_assoc' mul (by { intros, simp only [mul_apply, mul_assoc], }) x y z lemma one_mul (x : A ⊗[R] B) : mul (1 ⊗ₜ 1) x = x := begin apply tensor_product.induction_on x; simp {contextual := tt}, end lemma mul_one (x : A ⊗[R] B) : mul x (1 ⊗ₜ 1) = x := begin apply tensor_product.induction_on x; simp {contextual := tt}, end instance : semiring (A ⊗[R] B) := { zero := 0, add := (+), one := 1 ⊗ₜ 1, mul := λ a b, mul a b, one_mul := one_mul, mul_one := mul_one, mul_assoc := mul_assoc, zero_mul := by simp, mul_zero := by simp, left_distrib := by simp, right_distrib := by simp, .. (by apply_instance : add_comm_monoid (A ⊗[R] B)) }. lemma one_def : (1 : A ⊗[R] B) = (1 : A) ⊗ₜ (1 : B) := rfl @[simp] lemma tmul_mul_tmul (a₁ a₂ : A) (b₁ b₂ : B) : (a₁ ⊗ₜ[R] b₁) * (a₂ ⊗ₜ[R] b₂) = (a₁ * a₂) ⊗ₜ[R] (b₁ * b₂) := rfl @[simp] lemma tmul_pow (a : A) (b : B) (k : ℕ) : (a ⊗ₜ[R] b)^k = (a^k) ⊗ₜ[R] (b^k) := begin induction k with k ih, { simp [one_def], }, { simp [pow_succ, ih], } end /-- The algebra map `R →+* (A ⊗[R] B)` giving `A ⊗[R] B` the structure of an `R`-algebra. -/ def tensor_algebra_map : R →+* (A ⊗[R] B) := { to_fun := λ r, algebra_map R A r ⊗ₜ[R] 1, map_one' := by { simp, refl }, map_mul' := by simp, map_zero' := by simp [zero_tmul], map_add' := by simp [add_tmul], } instance : algebra R (A ⊗[R] B) := { commutes' := λ r x, begin apply tensor_product.induction_on x, { simp, }, { intros a b, simp [tensor_algebra_map, algebra.commutes], }, { intros y y' h h', simp at h h', simp [mul_add, add_mul, h, h'], }, end, smul_def' := λ r x, begin apply tensor_product.induction_on x, { simp [smul_zero], }, { intros a b, rw [tensor_algebra_map, ←tmul_smul, ←smul_tmul, algebra.smul_def r a], simp, }, { intros, dsimp, simp [smul_add, mul_add, *], }, end, .. tensor_algebra_map, .. (by apply_instance : semimodule R (A ⊗[R] B)) }. @[simp] lemma algebra_map_apply (r : R) : (algebra_map R (A ⊗[R] B)) r = ((algebra_map R A) r) ⊗ₜ[R] 1 := rfl variables {C : Type v₃} [semiring C] [algebra R C] @[ext] theorem ext {g h : (A ⊗[R] B) →ₐ[R] C} (H : ∀ a b, g (a ⊗ₜ b) = h (a ⊗ₜ b)) : g = h := begin apply @alg_hom.to_linear_map_inj R (A ⊗[R] B) C _ _ _ _ _ _ _ _, ext, simp [H], end /-- The algebra morphism `A →ₐ[R] A ⊗[R] B` sending `a` to `a ⊗ₜ 1`. -/ def include_left : A →ₐ[R] A ⊗[R] B := { to_fun := λ a, a ⊗ₜ 1, map_zero' := by simp, map_add' := by simp [add_tmul], map_one' := rfl, map_mul' := by simp, commutes' := by simp, } @[simp] lemma include_left_apply (a : A) : (include_left : A →ₐ[R] A ⊗[R] B) a = a ⊗ₜ 1 := rfl /-- The algebra morphism `B →ₐ[R] A ⊗[R] B` sending `b` to `1 ⊗ₜ b`. -/ def include_right : B →ₐ[R] A ⊗[R] B := { to_fun := λ b, 1 ⊗ₜ b, map_zero' := by simp, map_add' := by simp [tmul_add], map_one' := rfl, map_mul' := by simp, commutes' := λ r, begin simp only [algebra_map_apply], transitivity r • ((1 : A) ⊗ₜ[R] (1 : B)), { rw [←tmul_smul, algebra.smul_def], simp, }, { simp [algebra.smul_def], }, end, } @[simp] lemma include_right_apply (b : B) : (include_right : B →ₐ[R] A ⊗[R] B) b = 1 ⊗ₜ b := rfl end semiring section ring variables {R : Type u} [comm_ring R] variables {A : Type v₁} [ring A] [algebra R A] variables {B : Type v₂} [ring B] [algebra R B] instance : ring (A ⊗[R] B) := { .. (by apply_instance : add_comm_group (A ⊗[R] B)), .. (by apply_instance : semiring (A ⊗[R] B)) }. end ring section comm_ring variables {R : Type u} [comm_ring R] variables {A : Type v₁} [comm_ring A] [algebra R A] variables {B : Type v₂} [comm_ring B] [algebra R B] instance : comm_ring (A ⊗[R] B) := { mul_comm := λ x y, begin apply tensor_product.induction_on x, { simp, }, { intros a₁ b₁, apply tensor_product.induction_on y, { simp, }, { intros a₂ b₂, simp [mul_comm], }, { intros a₂ b₂ ha hb, simp [mul_add, add_mul, ha, hb], }, }, { intros x₁ x₂ h₁ h₂, simp [mul_add, add_mul, h₁, h₂], }, end .. (by apply_instance : ring (A ⊗[R] B)) }. end comm_ring /-- Verify that typeclass search finds the ring structure on `A ⊗[ℤ] B` when `A` and `B` are merely rings, by treating both as `ℤ`-algebras. -/ example {A : Type v₁} [ring A] {B : Type v₂} [ring B] : ring (A ⊗[ℤ] B) := by apply_instance /-- Verify that typeclass search finds the comm_ring structure on `A ⊗[ℤ] B` when `A` and `B` are merely comm_rings, by treating both as `ℤ`-algebras. -/ example {A : Type v₁} [comm_ring A] {B : Type v₂} [comm_ring B] : comm_ring (A ⊗[ℤ] B) := by apply_instance /-! We now build the structure maps for the symmetric monoidal category of `R`-algebras. -/ section monoidal section variables {R : Type u} [comm_semiring R] variables {A : Type v₁} [semiring A] [algebra R A] variables {B : Type v₂} [semiring B] [algebra R B] variables {C : Type v₃} [semiring C] [algebra R C] variables {D : Type v₄} [semiring D] [algebra R D] /-- Build an algebra morphism from a linear map out of a tensor product, and evidence of multiplicativity on pure tensors. -/ def alg_hom_of_linear_map_tensor_product (f : A ⊗[R] B →ₗ[R] C) (w₁ : ∀ (a₁ a₂ : A) (b₁ b₂ : B), f ((a₁ * a₂) ⊗ₜ (b₁ * b₂)) = f (a₁ ⊗ₜ b₁) * f (a₂ ⊗ₜ b₂)) (w₂ : ∀ r, f ((algebra_map R A) r ⊗ₜ[R] 1) = (algebra_map R C) r): A ⊗[R] B →ₐ[R] C := { map_one' := by simpa using w₂ 1, map_zero' := by simp, map_mul' := λ x y, begin apply tensor_product.induction_on x, { simp, }, { intros a₁ b₁, apply tensor_product.induction_on y, { simp, }, { intros a₂ b₂, simp [w₁], }, { intros x₁ x₂ h₁ h₂, simp at h₁, simp at h₂, simp [mul_add, add_mul, h₁, h₂], }, }, { intros x₁ x₂ h₁ h₂, simp at h₁, simp at h₂, simp [mul_add, add_mul, h₁, h₂], } end, commutes' := λ r, by simp [w₂], .. f } @[simp] lemma alg_hom_of_linear_map_tensor_product_apply (f w₁ w₂ x) : (alg_hom_of_linear_map_tensor_product f w₁ w₂ : A ⊗[R] B →ₐ[R] C) x = f x := rfl /-- Build an algebra equivalence from a linear equivalence out of a tensor product, and evidence of multiplicativity on pure tensors. -/ def alg_equiv_of_linear_equiv_tensor_product (f : A ⊗[R] B ≃ₗ[R] C) (w₁ : ∀ (a₁ a₂ : A) (b₁ b₂ : B), f ((a₁ * a₂) ⊗ₜ (b₁ * b₂)) = f (a₁ ⊗ₜ b₁) * f (a₂ ⊗ₜ b₂)) (w₂ : ∀ r, f ((algebra_map R A) r ⊗ₜ[R] 1) = (algebra_map R C) r): A ⊗[R] B ≃ₐ[R] C := { .. alg_hom_of_linear_map_tensor_product (f : A ⊗[R] B →ₗ[R] C) w₁ w₂, .. f } @[simp] lemma alg_equiv_of_linear_equiv_tensor_product_apply (f w₁ w₂ x) : (alg_equiv_of_linear_equiv_tensor_product f w₁ w₂ : A ⊗[R] B ≃ₐ[R] C) x = f x := rfl /-- Build an algebra equivalence from a linear equivalence out of a triple tensor product, and evidence of multiplicativity on pure tensors. -/ def alg_equiv_of_linear_equiv_triple_tensor_product (f : ((A ⊗[R] B) ⊗[R] C) ≃ₗ[R] D) (w₁ : ∀ (a₁ a₂ : A) (b₁ b₂ : B) (c₁ c₂ : C), f ((a₁ * a₂) ⊗ₜ (b₁ * b₂) ⊗ₜ (c₁ * c₂)) = f (a₁ ⊗ₜ b₁ ⊗ₜ c₁) * f (a₂ ⊗ₜ b₂ ⊗ₜ c₂)) (w₂ : ∀ r, f (((algebra_map R A) r ⊗ₜ[R] (1 : B)) ⊗ₜ[R] (1 : C)) = (algebra_map R D) r) : (A ⊗[R] B) ⊗[R] C ≃ₐ[R] D := { map_mul' := λ x y, begin apply tensor_product.induction_on x, { simp, }, { intros ab₁ c₁, apply tensor_product.induction_on y, { simp, }, { intros ab₂ c₂, apply tensor_product.induction_on ab₁, { simp, }, { intros a₁ b₁, apply tensor_product.induction_on ab₂, { simp, }, { simp [w₁], }, { intros x₁ x₂ h₁ h₂, simp at h₁, simp at h₂, simp [mul_add, add_tmul, h₁, h₂], }, }, { intros x₁ x₂ h₁ h₂, simp at h₁, simp at h₂, simp [add_mul, add_tmul, h₁, h₂], }, }, { intros x₁ x₂ h₁ h₂, simp at h₁, simp at h₂, simp [mul_add, add_mul, h₁, h₂], }, }, { intros x₁ x₂ h₁ h₂, simp at h₁, simp at h₂, simp [mul_add, add_mul, h₁, h₂], } end, commutes' := λ r, by simp [w₂], .. f } @[simp] lemma alg_equiv_of_linear_equiv_triple_tensor_product_apply (f w₁ w₂ x) : (alg_equiv_of_linear_equiv_triple_tensor_product f w₁ w₂ : (A ⊗[R] B) ⊗[R] C ≃ₐ[R] D) x = f x := rfl end variables {R : Type u} [comm_semiring R] variables {A : Type v₁} [semiring A] [algebra R A] variables {B : Type v₂} [semiring B] [algebra R B] variables {C : Type v₃} [semiring C] [algebra R C] variables {D : Type v₄} [semiring D] [algebra R D] section variables (R A) /-- The base ring is a left identity for the tensor product of algebra, up to algebra isomorphism. -/ protected def lid : R ⊗[R] A ≃ₐ[R] A := alg_equiv_of_linear_equiv_tensor_product (tensor_product.lid R A) (by simp [mul_smul]) (by simp [algebra.smul_def]) @[simp] theorem lid_tmul (r : R) (a : A) : (tensor_product.lid R A : (R ⊗ A → A)) (r ⊗ₜ a) = r • a := by simp [tensor_product.lid] /-- The base ring is a right identity for the tensor product of algebra, up to algebra isomorphism. -/ protected def rid : A ⊗[R] R ≃ₐ[R] A := alg_equiv_of_linear_equiv_tensor_product (tensor_product.rid R A) (by simp [mul_smul]) (by simp [algebra.smul_def]) @[simp] theorem rid_tmul (r : R) (a : A) : (tensor_product.rid R A : (A ⊗ R → A)) (a ⊗ₜ r) = r • a := by simp [tensor_product.rid] section variables (R A B) /-- The tensor product of R-algebras is commutative, up to algebra isomorphism. -/ protected def comm : A ⊗[R] B ≃ₐ[R] B ⊗[R] A := alg_equiv_of_linear_equiv_tensor_product (tensor_product.comm R A B) (by simp) (λ r, begin transitivity r • ((1 : B) ⊗ₜ[R] (1 : A)), { rw [←tmul_smul, algebra.smul_def], simp, }, { simp [algebra.smul_def], }, end) @[simp] theorem comm_tmul (a : A) (b : B) : (tensor_product.comm R A B : (A ⊗[R] B → B ⊗[R] A)) (a ⊗ₜ b) = (b ⊗ₜ a) := by simp [tensor_product.comm] end section variables {R A B C} lemma assoc_aux_1 (a₁ a₂ : A) (b₁ b₂ : B) (c₁ c₂ : C) : (tensor_product.assoc R A B C) (((a₁ * a₂) ⊗ₜ[R] b₁ * b₂) ⊗ₜ[R] c₁ * c₂) = (tensor_product.assoc R A B C) ((a₁ ⊗ₜ[R] b₁) ⊗ₜ[R] c₁) * (tensor_product.assoc R A B C) ((a₂ ⊗ₜ[R] b₂) ⊗ₜ[R] c₂) := rfl lemma assoc_aux_2 (r : R) : (tensor_product.assoc R A B C) (((algebra_map R A) r ⊗ₜ[R] 1) ⊗ₜ[R] 1) = (algebra_map R (A ⊗ (B ⊗ C))) r := rfl -- variables (R A B C) -- -- local attribute [elab_simple] alg_equiv_of_linear_equiv_triple_tensor_product -- /-- The associator for tensor product of R-algebras, as an algebra isomorphism. -/ -- -- FIXME This is _really_ slow to compile. :-( -- protected def assoc : ((A ⊗[R] B) ⊗[R] C) ≃ₐ[R] (A ⊗[R] (B ⊗[R] C)) := -- alg_equiv_of_linear_equiv_triple_tensor_product -- (tensor_product.assoc R A B C) -- assoc_aux_1 assoc_aux_2 -- variables {R A B C} -- @[simp] theorem assoc_tmul (a : A) (b : B) (c : C) : -- ((tensor_product.assoc R A B C) : (A ⊗[R] B) ⊗[R] C → A ⊗[R] (B ⊗[R] C)) ((a ⊗ₜ b) ⊗ₜ c) = a ⊗ₜ (b ⊗ₜ c) := -- rfl end variables {R A B C D} /-- The tensor product of a pair of algebra morphisms. -/ def map (f : A →ₐ[R] B) (g : C →ₐ[R] D) : A ⊗[R] C →ₐ[R] B ⊗[R] D := alg_hom_of_linear_map_tensor_product (tensor_product.map f.to_linear_map g.to_linear_map) (by simp) (by simp [alg_hom.commutes]) @[simp] theorem map_tmul (f : A →ₐ[R] B) (g : C →ₐ[R] D) (a : A) (c : C) : map f g (a ⊗ₜ c) = f a ⊗ₜ g c := rfl /-- Construct an isomorphism between tensor products of R-algebras from isomorphisms between the tensor factors. -/ def congr (f : A ≃ₐ[R] B) (g : C ≃ₐ[R] D) : A ⊗[R] C ≃ₐ[R] B ⊗[R] D := alg_equiv.of_alg_hom (map f g) (map f.symm g.symm) (ext $ λ b d, by simp) (ext $ λ a c, by simp) @[simp] lemma congr_apply (f : A ≃ₐ[R] B) (g : C ≃ₐ[R] D) (x) : congr f g x = (map (f : A →ₐ[R] B) (g : C →ₐ[R] D)) x := rfl @[simp] lemma congr_symm_apply (f : A ≃ₐ[R] B) (g : C ≃ₐ[R] D) (x) : (congr f g).symm x = (map (f.symm : B →ₐ[R] A) (g.symm : D →ₐ[R] C)) x := rfl end end monoidal end tensor_product end algebra
5c7227bf251f7c05362b161b1c7b3d79eaaa63a5
6fbf10071e62af7238f2de8f9aa83d55d8763907
/examples/sets.lean
4e86329d6290050512a61f104a8338b0f7b60f98
[]
no_license
HasanMukati/uva-cs-dm-s19
ee5aad4568a3ca330c2738ed579c30e1308b03b0
3e7177682acdb56a2d16914e0344c10335583dcf
refs/heads/master
1,596,946,213,130
1,568,221,949,000
1,568,221,949,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
30,845
lean
/- Intuitively a set is a collection of objects. That said, if one is not careful about what one allows a set to be, paradoxes can arise, making the logical system inconsistent, and thus useless. For more details, search for an explanation of Russell's paradox. The work needed to repair Russell's original mistake led to Zermelo-Frankel set theory, the set theory of everyday mathematics, and also, at least indirectly to the type theory that underpins Lean and relate proof assistants. There are two things to know about how sets and operations involving sets are reprented in Lean. First, in Lean, set is what we call a type constructor. Second, sets are identified with membership predicates. We discuss each of these idea next. -/ -- Type Constructors: set /- First, set is a type constructor, not a type. It takes a type parameter as an argument and returns a type, one now specialized to the argument type. Because it takes a type and returns a type, set (and a type constructor more generally) is a function: one of type, Type → Type. So, for example, set int is the type of sets with int-valued elements. Lean tells us that the set type constructor can actually take a type in any type universe, i.e., Type (which is really Type 0), Type 1, Type 2, etc. We needn't be concerned with that here. -/ #check set -- Membership Predicates /- Second, sets in Lean are identified with membership predicates: of type T → Prop, where T is type of elements in a set. The membership predicate is true for values in the set and not true otherwise. -/ #check set ℕ #reduce set ℕ -- Example: the empty set of ℕ /- For example the empty set of ℕ values, also written as ∅ ℕ, is literally defined as the predicate, λ n : ℕ, false. This predicate is satisfied for no value of type ℕ, and so the set it defines is the empty set. -/ #check (∅ : set ℕ ) /- We think of the predicate that defines a set as specifying a property of elements of the kind in or not in the set. The type, set ℕ, is thus equated with a predicate on ℕ, which we consider as defining the property of being of being a member of the set. Sets (at least) in Lean are identified with their membership predicates. As an example, the empty set of ℕ is defined by the predicate that is false for every ℕ. No natural number satisfies this predicate. The set it denotes is the set of values that satisfy it, which is the empty set. Study the following code with care and understand it. -/ #reduce (∅ : set ℕ) /- The predicate that defines the empty set is, as we've already discussed, false(n): i.e., the function of type ℕ → Prop that for any value, n : ℕ, returns the proposition false. No ℕ can satisfy this predicate by making it anything other than false. The set it designates is the empty set. -/ -- Display Notation /- Let's bind and empty set of ℕ to the identifier, e. We can also write the empty set using curly braces, or what we call set display notation. -/ def e: set ℕ := { } /- The symbol, ∅, is often used to represent the empty set (of values of whatever type). -/ def e': set ℕ := ∅ /- We can't write "e : set := {}"", because then Lean would not have enough context to infer the type of the set elements. -/ /- EXERCISE: What is the property of natural numbers that characterizes e, the empty set of natural numbers? Give you answer as a predicate: a function from ℕ to Prop. Give a λ abstraction as an answer. -/ /- EXERCISE: What predicate defines the set of all ℕ values? -/ -- Set Builder Notation /- We can also represent the empty set using set builder notation. Set builder notation is also called set comprehension notation. -/ /- Here we define the empty set of ℕ again -/ def e'' : set ℕ := { n | false } /- Now we define the entire set of even ℕ -/ def evs : set ℕ := { n | ∃ m, m + m = n } -- Singleton Sets /- Here's another set of ℕ, containing only the number, 1. We call such a set a singleton set. -/ def x: set nat := { 1 } /- EXERCISE: What property of natural numbers defines the property of being in this set? Try to come up with the answer before you look! -/ #reduce x /- The answer is a little surprising. The predicate λ n, n = 1, would do to define this set, but instead Lean uses λ n, n = 1 ∨ false. Lean could have, and in some cases will, leave off the (∨ false) at the end. See it is so in the following example code. -/ def x' := { n | n = 1 } #reduce x' /- The two different notations give rise to slightly different but equivalent predicates, and thus to the same sets. -/ -- SET MEMBERSHIP /- So what does set membership mean? By the notation 1 ∈ x we mean the proposition that "1 is in, or is a member of the set, x." This is simply the proposition obtained by applying the predicate, x, to the value, 1. x is the set and it is the predicate that defines the set. In Lean they are the same thing. The proposition 1 ∈ x is definitionally the same as (x 1). The predicate, i.e., the set, x, is defined as λ (n : ℕ), n = 1. Applying this predicate/function to 1 yields the proposition that: 1 = 1 ∨ false. This proposition, in turn, is easy to prove, and so, yes, indeed, 1 is in the set x. -/ /- Reducing 1 ∈ x reveals the proposition obtained by applying the x predicate to the value 1 to get a membership proposition for 1. -/ #reduce 1 ∈ x #reduce x 1 /- In this case, the membership proposition, 1 ∈ x, is true, as we prove next. -/ example : 1 ∈ x := -- 1 = 1 ∨ false begin /- It can be easier to work with proofs about sets if you use the change tactic to ask Lean to show you the predicate that the goal represents. You can use #reduce to see the proposition that the goal using set notation denotes. -/ change 1 = 1 ∨ false, -- the rest is straightforward apply or.intro_left, exact rfl, end /- Here we use some shorthand tactics to make it easier to write the proof. It's good to learn this shortcuts. They make quick work of some proof goals. -/ example : 1 ∈ x := -- 1 = 1 begin change 1 = 1 ∨ false, -- now or.intro_left, but with a shortcut left, -- and now exact rfl, but with a shortcut trivial, end -- MORE EXAMPLE /- Here's two sets with three elements each. -/ def y : set nat := { 1, 2, 3 } def z : set nat := { 2, 3, 4 } /- EXERCISE: What is a predicate that characterizes membership in the set, y? -/ #reduce y /- EXERCISE: Define the same set, y, with the name, y', using set builder notation. -/ def y' : set nat := { n | n = 1 ∨ n = 2 ∨ n = 3 } #reduce y /- With these basics in hand, we can define, understand, and work with the full range of set operations. Set operations are like operations with numbers but their operands and results are sets. -/ -- SET UNION /- The union of two sets, y and z, which we denote as y ∪ z, is the combined set of values from y and z. An element is either in or not in a given, but cannot be in a more than one time (otherwise you have what is called a multiset). The union of y and z as defined above is thus the set { 1, 2, 3, 4 }. -/ def u := y ∪ z /- EXERCISE: What predicate defines the set that is the union of y and z? -/ #reduce u /- Answer: It is the predicate that defines what it means to be in y or to be in z. That is, it is the disjunction of the predicates that define y and z, respectively. Union corresponds to "or." -/ /- Let's prove that 3 ∈ u. Let's start by reminding ourselves of the predicate that defines u and of the proposition represented by 3 ∈ u. -/ #reduce u /- The set, u, is defined as a predicate that takes a : ℕ and returns the proposition that that a is one of the values in the set, expressed as a somewhat long disjunction. Lean selects the variable name, a, for purposes of printing out the value of u. There is no special meaning to a; it is just an otherwise unbound name. -/ /- Now that we know that 3 ∈ u is just a proposition involving a bunch of disjunctions, it's easy to prove. -/ example : 3 ∈ u := begin /- Notice again that Lean leaves the goal written using set membership notation. Just bear in mind that the goal is just the disjunction, (3 = 3 ∨ 3 = 2 ∨ 3 = 1 ∨ false) ∨ 3 = 4 ∨ 3 = 3 ∨ 3 = 2 ∨ false. -/ left, left, trivial, end #reduce 3 ∈ y ∪ z /- Or, if you prefer, make the goal explicit as a disjunction. -/ example : 3 ∈ y ∪ z := begin change (3 = 3 ∨ 3 = 2 ∨ 3 = 1 ∨ false) ∨ 3 = 4 ∨ 3 = 3 ∨ 3 = 2 ∨ false, apply or.inl, apply or.inl, trivial, end -- SET INTERSECTION /- The intersection of two sets, y and z, which we denote as y ∩ z, is the set containing those values that are in y and that are in z. Intersection thus corresponds to the conjunction of the predicates defining the two individual sets. -/ def w := y ∩ z #reduce w example : 2 ∈ y ∩ z := -- (a = 3 ∨ a = 2 ∨ a = 1 ∨ false) ∧ (a = 4 ∨ a = 3 ∨ a = 2 ∨ false) begin apply and.intro, -- 2 ∈ y right, left, trivial, -- 2 ∈ z right, right, left, trivial, end -- SET DIFFERENCE /- The set difference y - z, also writen as y \ z, is the set of values that are in y but not in z. Think of the subtraction as saying that from y you take away z, and the result is what is left of y. EXERCISE: What predicate defines a set difference, y \ z? -/ #reduce y \ z example : 1 ∈ y \ z := begin -- apply and.intro, split, -- 1 ∈ y right, right, left, trivial, /- The goal looks funny, but think about what it means. It is the predicate, (λ (a : ℕ), a ∉ z), applied to the value, 1, which is to say it's the proposition, 1 ∉ z. That in turn is ¬ 1 ∈ z. And that, in turn, is just the proposition that 1 ∈ z → false. So assume 1 ∈ z and show false to prove it. What is 1 ∈ z? It's the proposition that 1 is one of the elements in the set, written as a disjunction, so use case analysis! -/ -- 1 ∉ z assume pf, cases pf, /- Now we need a proof that 1 ≠ 4. The dec_trivial tactic defined in the Lean's standard library "decides" many purely arithmetic propositions. That is, it generates either a proof that such a proposition is true if it's true. It will also generate a proof that its negation is true if that is the case. The dec_trivial tactic implements a "decision procedure" for sufficiently simple propositions involved numbers. Here we use it to give us a proof of 1 ≠ 4. We can then use that to get a proof of false and use false elim to eliminate the current case on grounds that it is based on contradictory assumptions (and thus can't happen). -/ have h : 1 ≠ 4 := dec_trivial, /- The contradiction tactic looks for a explicit contradiction in the context and if it finds one, applies false.elim to finish proving the goal. -/ contradiction, cases pf, have h : 1 ≠ 3 := dec_trivial, contradiction, cases pf, have h : 1 ≠ 2 := dec_trivial, contradiction, have f : false := pf, contradiction, end -- SUMMARY SO FAR /- The examples in this summary require you to recall that previously in this file we defined x, y, and z to be the ℕ sets, { 1 }, { 1, 2, 3 }, and { 2, 3, 4 }. -/ #print x #print y #print z /- A set can be, and in Lean is, characterized by a predicate: one that is true for each member of the set and false otherwise. It is a "membership predicate". Consider, for example, what it means for 1 or for 2 to be in the set, x. We write these propositions as 1 ∈ x and as 2 ∈ x respectively. -/ #reduce 1 ∈ x #reduce 2 ∈ x #reduce 3 ∈ z /- The union of two sets is given by the disjunction (or, ∨) of the respective membership predicates: (a ∈ y ∪ z) means (a ∈ y) ∨ (a ∈ z). -/ #reduce 1 ∈ (y ∪ z) #reduce (1 ∈ y) ∨ (1 ∈ z) /- The intersection of two sets is defined by the conjunction of the respective membership predicates: (x ∈ y ∩ z) = (x ∈ y ∧ a ∈ z) -/ #reduce (1 ∈ y ∩ z) /-The difference of two sets, y \ z, is defined by the conjunction of the first and the negation of the second membership predicates for the sets: (a ∈ y \ z) = ( a ∈ y) ∧ (¬ a ∈ z). -/ #reduce 1 ∈ y \ z -- PART II /- Now we introduce additional basic set theory concepts: these include notions of subsets, set equality, power sets, product sets, tuples, and a function that simulates an element insertion operator for sets. In all cases, we see that these set operations can be understood as operations on the predicates that define sets. The connection of set theory to predicate logic is thus made clear and explicit. -/ -- SUBSET /- Subset, denoted ⊆, is a binary relation on sets, denoted X ⊆ Y, where X and Y are sets. Viewed as a predicate on such sets, it is satisfied (made true by X and Y) iff every member of X is also a member of Y. Logically, X is a subset of Y if the property of being in X implies the property of being in Y. -/ #check x ⊆ y #reduce x ⊆ y /- Note that what is displayed when you hover over the reduce line includes "script" curly brace characters. These indicate a slight variant on implicit arguments that we needn't get in any detail right now. Just think of them as indicating implicit arguments. -/ /- So, { 1, 2 } ⊆ { 1, 2, 3 }, for example, but is is not the case that { 1, 2 } ⊆ { 1, 3, 4}. In the first case, every element of the set, { 1, 2 }, is also in the set { 1, 2, 3 }, so { 1, 2 } is a subset of { 1, 2, 3 }; but that is not the case for { 1, 2 } and { 1, 3, 4 }. -/ /- EXERCISE: List all of the subsets of each of the following sets of ℕ. * ∅ * { 1 } * { 1, 2 } * { 1, 2, 3 } EXERCISE: How many subsets are there of a set containing n elements. Does your formula work even for the empty set? -/ /- We can now see that the subset relation on sets has a precise logical meaning. x ⊆ y means ∀ a, a ∈ x → a ∈ y. -/ #check x ⊆ y #reduce x ⊆ y /- A quick note on a pattern that appears often in predicate logic: Let's look at the definition of the subset relation again, for sets of ℕ values, x and y. Here is what it means for y ⊆ x. ∀ (a : ℕ), a ∈ y → a ∈ z. Let's translate this to logicky English. For any natural number, a, if a is in y then e is in z. That is what is means for y to be a subset of z. What's interesting in this formulation is the combination of a ∀, which picks out *all* elements of the ℕ type, followed by a conditional (implication), where the premise imposes a further constraint on the elements being considered. It need only be true that every ℕ that is *also* and element of y be a member of z for y to be a subset of z. This is a common pattern in logic. The general form is ∀ x : T, P x → Q x. It is read as saying that for any x *with property P*, some other property, Q, must hold. In effect it quantifies over the values of type T with property P, and then makes a statement about those values, in particular: here they they also have property Q. -/ /- Okay, so let assert and prove a proposition involving the subset relation. We'll show that x ⊆ y, i.e., { 1 } ⊆ { 1, 2, 3 }. To do it we have to proving that if a ∈ x then a ∈ y. Now remember what x and a ∈ x are. First, x is understood to be a set, but it is specifically a membership predicate, of type ℕ → Prop, and a ∈ x is a proposition, namely the one obtained by applying the membership predicate to a: (x a). If (x a), i.e., a ∈ x, is true, i.e., provable, then a is said to be a member of the set, x. -/ /- Let's have another look at what the proposition, x ⊆ y, means: for any a, if a ∈ x then a ∈ y. -/ #reduce x ⊆ y /- So let's prove it's true. -/ example : x ⊆ y := begin /- It's sometimes helpful to change from set notation to the equivalent propositional notation. The change tactic will do this for you, as long as what you're changing the goal is is "definitionally equal" to the current goal. You cand find out what the exact proposition is using reduce, as we did above. -/ change ∀ ⦃a : ℕ⦄, a = 1 ∨ false → a = 3 ∨ a = 2 ∨ a = 1 ∨ false, /- The rest is just an everyday proof. Note that we can quickly zero in on the disjunct we need using a series of left and right tactics. (You do need to remember that ∨ is right associative, so left gives you the left disjunct and right gives you everything else to the right of the leftmost disjunct. -/ assume a, intro h, cases h, -- case a = 1 right, right, left, assumption, -- case false contradiction, end section sets /- We temporarily assume, within this section, that T is an arbitrary type, x is an arbitrary value of type T, and that A, B, and C are arbitrary sets of T-type elements. -/ variable T : Type variable x : T variables A B C : set T /- We can confirm our understanding of the subset relation using this notation. Now A and B are sets, and in Lean that means that these sets are represented by their membership predicates. They are membership predicates. -/ #reduce A ⊆ B /- EXERCISE: Explain precisely what the message produced by #reduce is saying. What is another way that Lean could have written A a or B a? -/ -- SET EQUALITY (and extensionality) /- The "principle of extensionality" for sets stipulates that if one can show that ∀ e, (e ∈ A ↔ e ∈ B) → (A = B). -/ #check ext /-When faced with a goal of proving that two sets, A and B are equal, i.e., that A = B, one can apply this principle to reduce the goal to that of showing that ∀ e, e ∈ A ↔ e ∈ B. -/ -- set equality example : A = B := begin apply ext, intro x, apply iff.intro, intro, /- We can proceed no further here, as we have nothing to use to prove that A actually does equal B in this case. A and B are just arbitary sets, so not equal, in general. What the example is meant to show is how to use ext and how to proceed. As for this proof, we will just abandon it as not possible to prove. -/ end /- Let's prove that { 1, 2 } = { 2, 1 }. -/ def p : set ℕ := { 1, 2 } def q : set ℕ := { 2, 1 } #reduce 1 ∈ p theorem oo : p = q := begin apply ext, intro x, apply iff.intro, -- forward direction intro, -- remember that a is a disjunction cases a with first rest, /- We introduce a new tactic: rewrite, written as rw h or rw ←h. When applied to a proof, h : x = y or h : x ↔ y, of an equality or a bi-implication, it rewrites any occurrences of the left side, x, in the goal, with the right side, y. If you want to rewrite by replacing occurrences of the right side, y, with the left, x, use rw ←h. -/ rw first, right, left, apply rfl, cases rest, rw rest, apply or.inl, apply rfl, -- rest is now ((λ n, false) x) = false! apply false.elim rest, -- backward direction intro, cases a, rw a, right, left, apply rfl, cases a, rw a, left, apply rfl, apply false.elim a, end -- POWERSET /- The powerset of a set, A, is the set of all of the subsets of A. -/ #check A #check powerset A #check 𝒫 A #reduce 𝒫 A /- Note about implicit arguments. In the preceding definition we see {{ }} brackets, rendered using the characters, ⦃ ⦄. This states that the argument is to be inferred from context (is implicit) but is expected only when it appears before another implicit argument. This notation tells Lean not to "eagerly" consume the argument, as soon as it can, but to wait to consume it until it appears, implicitly, before another implicit argument in a list of arguments. This is a notational detail that it's not worth worry about at the moment. -/ /- There are two members we always know are in the powerset of A: the emptyset and A itself. Of course, if A is the emptyset, this is technically only one member, but the proofs are the same. -/ #check A #check 𝒫 A #reduce 𝒫 A /- We define the powerset of A, itself a set, as, λ (t : T → Prop), ∀ ⦃a : T⦄, t a → A a. Let's analyze this. First, we note that it is a predicate, as we would expect, given that we use predicates to define sets. In particular, this a predicate on values of type, T → Prop, which is to say, this is a predicate on predicates that define sets! It's a predicate that's true whenever its argument, a set defined by a predicate, is a subst of A, which is to say that it's true when any element in the argument (set) is also in A. When applied to a set, t, this predicate is satisfied (true) if and only if every a in t is also in A: formally, ∀ ⦃a : T⦄, t a → A a. -/ #reduce ∅ ∈ 𝒫 A /- Lean is helping us here. We need to show that if a ∈ ∅ then a ∈ a to show that ∅ is a subset of A. But a ∈ ∅ is literally false. To see it, work through the application of the predicate for ∅ to any value, a. Lean is simplifying a ∈ ∅ to false. -/ example: ∅ ∈ 𝒫 A := /- To show that the set, ∅, is in the set 𝒫 A, we have to show that ∅ is a subset of A. To do that, we have to show that any t that is in ∅ is also in A. -/ begin -- change goal to logical form change ∀ ⦃a : T⦄, false → A a, -- use forall introduction intro t, -- now it's a trivial proof assume t_in_emptyset, contradiction, end #reduce A ∈ 𝒫 A /- To prove this, we need to prove that A is subset of A, which is to say any a in A is also in A. It's as simple as that and the proof is of course trivial. -/ example: A ∈ 𝒫 A := begin change ∀ ⦃a : T⦄, A a → A a, assume t, assume t_in_A, assumption end /- Slightly more interesting cases are also easy to prove. There's nothing involved here beyond what you already understand. -/ #reduce ({1, 3}: set ℕ) ∈ 𝒫 ({1, 2, 3}: set ℕ) /- One again to prove that {1, 3} is in the power set of {1, 2, 3} it suffices to show that every element of {1, 3} is in {1, 2, 3}, because that is what it means to be a subset. The proof is straightforward. -/ example: ({1, 3}: set ℕ) ∈ 𝒫 ({1, 2, 3}: set ℕ) := begin change ∀ ⦃a : ℕ⦄, a = 3 ∨ a = 1 ∨ false → a = 3 ∨ a = 2 ∨ a = 1 ∨ false, -- forall introduction intro t, -- assume premise of implication to be proved assume pf_t_in_1_3, -- use or elimination on proof of premise cases pf_t_in_1_3 with pf_t_is_3 pf_t_in_1 , -- show 3 from {1, 3} is in {1, 2, 3} exact or.inl pf_t_is_3, -- show 1 from {1, 3} is in {1, 2, 3} right, -- an ever so slightly clever or intro exact or.inr pf_t_in_1, end -- a more involved example; study this one -- {{1, 2}, {1, 3}, {2, 3}} ⊆ 𝒫 {1, 2, 3} #reduce ({{1, 2}, {1, 3}, {2, 3}}: set (set nat)) ⊆ 𝒫 ({1, 2, 3}) example : ({{1, 2}, {1, 3}, {2, 3}}) ⊆ 𝒫 ({1, 2, 3} : set nat) := begin change ∀ ⦃a : ℕ → Prop⦄, (a = λ (b : ℕ), b = 3 ∨ b = 2 ∨ false) ∨ (a = λ (b : ℕ), b = 3 ∨ b = 1 ∨ false) ∨ (a = λ (b : ℕ), b = 2 ∨ b = 1 ∨ false) ∨ false → ∀ ⦃a_1 : ℕ⦄, a a_1 → a_1 = 3 ∨ a_1 = 2 ∨ a_1 = 1 ∨ false, intro s, assume pf_s_in_subset, cases pf_s_in_subset with pf_s_is_2_3, assume t, assume pf_t_in_s, cases pf_s_is_2_3 with pf_s_is_3, cases pf_t_in_s with pf_t_is_3 pf_t_in_2, exact or.inl pf_t_is_3, apply or.inr, cases pf_t_in_2 with pf_t_in_2 pf_t_in_emptyset, exact or.inl pf_t_in_2, exact false.elim pf_t_in_emptyset, cases pf_s_in_subset with pf_s_is_1_3, assume t, assume pf_t_in_s, cases pf_s_is_1_3 with pf_s_is_3, cases pf_t_in_s with pf_t_is_3 pf_t_in_1, exact or.inl pf_t_is_3, apply or.inr, apply or.inr, assumption, cases pf_s_in_subset with pf_s_is_1_2 pf_s_in_emptyset, assume t, assume pf_t_in_s, cases pf_s_is_1_2 with pf_s_is_2, cases pf_t_in_s with pf_t_is_2 pf_t_in_1, apply or.inr, exact or.inl pf_t_is_2, apply or.inr, apply or.inr, assumption, exact false.elim pf_s_in_emptyset, end -- Tuples /- If S and T are types, then the product type of S and T, written out as (prod S T) and in shorthand as S × T, has as its values all of 2-tuples, or ordered pairs, (s, t), where s : S, and t : T. -/ /- In the following code, we see that ℕ × ℕ is a type, and the 2-tuple, or ordered pair, (1, 2), is a value of this type. -/ #check ℕ × ℕ #check prod ℕ ℕ #check (1, 2) /- We can form product types from any two types. Note the type of this 2-tuple. -/ #check ("Hello Lean", 1) /- This ordered pair notation in Lean in shorthand for the appliation of the constructor, prod.mk, two two arguments. The constructor takes the type arguments implicitly. -/ #check prod.mk 1 2 -- long way to write (1, 2) example : prod.mk 1 2 = (1, 2) := rfl /- We can form 3- and larger tuples using nested 2-tuples. Note that × is right associative, as you can see by studying the type of this term. -/ #check ("Hello Lean", (10, (tt,1))) #check ((0,0),(0,0)) -- PRODUCT SET /- The Cartesian product set of two sets, A and B, denoted as A × B in everyday math, is the set of all ordered pairs, (a, b) (values of type prod A B), where a ∈ A and b ∈ B. In Lean, the set product of sets, A and B, is denoted as set.prod A B. There is no nice infix operator notation for set products at this time. Note carefully: there is a distinction here between product types and product sets. Product types are types, while product sets are sets. And sets are not types in Lean. Rather they're specified as properties. This is potentially confusing. It is made more confusing by the fact that Lean has a way to convert a set into a special type called a subset type: the type of elements in the set, along with proofs of membership. And if you apply prod to two sets, you'll get a subset type! -/ #check set.prod y z -- product set type #reduce set.prod y z -- product set property #check prod y z -- oops, a subset type #check y × z -- oops, same thing #reduce prod y z -- oops, not what we want /- A set product is just a set, which is to say it's defined by a predicate, s. Such a predicate is true for exactly the members of the set. That is, (s x) is a proposition that is true iff x ∈ s. The predicate that defines a product set is a predicate on ordered pairs. It's basically defined like this: -/ def mysetprod (S T : Type) (s : set S) (t : set T) : set (S × T) := { p : S × T | p.1 ∈ s ∧ p.2 ∈ t } /- What this says, then, is that the product set of s (a set of S-type values) and t (a set of T-type values) is the set of pairs, p, each of type (prod S T), and each thus an ordered pair, p = (p.1, p.2), where p.1 ∈ s and p.2 ∈ t. Lean provides this function as set.prod. -/ example : (1, 2) ∈ set.prod y z := begin change (λ (p : ℕ × ℕ), (p.fst = 3 ∨ p.fst = 2 ∨ p.fst = 1 ∨ false) ∧ (p.snd = 4 ∨ p.snd = 3 ∨ p.snd = 2 ∨ false)) (1,2), split, right,right,left,apply rfl, right,right,left,apply rfl, end -- COMPLEMENT /- The complement of a set is the set of all values of the set's type that are not in that set. The complement is specified by the "-" sign -/ #check -y #reduce -y #reduce 5 ∈ -y example: 5 ∈ -y := begin change 5 = 3 ∨ 5 = 2 ∨ 5 = 1 ∨ false → false, assume pf_5_in_y, cases pf_5_in_y with pf_5_eq_3 h, have pf_5_ne_3: 5 ≠ 3 := dec_trivial, contradiction, cases h with pf_5_eq_2 h, have pf_5_ne_2: 5 ≠ 2 := dec_trivial, contradiction, cases h with pf_5_eq_1 h, have pf_5_ne_1: 5 ≠ 1 := dec_trivial, contradiction, assumption end -- INSERTION /- We can define an operation that we can think of as "inserting" an element into a set: as a function that takes an element and a set and returns the set containing that element along with the elements of the original set. Unlike in Python or Java, there's no change to a data structure in this case. In pure functional languages, such as Lean, there is no concept of a memory or of "mutable" objects. Rather, everything is defined by functions, here one that takes a set and a value and constructs a new set value just like the old one but with the new element included as well. -/ def myInsert { T : Type } (a : T) (s : set T) : set T := {b | b = a ∨ b ∈ s} /- The predicate for the set resulting from "inserting 5 into { 1, 2, 3, 5 }" admits that 5 is also a member of the result set. -/ #reduce myInsert 5 { 1, 2, 3, 4 } -- The Lean math library defines "insert" #reduce insert 5 { 1, 2, 3, 4 } -- MORE EXAMPLES /- Several of these examples are adapted from Jeremy Avigad's book, Logic and Proof. Prof. Avigad (CMU) is one of the main contributors to the development of Lean, and he leads the development of its mathematical libraries, including the one you're now using for sets, in particular. -/ /- A is a subset of A ∪ B -/ example : ∀ T : Type, ∀ s t: set T, s ⊆ s ∪ t := begin assume T s t x, assume h : x ∈ s, show x ∈ s ∪ t, change s x ∨ t x, change s x at h, from or.inl h end /- The empty set, ∅, is a subset of any set. -/ example : ∀ T : Type, ∀ s: set T, ∅ ⊆ s := begin assume T s x, assume h : x ∈ (∅ : set T), have f: false := h, contradiction, end /- Subset is a transitive relation on sets -/ example : ∀ T : Type, ∀ A B C: set T, A ⊆ B → B ⊆ C → A ⊆ C := begin assume T s t u, assume st tu, intro, intro, have z := st a_1, exact (tu z), end /- If an object is in both sets A and B then it is in their intersection. -/ example : ∀ T : Type, forall A B : set T, ∀ x, x ∈ A → x ∈ B → x ∈ A ∩ B := begin assume T A B x, assume hA : x ∈ A, assume hB : x ∈ B, show x ∈ A ∧ x ∈ B, from and.intro hA hB, end /- If an object is in set A or is in set B then it is in their union. -/ example : ∀ T : Type, forall A B : set T, ∀ x, x ∈ A ∨ x ∈ B → x ∈ A ∪ B := begin assume T A B x, intro dis, show x ∈ A ∨ x ∈ B, by assumption, end /- A minus B is a subset of A -/ example : A \ B ⊆ A := begin assume x, assume mem : x ∈ A \ B, cases mem, from mem_left, end /- A minus B is contained in the complement of B -/ example : A \ B ⊆ -B := begin assume x, assume mem : x ∈ A \ B, change x ∈ A ∧ ¬ x ∈ B at mem, change x ∉ B, exact mem.right, end /- A \ B is equal to the intersection of A with the complement of B. -/ example : A \ B = A ∩ -B := begin apply ext, intro, split, intro h, exact h, intro h, exact h, end end sets
dd2dcf227dd09382f3a2e90bb18a6293d02c0755
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/measure_theory/function/ae_eq_of_integral.lean
27c711e645a7eea7a23b56d9c17cea53c2b1e80c
[ "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
29,373
lean
/- Copyright (c) 2021 Rémy Degenne. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Rémy Degenne -/ import analysis.inner_product_space.basic import analysis.normed_space.dual import measure_theory.function.strongly_measurable.lp import measure_theory.integral.set_integral /-! # From equality of integrals to equality of functions > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file provides various statements of the general form "if two functions have the same integral on all sets, then they are equal almost everywhere". The different lemmas use various hypotheses on the class of functions, on the target space or on the possible finiteness of the measure. ## Main statements All results listed below apply to two functions `f, g`, together with two main hypotheses, * `f` and `g` are integrable on all measurable sets with finite measure, * for all measurable sets `s` with finite measure, `∫ x in s, f x ∂μ = ∫ x in s, g x ∂μ`. The conclusion is then `f =ᵐ[μ] g`. The main lemmas are: * `ae_eq_of_forall_set_integral_eq_of_sigma_finite`: case of a sigma-finite measure. * `ae_fin_strongly_measurable.ae_eq_of_forall_set_integral_eq`: for functions which are `ae_fin_strongly_measurable`. * `Lp.ae_eq_of_forall_set_integral_eq`: for elements of `Lp`, for `0 < p < ∞`. * `integrable.ae_eq_of_forall_set_integral_eq`: for integrable functions. For each of these results, we also provide a lemma about the equality of one function and 0. For example, `Lp.ae_eq_zero_of_forall_set_integral_eq_zero`. We also register the corresponding lemma for integrals of `ℝ≥0∞`-valued functions, in `ae_eq_of_forall_set_lintegral_eq_of_sigma_finite`. Generally useful lemmas which are not related to integrals: * `ae_eq_zero_of_forall_inner`: if for all constants `c`, `λ x, inner c (f x) =ᵐ[μ] 0` then `f =ᵐ[μ] 0`. * `ae_eq_zero_of_forall_dual`: if for all constants `c` in the dual space, `λ x, c (f x) =ᵐ[μ] 0` then `f =ᵐ[μ] 0`. -/ open measure_theory topological_space normed_space filter open_locale ennreal nnreal measure_theory namespace measure_theory section ae_eq_of_forall variables {α E 𝕜 : Type*} {m : measurable_space α} {μ : measure α} [is_R_or_C 𝕜] lemma ae_eq_zero_of_forall_inner [normed_add_comm_group E] [inner_product_space 𝕜 E] [second_countable_topology E] {f : α → E} (hf : ∀ c : E, (λ x, (inner c (f x) : 𝕜)) =ᵐ[μ] 0) : f =ᵐ[μ] 0 := begin let s := dense_seq E, have hs : dense_range s := dense_range_dense_seq E, have hf' : ∀ᵐ x ∂μ, ∀ n : ℕ, inner (s n) (f x) = (0 : 𝕜), from ae_all_iff.mpr (λ n, hf (s n)), refine hf'.mono (λ x hx, _), rw [pi.zero_apply, ← @inner_self_eq_zero 𝕜], have h_closed : is_closed {c : E | inner c (f x) = (0 : 𝕜)}, from is_closed_eq (continuous_id.inner continuous_const) continuous_const, exact @is_closed_property ℕ E _ s (λ c, inner c (f x) = (0 : 𝕜)) hs h_closed (λ n, hx n) _, end local notation `⟪`x`, `y`⟫` := y x variables (𝕜) lemma ae_eq_zero_of_forall_dual_of_is_separable [normed_add_comm_group E] [normed_space 𝕜 E] {t : set E} (ht : topological_space.is_separable t) {f : α → E} (hf : ∀ c : dual 𝕜 E, (λ x, ⟪f x, c⟫) =ᵐ[μ] 0) (h't : ∀ᵐ x ∂μ, f x ∈ t) : f =ᵐ[μ] 0 := begin rcases ht with ⟨d, d_count, hd⟩, haveI : encodable d := d_count.to_encodable, have : ∀ (x : d), ∃ g : E →L[𝕜] 𝕜, ‖g‖ ≤ 1 ∧ g x = ‖(x : E)‖ := λ x, exists_dual_vector'' 𝕜 x, choose s hs using this, have A : ∀ (a : E), a ∈ t → (∀ x, ⟪a, s x⟫ = (0 : 𝕜)) → a = 0, { assume a hat ha, contrapose! ha, have a_pos : 0 < ‖a‖, by simp only [ha, norm_pos_iff, ne.def, not_false_iff], have a_mem : a ∈ closure d := hd hat, obtain ⟨x, hx⟩ : ∃ (x : d), dist a x < ‖a‖ / 2, { rcases metric.mem_closure_iff.1 a_mem (‖a‖/2) (half_pos a_pos) with ⟨x, h'x, hx⟩, exact ⟨⟨x, h'x⟩, hx⟩ }, use x, have I : ‖a‖/2 < ‖(x : E)‖, { have : ‖a‖ ≤ ‖(x : E)‖ + ‖a - x‖ := norm_le_insert' _ _, have : ‖a - x‖ < ‖a‖/2, by rwa dist_eq_norm at hx, linarith }, assume h, apply lt_irrefl (‖s x x‖), calc ‖s x x‖ = ‖s x (x - a)‖ : by simp only [h, sub_zero, continuous_linear_map.map_sub] ... ≤ 1 * ‖(x : E) - a‖ : continuous_linear_map.le_of_op_norm_le _ (hs x).1 _ ... < ‖a‖ / 2 : by { rw [one_mul], rwa dist_eq_norm' at hx } ... < ‖(x : E)‖ : I ... = ‖s x x‖ : by rw [(hs x).2, is_R_or_C.norm_coe_norm] }, have hfs : ∀ (y : d), ∀ᵐ x ∂μ, ⟪f x, s y⟫ = (0 : 𝕜), from λ y, hf (s y), have hf' : ∀ᵐ x ∂μ, ∀ (y : d), ⟪f x, s y⟫ = (0 : 𝕜), by rwa ae_all_iff, filter_upwards [hf', h't] with x hx h'x, exact A (f x) h'x hx, end lemma ae_eq_zero_of_forall_dual [normed_add_comm_group E] [normed_space 𝕜 E] [second_countable_topology E] {f : α → E} (hf : ∀ c : dual 𝕜 E, (λ x, ⟪f x, c⟫) =ᵐ[μ] 0) : f =ᵐ[μ] 0 := ae_eq_zero_of_forall_dual_of_is_separable 𝕜 (is_separable_of_separable_space (set.univ : set E)) hf (eventually_of_forall (λ x, set.mem_univ _)) variables {𝕜} end ae_eq_of_forall variables {α E : Type*} {m m0 : measurable_space α} {μ : measure α} {s t : set α} [normed_add_comm_group E] [normed_space ℝ E] [complete_space E] {p : ℝ≥0∞} section ae_eq_of_forall_set_integral_eq lemma ae_const_le_iff_forall_lt_measure_zero {β} [linear_order β] [topological_space β] [order_topology β] [first_countable_topology β] (f : α → β) (c : β) : (∀ᵐ x ∂μ, c ≤ f x) ↔ ∀ b < c, μ {x | f x ≤ b} = 0 := begin rw ae_iff, push_neg, split, { assume h b hb, exact measure_mono_null (λ y hy, (lt_of_le_of_lt hy hb : _)) h }, assume hc, by_cases h : ∀ b, c ≤ b, { have : {a : α | f a < c} = ∅, { apply set.eq_empty_iff_forall_not_mem.2 (λ x hx, _), exact (lt_irrefl _ (lt_of_lt_of_le hx (h (f x)))).elim }, simp [this] }, by_cases H : ¬ (is_lub (set.Iio c) c), { have : c ∈ upper_bounds (set.Iio c) := λ y hy, le_of_lt hy, obtain ⟨b, b_up, bc⟩ : ∃ (b : β), b ∈ upper_bounds (set.Iio c) ∧ b < c, by simpa [is_lub, is_least, this, lower_bounds] using H, exact measure_mono_null (λ x hx, b_up hx) (hc b bc) }, push_neg at H h, obtain ⟨u, u_mono, u_lt, u_lim, -⟩ : ∃ (u : ℕ → β), strict_mono u ∧ (∀ (n : ℕ), u n < c) ∧ tendsto u at_top (nhds c) ∧ ∀ (n : ℕ), u n ∈ set.Iio c := H.exists_seq_strict_mono_tendsto_of_not_mem (lt_irrefl c) h, have h_Union : {x | f x < c} = ⋃ (n : ℕ), {x | f x ≤ u n}, { ext1 x, simp_rw [set.mem_Union, set.mem_set_of_eq], split; intro h, { obtain ⟨n, hn⟩ := ((tendsto_order.1 u_lim).1 _ h).exists, exact ⟨n, hn.le⟩ }, { obtain ⟨n, hn⟩ := h, exact hn.trans_lt (u_lt _), }, }, rw [h_Union, measure_Union_null_iff], assume n, exact hc _ (u_lt n), end section ennreal open_locale topology lemma ae_le_of_forall_set_lintegral_le_of_sigma_finite [sigma_finite μ] {f g : α → ℝ≥0∞} (hf : measurable f) (hg : measurable g) (h : ∀ s, measurable_set s → μ s < ∞ → ∫⁻ x in s, f x ∂μ ≤ ∫⁻ x in s, g x ∂μ) : f ≤ᵐ[μ] g := begin have A : ∀ (ε N : ℝ≥0) (p : ℕ), 0 < ε → μ ({x | g x + ε ≤ f x ∧ g x ≤ N} ∩ spanning_sets μ p) = 0, { assume ε N p εpos, let s := {x | g x + ε ≤ f x ∧ g x ≤ N} ∩ spanning_sets μ p, have s_meas : measurable_set s, { have A : measurable_set {x | g x + ε ≤ f x} := measurable_set_le (hg.add measurable_const) hf, have B : measurable_set {x | g x ≤ N} := measurable_set_le hg measurable_const, exact (A.inter B).inter (measurable_spanning_sets μ p) }, have s_lt_top : μ s < ∞ := (measure_mono (set.inter_subset_right _ _)).trans_lt (measure_spanning_sets_lt_top μ p), have A : ∫⁻ x in s, g x ∂μ + ε * μ s ≤ ∫⁻ x in s, g x ∂μ + 0 := calc ∫⁻ x in s, g x ∂μ + ε * μ s = ∫⁻ x in s, g x ∂μ + ∫⁻ x in s, ε ∂μ : by simp only [lintegral_const, set.univ_inter, measurable_set.univ, measure.restrict_apply] ... = ∫⁻ x in s, (g x + ε) ∂μ : (lintegral_add_right _ measurable_const).symm ... ≤ ∫⁻ x in s, f x ∂μ : set_lintegral_mono (hg.add measurable_const) hf (λ x hx, hx.1.1) ... ≤ ∫⁻ x in s, g x ∂μ + 0 : by { rw [add_zero], exact h s s_meas s_lt_top }, have B : ∫⁻ x in s, g x ∂μ ≠ ∞, { apply ne_of_lt, calc ∫⁻ x in s, g x ∂μ ≤ ∫⁻ x in s, N ∂μ : set_lintegral_mono hg measurable_const (λ x hx, hx.1.2) ... = N * μ s : by simp only [lintegral_const, set.univ_inter, measurable_set.univ, measure.restrict_apply] ... < ∞ : by simp only [lt_top_iff_ne_top, s_lt_top.ne, and_false, ennreal.coe_ne_top, with_top.mul_eq_top_iff, ne.def, not_false_iff, false_and, or_self] }, have : (ε : ℝ≥0∞) * μ s ≤ 0 := ennreal.le_of_add_le_add_left B A, simpa only [ennreal.coe_eq_zero, nonpos_iff_eq_zero, mul_eq_zero, εpos.ne', false_or] }, obtain ⟨u, u_mono, u_pos, u_lim⟩ : ∃ (u : ℕ → ℝ≥0), strict_anti u ∧ (∀ n, 0 < u n) ∧ tendsto u at_top (nhds 0) := exists_seq_strict_anti_tendsto (0 : ℝ≥0), let s := λ (n : ℕ), {x | g x + u n ≤ f x ∧ g x ≤ (n : ℝ≥0)} ∩ spanning_sets μ n, have μs : ∀ n, μ (s n) = 0 := λ n, A _ _ _ (u_pos n), have B : {x | f x ≤ g x}ᶜ ⊆ ⋃ n, s n, { assume x hx, simp at hx, have L1 : ∀ᶠ n in at_top, g x + u n ≤ f x, { have : tendsto (λ n, g x + u n) at_top (𝓝 (g x + (0 : ℝ≥0))) := tendsto_const_nhds.add (ennreal.tendsto_coe.2 u_lim), simp at this, exact eventually_le_of_tendsto_lt hx this }, have L2 : ∀ᶠ (n : ℕ) in (at_top : filter ℕ), g x ≤ (n : ℝ≥0), { have : tendsto (λ (n : ℕ), ((n : ℝ≥0) : ℝ≥0∞)) at_top (𝓝 ∞), { simp only [ennreal.coe_nat], exact ennreal.tendsto_nat_nhds_top }, exact eventually_ge_of_tendsto_gt (hx.trans_le le_top) this }, apply set.mem_Union.2, exact ((L1.and L2).and (eventually_mem_spanning_sets μ x)).exists }, refine le_antisymm _ bot_le, calc μ {x : α | (λ (x : α), f x ≤ g x) x}ᶜ ≤ μ (⋃ n, s n) : measure_mono B ... ≤ ∑' n, μ (s n) : measure_Union_le _ ... = 0 : by simp only [μs, tsum_zero] end lemma ae_eq_of_forall_set_lintegral_eq_of_sigma_finite [sigma_finite μ] {f g : α → ℝ≥0∞} (hf : measurable f) (hg : measurable g) (h : ∀ s, measurable_set s → μ s < ∞ → ∫⁻ x in s, f x ∂μ = ∫⁻ x in s, g x ∂μ) : f =ᵐ[μ] g := begin have A : f ≤ᵐ[μ] g := ae_le_of_forall_set_lintegral_le_of_sigma_finite hf hg (λ s hs h's, le_of_eq (h s hs h's)), have B : g ≤ᵐ[μ] f := ae_le_of_forall_set_lintegral_le_of_sigma_finite hg hf (λ s hs h's, ge_of_eq (h s hs h's)), filter_upwards [A, B] with x using le_antisymm, end end ennreal section real variables {f : α → ℝ} /-- Don't use this lemma. Use `ae_nonneg_of_forall_set_integral_nonneg`. -/ lemma ae_nonneg_of_forall_set_integral_nonneg_of_strongly_measurable (hfm : strongly_measurable f) (hf : integrable f μ) (hf_zero : ∀ s, measurable_set s → μ s < ∞ → 0 ≤ ∫ x in s, f x ∂μ) : 0 ≤ᵐ[μ] f := begin simp_rw [eventually_le, pi.zero_apply], rw ae_const_le_iff_forall_lt_measure_zero, intros b hb_neg, let s := {x | f x ≤ b}, have hs : measurable_set s, from hfm.measurable_set_le strongly_measurable_const, have mus : μ s < ∞, { let c : ℝ≥0 := ⟨|b|, abs_nonneg _⟩, have c_pos : (c : ℝ≥0∞) ≠ 0, by simpa using hb_neg.ne, calc μ s ≤ μ {x | (c : ℝ≥0∞) ≤ ‖f x‖₊} : begin apply measure_mono, assume x hx, simp only [set.mem_set_of_eq] at hx, simpa only [nnnorm, abs_of_neg hb_neg, abs_of_neg (hx.trans_lt hb_neg), real.norm_eq_abs, subtype.mk_le_mk, neg_le_neg_iff, set.mem_set_of_eq, ennreal.coe_le_coe] using hx, end ... ≤ (∫⁻ x, ‖f x‖₊ ∂μ) / c : meas_ge_le_lintegral_div hfm.ae_measurable.ennnorm c_pos ennreal.coe_ne_top ... < ∞ : ennreal.div_lt_top (ne_of_lt hf.2) c_pos }, have h_int_gt : ∫ x in s, f x ∂μ ≤ b * (μ s).to_real, { have h_const_le : ∫ x in s, f x ∂μ ≤ ∫ x in s, b ∂μ, { refine set_integral_mono_ae_restrict hf.integrable_on (integrable_on_const.mpr (or.inr mus)) _, rw [eventually_le, ae_restrict_iff hs], exact eventually_of_forall (λ x hxs, hxs), }, rwa [set_integral_const, smul_eq_mul, mul_comm] at h_const_le, }, by_contra, refine (lt_self_iff_false (∫ x in s, f x ∂μ)).mp (h_int_gt.trans_lt _), refine (mul_neg_iff.mpr (or.inr ⟨hb_neg, _⟩)).trans_le _, swap, { simp_rw measure.restrict_restrict hs, exact hf_zero s hs mus, }, refine (ennreal.to_real_nonneg).lt_of_ne (λ h_eq, h _), cases (ennreal.to_real_eq_zero_iff _).mp h_eq.symm with hμs_eq_zero hμs_eq_top, { exact hμs_eq_zero, }, { exact absurd hμs_eq_top mus.ne, }, end lemma ae_nonneg_of_forall_set_integral_nonneg (hf : integrable f μ) (hf_zero : ∀ s, measurable_set s → μ s < ∞ → 0 ≤ ∫ x in s, f x ∂μ) : 0 ≤ᵐ[μ] f := begin rcases hf.1 with ⟨f', hf'_meas, hf_ae⟩, have hf'_integrable : integrable f' μ, from integrable.congr hf hf_ae, have hf'_zero : ∀ s, measurable_set s → μ s < ∞ → 0 ≤ ∫ x in s, f' x ∂μ, { intros s hs h's, rw set_integral_congr_ae hs (hf_ae.mono (λ x hx hxs, hx.symm)), exact hf_zero s hs h's, }, exact (ae_nonneg_of_forall_set_integral_nonneg_of_strongly_measurable hf'_meas hf'_integrable hf'_zero).trans hf_ae.symm.le, end lemma ae_le_of_forall_set_integral_le {f g : α → ℝ} (hf : integrable f μ) (hg : integrable g μ) (hf_le : ∀ s, measurable_set s → μ s < ∞ → ∫ x in s, f x ∂μ ≤ ∫ x in s, g x ∂μ) : f ≤ᵐ[μ] g := begin rw ← eventually_sub_nonneg, refine ae_nonneg_of_forall_set_integral_nonneg (hg.sub hf) (λ s hs, _), rw [integral_sub' hg.integrable_on hf.integrable_on, sub_nonneg], exact hf_le s hs end lemma ae_nonneg_restrict_of_forall_set_integral_nonneg_inter {f : α → ℝ} {t : set α} (hf : integrable_on f t μ) (hf_zero : ∀ s, measurable_set s → μ (s ∩ t) < ∞ → 0 ≤ ∫ x in (s ∩ t), f x ∂μ) : 0 ≤ᵐ[μ.restrict t] f := begin refine ae_nonneg_of_forall_set_integral_nonneg hf (λ s hs h's, _), simp_rw measure.restrict_restrict hs, apply hf_zero s hs, rwa measure.restrict_apply hs at h's, end lemma ae_nonneg_of_forall_set_integral_nonneg_of_sigma_finite [sigma_finite μ] {f : α → ℝ} (hf_int_finite : ∀ s, measurable_set s → μ s < ∞ → integrable_on f s μ) (hf_zero : ∀ s, measurable_set s → μ s < ∞ → 0 ≤ ∫ x in s, f x ∂μ) : 0 ≤ᵐ[μ] f := begin apply ae_of_forall_measure_lt_top_ae_restrict, assume t t_meas t_lt_top, apply ae_nonneg_restrict_of_forall_set_integral_nonneg_inter (hf_int_finite t t_meas t_lt_top), assume s s_meas hs, exact hf_zero _ (s_meas.inter t_meas) (lt_of_le_of_lt (measure_mono (set.inter_subset_right _ _)) t_lt_top) end lemma ae_fin_strongly_measurable.ae_nonneg_of_forall_set_integral_nonneg {f : α → ℝ} (hf : ae_fin_strongly_measurable f μ) (hf_int_finite : ∀ s, measurable_set s → μ s < ∞ → integrable_on f s μ) (hf_zero : ∀ s, measurable_set s → μ s < ∞ → 0 ≤ ∫ x in s, f x ∂μ) : 0 ≤ᵐ[μ] f := begin let t := hf.sigma_finite_set, suffices : 0 ≤ᵐ[μ.restrict t] f, from ae_of_ae_restrict_of_ae_restrict_compl _ this hf.ae_eq_zero_compl.symm.le, haveI : sigma_finite (μ.restrict t) := hf.sigma_finite_restrict, refine ae_nonneg_of_forall_set_integral_nonneg_of_sigma_finite (λ s hs hμts, _) (λ s hs hμts, _), { rw [integrable_on, measure.restrict_restrict hs], rw measure.restrict_apply hs at hμts, exact hf_int_finite (s ∩ t) (hs.inter hf.measurable_set) hμts, }, { rw measure.restrict_restrict hs, rw measure.restrict_apply hs at hμts, exact hf_zero (s ∩ t) (hs.inter hf.measurable_set) hμts, }, end lemma ae_nonneg_restrict_of_forall_set_integral_nonneg {f : α → ℝ} (hf_int_finite : ∀ s, measurable_set s → μ s < ∞ → integrable_on f s μ) (hf_zero : ∀ s, measurable_set s → μ s < ∞ → 0 ≤ ∫ x in s, f x ∂μ) {t : set α} (ht : measurable_set t) (hμt : μ t ≠ ∞) : 0 ≤ᵐ[μ.restrict t] f := begin refine ae_nonneg_restrict_of_forall_set_integral_nonneg_inter (hf_int_finite t ht (lt_top_iff_ne_top.mpr hμt)) (λ s hs h's, _), refine (hf_zero (s ∩ t) (hs.inter ht) _), exact (measure_mono (set.inter_subset_right s t)).trans_lt (lt_top_iff_ne_top.mpr hμt), end lemma ae_eq_zero_restrict_of_forall_set_integral_eq_zero_real {f : α → ℝ} (hf_int_finite : ∀ s, measurable_set s → μ s < ∞ → integrable_on f s μ) (hf_zero : ∀ s, measurable_set s → μ s < ∞ → ∫ x in s, f x ∂μ = 0) {t : set α} (ht : measurable_set t) (hμt : μ t ≠ ∞) : f =ᵐ[μ.restrict t] 0 := begin suffices h_and : f ≤ᵐ[μ.restrict t] 0 ∧ 0 ≤ᵐ[μ.restrict t] f, from h_and.1.mp (h_and.2.mono (λ x hx1 hx2, le_antisymm hx2 hx1)), refine ⟨_, ae_nonneg_restrict_of_forall_set_integral_nonneg hf_int_finite (λ s hs hμs, (hf_zero s hs hμs).symm.le) ht hμt⟩, suffices h_neg : 0 ≤ᵐ[μ.restrict t] -f, { refine h_neg.mono (λ x hx, _), rw pi.neg_apply at hx, simpa using hx, }, refine ae_nonneg_restrict_of_forall_set_integral_nonneg (λ s hs hμs, (hf_int_finite s hs hμs).neg) (λ s hs hμs, _) ht hμt, simp_rw pi.neg_apply, rw [integral_neg, neg_nonneg], exact (hf_zero s hs hμs).le, end end real lemma ae_eq_zero_restrict_of_forall_set_integral_eq_zero {f : α → E} (hf_int_finite : ∀ s, measurable_set s → μ s < ∞ → integrable_on f s μ) (hf_zero : ∀ s : set α, measurable_set s → μ s < ∞ → ∫ x in s, f x ∂μ = 0) {t : set α} (ht : measurable_set t) (hμt : μ t ≠ ∞) : f =ᵐ[μ.restrict t] 0 := begin rcases (hf_int_finite t ht hμt.lt_top).ae_strongly_measurable.is_separable_ae_range with ⟨u, u_sep, hu⟩, refine ae_eq_zero_of_forall_dual_of_is_separable ℝ u_sep (λ c, _) hu, refine ae_eq_zero_restrict_of_forall_set_integral_eq_zero_real _ _ ht hμt, { assume s hs hμs, exact continuous_linear_map.integrable_comp c (hf_int_finite s hs hμs) }, { assume s hs hμs, rw [continuous_linear_map.integral_comp_comm c (hf_int_finite s hs hμs), hf_zero s hs hμs], exact continuous_linear_map.map_zero _ } end lemma ae_eq_restrict_of_forall_set_integral_eq {f g : α → E} (hf_int_finite : ∀ s, measurable_set s → μ s < ∞ → integrable_on f s μ) (hg_int_finite : ∀ s, measurable_set s → μ s < ∞ → integrable_on g s μ) (hfg_zero : ∀ s : set α, measurable_set s → μ s < ∞ → ∫ x in s, f x ∂μ = ∫ x in s, g x ∂μ) {t : set α} (ht : measurable_set t) (hμt : μ t ≠ ∞) : f =ᵐ[μ.restrict t] g := begin rw ← sub_ae_eq_zero, have hfg' : ∀ s : set α, measurable_set s → μ s < ∞ → ∫ x in s, (f - g) x ∂μ = 0, { intros s hs hμs, rw integral_sub' (hf_int_finite s hs hμs) (hg_int_finite s hs hμs), exact sub_eq_zero.mpr (hfg_zero s hs hμs), }, have hfg_int : ∀ s, measurable_set s → μ s < ∞ → integrable_on (f-g) s μ, from λ s hs hμs, (hf_int_finite s hs hμs).sub (hg_int_finite s hs hμs), exact ae_eq_zero_restrict_of_forall_set_integral_eq_zero hfg_int hfg' ht hμt, end lemma ae_eq_zero_of_forall_set_integral_eq_of_sigma_finite [sigma_finite μ] {f : α → E} (hf_int_finite : ∀ s, measurable_set s → μ s < ∞ → integrable_on f s μ) (hf_zero : ∀ s : set α, measurable_set s → μ s < ∞ → ∫ x in s, f x ∂μ = 0) : f =ᵐ[μ] 0 := begin let S := spanning_sets μ, rw [← @measure.restrict_univ _ _ μ, ← Union_spanning_sets μ, eventually_eq, ae_iff, measure.restrict_apply' (measurable_set.Union (measurable_spanning_sets μ))], rw [set.inter_Union, measure_Union_null_iff], intro n, have h_meas_n : measurable_set (S n), from (measurable_spanning_sets μ n), have hμn : μ (S n) < ∞, from measure_spanning_sets_lt_top μ n, rw ← measure.restrict_apply' h_meas_n, exact ae_eq_zero_restrict_of_forall_set_integral_eq_zero hf_int_finite hf_zero h_meas_n hμn.ne, end lemma ae_eq_of_forall_set_integral_eq_of_sigma_finite [sigma_finite μ] {f g : α → E} (hf_int_finite : ∀ s, measurable_set s → μ s < ∞ → integrable_on f s μ) (hg_int_finite : ∀ s, measurable_set s → μ s < ∞ → integrable_on g s μ) (hfg_eq : ∀ s : set α, measurable_set s → μ s < ∞ → ∫ x in s, f x ∂μ = ∫ x in s, g x ∂μ) : f =ᵐ[μ] g := begin rw ← sub_ae_eq_zero, have hfg : ∀ s : set α, measurable_set s → μ s < ∞ → ∫ x in s, (f - g) x ∂μ = 0, { intros s hs hμs, rw [integral_sub' (hf_int_finite s hs hμs) (hg_int_finite s hs hμs), sub_eq_zero.mpr (hfg_eq s hs hμs)], }, have hfg_int : ∀ s, measurable_set s → μ s < ∞ → integrable_on (f-g) s μ, from λ s hs hμs, (hf_int_finite s hs hμs).sub (hg_int_finite s hs hμs), exact ae_eq_zero_of_forall_set_integral_eq_of_sigma_finite hfg_int hfg, end lemma ae_fin_strongly_measurable.ae_eq_zero_of_forall_set_integral_eq_zero {f : α → E} (hf_int_finite : ∀ s, measurable_set s → μ s < ∞ → integrable_on f s μ) (hf_zero : ∀ s : set α, measurable_set s → μ s < ∞ → ∫ x in s, f x ∂μ = 0) (hf : ae_fin_strongly_measurable f μ) : f =ᵐ[μ] 0 := begin let t := hf.sigma_finite_set, suffices : f =ᵐ[μ.restrict t] 0, from ae_of_ae_restrict_of_ae_restrict_compl _ this hf.ae_eq_zero_compl, haveI : sigma_finite (μ.restrict t) := hf.sigma_finite_restrict, refine ae_eq_zero_of_forall_set_integral_eq_of_sigma_finite _ _, { intros s hs hμs, rw [integrable_on, measure.restrict_restrict hs], rw [measure.restrict_apply hs] at hμs, exact hf_int_finite _ (hs.inter hf.measurable_set) hμs, }, { intros s hs hμs, rw [measure.restrict_restrict hs], rw [measure.restrict_apply hs] at hμs, exact hf_zero _ (hs.inter hf.measurable_set) hμs, }, end lemma ae_fin_strongly_measurable.ae_eq_of_forall_set_integral_eq {f g : α → E} (hf_int_finite : ∀ s, measurable_set s → μ s < ∞ → integrable_on f s μ) (hg_int_finite : ∀ s, measurable_set s → μ s < ∞ → integrable_on g s μ) (hfg_eq : ∀ s : set α, measurable_set s → μ s < ∞ → ∫ x in s, f x ∂μ = ∫ x in s, g x ∂μ) (hf : ae_fin_strongly_measurable f μ) (hg : ae_fin_strongly_measurable g μ) : f =ᵐ[μ] g := begin rw ← sub_ae_eq_zero, have hfg : ∀ s : set α, measurable_set s → μ s < ∞ → ∫ x in s, (f - g) x ∂μ = 0, { intros s hs hμs, rw [integral_sub' (hf_int_finite s hs hμs) (hg_int_finite s hs hμs), sub_eq_zero.mpr (hfg_eq s hs hμs)], }, have hfg_int : ∀ s, measurable_set s → μ s < ∞ → integrable_on (f-g) s μ, from λ s hs hμs, (hf_int_finite s hs hμs).sub (hg_int_finite s hs hμs), exact (hf.sub hg).ae_eq_zero_of_forall_set_integral_eq_zero hfg_int hfg, end lemma Lp.ae_eq_zero_of_forall_set_integral_eq_zero (f : Lp E p μ) (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) (hf_int_finite : ∀ s, measurable_set s → μ s < ∞ → integrable_on f s μ) (hf_zero : ∀ s : set α, measurable_set s → μ s < ∞ → ∫ x in s, f x ∂μ = 0) : f =ᵐ[μ] 0 := ae_fin_strongly_measurable.ae_eq_zero_of_forall_set_integral_eq_zero hf_int_finite hf_zero (Lp.fin_strongly_measurable _ hp_ne_zero hp_ne_top).ae_fin_strongly_measurable lemma Lp.ae_eq_of_forall_set_integral_eq (f g : Lp E p μ) (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) (hf_int_finite : ∀ s, measurable_set s → μ s < ∞ → integrable_on f s μ) (hg_int_finite : ∀ s, measurable_set s → μ s < ∞ → integrable_on g s μ) (hfg : ∀ s : set α, measurable_set s → μ s < ∞ → ∫ x in s, f x ∂μ = ∫ x in s, g x ∂μ) : f =ᵐ[μ] g := ae_fin_strongly_measurable.ae_eq_of_forall_set_integral_eq hf_int_finite hg_int_finite hfg (Lp.fin_strongly_measurable _ hp_ne_zero hp_ne_top).ae_fin_strongly_measurable (Lp.fin_strongly_measurable _ hp_ne_zero hp_ne_top).ae_fin_strongly_measurable lemma ae_eq_zero_of_forall_set_integral_eq_of_fin_strongly_measurable_trim (hm : m ≤ m0) {f : α → E} (hf_int_finite : ∀ s, measurable_set[m] s → μ s < ∞ → integrable_on f s μ) (hf_zero : ∀ s : set α, measurable_set[m] s → μ s < ∞ → ∫ x in s, f x ∂μ = 0) (hf : fin_strongly_measurable f (μ.trim hm)) : f =ᵐ[μ] 0 := begin obtain ⟨t, ht_meas, htf_zero, htμ⟩ := hf.exists_set_sigma_finite, haveI : sigma_finite ((μ.restrict t).trim hm) := by rwa restrict_trim hm μ ht_meas at htμ, have htf_zero : f =ᵐ[μ.restrict tᶜ] 0, { rw [eventually_eq, ae_restrict_iff' (measurable_set.compl (hm _ ht_meas))], exact eventually_of_forall htf_zero, }, have hf_meas_m : strongly_measurable[m] f, from hf.strongly_measurable, suffices : f =ᵐ[μ.restrict t] 0, from ae_of_ae_restrict_of_ae_restrict_compl _ this htf_zero, refine measure_eq_zero_of_trim_eq_zero hm _, refine ae_eq_zero_of_forall_set_integral_eq_of_sigma_finite _ _, { intros s hs hμs, rw [integrable_on, restrict_trim hm (μ.restrict t) hs, measure.restrict_restrict (hm s hs)], rw [← restrict_trim hm μ ht_meas, measure.restrict_apply hs, trim_measurable_set_eq hm (hs.inter ht_meas)] at hμs, refine integrable.trim hm _ hf_meas_m, exact hf_int_finite _ (hs.inter ht_meas) hμs, }, { intros s hs hμs, rw [restrict_trim hm (μ.restrict t) hs, measure.restrict_restrict (hm s hs)], rw [← restrict_trim hm μ ht_meas, measure.restrict_apply hs, trim_measurable_set_eq hm (hs.inter ht_meas)] at hμs, rw ← integral_trim hm hf_meas_m, exact hf_zero _ (hs.inter ht_meas) hμs, }, end lemma integrable.ae_eq_zero_of_forall_set_integral_eq_zero {f : α → E} (hf : integrable f μ) (hf_zero : ∀ s, measurable_set s → μ s < ∞ → ∫ x in s, f x ∂μ = 0) : f =ᵐ[μ] 0 := begin have hf_Lp : mem_ℒp f 1 μ, from mem_ℒp_one_iff_integrable.mpr hf, let f_Lp := hf_Lp.to_Lp f, have hf_f_Lp : f =ᵐ[μ] f_Lp, from (mem_ℒp.coe_fn_to_Lp hf_Lp).symm, refine hf_f_Lp.trans _, refine Lp.ae_eq_zero_of_forall_set_integral_eq_zero f_Lp one_ne_zero ennreal.coe_ne_top _ _, { exact λ s hs hμs, integrable.integrable_on (L1.integrable_coe_fn _), }, { intros s hs hμs, rw integral_congr_ae (ae_restrict_of_ae hf_f_Lp.symm), exact hf_zero s hs hμs, }, end lemma integrable.ae_eq_of_forall_set_integral_eq (f g : α → E) (hf : integrable f μ) (hg : integrable g μ) (hfg : ∀ s : set α, measurable_set s → μ s < ∞ → ∫ x in s, f x ∂μ = ∫ x in s, g x ∂μ) : f =ᵐ[μ] g := begin rw ← sub_ae_eq_zero, have hfg' : ∀ s : set α, measurable_set s → μ s < ∞ → ∫ x in s, (f - g) x ∂μ = 0, { intros s hs hμs, rw integral_sub' hf.integrable_on hg.integrable_on, exact sub_eq_zero.mpr (hfg s hs hμs), }, exact integrable.ae_eq_zero_of_forall_set_integral_eq_zero (hf.sub hg) hfg', end end ae_eq_of_forall_set_integral_eq section lintegral lemma ae_measurable.ae_eq_of_forall_set_lintegral_eq {f g : α → ℝ≥0∞} (hf : ae_measurable f μ) (hg : ae_measurable g μ) (hfi : ∫⁻ x, f x ∂μ ≠ ∞) (hgi : ∫⁻ x, g x ∂μ ≠ ∞) (hfg : ∀ ⦃s⦄, measurable_set s → μ s < ∞ → ∫⁻ x in s, f x ∂μ = ∫⁻ x in s, g x ∂μ) : f =ᵐ[μ] g := begin refine ennreal.eventually_eq_of_to_real_eventually_eq (ae_lt_top' hf hfi).ne_of_lt (ae_lt_top' hg hgi).ne_of_lt (integrable.ae_eq_of_forall_set_integral_eq _ _ (integrable_to_real_of_lintegral_ne_top hf hfi) (integrable_to_real_of_lintegral_ne_top hg hgi) (λ s hs hs', _)), rw [integral_eq_lintegral_of_nonneg_ae, integral_eq_lintegral_of_nonneg_ae], { congr' 1, rw [lintegral_congr_ae (of_real_to_real_ae_eq _), lintegral_congr_ae (of_real_to_real_ae_eq _)], { exact hfg hs hs' }, { refine (ae_lt_top' hg.restrict (ne_of_lt (lt_of_le_of_lt _ hgi.lt_top))), exact @set_lintegral_univ α _ μ g ▸ lintegral_mono_set (set.subset_univ _) }, { refine (ae_lt_top' hf.restrict (ne_of_lt (lt_of_le_of_lt _ hfi.lt_top))), exact @set_lintegral_univ α _ μ f ▸ lintegral_mono_set (set.subset_univ _) } }, -- putting the proofs where they are used is extremely slow exacts [ ae_of_all _ (λ x, ennreal.to_real_nonneg), hg.ennreal_to_real.restrict.ae_strongly_measurable, ae_of_all _ (λ x, ennreal.to_real_nonneg), hf.ennreal_to_real.restrict.ae_strongly_measurable] end end lintegral end measure_theory
4833f70c4a7cb3f5fc6867a0123d01960d5beba5
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/data/real/ereal.lean
ee88291ff2fd04272f9851e1bef119d3640a4cf4
[ "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
19,718
lean
/- Copyright (c) 2019 Kevin Buzzard. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Buzzard -/ import data.real.basic import data.real.ennreal /-! # The extended reals [-∞, ∞]. This file defines `ereal`, the real numbers together with a top and bottom element, referred to as ⊤ and ⊥. It is implemented as `with_top (with_bot ℝ)` Addition and multiplication are problematic in the presence of ±∞, but negation has a natural definition and satisfies the usual properties. An ad hoc addition is defined, for which `ereal` is an `add_comm_monoid`, and even an ordered one (if `a ≤ a'` and `b ≤ b'` then `a + b ≤ a' + b'`). Note however that addition is badly behaved at `(⊥, ⊤)` and `(⊤, ⊥)` so this can not be upgraded to a group structure. Our choice is that `⊥ + ⊤ = ⊤ + ⊥ = ⊤`. An ad hoc subtraction is then defined by `x - y = x + (-y)`. It does not have nice properties, but it is sometimes convenient to have. An ad hoc multiplication is defined, for which `ereal` is a `comm_monoid_with_zero`. This does not distribute with addition, as `⊤ = ⊤ - ⊥ = 1*⊤ - 1*⊤ ≠ (1 - 1) * ⊤ = 0 * ⊤ = 0`. `ereal` is a `complete_linear_order`; this is deduced by type class inference from the fact that `with_top (with_bot L)` is a complete linear order if `L` is a conditionally complete linear order. Coercions from `ℝ` and from `ℝ≥0∞` are registered, and their basic properties are proved. The main one is the real coercion, and is usually referred to just as `coe` (lemmas such as `ereal.coe_add` deal with this coercion). The one from `ennreal` is usually called `coe_ennreal` in the `ereal` namespace. ## Tags real, ereal, complete lattice ## TODO abs : ereal → ℝ≥0∞ In Isabelle they define + - * and / (making junk choices for things like -∞ + ∞) and then prove whatever bits of the ordered ring/field axioms still hold. They also do some limits stuff (liminf/limsup etc). See https://isabelle.in.tum.de/dist/library/HOL/HOL-Library/Extended_Real.html -/ open_locale ennreal nnreal /-- ereal : The type `[-∞, ∞]` -/ @[derive [has_top, comm_monoid_with_zero, has_Sup, has_Inf, complete_linear_order, linear_ordered_add_comm_monoid_with_top]] def ereal := with_top (with_bot ℝ) /-- The canonical inclusion froms reals to ereals. Do not use directly: as this is registered as a coercion, use the coercion instead. -/ def real.to_ereal : ℝ → ereal := some ∘ some namespace ereal -- TODO: Provide explicitly, otherwise it is inferred noncomputably from `complete_linear_order` instance : has_bot ereal := ⟨some ⊥⟩ @[simp] lemma bot_lt_top : (⊥ : ereal) < ⊤ := with_top.coe_lt_top _ @[simp] lemma bot_ne_top : (⊥ : ereal) ≠ ⊤ := bot_lt_top.ne instance : has_coe ℝ ereal := ⟨real.to_ereal⟩ @[simp, norm_cast] protected lemma coe_le_coe_iff {x y : ℝ} : (x : ereal) ≤ (y : ereal) ↔ x ≤ y := by { unfold_coes, simp [real.to_ereal] } @[simp, norm_cast] protected lemma coe_lt_coe_iff {x y : ℝ} : (x : ereal) < (y : ereal) ↔ x < y := by { unfold_coes, simp [real.to_ereal] } @[simp, norm_cast] protected lemma coe_eq_coe_iff {x y : ℝ} : (x : ereal) = (y : ereal) ↔ x = y := by { unfold_coes, simp [real.to_ereal, option.some_inj] } /-- The canonical map from nonnegative extended reals to extended reals -/ def _root_.ennreal.to_ereal : ℝ≥0∞ → ereal | ⊤ := ⊤ | (some x) := x.1 instance has_coe_ennreal : has_coe ℝ≥0∞ ereal := ⟨ennreal.to_ereal⟩ instance : has_zero ereal := ⟨(0 : ℝ)⟩ instance : inhabited ereal := ⟨0⟩ /-- A recursor for `ereal` in terms of the coercion. A typical invocation looks like `induction x using ereal.rec`. Note that using `induction` directly will unfold `ereal` to `option` which is undesirable. When working in term mode, note that pattern matching can be used directly. -/ @[elab_as_eliminator] protected def rec {C : ereal → Sort*} (h_bot : C ⊥) (h_real : Π a : ℝ, C a) (h_top : C ⊤) : ∀ a : ereal, C a | ⊥ := h_bot | (a : ℝ) := h_real a | ⊤ := h_top /-! ### Real coercion -/ instance : can_lift ereal ℝ := { coe := coe, cond := λ r, r ≠ ⊤ ∧ r ≠ ⊥, prf := λ x hx, begin induction x using ereal.rec, { simpa using hx }, { simp }, { simpa using hx } end } /-- The map from extended reals to reals sending infinities to zero. -/ def to_real : ereal → ℝ | ⊥ := 0 | ⊤ := 0 | (x : ℝ) := x @[simp] lemma to_real_top : to_real ⊤ = 0 := rfl @[simp] lemma to_real_bot : to_real ⊥ = 0 := rfl @[simp] lemma to_real_zero : to_real 0 = 0 := rfl @[simp] lemma to_real_coe (x : ℝ) : to_real (x : ereal) = x := rfl @[simp] lemma bot_lt_coe (x : ℝ) : (⊥ : ereal) < x := by { apply with_top.coe_lt_coe.2, exact with_bot.bot_lt_coe _ } @[simp] lemma coe_ne_bot (x : ℝ) : (x : ereal) ≠ ⊥ := (bot_lt_coe x).ne' @[simp] lemma bot_ne_coe (x : ℝ) : (⊥ : ereal) ≠ x := (bot_lt_coe x).ne @[simp] lemma coe_lt_top (x : ℝ) : (x : ereal) < ⊤ := with_top.coe_lt_top _ @[simp] lemma coe_ne_top (x : ℝ) : (x : ereal) ≠ ⊤ := (coe_lt_top x).ne @[simp] lemma top_ne_coe (x : ℝ) : (⊤ : ereal) ≠ x := (coe_lt_top x).ne' @[simp] lemma bot_lt_zero : (⊥ : ereal) < 0 := bot_lt_coe 0 @[simp] lemma bot_ne_zero : (⊥ : ereal) ≠ 0 := (coe_ne_bot 0).symm @[simp] lemma zero_ne_bot : (0 : ereal) ≠ ⊥ := coe_ne_bot 0 @[simp] lemma zero_lt_top : (0 : ereal) < ⊤ := coe_lt_top 0 @[simp] lemma zero_ne_top : (0 : ereal) ≠ ⊤ := coe_ne_top 0 @[simp] lemma top_ne_zero : (⊤ : ereal) ≠ 0 := (coe_ne_top 0).symm @[simp, norm_cast] lemma coe_add (x y : ℝ) : ((x + y : ℝ) : ereal) = (x : ereal) + (y : ereal) := rfl @[simp] lemma coe_zero : ((0 : ℝ) : ereal) = 0 := rfl lemma to_real_le_to_real {x y : ereal} (h : x ≤ y) (hx : x ≠ ⊥) (hy : y ≠ ⊤) : x.to_real ≤ y.to_real := begin lift x to ℝ, lift y to ℝ, { simpa using h }, { simp [hy, ((bot_lt_iff_ne_bot.2 hx).trans_le h).ne'] }, { simp [hx, (h.trans_lt (lt_top_iff_ne_top.2 hy)).ne], }, end lemma coe_to_real {x : ereal} (hx : x ≠ ⊤) (h'x : x ≠ ⊥) : (x.to_real : ereal) = x := begin induction x using ereal.rec, { simpa using h'x }, { refl }, { simpa using hx }, end lemma le_coe_to_real {x : ereal} (h : x ≠ ⊤) : x ≤ x.to_real := begin by_cases h' : x = ⊥, { simp only [h', bot_le] }, { simp only [le_refl, coe_to_real h h'] }, end lemma coe_to_real_le {x : ereal} (h : x ≠ ⊥) : ↑x.to_real ≤ x := begin by_cases h' : x = ⊤, { simp only [h', le_top] }, { simp only [le_refl, coe_to_real h' h] }, end lemma eq_top_iff_forall_lt (x : ereal) : x = ⊤ ↔ ∀ (y : ℝ), (y : ereal) < x := begin split, { rintro rfl, exact ereal.coe_lt_top }, { contrapose!, intro h, exact ⟨x.to_real, le_coe_to_real h⟩, }, end lemma eq_bot_iff_forall_lt (x : ereal) : x = ⊥ ↔ ∀ (y : ℝ), x < (y : ereal) := begin split, { rintro rfl, exact bot_lt_coe }, { contrapose!, intro h, exact ⟨x.to_real, coe_to_real_le h⟩, }, end /-! ### ennreal coercion -/ @[simp] lemma to_real_coe_ennreal : ∀ {x : ℝ≥0∞}, to_real (x : ereal) = ennreal.to_real x | ⊤ := rfl | (some x) := rfl lemma coe_nnreal_eq_coe_real (x : ℝ≥0) : ((x : ℝ≥0∞) : ereal) = (x : ℝ) := rfl @[simp] lemma coe_ennreal_top : ((⊤ : ℝ≥0∞) : ereal) = ⊤ := rfl @[simp] lemma coe_ennreal_eq_top_iff : ∀ {x : ℝ≥0∞}, (x : ereal) = ⊤ ↔ x = ⊤ | ⊤ := by simp | (some x) := by { simp only [ennreal.coe_ne_top, iff_false, ennreal.some_eq_coe], dec_trivial } lemma coe_nnreal_ne_top (x : ℝ≥0) : ((x : ℝ≥0∞) : ereal) ≠ ⊤ := dec_trivial @[simp] lemma coe_nnreal_lt_top (x : ℝ≥0) : ((x : ℝ≥0∞) : ereal) < ⊤ := dec_trivial @[simp, norm_cast] lemma coe_ennreal_le_coe_ennreal_iff : ∀ {x y : ℝ≥0∞}, (x : ereal) ≤ (y : ereal) ↔ x ≤ y | x ⊤ := by simp | ⊤ (some y) := by simp | (some x) (some y) := by simp [coe_nnreal_eq_coe_real] @[simp, norm_cast] lemma coe_ennreal_lt_coe_ennreal_iff : ∀ {x y : ℝ≥0∞}, (x : ereal) < (y : ereal) ↔ x < y | ⊤ ⊤ := by simp | (some x) ⊤ := by simp | ⊤ (some y) := by simp | (some x) (some y) := by simp [coe_nnreal_eq_coe_real] @[simp, norm_cast] lemma coe_ennreal_eq_coe_ennreal_iff : ∀ {x y : ℝ≥0∞}, (x : ereal) = (y : ereal) ↔ x = y | ⊤ ⊤ := by simp | (some x) ⊤ := by simp | ⊤ (some y) := by simp [(coe_nnreal_lt_top y).ne'] | (some x) (some y) := by simp [coe_nnreal_eq_coe_real] lemma coe_ennreal_nonneg (x : ℝ≥0∞) : (0 : ereal) ≤ x := coe_ennreal_le_coe_ennreal_iff.2 (zero_le x) @[simp] lemma bot_lt_coe_ennreal (x : ℝ≥0∞) : (⊥ : ereal) < x := (bot_lt_coe 0).trans_le (coe_ennreal_nonneg _) @[simp] lemma coe_ennreal_ne_bot (x : ℝ≥0∞) : (x : ereal) ≠ ⊥ := (bot_lt_coe_ennreal x).ne' @[simp, norm_cast] lemma coe_ennreal_add : ∀ (x y : ennreal), ((x + y : ℝ≥0∞) : ereal) = x + y | ⊤ y := rfl | x ⊤ := by simp | (some x) (some y) := rfl @[simp] lemma coe_ennreal_zero : ((0 : ℝ≥0∞) : ereal) = 0 := rfl /-! ### Order -/ lemma exists_rat_btwn_of_lt : Π {a b : ereal} (hab : a < b), ∃ (x : ℚ), a < (x : ℝ) ∧ ((x : ℝ) : ereal) < b | ⊤ b h := (not_top_lt h).elim | (a : ℝ) ⊥ h := (lt_irrefl _ ((bot_lt_coe a).trans h)).elim | (a : ℝ) (b : ℝ) h := by simp [exists_rat_btwn (ereal.coe_lt_coe_iff.1 h)] | (a : ℝ) ⊤ h := let ⟨b, hab⟩ := exists_rat_gt a in ⟨b, by simpa using hab, coe_lt_top _⟩ | ⊥ ⊥ h := (lt_irrefl _ h).elim | ⊥ (a : ℝ) h := let ⟨b, hab⟩ := exists_rat_lt a in ⟨b, bot_lt_coe _, by simpa using hab⟩ | ⊥ ⊤ h := ⟨0, bot_lt_coe _, coe_lt_top _⟩ lemma lt_iff_exists_rat_btwn {a b : ereal} : a < b ↔ ∃ (x : ℚ), a < (x : ℝ) ∧ ((x : ℝ) : ereal) < b := ⟨λ hab, exists_rat_btwn_of_lt hab, λ ⟨x, ax, xb⟩, ax.trans xb⟩ lemma lt_iff_exists_real_btwn {a b : ereal} : a < b ↔ ∃ (x : ℝ), a < x ∧ (x : ereal) < b := ⟨λ hab, let ⟨x, ax, xb⟩ := exists_rat_btwn_of_lt hab in ⟨(x : ℝ), ax, xb⟩, λ ⟨x, ax, xb⟩, ax.trans xb⟩ /-- The set of numbers in `ereal` that are not equal to `±∞` is equivalent to `ℝ`. -/ def ne_top_bot_equiv_real : ({⊥, ⊤} : set ereal).compl ≃ ℝ := { to_fun := λ x, ereal.to_real x, inv_fun := λ x, ⟨x, by simp⟩, left_inv := λ ⟨x, hx⟩, subtype.eq $ begin lift x to ℝ, { simp }, { simpa [not_or_distrib, and_comm] using hx } end, right_inv := λ x, by simp } /-! ### Addition -/ @[simp] lemma add_top (x : ereal) : x + ⊤ = ⊤ := add_top _ @[simp] lemma top_add (x : ereal) : ⊤ + x = ⊤ := top_add _ @[simp] lemma bot_add_bot : (⊥ : ereal) + ⊥ = ⊥ := rfl @[simp] lemma bot_add_coe (x : ℝ) : (⊥ : ereal) + x = ⊥ := rfl @[simp] lemma coe_add_bot (x : ℝ) : (x : ereal) + ⊥ = ⊥ := rfl lemma to_real_add : ∀ {x y : ereal} (hx : x ≠ ⊤) (h'x : x ≠ ⊥) (hy : y ≠ ⊤) (h'y : y ≠ ⊥), to_real (x + y) = to_real x + to_real y | ⊥ y hx h'x hy h'y := (h'x rfl).elim | ⊤ y hx h'x hy h'y := (hx rfl).elim | x ⊤ hx h'x hy h'y := (hy rfl).elim | x ⊥ hx h'x hy h'y := (h'y rfl).elim | (x : ℝ) (y : ℝ) hx h'x hy h'y := by simp [← ereal.coe_add] lemma add_lt_add_right_coe {x y : ereal} (h : x < y) (z : ℝ) : x + z < y + z := begin induction x using ereal.rec; induction y using ereal.rec, { exact (lt_irrefl _ h).elim }, { simp only [bot_lt_coe, bot_add_coe, ← coe_add] }, { simp }, { exact (lt_irrefl _ (h.trans (bot_lt_coe x))).elim }, { norm_cast at h ⊢, exact add_lt_add_right h _ }, { simp only [← coe_add, top_add, coe_lt_top] }, { exact (lt_irrefl _ (h.trans_le le_top)).elim }, { exact (lt_irrefl _ (h.trans_le le_top)).elim }, { exact (lt_irrefl _ (h.trans_le le_top)).elim }, end lemma add_lt_add_of_lt_of_le {x y z t : ereal} (h : x < y) (h' : z ≤ t) (hz : z ≠ ⊥) (ht : t ≠ ⊤) : x + z < y + t := begin induction z using ereal.rec, { simpa only using hz }, { calc x + z < y + z : add_lt_add_right_coe h _ ... ≤ y + t : add_le_add (le_refl _) h' }, { exact (ht (top_le_iff.1 h')).elim } end lemma add_lt_add_left_coe {x y : ereal} (h : x < y) (z : ℝ) : (z : ereal) + x < z + y := by simpa [add_comm] using add_lt_add_right_coe h z lemma add_lt_add {x y z t : ereal} (h1 : x < y) (h2 : z < t) : x + z < y + t := begin induction y using ereal.rec, { exact (lt_irrefl _ (bot_le.trans_lt h1)).elim }, { calc x + z ≤ y + z : add_le_add h1.le (le_refl _) ... < y + t : add_lt_add_left_coe h2 _ }, { simp [lt_top_iff_ne_top, with_top.add_eq_top, h1.ne, (h2.trans_le le_top).ne] } end @[simp] lemma add_eq_top_iff {x y : ereal} : x + y = ⊤ ↔ x = ⊤ ∨ y = ⊤ := begin induction x using ereal.rec; induction y using ereal.rec; simp [← ereal.coe_add], end @[simp] lemma add_lt_top_iff {x y : ereal} : x + y < ⊤ ↔ x < ⊤ ∧ y < ⊤ := by simp [lt_top_iff_ne_top, not_or_distrib] /-! ### Negation -/ /-- negation on `ereal` -/ protected def neg : ereal → ereal | ⊥ := ⊤ | ⊤ := ⊥ | (x : ℝ) := (-x : ℝ) instance : has_neg ereal := ⟨ereal.neg⟩ @[norm_cast] protected lemma neg_def (x : ℝ) : ((-x : ℝ) : ereal) = -x := rfl @[simp] lemma neg_top : - (⊤ : ereal) = ⊥ := rfl @[simp] lemma neg_bot : - (⊥ : ereal) = ⊤ := rfl @[simp] lemma neg_zero : - (0 : ereal) = 0 := by { change ((-0 : ℝ) : ereal) = 0, simp } /-- `- -a = a` on `ereal`. -/ @[simp] protected theorem neg_neg : ∀ (a : ereal), - (- a) = a | ⊥ := rfl | ⊤ := rfl | (a : ℝ) := by { norm_cast, simp [neg_neg a] } theorem neg_inj {a b : ereal} (h : -a = -b) : a = b := by rw [←ereal.neg_neg a, h, ereal.neg_neg b] @[simp] theorem neg_eq_neg_iff (a b : ereal) : - a = - b ↔ a = b := ⟨λ h, neg_inj h, λ h, by rw [h]⟩ @[simp] lemma to_real_neg : ∀ {a : ereal}, to_real (-a) = - to_real a | ⊤ := by simp | ⊥ := by simp | (x : ℝ) := rfl /-- Even though `ereal` is not an additive group, `-a = b ↔ -b = a` still holds -/ theorem neg_eq_iff_neg_eq {a b : ereal} : -a = b ↔ -b = a := ⟨by {intro h, rw ←h, exact ereal.neg_neg a}, by {intro h, rw ←h, exact ereal.neg_neg b}⟩ @[simp] lemma neg_eg_top_iff {x : ereal} : - x = ⊤ ↔ x = ⊥ := by { rw neg_eq_iff_neg_eq, simp [eq_comm] } @[simp] lemma neg_eg_bot_iff {x : ereal} : - x = ⊥ ↔ x = ⊤ := by { rw neg_eq_iff_neg_eq, simp [eq_comm] } @[simp] lemma neg_eg_zero_iff {x : ereal} : - x = 0 ↔ x = 0 := by { rw neg_eq_iff_neg_eq, simp [eq_comm] } /-- if `-a ≤ b` then `-b ≤ a` on `ereal`. -/ protected theorem neg_le_of_neg_le : ∀ {a b : ereal} (h : -a ≤ b), -b ≤ a | ⊥ ⊥ h := h | ⊥ (some b) h := by cases (top_le_iff.1 h) | ⊤ l h := le_top | (a : ℝ) ⊥ h := by cases (le_bot_iff.1 h) | l ⊤ h := bot_le | (a : ℝ) (b : ℝ) h := by { norm_cast at h ⊢, exact _root_.neg_le_of_neg_le h } /-- `-a ≤ b ↔ -b ≤ a` on `ereal`. -/ protected theorem neg_le {a b : ereal} : -a ≤ b ↔ -b ≤ a := ⟨ereal.neg_le_of_neg_le, ereal.neg_le_of_neg_le⟩ /-- `a ≤ -b → b ≤ -a` on ereal -/ theorem le_neg_of_le_neg {a b : ereal} (h : a ≤ -b) : b ≤ -a := by rwa [←ereal.neg_neg b, ereal.neg_le, ereal.neg_neg] @[simp] lemma neg_le_neg_iff {a b : ereal} : - a ≤ - b ↔ b ≤ a := by conv_lhs { rw [ereal.neg_le, ereal.neg_neg] } @[simp, norm_cast] lemma coe_neg (x : ℝ) : ((- x : ℝ) : ereal) = - (x : ereal) := rfl /-- Negation as an order reversing isomorphism on `ereal`. -/ def neg_order_iso : ereal ≃o (order_dual ereal) := { to_fun := ereal.neg, inv_fun := ereal.neg, left_inv := ereal.neg_neg, right_inv := ereal.neg_neg, map_rel_iff' := λ x y, neg_le_neg_iff } lemma neg_lt_of_neg_lt {a b : ereal} (h : -a < b) : -b < a := begin apply lt_of_le_of_ne (ereal.neg_le_of_neg_le h.le), assume H, rw [← H, ereal.neg_neg] at h, exact lt_irrefl _ h end lemma neg_lt_iff_neg_lt {a b : ereal} : -a < b ↔ -b < a := ⟨λ h, ereal.neg_lt_of_neg_lt h, λ h, ereal.neg_lt_of_neg_lt h⟩ /-! ### Subtraction -/ /-- Subtraction on `ereal`, defined by `x - y = x + (-y)`. Since addition is badly behaved at some points, so is subtraction. There is no standard algebraic typeclass involving subtraction that is registered on `ereal` because of this bad behavior. -/ protected noncomputable def sub (x y : ereal) : ereal := x + (-y) noncomputable instance : has_sub ereal := ⟨ereal.sub⟩ @[simp] lemma top_sub (x : ereal) : ⊤ - x = ⊤ := top_add x @[simp] lemma sub_bot (x : ereal) : x - ⊥ = ⊤ := add_top x @[simp] lemma bot_sub_top : (⊥ : ereal) - ⊤ = ⊥ := rfl @[simp] lemma bot_sub_coe (x : ℝ) : (⊥ : ereal) - x = ⊥ := rfl @[simp] lemma coe_sub_bot (x : ℝ) : (x : ereal) - ⊤ = ⊥ := rfl @[simp] lemma sub_zero (x : ereal) : x - 0 = x := by { change x + (-0) = x, simp } @[simp] lemma zero_sub (x : ereal) : 0 - x = - x := by { change 0 + (-x) = - x, simp } lemma sub_eq_add_neg (x y : ereal) : x - y = x + -y := rfl lemma sub_le_sub {x y z t : ereal} (h : x ≤ y) (h' : t ≤ z) : x - z ≤ y - t := add_le_add h (neg_le_neg_iff.2 h') lemma sub_lt_sub_of_lt_of_le {x y z t : ereal} (h : x < y) (h' : z ≤ t) (hz : z ≠ ⊥) (ht : t ≠ ⊤) : x - t < y - z := add_lt_add_of_lt_of_le h (neg_le_neg_iff.2 h') (by simp [ht]) (by simp [hz]) lemma coe_real_ereal_eq_coe_to_nnreal_sub_coe_to_nnreal (x : ℝ) : (x : ereal) = real.to_nnreal x - real.to_nnreal (-x) := begin rcases le_or_lt 0 x with h|h, { have : real.to_nnreal x = ⟨x, h⟩, by { ext, simp [h] }, simp only [real.to_nnreal_of_nonpos (neg_nonpos.mpr h), this, sub_zero, ennreal.coe_zero, coe_ennreal_zero, coe_coe], refl }, { have : (x : ereal) = - (- x : ℝ), by simp, conv_lhs { rw this }, have : real.to_nnreal (-x) = ⟨-x, neg_nonneg.mpr h.le⟩, by { ext, simp [neg_nonneg.mpr h.le], }, simp only [real.to_nnreal_of_nonpos h.le, this, zero_sub, neg_eq_neg_iff, coe_neg, ennreal.coe_zero, coe_ennreal_zero, coe_coe], refl } end lemma to_real_sub {x y : ereal} (hx : x ≠ ⊤) (h'x : x ≠ ⊥) (hy : y ≠ ⊤) (h'y : y ≠ ⊥) : to_real (x - y) = to_real x - to_real y := begin rw [ereal.sub_eq_add_neg, to_real_add hx h'x, to_real_neg], { refl }, { simpa using hy }, { simpa using h'y } end /-! ### Multiplication -/ @[simp] lemma coe_one : ((1 : ℝ) : ereal) = 1 := rfl @[simp, norm_cast] lemma coe_mul (x y : ℝ) : ((x * y : ℝ) : ereal) = (x : ereal) * (y : ereal) := eq.trans (with_bot.coe_eq_coe.mpr with_bot.coe_mul) with_top.coe_mul @[simp] lemma mul_top (x : ereal) (h : x ≠ 0) : x * ⊤ = ⊤ := with_top.mul_top h @[simp] lemma top_mul (x : ereal) (h : x ≠ 0) : ⊤ * x = ⊤ := with_top.top_mul h @[simp] lemma bot_mul_bot : (⊥ : ereal) * ⊥ = ⊥ := rfl @[simp] lemma bot_mul_coe (x : ℝ) (h : x ≠ 0) : (⊥ : ereal) * x = ⊥ := with_top.coe_mul.symm.trans $ with_bot.coe_eq_coe.mpr $ with_bot.bot_mul $ function.injective.ne (@option.some.inj _) h @[simp] lemma coe_mul_bot (x : ℝ) (h : x ≠ 0) : (x : ereal) * ⊥ = ⊥ := with_top.coe_mul.symm.trans $ with_bot.coe_eq_coe.mpr $ with_bot.mul_bot $ function.injective.ne (@option.some.inj _) h @[simp] lemma to_real_one : to_real 1 = 1 := rfl lemma to_real_mul : ∀ {x y : ereal}, to_real (x * y) = to_real x * to_real y | ⊤ y := by by_cases hy : y = 0; simp [hy] | x ⊤ := by by_cases hx : x = 0; simp [hx] | (x : ℝ) (y : ℝ) := by simp [← ereal.coe_mul] | ⊥ (y : ℝ) := by by_cases hy : y = 0; simp [hy] | (x : ℝ) ⊥ := by by_cases hx : x = 0; simp [hx] | ⊥ ⊥ := by simp end ereal
18d70183c161769feefde0ce1792da8329f48859
2a73ce8bc0731b170b40e8c9faca9b49d34ba5c6
/cond_exp-question-20210401.lean
b458e240bf3b54641a6c7d6d67b56045ba810d6d
[]
no_license
kkytola/lean-questions
f383016b7870432807d8f4ced256f7506a59b0ff
9a7ded8036534575b682e28ddfed4c2f1089959d
refs/heads/main
1,692,989,428,439
1,634,665,853,000
1,634,665,853,000
353,850,251
0
0
null
null
null
null
UTF-8
Lean
false
false
4,507
lean
import tactic import measure_theory.integration import measure_theory.measurable_space noncomputable theory open set open classical open measure_theory open measurable_space open_locale ennreal namespace cond_exp def is_subsigmaalg {S : Type*} (Γ₁ Γ₂ : measurable_space S ) : Prop := ∀ (E : set S ) , (Γ₁.is_measurable' E) → (Γ₂.is_measurable' E) lemma is_subsigmaalg_iff_le {S : Type*} {Γ₁ Γ₂ : measurable_space S } : is_subsigmaalg Γ₁ Γ₂ ↔ Γ₁ ≤ Γ₂ := begin split , { intros hsub E hE , exact hsub E hE , } , { intros hle E hE , exact hle E hE , } , end @[simp] lemma self_is_subsigmaalg {S : Type*} (Γ : measurable_space S ) : is_subsigmaalg Γ Γ := by { intros E hE , exact hE , } @[simp] lemma self_le_self_sigmaalg {S : Type*} (Γ : measurable_space S ) : Γ ≤ Γ := by { intros E hE , exact hE , } lemma self_is_subsigmaalg' {S : Type*} (Γ : measurable_space S ) : is_subsigmaalg Γ Γ := begin have key : Γ ≤ Γ , { intros E hE , exact hE , } , rwa is_subsigmaalg_iff_le , end def is_trivial_sigmaalg {S : Type*} (Γ : measurable_space S ) : Prop := ∀ (E : set S ) , (Γ.is_measurable' E) → (E = ∅ ∨ E = univ) @[simp] lemma trivial_is_subsigmaalg {S : Type*} (Γtriv Γ : measurable_space S ) [htriv : is_trivial_sigmaalg Γtriv] : is_subsigmaalg Γtriv Γ := begin intros E hE , have key := htriv E hE , cases key with hemp huniv , { rw hemp , exact measurable_space.is_measurable_empty Γ , } , { rw huniv , have mbleuniv := (Γ.is_measurable_compl ∅ Γ.is_measurable_empty) , simp at mbleuniv , exact mbleuniv , } , end @[simp] lemma trivial_le_sigmaalg {S : Type*} (Γtriv Γ : measurable_space S ) [htriv : is_trivial_sigmaalg Γtriv] : Γtriv ≤ Γ := begin have key := ( @trivial_is_subsigmaalg S Γtriv Γ htriv ) , rw ← is_subsigmaalg_iff_le , exact key , end lemma mble_of_submble {S T : Type*} [measurable_space T] (f : S → T) (Γsub Γfull : measurable_space S ) [hsub : Γsub ≤ Γfull] : (@measurable S T Γsub _ f) → (@measurable S T Γfull _ f) := begin intros hf B hB , exact hsub (f⁻¹' B) (hf hB) , end structure cond_exp_enn {S : Type*} (f : S → ennreal) (Γsub Γfull : measurable_space S ) [hsub : Γsub ≤ Γfull] (μ : @measure_theory.measure S Γfull) := -- In the current version I am not requiring measurability -- [hf : @measurable S ennreal Γfull _ f] -- and integrability. I even failed to state integrability, -- the following does not work: -- [hintble : measure_theory.has_finite_integral f μ] -- Also I'm not requiring that the measure is a -- probability measure. -- [hproba : probability_measure μ] -- All of these would almost always -- be needed, so should they be a part of the definition? ( to_fun : S → ennreal ) ( is_submeasurable : @measurable S ennreal Γsub _ to_fun ) ( equal_subintegrals : ∀ (E : set S) , Γsub.is_measurable' E → ∫⁻ x in E , to_fun(x) ∂ μ = ∫⁻ x in E , f(x) ∂ μ ) -- Of course there should be a coercion to function, -- just pick the `to_fun`! Somehow, I did not manage to -- make this work at all... @[instance] def cond_exp_enn.has_coe_to_fun {S : Type*} {Γsub Γfull : measurable_space S} [hsub : is_subsigmaalg Γsub Γfull] {f : S → ennreal} {μ : @measure_theory.measure S Γfull} : has_coe_to_fun (@cond_exp_enn S f Γsub Γfull hsub μ) := { coe := sorry , F := sorry , } lemma cond_exp_enn_full {S : Type*} {Γ : measurable_space S} {f : S → ennreal} [hf : @measurable S ennreal Γ _ f] {μ : @measure_theory.measure S Γ} : @cond_exp_enn S f Γ Γ (self_le_self_sigmaalg Γ) μ := { to_fun := f , is_submeasurable := hf , equal_subintegrals := by { intros E hE , refl , } , } lemma cond_exp_enn_trivial {S : Type*} {Γtriv Γ : measurable_space S} [htriv : is_trivial_sigmaalg Γtriv] {f : S → ennreal} [hf : @measurable S ennreal Γ _ f] {μ : @measure_theory.measure S Γ} [hproba : probability_measure μ] : @cond_exp_enn S f Γtriv Γ (@trivial_le_sigmaalg S Γtriv Γ htriv) μ := { to_fun := ( λ (z : S) , ( ∫⁻ x , f(x) ∂ μ ) ) , is_submeasurable := by simp , equal_subintegrals := begin intros E hE , have key := htriv E hE , cases key with hemp huniv , { rw hemp , simp , } , { rw huniv , simp , rw hproba.measure_univ , simp , } , end } end cond_exp
9164d0264c42e81d77ce484afe1c11fd023aed81
5756a081670ba9c1d1d3fca7bd47cb4e31beae66
/Mathport/Bridge/Config.lean
840f58e86283d6c71797a7c72f2e4e4289aa4131
[ "Apache-2.0" ]
permissive
leanprover-community/mathport
2c9bdc8292168febf59799efdc5451dbf0450d4a
13051f68064f7638970d39a8fecaede68ffbf9e1
refs/heads/master
1,693,841,364,079
1,693,813,111,000
1,693,813,111,000
379,357,010
27
10
Apache-2.0
1,691,309,132,000
1,624,384,521,000
Lean
UTF-8
Lean
false
false
1,867
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Daniel Selsam -/ import Lean import Mathport.Util.Misc import Mathport.Util.Json import Mathport.Bridge.Path namespace Mathport open Lean Lean.Json /-- Controls the way that translated definitions are displayed when a lean 4 definition already exists. -/ inductive ReplacementStyle where /-- Completely skip the mathport output. Good for pruning an already ported file. -/ | skip /-- Show a `#print` command pointing at the lean 4 declaration, and then print the mathport output in a comment. Useful when we want to recover the mathport output without re-running the program in case the alignment is erroneous. -/ | comment /-- Ignore existing lean 4 definitions and just print all mathport outputs. Will cause redefinition errors. -/ | keep deriving FromJson, Inhabited structure CommitInfo where repo : String commit : String fileRevs : HashMap String String deriving FromJson, Inhabited structure Config where pathConfig : Path.Config commitInfo : Option CommitInfo := none baseModules : Array Name := #[`Mathlib] extraModules : Array Name := #[] defEqConstructions : HashSet String := {} forceAbbrevs : HashSet Name := {} stringsToKeep : HashSet Name := {} disabledInstances : HashSet Name := {} neverSorries : HashSet Name := {} sorries : HashSet Name := {} skipProofs : Bool := false skipDefEq : Bool := true error2warning : Bool := false replacementStyle : ReplacementStyle := .comment redundantAlign : Bool := true dubiousMsg : Bool := false dubiousMsgMaxDepth : Nat := 30 dubiousMsgMaxSize : Nat := 10000 deriving FromJson, Inhabited end Mathport
f51a2659be9ca881749e6c9cee1b7ddf5447d45c
8b9f17008684d796c8022dab552e42f0cb6fb347
/tests/lean/hott/329.hlean
f338639f16661e8fd0c213cb22e515c8b1bdb20d
[ "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
707
hlean
open eq sigma variables {A : Type} {B : A → Type} {C : Πa, B a → Type} {D : Πa b, C a b → Type} {a a' a'' : A} {b b₁ b₂ : B a} {b' : B a'} {b'' : B a''} {u v w : Σa, B a} definition path_sigma_dpair (p : a = a') (q : p ▹ b = b') : sigma.mk a b = sigma.mk a' b' := eq.rec_on p (λb b' q, eq.rec_on q idp) b b' q definition path_sigma (p : pr1 u = pr1 v) (q : p ▹ pr2 u = pr2 v) : u = v := destruct u (λu1 u2, destruct v (λ v1 v2, path_sigma_dpair)) p q definition path_path_sigma_lemma' {p1 : a = a'} {p2 : p1 ▹ b = b'} {q2 : p1 ▹ b = b'} (s : idp ▹ p2 = q2) : path_sigma p1 p2 = path_sigma p1 q2 := begin apply (eq.rec_on s), apply idp, end
879ec5ce263fb2193f5030b1aab718ac035fc77f
4efff1f47634ff19e2f786deadd394270a59ecd2
/src/data/sym2.lean
2316f15ca55e1a0d52c7a0c9f9e2c4d9176a134f
[ "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
11,162
lean
/- Copyright (c) 2020 Kyle Miller All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Kyle Miller. -/ import tactic.linarith import data.sym open function open sym /-! # The symmetric square This file defines the symmetric square, which is `α × α` modulo swapping. This is also known as the type of unordered pairs. More generally, the symmetric square is the second symmetric power (see `data.sym`). The equivalence is `sym2.equiv_sym`. From the point of view that an unordered pair is equivalent to a multiset of cardinality two (see `sym2.equiv_multiset`), there is a `has_mem` instance `sym2.mem`, which is a `Prop`-valued membership test. Given `a ∈ z` for `z : sym2 α`, it does not appear to be possible, in general, to *computably* give the other element in the pair. For this, `sym2.vmem a z` is a `Type`-valued membership test that gives a way to obtain the other element with `sym2.vmem.other`. Recall that an undirected graph (allowing self loops, but no multiple edges) is equivalent to a symmetric relation on the vertex type `α`. Given a symmetric relation on `α`, the corresponding edge set is constructed by `sym2.from_rel`. ## Notation The symmetric square has a setoid instance, so `⟦(a, b)⟧` denotes a term of the symmetric square. ## Tags symmetric square, unordered pairs, symmetric powers -/ universe u variables {α : Type u} namespace sym2 /-- This is the relation capturing the notion of pairs equivalent up to permutations. -/ inductive rel (α : Type u) : (α × α) → (α × α) → Prop | refl (x y : α) : rel (x, y) (x, y) | swap (x y : α) : rel (x, y) (y, x) attribute [refl] rel.refl @[symm] lemma rel.symm {x y : α × α} : rel α x y → rel α y x := by { rintro ⟨_,_⟩, exact a, apply rel.swap } @[trans] lemma rel.trans {x y z : α × α} : rel α x y → rel α y z → rel α x z := by { intros a b, cases_matching* rel _ _ _; apply rel.refl <|> apply rel.swap } lemma rel.is_equivalence : equivalence (rel α) := by tidy; apply rel.trans; assumption instance rel.setoid (α : Type u) : setoid (α × α) := ⟨rel α, rel.is_equivalence⟩ end sym2 /-- `sym2 α` is the symmetric square of `α`, which, in other words, is the type of unordered pairs. It is equivalent in a natural way to multisets of cardinality 2 (see `sym2.equiv_multiset`). -/ @[reducible] def sym2 (α : Type u) := quotient (sym2.rel.setoid α) namespace sym2 lemma eq_swap {a b : α} : ⟦(a, b)⟧ = ⟦(b, a)⟧ := by { rw quotient.eq, apply rel.swap } lemma congr_right (a b c : α) : ⟦(a, b)⟧ = ⟦(a, c)⟧ ↔ b = c := by { split; intro h, { rw quotient.eq at h, cases h; refl }, rw h } /-- The functor `sym2` is functorial, and this function constructs the induced maps. -/ def map {α β : Type*} (f : α → β) : sym2 α → sym2 β := quotient.map (prod.map f f) (by { rintros _ _ h, cases h, { refl }, apply rel.swap }) @[simp] lemma map_id : sym2.map (@id α) = id := by tidy lemma map_comp {α β γ : Type*} {g : β → γ} {f : α → β} : sym2.map (g ∘ f) = sym2.map g ∘ sym2.map f := by tidy section membership /-! ### Declarations about membership -/ /-- This is a predicate that determines whether a given term is a member of a term of the symmetric square. From this point of view, the symmetric square is the subtype of cardinality-two multisets on `α`. -/ def mem (x : α) (z : sym2 α) : Prop := ∃ (y : α), z = ⟦(x, y)⟧ instance : has_mem α (sym2 α) := ⟨mem⟩ lemma mk_has_mem (x y : α) : x ∈ ⟦(x, y)⟧ := ⟨y, rfl⟩ lemma mk_has_mem_right (x y : α) : y ∈ ⟦(x, y)⟧ := by { rw eq_swap, apply mk_has_mem } /-- This is a type-valued version of the membership predicate `mem` that contains the other element `y` of `z` such that `z = ⟦(x, y)⟧`. It is a subsingleton already, so there is no need to apply `trunc` to the type. -/ @[nolint has_inhabited_instance] def vmem (x : α) (z : sym2 α) : Type u := {y : α // z = ⟦(x, y)⟧} instance (x : α) (z : sym2 α) : subsingleton {y : α // z = ⟦(x, y)⟧} := ⟨by { rintros ⟨a, ha⟩ ⟨b, hb⟩, rw [ha, congr_right] at hb, tidy }⟩ /-- The `vmem` version of `mk_has_mem`. -/ def mk_has_vmem (x y : α) : vmem x ⟦(x, y)⟧ := ⟨y, rfl⟩ instance {a : α} {z : sym2 α} : has_lift (vmem a z) (mem a z) := ⟨λ h, ⟨h.val, h.property⟩⟩ /-- Given an element of a term of the symmetric square (using `vmem`), retrieve the other element. -/ def vmem.other {a : α} {p : sym2 α} (h : vmem a p) : α := h.val /-- The defining property of the other element is that it can be used to reconstruct the term of the symmetric square. -/ lemma vmem_other_spec {a : α} {z : sym2 α} (h : vmem a z) : z = ⟦(a, h.other)⟧ := by { delta vmem.other, tidy } /-- This is the `mem`-based version of `other`. -/ noncomputable def mem.other {a : α} {z : sym2 α} (h : a ∈ z) : α := classical.some h lemma mem_other_spec {a : α} {z : sym2 α} (h : a ∈ z) : ⟦(a, h.other)⟧ = z := by erw ← classical.some_spec h lemma other_is_mem_other {a : α} {z : sym2 α} (h : vmem a z) (h' : a ∈ z) : h.other = mem.other h' := by rw [← congr_right a, ← vmem_other_spec h, mem_other_spec] lemma eq_iff {x y z w : α} : ⟦(x, y)⟧ = ⟦(z, w)⟧ ↔ (x = z ∧ y = w) ∨ (x = w ∧ y = z) := begin split; intro h, { rw quotient.eq at h, cases h; tidy }, { cases h; rw [h.1, h.2], rw eq_swap } end @[simp] lemma mem_iff {a b c : α} : a ∈ ⟦(b, c)⟧ ↔ a = b ∨ a = c := { mp := by { rintro ⟨_, h⟩, rw eq_iff at h, tidy }, mpr := by { rintro ⟨_⟩; subst a, { apply mk_has_mem }, apply mk_has_mem_right } } lemma elems_iff_eq {x y : α} {z : sym2 α} (hne : x ≠ y) : x ∈ z ∧ y ∈ z ↔ z = ⟦(x, y)⟧ := begin split, { rintros ⟨hx, hy⟩, induction z, cases z with z₁ z₂, apply eq_iff.mpr, cases mem_iff.mp hx with hx hx; cases mem_iff.mp hy with hy hy; cc, refl }, { rintro rfl, simp }, end end membership /-- A type `α` is naturally included in the diagonal of `α × α`, and this function gives the image of this diagonal in `sym2 α`. -/ def diag (x : α) : sym2 α := ⟦(x, x)⟧ /-- A predicate for testing whether an element of `sym2 α` is on the diagonal. -/ def is_diag (z : sym2 α) : Prop := z ∈ set.range (@diag α) lemma is_diag_iff_proj_eq (z : α × α) : is_diag ⟦z⟧ ↔ z.1 = z.2 := begin cases z with a, split, { rintro ⟨_, h⟩, erw eq_iff at h, cc }, { rintro ⟨⟩, use a, refl }, end instance is_diag.decidable_pred (α : Type u) [decidable_eq α] : decidable_pred (@is_diag α) := by { intro z, induction z, { erw is_diag_iff_proj_eq, apply_instance }, apply subsingleton.elim } section relations /-! ### Declarations about symmetric relations -/ variables {r : α → α → Prop} /-- Symmetric relations define a set on `sym2 α` by taking all those pairs of elements that are related. -/ def from_rel (sym : symmetric r) : set (sym2 α) := λ z, quotient.rec_on z (λ z, r z.1 z.2) (by { rintros _ _ ⟨_,_⟩, tidy }) @[simp] lemma from_rel_proj_prop {sym : symmetric r} {z : α × α} : ⟦z⟧ ∈ from_rel sym ↔ r z.1 z.2 := by tidy @[simp] lemma from_rel_prop {sym : symmetric r} {a b : α} : ⟦(a, b)⟧ ∈ from_rel sym ↔ r a b := by simp only [from_rel_proj_prop] lemma from_rel_irreflexive {sym : symmetric r} : irreflexive r ↔ ∀ {z}, z ∈ from_rel sym → ¬is_diag z := { mp := by { intros h z hr hd, induction z, erw is_diag_iff_proj_eq at hd, erw from_rel_proj_prop at hr, tidy }, mpr := by { intros h x hr, rw ← @from_rel_prop _ _ sym at hr, exact h hr ⟨x, rfl⟩ }} instance from_rel.decidable_as_set (sym : symmetric r) [h : decidable_rel r] : decidable_pred (λ x, x ∈ sym2.from_rel sym) := λ (x : sym2 α), quotient.rec_on x (λ x', by { simp_rw from_rel_proj_prop, apply_instance }) (by tidy) instance from_rel.decidable_pred (sym : symmetric r) [h : decidable_rel r] : decidable_pred (sym2.from_rel sym) := by { change decidable_pred (λ x, x ∈ sym2.from_rel sym), apply_instance } end relations section sym_equiv /-! ### Equivalence to the second symmetric power -/ local attribute [instance] vector.perm.is_setoid private def from_vector {α : Type*} : vector α 2 → α × α | ⟨[a, b], h⟩ := (a, b) private lemma perm_card_two_iff {α : Type*} {a₁ b₁ a₂ b₂ : α} : [a₁, b₁].perm [a₂, b₂] ↔ (a₁ = a₂ ∧ b₁ = b₂) ∨ (a₁ = b₂ ∧ b₁ = a₂) := { mp := by { simp [← multiset.coe_eq_coe, ← multiset.cons_coe, multiset.cons_eq_cons]; tidy }, mpr := by { intro h, cases h; rw [h.1, h.2], apply list.perm.swap', refl } } /-- The symmetric square is equivalent to length-2 vectors up to permutations. -/ def sym2_equiv_sym' {α : Type*} : equiv (sym2 α) (sym' α 2) := { to_fun := quotient.map (λ (x : α × α), ⟨[x.1, x.2], rfl⟩) (by { rintros _ _ ⟨_⟩, { refl }, apply list.perm.swap', refl }), inv_fun := quotient.map from_vector (begin rintros ⟨x, hx⟩ ⟨y, hy⟩ h, cases x with _ x, { simp at hx; tauto }, cases x with _ x, { simp at hx; norm_num at hx }, cases x with _ x, swap, { exfalso, simp at hx; linarith [hx] }, cases y with _ y, { simp at hy; tauto }, cases y with _ y, { simp at hy; norm_num at hy }, cases y with _ y, swap, { exfalso, simp at hy; linarith [hy] }, rcases perm_card_two_iff.mp h with ⟨rfl,rfl⟩|⟨rfl,rfl⟩, { refl }, apply sym2.rel.swap, end), left_inv := by tidy, right_inv := λ x, begin induction x, { cases x with x hx, cases x with _ x, { simp at hx; tauto }, cases x with _ x, { simp at hx; norm_num at hx }, cases x with _ x, swap, { exfalso, simp at hx; linarith [hx] }, refl }, refl, end } /-- The symmetric square is equivalent to the second symmetric power. -/ def equiv_sym (α : Type*) : sym2 α ≃ sym α 2 := equiv.trans sym2_equiv_sym' (sym_equiv_sym' α 2).symm /-- The symmetric square is equivalent to multisets of cardinality two. (This is currently a synonym for `equiv_sym`, but it's provided in case the definition for `sym` changes.) -/ def equiv_multiset (α : Type*) : sym2 α ≃ {s : multiset α // s.card = 2} := equiv_sym α end sym_equiv section fintype /-- An algorithm for computing `sym2.rel`. -/ def rel_bool [decidable_eq α] (x y : α × α) : bool := if x.1 = y.1 then x.2 = y.2 else if x.1 = y.2 then x.2 = y.1 else ff lemma rel_bool_spec [decidable_eq α] (x y : α × α) : ↥(rel_bool x y) ↔ rel α x y := begin cases x with x₁ x₂, cases y with y₁ y₂, dsimp [rel_bool], split_ifs; simp only [false_iff, bool.coe_sort_ff, bool.of_to_bool_iff], rotate 2, { contrapose! h, cases h; cc }, all_goals { subst x₁, split; intro h1, { subst h1; apply sym2.rel.swap }, { cases h1; cc } } end /-- Given `[decidable_eq α]` and `[fintype α]`, the following instance gives `fintype (sym2 α)`. -/ instance (α : Type*) [decidable_eq α] : decidable_rel (sym2.rel α) := λ x y, decidable_of_bool (rel_bool x y) (rel_bool_spec x y) end fintype end sym2
665b8e8113b572ec492d4e595bb29a72bbfd69d1
30b012bb72d640ec30c8fdd4c45fdfa67beb012c
/data/nat/basic.lean
b65407642adaa88e41e078dac329fd66ac258099
[ "Apache-2.0" ]
permissive
kckennylau/mathlib
21fb810b701b10d6606d9002a4004f7672262e83
47b3477e20ffb5a06588dd3abb01fe0fe3205646
refs/heads/master
1,634,976,409,281
1,542,042,832,000
1,542,319,733,000
109,560,458
0
0
Apache-2.0
1,542,369,208,000
1,509,867,494,000
Lean
UTF-8
Lean
false
false
32,262
lean
/- Copyright (c) 2014 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Leonardo de Moura, Jeremy Avigad, Mario Carneiro Basic operations on the natural numbers. -/ import logic.basic algebra.ordered_ring data.option universes u v namespace nat variables {m n k : ℕ} theorem succ_inj' {n m : ℕ} : succ n = succ m ↔ n = m := ⟨succ_inj, congr_arg _⟩ theorem succ_le_succ_iff {m n : ℕ} : succ m ≤ succ n ↔ m ≤ n := ⟨le_of_succ_le_succ, succ_le_succ⟩ theorem lt_succ_iff {m n : ℕ} : m < succ n ↔ m ≤ n := succ_le_succ_iff lemma succ_le_iff {m n : ℕ} : succ m ≤ n ↔ m < n := ⟨lt_of_succ_le, succ_le_of_lt⟩ theorem pred_eq_of_eq_succ {m n : ℕ} (H : m = n.succ) : m.pred = n := by simp [H] theorem pred_sub (n m : ℕ) : pred n - m = pred (n - m) := by rw [← sub_one, nat.sub_sub, one_add]; refl lemma pred_eq_sub_one (n : ℕ) : pred n = n - 1 := rfl theorem pos_iff_ne_zero : n > 0 ↔ n ≠ 0 := ⟨ne_of_gt, nat.pos_of_ne_zero⟩ theorem pos_iff_ne_zero' : 0 < n ↔ n ≠ 0 := pos_iff_ne_zero theorem eq_of_lt_succ_of_not_lt {a b : ℕ} (h1 : a < b + 1) (h2 : ¬ a < b) : a = b := have h3 : a ≤ b, from le_of_lt_succ h1, or.elim (eq_or_lt_of_not_lt h2) (λ h, h) (λ h, absurd h (not_lt_of_ge h3)) protected theorem le_sub_add (n m : ℕ) : n ≤ n - m + m := or.elim (le_total n m) (assume : n ≤ m, begin rw [sub_eq_zero_of_le this, zero_add], exact this end) (assume : m ≤ n, begin rw (nat.sub_add_cancel this) end) theorem sub_add_eq_max (n m : ℕ) : n - m + m = max n m := eq_max (nat.le_sub_add _ _) (le_add_left _ _) $ λ k h₁ h₂, by rw ← nat.sub_add_cancel h₂; exact add_le_add_right (nat.sub_le_sub_right h₁ _) _ theorem sub_add_min (n m : ℕ) : n - m + min n m = n := (le_total n m).elim (λ h, by rw [min_eq_left h, sub_eq_zero_of_le h, zero_add]) (λ h, by rw [min_eq_right h, nat.sub_add_cancel h]) protected theorem add_sub_cancel' {n m : ℕ} (h : n ≥ m) : m + (n - m) = n := by rw [add_comm, nat.sub_add_cancel h] protected theorem sub_eq_of_eq_add (h : k = m + n) : k - m = n := begin rw [h, nat.add_sub_cancel_left] end theorem sub_min (n m : ℕ) : n - min n m = n - m := nat.sub_eq_of_eq_add $ by rw [add_comm, sub_add_min] protected theorem lt_of_sub_pos (h : n - m > 0) : m < n := lt_of_not_ge (assume : m ≥ n, have n - m = 0, from sub_eq_zero_of_le this, begin rw this at h, exact lt_irrefl _ h end) protected theorem lt_of_sub_lt_sub_right : m - k < n - k → m < n := lt_imp_lt_of_le_imp_le (λ h, nat.sub_le_sub_right h _) protected theorem lt_of_sub_lt_sub_left : m - n < m - k → k < n := lt_imp_lt_of_le_imp_le (nat.sub_le_sub_left _) protected theorem sub_lt_self (h₁ : m > 0) (h₂ : n > 0) : m - n < m := calc m - n = succ (pred m) - succ (pred n) : by rw [succ_pred_eq_of_pos h₁, succ_pred_eq_of_pos h₂] ... = pred m - pred n : by rw succ_sub_succ ... ≤ pred m : sub_le _ _ ... < succ (pred m) : lt_succ_self _ ... = m : succ_pred_eq_of_pos h₁ protected theorem le_sub_right_of_add_le (h : m + k ≤ n) : m ≤ n - k := by rw ← nat.add_sub_cancel m k; exact nat.sub_le_sub_right h k protected theorem le_sub_left_of_add_le (h : k + m ≤ n) : m ≤ n - k := nat.le_sub_right_of_add_le (by rwa add_comm at h) protected theorem lt_sub_right_of_add_lt (h : m + k < n) : m < n - k := lt_of_succ_le $ nat.le_sub_right_of_add_le $ by rw succ_add; exact succ_le_of_lt h protected theorem lt_sub_left_of_add_lt (h : k + m < n) : m < n - k := nat.lt_sub_right_of_add_lt (by rwa add_comm at h) protected theorem add_lt_of_lt_sub_right (h : m < n - k) : m + k < n := @nat.lt_of_sub_lt_sub_right _ _ k (by rwa nat.add_sub_cancel) protected theorem add_lt_of_lt_sub_left (h : m < n - k) : k + m < n := by rw add_comm; exact nat.add_lt_of_lt_sub_right h protected theorem le_add_of_sub_le_right : n - k ≤ m → n ≤ m + k := le_imp_le_of_lt_imp_lt nat.lt_sub_right_of_add_lt protected theorem le_add_of_sub_le_left : n - k ≤ m → n ≤ k + m := le_imp_le_of_lt_imp_lt nat.lt_sub_left_of_add_lt protected theorem lt_add_of_sub_lt_right : n - k < m → n < m + k := lt_imp_lt_of_le_imp_le nat.le_sub_right_of_add_le protected theorem lt_add_of_sub_lt_left : n - k < m → n < k + m := lt_imp_lt_of_le_imp_le nat.le_sub_left_of_add_le protected theorem sub_le_left_of_le_add : n ≤ k + m → n - k ≤ m := le_imp_le_of_lt_imp_lt nat.add_lt_of_lt_sub_left protected theorem sub_le_right_of_le_add : n ≤ m + k → n - k ≤ m := le_imp_le_of_lt_imp_lt nat.add_lt_of_lt_sub_right protected theorem sub_lt_left_iff_lt_add (H : n ≤ k) : k - n < m ↔ k < n + m := ⟨nat.lt_add_of_sub_lt_left, λ h₁, have succ k ≤ n + m, from succ_le_of_lt h₁, have succ (k - n) ≤ m, from calc succ (k - n) = succ k - n : by rw (succ_sub H) ... ≤ n + m - n : nat.sub_le_sub_right this n ... = m : by rw nat.add_sub_cancel_left, lt_of_succ_le this⟩ protected theorem le_sub_left_iff_add_le (H : m ≤ k) : n ≤ k - m ↔ m + n ≤ k := le_iff_le_iff_lt_iff_lt.2 (nat.sub_lt_left_iff_lt_add H) protected theorem le_sub_right_iff_add_le (H : n ≤ k) : m ≤ k - n ↔ m + n ≤ k := by rw [nat.le_sub_left_iff_add_le H, add_comm] protected theorem lt_sub_left_iff_add_lt : n < k - m ↔ m + n < k := ⟨nat.add_lt_of_lt_sub_left, nat.lt_sub_left_of_add_lt⟩ protected theorem lt_sub_right_iff_add_lt : m < k - n ↔ m + n < k := by rw [nat.lt_sub_left_iff_add_lt, add_comm] theorem sub_le_left_iff_le_add : m - n ≤ k ↔ m ≤ n + k := le_iff_le_iff_lt_iff_lt.2 nat.lt_sub_left_iff_add_lt theorem sub_le_right_iff_le_add : m - k ≤ n ↔ m ≤ n + k := by rw [nat.sub_le_left_iff_le_add, add_comm] protected theorem sub_lt_right_iff_lt_add (H : k ≤ m) : m - k < n ↔ m < n + k := by rw [nat.sub_lt_left_iff_lt_add H, add_comm] protected theorem sub_le_sub_left_iff (H : k ≤ m) : m - n ≤ m - k ↔ k ≤ n := ⟨λ h, have k + (m - k) - n ≤ m - k, by rwa nat.add_sub_cancel' H, nat.le_of_add_le_add_right (nat.le_add_of_sub_le_left this), nat.sub_le_sub_left _⟩ protected theorem sub_lt_sub_right_iff (H : k ≤ m) : m - k < n - k ↔ m < n := lt_iff_lt_of_le_iff_le (nat.sub_le_sub_right_iff _ _ _ H) protected theorem sub_lt_sub_left_iff (H : n ≤ m) : m - n < m - k ↔ k < n := lt_iff_lt_of_le_iff_le (nat.sub_le_sub_left_iff H) protected theorem sub_le_iff : m - n ≤ k ↔ m - k ≤ n := nat.sub_le_left_iff_le_add.trans nat.sub_le_right_iff_le_add.symm protected lemma sub_le_self (n m : ℕ) : n - m ≤ n := nat.sub_le_left_of_le_add (nat.le_add_left _ _) protected theorem sub_lt_iff (h₁ : n ≤ m) (h₂ : k ≤ m) : m - n < k ↔ m - k < n := (nat.sub_lt_left_iff_lt_add h₁).trans (nat.sub_lt_right_iff_lt_add h₂).symm lemma pred_le_iff {n m : ℕ} : pred n ≤ m ↔ n ≤ succ m := @nat.sub_le_right_iff_le_add n m 1 lemma lt_pred_iff {n m : ℕ} : n < pred m ↔ succ n < m := @nat.lt_sub_right_iff_add_lt n 1 m protected theorem mul_ne_zero {n m : ℕ} (n0 : n ≠ 0) (m0 : m ≠ 0) : n * m ≠ 0 | nm := (eq_zero_of_mul_eq_zero nm).elim n0 m0 @[simp] protected theorem mul_eq_zero {a b : ℕ} : a * b = 0 ↔ a = 0 ∨ b = 0 := iff.intro eq_zero_of_mul_eq_zero (by simp [or_imp_distrib] {contextual := tt}) @[simp] protected theorem zero_eq_mul {a b : ℕ} : 0 = a * b ↔ a = 0 ∨ b = 0 := by rw [eq_comm, nat.mul_eq_zero] @[elab_as_eliminator] protected def strong_rec' {p : ℕ → Sort u} (H : ∀ n, (∀ m, m < n → p m) → p n) : ∀ (n : ℕ), p n | n := H n (λ m hm, strong_rec' m) attribute [simp] nat.div_self protected lemma div_le_of_le_mul' {m n : ℕ} {k} (h : m ≤ k * n) : m / k ≤ n := (eq_zero_or_pos k).elim (λ k0, by rw [k0, nat.div_zero]; apply zero_le) (λ k0, (decidable.mul_le_mul_left k0).1 $ calc k * (m / k) ≤ m % k + k * (m / k) : le_add_left _ _ ... = m : mod_add_div _ _ ... ≤ k * n : h) protected lemma div_le_self' (m n : ℕ) : m / n ≤ m := (eq_zero_or_pos n).elim (λ n0, by rw [n0, nat.div_zero]; apply zero_le) (λ n0, nat.div_le_of_le_mul' $ calc m = 1 * m : (one_mul _).symm ... ≤ n * m : mul_le_mul_right _ n0) theorem le_div_iff_mul_le' {x y : ℕ} {k : ℕ} (k0 : 0 < k) : x ≤ y / k ↔ x * k ≤ y := begin revert x, refine nat.strong_rec' _ y, clear y, intros y IH x, cases decidable.lt_or_le y k with h h, { rw [div_eq_of_lt h], cases x with x, { simp [zero_mul, zero_le] }, { rw succ_mul, exact iff_of_false (not_succ_le_zero _) (not_le_of_lt $ lt_of_lt_of_le h (le_add_left _ _)) } }, { rw [div_eq_sub_div k0 h], cases x with x, { simp [zero_mul, zero_le] }, { rw [← add_one, nat.add_le_add_iff_le_right, succ_mul, IH _ (sub_lt_of_pos_le _ _ k0 h), add_le_to_le_sub _ h] } } end theorem div_mul_le_self' (m n : ℕ) : m / n * n ≤ m := (nat.eq_zero_or_pos n).elim (λ n0, by simp [n0, zero_le]) $ λ n0, (le_div_iff_mul_le' n0).1 (le_refl _) theorem div_lt_iff_lt_mul' {x y : ℕ} {k : ℕ} (k0 : 0 < k) : x / k < y ↔ x < y * k := lt_iff_lt_of_le_iff_le $ le_div_iff_mul_le' k0 protected theorem div_le_div_right {n m : ℕ} (h : n ≤ m) {k : ℕ} : n / k ≤ m / k := (nat.eq_zero_or_pos k).elim (λ k0, by simp [k0]) $ λ hk, (le_div_iff_mul_le' hk).2 $ le_trans (nat.div_mul_le_self' _ _) h protected theorem eq_mul_of_div_eq_right {a b c : ℕ} (H1 : b ∣ a) (H2 : a / b = c) : a = b * c := by rw [← H2, nat.mul_div_cancel' H1] protected theorem div_eq_iff_eq_mul_right {a b c : ℕ} (H : b > 0) (H' : b ∣ a) : a / b = c ↔ a = b * c := ⟨nat.eq_mul_of_div_eq_right H', nat.div_eq_of_eq_mul_right H⟩ protected theorem div_eq_iff_eq_mul_left {a b c : ℕ} (H : b > 0) (H' : b ∣ a) : a / b = c ↔ a = c * b := by rw mul_comm; exact nat.div_eq_iff_eq_mul_right H H' protected theorem eq_mul_of_div_eq_left {a b c : ℕ} (H1 : b ∣ a) (H2 : a / b = c) : a = c * b := by rw [mul_comm, nat.eq_mul_of_div_eq_right H1 H2] protected theorem mul_div_cancel_left' {a b : ℕ} (Hd : a ∣ b) : a * (b / a) = b := by rw [mul_comm,nat.div_mul_cancel Hd] protected theorem div_mod_unique {n k m d : ℕ} (h : 0 < k) : n / k = d ∧ n % k = m ↔ m + k * d = n ∧ m < k := ⟨λ ⟨e₁, e₂⟩, e₁ ▸ e₂ ▸ ⟨mod_add_div _ _, mod_lt _ h⟩, λ ⟨h₁, h₂⟩, h₁ ▸ by rw [add_mul_div_left _ _ h, add_mul_mod_self_left]; simp [div_eq_of_lt, mod_eq_of_lt, h₂]⟩ lemma two_mul_odd_div_two {n : ℕ} (hn : n % 2 = 1) : 2 * (n / 2) = n - 1 := by conv {to_rhs, rw [← nat.mod_add_div n 2, hn, nat.add_sub_cancel_left]} lemma div_dvd_of_dvd {a b : ℕ} (h : b ∣ a) : (a / b) ∣ a := ⟨b, (nat.div_mul_cancel h).symm⟩ protected lemma div_pos {a b : ℕ} (hba : b ≤ a) (hb : 0 < b) : 0 < a / b := nat.pos_of_ne_zero (λ h, lt_irrefl a (calc a = a % b : by simpa [h] using (mod_add_div a b).symm ... < b : nat.mod_lt a hb ... ≤ a : hba)) protected theorem mul_right_inj {a b c : ℕ} (ha : a > 0) : b * a = c * a ↔ b = c := ⟨nat.eq_of_mul_eq_mul_right ha, λ e, e ▸ rfl⟩ protected theorem mul_left_inj {a b c : ℕ} (ha : a > 0) : a * b = a * c ↔ b = c := ⟨nat.eq_of_mul_eq_mul_left ha, λ e, e ▸ rfl⟩ protected lemma div_div_self : ∀ {a b : ℕ}, b ∣ a → 0 < a → a / (a / b) = b | a 0 h₁ h₂ := by rw eq_zero_of_zero_dvd h₁; refl | 0 b h₁ h₂ := absurd h₂ dec_trivial | (a+1) (b+1) h₁ h₂ := (nat.mul_right_inj (nat.div_pos (le_of_dvd (succ_pos a) h₁) (succ_pos b))).1 $ by rw [nat.div_mul_cancel (div_dvd_of_dvd h₁), nat.mul_div_cancel' h₁] protected lemma div_lt_of_lt_mul {m n k : ℕ} (h : m < n * k) : m / n < k := lt_of_mul_lt_mul_left (calc n * (m / n) ≤ m % n + n * (m / n) : nat.le_add_left _ _ ... = m : mod_add_div _ _ ... < n * k : h) (nat.zero_le n) protected lemma div_eq_zero_iff {a b : ℕ} (hb : 0 < b) : a / b = 0 ↔ a < b := ⟨λ h, by rw [← mod_add_div a b, h, mul_zero, add_zero]; exact mod_lt _ hb, λ h, by rw [← nat.mul_left_inj hb, ← @add_left_cancel_iff _ _ (a % b), mod_add_div, mod_eq_of_lt h, mul_zero, add_zero]⟩ lemma mod_mul_right_div_self (a b c : ℕ) : a % (b * c) / b = (a / b) % c := if hb : b = 0 then by simp [hb] else if hc : c = 0 then by simp [hc] else by conv {to_rhs, rw ← mod_add_div a (b * c)}; rw [mul_assoc, nat.add_mul_div_left _ _ (nat.pos_of_ne_zero hb), add_mul_mod_self_left, mod_eq_of_lt (nat.div_lt_of_lt_mul (mod_lt _ (mul_pos (nat.pos_of_ne_zero hb) (nat.pos_of_ne_zero hc))))] lemma mod_mul_left_div_self (a b c : ℕ) : a % (c * b) / b = (a / b) % c := by rw [mul_comm c, mod_mul_right_div_self] @[simp] protected theorem dvd_one {n : ℕ} : n ∣ 1 ↔ n = 1 := ⟨eq_one_of_dvd_one, λ e, e.symm ▸ dvd_refl _⟩ protected theorem dvd_add_left {k m n : ℕ} (h : k ∣ n) : k ∣ m + n ↔ k ∣ m := (nat.dvd_add_iff_left h).symm protected theorem dvd_add_right {k m n : ℕ} (h : k ∣ m) : k ∣ m + n ↔ k ∣ n := (nat.dvd_add_iff_right h).symm protected theorem mul_dvd_mul_iff_left {a b c : ℕ} (ha : a > 0) : a * b ∣ a * c ↔ b ∣ c := exists_congr $ λ d, by rw [mul_assoc, nat.mul_left_inj ha] protected theorem mul_dvd_mul_iff_right {a b c : ℕ} (hc : c > 0) : a * c ∣ b * c ↔ a ∣ b := exists_congr $ λ d, by rw [mul_right_comm, nat.mul_right_inj hc] @[simp] theorem mod_mod (a n : ℕ) : (a % n) % n = a % n := (eq_zero_or_pos n).elim (λ n0, by simp [n0]) (λ npos, mod_eq_of_lt (mod_lt _ npos)) theorem add_pos_left {m : ℕ} (h : m > 0) (n : ℕ) : m + n > 0 := calc m + n > 0 + n : nat.add_lt_add_right h n ... = n : nat.zero_add n ... ≥ 0 : zero_le n theorem add_pos_right (m : ℕ) {n : ℕ} (h : n > 0) : m + n > 0 := begin rw add_comm, exact add_pos_left h m end theorem add_pos_iff_pos_or_pos (m n : ℕ) : m + n > 0 ↔ m > 0 ∨ n > 0 := iff.intro begin intro h, cases m with m, {simp [zero_add] at h, exact or.inr h}, exact or.inl (succ_pos _) end begin intro h, cases h with mpos npos, { apply add_pos_left mpos }, apply add_pos_right _ npos end lemma lt_succ_iff_lt_or_eq {n i : ℕ} : n < i.succ ↔ (n < i ∨ n = i) := lt_succ_iff.trans le_iff_lt_or_eq theorem le_zero_iff {i : ℕ} : i ≤ 0 ↔ i = 0 := ⟨nat.eq_zero_of_le_zero, assume h, h ▸ le_refl i⟩ theorem le_add_one_iff {i j : ℕ} : i ≤ j + 1 ↔ (i ≤ j ∨ i = j + 1) := ⟨assume h, match nat.eq_or_lt_of_le h with | or.inl h := or.inr h | or.inr h := or.inl $ nat.le_of_succ_le_succ h end, or.rec (assume h, le_trans h $ nat.le_add_right _ _) le_of_eq⟩ theorem mul_self_inj {n m : ℕ} : n * n = m * m ↔ n = m := le_antisymm_iff.trans (le_antisymm_iff.trans (and_congr mul_self_le_mul_self_iff mul_self_le_mul_self_iff)).symm instance decidable_ball_lt (n : nat) (P : Π k < n, Prop) : ∀ [H : ∀ n h, decidable (P n h)], decidable (∀ n h, P n h) := begin induction n with n IH; intro; resetI, { exact is_true (λ n, dec_trivial) }, cases IH (λ k h, P k (lt_succ_of_lt h)) with h, { refine is_false (mt _ h), intros hn k h, apply hn }, by_cases p : P n (lt_succ_self n), { exact is_true (λ k h', (lt_or_eq_of_le $ le_of_lt_succ h').elim (h _) (λ e, match k, e, h' with _, rfl, h := p end)) }, { exact is_false (mt (λ hn, hn _ _) p) } end instance decidable_forall_fin {n : ℕ} (P : fin n → Prop) [H : decidable_pred P] : decidable (∀ i, P i) := decidable_of_iff (∀ k h, P ⟨k, h⟩) ⟨λ a ⟨k, h⟩, a k h, λ a k h, a ⟨k, h⟩⟩ instance decidable_ball_le (n : ℕ) (P : Π k ≤ n, Prop) [H : ∀ n h, decidable (P n h)] : decidable (∀ n h, P n h) := decidable_of_iff (∀ k (h : k < succ n), P k (le_of_lt_succ h)) ⟨λ a k h, a k (lt_succ_of_le h), λ a k h, a k _⟩ instance decidable_lo_hi (lo hi : ℕ) (P : ℕ → Prop) [H : decidable_pred P] : decidable (∀x, lo ≤ x → x < hi → P x) := decidable_of_iff (∀ x < hi - lo, P (lo + x)) ⟨λal x hl hh, by have := al (x - lo) (lt_of_not_ge $ (not_congr (nat.sub_le_sub_right_iff _ _ _ hl)).2 $ not_le_of_gt hh); rwa [nat.add_sub_of_le hl] at this, λal x h, al _ (nat.le_add_right _ _) (nat.add_lt_of_lt_sub_left h)⟩ instance decidable_lo_hi_le (lo hi : ℕ) (P : ℕ → Prop) [H : decidable_pred P] : decidable (∀x, lo ≤ x → x ≤ hi → P x) := decidable_of_iff (∀x, lo ≤ x → x < hi + 1 → P x) $ ball_congr $ λ x hl, imp_congr lt_succ_iff iff.rfl protected theorem bit0_le {n m : ℕ} (h : n ≤ m) : bit0 n ≤ bit0 m := add_le_add h h protected theorem bit1_le {n m : ℕ} (h : n ≤ m) : bit1 n ≤ bit1 m := succ_le_succ (add_le_add h h) theorem bit_le : ∀ (b : bool) {n m : ℕ}, n ≤ m → bit b n ≤ bit b m | tt n m h := nat.bit1_le h | ff n m h := nat.bit0_le h theorem bit_ne_zero (b) {n} (h : n ≠ 0) : bit b n ≠ 0 := by cases b; [exact nat.bit0_ne_zero h, exact nat.bit1_ne_zero _] theorem bit0_le_bit : ∀ (b) {m n : ℕ}, m ≤ n → bit0 m ≤ bit b n | tt m n h := le_of_lt $ nat.bit0_lt_bit1 h | ff m n h := nat.bit0_le h theorem bit_le_bit1 : ∀ (b) {m n : ℕ}, m ≤ n → bit b m ≤ bit1 n | ff m n h := le_of_lt $ nat.bit0_lt_bit1 h | tt m n h := nat.bit1_le h theorem bit_lt_bit0 : ∀ (b) {n m : ℕ}, n < m → bit b n < bit0 m | tt n m h := nat.bit1_lt_bit0 h | ff n m h := nat.bit0_lt h theorem bit_lt_bit (a b) {n m : ℕ} (h : n < m) : bit a n < bit b m := lt_of_lt_of_le (bit_lt_bit0 _ h) (bit0_le_bit _ (le_refl _)) /- partial subtraction -/ /-- Partial predecessor operation. Returns `ppred n = some m` if `n = m + 1`, otherwise `none`. -/ @[simp] def ppred : ℕ → option ℕ | 0 := none | (n+1) := some n /-- Partial subtraction operation. Returns `psub m n = some k` if `m = n + k`, otherwise `none`. -/ @[simp] def psub (m : ℕ) : ℕ → option ℕ | 0 := some m | (n+1) := psub n >>= ppred theorem pred_eq_ppred (n : ℕ) : pred n = (ppred n).get_or_else 0 := by cases n; refl theorem sub_eq_psub (m : ℕ) : ∀ n, m - n = (psub m n).get_or_else 0 | 0 := rfl | (n+1) := (pred_eq_ppred (m-n)).trans $ by rw [sub_eq_psub, psub]; cases psub m n; refl @[simp] theorem ppred_eq_some {m : ℕ} : ∀ {n}, ppred n = some m ↔ succ m = n | 0 := by split; intro h; contradiction | (n+1) := by dsimp; split; intro h; injection h; subst n @[simp] theorem ppred_eq_none : ∀ {n : ℕ}, ppred n = none ↔ n = 0 | 0 := by simp | (n+1) := by dsimp; split; contradiction theorem psub_eq_some {m : ℕ} : ∀ {n k}, psub m n = some k ↔ k + n = m | 0 k := by simp [eq_comm] | (n+1) k := by dsimp; apply option.bind_eq_some.trans; simp [psub_eq_some] theorem psub_eq_none (m n : ℕ) : psub m n = none ↔ m < n := begin cases s : psub m n; simp [eq_comm], { show m < n, refine lt_of_not_ge (λ h, _), cases le.dest h with k e, injection s.symm.trans (psub_eq_some.2 $ (add_comm _ _).trans e) }, { show n ≤ m, rw ← psub_eq_some.1 s, apply le_add_left } end theorem ppred_eq_pred {n} (h : 0 < n) : ppred n = some (pred n) := ppred_eq_some.2 $ succ_pred_eq_of_pos h theorem psub_eq_sub {m n} (h : n ≤ m) : psub m n = some (m - n) := psub_eq_some.2 $ nat.sub_add_cancel h theorem psub_add (m n k) : psub m (n + k) = do x ← psub m n, psub x k := by induction k; simp [*, add_succ, bind_assoc] /- pow -/ attribute [simp] nat.pow_zero nat.pow_one @[simp] lemma one_pow : ∀ n : ℕ, 1 ^ n = 1 | 0 := rfl | (k+1) := show 1^k * 1 = 1, by rw [mul_one, one_pow] theorem pow_add (a m n : ℕ) : a^(m + n) = a^m * a^n := by induction n; simp [*, pow_succ, mul_assoc] theorem pow_two (a : ℕ) : a ^ 2 = a * a := show (1 * a) * a = _, by rw one_mul theorem pow_dvd_pow (a : ℕ) {m n : ℕ} (h : m ≤ n) : a^m ∣ a^n := by rw [← nat.add_sub_cancel' h, pow_add]; apply dvd_mul_right theorem pow_dvd_pow_of_dvd {a b : ℕ} (h : a ∣ b) : ∀ n:ℕ, a^n ∣ b^n | 0 := dvd_refl _ | (n+1) := mul_dvd_mul (pow_dvd_pow_of_dvd n) h theorem mul_pow (a b n : ℕ) : (a * b) ^ n = a ^ n * b ^ n := by induction n; simp [*, nat.pow_succ, mul_comm, mul_assoc, mul_left_comm] protected theorem pow_mul (a b n : ℕ) : n ^ (a * b) = (n ^ a) ^ b := by induction b; simp [*, nat.succ_eq_add_one, nat.pow_add, mul_add, mul_comm] theorem pow_pos {p : ℕ} (hp : p > 0) : ∀ n : ℕ, p ^ n > 0 | 0 := by simpa using zero_lt_one | (k+1) := mul_pos (pow_pos _) hp lemma pow_eq_mul_pow_sub (p : ℕ) {m n : ℕ} (h : m ≤ n) : p ^ m * p ^ (n - m) = p ^ n := by rw [←nat.pow_add, nat.add_sub_cancel' h] lemma pow_lt_pow_succ {p : ℕ} (h : p > 1) (n : ℕ) : p^n < p^(n+1) := suffices p^n*1 < p^n*p, by simpa, nat.mul_lt_mul_of_pos_left h (nat.pow_pos (lt_of_succ_lt h) n) lemma lt_pow_self {p : ℕ} (h : p > 1) : ∀ n : ℕ, n < p ^ n | 0 := by simp [zero_lt_one] | (n+1) := calc n + 1 < p^n + 1 : nat.add_lt_add_right (lt_pow_self _) _ ... ≤ p ^ (n+1) : pow_lt_pow_succ h _ lemma not_pos_pow_dvd : ∀ {p k : ℕ} (hp : p > 1) (hk : k > 1), ¬ p^k ∣ p | (succ p) (succ k) hp hk h := have (succ p)^k * succ p ∣ 1 * succ p, by simpa, have (succ p) ^ k ∣ 1, from dvd_of_mul_dvd_mul_right (succ_pos _) this, have he : (succ p) ^ k = 1, from eq_one_of_dvd_one this, have k < (succ p) ^ k, from lt_pow_self hp k, have k < 1, by rwa [he] at this, have k = 0, from eq_zero_of_le_zero $ le_of_lt_succ this, have 1 > 1, by rwa [this] at hk, absurd this dec_trivial @[simp] theorem bodd_div2_eq (n : ℕ) : bodd_div2 n = (bodd n, div2 n) := by unfold bodd div2; cases bodd_div2 n; refl @[simp] lemma bodd_bit0 (n) : bodd (bit0 n) = ff := bodd_bit ff n @[simp] lemma bodd_bit1 (n) : bodd (bit1 n) = tt := bodd_bit tt n @[simp] lemma div2_bit0 (n) : div2 (bit0 n) = n := div2_bit ff n @[simp] lemma div2_bit1 (n) : div2 (bit1 n) = n := div2_bit tt n /- iterate -/ section variables {α : Sort*} (op : α → α) @[simp] theorem iterate_zero (a : α) : op^[0] a = a := rfl @[simp] theorem iterate_succ (n : ℕ) (a : α) : op^[succ n] a = (op^[n]) (op a) := rfl theorem iterate_add : ∀ (m n : ℕ) (a : α), op^[m + n] a = (op^[m]) (op^[n] a) | m 0 a := rfl | m (succ n) a := iterate_add m n _ theorem iterate_succ' (n : ℕ) (a : α) : op^[succ n] a = op (op^[n] a) := by rw [← one_add, iterate_add]; refl theorem iterate₀ {α : Type u} {op : α → α} {x : α} (H : op x = x) {n : ℕ} : op^[n] x = x := by induction n; [simp only [iterate_zero], simp only [iterate_succ', H, *]] theorem iterate₁ {α : Type u} {β : Type v} {op : α → α} {op' : β → β} {op'' : α → β} (H : ∀ x, op' (op'' x) = op'' (op x)) {n : ℕ} {x : α} : op'^[n] (op'' x) = op'' (op^[n] x) := by induction n; [simp only [iterate_zero], simp only [iterate_succ', H, *]] theorem iterate₂ {α : Type u} {op : α → α} {op' : α → α → α} (H : ∀ x y, op (op' x y) = op' (op x) (op y)) {n : ℕ} {x y : α} : op^[n] (op' x y) = op' (op^[n] x) (op^[n] y) := by induction n; [simp only [iterate_zero], simp only [iterate_succ', H, *]] theorem iterate_cancel {α : Type u} {op op' : α → α} (H : ∀ x, op (op' x) = x) {n : ℕ} {x : α} : op^[n] (op'^[n] x) = x := by induction n; [refl, rwa [iterate_succ, iterate_succ', H]] theorem iterate_inj {α : Type u} {op : α → α} (Hinj : function.injective op) (n : ℕ) (x y : α) (H : (op^[n] x) = (op^[n] y)) : x = y := by induction n with n ih; simp only [iterate_zero, iterate_succ'] at H; [exact H, exact ih (Hinj H)] end /- size and shift -/ theorem shiftl'_ne_zero_left (b) {m} (h : m ≠ 0) (n) : shiftl' b m n ≠ 0 := by induction n; simp [shiftl', bit_ne_zero, *] theorem shiftl'_tt_ne_zero (m) : ∀ {n} (h : n ≠ 0), shiftl' tt m n ≠ 0 | 0 h := absurd rfl h | (succ n) _ := nat.bit1_ne_zero _ @[simp] theorem size_zero : size 0 = 0 := rfl @[simp] theorem size_bit {b n} (h : bit b n ≠ 0) : size (bit b n) = succ (size n) := begin rw size, conv { to_lhs, rw [binary_rec], simp [h] }, rw div2_bit, refl end @[simp] theorem size_bit0 {n} (h : n ≠ 0) : size (bit0 n) = succ (size n) := @size_bit ff n (nat.bit0_ne_zero h) @[simp] theorem size_bit1 (n) : size (bit1 n) = succ (size n) := @size_bit tt n (nat.bit1_ne_zero n) @[simp] theorem size_one : size 1 = 1 := by apply size_bit1 0 @[simp] theorem size_shiftl' {b m n} (h : shiftl' b m n ≠ 0) : size (shiftl' b m n) = size m + n := begin induction n with n IH; simp [shiftl'] at h ⊢, rw [size_bit h, nat.add_succ], by_cases s0 : shiftl' b m n = 0; [skip, rw [IH s0]], rw s0 at h ⊢, cases b, {exact absurd rfl h}, have : shiftl' tt m n + 1 = 1 := congr_arg (+1) s0, rw [shiftl'_tt_eq_mul_pow] at this, have m0 := succ_inj (eq_one_of_dvd_one ⟨_, this.symm⟩), subst m0, simp at this, have : n = 0 := eq_zero_of_le_zero (le_of_not_gt $ λ hn, ne_of_gt (pow_lt_pow_of_lt_right dec_trivial hn) this), subst n, refl end @[simp] theorem size_shiftl {m} (h : m ≠ 0) (n) : size (shiftl m n) = size m + n := size_shiftl' (shiftl'_ne_zero_left _ h _) theorem lt_size_self (n : ℕ) : n < 2^size n := begin rw [← one_shiftl], have : ∀ {n}, n = 0 → n < shiftl 1 (size n) := λ n e, by subst e; exact dec_trivial, apply binary_rec _ _ n, {apply this rfl}, intros b n IH, by_cases bit b n = 0, {apply this h}, rw [size_bit h, shiftl_succ], exact bit_lt_bit0 _ IH end theorem size_le {m n : ℕ} : size m ≤ n ↔ m < 2^n := ⟨λ h, lt_of_lt_of_le (lt_size_self _) (pow_le_pow_of_le_right dec_trivial h), begin rw [← one_shiftl], revert n, apply binary_rec _ _ m, { intros n h, apply zero_le }, { intros b m IH n h, by_cases e : bit b m = 0, { rw e, apply zero_le }, rw [size_bit e], cases n with n, { exact e.elim (eq_zero_of_le_zero (le_of_lt_succ h)) }, { apply succ_le_succ (IH _), apply lt_imp_lt_of_le_imp_le (λ h', bit0_le_bit _ h') h } } end⟩ theorem lt_size {m n : ℕ} : m < size n ↔ 2^m ≤ n := by rw [← not_lt, iff_not_comm, not_lt, size_le] theorem size_pos {n : ℕ} : 0 < size n ↔ 0 < n := by rw lt_size; refl theorem size_eq_zero {n : ℕ} : size n = 0 ↔ n = 0 := by have := @size_pos n; simp [pos_iff_ne_zero'] at this; exact not_iff_not.1 this theorem size_pow {n : ℕ} : size (2^n) = n+1 := le_antisymm (size_le.2 $ pow_lt_pow_of_lt_right dec_trivial (lt_succ_self _)) (lt_size.2 $ le_refl _) theorem size_le_size {m n : ℕ} (h : m ≤ n) : size m ≤ size n := size_le.2 $ lt_of_le_of_lt h (lt_size_self _) /- factorial -/ /-- `fact n` is the factorial of `n`. -/ @[simp] def fact : nat → nat | 0 := 1 | (succ n) := succ n * fact n @[simp] theorem fact_zero : fact 0 = 1 := rfl @[simp] theorem fact_one : fact 1 = 1 := rfl @[simp] theorem fact_succ (n) : fact (succ n) = succ n * fact n := rfl theorem fact_pos : ∀ n, fact n > 0 | 0 := zero_lt_one | (succ n) := mul_pos (succ_pos _) (fact_pos n) theorem fact_ne_zero (n : ℕ) : fact n ≠ 0 := ne_of_gt (fact_pos _) theorem fact_dvd_fact {m n} (h : m ≤ n) : fact m ∣ fact n := begin induction n with n IH; simp, { have := eq_zero_of_le_zero h, subst m, simp }, { cases eq_or_lt_of_le h with he hl, { subst m, simp }, { apply dvd_mul_of_dvd_right (IH (le_of_lt_succ hl)) } } end theorem dvd_fact : ∀ {m n}, m > 0 → m ≤ n → m ∣ fact n | (succ m) n _ h := dvd_of_mul_right_dvd (fact_dvd_fact h) theorem fact_le {m n} (h : m ≤ n) : fact m ≤ fact n := le_of_dvd (fact_pos _) (fact_dvd_fact h) lemma fact_mul_pow_le_fact : ∀ {m n : ℕ}, m.fact * m.succ ^ n ≤ (m + n).fact | m 0 := by simp | m (n+1) := by rw [← add_assoc, nat.fact_succ, mul_comm (nat.succ _), nat.pow_succ, ← mul_assoc]; exact mul_le_mul fact_mul_pow_le_fact (nat.succ_le_succ (nat.le_add_right _ _)) (nat.zero_le _) (nat.zero_le _) section find_greatest /-- `find_greatest P b` is the largest `i ≤ bound` such that `P i` holds, or `0` if no such `i` exists -/ protected def find_greatest (P : ℕ → Prop) [decidable_pred P] : ℕ → ℕ | 0 := 0 | (n + 1) := if P (n + 1) then n + 1 else find_greatest n variables {P : ℕ → Prop} [decidable_pred P] @[simp] lemma find_greatest_zero : nat.find_greatest P 0 = 0 := rfl @[simp] lemma find_greatest_eq : ∀{b}, P b → nat.find_greatest P b = b | 0 h := rfl | (n + 1) h := by simp [nat.find_greatest, h] @[simp] lemma find_greatest_of_not {b} (h : ¬ P (b + 1)) : nat.find_greatest P (b + 1) = nat.find_greatest P b := by simp [nat.find_greatest, h] lemma find_greatest_spec_and_le : ∀{b m}, m ≤ b → P m → P (nat.find_greatest P b) ∧ m ≤ nat.find_greatest P b | 0 m hm hP := have m = 0, from le_antisymm hm (nat.zero_le _), show P 0 ∧ m ≤ 0, from this ▸ ⟨hP, le_refl _⟩ | (b + 1) m hm hP := begin by_cases h : P (b + 1), { simp [h, hm] }, { have : m ≠ b + 1 := assume this, h $ this ▸ hP, have : m ≤ b := (le_of_not_gt $ assume h : b + 1 ≤ m, this $ le_antisymm hm h), have : P (nat.find_greatest P b) ∧ m ≤ nat.find_greatest P b := find_greatest_spec_and_le this hP, simp [h, this] } end lemma find_greatest_spec {b} : (∃m, m ≤ b ∧ P m) → P (nat.find_greatest P b) | ⟨m, hmb, hm⟩ := (find_greatest_spec_and_le hmb hm).1 lemma find_greatest_le : ∀ {b}, nat.find_greatest P b ≤ b | 0 := le_refl _ | (b + 1) := have nat.find_greatest P b ≤ b + 1, from le_trans find_greatest_le (nat.le_succ b), by by_cases P (b + 1); simp [h, this] lemma le_find_greatest {b m} (hmb : m ≤ b) (hm : P m) : m ≤ nat.find_greatest P b := (find_greatest_spec_and_le hmb hm).2 lemma find_greatest_is_greatest {P : ℕ → Prop} [decidable_pred P] {b} : (∃ m, m ≤ b ∧ P m) → ∀ k, nat.find_greatest P b < k ∧ k ≤ b → ¬ P k | ⟨m, hmb, hP⟩ k ⟨hk, hkb⟩ hPk := lt_irrefl k $ lt_of_le_of_lt (le_find_greatest hkb hPk) hk end find_greatest section div lemma dvd_div_of_mul_dvd {a b c : ℕ} (h : a * b ∣ c) : b ∣ c / a := if ha : a = 0 then by simp [ha] else have ha : a > 0, from nat.pos_of_ne_zero ha, have h1 : ∃ d, c = a * b * d, from h, let ⟨d, hd⟩ := h1 in have hac : a ∣ c, from dvd_of_mul_right_dvd h, have h2 : c / a = b * d, from nat.div_eq_of_eq_mul_right ha (by simpa [mul_assoc] using hd), show ∃ d, c / a = b * d, from ⟨d, h2⟩ lemma mul_dvd_of_dvd_div {a b c : ℕ} (hab : c ∣ b) (h : a ∣ b / c) : c * a ∣ b := have h1 : ∃ d, b / c = a * d, from h, have h2 : ∃ e, b = c * e, from hab, let ⟨d, hd⟩ := h1, ⟨e, he⟩ := h2 in have h3 : b = a * d * c, from nat.eq_mul_of_div_eq_left hab hd, show ∃ d, b = c * a * d, from ⟨d, by cc⟩ lemma div_mul_div {a b c d : ℕ} (hab : b ∣ a) (hcd : d ∣ c) : (a / b) * (c / d) = (a * c) / (b * d) := have exi1 : ∃ x, a = b * x, from hab, have exi2 : ∃ y, c = d * y, from hcd, if hb : b = 0 then by simp [hb] else have b > 0, from nat.pos_of_ne_zero hb, if hd : d = 0 then by simp [hd] else have d > 0, from nat.pos_of_ne_zero hd, begin cases exi1 with x hx, cases exi2 with y hy, rw [hx, hy, nat.mul_div_cancel_left, nat.mul_div_cancel_left], symmetry, apply nat.div_eq_of_eq_mul_left, apply mul_pos, repeat {assumption}, cc end lemma pow_dvd_of_le_of_pow_dvd {p m n k : ℕ} (hmn : m ≤ n) (hdiv : p ^ n ∣ k) : p ^ m ∣ k := have p ^ m ∣ p ^ n, from pow_dvd_pow _ hmn, dvd_trans this hdiv lemma dvd_of_pow_dvd {p k m : ℕ} (hk : 1 ≤ k) (hpk : p^k ∣ m) : p ∣ m := by rw ←nat.pow_one p; exact pow_dvd_of_le_of_pow_dvd hk hpk end div lemma exists_eq_add_of_le : ∀ {m n : ℕ}, m ≤ n → ∃ k : ℕ, n = m + k | 0 0 h := ⟨0, by simp⟩ | 0 (n+1) h := ⟨n+1, by simp⟩ | (m+1) (n+1) h := let ⟨k, hk⟩ := exists_eq_add_of_le (nat.le_of_succ_le_succ h) in ⟨k, by simp [hk]⟩ lemma exists_eq_add_of_lt : ∀ {m n : ℕ}, m < n → ∃ k : ℕ, n = m + k + 1 | 0 0 h := false.elim $ lt_irrefl _ h | 0 (n+1) h := ⟨n, by simp⟩ | (m+1) (n+1) h := let ⟨k, hk⟩ := exists_eq_add_of_le (nat.le_of_succ_le_succ h) in ⟨k, by simp [hk]⟩ end nat
2afbfb98d7d3e370bcd787704a7a01e5a0076a31
19cc34575500ee2e3d4586c15544632aa07a8e66
/src/data/option/basic.lean
50fb3cbe6a464301ff3aa7fe785c4d2c0fb3699f
[ "Apache-2.0" ]
permissive
LibertasSpZ/mathlib
b9fcd46625eb940611adb5e719a4b554138dade6
33f7870a49d7cc06d2f3036e22543e6ec5046e68
refs/heads/master
1,672,066,539,347
1,602,429,158,000
1,602,429,158,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
7,724
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import tactic.basic namespace option variables {α : Type*} {β : Type*} {γ : Type*} lemma coe_def : (coe : α → option α) = some := rfl lemma some_ne_none (x : α) : some x ≠ none := λ h, option.no_confusion h @[simp] theorem get_mem : ∀ {o : option α} (h : is_some o), option.get h ∈ o | (some a) _ := rfl theorem get_of_mem {a : α} : ∀ {o : option α} (h : is_some o), a ∈ o → option.get h = a | _ _ rfl := rfl @[simp] lemma not_mem_none (a : α) : a ∉ (none : option α) := λ h, option.no_confusion h @[simp] lemma some_get : ∀ {x : option α} (h : is_some x), some (option.get h) = x | (some x) hx := rfl @[simp] lemma get_some (x : α) (h : is_some (some x)) : option.get h = x := rfl @[simp] lemma get_or_else_some (x y : α) : option.get_or_else (some x) y = x := rfl lemma get_or_else_of_ne_none {x : option α} (hx : x ≠ none) (y : α) : some (x.get_or_else y) = x := by cases x; [contradiction, rw get_or_else_some] theorem mem_unique {o : option α} {a b : α} (ha : a ∈ o) (hb : b ∈ o) : a = b := option.some.inj $ ha.symm.trans hb theorem some_injective (α : Type*) : function.injective (@some α) := λ _ _, some_inj.mp /-- `option.map f` is injective if `f` is injective. -/ theorem map_injective {f : α → β} (Hf : function.injective f) : function.injective (option.map f) | none none H := rfl | (some a₁) (some a₂) H := by rw Hf (option.some.inj H) @[ext] theorem ext : ∀ {o₁ o₂ : option α}, (∀ a, a ∈ o₁ ↔ a ∈ o₂) → o₁ = o₂ | none none H := rfl | (some a) o H := ((H _).1 rfl).symm | o (some b) H := (H _).2 rfl theorem eq_none_iff_forall_not_mem {o : option α} : o = none ↔ (∀ a, a ∉ o) := ⟨λ e a h, by rw e at h; cases h, λ h, ext $ by simpa⟩ @[simp] theorem none_bind {α β} (f : α → option β) : none >>= f = none := rfl @[simp] theorem some_bind {α β} (a : α) (f : α → option β) : some a >>= f = f a := rfl @[simp] theorem none_bind' (f : α → option β) : none.bind f = none := rfl @[simp] theorem some_bind' (a : α) (f : α → option β) : (some a).bind f = f a := rfl @[simp] theorem bind_some : ∀ x : option α, x >>= some = x := @bind_pure α option _ _ @[simp] theorem bind_eq_some {α β} {x : option α} {f : α → option β} {b : β} : x >>= f = some b ↔ ∃ a, x = some a ∧ f a = some b := by cases x; simp @[simp] theorem bind_eq_some' {x : option α} {f : α → option β} {b : β} : x.bind f = some b ↔ ∃ a, x = some a ∧ f a = some b := by cases x; simp @[simp] theorem bind_eq_none' {o : option α} {f : α → option β} : o.bind f = none ↔ (∀ b a, a ∈ o → b ∉ f a) := by simp only [eq_none_iff_forall_not_mem, not_exists, not_and, mem_def, bind_eq_some'] @[simp] theorem bind_eq_none {α β} {o : option α} {f : α → option β} : o >>= f = none ↔ (∀ b a, a ∈ o → b ∉ f a) := bind_eq_none' lemma bind_comm {α β γ} {f : α → β → option γ} (a : option α) (b : option β) : a.bind (λx, b.bind (f x)) = b.bind (λy, a.bind (λx, f x y)) := by cases a; cases b; refl lemma bind_assoc (x : option α) (f : α → option β) (g : β → option γ) : (x.bind f).bind g = x.bind (λ y, (f y).bind g) := by cases x; refl @[simp] theorem map_none {α β} {f : α → β} : f <$> none = none := rfl @[simp] theorem map_some {α β} {a : α} {f : α → β} : f <$> some a = some (f a) := rfl @[simp] theorem map_none' {f : α → β} : option.map f none = none := rfl @[simp] theorem map_some' {a : α} {f : α → β} : option.map f (some a) = some (f a) := rfl @[simp] theorem map_eq_some {α β} {x : option α} {f : α → β} {b : β} : f <$> x = some b ↔ ∃ a, x = some a ∧ f a = b := by cases x; simp @[simp] theorem map_eq_some' {x : option α} {f : α → β} {b : β} : x.map f = some b ↔ ∃ a, x = some a ∧ f a = b := by cases x; simp @[simp] theorem map_id' : option.map (@id α) = id := map_id @[simp] theorem seq_some {α β} {a : α} {f : α → β} : some f <*> some a = some (f a) := rfl @[simp] theorem some_orelse' (a : α) (x : option α) : (some a).orelse x = some a := rfl @[simp] theorem some_orelse (a : α) (x : option α) : (some a <|> x) = some a := rfl @[simp] theorem none_orelse' (x : option α) : none.orelse x = x := by cases x; refl @[simp] theorem none_orelse (x : option α) : (none <|> x) = x := none_orelse' x @[simp] theorem orelse_none' (x : option α) : x.orelse none = x := by cases x; refl @[simp] theorem orelse_none (x : option α) : (x <|> none) = x := orelse_none' x @[simp] theorem is_some_none : @is_some α none = ff := rfl @[simp] theorem is_some_some {a : α} : is_some (some a) = tt := rfl theorem is_some_iff_exists {x : option α} : is_some x ↔ ∃ a, x = some a := by cases x; simp [is_some]; exact ⟨_, rfl⟩ @[simp] theorem is_none_none : @is_none α none = tt := rfl @[simp] theorem is_none_some {a : α} : is_none (some a) = ff := rfl @[simp] theorem not_is_some {a : option α} : is_some a = ff ↔ a.is_none = tt := by cases a; simp lemma eq_some_iff_get_eq {o : option α} {a : α} : o = some a ↔ ∃ h : o.is_some, option.get h = a := by cases o; simp lemma not_is_some_iff_eq_none {o : option α} : ¬o.is_some ↔ o = none := by cases o; simp lemma ne_none_iff_is_some {o : option α} : o ≠ none ↔ o.is_some := by cases o; simp lemma ne_none_iff_exists {o : option α} : o ≠ none ↔ ∃ (x : α), some x = o := by {cases o; simp} lemma ne_none_iff_exists' {o : option α} : o ≠ none ↔ ∃ (x : α), o = some x := ne_none_iff_exists.trans $ exists_congr $ λ _, eq_comm lemma bex_ne_none {p : option α → Prop} : (∃ x ≠ none, p x) ↔ ∃ x, p (some x) := ⟨λ ⟨x, hx, hp⟩, ⟨get $ ne_none_iff_is_some.1 hx, by rwa [some_get]⟩, λ ⟨x, hx⟩, ⟨some x, some_ne_none x, hx⟩⟩ lemma ball_ne_none {p : option α → Prop} : (∀ x ≠ none, p x) ↔ ∀ x, p (some x) := ⟨λ h x, h (some x) (some_ne_none x), λ h x hx, by simpa only [some_get] using h (get $ ne_none_iff_is_some.1 hx)⟩ theorem iget_mem [inhabited α] : ∀ {o : option α}, is_some o → o.iget ∈ o | (some a) _ := rfl theorem iget_of_mem [inhabited α] {a : α} : ∀ {o : option α}, a ∈ o → o.iget = a | _ rfl := rfl @[simp] theorem guard_eq_some {p : α → Prop} [decidable_pred p] {a b : α} : guard p a = some b ↔ a = b ∧ p a := by by_cases p a; simp [option.guard, h]; intro; contradiction @[simp] theorem guard_eq_some' {p : Prop} [decidable p] : ∀ u, _root_.guard p = some u ↔ p | () := by by_cases p; simp [guard, h, pure]; intro; contradiction theorem lift_or_get_choice {f : α → α → α} (h : ∀ a b, f a b = a ∨ f a b = b) : ∀ o₁ o₂, lift_or_get f o₁ o₂ = o₁ ∨ lift_or_get f o₁ o₂ = o₂ | none none := or.inl rfl | (some a) none := or.inl rfl | none (some b) := or.inr rfl | (some a) (some b) := by simpa [lift_or_get] using h a b @[simp] lemma lift_or_get_none_left {f} {b : option α} : lift_or_get f none b = b := by cases b; refl @[simp] lemma lift_or_get_none_right {f} {a : option α} : lift_or_get f a none = a := by cases a; refl @[simp] lemma lift_or_get_some_some {f} {a b : α} : lift_or_get f (some a) (some b) = f a b := rfl /-- given an element of `a : option α`, a default element `b : β` and a function `α → β`, apply this function to `a` if it comes from `α`, and return `b` otherwise. -/ def cases_on' : option α → β → (α → β) → β | none n s := n | (some a) n s := s a end option
414f4bd56e56b6fda9604a1ffbb6b231e28119ad
3aad12fe82645d2d3173fbedc2e5c2ba945a4d75
/test/tactic/to_int.lean
ff6ce8e99e0154619e95aa55bb14ec94c01f620a
[]
no_license
seanpm2001/LeanProver-Community_MathLIB-Nursery
4f88d539cb18d73a94af983092896b851e6640b5
0479b31fa5b4d39f41e89b8584c9f5bf5271e8ec
refs/heads/master
1,688,730,786,645
1,572,070,026,000
1,572,070,026,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
625
lean
import tactic.to_int example (a b c d : ℕ) (h : a + b ≥ c) (h₀ : a ∣ c) (h' : a + c + 17 ≥ d) (sol : a * c ≤ b + d * c) : a * c ≤ b + d * c := begin to_int, -- a : ℤ, -- a_nneg : a ≥ 0, -- b : ℤ, -- b_nneg : b ≥ 0, -- c : ℤ, -- c_nneg : c ≥ 0, -- d : ℤ, -- d_nneg : d ≥ 0, -- h : a + b ≥ c, -- h₀ : a ∣ c, -- h' : a + c + 17 ≥ d -- ⊢ a * c ≤ b + d * c guard_hyp a := ℤ, guard_hyp b := ℤ, guard_hyp c := ℤ, guard_hyp d := ℤ, guard_hyp h := a + b ≥ c, guard_target a * c ≤ b + d * c, exact sol end
9f99deac7a7cfd749022fa25fa45016d2bdcf9a5
2eab05920d6eeb06665e1a6df77b3157354316ad
/src/number_theory/padics/padic_norm.lean
2b213f6745bbc62634c4e3f55986c672fe7ea418
[ "Apache-2.0" ]
permissive
ayush1801/mathlib
78949b9f789f488148142221606bf15c02b960d2
ce164e28f262acbb3de6281b3b03660a9f744e3c
refs/heads/master
1,692,886,907,941
1,635,270,866,000
1,635,270,866,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
27,967
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 -/ import algebra.order.absolute_value import algebra.field_power import ring_theory.int.basic import tactic.basic import tactic.ring_exp /-! # p-adic norm This file defines the p-adic valuation and the p-adic norm on ℚ. The p-adic valuation on ℚ is the difference of the multiplicities of `p` in the numerator and denominator of `q`. This function obeys the standard properties of a valuation, with the appropriate assumptions on p. The valuation induces a norm on ℚ. This norm is a nonarchimedean absolute value. It takes values in {0} ∪ {1/p^k | k ∈ ℤ}. ## Notations This file uses the local notation `/.` for `rat.mk`. ## Implementation notes Much, but not all, of this file assumes that `p` is prime. This assumption is inferred automatically by taking `[fact (prime p)]` as a type class argument. ## References * [F. Q. Gouêva, *p-adic numbers*][gouvea1997] * [R. Y. Lewis, *A formal proof of Hensel's lemma over the p-adic integers*][lewis2019] * <https://en.wikipedia.org/wiki/P-adic_number> ## Tags p-adic, p adic, padic, norm, valuation -/ universe u open nat open_locale rat open multiplicity /-- For `p ≠ 1`, the p-adic valuation of an integer `z ≠ 0` is the largest natural number `n` such that p^n divides z. `padic_val_rat` defines the valuation of a rational `q` to be the valuation of `q.num` minus the valuation of `q.denom`. If `q = 0` or `p = 1`, then `padic_val_rat p q` defaults to 0. -/ def padic_val_rat (p : ℕ) (q : ℚ) : ℤ := if h : q ≠ 0 ∧ p ≠ 1 then (multiplicity (p : ℤ) q.num).get (multiplicity.finite_int_iff.2 ⟨h.2, rat.num_ne_zero_of_ne_zero h.1⟩) - (multiplicity (p : ℤ) q.denom).get (multiplicity.finite_int_iff.2 ⟨h.2, by exact_mod_cast rat.denom_ne_zero _⟩) else 0 /-- A simplification of the definition of `padic_val_rat p q` when `q ≠ 0` and `p` is prime. -/ lemma padic_val_rat_def (p : ℕ) [hp : fact p.prime] {q : ℚ} (hq : q ≠ 0) : padic_val_rat p q = (multiplicity (p : ℤ) q.num).get (finite_int_iff.2 ⟨hp.1.ne_one, rat.num_ne_zero_of_ne_zero hq⟩) - (multiplicity (p : ℤ) q.denom).get (finite_int_iff.2 ⟨hp.1.ne_one, by exact_mod_cast rat.denom_ne_zero _⟩) := dif_pos ⟨hq, hp.1.ne_one⟩ namespace padic_val_rat open multiplicity variables {p : ℕ} /-- `padic_val_rat p q` is symmetric in `q`. -/ @[simp] protected lemma neg (q : ℚ) : padic_val_rat p (-q) = padic_val_rat p q := begin unfold padic_val_rat, split_ifs, { simp [-add_comm]; refl }, { exfalso, simp * at * }, { exfalso, simp * at * }, { refl } end /-- `padic_val_rat p 1` is 0 for any `p`. -/ @[simp] protected lemma one : padic_val_rat p 1 = 0 := by unfold padic_val_rat; split_ifs; simp * /-- For `p ≠ 0, p ≠ 1, `padic_val_rat p p` is 1. -/ @[simp] lemma padic_val_rat_self (hp : 1 < p) : padic_val_rat p p = 1 := by unfold padic_val_rat; split_ifs; simp [*, nat.one_lt_iff_ne_zero_and_ne_one] at * /-- The p-adic value of an integer `z ≠ 0` is the multiplicity of `p` in `z`. -/ lemma padic_val_rat_of_int (z : ℤ) (hp : p ≠ 1) (hz : z ≠ 0) : padic_val_rat p (z : ℚ) = (multiplicity (p : ℤ) z).get (finite_int_iff.2 ⟨hp, hz⟩) := by rw [padic_val_rat, dif_pos]; simp *; refl end padic_val_rat /-- A convenience function for the case of `padic_val_rat` when both inputs are natural numbers. -/ def padic_val_nat (p : ℕ) (n : ℕ) : ℕ := int.to_nat (padic_val_rat p n) section padic_val_nat /-- `padic_val_nat` is defined as an `int.to_nat` cast; this lemma ensures that the cast is well-behaved. -/ lemma zero_le_padic_val_rat_of_nat (p n : ℕ) : 0 ≤ padic_val_rat p n := begin unfold padic_val_rat, split_ifs, { simp, }, { trivial, }, end /-- `padic_val_rat` coincides with `padic_val_nat`. -/ @[simp, norm_cast] lemma padic_val_rat_of_nat (p n : ℕ) : ↑(padic_val_nat p n) = padic_val_rat p n := begin unfold padic_val_nat, rw int.to_nat_of_nonneg (zero_le_padic_val_rat_of_nat p n), end /-- A simplification of `padic_val_nat` when one input is prime, by analogy with `padic_val_rat_def`. -/ lemma padic_val_nat_def {p : ℕ} [hp : fact p.prime] {n : ℕ} (hn : n ≠ 0) : padic_val_nat p n = (multiplicity p n).get (multiplicity.finite_nat_iff.2 ⟨nat.prime.ne_one hp.1, bot_lt_iff_ne_bot.mpr hn⟩) := begin have n_nonzero : (n : ℚ) ≠ 0, by simpa only [cast_eq_zero, ne.def], -- Infinite loop with @simp padic_val_rat_of_nat unless we restrict the available lemmas here, -- hence the very long list simpa only [ int.coe_nat_multiplicity p n, rat.coe_nat_denom n, (padic_val_rat_of_nat p n).symm, int.coe_nat_zero, int.coe_nat_inj', sub_zero, get_one_right, int.coe_nat_succ, zero_add, rat.coe_nat_num ] using padic_val_rat_def p n_nonzero, end lemma one_le_padic_val_nat_of_dvd {n p : nat} [prime : fact p.prime] (nonzero : n ≠ 0) (div : p ∣ n) : 1 ≤ padic_val_nat p n := begin rw @padic_val_nat_def _ prime _ nonzero, let one_le_mul : _ ≤ multiplicity p n := @multiplicity.le_multiplicity_of_pow_dvd _ _ _ p n 1 (begin norm_num, exact div end), simp only [nat.cast_one] at one_le_mul, rcases one_le_mul with ⟨_, q⟩, dsimp at q, solve_by_elim, end @[simp] lemma padic_val_nat_zero (m : nat) : padic_val_nat m 0 = 0 := by simpa @[simp] lemma padic_val_nat_one (m : nat) : padic_val_nat m 1 = 0 := by simp [padic_val_nat] end padic_val_nat namespace padic_val_rat open multiplicity variables (p : ℕ) [p_prime : fact p.prime] include p_prime /-- The multiplicity of `p : ℕ` in `a : ℤ` is finite exactly when `a ≠ 0`. -/ lemma finite_int_prime_iff {p : ℕ} [p_prime : fact p.prime] {a : ℤ} : finite (p : ℤ) a ↔ a ≠ 0 := by simp [finite_int_iff, ne.symm (ne_of_lt (p_prime.1.one_lt))] /-- A rewrite lemma for `padic_val_rat p q` when `q` is expressed in terms of `rat.mk`. -/ protected lemma defn {q : ℚ} {n d : ℤ} (hqz : q ≠ 0) (qdf : q = n /. d) : padic_val_rat p q = (multiplicity (p : ℤ) n).get (finite_int_iff.2 ⟨ne.symm $ ne_of_lt p_prime.1.one_lt, λ hn, by simp * at *⟩) - (multiplicity (p : ℤ) d).get (finite_int_iff.2 ⟨ne.symm $ ne_of_lt p_prime.1.one_lt, λ hd, by simp * at *⟩) := have hn : n ≠ 0, from rat.mk_num_ne_zero_of_ne_zero hqz qdf, have hd : d ≠ 0, from rat.mk_denom_ne_zero_of_ne_zero hqz qdf, let ⟨c, hc1, hc2⟩ := rat.num_denom_mk hn hd qdf in by rw [padic_val_rat, dif_pos]; simp [hc1, hc2, multiplicity.mul' (nat.prime_iff_prime_int.1 p_prime.1), (ne.symm (ne_of_lt p_prime.1.one_lt)), hqz] /-- A rewrite lemma for `padic_val_rat p (q * r)` with conditions `q ≠ 0`, `r ≠ 0`. -/ protected lemma mul {q r : ℚ} (hq : q ≠ 0) (hr : r ≠ 0) : padic_val_rat p (q * r) = padic_val_rat p q + padic_val_rat p r := have q*r = (q.num * r.num) /. (↑q.denom * ↑r.denom), by rw_mod_cast rat.mul_num_denom, have hq' : q.num /. q.denom ≠ 0, by rw rat.num_denom; exact hq, have hr' : r.num /. r.denom ≠ 0, by rw rat.num_denom; exact hr, have hp' : _root_.prime (p : ℤ), from nat.prime_iff_prime_int.1 p_prime.1, begin rw [padic_val_rat.defn p (mul_ne_zero hq hr) this], conv_rhs { rw [←(@rat.num_denom q), padic_val_rat.defn p hq', ←(@rat.num_denom r), padic_val_rat.defn p hr'] }, rw [multiplicity.mul' hp', multiplicity.mul' hp']; simp [add_comm, add_left_comm, sub_eq_add_neg] end /-- A rewrite lemma for `padic_val_rat p (q^k)` with condition `q ≠ 0`. -/ protected lemma pow {q : ℚ} (hq : q ≠ 0) {k : ℕ} : padic_val_rat p (q ^ k) = k * padic_val_rat p q := by induction k; simp [*, padic_val_rat.mul _ hq (pow_ne_zero _ hq), pow_succ, add_mul, add_comm] /-- A rewrite lemma for `padic_val_rat p (q⁻¹)` with condition `q ≠ 0`. -/ protected lemma inv {q : ℚ} (hq : q ≠ 0) : padic_val_rat p (q⁻¹) = -padic_val_rat p q := by rw [eq_neg_iff_add_eq_zero, ← padic_val_rat.mul p (inv_ne_zero hq) hq, inv_mul_cancel hq, padic_val_rat.one] /-- A rewrite lemma for `padic_val_rat p (q / r)` with conditions `q ≠ 0`, `r ≠ 0`. -/ protected lemma div {q r : ℚ} (hq : q ≠ 0) (hr : r ≠ 0) : padic_val_rat p (q / r) = padic_val_rat p q - padic_val_rat p r := by rw [div_eq_mul_inv, padic_val_rat.mul p hq (inv_ne_zero hr), padic_val_rat.inv p hr, sub_eq_add_neg] /-- A condition for `padic_val_rat p (n₁ / d₁) ≤ padic_val_rat p (n₂ / d₂), in terms of divisibility by `p^n`. -/ lemma padic_val_rat_le_padic_val_rat_iff {n₁ n₂ d₁ d₂ : ℤ} (hn₁ : n₁ ≠ 0) (hn₂ : n₂ ≠ 0) (hd₁ : d₁ ≠ 0) (hd₂ : d₂ ≠ 0) : padic_val_rat p (n₁ /. d₁) ≤ padic_val_rat p (n₂ /. d₂) ↔ ∀ (n : ℕ), ↑p ^ n ∣ n₁ * d₂ → ↑p ^ n ∣ n₂ * d₁ := have hf1 : finite (p : ℤ) (n₁ * d₂), from finite_int_prime_iff.2 (mul_ne_zero hn₁ hd₂), have hf2 : finite (p : ℤ) (n₂ * d₁), from finite_int_prime_iff.2 (mul_ne_zero hn₂ hd₁), by conv { to_lhs, rw [padic_val_rat.defn p (rat.mk_ne_zero_of_ne_zero hn₁ hd₁) rfl, padic_val_rat.defn p (rat.mk_ne_zero_of_ne_zero hn₂ hd₂) rfl, sub_le_iff_le_add', ← add_sub_assoc, le_sub_iff_add_le], norm_cast, rw [← multiplicity.mul' (nat.prime_iff_prime_int.1 p_prime.1) hf1, add_comm, ← multiplicity.mul' (nat.prime_iff_prime_int.1 p_prime.1) hf2, enat.get_le_get, multiplicity_le_multiplicity_iff] } /-- Sufficient conditions to show that the p-adic valuation of `q` is less than or equal to the p-adic vlauation of `q + r`. -/ theorem le_padic_val_rat_add_of_le {q r : ℚ} (hq : q ≠ 0) (hr : r ≠ 0) (hqr : q + r ≠ 0) (h : padic_val_rat p q ≤ padic_val_rat p r) : padic_val_rat p q ≤ padic_val_rat p (q + r) := have hqn : q.num ≠ 0, from rat.num_ne_zero_of_ne_zero hq, have hqd : (q.denom : ℤ) ≠ 0, by exact_mod_cast rat.denom_ne_zero _, have hrn : r.num ≠ 0, from rat.num_ne_zero_of_ne_zero hr, have hrd : (r.denom : ℤ) ≠ 0, by exact_mod_cast rat.denom_ne_zero _, have hqreq : q + r = (((q.num * r.denom + q.denom * r.num : ℤ)) /. (↑q.denom * ↑r.denom : ℤ)), from rat.add_num_denom _ _, have hqrd : q.num * ↑(r.denom) + ↑(q.denom) * r.num ≠ 0, from rat.mk_num_ne_zero_of_ne_zero hqr hqreq, begin conv_lhs { rw ←(@rat.num_denom q) }, rw [hqreq, padic_val_rat_le_padic_val_rat_iff p hqn hqrd hqd (mul_ne_zero hqd hrd), ← multiplicity_le_multiplicity_iff, mul_left_comm, multiplicity.mul (nat.prime_iff_prime_int.1 p_prime.1), add_mul], rw [←(@rat.num_denom q), ←(@rat.num_denom r), padic_val_rat_le_padic_val_rat_iff p hqn hrn hqd hrd, ← multiplicity_le_multiplicity_iff] at h, calc _ ≤ min (multiplicity ↑p (q.num * ↑(r.denom) * ↑(q.denom))) (multiplicity ↑p (↑(q.denom) * r.num * ↑(q.denom))) : (le_min (by rw [@multiplicity.mul _ _ _ _ (_ * _) _ (nat.prime_iff_prime_int.1 p_prime.1), add_comm]) (by rw [mul_assoc, @multiplicity.mul _ _ _ _ (q.denom : ℤ) (_ * _) (nat.prime_iff_prime_int.1 p_prime.1)]; exact add_le_add_left h _)) ... ≤ _ : min_le_multiplicity_add end /-- The minimum of the valuations of `q` and `r` is less than or equal to the valuation of `q + r`. -/ theorem min_le_padic_val_rat_add {q r : ℚ} (hq : q ≠ 0) (hr : r ≠ 0) (hqr : q + r ≠ 0) : min (padic_val_rat p q) (padic_val_rat p r) ≤ padic_val_rat p (q + r) := (le_total (padic_val_rat p q) (padic_val_rat p r)).elim (λ h, by rw [min_eq_left h]; exact le_padic_val_rat_add_of_le _ hq hr hqr h) (λ h, by rw [min_eq_right h, add_comm]; exact le_padic_val_rat_add_of_le _ hr hq (by rwa add_comm) h) open_locale big_operators /-- A finite sum of rationals with positive p-adic valuation has positive p-adic valuation (if the sum is non-zero). -/ theorem sum_pos_of_pos {n : ℕ} {F : ℕ → ℚ} (hF : ∀ i, i < n → 0 < padic_val_rat p (F i)) (hn0 : ∑ i in finset.range n, F i ≠ 0) : 0 < padic_val_rat p (∑ i in finset.range n, F i) := begin induction n with d hd, { exact false.elim (hn0 rfl) }, { rw finset.sum_range_succ at hn0 ⊢, by_cases h : ∑ (x : ℕ) in finset.range d, F x = 0, { rw [h, zero_add], exact hF d (lt_add_one _) }, { refine lt_of_lt_of_le _ (min_le_padic_val_rat_add p h (λ h1, _) hn0), { refine lt_min (hd (λ i hi, _) h) (hF d (lt_add_one _)), exact hF _ (lt_trans hi (lt_add_one _)) }, { have h2 := hF d (lt_add_one _), rw h1 at h2, exact lt_irrefl _ h2 } } } end end padic_val_rat namespace padic_val_nat /-- A rewrite lemma for `padic_val_nat p (q * r)` with conditions `q ≠ 0`, `r ≠ 0`. -/ protected lemma mul (p : ℕ) [p_prime : fact p.prime] {q r : ℕ} (hq : q ≠ 0) (hr : r ≠ 0) : padic_val_nat p (q * r) = padic_val_nat p q + padic_val_nat p r := begin apply int.coe_nat_inj, simp only [padic_val_rat_of_nat, nat.cast_mul], rw padic_val_rat.mul, norm_cast, exact cast_ne_zero.mpr hq, exact cast_ne_zero.mpr hr, end /-- Dividing out by a prime factor reduces the padic_val_nat by 1. -/ protected lemma div {p : ℕ} [p_prime : fact p.prime] {b : ℕ} (dvd : p ∣ b) : (padic_val_nat p (b / p)) = (padic_val_nat p b) - 1 := begin by_cases b_split : (b = 0), { simp [b_split], }, { have split_frac : padic_val_rat p (b / p) = padic_val_rat p b - padic_val_rat p p := padic_val_rat.div p (nat.cast_ne_zero.mpr b_split) (nat.cast_ne_zero.mpr (nat.prime.ne_zero p_prime.1)), rw padic_val_rat.padic_val_rat_self (nat.prime.one_lt p_prime.1) at split_frac, have r : 1 ≤ padic_val_nat p b := one_le_padic_val_nat_of_dvd b_split dvd, exact_mod_cast split_frac, } end /-- A version of `padic_val_rat.pow` for `padic_val_nat` -/ protected lemma pow (p q n : ℕ) [fact p.prime] (hq : q ≠ 0) : padic_val_nat p (q ^ n) = n * padic_val_nat p q := begin apply @nat.cast_injective ℤ, push_cast, exact padic_val_rat.pow _ (cast_ne_zero.mpr hq), end end padic_val_nat section padic_val_nat /-- If a prime doesn't appear in `n`, `padic_val_nat p n` is `0`. -/ lemma padic_val_nat_of_not_dvd {p : ℕ} [fact p.prime] {n : ℕ} (not_dvd : ¬(p ∣ n)) : padic_val_nat p n = 0 := begin by_cases hn : n = 0, { subst hn, simp at not_dvd, trivial, }, { rw padic_val_nat_def hn, exact (@multiplicity.unique' _ _ _ p n 0 (by simp) (by simpa using not_dvd)).symm, assumption, }, end lemma dvd_of_one_le_padic_val_nat {n p : nat} [prime : fact p.prime] (hp : 1 ≤ padic_val_nat p n) : p ∣ n := begin by_contra h, rw padic_val_nat_of_not_dvd h at hp, exact lt_irrefl 0 (lt_of_lt_of_le zero_lt_one hp), end lemma pow_padic_val_nat_dvd {p n : ℕ} [fact (nat.prime p)] : p ^ (padic_val_nat p n) ∣ n := begin cases nat.eq_zero_or_pos n with hn hn, { rw hn, exact dvd_zero (p ^ padic_val_nat p 0) }, { rw multiplicity.pow_dvd_iff_le_multiplicity, apply le_of_eq, rw padic_val_nat_def (ne_of_gt hn), { apply enat.coe_get }, { apply_instance } } end lemma pow_succ_padic_val_nat_not_dvd {p n : ℕ} [hp : fact (nat.prime p)] (hn : 0 < n) : ¬ p ^ (padic_val_nat p n + 1) ∣ n := begin { rw multiplicity.pow_dvd_iff_le_multiplicity, rw padic_val_nat_def (ne_of_gt hn), { rw [nat.cast_add, enat.coe_get], simp only [nat.cast_one, not_le], apply enat.lt_add_one (ne_top_iff_finite.2 (finite_nat_iff.2 ⟨hp.elim.ne_one, hn⟩)) }, { apply_instance } } end lemma padic_val_nat_primes {p q : ℕ} [p_prime : fact p.prime] [q_prime : fact q.prime] (neq : p ≠ q) : padic_val_nat p q = 0 := @padic_val_nat_of_not_dvd p p_prime q $ (not_congr (iff.symm (prime_dvd_prime_iff_eq p_prime.1 q_prime.1))).mp neq protected lemma padic_val_nat.div' {p : ℕ} [p_prime : fact p.prime] : ∀ {m : ℕ} (cpm : coprime p m) {b : ℕ} (dvd : m ∣ b), padic_val_nat p (b / m) = padic_val_nat p b | 0 := λ cpm b dvd, by { rw zero_dvd_iff at dvd, rw [dvd, nat.zero_div], } | (n + 1) := λ cpm b dvd, begin rcases dvd with ⟨c, rfl⟩, rw [mul_div_right c (nat.succ_pos _)],by_cases hc : c = 0, { rw [hc, mul_zero] }, { rw padic_val_nat.mul, { suffices : ¬ p ∣ (n+1), { rw [padic_val_nat_of_not_dvd this, zero_add] }, contrapose! cpm, exact p_prime.1.dvd_iff_not_coprime.mp cpm }, { exact nat.succ_ne_zero _ }, { exact hc } }, end lemma padic_val_nat_eq_factors_count (p : ℕ) [hp : fact p.prime] : ∀ (n : ℕ), padic_val_nat p n = (factors n).count p | 0 := by simp | 1 := by simp | (m + 2) := let n := m + 2 in let q := min_fac n in have hq : fact q.prime := ⟨min_fac_prime (show m + 2 ≠ 1, by linarith)⟩, have wf : n / q < n := nat.div_lt_self (nat.succ_pos _) hq.1.one_lt, begin rw factors_add_two, show padic_val_nat p n = list.count p (q :: (factors (n / q))), rw [list.count_cons', ← padic_val_nat_eq_factors_count], split_ifs with h, have p_dvd_n : p ∣ n, { have: q ∣ n := nat.min_fac_dvd n, cc }, { rw [←h, padic_val_nat.div], { have: 1 ≤ padic_val_nat p n := one_le_padic_val_nat_of_dvd (by linarith) p_dvd_n, exact (tsub_eq_iff_eq_add_of_le this).mp rfl, }, { exact p_dvd_n, }, }, { suffices : p.coprime q, { rw [padic_val_nat.div' this (min_fac_dvd n), add_zero], }, rwa nat.coprime_primes hp.1 hq.1, }, end @[simp] lemma padic_val_nat_self (p : ℕ) [fact p.prime] : padic_val_nat p p = 1 := by simp [padic_val_nat_def (fact.out p.prime).ne_zero] @[simp] lemma padic_val_nat_prime_pow (p n : ℕ) [fact p.prime] : padic_val_nat p (p ^ n) = n := by rw [padic_val_nat.pow p _ _ (fact.out p.prime).ne_zero, padic_val_nat_self p, mul_one] open_locale big_operators lemma prod_pow_prime_padic_val_nat (n : nat) (hn : n ≠ 0) (m : nat) (pr : n < m) : ∏ p in finset.filter nat.prime (finset.range m), p ^ (padic_val_nat p n) = n := begin rw ← pos_iff_ne_zero at hn, have H : (factors n : multiset ℕ).prod = n, { rw [multiset.coe_prod, prod_factors hn], }, rw finset.prod_multiset_count at H, conv_rhs { rw ← H, }, refine finset.prod_bij_ne_one (λ p hp hp', p) _ _ _ _, { rintro p hp hpn, rw [finset.mem_filter, finset.mem_range] at hp, rw [multiset.mem_to_finset, multiset.mem_coe, mem_factors_iff_dvd hn hp.2], contrapose! hpn, haveI Hp : fact p.prime := ⟨hp.2⟩, rw [padic_val_nat_of_not_dvd hpn, pow_zero], }, { intros, assumption }, { intros p hp hpn, rw [multiset.mem_to_finset, multiset.mem_coe] at hp, haveI Hp : fact p.prime := ⟨prime_of_mem_factors hp⟩, simp only [exists_prop, ne.def, finset.mem_filter, finset.mem_range], refine ⟨p, ⟨_, Hp.1⟩, ⟨_, rfl⟩⟩, { rw mem_factors_iff_dvd hn Hp.1 at hp, exact lt_of_le_of_lt (le_of_dvd hn hp) pr }, { rw padic_val_nat_eq_factors_count, simpa [ne.def, multiset.coe_count] using hpn } }, { intros p hp hpn, rw [finset.mem_filter, finset.mem_range] at hp, haveI Hp : fact p.prime := ⟨hp.2⟩, rw [padic_val_nat_eq_factors_count, multiset.coe_count] } end end padic_val_nat /-- If `q ≠ 0`, the p-adic norm of a rational `q` is `p ^ (-(padic_val_rat p q))`. If `q = 0`, the p-adic norm of `q` is 0. -/ def padic_norm (p : ℕ) (q : ℚ) : ℚ := if q = 0 then 0 else (↑p : ℚ) ^ (-(padic_val_rat p q)) namespace padic_norm section padic_norm open padic_val_rat variables (p : ℕ) /-- Unfolds the definition of the p-adic norm of `q` when `q ≠ 0`. -/ @[simp] protected lemma eq_fpow_of_nonzero {q : ℚ} (hq : q ≠ 0) : padic_norm p q = p ^ (-(padic_val_rat p q)) := by simp [hq, padic_norm] /-- The p-adic norm is nonnegative. -/ protected lemma nonneg (q : ℚ) : 0 ≤ padic_norm p q := if hq : q = 0 then by simp [hq, padic_norm] else begin unfold padic_norm; split_ifs, apply fpow_nonneg, exact_mod_cast nat.zero_le _ end /-- The p-adic norm of 0 is 0. -/ @[simp] protected lemma zero : padic_norm p 0 = 0 := by simp [padic_norm] /-- The p-adic norm of 1 is 1. -/ @[simp] protected lemma one : padic_norm p 1 = 1 := by simp [padic_norm] /-- The p-adic norm of `p` is `1/p` if `p > 1`. See also `padic_norm.padic_norm_p_of_prime` for a version that assumes `p` is prime. -/ lemma padic_norm_p {p : ℕ} (hp : 1 < p) : padic_norm p p = 1 / p := by simp [padic_norm, (show p ≠ 0, by linarith), padic_val_rat.padic_val_rat_self hp] /-- The p-adic norm of `p` is `1/p` if `p` is prime. See also `padic_norm.padic_norm_p` for a version that assumes `1 < p`. -/ @[simp] lemma padic_norm_p_of_prime (p : ℕ) [fact p.prime] : padic_norm p p = 1 / p := padic_norm_p $ nat.prime.one_lt (fact.out _) /-- The p-adic norm of `q` is `1` if `q` is prime and not equal to `p`. -/ lemma padic_norm_of_prime_of_ne {p q : ℕ} [p_prime : fact p.prime] [q_prime : fact q.prime] (neq : p ≠ q) : padic_norm p q = 1 := begin have p : padic_val_rat p q = 0, { exact_mod_cast @padic_val_nat_primes p q p_prime q_prime neq }, simp [padic_norm, p, q_prime.1.1, q_prime.1.ne_zero], end /-- The p-adic norm of `p` is less than 1 if `1 < p`. See also `padic_norm.padic_norm_p_lt_one_of_prime` for a version assuming `prime p`. -/ lemma padic_norm_p_lt_one {p : ℕ} (hp : 1 < p) : padic_norm p p < 1 := begin rw [padic_norm_p hp, div_lt_iff, one_mul], { exact_mod_cast hp }, { exact_mod_cast zero_lt_one.trans hp }, end /-- The p-adic norm of `p` is less than 1 if `p` is prime. See also `padic_norm.padic_norm_p_lt_one` for a version assuming `1 < p`. -/ lemma padic_norm_p_lt_one_of_prime (p : ℕ) [fact p.prime] : padic_norm p p < 1 := padic_norm_p_lt_one $ nat.prime.one_lt (fact.out _) /-- `padic_norm p q` takes discrete values `p ^ -z` for `z : ℤ`. -/ protected theorem values_discrete {q : ℚ} (hq : q ≠ 0) : ∃ z : ℤ, padic_norm p q = p ^ (-z) := ⟨ (padic_val_rat p q), by simp [padic_norm, hq] ⟩ /-- `padic_norm p` is symmetric. -/ @[simp] protected lemma neg (q : ℚ) : padic_norm p (-q) = padic_norm p q := if hq : q = 0 then by simp [hq] else by simp [padic_norm, hq] variable [hp : fact p.prime] include hp /-- If `q ≠ 0`, then `padic_norm p q ≠ 0`. -/ protected lemma nonzero {q : ℚ} (hq : q ≠ 0) : padic_norm p q ≠ 0 := begin rw padic_norm.eq_fpow_of_nonzero p hq, apply fpow_ne_zero_of_ne_zero, exact_mod_cast ne_of_gt hp.1.pos end /-- If the p-adic norm of `q` is 0, then `q` is 0. -/ lemma zero_of_padic_norm_eq_zero {q : ℚ} (h : padic_norm p q = 0) : q = 0 := begin apply by_contradiction, intro hq, unfold padic_norm at h, rw if_neg hq at h, apply absurd h, apply fpow_ne_zero_of_ne_zero, exact_mod_cast hp.1.ne_zero end /-- The p-adic norm is multiplicative. -/ @[simp] protected theorem mul (q r : ℚ) : padic_norm p (q*r) = padic_norm p q * padic_norm p r := if hq : q = 0 then by simp [hq] else if hr : r = 0 then by simp [hr] else have q*r ≠ 0, from mul_ne_zero hq hr, have (↑p : ℚ) ≠ 0, by simp [hp.1.ne_zero], by simp [padic_norm, *, padic_val_rat.mul, fpow_add this, mul_comm] /-- The p-adic norm respects division. -/ @[simp] protected theorem div (q r : ℚ) : padic_norm p (q / r) = padic_norm p q / padic_norm p r := if hr : r = 0 then by simp [hr] else eq_div_of_mul_eq (padic_norm.nonzero _ hr) (by rw [←padic_norm.mul, div_mul_cancel _ hr]) /-- The p-adic norm of an integer is at most 1. -/ protected theorem of_int (z : ℤ) : padic_norm p ↑z ≤ 1 := if hz : z = 0 then by simp [hz, zero_le_one] else begin unfold padic_norm, rw [if_neg _], { refine fpow_le_one_of_nonpos _ _, { exact_mod_cast le_of_lt hp.1.one_lt, }, { rw [padic_val_rat_of_int _ hp.1.ne_one hz, neg_nonpos], norm_cast, simp }}, exact_mod_cast hz end private lemma nonarchimedean_aux {q r : ℚ} (h : padic_val_rat p q ≤ padic_val_rat p r) : padic_norm p (q + r) ≤ max (padic_norm p q) (padic_norm p r) := have hnqp : padic_norm p q ≥ 0, from padic_norm.nonneg _ _, have hnrp : padic_norm p r ≥ 0, from padic_norm.nonneg _ _, if hq : q = 0 then by simp [hq, max_eq_right hnrp, le_max_right] else if hr : r = 0 then by simp [hr, max_eq_left hnqp, le_max_left] else if hqr : q + r = 0 then le_trans (by simpa [hqr] using hnqp) (le_max_left _ _) else begin unfold padic_norm, split_ifs, apply le_max_iff.2, left, apply fpow_le_of_le, { exact_mod_cast le_of_lt hp.1.one_lt }, { apply neg_le_neg, have : padic_val_rat p q = min (padic_val_rat p q) (padic_val_rat p r), from (min_eq_left h).symm, rw this, apply min_le_padic_val_rat_add; assumption } end /-- The p-adic norm is nonarchimedean: the norm of `p + q` is at most the max of the norm of `p` and the norm of `q`. -/ protected theorem nonarchimedean {q r : ℚ} : padic_norm p (q + r) ≤ max (padic_norm p q) (padic_norm p r) := begin wlog hle := le_total (padic_val_rat p q) (padic_val_rat p r) using [q r], exact nonarchimedean_aux p hle end /-- The p-adic norm respects the triangle inequality: the norm of `p + q` is at most the norm of `p` plus the norm of `q`. -/ theorem triangle_ineq (q r : ℚ) : padic_norm p (q + r) ≤ padic_norm p q + padic_norm p r := calc padic_norm p (q + r) ≤ max (padic_norm p q) (padic_norm p r) : padic_norm.nonarchimedean p ... ≤ padic_norm p q + padic_norm p r : max_le_add_of_nonneg (padic_norm.nonneg p _) (padic_norm.nonneg p _) /-- The p-adic norm of a difference is at most the max of each component. Restates the archimedean property of the p-adic norm. -/ protected theorem sub {q r : ℚ} : padic_norm p (q - r) ≤ max (padic_norm p q) (padic_norm p r) := by rw [sub_eq_add_neg, ←padic_norm.neg p r]; apply padic_norm.nonarchimedean /-- If the p-adic norms of `q` and `r` are different, then the norm of `q + r` is equal to the max of the norms of `q` and `r`. -/ lemma add_eq_max_of_ne {q r : ℚ} (hne : padic_norm p q ≠ padic_norm p r) : padic_norm p (q + r) = max (padic_norm p q) (padic_norm p r) := begin wlog hle := le_total (padic_norm p r) (padic_norm p q) using [q r], have hlt : padic_norm p r < padic_norm p q, from lt_of_le_of_ne hle hne.symm, have : padic_norm p q ≤ max (padic_norm p (q + r)) (padic_norm p r), from calc padic_norm p q = padic_norm p (q + r - r) : by congr; ring ... ≤ max (padic_norm p (q + r)) (padic_norm p (-r)) : padic_norm.nonarchimedean p ... = max (padic_norm p (q + r)) (padic_norm p r) : by simp, have hnge : padic_norm p r ≤ padic_norm p (q + r), { apply le_of_not_gt, intro hgt, rw max_eq_right_of_lt hgt at this, apply not_lt_of_ge this, assumption }, have : padic_norm p q ≤ padic_norm p (q + r), by rwa [max_eq_left hnge] at this, apply _root_.le_antisymm, { apply padic_norm.nonarchimedean p }, { rw max_eq_left_of_lt hlt, assumption } end /-- The p-adic norm is an absolute value: positive-definite and multiplicative, satisfying the triangle inequality. -/ instance : is_absolute_value (padic_norm p) := { abv_nonneg := padic_norm.nonneg p, abv_eq_zero := begin intros, constructor; intro, { apply zero_of_padic_norm_eq_zero p, assumption }, { simp [*] } end, abv_add := padic_norm.triangle_ineq p, abv_mul := padic_norm.mul p } variable {p} lemma dvd_iff_norm_le {n : ℕ} {z : ℤ} : ↑(p^n) ∣ z ↔ padic_norm p z ≤ ↑p ^ (-n : ℤ) := begin unfold padic_norm, split_ifs with hz, { norm_cast at hz, have : 0 ≤ (p^n : ℚ), {apply pow_nonneg, exact_mod_cast le_of_lt hp.1.pos }, simp [hz, this] }, { rw [fpow_le_iff_le, neg_le_neg_iff, padic_val_rat_of_int _ hp.1.ne_one _], { norm_cast, rw [← enat.coe_le_coe, enat.coe_get, ← multiplicity.pow_dvd_iff_le_multiplicity], simp }, { exact_mod_cast hz }, { exact_mod_cast hp.1.one_lt } } end end padic_norm end padic_norm
136103ce63442720e41ab303558bb61b22f1acba
efce24474b28579aba3272fdb77177dc2b11d7aa
/src/homotopy_theory/topological_spaces/pointed.lean
baaa749bbb9111e5fcf53941cdb994d98480b50a
[ "Apache-2.0" ]
permissive
rwbarton/lean-homotopy-theory
cff499f24268d60e1c546e7c86c33f58c62888ed
39e1b4ea1ed1b0eca2f68bc64162dde6a6396dee
refs/heads/lean-3.4.2
1,622,711,883,224
1,598,550,958,000
1,598,550,958,000
136,023,667
12
6
Apache-2.0
1,573,187,573,000
1,528,116,262,000
Lean
UTF-8
Lean
false
false
1,558
lean
import .category import .homeomorphism open category_theory local notation f ` ∘ `:80 g:80 := g ≫ f universe u namespace homotopy_theory.topological_spaces open Top structure Top_ptd : Type (u+1) := (space : Top.{u}) (pt : space) namespace Top_ptd local notation `Top_ptd` := Top_ptd.{u} instance : has_coe Top_ptd Top := ⟨λ X, X.space⟩ instance : has_coe_to_sort Top_ptd := { S := Type u, coe := λ X, X.space.α } def ptd_map (X Y : Top_ptd) : Type u := { f : X.space ⟶ Y.space // (f : X.space ⟶ Y.space) X.pt = Y.pt } instance {X Y : Top_ptd} : has_coe_to_fun (ptd_map X Y) := { F := λ _, X → Y, coe := λ f, f.val.1 } instance : category Top_ptd := { hom := ptd_map, id := λ X, ⟨𝟙 X, rfl⟩, comp := λ _ _ _ f g, ⟨g.val ∘ f.val, show g.val (f.val _) = _, by rw [f.property, g.property]⟩ } protected def mk_ob (X : Top) (x : X) : Top_ptd := ⟨X, x⟩ protected def mk_hom {X Y : Top_ptd} (f : X.space ⟶ Y.space) (hf : f X.pt = Y.pt) : X ⟶ Y := subtype.mk f hf protected def mk_iso {X Y : Top_ptd} (i : Top.homeomorphism X.space Y.space) (hi : i.hom X.pt = Y.pt) : X ≅ Y := { hom := ⟨i.hom, hi⟩, inv := ⟨i.inv, begin rw ←hi, change i.equiv.symm (i.equiv X.pt) = X.pt, simp end⟩, hom_inv_id' := subtype.eq i.hom_inv_id, inv_hom_id' := subtype.eq i.inv_hom_id } protected def mk_iso' {X Y : Top} (i : Top.homeomorphism X Y) (x : X) : Top_ptd.mk_ob X x ≅ Top_ptd.mk_ob Y (i.hom x) := Top_ptd.mk_iso i rfl end «Top_ptd» end homotopy_theory.topological_spaces
497593920a6e5e5aba4960cfed30c62be9bc0c4b
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/algebra/monoid_algebra/basic.lean
7fcebf51852537060128bca43c65b66974f54308
[ "Apache-2.0" ]
permissive
waynemunro/mathlib
e3fd4ff49f4cb43d4a8ded59d17be407bc5ee552
065a70810b5480d584033f7bbf8e0409480c2118
refs/heads/master
1,693,417,182,397
1,634,644,781,000
1,634,644,781,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
57,115
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Yury G. Kudryashov, Scott Morrison -/ import algebra.big_operators.finsupp import linear_algebra.finsupp import algebra.non_unital_alg_hom /-! # Monoid algebras When the domain of a `finsupp` has a multiplicative or additive structure, we can define a convolution product. To mathematicians this structure is known as the "monoid algebra", i.e. the finite formal linear combinations over a given semiring of elements of the monoid. The "group ring" ℤ[G] or the "group algebra" k[G] are typical uses. In fact the construction of the "monoid algebra" makes sense when `G` is not even a monoid, but merely a magma, i.e., when `G` carries a multiplication which is not required to satisfy any conditions at all. In this case the construction yields a not-necessarily-unital, not-necessarily-associative algebra but it is still adjoint to the forgetful functor from such algebras to magmas, and we prove this as `monoid_algebra.lift_magma`. In this file we define `monoid_algebra k G := G →₀ k`, and `add_monoid_algebra k G` in the same way, and then define the convolution product on these. When the domain is additive, this is used to define polynomials: ``` polynomial α := add_monoid_algebra ℕ α mv_polynomial σ α := add_monoid_algebra (σ →₀ ℕ) α ``` When the domain is multiplicative, e.g. a group, this will be used to define the group ring. ## Implementation note Unfortunately because additive and multiplicative structures both appear in both cases, it doesn't appear to be possible to make much use of `to_additive`, and we just settle for saying everything twice. Similarly, I attempted to just define `add_monoid_algebra k G := monoid_algebra k (multiplicative G)`, but the definitional equality `multiplicative G = G` leaks through everywhere, and seems impossible to use. -/ noncomputable theory open_locale classical big_operators open finset finsupp universes u₁ u₂ u₃ variables (k : Type u₁) (G : Type u₂) /-! ### Multiplicative monoids -/ section variables [semiring k] /-- The monoid algebra over a semiring `k` generated by the monoid `G`. It is the type of finite formal `k`-linear combinations of terms of `G`, endowed with the convolution product. -/ @[derive [inhabited, add_comm_monoid, has_coe_to_fun]] def monoid_algebra : Type (max u₁ u₂) := G →₀ k end namespace monoid_algebra variables {k G} section has_mul variables [semiring k] [has_mul G] /-- The product of `f g : monoid_algebra k G` is the finitely supported function whose value at `a` is the sum of `f x * g y` over all pairs `x, y` such that `x * y = a`. (Think of the group ring of a group.) -/ instance : has_mul (monoid_algebra k G) := ⟨λf g, f.sum $ λa₁ b₁, g.sum $ λa₂ b₂, single (a₁ * a₂) (b₁ * b₂)⟩ lemma mul_def {f g : monoid_algebra k G} : f * g = (f.sum $ λa₁ b₁, g.sum $ λa₂ b₂, single (a₁ * a₂) (b₁ * b₂)) := rfl instance : non_unital_non_assoc_semiring (monoid_algebra k G) := { zero := 0, mul := (*), add := (+), left_distrib := assume f g h, by simp only [mul_def, sum_add_index, mul_add, mul_zero, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, sum_add], right_distrib := assume f g h, by simp only [mul_def, sum_add_index, add_mul, zero_mul, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, sum_zero, sum_add], zero_mul := assume f, by simp only [mul_def, sum_zero_index], mul_zero := assume f, by simp only [mul_def, sum_zero_index, sum_zero], .. finsupp.add_comm_monoid } end has_mul section semigroup variables [semiring k] [semigroup G] instance : non_unital_semiring (monoid_algebra k G) := { zero := 0, mul := (*), add := (+), mul_assoc := assume f g h, by simp only [mul_def, sum_sum_index, sum_zero_index, sum_add_index, sum_single_index, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, add_mul, mul_add, add_assoc, mul_assoc, zero_mul, mul_zero, sum_zero, sum_add], .. monoid_algebra.non_unital_non_assoc_semiring} end semigroup section has_one variables [semiring k] [has_one G] /-- The unit of the multiplication is `single 1 1`, i.e. the function that is `1` at `1` and zero elsewhere. -/ instance : has_one (monoid_algebra k G) := ⟨single 1 1⟩ lemma one_def : (1 : monoid_algebra k G) = single 1 1 := rfl end has_one section mul_one_class variables [semiring k] [mul_one_class G] instance : non_assoc_semiring (monoid_algebra k G) := { one := 1, mul := (*), zero := 0, add := (+), one_mul := assume f, by simp only [mul_def, one_def, sum_single_index, zero_mul, single_zero, sum_zero, zero_add, one_mul, sum_single], mul_one := assume f, by simp only [mul_def, one_def, sum_single_index, mul_zero, single_zero, sum_zero, add_zero, mul_one, sum_single], ..monoid_algebra.non_unital_non_assoc_semiring } variables {R : Type*} [semiring R] /-- A non-commutative version of `monoid_algebra.lift`: given a additive homomorphism `f : k →+ R` and a multiplicative monoid homomorphism `g : G →* R`, returns the additive homomorphism from `monoid_algebra k G` such that `lift_nc f g (single a b) = f b * g a`. If `f` is a ring homomorphism and the range of either `f` or `g` is in center of `R`, then the result is a ring homomorphism. If `R` is a `k`-algebra and `f = algebra_map k R`, then the result is an algebra homomorphism called `monoid_algebra.lift`. -/ def lift_nc (f : k →+ R) (g : G →* R) : monoid_algebra k G →+ R := lift_add_hom (λ x : G, (add_monoid_hom.mul_right (g x)).comp f) @[simp] lemma lift_nc_single (f : k →+ R) (g : G →* R) (a : G) (b : k) : lift_nc f g (single a b) = f b * g a := lift_add_hom_apply_single _ _ _ @[simp] lemma lift_nc_one (f : k →+* R) (g : G →* R) : lift_nc (f : k →+ R) g 1 = 1 := by simp [one_def] lemma lift_nc_mul (f : k →+* R) (g : G →* R) (a b : monoid_algebra k G) (h_comm : ∀ {x y}, y ∈ a.support → commute (f (b x)) (g y)) : lift_nc (f : k →+ R) g (a * b) = lift_nc (f : k →+ R) g a * lift_nc (f : k →+ R) g b := begin conv_rhs { rw [← sum_single a, ← sum_single b] }, simp_rw [mul_def, (lift_nc _ g).map_finsupp_sum, lift_nc_single, finsupp.sum_mul, finsupp.mul_sum], refine finset.sum_congr rfl (λ y hy, finset.sum_congr rfl (λ x hx, _)), simp [mul_assoc, (h_comm hy).left_comm] end end mul_one_class /-! #### Semiring structure -/ section semiring variables [semiring k] [monoid G] instance : semiring (monoid_algebra k G) := { one := 1, mul := (*), zero := 0, add := (+), .. monoid_algebra.non_unital_semiring, .. monoid_algebra.non_assoc_semiring } variables {R : Type*} [semiring R] /-- `lift_nc` as a `ring_hom`, for when `f x` and `g y` commute -/ def lift_nc_ring_hom (f : k →+* R) (g : G →* R) (h_comm : ∀ x y, commute (f x) (g y)) : monoid_algebra k G →+* R := { to_fun := lift_nc (f : k →+ R) g, map_one' := lift_nc_one _ _, map_mul' := λ a b, lift_nc_mul _ _ _ _ $ λ _ _ _, h_comm _ _, ..(lift_nc (f : k →+ R) g)} end semiring instance [comm_semiring k] [comm_monoid G] : comm_semiring (monoid_algebra k G) := { mul_comm := assume f g, begin simp only [mul_def, finsupp.sum, mul_comm], rw [finset.sum_comm], simp only [mul_comm] end, .. monoid_algebra.semiring } instance [semiring k] [nontrivial k] [nonempty G]: nontrivial (monoid_algebra k G) := finsupp.nontrivial /-! #### Derived instances -/ section derived_instances instance [semiring k] [subsingleton k] : unique (monoid_algebra k G) := finsupp.unique_of_right instance [ring k] : add_group (monoid_algebra k G) := finsupp.add_group instance [ring k] [monoid G] : ring (monoid_algebra k G) := { neg := has_neg.neg, add_left_neg := add_left_neg, .. monoid_algebra.semiring } instance [comm_ring k] [comm_monoid G] : comm_ring (monoid_algebra k G) := { mul_comm := mul_comm, .. monoid_algebra.ring} variables {R S : Type*} instance [monoid R] [semiring k] [distrib_mul_action R k] : has_scalar R (monoid_algebra k G) := finsupp.has_scalar instance [monoid R] [semiring k] [distrib_mul_action R k] : distrib_mul_action R (monoid_algebra k G) := finsupp.distrib_mul_action G k instance [semiring R] [semiring k] [module R k] : module R (monoid_algebra k G) := finsupp.module G k instance [monoid R] [semiring k] [distrib_mul_action R k] [has_faithful_scalar R k] [nonempty G] : has_faithful_scalar R (monoid_algebra k G) := finsupp.has_faithful_scalar instance [monoid R] [monoid S] [semiring k] [distrib_mul_action R k] [distrib_mul_action S k] [has_scalar R S] [is_scalar_tower R S k] : is_scalar_tower R S (monoid_algebra k G) := finsupp.is_scalar_tower G k instance [monoid R] [monoid S] [semiring k] [distrib_mul_action R k] [distrib_mul_action S k] [smul_comm_class R S k] : smul_comm_class R S (monoid_algebra k G) := finsupp.smul_comm_class G k instance comap_distrib_mul_action_self [group G] [semiring k] : distrib_mul_action G (monoid_algebra k G) := finsupp.comap_distrib_mul_action_self end derived_instances section misc_theorems variables [semiring k] local attribute [reducible] monoid_algebra lemma mul_apply [has_mul G] (f g : monoid_algebra k G) (x : G) : (f * g) x = (f.sum $ λa₁ b₁, g.sum $ λa₂ b₂, if a₁ * a₂ = x then b₁ * b₂ else 0) := begin rw [mul_def], simp only [finsupp.sum_apply, single_apply], end lemma mul_apply_antidiagonal [has_mul G] (f g : monoid_algebra k G) (x : G) (s : finset (G × G)) (hs : ∀ {p : G × G}, p ∈ s ↔ p.1 * p.2 = x) : (f * g) x = ∑ p in s, (f p.1 * g p.2) := let F : G × G → k := λ p, if p.1 * p.2 = x then f p.1 * g p.2 else 0 in calc (f * g) x = (∑ a₁ in f.support, ∑ a₂ in g.support, F (a₁, a₂)) : mul_apply f g x ... = ∑ p in f.support.product g.support, F p : finset.sum_product.symm ... = ∑ p in (f.support.product g.support).filter (λ p : G × G, p.1 * p.2 = x), f p.1 * g p.2 : (finset.sum_filter _ _).symm ... = ∑ p in s.filter (λ p : G × G, p.1 ∈ f.support ∧ p.2 ∈ g.support), f p.1 * g p.2 : sum_congr (by { ext, simp only [mem_filter, mem_product, hs, and_comm] }) (λ _ _, rfl) ... = ∑ p in s, f p.1 * g p.2 : sum_subset (filter_subset _ _) $ λ p hps hp, begin simp only [mem_filter, mem_support_iff, not_and, not_not] at hp ⊢, by_cases h1 : f p.1 = 0, { rw [h1, zero_mul] }, { rw [hp hps h1, mul_zero] } end lemma support_mul [has_mul G] (a b : monoid_algebra k G) : (a * b).support ⊆ a.support.bUnion (λa₁, b.support.bUnion $ λa₂, {a₁ * a₂}) := subset.trans support_sum $ bUnion_mono $ assume a₁ _, subset.trans support_sum $ bUnion_mono $ assume a₂ _, support_single_subset @[simp] lemma single_mul_single [has_mul G] {a₁ a₂ : G} {b₁ b₂ : k} : (single a₁ b₁ : monoid_algebra k G) * single a₂ b₂ = single (a₁ * a₂) (b₁ * b₂) := (sum_single_index (by simp only [zero_mul, single_zero, sum_zero])).trans (sum_single_index (by rw [mul_zero, single_zero])) @[simp] lemma single_pow [monoid G] {a : G} {b : k} : ∀ n : ℕ, (single a b : monoid_algebra k G)^n = single (a^n) (b ^ n) | 0 := by { simp only [pow_zero], refl } | (n+1) := by simp only [pow_succ, single_pow n, single_mul_single] section /-- Like `finsupp.map_domain_add`, but for the convolutive multiplication we define in this file -/ lemma map_domain_mul {α : Type*} {β : Type*} {α₂ : Type*} [semiring β] [has_mul α] [has_mul α₂] {x y : monoid_algebra β α} (f : mul_hom α α₂) : (map_domain f (x * y : monoid_algebra β α) : monoid_algebra β α₂) = (map_domain f x * map_domain f y : monoid_algebra β α₂) := begin simp_rw [mul_def, map_domain_sum, map_domain_single, f.map_mul], rw finsupp.sum_map_domain_index, { congr, ext a b, rw finsupp.sum_map_domain_index, { simp }, { simp [mul_add] } }, { simp }, { simp [add_mul] } end variables (k G) /-- The embedding of a magma into its magma algebra. -/ @[simps] def of_magma [has_mul G] : mul_hom G (monoid_algebra k G) := { to_fun := λ a, single a 1, map_mul' := λ a b, by simp only [mul_def, mul_one, sum_single_index, single_eq_zero, mul_zero], } /-- The embedding of a unital magma into its magma algebra. -/ @[simps] def of [mul_one_class G] : G →* monoid_algebra k G := { to_fun := λ a, single a 1, map_one' := rfl, .. of_magma k G } end lemma of_injective [mul_one_class G] [nontrivial k] : function.injective (of k G) := λ a b h, by simpa using (single_eq_single_iff _ _ _ _).mp h lemma mul_single_apply_aux [has_mul G] (f : monoid_algebra k G) {r : k} {x y z : G} (H : ∀ a, a * x = z ↔ a = y) : (f * single x r) z = f y * r := have A : ∀ a₁ b₁, (single x r).sum (λ a₂ b₂, ite (a₁ * a₂ = z) (b₁ * b₂) 0) = ite (a₁ * x = z) (b₁ * r) 0, from λ a₁ b₁, sum_single_index $ by simp, calc (f * single x r) z = sum f (λ a b, if (a = y) then (b * r) else 0) : -- different `decidable` instances make it not trivial by { simp only [mul_apply, A, H], congr, funext, split_ifs; refl } ... = if y ∈ f.support then f y * r else 0 : f.support.sum_ite_eq' _ _ ... = f y * r : by split_ifs with h; simp at h; simp [h] lemma mul_single_one_apply [mul_one_class G] (f : monoid_algebra k G) (r : k) (x : G) : (f * single 1 r) x = f x * r := f.mul_single_apply_aux $ λ a, by rw [mul_one] lemma support_mul_single [right_cancel_semigroup G] (f : monoid_algebra k G) (r : k) (hr : ∀ y, y * r = 0 ↔ y = 0) (x : G) : (f * single x r).support = f.support.map (mul_right_embedding x) := begin ext y, simp only [mem_support_iff, mem_map, exists_prop, mul_right_embedding_apply], by_cases H : ∃ a, a * x = y, { rcases H with ⟨a, rfl⟩, rw [mul_single_apply_aux f (λ _, mul_left_inj x)], simp [hr] }, { push_neg at H, simp [mul_apply, H] } end lemma single_mul_apply_aux [has_mul G] (f : monoid_algebra k G) {r : k} {x y z : G} (H : ∀ a, x * a = y ↔ a = z) : (single x r * f) y = r * f z := have f.sum (λ a b, ite (x * a = y) (0 * b) 0) = 0, by simp, calc (single x r * f) y = sum f (λ a b, ite (x * a = y) (r * b) 0) : (mul_apply _ _ _).trans $ sum_single_index this ... = f.sum (λ a b, ite (a = z) (r * b) 0) : by { simp only [H], congr' with g s, split_ifs; refl } ... = if z ∈ f.support then (r * f z) else 0 : f.support.sum_ite_eq' _ _ ... = _ : by split_ifs with h; simp at h; simp [h] lemma single_one_mul_apply [mul_one_class G] (f : monoid_algebra k G) (r : k) (x : G) : (single 1 r * f) x = r * f x := f.single_mul_apply_aux $ λ a, by rw [one_mul] lemma lift_nc_smul [mul_one_class G] {R : Type*} [semiring R] (f : k →+* R) (g : G →* R) (c : k) (φ : monoid_algebra k G) : lift_nc (f : k →+ R) g (c • φ) = f c * lift_nc (f : k →+ R) g φ := begin suffices : (lift_nc ↑f g).comp (smul_add_hom k (monoid_algebra k G) c) = (add_monoid_hom.mul_left (f c)).comp (lift_nc ↑f g), from add_monoid_hom.congr_fun this φ, ext a b, simp [mul_assoc] end end misc_theorems /-! #### Non-unital, non-associative algebra structure -/ section non_unital_non_assoc_algebra variables {R : Type*} (k) [semiring R] [semiring k] [distrib_mul_action R k] [has_mul G] instance is_scalar_tower_self [is_scalar_tower R k k] : is_scalar_tower R (monoid_algebra k G) (monoid_algebra k G) := ⟨λ t a b, begin ext m, simp only [mul_apply, finsupp.smul_sum, smul_ite, smul_mul_assoc, sum_smul_index', zero_mul, if_t_t, implies_true_iff, eq_self_iff_true, sum_zero, coe_smul, smul_eq_mul, pi.smul_apply, smul_zero], end⟩ /-- Note that if `k` is a `comm_semiring` then we have `smul_comm_class k k k` and so we can take `R = k` in the below. In other words, if the coefficients are commutative amongst themselves, they also commute with the algebra multiplication. -/ instance smul_comm_class_self [smul_comm_class R k k] : smul_comm_class R (monoid_algebra k G) (monoid_algebra k G) := ⟨λ t a b, begin ext m, simp only [mul_apply, finsupp.sum, finset.smul_sum, smul_ite, mul_smul_comm, sum_smul_index', implies_true_iff, eq_self_iff_true, coe_smul, ite_eq_right_iff, smul_eq_mul, pi.smul_apply, mul_zero, smul_zero], end⟩ instance smul_comm_class_symm_self [smul_comm_class k R k] : smul_comm_class (monoid_algebra k G) R (monoid_algebra k G) := ⟨λ t a b, by { haveI := smul_comm_class.symm k R k, rw ← smul_comm, } ⟩ variables {A : Type u₃} [non_unital_non_assoc_semiring A] /-- A non_unital `k`-algebra homomorphism from `monoid_algebra k G` is uniquely defined by its values on the functions `single a 1`. -/ lemma non_unital_alg_hom_ext [distrib_mul_action k A] {φ₁ φ₂ : non_unital_alg_hom k (monoid_algebra k G) A} (h : ∀ x, φ₁ (single x 1) = φ₂ (single x 1)) : φ₁ = φ₂ := non_unital_alg_hom.to_distrib_mul_action_hom_injective $ finsupp.distrib_mul_action_hom_ext' $ λ a, distrib_mul_action_hom.ext_ring (h a) /-- See note [partially-applied ext lemmas]. -/ @[ext] lemma non_unital_alg_hom_ext' [distrib_mul_action k A] {φ₁ φ₂ : non_unital_alg_hom k (monoid_algebra k G) A} (h : φ₁.to_mul_hom.comp (of_magma k G) = φ₂.to_mul_hom.comp (of_magma k G)) : φ₁ = φ₂ := non_unital_alg_hom_ext k $ mul_hom.congr_fun h /-- The functor `G ↦ monoid_algebra k G`, from the category of magmas to the category of non-unital, non-associative algebras over `k` is adjoint to the forgetful functor in the other direction. -/ @[simps] def lift_magma [module k A] [is_scalar_tower k A A] [smul_comm_class k A A] : mul_hom G A ≃ non_unital_alg_hom k (monoid_algebra k G) A := { to_fun := λ f, { to_fun := λ a, a.sum (λ m t, t • f m), map_smul' := λ t' a, begin rw [finsupp.smul_sum, sum_smul_index'], { simp_rw smul_assoc, }, { intros m, exact zero_smul k (f m), }, end, map_mul' := λ a₁ a₂, begin let g : G → k → A := λ m t, t • f m, have h₁ : ∀ m, g m 0 = 0, { intros, exact zero_smul k (f m), }, have h₂ : ∀ m (t₁ t₂ : k), g m (t₁ + t₂) = g m t₁ + g m t₂, { intros, rw ← add_smul, }, simp_rw [finsupp.mul_sum, finsupp.sum_mul, smul_mul_smul, ← f.map_mul, mul_def, sum_comm a₂ a₁, sum_sum_index h₁ h₂, sum_single_index (h₁ _)], end, .. lift_add_hom (λ x, (smul_add_hom k A).flip (f x)) }, inv_fun := λ F, F.to_mul_hom.comp (of_magma k G), left_inv := λ f, by { ext m, simp only [non_unital_alg_hom.coe_mk, of_magma_apply, non_unital_alg_hom.to_mul_hom_eq_coe, sum_single_index, function.comp_app, one_smul, zero_smul, mul_hom.coe_comp, non_unital_alg_hom.coe_to_mul_hom], }, right_inv := λ F, by { ext m, simp only [non_unital_alg_hom.coe_mk, of_magma_apply, non_unital_alg_hom.to_mul_hom_eq_coe, sum_single_index, function.comp_app, one_smul, zero_smul, mul_hom.coe_comp, non_unital_alg_hom.coe_to_mul_hom], }, } end non_unital_non_assoc_algebra /-! #### Algebra structure -/ section algebra local attribute [reducible] monoid_algebra lemma single_one_comm [comm_semiring k] [mul_one_class G] (r : k) (f : monoid_algebra k G) : single 1 r * f = f * single 1 r := by { ext, rw [single_one_mul_apply, mul_single_one_apply, mul_comm] } /-- `finsupp.single 1` as a `ring_hom` -/ @[simps] def single_one_ring_hom [semiring k] [monoid G] : k →+* monoid_algebra k G := { map_one' := rfl, map_mul' := λ x y, by rw [single_add_hom, single_mul_single, one_mul], ..finsupp.single_add_hom 1} /-- If two ring homomorphisms from `monoid_algebra k G` are equal on all `single a 1` and `single 1 b`, then they are equal. -/ lemma ring_hom_ext {R} [semiring k] [monoid G] [semiring R] {f g : monoid_algebra k G →+* R} (h₁ : ∀ b, f (single 1 b) = g (single 1 b)) (h_of : ∀ a, f (single a 1) = g (single a 1)) : f = g := ring_hom.coe_add_monoid_hom_injective $ add_hom_ext $ λ a b, by rw [← one_mul a, ← mul_one b, ← single_mul_single, f.coe_add_monoid_hom, g.coe_add_monoid_hom, f.map_mul, g.map_mul, h₁, h_of] /-- If two ring homomorphisms from `monoid_algebra k G` are equal on all `single a 1` and `single 1 b`, then they are equal. See note [partially-applied ext lemmas]. -/ @[ext] lemma ring_hom_ext' {R} [semiring k] [monoid G] [semiring R] {f g : monoid_algebra k G →+* R} (h₁ : f.comp single_one_ring_hom = g.comp single_one_ring_hom) (h_of : (f : monoid_algebra k G →* R).comp (of k G) = (g : monoid_algebra k G →* R).comp (of k G)) : f = g := ring_hom_ext (ring_hom.congr_fun h₁) (monoid_hom.congr_fun h_of) /-- The instance `algebra k (monoid_algebra A G)` whenever we have `algebra k A`. In particular this provides the instance `algebra k (monoid_algebra k G)`. -/ instance {A : Type*} [comm_semiring k] [semiring A] [algebra k A] [monoid G] : algebra k (monoid_algebra A G) := { smul_def' := λ r a, by { ext, simp [single_one_mul_apply, algebra.smul_def'', pi.smul_apply], }, commutes' := λ r f, by { ext, simp [single_one_mul_apply, mul_single_one_apply, algebra.commutes], }, ..single_one_ring_hom.comp (algebra_map k A) } /-- `finsupp.single 1` as a `alg_hom` -/ @[simps] def single_one_alg_hom {A : Type*} [comm_semiring k] [semiring A] [algebra k A] [monoid G] : A →ₐ[k] monoid_algebra A G := { commutes' := λ r, by { ext, simp, refl, }, ..single_one_ring_hom} @[simp] lemma coe_algebra_map {A : Type*} [comm_semiring k] [semiring A] [algebra k A] [monoid G] : ⇑(algebra_map k (monoid_algebra A G)) = single 1 ∘ (algebra_map k A) := rfl lemma single_eq_algebra_map_mul_of [comm_semiring k] [monoid G] (a : G) (b : k) : single a b = algebra_map k (monoid_algebra k G) b * of k G a := by simp lemma single_algebra_map_eq_algebra_map_mul_of {A : Type*} [comm_semiring k] [semiring A] [algebra k A] [monoid G] (a : G) (b : k) : single a (algebra_map k A b) = algebra_map k (monoid_algebra A G) b * of A G a := by simp lemma induction_on [semiring k] [monoid G] {p : monoid_algebra k G → Prop} (f : monoid_algebra k G) (hM : ∀ g, p (of k G g)) (hadd : ∀ f g : monoid_algebra k G, p f → p g → p (f + g)) (hsmul : ∀ (r : k) f, p f → p (r • f)) : p f := begin refine finsupp.induction_linear f _ (λ f g hf hg, hadd f g hf hg) (λ g r, _), { simpa using hsmul 0 (of k G 1) (hM 1) }, { convert hsmul r (of k G g) (hM g), simp only [mul_one, smul_single', of_apply] }, end end algebra section lift variables {k G} [comm_semiring k] [monoid G] variables {A : Type u₃} [semiring A] [algebra k A] {B : Type*} [semiring B] [algebra k B] /-- `lift_nc_ring_hom` as a `alg_hom`, for when `f` is an `alg_hom` -/ def lift_nc_alg_hom (f : A →ₐ[k] B) (g : G →* B) (h_comm : ∀ x y, commute (f x) (g y)) : monoid_algebra A G →ₐ[k] B := { to_fun := lift_nc_ring_hom (f : A →+* B) g h_comm, commutes' := by simp [lift_nc_ring_hom], ..(lift_nc_ring_hom (f : A →+* B) g h_comm)} /-- A `k`-algebra homomorphism from `monoid_algebra k G` is uniquely defined by its values on the functions `single a 1`. -/ lemma alg_hom_ext ⦃φ₁ φ₂ : monoid_algebra k G →ₐ[k] A⦄ (h : ∀ x, φ₁ (single x 1) = φ₂ (single x 1)) : φ₁ = φ₂ := alg_hom.to_linear_map_injective $ finsupp.lhom_ext' $ λ a, linear_map.ext_ring (h a) /-- See note [partially-applied ext lemmas]. -/ @[ext] lemma alg_hom_ext' ⦃φ₁ φ₂ : monoid_algebra k G →ₐ[k] A⦄ (h : (φ₁ : monoid_algebra k G →* A).comp (of k G) = (φ₂ : monoid_algebra k G →* A).comp (of k G)) : φ₁ = φ₂ := alg_hom_ext $ monoid_hom.congr_fun h variables (k G A) /-- Any monoid homomorphism `G →* A` can be lifted to an algebra homomorphism `monoid_algebra k G →ₐ[k] A`. -/ def lift : (G →* A) ≃ (monoid_algebra k G →ₐ[k] A) := { inv_fun := λ f, (f : monoid_algebra k G →* A).comp (of k G), to_fun := λ F, lift_nc_alg_hom (algebra.of_id k A) F $ λ _ _, algebra.commutes _ _, left_inv := λ f, by { ext, simp [lift_nc_alg_hom, lift_nc_ring_hom] }, right_inv := λ F, by { ext, simp [lift_nc_alg_hom, lift_nc_ring_hom] } } variables {k G A} lemma lift_apply' (F : G →* A) (f : monoid_algebra k G) : lift k G A F f = f.sum (λ a b, (algebra_map k A b) * F a) := rfl lemma lift_apply (F : G →* A) (f : monoid_algebra k G) : lift k G A F f = f.sum (λ a b, b • F a) := by simp only [lift_apply', algebra.smul_def] lemma lift_def (F : G →* A) : ⇑(lift k G A F) = lift_nc ((algebra_map k A : k →+* A) : k →+ A) F := rfl @[simp] lemma lift_symm_apply (F : monoid_algebra k G →ₐ[k] A) (x : G) : (lift k G A).symm F x = F (single x 1) := rfl lemma lift_of (F : G →* A) (x) : lift k G A F (of k G x) = F x := by rw [of_apply, ← lift_symm_apply, equiv.symm_apply_apply] @[simp] lemma lift_single (F : G →* A) (a b) : lift k G A F (single a b) = b • F a := by rw [lift_def, lift_nc_single, algebra.smul_def, ring_hom.coe_add_monoid_hom] lemma lift_unique' (F : monoid_algebra k G →ₐ[k] A) : F = lift k G A ((F : monoid_algebra k G →* A).comp (of k G)) := ((lift k G A).apply_symm_apply F).symm /-- Decomposition of a `k`-algebra homomorphism from `monoid_algebra k G` by its values on `F (single a 1)`. -/ lemma lift_unique (F : monoid_algebra k G →ₐ[k] A) (f : monoid_algebra k G) : F f = f.sum (λ a b, b • F (single a 1)) := by conv_lhs { rw lift_unique' F, simp [lift_apply] } end lift section local attribute [reducible] monoid_algebra variables (k) /-- When `V` is a `k[G]`-module, multiplication by a group element `g` is a `k`-linear map. -/ def group_smul.linear_map [monoid G] [comm_semiring k] (V : Type u₃) [add_comm_monoid V] [module k V] [module (monoid_algebra k G) V] [is_scalar_tower k (monoid_algebra k G) V] (g : G) : V →ₗ[k] V := { to_fun := λ v, (single g (1 : k) • v : V), map_add' := λ x y, smul_add (single g (1 : k)) x y, map_smul' := λ c x, smul_algebra_smul_comm _ _ _ } @[simp] lemma group_smul.linear_map_apply [monoid G] [comm_semiring k] (V : Type u₃) [add_comm_monoid V] [module k V] [module (monoid_algebra k G) V] [is_scalar_tower k (monoid_algebra k G) V] (g : G) (v : V) : (group_smul.linear_map k V g) v = (single g (1 : k) • v : V) := rfl section variables {k} variables [monoid G] [comm_semiring k] {V W : Type u₃} [add_comm_monoid V] [module k V] [module (monoid_algebra k G) V] [is_scalar_tower k (monoid_algebra k G) V] [add_comm_monoid W] [module k W] [module (monoid_algebra k G) W] [is_scalar_tower k (monoid_algebra k G) W] (f : V →ₗ[k] W) (h : ∀ (g : G) (v : V), f (single g (1 : k) • v : V) = (single g (1 : k) • (f v) : W)) include h /-- Build a `k[G]`-linear map from a `k`-linear map and evidence that it is `G`-equivariant. -/ def equivariant_of_linear_of_comm : V →ₗ[monoid_algebra k G] W := { to_fun := f, map_add' := λ v v', by simp, map_smul' := λ c v, begin apply finsupp.induction c, { simp, }, { intros g r c' nm nz w, dsimp at *, simp only [add_smul, f.map_add, w, add_left_inj, single_eq_algebra_map_mul_of, ← smul_smul], erw [algebra_map_smul (monoid_algebra k G) r, algebra_map_smul (monoid_algebra k G) r, f.map_smul, h g v, of_apply], all_goals { apply_instance } } end, } @[simp] lemma equivariant_of_linear_of_comm_apply (v : V) : (equivariant_of_linear_of_comm f h) v = f v := rfl end end section universe ui variable {ι : Type ui} local attribute [reducible] monoid_algebra lemma prod_single [comm_semiring k] [comm_monoid G] {s : finset ι} {a : ι → G} {b : ι → k} : (∏ i in s, single (a i) (b i)) = single (∏ i in s, a i) (∏ i in s, b i) := finset.induction_on s rfl $ λ a s has ih, by rw [prod_insert has, ih, single_mul_single, prod_insert has, prod_insert has] end section -- We now prove some additional statements that hold for group algebras. variables [semiring k] [group G] local attribute [reducible] monoid_algebra @[simp] lemma mul_single_apply (f : monoid_algebra k G) (r : k) (x y : G) : (f * single x r) y = f (y * x⁻¹) * r := f.mul_single_apply_aux $ λ a, eq_mul_inv_iff_mul_eq.symm @[simp] lemma single_mul_apply (r : k) (x : G) (f : monoid_algebra k G) (y : G) : (single x r * f) y = r * f (x⁻¹ * y) := f.single_mul_apply_aux $ λ z, eq_inv_mul_iff_mul_eq.symm lemma mul_apply_left (f g : monoid_algebra k G) (x : G) : (f * g) x = (f.sum $ λ a b, b * (g (a⁻¹ * x))) := calc (f * g) x = sum f (λ a b, (single a b * g) x) : by rw [← finsupp.sum_apply, ← finsupp.sum_mul, f.sum_single] ... = _ : by simp only [single_mul_apply, finsupp.sum] -- If we'd assumed `comm_semiring`, we could deduce this from `mul_apply_left`. lemma mul_apply_right (f g : monoid_algebra k G) (x : G) : (f * g) x = (g.sum $ λa b, (f (x * a⁻¹)) * b) := calc (f * g) x = sum g (λ a b, (f * single a b) x) : by rw [← finsupp.sum_apply, ← finsupp.mul_sum, g.sum_single] ... = _ : by simp only [mul_single_apply, finsupp.sum] end section span variables [semiring k] [mul_one_class G] /-- An element of `monoid_algebra R M` is in the subalgebra generated by its support. -/ lemma mem_span_support (f : monoid_algebra k G) : f ∈ submodule.span k (of k G '' (f.support : set G)) := by rw [of, monoid_hom.coe_mk, ← finsupp.supported_eq_span_single, finsupp.mem_supported] end span section opposite open finsupp opposite variables [semiring k] /-- The opposite of an `monoid_algebra R I` equivalent as a ring to the `monoid_algebra Rᵒᵖ Iᵒᵖ` over the opposite ring, taking elements to their opposite. -/ @[simps {simp_rhs := tt}] protected noncomputable def op_ring_equiv [monoid G] : (monoid_algebra k G)ᵒᵖ ≃+* monoid_algebra kᵒᵖ Gᵒᵖ := { map_mul' := begin dsimp only [add_equiv.to_fun_eq_coe, ←add_equiv.coe_to_add_monoid_hom], rw add_monoid_hom.map_mul_iff, ext i₁ r₁ i₂ r₂ : 6, simp end, ..op_add_equiv.symm.trans $ (finsupp.map_range.add_equiv (op_add_equiv : k ≃+ kᵒᵖ)).trans $ finsupp.dom_congr equiv_to_opposite } @[simp] lemma op_ring_equiv_single [monoid G] (r : k) (x : G) : monoid_algebra.op_ring_equiv (op (single x r)) = single (op x) (op r) := by simp @[simp] lemma op_ring_equiv_symm_single [monoid G] (r : kᵒᵖ) (x : Gᵒᵖ) : monoid_algebra.op_ring_equiv.symm (single x r) = op (single x.unop r.unop) := by simp end opposite end monoid_algebra /-! ### Additive monoids -/ section variables [semiring k] /-- The monoid algebra over a semiring `k` generated by the additive monoid `G`. It is the type of finite formal `k`-linear combinations of terms of `G`, endowed with the convolution product. -/ @[derive [inhabited, add_comm_monoid, has_coe_to_fun]] def add_monoid_algebra := G →₀ k end namespace add_monoid_algebra variables {k G} section has_mul variables [semiring k] [has_add G] /-- The product of `f g : add_monoid_algebra k G` is the finitely supported function whose value at `a` is the sum of `f x * g y` over all pairs `x, y` such that `x + y = a`. (Think of the product of multivariate polynomials where `α` is the additive monoid of monomial exponents.) -/ instance : has_mul (add_monoid_algebra k G) := ⟨λf g, f.sum $ λa₁ b₁, g.sum $ λa₂ b₂, single (a₁ + a₂) (b₁ * b₂)⟩ lemma mul_def {f g : add_monoid_algebra k G} : f * g = (f.sum $ λa₁ b₁, g.sum $ λa₂ b₂, single (a₁ + a₂) (b₁ * b₂)) := rfl instance : non_unital_non_assoc_semiring (add_monoid_algebra k G) := { zero := 0, mul := (*), add := (+), left_distrib := assume f g h, by simp only [mul_def, sum_add_index, mul_add, mul_zero, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, sum_add], right_distrib := assume f g h, by simp only [mul_def, sum_add_index, add_mul, mul_zero, zero_mul, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, sum_zero, sum_add], zero_mul := assume f, by simp only [mul_def, sum_zero_index], mul_zero := assume f, by simp only [mul_def, sum_zero_index, sum_zero], nsmul := λ n f, n • f, nsmul_zero' := by { intros, ext, simp [-nsmul_eq_mul, add_smul] }, nsmul_succ' := by { intros, ext, simp [-nsmul_eq_mul, nat.succ_eq_one_add, add_smul] }, .. finsupp.add_comm_monoid } end has_mul section has_one variables [semiring k] [has_zero G] /-- The unit of the multiplication is `single 1 1`, i.e. the function that is `1` at `0` and zero elsewhere. -/ instance : has_one (add_monoid_algebra k G) := ⟨single 0 1⟩ lemma one_def : (1 : add_monoid_algebra k G) = single 0 1 := rfl end has_one section semigroup variables [semiring k] [add_semigroup G] instance : non_unital_semiring (add_monoid_algebra k G) := { zero := 0, mul := (*), add := (+), mul_assoc := assume f g h, by simp only [mul_def, sum_sum_index, sum_zero_index, sum_add_index, sum_single_index, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, add_mul, mul_add, add_assoc, mul_assoc, zero_mul, mul_zero, sum_zero, sum_add], .. add_monoid_algebra.non_unital_non_assoc_semiring } end semigroup section mul_one_class variables [semiring k] [add_zero_class G] instance : non_assoc_semiring (add_monoid_algebra k G) := { one := 1, mul := (*), zero := 0, add := (+), one_mul := assume f, by simp only [mul_def, one_def, sum_single_index, zero_mul, single_zero, sum_zero, zero_add, one_mul, sum_single], mul_one := assume f, by simp only [mul_def, one_def, sum_single_index, mul_zero, single_zero, sum_zero, add_zero, mul_one, sum_single], .. add_monoid_algebra.non_unital_non_assoc_semiring } variables {R : Type*} [semiring R] /-- A non-commutative version of `add_monoid_algebra.lift`: given a additive homomorphism `f : k →+ R` and a multiplicative monoid homomorphism `g : multiplicative G →* R`, returns the additive homomorphism from `add_monoid_algebra k G` such that `lift_nc f g (single a b) = f b * g a`. If `f` is a ring homomorphism and the range of either `f` or `g` is in center of `R`, then the result is a ring homomorphism. If `R` is a `k`-algebra and `f = algebra_map k R`, then the result is an algebra homomorphism called `add_monoid_algebra.lift`. -/ def lift_nc (f : k →+ R) (g : multiplicative G →* R) : add_monoid_algebra k G →+ R := lift_add_hom (λ x : G, (add_monoid_hom.mul_right (g $ multiplicative.of_add x)).comp f) @[simp] lemma lift_nc_single (f : k →+ R) (g : multiplicative G →* R) (a : G) (b : k) : lift_nc f g (single a b) = f b * g (multiplicative.of_add a) := lift_add_hom_apply_single _ _ _ @[simp] lemma lift_nc_one (f : k →+* R) (g : multiplicative G →* R) : lift_nc (f : k →+ R) g 1 = 1 := @monoid_algebra.lift_nc_one k (multiplicative G) _ _ _ _ f g lemma lift_nc_mul (f : k →+* R) (g : multiplicative G →* R) (a b : add_monoid_algebra k G) (h_comm : ∀ {x y}, y ∈ a.support → commute (f (b x)) (g $ multiplicative.of_add y)) : lift_nc (f : k →+ R) g (a * b) = lift_nc (f : k →+ R) g a * lift_nc (f : k →+ R) g b := @monoid_algebra.lift_nc_mul k (multiplicative G) _ _ _ _ f g a b @h_comm end mul_one_class /-! #### Semiring structure -/ section semiring instance {R : Type*} [monoid R] [semiring k] [distrib_mul_action R k] : has_scalar R (add_monoid_algebra k G) := finsupp.has_scalar variables [semiring k] [add_monoid G] instance : semiring (add_monoid_algebra k G) := { one := 1, mul := (*), zero := 0, add := (+), .. add_monoid_algebra.non_unital_semiring, .. add_monoid_algebra.non_assoc_semiring, } variables {R : Type*} [semiring R] /-- `lift_nc` as a `ring_hom`, for when `f` and `g` commute -/ def lift_nc_ring_hom (f : k →+* R) (g : multiplicative G →* R) (h_comm : ∀ x y, commute (f x) (g y)) : add_monoid_algebra k G →+* R := { to_fun := lift_nc (f : k →+ R) g, map_one' := lift_nc_one _ _, map_mul' := λ a b, lift_nc_mul _ _ _ _ $ λ _ _ _, h_comm _ _, ..(lift_nc (f : k →+ R) g)} end semiring instance [comm_semiring k] [add_comm_monoid G] : comm_semiring (add_monoid_algebra k G) := { mul_comm := @mul_comm (monoid_algebra k $ multiplicative G) _, .. add_monoid_algebra.semiring } instance [semiring k] [nontrivial k] [nonempty G] : nontrivial (add_monoid_algebra k G) := finsupp.nontrivial /-! #### Derived instances -/ section derived_instances instance [semiring k] [subsingleton k] : unique (add_monoid_algebra k G) := finsupp.unique_of_right instance [ring k] : add_group (add_monoid_algebra k G) := finsupp.add_group instance [ring k] [add_monoid G] : ring (add_monoid_algebra k G) := { neg := has_neg.neg, add_left_neg := add_left_neg, sub := has_sub.sub, sub_eq_add_neg := finsupp.add_group.sub_eq_add_neg, .. add_monoid_algebra.semiring } instance [comm_ring k] [add_comm_monoid G] : comm_ring (add_monoid_algebra k G) := { mul_comm := mul_comm, .. add_monoid_algebra.ring} variables {R S : Type*} instance [monoid R] [semiring k] [distrib_mul_action R k] : distrib_mul_action R (add_monoid_algebra k G) := finsupp.distrib_mul_action G k instance [monoid R] [semiring k] [distrib_mul_action R k] [has_faithful_scalar R k] [nonempty G] : has_faithful_scalar R (add_monoid_algebra k G) := finsupp.has_faithful_scalar instance [semiring R] [semiring k] [module R k] : module R (add_monoid_algebra k G) := finsupp.module G k instance [monoid R] [monoid S] [semiring k] [distrib_mul_action R k] [distrib_mul_action S k] [has_scalar R S] [is_scalar_tower R S k] : is_scalar_tower R S (add_monoid_algebra k G) := finsupp.is_scalar_tower G k instance [monoid R] [monoid S] [semiring k] [distrib_mul_action R k] [distrib_mul_action S k] [smul_comm_class R S k] : smul_comm_class R S (add_monoid_algebra k G) := finsupp.smul_comm_class G k /-! It is hard to state the equivalent of `distrib_mul_action G (add_monoid_algebra k G)` because we've never discussed actions of additive groups. -/ end derived_instances section misc_theorems variables [semiring k] lemma mul_apply [has_add G] (f g : add_monoid_algebra k G) (x : G) : (f * g) x = (f.sum $ λa₁ b₁, g.sum $ λa₂ b₂, if a₁ + a₂ = x then b₁ * b₂ else 0) := @monoid_algebra.mul_apply k (multiplicative G) _ _ _ _ _ lemma mul_apply_antidiagonal [has_add G] (f g : add_monoid_algebra k G) (x : G) (s : finset (G × G)) (hs : ∀ {p : G × G}, p ∈ s ↔ p.1 + p.2 = x) : (f * g) x = ∑ p in s, (f p.1 * g p.2) := @monoid_algebra.mul_apply_antidiagonal k (multiplicative G) _ _ _ _ _ s @hs lemma support_mul [has_add G] (a b : add_monoid_algebra k G) : (a * b).support ⊆ a.support.bUnion (λa₁, b.support.bUnion $ λa₂, {a₁ + a₂}) := @monoid_algebra.support_mul k (multiplicative G) _ _ _ _ lemma single_mul_single [has_add G] {a₁ a₂ : G} {b₁ b₂ : k} : (single a₁ b₁ * single a₂ b₂ : add_monoid_algebra k G) = single (a₁ + a₂) (b₁ * b₂) := @monoid_algebra.single_mul_single k (multiplicative G) _ _ _ _ _ _ -- This should be a `@[simp]` lemma, but the simp_nf linter times out if we add this. -- Probably the correct fix is to make a `[add_]monoid_algebra.single` with the correct type, -- instead of relying on `finsupp.single`. lemma single_pow [add_monoid G] {a : G} {b : k} : ∀ n : ℕ, ((single a b)^n : add_monoid_algebra k G) = single (n • a) (b ^ n) | 0 := by { simp only [pow_zero, zero_nsmul], refl } | (n+1) := by rw [pow_succ, pow_succ, single_pow n, single_mul_single, add_comm, add_nsmul, one_nsmul] /-- Like `finsupp.map_domain_add`, but for the convolutive multiplication we define in this file -/ lemma map_domain_mul {α : Type*} {β : Type*} {α₂ : Type*} [semiring β] [has_add α] [has_add α₂] {x y : add_monoid_algebra β α} (f : add_hom α α₂) : (map_domain f (x * y : add_monoid_algebra β α) : add_monoid_algebra β α₂) = (map_domain f x * map_domain f y : add_monoid_algebra β α₂) := begin simp_rw [mul_def, map_domain_sum, map_domain_single, f.map_add], rw finsupp.sum_map_domain_index, { congr, ext a b, rw finsupp.sum_map_domain_index, { simp }, { simp [mul_add] } }, { simp }, { simp [add_mul] } end section variables (k G) /-- The embedding of an additive magma into its additive magma algebra. -/ @[simps] def of_magma [has_add G] : mul_hom (multiplicative G) (add_monoid_algebra k G) := { to_fun := λ a, single a 1, map_mul' := λ a b, by simpa only [mul_def, mul_one, sum_single_index, single_eq_zero, mul_zero], } /-- Embedding of a magma with zero into its magma algebra. -/ def of [add_zero_class G] : multiplicative G →* add_monoid_algebra k G := { to_fun := λ a, single a 1, map_one' := rfl, .. of_magma k G } /-- Embedding of a magma with zero `G`, into its magma algebra, having `G` as source. -/ def of' : G → add_monoid_algebra k G := λ a, single a 1 end @[simp] lemma of_apply [add_zero_class G] (a : multiplicative G) : of k G a = single a.to_add 1 := rfl @[simp] lemma of'_apply (a : G) : of' k G a = single a 1 := rfl lemma of'_eq_of [add_zero_class G] (a : G) : of' k G a = of k G a := rfl lemma of_injective [nontrivial k] [add_zero_class G] : function.injective (of k G) := λ a b h, by simpa using (single_eq_single_iff _ _ _ _).mp h lemma mul_single_apply_aux [has_add G] (f : add_monoid_algebra k G) (r : k) (x y z : G) (H : ∀ a, a + x = z ↔ a = y) : (f * single x r) z = f y * r := @monoid_algebra.mul_single_apply_aux k (multiplicative G) _ _ _ _ _ _ _ H lemma mul_single_zero_apply [add_zero_class G] (f : add_monoid_algebra k G) (r : k) (x : G) : (f * single 0 r) x = f x * r := f.mul_single_apply_aux r _ _ _ $ λ a, by rw [add_zero] lemma single_mul_apply_aux [has_add G] (f : add_monoid_algebra k G) (r : k) (x y z : G) (H : ∀ a, x + a = y ↔ a = z) : (single x r * f : add_monoid_algebra k G) y = r * f z := @monoid_algebra.single_mul_apply_aux k (multiplicative G) _ _ _ _ _ _ _ H lemma single_zero_mul_apply [add_zero_class G] (f : add_monoid_algebra k G) (r : k) (x : G) : (single 0 r * f : add_monoid_algebra k G) x = r * f x := f.single_mul_apply_aux r _ _ _ $ λ a, by rw [zero_add] lemma mul_single_apply [add_group G] (f : add_monoid_algebra k G) (r : k) (x y : G) : (f * single x r) y = f (y - x) * r := (sub_eq_add_neg y x).symm ▸ @monoid_algebra.mul_single_apply k (multiplicative G) _ _ _ _ _ _ lemma single_mul_apply [add_group G] (r : k) (x : G) (f : add_monoid_algebra k G) (y : G) : (single x r * f : add_monoid_algebra k G) y = r * f (- x + y) := @monoid_algebra.single_mul_apply k (multiplicative G) _ _ _ _ _ _ lemma support_mul_single [add_right_cancel_semigroup G] (f : add_monoid_algebra k G) (r : k) (hr : ∀ y, y * r = 0 ↔ y = 0) (x : G) : (f * single x r : add_monoid_algebra k G).support = f.support.map (add_right_embedding x) := @monoid_algebra.support_mul_single k (multiplicative G) _ _ _ _ hr _ lemma lift_nc_smul {R : Type*} [add_zero_class G] [semiring R] (f : k →+* R) (g : multiplicative G →* R) (c : k) (φ : monoid_algebra k G) : lift_nc (f : k →+ R) g (c • φ) = f c * lift_nc (f : k →+ R) g φ := @monoid_algebra.lift_nc_smul k (multiplicative G) _ _ _ _ f g c φ variables {k G} lemma induction_on [add_monoid G] {p : add_monoid_algebra k G → Prop} (f : add_monoid_algebra k G) (hM : ∀ g, p (of k G (multiplicative.of_add g))) (hadd : ∀ f g : add_monoid_algebra k G, p f → p g → p (f + g)) (hsmul : ∀ (r : k) f, p f → p (r • f)) : p f := begin refine finsupp.induction_linear f _ (λ f g hf hg, hadd f g hf hg) (λ g r, _), { simpa using hsmul 0 (of k G (multiplicative.of_add 0)) (hM 0) }, { convert hsmul r (of k G (multiplicative.of_add g)) (hM g), simp only [mul_one, to_add_of_add, smul_single', of_apply] }, end end misc_theorems section span variables [semiring k] /-- An element of `add_monoid_algebra R M` is in the submodule generated by its support. -/ lemma mem_span_support [add_zero_class G] (f : add_monoid_algebra k G) : f ∈ submodule.span k (of k G '' (f.support : set G)) := by rw [of, monoid_hom.coe_mk, ← finsupp.supported_eq_span_single, finsupp.mem_supported] /-- An element of `add_monoid_algebra R M` is in the subalgebra generated by its support, using unbundled inclusion. -/ lemma mem_span_support' (f : add_monoid_algebra k G) : f ∈ submodule.span k (of' k G '' (f.support : set G)) := by rw [of', ← finsupp.supported_eq_span_single, finsupp.mem_supported] end span end add_monoid_algebra /-! #### Conversions between `add_monoid_algebra` and `monoid_algebra` We have not defined `add_monoid_algebra k G = monoid_algebra k (multiplicative G)` because historically this caused problems; since the changes that have made `nsmul` definitional, this would be possible, but for now we just contruct the ring isomorphisms using `ring_equiv.refl _`. -/ /-- The equivalence between `add_monoid_algebra` and `monoid_algebra` in terms of `multiplicative` -/ protected def add_monoid_algebra.to_multiplicative [semiring k] [has_add G] : add_monoid_algebra k G ≃+* monoid_algebra k (multiplicative G) := { to_fun := equiv_map_domain multiplicative.of_add, map_mul' := λ x y, begin repeat {rw equiv_map_domain_eq_map_domain}, dsimp [multiplicative.of_add], convert monoid_algebra.map_domain_mul (mul_hom.id (multiplicative G)), end, ..finsupp.dom_congr multiplicative.of_add } /-- The equivalence between `monoid_algebra` and `add_monoid_algebra` in terms of `additive` -/ protected def monoid_algebra.to_additive [semiring k] [has_mul G] : monoid_algebra k G ≃+* add_monoid_algebra k (additive G) := { to_fun := equiv_map_domain additive.of_mul, map_mul' := λ x y, begin repeat {rw equiv_map_domain_eq_map_domain}, dsimp [additive.of_mul], convert monoid_algebra.map_domain_mul (mul_hom.id G), end, ..finsupp.dom_congr additive.of_mul } namespace add_monoid_algebra variables {k G} /-! #### Non-unital, non-associative algebra structure -/ section non_unital_non_assoc_algebra variables {R : Type*} (k) [semiring R] [semiring k] [distrib_mul_action R k] [has_add G] instance is_scalar_tower_self [is_scalar_tower R k k] : is_scalar_tower R (add_monoid_algebra k G) (add_monoid_algebra k G) := @monoid_algebra.is_scalar_tower_self k (multiplicative G) R _ _ _ _ _ /-- Note that if `k` is a `comm_semiring` then we have `smul_comm_class k k k` and so we can take `R = k` in the below. In other words, if the coefficients are commutative amongst themselves, they also commute with the algebra multiplication. -/ instance smul_comm_class_self [smul_comm_class R k k] : smul_comm_class R (add_monoid_algebra k G) (add_monoid_algebra k G) := @monoid_algebra.smul_comm_class_self k (multiplicative G) R _ _ _ _ _ instance smul_comm_class_symm_self [smul_comm_class k R k] : smul_comm_class (add_monoid_algebra k G) R (add_monoid_algebra k G) := @monoid_algebra.smul_comm_class_symm_self k (multiplicative G) R _ _ _ _ _ variables {A : Type u₃} [non_unital_non_assoc_semiring A] /-- A non_unital `k`-algebra homomorphism from `add_monoid_algebra k G` is uniquely defined by its values on the functions `single a 1`. -/ lemma non_unital_alg_hom_ext [distrib_mul_action k A] {φ₁ φ₂ : non_unital_alg_hom k (add_monoid_algebra k G) A} (h : ∀ x, φ₁ (single x 1) = φ₂ (single x 1)) : φ₁ = φ₂ := @monoid_algebra.non_unital_alg_hom_ext k (multiplicative G) _ _ _ _ _ φ₁ φ₂ h /-- See note [partially-applied ext lemmas]. -/ @[ext] lemma non_unital_alg_hom_ext' [distrib_mul_action k A] {φ₁ φ₂ : non_unital_alg_hom k (add_monoid_algebra k G) A} (h : φ₁.to_mul_hom.comp (of_magma k G) = φ₂.to_mul_hom.comp (of_magma k G)) : φ₁ = φ₂ := @monoid_algebra.non_unital_alg_hom_ext' k (multiplicative G) _ _ _ _ _ φ₁ φ₂ h /-- The functor `G ↦ add_monoid_algebra k G`, from the category of magmas to the category of non-unital, non-associative algebras over `k` is adjoint to the forgetful functor in the other direction. -/ @[simps] def lift_magma [module k A] [is_scalar_tower k A A] [smul_comm_class k A A] : mul_hom (multiplicative G) A ≃ non_unital_alg_hom k (add_monoid_algebra k G) A := { to_fun := λ f, { to_fun := λ a, sum a (λ m t, t • f (multiplicative.of_add m)), .. (monoid_algebra.lift_magma k f : _)}, inv_fun := λ F, F.to_mul_hom.comp (of_magma k G), .. (monoid_algebra.lift_magma k : mul_hom (multiplicative G) A ≃ non_unital_alg_hom k _ A) } end non_unital_non_assoc_algebra /-! #### Algebra structure -/ section algebra variables {R : Type*} local attribute [reducible] add_monoid_algebra /-- `finsupp.single 0` as a `ring_hom` -/ @[simps] def single_zero_ring_hom [semiring k] [add_monoid G] : k →+* add_monoid_algebra k G := { map_one' := rfl, map_mul' := λ x y, by rw [single_add_hom, single_mul_single, zero_add], ..finsupp.single_add_hom 0} /-- If two ring homomorphisms from `add_monoid_algebra k G` are equal on all `single a 1` and `single 0 b`, then they are equal. -/ lemma ring_hom_ext {R} [semiring k] [add_monoid G] [semiring R] {f g : add_monoid_algebra k G →+* R} (h₀ : ∀ b, f (single 0 b) = g (single 0 b)) (h_of : ∀ a, f (single a 1) = g (single a 1)) : f = g := @monoid_algebra.ring_hom_ext k (multiplicative G) R _ _ _ _ _ h₀ h_of /-- If two ring homomorphisms from `add_monoid_algebra k G` are equal on all `single a 1` and `single 0 b`, then they are equal. See note [partially-applied ext lemmas]. -/ @[ext] lemma ring_hom_ext' {R} [semiring k] [add_monoid G] [semiring R] {f g : add_monoid_algebra k G →+* R} (h₁ : f.comp single_zero_ring_hom = g.comp single_zero_ring_hom) (h_of : (f : add_monoid_algebra k G →* R).comp (of k G) = (g : add_monoid_algebra k G →* R).comp (of k G)) : f = g := ring_hom_ext (ring_hom.congr_fun h₁) (monoid_hom.congr_fun h_of) section opposite open finsupp opposite variables [semiring k] /-- The opposite of an `add_monoid_algebra R I` is ring equivalent to the `add_monoid_algebra Rᵒᵖ I` over the opposite ring, taking elements to their opposite. -/ @[simps {simp_rhs := tt}] protected noncomputable def op_ring_equiv [add_comm_monoid G] : (add_monoid_algebra k G)ᵒᵖ ≃+* add_monoid_algebra kᵒᵖ G := { map_mul' := begin dsimp only [add_equiv.to_fun_eq_coe, ←add_equiv.coe_to_add_monoid_hom], rw add_monoid_hom.map_mul_iff, ext i r i' r' : 6, dsimp, simp only [map_range_single, single_mul_single, ←op_mul, add_comm] end, ..opposite.op_add_equiv.symm.trans (finsupp.map_range.add_equiv (opposite.op_add_equiv : k ≃+ kᵒᵖ))} @[simp] lemma op_ring_equiv_single [add_comm_monoid G] (r : k) (x : G) : add_monoid_algebra.op_ring_equiv (op (single x r)) = single x (op r) := by simp @[simp] lemma op_ring_equiv_symm_single [add_comm_monoid G] (r : kᵒᵖ) (x : Gᵒᵖ) : add_monoid_algebra.op_ring_equiv.symm (single x r) = op (single x r.unop) := by simp end opposite /-- The instance `algebra R (add_monoid_algebra k G)` whenever we have `algebra R k`. In particular this provides the instance `algebra k (add_monoid_algebra k G)`. -/ instance [comm_semiring R] [semiring k] [algebra R k] [add_monoid G] : algebra R (add_monoid_algebra k G) := { smul_def' := λ r a, by { ext, simp [single_zero_mul_apply, algebra.smul_def'', pi.smul_apply], }, commutes' := λ r f, by { ext, simp [single_zero_mul_apply, mul_single_zero_apply, algebra.commutes], }, ..single_zero_ring_hom.comp (algebra_map R k) } /-- `finsupp.single 0` as a `alg_hom` -/ @[simps] def single_zero_alg_hom [comm_semiring R] [semiring k] [algebra R k] [add_monoid G] : k →ₐ[R] add_monoid_algebra k G := { commutes' := λ r, by { ext, simp, refl, }, ..single_zero_ring_hom} @[simp] lemma coe_algebra_map [comm_semiring R] [semiring k] [algebra R k] [add_monoid G] : (algebra_map R (add_monoid_algebra k G) : R → add_monoid_algebra k G) = single 0 ∘ (algebra_map R k) := rfl end algebra section lift variables {k G} [comm_semiring k] [add_monoid G] variables {A : Type u₃} [semiring A] [algebra k A] {B : Type*} [semiring B] [algebra k B] /-- `lift_nc_ring_hom` as a `alg_hom`, for when `f` is an `alg_hom` -/ def lift_nc_alg_hom (f : A →ₐ[k] B) (g : multiplicative G →* B) (h_comm : ∀ x y, commute (f x) (g y)) : add_monoid_algebra A G →ₐ[k] B := { to_fun := lift_nc_ring_hom (f : A →+* B) g h_comm, commutes' := by simp [lift_nc_ring_hom], ..(lift_nc_ring_hom (f : A →+* B) g h_comm)} /-- A `k`-algebra homomorphism from `monoid_algebra k G` is uniquely defined by its values on the functions `single a 1`. -/ lemma alg_hom_ext ⦃φ₁ φ₂ : add_monoid_algebra k G →ₐ[k] A⦄ (h : ∀ x, φ₁ (single x 1) = φ₂ (single x 1)) : φ₁ = φ₂ := @monoid_algebra.alg_hom_ext k (multiplicative G) _ _ _ _ _ _ _ h /-- See note [partially-applied ext lemmas]. -/ @[ext] lemma alg_hom_ext' ⦃φ₁ φ₂ : add_monoid_algebra k G →ₐ[k] A⦄ (h : (φ₁ : add_monoid_algebra k G →* A).comp (of k G) = (φ₂ : add_monoid_algebra k G →* A).comp (of k G)) : φ₁ = φ₂ := alg_hom_ext $ monoid_hom.congr_fun h variables (k G A) /-- Any monoid homomorphism `G →* A` can be lifted to an algebra homomorphism `monoid_algebra k G →ₐ[k] A`. -/ def lift : (multiplicative G →* A) ≃ (add_monoid_algebra k G →ₐ[k] A) := { inv_fun := λ f, (f : add_monoid_algebra k G →* A).comp (of k G), to_fun := λ F, { to_fun := lift_nc_alg_hom (algebra.of_id k A) F $ λ _ _, algebra.commutes _ _, .. @monoid_algebra.lift k (multiplicative G) _ _ A _ _ F}, .. @monoid_algebra.lift k (multiplicative G) _ _ A _ _ } variables {k G A} lemma lift_apply' (F : multiplicative G →* A) (f : monoid_algebra k G) : lift k G A F f = f.sum (λ a b, (algebra_map k A b) * F (multiplicative.of_add a)) := rfl lemma lift_apply (F : multiplicative G →* A) (f : monoid_algebra k G) : lift k G A F f = f.sum (λ a b, b • F (multiplicative.of_add a)) := by simp only [lift_apply', algebra.smul_def] lemma lift_def (F : multiplicative G →* A) : ⇑(lift k G A F) = lift_nc ((algebra_map k A : k →+* A) : k →+ A) F := rfl @[simp] lemma lift_symm_apply (F : add_monoid_algebra k G →ₐ[k] A) (x : multiplicative G) : (lift k G A).symm F x = F (single x.to_add 1) := rfl lemma lift_of (F : multiplicative G →* A) (x : multiplicative G) : lift k G A F (of k G x) = F x := by rw [of_apply, ← lift_symm_apply, equiv.symm_apply_apply] @[simp] lemma lift_single (F : multiplicative G →* A) (a b) : lift k G A F (single a b) = b • F (multiplicative.of_add a) := by rw [lift_def, lift_nc_single, algebra.smul_def, ring_hom.coe_add_monoid_hom] lemma lift_unique' (F : add_monoid_algebra k G →ₐ[k] A) : F = lift k G A ((F : add_monoid_algebra k G →* A).comp (of k G)) := ((lift k G A).apply_symm_apply F).symm /-- Decomposition of a `k`-algebra homomorphism from `monoid_algebra k G` by its values on `F (single a 1)`. -/ lemma lift_unique (F : add_monoid_algebra k G →ₐ[k] A) (f : monoid_algebra k G) : F f = f.sum (λ a b, b • F (single a 1)) := by conv_lhs { rw lift_unique' F, simp [lift_apply] } lemma alg_hom_ext_iff {φ₁ φ₂ : add_monoid_algebra k G →ₐ[k] A} : (∀ x, φ₁ (finsupp.single x 1) = φ₂ (finsupp.single x 1)) ↔ φ₁ = φ₂ := ⟨λ h, alg_hom_ext h, by rintro rfl _; refl⟩ end lift section local attribute [reducible] add_monoid_algebra universe ui variable {ι : Type ui} lemma prod_single [comm_semiring k] [add_comm_monoid G] {s : finset ι} {a : ι → G} {b : ι → k} : (∏ i in s, single (a i) (b i)) = single (∑ i in s, a i) (∏ i in s, b i) := finset.induction_on s rfl $ λ a s has ih, by rw [prod_insert has, ih, single_mul_single, sum_insert has, prod_insert has] end end add_monoid_algebra variables {R : Type*} [comm_semiring R] (k G) /-- The algebra equivalence between `add_monoid_algebra` and `monoid_algebra` in terms of `multiplicative`. -/ def add_monoid_algebra.to_multiplicative_alg_equiv [semiring k] [algebra R k] [add_monoid G] : add_monoid_algebra k G ≃ₐ[R] monoid_algebra k (multiplicative G) := { commutes' := λ r, by simp [add_monoid_algebra.to_multiplicative], ..add_monoid_algebra.to_multiplicative k G } /-- The algebra equivalence between `monoid_algebra` and `add_monoid_algebra` in terms of `additive`. -/ def monoid_algebra.to_additive_alg_equiv [semiring k] [algebra R k] [monoid G] : monoid_algebra k G ≃ₐ[R] add_monoid_algebra k (additive G) := { commutes' := λ r, by simp [monoid_algebra.to_additive], ..monoid_algebra.to_additive k G }
53c11db73683d21e6141064fd42dcef8e70ebff6
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/compiler/map_big.lean
5b35263d007b20c2ad0f87f4e2bd77d5939b8ff2
[ "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
220
lean
def f2 (n : Nat) (xs : List Nat) : List (List Nat) := let ys := List.replicate n 0; xs.map (fun x => x :: ys) def main : IO UInt32 := let n := 100000; IO.println (toString (f2 n (List.replicate n 0)).length) *> pure 0
1663081cc254fe3d15a3972a8d8dc2fd7c879e6e
37da0369b6c03e380e057bf680d81e6c9fdf9219
/hott/homotopy/connectedness.hlean
3fff7d6eff728542bd515e838be8cac9cdf9b6d7
[ "Apache-2.0" ]
permissive
kodyvajjha/lean2
72b120d95c3a1d77f54433fa90c9810e14a931a4
227fcad22ab2bc27bb7471be7911075d101ba3f9
refs/heads/master
1,627,157,512,295
1,501,855,676,000
1,504,809,427,000
109,317,326
0
0
null
1,509,839,253,000
1,509,655,713,000
C++
UTF-8
Lean
false
false
21,008
hlean
/- Copyright (c) 2015 Ulrik Buchholtz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Ulrik Buchholtz, Floris van Doorn Connectedness of types and functions -/ import types.trunc types.arrow_2 types.lift open eq is_trunc is_equiv nat equiv trunc function fiber funext pi pointed definition is_conn [reducible] (n : ℕ₋₂) (A : Type) : Type := is_contr (trunc n A) definition is_conn_fun [reducible] (n : ℕ₋₂) {A B : Type} (f : A → B) : Type := Πb : B, is_conn n (fiber f b) definition is_conn_inf [reducible] (A : Type) : Type := Πn, is_conn n A definition is_conn_fun_inf [reducible] {A B : Type} (f : A → B) : Type := Πn, is_conn_fun n f namespace is_conn definition is_conn_equiv_closed (n : ℕ₋₂) {A B : Type} : A ≃ B → is_conn n A → is_conn n B := begin intros H C, fapply @is_contr_equiv_closed (trunc n A) _, apply trunc_equiv_trunc, assumption end theorem is_conn_of_le (A : Type) {n k : ℕ₋₂} (H : n ≤ k) [is_conn k A] : is_conn n A := begin apply is_contr_equiv_closed, apply trunc_trunc_equiv_left _ H end theorem is_conn_fun_of_le {A B : Type} (f : A → B) {n k : ℕ₋₂} (H : n ≤ k) [is_conn_fun k f] : is_conn_fun n f := λb, is_conn_of_le _ H definition is_conn_of_is_conn_succ (n : ℕ₋₂) (A : Type) [is_conn (n.+1) A] : is_conn n A := is_trunc_trunc_of_le A -2 (trunc_index.self_le_succ n) namespace is_conn_fun section parameters (n : ℕ₋₂) {A B : Type} {h : A → B} (H : is_conn_fun n h) (P : B → Type) [Πb, is_trunc n (P b)] private definition rec.helper : (Πa : A, P (h a)) → Πb : B, trunc n (fiber h b) → P b := λt b, trunc.rec (λx, point_eq x ▸ t (point x)) private definition rec.g : (Πa : A, P (h a)) → (Πb : B, P b) := λt b, rec.helper t b (@center (trunc n (fiber h b)) (H b)) -- induction principle for n-connected maps (Lemma 7.5.7) protected definition rec : is_equiv (λs : Πb : B, P b, λa : A, s (h a)) := adjointify (λs a, s (h a)) rec.g begin intro t, apply eq_of_homotopy, intro a, unfold rec.g, unfold rec.helper, rewrite [@center_eq _ (H (h a)) (tr (fiber.mk a idp))], end begin intro k, apply eq_of_homotopy, intro b, unfold rec.g, generalize (@center _ (H b)), apply trunc.rec, apply fiber.rec, intros a p, induction p, reflexivity end protected definition elim : (Πa : A, P (h a)) → (Πb : B, P b) := @is_equiv.inv _ _ (λs a, s (h a)) rec protected definition elim_β : Πf : (Πa : A, P (h a)), Πa : A, elim f (h a) = f a := λf, apd10 (@is_equiv.right_inv _ _ (λs a, s (h a)) rec f) end section parameters (n k : ℕ₋₂) {A B : Type} {f : A → B} (H : is_conn_fun n f) (P : B → Type) [HP : Πb, is_trunc (n +2+ k) (P b)] include H HP -- Lemma 8.6.1 proposition elim_general : is_trunc_fun k (pi_functor_left f P) := begin revert P HP, induction k with k IH: intro P HP t, { apply is_contr_fiber_of_is_equiv, apply is_conn_fun.rec, exact H, exact HP}, { apply is_trunc_succ_intro, intros x y, cases x with g p, cases y with h q, have e : fiber (λr : g ~ h, (λa, r (f a))) (apd10 (p ⬝ q⁻¹)) ≃ (fiber.mk g p = fiber.mk h q :> fiber (λs : (Πb, P b), (λa, s (f a))) t), begin apply equiv.trans !fiber.sigma_char, have e' : Πr : g ~ h, ((λa, r (f a)) = apd10 (p ⬝ q⁻¹)) ≃ (ap (λv, (λa, v (f a))) (eq_of_homotopy r) ⬝ q = p), begin intro r, refine equiv.trans _ (eq_con_inv_equiv_con_eq q p (ap (λv a, v (f a)) (eq_of_homotopy r))), rewrite [-(ap (λv a, v (f a)) (apd10_eq_of_homotopy r))], rewrite [-(apd10_ap_precompose_dependent f (eq_of_homotopy r))], apply equiv.symm, apply eq_equiv_fn_eq (@apd10 A (λa, P (f a)) (λa, g (f a)) (λa, h (f a))) end, apply equiv.trans (sigma.sigma_equiv_sigma_right e'), clear e', apply equiv.trans (equiv.symm (sigma.sigma_equiv_sigma_left eq_equiv_homotopy)), apply equiv.symm, apply equiv.trans !fiber_eq_equiv, apply sigma.sigma_equiv_sigma_right, intro r, apply eq_equiv_eq_symm end, apply @is_trunc_equiv_closed _ _ k e, clear e, apply IH (λb : B, (g b = h b)) (λb, @is_trunc_eq (P b) (n +2+ k) (HP b) (g b) (h b)) } end end section universe variables u v parameters (n : ℕ₋₂) {A : Type.{u}} {B : Type.{v}} {h : A → B} parameter sec : ΠP : B → trunctype.{max u v} n, is_retraction (λs : (Πb : B, P b), λ a, s (h a)) private definition s := sec (λb, trunctype.mk' n (trunc n (fiber h b))) include sec -- the other half of Lemma 7.5.7 definition intro : is_conn_fun n h := begin intro b, apply is_contr.mk (@is_retraction.sect _ _ _ s (λa, tr (fiber.mk a idp)) b), esimp, apply trunc.rec, apply fiber.rec, intros a p, apply transport (λz : (Σy, h a = y), @sect _ _ _ s (λa, tr (mk a idp)) (sigma.pr1 z) = tr (fiber.mk a (sigma.pr2 z))) (@center_eq _ (is_contr_sigma_eq (h a)) (sigma.mk b p)), exact apd10 (@right_inverse _ _ _ s (λa, tr (fiber.mk a idp))) a end end end is_conn_fun -- Connectedness is related to maps to and from the unit type, first to section parameters (n : ℕ₋₂) (A : Type) definition is_conn_of_map_to_unit : is_conn_fun n (const A unit.star) → is_conn n A := begin intro H, unfold is_conn_fun at H, exact is_conn_equiv_closed n (fiber.fiber_star_equiv A) _, end definition is_conn_fun_to_unit_of_is_conn [H : is_conn n A] : is_conn_fun n (const A unit.star) := begin intro u, induction u, exact is_conn_equiv_closed n (fiber.fiber_star_equiv A)⁻¹ᵉ _, end -- now maps from unit definition is_conn_of_map_from_unit (a₀ : A) (H : is_conn_fun n (const unit a₀)) : is_conn n .+1 A := is_contr.mk (tr a₀) begin apply trunc.rec, intro a, exact trunc.elim (λz : fiber (const unit a₀) a, ap tr (point_eq z)) (@center _ (H a)) end definition is_conn_fun_from_unit (a₀ : A) [H : is_conn n .+1 A] : is_conn_fun n (const unit a₀) := begin intro a, apply is_conn_equiv_closed n (equiv.symm (fiber_const_equiv A a₀ a)), apply @is_contr_equiv_closed _ _ (tr_eq_tr_equiv n a₀ a), end end -- as special case we get elimination principles for pointed connected types namespace is_conn open pointed unit section parameters (n : ℕ₋₂) {A : Type*} [H : is_conn n .+1 A] (P : A → Type) [Πa, is_trunc n (P a)] include H protected definition rec : is_equiv (λs : Πa : A, P a, s (Point A)) := @is_equiv_compose (Πa : A, P a) (unit → P (Point A)) (P (Point A)) (λf, f unit.star) (λs x, s (Point A)) (is_conn_fun.rec n (is_conn_fun_from_unit n A (Point A)) P) (to_is_equiv (arrow_unit_left (P (Point A)))) protected definition elim : P (Point A) → (Πa : A, P a) := @is_equiv.inv _ _ (λs, s (Point A)) rec protected definition elim_β (p : P (Point A)) : elim p (Point A) = p := @is_equiv.right_inv _ _ (λs, s (Point A)) rec p end section parameters (n k : ℕ₋₂) {A : Type*} [H : is_conn n .+1 A] (P : A → Type) [Πa, is_trunc (n +2+ k) (P a)] include H proposition elim_general (p : P (Point A)) : is_trunc k (fiber (λs : (Πa : A, P a), s (Point A)) p) := @is_trunc_equiv_closed (fiber (λs x, s (Point A)) (λx, p)) (fiber (λs, s (Point A)) p) k (equiv.symm (fiber.equiv_postcompose _ (arrow_unit_left (P (Point A))) _)) (is_conn_fun.elim_general n k (is_conn_fun_from_unit n A (Point A)) P (λx, p)) end end is_conn -- Lemma 7.5.2 definition minus_one_conn_of_surjective {A B : Type} (f : A → B) : is_surjective f → is_conn_fun -1 f := begin intro H, intro b, exact @is_contr_of_inhabited_prop (∥fiber f b∥) (is_trunc_trunc -1 (fiber f b)) (H b), end definition is_surjection_of_minus_one_conn {A B : Type} (f : A → B) : is_conn_fun -1 f → is_surjective f := begin intro H, intro b, exact @center (∥fiber f b∥) (H b), end definition merely_of_minus_one_conn {A : Type} : is_conn -1 A → ∥A∥ := λH, @center (∥A∥) H definition minus_one_conn_of_merely {A : Type} : ∥A∥ → is_conn -1 A := @is_contr_of_inhabited_prop (∥A∥) (is_trunc_trunc -1 A) section open arrow variables {f g : arrow} -- Lemma 7.5.4 definition retract_of_conn_is_conn [instance] (r : arrow_hom f g) [H : is_retraction r] (n : ℕ₋₂) [K : is_conn_fun n f] : is_conn_fun n g := begin intro b, unfold is_conn, apply is_contr_retract (trunc_functor n (retraction_on_fiber r b)), exact K (on_cod (arrow.is_retraction.sect r) b) end end -- Corollary 7.5.5 definition is_conn_homotopy (n : ℕ₋₂) {A B : Type} {f g : A → B} (p : f ~ g) (H : is_conn_fun n f) : is_conn_fun n g := @retract_of_conn_is_conn _ _ (arrow.arrow_hom_of_homotopy p) (arrow.is_retraction_arrow_hom_of_homotopy p) n H -- all types are -2-connected definition is_conn_minus_two (A : Type) : is_conn -2 A := _ -- merely inhabited types are -1-connected definition is_conn_minus_one (A : Type) (a : ∥ A ∥) : is_conn -1 A := is_contr.mk a (is_prop.elim _) definition is_conn_minus_one_pointed [instance] (A : Type*) : is_conn -1 A := is_conn_minus_one A (tr pt) definition is_conn_trunc [instance] (A : Type) (n k : ℕ₋₂) [H : is_conn n A] : is_conn n (trunc k A) := begin apply is_trunc_equiv_closed, apply trunc_trunc_equiv_trunc_trunc end definition is_conn_eq [instance] (n : ℕ₋₂) {A : Type} (a a' : A) [is_conn (n.+1) A] : is_conn n (a = a') := begin apply is_trunc_equiv_closed, apply tr_eq_tr_equiv, end definition is_conn_loop [instance] (n : ℕ₋₂) (A : Type*) [is_conn (n.+1) A] : is_conn n (Ω A) := !is_conn_eq open pointed definition is_conn_ptrunc [instance] (A : Type*) (n k : ℕ₋₂) [H : is_conn n A] : is_conn n (ptrunc k A) := is_conn_trunc A n k -- the following trivial cases are solved by type class inference definition is_conn_of_is_contr (k : ℕ₋₂) (A : Type) [is_contr A] : is_conn k A := _ definition is_conn_fun_of_is_equiv (k : ℕ₋₂) {A B : Type} (f : A → B) [is_equiv f] : is_conn_fun k f := _ -- Lemma 7.5.14 theorem is_equiv_trunc_functor_of_is_conn_fun [instance] {A B : Type} (n : ℕ₋₂) (f : A → B) [H : is_conn_fun n f] : is_equiv (trunc_functor n f) := begin fapply adjointify, { intro b, induction b with b, exact trunc_functor n point (center (trunc n (fiber f b)))}, { intro b, induction b with b, esimp, generalize center (trunc n (fiber f b)), intro v, induction v with v, induction v with a p, esimp, exact ap tr p}, { intro a, induction a with a, esimp, rewrite [center_eq (tr (fiber.mk a idp))]} end theorem trunc_equiv_trunc_of_is_conn_fun {A B : Type} (n : ℕ₋₂) (f : A → B) [H : is_conn_fun n f] : trunc n A ≃ trunc n B := equiv.mk (trunc_functor n f) (is_equiv_trunc_functor_of_is_conn_fun n f) definition is_conn_fun_trunc_functor_of_le {n k : ℕ₋₂} {A B : Type} (f : A → B) (H : k ≤ n) [H2 : is_conn_fun k f] : is_conn_fun k (trunc_functor n f) := begin apply is_conn_fun.intro, intro P, have Πb, is_trunc n (P b), from (λb, is_trunc_of_le _ H), fconstructor, { intro f' b, induction b with b, refine is_conn_fun.elim k H2 _ _ b, intro a, exact f' (tr a)}, { intro f', apply eq_of_homotopy, intro a, induction a with a, esimp, rewrite [is_conn_fun.elim_β]} end definition is_conn_fun_trunc_functor_of_ge {n k : ℕ₋₂} {A B : Type} (f : A → B) (H : n ≤ k) [H2 : is_conn_fun k f] : is_conn_fun k (trunc_functor n f) := begin apply is_conn_fun_of_is_equiv, apply is_equiv_trunc_functor_of_le f H end -- Exercise 7.18 definition is_conn_fun_trunc_functor {n k : ℕ₋₂} {A B : Type} (f : A → B) [H2 : is_conn_fun k f] : is_conn_fun k (trunc_functor n f) := begin eapply algebra.le_by_cases k n: intro H, { exact is_conn_fun_trunc_functor_of_le f H}, { exact is_conn_fun_trunc_functor_of_ge f H} end open lift definition is_conn_fun_lift_functor (n : ℕ₋₂) {A B : Type} (f : A → B) [is_conn_fun n f] : is_conn_fun n (lift_functor f) := begin intro b, cases b with b, apply is_trunc_equiv_closed_rev, { apply trunc_equiv_trunc, apply fiber_lift_functor} end open trunc_index definition is_conn_fun_inf.mk_nat {A B : Type} {f : A → B} (H : Π(n : ℕ), is_conn_fun n f) : is_conn_fun_inf f := begin intro n, cases n with n, { exact _}, cases n with n, { have -1 ≤ of_nat 0, from dec_star, apply is_conn_fun_of_le f this}, rewrite -of_nat_add_two, exact _ end definition is_conn_inf.mk_nat {A : Type} (H : Π(n : ℕ), is_conn n A) : is_conn_inf A := begin intro n, cases n with n, { exact _}, cases n with n, { have -1 ≤ of_nat 0, from dec_star, apply is_conn_of_le A this}, rewrite -of_nat_add_two, exact _ end definition is_conn_equiv_closed_rev (n : ℕ₋₂) {A B : Type} (f : A ≃ B) (H : is_conn n B) : is_conn n A := is_conn_equiv_closed n f⁻¹ᵉ _ definition is_conn_succ_intro {n : ℕ₋₂} {A : Type} (a : trunc (n.+1) A) (H2 : Π(a a' : A), is_conn n (a = a')) : is_conn (n.+1) A := begin apply @is_contr_of_inhabited_prop, { apply is_trunc_succ_intro, refine trunc.rec _, intro a, refine trunc.rec _, intro a', apply is_contr_equiv_closed !tr_eq_tr_equiv⁻¹ᵉ }, exact a end definition is_conn_pathover (n : ℕ₋₂) {A : Type} {B : A → Type} {a a' : A} (p : a = a') (b : B a) (b' : B a') [is_conn (n.+1) (B a')] : is_conn n (b =[p] b') := is_conn_equiv_closed_rev n !pathover_equiv_tr_eq _ open sigma lemma is_conn_sigma [instance] {A : Type} (B : A → Type) (n : ℕ₋₂) [HA : is_conn n A] [HB : Πa, is_conn n (B a)] : is_conn n (Σa, B a) := begin revert A B HA HB, induction n with n IH: intro A B HA HB, { apply is_conn_minus_two }, apply is_conn_succ_intro, { induction center (trunc (n.+1) A) with a, induction center (trunc (n.+1) (B a)) with b, exact tr ⟨a, b⟩ }, intro a a', refine is_conn_equiv_closed_rev n !sigma_eq_equiv _, apply IH, apply is_conn_eq, intro p, apply is_conn_pathover /- an alternative proof of the successor case -/ -- induction center (trunc (n.+1) A) with a₀, -- induction center (trunc (n.+1) (B a₀)) with b₀, -- apply is_contr.mk (tr ⟨a₀, b₀⟩), -- intro ab, induction ab with ab, induction ab with a b, -- induction tr_eq_tr_equiv n a₀ a !is_prop.elim with p, induction p, -- induction tr_eq_tr_equiv n b₀ b !is_prop.elim with q, induction q, -- reflexivity end lemma is_conn_prod [instance] (A B : Type) (n : ℕ₋₂) [is_conn n A] [is_conn n B] : is_conn n (A × B) := is_conn_equiv_closed n !sigma.equiv_prod _ lemma is_conn_fun_of_is_conn {A B : Type} (n : ℕ₋₂) (f : A → B) [HA : is_conn n A] [HB : is_conn (n.+1) B] : is_conn_fun n f := λb, is_conn_equiv_closed_rev n !fiber.sigma_char _ lemma is_conn_pfiber {A B : Type*} (n : ℕ₋₂) (f : A →* B) [HA : is_conn n A] [HB : is_conn (n.+1) B] : is_conn n (pfiber f) := is_conn_fun_of_is_conn n f pt definition is_conn_fun_trunc_elim_of_le {n k : ℕ₋₂} {A B : Type} [is_trunc n B] (f : A → B) (H : k ≤ n) [H2 : is_conn_fun k f] : is_conn_fun k (trunc.elim f : trunc n A → B) := begin apply is_conn_fun.intro, intro P, have Πb, is_trunc n (P b), from (λb, is_trunc_of_le _ H), fconstructor, { intro f' b, refine is_conn_fun.elim k H2 _ _ b, intro a, exact f' (tr a) }, { intro f', apply eq_of_homotopy, intro a, induction a with a, esimp, rewrite [is_conn_fun.elim_β] } end definition is_conn_fun_trunc_elim_of_ge {n k : ℕ₋₂} {A B : Type} [is_trunc n B] (f : A → B) (H : n ≤ k) [H2 : is_conn_fun k f] : is_conn_fun k (trunc.elim f : trunc n A → B) := begin apply is_conn_fun_of_is_equiv, have H3 : is_equiv (trunc_functor k f), from !is_equiv_trunc_functor_of_is_conn_fun, have H4 : is_equiv (trunc_functor n f), from is_equiv_trunc_functor_of_le _ H, apply is_equiv_of_equiv_of_homotopy (equiv.mk (trunc_functor n f) _ ⬝e !trunc_equiv), intro x, induction x, reflexivity end definition is_conn_fun_trunc_elim {n k : ℕ₋₂} {A B : Type} [is_trunc n B] (f : A → B) [H2 : is_conn_fun k f] : is_conn_fun k (trunc.elim f : trunc n A → B) := begin eapply algebra.le_by_cases k n: intro H, { exact is_conn_fun_trunc_elim_of_le f H }, { exact is_conn_fun_trunc_elim_of_ge f H } end lemma is_conn_fun_tr (n : ℕ₋₂) (A : Type) : is_conn_fun n (tr : A → trunc n A) := begin apply is_conn_fun.intro, intro P, fconstructor, { intro f' b, induction b with a, exact f' a }, { intro f', reflexivity } end definition is_contr_of_is_conn_of_is_trunc {n : ℕ₋₂} {A : Type} (H : is_trunc n A) (K : is_conn n A) : is_contr A := is_contr_equiv_closed (trunc_equiv n A) end is_conn /- (bundled) connected types, possibly also truncated or with a point The notation is n-Type*[k] for k-connected n-truncated pointed types, and you can remove `n-`, `[k]` or `*` in any combination to remove some conditions -/ structure conntype (n : ℕ₋₂) : Type := (carrier : Type) (struct : is_conn n carrier) notation `Type[`:95 n:0 `]`:0 := conntype n attribute conntype.carrier [coercion] attribute conntype.struct [instance] [priority 1300] section universe variable u structure pconntype (n : ℕ₋₂) extends conntype.{u} n, pType.{u} notation `Type*[`:95 n:0 `]`:0 := pconntype n /- There are multiple coercions from pconntype to Type. Type class inference doesn't recognize that all of them are definitionally equal (for performance reasons). One instance is automatically generated, and we manually add the missing instances. -/ definition is_conn_pconntype [instance] {n : ℕ₋₂} (X : Type*[n]) : is_conn n X := conntype.struct X structure truncconntype (n k : ℕ₋₂) extends trunctype.{u} n, conntype.{u} k renaming struct→conn_struct notation n `-Type[`:95 k:0 `]`:0 := truncconntype n k definition is_conn_truncconntype [instance] {n k : ℕ₋₂} (X : n-Type[k]) : is_conn k (truncconntype._trans_of_to_trunctype X) := conntype.struct X definition is_trunc_truncconntype [instance] {n k : ℕ₋₂} (X : n-Type[k]) : is_trunc n X := trunctype.struct X structure ptruncconntype (n k : ℕ₋₂) extends ptrunctype.{u} n, pconntype.{u} k renaming struct→conn_struct notation n `-Type*[`:95 k:0 `]`:0 := ptruncconntype n k attribute ptruncconntype._trans_of_to_pconntype ptruncconntype._trans_of_to_ptrunctype ptruncconntype._trans_of_to_pconntype_1 ptruncconntype._trans_of_to_ptrunctype_1 ptruncconntype._trans_of_to_pconntype_2 ptruncconntype._trans_of_to_ptrunctype_2 ptruncconntype.to_pconntype ptruncconntype.to_ptrunctype truncconntype._trans_of_to_conntype truncconntype._trans_of_to_trunctype truncconntype.to_conntype truncconntype.to_trunctype [unfold 3] attribute pconntype._trans_of_to_conntype pconntype._trans_of_to_pType pconntype.to_pType pconntype.to_conntype [unfold 2] definition is_conn_ptruncconntype [instance] {n k : ℕ₋₂} (X : n-Type*[k]) : is_conn k (ptruncconntype._trans_of_to_ptrunctype X) := conntype.struct X definition is_trunc_ptruncconntype [instance] {n k : ℕ₋₂} (X : n-Type*[k]) : is_trunc n (ptruncconntype._trans_of_to_pconntype X) := trunctype.struct X definition ptruncconntype_eq {n k : ℕ₋₂} {X Y : n-Type*[k]} (p : X ≃* Y) : X = Y := begin induction X with X Xt Xp Xc, induction Y with Y Yt Yp Yc, note q := pType_eq_elim (eq_of_pequiv p), cases q with r s, esimp at *, induction r, exact ap0111 (ptruncconntype.mk X) !is_prop.elim (eq_of_pathover_idp s) !is_prop.elim end end
2b27b7c8c58b4913ea9f830b0ed75bedecf03960
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/blast4.lean
3d08abde2cf1d021195a8c016be2ca24c59f1444
[ "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
318
lean
set_option blast.strategy "preprocess" lemma T1 (a b : Prop) : false → a := by blast reveal T1 print T1 lemma T2 (a b c : Prop) : ¬ a → b → a → c := by blast reveal T2 print T2 example (a b c : Prop) : a → b → ¬ a → c := by blast example (a b c : Prop) : a → b → b → ¬ a → c := by blast
8113119d744c31bead163428e5a01674f7610346
048b0801f6dafb6486ca4f22bd0957671c3002ff
/src/basic/table.lean
24252963265482ba75d6e71edd21c27d144bad0a
[ "Apache-2.0" ]
permissive
Scikud/lean-gym
e0782e36389ecfa1605a0c12dc95f67014a0fa05
a1ca851b7c09eca1f72be2d059e3ed5536348b0b
refs/heads/main
1,693,940,033,482
1,633,522,864,000
1,633,522,864,000
419,793,692
0
0
null
null
null
null
UTF-8
Lean
false
false
15,631
lean
/- Author: E.W.Ayers © 2019 Copied from the lean-tpe projcet: https://github.com/jesse-michael-han/lean-tpe-public -/ import .list open expr open native section foldable universes u v class foldable (F : Type u → Type u) := (fold : ∀ {α σ : Type u}, (α → σ → σ) → σ → F α → σ) end foldable /-- Lightweight wrapper around `rbtree`. I did this because I keep swapping out which dictionary implementation I am using. -/ meta def table (α : Type) [has_lt α] : Type := rb_set α meta def dict (k : Type) (α : Type) : Type := rb_map k α infixl ` ⇀ `:1 := dict namespace table variables {α : Type} [has_lt α] [decidable_rel ((<) : α → α → Prop)] meta def empty : table α := rb_map.mk α unit meta def is_empty : table α → bool := λ t, rb_map.size t = 0 meta instance has_emptyc : has_emptyc (table α) := ⟨empty⟩ meta def from_list (l : list α) : table α := rb_map.set_of_list l meta def to_list (d : table α) : list α := rb_set.to_list d meta def to_dict (d : table α) : dict α unit := d meta def union (l : table α ) (r : table α) := rb_set.fold r l (λ x s, rb_set.insert s x) meta instance has_union : has_union (table α) := ⟨union⟩ /-- `subtract l r = {x ∈ l | x ∉ r}`-/ meta def subtract (l : table α) (r : table α) := rb_set.fold r l (λ x s, rb_set.erase s x) meta instance has_sdiff : has_sdiff (table α) := ⟨subtract⟩ meta def contains : α → table α → bool := λ a t, rb_set.contains t a meta instance has_mem : has_mem α (table α) := ⟨λ x T, contains x T⟩ meta instance {x : α} {T : table α} : decidable (x ∈ T) := dite (contains x T) is_true is_false meta def insert : α → table α → table α := λ a t, rb_set.insert t a meta def singleton : α → table α | a := insert a ∅ meta def insert_many : list α → table α → table α := λ xs t, xs.foldl (λ t x, insert x t) t meta instance has_insert : has_insert α (table α) := ⟨insert⟩ meta instance has_singleton : has_singleton α (table α) := ⟨λ x, insert x ∅⟩ meta def erase : α → table α → table α := λ x t, rb_set.erase t x meta def fold {β} : (β → α → β) → β → table α → β := λ r z t, rb_set.fold t z (function.swap r) -- meta instance : foldable (table) := ⟨λ α σ f i t, rb_set.fold t i f⟩ meta def mfold {T} [monad T] {β} (f : β → α → T β) (init : β) (t : table α) : T β := rb_set.mfold t init (function.swap f) meta def inter (l : table α) (r : table α) : table α := fold (λ acc a, if a ∈ r then insert a acc else acc) ∅ l meta instance has_inter : has_inter (table α) := ⟨λ l r, inter l r⟩ /-- Return `tt` if all of the items in the table satisfy the predicate. -/ meta def all (p : α → bool) : table α → bool := option.is_some ∘ mfold (λ _ a, if p a then some () else none) () /-- Return `tt` if at least one of the elements satisfies the predicate-/ meta def any (p : α → bool) : table α → bool := option.is_none ∘ mfold (λ (x : unit) a, if p a then none else some ()) () meta def filter (p : α → bool) : table α → table α := fold (λ t k, if p k then insert k t else t) empty meta def first : table α → option α := fold (λ o a, option.rec_on o (some a) some) none -- [HACK] highly inefficient but I can't see a better way given the interface. meta def disjoint : table α → table α → bool := λ t₁ t₂, bnot $ any (λ a, contains a t₁) $ t₂ meta instance [has_to_string α] : has_to_string (table α) := ⟨λ t, (λ s, "{|" ++ s ++ "|}") $ list.to_string $ to_list $ t⟩ meta instance has_to_tactic_format [has_to_tactic_format α] : has_to_tactic_format (table α) := ⟨λ t, do items ← t.to_list.mmap (tactic.pp), pure $ to_fmt "{" ++ (format.group $ format.nest 1 $ format.join $ list.intersperse ("," ++ format.line) $ items ) ++ "}"⟩ meta def are_equal [decidable_eq α] : table α → table α → bool := (λ l₁ l₂, l₁ = l₂) on (to_list) -- meta instance [decidable_eq α] {t₁ t₂ : table α} : decidable (t₁ = t₂) := dite (are_equal t₁ t₂) (is_true) (is_false) /-- A total ordering on tables. [TODO] make more efficient. -/ @[reducible] meta def compare : table α → table α → Prop := λ t₁ t₂, to_list t₁ < to_list t₂ meta def size : table α → ℕ := rb_set.size -- meta instance : has_lt (table α) := ⟨compare⟩ -- meta instance [decidable_eq α] : decidable_rel ((<) : table α → table α → Prop) -- | t₁ t₂ := -- show decidable ((to_list t₁) < (to_list t₂)), from -- list.has_decidable_lt _ _ end table namespace dict variables {k : Type} [has_lt k] [decidable_rel ((<) : k → k → Prop)] variable {α : Type} meta instance : has_sizeof (dict k α) := ⟨λ d, rb_map.size d⟩ meta def size : dict k α → ℕ := has_sizeof.sizeof meta def empty : dict k α := rb_map.mk k α meta def is_empty : dict k α → bool := rb_map.empty meta instance : has_emptyc (dict k α) := ⟨empty⟩ meta def insert : k → α → dict k α → dict k α := λ k a d, rb_map.insert d k a meta def get : k → dict k α → option α := λ k d, rb_map.find d k meta def contains : k → dict k α → bool := λ k d, rb_map.contains d k meta instance : has_mem k (dict k α) := ⟨λ k d, contains k d⟩ meta instance (key : k) (d : dict k α) : decidable (key ∈ d) := by apply_instance meta def modify (f : option α → α) (key : k) (d : dict k α) : dict k α := insert key (f $ get key d) d meta def modify_default (default : α) (f : α → α) : k → dict k α → dict k α := modify (λ o, f $ option.get_or_else o default) meta def modify_when_present (f : α → α) : k → dict k α → dict k α := λ key d, option.rec_on (get key d) d (λ a, insert key a d) meta def get_default (default : α) (key : k) (d: dict k α) : α := option.get_or_else (get key d) default meta def erase : k → dict k α → dict k α := λ k d, rb_map.erase d k /--Merge the two dictionaries with `r` clobbering `l` in the event of a key clash. Performance tip; it iterates over all members of `r` so make sure `r` is smaller than `l`.-/ meta def merge (l r : dict k α) := rb_map.fold r l insert /--Merge two dictionaries calling `merger` in the event of a clash. Iterates over the second dictionary `r`.-/ meta def merge_with (merger : k → α → α → α) (l r : dict k α) : dict k α := rb_map.fold r l $ λ k a acc, acc.modify (λ o, option.rec_on o a $ merger k a) k meta instance : has_append (dict k α) := ⟨merge⟩ meta def fold {β} (r : β → k → α → β) (z : β) (d : dict k α) : β := rb_map.fold d z (λ k a b, r b k a) meta def mfold {T} [monad T] {β} (f : β → k → α → T β) (z : β) (d : dict k α) : T β := rb_map.mfold d z (λ k a b, f b k a) meta def map {β} (f : α → β) (d : dict k α) : dict k β := rb_map.map f d meta def filter (p : k → α → bool) (d : dict k α) := fold (λ d k a, if p k a then insert k a d else d) empty d meta def collect {β} (f : k → α → dict k β) := fold (λ d k a, d ++ f k a) empty meta def choose {β} (f : k → α → option β) := fold (λ d k a, match f k a with (some b) := insert k b d | none := d end) empty meta def keys : dict k α → table k := fold (λ acc k v, table.insert k acc) ∅ meta def to_list : dict k α → list (k×α) := rb_map.to_list meta def min : dict k α → option α := rb_map.min meta def max : dict k α → option α := rb_map.max /--[HACK] not efficient, don't use in perf critical code. Use min.-/ meta def first : dict k α → option (k×α) := fold (λ o k a, option.rec_on o (some (k,a)) some) none meta def any (f : k → α → bool) : dict k α → bool := fold (λ o k a, o || f k a) ff meta def all (f : k → α → bool) : dict k α → bool := fold (λ o k a, o && f k a) tt meta instance dict_has_union [has_union α] : has_union (dict k α) := ⟨dict.merge_with (λ _, (∪))⟩ section formatting open format meta instance [has_to_string α] [has_to_string k] : has_to_string (dict k α) := ⟨λ d, (λ s, "{" ++ s ++ "}") $ list.to_string $ dict.to_list $ d⟩ -- meta instance has_to_format [has_to_format α] [has_to_format k] : has_to_format (dict k α) := ⟨λ d, -- to_fmt "{" ++ group (nest 1 $ join $ list.intersperse ("," ++ line) $ list.map (λ (p:k×α), to_fmt p.1 ++ " ↦ " ++ to_fmt p.2) $ dict.to_list d) ++ to_fmt "}" -- ⟩ meta instance has_to_tactic_format [has_to_tactic_format α] [has_to_tactic_format k] : has_to_tactic_format (dict k α) := ⟨λ d, do items ← list.mmap (λ (p:k×α), do f1 ← tactic.pp p.1, f2 ← tactic.pp p.2, pure $ f1 ++ line ++ "↦ " ++ nest 3 (f2)) (to_list d), pure $ "{" ++ group (nest 1 $ join $ list.intersperse ("," ++ line) $ items) ++ "}" ⟩ end formatting end dict /--dictionary with a default if it doesn't exist. You define the default when you make the dictionary. -/ meta structure dictd (k : Type) (α : Type) : Type := (val : dict k α) (default : k → α) namespace dictd variables {k : Type} [has_lt k] [decidable_rel ((<) : k → k → Prop)] {α : Type} meta def empty (default : k → α) : dictd k α := ⟨dict.empty, default⟩ meta instance [inhabited α] : has_emptyc (dictd k α) := ⟨empty (λ _, inhabited.default α)⟩ meta def get (key : k) (dd : dictd k α) : α := dict.get_default (dd.2 key) key dd.1 meta def insert (key : k) (a : α) (dd : dictd k α) : dictd k α := ⟨dict.insert key a dd.1, dd.2⟩ meta def modify (f : α → α) (key : k) (dd : dictd k α) : dictd k α := ⟨dict.modify (λ o, f $ option.get_or_else o (dd.2 key)) key dd.1, dd.2⟩ meta def of_dict (default : k → α) : dict k α → dictd k α := λ d, ⟨d, default⟩ end dictd meta def tabledict (κ : Type) (α : Type) [has_lt κ] [decidable_rel ((<) : κ → κ → Prop)] [has_lt α] [decidable_rel ((<) : α → α → Prop)] : Type := dict κ (table α) namespace tabledict variables {κ α : Type} [has_lt κ] [decidable_rel ((<) : κ → κ → Prop)] [has_lt α] [decidable_rel ((<) : α → α → Prop)] meta def empty : tabledict κ α := dict.empty meta instance : has_emptyc (tabledict κ α) := ⟨empty⟩ meta def insert : κ → α → tabledict κ α → tabledict κ α := λ k a d, dict.modify_default ∅ (λ t, t.insert a) k d meta def erase : κ → α → tabledict κ α → tabledict κ α := λ k a d, dict.modify_when_present (λ t, t.erase a) k d meta def get : κ → tabledict κ α → table α := λ k t, dict.get_default ∅ k t meta def contains : κ → α → tabledict κ α → bool := λ k a d, match dict.get k d with |(some t) := t.contains a | none := ff end meta instance [has_to_tactic_format κ] [has_to_tactic_format α] : has_to_tactic_format (tabledict κ α) := ⟨λ (d : dict κ (table α)), tactic.pp d⟩ meta def fold {β} (f : β → κ → α → β) : β → tabledict κ α → β := dict.fold (λ b k, table.fold (λ b, f b k) b) meta def mfold {T} [monad T] {β} (f : β → κ → α → T β) : β → tabledict κ α → T β := dict.mfold (λ b k, table.mfold (λ b, f b k) b) meta def to_list : tabledict κ α → list α := list.collect (table.to_list ∘ prod.snd) ∘ dict.to_list /-- Merge the dictionaries, each clash is unioned. -/ meta instance td_has_union : has_union (tabledict κ α) := show has_union (dict κ (table α)), by apply_instance end tabledict meta def listdict (κ : Type) (α : Type) [has_lt κ] [decidable_rel ((<) : κ → κ → Prop)] : Type := dict κ (list α) namespace listdict variables {κ α : Type} [has_lt κ] [decidable_rel ((<) : κ → κ → Prop)] meta def empty : listdict κ α := dict.empty meta instance : has_emptyc (listdict κ α) := ⟨empty⟩ meta def insert : κ → α → listdict κ α → listdict κ α | k a d := dict.modify_default [] (λ t, a :: t) k d meta def inserts: κ → list α → listdict κ α → listdict κ α | k as d := dict.modify_default [] (λ t, as ++ t) k d meta def pop : κ → listdict κ α → option (α × listdict κ α) | k d := match dict.get_default [] k d with |[] := none |(h::t) := some (h, dict.insert k t d) end meta def get : κ → listdict κ α → list α | k d := dict.get_default [] k d meta def fold {β} (f : β → κ → α → β) : β → listdict κ α → β := dict.fold (λ b k, list.foldl (λ b, f b k) b) meta def mfold {T} [monad T] {β} (f:β → κ → α → T β) : β → listdict κ α → T β := dict.mfold (λ b k, list.mfoldl (λ b, f b k) b) meta def first : listdict κ α → option (κ × α) := fold (λ o k a, option.rec_on o (some (k,a)) some) none -- meta instance [has_to_format κ] [has_to_format α] : has_to_format (listdict κ α) := ⟨λ (d : dict κ (list α)), to_fmt d⟩ meta instance has_pp [has_to_tactic_format κ] [has_to_tactic_format α] : has_to_tactic_format (listdict κ α) := ⟨λ (d : dict κ (list α)), tactic.pp d⟩ meta def of : list (κ × list α) → listdict κ α := list.foldl (λ acc p, prod.rec inserts p acc) ∅ meta def map {β} (f : α → β) : listdict κ α → listdict κ β := dict.map (list.map f) end listdict /--A table which tracks the number of times that a given key has been inserted. -/ meta def mtable (κ : Type) [has_lt κ] [decidable_rel ((<) : κ → κ → Prop)] : Type := dict κ ℕ namespace mtable variables {κ : Type} [has_lt κ] [decidable_rel ((<) : κ → κ → Prop)] meta def empty : mtable κ := dict.empty meta def is_empty : mtable κ → bool := dict.is_empty meta instance : has_emptyc (mtable κ) := ⟨empty⟩ meta def get : κ → mtable κ → ℕ := dict.get_default 0 meta def of_table : table κ → mtable κ := dict.map (λ x, 1) ∘ table.to_dict meta def insert : κ → mtable κ → mtable κ := dict.modify_default 0 nat.succ meta instance : has_insert κ (mtable κ) := ⟨insert⟩ meta def of_list : list κ → mtable κ := list.foldl (function.swap insert) ∅ meta def erase : κ → mtable κ → mtable κ := dict.modify_when_present (λ x, x - 1) /--Reset the given key to zero. Contrast with erase which merely decrements the count. -/ meta def clear : κ → mtable κ → mtable κ := dict.erase /--Union two mtables. For performance make sure the second argument is smaller. -/ meta def join : mtable κ → mtable κ → mtable κ := dict.merge_with (λ _, (+)) meta def to_table : mtable κ → table κ := dict.map (λ _, ⟨⟩) meta def filter (f : κ → bool) : mtable κ → mtable κ := dict.filter $ λ k _, f k meta instance : has_coe_to_fun (mtable κ) := ⟨λ _, κ → ℕ, λ t k, get k t⟩ open format meta instance [has_to_tactic_format κ] : has_to_tactic_format (mtable κ) := ⟨λ t, do items ← list.mmap (λ (p:κ×ℕ), do ppk ← tactic.pp p.1, ppn ← tactic.pp p.2, pure $ ppn ++ "\t| " ++ ppk ) $ dict.to_list $ t, pure $ "{" ++ group (nest 1 $ format.join $ list.intersperse (to_fmt "," ++ line) $ items) ++ "}" ⟩ end mtable
4083e7b0719d4caebd14d590ea000efe6d36f517
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/pkg/frontend/Frontend/Main_with_Import2_and_eval.lean
443c3368209407bd883b61a992afff6691ef0ed1
[ "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
175
lean
import Frontend.Main import Frontend.Import2 #eval show IO _ from do let r ← main ["Frontend.Import1"] if r ≠ 0 then throw <| IO.userError "Messages were generated!"
956fc5df99de91b1006ffb8c5ef4b215a4225303
36938939954e91f23dec66a02728db08a7acfcf9
/lean4/deps/x86_semantics/src/X86Semantics/ConcreteBackend.lean
b31c70052e5e5519d0147a2b58c2f2d3dbb0d71f
[]
no_license
pnwamk/reopt-vcg
f8b56dd0279392a5e1c6aee721be8138e6b558d3
c9f9f185fbefc25c36c4b506bbc85fd1a03c3b6d
refs/heads/master
1,631,145,017,772
1,593,549,019,000
1,593,549,143,000
254,191,418
0
0
null
1,586,377,077,000
1,586,377,077,000
null
UTF-8
Lean
false
false
18,136
lean
import Galois.Data.Bitvec import X86Semantics.Common import X86Semantics.BackendAPI import X86Semantics.MachineMemory namespace x86 open mc_semantics open mc_semantics.type axiom I_am_really_sorry2 : ∀(P : Prop), P @[reducible] def machine_word := bitvec 64 def bitvec.uext {n} (m : Nat) (p: n ≤ m) (x:bitvec n) : bitvec m := bitvec.set_bits 0 0 x (I_am_really_sorry2 _) -- (begin simp, exact p end) def bitvec.sext {n} (m : Nat) (p: n ≤ m) (x:bitvec n) : bitvec m := bitvec.set_bits (if x.msb then (bitvec.zero m).not else 0) 0 x (I_am_really_sorry2 _)-- (begin simp, exact p end) -- Returns the number of bits that are tt mod 2 def bitvec.parity {n : Nat} (b : bitvec n) : Bool := bitvec.foldl xor false b -- example : bitvec.parity (3 : bitvec 4) = false := by refl -- example : bitvec.parity (7 : bitvec 4) = true := by refl def bitvec.trunc {n} (m : Nat) (p: m ≤ n) (x:bitvec n) : bitvec m := bitvec.get_bits x 0 m (I_am_really_sorry2 _) --(begin simp, exact p end) def bit_to_bitvec (n : Nat) (b : Bool) : bitvec n := if b then 1 else 0 structure machine_state : Type := (mem : memory) (gpregs : Array machine_word) -- 16 (flags : Array Bool) -- 32 (ip : machine_word) namespace machine_state -- Constructs an empty machine state, with 0 where we need a value. def empty : machine_state := { mem := memory.empty , gpregs := mkArray 16 0 , flags := mkArray 32 false , ip := 0 } def get_gpreg (s : machine_state) (idx : Fin 16) : machine_word := -- FIXME if h : 16 = s.gpregs.size then Array.get s.gpregs (Eq.recOn h idx) else 0 def update_gpreg (idx : Fin 16) (f : machine_word -> machine_word) (s : machine_state) : machine_state := -- FIXME if h : 16 = s.gpregs.size then { s with gpregs := Array.set s.gpregs (Eq.recOn h idx) (f (get_gpreg s idx)) } else s def get_flag (s : machine_state) (idx : Fin 32) : Bool := if h : 32 = s.flags.size then Array.get s.flags (Eq.recOn h idx) else false def update_flag (idx : Fin 32) (f : Bool -> Bool) (s : machine_state) : machine_state := if h : 32 = s.flags.size then { s with flags := Array.set s.flags (Eq.recOn h idx) (f (get_flag s idx)) } else s -- def store_bytes (addr : machine_word) (bs : List (bitvec 8)) (s : machine_state) : machine_state := -- { s with mem := s.mem.store_bytes addr bs } -- def read_bytes (s : machine_state) (addr : machine_word) (n : Nat) : Option (List (bitvec 8)) := -- s.mem.read_bytes addr n -- lemma read_bytes_length {s : machine_state} {addr : machine_word} {n : Nat} {bs : List (bitvec 8)} -- : read_bytes s addr n = some bs -> bs.length = n := memory.read_bytes_length def store_word {n : Nat} (s : machine_state) (addr : machine_word) (b : bitvec (8 * n)) : machine_state := {s with mem := s.mem.store_word addr b } def read_word (s : machine_state) (addr : machine_word) (n : Nat) : bitvec (8 * n) := do match s.mem.read_word addr n with | some v => v | none => bitvec.of_nat _ 0 -- FIXME def print_regs (s : machine_state) : String := let lines := List.zipWith (fun n (r : bitvec 64) => if r = 0 then "" else (n ++ ": " ++ r.pp_hex ++ ", ")) reg.r64_names s.gpregs.toList; String.join lines def print_set_flags (s : machine_state) : String := let lines := List.zipWith (fun n (r : Bool) => if r then n else "") reg.flag_names s.flags.toList; "[" ++ String.intercalate ", " (List.filter (fun s => s.length > 0) lines) ++ "]" end machine_state namespace sysv_abi namespace x86_64 -- c.f. https://www.uclibc.org/docs/psABI-x86_64.pdf -- Basically: -- - rFLAGS are all 0 (ff) -- - (%rsp) has argc -- - 8(%rsp) has an an argc-long array of 64 bit words -- - (8 + 8 * argc)(%rsp) is 0 -- - followed by a 0 terminated array of envps -- - followed by auxiliary vectors (16 bytes each), terminated by an essentially 0 entry. -- - At some higher address the strings -- Also: -- - %rsp should be 16 byte aligned -- -- For now we just pick a reasonably rsp, initialise s.t. argc == 0 def initialise (st : machine_state) : machine_state := let rsp_idx : Fin 16 := 4; -- FIXME let stack_top := bitvec.of_nat 64 (2 ^ 47); let words := [ 0 /- argc -/, 0 /- argv term. -/, 0 /- envp term -/, 0, 0 /- auxv term (2 words) -/ ]; let f (acc : (bitvec 64 × machine_state)) (v : Nat) : bitvec 64 × machine_state := (acc.fst + bitvec.of_nat _ 8, acc.snd.store_word acc.fst (bitvec.of_nat (8 * 8) v)); let s' := List.foldl f (stack_top, st) words; machine_state.update_gpreg rsp_idx (fun _ => stack_top) s'.snd end x86_64 end sysv_abi namespace linux namespace x86_64 open mc_semantics inductive trace_event | syscall : Nat -> List machine_word -> trace_event | read : machine_word -> ∀(n:Nat), bitvec n -> trace_event | write : machine_word -> ∀(n:Nat), bitvec n -> trace_event def trace_event.repr : trace_event -> String | trace_event.syscall n args => let pfx := "syscall " ++ repr n ++ " " ++ repr args.length; List.foldl (fun (s : String) (w : machine_word) => s ++ " " ++ w.pp_hex) pfx args | trace_event.read addr n b => "read " ++ addr.pp_hex ++ " " ++ repr n ++ " " ++ b.pp_hex | trace_event.write addr n b => "write " ++ addr.pp_hex ++ " " ++ repr n ++ " " ++ b.pp_hex instance trace_event_repr : HasRepr trace_event := ⟨trace_event.repr⟩ structure os_state := (current_ip : machine_word) (trace : List (machine_word × trace_event)) def os_state.empty : os_state := os_state.mk 0 [] -- Stacking like this makes it easier to derive MonadState def base_system_m := (StateT os_state (ExceptT String IO)) def system_m := StateT machine_state base_system_m instance : Monad base_system_m := inferInstanceAs (Monad (StateT os_state (ExceptT String IO))) instance : MonadState os_state base_system_m := inferInstanceAs (MonadState os_state (StateT os_state (ExceptT String IO))) instance : MonadExcept String base_system_m := inferInstanceAs (MonadExcept String (StateT os_state (ExceptT String IO))) instance : MonadIO base_system_m := inferInstanceAs (MonadIO (StateT os_state (ExceptT String IO))) instance system_m.Monad : Monad system_m := inferInstanceAs (Monad (StateT machine_state base_system_m)) instance system_m.MonadState : MonadState machine_state system_m := inferInstanceAs (MonadState machine_state (StateT machine_state base_system_m)) instance system_m.MonadExcept : MonadExcept String system_m := inferInstanceAs (MonadExcept String (StateT machine_state base_system_m)) instance : HasMonadLiftT base_system_m system_m := inferInstanceAs (HasMonadLiftT base_system_m (StateT machine_state base_system_m)) instance system_m.MonadIO : MonadIO system_m := inferInstanceAs (MonadIO (StateT machine_state base_system_m)) def system_m.run {a : Type} (m : system_m a) (os : os_state) (s : machine_state) : IO (Except String ((a × machine_state) × os_state)) := do ((m.run s).run os).run -- def system_m.run' {a : Type} (m : system_m a) (s : machine_state) : IO (Except String a) := -- do x <- m.run os_state.empty s; -- pure def emit_trace_event (e : trace_event) : system_m Unit := monadLift (modify (fun (s : os_state) => { s with trace := (s.current_ip, e) :: s.trace }) : base_system_m Unit) -- Linux calling conv: %rdi, %rsi, %rdx, %r10, %r8 and %r9, with %rax holding syscall number. -- FIXME: these should maybe be in common? def rax_idx : Fin 16 := 0 def rcx_idx : Fin 16 := 1 def rdx_idx : Fin 16 := 2 def rbx_idx : Fin 16 := 3 def rsp_idx : Fin 16 := 4 def rbp_idx : Fin 16 := 5 def rsi_idx : Fin 16 := 6 def rdi_idx : Fin 16 := 7 def r8_idx : Fin 16 := 8 def r9_idx : Fin 16 := 9 def r10_idx : Fin 16 := 10 def r11_idx : Fin 16 := 11 def r12_idx : Fin 16 := 12 def r13_idx : Fin 16 := 13 def r14_idx : Fin 16 := 14 def r15_idx : Fin 16 := 15 -- def simple_syscall (f : system_state os_state -> machine_word) : system_m Unit := -- modify (fun s => { s with machine_state := s.machine_state.update_gpreg rax_idx (fun _ => f s) }) def emit_syscall_trace (syscall_no : Nat) (args : List machine_word) : system_m Unit := emit_trace_event (trace_event.syscall syscall_no args) def raw_syscall {a : Type} (f : machine_word -> machine_word -> machine_word -> machine_word -> machine_word -> machine_word -> system_m a) : system_m a := do s <- get; f (s.get_gpreg rdi_idx) (s.get_gpreg rsi_idx) (s.get_gpreg rdx_idx) (s.get_gpreg r10_idx) (s.get_gpreg r8_idx) (s.get_gpreg r9_idx) def syscall0 (sys_f : system_m machine_word) (syscall_no : Nat) : system_m Unit := do res <- raw_syscall (fun _ _ _ _ _ _ => do emit_syscall_trace syscall_no []; sys_f); modify (machine_state.update_gpreg rax_idx (fun _ => res)) def syscall1 (sys_f : machine_word -> system_m machine_word) (syscall_no : Nat) : system_m Unit := do res <- raw_syscall (fun a _ _ _ _ _ => do emit_syscall_trace syscall_no [a]; sys_f a); modify (machine_state.update_gpreg rax_idx (fun _ => res)) def syscall3 (sys_f : machine_word -> machine_word -> machine_word -> system_m machine_word) (syscall_no : Nat) : system_m Unit := do res <- raw_syscall (fun a b c _ _ _ => do emit_syscall_trace syscall_no [a, b, c]; sys_f a b c); modify (machine_state.update_gpreg rax_idx (fun _ => res)) def syscall6 (sys_f : machine_word -> machine_word -> machine_word -> machine_word -> machine_word -> machine_word -> system_m machine_word) (syscall_no : Nat) : system_m Unit := do res <- raw_syscall (fun a b c d e f => do emit_syscall_trace syscall_no [a, b, c, d, e, f]; sys_f a b c d e f); modify (machine_state.update_gpreg rax_idx (fun _ => res)) -- Stub calls abbrev syscall_t := ∀(syscall_no : Nat), system_m Unit def sys_getuid : syscall_t := syscall0 (pure (bitvec.of_nat 64 4242)) -- FIXME: maybe use the euid of the current (lean) process? We could -- also forward these to the underlying (Linux) kernel def sys_geteuid : syscall_t := syscall0 (pure (bitvec.of_nat 64 4242)) def sys_getgid : syscall_t := syscall0 (pure (bitvec.of_nat 64 4242)) -- FIXME: maybe use the euid of the current (lean) process? We could -- also forward these to the underlying (Linux) kernel def sys_getegid : syscall_t := syscall0 (pure (bitvec.of_nat 64 4242)) def sys_exit : syscall_t := syscall1 (fun _ => throw "Exit system call") def sys_write : syscall_t := syscall3 (fun filedes buf nbytes => do s <- get; let m_bytes := s.mem.read_bytes buf nbytes.to_nat; match m_bytes with | none => throw ("sys_write: unable to read " ++ nbytes.to_nat.repr ++ " bytes at " ++ buf.pp_hex) | (some bs) => if filedes = 1 then do let str := String.mk (bs.map (fun (b : byte) => Char.ofNat b.toNat)); IO.print str; pure nbytes -- always succeed else throw ("sys_write: unable to write to filedes " ++ filedes.to_nat.repr) ) def syscalls : RBMap Nat syscall_t (fun x y => decide (x < y)) := RBMap.fromList [ (0x01, sys_write) , (0x3c, sys_exit) , (0x66, sys_geteuid) , (0x6b, sys_geteuid) , (0x68, sys_getgid) , (0x6c, sys_getegid) ] (fun x y => decide (x < y)) def syscall_handler : system_m Unit := do s <- get; let syscall_no := (s.get_gpreg rax_idx).to_nat; match syscalls.find? syscall_no with | none => throw ("Unknown syscall: " ++ repr syscall_no) | (some m) => m syscall_no def set_reg32 (idx : Fin 16) (x : bitvec 32) : system_m Unit := modify (machine_state.update_gpreg rax_idx (fun _ => bitvec.uresize x 64)) -- FIXME: a hack def read_cpuid : system_m Unit := -- Copied from the cpuid results from my macbook -- Note: CPUID is allowed to return 0s let cpuid_values : RBMap Nat (Nat × Nat × Nat × Nat) (fun x y => decide (x < y)) := RBMap.fromList [ (0, (0xd, 0x756e6547, 0x6c65746e, 0x49656e69)) , (1, (0x40661, 0x2100800, 0x7ffafbff, 0xbfebfbff)) , (2, (0x76036301, 0xf0b5ff, 0x0, 0xc10000)) , (3, (0x0, 0x0, 0x0, 0x0)) , (4, (0x1c004121, 0x1c0003f, 0x3f, 0x0)) , (5, (0x40, 0x40, 0x3, 0x42120)) , (6, (0x77, 0x2, 0x9, 0x0)) , (7, (0x0, 0x27ab, 0x0, 0x9c000000)) , (8, (0x0, 0x0, 0x0, 0x0)) , (9, (0x0, 0x0, 0x0, 0x0)) , (10, (0x7300403, 0x0, 0x0, 0x603)) , (11, (0x1, 0x2, 0x100, 0x6)) , (12, (0x0, 0x0, 0x0, 0x0)) , (13, (0x7, 0x340, 0x340, 0x0)) , (2147483648, (0x80000008, 0x0, 0x0, 0x0)) , (2147483649, (0x0, 0x0, 0x21, 0x2c100800)) , (2147483650, (0x65746e49, 0x2952286c, 0x726f4320, 0x4d542865)) , (2147483651, (0x37692029, 0x3839342d, 0x20514830, 0x20555043)) , (2147483652, (0x2e322040, 0x48473038, 0x7a, 0x0)) , (2147483653, (0x0, 0x0, 0x0, 0x0)) , (2147483654, (0x0, 0x0, 0x1006040, 0x0)) , (2147483655, (0x0, 0x0, 0x0, 0x100)) , (2147483656, (0x3027, 0x0, 0x0, 0x0)) ] (fun x y => decide (x < y)); -- FIXME: we need to look at rcx sometimes as well let cpuid_fn (n : Nat) : (Nat × Nat × Nat × Nat) := match cpuid_values.find? n with | none => (0, 0, 0, 0) | (some r) => r; do s <- get; let raxv := bitvec.uresize (s.get_gpreg rax_idx) 32; match cpuid_fn raxv.to_nat with | (axv, bxv, cxv, dxv) => do set_reg32 rax_idx (bitvec.of_nat 32 axv); set_reg32 rbx_idx (bitvec.of_nat 32 bxv); set_reg32 rcx_idx (bitvec.of_nat 32 cxv); set_reg32 rdx_idx (bitvec.of_nat 32 dxv) end x86_64 end linux section in_linux open linux.x86_64 def concreteBackend : Backend := { s_bv := bitvec , s_bool := Bool , s_bv_imm := bitvec.of_nat , s_bool_imm := fun b => b , monad := linux.x86_64.system_m -- , Monad_backend := -- , MonadExcept_backend := , store_word := fun n addr v => do emit_trace_event (trace_event.write addr _ v); modify (fun s => machine_state.store_word s addr v) , read_word := fun addr n => do v <- (fun s => machine_state.read_word s addr n) <$> get; emit_trace_event (trace_event.read addr _ v); pure v , get_gpreg := fun i => (fun s => machine_state.get_gpreg s i) <$> get , set_gpreg := fun i v => modify (machine_state.update_gpreg i (fun _ => v)) , get_flag := fun i => (fun s => machine_state.get_flag s i) <$> get , set_flag := fun i v => modify (machine_state.update_flag i (fun _ => v)) , s_mux_bool := fun (b : Bool) (x y : Bool) => if b then x else y , s_mux_bv := fun {n : Nat} (b : Bool) (x y : bitvec n) => if b then x else y , s_mux_m := fun (b : Bool) x y => if b then x else y , s_not := not , s_or := or , s_and := and , s_xor := xor -- - Comparison , s_bveq := fun {n : Nat} x y => decide (x = y) , s_bvult := fun {n : Nat} x y => decide (bitvec.ult x y) , s_bvslt := fun {n : Nat} x y => decide (bitvec.slt x y) -- - Arithmetic , s_bvneg := @bitvec.neg , s_bvnot := @bitvec.not , s_bvadd := @bitvec.add , s_bvsub := @bitvec.sub , s_bvmul := @bitvec.mul , s_bvudiv := fun (n : Nat) (x y : bitvec n) => bitvec.of_nat n 0 -- FIXME , s_bvurem := fun (n : Nat) (x y : bitvec n) => bitvec.of_nat n 0 -- FIXME , s_bvextract := fun (w i j : Nat) (x : bitvec w) => let n := i + 1 - j; if H : w = w - n + n then bitvec.slice i j (w - n) H x else bitvec.of_nat _ 0 -- FIXME -- FIXME: use resize , s_sext := fun (n m : Nat) (x : bitvec n) => if H : n ≤ m then bitvec.sext m H x else bitvec.trunc m (Nat.leOfLt (Nat.gtOfNotLe H)) x , s_uext := fun (n m : Nat) (x : bitvec n) => if H : n ≤ m then bitvec.sext m H x else bitvec.trunc m (Nat.leOfLt (Nat.gtOfNotLe H)) x , s_trunc := fun (n m : Nat) (x : bitvec n) => if H : m ≤ n then bitvec.trunc m H x else bitvec.of_nat _ 0 -- FIXME , s_bvappend := @bitvec.append , s_bvgetbits := fun {n : Nat} (off m : Nat) (x : bitvec n) => bitvec.get_bits (bitvec.uresize x (off + m)) off m (Nat.leRefl _) , s_bvsetbits := fun {n m : Nat} (off : Nat) (x : bitvec n) (bs : bitvec m) => if H : off + m ≤ n then bitvec.set_bits x off bs H else bitvec.of_nat _ 0 -- FIXME , s_bvand := @bitvec.and , s_bvor := @bitvec.or , s_bvxor := @bitvec.xor , s_bvshl := fun (n : Nat) ( x y : bitvec n) => bitvec.shl x (y.to_nat) , s_bvmsb := @bitvec.msb -- unsigned , s_bvlshr := fun (n : Nat) ( x y : bitvec n) => bitvec.ushr x (y.to_nat) -- signed , s_bvsshr := fun (n : Nat) ( x y : bitvec n) => bitvec.sshr x (y.to_nat) , s_parity := @bitvec.parity , s_bit_test := fun {wr wi : Nat} (x : bitvec wr) (y : bitvec wi) => bitvec.nth x (y.to_nat) -- System operations , s_os_transition := linux.x86_64.syscall_handler , s_get_ip := (fun (s : machine_state) => s.ip) <$> get , s_set_ip := fun x => modify (fun s => { s with ip := x }) , s_cond_set_ip := fun b x => when b $ modify (fun s => { s with ip := x }) , s_read_cpuid := linux.x86_64.read_cpuid } end in_linux end x86
3310437c083509fa94400d37ce0649acab19a258
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/field_theory/splitting_field_auto.lean
6e7e90de185ca228ead6567fa1132e2e1d758cd3
[]
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
21,665
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes Definition of splitting fields, and definition of homomorphism into any field that splits -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.ring_theory.adjoin_root import Mathlib.ring_theory.algebra_tower import Mathlib.ring_theory.algebraic import Mathlib.ring_theory.polynomial.default import Mathlib.field_theory.minpoly import Mathlib.linear_algebra.finite_dimensional import Mathlib.tactic.field_simp import Mathlib.PostPort universes u v w u_1 u_2 u_3 l namespace Mathlib namespace polynomial /-- a polynomial `splits` iff it is zero or all of its irreducible factors have `degree` 1 -/ def splits {α : Type u} {β : Type v} [field α] [field β] (i : α →+* β) (f : polynomial α) := f = 0 ∨ ∀ {g : polynomial β}, irreducible g → g ∣ map i f → degree g = 1 @[simp] theorem splits_zero {α : Type u} {β : Type v} [field α] [field β] (i : α →+* β) : splits i 0 := Or.inl rfl @[simp] theorem splits_C {α : Type u} {β : Type v} [field α] [field β] (i : α →+* β) (a : α) : splits i (coe_fn C a) := sorry theorem splits_of_degree_eq_one {α : Type u} {β : Type v} [field α] [field β] (i : α →+* β) {f : polynomial α} (hf : degree f = 1) : splits i f := sorry theorem splits_of_degree_le_one {α : Type u} {β : Type v} [field α] [field β] (i : α →+* β) {f : polynomial α} (hf : degree f ≤ 1) : splits i f := sorry theorem splits_mul {α : Type u} {β : Type v} [field α] [field β] (i : α →+* β) {f : polynomial α} {g : polynomial α} (hf : splits i f) (hg : splits i g) : splits i (f * g) := sorry theorem splits_of_splits_mul {α : Type u} {β : Type v} [field α] [field β] (i : α →+* β) {f : polynomial α} {g : polynomial α} (hfg : f * g ≠ 0) (h : splits i (f * g)) : splits i f ∧ splits i g := sorry theorem splits_of_splits_of_dvd {α : Type u} {β : Type v} [field α] [field β] (i : α →+* β) {f : polynomial α} {g : polynomial α} (hf0 : f ≠ 0) (hf : splits i f) (hgf : g ∣ f) : splits i g := sorry theorem splits_of_splits_gcd_left {α : Type u} {β : Type v} [field α] [field β] (i : α →+* β) {f : polynomial α} {g : polynomial α} (hf0 : f ≠ 0) (hf : splits i f) : splits i (euclidean_domain.gcd f g) := splits_of_splits_of_dvd i hf0 hf (euclidean_domain.gcd_dvd_left f g) theorem splits_of_splits_gcd_right {α : Type u} {β : Type v} [field α] [field β] (i : α →+* β) {f : polynomial α} {g : polynomial α} (hg0 : g ≠ 0) (hg : splits i g) : splits i (euclidean_domain.gcd f g) := splits_of_splits_of_dvd i hg0 hg (euclidean_domain.gcd_dvd_right f g) theorem splits_map_iff {α : Type u} {β : Type v} {γ : Type w} [field α] [field β] [field γ] (i : α →+* β) (j : β →+* γ) {f : polynomial α} : splits j (map i f) ↔ splits (ring_hom.comp j i) f := sorry theorem splits_one {α : Type u} {β : Type v} [field α] [field β] (i : α →+* β) : splits i 1 := splits_C i 1 theorem splits_of_is_unit {α : Type u} {β : Type v} [field α] [field β] (i : α →+* β) {u : polynomial α} (hu : is_unit u) : splits i u := splits_of_splits_of_dvd i one_ne_zero (splits_one i) (iff.mp is_unit_iff_dvd_one hu) theorem splits_X_sub_C {α : Type u} {β : Type v} [field α] [field β] (i : α →+* β) {x : α} : splits i (X - coe_fn C x) := splits_of_degree_eq_one i (degree_X_sub_C x) theorem splits_X {α : Type u} {β : Type v} [field α] [field β] (i : α →+* β) : splits i X := splits_of_degree_eq_one i degree_X theorem splits_id_iff_splits {α : Type u} {β : Type v} [field α] [field β] (i : α →+* β) {f : polynomial α} : splits (ring_hom.id β) (map i f) ↔ splits i f := sorry theorem splits_mul_iff {α : Type u} {β : Type v} [field α] [field β] (i : α →+* β) {f : polynomial α} {g : polynomial α} (hf : f ≠ 0) (hg : g ≠ 0) : splits i (f * g) ↔ splits i f ∧ splits i g := sorry theorem splits_prod {α : Type u} {β : Type v} [field α] [field β] (i : α →+* β) {ι : Type w} {s : ι → polynomial α} {t : finset ι} : (∀ (j : ι), j ∈ t → splits i (s j)) → splits i (finset.prod t fun (x : ι) => s x) := sorry theorem splits_prod_iff {α : Type u} {β : Type v} [field α] [field β] (i : α →+* β) {ι : Type w} {s : ι → polynomial α} {t : finset ι} : (∀ (j : ι), j ∈ t → s j ≠ 0) → (splits i (finset.prod t fun (x : ι) => s x) ↔ ∀ (j : ι), j ∈ t → splits i (s j)) := sorry theorem degree_eq_one_of_irreducible_of_splits {β : Type v} [field β] {p : polynomial β} (h_nz : p ≠ 0) (hp : irreducible p) (hp_splits : splits (ring_hom.id β) p) : degree p = 1 := sorry theorem exists_root_of_splits {α : Type u} {β : Type v} [field α] [field β] (i : α →+* β) {f : polynomial α} (hs : splits i f) (hf0 : degree f ≠ 0) : ∃ (x : β), eval₂ i x f = 0 := sorry theorem exists_multiset_of_splits {α : Type u} {β : Type v} [field α] [field β] (i : α →+* β) {f : polynomial α} : splits i f → ∃ (s : multiset β), map i f = coe_fn C (coe_fn i (leading_coeff f)) * multiset.prod (multiset.map (fun (a : β) => X - coe_fn C a) s) := sorry /-- Pick a root of a polynomial that splits. -/ def root_of_splits {α : Type u} {β : Type v} [field α] [field β] (i : α →+* β) {f : polynomial α} (hf : splits i f) (hfd : degree f ≠ 0) : β := classical.some (exists_root_of_splits i hf hfd) theorem map_root_of_splits {α : Type u} {β : Type v} [field α] [field β] (i : α →+* β) {f : polynomial α} (hf : splits i f) (hfd : degree f ≠ 0) : eval₂ i (root_of_splits i hf hfd) f = 0 := classical.some_spec (exists_root_of_splits i hf hfd) theorem roots_map {α : Type u} {β : Type v} [field α] [field β] (i : α →+* β) {f : polynomial α} (hf : splits (ring_hom.id α) f) : roots (map i f) = multiset.map (⇑i) (roots f) := sorry theorem eq_prod_roots_of_splits {α : Type u} {β : Type v} [field α] [field β] {p : polynomial α} {i : α →+* β} (hsplit : splits i p) : map i p = coe_fn C (coe_fn i (leading_coeff p)) * multiset.prod (multiset.map (fun (a : β) => X - coe_fn C a) (roots (map i p))) := sorry theorem eq_X_sub_C_of_splits_of_single_root {α : Type u} {β : Type v} [field α] [field β] (i : α →+* β) {x : α} {h : polynomial α} (h_splits : splits i h) (h_roots : roots (map i h) = singleton (coe_fn i x)) : h = coe_fn C (leading_coeff h) * (X - coe_fn C x) := sorry theorem nat_degree_multiset_prod {R : Type u_1} [integral_domain R] {s : multiset (polynomial R)} (h : ∀ (p : polynomial R), p ∈ s → p ≠ 0) : nat_degree (multiset.prod s) = multiset.sum (multiset.map nat_degree s) := sorry theorem nat_degree_eq_card_roots {α : Type u} {β : Type v} [field α] [field β] {p : polynomial α} {i : α →+* β} (hsplit : splits i p) : nat_degree p = coe_fn multiset.card (roots (map i p)) := sorry theorem degree_eq_card_roots {α : Type u} {β : Type v} [field α] [field β] {p : polynomial α} {i : α →+* β} (p_ne_zero : p ≠ 0) (hsplit : splits i p) : degree p = ↑(coe_fn multiset.card (roots (map i p))) := sorry theorem splits_of_exists_multiset {α : Type u} {β : Type v} [field α] [field β] (i : α →+* β) {f : polynomial α} {s : multiset β} (hs : map i f = coe_fn C (coe_fn i (leading_coeff f)) * multiset.prod (multiset.map (fun (a : β) => X - coe_fn C a) s)) : splits i f := sorry theorem splits_of_splits_id {α : Type u} {β : Type v} [field α] [field β] (i : α →+* β) {f : polynomial α} : splits (ring_hom.id α) f → splits i f := sorry theorem splits_iff_exists_multiset {α : Type u} {β : Type v} [field α] [field β] (i : α →+* β) {f : polynomial α} : splits i f ↔ ∃ (s : multiset β), map i f = coe_fn C (coe_fn i (leading_coeff f)) * multiset.prod (multiset.map (fun (a : β) => X - coe_fn C a) s) := sorry theorem splits_comp_of_splits {α : Type u} {β : Type v} {γ : Type w} [field α] [field β] [field γ] (i : α →+* β) (j : β →+* γ) {f : polynomial α} (h : splits i f) : splits (ring_hom.comp j i) f := sorry /-- A monic polynomial `p` that has as much roots as its degree can be written `p = ∏(X - a)`, for `a` in `p.roots`. -/ theorem prod_multiset_X_sub_C_of_monic_of_roots_card_eq {α : Type u} [field α] {p : polynomial α} (hmonic : monic p) (hroots : coe_fn multiset.card (roots p) = nat_degree p) : multiset.prod (multiset.map (fun (a : α) => X - coe_fn C a) (roots p)) = p := sorry /-- A polynomial `p` that has as much roots as its degree can be written `p = p.leading_coeff * ∏(X - a)`, for `a` in `p.roots`. -/ theorem C_leading_coeff_mul_prod_multiset_X_sub_C {α : Type u} [field α] {p : polynomial α} (hroots : coe_fn multiset.card (roots p) = nat_degree p) : coe_fn C (leading_coeff p) * multiset.prod (multiset.map (fun (a : α) => X - coe_fn C a) (roots p)) = p := sorry /-- A polynomial splits if and only if it has as much roots as its degree. -/ theorem splits_iff_card_roots {α : Type u} [field α] {p : polynomial α} : splits (ring_hom.id α) p ↔ coe_fn multiset.card (roots p) = nat_degree p := sorry end polynomial /-- If `p` is the minimal polynomial of `a` over `F` then `F[a] ≃ₐ[F] F[x]/(p)` -/ def alg_equiv.adjoin_singleton_equiv_adjoin_root_minpoly (F : Type u_1) [field F] {R : Type u_2} [comm_ring R] [algebra F R] (x : R) : alg_equiv F (↥(algebra.adjoin F (singleton x))) (adjoin_root (minpoly F x)) := alg_equiv.symm (alg_equiv.of_bijective (alg_hom.cod_restrict (adjoin_root.lift_hom (minpoly F x) x sorry) (algebra.adjoin F (singleton x)) sorry) sorry) -- Speed up the following proof. -- TODO: Why is this so slow? /-- If `K` and `L` are field extensions of `F` and we have `s : finset K` such that the minimal polynomial of each `x ∈ s` splits in `L` then `algebra.adjoin F s` embeds in `L`. -/ theorem lift_of_splits {F : Type u_1} {K : Type u_2} {L : Type u_3} [field F] [field K] [field L] [algebra F K] [algebra F L] (s : finset K) : (∀ (x : K), x ∈ s → is_integral F x ∧ polynomial.splits (algebra_map F L) (minpoly F x)) → Nonempty (alg_hom F (↥(algebra.adjoin F ↑s)) L) := sorry namespace polynomial /-- Non-computably choose an irreducible factor from a polynomial. -/ def factor {α : Type u} [field α] (f : polynomial α) : polynomial α := dite (∃ (g : polynomial α), irreducible g ∧ g ∣ f) (fun (H : ∃ (g : polynomial α), irreducible g ∧ g ∣ f) => classical.some H) fun (H : ¬∃ (g : polynomial α), irreducible g ∧ g ∣ f) => X protected instance irreducible_factor {α : Type u} [field α] (f : polynomial α) : irreducible (factor f) := sorry theorem factor_dvd_of_not_is_unit {α : Type u} [field α] {f : polynomial α} (hf1 : ¬is_unit f) : factor f ∣ f := sorry theorem factor_dvd_of_degree_ne_zero {α : Type u} [field α] {f : polynomial α} (hf : degree f ≠ 0) : factor f ∣ f := factor_dvd_of_not_is_unit (mt degree_eq_zero_of_is_unit hf) theorem factor_dvd_of_nat_degree_ne_zero {α : Type u} [field α] {f : polynomial α} (hf : nat_degree f ≠ 0) : factor f ∣ f := factor_dvd_of_degree_ne_zero (mt nat_degree_eq_of_degree_eq_some hf) /-- Divide a polynomial f by X - C r where r is a root of f in a bigger field extension. -/ def remove_factor {α : Type u} [field α] (f : polynomial α) : polynomial (adjoin_root (factor f)) := map (adjoin_root.of (factor f)) f /ₘ (X - coe_fn C (adjoin_root.root (factor f))) theorem X_sub_C_mul_remove_factor {α : Type u} [field α] (f : polynomial α) (hf : nat_degree f ≠ 0) : (X - coe_fn C (adjoin_root.root (factor f))) * remove_factor f = map (adjoin_root.of (factor f)) f := sorry theorem nat_degree_remove_factor {α : Type u} [field α] (f : polynomial α) : nat_degree (remove_factor f) = nat_degree f - 1 := sorry theorem nat_degree_remove_factor' {α : Type u} [field α] {f : polynomial α} {n : ℕ} (hfn : nat_degree f = n + 1) : nat_degree (remove_factor f) = n := eq.mpr (id (Eq._oldrec (Eq.refl (nat_degree (remove_factor f) = n)) (nat_degree_remove_factor f))) (eq.mpr (id (Eq._oldrec (Eq.refl (nat_degree f - 1 = n)) hfn)) (eq.mpr (id (Eq._oldrec (Eq.refl (n + 1 - 1 = n)) (nat.add_sub_cancel n 1))) (Eq.refl n))) /-- Auxiliary construction to a splitting field of a polynomial. Uses induction on the degree. -/ def splitting_field_aux (n : ℕ) {α : Type u} [field α] (f : polynomial α) : nat_degree f = n → Type u := nat.rec_on n (fun (α : Type u) (_x : field α) (_x_1 : polynomial α) (_x : nat_degree _x_1 = 0) => α) fun (n : ℕ) (ih : {α : Type u} → [_inst_4 : field α] → (f : polynomial α) → nat_degree f = n → Type u) (α : Type u) (_x : field α) (f : polynomial α) (hf : nat_degree f = Nat.succ n) => ih (remove_factor f) (nat_degree_remove_factor' hf) namespace splitting_field_aux theorem succ {α : Type u} [field α] (n : ℕ) (f : polynomial α) (hfn : nat_degree f = n + 1) : splitting_field_aux (n + 1) f hfn = splitting_field_aux n (remove_factor f) (nat_degree_remove_factor' hfn) := rfl protected instance field (n : ℕ) {α : Type u} [field α] {f : polynomial α} (hfn : nat_degree f = n) : field (splitting_field_aux n f hfn) := nat.rec_on n (fun (α : Type u) (_x : field α) (_x_1 : polynomial α) (_x_2 : nat_degree _x_1 = 0) => _x) fun (n : ℕ) (ih : {α : Type u} → [_inst_4 : field α] → {f : polynomial α} → (hfn : nat_degree f = n) → field (splitting_field_aux n f hfn)) (α : Type u) (_x : field α) (f : polynomial α) (hf : nat_degree f = Nat.succ n) => ih (nat_degree_remove_factor' hf) protected instance inhabited {α : Type u} [field α] {n : ℕ} {f : polynomial α} (hfn : nat_degree f = n) : Inhabited (splitting_field_aux n f hfn) := { default := bit1 (bit0 (bit1 (bit0 (bit0 1)))) } protected instance algebra (n : ℕ) {α : Type u} [field α] {f : polynomial α} (hfn : nat_degree f = n) : algebra α (splitting_field_aux n f hfn) := nat.rec_on n (fun (α : Type u) (_x : field α) (_x_1 : polynomial α) (_x_2 : nat_degree _x_1 = 0) => algebra.id α) fun (n : ℕ) (ih : {α : Type u} → [_inst_4 : field α] → {f : polynomial α} → (hfn : nat_degree f = n) → algebra α (splitting_field_aux n f hfn)) (α : Type u) (_x : field α) (f : polynomial α) (hfn : nat_degree f = Nat.succ n) => algebra.comap.algebra α (adjoin_root (factor f)) (splitting_field_aux n (remove_factor f) (nat_degree_remove_factor' hfn)) protected instance algebra' {α : Type u} [field α] {n : ℕ} {f : polynomial α} (hfn : nat_degree f = n + 1) : algebra (adjoin_root (factor f)) (splitting_field_aux (n + 1) f hfn) := splitting_field_aux.algebra n sorry protected instance algebra'' {α : Type u} [field α] {n : ℕ} {f : polynomial α} (hfn : nat_degree f = n + 1) : algebra α (splitting_field_aux n (remove_factor f) (nat_degree_remove_factor' hfn)) := splitting_field_aux.algebra (n + 1) hfn protected instance algebra''' {α : Type u} [field α] {n : ℕ} {f : polynomial α} (hfn : nat_degree f = n + 1) : algebra (adjoin_root (factor f)) (splitting_field_aux n (remove_factor f) (nat_degree_remove_factor' hfn)) := splitting_field_aux.algebra n (nat_degree_remove_factor' hfn) protected instance scalar_tower {α : Type u} [field α] {n : ℕ} {f : polynomial α} (hfn : nat_degree f = n + 1) : is_scalar_tower α (adjoin_root (factor f)) (splitting_field_aux (n + 1) f hfn) := is_scalar_tower.of_algebra_map_eq fun (x : α) => rfl protected instance scalar_tower' {α : Type u} [field α] {n : ℕ} {f : polynomial α} (hfn : nat_degree f = n + 1) : is_scalar_tower α (adjoin_root (factor f)) (splitting_field_aux n (remove_factor f) (nat_degree_remove_factor' hfn)) := is_scalar_tower.of_algebra_map_eq fun (x : α) => rfl theorem algebra_map_succ {α : Type u} [field α] (n : ℕ) (f : polynomial α) (hfn : nat_degree f = n + 1) : algebra_map α (splitting_field_aux (n + 1) f hfn) = ring_hom.comp (algebra_map (adjoin_root (factor f)) (splitting_field_aux n (remove_factor f) (nat_degree_remove_factor' hfn))) (adjoin_root.of (factor f)) := rfl protected theorem splits (n : ℕ) {α : Type u} [field α] (f : polynomial α) (hfn : nat_degree f = n) : splits (algebra_map α (splitting_field_aux n f hfn)) f := sorry theorem exists_lift (n : ℕ) {α : Type u} [field α] (f : polynomial α) (hfn : nat_degree f = n) {β : Type u_1} [field β] (j : α →+* β) (hf : splits j f) : ∃ (k : splitting_field_aux n f hfn →+* β), ring_hom.comp k (algebra_map α (splitting_field_aux n f hfn)) = j := sorry theorem adjoin_roots (n : ℕ) {α : Type u} [field α] (f : polynomial α) (hfn : nat_degree f = n) : algebra.adjoin α ↑(multiset.to_finset (roots (map (algebra_map α (splitting_field_aux n f hfn)) f))) = ⊤ := sorry end splitting_field_aux /-- A splitting field of a polynomial. -/ def splitting_field {α : Type u} [field α] (f : polynomial α) := splitting_field_aux (nat_degree f) f sorry namespace splitting_field protected instance field {α : Type u} [field α] (f : polynomial α) : field (splitting_field f) := splitting_field_aux.field (nat_degree f) (_proof_1 f) protected instance inhabited {α : Type u} [field α] (f : polynomial α) : Inhabited (splitting_field f) := { default := bit1 (bit0 (bit1 (bit0 (bit0 1)))) } protected instance algebra {α : Type u} [field α] (f : polynomial α) : algebra α (splitting_field f) := splitting_field_aux.algebra (nat_degree f) (_proof_1 f) protected theorem splits {α : Type u} [field α] (f : polynomial α) : splits (algebra_map α (splitting_field f)) f := splitting_field_aux.splits (nat_degree f) f (_proof_1 f) /-- Embeds the splitting field into any other field that splits the polynomial. -/ def lift {α : Type u} {β : Type v} [field α] [field β] (f : polynomial α) [algebra α β] (hb : splits (algebra_map α β) f) : alg_hom α (splitting_field f) β := alg_hom.mk (ring_hom.to_fun (classical.some sorry)) sorry sorry sorry sorry sorry theorem adjoin_roots {α : Type u} [field α] (f : polynomial α) : algebra.adjoin α ↑(multiset.to_finset (roots (map (algebra_map α (splitting_field f)) f))) = ⊤ := splitting_field_aux.adjoin_roots (nat_degree f) f (_proof_1 f) end splitting_field /-- Typeclass characterising splitting fields. -/ class is_splitting_field (α : Type u) (β : Type v) [field α] [field β] [algebra α β] (f : polynomial α) where splits : splits (algebra_map α β) f adjoin_roots : algebra.adjoin α ↑(multiset.to_finset (roots (map (algebra_map α β) f))) = ⊤ namespace is_splitting_field protected instance splitting_field {α : Type u} [field α] (f : polynomial α) : is_splitting_field α (splitting_field f) f := mk (splitting_field.splits f) (splitting_field.adjoin_roots f) protected instance map {α : Type u} {β : Type v} {γ : Type w} [field α] [field β] [field γ] [algebra α β] [algebra β γ] [algebra α γ] [is_scalar_tower α β γ] (f : polynomial α) [is_splitting_field α γ f] : is_splitting_field β γ (map (algebra_map α β) f) := sorry theorem splits_iff {α : Type u} (β : Type v) [field α] [field β] [algebra α β] (f : polynomial α) [is_splitting_field α β f] : splits (ring_hom.id α) f ↔ ⊤ = ⊥ := sorry theorem mul {α : Type u} (β : Type v) {γ : Type w} [field α] [field β] [field γ] [algebra α β] [algebra β γ] [algebra α γ] [is_scalar_tower α β γ] (f : polynomial α) (g : polynomial α) (hf : f ≠ 0) (hg : g ≠ 0) [is_splitting_field α β f] [is_splitting_field β γ (map (algebra_map α β) g)] : is_splitting_field α γ (f * g) := sorry /-- Splitting field of `f` embeds into any field that splits `f`. -/ def lift {α : Type u} (β : Type v) {γ : Type w} [field α] [field β] [field γ] [algebra α β] [algebra α γ] (f : polynomial α) [is_splitting_field α β f] (hf : splits (algebra_map α γ) f) : alg_hom α β γ := dite (f = 0) (fun (hf0 : f = 0) => alg_hom.comp (algebra.of_id α γ) (alg_hom.comp (↑(algebra.bot_equiv α β)) (eq.mpr sorry algebra.to_top))) fun (hf0 : ¬f = 0) => alg_hom.comp (eq.mpr sorry (Classical.choice sorry)) algebra.to_top theorem finite_dimensional {α : Type u} (β : Type v) [field α] [field β] [algebra α β] (f : polynomial α) [is_splitting_field α β f] : finite_dimensional α β := sorry /-- Any splitting field is isomorphic to `splitting_field f`. -/ def alg_equiv {α : Type u} (β : Type v) [field α] [field β] [algebra α β] (f : polynomial α) [is_splitting_field α β f] : alg_equiv α β (splitting_field f) := alg_equiv.of_bijective (lift β f sorry) sorry end Mathlib
d25083448a51522695447a34a63085fae2951c60
4727251e0cd73359b15b664c3170e5d754078599
/counterexamples/girard.lean
218c892caa71ea674fc3df9455084296d26898b1
[ "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
2,186
lean
/- Copyright (c) 2021 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import logic.basic /-! # Girard's paradox Girard's paradox is a proof that `Type : Type` entails a contradiction. We can't say this directly in Lean because `Type : Type 1` and it's not possible to give `Type` a different type via an axiom, so instead we axiomatize the behavior of the Pi type and application if the typing rule for Pi was `(Type → Type) → Type` instead of `(Type → Type) → Type 1`. Furthermore, we don't actually want false axioms in mathlib, so rather than introducing the axioms using `axiom` or `constant` declarations, we take them as assumptions to the `girard` theorem. Based on Watkins' LF implementation of Hurkens' simplification of Girard's paradox: <http://www.cs.cmu.edu/~kw/research/hurkens95tlca.elf>. ## Main statements * `girard`: there are no Girard universes. -/ /-- **Girard's paradox**: there are no universes `u` such that `Type u : Type u`. Since we can't actually change the type of Lean's `Π` operator, we assume the existence of `pi`, `lam`, `app` and the `beta` rule equivalent to the `Π` and `app` constructors of type theory. -/ theorem {u} girard (pi : (Type u → Type u) → Type u) (lam : ∀ {A : Type u → Type u}, (∀ x, A x) → pi A) (app : ∀ {A}, pi A → ∀ x, A x) (beta : ∀ {A : Type u → Type u} (f : ∀ x, A x) (x), app (lam f) x = f x) : false := let F (X) := (set (set X) → X) → set (set X), U := pi F in let G (T : set (set U)) (X) : F X := λ f, {p | {x : U | f (app x X f) ∈ p} ∈ T} in let τ (T : set (set U)) : U := lam (G T) in let σ (S : U) : set (set U) := app S U τ in have στ : ∀ {s S}, s ∈ σ (τ S) ↔ {x | τ (σ x) ∈ s} ∈ S := λ s S, iff_of_eq (congr_arg (λ f : F U, s ∈ f τ) (beta (G S) U) : _), let ω : set (set U) := {p | ∀ x, p ∈ σ x → x ∈ p} in let δ (S : set (set U)) := ∀ p, p ∈ S → τ S ∈ p in have δ ω := λ p d, d (τ ω) $ στ.2 $ λ x h, d (τ (σ x)) (στ.2 h), this {y | ¬ δ (σ y)} (λ x e f, f _ e (λ p h, f _ (στ.1 h))) (λ p h, this _ (στ.1 h))
16e2c36649f9e2027f1ecbb9979d827eb146011e
3dd1b66af77106badae6edb1c4dea91a146ead30
/tests/lean/run/nat_bug4.lean
73d83a6013a8f5fb2a71e39997ad17f3be34a07a
[ "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
442
lean
import logic num using num eq_proofs inductive nat : Type := | zero : nat | succ : nat → nat definition add (x y : nat) : nat := nat_rec x (λn r, succ r) y infixl `+`:65 := add definition mul (n m : nat) := nat_rec zero (fun m x, x + n) m infixl `*`:75 := mul axiom mul_succ_right (n m : nat) : n * succ m = n * m + n theorem small2 (n m l : nat) : n * succ l + m = n * l + n + m := subst (mul_succ_right _ _) (refl (n * succ l + m))
b1f719056f3c3cab7cc444be60675229889918ef
4727251e0cd73359b15b664c3170e5d754078599
/src/algebra/ring/idempotents.lean
14f652b90269e92a341f414013a47b180497a5c4
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
4,112
lean
/- Copyright (c) 2022 Christopher Hoskin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Christopher Hoskin -/ import algebra.ring.basic import algebra.group_power.basic import tactic.nth_rewrite.default /-! # Idempotents This file defines idempotents for an arbitary multiplication and proves some basic results, including: * `is_idempotent_elem.mul_of_commute`: In a semigroup, the product of two commuting idempotents is an idempotent; * `is_idempotent_elem.one_sub_iff`: In a (non-associative) ring, `p` is an idempotent if and only if `1-p` is an idempotent. * `is_idempotent_elem.pow_succ_eq`: In a monoid `p ^ (n+1) = p` for `p` an idempotent and `n` a natural number. ## Tags projection, idempotent -/ variables {M N S M₀ M₁ R G G₀ : Type*} variables [has_mul M] [monoid N] [semigroup S] [mul_zero_class M₀] [mul_one_class M₁] [non_assoc_ring R] [group G] [group_with_zero G₀] /-- An element `p` is said to be idempotent if `p * p = p` -/ def is_idempotent_elem (p : M) : Prop := p * p = p namespace is_idempotent_elem lemma of_is_idempotent [is_idempotent M (*)] (a : M) : is_idempotent_elem a := is_idempotent.idempotent a lemma eq {p : M} (h : is_idempotent_elem p) : p * p = p := h lemma mul_of_commute {p q : S} (h : commute p q) (h₁ : is_idempotent_elem p) (h₂ : is_idempotent_elem q) : is_idempotent_elem (p * q) := by rw [is_idempotent_elem, mul_assoc, ← mul_assoc q, ←h.eq, mul_assoc p, h₂.eq, ← mul_assoc, h₁.eq] lemma zero : is_idempotent_elem (0 : M₀) := mul_zero _ lemma one : is_idempotent_elem (1 : M₁) := mul_one _ lemma one_sub {p : R} (h : is_idempotent_elem p) : is_idempotent_elem (1 - p) := begin rw is_idempotent_elem at h, rw [is_idempotent_elem, mul_sub_left_distrib, mul_one, sub_mul, one_mul, h, sub_self, sub_zero], end @[simp] lemma one_sub_iff {p : R} : is_idempotent_elem (1 - p) ↔ is_idempotent_elem p := ⟨ λ h, sub_sub_cancel 1 p ▸ h.one_sub, is_idempotent_elem.one_sub ⟩ lemma pow {p : N} (n : ℕ) (h : is_idempotent_elem p) : is_idempotent_elem (p ^ n) := begin induction n with n ih, { rw pow_zero, apply one, }, { unfold is_idempotent_elem, rw [pow_succ, ← mul_assoc, ← pow_mul_comm', mul_assoc (p^n), h.eq, pow_mul_comm', mul_assoc, ih.eq], } end lemma pow_succ_eq {p : N} (n : ℕ) (h : is_idempotent_elem p) : p ^ (n + 1) = p := begin induction n with n ih, { rw [nat.zero_add, pow_one], }, { rw [pow_succ, ih, h.eq], } end @[simp] lemma iff_eq_one {p : G} : is_idempotent_elem p ↔ p = 1 := begin split, { intro h, rw ← mul_left_inv p, nth_rewrite_rhs 1 ← h.eq, rw [← mul_assoc, mul_left_inv, one_mul] }, { intro h, rw h, apply one, } end @[simp] lemma iff_eq_zero_or_one {p : G₀} : is_idempotent_elem p ↔ p = 0 ∨ p = 1 := begin refine ⟨λ h, or_iff_not_imp_left.mpr (λ hp, _), _⟩, { rw ← mul_inv_cancel hp, nth_rewrite_rhs 0 ← h.eq, rw [mul_assoc, mul_inv_cancel hp, mul_one] }, { rintro (h₁ | h₂), { rw h₁, exact zero, }, { rw h₂, exact one, } } end /-! ### Instances on `subtype is_idempotent_elem` -/ section instances instance : has_zero { p : M₀ // is_idempotent_elem p } := { zero := ⟨ 0, zero ⟩ } @[simp] lemma coe_zero : ↑(0 : {p : M₀ // is_idempotent_elem p}) = (0 : M₀) := rfl instance : has_one { p : M₁ // is_idempotent_elem p } := { one := ⟨ 1, one ⟩ } @[simp] lemma coe_one : ↑(1 : { p : M₁ // is_idempotent_elem p }) = (1 : M₁) := rfl instance : has_compl { p : R // is_idempotent_elem p } := ⟨λ p, ⟨1 - p, p.prop.one_sub⟩⟩ @[simp] lemma coe_compl (p : { p : R // is_idempotent_elem p }) : ↑(pᶜ) = (1 : R) - ↑p := rfl @[simp] lemma compl_compl (p : {p : R // is_idempotent_elem p}) : pᶜᶜ = p := subtype.ext $ sub_sub_cancel _ _ @[simp] lemma zero_compl : (0 : {p : R // is_idempotent_elem p})ᶜ = 1 := subtype.ext $ sub_zero _ @[simp] lemma one_compl : (1 : {p : R // is_idempotent_elem p})ᶜ = 0 := subtype.ext $ sub_self _ end instances end is_idempotent_elem