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
df615f6493cef05ec8316c56c0143acdb2d76992
d1a52c3f208fa42c41df8278c3d280f075eb020c
/src/Lean/Elab/Tactic/ElabTerm.lean
b1ee2c900d3bcefbbc4a1c9d52997913c5423fda
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach" ]
permissive
cipher1024/lean4
6e1f98bb58e7a92b28f5364eb38a14c8d0aae393
69114d3b50806264ef35b57394391c3e738a9822
refs/heads/master
1,642,227,983,603
1,642,011,696,000
1,642,011,696,000
228,607,691
0
0
Apache-2.0
1,576,584,269,000
1,576,584,268,000
null
UTF-8
Lean
false
false
12,548
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.Meta.CollectMVars import Lean.Meta.Tactic.Apply import Lean.Meta.Tactic.Constructor import Lean.Meta.Tactic.Assert import Lean.Elab.Tactic.Basic import Lean.Elab.SyntheticMVars namespace Lean.Elab.Tactic open Meta /- `elabTerm` for Tactics and basic tactics that use it. -/ def elabTerm (stx : Syntax) (expectedType? : Option Expr) (mayPostpone := false) : TacticM Expr := do /- We have disabled `Term.withoutErrToSorry` to improve error recovery. When we were using it, any tactic using `elabTerm` would be interrupted at elaboration errors. Tactics that do not want to proceed should check whether the result contains sythetic sorrys or disable `errToSorry` before invoking `elabTerm` -/ withRef stx do -- <| Term.withoutErrToSorry do let e ← Term.elabTerm stx expectedType? Term.synthesizeSyntheticMVars mayPostpone instantiateMVars e def elabTermEnsuringType (stx : Syntax) (expectedType? : Option Expr) (mayPostpone := false) : TacticM Expr := do let e ← elabTerm stx expectedType? mayPostpone -- We do use `Term.ensureExpectedType` because we don't want coercions being inserted here. match expectedType? with | none => return e | some expectedType => let eType ← inferType e -- We allow synthetic opaque metavars to be assigned in the following step since the `isDefEq` is not really -- part of the elaboration, but part of the tactic. See issue #492 unless (← withAssignableSyntheticOpaque do isDefEq eType expectedType) do Term.throwTypeMismatchError none expectedType eType e return e /- Try to close main goal using `x target`, where `target` is the type of the main goal. -/ def closeMainGoalUsing (x : Expr → TacticM Expr) (checkUnassigned := true) : TacticM Unit := withMainContext do closeMainGoal (checkUnassigned := checkUnassigned) (← x (← getMainTarget)) def logUnassignedAndAbort (mvarIds : Array MVarId) : TacticM Unit := do if (← Term.logUnassignedUsingErrorInfos mvarIds) then throwAbortTactic def filterOldMVars (mvarIds : Array MVarId) (mvarCounterSaved : Nat) : MetaM (Array MVarId) := do let mctx ← getMCtx return mvarIds.filter fun mvarId => (mctx.getDecl mvarId |>.index) >= mvarCounterSaved @[builtinTactic «exact»] def evalExact : Tactic := fun stx => match stx with | `(tactic| exact $e) => closeMainGoalUsing (checkUnassigned := false) fun type => do let mvarCounterSaved := (← getMCtx).mvarCounter let r ← elabTermEnsuringType e type logUnassignedAndAbort (← filterOldMVars (← getMVars r) mvarCounterSaved) return r | _ => throwUnsupportedSyntax def elabTermWithHoles (stx : Syntax) (expectedType? : Option Expr) (tagSuffix : Name) (allowNaturalHoles := false) : TacticM (Expr × List MVarId) := do let mvarCounterSaved := (← getMCtx).mvarCounter let val ← elabTermEnsuringType stx expectedType? let newMVarIds ← getMVarsNoDelayed val /- ignore let-rec auxiliary variables, they are synthesized automatically later -/ let newMVarIds ← newMVarIds.filterM fun mvarId => return !(← Term.isLetRecAuxMVar mvarId) let newMVarIds ← if allowNaturalHoles then pure newMVarIds.toList else let naturalMVarIds ← newMVarIds.filterM fun mvarId => return (← getMVarDecl mvarId).kind.isNatural let syntheticMVarIds ← newMVarIds.filterM fun mvarId => return !(← getMVarDecl mvarId).kind.isNatural let naturalMVarIds ← filterOldMVars naturalMVarIds mvarCounterSaved logUnassignedAndAbort naturalMVarIds pure syntheticMVarIds.toList tagUntaggedGoals (← getMainTag) tagSuffix newMVarIds pure (val, newMVarIds) /- If `allowNaturalHoles == true`, then we allow the resultant expression to contain unassigned "natural" metavariables. Recall that "natutal" metavariables are created for explicit holes `_` and implicit arguments. They are meant to be filled by typing constraints. "Synthetic" metavariables are meant to be filled by tactics and are usually created using the synthetic hole notation `?<hole-name>`. -/ def refineCore (stx : Syntax) (tagSuffix : Name) (allowNaturalHoles : Bool) : TacticM Unit := do withMainContext do let (val, mvarIds') ← elabTermWithHoles stx (← getMainTarget) tagSuffix allowNaturalHoles assignExprMVar (← getMainGoal) val replaceMainGoal mvarIds' @[builtinTactic «refine»] def evalRefine : Tactic := fun stx => match stx with | `(tactic| refine $e) => refineCore e `refine (allowNaturalHoles := false) | _ => throwUnsupportedSyntax @[builtinTactic «refine'»] def evalRefine' : Tactic := fun stx => match stx with | `(tactic| refine' $e) => refineCore e `refine' (allowNaturalHoles := true) | _ => throwUnsupportedSyntax @[builtinTactic «specialize»] def evalSpecialize : Tactic := fun stx => withMainContext do match stx with | `(tactic| specialize $e:term) => let (e, mvarIds') ← elabTermWithHoles e none `specialize (allowNaturalHoles := true) let h := e.getAppFn if h.isFVar then let localDecl ← getLocalDecl h.fvarId! let mvarId ← assert (← getMainGoal) localDecl.userName (← inferType e).headBeta e let (_, mvarId) ← intro1P mvarId let mvarId ← tryClear mvarId h.fvarId! replaceMainGoal (mvarId :: mvarIds') else throwError "'specialize' requires a term of the form `h x_1 .. x_n` where `h` appears in the local context" | _ => throwUnsupportedSyntax /-- Given a tactic ``` apply f ``` we want the `apply` tactic to create all metavariables. The following definition will return `@f` for `f`. That is, it will **not** create metavariables for implicit arguments. A similar method is also used in Lean 3. This method is useful when applying lemmas such as: ``` theorem infLeRight {s t : Set α} : s ⊓ t ≤ t ``` where `s ≤ t` here is defined as ``` ∀ {x : α}, x ∈ s → x ∈ t ``` -/ def elabTermForApply (stx : Syntax) (mayPostpone := true) : TacticM Expr := do if stx.isIdent then match (← Term.resolveId? stx (withInfo := true)) with | some e => return e | _ => pure () elabTerm stx none mayPostpone def evalApplyLikeTactic (tac : MVarId → Expr → MetaM (List MVarId)) (e : Syntax) : TacticM Unit := do withMainContext do let val ← elabTermForApply e let mvarIds' ← tac (← getMainGoal) val Term.synthesizeSyntheticMVarsNoPostponing replaceMainGoal mvarIds' def getFVarId (id : Syntax) : TacticM FVarId := withRef id do -- use apply-like elaboration to suppress insertion of implicit arguments let e ← withMainContext do elabTermForApply id (mayPostpone := false) match e with | Expr.fvar fvarId _ => return fvarId | _ => throwError "unexpected term '{e}'; expected single reference to variable" def getFVarIds (ids : Array Syntax) : TacticM (Array FVarId) := do withMainContext do ids.mapM getFVarId @[builtinTactic Lean.Parser.Tactic.apply] def evalApply : Tactic := fun stx => match stx with | `(tactic| apply $e) => evalApplyLikeTactic Meta.apply e | _ => throwUnsupportedSyntax @[builtinTactic Lean.Parser.Tactic.constructor] def evalConstructor : Tactic := fun stx => withMainContext do let mvarIds' ← Meta.constructor (← getMainGoal) Term.synthesizeSyntheticMVarsNoPostponing replaceMainGoal mvarIds' @[builtinTactic Lean.Parser.Tactic.existsIntro] def evalExistsIntro : Tactic := fun stx => match stx with | `(tactic| exists $e) => evalApplyLikeTactic (fun mvarId e => return [(← Meta.existsIntro mvarId e)]) e | _ => throwUnsupportedSyntax @[builtinTactic Lean.Parser.Tactic.withReducible] def evalWithReducible : Tactic := fun stx => withReducible <| evalTactic stx[1] @[builtinTactic Lean.Parser.Tactic.withReducibleAndInstances] def evalWithReducibleAndInstances : Tactic := fun stx => withReducibleAndInstances <| evalTactic stx[1] /-- Elaborate `stx`. If it a free variable, return it. Otherwise, assert it, and return the free variable. Note that, the main goal is updated when `Meta.assert` is used in the second case. -/ def elabAsFVar (stx : Syntax) (userName? : Option Name := none) : TacticM FVarId := withMainContext do let e ← elabTerm stx none match e with | Expr.fvar fvarId _ => pure fvarId | _ => let type ← inferType e let intro (userName : Name) (preserveBinderNames : Bool) : TacticM FVarId := do let mvarId ← getMainGoal let (fvarId, mvarId) ← liftMetaM do let mvarId ← Meta.assert mvarId userName type e Meta.intro1Core mvarId preserveBinderNames replaceMainGoal [mvarId] return fvarId match userName? with | none => intro `h false | some userName => intro userName true @[builtinTactic Lean.Parser.Tactic.rename] def evalRename : Tactic := fun stx => match stx with | `(tactic| rename $typeStx:term => $h:ident) => do withMainContext do /- Remark: we must not use `withoutModifyingState` because we may miss errors message. For example, suppose the following `elabTerm` logs an error during elaboration. In this scenario, the term `type` contains a synthetic `sorry`, and the error message `"failed to find ..."` is not logged by the outer loop. By using `withoutModifyingStateWithInfoAndMessages`, we ensure that the messages and the info trees are preserved while the rest of the state is backtracked. -/ let fvarId ← withoutModifyingStateWithInfoAndMessages <| withNewMCtxDepth do let type ← elabTerm typeStx none (mayPostpone := true) let fvarId? ← (← getLCtx).findDeclRevM? fun localDecl => do if (← isDefEq type localDecl.type) then return localDecl.fvarId else return none match fvarId? with | none => throwError "failed to find a hypothesis with type{indentExpr type}" | some fvarId => return fvarId let lctxNew := (← getLCtx).setUserName fvarId h.getId let mvarNew ← mkFreshExprMVarAt lctxNew (← getLocalInstances) (← getMainTarget) MetavarKind.syntheticOpaque (← getMainTag) assignExprMVar (← getMainGoal) mvarNew replaceMainGoal [mvarNew.mvarId!] | _ => throwUnsupportedSyntax /-- Make sure `expectedType` does not contain free and metavariables. It applies zeta-reduction to eliminate let-free-vars. -/ private def preprocessPropToDecide (expectedType : Expr) : TermElabM Expr := do let mut expectedType ← instantiateMVars expectedType if expectedType.hasFVar then expectedType ← zetaReduce expectedType if expectedType.hasFVar || expectedType.hasMVar then throwError "expected type must not contain free or meta variables{indentExpr expectedType}" return expectedType @[builtinTactic Lean.Parser.Tactic.decide] def evalDecide : Tactic := fun stx => closeMainGoalUsing fun expectedType => do let expectedType ← preprocessPropToDecide expectedType let d ← mkDecide expectedType let d ← instantiateMVars d let r ← withDefault <| whnf d unless r.isConstOf ``true do throwError "failed to reduce to 'true'{indentExpr r}" let s := d.appArg! -- get instance from `d` let rflPrf ← mkEqRefl (toExpr true) return mkApp3 (Lean.mkConst ``of_decide_eq_true) expectedType s rflPrf private def mkNativeAuxDecl (baseName : Name) (type val : Expr) : TermElabM Name := do let auxName ← Term.mkAuxName baseName let decl := Declaration.defnDecl { name := auxName, levelParams := [], type := type, value := val, hints := ReducibilityHints.abbrev, safety := DefinitionSafety.safe } addDecl decl compileDecl decl pure auxName @[builtinTactic Lean.Parser.Tactic.nativeDecide] def evalNativeDecide : Tactic := fun stx => closeMainGoalUsing fun expectedType => do let expectedType ← preprocessPropToDecide expectedType let d ← mkDecide expectedType let auxDeclName ← mkNativeAuxDecl `_nativeDecide (Lean.mkConst `Bool) d let rflPrf ← mkEqRefl (toExpr true) let s := d.appArg! -- get instance from `d` return mkApp3 (Lean.mkConst ``of_decide_eq_true) expectedType s <| mkApp3 (Lean.mkConst ``Lean.ofReduceBool) (Lean.mkConst auxDeclName) (toExpr true) rflPrf end Lean.Elab.Tactic
b21f4e7d414984cec4da2f09c67407339ac7a49a
e7de183433d907275be4926240a8c8ddb5915131
/recipes/literate_lean4.lean
d7305d0b00807b8d559285caaf552c31335f9fe2
[ "MIT" ]
permissive
cpitclaudel/alectryon
70b01086e9b4aee4f18017621578004903ce74d3
11e8cdc8395d66858baa7371b6cf8e827ca38f4a
refs/heads/master
1,683,666,125,135
1,683,473,634,000
1,683,473,844,000
260,735,576
206
29
MIT
1,655,000,579,000
1,588,439,321,000
HTML
UTF-8
Lean
false
false
1,625
lean
/-! ================================================== Literate programming with Alectryon (Lean4 input) ================================================== Alectryon supports literate programs and documents (combinations of code and prose) written in Lean4 and reStructuredText. Here is an example written in Lean4. It can be converted to reST, HTML, or LaTeX using the following commands:: alectryon literate_lean4.lean # Lean4+reST → HTML; produces ‘literate_lean4.html’ alectryon literate_lean4.lean --backend latex \ --latex-dialect xelatex \ -o literate_lean4.xe.tex # Lean4+reST → LaTeX; produces ‘literate_lean4.xe.tex’ alectryon literate_lean4.lean --backend rst # Lean4+reST → reST; produces ‘literate_lean4.lean.rst’ ----- .. default-role:: lean4 Running queries =============== Alectryon captures the results of `#check`, `#eval`, and the like: -/ def x : Nat := 5 #reduce 5 + x /-! By default, these results are folded and are displayed upon hovering or clicking. We can unfold them by default using annotations or directives: -/ #check Nat /- .unfold -/ /-! .. lean4:: unfold -/ #check Bool #eval 1 + 1 /-! Other flags can be used to control display, like ``.no-in``: -/ #print Iff /- .unfold .no-in -/ /-! Documenting proofs ================== Alectryon also captures goals and hypotheses as proofs progress: -/ theorem test (p q : Prop) (hp : p) (hq : q): p ∧ q ↔ q ∧ p := by apply Iff.intro . intro h apply And.intro . exact hq . exact hp . intro h apply And.intro . exact hp . exact hq
a863f8e504105f9db6b7ea2c497d800cc482387f
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/category_theory/sites/limits.lean
8fcf0570551903b53b083ec521996cfd159f4bb4
[ "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,140
lean
/- Copyright (c) 2021 Adam Topaz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Adam Topaz -/ import category_theory.limits.creates import category_theory.sites.sheafification /-! # Limits and colimits of sheaves > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. ## Limits We prove that the forgetful functor from `Sheaf J D` to presheaves creates limits. If the target category `D` has limits (of a certain shape), this then implies that `Sheaf J D` has limits of the same shape and that the forgetful functor preserves these limits. ## Colimits Given a diagram `F : K ⥤ Sheaf J D` of sheaves, and a colimit cocone on the level of presheaves, we show that the cocone obtained by sheafifying the cocone point is a colimit cocone of sheaves. This allows us to show that `Sheaf J D` has colimits (of a certain shape) as soon as `D` does. -/ namespace category_theory namespace Sheaf open category_theory.limits open opposite section limits universes w v u z variables {C : Type u} [category.{v} C] {J : grothendieck_topology C} variables {D : Type w} [category.{max v u} D] variables {K : Type z} [small_category K] noncomputable theory section /-- An auxiliary definition to be used below. Whenever `E` is a cone of shape `K` of sheaves, and `S` is the multifork associated to a covering `W` of an object `X`, with respect to the cone point `E.X`, this provides a cone of shape `K` of objects in `D`, with cone point `S.X`. See `is_limit_multifork_of_is_limit` for more on how this definition is used. -/ def multifork_evaluation_cone (F : K ⥤ Sheaf J D) (E : cone (F ⋙ Sheaf_to_presheaf J D)) (X : C) (W : J.cover X) (S : multifork (W.index E.X)) : cone (F ⋙ Sheaf_to_presheaf J D ⋙ (evaluation Cᵒᵖ D).obj (op X)) := { X := S.X, π := { app := λ k, (presheaf.is_limit_of_is_sheaf J (F.obj k).1 W (F.obj k).2).lift $ multifork.of_ι _ S.X (λ i, S.ι i ≫ (E.π.app k).app (op i.Y)) begin intros i, simp only [category.assoc], erw [← (E.π.app k).naturality, ← (E.π.app k).naturality], dsimp, simp only [← category.assoc], congr' 1, apply S.condition, end, naturality' := begin intros i j f, dsimp [presheaf.is_limit_of_is_sheaf], rw [category.id_comp], apply presheaf.is_sheaf.hom_ext (F.obj j).2 W, intros ii, rw [presheaf.is_sheaf.amalgamate_map, category.assoc, ← (F.map f).val.naturality, ← category.assoc, presheaf.is_sheaf.amalgamate_map], dsimp [multifork.of_ι], erw [category.assoc, ← E.w f], tidy, end } } variables [has_limits_of_shape K D] /-- If `E` is a cone of shape `K` of sheaves, which is a limit on the level of presheves, this definition shows that the limit presheaf satisfies the multifork variant of the sheaf condition, at a given covering `W`. This is used below in `is_sheaf_of_is_limit` to show that the limit presheaf is indeed a sheaf. -/ def is_limit_multifork_of_is_limit (F : K ⥤ Sheaf J D) (E : cone (F ⋙ Sheaf_to_presheaf J D)) (hE : is_limit E) (X : C) (W : J.cover X) : is_limit (W.multifork E.X) := multifork.is_limit.mk _ (λ S, (is_limit_of_preserves ((evaluation Cᵒᵖ D).obj (op X)) hE).lift $ multifork_evaluation_cone F E X W S) begin intros S i, apply (is_limit_of_preserves ((evaluation Cᵒᵖ D).obj (op i.Y)) hE).hom_ext, intros k, dsimp [multifork.of_ι], erw [category.assoc, (E.π.app k).naturality], dsimp, rw ← category.assoc, erw (is_limit_of_preserves ((evaluation Cᵒᵖ D).obj (op X)) hE).fac (multifork_evaluation_cone F E X W S), dsimp [multifork_evaluation_cone, presheaf.is_limit_of_is_sheaf], erw presheaf.is_sheaf.amalgamate_map, refl, end begin intros S m hm, apply (is_limit_of_preserves ((evaluation Cᵒᵖ D).obj (op X)) hE).hom_ext, intros k, dsimp, erw (is_limit_of_preserves ((evaluation Cᵒᵖ D).obj (op X)) hE).fac, apply presheaf.is_sheaf.hom_ext (F.obj k).2 W, intros i, erw presheaf.is_sheaf.amalgamate_map, dsimp [multifork.of_ι], change _ = S.ι i ≫ _, erw [← hm, category.assoc, ← (E.π.app k).naturality, category.assoc], refl, end /-- If `E` is a cone which is a limit on the level of presheaves, then the limit presheaf is again a sheaf. This is used to show that the forgetful functor from sheaves to presheaves creates limits. -/ lemma is_sheaf_of_is_limit (F : K ⥤ Sheaf J D) (E : cone (F ⋙ Sheaf_to_presheaf J D)) (hE : is_limit E) : presheaf.is_sheaf J E.X := begin rw presheaf.is_sheaf_iff_multifork, intros X S, exact ⟨is_limit_multifork_of_is_limit _ _ hE _ _⟩, end instance (F : K ⥤ Sheaf J D) : creates_limit F (Sheaf_to_presheaf J D) := creates_limit_of_reflects_iso $ λ E hE, { lifted_cone := ⟨⟨E.X, is_sheaf_of_is_limit _ _ hE⟩, ⟨λ t, ⟨E.π.app _⟩, λ u v e, Sheaf.hom.ext _ _ $ E.π.naturality _⟩⟩, valid_lift := cones.ext (eq_to_iso rfl) $ λ j, by { dsimp, simp }, makes_limit := { lift := λ S, ⟨hE.lift ((Sheaf_to_presheaf J D).map_cone S)⟩, fac' := λ S j, by { ext1, apply hE.fac ((Sheaf_to_presheaf J D).map_cone S) j }, uniq' := λ S m hm, begin ext1, exact hE.uniq ((Sheaf_to_presheaf J D).map_cone S) m.val (λ j, congr_arg hom.val (hm j)), end } } instance : creates_limits_of_shape K (Sheaf_to_presheaf J D) := {} instance : has_limits_of_shape K (Sheaf J D) := has_limits_of_shape_of_has_limits_of_shape_creates_limits_of_shape (Sheaf_to_presheaf J D) end instance [has_limits D] : creates_limits (Sheaf_to_presheaf J D) := ⟨⟩ instance [has_limits D] : has_limits (Sheaf J D) := has_limits_of_has_limits_creates_limits (Sheaf_to_presheaf J D) end limits section colimits universes w v u variables {C : Type u} [category.{v} C] {J : grothendieck_topology C} variables {D : Type w} [category.{max v u} D] variables {K : Type (max v u)} [small_category K] -- Now we need a handful of instances to obtain sheafification... variables [concrete_category.{max v u} D] variables [∀ (P : Cᵒᵖ ⥤ D) (X : C) (S : J.cover X), has_multiequalizer (S.index P)] variables [preserves_limits (forget D)] variables [∀ (X : C), has_colimits_of_shape (J.cover X)ᵒᵖ D] variables [∀ (X : C), preserves_colimits_of_shape (J.cover X)ᵒᵖ (forget D)] variables [reflects_isomorphisms (forget D)] /-- Construct a cocone by sheafifying a cocone point of a cocone `E` of presheaves over a functor which factors through sheaves. In `is_colimit_sheafify_cocone`, we show that this is a colimit cocone when `E` is a colimit. -/ @[simps] def sheafify_cocone {F : K ⥤ Sheaf J D} (E : cocone (F ⋙ Sheaf_to_presheaf J D)) : cocone F := { X := ⟨J.sheafify E.X, grothendieck_topology.plus.is_sheaf_plus_plus _ _⟩, ι := { app := λ k, ⟨E.ι.app k ≫ J.to_sheafify E.X⟩, naturality' := λ i j f, by { ext1, dsimp, erw [category.comp_id, ← category.assoc, E.w f] } } } /-- If `E` is a colimit cocone of presheaves, over a diagram factoring through sheaves, then `sheafify_cocone E` is a colimit cocone. -/ @[simps] def is_colimit_sheafify_cocone {F : K ⥤ Sheaf J D} (E : cocone (F ⋙ Sheaf_to_presheaf J D)) (hE : is_colimit E) : is_colimit (sheafify_cocone E) := { desc := λ S, ⟨J.sheafify_lift (hE.desc ((Sheaf_to_presheaf J D).map_cocone S)) S.X.2⟩, fac' := begin intros S j, ext1, dsimp [sheafify_cocone], erw [category.assoc, J.to_sheafify_sheafify_lift, hE.fac], refl, end, uniq' := begin intros S m hm, ext1, apply J.sheafify_lift_unique, apply hE.uniq ((Sheaf_to_presheaf J D).map_cocone S), intros j, dsimp, simpa only [← category.assoc, ← hm], end } instance [has_colimits_of_shape K D] : has_colimits_of_shape K (Sheaf J D) := ⟨λ F, has_colimit.mk ⟨sheafify_cocone (colimit.cocone _), is_colimit_sheafify_cocone _ (colimit.is_colimit _)⟩⟩ instance [has_colimits D] : has_colimits (Sheaf J D) := ⟨infer_instance⟩ end colimits end Sheaf end category_theory
b48111f25711802541475486bf0dc31a020ceba7
d1bbf1801b3dcb214451d48214589f511061da63
/src/field_theory/galois.lean
c79776fcd329ebd6faf35cfb8b60fa86c3b8bbd0
[ "Apache-2.0" ]
permissive
cheraghchi/mathlib
5c366f8c4f8e66973b60c37881889da8390cab86
f29d1c3038422168fbbdb2526abf7c0ff13e86db
refs/heads/master
1,676,577,831,283
1,610,894,638,000
1,610,894,638,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
18,409
lean
/- Copyright (c) 2020 Thomas Browning and Patrick Lutz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Thomas Browning and Patrick Lutz -/ import field_theory.normal import field_theory.primitive_element import field_theory.fixed import ring_theory.power_basis /-! # Galois Extensions In this file we define Galois extensions as extensions which are both separable and normal. ## Main definitions - `is_galois F E` where `E` is an extension of `F` - `fixed_field H` where `H : subgroup (E ≃ₐ[F] E)` - `fixing_subgroup K` where `K : intermediate_field F E` - `galois_correspondence` where `E/F` is finite dimensional and Galois ## Main results - `fixing_subgroup_of_fixed_field` : If `E/F` is finite dimensional (but not necessarily Galois) then `fixing_subgroup (fixed_field H) = H` - `fixed_field_of_fixing_subgroup`: If `E/F` is finite dimensional and Galois then `fixed_field (fixing_subgroup K) = K` Together, these two result prove the Galois correspondence - `is_galois.tfae` : Equivalent characterizations of a Galois extension of finite degree -/ noncomputable theory open_locale classical open finite_dimensional alg_equiv section variables (F : Type*) [field F] (E : Type*) [field E] [algebra F E] /-- A field extension E/F is galois if it is both separable and normal -/ @[class] def is_galois : Prop := is_separable F E ∧ normal F E namespace is_galois instance self : is_galois F F := ⟨is_separable_self F, normal_self F⟩ @[priority 100] -- see Note [lower instance priority] instance to_is_separable [h : is_galois F E] : is_separable F E := h.1 @[priority 100] -- see Note [lower instance priority] instance to_normal [h : is_galois F E] : normal F E := h.2 variables {F} {E} lemma integral (h : is_galois F E) (x : E) : is_integral F x := Exists.cases_on (h.1 x) (λ H _, H) lemma separable (h : is_galois F E) (x : E) : (minpoly (integral h x)).separable := Exists.cases_on (h.1 x) (λ _ H, H) lemma normal (h : is_galois F E) (x : E) : (minpoly (integral h x)).splits (algebra_map F E) := Exists.cases_on (h.2 x) (λ _ H, H) variables (F) (E) instance of_fixed_field (G : Type*) [group G] [fintype G] [mul_semiring_action G E] : is_galois (mul_action.fixed_points G E) E := ⟨fixed_points.separable G E, fixed_points.normal G E⟩ lemma intermediate_field.adjoin_simple.card_aut_eq_findim [finite_dimensional F E] {α : E} (hα : is_integral F α) (h_sep : (minpoly hα).separable) (h_splits : (minpoly hα).splits (algebra_map F F⟮α⟯)) : fintype.card (F⟮α⟯ ≃ₐ[F] F⟮α⟯) = findim F F⟮α⟯ := begin letI : fintype (F⟮α⟯ →ₐ[F] F⟮α⟯) := intermediate_field.fintype_of_alg_hom_adjoin_integral F hα, rw intermediate_field.adjoin.findim hα, rw ← intermediate_field.card_alg_hom_adjoin_integral F hα h_sep h_splits, exact fintype.card_congr (alg_equiv_equiv_alg_hom F F⟮α⟯) end lemma card_aut_eq_findim [finite_dimensional F E] [h : is_galois F E] : fintype.card (E ≃ₐ[F] E) = findim F E := begin cases field.exists_primitive_element h.1 with α hα, let iso : F⟮α⟯ ≃ₐ[F] E := { to_fun := λ e, e.val, inv_fun := λ e, ⟨e, by { rw hα, exact intermediate_field.mem_top }⟩, left_inv := λ _, by { ext, refl }, right_inv := λ _, rfl, map_mul' := λ _ _, rfl, map_add' := λ _ _, rfl, commutes' := λ _, rfl }, have H : is_integral F α := h.integral α, have h_sep : (minpoly H).separable := h.separable α, have h_splits : (minpoly H).splits (algebra_map F E) := h.normal α, replace h_splits : polynomial.splits (algebra_map F F⟮α⟯) (minpoly H), { convert polynomial.splits_comp_of_splits (algebra_map F E) iso.symm.to_alg_hom.to_ring_hom h_splits }, rw ← linear_equiv.findim_eq iso.to_linear_equiv, rw ← intermediate_field.adjoin_simple.card_aut_eq_findim F E H h_sep h_splits, apply fintype.card_congr, apply equiv.mk (λ ϕ, iso.trans (trans ϕ iso.symm)) (λ ϕ, iso.symm.trans (trans ϕ iso)), { intro ϕ, ext1, simp only [trans_apply, apply_symm_apply] }, { intro ϕ, ext1, simp only [trans_apply, symm_apply_apply] }, end end is_galois end section is_galois_tower variables (F K E : Type*) [field F] [field K] [field E] {E' : Type*} [field E'] [algebra F E'] variables [algebra F K] [algebra F E] [algebra K E] [is_scalar_tower F K E] lemma is_galois.tower_top_of_is_galois [h : is_galois F E] : is_galois K E := ⟨is_separable_tower_top_of_is_separable K h.1, normal.tower_top_of_normal F K E h.2⟩ variables {F E} @[priority 100] -- see Note [lower instance priority] instance is_galois.tower_top_intermediate_field (K : intermediate_field F E) [h : is_galois F E] : is_galois K E := is_galois.tower_top_of_is_galois F K E lemma is_galois_iff_is_galois_bot : is_galois (⊥ : intermediate_field F E) E ↔ is_galois F E := begin split, { introI h, exact is_galois.tower_top_of_is_galois (⊥ : intermediate_field F E) F E }, { introI h, apply_instance }, end lemma is_galois.of_alg_equiv [h : is_galois F E] (f : E ≃ₐ[F] E') : is_galois F E' := ⟨h.1.of_alg_hom f.symm, normal.of_alg_equiv f⟩ lemma alg_equiv.transfer_galois (f : E ≃ₐ[F] E') : is_galois F E ↔ is_galois F E' := ⟨λ h, by exactI is_galois.of_alg_equiv f, λ h, by exactI is_galois.of_alg_equiv f.symm⟩ lemma is_galois_iff_is_galois_top : is_galois F (⊤ : intermediate_field F E) ↔ is_galois F E := (intermediate_field.top_equiv).transfer_galois instance is_galois_bot : is_galois F (⊥ : intermediate_field F E) := intermediate_field.bot_equiv.transfer_galois.mpr (is_galois.self F) end is_galois_tower section galois_correspondence variables {F : Type*} [field F] {E : Type*} [field E] [algebra F E] variables (H : subgroup (E ≃ₐ[F] E)) (K : intermediate_field F E) namespace intermediate_field instance subgroup_action : faithful_mul_semiring_action H E := { smul := λ h x, h x, smul_zero := λ _, map_zero _, smul_add := λ _, map_add _, one_smul := λ _, rfl, smul_one := λ _, map_one _, mul_smul := λ _ _ _, rfl, smul_mul := λ _, map_mul _, eq_of_smul_eq_smul' := λ x y z, subtype.ext (alg_equiv.ext z) } /-- The intermediate_field fixed by a subgroup -/ def fixed_field : intermediate_field F E := { carrier := mul_action.fixed_points H E, zero_mem' := λ g, smul_zero g, add_mem' := λ a b hx hy g, by rw [smul_add g a b, hx, hy], neg_mem' := λ a hx g, by rw [smul_neg g a, hx], one_mem' := λ g, smul_one g, mul_mem' := λ a b hx hy g, by rw [smul_mul' g a b, hx, hy], inv_mem' := λ a hx g, by rw [smul_inv _ g a, hx], algebra_map_mem' := λ a g, commutes g a } lemma findim_fixed_field_eq_card [finite_dimensional F E] : findim (fixed_field H) E = fintype.card H := fixed_points.findim_eq_card H E /-- The subgroup fixing an intermediate_field -/ def fixing_subgroup : subgroup (E ≃ₐ[F] E) := { carrier := λ ϕ, ∀ x : K, ϕ x = x, one_mem' := λ _, rfl, mul_mem' := λ _ _ hx hy _, (congr_arg _ (hy _)).trans (hx _), inv_mem' := λ _ hx _, (equiv.symm_apply_eq (to_equiv _)).mpr (hx _).symm } lemma le_iff_le : K ≤ fixed_field H ↔ H ≤ fixing_subgroup K := ⟨λ h g hg x, h (subtype.mem x) ⟨g, hg⟩, λ h x hx g, h (subtype.mem g) ⟨x, hx⟩⟩ /-- The fixing_subgroup of `K : intermediate_field F E` is isomorphic to `E ≃ₐ[K] E` -/ def fixing_subgroup_equiv : fixing_subgroup K ≃* (E ≃ₐ[K] E) := { to_fun := λ ϕ, of_bijective (alg_hom.mk ϕ (map_one ϕ) (map_mul ϕ) (map_zero ϕ) (map_add ϕ) (ϕ.mem)) (bijective ϕ), inv_fun := λ ϕ, ⟨of_bijective (alg_hom.mk ϕ (ϕ.map_one) (ϕ.map_mul) (ϕ.map_zero) (ϕ.map_add) (λ r, ϕ.commutes (algebra_map F K r))) (ϕ.bijective), ϕ.commutes⟩, left_inv := λ _, by { ext, refl }, right_inv := λ _, by { ext, refl }, map_mul' := λ _ _, by { ext, refl } } theorem fixing_subgroup_fixed_field [finite_dimensional F E] : fixing_subgroup (fixed_field H) = H := begin have H_le : H ≤ (fixing_subgroup (fixed_field H)) := (le_iff_le _ _).mp (le_refl _), suffices : fintype.card H = fintype.card (fixing_subgroup (fixed_field H)), { exact subgroup.ext' (set.eq_of_inclusion_surjective ((fintype.bijective_iff_injective_and_card (set.inclusion H_le)).mpr ⟨set.inclusion_injective H_le, this⟩).2).symm }, apply fintype.card_congr, refine (fixed_points.to_alg_hom_equiv H E).trans _, refine (alg_equiv_equiv_alg_hom (fixed_field H) E).symm.trans _, exact (fixing_subgroup_equiv (fixed_field H)).to_equiv.symm end instance fixed_field.algebra : algebra K (fixed_field (fixing_subgroup K)) := { smul := λ x y, ⟨x*y, λ ϕ, by rw [smul_mul', (show ϕ • ↑x = ↑x, by exact subtype.mem ϕ x), (show ϕ • ↑y = ↑y, by exact subtype.mem y ϕ)]⟩, to_fun := λ x, ⟨x, λ ϕ, subtype.mem ϕ x⟩, map_zero' := rfl, map_add' := λ _ _, rfl, map_one' := rfl, map_mul' := λ _ _, rfl, commutes' := λ _ _, mul_comm _ _, smul_def' := λ _ _, rfl } instance fixed_field.is_scalar_tower : is_scalar_tower K (fixed_field (fixing_subgroup K)) E := ⟨λ _ _ _, mul_assoc _ _ _⟩ end intermediate_field namespace is_galois theorem fixed_field_fixing_subgroup [finite_dimensional F E] [h : is_galois F E] : intermediate_field.fixed_field (intermediate_field.fixing_subgroup K) = K := begin have K_le : K ≤ intermediate_field.fixed_field (intermediate_field.fixing_subgroup K) := (intermediate_field.le_iff_le _ _).mpr (le_refl _), suffices : findim K E = findim (intermediate_field.fixed_field (intermediate_field.fixing_subgroup K)) E, { exact (intermediate_field.eq_of_le_of_findim_eq' K_le this).symm }, rw [intermediate_field.findim_fixed_field_eq_card, fintype.card_congr (intermediate_field.fixing_subgroup_equiv K).to_equiv], exact (card_aut_eq_findim K E).symm, end lemma card_fixing_subgroup_eq_findim [finite_dimensional F E] [is_galois F E] : fintype.card (intermediate_field.fixing_subgroup K) = findim K E := by conv { to_rhs, rw [←fixed_field_fixing_subgroup K, intermediate_field.findim_fixed_field_eq_card] } /-- The Galois correspondence from intermediate fields to subgroups -/ def intermediate_field_equiv_subgroup [finite_dimensional F E] [is_galois F E] : intermediate_field F E ≃o order_dual (subgroup (E ≃ₐ[F] E)) := { to_fun := intermediate_field.fixing_subgroup, inv_fun := intermediate_field.fixed_field, left_inv := λ K, fixed_field_fixing_subgroup K, right_inv := λ H, intermediate_field.fixing_subgroup_fixed_field H, map_rel_iff' := λ K L, by { rw [←fixed_field_fixing_subgroup L, intermediate_field.le_iff_le, fixed_field_fixing_subgroup L, ←order_dual.dual_le], refl } } /-- The Galois correspondence as a galois_insertion -/ def galois_insertion_intermediate_field_subgroup [finite_dimensional F E] : galois_insertion (order_dual.to_dual ∘ (intermediate_field.fixing_subgroup : intermediate_field F E → subgroup (E ≃ₐ[F] E))) ((intermediate_field.fixed_field : subgroup (E ≃ₐ[F] E) → intermediate_field F E) ∘ order_dual.to_dual) := { choice := λ K _, intermediate_field.fixing_subgroup K, gc := λ K H, (intermediate_field.le_iff_le H K).symm, le_l_u := λ H, le_of_eq (intermediate_field.fixing_subgroup_fixed_field H).symm, choice_eq := λ K _, rfl } /-- The Galois correspondence as a galois_coinsertion -/ def galois_coinsertion_intermediate_field_subgroup [finite_dimensional F E] [is_galois F E] : galois_coinsertion (order_dual.to_dual ∘ (intermediate_field.fixing_subgroup : intermediate_field F E → subgroup (E ≃ₐ[F] E))) ((intermediate_field.fixed_field : subgroup (E ≃ₐ[F] E) → intermediate_field F E) ∘ order_dual.to_dual) := { choice := λ H _, intermediate_field.fixed_field H, gc := λ K H, (intermediate_field.le_iff_le H K).symm, u_l_le := λ K, le_of_eq (fixed_field_fixing_subgroup K), choice_eq := λ H _, rfl } end is_galois end galois_correspondence section galois_equivalent_definitions variables (F : Type*) [field F] (E : Type*) [field E] [algebra F E] namespace is_galois lemma is_separable_splitting_field [finite_dimensional F E] [h : is_galois F E] : ∃ p : polynomial F, p.separable ∧ p.is_splitting_field F E := begin cases field.exists_primitive_element h.1 with α h1, have h2 : is_integral F α := h.integral α, have h3 : (minpoly h2).separable := h.separable α, have h4 : (minpoly h2).splits (algebra_map F E) := h.normal α, use [minpoly h2, h3, h4], rw [eq_top_iff, ←intermediate_field.top_to_subalgebra, ←h1], rw intermediate_field.adjoin_simple_to_subalgebra_of_integral F α h2, apply algebra.adjoin_mono, rw [set.singleton_subset_iff, finset.mem_coe, multiset.mem_to_finset, polynomial.mem_roots], { dsimp only [polynomial.is_root], rw [polynomial.eval_map, ←polynomial.aeval_def], exact minpoly.aeval h2 }, { exact polynomial.map_ne_zero (minpoly.ne_zero h2) } end lemma of_fixed_field_eq_bot [finite_dimensional F E] (h : intermediate_field.fixed_field (⊤ : subgroup (E ≃ₐ[F] E)) = ⊥) : is_galois F E := begin rw [←is_galois_iff_is_galois_bot, ←h], exact is_galois.of_fixed_field E (⊤ : subgroup (E ≃ₐ[F] E)), end lemma of_card_aut_eq_findim [finite_dimensional F E] (h : fintype.card (E ≃ₐ[F] E) = findim F E) : is_galois F E := begin apply of_fixed_field_eq_bot, have p : 0 < findim (intermediate_field.fixed_field (⊤ : subgroup (E ≃ₐ[F] E))) E := findim_pos, rw [←intermediate_field.findim_eq_one_iff, ←mul_left_inj' (ne_of_lt p).symm, findim_mul_findim, ←h, one_mul, intermediate_field.findim_fixed_field_eq_card], exact fintype.card_congr { to_fun := λ g, ⟨g, subgroup.mem_top g⟩, inv_fun := coe, left_inv := λ g, rfl, right_inv := λ _, by { ext, refl } }, end variables {F} {E} {p : polynomial F} lemma of_separable_splitting_field_aux [hFE : finite_dimensional F E] [sp : p.is_splitting_field F E] (hp : p.separable) (K : intermediate_field F E) {x : E} (hx : x ∈ (p.map (algebra_map F E)).roots) : fintype.card ((↑K⟮x⟯ : intermediate_field F E) →ₐ[F] E) = fintype.card (K →ₐ[F] E) * findim K K⟮x⟯ := begin have h : is_integral K x := is_integral_of_is_scalar_tower x (is_integral_of_noetherian hFE x), have h1 : p ≠ 0 := λ hp, by rwa [hp, polynomial.map_zero, polynomial.roots_zero] at hx, have h2 : (minpoly h) ∣ p.map (algebra_map F K), { apply minpoly.dvd, rw [polynomial.aeval_def, polynomial.eval₂_map, ←polynomial.eval_map], exact (polynomial.mem_roots (polynomial.map_ne_zero h1)).mp hx }, let key_equiv : ((↑K⟮x⟯ : intermediate_field F E) →ₐ[F] E) ≃ Σ (f : K →ₐ[F] E), @alg_hom K K⟮x⟯ E _ _ _ _ (ring_hom.to_algebra f) := equiv.trans (alg_equiv.arrow_congr (intermediate_field.lift2_alg_equiv K⟮x⟯) (alg_equiv.refl)) alg_hom_equiv_sigma, haveI : Π (f : K →ₐ[F] E), fintype (@alg_hom K K⟮x⟯ E _ _ _ _ (ring_hom.to_algebra f)) := λ f, by { apply fintype.of_injective (sigma.mk f) (λ _ _ H, eq_of_heq ((sigma.mk.inj H).2)), exact fintype.of_equiv _ key_equiv }, rw [fintype.card_congr key_equiv, fintype.card_sigma, intermediate_field.adjoin.findim h], apply finset.sum_const_nat, intros f hf, rw ← @intermediate_field.card_alg_hom_adjoin_integral K _ E _ _ x E _ (ring_hom.to_algebra f) h, { apply fintype.card_congr, refl }, { exact polynomial.separable.of_dvd ((polynomial.separable_map (algebra_map F K)).mpr hp) h2 }, { refine polynomial.splits_of_splits_of_dvd _ (polynomial.map_ne_zero h1) _ h2, rw [polynomial.splits_map_iff, ←is_scalar_tower.algebra_map_eq], exact sp.splits }, end lemma of_separable_splitting_field [sp : p.is_splitting_field F E] (hp : p.separable) : is_galois F E := begin haveI hFE : finite_dimensional F E := polynomial.is_splitting_field.finite_dimensional E p, let s := (p.map (algebra_map F E)).roots.to_finset, have adjoin_root := intermediate_field.ext (subalgebra.ext_iff.mp (eq.trans (top_le_iff.mp (eq.trans_le sp.adjoin_roots.symm (intermediate_field.algebra_adjoin_le_adjoin F ↑s))) intermediate_field.top_to_subalgebra.symm)), let P : intermediate_field F E → Prop := λ K, fintype.card (K →ₐ[F] E) = findim F K, suffices : P (intermediate_field.adjoin F ↑s), { rw adjoin_root at this, apply of_card_aut_eq_findim, rw ← eq.trans this (linear_equiv.findim_eq intermediate_field.top_equiv.to_linear_equiv), exact fintype.card_congr (equiv.trans (alg_equiv_equiv_alg_hom F E) (alg_equiv.arrow_congr intermediate_field.top_equiv.symm alg_equiv.refl)) }, apply intermediate_field.induction_on_adjoin_finset s P, { have key := intermediate_field.card_alg_hom_adjoin_integral F (show is_integral F (0 : E), by exact is_integral_zero), rw [minpoly.zero, polynomial.nat_degree_X] at key, specialize key polynomial.separable_X (polynomial.splits_X (algebra_map F E)), rw [←@subalgebra.findim_bot F E _ _ _, ←intermediate_field.bot_to_subalgebra] at key, refine eq.trans _ key, apply fintype.card_congr, rw intermediate_field.adjoin_zero }, intros K x hx hK, simp only [P] at *, rw [of_separable_splitting_field_aux hp K (multiset.mem_to_finset.mp hx), hK, findim_mul_findim], exact (linear_equiv.findim_eq (intermediate_field.lift2_alg_equiv K⟮x⟯).to_linear_equiv).symm, end /--Equivalent characterizations of a Galois extension of finite degree-/ theorem tfae [finite_dimensional F E] : tfae [is_galois F E, intermediate_field.fixed_field (⊤ : subgroup (E ≃ₐ[F] E)) = ⊥, fintype.card (E ≃ₐ[F] E) = findim F E, ∃ p : polynomial F, p.separable ∧ p.is_splitting_field F E] := begin tfae_have : 1 → 2, { exact λ h, order_iso.map_bot (@intermediate_field_equiv_subgroup F _ E _ _ _ h).symm }, tfae_have : 1 → 3, { introI _, exact card_aut_eq_findim F E }, tfae_have : 1 → 4, { introI _, exact is_separable_splitting_field F E }, tfae_have : 2 → 1, { exact of_fixed_field_eq_bot F E }, tfae_have : 3 → 1, { exact of_card_aut_eq_findim F E }, tfae_have : 4 → 1, { rintros ⟨h, hp1, _⟩, exactI of_separable_splitting_field hp1 }, tfae_finish, end end is_galois end galois_equivalent_definitions
ecbd9a4edca084648064dae43d6e2876fad1a66f
31f556cdeb9239ffc2fad8f905e33987ff4feab9
/src/Lean/Modifiers.lean
710cc3d65efa46ee0afb0aeb57f805b3dd612e94
[ "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
tobiasgrosser/lean4
ce0fd9cca0feba1100656679bf41f0bffdbabb71
ebdbdc10436a4d9d6b66acf78aae7a23f5bd073f
refs/heads/master
1,673,103,412,948
1,664,930,501,000
1,664,930,501,000
186,870,185
0
0
Apache-2.0
1,665,129,237,000
1,557,939,901,000
Lean
UTF-8
Lean
false
false
2,444
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Environment namespace Lean builtin_initialize protectedExt : TagDeclarationExtension ← mkTagDeclarationExtension `protected @[export lean_add_protected] def addProtected (env : Environment) (n : Name) : Environment := protectedExt.tag env n @[export lean_is_protected] def isProtected (env : Environment) (n : Name) : Bool := protectedExt.isTagged env n /-! # Private name support. Suppose the user marks as declaration `n` as private. Then, we create the name: `_private.<module_name>.0 ++ n`. We say `_private.<module_name>.0` is the "private prefix" We assume that `n` is a valid user name and does not contain `Name.num` constructors. Thus, we can easily convert from private internal name to the user given name. -/ def privateHeader : Name := `_private def mkPrivateName (env : Environment) (n : Name) : Name := Name.mkNum (privateHeader ++ env.mainModule) 0 ++ n def isPrivateName : Name → Bool | n@(.str p _) => n == privateHeader || isPrivateName p | .num p _ => isPrivateName p | _ => false @[export lean_is_private_name] def isPrivateNameExport (n : Name) : Bool := isPrivateName n /-- Return `true` if `n` is of the form `_private.<module_name>.0` See comment above. -/ private def isPrivatePrefix (n : Name) : Bool := match n with | .num p 0 => go p | _ => false where go (n : Name) : Bool := n == privateHeader || match n with | .str p _ => go p | _ => false private def privateToUserNameAux (n : Name) : Name := match n with | .str p s => .str (privateToUserNameAux p) s | .num p i => if isPrivatePrefix n then .anonymous else .num (privateToUserNameAux p) i | _ => .anonymous @[export lean_private_to_user_name] def privateToUserName? (n : Name) : Option Name := if isPrivateName n then privateToUserNameAux n else none def isPrivateNameFromImportedModule (env : Environment) (n : Name) : Bool := match privateToUserName? n with | some userName => mkPrivateName env userName != n | _ => false private def privatePrefixAux : Name → Name | .str p _ => privatePrefixAux p | n => n @[export lean_private_prefix] def privatePrefix? (n : Name) : Option Name := if isPrivateName n then privatePrefixAux n else none end Lean
77cadfe673a51c017eff699aabfef59a7692ae6f
80746c6dba6a866de5431094bf9f8f841b043d77
/src/algebra/euclidean_domain.lean
39acc4ee5dd2782673b72effc9a78369c85ca02b
[ "Apache-2.0" ]
permissive
leanprover-fork/mathlib-backup
8b5c95c535b148fca858f7e8db75a76252e32987
0eb9db6a1a8a605f0cf9e33873d0450f9f0ae9b0
refs/heads/master
1,585,156,056,139
1,548,864,430,000
1,548,864,438,000
143,964,213
0
0
Apache-2.0
1,550,795,966,000
1,533,705,322,000
Lean
UTF-8
Lean
false
false
9,504
lean
/- Copyright (c) 2018 Louis Carlin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Louis Carlin, Mario Carneiro Euclidean domains and Euclidean algorithm (extended to come) A lot is based on pre-existing code in mathlib for natural number gcds -/ import data.int.basic universe u class euclidean_domain (α : Type u) extends nonzero_comm_ring α := (quotient : α → α → α) (remainder : α → α → α) -- This could be changed to the same order as int.mod_add_div. -- We normally write qb+r rather than r + qb though. (quotient_mul_add_remainder_eq : ∀ a b, b * quotient a b + remainder a b = a) (r : α → α → Prop) (r_well_founded : well_founded r) (remainder_lt : ∀ a {b}, b ≠ 0 → r (remainder a b) b) /- `val_le_mul_left` is often not a required in definitions of a euclidean domain since given the other properties we can show there is a (noncomputable) euclidean domain α with the property `val_le_mul_left`. So potentially this definition could be split into two different ones (euclidean_domain_weak and euclidean_domain_strong) with a noncomputable function from weak to strong. I've currently divided the lemmas into strong and weak depending on whether they require `val_le_mul_left` or not. -/ (mul_left_not_lt : ∀ a {b}, b ≠ 0 → ¬r (a * b) a) namespace euclidean_domain variable {α : Type u} variables [euclidean_domain α] local infix ` ≺ `:50 := euclidean_domain.r instance : has_div α := ⟨quotient⟩ instance : has_mod α := ⟨remainder⟩ theorem div_add_mod (a b : α) : b * (a / b) + a % b = a := quotient_mul_add_remainder_eq _ _ lemma mod_eq_sub_mul_div {α : Type*} [euclidean_domain α] (a b : α) : a % b = a - b * (a / b) := calc a % b = b * (a / b) + a % b - b * (a / b) : (add_sub_cancel' _ _).symm ... = a - b * (a / b) : by rw div_add_mod theorem mod_lt : ∀ a {b : α}, b ≠ 0 → (a % b) ≺ b := remainder_lt theorem mul_right_not_lt {a : α} (b) (h : a ≠ 0) : ¬(a * b) ≺ b := by rw mul_comm; exact mul_left_not_lt b h lemma mul_div_cancel_left {a : α} (b) (a0 : a ≠ 0) : a * b / a = b := eq.symm $ eq_of_sub_eq_zero $ classical.by_contradiction $ λ h, begin have := mul_left_not_lt a h, rw [mul_sub, sub_eq_iff_eq_add'.2 (div_add_mod (a*b) a).symm] at this, exact this (mod_lt _ a0) end lemma mul_div_cancel (a) {b : α} (b0 : b ≠ 0) : a * b / b = a := by rw mul_comm; exact mul_div_cancel_left a b0 @[simp] lemma mod_zero (a : α) : a % 0 = a := by simpa only [zero_mul, zero_add] using div_add_mod a 0 @[simp] lemma mod_eq_zero {a b : α} : a % b = 0 ↔ b ∣ a := ⟨λ h, by rw [← div_add_mod a b, h, add_zero]; exact dvd_mul_right _ _, λ ⟨c, e⟩, begin rw [e, ← add_left_cancel_iff, div_add_mod, add_zero], haveI := classical.dec, by_cases b0 : b = 0, { simp only [b0, zero_mul] }, { rw [mul_div_cancel_left _ b0] } end⟩ @[simp] lemma mod_self (a : α) : a % a = 0 := mod_eq_zero.2 (dvd_refl _) lemma dvd_mod_iff {a b c : α} (h : c ∣ b) : c ∣ a % b ↔ c ∣ a := by rw [dvd_add_iff_right (dvd_mul_of_dvd_left h _), div_add_mod] lemma lt_one (a : α) : a ≺ (1:α) → a = 0 := by haveI := classical.dec; exact not_imp_not.1 (λ h, by simpa only [one_mul] using mul_left_not_lt 1 h) lemma val_dvd_le : ∀ a b : α, b ∣ a → a ≠ 0 → ¬a ≺ b | _ b ⟨d, rfl⟩ ha := mul_left_not_lt b (mt (by rintro rfl; exact mul_zero _) ha) @[simp] lemma mod_one (a : α) : a % 1 = 0 := mod_eq_zero.2 (one_dvd _) @[simp] lemma zero_mod (b : α) : 0 % b = 0 := mod_eq_zero.2 (dvd_zero _) @[simp] lemma zero_div {a : α} (a0 : a ≠ 0) : 0 / a = 0 := by simpa only [zero_mul] using mul_div_cancel 0 a0 @[simp] lemma div_self {a : α} (a0 : a ≠ 0) : a / a = 1 := by simpa only [one_mul] using mul_div_cancel 1 a0 lemma eq_div_of_mul_eq_left {a b c : α} (hb : b ≠ 0) (h : a * b = c) : a = c / b := by rw [← h, mul_div_cancel _ hb] lemma eq_div_of_mul_eq_right {a b c : α} (ha : a ≠ 0) (h : a * b = c) : b = c / a := by rw [← h, mul_div_cancel_left _ ha] section gcd variable [decidable_eq α] def gcd : α → α → α | a := λ b, if a0 : a = 0 then b else have h:_ := mod_lt b a0, gcd (b%a) a using_well_founded {dec_tac := tactic.assumption, rel_tac := λ _ _, `[exact ⟨_, r_well_founded α⟩]} @[simp] theorem gcd_zero_left (a : α) : gcd 0 a = a := by rw gcd; exact if_pos rfl @[simp] theorem gcd_zero_right (a : α) : gcd a 0 = a := by rw gcd; split_ifs; simp only [h, zero_mod, gcd_zero_left] theorem gcd_val (a b : α) : gcd a b = gcd (b % a) a := by rw gcd; split_ifs; [simp only [h, mod_zero, gcd_zero_right], refl] @[elab_as_eliminator] theorem gcd.induction {P : α → α → Prop} : ∀ a b : α, (∀ x, P 0 x) → (∀ a b, a ≠ 0 → P (b % a) a → P a b) → P a b | a := λ b H0 H1, if a0 : a = 0 then by rw [a0]; apply H0 else have h:_ := mod_lt b a0, H1 _ _ a0 (gcd.induction (b%a) a H0 H1) using_well_founded {dec_tac := tactic.assumption, rel_tac := λ _ _, `[exact ⟨_, r_well_founded α⟩]} theorem gcd_dvd (a b : α) : gcd a b ∣ a ∧ gcd a b ∣ b := gcd.induction a b (λ b, by rw [gcd_zero_left]; exact ⟨dvd_zero _, dvd_refl _⟩) (λ a b aneq ⟨IH₁, IH₂⟩, by rw gcd_val; exact ⟨IH₂, (dvd_mod_iff IH₂).1 IH₁⟩) theorem gcd_dvd_left (a b : α) : gcd a b ∣ a := (gcd_dvd a b).left theorem gcd_dvd_right (a b : α) : gcd a b ∣ b := (gcd_dvd a b).right protected theorem gcd_eq_zero_iff {a b : α} : gcd a b = 0 ↔ a = 0 ∧ b = 0 := ⟨λ h, by simpa [h] using gcd_dvd a b, by rintro ⟨rfl, rfl⟩; simp⟩ theorem dvd_gcd {a b c : α} : c ∣ a → c ∣ b → c ∣ gcd a b := gcd.induction a b (λ _ _ H, by simpa only [gcd_zero_left] using H) (λ a b a0 IH ca cb, by rw gcd_val; exact IH ((dvd_mod_iff ca).2 cb) ca) theorem gcd_eq_left {a b : α} : gcd a b = a ↔ a ∣ b := ⟨λ h, by rw ← h; apply gcd_dvd_right, λ h, by rw [gcd_val, mod_eq_zero.2 h, gcd_zero_left]⟩ @[simp] theorem gcd_one_left (a : α) : gcd 1 a = 1 := gcd_eq_left.2 (one_dvd _) @[simp] theorem gcd_self (a : α) : gcd a a = a := gcd_eq_left.2 (dvd_refl _) def xgcd_aux : α → α → α → α → α → α → α × α × α | r := λ s t r' s' t', if hr : r = 0 then (r', s', t') else have r' % r ≺ r, from mod_lt _ hr, let q := r' / r in xgcd_aux (r' % r) (s' - q * s) (t' - q * t) r s t using_well_founded {dec_tac := tactic.assumption, rel_tac := λ _ _, `[exact ⟨_, r_well_founded α⟩]} @[simp] theorem xgcd_zero_left {s t r' s' t' : α} : xgcd_aux 0 s t r' s' t' = (r', s', t') := by unfold xgcd_aux; exact if_pos rfl @[simp] theorem xgcd_aux_rec {r s t r' s' t' : α} (h : r ≠ 0) : xgcd_aux r s t r' s' t' = xgcd_aux (r' % r) (s' - (r' / r) * s) (t' - (r' / r) * t) r s t := by conv {to_lhs, rw [xgcd_aux]}; exact if_neg h /-- Use the extended GCD algorithm to generate the `a` and `b` values satisfying `gcd x y = x * a + y * b`. -/ def xgcd (x y : α) : α × α := (xgcd_aux x 1 0 y 0 1).2 /-- The extended GCD `a` value in the equation `gcd x y = x * a + y * b`. -/ def gcd_a (x y : α) : α := (xgcd x y).1 /-- The extended GCD `b` value in the equation `gcd x y = x * a + y * b`. -/ def gcd_b (x y : α) : α := (xgcd x y).2 @[simp] theorem xgcd_aux_fst (x y : α) : ∀ s t s' t', (xgcd_aux x s t y s' t').1 = gcd x y := gcd.induction x y (by intros; rw [xgcd_zero_left, gcd_zero_left]) (λ x y h IH s t s' t', by simp only [xgcd_aux_rec h, if_neg h, IH]; rw ← gcd_val) theorem xgcd_aux_val (x y : α) : xgcd_aux x 1 0 y 0 1 = (gcd x y, xgcd x y) := by rw [xgcd, ← xgcd_aux_fst x y 1 0 0 1, prod.mk.eta] theorem xgcd_val (x y : α) : xgcd x y = (gcd_a x y, gcd_b x y) := prod.mk.eta.symm private def P (a b : α) : α × α × α → Prop | (r, s, t) := (r : α) = a * s + b * t theorem xgcd_aux_P (a b : α) {r r' : α} : ∀ {s t s' t'}, P a b (r, s, t) → P a b (r', s', t') → P a b (xgcd_aux r s t r' s' t') := gcd.induction r r' (by intros; simpa only [xgcd_zero_left]) $ λ x y h IH s t s' t' p p', begin rw [xgcd_aux_rec h], refine IH _ p, unfold P at p p' ⊢, rw [mul_sub, mul_sub, add_sub, sub_add_eq_add_sub, ← p', sub_sub, mul_comm _ s, ← mul_assoc, mul_comm _ t, ← mul_assoc, ← add_mul, ← p, mod_eq_sub_mul_div] end theorem gcd_eq_gcd_ab (a b : α) : (gcd a b : α) = a * gcd_a a b + b * gcd_b a b := by have := @xgcd_aux_P _ _ _ a b a b 1 0 0 1 (by rw [P, mul_one, mul_zero, add_zero]) (by rw [P, mul_one, mul_zero, zero_add]); rwa [xgcd_aux_val, xgcd_val] at this instance (α : Type*) [e : euclidean_domain α] : integral_domain α := by haveI := classical.dec_eq α; exact { eq_zero_or_eq_zero_of_mul_eq_zero := λ a b (h : a * b = 0), or_iff_not_and_not.2 $ λ h0 : a ≠ 0 ∧ b ≠ 0, h0.1 $ by rw [← mul_div_cancel a h0.2, h, zero_div h0.2], ..e } end gcd instance : euclidean_domain ℤ := { quotient := (/), remainder := (%), quotient_mul_add_remainder_eq := λ a b, by rw add_comm; exact int.mod_add_div _ _, r := λ a b, a.nat_abs < b.nat_abs, r_well_founded := measure_wf (λ a, int.nat_abs a), remainder_lt := λ a b b0, int.coe_nat_lt.1 $ by rw [int.nat_abs_of_nonneg (int.mod_nonneg _ b0), ← int.abs_eq_nat_abs]; exact int.mod_lt _ b0, mul_left_not_lt := λ a b b0, not_lt_of_ge $ by rw [← mul_one a.nat_abs, int.nat_abs_mul]; exact mul_le_mul_of_nonneg_left (int.nat_abs_pos_of_ne_zero b0) (nat.zero_le _) } end euclidean_domain
9f02d3af5e044ea6f7f37ac9a7f3e2b361da28a4
957a80ea22c5abb4f4670b250d55534d9db99108
/tests/lean/coe5.lean
050b751742cbf4856fa2cefedd880161683ec92f
[ "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
315
lean
open tactic attribute [instance] meta def expr_to_app : has_coe_to_fun expr := { F := λ e, expr → expr, coe := expr.app } meta constants f a b : expr #check f a #check f a b #check f a b a set_option pp.coercions false #check f a b a set_option pp.all true set_option pp.coercions true #check f a b
ede8b3ac1114f1c2f454c8c154bc7790d3ebf3c5
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/data/nat/sqrt.lean
efa81ca1e8bac375fe33e451e30a44b927a2d503
[ "Apache-2.0" ]
permissive
gebner/mathlib
eab0150cc4f79ec45d2016a8c21750244a2e7ff0
cc6a6edc397c55118df62831e23bfbd6e6c6b4ab
refs/heads/master
1,625,574,853,976
1,586,712,827,000
1,586,712,827,000
99,101,412
1
0
Apache-2.0
1,586,716,389,000
1,501,667,958,000
Lean
UTF-8
Lean
false
false
6,987
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, Johannes Hölzl, Mario Carneiro An efficient binary implementation of a (sqrt n) function that returns s s.t. s*s ≤ n ≤ s*s + s + s -/ import data.nat.basic algebra.ordered_group algebra.ring tactic.alias namespace nat theorem sqrt_aux_dec {b} (h : b ≠ 0) : shiftr b 2 < b := begin simp [shiftr_eq_div_pow], apply (nat.div_lt_iff_lt_mul' (dec_trivial : 0 < 4)).2, have := nat.mul_lt_mul_of_pos_left (dec_trivial : 1 < 4) (nat.pos_of_ne_zero h), rwa mul_one at this end def sqrt_aux : ℕ → ℕ → ℕ → ℕ | b r n := if b0 : b = 0 then r else let b' := shiftr b 2 in have b' < b, from sqrt_aux_dec b0, match (n - (r + b : ℕ) : ℤ) with | (n' : ℕ) := sqrt_aux b' (div2 r + b) n' | _ := sqrt_aux b' (div2 r) n end /-- `sqrt n` is the square root of a natural number `n`. If `n` is not a perfect square, it returns the largest `k:ℕ` such that `k*k ≤ n`. -/ def sqrt (n : ℕ) : ℕ := match size n with | 0 := 0 | succ s := sqrt_aux (shiftl 1 (bit0 (div2 s))) 0 n end theorem sqrt_aux_0 (r n) : sqrt_aux 0 r n = r := by rw sqrt_aux; simp local attribute [simp] sqrt_aux_0 theorem sqrt_aux_1 {r n b} (h : b ≠ 0) {n'} (h₂ : r + b + n' = n) : sqrt_aux b r n = sqrt_aux (shiftr b 2) (div2 r + b) n' := by rw sqrt_aux; simp only [h, h₂.symm, int.coe_nat_add, if_false]; rw [add_comm _ (n':ℤ), add_sub_cancel, sqrt_aux._match_1] theorem sqrt_aux_2 {r n b} (h : b ≠ 0) (h₂ : n < r + b) : sqrt_aux b r n = sqrt_aux (shiftr b 2) (div2 r) n := begin rw sqrt_aux; simp only [h, h₂, if_false], cases int.eq_neg_succ_of_lt_zero (sub_lt_zero.2 (int.coe_nat_lt_coe_nat_of_lt h₂)) with k e, rw [e, sqrt_aux._match_1] end private def is_sqrt (n q : ℕ) : Prop := q*q ≤ n ∧ n < (q+1)*(q+1) private lemma sqrt_aux_is_sqrt_lemma (m r n : ℕ) (h₁ : r*r ≤ n) (m') (hm : shiftr (2^m * 2^m) 2 = m') (H1 : n < (r + 2^m) * (r + 2^m) → is_sqrt n (sqrt_aux m' (r * 2^m) (n - r * r))) (H2 : (r + 2^m) * (r + 2^m) ≤ n → is_sqrt n (sqrt_aux m' ((r + 2^m) * 2^m) (n - (r + 2^m) * (r + 2^m)))) : is_sqrt n (sqrt_aux (2^m * 2^m) ((2*r)*2^m) (n - r*r)) := begin have b0 := have b0:_, from ne_of_gt (@pos_pow_of_pos 2 m dec_trivial), nat.mul_ne_zero b0 b0, have lb : n - r * r < 2 * r * 2^m + 2^m * 2^m ↔ n < (r+2^m)*(r+2^m), { rw [nat.sub_lt_right_iff_lt_add h₁], simp [left_distrib, right_distrib, two_mul, mul_comm, mul_assoc, add_comm, add_left_comm] }, have re : div2 (2 * r * 2^m) = r * 2^m, { rw [div2_val, mul_assoc, nat.mul_div_cancel_left _ (dec_trivial:2>0)] }, cases lt_or_ge n ((r+2^m)*(r+2^m)) with hl hl, { rw [sqrt_aux_2 b0 (lb.2 hl), hm, re], apply H1 hl }, { cases le.dest hl with n' e, rw [@sqrt_aux_1 (2 * r * 2^m) (n-r*r) (2^m * 2^m) b0 (n - (r + 2^m) * (r + 2^m)), hm, re, ← right_distrib], { apply H2 hl }, apply eq.symm, apply nat.sub_eq_of_eq_add, rw [← add_assoc, (_ : r*r + _ = _)], exact (nat.add_sub_cancel' hl).symm, simp [left_distrib, right_distrib, two_mul, mul_comm, mul_assoc] }, end private lemma sqrt_aux_is_sqrt (n) : ∀ m r, r*r ≤ n → n < (r + 2^(m+1)) * (r + 2^(m+1)) → is_sqrt n (sqrt_aux (2^m * 2^m) (2*r*2^m) (n - r*r)) | 0 r h₁ h₂ := by apply sqrt_aux_is_sqrt_lemma 0 r n h₁ 0 rfl; intros; simp; [exact ⟨h₁, a⟩, exact ⟨a, h₂⟩] | (m+1) r h₁ h₂ := begin apply sqrt_aux_is_sqrt_lemma (m+1) r n h₁ (2^m * 2^m) (by simp [shiftr, pow_succ, div2_val, mul_comm, mul_left_comm]; repeat {rw @nat.mul_div_cancel_left _ 2 dec_trivial}); intros, { have := sqrt_aux_is_sqrt m r h₁ a, simpa [pow_succ, mul_comm, mul_assoc] }, { rw [pow_succ, mul_two, ← add_assoc] at h₂, have := sqrt_aux_is_sqrt m (r + 2^(m+1)) a h₂, rwa show (r + 2^(m + 1)) * 2^(m+1) = 2 * (r + 2^(m + 1)) * 2^m, by simp [pow_succ, mul_comm, mul_left_comm] } end private lemma sqrt_is_sqrt (n : ℕ) : is_sqrt n (sqrt n) := begin generalize e : size n = s, cases s with s; simp [e, sqrt], { rw [size_eq_zero.1 e, is_sqrt], exact dec_trivial }, { have := sqrt_aux_is_sqrt n (div2 s) 0 (zero_le _), simp [show 2^div2 s * 2^div2 s = shiftl 1 (bit0 (div2 s)), by { generalize: div2 s = x, change bit0 x with x+x, rw [one_shiftl, pow_add] }] at this, apply this, rw [← pow_add, ← mul_two], apply size_le.1, rw e, apply (@div_lt_iff_lt_mul _ _ 2 dec_trivial).1, rw [div2_val], apply lt_succ_self } end theorem sqrt_le (n : ℕ) : sqrt n * sqrt n ≤ n := (sqrt_is_sqrt n).left theorem lt_succ_sqrt (n : ℕ) : n < succ (sqrt n) * succ (sqrt n) := (sqrt_is_sqrt n).right theorem sqrt_le_add (n : ℕ) : n ≤ sqrt n * sqrt n + sqrt n + sqrt n := by rw ← succ_mul; exact le_of_lt_succ (lt_succ_sqrt n) theorem le_sqrt {m n : ℕ} : m ≤ sqrt n ↔ m*m ≤ n := ⟨λ h, le_trans (mul_self_le_mul_self h) (sqrt_le n), λ h, le_of_lt_succ $ mul_self_lt_mul_self_iff.2 $ lt_of_le_of_lt h (lt_succ_sqrt n)⟩ theorem sqrt_lt {m n : ℕ} : sqrt m < n ↔ m < n*n := lt_iff_lt_of_le_iff_le le_sqrt theorem sqrt_le_self (n : ℕ) : sqrt n ≤ n := le_trans (le_mul_self _) (sqrt_le n) theorem sqrt_le_sqrt {m n : ℕ} (h : m ≤ n) : sqrt m ≤ sqrt n := le_sqrt.2 (le_trans (sqrt_le _) h) theorem sqrt_eq_zero {n : ℕ} : sqrt n = 0 ↔ n = 0 := ⟨λ h, eq_zero_of_le_zero $ le_of_lt_succ $ (@sqrt_lt n 1).1 $ by rw [h]; exact dec_trivial, λ e, e.symm ▸ rfl⟩ theorem eq_sqrt {n q} : q = sqrt n ↔ q*q ≤ n ∧ n < (q+1)*(q+1) := ⟨λ e, e.symm ▸ sqrt_is_sqrt n, λ ⟨h₁, h₂⟩, le_antisymm (le_sqrt.2 h₁) (le_of_lt_succ $ sqrt_lt.2 h₂)⟩ theorem le_three_of_sqrt_eq_one {n : ℕ} (h : sqrt n = 1) : n ≤ 3 := le_of_lt_succ $ (@sqrt_lt n 2).1 $ by rw [h]; exact dec_trivial theorem sqrt_lt_self {n : ℕ} (h : 1 < n) : sqrt n < n := sqrt_lt.2 $ by have := nat.mul_lt_mul_of_pos_left h (lt_of_succ_lt h); rwa [mul_one] at this theorem sqrt_pos {n : ℕ} : 0 < sqrt n ↔ 0 < n := le_sqrt theorem sqrt_add_eq (n : ℕ) {a : ℕ} (h : a ≤ n + n) : sqrt (n*n + a) = n := le_antisymm (le_of_lt_succ $ sqrt_lt.2 $ by rw [succ_mul, mul_succ, add_succ, add_assoc]; exact lt_succ_of_le (nat.add_le_add_left h _)) (le_sqrt.2 $ nat.le_add_right _ _) theorem sqrt_eq (n : ℕ) : sqrt (n*n) = n := sqrt_add_eq n (zero_le _) theorem sqrt_succ_le_succ_sqrt (n : ℕ) : sqrt n.succ ≤ n.sqrt.succ := le_of_lt_succ $ sqrt_lt.2 $ lt_succ_of_le $ succ_le_succ $ le_trans (sqrt_le_add n) $ add_le_add_right (by refine add_le_add (mul_le_mul_right _ _) _; exact le_add_right _ 2) _ theorem exists_mul_self (x : ℕ) : (∃ n, n * n = x) ↔ sqrt x * sqrt x = x := ⟨λ ⟨n, hn⟩, by rw [← hn, sqrt_eq], λ h, ⟨sqrt x, h⟩⟩ end nat
c588a27e536e0b68f0da2fd1b0442afb8c26382c
3dc4623269159d02a444fe898d33e8c7e7e9461b
/.github/workflows/group_objet.lean
bc865fff51c6c034da027e15db61a41cfed45527
[]
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
6,439
lean
import to_product import sugar_yoneda import category_theory.limits.limits import category_theory.limits.shapes universes v u open Product_stuff open Yoneda open category_theory open category_theory.limits open category_theory.category /- The goal is define group obj in a category. reference : Douady : Algebre et théories galoisiennes page 45 exemple : in the category of presheaf. in Ring ? Idem ? contexte : 𝒞 a un objet final et a les produit finis ! Pour coder μ X × X ⟶ X We see that has X ⟶ T cospan f f) -/ -- notation f ` ⊗ `:20 g :20 := category_theory.limits.prod.map f g -- notation `T`C :20 := (terminal C) -- notation `T`X : 20 := (terminal.from X) -- notation f ` | `:20 g :20 := prod.lift f g /-! # notations : # T C : C (objet terminal) # (f | g) : Z ⟶ X ⨯ Y # T X : X ⟶ T C # (f ⊗ g) : Z1 ⨯ Z2 ⟶ X1 ⨯ X2 !-/ structure group_obj (C : Type u)[ 𝒞 : category.{v} C ] [ (has_binary_products.{v} C) ] [ (has_terminal.{v} C) ] := (X : C) (μ : X ⨯ X ⟶ X) (inv : X ⟶ X) (ε : T C ⟶ X) (hyp_one_mul : (T X | 𝟙 X) ≫ (ε ⊗ 𝟙 X) ≫ μ = 𝟙 X) (hyp_mul_one : (𝟙 X | T X) ≫ ( 𝟙 X ⊗ ε) ≫ μ = 𝟙 X) (hyp_inv_mul : (inv | 𝟙 X) ≫ μ = (T X) ≫ ε ) (hyp_assoc : (μ ⊗ 𝟙 X) ≫ (μ) = (prod.associator X X X).hom ≫ (𝟙 X ⊗ μ) ≫ μ ) -- (a *b) * c = (a * (b * c)) variables (C : Type u) variables [𝒞 : category.{v} C] variables [has_binary_products.{v} C][has_terminal.{v} C] include 𝒞 instance coee : has_coe (group_obj C) C := ⟨λ F, F.X⟩ --- ? variables (G : group_obj C) include G -- we start by rewriting a little lemma mul_one' : (𝟙 G.X | T G.X) ≫ ( 𝟙 G.X ⊗ G.ε) ≫ G.μ = (𝟙 G.X | (T G.X) ≫ G.ε) ≫ G.μ := begin rw ← assoc, rw prod.prod_comp_otimes, rw comp_id, end lemma one_mul' : (T G.X | 𝟙 G.X) ≫ (G.ε ⊗ 𝟙 G.X) ≫ G.μ = ((T G.X) ≫ G.ε | 𝟙 G.X) ≫ G.μ := begin rw ← assoc, rw prod.prod_comp_otimes, rw comp_id, end lemma one_mul_R (R A : C) (ζ : R⟦G.X⟧ ): R < ((T G.X) ≫ G.ε | 𝟙 G.X) ≫ G.μ > ζ = ζ := begin rw ← one_mul', rw G.hyp_one_mul, rw Yoneda.id,exact rfl, end lemma mul_one_R (R A : C) (ζ : R⟦G.X⟧ ): R < ( 𝟙 G.X | (T G.X) ≫ G.ε ) ≫ G.μ > ζ = ζ := begin rw ← mul_one', rw G.hyp_mul_one,rw Yoneda.id, exact rfl, end def one (R : C) : R ⟦(G.X) ⟧ := begin ---- ici l'unité est R<ε> (T G.X) l'image du terminal exact (terminal.from R ≫ G.ε), end def mul (R : C) : R⟦ G.X⟧ → R⟦ G.X⟧ → R ⟦ G.X ⟧ := λ g1 g2, begin let φ := ( g1 | g2), let β := (R< (G.μ) > : R⟦ G.X ⨯ G.X⟧ ⟶ R⟦G.X⟧), exact β φ, end variables (R : C) include R instance yoneda_mul : has_mul (R⟦ G.X⟧) := ⟨mul C G R ⟩ instance yoneda_one : has_one (R⟦ G.X⟧) := ⟨one C G R ⟩ @[PRODUCT]lemma mul_comp (a b : R ⟦ G.X⟧ ) : a * b = (R < G.μ >) (a | b) := rfl -- priority R < g.μ > (a | b) not () @[PRODUCT]lemma one_comp : (1 : R ⟦ G.X ⟧) = terminal.from R ≫ G.ε := rfl notation Y `⟶•` := T Y @[PRODUCT]lemma Terminal_comp{Y : C} ( a : R ⟶ Y) : a ≫ (Y ⟶•) = (R ⟶•) := by exact subsingleton.elim (a ≫ T Y) (T R) lemma one_mulf' (ζ : R⟦G.X ⟧) : 1 * ζ = ζ := begin rw mul_comp,rw one_comp, --- (T X | 𝟙 X) ≫ (ε ⊗ 𝟙 X) ≫ μ = 𝟙 X) let V := one_mul_R C G R R ζ, rw [Yoneda.apply_to_composition, ← assoc,prod.left_composition,comp_id ,← assoc,Terminal_comp,Yoneda.composition_to_apply] at V, exact V, use G, end lemma mul_onef'(ζ : R⟦G.X ⟧) : ζ * 1 = ζ := begin rw mul_comp,rw one_comp, have V := mul_one_R C G R R ζ, rw [Yoneda.apply_to_composition, ← assoc,prod.left_composition,comp_id,← assoc ,Terminal_comp,Yoneda.composition_to_apply] at V, exact V, use G, end def inv' (R :C) : R⟦ G.X⟧ → R⟦ G.X⟧ := λ ζ, begin exact R<G.inv> ζ, end instance yoneda_inv (R :C) : has_inv (R⟦G.X⟧) := ⟨inv' C G R⟩ lemma inv_comp (ζ : R ⟦ G.X⟧ ) : ζ⁻¹ = (R<G.inv>) ζ := rfl lemma mul_left_inv' (ζ : R ⟦ G.X ⟧) : (ζ⁻¹ * ζ ) = 1 := begin rw inv_comp,rw mul_comp,rw one_comp, rw Yoneda.apply_to_composition, have V : R< (G.inv | 𝟙 G.X ) ≫ G.μ> ζ = (R<(T G.X) ≫ G.ε>) ζ , rw G.hyp_inv_mul, rw [Yoneda.apply_to_composition,Yoneda.apply_to_composition, ← assoc,prod.left_composition,comp_id,← assoc,Terminal_comp,Yoneda.composition_to_apply] at V, assumption, use G, end lemma Grall (a b c : R ⟦G.X ⟧) : R < (prod.associator G.X G.X G.X).hom ≫ (𝟙 G.X ⊗ G.μ) ≫ G.μ> (a | b | c) =(R < G.μ>) (a | (R < G.μ> (b | c))) := begin tidy, rw [Yoneda.apply_to_composition, ← assoc,prod.left_composition, ← assoc,prod.prod_comp_otimes,Yoneda.apply_to_composition,Yoneda.apply_to_composition], rw comp_id, rw [← assoc,prod.left_composition,prod.lift_fst,prod.lift_fst, prod.lift_snd,← assoc,prod.lift_fst,prod.lift_snd], end -- (hyp_mul_inv : (inv | 𝟙 X ) ≫ μ = (T X) ≫ ε ) lemma mul_assoc' (a b c : R ⟦G.X ⟧) : a * b *c = a * ( b * c ) := begin iterate 4 { rw mul_comp}, PRODUCT_CAT, have ASSOC : R<((G.μ ⊗ (𝟙 G.X)) ≫ (G.μ)) >(a | b | c) = (R<(prod.associator G.X G.X G.X).hom ≫ (𝟙 G.X ⊗ G.μ) ≫ G.μ>) (a | b | c), rw G.hyp_assoc, rw [Yoneda.apply_to_composition,← assoc,prod.prod_comp_otimes,comp_id, ← Yoneda.apply_to_composition, ← Yoneda.apply_to_composition] at ASSOC, have G_hyp : R < (prod.associator G.X G.X G.X).hom ≫ (𝟙 G.X ⊗ G.μ) ≫ G.μ> (a | b | c) =(R < G.μ>) (a | (R < G.μ> (b | c))), exact Grall C G R a b c , rw G_hyp at ASSOC,assumption, -- R<(prod.associator G.X G.X G.X).hom ≫ (𝟙 G.X ⊗ G.μ) ≫ G.μ> (a | b | c), end instance : group (R⟦G.X⟧) := { mul := has_mul.mul, mul_assoc := mul_assoc' C G R, one := (1 : R⟦ G.X⟧), mul_one := mul_onef' C G R, one_mul := one_mulf' C G R, inv := inv' C G R, mul_left_inv := mul_left_inv' C G R, }
9fdc865c9b168a00df8829ffce911f08e9027125
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/elabissues/let_destruct_inside_forall.lean
be5eb30665d36f1a5b065c9c4b312719604fafd4
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach" ]
permissive
leanprover/lean4
4bdf9790294964627eb9be79f5e8f6157780b4cc
f1f9dc0f2f531af3312398999d8b8303fa5f096b
refs/heads/master
1,693,360,665,786
1,693,350,868,000
1,693,350,868,000
129,571,436
2,827
311
Apache-2.0
1,694,716,156,000
1,523,760,560,000
Lean
UTF-8
Lean
false
false
1,253
lean
axiom Foo : Type axiom foo : Foo /- This works -/ def works : Prop := -- ∀ (x : Foo), let ⟨foo₁, foo₂⟩ := (foo, foo); false /- The following tests fail because the elaborator fails to propagate the expected type. The main issue is that the elaborator is missing the following case: If the expected type is `Prop` for an expression `Forall (x : A), t`, then we should also elaborate `t` with expected type `Prop`. Note that every single example works if we write them as `Forall (x : A), (t : Prop)`. -/ /- Uncommenting breaks it -/ def fails₁ : Prop := ∀ (x : Foo), let ⟨foo₁, foo₂⟩ := (foo, foo); false -- let_destruct_inside_forall.lean:11:4: error: invalid match/convoy expression, expected type is not known /- All the following variations fail as well with the same message -/ def fails₂ : Prop := ∀ (x : Foo), let (⟨foo₁, foo₂⟩ : Foo × Foo) := (foo, foo); foo₁ = foo₂ def fails₃ : Prop := ∀ (x : Foo), let (⟨foo₁, foo₂⟩ : Foo × Foo) := (foo, foo); false def fails₄ : Prop := ∀ (x : Foo), let (⟨foo₁, foo₂⟩ : Foo × Foo) := ((foo, foo) : Foo × Foo); false def fails₅ : Prop := ∀ (x : Foo), let p : Foo × Foo := (foo, foo); let ⟨foo₁, foo₂⟩ := p; foo₁ = foo₂
9eed641ddedff037a076a6ed9721b93564c27426
a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940
/src/Init/Notation.lean
70b7f00db7ec682f48f30d9eb92ceee8c9e0e9da
[ "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
18,660
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura Notation for operators defined at Prelude.lean -/ prelude import Init.Prelude -- DSL for specifying parser precedences and priorities namespace Lean.Parser.Syntax syntax:65 (name := addPrec) prec " + " prec:66 : prec syntax:65 (name := subPrec) prec " - " prec:66 : prec syntax:65 (name := addPrio) prio " + " prio:66 : prio syntax:65 (name := subPrio) prio " - " prio:66 : prio end Lean.Parser.Syntax macro "max" : prec => `(1024) -- maximum precedence used in term parsers, in particular for terms in function position (`ident`, `paren`, ...) macro "arg" : prec => `(1023) -- precedence used for application arguments (`do`, `by`, ...) macro "lead" : prec => `(1022) -- precedence used for terms not supposed to be used as arguments (`let`, `have`, ...) macro "(" p:prec ")" : prec => p macro "min" : prec => `(10) -- minimum precedence used in term parsers macro "min1" : prec => `(11) -- `(min+1) we can only `min+1` after `Meta.lean` /- `max:prec` as a term. It is equivalent to `eval_prec max` for `eval_prec` defined at `Meta.lean`. We use `max_prec` to workaround bootstrapping issues. -/ macro "max_prec" : term => `(1024) macro "default" : prio => `(1000) macro "low" : prio => `(100) macro "mid" : prio => `(1000) macro "high" : prio => `(10000) macro "(" p:prio ")" : prio => p -- Basic notation for defining parsers syntax stx "+" : stx syntax stx "*" : stx syntax stx "?" : stx syntax:2 stx " <|> " stx:1 : stx macro_rules | `(stx| $p +) => `(stx| many1($p)) | `(stx| $p *) => `(stx| many($p)) | `(stx| $p ?) => `(stx| optional($p)) | `(stx| $p₁ <|> $p₂) => `(stx| orelse($p₁, $p₂)) /- Comma-separated sequence. -/ macro:max x:stx ",*" : stx => `(stx| sepBy($x, ",", ", ")) macro:max x:stx ",+" : stx => `(stx| sepBy1($x, ",", ", ")) /- Comma-separated sequence with optional trailing comma. -/ macro:max x:stx ",*,?" : stx => `(stx| sepBy($x, ",", ", ", allowTrailingSep)) macro:max x:stx ",+,?" : stx => `(stx| sepBy1($x, ",", ", ", allowTrailingSep)) macro "!" x:stx : stx => `(stx| notFollowedBy($x)) syntax (name := rawNatLit) "nat_lit " num : term infixr:90 " ∘ " => Function.comp infixr:35 " × " => Prod infixl:55 " ||| " => HOr.hOr infixl:58 " ^^^ " => HXor.hXor infixl:60 " &&& " => HAnd.hAnd infixl:65 " + " => HAdd.hAdd infixl:65 " - " => HSub.hSub infixl:70 " * " => HMul.hMul infixl:70 " / " => HDiv.hDiv infixl:70 " % " => HMod.hMod infixl:75 " <<< " => HShiftLeft.hShiftLeft infixl:75 " >>> " => HShiftRight.hShiftRight infixr:80 " ^ " => HPow.hPow prefix:100 "-" => Neg.neg prefix:100 "~~~" => Complement.complement /- Remark: the infix commands above ensure a delaborator is generated for each relations. We redefine the macros below to be able to use the auxiliary `binop%` elaboration helper for binary operators. It addresses issue #382. -/ macro_rules | `($x ||| $y) => `(binop% HOr.hOr $x $y) macro_rules | `($x ^^^ $y) => `(binop% HXor.hXor $x $y) macro_rules | `($x &&& $y) => `(binop% HAnd.hAnd $x $y) macro_rules | `($x + $y) => `(binop% HAdd.hAdd $x $y) macro_rules | `($x - $y) => `(binop% HSub.hSub $x $y) macro_rules | `($x * $y) => `(binop% HMul.hMul $x $y) macro_rules | `($x / $y) => `(binop% HDiv.hDiv $x $y) macro_rules | `($x % $y) => `(binop% HMod.hMod $x $y) macro_rules | `($x <<< $y) => `(binop% HShiftLeft.hShiftLeft $x $y) macro_rules | `($x >>> $y) => `(binop% HShiftRight.hShiftRight $x $y) macro_rules | `($x ^ $y) => `(binop% HPow.hPow $x $y) -- declare ASCII alternatives first so that the latter Unicode unexpander wins infix:50 " <= " => LE.le infix:50 " ≤ " => LE.le infix:50 " < " => LT.lt infix:50 " >= " => GE.ge infix:50 " ≥ " => GE.ge infix:50 " > " => GT.gt infix:50 " = " => Eq infix:50 " == " => BEq.beq infix:50 " ~= " => HEq infix:50 " ≅ " => HEq /- Remark: the infix commands above ensure a delaborator is generated for each relations. We redefine the macros below to be able to use the auxiliary `binrel%` elaboration helper for binary relations. It has better support for applying coercions. For example, suppose we have `binrel% Eq n i` where `n : Nat` and `i : Int`. The default elaborator fails because we don't have a coercion from `Int` to `Nat`, but `binrel%` succeeds because it also tries a coercion from `Nat` to `Int` even when the nat occurs before the int. -/ macro_rules | `($x <= $y) => `(binrel% LE.le $x $y) macro_rules | `($x ≤ $y) => `(binrel% LE.le $x $y) macro_rules | `($x < $y) => `(binrel% LT.lt $x $y) macro_rules | `($x > $y) => `(binrel% GT.gt $x $y) macro_rules | `($x >= $y) => `(binrel% GE.ge $x $y) macro_rules | `($x ≥ $y) => `(binrel% GE.ge $x $y) macro_rules | `($x = $y) => `(binrel% Eq $x $y) macro_rules | `($x == $y) => `(binrel% BEq.beq $x $y) infixr:35 " /\\ " => And infixr:35 " ∧ " => And infixr:30 " \\/ " => Or infixr:30 " ∨ " => Or notation:max "¬" p:40 => Not p infixl:35 " && " => and infixl:30 " || " => or notation:max "!" b:40 => not b infixl:65 " ++ " => HAppend.hAppend infixr:67 " :: " => List.cons infixr:20 " <|> " => HOrElse.hOrElse infixr:60 " >> " => HAndThen.hAndThen infixl:55 " >>= " => Bind.bind infixl:60 " <*> " => Seq.seq infixl:60 " <* " => SeqLeft.seqLeft infixr:60 " *> " => SeqRight.seqRight infixr:100 " <$> " => Functor.map syntax (name := termDepIfThenElse) ppGroup(ppDedent("if " ident " : " term " then" ppSpace term ppDedent(ppSpace "else") ppSpace term)) : term macro_rules | `(if $h:ident : $c then $t:term else $e:term) => ``(dite $c (fun $h:ident => $t) (fun $h:ident => $e)) syntax (name := termIfThenElse) ppGroup(ppDedent("if " term " then" ppSpace term ppDedent(ppSpace "else") ppSpace term)) : term macro_rules | `(if $c then $t:term else $e:term) => ``(ite $c $t $e) macro "if " "let " pat:term " := " d:term " then " t:term " else " e:term : term => `(match $d:term with | $pat:term => $t | _ => $e) syntax:min term "<|" term:min : term macro_rules | `($f $args* <| $a) => let args := args.push a; `($f $args*) | `($f <| $a) => `($f $a) syntax:min term "|>" term:min1 : term macro_rules | `($a |> $f $args*) => let args := args.push a; `($f $args*) | `($a |> $f) => `($f $a) -- Haskell-like pipe <| -- Note that we have a whitespace after `$` to avoid an ambiguity with the antiquotations. syntax:min term atomic("$" ws) term:min : term macro_rules | `($f $args* $ $a) => let args := args.push a; `($f $args*) | `($f $ $a) => `($f $a) syntax "{ " ident (" : " term)? " // " term " }" : term macro_rules | `({ $x : $type // $p }) => ``(Subtype (fun ($x:ident : $type) => $p)) | `({ $x // $p }) => ``(Subtype (fun ($x:ident : _) => $p)) /- `without_expected_type t` instructs Lean to elaborate `t` without an expected type. Recall that terms such as `match ... with ...` and `⟨...⟩` will postpone elaboration until expected type is known. So, `without_expected_type` is not effective in this case. -/ macro "without_expected_type " x:term : term => `(let aux := $x; aux) syntax "[" term,* "]" : term syntax "%[" term,* "|" term "]" : term -- auxiliary notation for creating big list literals namespace Lean macro_rules | `([ $elems,* ]) => do let rec expandListLit (i : Nat) (skip : Bool) (result : Syntax) : MacroM Syntax := do match i, skip with | 0, _ => pure result | i+1, true => expandListLit i false result | i+1, false => expandListLit i true (← ``(List.cons $(elems.elemsAndSeps[i]) $result)) if elems.elemsAndSeps.size < 64 then expandListLit elems.elemsAndSeps.size false (← ``(List.nil)) else `(%[ $elems,* | List.nil ]) notation:50 e:51 " matches " p:51 => match e with | p => true | _ => false namespace Parser.Tactic /-- Introduce one or more hypotheses, optionally naming and/or pattern-matching them. For each hypothesis to be introduced, the remaining main goal's target type must be a `let` or function type. * `intro` by itself introduces one anonymous hypothesis, which can be accessed by e.g. `assumption`. * `intro x y` introduces two hypotheses and names them. Individual hypotheses can be anonymized via `_`, or matched against a pattern: ```lean -- ... ⊢ α × β → ... intro (a, b) -- ..., a : α, b : β ⊢ ... ``` * Alternatively, `intro` can be combined with pattern matching much like `fun`: ```lean intro | n + 1, 0 => tac | ... ``` -/ syntax (name := intro) "intro " notFollowedBy("|") (colGt term:max)* : tactic /-- `intros x...` behaves like `intro x...`, but then keeps introducing (anonymous) hypotheses until goal is not of a function type. -/ syntax (name := intros) "intros " (colGt (ident <|> "_"))* : tactic /-- `rename t => x` renames the most recent hypothesis whose type matches `t` (which may contain placeholders) to `x`, or fails if no such hypothesis could be found. -/ syntax (name := rename) "rename " term " => " ident : tactic /-- `revert x...` is the inverse of `intro x...`: it moves the given hypotheses into the main goal's target type. -/ syntax (name := revert) "revert " (colGt ident)+ : tactic /-- `clear x...` removes the given hypotheses, or fails if there are remaining references to a hypothesis. -/ syntax (name := clear) "clear " (colGt ident)+ : tactic /-- `subst x...` substitutes each `x` with `e` in the goal if there is a hypothesis of type `x = e` or `e = x`. If `x` is itself a hypothesis of type `y = e` or `e = y`, `y` is substituted instead. -/ syntax (name := subst) "subst " (colGt ident)+ : tactic /-- `assumption` tries to solve the main goal using a hypothesis of compatible type, or else fails. Note also the `‹t›` term notation, which is a shorthand for `show t by assumption`. -/ syntax (name := assumption) "assumption" : tactic /-- `contradiction` closes the main goal if its hypotheses are "trivially contradictory". ```lean example (h : False) : p := by contradiction -- inductive type/family with no applicable constructors example (h : none = some true) : p := by contradiction -- injectivity of constructors example (h : 2 + 2 = 3) : p := by contradiction -- decidable false proposition example (h : p) (h' : ¬ p) : q := by contradiction example (x : Nat) (h : x ≠ x) : p := by contradiction ``` -/ syntax (name := contradiction) "contradiction" : tactic /-- `apply e` tries to match the current goal against the conclusion of `e`'s type. If it succeeds, then the tactic returns as many subgoals as the number of premises that have not been fixed by type inference or type class resolution. Non-dependent premises are added before dependent ones. The `apply` tactic uses higher-order pattern matching, type class resolution, and first-order unification with dependent types. -/ syntax (name := apply) "apply " term : tactic /-- `exact e` closes the main goal if its target type matches that of `e`. -/ syntax (name := exact) "exact " term : tactic /-- `refine e` behaves like `exact e`, except that named (`?x`) or unnamed (`?_`) holes in `e` that are not solved by unification with the main goal's target type are converted into new goals, using the hole's name, if any, as the goal case name. -/ syntax (name := refine) "refine " term : tactic /-- `refine' e` behaves like `refine e`, except that unsolved placeholders (`_`) and implicit parameters are also converted into new goals. -/ syntax (name := refine') "refine' " term : tactic /-- If the main goal's target type is an inductive type, `constructor` solves it with the first matching constructor, or else fails. -/ syntax (name := constructor) "constructor" : tactic /-- `case tag => tac` focuses on the goal with case name `tag` and solves it using `tac`, or else fails. `case tag x₁ ... xₙ => tac` additionally renames the `n` most recent hypotheses with inaccessible names to the given names. -/ syntax (name := case) "case " ident (ident <|> "_")* " => " tacticSeq : tactic /-- `allGoals tac` runs `tac` on each goal, concatenating the resulting goals, if any. -/ syntax (name := allGoals) "allGoals " tacticSeq : tactic /-- `focus tac` focuses on the main goal, suppressing all other goals, and runs `tac` on it. Usually `· tac`, which enforces that the goal is closed by `tac`, should be preferred. -/ syntax (name := focus) "focus " tacticSeq : tactic /-- `skip` does nothing. -/ syntax (name := skip) "skip" : tactic /-- `done` succeeds iff there are no remaining goals. -/ syntax (name := done) "done" : tactic syntax (name := traceState) "traceState" : tactic syntax (name := failIfSuccess) "failIfSuccess " tacticSeq : tactic /-- `generalize [h :] e = x` replaces all occurrences of the term `e` in the main goal with a fresh hypothesis `x`. If `h` is given, `h : e = x` is introduced as well. -/ syntax (name := generalize) "generalize " atomic(ident " : ")? term:51 " = " ident : tactic syntax (name := paren) "(" tacticSeq ")" : tactic syntax (name := withReducible) "withReducible " tacticSeq : tactic syntax (name := withReducibleAndInstances) "withReducibleAndInstances " tacticSeq : tactic /-- `first | tac | ...` runs each `tac` until one succeeds, or else fails. -/ syntax (name := first) "first " withPosition((group(colGe "|" tacticSeq))+) : tactic syntax (name := rotateLeft) "rotateLeft" (num)? : tactic syntax (name := rotateRight) "rotateRight" (num)? : tactic /-- `try tac` runs `tac` and succeeds even if `tac` failed. -/ macro "try " t:tacticSeq : tactic => `(first | $t | skip) /-- `tac <;> tac'` runs `tac` on the main goal and `tac'` on each produced goal, concatenating all goals produced by `tac'`. -/ macro:1 x:tactic " <;> " y:tactic:0 : tactic => `(tactic| focus ($x:tactic; allGoals $y:tactic)) /-- `· tac` focuses on the main goal and tries to solve it using `tac`, or else fails. -/ macro dot:("·" <|> ".") ts:tacticSeq : tactic => `(tactic| {%$dot ($ts:tacticSeq) }) /-- `rfl` is a shorthand for `exact rfl`. -/ macro "rfl" : tactic => `(exact rfl) /-- `admit` is a shorthand for `exact sorry`. -/ macro "admit" : tactic => `(exact sorry) macro "inferInstance" : tactic => `(exact inferInstance) syntax locationWildcard := "*" syntax locationHyp := (colGt ident)+ ("⊢" <|> "|-")? -- TODO: delete syntax locationTargets := (colGt ident)+ ("⊢" <|> "|-")? syntax location := withPosition("at " locationWildcard <|> locationHyp) syntax (name := change) "change " term (location)? : tactic syntax (name := changeWith) "change " term " with " term (location)? : tactic syntax rwRule := ("←" <|> "<-")? term syntax rwRuleSeq := "[" rwRule,+,? "]" syntax (name := rewriteSeq) "rewrite " rwRuleSeq (location)? : tactic syntax (name := erewriteSeq) "erewrite " rwRuleSeq (location)? : tactic syntax (name := rwSeq) "rw " rwRuleSeq (location)? : tactic syntax (name := erwSeq) "erw " rwRuleSeq (location)? : tactic def rwWithRfl (kind : SyntaxNodeKind) (atom : String) (stx : Syntax) : MacroM Syntax := do -- We show the `rfl` state on `]` let seq := stx[1] let rbrak := seq[2] -- Replace `]` token with one without position information in the expanded tactic let seq := seq.setArg 2 (mkAtom "]") let tac := stx.setKind kind |>.setArg 0 (mkAtomFrom stx atom) |>.setArg 1 seq `(tactic| $tac; try (withReducible rfl%$rbrak)) @[macro rwSeq] def expandRwSeq : Macro := rwWithRfl ``Lean.Parser.Tactic.rewriteSeq "rewrite" @[macro erwSeq] def expandERwSeq : Macro := rwWithRfl ``Lean.Parser.Tactic.erewriteSeq "erewrite" syntax (name := injection) "injection " term (" with " (colGt (ident <|> "_"))+)? : tactic syntax simpPre := "↓" syntax simpPost := "↑" syntax simpLemma := (simpPre <|> simpPost)? term syntax simpErase := "-" ident syntax (name := simp) "simp " ("(" &"config" " := " term ")")? (&"only ")? ("[" (simpErase <|> simpLemma),* "]")? (location)? : tactic syntax (name := simpAll) "simp_all " ("(" &"config" " := " term ")")? (&"only ")? ("[" (simpErase <|> simpLemma),* "]")? : tactic -- Auxiliary macro for lifting have/suffices/let/... -- It makes sure the "continuation" `?_` is the main goal after refining macro "refineLift " e:term : tactic => `(focus (refine noImplicitLambda% $e; rotateRight)) macro "have " d:haveDecl : tactic => `(refineLift have $d:haveDecl; ?_) /- We use a priority > default, to avoid ambiguity with previous `have` notation -/ macro (priority := high) "have" x:ident " := " p:term : tactic => `(have $x:ident : _ := $p) macro "suffices " d:sufficesDecl : tactic => `(refineLift suffices $d:sufficesDecl; ?_) macro "let " d:letDecl : tactic => `(refineLift let $d:letDecl; ?_) macro "show " e:term : tactic => `(refineLift show $e:term from ?_) syntax (name := letrec) withPosition(atomic(group("let " &"rec ")) letRecDecls) : tactic macro_rules | `(tactic| let rec $d:letRecDecls) => `(tactic| refineLift let rec $d:letRecDecls; ?_) -- Similar to `refineLift`, but using `refine'` macro "refineLift' " e:term : tactic => `(focus (refine' noImplicitLambda% $e; rotateRight)) macro "have' " d:haveDecl : tactic => `(refineLift' have $d:haveDecl; ?_) macro (priority := high) "have'" x:ident " := " p:term : tactic => `(have' $x:ident : _ := $p) macro "let' " d:letDecl : tactic => `(refineLift' let $d:letDecl; ?_) syntax inductionAlt := "| " (group("@"? ident) <|> "_") (ident <|> "_")* " => " (hole <|> syntheticHole <|> tacticSeq) syntax inductionAlts := "with " (tactic)? withPosition( (colGe inductionAlt)+) syntax (name := induction) "induction " term,+ (" using " ident)? ("generalizing " ident+)? (inductionAlts)? : tactic syntax casesTarget := atomic(ident " : ")? term syntax (name := cases) "cases " casesTarget,+ (" using " ident)? (inductionAlts)? : tactic syntax (name := existsIntro) "exists " term : tactic syntax "repeat " tacticSeq : tactic macro_rules | `(tactic| repeat $seq) => `(tactic| first | ($seq); repeat $seq | skip) syntax "trivial" : tactic macro_rules | `(tactic| trivial) => `(tactic| assumption) macro_rules | `(tactic| trivial) => `(tactic| rfl) macro_rules | `(tactic| trivial) => `(tactic| contradiction) macro_rules | `(tactic| trivial) => `(tactic| apply True.intro) macro_rules | `(tactic| trivial) => `(tactic| apply And.intro <;> trivial) macro "unhygienic " t:tacticSeq : tactic => `(set_option tactic.hygienic false in $t:tacticSeq) end Tactic namespace Attr -- simp attribute syntax syntax (name := simp) "simp" (Tactic.simpPre <|> Tactic.simpPost)? (prio)? : attr end Attr end Parser end Lean macro "‹" type:term "›" : term => `((by assumption : $type))
ae8c1726dd047a779f685857c47f3c2a911e430f
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/order/galois_connection.lean
a2f681c6cac2872243ac0c9c2847163ea471dc31
[ "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
24,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 -/ import order.complete_lattice import order.order_dual /-! # Galois connections, insertions and coinsertions Galois connections are order theoretic adjoints, i.e. a pair of functions `u` and `l`, such that `∀ a b, l a ≤ b ↔ a ≤ u b`. ## Main definitions * `galois_connection`: A Galois connection is a pair of functions `l` and `u` satisfying `l a ≤ b ↔ a ≤ u b`. They are special cases of adjoint functors in category theory, but do not depend on the category theory library in mathlib. * `galois_insertion`: A Galois insertion is a Galois connection where `l ∘ u = id` * `galois_coinsertion`: A Galois coinsertion is a Galois connection where `u ∘ l = id` ## Implementation details Galois insertions can be used to lift order structures from one type to another. For example if `α` is a complete lattice, and `l : α → β`, and `u : β → α` form a Galois insertion, then `β` is also a complete lattice. `l` is the lower adjoint and `u` is the upper adjoint. An example of a Galois insertion is in group theory. If `G` is a group, then there is a Galois insertion between the set of subsets of `G`, `set G`, and the set of subgroups of `G`, `subgroup G`. The lower adjoint is `subgroup.closure`, taking the `subgroup` generated by a `set`, and the upper adjoint is the coercion from `subgroup G` to `set G`, taking the underlying set of a subgroup. Naively lifting a lattice structure along this Galois insertion would mean that the definition of `inf` on subgroups would be `subgroup.closure (↑S ∩ ↑T)`. This is an undesirable definition because the intersection of subgroups is already a subgroup, so there is no need to take the closure. For this reason a `choice` function is added as a field to the `galois_insertion` structure. It has type `Π S : set G, ↑(closure S) ≤ S → subgroup G`. When `↑(closure S) ≤ S`, then `S` is already a subgroup, so this function can be defined using `subgroup.mk` and not `closure`. This means the infimum of subgroups will be defined to be the intersection of sets, paired with a proof that intersection of subgroups is a subgroup, rather than the closure of the intersection. -/ open function set universes u v w x variables {α : Type u} {β : Type v} {γ : Type w} {ι : Sort x} {a a₁ a₂ : α} {b b₁ b₂ : β} /-- A Galois connection is a pair of functions `l` and `u` satisfying `l a ≤ b ↔ a ≤ u b`. They are special cases of adjoint functors in category theory, but do not depend on the category theory library in mathlib. -/ def galois_connection [preorder α] [preorder β] (l : α → β) (u : β → α) := ∀ a b, l a ≤ b ↔ a ≤ u b /-- Makes a Galois connection from an order-preserving bijection. -/ theorem order_iso.to_galois_connection [preorder α] [preorder β] (oi : α ≃o β) : galois_connection oi oi.symm := λ b g, oi.rel_symm_apply.symm namespace galois_connection section variables [preorder α] [preorder β] {l : α → β} {u : β → α} (gc : galois_connection l u) lemma monotone_intro (hu : monotone u) (hl : monotone l) (hul : ∀ a, a ≤ u (l a)) (hlu : ∀ a, l (u a) ≤ a) : galois_connection l u := λ a b, ⟨λ h, (hul _).trans (hu h), λ h, (hl h).trans (hlu _)⟩ include gc protected lemma dual {l : α → β} {u : β → α} (gc : galois_connection l u) : galois_connection (order_dual.to_dual ∘ u ∘ order_dual.of_dual) (order_dual.to_dual ∘ l ∘ order_dual.of_dual) := λ a b, (gc b a).symm lemma l_le {a : α} {b : β} : a ≤ u b → l a ≤ b := (gc _ _).mpr lemma le_u {a : α} {b : β} : l a ≤ b → a ≤ u b := (gc _ _).mp lemma le_u_l (a) : a ≤ u (l a) := gc.le_u $ le_refl _ lemma l_u_le (a) : l (u a) ≤ a := gc.l_le $ le_refl _ lemma monotone_u : monotone u := λ a b H, gc.le_u ((gc.l_u_le a).trans H) lemma monotone_l : monotone l := gc.dual.monotone_u.dual lemma upper_bounds_l_image (s : set α) : upper_bounds (l '' s) = u ⁻¹' upper_bounds s := set.ext $ λ b, by simp [upper_bounds, gc _ _] lemma lower_bounds_u_image (s : set β) : lower_bounds (u '' s) = l ⁻¹' lower_bounds s := gc.dual.upper_bounds_l_image s lemma bdd_above_l_image {s : set α} : bdd_above (l '' s) ↔ bdd_above s := ⟨λ ⟨x, hx⟩, ⟨u x, by rwa [gc.upper_bounds_l_image] at hx⟩, gc.monotone_l.map_bdd_above⟩ lemma bdd_below_u_image {s : set β} : bdd_below (u '' s) ↔ bdd_below s := gc.dual.bdd_above_l_image lemma is_lub_l_image {s : set α} {a : α} (h : is_lub s a) : is_lub (l '' s) (l a) := ⟨gc.monotone_l.mem_upper_bounds_image h.left, λ b hb, gc.l_le $ h.right $ by rwa [gc.upper_bounds_l_image] at hb⟩ lemma is_glb_u_image {s : set β} {b : β} (h : is_glb s b) : is_glb (u '' s) (u b) := gc.dual.is_lub_l_image h lemma is_glb_l {a : α} : is_glb { b | a ≤ u b } (l a) := ⟨λ b, gc.l_le, λ b h, h $ gc.le_u_l _⟩ lemma is_lub_u {b : β} : is_lub { a | l a ≤ b } (u b) := ⟨λ b, gc.le_u, λ b h, h $ gc.l_u_le _⟩ end section partial_order variables [partial_order α] [preorder β] {l : α → β} {u : β → α} (gc : galois_connection l u) include gc lemma u_l_u_eq_u : u ∘ l ∘ u = u := funext (λ x, (gc.monotone_u (gc.l_u_le _)).antisymm (gc.le_u_l _)) lemma u_unique {l' : α → β} {u' : β → α} (gc' : galois_connection l' u') (hl : ∀ a, l a = l' a) {b : β} : u b = u' b := le_antisymm (gc'.le_u $ hl (u b) ▸ gc.l_u_le _) (gc.le_u $ (hl (u' b)).symm ▸ gc'.l_u_le _) end partial_order section partial_order variables [preorder α] [partial_order β] {l : α → β} {u : β → α} (gc : galois_connection l u) include gc lemma l_u_l_eq_l : l ∘ u ∘ l = l := funext (λ x, (gc.l_u_le _).antisymm (gc.monotone_l (gc.le_u_l _))) lemma l_unique {l' : α → β} {u' : β → α} (gc' : galois_connection l' u') (hu : ∀ b, u b = u' b) {a : α} : l a = l' a := le_antisymm (gc.l_le $ (hu (l' a)).symm ▸ gc'.le_u_l _) (gc'.l_le $ hu (l a) ▸ gc.le_u_l _) end partial_order section order_top variables [order_top α] [order_top β] {l : α → β} {u : β → α} (gc : galois_connection l u) include gc lemma u_top : u ⊤ = ⊤ := top_unique $ gc.le_u le_top end order_top section order_bot variables [order_bot α] [order_bot β] {l : α → β} {u : β → α} (gc : galois_connection l u) include gc lemma l_bot : l ⊥ = ⊥ := gc.dual.u_top end order_bot section semilattice_sup variables [semilattice_sup α] [semilattice_sup β] {l : α → β} {u : β → α} (gc : galois_connection l u) include gc lemma l_sup : l (a₁ ⊔ a₂) = l a₁ ⊔ l a₂ := (gc.is_lub_l_image is_lub_pair).unique $ by simp only [image_pair, is_lub_pair] end semilattice_sup section semilattice_inf variables [semilattice_inf α] [semilattice_inf β] {l : α → β} {u : β → α} (gc : galois_connection l u) include gc lemma u_inf : u (b₁ ⊓ b₂) = u b₁ ⊓ u b₂ := gc.dual.l_sup end semilattice_inf section complete_lattice variables [complete_lattice α] [complete_lattice β] {l : α → β} {u : β → α} (gc : galois_connection l u) include gc lemma l_supr {f : ι → α} : l (supr f) = (⨆i, l (f i)) := eq.symm $ is_lub.supr_eq $ show is_lub (range (l ∘ f)) (l (supr f)), by rw [range_comp, ← Sup_range]; exact gc.is_lub_l_image (is_lub_Sup _) lemma u_infi {f : ι → β} : u (infi f) = (⨅i, u (f i)) := gc.dual.l_supr lemma l_Sup {s : set α} : l (Sup s) = (⨆a ∈ s, l a) := by simp only [Sup_eq_supr, gc.l_supr] lemma u_Inf {s : set β} : u (Inf s) = (⨅a ∈ s, u a) := gc.dual.l_Sup end complete_lattice /- Constructing Galois connections -/ section constructions protected lemma id [pα : preorder α] : @galois_connection α α pα pα id id := λ a b, iff.intro (λ x, x) (λ x, x) protected lemma compose [preorder α] [preorder β] [preorder γ] (l1 : α → β) (u1 : β → α) (l2 : β → γ) (u2 : γ → β) (gc1 : galois_connection l1 u1) (gc2 : galois_connection l2 u2) : galois_connection (l2 ∘ l1) (u1 ∘ u2) := by intros a b; rw [gc2, gc1] protected lemma dfun {ι : Type u} {α : ι → Type v} {β : ι → Type w} [∀ i, preorder (α i)] [∀ i, preorder (β i)] (l : Πi, α i → β i) (u : Πi, β i → α i) (gc : ∀ i, galois_connection (l i) (u i)) : @galois_connection (Π i, α i) (Π i, β i) _ _ (λ a i, l i (a i)) (λ b i, u i (b i)) := λ a b, forall_congr $ λ i, gc i (a i) (b i) end constructions end galois_connection namespace order_iso variables [preorder α] [preorder β] @[simp] lemma bdd_above_image (e : α ≃o β) {s : set α} : bdd_above (e '' s) ↔ bdd_above s := e.to_galois_connection.bdd_above_l_image @[simp] lemma bdd_below_image (e : α ≃o β) {s : set α} : bdd_below (e '' s) ↔ bdd_below s := e.dual.bdd_above_image @[simp] lemma bdd_above_preimage (e : α ≃o β) {s : set β} : bdd_above (e ⁻¹' s) ↔ bdd_above s := by rw [← e.bdd_above_image, e.image_preimage] @[simp] lemma bdd_below_preimage (e : α ≃o β) {s : set β} : bdd_below (e ⁻¹' s) ↔ bdd_below s := by rw [← e.bdd_below_image, e.image_preimage] end order_iso namespace nat lemma galois_connection_mul_div {k : ℕ} (h : 0 < k) : galois_connection (λ n, n * k) (λ n, n / k) := λ x y, (le_div_iff_mul_le x y h).symm end nat /-- A Galois insertion is a Galois connection where `l ∘ u = id`. It also contains a constructive choice function, to give better definitional equalities when lifting order structures. Dual to `galois_coinsertion` -/ @[nolint has_inhabited_instance] structure galois_insertion {α β : Type*} [preorder α] [preorder β] (l : α → β) (u : β → α) := (choice : Πx : α, u (l x) ≤ x → β) (gc : galois_connection l u) (le_l_u : ∀ x, x ≤ l (u x)) (choice_eq : ∀ a h, choice a h = l a) /-- A constructor for a Galois insertion with the trivial `choice` function. -/ def galois_insertion.monotone_intro {α β : Type*} [preorder α] [preorder β] {l : α → β} {u : β → α} (hu : monotone u) (hl : monotone l) (hul : ∀ a, a ≤ u (l a)) (hlu : ∀ b, l (u b) = b) : galois_insertion l u := { choice := λ x _, l x, gc := galois_connection.monotone_intro hu hl hul (λ b, le_of_eq (hlu b)), le_l_u := λ b, le_of_eq $ (hlu b).symm, choice_eq := λ _ _, rfl } /-- Makes a Galois insertion from an order-preserving bijection. -/ protected def order_iso.to_galois_insertion [preorder α] [preorder β] (oi : α ≃o β) : @galois_insertion α β _ _ (oi) (oi.symm) := { choice := λ b h, oi b, gc := oi.to_galois_connection, le_l_u := λ g, le_of_eq (oi.right_inv g).symm, choice_eq := λ b h, rfl } /-- Make a `galois_insertion l u` from a `galois_connection l u` such that `∀ b, b ≤ l (u b)` -/ def galois_connection.to_galois_insertion {α β : Type*} [preorder α] [preorder β] {l : α → β} {u : β → α} (gc : galois_connection l u) (h : ∀ b, b ≤ l (u b)) : galois_insertion l u := { choice := λ x _, l x, gc := gc, le_l_u := h, choice_eq := λ _ _, rfl } /-- Lift the bottom along a Galois connection -/ def galois_connection.lift_order_bot {α β : Type*} [order_bot α] [partial_order β] {l : α → β} {u : β → α} (gc : galois_connection l u) : order_bot β := { bot := l ⊥, bot_le := λ b, gc.l_le $ bot_le, .. ‹partial_order β› } namespace galois_insertion variables {l : α → β} {u : β → α} lemma l_u_eq [preorder α] [partial_order β] (gi : galois_insertion l u) (b : β) : l (u b) = b := (gi.gc.l_u_le _).antisymm (gi.le_l_u _) lemma left_inverse_l_u [preorder α] [partial_order β] (gi : galois_insertion l u) : left_inverse l u := gi.l_u_eq lemma l_surjective [preorder α] [partial_order β] (gi : galois_insertion l u) : surjective l := gi.left_inverse_l_u.surjective lemma u_injective [preorder α] [partial_order β] (gi : galois_insertion l u) : injective u := gi.left_inverse_l_u.injective lemma l_sup_u [semilattice_sup α] [semilattice_sup β] (gi : galois_insertion l u) (a b : β) : l (u a ⊔ u b) = a ⊔ b := calc l (u a ⊔ u b) = l (u a) ⊔ l (u b) : gi.gc.l_sup ... = a ⊔ b : by simp only [gi.l_u_eq] lemma l_supr_u [complete_lattice α] [complete_lattice β] (gi : galois_insertion l u) {ι : Sort x} (f : ι → β) : l (⨆ i, u (f i)) = ⨆ i, (f i) := calc l (⨆ (i : ι), u (f i)) = ⨆ (i : ι), l (u (f i)) : gi.gc.l_supr ... = ⨆ (i : ι), f i : congr_arg _ $ funext $ λ i, gi.l_u_eq (f i) lemma l_inf_u [semilattice_inf α] [semilattice_inf β] (gi : galois_insertion l u) (a b : β) : l (u a ⊓ u b) = a ⊓ b := calc l (u a ⊓ u b) = l (u (a ⊓ b)) : congr_arg l gi.gc.u_inf.symm ... = a ⊓ b : by simp only [gi.l_u_eq] lemma l_infi_u [complete_lattice α] [complete_lattice β] (gi : galois_insertion l u) {ι : Sort x} (f : ι → β) : l (⨅ i, u (f i)) = ⨅ i, (f i) := calc l (⨅ (i : ι), u (f i)) = l (u (⨅ (i : ι), (f i))) : congr_arg l gi.gc.u_infi.symm ... = ⨅ (i : ι), f i : gi.l_u_eq _ lemma l_infi_of_ul_eq_self [complete_lattice α] [complete_lattice β] (gi : galois_insertion l u) {ι : Sort x} (f : ι → α) (hf : ∀ i, u (l (f i)) = f i) : l (⨅ i, (f i)) = ⨅ i, l (f i) := calc l (⨅ i, (f i)) = l ⨅ (i : ι), (u (l (f i))) : by simp [hf] ... = ⨅ i, l (f i) : gi.l_infi_u _ lemma u_le_u_iff [preorder α] [preorder β] (gi : galois_insertion l u) {a b} : u a ≤ u b ↔ a ≤ b := ⟨λ h, (gi.le_l_u _).trans (gi.gc.l_le h), λ h, gi.gc.monotone_u h⟩ lemma strict_mono_u [preorder α] [preorder β] (gi : galois_insertion l u) : strict_mono u := strict_mono_of_le_iff_le $ λ _ _, gi.u_le_u_iff.symm lemma is_lub_of_u_image [preorder α] [preorder β] (gi : galois_insertion l u) {s : set β} {a : α} (hs : is_lub (u '' s) a) : is_lub s (l a) := ⟨λ x hx, (gi.le_l_u x).trans $ gi.gc.monotone_l $ hs.1 $ mem_image_of_mem _ hx, λ x hx, gi.gc.l_le $ hs.2 $ gi.gc.monotone_u.mem_upper_bounds_image hx⟩ lemma is_glb_of_u_image [preorder α] [preorder β] (gi : galois_insertion l u) {s : set β} {a : α} (hs : is_glb (u '' s) a) : is_glb s (l a) := ⟨λ x hx, gi.gc.l_le $ hs.1 $ mem_image_of_mem _ hx, λ x hx, (gi.le_l_u x).trans $ gi.gc.monotone_l $ hs.2 $ gi.gc.monotone_u.mem_lower_bounds_image hx⟩ section lift variables [partial_order β] /-- Lift the suprema along a Galois insertion -/ def lift_semilattice_sup [semilattice_sup α] (gi : galois_insertion l u) : semilattice_sup β := { sup := λ a b, l (u a ⊔ u b), le_sup_left := λ a b, (gi.le_l_u a).trans $ gi.gc.monotone_l $ le_sup_left, le_sup_right := λ a b, (gi.le_l_u b).trans $ gi.gc.monotone_l $ le_sup_right, sup_le := λ a b c hac hbc, gi.gc.l_le $ sup_le (gi.gc.monotone_u hac) (gi.gc.monotone_u hbc), .. ‹partial_order β› } /-- Lift the infima along a Galois insertion -/ def lift_semilattice_inf [semilattice_inf α] (gi : galois_insertion l u) : semilattice_inf β := { inf := λ a b, gi.choice (u a ⊓ u b) $ (le_inf (gi.gc.monotone_u $ gi.gc.l_le $ inf_le_left) (gi.gc.monotone_u $ gi.gc.l_le $ inf_le_right)), inf_le_left := by simp only [gi.choice_eq]; exact λ a b, gi.gc.l_le inf_le_left, inf_le_right := by simp only [gi.choice_eq]; exact λ a b, gi.gc.l_le inf_le_right, le_inf := by simp only [gi.choice_eq]; exact λ a b c hac hbc, (gi.le_l_u a).trans $ gi.gc.monotone_l $ le_inf (gi.gc.monotone_u hac) (gi.gc.monotone_u hbc), .. ‹partial_order β› } /-- Lift the suprema and infima along a Galois insertion -/ def lift_lattice [lattice α] (gi : galois_insertion l u) : lattice β := { .. gi.lift_semilattice_sup, .. gi.lift_semilattice_inf } /-- Lift the top along a Galois insertion -/ def lift_order_top [order_top α] (gi : galois_insertion l u) : order_top β := { top := gi.choice ⊤ $ le_top, le_top := by simp only [gi.choice_eq]; exact λ b, (gi.le_l_u b).trans (gi.gc.monotone_l le_top), .. ‹partial_order β› } /-- Lift the top, bottom, suprema, and infima along a Galois insertion -/ def lift_bounded_lattice [bounded_lattice α] (gi : galois_insertion l u) : bounded_lattice β := { .. gi.lift_lattice, .. gi.lift_order_top, .. gi.gc.lift_order_bot } /-- Lift all suprema and infima along a Galois insertion -/ def lift_complete_lattice [complete_lattice α] (gi : galois_insertion l u) : complete_lattice β := { Sup := λ s, l (Sup (u '' s)), Sup_le := λ s, (gi.is_lub_of_u_image (is_lub_Sup _)).2, le_Sup := λ s, (gi.is_lub_of_u_image (is_lub_Sup _)).1, Inf := λ s, gi.choice (Inf (u '' s)) $ gi.gc.monotone_u.le_is_glb_image (gi.is_glb_of_u_image $ is_glb_Inf _) (is_glb_Inf _), Inf_le := λ s, by { rw gi.choice_eq, exact (gi.is_glb_of_u_image (is_glb_Inf _)).1 }, le_Inf := λ s, by { rw gi.choice_eq, exact (gi.is_glb_of_u_image (is_glb_Inf _)).2 }, .. gi.lift_bounded_lattice } end lift end galois_insertion /-- A Galois coinsertion is a Galois connection where `u ∘ l = id`. It also contains a constructive choice function, to give better definitional equalities when lifting order structures. Dual to `galois_insertion` -/ @[nolint has_inhabited_instance] structure galois_coinsertion {α β : Type*} [preorder α] [preorder β] (l : α → β) (u : β → α) := (choice : Πx : β, x ≤ l (u x) → α) (gc : galois_connection l u) (u_l_le : ∀ x, u (l x) ≤ x) (choice_eq : ∀ a h, choice a h = u a) /-- Make a `galois_insertion u l` in the `order_dual`, from a `galois_coinsertion l u` -/ def galois_coinsertion.dual {α β : Type*} [preorder α] [preorder β] {l : α → β} {u : β → α} : galois_coinsertion l u → @galois_insertion (order_dual β) (order_dual α) _ _ u l := λ x, ⟨x.1, x.2.dual, x.3, x.4⟩ /-- Make a `galois_coinsertion u l` in the `order_dual`, from a `galois_insertion l u` -/ def galois_insertion.dual {α β : Type*} [preorder α] [preorder β] {l : α → β} {u : β → α} : galois_insertion l u → @galois_coinsertion (order_dual β) (order_dual α) _ _ u l := λ x, ⟨x.1, x.2.dual, x.3, x.4⟩ /-- Make a `galois_coinsertion l u` from a `galois_insertion l u` in the `order_dual` -/ def galois_coinsertion.of_dual {α β : Type*} [preorder α] [preorder β] {l : α → β} {u : β → α} : @galois_insertion (order_dual β) (order_dual α) _ _ u l → galois_coinsertion l u := λ x, ⟨x.1, x.2.dual, x.3, x.4⟩ /-- Make a `galois_insertion l u` from a `galois_coinsertion l u` in the `order_dual` -/ def galois_insertion.of_dual {α β : Type*} [preorder α] [preorder β] {l : α → β} {u : β → α} : @galois_coinsertion (order_dual β) (order_dual α) _ _ u l → galois_insertion l u := λ x, ⟨x.1, x.2.dual, x.3, x.4⟩ /-- Makes a Galois coinsertion from an order-preserving bijection. -/ protected def rel_iso.to_galois_coinsertion [preorder α] [preorder β] (oi : α ≃o β) : @galois_coinsertion α β _ _ (oi) (oi.symm) := { choice := λ b h, oi.symm b, gc := oi.to_galois_connection, u_l_le := λ g, le_of_eq (oi.left_inv g), choice_eq := λ b h, rfl } /-- A constructor for a Galois coinsertion with the trivial `choice` function. -/ def galois_coinsertion.monotone_intro [preorder α] [preorder β] {l : α → β} {u : β → α} (hu : monotone u) (hl : monotone l) (hlu : ∀ b, l (u b) ≤ b) (hul : ∀ a, u (l a) = a) : galois_coinsertion l u := galois_coinsertion.of_dual (galois_insertion.monotone_intro hl.dual hu.dual hlu hul) /-- Make a `galois_coinsertion l u` from a `galois_connection l u` such that `∀ b, b ≤ l (u b)` -/ def galois_connection.to_galois_coinsertion {α β : Type*} [preorder α] [preorder β] {l : α → β} {u : β → α} (gc : galois_connection l u) (h : ∀ a, u (l a) ≤ a) : galois_coinsertion l u := { choice := λ x _, u x, gc := gc, u_l_le := h, choice_eq := λ _ _, rfl } /-- Lift the top along a Galois connection -/ def galois_connection.lift_order_top {α β : Type*} [partial_order α] [order_top β] {l : α → β} {u : β → α} (gc : galois_connection l u) : order_top α := { top := u ⊤, le_top := λ b, gc.le_u $ le_top, .. ‹partial_order α› } namespace galois_coinsertion variables {l : α → β} {u : β → α} lemma u_l_eq [partial_order α] [preorder β] (gi : galois_coinsertion l u) (a : α) : u (l a) = a := gi.dual.l_u_eq a lemma u_l_left_inverse [partial_order α] [preorder β] (gi : galois_coinsertion l u) : left_inverse u l := gi.u_l_eq lemma u_surjective [partial_order α] [preorder β] (gi : galois_coinsertion l u) : surjective u := gi.dual.l_surjective lemma l_injective [partial_order α] [preorder β] (gi : galois_coinsertion l u) : injective l := gi.dual.u_injective lemma u_inf_l [semilattice_inf α] [semilattice_inf β] (gi : galois_coinsertion l u) (a b : α) : u (l a ⊓ l b) = a ⊓ b := gi.dual.l_sup_u a b lemma u_infi_l [complete_lattice α] [complete_lattice β] (gi : galois_coinsertion l u) {ι : Sort x} (f : ι → α) : u (⨅ i, l (f i)) = ⨅ i, (f i) := gi.dual.l_supr_u _ lemma u_sup_l [semilattice_sup α] [semilattice_sup β] (gi : galois_coinsertion l u) (a b : α) : u (l a ⊔ l b) = a ⊔ b := gi.dual.l_inf_u _ _ lemma u_supr_l [complete_lattice α] [complete_lattice β] (gi : galois_coinsertion l u) {ι : Sort x} (f : ι → α) : u (⨆ i, l (f i)) = ⨆ i, (f i) := gi.dual.l_infi_u _ lemma u_supr_of_lu_eq_self [complete_lattice α] [complete_lattice β] (gi : galois_coinsertion l u) {ι : Sort x} (f : ι → β) (hf : ∀ i, l (u (f i)) = f i) : u (⨆ i, (f i)) = ⨆ i, u (f i) := gi.dual.l_infi_of_ul_eq_self _ hf lemma l_le_l_iff [preorder α] [preorder β] (gi : galois_coinsertion l u) {a b} : l a ≤ l b ↔ a ≤ b := gi.dual.u_le_u_iff lemma strict_mono_l [partial_order α] [preorder β] (gi : galois_coinsertion l u) : strict_mono l := λ a b h, gi.dual.strict_mono_u h lemma is_glb_of_l_image [preorder α] [preorder β] (gi : galois_coinsertion l u) {s : set α} {a : β} (hs : is_glb (l '' s) a) : is_glb s (u a) := gi.dual.is_lub_of_u_image hs lemma is_lub_of_l_image [preorder α] [preorder β] (gi : galois_coinsertion l u) {s : set α} {a : β} (hs : is_lub (l '' s) a) : is_lub s (u a) := gi.dual.is_glb_of_u_image hs section lift variables [partial_order α] /-- Lift the infima along a Galois coinsertion -/ def lift_semilattice_inf [semilattice_inf β] (gi : galois_coinsertion l u) : semilattice_inf α := { inf := λ a b, u (l a ⊓ l b), .. ‹partial_order α›, .. @order_dual.semilattice_inf _ gi.dual.lift_semilattice_sup } /-- Lift the suprema along a Galois coinsertion -/ def lift_semilattice_sup [semilattice_sup β] (gi : galois_coinsertion l u) : semilattice_sup α := { sup := λ a b, gi.choice (l a ⊔ l b) $ (sup_le (gi.gc.monotone_l $ gi.gc.le_u $ le_sup_left) (gi.gc.monotone_l $ gi.gc.le_u $ le_sup_right)), .. ‹partial_order α›, .. @order_dual.semilattice_sup _ gi.dual.lift_semilattice_inf } /-- Lift the suprema and infima along a Galois coinsertion -/ def lift_lattice [lattice β] (gi : galois_coinsertion l u) : lattice α := { .. gi.lift_semilattice_sup, .. gi.lift_semilattice_inf } /-- Lift the bot along a Galois coinsertion -/ def lift_order_bot [order_bot β] (gi : galois_coinsertion l u) : order_bot α := { bot := gi.choice ⊥ $ bot_le, .. ‹partial_order α›, .. @order_dual.order_bot _ gi.dual.lift_order_top } /-- Lift the top, bottom, suprema, and infima along a Galois coinsertion -/ def lift_bounded_lattice [bounded_lattice β] (gi : galois_coinsertion l u) : bounded_lattice α := { .. gi.lift_lattice, .. gi.lift_order_bot, .. gi.gc.lift_order_top } /-- Lift all suprema and infima along a Galois coinsertion -/ def lift_complete_lattice [complete_lattice β] (gi : galois_coinsertion l u) : complete_lattice α := { Inf := λ s, u (Inf (l '' s)), Sup := λ s, gi.choice (Sup (l '' s)) _, .. gi.lift_bounded_lattice, .. @order_dual.complete_lattice _ gi.dual.lift_complete_lattice } end lift end galois_coinsertion /-- If `α` is a partial order with bottom element (e.g., `ℕ`, `ℝ≥0`), then `λ o : with_bot α, o.get_or_else ⊥` and coercion form a Galois insertion. -/ def with_bot.gi_get_or_else_bot [order_bot α] : galois_insertion (λ o : with_bot α, o.get_or_else ⊥) coe := { gc := λ a b, with_bot.get_or_else_bot_le_iff, le_l_u := λ a, le_rfl, choice := λ o ho, _, choice_eq := λ _ _, rfl }
bbece5f887b1eced3c954c7f21df5afc89c5587a
5ee26964f602030578ef0159d46145dd2e357ba5
/src/Spa.lean
d7a5188fb251ec19e8a2e8c78b019d0a2ef836f7
[ "Apache-2.0" ]
permissive
fpvandoorn/lean-perfectoid-spaces
569b4006fdfe491ca8b58dd817bb56138ada761f
06cec51438b168837fc6e9268945735037fd1db6
refs/heads/master
1,590,154,571,918
1,557,685,392,000
1,557,685,392,000
186,363,547
0
0
Apache-2.0
1,557,730,933,000
1,557,730,933,000
null
UTF-8
Lean
false
false
29,134
lean
import ring_theory.localization import ring_theory.subring import continuous_valuations import Huber_pair import Huber_ring.localization import for_mathlib.nonarchimedean.basic import for_mathlib.uniform_space.ring -- need completions of rings plus UMP import for_mathlib.group -- some stupid lemma about units import for_mathlib.sheaves.presheaf_of_topological_rings import for_mathlib.topological_rings -- subring of a top ring universes u₁ u₂ u₃ local attribute [instance, priority 0] classical.prop_decidable local attribute [instance] set.pointwise_mul_comm_semiring open set function Spv valuation variables {Γ : Type*} [linear_ordered_comm_group Γ] -- Wedhorn def 7.23. definition spa (A : Huber_pair) : set (Spv A) := {v | v.is_continuous ∧ ∀ r ∈ A⁺, v r ≤ 1} lemma mk_mem_spa {A : Huber_pair} {v : valuation A Γ} : mk v ∈ spa A ↔ v.is_continuous ∧ ∀ r ∈ A⁺, v r ≤ 1 := begin apply and_congr, { apply is_equiv.is_continuous_iff, apply out_mk, }, { apply forall_congr, intro r, apply forall_congr, intro hr, convert (out_mk v) r 1; rw [valuation.map_one] } end namespace spa variable {A : Huber_pair} instance : has_coe (spa A) (Spv A) := ⟨subtype.val⟩ definition basic_open (r s : A) : set (spa A) := {v | v r ≤ v s ∧ v s ≠ 0 } lemma mk_mem_basic_open {r s : A} {v : valuation A Γ} {hv : mk v ∈ spa A} : (⟨mk v, hv⟩ : spa A) ∈ basic_open r s ↔ v r ≤ v s ∧ v s ≠ 0 := begin apply and_congr, { apply out_mk, }, { apply (out_mk v).ne_zero, }, end lemma basic_open_eq (s : A) : basic_open s s = {v | v s ≠ 0} := set.ext $ λ v, ⟨λ h, h.right, λ h, ⟨le_refl _, h⟩⟩ -- should only be applied with (Hfin : fintype T) and (Hopen: is_open (span T)) definition rational_open (s : A) (T : set A) : set (spa A) := {v | (∀ t ∈ T, (v t ≤ v s)) ∧ (v s ≠ 0)} -- Here's everything in one package. structure rational_open_data (A : Huber_pair) := (s : A) (T : set A) (Tfin : fintype T) (Hopen : is_open ((ideal.span T) : set A)) instance (r : rational_open_data A) : fintype r.T := r.Tfin namespace rational_open_data def ext (r₁ r₂ : rational_open_data A) (hs : r₁.s = r₂.s) (hT : r₁.T = r₂.T) : r₁ = r₂ := begin cases r₁, cases r₂, congr; assumption end def rational_open (r : rational_open_data A) : set (spa A) := rational_open r.s r.T def localization (r : rational_open_data A) := Huber_ring.away r.T r.s instance ring_with_zero_nhd_of_localization (r : rational_open_data A) : ring_with_zero_nhd (localization r) := Huber_ring.away.ring_with_nhds r.T r.s r.Hopen instance (r : rational_open_data A) : comm_ring (localization r) := by unfold localization; apply_instance instance (r : rational_open_data A) : topological_space (localization r) := ring_with_zero_nhd.topological_space _ instance (r : rational_open_data A) : topological_ring (localization r) := ring_with_zero_nhd.is_topological_ring _ open algebra instance (r : rational_open_data A) : algebra A (localization r) := Huber_ring.away.algebra r.T r.s /- In this file, we are going to take a projective limit over a preordered set of rings, to make a presheaf. The underlying type of this preorder is `rational_open_data A`. The correct preorder on rational open data: def correct_preorder : preorder (rational_open_data A) := { le := λ r1 r2, rational_open r1 ⊆ rational_open r2, le_refl := λ _ _, id, le_trans := λ _ _ _, subset.trans, } One can prove (in maths) that r1 ≤ r2 iff there's a continuous R-algebra morphism of Huber pairs localization r2 → localization r1. I think the ← direction of this iff is straightforward (but I didn't think about it too carefully). However we definitely cannot prove the → direction of this iff in this repo yet because we don't have enough API for cont. Here is an indication of part of the problem. localization r2 is just A[1/r2.s]. But we cannot prove yet r2.s is invertible in localization.r1, even though we know it doesn't canish anywhere on rational_open r2 and hence on rational_open r1, because the fact that it doesn't vanish anywhere on rational_open r1 only means that it's not in any prime ideal corresponding to a *continuous* valuation on localization r1 which is bounded by 1 on some + subring; one would now need to prove, at least, that every maximal ideal is the support of a continuous valuation, which is Wedhorn 7.52(2). This is not too bad -- but it is work that we have not yet done. However this is by no means the whole story; we would also need that r1.T is power-bounded in localization.r2 and this looks much worse: it's Wedhorn 7.52(1). Everything is do-able, but it's just *long*. Long as in "thousands more lines of code". We will need a good theory of primary and secondary specialisation of valuations and so on and so on. None of this is there at the time of writing, although I see no obstruction to putting it there, other than the fact that it would take weeks of work. We have to work with a weaker preorder then, because haven't made a good enough API for continuous valuations. We basically work with the preorder r1 ≤ r2 iff there's a continuous R-algebra map localization r2 → localization r1, i.e, we define our way around the problem. We are fortunate in that we can prove (in maths) that the projective limit over this preorder agrees with the projective limit over the correct preorder. -/ -- note: I don't think we ever use le_refl or le_trans. I only proved them to -- validate the paper calculation I did which proves that the limit over these things -- equals the limit over the things we'd rather be taking a limit over. instance : preorder (rational_open_data A) := { le := λ r1 r2, ∃ k : A, r1.s * k = r2.s ∧ ∀ t₁ ∈ r1.T, ∃ t₂ ∈ r2.T, ∃ N : ℕ, r2.s ^ N * t₂ = r2.s ^ N * (t₁ * k), le_refl := λ r, ⟨1, mul_one _, λ t ht, ⟨t, ht, 0, by rw mul_one⟩⟩, le_trans := λ a b c ⟨k, hk, hab⟩ ⟨l, hl, hbc⟩, ⟨k * l, by rw [←mul_assoc, hk, hl], λ ta hta, begin rcases hab ta hta with ⟨tb, htb, Nab, h1⟩, rcases hbc tb htb with ⟨hc, htc, Nbc, h2⟩, use hc, use htc, use (Nab + Nbc), rw [←mul_assoc, pow_add, mul_assoc, h2, ←hl, mul_pow, mul_pow], rw (show b.s ^ Nab * l ^ Nab * (b.s ^ Nbc * l ^ Nbc * (tb * l)) = b.s ^ Nab * tb * (l ^ Nab * (b.s ^ Nbc * l ^ Nbc * l)), by ring), rw h1, ring end⟩ } -- our preorder is weaker than the preorder we're supposed to have but don't. However -- the projective limit we take over our preorder is provably (in maths) equal to -- the projective limit that we cannot even formalise. The thing we definitely need -- is that if r1 ≤ r2 then there's a map localization r1 → localization r2 /-- This awful function produces r1.s as a unit in localization r2 -/ noncomputable def s_inv_aux (r1 r2 : rational_open_data A) (h : r1 ≤ r2) : units (localization r2) := @units.unit_of_mul_left_eq_unit _ _ ((of_id A (localization r2)).to_fun r1.s) ((of_id A (localization r2)).to_fun (classical.some h)) (localization.to_units (⟨r2.s, 1, by simp⟩ : powers r2.s)) (begin suffices : (of_id A (localization r2)).to_fun (r1.s * classical.some h) = (localization.to_units (⟨r2.s, 1, by simp⟩ : powers r2.s)).val, convert this, convert (is_ring_hom.map_mul _).symm, apply_instance, -- stupid type class inference rw (classical.some_spec h).1, refl, end) /-- The map A(T1/s1) -> A(T2/s2) coming from the inequality r1 ≤ r2 -/ noncomputable def localization_map {r1 r2 : rational_open_data A} (h : r1 ≤ r2) : localization r1 → localization r2 := Huber_ring.away.lift r1.T r1.s ( show ((s_inv_aux r1 r2 h)⁻¹).inv = (of_id A (localization r2)).to_fun r1.s, from rfl) instance {r1 r2 : rational_open_data A} (h : r1 ≤ r2) : is_ring_hom (localization_map h) := by delta localization_map; apply_instance lemma localization.nonarchimedean (r : rational_open_data A) : topological_add_group.nonarchimedean (localization r) := @is_subgroups_basis.nonarchimedean _ _ _ _ _ (Huber_ring.away.is_basis _ _ _) section open localization submodule Huber_ring.away local attribute [instance] set.pointwise_mul_comm_semiring local attribute [instance] set.mul_action def localization.power_bounded_data (r : rational_open_data A) : set (localization r) := let s_inv : localization r := ((to_units ⟨r.s, ⟨1, by simp⟩⟩)⁻¹ : units (localization r)) in (s_inv • of_id A (localization r) '' r.T) set_option class.instance_max_depth 50 theorem localization.power_bounded (r : rational_open_data A) : is_power_bounded_subset (localization.power_bounded_data r) := begin haveI := Huber_ring.away.is_basis r.T r.s r.Hopen, apply bounded.subset, work_on_goal 0 { apply add_group.subset_closure }, show is_bounded (ring.closure (localization.power_bounded_data r)), intros U hU, rw is_subgroups_basis.nhds_zero at hU, cases hU with V hV, refine ⟨_, mem_nhds_sets (is_subgroups_basis.is_op _ V) _, _⟩, { rw submodule.mem_coe, exact submodule.zero_mem _ }, { intros v hv b hb, apply hV, rw mul_comm, rw submodule.mem_coe at hv ⊢, convert submodule.smul_mem _ _ hv, work_on_goal 1 { exact ⟨b, hb⟩ }, refl } end end -- section -- To prove continuity of the localisation map coming from r1 ≤ r2 I need to check -- that the image of T/s in the r1 ring is power-bounded in the r2 ring. This is -- this lemma. lemma localization_map_is_cts_aux {r1 r2 : rational_open_data A} (h : r1 ≤ r2) : is_power_bounded_subset ((s_inv_aux r1 r2 h)⁻¹.val • (λ (x : ↥A), to_fun (localization r2) x) '' r1.T) := begin refine power_bounded.subset _ (localization.power_bounded r2), intros x hx, rcases hx with ⟨y, hy, hz, ⟨t₁, ht₁, rfl⟩, rfl⟩, rw mem_singleton_iff at hy, rw hy, clear hy, clear y, let h' := h, -- need it later rcases h with ⟨a, ha, h₂⟩, rcases h₂ t₁ ht₁ with ⟨t₂, ht₂, N, hN⟩, show ↑(s_inv_aux r1 r2 _)⁻¹ * to_fun (localization r2) t₁ ∈ localization.mk 1 ⟨r2.s, _⟩ • (of_id ↥A (localization r2)).to_fun '' r2.T, rw mem_smul_set, use (of_id ↥A (localization r2)).to_fun t₂, existsi _, swap, rw mem_image, use t₂, use ht₂, rw [←units.mul_left_inj (s_inv_aux r1 r2 h'), units.mul_inv_cancel_left], show to_fun (localization r2) t₁ = to_fun (localization r2) (r1.s) * (localization.mk 1 ⟨r2.s, _⟩ * to_fun (localization r2) t₂), rw [mul_comm, mul_assoc], rw ←units.mul_left_inj (localization.to_units (⟨r2.s, 1, by simp⟩ : powers r2.s)), rw ←mul_assoc, -- t1=s1*(1/s2 * t2) in r2 have : ↑(localization.to_units (⟨r2.s, 1, by simp⟩ : powers r2.s)) * localization.mk (1 : A) (⟨r2.s, 1, by simp⟩ : powers r2.s) = 1, convert units.mul_inv _, rw [this, one_mul], clear this, show to_fun (localization r2) r2.s * _ = _, rw ←units.mul_left_inj (localization.to_units (⟨r2.s ^ N, N, rfl⟩ : powers r2.s)), show to_fun (localization r2) (r2.s ^ N) * _ = to_fun (localization r2) (r2.s ^ N) * _, have hrh : is_ring_hom (to_fun (localization r2)) := begin change is_ring_hom ((of_id ↥A (localization r2)).to_fun), apply_instance, end, rw ←@is_ring_hom.map_mul _ _ _ _ (to_fun (localization r2)) hrh, rw ←@is_ring_hom.map_mul _ _ _ _ (to_fun (localization r2)) hrh, rw ←@is_ring_hom.map_mul _ _ _ _ (to_fun (localization r2)) hrh, rw ←@is_ring_hom.map_mul _ _ _ _ (to_fun (localization r2)) hrh, congr' 1, rw [←mul_assoc _ t₂, hN], rw ←ha, ring, end -- Continuity now follows from the universal property. lemma localization_map_is_cts {r1 r2 : rational_open_data A} (h : r1 ≤ r2) : continuous (localization_map h) := Huber_ring.away.lift_continuous r1.T r1.s (localization.nonarchimedean r2) (Huber_ring.away.of_continuous r2.T r2.s (show ((localization.to_units (⟨r2.s, 1, by simp⟩ : powers r2.s))⁻¹ : units (localization r2)).inv = (of_id A (localization r2)).to_fun r2.s, from rfl) r2.Hopen) _ _ (localization_map_is_cts_aux h) noncomputable def insert_s (r : rational_open_data A) : rational_open_data A := { s := r.s, T := insert r.s r.T, Tfin := set.fintype_insert r.s r.T, -- noncomputable! Hopen := submodule.is_open_of_open_submodule ⟨ideal.span (r.T), r.Hopen, ideal.span_mono $ set.subset_insert _ _⟩ } end rational_open_data -- namespace lemma mk_mem_rational_open {s : A} {T : set A} {v : valuation A Γ} {hv : mk v ∈ spa A} : (⟨mk v, hv⟩ : spa A) ∈ rational_open s T ↔ (∀ t ∈ T, (v t ≤ v s)) ∧ (v s ≠ 0) := begin apply and_congr, { apply forall_congr, intro t, apply forall_congr, intro ht, apply out_mk }, { apply (out_mk v).ne_zero } end definition rational_open_bInter (s : A) (T : set A) : rational_open s T = (⋂ t ∈ T, basic_open t s) ∩ {v | v s ≠ 0} := begin ext v, split; rintros ⟨h₁, h₂⟩; split; try { exact h₂ }, { erw set.mem_bInter_iff, intros t ht, split, { exact h₁ t ht, }, { exact h₂ } }, { intros t ht, erw set.mem_bInter_iff at h₁, exact (h₁ t ht).1 } end lemma rational_open_add_s (s : A) (T : set A) : rational_open s T = rational_open s (insert s T) := begin ext v, split; rintros ⟨h₁, h₂⟩; split; try { exact h₂ }; intros t ht, { cases ht, { rw ht, exact le_refl _ }, { exact h₁ t ht } }, { apply h₁ t, exact mem_insert_of_mem _ ht } end namespace rational_open_data lemma insert_s_rational_open (r : rational_open_data A) : (insert_s r).rational_open = r.rational_open := (rational_open_add_s r.s r.T).symm lemma mem_insert_s (r : rational_open_data A) : r.s ∈ (insert_s r).T := by {left, refl} end rational_open_data instance (A : Huber_pair) : topological_space (spa A) := topological_space.generate_from {U : set (spa A) | ∃ r : rational_open_data A, U = r.rational_open} lemma rational_open_inter.aux₁ {s₁ s₂ : A} {T₁ T₂ : set A} (h₁ : s₁ ∈ T₁) (h₂ : s₂ ∈ T₂) : rational_open s₁ T₁ ∩ rational_open s₂ T₂ ⊆ rational_open (s₁ * s₂) (T₁ * T₂) := begin rintros v ⟨⟨hv₁, hs₁⟩, ⟨hv₂, hs₂⟩⟩, split, { rintros t ⟨t₁, ht₁, t₂, ht₂, rfl⟩, convert le_trans (linear_ordered_comm_monoid.mul_le_mul_right (hv₁ t₁ ht₁) _) (linear_ordered_comm_monoid.mul_le_mul_left (hv₂ t₂ ht₂) _); apply valuation.map_mul }, { rw with_zero.ne_zero_iff_exists at hs₁ hs₂, cases hs₁ with γ₁ hγ₁, cases hs₂ with γ₂ hγ₂, erw [valuation.map_mul, hγ₁, hγ₂], exact with_zero.coe_ne_zero }, end lemma rational_open_inter.aux₂ {s₁ s₂ : A} {T₁ T₂ : set A} (h₁ : s₁ ∈ T₁) (h₂ : s₂ ∈ T₂) : rational_open (s₁ * s₂) (T₁ * T₂) ⊆ rational_open s₁ T₁ ∩ rational_open s₂ T₂ := begin rintros v ⟨hv, hs⟩, have vmuls : v (s₁ * s₂) = v s₁ * v s₂ := valuation.map_mul _ _ _, have hs₁ : v s₁ ≠ 0 := λ H, by simpa [-coe_fn_coe_base, vmuls, H] using hs, have hs₂ : v s₂ ≠ 0 := λ H, by simpa [-coe_fn_coe_base, vmuls, H] using hs, split; split; try { assumption }; intros t ht; rw with_zero.ne_zero_iff_exists at hs₁ hs₂, { suffices H : v t * v s₂ ≤ v s₁ * v s₂, { cases hs₂ with γ hγ, rw hγ at H, have := linear_ordered_comm_monoid.mul_le_mul_right H γ⁻¹, simp [mul_assoc, -coe_fn_coe_base] at this, erw [mul_one, mul_one] at this, exact this }, { erw [← valuation.map_mul, ← valuation.map_mul], exact hv (t * s₂) ⟨t, ht, s₂, h₂, rfl⟩, } }, { suffices H : v s₁ * v t ≤ v s₁ * v s₂, { cases hs₁ with γ hγ, rw hγ at H, have := linear_ordered_comm_monoid.mul_le_mul_left H γ⁻¹, erw [← mul_assoc, ← mul_assoc] at this, simp [-coe_fn_coe_base] at this, erw [one_mul, one_mul] at this, exact this }, { erw [← valuation.map_mul, ← valuation.map_mul], exact hv _ ⟨s₁, h₁, t, ht, rfl⟩ } }, end lemma rational_open_inter {s₁ s₂ : A} {T₁ T₂ : set A} (h₁ : s₁ ∈ T₁) (h₂ : s₂ ∈ T₂) : rational_open s₁ T₁ ∩ rational_open s₂ T₂ = rational_open (s₁ * s₂) (T₁ * T₂) := le_antisymm (rational_open_inter.aux₁ h₁ h₂) (rational_open_inter.aux₂ h₁ h₂) @[simp] lemma rational_open_singleton {r s : A} : rational_open s {r} = basic_open r s := begin apply le_antisymm; rintros v ⟨h₁, h₂⟩; split; intros; simp [*] at *, end @[simp] lemma basic_open_eq_univ : basic_open (1 : A) (1 : A) = univ := univ_subset_iff.1 $ λ v h, ⟨le_refl _,by erw valuation.map_one; exact one_ne_zero⟩ @[simp] lemma rational_open_eq_univ : rational_open (1 : A) {(1 : A)} = univ := by simp def rational_basis (A : Huber_pair) : set (set (spa A)) := {U : set (spa A) | ∃ r : rational_open_data A, U = r.rational_open } section open algebra lattice lemma rational_basis.is_basis.mul (T₁ T₂ : set A) (h₁ : is_open (↑(ideal.span T₁) : set A)) (h₂ : is_open (↑(ideal.span T₂) : set A)) : is_open (↑(ideal.span (T₁ * T₂)) : set A) := begin rcases Huber_ring.exists_pod_subset _ (mem_nhds_sets h₁ $ ideal.zero_mem $ ideal.span T₁) with ⟨A₀, _, _, _, ⟨_, emb, hf, I, fg, top⟩, hI⟩, dsimp only at hI, resetI, rw is_ideal_adic_iff at top, cases top.2 (algebra_map A ⁻¹' ↑(ideal.span T₂)) _ with n hn, { apply submodule.is_open_of_open_submodule, use ideal.map (of_id A₀ A) (I^(n+1)), refine ⟨is_open_ideal_map_open_embedding emb hf _ (top.1 (n+1)), _⟩, delta ideal.span, erw [pow_succ, ideal.map_mul, ← submodule.span_mul_span], apply submodule.mul_le_mul, { exact (ideal.span_le.mpr hI) }, { rw ← image_subset_iff at hn, exact (ideal.span_le.mpr hn) } }, { apply emb.continuous.tendsto, rw show algebra.to_fun A (0:A₀) = 0, { apply is_ring_hom.map_zero }, exact (mem_nhds_sets h₂ $ ideal.zero_mem $ ideal.span T₂) } end end namespace rational_open_data noncomputable def inter_aux (r1 r2 : rational_open_data A) : rational_open_data A := { s := r1.s * r2.s, T := r1.T * r2.T, Tfin := by apply_instance, Hopen := rational_basis.is_basis.mul r1.T r2.T r1.Hopen r2.Hopen } noncomputable def inter (r1 r2 : rational_open_data A) : rational_open_data A := inter_aux (rational_open_data.insert_s r1) (rational_open_data.insert_s r2) lemma rational_open_data_inter (r1 r2 : rational_open_data A) : (inter r1 r2).rational_open = r1.rational_open ∩ r2.rational_open := begin rw ←insert_s_rational_open r1, rw ←insert_s_rational_open r2, exact (rational_open_inter (mem_insert_s r1) (mem_insert_s r2)).symm end lemma rational_open_data_le_inter_left (r1 r2 : rational_open_data A) : r1 ≤ (inter r1 r2) := begin use r2.s, split, refl, intros t1 ht1, use t1 * r2.s, existsi _, use 0, use t1, existsi _, use r2.s, existsi _, refl, exact mem_insert_s r2, right, assumption end lemma rational_open_data_le_inter_right (r1 r2 : rational_open_data A) : r2 ≤ (inter r1 r2) := begin use r1.s, split, apply mul_comm, intros t2 ht2, use t2 * r1.s, existsi _, use 0, use r1.s, existsi _, use t2, existsi _, apply mul_comm, right, assumption, exact mem_insert_s r1, end lemma rational_open_data_symm (r1 r2 : rational_open_data A) : inter r1 r2 = inter r2 r1 := begin cases r1, cases r2, unfold inter inter_aux, congr' 1, unfold insert_s, dsimp, exact mul_comm _ _, unfold insert_s, dsimp, exact mul_comm _ _, end end rational_open_data lemma rational_basis.is_basis.pow (T : set A) (hT : is_open (↑(ideal.span T) : set A)) (n : ℕ) : is_open (↑(ideal.span (T^n)) : set A) := begin induction n with n ih, { erw [pow_zero, ideal.span_singleton_one], exact is_open_univ }, { rw pow_succ, exact rational_basis.is_basis.mul _ _ hT ih } end variable (A) def rational_open_data.univ : rational_open_data A := { s := 1, T := {1}, Tfin := by apply_instance, Hopen := begin rw ideal.span_singleton_one, exact is_open_univ end } lemma rational_open_data_univ : (rational_open_data.univ A).rational_open = univ := begin apply subset.antisymm (subset_univ _), intros v hv, split, { intros t ht, erw mem_singleton_iff at ht, subst ht, exact le_refl _ }, { show v 1 ≠ 0, erw Spv.map_one, simp } end lemma rational_basis.is_basis : topological_space.is_topological_basis (rational_basis A) := begin refine ⟨_, _, rfl⟩, { rintros _ ⟨r₁, rfl⟩ _ ⟨r₂, rfl⟩ x hx, refine ⟨_, _, hx, subset.refl _⟩, { use rational_open_data.inter r₁ r₂, symmetry, apply rational_open_data.rational_open_data_inter } }, { apply subset.antisymm (subset_univ _), apply subset_sUnion_of_mem, exact ⟨_, (rational_open_data_univ A).symm⟩ } end variable {A} section open topological_space def rational_open_data_subsets (U : opens (spa A)) := { r : rational_open_data A // r.rational_open ⊆ U} def rational_open_data_subsets.map {U V : opens (spa A)} (hUV : U ≤ V) (rd : rational_open_data_subsets U) : rational_open_data_subsets V := ⟨rd.val, set.subset.trans rd.property hUV⟩ noncomputable def rational_open_data_subsets_inter {U : opens (spa A)} (r1 r2 : rational_open_data_subsets U) : rational_open_data_subsets U := ⟨rational_open_data.inter r1.1 r2.1, begin rw rational_open_data.rational_open_data_inter, refine set.subset.trans (inter_subset_left r1.1.rational_open r2.1.rational_open) _, exact r1.2 end⟩ lemma rational_open_data_subsets_symm {U : opens (spa A)} (r1 r2 : rational_open_data_subsets U) : rational_open_data_subsets_inter r1 r2 = rational_open_data_subsets_inter r2 r1 := begin rw subtype.ext, exact rational_open_data.rational_open_data_symm r1.1 r2.1 end instance (r : rational_open_data A) : uniform_space (rational_open_data.localization r) := topological_add_group.to_uniform_space _ instance (rd : rational_open_data A): uniform_add_group (rational_open_data.localization rd) := topological_add_group_is_uniform def localization_map_is_uniform_continuous {r1 r2 : rational_open_data A} (h : r1 ≤ r2) : uniform_continuous (rational_open_data.localization_map h) := uniform_continuous_of_continuous (rational_open_data.localization_map_is_cts h) end -- section -- r_o_d is short for "rational open data". KB needs to think more clearly -- about namespaces etc. /-- A<T/s>, the functions on D(T,s). A topological ring -/ def r_o_d_completion (r : rational_open_data A) := ring_completion (rational_open_data.localization r) namespace r_o_d_completion open topological_space noncomputable instance (r : rational_open_data A) : comm_ring (r_o_d_completion r) := by dunfold r_o_d_completion; apply_instance instance uniform_space (r : rational_open_data A) : uniform_space (r_o_d_completion r) := by dunfold r_o_d_completion; apply_instance -- example (r : rational_open_data A) : topological_space (r_o_d_completion r) := by apply_instance instance (r : rational_open_data A) : topological_ring (r_o_d_completion r) := by dunfold r_o_d_completion; apply_instance noncomputable def restriction {r1 r2 : rational_open_data A} (h : r1 ≤ r2) : r_o_d_completion r1 → r_o_d_completion r2 := ring_completion.map (rational_open_data.localization_map h) instance restriction_is_ring_hom {r1 r2 : rational_open_data A} (h : r1 ≤ r2) : is_ring_hom (restriction h) := by delta r_o_d_completion.restriction; exact ring_completion.map_is_ring_hom _ _ (rational_open_data.localization_map_is_cts h) lemma restriction_is_uniform_continuous {r1 r2 : rational_open_data A} (h : r1 ≤ r2) : uniform_continuous (r_o_d_completion.restriction h) := ring_completion.map_uniform_continuous $ localization_map_is_uniform_continuous h end r_o_d_completion -- namespace open topological_space /-- The underlying type of 𝒪_X(U), the structure presheaf on Spa(A) -/ def presheaf_value (U : opens (spa A)) := {f : Π (rd : rational_open_data_subsets U), r_o_d_completion rd.1 // ∀ (rd1 rd2 : rational_open_data_subsets U) (h : rd1.1 ≤ rd2.1), r_o_d_completion.restriction h (f rd1) = (f rd2)} -- agrees on overlaps def presheaf_value_set (U : opens (spa A)) := {f : Π (rd : rational_open_data_subsets U), r_o_d_completion rd.1 | ∀ (rd1 rd2 : rational_open_data_subsets U) (h : rd1.1 ≤ rd2.1), r_o_d_completion.restriction h (f rd1) = (f rd2)} -- We need to check it's a ring instance presheaf_subring (U : opens (spa A)) : is_subring (presheaf_value_set U) := begin refine {..}, { -- zero_mem intros rd₁ rd₂ h, exact is_ring_hom.map_zero _ }, { -- add_mem intros a b ha hb rd₁ rd₂ h, change r_o_d_completion.restriction h (a rd₁ + b rd₁) = a rd₂ + b rd₂, rw is_ring_hom.map_add (r_o_d_completion.restriction h), rw [ha _ _ h, hb _ _ h] }, { -- neg_mem intros a ha rd₁ rd₂ h, change r_o_d_completion.restriction h (-(a rd₁)) = -(a rd₂), rw is_ring_hom.map_neg (r_o_d_completion.restriction h), rw ha _ _ h }, { -- one_mem intros rd₁ rd₂ h, exact is_ring_hom.map_one _ }, { -- mul_mem intros a b ha hb rd₁ rd₂ h, change r_o_d_completion.restriction h (a rd₁ * b rd₁) = a rd₂ * b rd₂, rw is_ring_hom.map_mul (r_o_d_completion.restriction h), rw [ha _ _ h, hb _ _ h] } end noncomputable instance presheaf_comm_ring (U : opens (spa A)) : comm_ring (presheaf_value U) := begin apply @subset.comm_ring _ pi.comm_ring _ _, apply_instance, exact spa.presheaf_subring U end instance presheaf_top_space (U : opens (spa A)) : topological_space (presheaf_value U) := by unfold presheaf_value; apply_instance example (U : opens (spa A)) : topological_ring (Π (rd : rational_open_data_subsets U), r_o_d_completion (rd.1)) := by apply_instance -- tactic mode because I can't get Lean to behave. Note: switching to tactic -- mode indicated the problem was that Lean was not finding the two instances I flag -- with haveI and letI; probably now I know this one could try to go back into term mode. instance presheaf_top_ring (U : opens (spa A)) : topological_ring (presheaf_value U) := begin haveI := spa.presheaf_subring U, letI : topological_ring (Π (rd : rational_open_data_subsets U), r_o_d_completion (rd.1)) := by apply_instance, apply topological_subring (presheaf_value_set U), end instance (U : opens (spa A)) (r : rational_open_data_subsets U) : is_ring_hom (λ (f : presheaf_value U), f.val r) := { map_one := rfl, map_mul := λ _ _, rfl, map_add := λ _ _, rfl } def presheaf_map {U V : opens (spa A)} (hUV : U ≤ V) : presheaf_value V → presheaf_value U := λ f, ⟨λ rd, f.val ⟨rd.val, set.subset.trans rd.2 hUV⟩, begin intros, let X := f.2 (rational_open_data_subsets.map hUV rd1) (rational_open_data_subsets.map hUV rd2) h, exact X, end⟩ lemma presheaf_map_id (U : opens (spa A)) : presheaf_map (le_refl U) = id := by { delta presheaf_map, tidy } lemma presheaf_map_comp {U V W : opens (spa A)} (hUV : U ≤ V) (hVW : V ≤ W) : presheaf_map hUV ∘ presheaf_map hVW = presheaf_map (le_trans hUV hVW) := by { delta presheaf_map, tidy } instance presheaf_map_is_ring_hom {U V : opens (spa A)} (hUV : U ≤ V) : is_ring_hom (presheaf_map hUV) := { map_one := rfl, map_mul := λ _ _, rfl, map_add := λ _ _, rfl } def presheaf_map_cts {U V : opens (spa A)} (hUV : U ≤ V) : continuous (presheaf_map hUV) := continuous_subtype_mk _ (continuous_pi (λ i, (continuous.comp (continuous_subtype_val) (continuous_apply _)))) variable (A) noncomputable def presheaf_of_topological_rings : presheaf_of_topological_rings (spa A) := { F := presheaf_value, res := λ U V, presheaf_map, Hid := presheaf_map_id, Hcomp := λ U V W, presheaf_map_comp, Fring := spa.presheaf_comm_ring, res_is_ring_hom := λ U V, spa.presheaf_map_is_ring_hom, Ftop := spa.presheaf_top_space, Ftop_ring := spa.presheaf_top_ring, res_continuous := λ U V, presheaf_map_cts } end spa -- namespace I think -- old notes -- remember that a rational open is not actually `rational_open s T` in full -- generality -- we also need that T is finite and that T generates an open ideal in A. -- The construction on p73/74 (note typo in first line of p74 -- ideal should be I.D) -- gives A<T/s> (need completion) and A<T/s>^+ (need integral closure). -- KB idle comment: I guess we never make A<T/s> a Huber pair if A is a Huber pair? -- We would need integral closure for this and I don't think we have it in mathlib. -- We see mid way through p75 that the definition of the presheaf -- on V is proj lim of O_X(U) as U runs through rationals opens in V. This gets -- the projective limit topology and then we have a presheaf (hopefully this is -- straightforward) of complete topological rings (need proj lim of complete is complete)
3125d6001d9913e3c9b278be3b9578aebaca5d0f
8034095e1be60c0b8f6559c39220bd537d1f9933
/lambda/unicode.lean
2e36eab4499061b6920b3b6fcd8aa9bfb08e8fa8
[]
no_license
teodorov/lambda
40c573e2dd268824641702d9f94cf61e019ae6c5
4dc4d595dd0ee20c59913ef0171fd33eb0d637a1
refs/heads/master
1,585,318,070,303
1,530,364,515,000
1,530,364,515,000
146,645,753
1
0
null
1,535,569,435,000
1,535,569,434,000
null
UTF-8
Lean
false
false
1,651
lean
import data.buffer system.io import data.bitvec namespace unicode private def utf_8_convert_bitvec : list (bitvec 8) → option (list char) -- 0xxxxxxx -- symbols from 0 to 7Fh | (⟨ff :: tl₁, _⟩ :: chars) := list.cons (char.of_nat (bitvec.bits_to_nat tl₁)) <$> utf_8_convert_bitvec chars -- 110xxxxx 10xxxxxx -- symbols from 80h to 7FFh | (⟨tt :: tt :: ff :: tl₁, _⟩ :: ⟨tt :: ff :: tl₂, _⟩ :: chars) := list.cons (char.of_nat (bitvec.bits_to_nat $ tl₁ ++ tl₂)) <$> utf_8_convert_bitvec chars -- 1110xxxx 10xxxxxx 10xxxxxx -- symbols from 800h to FFFFh | (⟨tt :: tt :: tt :: ff :: tl₁, _⟩ :: ⟨tt :: ff :: tl₂, _⟩ :: ⟨tt :: ff :: tl₃, _⟩ :: chars) := list.cons (char.of_nat (bitvec.bits_to_nat $ tl₁ ++ tl₂ ++ tl₃)) <$> utf_8_convert_bitvec chars -- 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx -- symbols from 10000h to 10FFFFh | (⟨tt :: tt :: tt :: tt :: ff :: tl₁, _⟩ :: ⟨tt :: ff :: tl₂, _⟩ :: ⟨tt :: ff :: tl₃, _⟩ :: ⟨tt :: ff :: tl₄, _⟩ :: chars) := list.cons (char.of_nat (bitvec.bits_to_nat $ tl₁ ++ tl₂ ++ tl₃ ++ tl₄)) <$> utf_8_convert_bitvec chars | [] := some [] | _ := none -- Default Lean’s convert from char_buffer to string is incorrect -- (when we get a string using get_line, for example), -- because it does not work correctly with Unicode and UTF-8, in particular. -- This is simple realisation of correct convert from char_buffer to string -- for UTF-8 encoding. def utf8_to_string (buff : char_buffer) : option string := list.as_string <$> utf_8_convert_bitvec (list.map (bitvec.of_nat 8 ∘ char.to_nat) buff.to_list) end unicode
a1c620ea75ab82779f34c6ce8b5599c10bb6e4a8
69d4931b605e11ca61881fc4f66db50a0a875e39
/src/data/multiset/range.lean
65dfa8ef19bc8d21803a83904b5e096445a40cb6
[ "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
1,018
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import data.multiset.basic import data.list.range open list nat namespace multiset /- range -/ /-- `range n` is the multiset lifted from the list `range n`, that is, the set `{0, 1, ..., n-1}`. -/ def range (n : ℕ) : multiset ℕ := range n @[simp] theorem range_zero : range 0 = 0 := rfl @[simp] theorem range_succ (n : ℕ) : range (succ n) = n ::ₘ range n := by rw [range, range_succ, ← coe_add, add_comm]; refl @[simp] theorem card_range (n : ℕ) : card (range n) = n := length_range _ theorem range_subset {m n : ℕ} : range m ⊆ range n ↔ m ≤ n := range_subset @[simp] theorem mem_range {m n : ℕ} : m ∈ range n ↔ m < n := mem_range @[simp] theorem not_mem_range_self {n : ℕ} : n ∉ range n := not_mem_range_self theorem self_mem_range_succ (n : ℕ) : n ∈ range (n + 1) := list.self_mem_range_succ n end multiset
2abfcdf68f6a95ba737939ef2007b66264ff2a54
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/category_theory/subobject/default.lean
ac3af1749199b89f0999ec2b2a0c42fc2d25ba16
[ "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
214
lean
import category_theory.subobject.mono_over import category_theory.subobject.basic import category_theory.subobject.factor_thru import category_theory.subobject.well_powered import category_theory.subobject.lattice
3b13dccd0cc977a570d05208ee4f10354749b6c7
67190c9aacc0cac64fb4463d93e84c696a5be896
/Lists of exercises/List 2/capitulo-04-LucasDomingues.lean
3ca2d729fc128670892ab03b9ffd68273b1fa524
[]
no_license
lucasresck/Discrete-Mathematics
ffbaf55943e7ce2c7bc50cef7e3ef66a0212f738
0a08081c5f393e5765259d3f1253c3a6dd043dac
refs/heads/master
1,596,627,857,734
1,573,411,500,000
1,573,411,500,000
212,489,764
0
0
null
null
null
null
UTF-8
Lean
false
false
1,374
lean
variables A B C D : Prop example : A ∧ (A → B) → B := assume h : A ∧ (A → B), show B, from (and.right h) (and.left h) example : A → ¬ (¬ A ∧ B) := assume h1 : A, show ¬ (¬ A ∧ B), from assume h2 : ¬ A ∧ B, have h3 : ¬ A, from and.left h2, show false, from h3 h1 example : ¬ (A ∧ B) → (A → ¬ B) := assume h1 : ¬ (A ∧ B), show A → ¬ B, from assume h2 : A, show ¬ B, from assume h3 : B, show false, from h1 (and.intro h2 h3) example (h₁ : A ∨ B) (h₂ : A → C) (h₃ : B → D) : C ∨ D := or.elim h₁ (assume h₄ : A, have h₆ : C, from h₂ h₄, show C ∨ D, from or.inl h₆) (assume h₅ : B, have h₇ : D, from h₃ h₅, show C ∨ D, from or.inr h₇) example (h : ¬ A ∧ ¬ B) : ¬ (A ∨ B) := assume h1 : A ∨ B, show false, from or.elim h1 (assume h2: A, show false, from h.left h2) (assume h2: B, show false, from h.right h2) example : ¬ (A ↔ ¬ A) := assume h1 : A ↔ ¬ A, show false, from have h4: ¬ A, from (assume h2 : A, have h3 : ¬ A, from iff.elim_left h1 h2, show false, from h3 h2), h4 (show A, from iff.elim_right h1 h4)
8b2920149521dc19343eaab10cf08cbd242e2cf6
08a8ee10652ba4f8592710ceb654b37e951d9082
/src/hott/types/pointed.lean
209be453f04007fc0a69b54cb1cb04a39f20ba91
[ "Apache-2.0" ]
permissive
felixwellen/hott3
e9f299c84d30a782a741c40d38741ec024d391fb
8ac87a2699ab94c23ea7984b4a5fbd5a7052575c
refs/heads/master
1,619,972,899,098
1,509,047,351,000
1,518,040,986,000
120,676,559
0
0
null
1,518,040,503,000
1,518,040,503,000
null
UTF-8
Lean
false
false
48,833
lean
/- Copyright (c) 2014-2016 Jakob von Raumer. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jakob von Raumer, Floris van Doorn Early library ported from Coq HoTT, but greatly extended since. The basic definitions are in init.pointed See also .pointed2 -/ /-.nat.basic-/ /-..prop_trunc-/ import ..arity ..prop_trunc .bool universes u u₁ u₂ u₃ u₄ --open eq prod sigma equiv option is_equiv bool unit sigma.ops sum algebra function namespace hott hott_theory open is_trunc nat hott.bool hott.is_equiv hott.equiv hott.sigma namespace pointed variables {A : Type _} {B : Type _} @[hott, instance] def pointed_loop (a : A) : pointed (a = a) := pointed.mk idp @[hott] def pointed_fun_closed (f : A → B) [H : pointed A] : pointed B := pointed.mk (f pt) @[hott, reducible] def loop (A : Type*) : Type* := pointed.mk' (point A = point A) @[hott, reducible] def loopn : ℕ → Type* → Type* | 0 X := X | (n+1) X := loop (loopn n X) notation `Ω` := loop notation `Ω[`:95 n:0 `]`:0 := loopn n @[hott] def is_trunc_pointed_MK (n : ℕ₋₂) {A : Type _} (a : A) [H : is_trunc n A] : is_trunc n (pointed.MK A a) := H @[hott, instance, priority 1100] def is_trunc_loop (A : Type*) (n : ℕ₋₂) [H : is_trunc (n.+1) A] : is_trunc n (Ω A) := is_trunc_eq _ _ _ @[hott] def loopn_zero_eq (A : Type*) : Ω[0] A = A := rfl @[hott] def loopn_succ_eq (k : ℕ) (A : Type*) : Ω[succ k] A = Ω (Ω[k] A) := rfl @[hott,reducible] def rfln {n : ℕ} {A : Type*} : Ω[n] A := pt @[hott,reducible] def refln (n : ℕ) (A : Type*) : Ω[n] A := Point _ @[hott] def refln_eq_refl (A : Type*) (n : ℕ) : rfln = rfl :> Ω[succ n] A := rfl @[hott] def loopn_space (A : Type _) [H : pointed A] (n : ℕ) : Type _ := Ω[n] (pointed.mk' A) @[hott] def loop_mul {k : ℕ} {A : Type*} (mul : A → A → A) : Ω[k] A → Ω[k] A → Ω[k] A := begin cases k with k, exact mul, exact concat end @[hott] def pType_eq {A B : Type*} (f : A ≃ B) (p : f pt = pt) : A = B := begin cases A with A a, cases B with B b, dsimp at f p, fapply apdt011 @pType.mk, { apply ua f }, { rwr [←cast_def, cast_ua, p] }, end @[hott] def pType_eq_elim {A B : Type*} (p : A = B :> Type*) : Σ(p : carrier A = carrier B :> Type _), Point A =[p; λX, X] Point B := by induction p; exact ⟨idp, idpo⟩ @[hott] protected def pType.sigma_char : pType.{u} ≃ Σ(X : Type u), X := begin fapply equiv.MK, { intro x, induction x with X x, exact ⟨X, x⟩}, { intro x, induction x with X x, exact pointed.MK X x}, { intro x, induction x with X x, reflexivity}, { intro x, induction x with X x, reflexivity}, end @[hott] def pType.eta_expand (A : Type*) : Type* := pointed.MK A pt @[hott] def add_point (A : Type _) : Type* := pointed.Mk (none : option A) postfix `₊`:(max+1) := add_point -- the inclusion A → A₊ is called "some", the extra point "pt" or "none" ("@none A") end pointed namespace pointed /- truncated pointed types -/ @[hott] def ptrunctype_eq {n : ℕ₋₂} {A B : n-Type*} (p : A = B :> Type _) (q : Point (A.to_pType) =[p; λX, X] Point (B.to_pType)) : A = B := begin induction A with A HA a, induction B with B HB b, dsimp at p q, induction q, refine @ap010 _ (is_trunc n A) _ _ _ (ptrunctype.mk A) _ a, exact is_prop.elim _ _ end @[hott] def ptrunctype_eq_of_pType_eq {n : ℕ₋₂} {A B : n-Type*} (p : A.to_pType = B.to_pType) : A = B := begin cases pType_eq_elim p with q r, exact ptrunctype_eq q r end @[hott, instance] def is_trunc_ptrunctype {n : ℕ₋₂} (A : n-Type*) : is_trunc n A := trunctype.struct A end pointed open pointed namespace pointed variables {A : pType.{u₁}} {B : pType.{u₂}} {C : pType.{u₃}} {D : pType.{u₄}} {f g h : A →* B} {P : A → Type _} {p₀ : P pt} {k k' l m : ppi P p₀} /- categorical properties of pointed maps -/ @[hott, refl] def pid (A : Type*) : A →* A := pmap.mk id idp @[hott, trans] def pcompose {A B C : Type*} (g : B →* C) (f : A →* B) : A →* C := pmap.mk (λa, g (f a)) (ap g (respect_pt f) ⬝ respect_pt g) infixr ` ∘* `:60 := pcompose @[hott, hsimp] def respect_comp_eq_con {A B C : Type*} (g : B →* C) (f : A →* B) : respect_pt (g ∘* f) = ap g (respect_pt f) ⬝ (respect_pt g) := refl _ @[hott] def pmap_of_map {A B : Type _} (f : A → B) (a : A) : pointed.MK A a →* pointed.MK B (f a) := pmap.mk f idp @[hott, hsimp] def respect_pt_pcompose {A B C : Type*} (g : B →* C) (f : A →* B) : respect_pt (g ∘* f) = ap g (respect_pt f) ⬝ respect_pt g := idp @[hott] def passoc (h : C →* D) (g : B →* C) (f : A →* B) : (h ∘* g) ∘* f ~* h ∘* (g ∘* f) := phomotopy.mk (λa, idp) begin abstract { refine (idp_con _ ⬝ whisker_right _ (ap_con _ _ _ ⬝ whisker_right _ _) ⬝ (con.assoc _ _ _)), exact ap_compose' h g (respect_pt f) } end @[hott] def pid_pcompose (f : A →* B) : pid B ∘* f ~* f := begin fapply phomotopy.mk, { intro a, reflexivity}, { reflexivity} end @[hott] def pcompose_pid (f : A →* B) : f ∘* pid A ~* f := begin fapply phomotopy.mk, { intro a, reflexivity}, { reflexivity} end /- equivalences and equalities -/ @[hott] protected def ppi.sigma_char {A : Type*} (B : A → Type _) (b₀ : B pt) : ppi B b₀ ≃ Σ(k : Πa, B a), k pt = b₀ := begin fapply equiv.MK; all_goals {intro x}, { constructor, exact respect_pt x }, { induction x with f p, constructor, exact p }, { induction x, reflexivity }, { induction x, reflexivity } end @[hott] def pmap.sigma_char {A B : Type*} : (A →* B) ≃ Σ(f : A → B), f pt = pt := ppi.sigma_char _ _ @[hott] def pmap.eta_expand {A B : Type*} (f : A →* B) : A →* B := pmap.mk f (respect_pt f) @[hott] def pmap_equiv_right (A : Type*) (B : Type _) : (Σ(b : B), A →* (pointed.Mk b)) ≃ (A → B) := begin fapply equiv.MK, { intros u a, exact pmap.to_fun u.2 a}, { intro f, refine ⟨f pt, _⟩, fapply pmap.mk, intro a, exact f a, reflexivity}, { intro f, reflexivity}, { intro u, cases u with b f, cases f with f p, dsimp at f p, induction p, reflexivity} end /- some specific pointed maps -/ -- The constant pointed map between any two types @[hott] def pconst (A B : Type*) : A →* B := ppi_const _ -- the pointed type of pointed maps -- TODO: remove @[hott] def ppmap (A B : Type*) : Type* := @pppi A (λa, B) @[hott] def pcast {A B : Type*} (p : A = B) : A →* B := pmap.mk (cast (ap pType.carrier p)) (by induction p; reflexivity) @[hott] def pinverse (X : Type*) : Ω X →* Ω X := pmap.mk eq.inverse idp /- we generalize the @[hott] def of ap1 to arbitrary paths, so that we can prove properties about it using path induction (see for example ap1_gen_con and ap1_gen_con_natural) -/ @[hott, reducible] def ap1_gen {A B : Type _} (f : A → B) {a a' : A} {b b' : B} (q : f a = b) (q' : f a' = b') (p : a = a') : b = b' := q⁻¹ ⬝ ap f p ⬝ q' @[hott] def ap1_gen_idp {A B : Type _} (f : A → B) {a : A} {b : B} (q : f a = b) : ap1_gen f q q idp = idp := con.left_inv q @[hott] def ap1_gen_idp_left {A B : Type _} (f : A → B) {a a' : A} (p : a = a') : ap1_gen f idp idp p = ap f p := idp_con (ap f p) @[hott] def ap1_gen_idp_left_con {A B : Type _} (f : A → B) {a : A} (p : a = a) (q : ap f p = idp) : ap1_gen_idp_left f p ⬝ q = ap (concat idp) q := idp_con_idp q @[hott] def ap1 (f : A →* B) : Ω A →* Ω B := pmap.mk (λp, ap1_gen f (respect_pt f) (respect_pt f) p) (ap1_gen_idp f (respect_pt f)) @[hott] def apn (n : ℕ) (f : A →* B) : Ω[n] A →* Ω[n] B := begin induction n with n IH, { exact f }, { exact ap1 IH } end notation `Ω→`:(max+5) := ap1 notation `Ω→[`:95 n:0 `]`:0 := apn n @[hott] def ptransport {A : Type _} (B : A → Type*) {a a' : A} (p : a = a') : B a →* B a' := pmap.mk (transport _ p) (apdt (λa, Point (B a)) p) @[hott] def pmap_of_eq_pt {A : Type _} {a a' : A} (p : a = a') : pointed.MK A a →* pointed.MK A a' := pmap.mk id p @[hott] def pbool_pmap {A : Type*} (a : A) : pbool →* A := pmap.mk (λb, bool.rec pt a b) idp /- properties of pointed maps -/ @[hott] def apn_zero (f : A →* B) : Ω→[0] f = f := idp @[hott] def apn_succ (n : ℕ) (f : A →* B) : Ω→[n + 1] f = Ω→ (Ω→[n] f) := idp @[hott] def ap1_gen_con {A B : Type _} (f : A → B) {a₁ a₂ a₃ : A} {b₁ b₂ b₃ : B} (q₁ : f a₁ = b₁) (q₂ : f a₂ = b₂) (q₃ : f a₃ = b₃) (p₁ : a₁ = a₂) (p₂ : a₂ = a₃) : ap1_gen f q₁ q₃ (p₁ ⬝ p₂) = ap1_gen f q₁ q₂ p₁ ⬝ ap1_gen f q₂ q₃ p₂ := begin induction p₂, induction q₃, induction q₂, reflexivity end @[hott] def ap1_gen_inv {A B : Type _} (f : A → B) {a₁ a₂ : A} {b₁ b₂ : B} (q₁ : f a₁ = b₁) (q₂ : f a₂ = b₂) (p₁ : a₁ = a₂) : ap1_gen f q₂ q₁ p₁⁻¹ = (ap1_gen f q₁ q₂ p₁)⁻¹ := begin induction p₁, induction q₁, induction q₂, reflexivity end @[hott] def ap1_con {A B : Type*} (f : A →* B) (p q : Ω A) : ap1 f (p ⬝ q) = ap1 f p ⬝ ap1 f q := ap1_gen_con f (respect_pt f) (respect_pt f) (respect_pt f) p q @[hott] def ap1_inv (f : A →* B) (p : Ω A) : ap1 f p⁻¹ = (ap1 f p)⁻¹ := ap1_gen_inv f (respect_pt f) (respect_pt f) p -- the following two facts are used for the suspension axiom to define spectrum cohomology @[hott] def ap1_gen_con_natural {A B : Type _} (f : A → B) {a₁ a₂ a₃ : A} {p₁ p₁' : a₁ = a₂} {p₂ p₂' : a₂ = a₃} {b₁ b₂ b₃ : B} (q₁ : f a₁ = b₁) (q₂ : f a₂ = b₂) (q₃ : f a₃ = b₃) (r₁ : p₁ = p₁') (r₂ : p₂ = p₂') : square (ap1_gen_con f q₁ q₂ q₃ p₁ p₂) (ap1_gen_con f q₁ q₂ q₃ p₁' p₂') (ap (ap1_gen f q₁ q₃) (r₁ ◾ r₂)) (ap (ap1_gen f q₁ q₂) r₁ ◾ ap (ap1_gen f q₂ q₃) r₂) := begin induction r₁, induction r₂, exact vrfl end @[hott] def ap1_gen_con_idp {A B : Type _} (f : A → B) {a : A} {b : B} (q : f a = b) : ap1_gen_con f q q q idp idp ⬝ con.left_inv q ◾ con.left_inv q = con.left_inv q := by induction q; reflexivity @[hott] def apn_con (n : ℕ) (f : A →* B) (p q : Ω[succ n] A) : (Ω→[succ n] f) (p ⬝ q) = (Ω→[succ n] f) p ⬝ (Ω→[succ n] f) q := ap1_con (Ω→[n] f) p q @[hott] def apn_inv (n : ℕ) (f : A →* B) (p : Ω[succ n] A) : Ω→[succ n] f p⁻¹ᵖ = (Ω→[succ n] f p)⁻¹ᵖ := ap1_inv (Ω→[n] f) p @[hott] def is_equiv_ap1 (f : A →* B) [H : is_equiv f] : is_equiv (ap1 f) := begin induction B with B b, induction f with f pf, dsimp at f pf H, induction pf, apply is_equiv.homotopy_closed (ap f), intro p, exact (idp_con _)⁻¹, apply_instance end @[hott] def is_equiv_apn (n : ℕ) (f : A →* B) [H : is_equiv f] : is_equiv (Ω→[n] f) := begin induction n with n IH, { exact H }, { exact @is_equiv_ap1 _ _ (Ω→[n] f) IH } end @[hott] def pinverse_con {X : Type*} (p q : Ω X) : pinverse X (p ⬝ q) = pinverse X q ⬝ pinverse X p := con_inv p q @[hott] def pinverse_inv {X : Type*} (p : Ω X) : pinverse X p⁻¹ = (pinverse X p)⁻¹ := idp @[hott] def ap1_pcompose_pinverse {X Y : Type*} (f : X →* Y) : Ω→ f ∘* pinverse X ~* pinverse Y ∘* Ω→ f := phomotopy.mk (ap1_gen_inv f (respect_pt f) (respect_pt f)) begin induction Y with Y y₀, induction f with f f₀, dsimp at f f₀, induction f₀, refl end @[hott, instance] def is_equiv_pcast {A B : Type*} (p : A = B) : is_equiv (pcast p) := is_equiv_cast _ /- categorical properties of pointed homotopies -/ variable (k) @[hott] protected def phomotopy.refl : k ~* k := phomotopy.mk homotopy.rfl (idp_con _) variable {k} @[hott, reducible, refl] protected def phomotopy.rfl : k ~* k := phomotopy.refl k @[hott, symm] protected def phomotopy.symm (p : k ~* l) : l ~* k := phomotopy.mk p⁻¹ʰᵗʸ (inv_con_eq_of_eq_con (to_homotopy_pt p)⁻¹) @[hott, trans] protected def phomotopy.trans (p : k ~* l) (q : l ~* m) : k ~* m := phomotopy.mk (λa, p a ⬝ q a) (con.assoc _ _ _ ⬝ whisker_left (p pt) (to_homotopy_pt q) ⬝ to_homotopy_pt p) infix ` ⬝* `:75 := phomotopy.trans postfix `⁻¹*`:(max+1) := phomotopy.symm /- equalities and equivalences relating pointed homotopies -/ @[hott, reducible, elab_as_eliminator] def phomotopy.rec' (B : k ~* l → Type _) (H : Π(h : k ~ l) (p : h pt ⬝ respect_pt l = respect_pt k), B (phomotopy.mk h p)) (h : k ~* l) : B h := begin induction h with h p, refine transport (λp, B (ppi.mk h p)) _ (H h (con_eq_of_eq_con_inv p)), apply (eq_con_inv_equiv_con_eq _ _ _).to_left_inv p end @[hott] def phomotopy.eta_expand (p : k ~* l) : k ~* l := phomotopy.mk p (to_homotopy_pt p) @[hott, instance] def is_trunc_ppi (n : ℕ₋₂) {A : Type*} (B : A → Type _) (b₀ : B pt) [Πa, is_trunc n (B a)] : is_trunc n (ppi B b₀) := is_trunc_equiv_closed_rev _ (ppi.sigma_char _ _) (by infer) @[hott, instance] def is_trunc_pmap (n : ℕ₋₂) (A B : Type*) [is_trunc n B] : is_trunc n (A →* B) := is_trunc_ppi _ _ _ @[hott, instance] def is_trunc_ppmap (n : ℕ₋₂) {A B : Type*} [is_trunc n B] : is_trunc n (ppmap A B) := is_trunc_pmap _ _ _ @[hott] def phomotopy_of_eq (p : k = l) : k ~* l := phomotopy.mk (ap010 ppi.to_fun p) begin induction p, refine !idp_con end @[hott] def phomotopy_of_eq_idp (k : ppi P p₀) : phomotopy_of_eq idp = phomotopy.refl k := idp @[hott] def pconcat_eq (p : k ~* l) (q : l = m) : k ~* m := p ⬝* phomotopy_of_eq q @[hott] def eq_pconcat (p : k = l) (q : l ~* m) : k ~* m := phomotopy_of_eq p ⬝* q infix ` ⬝*p `:75 := pconcat_eq infix ` ⬝p* `:75 := eq_pconcat @[hott] def fst_phomotopy_eq {p q : k ~* l} (r : p = q) (a : A) : p a = q a := ap010 to_homotopy r a @[hott] def pwhisker_left (h : B →* C) (p : f ~* g) : h ∘* f ~* h ∘* g := phomotopy.mk (λa, ap h (p a)) begin abstract {exact con.assoc' _ _ _ ⬝ whisker_right _ ((ap_con _ _ _)⁻¹ ⬝ ap02 _ (to_homotopy_pt p))} end @[hott] def pwhisker_right (h : C →* A) (p : f ~* g) : f ∘* h ~* g ∘* h := phomotopy.mk (λc, p (h c)) (by abstract {exact con.assoc' _ _ _ ⬝ whisker_right _ (ap_con_eq_con_ap _ _)⁻¹ ⬝ con.assoc _ _ _ ⬝ whisker_left _ (to_homotopy_pt p)}) @[hott] def pconcat2 {A B C : Type*} {h i : B →* C} {f g : A →* B} (q : h ~* i) (p : f ~* g) : h ∘* f ~* i ∘* g := pwhisker_left _ p ⬝* pwhisker_right _ q variables (k l) @[hott] def phomotopy.sigma_char : (k ~* l) ≃ Σ(p : k ~ l), p pt ⬝ respect_pt l = respect_pt k := begin fapply equiv.MK, all_goals {intros h}, { exact ⟨h , to_homotopy_pt h⟩ }, { cases h with h p, exact phomotopy.mk h p }, { cases h with h p, exact ap (dpair h) ((eq_con_inv_equiv_con_eq _ _ _).to_right_inv p) }, { refine phomotopy.rec' _ _ h, clear h, intros h p, exact (ap (phomotopy.mk h) $ (eq_con_inv_equiv_con_eq _ _ _).to_right_inv p) } end @[hott] def ppi_eq_equiv_internal : (k = l) ≃ (k ~* l) := calc (k = l) ≃ ppi.sigma_char P p₀ k = ppi.sigma_char P p₀ l : eq_equiv_fn_eq (ppi.sigma_char P p₀) k l ... ≃ Σ(p : k = l :> Πa, P a), respect_pt k =[p; λ(h : Πa, P a), h pt = p₀] respect_pt l : sigma_eq_equiv _ _ ... ≃ Σ(p : k = l :> Πa, P a), respect_pt k = ap (λ(h : Πa, P a), h pt) p ⬝ respect_pt l : sigma_equiv_sigma_right (λp, eq_pathover_equiv_Fl p (respect_pt k) (respect_pt l)) ... ≃ Σ(p : k = l :> Πa, P a), respect_pt k = apd10 p pt ⬝ respect_pt l : sigma_equiv_sigma_right (λp, equiv_eq_closed_right _ (whisker_right _ (ap_eq_apd10 p _))) ... ≃ Σ(p : k ~ l), respect_pt k = p pt ⬝ respect_pt l : sigma_equiv_sigma_left' (λ(p : k ~ l), respect_pt k = p pt ⬝ respect_pt l) (eq_equiv_homotopy k l) ... ≃ Σ(p : k ~ l), p pt ⬝ respect_pt l = respect_pt k : sigma_equiv_sigma_right (λp, eq_equiv_eq_symm _ _) ... ≃ (k ~* l) : (phomotopy.sigma_char k l)⁻¹ᵉ @[hott] def ppi_eq_equiv_internal_idp : ppi_eq_equiv_internal k k idp = phomotopy.refl k := begin --apply ap (phomotopy.mk (homotopy.refl _)), /- do we need this? -/ induction k with k k₀, induction k₀, reflexivity end @[hott] def ppi_eq_equiv : (k = l) ≃ (k ~* l) := begin refine equiv_change_fun (ppi_eq_equiv_internal k l) _, { apply phomotopy_of_eq }, { intro p, induction p, exact ppi_eq_equiv_internal_idp k } end variables {k l} @[hott] def pmap_eq_equiv (f g : A →* B) : (f = g) ≃ (f ~* g) := ppi_eq_equiv f g @[hott] def eq_of_phomotopy (p : k ~* l) : k = l := to_inv (ppi_eq_equiv k l) p @[hott] def eq_of_phomotopy_refl (k : ppi P p₀) : eq_of_phomotopy (phomotopy.refl k) = idpath k := begin apply to_inv_eq_of_eq, reflexivity end @[hott] def phomotopy_of_homotopy (h : k ~ l) [Πa, is_set (P a)] : k ~* l := begin fapply phomotopy.mk, { exact h }, { apply is_set.elim } end @[hott] def ppi_eq_of_homotopy [Πa, is_set (P a)] (p : k ~ l) : k = l := eq_of_phomotopy (phomotopy_of_homotopy p) @[hott] def pmap_eq_of_homotopy [is_set B] (p : f ~ g) : f = g := ppi_eq_of_homotopy p @[hott] def phomotopy_of_eq_of_phomotopy (p : k ~* l) : phomotopy_of_eq (eq_of_phomotopy p) = p := to_right_inv (ppi_eq_equiv k l) p @[hott, induction, reducible] def phomotopy_rec_eq {Q : (k ~* k') → Type _} (p : k ~* k') (H : Π(q : k = k'), Q (phomotopy_of_eq q)) : Q p := phomotopy_of_eq_of_phomotopy p ▸ H (eq_of_phomotopy p) @[hott, induction, reducible] def phomotopy_rec_idp {Q : Π {k' : ppi P p₀}, (k ~* k') → Type _} {k' : ppi P p₀} (H : k ~* k') (q : Q (phomotopy.refl k)) : Q H := begin hinduction H using phomotopy_rec_eq with t, induction t, exact phomotopy_of_eq_idp k ▸ q, end @[hott] def phomotopy_rec_idp' (Q : Π ⦃k' : ppi P p₀⦄, (k ~* k') → (k = k') → Type _) (q : Q phomotopy.rfl idp) ⦃k' : ppi P p₀⦄ (H : k ~* k') : Q H (eq_of_phomotopy H) := begin hinduction H using phomotopy_rec_idp, exact transport (Q phomotopy.rfl) (eq_of_phomotopy_refl _)⁻¹ q end @[hott] theorem phomotopy_rec_eq_phomotopy_of_eq {Q : (k ~* l) → Type _} (p : k = l) (H : Π(q : k = l), Q (phomotopy_of_eq q)) : phomotopy_rec_eq (phomotopy_of_eq p) H = H p := begin refine transport2 _ (adj (ppi_eq_equiv _ _).to_fun _) _ ⬝ _, refine tr_ap _ _ _ _ ⬝ _, apply apdt end @[hott] def phomotopy_rec_idp_refl {Q : Π{l}, (k ~* l) → Type _} (H : Q (phomotopy.refl k)) : phomotopy_rec_idp phomotopy.rfl H = H := begin apply phomotopy_rec_eq_phomotopy_of_eq idp end @[hott] def phomotopy_rec_idp'_refl (Q : Π ⦃k' : ppi P p₀⦄, (k ~* k') → (k = k') → Type _) (q : Q phomotopy.rfl idp) : phomotopy_rec_idp' Q q phomotopy.rfl = transport (Q phomotopy.rfl) (eq_of_phomotopy_refl _)⁻¹ q := begin dsimp [phomotopy_rec_idp'], exact phomotopy_rec_idp_refl _ end /- maps out of or into contractible types -/ @[hott] def phomotopy_of_is_contr_cod (k l : ppi P p₀) [Πa, is_contr (P a)] : k ~* l := phomotopy.mk (λa, eq_of_is_contr _ _) (eq_of_is_contr _ _) @[hott] def phomotopy_of_is_contr_cod_pmap (f g : A →* B) [is_contr B] : f ~* g := phomotopy_of_is_contr_cod f g @[hott] def phomotopy_of_is_contr_dom (k l : ppi P p₀) [is_contr A] : k ~* l := begin fapply phomotopy.mk, { hintro a, exact eq_of_pathover_idp (change_path (is_prop.elim _ _) (apd k (is_prop.elim _ _) ⬝op respect_pt k ⬝ (respect_pt l)⁻¹ ⬝o apd l (is_prop.elim _ _))) }, dsimp, rwr [is_prop_elim_self], dsimp, rwr [is_prop_elim_self, apd_idp, apd_idp], dsimp, rwr [idpo_concato_eq, inv_con_cancel_right], end /- adjunction between (-)₊ : Type _ → Type* and pType.carrier : Type* → Type _ -/ @[hott] def pmap_equiv_left (A : Type _) (B : Type*) : A₊ →* B ≃ (A → B) := begin fapply equiv.MK, { intros f a, cases f with f p, exact f (some a) }, { intro f, fconstructor, intro a, cases a, exact pt, exact f a, reflexivity }, { intro f, reflexivity }, { intro f, cases f with f p, fapply eq_of_phomotopy, fapply phomotopy.mk, { intro a, cases a, exact p⁻¹, refl }, { apply con.left_inv }}, end -- pmap_pbool_pequiv is the pointed equivalence @[hott] def pmap_pbool_equiv (B : Type*) : (pbool →* B) ≃ B := begin fapply equiv.MK, { intro f, cases f with f p, exact f tt }, { intro b, fconstructor, intro u, cases u, exact pt, exact b, reflexivity }, { intro b, reflexivity }, { intro f, cases f with f p, fapply eq_of_phomotopy, fapply phomotopy.mk, { intro a, cases a, exact p⁻¹, refl }, { apply con.left_inv }}, end /- Pointed maps respecting pointed homotopies. In general we need function extensionality for pap, but for particular F we can do it without function extensionality. This might be preferred, because such pointed homotopies compute. On the other hand, when using function extensionality, it's easier to prove that if p is reflexivity, then the resulting pointed homotopy is reflexivity -/ @[hott] def pap (F : (A →* B) → (C →* D)) {f g : A →* B} (p : f ~* g) : F f ~* F g := begin hinduction p using phomotopy_rec_idp, refl end @[hott] def pap_refl (F : (A →* B) → (C →* D)) (f : A →* B) : pap F (phomotopy.refl f) = phomotopy.refl (F f) := begin dsimp [pap], exact phomotopy_rec_idp_refl _ end @[hott] def ap1_phomotopy {f g : A →* B} (p : f ~* g) : Ω→ f ~* Ω→ g := pap Ω→ p @[hott] def ap1_phomotopy_refl {X Y : Type*} (f : X →* Y) : ap1_phomotopy (phomotopy.refl f) = phomotopy.refl (Ω→ f) := pap_refl _ _ --a proof not using function extensionality: @[hott] def ap1_phomotopy_explicit {f g : A →* B} (p : f ~* g) : Ω→ f ~* Ω→ g := begin induction p with p q, induction f with f pf, induction g with g pg, induction B with B b, dsimp at *, induction pg, dsimp [respect_pt] at *, induction q, fapply phomotopy.mk, { hintro l, refine _ ⬝ (idp_con _)⁻¹, refine con.assoc _ _ _ ⬝ _, exact inv_con_eq_of_eq_con (ap_con_eq_con_ap p l) }, { induction A with A a, dsimp [respect_pt, point, ap_con_eq_con_ap, ap1, pmap.mk, pppi.mk, ap1_gen_idp], hgeneralize : p a = q, revert q, clear p, hgeneralize : g a = b', intro q, induction q, reflexivity } end @[hott] def apn_phomotopy {f g : A →* B} (n : ℕ) (p : f ~* g) : apn n f ~* apn n g := begin induction n with n IH, { exact p}, { exact ap1_phomotopy IH} end -- the following two definitiongs are mostly the same, maybe we should remove one @[hott] def ap_eq_of_phomotopy {A B : Type*} {f g : A →* B} (p : f ~* g) (a : A) : ap (λf : A →* B, f a) (eq_of_phomotopy p) = p a := ap010 to_homotopy (phomotopy_of_eq_of_phomotopy p) a @[hott] def to_fun_eq_of_phomotopy {A B : Type*} {f g : A →* B} (p : f ~* g) (a : A) : ap010 pmap.to_fun (eq_of_phomotopy p) a = p a := begin hinduction p using phomotopy_rec_idp, exact ap (λx, ap010 pmap.to_fun x a) (eq_of_phomotopy_refl _) end @[hott] def ap1_eq_of_phomotopy {A B : Type*} {f g : A →* B} (p : f ~* g) : ap Ω→ (eq_of_phomotopy p) = eq_of_phomotopy (ap1_phomotopy p) := begin hinduction p using phomotopy_rec_idp, refine ap02 _ (eq_of_phomotopy_refl _) ⬝ (eq_of_phomotopy_refl _)⁻¹ ⬝ ap eq_of_phomotopy _, exact (ap1_phomotopy_refl _)⁻¹ end /- pointed homotopies between the given pointed maps -/ @[hott] def ap1_pid {A : Type*} : ap1 (pid A) ~* pid (Ω A) := begin fapply phomotopy.mk, { intro p, refine idp_con _ ⬝ ap_id _ }, { refl } end @[hott] def ap1_pinverse {A : Type*} : ap1 (@pinverse A) ~* @pinverse (Ω A) := begin fapply phomotopy.mk, { intro p, refine idp_con _ ⬝ _, exact (inv_eq_inv2 _)⁻¹ }, { refl } end @[hott] def ap1_gen_compose {A B C : Type _} (g : B → C) (f : A → B) {a₁ a₂ : A} {b₁ b₂ : B} {c₁ c₂ : C} (q₁ : f a₁ = b₁) (q₂ : f a₂ = b₂) (r₁ : g b₁ = c₁) (r₂ : g b₂ = c₂) (p : a₁ = a₂) : ap1_gen (g ∘ f) (ap g q₁ ⬝ r₁) (ap g q₂ ⬝ r₂) p = ap1_gen g r₁ r₂ (ap1_gen f q₁ q₂ p) := begin induction p, induction q₁, induction q₂, induction r₁, induction r₂, reflexivity end @[hott] def ap1_gen_compose_idp {A B C : Type _} (g : B → C) (f : A → B) {a : A} {b : B} {c : C} (q : f a = b) (r : g b = c) : ap1_gen_compose g f q q r r idp ⬝ (ap (ap1_gen g r r) (ap1_gen_idp f q) ⬝ ap1_gen_idp g r) = ap1_gen_idp (g ∘ f) (ap g q ⬝ r) := begin induction q, induction r, reflexivity end @[hott] def ap1_pcompose {A B C : Type*} (g : B →* C) (f : A →* B) : ap1 (g ∘* f) ~* ap1 g ∘* ap1 f := phomotopy.mk (ap1_gen_compose g f (respect_pt f) (respect_pt f) (respect_pt g) (respect_pt g)) (ap1_gen_compose_idp g f (respect_pt f) (respect_pt g)) @[hott] def ap1_pconst (A B : Type*) : Ω→(pconst A B) ~* pconst (Ω A) (Ω B) := phomotopy.mk (λp, ap1_gen_idp_left (const A pt) p ⬝ ap_constant p pt) rfl @[hott] def ap1_gen_con_left {A B : Type _} {a a' : A} {b₀ b₁ b₂ : B} {f : A → b₀ = b₁} {f' : A → b₁ = b₂} {q₀ q₁ : b₀ = b₁} {q₀' q₁' : b₁ = b₂} (r₀ : f a = q₀) (r₁ : f a' = q₁) (r₀' : f' a = q₀') (r₁' : f' a' = q₁') (p : a = a') : ap1_gen (λa, f a ⬝ f' a) (r₀ ◾ r₀') (r₁ ◾ r₁') p = whisker_right q₀' (ap1_gen f r₀ r₁ p) ⬝ whisker_left q₁ (ap1_gen f' r₀' r₁' p) := begin induction r₀, induction r₁, induction r₀', induction r₁', induction p, reflexivity end @[hott] def ap1_gen_con_left_idp {A B : Type _} {a : A} {b₀ b₁ b₂ : B} {f : A → b₀ = b₁} {f' : A → b₁ = b₂} {q₀ : b₀ = b₁} {q₁ : b₁ = b₂} (r₀ : f a = q₀) (r₁ : f' a = q₁) : ap1_gen_con_left r₀ r₀ r₁ r₁ idp = con.left_inv _ ⬝ (ap (whisker_right q₁) (con.left_inv _) ◾ ap (whisker_left _) (con.left_inv _))⁻¹ := begin induction r₀, induction r₁, reflexivity end @[hott] def ptransport_change_eq {A : Type _} (B : A → Type*) {a a' : A} {p q : a = a'} (r : p = q) : ptransport B p ~* ptransport B q := phomotopy.mk (λb, ap (λp, transport (λa, B a) p b) r) begin induction r, apply idp_con end @[hott] def pnatural_square {A B : Type _} (X : B → Type*) {f g : A → B} (h : Πa, X (f a) →* X (g a)) {a a' : A} (p : a = a') : h a' ∘* ptransport X (ap f p) ~* ptransport X (ap g p) ∘* h a := by induction p; exact pcompose_pid _ ⬝* (pid_pcompose _)⁻¹* @[hott] def apn_pid {A : Type*} (n : ℕ) : apn n (pid A) ~* pid (Ω[n] A) := begin induction n with n IH, { reflexivity}, { exact ap1_phomotopy IH ⬝* ap1_pid} end @[hott] def apn_pconst (A B : Type*) (n : ℕ) : apn n (pconst A B) ~* pconst (Ω[n] A) (Ω[n] B) := begin induction n with n IH, { reflexivity }, { exact ap1_phomotopy IH ⬝* ap1_pconst _ _ } end @[hott] def apn_pcompose (n : ℕ) (g : B →* C) (f : A →* B) : apn n (g ∘* f) ~* apn n g ∘* apn n f := begin induction n with n IH, { reflexivity}, { refine ap1_phomotopy IH ⬝* _, apply ap1_pcompose} end @[hott] def pcast_idp {A : Type*} : pcast (idpath A) ~* pid A := by reflexivity @[hott] def pinverse_pinverse (A : Type*) : pinverse A ∘* pinverse A ~* pid (Ω A) := begin fapply phomotopy.mk, { apply hott.eq.inv_inv }, { reflexivity} end @[hott] def pcast_ap_loop {A B : Type*} (p : A = B) : pcast (ap Ω p) ~* ap1 (pcast p) := begin fapply phomotopy.mk, { intro a, induction p, symmetry, exact idp_con _ ⬝ ap_id _ }, { induction p, refl } end @[hott] def ap1_pmap_of_map {A B : Type _} (f : A → B) (a : A) : ap1 (pmap_of_map f a) ~* pmap_of_map (ap f) (idpath a) := begin fapply phomotopy.mk, { intro a, apply idp_con }, { reflexivity } end @[hott] def pcast_commute {A : Type _} {B C : A → Type*} (f : Πa, B a →* C a) {a₁ a₂ : A} (p : a₁ = a₂) : pcast (ap C p) ∘* f a₁ ~* f a₂ ∘* pcast (ap B p) := phomotopy.mk begin induction p, reflexivity end begin induction p, refine idp_con _ ⬝ idp_con _ ⬝ _, symmetry, apply ap_id end /- pointed equivalences -/ structure pequiv (A B : Type*) := mk' :: (to_pmap : A →* B) (to_pinv1 : B →* A) (to_pinv2 : B →* A) (pright_inv : to_pmap ∘* to_pinv1 ~* pid B) (pleft_inv : to_pinv2 ∘* to_pmap ~* pid A) infix ` ≃* `:25 := pequiv @[hott, reducible] def pmap_of_pequiv {A B : Type*} (f : A ≃* B) : @ppi A (λa, B) pt := pequiv.to_pmap f @[hott] instance {A B : Type*} (f : A ≃* B) : has_coe (A ≃* B) (A →* B) := ⟨pmap_of_pequiv⟩ @[hott] def to_pinv (f : A ≃* B) : B →* A := pequiv.to_pinv1 f @[hott] def pleft_inv' (f : A ≃* B) : to_pinv f ∘* f.to_pmap ~* pid A := let g := to_pinv f in let h := pequiv.to_pinv2 f in calc g ∘* f.to_pmap ~* pid A ∘* (g ∘* f.to_pmap) : by exact (pid_pcompose _)⁻¹* ... ~* (h ∘* f.to_pmap) ∘* (g ∘* f.to_pmap) : by exact pwhisker_right _ (pequiv.pleft_inv f)⁻¹* ... ~* h ∘* (f.to_pmap ∘* g) ∘* f.to_pmap : by exact passoc _ _ _ ⬝* pwhisker_left _ (passoc _ _ _)⁻¹* ... ~* h ∘* pid B ∘* f.to_pmap : by exact pwhisker_left _ (pwhisker_right _ (pequiv.pright_inv _)) ... ~* h ∘* f.to_pmap : by exact pwhisker_left _ (pid_pcompose _) ... ~* pid A : by exact pequiv.pleft_inv f @[hott] def equiv_of_pequiv (f : A ≃* B) : A ≃ B := equiv.mk f.to_pmap $ adjointify f.to_pmap (to_pinv f) (pequiv.pright_inv f) (pleft_inv' f) @[hott] instance pequiv_to_equiv {A B : Type*} (f : A ≃* B) : has_coe (A ≃* B) (A ≃ B) := ⟨equiv_of_pequiv⟩ @[hott, instance] def pequiv.to_is_equiv (f : A ≃* B) : is_equiv (f.to_pmap) := to_is_equiv (equiv_of_pequiv f) @[hott] protected def pequiv.MK (f : A →* B) (g : B →* A) (gf : g ∘* f ~* pid A) (fg : f ∘* g ~* pid B) : A ≃* B := pequiv.mk' f g g fg gf @[hott] def pinv (f : A →* B) (H : is_equiv f) : B →* A := pmap.mk f⁻¹ᶠ (ap f⁻¹ᶠ (respect_pt f)⁻¹ ⬝ (left_inv f pt)) @[hott] def pequiv_of_pmap (f : A →* B) (H : is_equiv f) : A ≃* B := pequiv.mk' f (pinv f H) (pinv f H) begin abstract {fapply phomotopy.mk, exact right_inv f, induction f with f f₀, induction B with B b₀, dsimp at *, induction f₀, exact adj f pt ⬝ ap02 f (idp_con _)⁻¹ᵖ } end begin abstract {fapply phomotopy.mk, exact left_inv f, induction f with f f₀, induction B with B b₀, dsimp at *, induction f₀, exact (idp_con _)⁻¹ ⬝ (idp_con _)⁻¹} end @[hott] def pequiv.mk (f : A → B) (H : is_equiv f) (p : f pt = pt) : A ≃* B := pequiv_of_pmap (pmap.mk f p) H @[hott] def pequiv_of_equiv (f : A ≃ B) (H : f pt = pt) : A ≃* B := pequiv.mk f f.to_is_equiv H @[hott] protected def pequiv.MK' (f : A →* B) (g : B → A) (gf : Πa, g (f a) = a) (fg : Πb, f (g b) = b) : A ≃* B := pequiv.mk f (adjointify f g fg gf) (respect_pt f) /- reflexivity and symmetry (transitivity is below) -/ @[hott] protected def pequiv.refl (A : Type*) : A ≃* A := pequiv.mk' (pid A) (pid A) (pid A) (pid_pcompose _) (pcompose_pid _) @[hott, refl, reducible] protected def pequiv.rfl : A ≃* A := pequiv.refl A @[hott, symm] protected def pequiv.symm (f : A ≃* B) : B ≃* A := pequiv.MK (to_pinv f) f.to_pmap (pequiv.pright_inv f) (pleft_inv' f) postfix `⁻¹ᵉ*`:(max + 1) := pequiv.symm @[hott] def pleft_inv (f : A ≃* B) : f⁻¹ᵉ*.to_pmap ∘* f.to_pmap ~* pid A := pleft_inv' f @[hott] def pright_inv (f : A ≃* B) : f.to_pmap ∘* f⁻¹ᵉ*.to_pmap ~* pid B := pequiv.pright_inv f @[hott] def to_pmap_pequiv_of_pmap {A B : Type*} (f : A →* B) (H : is_equiv f) : pequiv.to_pmap (pequiv_of_pmap f H) = f := by reflexivity @[hott] def to_pmap_pequiv_MK (f : A →* B) (g : B →* A) (gf : g ∘* f ~* pid A) (fg : f ∘* g ~* pid B) : (pequiv.MK f g gf fg).to_pmap ~* f := by reflexivity @[hott] def to_pinv_pequiv_MK (f : A →* B) (g : B →* A) (gf : g ∘* f ~* pid A) (fg : f ∘* g ~* pid B) : to_pinv (pequiv.MK f g gf fg) ~* g := by reflexivity /- more on pointed equivalences -/ @[hott] def pequiv_ap {A : Type _} (B : A → Type*) {a a' : A} (p : a = a') : B a ≃* B a' := pequiv_of_pmap (ptransport B p) (is_equiv_tr (λa, B a) p) @[hott] def pequiv_change_fun (f : A ≃* B) (f' : A →* B) (Heq : f.to_pmap ~ f') : A ≃* B := pequiv_of_pmap f' (is_equiv.homotopy_closed f.to_pmap Heq) @[hott] def pequiv_change_inv (f : A ≃* B) (f' : B →* A) (Heq : to_pinv f ~ f') : A ≃* B := pequiv.MK' f.to_pmap f' (to_left_inv (equiv_change_inv (equiv_of_pequiv f) Heq)) (to_right_inv (equiv_change_inv (equiv_of_pequiv f) Heq)) @[hott] def pequiv_rect' (f : A ≃* B) (P : A → B → Type _) (g : Πb, P ((equiv_of_pequiv f)⁻¹ᵉ b) b) (a : A) : P a (f.to_pmap a) := transport (λx, P x (f.to_pmap a)) (left_inv f.to_pmap a) (g (f.to_pmap a)) @[hott] def pua {A B : Type*} (f : A ≃* B) : A = B := pType_eq (equiv_of_pequiv f) (respect_pt _) @[hott] def pequiv_of_eq {A B : Type*} (p : A = B) : A ≃* B := pequiv_of_pmap (pcast p) (is_equiv_tr (λa, a) _) @[hott] def eq_of_pequiv {A B : Type*} (p : A ≃* B) : A = B := pType_eq (equiv_of_pequiv p) (respect_pt _) @[hott] def peap {A B : Type*} (F : Type* → Type*) (p : A ≃* B) : F A ≃* F B := pequiv_of_pmap (pcast (ap F (eq_of_pequiv p))) begin induction eq_of_pequiv p, apply is_equiv_id end -- rename pequiv_of_eq_natural @[hott] def pequiv_of_eq_commute {A : Type _} {B C : A → Type*} (f : Πa, B a →* C a) {a₁ a₂ : A} (p : a₁ = a₂) : (pequiv_of_eq (ap C p)).to_pmap ∘* f a₁ ~* f a₂ ∘* (pequiv_of_eq (ap B p)).to_pmap := pcast_commute f p -- @[hott] def pequiv.eta_expand {A B : Type*} (f : A ≃* B) : A ≃* B := -- pequiv.mk' f (to_pinv f) (pequiv.to_pinv2 f) (pright_inv f) _ /- the @[hott] theorem pequiv_eq, which gives a condition for two pointed equivalences are equal is in types.equiv to avoid circular imports -/ /- computation rules of pointed homotopies, possibly combined with pointed equivalences -/ @[hott] def pcancel_left (f : B ≃* C) {g h : A →* B} (p : f.to_pmap ∘* g ~* f.to_pmap ∘* h) : g ~* h := begin refine _⁻¹* ⬝* pwhisker_left f⁻¹ᵉ*.to_pmap p ⬝* _, all_goals {refine (passoc _ _ _)⁻¹* ⬝* _, refine pwhisker_right _ (pleft_inv f) ⬝* _, apply pid_pcompose } end @[hott] def pcancel_right (f : A ≃* B) {g h : B →* C} (p : g ∘* f.to_pmap ~* h ∘* f.to_pmap) : g ~* h := begin refine _⁻¹* ⬝* pwhisker_right f⁻¹ᵉ*.to_pmap p ⬝* _, all_goals {refine passoc _ _ _ ⬝* _, refine pwhisker_left _ (pright_inv f) ⬝* _, apply pcompose_pid } end @[hott] def phomotopy_pinv_right_of_phomotopy {f : A ≃* B} {g : B →* C} {h : A →* C} (p : g ∘* f.to_pmap ~* h) : g ~* h ∘* f⁻¹ᵉ*.to_pmap := begin refine _ ⬝* pwhisker_right _ p, symmetry, refine passoc _ _ _ ⬝* _, refine pwhisker_left _ (pright_inv f) ⬝* _, apply pcompose_pid end @[hott] def phomotopy_of_pinv_right_phomotopy {f : B ≃* A} {g : B →* C} {h : A →* C} (p : g ∘* f⁻¹ᵉ*.to_pmap ~* h) : g ~* h ∘* f.to_pmap := begin refine _ ⬝* pwhisker_right _ p, symmetry, refine passoc _ _ _ ⬝* _, refine pwhisker_left _ (pleft_inv f) ⬝* _, apply pcompose_pid end @[hott] def pinv_right_phomotopy_of_phomotopy {f : A ≃* B} {g : B →* C} {h : A →* C} (p : h ~* g ∘* f.to_pmap) : h ∘* f⁻¹ᵉ*.to_pmap ~* g := (phomotopy_pinv_right_of_phomotopy p⁻¹*)⁻¹* @[hott] def phomotopy_of_phomotopy_pinv_right {f : B ≃* A} {g : B →* C} {h : A →* C} (p : h ~* g ∘* f⁻¹ᵉ*.to_pmap) : h ∘* f.to_pmap ~* g := (phomotopy_of_pinv_right_phomotopy p⁻¹*)⁻¹* @[hott] def phomotopy_pinv_left_of_phomotopy {f : B ≃* C} {g : A →* B} {h : A →* C} (p : f.to_pmap ∘* g ~* h) : g ~* f⁻¹ᵉ*.to_pmap ∘* h := begin refine _ ⬝* pwhisker_left _ p, symmetry, refine (passoc _ _ _)⁻¹* ⬝* _, refine pwhisker_right _ (pleft_inv f) ⬝* _, apply pid_pcompose end @[hott] def phomotopy_of_pinv_left_phomotopy {f : C ≃* B} {g : A →* B} {h : A →* C} (p : f⁻¹ᵉ*.to_pmap ∘* g ~* h) : g ~* f.to_pmap ∘* h := begin refine _ ⬝* pwhisker_left _ p, symmetry, refine (passoc _ _ _)⁻¹* ⬝* _, refine pwhisker_right _ (pright_inv f) ⬝* _, apply pid_pcompose end @[hott] def pinv_left_phomotopy_of_phomotopy {f : B ≃* C} {g : A →* B} {h : A →* C} (p : h ~* f.to_pmap ∘* g) : f⁻¹ᵉ*.to_pmap ∘* h ~* g := (phomotopy_pinv_left_of_phomotopy p⁻¹*)⁻¹* @[hott] def phomotopy_of_phomotopy_pinv_left {f : C ≃* B} {g : A →* B} {h : A →* C} (p : h ~* f⁻¹ᵉ*.to_pmap ∘* g) : f.to_pmap ∘* h ~* g := (phomotopy_of_pinv_left_phomotopy p⁻¹*)⁻¹* @[hott] def pcompose2 {A B C : Type*} {g g' : B →* C} {f f' : A →* B} (q : g ~* g') (p : f ~* f') : g ∘* f ~* g' ∘* f' := pwhisker_right f q ⬝* pwhisker_left g' p infixr ` ◾* `:80 := pcompose2 @[hott] def phomotopy_pinv_of_phomotopy_pid {A B : Type*} {f : A →* B} {g : B ≃* A} (p : g.to_pmap ∘* f ~* pid A) : f ~* g⁻¹ᵉ*.to_pmap := phomotopy_pinv_left_of_phomotopy p ⬝* pcompose_pid _ @[hott] def phomotopy_pinv_of_phomotopy_pid' {A B : Type*} {f : A →* B} {g : B ≃* A} (p : f ∘* g.to_pmap ~* pid B) : f ~* g⁻¹ᵉ*.to_pmap := phomotopy_pinv_right_of_phomotopy p ⬝* pid_pcompose _ @[hott] def pinv_phomotopy_of_pid_phomotopy {A B : Type*} {f : A →* B} {g : B ≃* A} (p : pid A ~* g.to_pmap ∘* f) : g⁻¹ᵉ*.to_pmap ~* f := (phomotopy_pinv_of_phomotopy_pid p⁻¹*)⁻¹* @[hott] def pinv_phomotopy_of_pid_phomotopy' {A B : Type*} {f : A →* B} {g : B ≃* A} (p : pid B ~* f ∘* g.to_pmap) : g⁻¹ᵉ*.to_pmap ~* f := (phomotopy_pinv_of_phomotopy_pid' p⁻¹*)⁻¹* @[hott] def pinv_pcompose_cancel_left {A B C : Type*} (g : B ≃* C) (f : A →* B) : g⁻¹ᵉ*.to_pmap ∘* (g.to_pmap ∘* f) ~* f := (passoc _ _ _)⁻¹* ⬝* pwhisker_right f (pleft_inv _) ⬝* pid_pcompose _ @[hott] def pcompose_pinv_cancel_left {A B C : Type*} (g : C ≃* B) (f : A →* B) : g.to_pmap ∘* (g⁻¹ᵉ*.to_pmap ∘* f) ~* f := (passoc _ _ _)⁻¹* ⬝* pwhisker_right f (pright_inv _) ⬝* pid_pcompose _ @[hott] def pinv_pcompose_cancel_right {A B C : Type*} (g : B →* C) (f : B ≃* A) : (g ∘* f⁻¹ᵉ*.to_pmap) ∘* f.to_pmap ~* g := passoc _ _ _ ⬝* pwhisker_left g (pleft_inv _) ⬝* pcompose_pid _ @[hott] def pcompose_pinv_cancel_right {A B C : Type*} (g : B →* C) (f : A ≃* B) : (g ∘* f.to_pmap) ∘* f⁻¹ᵉ*.to_pmap ~* g := passoc _ _ _ ⬝* pwhisker_left g (pright_inv _) ⬝* pcompose_pid _ @[hott] def pinv_pinv {A B : Type*} (f : A ≃* B) : (f⁻¹ᵉ*)⁻¹ᵉ*.to_pmap ~* f.to_pmap := (phomotopy_pinv_of_phomotopy_pid (pleft_inv f))⁻¹* @[hott] def pinv2 {A B : Type*} {f f' : A ≃* B} (p : f.to_pmap ~* f'.to_pmap) : f⁻¹ᵉ*.to_pmap ~* f'⁻¹ᵉ*.to_pmap := phomotopy_pinv_of_phomotopy_pid (pinv_right_phomotopy_of_phomotopy (pid_pcompose _ ⬝* p)⁻¹*) postfix [parsing_only] `⁻²*`:(max+10) := pinv2 @[hott, trans] protected def pequiv.trans (f : A ≃* B) (g : B ≃* C) : A ≃* C := pequiv.MK (g.to_pmap ∘* f.to_pmap) (f⁻¹ᵉ*.to_pmap ∘* g⁻¹ᵉ*.to_pmap) begin abstract {exact passoc _ _ _ ⬝* pwhisker_left _ (pinv_pcompose_cancel_left g f.to_pmap) ⬝* pleft_inv f} end begin abstract {exact passoc _ _ _ ⬝* pwhisker_left _ (pcompose_pinv_cancel_left f g⁻¹ᵉ*.to_pmap) ⬝* pright_inv g} end @[hott] def pequiv_compose {A B C : Type*} (g : B ≃* C) (f : A ≃* B) : A ≃* C := pequiv.trans f g infix ` ⬝e* `:75 := pequiv.trans infixr ` ∘*ᵉ `:60 := pequiv_compose @[hott] def to_pmap_pequiv_trans {A B C : Type*} (f : A ≃* B) (g : B ≃* C) : (f ⬝e* g).to_pmap = g.to_pmap ∘* f.to_pmap := by reflexivity @[hott] def to_fun_pequiv_trans {X Y Z : Type*} (f : X ≃* Y) (g :Y ≃* Z) : (f ⬝e* g).to_pmap ~ g.to_pmap ∘ f.to_pmap := λx, idp @[hott] def peconcat_eq {A B C : Type*} (p : A ≃* B) (q : B = C) : A ≃* C := p ⬝e* pequiv_of_eq q @[hott] def eq_peconcat {A B C : Type*} (p : A = B) (q : B ≃* C) : A ≃* C := pequiv_of_eq p ⬝e* q infix ` ⬝e*p `:75 := peconcat_eq infix ` ⬝pe* `:75 := eq_peconcat @[hott] def trans_pinv {A B C : Type*} (f : A ≃* B) (g : B ≃* C) : (f ⬝e* g)⁻¹ᵉ*.to_pmap ~* f⁻¹ᵉ*.to_pmap ∘* g⁻¹ᵉ*.to_pmap := by reflexivity @[hott] def pinv_trans_pinv_left {A B C : Type*} (f : B ≃* A) (g : B ≃* C) : (f⁻¹ᵉ* ⬝e* g)⁻¹ᵉ*.to_pmap ~* f.to_pmap ∘* g⁻¹ᵉ*.to_pmap := by reflexivity @[hott] def pinv_trans_pinv_right {A B C : Type*} (f : A ≃* B) (g : C ≃* B) : (f ⬝e* g⁻¹ᵉ*)⁻¹ᵉ*.to_pmap ~* f⁻¹ᵉ*.to_pmap ∘* g.to_pmap := by reflexivity @[hott] def pinv_trans_pinv_pinv {A B C : Type*} (f : B ≃* A) (g : C ≃* B) : (f⁻¹ᵉ* ⬝e* g⁻¹ᵉ*)⁻¹ᵉ*.to_pmap ~* f.to_pmap ∘* g.to_pmap := by reflexivity /- pointed equivalences between particular pointed types -/ -- TODO: remove is_equiv_apn, which is proven again here @[hott] def loopn_pequiv_loopn (n : ℕ) (f : A ≃* B) : Ω[n] A ≃* Ω[n] B := pequiv.MK (apn n f.to_pmap) (apn n f⁻¹ᵉ*.to_pmap) begin abstract {induction n with n IH, { apply pleft_inv}, { rwr [show nat.succ n = n + 1, from idp, apn_succ], refine (ap1_pcompose _ _)⁻¹* ⬝* _, refine ap1_phomotopy IH ⬝* _, apply ap1_pid}} end begin abstract {induction n with n IH, { apply pright_inv}, { rwr [show nat.succ n = n + 1, from idp, apn_succ], refine (ap1_pcompose _ _)⁻¹* ⬝* _, refine ap1_phomotopy IH ⬝* _, apply ap1_pid}} end @[hott] def loop_pequiv_loop (f : A ≃* B) : Ω A ≃* Ω B := loopn_pequiv_loopn 1 f @[hott] def loop_pequiv_eq_closed {A : Type _} {a a' : A} (p : a = a') : pointed.MK (a = a) idp ≃* pointed.MK (a' = a') idp := pequiv_of_equiv (loop_equiv_eq_closed p) (con.left_inv p) @[hott] def to_pmap_loopn_pequiv_loopn (n : ℕ) (f : A ≃* B) : (loopn_pequiv_loopn n f).to_pmap ~* apn n f.to_pmap := by refl @[hott] def to_pinv_loopn_pequiv_loopn (n : ℕ) (f : A ≃* B) : (loopn_pequiv_loopn n f)⁻¹ᵉ*.to_pmap ~* apn n f⁻¹ᵉ*.to_pmap := by refl @[hott] def loopn_pequiv_loopn_con (n : ℕ) (f : A ≃* B) (p q : Ω[n+1] A) : (loopn_pequiv_loopn (n+1) f).to_pmap.to_fun (p ⬝ q) = (loopn_pequiv_loopn (n+1) f).to_pmap.to_fun p ⬝ (loopn_pequiv_loopn (n+1) f).to_pmap.to_fun q := ap1_con (loopn_pequiv_loopn n f).to_pmap p q @[hott] def loop_pequiv_loop_con {A B : Type*} (f : A ≃* B) (p q : Ω A) : (loop_pequiv_loop f).to_pmap (p ⬝ q) = (loop_pequiv_loop f).to_pmap p ⬝ (loop_pequiv_loop f).to_pmap q := loopn_pequiv_loopn_con 0 f p q @[hott] def loopn_pequiv_loopn_rfl (n : ℕ) (A : Type*) : (loopn_pequiv_loopn n (pequiv.refl A)).to_pmap ~* (pequiv.refl (Ω[n] A)).to_pmap := begin exact to_pmap_loopn_pequiv_loopn _ _ ⬝* apn_pid n, end @[hott] def loop_pequiv_loop_rfl (A : Type*) : (loop_pequiv_loop (pequiv.refl A)).to_pmap ~* (pequiv.refl (Ω A)).to_pmap := loopn_pequiv_loopn_rfl 1 A -- duplicate of to_pinv_loopn_pequiv_loopn @[hott] def apn_pinv (n : ℕ) {A B : Type*} (f : A ≃* B) : Ω→[n] f⁻¹ᵉ*.to_pmap ~* (loopn_pequiv_loopn n f)⁻¹ᵉ*.to_pmap := by reflexivity @[hott] def pmap_functor {A A' B B' : Type*} (f : A' →* A) (g : B →* B') : ppmap A B →* ppmap A' B' := pmap.mk (λh, g ∘* h ∘* f) begin abstract {fapply eq_of_phomotopy, fapply phomotopy.mk, { hintro a, exact respect_pt g}, { symmetry, refine _ ◾ idp ⬝ idp_con _, exact ap02 g (ap_constant _ _) }} end @[hott] def pequiv_pinverse (A : Type*) : Ω A ≃* Ω A := pequiv_of_pmap (pinverse A) (is_equiv_eq_inverse _ _) @[hott] def pequiv_of_eq_pt {A : Type _} {a a' : A} (p : a = a') : pointed.MK A a ≃* pointed.MK A a' := pequiv_of_pmap (pmap_of_eq_pt p) (is_equiv_id _) @[hott] def pointed_eta_pequiv (A : Type*) : A ≃* pointed.MK A pt := pequiv.mk id (is_equiv_id _) idp /- every pointed map is homotopic to one of the form `pmap_of_map _ _`, up to some pointed equivalences -/ @[hott] def phomotopy_pmap_of_map {A B : Type*} (f : A →* B) : (pointed_eta_pequiv B ⬝e* (pequiv_of_eq_pt (respect_pt f))⁻¹ᵉ*).to_pmap ∘* f ∘* (pointed_eta_pequiv A)⁻¹ᵉ*.to_pmap ~* pmap_of_map f pt := begin fapply phomotopy.mk, { reflexivity}, { symmetry, exact (ap_id _ ⬝ idp_con _) ◾ (idp_con _ ⬝ ap_id _) ⬝ con.right_inv _ } end /- properties of iterated loop space -/ variable (A) @[hott] def loopn_succ_in (n : ℕ) : Ω[succ n] A ≃* Ω[n] (Ω A) := begin induction n with n IH, { reflexivity}, { exact loop_pequiv_loop IH} end @[hott] def loopn_add (n m : ℕ) : Ω[n] (Ω[m] A) ≃* Ω[m+n] (A) := begin induction n with n IH, { reflexivity}, { exact loop_pequiv_loop IH} end @[hott] def loopn_succ_out (n : ℕ) : Ω[succ n] A ≃* Ω(Ω[n] A) := by reflexivity variable {A} @[hott] def loopn_succ_in_con {n : ℕ} (p q : Ω[succ (succ n)] A) : (loopn_succ_in A (succ n)).to_pmap (p ⬝ q) = (loopn_succ_in A (succ n)).to_pmap p ⬝ (loopn_succ_in A (succ n)).to_pmap q := loop_pequiv_loop_con _ _ _ @[hott] def loopn_loop_irrel (p : point A = point A) : Ω(pointed.Mk p) = Ω[2] A := begin intros, fapply pType_eq, { transitivity _, apply eq_equiv_fn_eq_of_equiv (equiv_eq_closed_right _ p⁻¹), apply eq_equiv_eq_closed, apply con.right_inv, apply con.right_inv}, { apply con.left_inv} end @[hott] def loopn_space_loop_irrel (n : ℕ) (p : point A = point A) : Ω[succ n](pointed.Mk p) = Ω[succ (succ n)] A :> pType := calc Ω[succ n](pointed.Mk p) = Ω[n](Ω (pointed.Mk p)) : eq_of_pequiv $ loopn_succ_in _ _ ... = Ω[n] (Ω[2] A) : ap Ω[n] $ loopn_loop_irrel p ... = Ω[n+1] (Ω A) : eq_of_pequiv $ (loopn_succ_in _ _)⁻¹ᵉ* ... = Ω[n+2] A : eq_of_pequiv $ (loopn_succ_in _ _)⁻¹ᵉ* @[hott] def apn_succ_phomotopy_in (n : ℕ) (f : A →* B) : (loopn_succ_in B n).to_pmap ∘* Ω→[n + 1] f ~* Ω→[n] (Ω→ f) ∘* (loopn_succ_in A n).to_pmap := begin induction n with n IH, { reflexivity}, { exact (ap1_pcompose _ _)⁻¹* ⬝* ap1_phomotopy IH ⬝* (ap1_pcompose _ _)} end @[hott] def loopn_succ_in_natural {A B : Type*} (n : ℕ) (f : A →* B) : (loopn_succ_in B n).to_pmap ∘* Ω→[n+1] f ~* Ω→[n] (Ω→ f) ∘* (loopn_succ_in A n).to_pmap := apn_succ_phomotopy_in _ _ @[hott] def loopn_succ_in_inv_natural {A B : Type*} (n : ℕ) (f : A →* B) : Ω→[n + 1] f ∘* (loopn_succ_in A n)⁻¹ᵉ*.to_pmap ~* (loopn_succ_in B n)⁻¹ᵉ*.to_pmap ∘* Ω→[n] (Ω→ f):= begin apply pinv_right_phomotopy_of_phomotopy, refine _ ⬝* (passoc _ _ _)⁻¹*, apply phomotopy_pinv_left_of_phomotopy, apply apn_succ_phomotopy_in end end pointed end hott
e228ff6c49a11afe818b8eb6e48c8190f4538466
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/real/sqrt.lean
5f8c27269e8f33a2876e516e71394669a29b0395
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
9,739
lean
/- Copyright (c) 2020 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Floris van Doorn, Yury Kudryashov -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.topology.instances.nnreal import Mathlib.PostPort universes u_1 namespace Mathlib /-! # Square root of a real number In this file we define * `nnreal.sqrt` to be the square root of a nonnegative real number. * `real.sqrt` to be the square root of a real number, defined to be zero on negative numbers. Then we prove some basic properties of these functions. ## Implementation notes We define `nnreal.sqrt` as the noncomputable inverse to the function `x ↦ x * x`. We use general theory of inverses of strictly monotone functions to prove that `nnreal.sqrt x` exists. As a side effect, `nnreal.sqrt` is a bundled `order_iso`, so for `nnreal` numbers we get continuity as well as theorems like `sqrt x ≤ y ↔ x * x ≤ y` for free. Then we define `real.sqrt x` to be `nnreal.sqrt (nnreal.of_real x)`. We also define a Cauchy sequence `real.sqrt_aux (f : cau_seq ℚ abs)` which converges to `sqrt (mk f)` but do not prove (yet) that this sequence actually converges to `sqrt (mk f)`. ## Tags square root -/ namespace nnreal /-- Square root of a nonnegative real number. -/ def sqrt : nnreal ≃o nnreal := order_iso.symm (strict_mono.order_iso_of_surjective (fun (x : nnreal) => x * x) sorry sorry) theorem sqrt_eq_iff_sqr_eq {x : nnreal} {y : nnreal} : coe_fn sqrt x = y ↔ y * y = x := iff.trans (equiv.apply_eq_iff_eq_symm_apply (rel_iso.to_equiv sqrt)) eq_comm theorem sqrt_le_iff {x : nnreal} {y : nnreal} : coe_fn sqrt x ≤ y ↔ x ≤ y * y := order_iso.to_galois_connection sqrt x y theorem le_sqrt_iff {x : nnreal} {y : nnreal} : x ≤ coe_fn sqrt y ↔ x * x ≤ y := iff.symm (order_iso.to_galois_connection (order_iso.symm sqrt) x y) @[simp] theorem sqrt_eq_zero {x : nnreal} : coe_fn sqrt x = 0 ↔ x = 0 := iff.trans sqrt_eq_iff_sqr_eq (eq.mpr (id (Eq._oldrec (Eq.refl (0 * 0 = x ↔ x = 0)) (propext eq_comm))) (eq.mpr (id (Eq._oldrec (Eq.refl (x = 0 * 0 ↔ x = 0)) (zero_mul 0))) (iff.refl (x = 0)))) @[simp] theorem sqrt_zero : coe_fn sqrt 0 = 0 := iff.mpr sqrt_eq_zero rfl @[simp] theorem sqrt_one : coe_fn sqrt 1 = 1 := iff.mpr sqrt_eq_iff_sqr_eq (mul_one 1) @[simp] theorem mul_sqrt_self (x : nnreal) : coe_fn sqrt x * coe_fn sqrt x = x := order_iso.symm_apply_apply sqrt x @[simp] theorem sqrt_mul_self (x : nnreal) : coe_fn sqrt (x * x) = x := order_iso.apply_symm_apply sqrt x theorem sqrt_mul (x : nnreal) (y : nnreal) : coe_fn sqrt (x * y) = coe_fn sqrt x * coe_fn sqrt y := sorry /-- `nnreal.sqrt` as a `monoid_with_zero_hom`. -/ def sqrt_hom : monoid_with_zero_hom nnreal nnreal := monoid_with_zero_hom.mk (⇑sqrt) sqrt_zero sqrt_one sqrt_mul theorem sqrt_inv (x : nnreal) : coe_fn sqrt (x⁻¹) = (coe_fn sqrt x⁻¹) := monoid_with_zero_hom.map_inv' sqrt_hom x theorem sqrt_div (x : nnreal) (y : nnreal) : coe_fn sqrt (x / y) = coe_fn sqrt x / coe_fn sqrt y := monoid_with_zero_hom.map_div sqrt_hom x y theorem continuous_sqrt : continuous ⇑sqrt := order_iso.continuous sqrt end nnreal namespace real /-- An auxiliary sequence of rational numbers that converges to `real.sqrt (mk f)`. Currently this sequence is not used in `mathlib`. -/ def sqrt_aux (f : cau_seq ℚ abs) : ℕ → ℚ := sorry theorem sqrt_aux_nonneg (f : cau_seq ℚ abs) (i : ℕ) : 0 ≤ sqrt_aux f i := sorry /- TODO(Mario): finish the proof theorem sqrt_aux_converges (f : cau_seq ℚ abs) : ∃ h x, 0 ≤ x ∧ x * x = max 0 (mk f) ∧ mk ⟨sqrt_aux f, h⟩ = x := begin rcases sqrt_exists (le_max_left 0 (mk f)) with ⟨x, x0, hx⟩, suffices : ∃ h, mk ⟨sqrt_aux f, h⟩ = x, { exact this.imp (λ h e, ⟨x, x0, hx, e⟩) }, apply of_near, suffices : ∃ δ > 0, ∀ i, abs (↑(sqrt_aux f i) - x) < δ / 2 ^ i, { rcases this with ⟨δ, δ0, hδ⟩, intros, } end -/ /-- The square root of a real number. This returns 0 for negative inputs. -/ def sqrt (x : ℝ) : ℝ := ↑(coe_fn nnreal.sqrt (nnreal.of_real x)) /-quotient.lift_on x (λ f, mk ⟨sqrt_aux f, (sqrt_aux_converges f).fst⟩) (λ f g e, begin rcases sqrt_aux_converges f with ⟨hf, x, x0, xf, xs⟩, rcases sqrt_aux_converges g with ⟨hg, y, y0, yg, ys⟩, refine xs.trans (eq.trans _ ys.symm), rw [← @mul_self_inj_of_nonneg ℝ _ x y x0 y0, xf, yg], congr' 1, exact quotient.sound e end)-/ theorem continuous_sqrt : continuous sqrt := continuous.comp nnreal.continuous_coe (continuous.comp (order_iso.continuous nnreal.sqrt) nnreal.continuous_of_real) theorem sqrt_eq_zero_of_nonpos {x : ℝ} (h : x ≤ 0) : sqrt x = 0 := sorry theorem sqrt_nonneg (x : ℝ) : 0 ≤ sqrt x := nnreal.coe_nonneg (coe_fn nnreal.sqrt (nnreal.of_real x)) @[simp] theorem mul_self_sqrt {x : ℝ} (h : 0 ≤ x) : sqrt x * sqrt x = x := sorry @[simp] theorem sqrt_mul_self {x : ℝ} (h : 0 ≤ x) : sqrt (x * x) = x := iff.mp (mul_self_inj_of_nonneg (sqrt_nonneg (x * x)) h) (mul_self_sqrt (mul_self_nonneg x)) theorem sqrt_eq_iff_mul_self_eq {x : ℝ} {y : ℝ} (hx : 0 ≤ x) (hy : 0 ≤ y) : sqrt x = y ↔ y * y = x := sorry @[simp] theorem sqr_sqrt {x : ℝ} (h : 0 ≤ x) : sqrt x ^ bit0 1 = x := eq.mpr (id (Eq._oldrec (Eq.refl (sqrt x ^ bit0 1 = x)) (pow_two (sqrt x)))) (eq.mpr (id (Eq._oldrec (Eq.refl (sqrt x * sqrt x = x)) (mul_self_sqrt h))) (Eq.refl x)) @[simp] theorem sqrt_sqr {x : ℝ} (h : 0 ≤ x) : sqrt (x ^ bit0 1) = x := eq.mpr (id (Eq._oldrec (Eq.refl (sqrt (x ^ bit0 1) = x)) (pow_two x))) (eq.mpr (id (Eq._oldrec (Eq.refl (sqrt (x * x) = x)) (sqrt_mul_self h))) (Eq.refl x)) theorem sqrt_eq_iff_sqr_eq {x : ℝ} {y : ℝ} (hx : 0 ≤ x) (hy : 0 ≤ y) : sqrt x = y ↔ y ^ bit0 1 = x := eq.mpr (id (Eq._oldrec (Eq.refl (sqrt x = y ↔ y ^ bit0 1 = x)) (pow_two y))) (eq.mpr (id (Eq._oldrec (Eq.refl (sqrt x = y ↔ y * y = x)) (propext (sqrt_eq_iff_mul_self_eq hx hy)))) (iff.refl (y * y = x))) theorem sqrt_mul_self_eq_abs (x : ℝ) : sqrt (x * x) = abs x := eq.mpr (id (Eq._oldrec (Eq.refl (sqrt (x * x) = abs x)) (Eq.symm (abs_mul_abs_self x)))) (eq.mpr (id (Eq._oldrec (Eq.refl (sqrt (abs x * abs x) = abs x)) (sqrt_mul_self (abs_nonneg x)))) (Eq.refl (abs x))) theorem sqrt_sqr_eq_abs (x : ℝ) : sqrt (x ^ bit0 1) = abs x := eq.mpr (id (Eq._oldrec (Eq.refl (sqrt (x ^ bit0 1) = abs x)) (pow_two x))) (eq.mpr (id (Eq._oldrec (Eq.refl (sqrt (x * x) = abs x)) (sqrt_mul_self_eq_abs x))) (Eq.refl (abs x))) @[simp] theorem sqrt_zero : sqrt 0 = 0 := sorry @[simp] theorem sqrt_one : sqrt 1 = 1 := sorry @[simp] theorem sqrt_le {x : ℝ} {y : ℝ} (hy : 0 ≤ y) : sqrt x ≤ sqrt y ↔ x ≤ y := sorry @[simp] theorem sqrt_lt {x : ℝ} {y : ℝ} (hx : 0 ≤ x) : sqrt x < sqrt y ↔ x < y := lt_iff_lt_of_le_iff_le (sqrt_le hx) theorem sqrt_le_sqrt {x : ℝ} {y : ℝ} (h : x ≤ y) : sqrt x ≤ sqrt y := sorry theorem sqrt_le_left {x : ℝ} {y : ℝ} (hy : 0 ≤ y) : sqrt x ≤ y ↔ x ≤ y ^ bit0 1 := sorry theorem sqrt_le_iff {x : ℝ} {y : ℝ} : sqrt x ≤ y ↔ 0 ≤ y ∧ x ≤ y ^ bit0 1 := sorry /- note: if you want to conclude `x ≤ sqrt y`, then use `le_sqrt_of_sqr_le`. if you have `x > 0`, consider using `le_sqrt'` -/ theorem le_sqrt {x : ℝ} {y : ℝ} (hx : 0 ≤ x) (hy : 0 ≤ y) : x ≤ sqrt y ↔ x ^ bit0 1 ≤ y := sorry theorem le_sqrt' {x : ℝ} {y : ℝ} (hx : 0 < x) : x ≤ sqrt y ↔ x ^ bit0 1 ≤ y := sorry theorem le_sqrt_of_sqr_le {x : ℝ} {y : ℝ} (h : x ^ bit0 1 ≤ y) : x ≤ sqrt y := or.dcases_on (lt_or_ge 0 x) (fun (hx : 0 < x) => eq.mpr (id (Eq._oldrec (Eq.refl (x ≤ sqrt y)) (propext (le_sqrt' hx)))) h) fun (hx : 0 ≥ x) => le_trans hx (sqrt_nonneg y) @[simp] theorem sqrt_inj {x : ℝ} {y : ℝ} (hx : 0 ≤ x) (hy : 0 ≤ y) : sqrt x = sqrt y ↔ x = y := sorry @[simp] theorem sqrt_eq_zero {x : ℝ} (h : 0 ≤ x) : sqrt x = 0 ↔ x = 0 := sorry theorem sqrt_eq_zero' {x : ℝ} : sqrt x = 0 ↔ x ≤ 0 := sorry @[simp] theorem sqrt_pos {x : ℝ} : 0 < sqrt x ↔ 0 < x := sorry @[simp] theorem sqrt_mul {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : sqrt (x * y) = sqrt x * sqrt y := sorry @[simp] theorem sqrt_mul' (x : ℝ) {y : ℝ} (hy : 0 ≤ y) : sqrt (x * y) = sqrt x * sqrt y := sorry @[simp] theorem sqrt_inv (x : ℝ) : sqrt (x⁻¹) = (sqrt x⁻¹) := sorry @[simp] theorem sqrt_div {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : sqrt (x / y) = sqrt x / sqrt y := sorry end real theorem filter.tendsto.sqrt {α : Type u_1} {f : α → ℝ} {l : filter α} {x : ℝ} (h : filter.tendsto f l (nhds x)) : filter.tendsto (fun (x : α) => real.sqrt (f x)) l (nhds (real.sqrt x)) := filter.tendsto.comp (continuous.tendsto real.continuous_sqrt x) h theorem continuous_within_at.sqrt {α : Type u_1} [topological_space α] {f : α → ℝ} {s : set α} {x : α} (h : continuous_within_at f s x) : continuous_within_at (fun (x : α) => real.sqrt (f x)) s x := filter.tendsto.sqrt h theorem continuous_at.sqrt {α : Type u_1} [topological_space α] {f : α → ℝ} {x : α} (h : continuous_at f x) : continuous_at (fun (x : α) => real.sqrt (f x)) x := filter.tendsto.sqrt h theorem continuous_on.sqrt {α : Type u_1} [topological_space α] {f : α → ℝ} {s : set α} (h : continuous_on f s) : continuous_on (fun (x : α) => real.sqrt (f x)) s := fun (x : α) (hx : x ∈ s) => continuous_within_at.sqrt (h x hx) theorem continuous.sqrt {α : Type u_1} [topological_space α] {f : α → ℝ} (h : continuous f) : continuous fun (x : α) => real.sqrt (f x) := continuous.comp real.continuous_sqrt h
52e6c204eb4f8cb87f64810c51b13dea7a6f83ef
302c785c90d40ad3d6be43d33bc6a558354cc2cf
/src/order/compactly_generated.lean
971a27ca7b9ed5e062ab1471f04dd05ae7763551
[ "Apache-2.0" ]
permissive
ilitzroth/mathlib
ea647e67f1fdfd19a0f7bdc5504e8acec6180011
5254ef14e3465f6504306132fe3ba9cec9ffff16
refs/heads/master
1,680,086,661,182
1,617,715,647,000
1,617,715,647,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
20,344
lean
/- Copyright (c) 2021 Oliver Nash. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Oliver Nash -/ import data.set.finite import data.finset.order import order.well_founded import order.order_iso_nat import order.atoms import order.zorn import tactic.tfae /-! # Compactness properties for complete lattices For complete lattices, there are numerous equivalent ways to express the fact that the relation `>` is well-founded. In this file we define three especially-useful characterisations and provide proofs that they are indeed equivalent to well-foundedness. ## Main definitions * `complete_lattice.is_sup_closed_compact` * `complete_lattice.is_Sup_finite_compact` * `complete_lattice.is_compact_element` * `complete_lattice.is_compactly_generated` ## Main results The main result is that the following four conditions are equivalent for a complete lattice: * `well_founded (>)` * `complete_lattice.is_sup_closed_compact` * `complete_lattice.is_Sup_finite_compact` * `∀ k, complete_lattice.is_compact_element k` This is demonstrated by means of the following four lemmas: * `complete_lattice.well_founded.is_Sup_finite_compact` * `complete_lattice.is_Sup_finite_compact.is_sup_closed_compact` * `complete_lattice.is_sup_closed_compact.well_founded` * `complete_lattice.is_Sup_finite_compact_iff_all_elements_compact` We also show well-founded lattices are compactly generated (`complete_lattice.compactly_generated_of_well_founded`). ## References - [G. Călugăreanu, *Lattice Concepts of Module Theory*][calugareanu] ## Tags complete lattice, well-founded, compact -/ variables {α : Type*} [complete_lattice α] namespace complete_lattice variables (α) /-- A compactness property for a complete lattice is that any `sup`-closed non-empty subset contains its `Sup`. -/ def is_sup_closed_compact : Prop := ∀ (s : set α) (h : s.nonempty), (∀ a b, a ∈ s → b ∈ s → a ⊔ b ∈ s) → (Sup s) ∈ s /-- A compactness property for a complete lattice is that any subset has a finite subset with the same `Sup`. -/ def is_Sup_finite_compact : Prop := ∀ (s : set α), ∃ (t : finset α), ↑t ⊆ s ∧ Sup s = t.sup id /-- An element `k` of a complete lattice is said to be compact if any set with `Sup` above `k` has a finite subset with `Sup` above `k`. Such an element is also called "finite" or "S-compact". -/ def is_compact_element {α : Type*} [complete_lattice α] (k : α) := ∀ s : set α, k ≤ Sup s → ∃ t : finset α, ↑t ⊆ s ∧ k ≤ t.sup id /-- An element `k` is compact if and only if any directed set with `Sup` above `k` already got above `k` at some point in the set. -/ theorem is_compact_element_iff_le_of_directed_Sup_le (k : α) : is_compact_element k ↔ ∀ s : set α, s.nonempty → directed_on (≤) s → k ≤ Sup s → ∃ x : α, x ∈ s ∧ k ≤ x := begin classical, split, { by_cases hbot : k = ⊥, -- Any nonempty directed set certainly has sup above ⊥ { rintros _ _ ⟨x, hx⟩ _ _, use x, by simp only [hx, hbot, bot_le, and_self], }, { intros hk s hne hdir hsup, obtain ⟨t, ht⟩ := hk s hsup, -- If t were empty, its sup would be ⊥, which is not above k ≠ ⊥. have tne : t.nonempty, { by_contradiction n, rw [finset.nonempty_iff_ne_empty, not_not] at n, simp only [n, true_and, set.empty_subset, finset.coe_empty, finset.sup_empty, le_bot_iff] at ht, exact absurd ht hbot, }, -- certainly every element of t is below something in s, since ↑t ⊆ s. have t_below_s : ∀ x ∈ t, ∃ y ∈ s, x ≤ y, from λ x hxt, ⟨x, ht.left hxt, by refl⟩, obtain ⟨x, ⟨hxs, hsupx⟩⟩ := finset.sup_le_of_le_directed s hne hdir t t_below_s, exact ⟨x, ⟨hxs, le_trans ht.right hsupx⟩⟩, }, }, { intros hk s hsup, -- Consider the set of finite joins of elements of the (plain) set s. let S : set α := { x | ∃ t : finset α, ↑t ⊆ s ∧ x = t.sup id }, -- S is directed, nonempty, and still has sup above k. have dir_US : directed_on (≤) S, { rintros x ⟨c, hc⟩ y ⟨d, hd⟩, use x ⊔ y, split, { use c ∪ d, split, { simp only [hc.left, hd.left, set.union_subset_iff, finset.coe_union, and_self], }, { simp only [hc.right, hd.right, finset.sup_union], }, }, simp only [and_self, le_sup_left, le_sup_right], }, have sup_S : Sup s ≤ Sup S, { apply Sup_le_Sup, intros x hx, use {x}, simpa only [and_true, id.def, finset.coe_singleton, eq_self_iff_true, finset.sup_singleton, set.singleton_subset_iff], }, have Sne : S.nonempty, { suffices : ⊥ ∈ S, from set.nonempty_of_mem this, use ∅, simp only [set.empty_subset, finset.coe_empty, finset.sup_empty, eq_self_iff_true, and_self], }, -- Now apply the defn of compact and finish. obtain ⟨j, ⟨hjS, hjk⟩⟩ := hk S Sne dir_US (le_trans hsup sup_S), obtain ⟨t, ⟨htS, htsup⟩⟩ := hjS, use t, exact ⟨htS, by rwa ←htsup⟩, }, end /-- A compact element `k` has the property that any directed set lying strictly below `k` has its Sup strictly below `k`. -/ lemma is_compact_element.directed_Sup_lt_of_lt {α : Type*} [complete_lattice α] {k : α} (hk : is_compact_element k) {s : set α} (hemp : s.nonempty) (hdir : directed_on (≤) s) (hbelow : ∀ x ∈ s, x < k) : Sup s < k := begin rw is_compact_element_iff_le_of_directed_Sup_le at hk, by_contradiction, have sSup : Sup s ≤ k, from Sup_le (λ s hs, (hbelow s hs).le), replace sSup : Sup s = k := eq_iff_le_not_lt.mpr ⟨sSup, h⟩, obtain ⟨x, hxs, hkx⟩ := hk s hemp hdir sSup.symm.le, obtain hxk := hbelow x hxs, exact hxk.ne (hxk.le.antisymm hkx), end lemma finset_sup_compact_of_compact {α β : Type*} [complete_lattice α] {f : β → α} (s : finset β) (h : ∀ x ∈ s, is_compact_element (f x)) : is_compact_element (s.sup f) := begin classical, rw is_compact_element_iff_le_of_directed_Sup_le, intros d hemp hdir hsup, change f with id ∘ f, rw ←finset.sup_finset_image, apply finset.sup_le_of_le_directed d hemp hdir, rintros x hx, obtain ⟨p, ⟨hps, rfl⟩⟩ := finset.mem_image.mp hx, specialize h p hps, rw is_compact_element_iff_le_of_directed_Sup_le at h, specialize h d hemp hdir (le_trans (finset.le_sup hps) hsup), simpa only [exists_prop], end lemma well_founded.is_Sup_finite_compact (h : well_founded ((>) : α → α → Prop)) : is_Sup_finite_compact α := begin intros s, let p : set α := { x | ∃ (t : finset α), ↑t ⊆ s ∧ t.sup id = x }, have hp : p.nonempty, { use [⊥, ∅], simp, }, obtain ⟨m, ⟨t, ⟨ht₁, ht₂⟩⟩, hm⟩ := well_founded.well_founded_iff_has_max'.mp h p hp, use t, simp only [ht₁, ht₂, true_and], apply le_antisymm, { apply Sup_le, intros y hy, classical, have hy' : (insert y t).sup id ∈ p, { use insert y t, simp, rw set.insert_subset, exact ⟨hy, ht₁⟩, }, have hm' : m ≤ (insert y t).sup id, { rw ← ht₂, exact finset.sup_mono (t.subset_insert y), }, rw ← hm _ hy' hm', simp, }, { rw [← ht₂, finset.sup_eq_Sup], exact Sup_le_Sup ht₁, }, end lemma is_Sup_finite_compact.is_sup_closed_compact (h : is_Sup_finite_compact α) : is_sup_closed_compact α := begin intros s hne hsc, obtain ⟨t, ht₁, ht₂⟩ := h s, clear h, cases t.eq_empty_or_nonempty with h h, { subst h, rw finset.sup_empty at ht₂, rw ht₂, simp [eq_singleton_bot_of_Sup_eq_bot_of_nonempty ht₂ hne], }, { rw ht₂, exact t.sup_closed_of_sup_closed h ht₁ hsc, }, end lemma is_sup_closed_compact.well_founded (h : is_sup_closed_compact α) : well_founded ((>) : α → α → Prop) := begin rw rel_embedding.well_founded_iff_no_descending_seq, rintros ⟨a⟩, suffices : Sup (set.range a) ∈ set.range a, { obtain ⟨n, hn⟩ := set.mem_range.mp this, have h' : Sup (set.range a) < a (n+1), { change _ > _, simp [← hn, a.map_rel_iff], }, apply lt_irrefl (a (n+1)), apply lt_of_le_of_lt _ h', apply le_Sup, apply set.mem_range_self, }, apply h (set.range a), { use a 37, apply set.mem_range_self, }, { rintros x y ⟨m, hm⟩ ⟨n, hn⟩, use m ⊔ n, rw [← hm, ← hn], apply a.to_rel_hom.map_sup, }, end lemma is_Sup_finite_compact_iff_all_elements_compact : is_Sup_finite_compact α ↔ (∀ k : α, is_compact_element k) := begin split, { intros h k s hs, obtain ⟨t, ⟨hts, htsup⟩⟩ := h s, use [t, hts], rwa ←htsup, }, { intros h s, obtain ⟨t, ⟨hts, htsup⟩⟩ := h (Sup s) s (by refl), have : Sup s = t.sup id, { suffices : t.sup id ≤ Sup s, by { apply le_antisymm; assumption }, simp only [id.def, finset.sup_le_iff], intros x hx, apply le_Sup, exact hts hx, }, use [t, hts], assumption, }, end lemma well_founded_characterisations : tfae [well_founded ((>) : α → α → Prop), is_Sup_finite_compact α, is_sup_closed_compact α, ∀ k : α, is_compact_element k] := begin tfae_have : 1 → 2, by { exact well_founded.is_Sup_finite_compact α, }, tfae_have : 2 → 3, by { exact is_Sup_finite_compact.is_sup_closed_compact α, }, tfae_have : 3 → 1, by { exact is_sup_closed_compact.well_founded α, }, tfae_have : 2 ↔ 4, by { exact is_Sup_finite_compact_iff_all_elements_compact α }, tfae_finish, end lemma well_founded_iff_is_Sup_finite_compact : well_founded ((>) : α → α → Prop) ↔ is_Sup_finite_compact α := (well_founded_characterisations α).out 0 1 lemma is_Sup_finite_compact_iff_is_sup_closed_compact : is_Sup_finite_compact α ↔ is_sup_closed_compact α := (well_founded_characterisations α).out 1 2 lemma is_sup_closed_compact_iff_well_founded : is_sup_closed_compact α ↔ well_founded ((>) : α → α → Prop) := (well_founded_characterisations α).out 2 0 alias well_founded_iff_is_Sup_finite_compact ↔ _ is_Sup_finite_compact.well_founded alias is_Sup_finite_compact_iff_is_sup_closed_compact ↔ _ is_sup_closed_compact.is_Sup_finite_compact alias is_sup_closed_compact_iff_well_founded ↔ _ well_founded.is_sup_closed_compact end complete_lattice /-- A complete lattice is said to be compactly generated if any element is the `Sup` of compact elements. -/ class is_compactly_generated (α : Type*) [complete_lattice α] : Prop := (exists_Sup_eq : ∀ (x : α), ∃ (s : set α), (∀ x ∈ s, complete_lattice.is_compact_element x) ∧ Sup s = x) section variables {α} [is_compactly_generated α] {a b : α} {s : set α} @[simp] lemma Sup_compact_le_eq (b) : Sup {c : α | complete_lattice.is_compact_element c ∧ c ≤ b} = b := begin rcases is_compactly_generated.exists_Sup_eq b with ⟨s, hs, rfl⟩, exact le_antisymm (Sup_le (λ c hc, hc.2)) (Sup_le_Sup (λ c cs, ⟨hs c cs, le_Sup cs⟩)), end @[simp] theorem Sup_compact_eq_top : Sup {a : α | complete_lattice.is_compact_element a} = ⊤ := begin refine eq.trans (congr rfl (set.ext (λ x, _))) (Sup_compact_le_eq ⊤), exact (and_iff_left le_top).symm, end theorem le_iff_compact_le_imp {a b : α} : a ≤ b ↔ ∀ c : α, complete_lattice.is_compact_element c → c ≤ a → c ≤ b := ⟨λ ab c hc ca, le_trans ca ab, λ h, begin rw [← Sup_compact_le_eq a, ← Sup_compact_le_eq b], exact Sup_le_Sup (λ c hc, ⟨hc.1, h c hc.1 hc.2⟩), end⟩ /-- This property is sometimes referred to as `α` being upper continuous. -/ theorem inf_Sup_eq_of_directed_on (h : directed_on (≤) s): a ⊓ Sup s = ⨆ b ∈ s, a ⊓ b := le_antisymm (begin rw le_iff_compact_le_imp, by_cases hs : s.nonempty, { intros c hc hcinf, rw le_inf_iff at hcinf, rw complete_lattice.is_compact_element_iff_le_of_directed_Sup_le at hc, rcases hc s hs h hcinf.2 with ⟨d, ds, cd⟩, exact (le_inf hcinf.1 cd).trans (le_bsupr d ds) }, { rw set.not_nonempty_iff_eq_empty at hs, simp [hs] } end) supr_inf_le_inf_Sup /-- This property is equivalent to `α` being upper continuous. -/ theorem inf_Sup_eq_supr_inf_sup_finset : a ⊓ Sup s = ⨆ (t : finset α) (H : ↑t ⊆ s), a ⊓ (t.sup id) := le_antisymm (begin rw le_iff_compact_le_imp, intros c hc hcinf, rw le_inf_iff at hcinf, rcases hc s hcinf.2 with ⟨t, ht1, ht2⟩, exact (le_inf hcinf.1 ht2).trans (le_bsupr t ht1), end) (supr_le $ λ t, supr_le $ λ h, inf_le_inf_left _ ((finset.sup_eq_Sup t).symm ▸ (Sup_le_Sup h))) theorem complete_lattice.independent_iff_finite {s : set α} : complete_lattice.independent s ↔ ∀ t : finset α, ↑t ⊆ s → complete_lattice.independent (↑t : set α) := ⟨λ hs t ht, hs.mono ht, λ h a ha, begin rw [disjoint_iff, inf_Sup_eq_supr_inf_sup_finset, supr_eq_bot], intro t, rw [supr_eq_bot, finset.sup_eq_Sup], intro ht, classical, have h' := (h (insert a t) _ (t.mem_insert_self a)).eq_bot, { rwa [finset.coe_insert, set.insert_diff_self_of_not_mem] at h', exact λ con, ((set.mem_diff a).1 (ht con)).2 (set.mem_singleton a) }, { rw [finset.coe_insert, set.insert_subset], exact ⟨ha, set.subset.trans ht (set.diff_subset _ _)⟩ } end⟩ lemma complete_lattice.independent_Union_of_directed {η : Type*} {s : η → set α} (hs : directed (⊆) s) (h : ∀ i, complete_lattice.independent (s i)) : complete_lattice.independent (⋃ i, s i) := begin by_cases hη : nonempty η, { resetI, rw complete_lattice.independent_iff_finite, intros t ht, obtain ⟨I, fi, hI⟩ := set.finite_subset_Union t.finite_to_set ht, obtain ⟨i, hi⟩ := hs.finset_le fi.to_finset, exact (h i).mono (set.subset.trans hI $ set.bUnion_subset $ λ j hj, hi j (fi.mem_to_finset.2 hj)) }, { rintros a ⟨_, ⟨i, _⟩, _⟩, exfalso, exact hη ⟨i⟩, }, end lemma complete_lattice.independent_sUnion_of_directed {s : set (set α)} (hs : directed_on (⊆) s) (h : ∀ a ∈ s, complete_lattice.independent a) : complete_lattice.independent (⋃₀ s) := by rw set.sUnion_eq_Union; exact complete_lattice.independent_Union_of_directed hs.directed_coe (by simpa using h) end namespace complete_lattice lemma compactly_generated_of_well_founded (h : well_founded ((>) : α → α → Prop)) : is_compactly_generated α := begin rw [well_founded_iff_is_Sup_finite_compact, is_Sup_finite_compact_iff_all_elements_compact] at h, -- x is the join of the set of compact elements {x} exact ⟨λ x, ⟨{x}, ⟨λ x _, h x, Sup_singleton⟩⟩⟩, end /-- A compact element `k` has the property that any `b < `k lies below a "maximal element below `k`", which is to say `[⊥, k]` is coatomic. -/ theorem Iic_coatomic_of_compact_element {k : α} (h : is_compact_element k) : is_coatomic (set.Iic k) := ⟨λ ⟨b, hbk⟩, begin by_cases htriv : b = k, { left, ext, simp only [htriv, set.Iic.coe_top, subtype.coe_mk], }, right, rcases zorn.zorn_partial_order₀ (set.Iio k) _ b (lt_of_le_of_ne hbk htriv) with ⟨a, a₀, ba, h⟩, { refine ⟨⟨a, le_of_lt a₀⟩, ⟨ne_of_lt a₀, λ c hck, by_contradiction $ λ c₀, _⟩, ba⟩, cases h c.1 (lt_of_le_of_ne c.2 (λ con, c₀ (subtype.ext con))) hck.le, exact lt_irrefl _ hck, }, { intros S SC cC I IS, by_cases hS : S.nonempty, { exact ⟨Sup S, h.directed_Sup_lt_of_lt hS cC.directed_on SC, λ _, le_Sup⟩, }, exact ⟨b, lt_of_le_of_ne hbk htriv, by simp only [set.not_nonempty_iff_eq_empty.mp hS, set.mem_empty_eq, forall_const, forall_prop_of_false, not_false_iff]⟩, }, end⟩ lemma coatomic_of_top_compact (h : is_compact_element (⊤ : α)) : is_coatomic α := (@order_iso.Iic_top α _).is_coatomic_iff.mp (Iic_coatomic_of_compact_element h) end complete_lattice section variables [is_modular_lattice α] [is_compactly_generated α] @[priority 100] instance is_atomic_of_is_complemented [is_complemented α] : is_atomic α := ⟨λ b, begin by_cases h : {c : α | complete_lattice.is_compact_element c ∧ c ≤ b} ⊆ {⊥}, { left, rw [← Sup_compact_le_eq b, Sup_eq_bot], exact h }, { rcases set.not_subset.1 h with ⟨c, ⟨hc, hcb⟩, hcbot⟩, right, have hc' := complete_lattice.Iic_coatomic_of_compact_element hc, rw ← is_atomic_iff_is_coatomic at hc', haveI := hc', obtain con | ⟨a, ha, hac⟩ := eq_bot_or_exists_atom_le (⟨c, le_refl c⟩ : set.Iic c), { exfalso, apply hcbot, simp only [subtype.ext_iff, set.Iic.coe_bot, subtype.coe_mk] at con, exact con }, rw [← subtype.coe_le_coe, subtype.coe_mk] at hac, exact ⟨a, ha.of_is_atom_coe_Iic, hac.trans hcb⟩ }, end⟩ /-- See Lemma 5.1, Călugăreanu -/ @[priority 100] instance is_atomistic_of_is_complemented [is_complemented α] : is_atomistic α := ⟨λ b, ⟨{a | is_atom a ∧ a ≤ b}, begin symmetry, have hle : Sup {a : α | is_atom a ∧ a ≤ b} ≤ b := (Sup_le $ λ _, and.right), apply (lt_or_eq_of_le hle).resolve_left (λ con, _), obtain ⟨c, hc⟩ := exists_is_compl (⟨Sup {a : α | is_atom a ∧ a ≤ b}, hle⟩ : set.Iic b), obtain rfl | ⟨a, ha, hac⟩ := eq_bot_or_exists_atom_le c, { exact ne_of_lt con (subtype.ext_iff.1 (eq_top_of_is_compl_bot hc)) }, { apply ha.1, rw eq_bot_iff, apply le_trans (le_inf _ hac) hc.1, rw [← subtype.coe_le_coe, subtype.coe_mk], exact le_Sup ⟨ha.of_is_atom_coe_Iic, a.2⟩ } end, λ _, and.left⟩⟩ /-- See Theorem 6.6, Călugăreanu -/ theorem is_complemented_of_Sup_atoms_eq_top (h : Sup {a : α | is_atom a} = ⊤) : is_complemented α := ⟨λ b, begin obtain ⟨s, ⟨s_ind, b_inf_Sup_s, s_atoms⟩, s_max⟩ := zorn.zorn_subset {s : set α | complete_lattice.independent s ∧ b ⊓ Sup s = ⊥ ∧ ∀ a ∈ s, is_atom a} _, { refine ⟨Sup s, le_of_eq b_inf_Sup_s, _⟩, rw [← h, Sup_le_iff], intros a ha, rw ← inf_eq_left, refine (eq_bot_or_eq_of_le_atom ha inf_le_left).resolve_left (λ con, ha.1 _), rw [eq_bot_iff, ← con], refine le_inf (le_refl a) ((le_Sup _).trans le_sup_right), rw ← disjoint_iff at *, have a_dis_Sup_s : disjoint a (Sup s) := con.mono_right le_sup_right, rw ← s_max (s ∪ {a}) ⟨λ x hx, _, ⟨_, λ x hx, _⟩⟩ (set.subset_union_left _ _), { exact set.mem_union_right _ (set.mem_singleton _) }, { rw [set.mem_union, set.mem_singleton_iff] at hx, by_cases xa : x = a, { simp only [xa, set.mem_singleton, set.insert_diff_of_mem, set.union_singleton], exact con.mono_right (le_trans (Sup_le_Sup (set.diff_subset s {a})) le_sup_right) }, { have h : (s ∪ {a}) \ {x} = (s \ {x}) ∪ {a}, { simp only [set.union_singleton], rw set.insert_diff_of_not_mem, rw set.mem_singleton_iff, exact ne.symm xa }, rw [h, Sup_union, Sup_singleton], apply (s_ind (hx.resolve_right xa)).disjoint_sup_right_of_disjoint_sup_left (a_dis_Sup_s.mono_right _).symm, rw [← Sup_insert, set.insert_diff_singleton, set.insert_eq_of_mem (hx.resolve_right xa)] } }, { rw [Sup_union, Sup_singleton, ← disjoint_iff], exact b_inf_Sup_s.disjoint_sup_right_of_disjoint_sup_left con.symm }, { rw [set.mem_union, set.mem_singleton_iff] at hx, cases hx, { exact s_atoms x hx }, { rw hx, exact ha } } }, { intros c hc1 hc2, refine ⟨⋃₀ c, ⟨complete_lattice.independent_sUnion_of_directed hc2.directed_on (λ s hs, (hc1 hs).1), _, λ a ha, _⟩, λ _, set.subset_sUnion_of_mem⟩, { rw [Sup_sUnion, ← Sup_image, inf_Sup_eq_of_directed_on, supr_eq_bot], { intro i, rw supr_eq_bot, intro hi, obtain ⟨x, xc, rfl⟩ := (set.mem_image _ _ _).1 hi, exact (hc1 xc).2.1 }, { rw directed_on_image, refine hc2.directed_on.mono (λ s t, Sup_le_Sup) } }, { rcases set.mem_sUnion.1 ha with ⟨s, sc, as⟩, exact (hc1 sc).2.2 a as } } end⟩ /-- See Theorem 6.6, Călugăreanu -/ theorem is_complemented_of_is_atomistic [is_atomistic α] : is_complemented α := is_complemented_of_Sup_atoms_eq_top Sup_atoms_eq_top theorem is_complemented_iff_is_atomistic : is_complemented α ↔ is_atomistic α := begin split; introsI, { exact is_atomistic_of_is_complemented }, { exact is_complemented_of_is_atomistic } end end
f126ca3c87f942c0c298fecbff1aa2ba73bca958
1f6fe2f89976b14a4567ab298c35792b21f2e50b
/spectrum/basic.hlean
851c89d458583abfa591c3a9e2b15446a2ec8e89
[ "Apache-2.0" ]
permissive
jonas-frey/Spectral
e5c1c2f7bcac26aa55f7b1e041a81272a146198d
72d521091525a4bc9a31cac859840efe9461cf66
refs/heads/master
1,610,235,743,345
1,505,417,795,000
1,505,417,795,000
102,653,342
0
0
null
1,504,728,483,000
1,504,728,483,000
null
UTF-8
Lean
false
false
37,532
hlean
/- Copyright (c) 2016 Michael Shulman. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Michael Shulman, Floris van Doorn, Egbert Rijke, Stefano Piceghello, Yuri Sulyma -/ import homotopy.LES_of_homotopy_groups ..algebra.splice ..algebra.seq_colim ..homotopy.EM ..homotopy.fwedge ..pointed_cubes open eq nat int susp pointed pmap sigma is_equiv equiv fiber algebra trunc trunc_index pi group succ_str EM EM.ops function unit lift is_trunc /--------------------- Basic definitions ---------------------/ /- The basic definitions of spectra and prespectra make sense for any successor-structure. -/ structure gen_prespectrum (N : succ_str) := (deloop : N → Type*) (glue : Π(n:N), (deloop n) →* (Ω (deloop (S n)))) attribute gen_prespectrum.deloop [coercion] structure is_spectrum [class] {N : succ_str} (E : gen_prespectrum N) := (is_equiv_glue : Πn, is_equiv (gen_prespectrum.glue E n)) attribute is_spectrum.is_equiv_glue [instance] structure gen_spectrum (N : succ_str) := (to_prespectrum : gen_prespectrum N) (to_is_spectrum : is_spectrum to_prespectrum) attribute gen_spectrum.to_prespectrum [coercion] attribute gen_spectrum.to_is_spectrum [instance] attribute gen_spectrum._trans_of_to_prespectrum [unfold 2] -- Classically, spectra and prespectra use the successor structure +ℕ. -- But we will use +ℤ instead, to reduce case analysis later on. abbreviation prespectrum := gen_prespectrum +ℤ definition prespectrum.mk (Y : ℤ → Type*) (e : Π(n : ℤ), Y n →* Ω (Y (n+1))) : prespectrum := gen_prespectrum.mk Y e abbreviation spectrum := gen_spectrum +ℤ abbreviation spectrum.mk (Y : prespectrum) (e : is_spectrum Y) : spectrum := gen_spectrum.mk Y e namespace spectrum definition glue [unfold 2] {{N : succ_str}} := @gen_prespectrum.glue N --definition glue := (@gen_prespectrum.glue +ℤ) definition equiv_glue {N : succ_str} (E : gen_prespectrum N) [H : is_spectrum E] (n:N) : (E n) ≃* (Ω (E (S n))) := pequiv_of_pmap (glue E n) (is_spectrum.is_equiv_glue E n) definition equiv_glue2 (Y : spectrum) (n : ℤ) : Ω (Ω (Y (n+2))) ≃* Y n := begin refine (!equiv_glue ⬝e* loop_pequiv_loop (!equiv_glue ⬝e* loop_pequiv_loop _))⁻¹ᵉ*, refine pequiv_of_eq (ap Y _), exact add.assoc n 1 1 end definition gluen {N : succ_str} (X : gen_prespectrum N) (n : N) (k : ℕ) : X n →* Ω[k] (X (n +' k)) := by induction k with k f; reflexivity; exact !loopn_succ_in⁻¹ᵉ* ∘* Ω→[k] (glue X (n +' k)) ∘* f -- note: the forward map is (currently) not definitionally equal to gluen. Is that a problem? definition equiv_gluen {N : succ_str} (X : gen_spectrum N) (n : N) (k : ℕ) : X n ≃* Ω[k] (X (n +' k)) := by induction k with k f; reflexivity; exact f ⬝e* (loopn_pequiv_loopn k (equiv_glue X (n +' k)) ⬝e* !loopn_succ_in⁻¹ᵉ*) definition equiv_gluen_inv_succ {N : succ_str} (X : gen_spectrum N) (n : N) (k : ℕ) : (equiv_gluen X n (k+1))⁻¹ᵉ* ~* (equiv_gluen X n k)⁻¹ᵉ* ∘* Ω→[k] (equiv_glue X (n +' k))⁻¹ᵉ* ∘* !loopn_succ_in := begin refine !trans_pinv ⬝* pwhisker_left _ _, refine !trans_pinv ⬝* _, refine pwhisker_left _ !pinv_pinv end definition succ_str_add_eq_int_add (n : ℤ) (m : ℕ) : @succ_str.add sint n m = n + m := begin induction m with m IH, { symmetry, exact add_zero n }, { exact ap int.succ IH ⬝ add.assoc n m 1 } end -- a square when we compose glue with transporting over a path in N definition glue_ptransport {N : succ_str} (X : gen_prespectrum N) {n n' : N} (p : n = n') : glue X n' ∘* ptransport X p ~* Ω→ (ptransport X (ap S p)) ∘* glue X n := by induction p; exact !pcompose_pid ⬝* !pid_pcompose⁻¹* ⬝* pwhisker_right _ !ap1_pid⁻¹* -- Sometimes an ℕ-indexed version does arise naturally, however, so -- we give a standard way to extend an ℕ-indexed (pre)spectrum to a -- ℤ-indexed one. definition psp_of_nat_indexed [constructor] (E : gen_prespectrum +ℕ) : gen_prespectrum +ℤ := gen_prespectrum.mk (λ(n:ℤ), match n with | of_nat k := E k | neg_succ_of_nat k := Ω[succ k] (E 0) end) begin intros n, cases n with n n: esimp, { exact (gen_prespectrum.glue E n) }, cases n with n, { exact (pid _) }, { exact (pid _) } end definition is_spectrum_of_nat_indexed [instance] (E : gen_prespectrum +ℕ) [H : is_spectrum E] : is_spectrum (psp_of_nat_indexed E) := begin apply is_spectrum.mk, intros n, cases n with n n: esimp, { apply is_spectrum.is_equiv_glue }, cases n with n: apply is_equiv_id end protected definition of_nat_indexed (E : gen_prespectrum +ℕ) [H : is_spectrum E] : spectrum := spectrum.mk (psp_of_nat_indexed E) (is_spectrum_of_nat_indexed E) -- In fact, a (pre)spectrum indexed on any pointed successor structure -- gives rise to one indexed on +ℕ, so in this sense +ℤ is a -- "universal" successor structure for indexing spectra. definition succ_str.of_nat {N : succ_str} (z : N) : ℕ → N | succ_str.of_nat zero := z | succ_str.of_nat (succ k) := S (succ_str.of_nat k) definition psp_of_gen_indexed [constructor] {N : succ_str} (z : N) (E : gen_prespectrum N) : prespectrum := psp_of_nat_indexed (gen_prespectrum.mk (λn, E (succ_str.of_nat z n)) (λn, gen_prespectrum.glue E (succ_str.of_nat z n))) definition is_spectrum_of_gen_indexed [instance] {N : succ_str} (z : N) (E : gen_prespectrum N) [H : is_spectrum E] : is_spectrum (psp_of_gen_indexed z E) := begin apply is_spectrum_of_nat_indexed, apply is_spectrum.mk, intros n, esimp, apply is_spectrum.is_equiv_glue end protected definition of_gen_indexed [constructor] {N : succ_str} (z : N) (E : gen_spectrum N) : spectrum := gen_spectrum.mk (psp_of_gen_indexed z E) (is_spectrum_of_gen_indexed z E) -- Generally it's easiest to define a spectrum by giving 'equiv's -- directly. This works for any indexing succ_str. protected definition MK [constructor] {N : succ_str} (deloop : N → Type*) (glue : Π(n:N), (deloop n) ≃* (Ω (deloop (S n)))) : gen_spectrum N := gen_spectrum.mk (gen_prespectrum.mk deloop (λ(n:N), glue n)) (begin apply is_spectrum.mk, intros n, esimp, apply pequiv.to_is_equiv -- Why doesn't typeclass inference find this? end) -- Finally, we combine them and give a way to produce a (ℤ-)spectrum from a ℕ-indexed family of 'equiv's. protected definition Mk [constructor] (deloop : ℕ → Type*) (glue : Π(n:ℕ), (deloop n) ≃* (Ω (deloop (nat.succ n)))) : spectrum := spectrum.of_nat_indexed (spectrum.MK deloop glue) ------------------------------ -- Maps and homotopies of (pre)spectra ------------------------------ -- These make sense for any succ_str. structure smap {N : succ_str} (E F : gen_prespectrum N) := (to_fun : Π(n:N), E n →* F n) (glue_square : Π(n:N), psquare (to_fun n) (Ω→ (to_fun (S n))) (glue E n) (glue F n) ) definition smap_sigma {N : succ_str} (X Y : gen_prespectrum N) : Type := Σ (to_fun : Π(n:N), X n →* Y n), Π(n:N), psquare (to_fun n) (Ω→ (to_fun (S n))) (glue X n) (glue Y n) open smap infix ` →ₛ `:30 := smap attribute smap.to_fun [coercion] definition smap_to_sigma [unfold 4] {N : succ_str} {X Y : gen_prespectrum N} (f : X →ₛ Y) : smap_sigma X Y := begin induction f with f fsq, exact sigma.mk f fsq, end definition smap_to_struc [unfold 4] {N : succ_str} {X Y : gen_prespectrum N} (f : smap_sigma X Y) : X →ₛ Y := begin induction f with f fsq, exact smap.mk f fsq, end definition smap_to_sigma_isretr {N : succ_str} {X Y : gen_prespectrum N} (f : smap_sigma X Y) : smap_to_sigma (smap_to_struc f) = f := begin induction f, reflexivity end definition smap_to_sigma_issec {N : succ_str} {X Y : gen_prespectrum N} (f : X →ₛ Y) : smap_to_struc (smap_to_sigma f) = f := begin induction f, reflexivity end definition smap_sigma_equiv [constructor] {N : succ_str} (X Y : gen_prespectrum N) : (smap_sigma X Y) ≃ (X →ₛ Y) := begin fapply equiv.mk, exact smap_to_struc, fapply adjointify, exact smap_to_sigma, exact smap_to_sigma_issec, exact smap_to_sigma_isretr end -- A version of 'glue_square' in the spectrum case that uses 'equiv_glue' definition sglue_square {N : succ_str} {E F : gen_spectrum N} (f : E →ₛ F) (n : N) : psquare (f n) (Ω→ (f (S n))) (equiv_glue E n) (equiv_glue F n) := glue_square f n definition sid [constructor] [refl] {N : succ_str} (E : gen_prespectrum N) : E →ₛ E := smap.mk (λ n, pid (E n)) (λ n, psquare_of_phtpy_bot (ap1_pid) (psquare_of_pid_top_bot (phomotopy.rfl))) --print sid -- smap.mk (λn, pid (E n)) -- (λn, calc glue E n ∘* pid (E n) ~* glue E n : pcompose_pid -- ... ~* pid (Ω(E (S n))) ∘* glue E n : pid_pcompose -- ... ~* Ω→(pid (E (S n))) ∘* glue E n : pwhisker_right (glue E n) ap1_pid⁻¹*) definition scompose [trans] {N : succ_str} {X Y Z : gen_prespectrum N} (g : Y →ₛ Z) (f : X →ₛ Y) : X →ₛ Z := smap.mk (λn, g n ∘* f n) (λ n, psquare_of_phtpy_bot (ap1_pcompose (g (S n)) (f (S n))) (psquare_hcompose (glue_square f n) (glue_square g n))) /- (λn, calc glue Z n ∘* to_fun g n ∘* to_fun f n ~* (glue Z n ∘* to_fun g n) ∘* to_fun f n : passoc ... ~* (Ω→(to_fun g (S n)) ∘* glue Y n) ∘* to_fun f n : pwhisker_right (to_fun f n) (glue_square g n) ... ~* Ω→(to_fun g (S n)) ∘* (glue Y n ∘* to_fun f n) : passoc ... ~* Ω→(to_fun g (S n)) ∘* (Ω→ (f (S n)) ∘* glue X n) : pwhisker_left (Ω→(to_fun g (S n))) (glue_square f n) ... ~* (Ω→(to_fun g (S n)) ∘* Ω→(f (S n))) ∘* glue X n : passoc ... ~* Ω→(to_fun g (S n) ∘* to_fun f (S n)) ∘* glue X n : pwhisker_right (glue X n) (ap1_pcompose _ _)) -/ infixr ` ∘ₛ `:60 := scompose definition szero [constructor] {N : succ_str} (E F : gen_prespectrum N) : E →ₛ F := smap.mk (λn, pconst (E n) (F n)) (λn, psquare_of_phtpy_bot (ap1_pconst (E (S n)) (F (S n))) (psquare_of_pconst_top_bot (glue E n) (glue F n))) /- (λn, calc glue F n ∘* pconst (E n) (F n) ~* pconst (E n) (Ω(F (S n))) : pcompose_pconst ... ~* pconst (Ω(E (S n))) (Ω(F (S n))) ∘* glue E n : pconst_pcompose ... ~* Ω→(pconst (E (S n)) (F (S n))) ∘* glue E n : pwhisker_right (glue E n) (ap1_pconst _ _)) -/ definition stransport [constructor] {N : succ_str} {A : Type} {a a' : A} (p : a = a') (E : A → gen_prespectrum N) : E a →ₛ E a' := smap.mk (λn, ptransport (λa, E a n) p) begin intro n, induction p, exact !pcompose_pid ⬝* !pid_pcompose⁻¹* ⬝* pwhisker_right _ !ap1_pid⁻¹*, end structure shomotopy {N : succ_str} {E F : gen_prespectrum N} (f g : E →ₛ F) := (to_phomotopy : Πn, f n ~* g n) (glue_homotopy : Πn, ptube_v (to_phomotopy n) (ap1_phomotopy (to_phomotopy (S n))) (glue_square f n) (glue_square g n)) /- (glue_homotopy : Πn, phsquare (pwhisker_left (glue F n) (to_phomotopy n)) (pwhisker_right (glue E n) (ap1_phomotopy (to_phomotopy (S n)))) (glue_square f n) (glue_square g n)) -/ infix ` ~ₛ `:50 := shomotopy definition shomotopy_compose {N : succ_str} {E F : gen_prespectrum N} {f g h : E →ₛ F} (p : g ~ₛ h) (q : f ~ₛ g) : f ~ₛ h := shomotopy.mk (λn, (shomotopy.to_phomotopy q n) ⬝* (shomotopy.to_phomotopy p n)) begin intro n, unfold [ptube_v], rewrite (pwhisker_left_trans _), rewrite ap1_phomotopy_trans, rewrite (pwhisker_right_trans _), exact phhconcat ((shomotopy.glue_homotopy q) n) ((shomotopy.glue_homotopy p) n) end definition shomotopy_inverse {N : succ_str} {E F : gen_prespectrum N} {f g : E →ₛ F} (p : f ~ₛ g) : g ~ₛ f := shomotopy.mk (λn, (shomotopy.to_phomotopy p n)⁻¹*) begin intro n, unfold [ptube_v], rewrite (pwhisker_left_symm _ _), rewrite [-ap1_phomotopy_symm], rewrite (pwhisker_right_symm _ _), exact phhinverse ((shomotopy.glue_homotopy p) n) end /- Comparing the structure of shomotopy with a Σ-type -/ definition shomotopy_sigma {N : succ_str} {X Y : gen_prespectrum N} (f g : X →ₛ Y) : Type := Σ (phtpy : Π (n : N), f n ~* g n), Πn, ptube_v (phtpy n) (ap1_phomotopy (phtpy (S n))) (glue_square f n) (glue_square g n) definition shomotopy_to_sigma [unfold 6] {N : succ_str} {X Y : gen_prespectrum N} {f g : X →ₛ Y} (H : f ~ₛ g) : shomotopy_sigma f g := begin induction H with H Hsq, exact sigma.mk H Hsq, end definition shomotopy_to_struct [unfold 6] {N : succ_str} {X Y : gen_prespectrum N} {f g : X →ₛ Y} (H : shomotopy_sigma f g) : f ~ₛ g := begin induction H with H Hsq, exact shomotopy.mk H Hsq, end definition shomotopy_to_sigma_isretr {N : succ_str} {X Y : gen_prespectrum N} {f g : X →ₛ Y} (H : shomotopy_sigma f g) : shomotopy_to_sigma (shomotopy_to_struct H) = H := begin induction H with H Hsq, reflexivity end definition shomotopy_to_sigma_issec {N : succ_str} {X Y : gen_prespectrum N} {f g : X →ₛ Y} (H : f ~ₛ g) : shomotopy_to_struct (shomotopy_to_sigma H) = H := begin induction H, reflexivity end definition shomotopy_sigma_equiv [constructor] {N : succ_str} {X Y : gen_prespectrum N} (f g : X →ₛ Y) : shomotopy_sigma f g ≃ (f ~ₛ g) := begin fapply equiv.mk, exact shomotopy_to_struct, fapply adjointify, exact shomotopy_to_sigma, exact shomotopy_to_sigma_issec, exact shomotopy_to_sigma_isretr, end /- equivalence of shomotopy and eq -/ /- definition eq_of_shomotopy_pfun {N : succ_str} {X Y : gen_prespectrum N} {f g : X →ₛ Y} (H : f ~ₛ g) (n : N) : f n = g n := begin fapply eq_of_fn_eq_fn (smap_sigma_equiv X Y), repeat exact sorry end-/ definition fam_phomotopy_of_eq {N : Type} {X Y: N → Type*} (f g : Π n, X n →* Y n) : (f = g) ≃ (Π n, f n ~* g n) := (eq.eq_equiv_homotopy) ⬝e pi_equiv_pi_right (λ n, pmap_eq_equiv (f n) (g n)) /- definition phomotopy_rec_on_eq [recursor] {k' : ppi B x₀} {Q : (k ~* k') → Type} (p : k ~* k') (H : Π(q : k = k'), Q (phomotopy_of_eq q)) : Q p := phomotopy_of_eq_of_phomotopy p ▸ H (eq_of_phomotopy p) -/ definition fam_phomotopy_rec_on_eq {N : Type} {X Y : N → Type*} (f g : Π n, X n →* Y n) {Q : (Π n, f n ~* g n) → Type} (p : Π n, f n ~* g n) (H : Π (q : f = g), Q (fam_phomotopy_of_eq f g q)) : Q p := begin refine _ ▸ H ((fam_phomotopy_of_eq f g)⁻¹ᵉ p), have q : to_fun (fam_phomotopy_of_eq f g) (to_fun (fam_phomotopy_of_eq f g)⁻¹ᵉ p) = p, from right_inv (fam_phomotopy_of_eq f g) p, krewrite q end /- definition phomotopy_rec_idp [recursor] {Q : Π {k' : ppi B x₀}, (k ~* k') → Type} (q : Q (phomotopy.refl k)) {k' : ppi B x₀} (H : k ~* k') : Q H := begin induction H using phomotopy_rec_on_eq with t, induction t, exact eq_phomotopy_refl_phomotopy_of_eq_refl k ▸ q, end -/ --set_option pp.coercions true definition fam_phomotopy_rec_idp {N : Type} {X Y : N → Type*} (f : Π n, X n →* Y n) (Q : Π (g : Π n, X n →* Y n) (H : Π n, f n ~* g n), Type) (q : Q f (λ n, phomotopy.rfl)) (g : Π n, X n →* Y n) (H : Π n, f n ~* g n) : Q g H := begin fapply fam_phomotopy_rec_on_eq, refine λ(p : f = g), _, --ugly trick intro p, induction p, exact q, end definition eq_of_shomotopy {N : succ_str} {X Y : gen_prespectrum N} {f g : X →ₛ Y} (H : f ~ₛ g) : f = g := begin fapply eq_of_fn_eq_fn (smap_sigma_equiv X Y)⁻¹ᵉ, induction f with f fsq, induction g with g gsq, induction H with H Hsq, fapply sigma_eq, fapply eq_of_homotopy, intro n, fapply eq_of_phomotopy, exact H n, fapply pi_pathover_constant, intro n, esimp at *, revert g H gsq Hsq n, refine fam_phomotopy_rec_idp f _ _, intro gsq Hsq n, refine change_path _ _, -- have p : eq_of_homotopy (λ n, eq_of_phomotopy phomotopy.rfl) = refl f, reflexivity, refine (eq_of_homotopy_eta rfl)⁻¹ ⬝ _, fapply ap (eq_of_homotopy), fapply eq_of_homotopy, intro n, refine (eq_of_phomotopy_refl _)⁻¹, -- fapply eq_of_phomotopy, fapply pathover_idp_of_eq, note Hsq' := ptube_v_eq_bot phomotopy.rfl (ap1_phomotopy_refl _) (fsq n) (gsq n) (Hsq n), unfold ptube_v at *, unfold phsquare at *, refine _ ⬝ Hsq'⁻¹ ⬝ _, refine (trans_refl (fsq n))⁻¹ ⬝ _, exact idp ◾** (pwhisker_right_refl _ _)⁻¹, refine _ ⬝ (refl_trans (gsq n)), refine _ ◾** idp, exact pwhisker_left_refl _ _, end ------------------------------ -- Equivalences of prespectra ------------------------------ definition spectrum_pequiv_of_pequiv_succ {E F : spectrum} (n : ℤ) (e : E (n + 1) ≃* F (n + 1)) : E n ≃* F n := equiv_glue E n ⬝e* loop_pequiv_loop e ⬝e* (equiv_glue F n)⁻¹ᵉ* definition spectrum_pequiv_of_nat {E F : spectrum} (e : Π(n : ℕ), E n ≃* F n) (n : ℤ) : E n ≃* F n := begin induction n with n n, exact e n, induction n with n IH, { exact spectrum_pequiv_of_pequiv_succ -[1+0] (e 0) }, { exact spectrum_pequiv_of_pequiv_succ -[1+succ n] IH } end definition spectrum_pequiv_of_nat_add {E F : spectrum} (m : ℕ) (e : Π(n : ℕ), E (n + m) ≃* F (n + m)) : Π(n : ℤ), E n ≃* F n := begin apply spectrum_pequiv_of_nat, refine nat.rec_down _ m e _, intro n f k, cases k with k, exact spectrum_pequiv_of_pequiv_succ _ (f 0), exact pequiv_ap E (ap of_nat (succ_add k n)) ⬝e* f k ⬝e* pequiv_ap F (ap of_nat (succ_add k n))⁻¹ end definition is_contr_spectrum_of_nat {E : spectrum} (e : Π(n : ℕ), is_contr (E n)) (n : ℤ) : is_contr (E n) := begin have Πn, is_contr (E (n + 1)) → is_contr (E n), from λn H, @(is_trunc_equiv_closed_rev -2 !equiv_glue) (is_contr_loop_of_is_contr H), induction n with n n, exact e n, induction n with n IH, { exact this -[1+0] (e 0) }, { exact this -[1+succ n] IH } end structure is_sequiv {N : succ_str} {E F : gen_prespectrum N} (f : E →ₛ F) : Type := (to_linv : F →ₛ E) (is_retr : to_linv ∘ₛf ~ₛ sid E) (to_rinv : F →ₛ E) (is_sec : f ∘ₛ to_rinv ~ₛ sid F) structure sequiv {N : succ_str} (E F : gen_prespectrum N) : Type := (to_fun : E →ₛ F) (to_is_sequiv : is_sequiv to_fun) infix ` ≃ₛ ` : 25 := sequiv definition is_sequiv_smap {N : succ_str} {E F : gen_prespectrum N} (f : E →ₛ F) : Type := Π (n: N), is_equiv (f n) definition is_sequiv_of_smap_pequiv {N : succ_str} {E F : gen_prespectrum N} (f : E →ₛ F) (H : is_sequiv_smap f) (n : N) : E n ≃* F n := begin fapply pequiv_of_pmap, exact f n, fapply H, end definition is_sequiv_of_smap_inv {N : succ_str} {E F : gen_prespectrum N} (f : E →ₛ F) (H : is_sequiv_smap f) : F →ₛ E := begin fapply smap.mk, intro n, exact (is_sequiv_of_smap_pequiv f H n)⁻¹ᵉ*, intro n, refine _ ⬝vp* (to_pinv_loopn_pequiv_loopn 1 (is_sequiv_of_smap_pequiv f H (S n)))⁻¹*, fapply phinverse, exact glue_square f n, end local postfix `⁻¹ˢ` : (max + 1) := is_sequiv_of_smap_inv definition is_sequiv_of_smap_isretr {N : succ_str} {E F : gen_prespectrum N} (f : E →ₛ F) (H : is_sequiv_smap f) : is_sequiv_of_smap_inv f H ∘ₛ f ~ₛ sid E := begin fapply shomotopy.mk, intro n, fapply pleft_inv, intro n, refine _ ⬝hp** _, repeat exact sorry, end definition is_sequiv_of_smap_issec {N : succ_str} {E F : gen_prespectrum N} (f : E →ₛ F) (H : is_sequiv_smap f) : f ∘ₛ is_sequiv_of_smap_inv f H ~ₛ sid F := begin repeat exact sorry end definition is_sequiv_of_smap {N : succ_str} {E F : gen_prespectrum N} (f : E →ₛ F) : is_sequiv_smap f → is_sequiv f := begin intro H, fapply is_sequiv.mk, fapply is_sequiv_of_smap_inv f H, fapply is_sequiv_of_smap_isretr f H, fapply is_sequiv_of_smap_inv f H, fapply is_sequiv_of_smap_issec f H, end /--------- Fibers ----------/ definition sfiber [constructor] {N : succ_str} {X Y : gen_spectrum N} (f : X →ₛ Y) : gen_spectrum N := spectrum.MK (λn, pfiber (f n)) (λn, (loop_pfiber (f (S n)))⁻¹ᵉ* ∘*ᵉ pfiber_pequiv_of_square _ _ (sglue_square f n)) /- the map from the fiber to the domain -/ definition spoint {N : succ_str} {X Y : gen_spectrum N} (f : X →ₛ Y) : sfiber f →ₛ X := smap.mk (λn, ppoint (f n)) begin intro n, refine _ ⬝* !passoc, refine _ ⬝* pwhisker_right _ !ppoint_loop_pfiber_inv⁻¹*, rexact (pfiber_pequiv_of_square_ppoint (equiv_glue X n) (equiv_glue Y n) (sglue_square f n))⁻¹* end definition scompose_spoint {N : succ_str} {X Y : gen_spectrum N} (f : X →ₛ Y) : f ∘ₛ spoint f ~ₛ !szero := begin fapply shomotopy.mk, { intro n, exact pcompose_ppoint (f n) }, { intro n, exact sorry } end /--------------------- Homotopy groups ---------------------/ -- Here we start to reap the rewards of using ℤ-indexing: we can -- read off the homotopy groups without any tedious case-analysis of -- n. We increment by 2 in order to ensure that they are all -- automatically abelian groups. definition shomotopy_group (n : ℤ) (E : spectrum) : AbGroup := πag[2] (E (2 - n)) notation `πₛ[`:95 n:0 `]`:0 := shomotopy_group n definition shomotopy_group_fun (n : ℤ) {E F : spectrum} (f : E →ₛ F) : πₛ[n] E →g πₛ[n] F := proof π→g[2] (f (2 - n)) qed definition shomotopy_group_isomorphism_of_pequiv (n : ℤ) {E F : spectrum} (f : Πn, E n ≃* F n) : πₛ[n] E ≃g πₛ[n] F := proof homotopy_group_isomorphism_of_pequiv 1 (f (2 - n)) qed definition shomotopy_group_isomorphism_of_pequiv_nat (n : ℕ) {E F : spectrum} (f : Πn, E n ≃* F n) : πₛ[n] E ≃g πₛ[n] F := shomotopy_group_isomorphism_of_pequiv n (spectrum_pequiv_of_nat f) notation `πₛ→[`:95 n:0 `]`:0 := shomotopy_group_fun n /- properties about homotopy groups -/ definition equiv_glue_neg (X : spectrum) (n : ℤ) : X (2 - succ n) ≃* Ω (X (2 - n)) := have H : succ (2 - succ n) = 2 - n, from ap succ !sub_sub⁻¹ ⬝ sub_add_cancel (2-n) 1, equiv_glue X (2 - succ n) ⬝e* loop_pequiv_loop (pequiv_of_eq (ap X H)) definition π_glue (X : spectrum) (n : ℤ) : π[2] (X (2 - succ n)) ≃* π[3] (X (2 - n)) := homotopy_group_pequiv 2 (equiv_glue_neg X n) definition πg_glue (X : spectrum) (n : ℤ) : πg[2] (X (2 - succ n)) ≃g πg[3] (X (2 - n)) := begin change πg[2] (X (2 - succ n)) ≃g πg[2] (Ω (X (2 - n))), apply homotopy_group_isomorphism_of_pequiv, exact equiv_glue_neg X n end definition πg_glue_homotopy_π_glue (X : spectrum) (n : ℤ) : πg_glue X n ~ π_glue X n := by reflexivity definition π_glue_square {X Y : spectrum} (f : X →ₛ Y) (n : ℤ) : π_glue Y n ∘* π→[2] (f (2 - succ n)) ~* π→[3] (f (2 - n)) ∘* π_glue X n := begin change π→[2] (equiv_glue_neg Y n) ∘* π→[2] (f (2 - succ n)) ~* π→[2] (Ω→ (f (2 - n))) ∘* π→[2] (equiv_glue_neg X n), refine homotopy_group_functor_psquare 2 _, refine !sglue_square ⬝v* ap1_psquare !pequiv_of_eq_commute end definition homotopy_group_spectrum_irrel_one {n m : ℤ} {k : ℕ} (E : spectrum) (p : n + 1 = m + k) [Hk : is_succ k] : πg[k] (E n) ≃g π₁ (E m) := begin induction Hk with k, change π₁ (Ω[k] (E n)) ≃g π₁ (E m), apply homotopy_group_isomorphism_of_pequiv 0, symmetry, have m + k = n, from (pred_succ (m + k))⁻¹ ⬝ ap pred (add.assoc m k 1 ⬝ p⁻¹) ⬝ pred_succ n, induction (succ_str_add_eq_int_add m k ⬝ this), exact equiv_gluen E m k end definition homotopy_group_spectrum_irrel {n m : ℤ} {l k : ℕ} (E : spectrum) (p : n + l = m + k) [Hk : is_succ k] [Hl : is_succ l] : πg[k] (E n) ≃g πg[l] (E m) := proof have Πa b c : ℤ, a + (b + c) = c + (b + a), from λa b c, !add.assoc⁻¹ ⬝ add.comm (a + b) c ⬝ ap (λx, c + x) (add.comm a b), have n + 1 = m + 1 - l + k, from ap succ (add_sub_cancel n l)⁻¹ ⬝ !add.assoc ⬝ ap (λx, x + (-l + 1)) p ⬝ !add.assoc ⬝ ap (λx, m + x) (this k (-l) 1) ⬝ !add.assoc⁻¹ ⬝ !add.assoc⁻¹, homotopy_group_spectrum_irrel_one E this ⬝g (homotopy_group_spectrum_irrel_one E (sub_add_cancel (m+1) l)⁻¹)⁻¹ᵍ qed definition shomotopy_group_isomorphism_homotopy_group {n m : ℤ} {l : ℕ} (E : spectrum) (p : n + m = l) [H : is_succ l] : πₛ[n] E ≃g πg[l] (E m) := have 2 - n + l = m + 2, from ap (λx, 2 - n + x) p⁻¹ ⬝ !add.assoc⁻¹ ⬝ ap (λx, x + m) (sub_add_cancel 2 n) ⬝ add.comm 2 m, homotopy_group_spectrum_irrel E this definition shomotopy_group_pequiv_homotopy_group_ab {n m : ℤ} {l : ℕ} (E : spectrum) (p : n + m = l) [H : is_at_least_two l] : πₛ[n] E ≃g πag[l] (E m) := begin induction H with l, exact shomotopy_group_isomorphism_homotopy_group E p end definition shomotopy_group_pequiv_homotopy_group {n m : ℤ} {l : ℕ} (E : spectrum) (p : n + m = l) : πₛ[n] E ≃* π[l] (E m) := begin cases l with l, { apply ptrunc_pequiv_ptrunc, symmetry, change E m ≃* Ω (Ω (E (2 - n))), refine !equiv_glue ⬝e* loop_pequiv_loop _, refine !equiv_glue ⬝e* loop_pequiv_loop _, apply pequiv_ap E, have -n = m, from neg_eq_of_add_eq_zero p, induction this, rexact add.assoc (-n) 1 1 ⬝ add.comm (-n) 2 }, { exact pequiv_of_isomorphism (shomotopy_group_isomorphism_homotopy_group E p) } end /- the long exact sequence of homotopy groups for spectra -/ section LES open chain_complex prod fin group universe variable u parameters {X Y : spectrum.{u}} (f : X →ₛ Y) definition LES_of_shomotopy_groups : chain_complex +3ℤ := splice (λ(n : ℤ), LES_of_homotopy_groups (f (2 - n))) (2, 0) (π_glue Y) (π_glue X) (π_glue_square f) -- This LES is definitionally what we want: example (n : ℤ) : LES_of_shomotopy_groups (n, 0) = πₛ[n] Y := idp example (n : ℤ) : LES_of_shomotopy_groups (n, 1) = πₛ[n] X := idp example (n : ℤ) : LES_of_shomotopy_groups (n, 2) = πₛ[n] (sfiber f) := idp example (n : ℤ) : cc_to_fn LES_of_shomotopy_groups (n, 0) = πₛ→[n] f := idp example (n : ℤ) : cc_to_fn LES_of_shomotopy_groups (n, 1) = πₛ→[n] (spoint f) := idp -- the maps are ugly for (n, 2) definition ab_group_LES_of_shomotopy_groups : Π(v : +3ℤ), ab_group (LES_of_shomotopy_groups v) | (n, fin.mk 0 H) := proof AbGroup.struct (πₛ[n] Y) qed | (n, fin.mk 1 H) := proof AbGroup.struct (πₛ[n] X) qed | (n, fin.mk 2 H) := proof AbGroup.struct (πₛ[n] (sfiber f)) qed | (n, fin.mk (k+3) H) := begin exfalso, apply lt_le_antisymm H, apply le_add_left end local attribute ab_group_LES_of_shomotopy_groups [instance] definition is_mul_hom_LES_of_shomotopy_groups : Π(v : +3ℤ), is_mul_hom (cc_to_fn LES_of_shomotopy_groups v) | (n, fin.mk 0 H) := proof homomorphism.struct (πₛ→[n] f) qed | (n, fin.mk 1 H) := proof homomorphism.struct (πₛ→[n] (spoint f)) qed | (n, fin.mk 2 H) := proof homomorphism.struct (homomorphism_LES_of_homotopy_groups_fun (f (2 - n)) (1, 2) ∘g πg_glue Y n) qed | (n, fin.mk (k+3) H) := begin exfalso, apply lt_le_antisymm H, apply le_add_left end definition is_exact_LES_of_shomotopy_groups : is_exact LES_of_shomotopy_groups := begin apply is_exact_splice, intro n, apply is_exact_LES_of_homotopy_groups, end -- In the comments below is a start on an explicit description of the LES for spectra -- Maybe it's slightly nicer to work with than the above version definition shomotopy_groups [reducible] : +3ℤ → AbGroup | (n, fin.mk 0 H) := πₛ[n] Y | (n, fin.mk 1 H) := πₛ[n] X | (n, fin.mk k H) := πₛ[n] (sfiber f) definition shomotopy_groups_fun : Π(v : +3ℤ), shomotopy_groups (S v) →g shomotopy_groups v | (n, fin.mk 0 H) := proof πₛ→[n] f qed | (n, fin.mk 1 H) := proof πₛ→[n] (spoint f) qed | (n, fin.mk 2 H) := proof homomorphism_LES_of_homotopy_groups_fun (f (2 - n)) (nat.succ nat.zero, 2) ∘g πg_glue Y n ∘g (by reflexivity) qed | (n, fin.mk (k+3) H) := begin exfalso, apply lt_le_antisymm H, apply le_add_left end --(homomorphism_LES_of_homotopy_groups_fun (f (2 - n)) (1, 2) ∘g πg_glue Y n) end LES /- homotopy group of a prespectrum -/ definition pshomotopy_group_hom (n : ℤ) (E : prespectrum) (k : ℕ) : πag[k + 2] (E (-n - 2 + k)) →g πag[k + 3] (E (-n - 2 + (k + 1))) := begin refine _ ∘g π→g[k+2] (glue E _), refine (ghomotopy_group_succ_in _ (k+1))⁻¹ᵍ ∘g _, refine homotopy_group_isomorphism_of_pequiv (k+1) (loop_pequiv_loop (pequiv_of_eq (ap E (add.assoc (-n - 2) k 1)))) end definition pshomotopy_group (n : ℤ) (E : prespectrum) : AbGroup := group.seq_colim (λ(k : ℕ), πag[k+2] (E (-n - 2 + k))) (pshomotopy_group_hom n E) notation `πₚₛ[`:95 n:0 `]`:0 := pshomotopy_group n definition pshomotopy_group_fun (n : ℤ) {E F : prespectrum} (f : E →ₛ F) : πₚₛ[n] E →g πₚₛ[n] F := proof group.seq_colim_functor (λk, π→g[k+2] (f (-n - 2 +[ℤ] k))) begin intro k, note sq1 := homotopy_group_homomorphism_psquare (k+2) (ptranspose (smap.glue_square f (-n - 2 +[ℤ] k))), note sq2 := homotopy_group_functor_hsquare (k+2) (ap1_psquare (ptransport_natural E F f (add.assoc (-n - 2) k 1))), note sq3 := (homotopy_group_succ_in_natural (k+2) (f (-n - 2 +[ℤ] (k+1))))⁻¹ʰᵗʸʰ, note sq4 := hsquare_of_psquare sq2, note rect := sq1 ⬝htyh sq4 ⬝htyh sq3, exact sorry --sq1 ⬝htyh sq4 ⬝htyh sq3, end qed notation `πₚₛ→[`:95 n:0 `]`:0 := pshomotopy_group_fun n /- a chain complex of spectra (not yet used anywhere) -/ structure sp_chain_complex (N : succ_str) : Type := (car : N → spectrum) (fn : Π(n : N), car (S n) →ₛ car n) (is_chain_complex : Πn, fn n ∘ₛ fn (S n) ~ₛ szero _ _) section variables {N : succ_str} (X : sp_chain_complex N) (n : N) definition scc_to_car [unfold 2] [coercion] := @sp_chain_complex.car definition scc_to_fn [unfold 2] : X (S n) →ₛ X n := sp_chain_complex.fn X n definition scc_is_chain_complex [unfold 2] : scc_to_fn X n ∘ₛ scc_to_fn X (S n) ~ₛ szero _ _ := sp_chain_complex.is_chain_complex X n end ------------------------------ -- Suspension prespectra ------------------------------ -- Suspension prespectra are one that's naturally indexed on the natural numbers definition psp_susp (X : Type*) : gen_prespectrum +ℕ := gen_prespectrum.mk (λn, iterate_susp n X) (λn, loop_susp_unit (iterate_susp n X)) -- The sphere prespectrum definition psp_sphere : gen_prespectrum +ℕ := psp_susp bool.pbool /------------------------------- Cotensor of spectra by types -------------------------------/ -- Makes sense for any indexing succ_str. Could be done for -- prespectra too, but as with truncation, why bother? definition sp_cotensor [constructor] {N : succ_str} (A : Type*) (B : gen_spectrum N) : gen_spectrum N := spectrum.MK (λn, ppmap A (B n)) (λn, (loop_ppmap_commute A (B (S n)))⁻¹ᵉ* ∘*ᵉ (pequiv_ppcompose_left (equiv_glue B n))) /- unpointed cotensor -/ definition sp_ucotensor [constructor] {N : succ_str} (A : Type) (B : gen_spectrum N) : gen_spectrum N := spectrum.MK (λn, A →ᵘ* B n) (λn, pumap_pequiv_right A (equiv_glue B n) ⬝e* (loop_pumap A (B (S n)))⁻¹ᵉ*) ---------------------------------------- -- Sections of parametrized spectra ---------------------------------------- definition spi [constructor] {N : succ_str} (A : Type*) (E : A → gen_spectrum N) : gen_spectrum N := spectrum.MK (λn, Π*a, E a n) (λn, !loop_pppi_pequiv⁻¹ᵉ* ∘*ᵉ ppi_pequiv_right (λa, equiv_glue (E a) n)) definition spi_compose_left [constructor] {N : succ_str} {A : Type*} {E F : A -> gen_spectrum N} (f : Πa, E a →ₛ F a) : spi A E →ₛ spi A F := smap.mk (λn, pppi_compose_left (λa, f a n)) begin intro n, exact psquare_pppi_compose_left (λa, (glue_square (f a) n)) ⬝v* !loop_pppi_pequiv_natural⁻¹ᵛ* end -- unpointed spi definition supi [constructor] {N : succ_str} (A : Type) (E : A → gen_spectrum N) : gen_spectrum N := spectrum.MK (λn, Πᵘ*a, E a n) (λn, pupi_pequiv_right (λa, equiv_glue (E a) n) ⬝e* (loop_pupi (λa, E a (S n)))⁻¹ᵉ*) /- Mapping spectra -/ -- note: see also cotensor above /- suspension of a spectrum this is just a shift. We could call a shift in the other direction loopn, though it might be more convenient to just take a negative suspension -/ definition ssusp [constructor] {N : succ_str} (X : gen_spectrum N) : gen_spectrum N := spectrum.MK (λn, X (S n)) (λn, equiv_glue X (S n)) definition ssuspn [constructor] (k : ℤ) (X : spectrum) : spectrum := spectrum.MK (λn, X (n + k)) (λn, equiv_glue X (n + k) ⬝e* loop_pequiv_loop (pequiv_ap X !add.right_comm)) definition shomotopy_group_ssuspn (k : ℤ) (X : spectrum) (n : ℤ) : πₛ[k] (ssuspn n X) ≃g πₛ[k - n] X := have k - n + (2 - k + n) = 2, from !add.comm ⬝ ap (λx, x + (k - n)) (!add.assoc ⬝ ap (λx, 2 + x) (ap (λx, -k + x) !neg_neg⁻¹ ⬝ !neg_add⁻¹)) ⬝ sub_add_cancel 2 (k - n), (shomotopy_group_isomorphism_homotopy_group X this)⁻¹ᵍ /- Tensor by spaces -/ /- Cofibers and stability -/ ------------------------------ -- Contractible spectrum ------------------------------ definition sunit.{u} [constructor] : spectrum.{u} := spectrum.MK (λn, plift punit) (λn, pequiv_of_is_contr _ _ _ _) definition shomotopy_group_sunit.{u} (n : ℤ) : πₛ[n] sunit.{u} ≃g trivial_ab_group_lift.{u} := phomotopy_group_plift_punit 2 definition add_point_spectrum [constructor] {X : Type} (Y : X → spectrum) (x : X₊) : spectrum := spectrum.MK (λn, add_point_over (λx, Y x n) x) begin intro n, induction x with x, apply pequiv_of_is_contr, apply is_trunc_lift, apply is_contr_loop_of_is_contr, apply is_trunc_lift, exact equiv_glue (Y x) n end open option definition shomotopy_group_add_point_spectrum {X : Type} (Y : X → spectrum) (n : ℤ) : Π(x : X₊), πₛ[n] (add_point_spectrum Y x) ≃g add_point_AbGroup (λ (x : X), πₛ[n] (Y x)) x | (some x) := by reflexivity | none := proof phomotopy_group_plift_punit 2 qed /- The Eilenberg-MacLane spectrum -/ definition EM_spectrum /-[constructor]-/ (G : AbGroup) : spectrum := spectrum.Mk (K G) (λn, (loop_EM G n)⁻¹ᵉ*) definition EM_spectrum_pequiv {G H : AbGroup} (e : G ≃g H) (n : ℤ) : EM_spectrum G n ≃* EM_spectrum H n := spectrum_pequiv_of_nat (λk, EM_pequiv_EM k e) n definition EM_spectrum_trivial.{u} (n : ℤ) : EM_spectrum trivial_ab_group_lift.{u} n ≃* trivial_ab_group_lift.{u} := pequiv_of_is_contr _ _ (is_contr_spectrum_of_nat (λk, is_contr_EM k !is_trunc_lift) n) !is_trunc_lift definition is_contr_EM_spectrum_neg (G : AbGroup) (n : ℕ) : is_contr (EM_spectrum G (-[1+n])) := begin induction n with n IH, { apply is_contr_loop, exact is_trunc_EM G 0 }, { apply is_contr_loop_of_is_contr, exact IH } end /- K(πₗ(Aₖ),l) ≃* K(πₙ(A),l) for l = n + k -/ definition EM_type_pequiv_EM (A : spectrum) {n k : ℤ} {l : ℕ} (p : n + k = l) : EM_type (A k) l ≃* EM (πₛ[n] A) l := begin symmetry, cases l with l, { exact shomotopy_group_pequiv_homotopy_group A p }, { cases l with l, { apply EM1_pequiv_EM1, exact shomotopy_group_isomorphism_homotopy_group A p }, { apply EMadd1_pequiv_EMadd1 (l+1), exact shomotopy_group_isomorphism_homotopy_group A p }} end /- Wedge of prespectra -/ open fwedge definition fwedge_prespectrum.{u v} {I : Type.{v}} (X : I -> prespectrum.{u}) : prespectrum.{max u v} := begin fconstructor, { intro n, exact fwedge (λ i, X i n) }, { intro n, fapply fwedge_pmap, intro i, exact Ω→ !pinl ∘* !glue } end end spectrum
c8eddc6c25f2f4b285c081b28eecea88eae1cd11
d450724ba99f5b50b57d244eb41fef9f6789db81
/src/mywork/sep_3_2021.lean
5259c14497cffcfe6d13085ddef5d37e4837e7d2
[]
no_license
jakekauff/CS2120F21
4f009adeb4ce4a148442b562196d66cc6c04530c
e69529ec6f5d47a554291c4241a3d8ec4fe8f5ad
refs/heads/main
1,693,841,880,030
1,637,604,848,000
1,637,604,848,000
399,946,698
0
0
null
null
null
null
UTF-8
Lean
false
false
1,279
lean
/- Theorem: Equality is symmetric -/ theorem eq_symm : ∀ (T : Type)(x y : T), x = y → y = x := begin assume (T : Type), assume (x y : T), assume (e : x = y), rw e, end /- Theorem : Equality is symmetric. In other words, ∀ (T : Type)(x y : T), x = y → y = x Proof: First we'll assume that T is any type and we have objects x and y of this type. What remains to be shown is that x = y → y = x. To prove this, we'll assume the premise, that x = y, and in this context we have to prove y = x. But by using the axiom of substitutability of equals, and using the fact x = y, we can rewrite x in the goal as y, yielding y = y as our goal. But this is true by the axiom of reflexivity of equality, QED. -/ /- Theorem: Equality is transitive. -/ theorem eq_trans: ∀ (T : Type) (x y z : T), x = y → y = z → x = z := begin assume (T : Type), assume (x y z : T), assume(e1 : x = y), assume(e2 : y = z), rw e1, exact e2, end /- Theorem: Equality is substitutive (Homework for Monday, September 6, 2021) -/ theorem eq_sub: ∀ (T : Type) (x y z : T), x = y → z = y → z = x := begin assume (T : Type), assume(x y z : T), assume(e1 : x = y), assume(e2 : z = y), rw e1, exact e2, end
f6917e8f6381f35832c7fc32aac8e99650905b39
137c667471a40116a7afd7261f030b30180468c2
/src/analysis/normed_space/int.lean
ba5180ad9e1d9b5e95234ee8d014d3724bc183d6
[ "Apache-2.0" ]
permissive
bragadeesh153/mathlib
46bf814cfb1eecb34b5d1549b9117dc60f657792
b577bb2cd1f96eb47031878256856020b76f73cd
refs/heads/master
1,687,435,188,334
1,626,384,207,000
1,626,384,207,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,531
lean
/- Copyright (c) 2021 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import analysis.normed_space.basic /-! # The integers as normed ring This file contains basic facts about the integers as normed ring. Recall that `∥n∥` denotes the norm of `n` as real number. This norm is always nonnegative, so we can bundle the norm together with this fact, to obtain a term of type `nnreal` (the nonnegative real numbers). The resulting nonnegative real number is denoted by `∥n∥₊`. -/ open_locale big_operators namespace int lemma nnnorm_coe_units (e : units ℤ) : ∥(e : ℤ)∥₊ = 1 := begin obtain (rfl|rfl) := int.units_eq_one_or e; simp only [units.coe_neg_one, units.coe_one, nnnorm_neg, nnnorm_one], end lemma norm_coe_units (e : units ℤ) : ∥(e : ℤ)∥ = 1 := by rw [← coe_nnnorm, int.nnnorm_coe_units, nnreal.coe_one] @[simp] lemma nnnorm_coe_nat (n : ℕ) : ∥(n : ℤ)∥₊ = n := real.nnnorm_coe_nat _ @[simp] lemma norm_coe_nat (n : ℕ) : ∥(n : ℤ)∥ = n := real.norm_coe_nat _ @[simp] lemma to_nat_add_to_nat_neg_eq_nnnorm (n : ℤ) : ↑(n.to_nat) + ↑((-n).to_nat) = ∥n∥₊ := by rw [← nat.cast_add, to_nat_add_to_nat_neg_eq_nat_abs, nnreal.coe_nat_abs] @[simp] lemma to_nat_add_to_nat_neg_eq_norm (n : ℤ) : ↑(n.to_nat) + ↑((-n).to_nat) = ∥n∥ := by simpa only [nnreal.coe_nat_cast, nnreal.coe_add] using congr_arg (coe : _ → ℝ) (to_nat_add_to_nat_neg_eq_nnnorm n) end int
5b17dafc1c12ab6906e2bb3ce8b8db1b1a6d6488
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/stage0/src/Lean/Compiler/IR/ElimDeadBranches.lean
d85163927ca7e78ae3a0bf04a76a1fbc38d8dfa3
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach" ]
permissive
EdAyers/lean4
57ac632d6b0789cb91fab2170e8c9e40441221bd
37ba0df5841bde51dbc2329da81ac23d4f6a4de4
refs/heads/master
1,676,463,245,298
1,660,619,433,000
1,660,619,433,000
183,433,437
1
0
Apache-2.0
1,657,612,672,000
1,556,196,574,000
Lean
UTF-8
Lean
false
false
11,573
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.Compiler.IR.Format import Lean.Compiler.IR.Basic import Lean.Compiler.IR.CompilerM namespace Lean.IR.UnreachableBranches /-- Value used in the abstract interpreter -/ inductive Value where | bot -- undefined | top -- any value | ctor (i : CtorInfo) (vs : Array Value) | choice (vs : List Value) deriving Inhabited, Repr protected partial def Value.toFormat : Value → Format | Value.bot => "⊥" | Value.top => "⊤" | Value.ctor info vs => if vs.isEmpty then format info.name else Format.paren <| format info.name ++ Std.Format.join (vs.toList.map fun v => " " ++ Value.toFormat v) | Value.choice vs => Format.paren <| Std.Format.joinSep (vs.map Value.toFormat) " | " instance : ToFormat Value where format := Value.toFormat instance : ToString Value where toString v := toString (format v) namespace Value protected partial def beq : Value → Value → Bool | bot, bot => true | top, top => true | ctor i₁ vs₁, ctor i₂ vs₂ => i₁ == i₂ && Array.isEqv vs₁ vs₂ Value.beq | choice vs₁, choice vs₂ => vs₁.all (fun v₁ => vs₂.any fun v₂ => Value.beq v₁ v₂) && vs₂.all (fun v₂ => vs₁.any fun v₁ => Value.beq v₁ v₂) | _, _ => false instance : BEq Value := ⟨Value.beq⟩ partial def addChoice (merge : Value → Value → Value) : List Value → Value → List Value | [], v => [v] | v₁@(ctor i₁ _) :: cs, v₂@(ctor i₂ _) => if i₁ == i₂ then merge v₁ v₂ :: cs else v₁ :: addChoice merge cs v₂ | _, _ => panic! "invalid addChoice" partial def merge (v₁ v₂ : Value) : Value := match v₁, v₂ with | bot, v => v | v, bot => v | top, _ => top | _, top => top | v₁@(ctor i₁ vs₁), v₂@(ctor i₂ vs₂) => if i₁ == i₂ then ctor i₁ <| vs₁.size.fold (init := #[]) fun i r => r.push (merge vs₁[i]! vs₂[i]!) else choice [v₁, v₂] | choice vs₁, choice vs₂ => choice <| vs₁.foldl (addChoice merge) vs₂ | choice vs, v => choice <| addChoice merge vs v | v, choice vs => choice <| addChoice merge vs v protected partial def format : Value → Format | top => "top" | bot => "bot" | choice vs => format "@" ++ @List.format _ ⟨Value.format⟩ vs | ctor i vs => format "#" ++ if vs.isEmpty then format i.name else Format.paren (format i.name ++ @formatArray _ ⟨Value.format⟩ vs) instance : ToFormat Value := ⟨Value.format⟩ instance : ToString Value := ⟨Format.pretty ∘ Value.format⟩ /-- In `truncate`, we approximate a value as `top` if depth > `truncateMaxDepth`. TODO: add option to control this parameter. -/ def truncateMaxDepth := 8 /-- Make sure constructors of recursive inductive datatypes can only occur once in each path. Values at depth > truncateMaxDepth are also approximated at `top`. We use this function this function to implement a simple widening operation for our abstract interpreter. Recall the widening functions is used to ensure termination in abstract interpreters. -/ partial def truncate (env : Environment) (v : Value) (s : NameSet) : Value := go v s truncateMaxDepth where go (v : Value) (s : NameSet) (depth : Nat) : Value := match depth with | 0 => top | depth+1 => match v, s with | ctor i vs, found => let I := i.name.getPrefix if found.contains I then top else let cont (found' : NameSet) : Value := ctor i (vs.map fun v => go v found' depth) match env.find? I with | some (ConstantInfo.inductInfo d) => if d.isRec then cont (found.insert I) else cont found | _ => cont found | choice vs, found => let newVs := vs.map fun v => go v found depth if newVs.elem top then top else choice newVs | v, _ => v /-- Widening operator that guarantees termination in our abstract interpreter. -/ def widening (env : Environment) (v₁ v₂ : Value) : Value := truncate env (merge v₁ v₂) {} end Value abbrev FunctionSummaries := SMap FunId Value builtin_initialize functionSummariesExt : SimplePersistentEnvExtension (FunId × Value) FunctionSummaries ← registerSimplePersistentEnvExtension { name := `unreachBranchesFunSummary, addImportedFn := fun as => let cache : FunctionSummaries := mkStateFromImportedEntries (fun s (p : FunId × Value) => s.insert p.1 p.2) {} as cache.switch, addEntryFn := fun s ⟨e, n⟩ => s.insert e n } def addFunctionSummary (env : Environment) (fid : FunId) (v : Value) : Environment := functionSummariesExt.addEntry env (fid, v) def getFunctionSummary? (env : Environment) (fid : FunId) : Option Value := (functionSummariesExt.getState env).find? fid abbrev Assignment := Std.HashMap VarId Value structure InterpContext where currFnIdx : Nat := 0 decls : Array Decl env : Environment lctx : LocalContext := {} structure InterpState where assignments : Array Assignment funVals : Std.PArray Value -- we take snapshots during fixpoint computations abbrev M := ReaderT InterpContext (StateM InterpState) open Value def findVarValue (x : VarId) : M Value := do let ctx ← read let s ← get let assignment := s.assignments[ctx.currFnIdx]! return assignment.findD x bot def findArgValue (arg : Arg) : M Value := match arg with | Arg.var x => findVarValue x | _ => pure top def updateVarAssignment (x : VarId) (v : Value) : M Unit := do let v' ← findVarValue x let ctx ← read modify fun s => { s with assignments := s.assignments.modify ctx.currFnIdx fun a => a.insert x (merge v v') } def resetVarAssignment (x : VarId) : M Unit := do let ctx ← read modify fun s => { s with assignments := s.assignments.modify ctx.currFnIdx fun a => a.insert x Value.bot } def resetParamAssignment (y : Param) : M Unit := resetVarAssignment y.x partial def projValue : Value → Nat → Value | ctor _ vs, i => vs.getD i bot | choice vs, i => vs.foldl (fun r v => merge r (projValue v i)) bot | v, _ => v def interpExpr : Expr → M Value | Expr.ctor i ys => return ctor i (← ys.mapM fun y => findArgValue y) | Expr.proj i x => return projValue (← findVarValue x) i | Expr.fap fid _ => do let ctx ← read match getFunctionSummary? ctx.env fid with | some v => pure v | none => do let s ← get match ctx.decls.findIdx? (fun decl => decl.name == fid) with | some idx => pure s.funVals[idx]! | none => pure top | _ => pure top partial def containsCtor : Value → CtorInfo → Bool | top, _ => true | ctor i _, j => i == j | choice vs, j => vs.any fun v => containsCtor v j | _, _ => false def updateCurrFnSummary (v : Value) : M Unit := do let ctx ← read let currFnIdx := ctx.currFnIdx modify fun s => { s with funVals := s.funVals.modify currFnIdx (fun v' => widening ctx.env v v') } /-- Return true if the assignment of at least one parameter has been updated. -/ def updateJPParamsAssignment (ys : Array Param) (xs : Array Arg) : M Bool := do let ctx ← read let currFnIdx := ctx.currFnIdx ys.size.foldM (init := false) fun i r => do let y := ys[i]! let x := xs[i]! let yVal ← findVarValue y.x let xVal ← findArgValue x let newVal := merge yVal xVal if newVal == yVal then pure r else modify fun s => { s with assignments := s.assignments.modify currFnIdx fun a => a.insert y.x newVal } pure true private partial def resetNestedJPParams : FnBody → M Unit | FnBody.jdecl _ ys _ k => do ys.forM resetParamAssignment /- Remark we don't need to reset the parameters of joint-points nested in `b` since they will be reset if this JP is used. -/ resetNestedJPParams k | FnBody.case _ _ _ alts => alts.forM fun alt => match alt with | Alt.ctor _ b => resetNestedJPParams b | Alt.default b => resetNestedJPParams b | e => do unless e.isTerminal do resetNestedJPParams e.body partial def interpFnBody : FnBody → M Unit | FnBody.vdecl x _ e b => do let v ← interpExpr e updateVarAssignment x v interpFnBody b | FnBody.jdecl j ys v b => withReader (fun ctx => { ctx with lctx := ctx.lctx.addJP j ys v }) do interpFnBody b | FnBody.case _ x _ alts => do let v ← findVarValue x alts.forM fun alt => do match alt with | Alt.ctor i b => if containsCtor v i then interpFnBody b | Alt.default b => interpFnBody b | FnBody.ret x => do let v ← findArgValue x updateCurrFnSummary v | FnBody.jmp j xs => do let ctx ← read let ys := (ctx.lctx.getJPParams j).get! let b := (ctx.lctx.getJPBody j).get! let updated ← updateJPParamsAssignment ys xs if updated then -- We must reset the value of nested join-point parameters since they depend on `ys` values resetNestedJPParams b interpFnBody b | e => do unless e.isTerminal do interpFnBody e.body def inferStep : M Bool := do let ctx ← read modify fun s => { s with assignments := ctx.decls.map fun _ => {} } ctx.decls.size.foldM (init := false) fun idx modified => do match ctx.decls[idx]! with | .fdecl (xs := ys) (body := b) .. => do let s ← get let currVals := s.funVals[idx]! withReader (fun ctx => { ctx with currFnIdx := idx }) do ys.forM fun y => updateVarAssignment y.x top interpFnBody b let s ← get let newVals := s.funVals[idx]! pure (modified || currVals != newVals) | .extern .. => pure modified partial def inferMain : M Unit := do let modified ← inferStep if modified then inferMain else pure () partial def elimDeadAux (assignment : Assignment) : FnBody → FnBody | FnBody.vdecl x t e b => FnBody.vdecl x t e (elimDeadAux assignment b) | FnBody.jdecl j ys v b => FnBody.jdecl j ys (elimDeadAux assignment v) (elimDeadAux assignment b) | FnBody.case tid x xType alts => let v := assignment.findD x bot let alts := alts.map fun alt => match alt with | Alt.ctor i b => Alt.ctor i <| if containsCtor v i then elimDeadAux assignment b else FnBody.unreachable | Alt.default b => Alt.default (elimDeadAux assignment b) FnBody.case tid x xType alts | e => if e.isTerminal then e else let (instr, b) := e.split let b := elimDeadAux assignment b instr.setBody b partial def elimDead (assignment : Assignment) (d : Decl) : Decl := match d with | .fdecl (body := b) .. => d.updateBody! <| elimDeadAux assignment b | other => other end UnreachableBranches open UnreachableBranches def elimDeadBranches (decls : Array Decl) : CompilerM (Array Decl) := do let s ← get let env := s.env let assignments : Array Assignment := decls.map fun _ => {} let funVals := Std.mkPArray decls.size Value.bot let ctx : InterpContext := { decls := decls, env := env } let s : InterpState := { assignments := assignments, funVals := funVals } let (_, s) := (inferMain ctx).run s let funVals := s.funVals let assignments := s.assignments modify fun s => let env := decls.size.fold (init := s.env) fun i env => addFunctionSummary env decls[i]!.name funVals[i]! { s with env := env } return decls.mapIdx fun i decl => elimDead assignments[i]! decl end Lean.IR
61868724dbfc7b48aa22b0f41990f4c5f6671cbb
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/category_theory/noetherian.lean
629df87cea753723b55af12aa53f6dfe932d5349
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
3,097
lean
/- Copyright (c) 2022 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import category_theory.subobject.lattice import category_theory.essentially_small import category_theory.simple /-! # Artinian and noetherian categories An artinian category is a category in which objects do not have infinite decreasing sequences of subobjects. A noetherian category is a category in which objects do not have infinite increasing sequences of subobjects. We show that any nonzero artinian object has a simple subobject. ## Future work The Jordan-Hölder theorem, following https://stacks.math.columbia.edu/tag/0FCK. -/ namespace category_theory open category_theory.limits variables {C : Type*} [category C] /-- A noetherian object is an object which does not have infinite increasing sequences of subobjects. See https://stacks.math.columbia.edu/tag/0FCG -/ class noetherian_object (X : C) : Prop := (subobject_gt_well_founded : well_founded ((>) : subobject X → subobject X → Prop)) /-- An artinian object is an object which does not have infinite decreasing sequences of subobjects. See https://stacks.math.columbia.edu/tag/0FCF -/ class artinian_object (X : C) : Prop := (subobject_lt_well_founded [] : well_founded ((<) : subobject X → subobject X → Prop)) variables (C) /-- A category is noetherian if it is essentially small and all objects are noetherian. -/ class noetherian extends essentially_small C := (noetherian_object : ∀ (X : C), noetherian_object X) attribute [priority 100, instance] noetherian.noetherian_object /-- A category is artinian if it is essentially small and all objects are artinian. -/ class artinian extends essentially_small C := (artinian_object : ∀ (X : C), artinian_object X) attribute [priority 100, instance] artinian.artinian_object variables {C} open subobject variables [has_zero_morphisms C] [has_zero_object C] lemma exists_simple_subobject {X : C} [artinian_object X] (h : ¬ is_zero X) : ∃ (Y : subobject X), simple (Y : C) := begin haveI : nontrivial (subobject X) := nontrivial_of_not_is_zero h, haveI := is_atomic_of_order_bot_well_founded_lt (artinian_object.subobject_lt_well_founded X), have := is_atomic.eq_bot_or_exists_atom_le (⊤ : subobject X), obtain ⟨Y, s⟩ := (is_atomic.eq_bot_or_exists_atom_le (⊤ : subobject X)).resolve_left top_ne_bot, exact ⟨Y, (subobject_simple_iff_is_atom _).mpr s.1⟩, end /-- Choose an arbitrary simple subobject of a non-zero artinian object. -/ noncomputable def simple_subobject {X : C} [artinian_object X] (h : ¬ is_zero X) : C := (exists_simple_subobject h).some /-- The monomorphism from the arbitrary simple subobject of a non-zero artinian object. -/ @[derive mono] noncomputable def simple_subobject_arrow {X : C} [artinian_object X] (h : ¬ is_zero X) : simple_subobject h ⟶ X := (exists_simple_subobject h).some.arrow instance {X : C} [artinian_object X] (h : ¬ is_zero X) : simple (simple_subobject h) := (exists_simple_subobject h).some_spec end category_theory
a9594a712f592311ab3f3861a558b489bfce70ee
1abd1ed12aa68b375cdef28959f39531c6e95b84
/src/group_theory/monoid_localization.lean
938f86fa92efb212b90a4257b8ab4bdee2744c0c
[ "Apache-2.0" ]
permissive
jumpy4/mathlib
d3829e75173012833e9f15ac16e481e17596de0f
af36f1a35f279f0e5b3c2a77647c6bf2cfd51a13
refs/heads/master
1,693,508,842,818
1,636,203,271,000
1,636,203,271,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
60,953
lean
/- Copyright (c) 2019 Amelia Livingston. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Amelia Livingston -/ import group_theory.congruence import group_theory.submonoid import algebra.group.units import algebra.punit_instances /-! # Localizations of commutative monoids Localizing a commutative ring at one of its submonoids does not rely on the ring's addition, so we can generalize localizations to commutative monoids. We characterize the localization of a commutative monoid `M` at a submonoid `S` up to isomorphism; that is, a commutative monoid `N` is the localization of `M` at `S` iff we can find a monoid homomorphism `f : M →* N` satisfying 3 properties: 1. For all `y ∈ S`, `f y` is a unit; 2. For all `z : N`, there exists `(x, y) : M × S` such that `z * f y = f x`; 3. For all `x, y : M`, `f x = f y` iff there exists `c ∈ S` such that `x * c = y * c`. Given such a localization map `f : M →* N`, we can define the surjection `localization_map.mk'` sending `(x, y) : M × S` to `f x * (f y)⁻¹`, and `localization_map.lift`, the homomorphism from `N` induced by a homomorphism from `M` which maps elements of `S` to invertible elements of the codomain. Similarly, given commutative monoids `P, Q`, a submonoid `T` of `P` and a localization map for `T` from `P` to `Q`, then a homomorphism `g : M →* P` such that `g(S) ⊆ T` induces a homomorphism of localizations, `localization_map.map`, from `N` to `Q`. We treat the special case of localizing away from an element in the sections `away_map` and `away`. We also define the quotient of `M × S` by the unique congruence relation (equivalence relation preserving a binary operation) `r` such that for any other congruence relation `s` on `M × S` satisfying '`∀ y ∈ S`, `(1, 1) ∼ (y, y)` under `s`', we have that `(x₁, y₁) ∼ (x₂, y₂)` by `s` whenever `(x₁, y₁) ∼ (x₂, y₂)` by `r`. We show this relation is equivalent to the standard localization relation. This defines the localization as a quotient type, `localization`, but the majority of subsequent lemmas in the file are given in terms of localizations up to isomorphism, using maps which satisfy the characteristic predicate. ## Implementation notes In maths it is natural to reason up to isomorphism, but in Lean we cannot naturally `rewrite` one structure with an isomorphic one; one way around this is to isolate a predicate characterizing a structure up to isomorphism, and reason about things that satisfy the predicate. The infimum form of the localization congruence relation is chosen as 'canonical' here, since it shortens some proofs. To apply a localization map `f` as a function, we use `f.to_map`, as coercions don't work well for this structure. To reason about the localization as a quotient type, use `mk_eq_monoid_of_mk'` and associated lemmas. These show the quotient map `mk : M → S → localization S` equals the surjection `localization_map.mk'` induced by the map `monoid_of : localization_map S (localization S)` (where `of` establishes the localization as a quotient type satisfies the characteristic predicate). The lemma `mk_eq_monoid_of_mk'` hence gives you access to the results in the rest of the file, which are about the `localization_map.mk'` induced by any localization map. ## Tags localization, monoid localization, quotient monoid, congruence relation, characteristic predicate, commutative monoid -/ namespace add_submonoid variables {M : Type*} [add_comm_monoid M] (S : add_submonoid M) (N : Type*) [add_comm_monoid N] /-- The type of add_monoid homomorphisms satisfying the characteristic predicate: if `f : M →+ N` satisfies this predicate, then `N` is isomorphic to the localization of `M` at `S`. -/ @[nolint has_inhabited_instance] structure localization_map extends add_monoid_hom M N := (map_add_units' : ∀ y : S, is_add_unit (to_fun y)) (surj' : ∀ z : N, ∃ x : M × S, z + to_fun x.2 = to_fun x.1) (eq_iff_exists' : ∀ x y, to_fun x = to_fun y ↔ ∃ c : S, x + c = y + c) /-- The add_monoid hom underlying a `localization_map` of `add_comm_monoid`s. -/ add_decl_doc localization_map.to_add_monoid_hom end add_submonoid variables {M : Type*} [comm_monoid M] (S : submonoid M) (N : Type*) [comm_monoid N] {P : Type*} [comm_monoid P] namespace submonoid /-- The type of monoid homomorphisms satisfying the characteristic predicate: if `f : M →* N` satisfies this predicate, then `N` is isomorphic to the localization of `M` at `S`. -/ @[nolint has_inhabited_instance] structure localization_map extends monoid_hom M N := (map_units' : ∀ y : S, is_unit (to_fun y)) (surj' : ∀ z : N, ∃ x : M × S, z * to_fun x.2 = to_fun x.1) (eq_iff_exists' : ∀ x y, to_fun x = to_fun y ↔ ∃ c : S, x * c = y * c) attribute [to_additive add_submonoid.localization_map] submonoid.localization_map attribute [to_additive add_submonoid.localization_map.to_add_monoid_hom] submonoid.localization_map.to_monoid_hom /-- The monoid hom underlying a `localization_map`. -/ add_decl_doc localization_map.to_monoid_hom end submonoid namespace localization run_cmd to_additive.map_namespace `localization `add_localization /-- The congruence relation on `M × S`, `M` a `comm_monoid` and `S` a submonoid of `M`, whose quotient is the localization of `M` at `S`, defined as the unique congruence relation on `M × S` such that for any other congruence relation `s` on `M × S` where for all `y ∈ S`, `(1, 1) ∼ (y, y)` under `s`, we have that `(x₁, y₁) ∼ (x₂, y₂)` by `r` implies `(x₁, y₁) ∼ (x₂, y₂)` by `s`. -/ @[to_additive "The congruence relation on `M × S`, `M` an `add_comm_monoid` and `S` an `add_submonoid` of `M`, whose quotient is the localization of `M` at `S`, defined as the unique congruence relation on `M × S` such that for any other congruence relation `s` on `M × S` where for all `y ∈ S`, `(0, 0) ∼ (y, y)` under `s`, we have that `(x₁, y₁) ∼ (x₂, y₂)` by `r` implies `(x₁, y₁) ∼ (x₂, y₂)` by `s`."] def r (S : submonoid M) : con (M × S) := Inf {c | ∀ y : S, c 1 (y, y)} /-- An alternate form of the congruence relation on `M × S`, `M` a `comm_monoid` and `S` a submonoid of `M`, whose quotient is the localization of `M` at `S`. -/ @[to_additive "An alternate form of the congruence relation on `M × S`, `M` a `comm_monoid` and `S` a submonoid of `M`, whose quotient is the localization of `M` at `S`."] def r' : con (M × S) := begin refine { r := λ a b : M × S, ∃ c : S, a.1 * b.2 * c = b.1 * a.2 * c, iseqv := ⟨λ a, ⟨1, rfl⟩, λ a b ⟨c, hc⟩, ⟨c, hc.symm⟩, _⟩, .. }, { rintros a b c ⟨t₁, ht₁⟩ ⟨t₂, ht₂⟩, use b.2 * t₁ * t₂, simp only [submonoid.coe_mul], calc a.1 * c.2 * (b.2 * t₁ * t₂) = a.1 * b.2 * t₁ * c.2 * t₂ : by ac_refl ... = b.1 * c.2 * t₂ * a.2 * t₁ : by { rw ht₁, ac_refl } ... = c.1 * a.2 * (b.2 * t₁ * t₂) : by { rw ht₂, ac_refl } }, { rintros a b c d ⟨t₁, ht₁⟩ ⟨t₂, ht₂⟩, use t₁ * t₂, calc (a.1 * c.1) * (b.2 * d.2) * (t₁ * t₂) = (a.1 * b.2 * t₁) * (c.1 * d.2 * t₂) : by ac_refl ... = (b.1 * d.1) * (a.2 * c.2) * (t₁ * t₂) : by { rw [ht₁, ht₂], ac_refl } } end /-- The congruence relation used to localize a `comm_monoid` at a submonoid can be expressed equivalently as an infimum (see `localization.r`) or explicitly (see `localization.r'`). -/ @[to_additive "The additive congruence relation used to localize an `add_comm_monoid` at a submonoid can be expressed equivalently as an infimum (see `add_localization.r`) or explicitly (see `add_localization.r'`)."] theorem r_eq_r' : r S = r' S := le_antisymm (Inf_le $ λ _, ⟨1, by simp⟩) $ le_Inf $ λ b H ⟨p, q⟩ y ⟨t, ht⟩, begin rw [← mul_one (p, q), ← mul_one y], refine b.trans (b.mul (b.refl _) (H (y.2 * t))) _, convert b.symm (b.mul (b.refl y) (H (q * t))) using 1, rw [prod.mk_mul_mk, submonoid.coe_mul, ← mul_assoc, ht, mul_left_comm, mul_assoc], refl end variables {S} @[to_additive] lemma r_iff_exists {x y : M × S} : r S x y ↔ ∃ c : S, x.1 * y.2 * c = y.1 * x.2 * c := by rw r_eq_r' S; refl end localization /-- The localization of a `comm_monoid` at one of its submonoids (as a quotient type). -/ @[to_additive add_localization "The localization of an `add_comm_monoid` at one of its submonoids (as a quotient type)."] def localization := (localization.r S).quotient namespace localization @[to_additive] instance inhabited : inhabited (localization S) := con.quotient.inhabited /-- Multiplication in a localization is defined as `⟨a, b⟩ * ⟨c, d⟩ = ⟨a * c, b * d⟩`. -/ @[to_additive "Addition in an `add_localization` is defined as `⟨a, b⟩ + ⟨c, d⟩ = ⟨a + c, b + d⟩`. Should not be confused with the ring localization counterpart `localization.add`, which maps `⟨a, b⟩ + ⟨c, d⟩` to `⟨d * a + b * c, b * d⟩`.", irreducible] protected def mul : localization S → localization S → localization S := (r S).comm_monoid.mul @[to_additive] instance : has_mul (localization S) := ⟨localization.mul S⟩ /-- The identity element of a localization is defined as `⟨1, 1⟩`. -/ @[to_additive "The identity element of an `add_localization` is defined as `⟨0, 0⟩`. Should not be confused with the ring localization counterpart `localization.zero`, which is defined as `⟨0, 1⟩`.", irreducible] protected def one : localization S := (r S).comm_monoid.one @[to_additive] instance : has_one (localization S) := ⟨localization.one S⟩ /-- Exponentiation in a localization is defined as `⟨a, b⟩ ^ n = ⟨a ^ n, b ^ n⟩`. This is a separate `irreducible` def to ensure the elaborator doesn't waste its time trying to unify some huge recursive definition with itself, but unfolded one step less. -/ @[to_additive "Multiplication with a natural in an `add_localization` is defined as `n • ⟨a, b⟩ = ⟨n • a, n • b⟩`. This is a separate `irreducible` def to ensure the elaborator doesn't waste its time trying to unify some huge recursive definition with itself, but unfolded one step less.", irreducible] protected def npow : ℕ → localization S → localization S := (r S).comm_monoid.npow local attribute [semireducible] localization.mul localization.one localization.npow @[to_additive] instance : comm_monoid (localization S) := { mul := (*), one := 1, mul_assoc := show ∀ (x y z : localization S), x * y * z = x * (y * z), from (r S).comm_monoid.mul_assoc, mul_comm := show ∀ (x y : localization S), x * y = y * x, from (r S).comm_monoid.mul_comm, mul_one := show ∀ (x : localization S), x * 1 = x, from (r S).comm_monoid.mul_one, one_mul := show ∀ (x : localization S), 1 * x = x, from (r S).comm_monoid.one_mul, npow := localization.npow S, npow_zero' := show ∀ (x : localization S), localization.npow S 0 x = 1, from pow_zero, npow_succ' := show ∀ (n : ℕ) (x : localization S), localization.npow S n.succ x = x * localization.npow S n x, from λ n x, pow_succ x n } variables {S} /-- Given a `comm_monoid` `M` and submonoid `S`, `mk` sends `x : M`, `y ∈ S` to the equivalence class of `(x, y)` in the localization of `M` at `S`. -/ @[to_additive "Given an `add_comm_monoid` `M` and submonoid `S`, `mk` sends `x : M`, `y ∈ S` to the equivalence class of `(x, y)` in the localization of `M` at `S`."] def mk (x : M) (y : S) : localization S := (r S).mk' (x, y) @[to_additive] theorem mk_eq_mk_iff {a c : M} {b d : S} : mk a b = mk c d ↔ r S ⟨a, b⟩ ⟨c, d⟩ := (r S).eq universes u /-- Dependent recursion principle for localizations: given elements `f a b : p (mk a b)` for all `a b`, such that `r S (a, b) (c, d)` implies `f a b = f c d` (wih the correct coercions), then `f` is defined on the whole `localization S`. -/ @[elab_as_eliminator, to_additive "Dependent recursion principle for `add_localizations`: given elements `f a b : p (mk a b)` for all `a b`, such that `r S (a, b) (c, d)` implies `f a b = f c d` (wih the correct coercions), then `f` is defined on the whole `add_localization S`."] def rec {p : localization S → Sort u} (f : ∀ (a : M) (b : S), p (mk a b)) (H : ∀ {a c : M} {b d : S} (h : r S (a, b) (c, d)), (eq.rec (f a b) (mk_eq_mk_iff.mpr h) : p (mk c d)) = f c d) (x) : p x := quot.rec (λ y, eq.rec (f y.1 y.2) (prod.mk.eta : (y.1, y.2) = y)) (λ y z h, by { cases y, cases z, exact H h }) x attribute [irreducible] localization @[to_additive] lemma mk_mul (a c : M) (b d : S) : mk a b * mk c d = mk (a * c) (b * d) := rfl @[to_additive] lemma mk_one : mk 1 (1 : S) = 1 := rfl @[simp, to_additive] lemma rec_mk {p : localization S → Sort u} (f : ∀ (a : M) (b : S), p (mk a b)) (H) (a : M) (b : S) : (rec f H (mk a b) : p (mk a b)) = f a b := rfl /-- Non-dependent recursion principle for localizations: given elements `f a b : p` for all `a b`, such that `r S (a, b) (c, d)` implies `f a b = f c d`, then `f` is defined on the whole `localization S`. -/ @[elab_as_eliminator, to_additive "Non-dependent recursion principle for `add_localizations`: given elements `f a b : p` for all `a b`, such that `r S (a, b) (c, d)` implies `f a b = f c d`, then `f` is defined on the whole `localization S`."] def lift_on {p : Sort u} (x : localization S) (f : M → S → p) (H : ∀ {a c : M} {b d : S} (h : r S (a, b) (c, d)), f a b = f c d) : p := rec f (λ a c b d h, by rw [eq_rec_constant, H h]) x @[to_additive] lemma lift_on_mk {p : Sort u} (f : ∀ (a : M) (b : S), p) (H) (a : M) (b : S) : lift_on (mk a b) f H = f a b := rfl @[elab_as_eliminator, to_additive] theorem ind {p : localization S → Prop} (H : ∀ (y : M × S), p (mk y.1 y.2)) (x) : p x := rec (λ a b, H (a, b)) (λ _ _ _ _ _, rfl) x @[elab_as_eliminator, to_additive] theorem induction_on {p : localization S → Prop} (x) (H : ∀ (y : M × S), p (mk y.1 y.2)) : p x := ind H x /-- Non-dependent recursion principle for localizations: given elements `f x y : p` for all `x` and `y`, such that `r S x x'` and `r S y y'` implies `f x y = f x' y'`, then `f` is defined on the whole `localization S`. -/ @[elab_as_eliminator, to_additive "Non-dependent recursion principle for localizations: given elements `f x y : p` for all `x` and `y`, such that `r S x x'` and `r S y y'` implies `f x y = f x' y'`, then `f` is defined on the whole `localization S`."] def lift_on₂ {p : Sort u} (x y : localization S) (f : M → S → M → S → p) (H : ∀ {a a' b b' c c' d d'} (hx : r S (a, b) (a', b')) (hy : r S (c, d) (c', d')), f a b c d = f a' b' c' d') : p := lift_on x (λ a b, lift_on y (f a b) (λ c c' d d' hy, H ((r S).refl _) hy)) (λ a a' b b' hx, induction_on y (λ ⟨c, d⟩, H hx ((r S).refl _))) @[to_additive] lemma lift_on₂_mk {p : Sort*} (f : M → S → M → S → p) (H) (a c : M) (b d : S) : lift_on₂ (mk a b) (mk c d) f H = f a b c d := rfl @[elab_as_eliminator, to_additive] theorem induction_on₂ {p : localization S → localization S → Prop} (x y) (H : ∀ (x y : M × S), p (mk x.1 x.2) (mk y.1 y.2)) : p x y := induction_on x $ λ x, induction_on y $ H x @[elab_as_eliminator, to_additive] theorem induction_on₃ {p : localization S → localization S → localization S → Prop} (x y z) (H : ∀ (x y z : M × S), p (mk x.1 x.2) (mk y.1 y.2) (mk z.1 z.2)) : p x y z := induction_on₂ x y $ λ x y, induction_on z $ H x y @[to_additive] lemma one_rel (y : S) : r S 1 (y, y) := λ b hb, hb y @[to_additive] theorem r_of_eq {x y : M × S} (h : y.1 * x.2 = x.1 * y.2) : r S x y := r_iff_exists.2 ⟨1, by rw h⟩ @[to_additive] lemma mk_self (a : S) : mk (a : M) a = 1 := by { symmetry, rw [← mk_one, mk_eq_mk_iff], exact one_rel a } end localization variables {S N} namespace monoid_hom /-- Makes a localization map from a `comm_monoid` hom satisfying the characteristic predicate. -/ @[to_additive "Makes a localization map from an `add_comm_monoid` hom satisfying the characteristic predicate."] def to_localization_map (f : M →* N) (H1 : ∀ y : S, is_unit (f y)) (H2 : ∀ z, ∃ x : M × S, z * f x.2 = f x.1) (H3 : ∀ x y, f x = f y ↔ ∃ c : S, x * c = y * c) : submonoid.localization_map S N := { map_units' := H1, surj' := H2, eq_iff_exists' := H3, .. f } end monoid_hom namespace submonoid namespace localization_map /-- Short for `to_monoid_hom`; used to apply a localization map as a function. -/ @[to_additive "Short for `to_add_monoid_hom`; used to apply a localization map as a function."] abbreviation to_map (f : localization_map S N) := f.to_monoid_hom @[ext, to_additive] lemma ext {f g : localization_map S N} (h : ∀ x, f.to_map x = g.to_map x) : f = g := by { rcases f with ⟨⟨⟩⟩, rcases g with ⟨⟨⟩⟩, simp only, exact funext h, } @[to_additive] lemma ext_iff {f g : localization_map S N} : f = g ↔ ∀ x, f.to_map x = g.to_map x := ⟨λ h x, h ▸ rfl, ext⟩ @[to_additive] lemma to_map_injective : function.injective (@localization_map.to_map _ _ S N _) := λ _ _ h, ext $ monoid_hom.ext_iff.1 h @[to_additive] lemma map_units (f : localization_map S N) (y : S) : is_unit (f.to_map y) := f.2 y @[to_additive] lemma surj (f : localization_map S N) (z : N) : ∃ x : M × S, z * f.to_map x.2 = f.to_map x.1 := f.3 z @[to_additive] lemma eq_iff_exists (f : localization_map S N) {x y} : f.to_map x = f.to_map y ↔ ∃ c : S, x * c = y * c := f.4 x y /-- Given a localization map `f : M →* N`, a section function sending `z : N` to some `(x, y) : M × S` such that `f x * (f y)⁻¹ = z`. -/ @[to_additive "Given a localization map `f : M →+ N`, a section function sending `z : N` to some `(x, y) : M × S` such that `f x - f y = z`."] noncomputable def sec (f : localization_map S N) (z : N) : M × S := classical.some $ f.surj z @[to_additive] lemma sec_spec {f : localization_map S N} (z : N) : z * f.to_map (f.sec z).2 = f.to_map (f.sec z).1 := classical.some_spec $ f.surj z @[to_additive] lemma sec_spec' {f : localization_map S N} (z : N) : f.to_map (f.sec z).1 = f.to_map (f.sec z).2 * z := by rw [mul_comm, sec_spec] /-- Given a monoid hom `f : M →* N` and submonoid `S ⊆ M` such that `f(S) ⊆ units N`, for all `w : M, z : N` and `y ∈ S`, we have `w * (f y)⁻¹ = z ↔ w = f y * z`. -/ @[to_additive "Given an add_monoid hom `f : M →+ N` and submonoid `S ⊆ M` such that `f(S) ⊆ add_units N`, for all `w : M, z : N` and `y ∈ S`, we have `w - f y = z ↔ w = f y + z`."] lemma mul_inv_left {f : M →* N} (h : ∀ y : S, is_unit (f y)) (y : S) (w z) : w * ↑(is_unit.lift_right (f.mrestrict S) h y)⁻¹ = z ↔ w = f y * z := by rw mul_comm; convert units.inv_mul_eq_iff_eq_mul _; exact (is_unit.coe_lift_right (f.mrestrict S) h _).symm /-- Given a monoid hom `f : M →* N` and submonoid `S ⊆ M` such that `f(S) ⊆ units N`, for all `w : M, z : N` and `y ∈ S`, we have `z = w * (f y)⁻¹ ↔ z * f y = w`. -/ @[to_additive "Given an add_monoid hom `f : M →+ N` and submonoid `S ⊆ M` such that `f(S) ⊆ add_units N`, for all `w : M, z : N` and `y ∈ S`, we have `z = w - f y ↔ z + f y = w`."] lemma mul_inv_right {f : M →* N} (h : ∀ y : S, is_unit (f y)) (y : S) (w z) : z = w * ↑(is_unit.lift_right (f.mrestrict S) h y)⁻¹ ↔ z * f y = w := by rw [eq_comm, mul_inv_left h, mul_comm, eq_comm] /-- Given a monoid hom `f : M →* N` and submonoid `S ⊆ M` such that `f(S) ⊆ units N`, for all `x₁ x₂ : M` and `y₁, y₂ ∈ S`, we have `f x₁ * (f y₁)⁻¹ = f x₂ * (f y₂)⁻¹ ↔ f (x₁ * y₂) = f (x₂ * y₁)`. -/ @[simp, to_additive "Given an add_monoid hom `f : M →+ N` and submonoid `S ⊆ M` such that `f(S) ⊆ add_units N`, for all `x₁ x₂ : M` and `y₁, y₂ ∈ S`, we have `f x₁ - f y₁ = f x₂ - f y₂ ↔ f (x₁ + y₂) = f (x₂ + y₁)`."] lemma mul_inv {f : M →* N} (h : ∀ y : S, is_unit (f y)) {x₁ x₂} {y₁ y₂ : S} : f x₁ * ↑(is_unit.lift_right (f.mrestrict S) h y₁)⁻¹ = f x₂ * ↑(is_unit.lift_right (f.mrestrict S) h y₂)⁻¹ ↔ f (x₁ * y₂) = f (x₂ * y₁) := by rw [mul_inv_right h, mul_assoc, mul_comm _ (f y₂), ←mul_assoc, mul_inv_left h, mul_comm x₂, f.map_mul, f.map_mul] /-- Given a monoid hom `f : M →* N` and submonoid `S ⊆ M` such that `f(S) ⊆ units N`, for all `y, z ∈ S`, we have `(f y)⁻¹ = (f z)⁻¹ → f y = f z`. -/ @[to_additive "Given an add_monoid hom `f : M →+ N` and submonoid `S ⊆ M` such that `f(S) ⊆ add_units N`, for all `y, z ∈ S`, we have `- (f y) = - (f z) → f y = f z`."] lemma inv_inj {f : M →* N} (hf : ∀ y : S, is_unit (f y)) {y z} (h : (is_unit.lift_right (f.mrestrict S) hf y)⁻¹ = (is_unit.lift_right (f.mrestrict S) hf z)⁻¹) : f y = f z := by rw [←mul_one (f y), eq_comm, ←mul_inv_left hf y (f z) 1, h]; convert units.inv_mul _; exact (is_unit.coe_lift_right (f.mrestrict S) hf _).symm /-- Given a monoid hom `f : M →* N` and submonoid `S ⊆ M` such that `f(S) ⊆ units N`, for all `y ∈ S`, `(f y)⁻¹` is unique. -/ @[to_additive "Given an add_monoid hom `f : M →+ N` and submonoid `S ⊆ M` such that `f(S) ⊆ add_units N`, for all `y ∈ S`, `- (f y)` is unique."] lemma inv_unique {f : M →* N} (h : ∀ y : S, is_unit (f y)) {y : S} {z} (H : f y * z = 1) : ↑(is_unit.lift_right (f.mrestrict S) h y)⁻¹ = z := by rw [←one_mul ↑(_)⁻¹, mul_inv_left, ←H] variables (f : localization_map S N) @[to_additive] lemma map_right_cancel {x y} {c : S} (h : f.to_map (c * x) = f.to_map (c * y)) : f.to_map x = f.to_map y := begin rw [f.to_map.map_mul, f.to_map.map_mul] at h, cases f.map_units c with u hu, rw ←hu at h, exact (units.mul_right_inj u).1 h, end @[to_additive] lemma map_left_cancel {x y} {c : S} (h : f.to_map (x * c) = f.to_map (y * c)) : f.to_map x = f.to_map y := f.map_right_cancel $ by rw [mul_comm _ x, mul_comm _ y, h] /-- Given a localization map `f : M →* N`, the surjection sending `(x, y) : M × S` to `f x * (f y)⁻¹`. -/ @[to_additive "Given a localization map `f : M →+ N`, the surjection sending `(x, y) : M × S` to `f x - f y`."] noncomputable def mk' (f : localization_map S N) (x : M) (y : S) : N := f.to_map x * ↑(is_unit.lift_right (f.to_map.mrestrict S) f.map_units y)⁻¹ @[to_additive] lemma mk'_mul (x₁ x₂ : M) (y₁ y₂ : S) : f.mk' (x₁ * x₂) (y₁ * y₂) = f.mk' x₁ y₁ * f.mk' x₂ y₂ := (mul_inv_left f.map_units _ _ _).2 $ show _ = _ * (_ * _ * (_ * _)), by rw [←mul_assoc, ←mul_assoc, mul_inv_right f.map_units, mul_assoc, mul_assoc, mul_comm _ (f.to_map x₂), ←mul_assoc, ←mul_assoc, mul_inv_right f.map_units, submonoid.coe_mul, f.to_map.map_mul, f.to_map.map_mul]; ac_refl @[to_additive] lemma mk'_one (x) : f.mk' x (1 : S) = f.to_map x := by rw [mk', monoid_hom.map_one]; exact mul_one _ /-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M`, for all `z : N` we have that if `x : M, y ∈ S` are such that `z * f y = f x`, then `f x * (f y)⁻¹ = z`. -/ @[simp, to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M`, for all `z : N` we have that if `x : M, y ∈ S` are such that `z + f y = f x`, then `f x - f y = z`."] lemma mk'_sec (z : N) : f.mk' (f.sec z).1 (f.sec z).2 = z := show _ * _ = _, by rw [←sec_spec, mul_inv_left, mul_comm] @[to_additive] lemma mk'_surjective (z : N) : ∃ x (y : S), f.mk' x y = z := ⟨(f.sec z).1, (f.sec z).2, f.mk'_sec z⟩ @[to_additive] lemma mk'_spec (x) (y : S) : f.mk' x y * f.to_map y = f.to_map x := show _ * _ * _ = _, by rw [mul_assoc, mul_comm _ (f.to_map y), ←mul_assoc, mul_inv_left, mul_comm] @[to_additive] lemma mk'_spec' (x) (y : S) : f.to_map y * f.mk' x y = f.to_map x := by rw [mul_comm, mk'_spec] @[to_additive] theorem eq_mk'_iff_mul_eq {x} {y : S} {z} : z = f.mk' x y ↔ z * f.to_map y = f.to_map x := ⟨λ H, by rw [H, mk'_spec], λ H, by erw [mul_inv_right, H]; refl⟩ @[to_additive] theorem mk'_eq_iff_eq_mul {x} {y : S} {z} : f.mk' x y = z ↔ f.to_map x = z * f.to_map y := by rw [eq_comm, eq_mk'_iff_mul_eq, eq_comm] @[to_additive] lemma mk'_eq_iff_eq {x₁ x₂} {y₁ y₂ : S} : f.mk' x₁ y₁ = f.mk' x₂ y₂ ↔ f.to_map (x₁ * y₂) = f.to_map (x₂ * y₁) := ⟨λ H, by rw [f.to_map.map_mul, f.mk'_eq_iff_eq_mul.1 H, mul_assoc, mul_comm (f.to_map _), ←mul_assoc, mk'_spec, f.to_map.map_mul], λ H, by rw [mk'_eq_iff_eq_mul, mk', mul_assoc, mul_comm _ (f.to_map y₁), ←mul_assoc, ←f.to_map.map_mul, ←H, f.to_map.map_mul, mul_inv_right f.map_units]⟩ @[to_additive] protected lemma eq {a₁ b₁} {a₂ b₂ : S} : f.mk' a₁ a₂ = f.mk' b₁ b₂ ↔ ∃ c : S, a₁ * b₂ * c = b₁ * a₂ * c := f.mk'_eq_iff_eq.trans $ f.eq_iff_exists @[to_additive] protected lemma eq' {a₁ b₁} {a₂ b₂ : S} : f.mk' a₁ a₂ = f.mk' b₁ b₂ ↔ localization.r S (a₁, a₂) (b₁, b₂) := by rw [f.eq, localization.r_iff_exists] @[to_additive] lemma eq_iff_eq (g : localization_map S P) {x y} : f.to_map x = f.to_map y ↔ g.to_map x = g.to_map y := f.eq_iff_exists.trans g.eq_iff_exists.symm @[to_additive] lemma mk'_eq_iff_mk'_eq (g : localization_map S P) {x₁ x₂} {y₁ y₂ : S} : f.mk' x₁ y₁ = f.mk' x₂ y₂ ↔ g.mk' x₁ y₁ = g.mk' x₂ y₂ := f.eq'.trans g.eq'.symm /-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M`, for all `x₁ : M` and `y₁ ∈ S`, if `x₂ : M, y₂ ∈ S` are such that `f x₁ * (f y₁)⁻¹ * f y₂ = f x₂`, then there exists `c ∈ S` such that `x₁ * y₂ * c = x₂ * y₁ * c`. -/ @[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M`, for all `x₁ : M` and `y₁ ∈ S`, if `x₂ : M, y₂ ∈ S` are such that `(f x₁ - f y₁) + f y₂ = f x₂`, then there exists `c ∈ S` such that `x₁ + y₂ + c = x₂ + y₁ + c`."] lemma exists_of_sec_mk' (x) (y : S) : ∃ c : S, x * (f.sec $ f.mk' x y).2 * c = (f.sec $ f.mk' x y).1 * y * c := f.eq_iff_exists.1 $ f.mk'_eq_iff_eq.1 $ (mk'_sec _ _).symm @[to_additive] lemma mk'_eq_of_eq {a₁ b₁ : M} {a₂ b₂ : S} (H : b₁ * a₂ = a₁ * b₂) : f.mk' a₁ a₂ = f.mk' b₁ b₂ := f.mk'_eq_iff_eq.2 $ H ▸ rfl @[simp, to_additive] lemma mk'_self' (y : S) : f.mk' (y : M) y = 1 := show _ * _ = _, by rw [mul_inv_left, mul_one] @[simp, to_additive] lemma mk'_self (x) (H : x ∈ S) : f.mk' x ⟨x, H⟩ = 1 := by convert mk'_self' _ _; refl @[to_additive] lemma mul_mk'_eq_mk'_of_mul (x₁ x₂) (y : S) : f.to_map x₁ * f.mk' x₂ y = f.mk' (x₁ * x₂) y := by rw [←mk'_one, ←mk'_mul, one_mul] @[to_additive] lemma mk'_mul_eq_mk'_of_mul (x₁ x₂) (y : S) : f.mk' x₂ y * f.to_map x₁ = f.mk' (x₁ * x₂) y := by rw [mul_comm, mul_mk'_eq_mk'_of_mul] @[to_additive] lemma mul_mk'_one_eq_mk' (x) (y : S) : f.to_map x * f.mk' 1 y = f.mk' x y := by rw [mul_mk'_eq_mk'_of_mul, mul_one] @[simp, to_additive] lemma mk'_mul_cancel_right (x : M) (y : S) : f.mk' (x * y) y = f.to_map x := by rw [←mul_mk'_one_eq_mk', f.to_map.map_mul, mul_assoc, mul_mk'_one_eq_mk', mk'_self', mul_one] @[to_additive] lemma mk'_mul_cancel_left (x) (y : S) : f.mk' ((y : M) * x) y = f.to_map x := by rw [mul_comm, mk'_mul_cancel_right] @[to_additive] lemma is_unit_comp (j : N →* P) (y : S) : is_unit (j.comp f.to_map y) := ⟨units.map j $ is_unit.lift_right (f.to_map.mrestrict S) f.map_units y, show j _ = j _, from congr_arg j $ (is_unit.coe_lift_right (f.to_map.mrestrict S) f.map_units _)⟩ variables {g : M →* P} /-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M` and a map of `comm_monoid`s `g : M →* P` such that `g(S) ⊆ units P`, `f x = f y → g x = g y` for all `x y : M`. -/ @[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M` and a map of `add_comm_monoid`s `g : M →+ P` such that `g(S) ⊆ add_units P`, `f x = f y → g x = g y` for all `x y : M`."] lemma eq_of_eq (hg : ∀ y : S, is_unit (g y)) {x y} (h : f.to_map x = f.to_map y) : g x = g y := begin obtain ⟨c, hc⟩ := f.eq_iff_exists.1 h, rw [←mul_one (g x), ←is_unit.mul_lift_right_inv (g.mrestrict S) hg c], show _ * (g c * _) = _, rw [←mul_assoc, ←g.map_mul, hc, mul_inv_left hg, g.map_mul, mul_comm], end /-- Given `comm_monoid`s `M, P`, localization maps `f : M →* N, k : P →* Q` for submonoids `S, T` respectively, and `g : M →* P` such that `g(S) ⊆ T`, `f x = f y` implies `k (g x) = k (g y)`. -/ @[to_additive "Given `add_comm_monoid`s `M, P`, localization maps `f : M →+ N, k : P →+ Q` for submonoids `S, T` respectively, and `g : M →+ P` such that `g(S) ⊆ T`, `f x = f y` implies `k (g x) = k (g y)`."] lemma comp_eq_of_eq {T : submonoid P} {Q : Type*} [comm_monoid Q] (hg : ∀ y : S, g y ∈ T) (k : localization_map T Q) {x y} (h : f.to_map x = f.to_map y) : k.to_map (g x) = k.to_map (g y) := f.eq_of_eq (λ y : S, show is_unit (k.to_map.comp g y), from k.map_units ⟨g y, hg y⟩) h variables (hg : ∀ y : S, is_unit (g y)) /-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M` and a map of `comm_monoid`s `g : M →* P` such that `g y` is invertible for all `y : S`, the homomorphism induced from `N` to `P` sending `z : N` to `g x * (g y)⁻¹`, where `(x, y) : M × S` are such that `z = f x * (f y)⁻¹`. -/ @[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M` and a map of `add_comm_monoid`s `g : M →+ P` such that `g y` is invertible for all `y : S`, the homomorphism induced from `N` to `P` sending `z : N` to `g x - g y`, where `(x, y) : M × S` are such that `z = f x - f y`."] noncomputable def lift : N →* P := { to_fun := λ z, g (f.sec z).1 * ↑(is_unit.lift_right (g.mrestrict S) hg (f.sec z).2)⁻¹, map_one' := by rw [mul_inv_left, mul_one]; exact f.eq_of_eq hg (by rw [←sec_spec, one_mul]), map_mul' := λ x y, begin rw [mul_inv_left hg, ←mul_assoc, ←mul_assoc, mul_inv_right hg, mul_comm _ (g (f.sec y).1), ←mul_assoc, ←mul_assoc, mul_inv_right hg], repeat { rw ←g.map_mul }, exact f.eq_of_eq hg (by repeat { rw f.to_map.map_mul <|> rw sec_spec' }; ac_refl) end } variables {S g} /-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M` and a map of `comm_monoid`s `g : M →* P` such that `g y` is invertible for all `y : S`, the homomorphism induced from `N` to `P` maps `f x * (f y)⁻¹` to `g x * (g y)⁻¹` for all `x : M, y ∈ S`. -/ @[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M` and a map of `add_comm_monoid`s `g : M →+ P` such that `g y` is invertible for all `y : S`, the homomorphism induced from `N` to `P` maps `f x - f y` to `g x - g y` for all `x : M, y ∈ S`."] lemma lift_mk' (x y) : f.lift hg (f.mk' x y) = g x * ↑(is_unit.lift_right (g.mrestrict S) hg y)⁻¹ := (mul_inv hg).2 $ f.eq_of_eq hg $ by rw [f.to_map.map_mul, f.to_map.map_mul, sec_spec', mul_assoc, f.mk'_spec, mul_comm] /-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M`, if a `comm_monoid` map `g : M →* P` induces a map `f.lift hg : N →* P` then for all `z : N, v : P`, we have `f.lift hg z = v ↔ g x = g y * v`, where `x : M, y ∈ S` are such that `z * f y = f x`. -/ @[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M`, if an `add_comm_monoid` map `g : M →+ P` induces a map `f.lift hg : N →+ P` then for all `z : N, v : P`, we have `f.lift hg z = v ↔ g x = g y + v`, where `x : M, y ∈ S` are such that `z + f y = f x`."] lemma lift_spec (z v) : f.lift hg z = v ↔ g (f.sec z).1 = g (f.sec z).2 * v := mul_inv_left hg _ _ v /-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M`, if a `comm_monoid` map `g : M →* P` induces a map `f.lift hg : N →* P` then for all `z : N, v w : P`, we have `f.lift hg z * w = v ↔ g x * w = g y * v`, where `x : M, y ∈ S` are such that `z * f y = f x`. -/ @[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M`, if an `add_comm_monoid` map `g : M →+ P` induces a map `f.lift hg : N →+ P` then for all `z : N, v w : P`, we have `f.lift hg z + w = v ↔ g x + w = g y + v`, where `x : M, y ∈ S` are such that `z + f y = f x`."] lemma lift_spec_mul (z w v) : f.lift hg z * w = v ↔ g (f.sec z).1 * w = g (f.sec z).2 * v := begin rw mul_comm, show _ * (_ * _) = _ ↔ _, rw [←mul_assoc, mul_inv_left hg, mul_comm], end @[to_additive] lemma lift_mk'_spec (x v) (y : S) : f.lift hg (f.mk' x y) = v ↔ g x = g y * v := by rw f.lift_mk' hg; exact mul_inv_left hg _ _ _ /-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M`, if a `comm_monoid` map `g : M →* P` induces a map `f.lift hg : N →* P` then for all `z : N`, we have `f.lift hg z * g y = g x`, where `x : M, y ∈ S` are such that `z * f y = f x`. -/ @[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M`, if an `add_comm_monoid` map `g : M →+ P` induces a map `f.lift hg : N →+ P` then for all `z : N`, we have `f.lift hg z + g y = g x`, where `x : M, y ∈ S` are such that `z + f y = f x`."] lemma lift_mul_right (z) : f.lift hg z * g (f.sec z).2 = g (f.sec z).1 := show _ * _ * _ = _, by erw [mul_assoc, is_unit.lift_right_inv_mul, mul_one] /-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M`, if a `comm_monoid` map `g : M →* P` induces a map `f.lift hg : N →* P` then for all `z : N`, we have `g y * f.lift hg z = g x`, where `x : M, y ∈ S` are such that `z * f y = f x`. -/ @[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M`, if an `add_comm_monoid` map `g : M →+ P` induces a map `f.lift hg : N →+ P` then for all `z : N`, we have `g y + f.lift hg z = g x`, where `x : M, y ∈ S` are such that `z + f y = f x`."] lemma lift_mul_left (z) : g (f.sec z).2 * f.lift hg z = g (f.sec z).1 := by rw [mul_comm, lift_mul_right] @[simp, to_additive] lemma lift_eq (x : M) : f.lift hg (f.to_map x) = g x := by rw [lift_spec, ←g.map_mul]; exact f.eq_of_eq hg (by rw [sec_spec', f.to_map.map_mul]) @[to_additive] lemma lift_eq_iff {x y : M × S} : f.lift hg (f.mk' x.1 x.2) = f.lift hg (f.mk' y.1 y.2) ↔ g (x.1 * y.2) = g (y.1 * x.2) := by rw [lift_mk', lift_mk', mul_inv hg] @[simp, to_additive] lemma lift_comp : (f.lift hg).comp f.to_map = g := by ext; exact f.lift_eq hg _ @[simp, to_additive] lemma lift_of_comp (j : N →* P) : f.lift (f.is_unit_comp j) = j := begin ext, rw lift_spec, show j _ = j _ * _, erw [←j.map_mul, sec_spec'], end @[to_additive] lemma epic_of_localization_map {j k : N →* P} (h : ∀ a, j.comp f.to_map a = k.comp f.to_map a) : j = k := begin rw [←f.lift_of_comp j, ←f.lift_of_comp k], congr' 1 with x, exact h x, end @[to_additive] lemma lift_unique {j : N →* P} (hj : ∀ x, j (f.to_map x) = g x) : f.lift hg = j := begin ext, rw [lift_spec, ←hj, ←hj, ←j.map_mul], apply congr_arg, rw ←sec_spec', end @[simp, to_additive] lemma lift_id (x) : f.lift f.map_units x = x := monoid_hom.ext_iff.1 (f.lift_of_comp $ monoid_hom.id N) x /-- Given two localization maps `f : M →* N, k : M →* P` for a submonoid `S ⊆ M`, the hom from `P` to `N` induced by `f` is left inverse to the hom from `N` to `P` induced by `k`. -/ @[simp, to_additive] lemma lift_left_inverse {k : localization_map S P} (z : N) : k.lift f.map_units (f.lift k.map_units z) = z := begin rw lift_spec, cases f.surj z with x hx, conv_rhs {congr, skip, rw f.eq_mk'_iff_mul_eq.2 hx}, rw [mk', ←mul_assoc, mul_inv_right f.map_units, ←f.to_map.map_mul, ←f.to_map.map_mul], apply k.eq_of_eq f.map_units, rw [k.to_map.map_mul, k.to_map.map_mul, ←sec_spec, mul_assoc, lift_spec_mul], repeat { rw ←k.to_map.map_mul }, apply f.eq_of_eq k.map_units, repeat { rw f.to_map.map_mul }, rw [sec_spec', ←hx], ac_refl, end @[to_additive] lemma lift_surjective_iff : function.surjective (f.lift hg) ↔ ∀ v : P, ∃ x : M × S, v * g x.2 = g x.1 := begin split, { intros H v, obtain ⟨z, hz⟩ := H v, obtain ⟨x, hx⟩ := f.surj z, use x, rw [←hz, f.eq_mk'_iff_mul_eq.2 hx, lift_mk', mul_assoc, mul_comm _ (g ↑x.2)], erw [is_unit.mul_lift_right_inv (g.mrestrict S) hg, mul_one] }, { intros H v, obtain ⟨x, hx⟩ := H v, use f.mk' x.1 x.2, rw [lift_mk', mul_inv_left hg, mul_comm, ←hx] } end @[to_additive] lemma lift_injective_iff : function.injective (f.lift hg) ↔ ∀ x y, f.to_map x = f.to_map y ↔ g x = g y := begin split, { intros H x y, split, { exact f.eq_of_eq hg }, { intro h, rw [←f.lift_eq hg, ←f.lift_eq hg] at h, exact H h }}, { intros H z w h, obtain ⟨x, hx⟩ := f.surj z, obtain ⟨y, hy⟩ := f.surj w, rw [←f.mk'_sec z, ←f.mk'_sec w], exact (mul_inv f.map_units).2 ((H _ _).2 $ (mul_inv hg).1 h) } end variables {T : submonoid P} (hy : ∀ y : S, g y ∈ T) {Q : Type*} [comm_monoid Q] (k : localization_map T Q) /-- Given a `comm_monoid` homomorphism `g : M →* P` where for submonoids `S ⊆ M, T ⊆ P` we have `g(S) ⊆ T`, the induced monoid homomorphism from the localization of `M` at `S` to the localization of `P` at `T`: if `f : M →* N` and `k : P →* Q` are localization maps for `S` and `T` respectively, we send `z : N` to `k (g x) * (k (g y))⁻¹`, where `(x, y) : M × S` are such that `z = f x * (f y)⁻¹`. -/ @[to_additive "Given a `add_comm_monoid` homomorphism `g : M →+ P` where for submonoids `S ⊆ M, T ⊆ P` we have `g(S) ⊆ T`, the induced add_monoid homomorphism from the localization of `M` at `S` to the localization of `P` at `T`: if `f : M →+ N` and `k : P →+ Q` are localization maps for `S` and `T` respectively, we send `z : N` to `k (g x) - k (g y)`, where `(x, y) : M × S` are such that `z = f x - f y`."] noncomputable def map : N →* Q := @lift _ _ _ _ _ _ _ f (k.to_map.comp g) $ λ y, k.map_units ⟨g y, hy y⟩ variables {k} @[to_additive] lemma map_eq (x) : f.map hy k (f.to_map x) = k.to_map (g x) := f.lift_eq (λ y, k.map_units ⟨g y, hy y⟩) x @[simp, to_additive] lemma map_comp : (f.map hy k).comp f.to_map = k.to_map.comp g := f.lift_comp $ λ y, k.map_units ⟨g y, hy y⟩ @[to_additive] lemma map_mk' (x) (y : S) : f.map hy k (f.mk' x y) = k.mk' (g x) ⟨g y, hy y⟩ := begin rw [map, lift_mk', mul_inv_left], { show k.to_map (g x) = k.to_map (g y) * _, rw mul_mk'_eq_mk'_of_mul, exact (k.mk'_mul_cancel_left (g x) ⟨(g y), hy y⟩).symm }, end /-- Given localization maps `f : M →* N, k : P →* Q` for submonoids `S, T` respectively, if a `comm_monoid` homomorphism `g : M →* P` induces a `f.map hy k : N →* Q`, then for all `z : N`, `u : Q`, we have `f.map hy k z = u ↔ k (g x) = k (g y) * u` where `x : M, y ∈ S` are such that `z * f y = f x`. -/ @[to_additive "Given localization maps `f : M →+ N, k : P →+ Q` for submonoids `S, T` respectively, if an `add_comm_monoid` homomorphism `g : M →+ P` induces a `f.map hy k : N →+ Q`, then for all `z : N`, `u : Q`, we have `f.map hy k z = u ↔ k (g x) = k (g y) + u` where `x : M, y ∈ S` are such that `z + f y = f x`."] lemma map_spec (z u) : f.map hy k z = u ↔ k.to_map (g (f.sec z).1) = k.to_map (g (f.sec z).2) * u := f.lift_spec (λ y, k.map_units ⟨g y, hy y⟩) _ _ /-- Given localization maps `f : M →* N, k : P →* Q` for submonoids `S, T` respectively, if a `comm_monoid` homomorphism `g : M →* P` induces a `f.map hy k : N →* Q`, then for all `z : N`, we have `f.map hy k z * k (g y) = k (g x)` where `x : M, y ∈ S` are such that `z * f y = f x`. -/ @[to_additive "Given localization maps `f : M →+ N, k : P →+ Q` for submonoids `S, T` respectively, if an `add_comm_monoid` homomorphism `g : M →+ P` induces a `f.map hy k : N →+ Q`, then for all `z : N`, we have `f.map hy k z + k (g y) = k (g x)` where `x : M, y ∈ S` are such that `z + f y = f x`."] lemma map_mul_right (z) : f.map hy k z * (k.to_map (g (f.sec z).2)) = k.to_map (g (f.sec z).1) := f.lift_mul_right (λ y, k.map_units ⟨g y, hy y⟩) _ /-- Given localization maps `f : M →* N, k : P →* Q` for submonoids `S, T` respectively, if a `comm_monoid` homomorphism `g : M →* P` induces a `f.map hy k : N →* Q`, then for all `z : N`, we have `k (g y) * f.map hy k z = k (g x)` where `x : M, y ∈ S` are such that `z * f y = f x`. -/ @[to_additive "Given localization maps `f : M →+ N, k : P →+ Q` for submonoids `S, T` respectively, if an `add_comm_monoid` homomorphism `g : M →+ P` induces a `f.map hy k : N →+ Q`, then for all `z : N`, we have `k (g y) + f.map hy k z = k (g x)` where `x : M, y ∈ S` are such that `z + f y = f x`."] lemma map_mul_left (z) : k.to_map (g (f.sec z).2) * f.map hy k z = k.to_map (g (f.sec z).1) := by rw [mul_comm, f.map_mul_right] @[simp, to_additive] lemma map_id (z : N) : f.map (λ y, show monoid_hom.id M y ∈ S, from y.2) f z = z := f.lift_id z /-- If `comm_monoid` homs `g : M →* P, l : P →* A` induce maps of localizations, the composition of the induced maps equals the map of localizations induced by `l ∘ g`. -/ @[to_additive "If `add_comm_monoid` homs `g : M →+ P, l : P →+ A` induce maps of localizations, the composition of the induced maps equals the map of localizations induced by `l ∘ g`."] lemma map_comp_map {A : Type*} [comm_monoid A] {U : submonoid A} {R} [comm_monoid R] (j : localization_map U R) {l : P →* A} (hl : ∀ w : T, l w ∈ U) : (k.map hl j).comp (f.map hy k) = f.map (λ x, show l.comp g x ∈ U, from hl ⟨g x, hy x⟩) j := begin ext z, show j.to_map _ * _ = j.to_map (l _) * _, { rw [mul_inv_left, ←mul_assoc, mul_inv_right], show j.to_map _ * j.to_map (l (g _)) = j.to_map (l _) * _, rw [←j.to_map.map_mul, ←j.to_map.map_mul, ←l.map_mul, ←l.map_mul], exact k.comp_eq_of_eq hl j (by rw [k.to_map.map_mul, k.to_map.map_mul, sec_spec', mul_assoc, map_mul_right]) }, end /-- If `comm_monoid` homs `g : M →* P, l : P →* A` induce maps of localizations, the composition of the induced maps equals the map of localizations induced by `l ∘ g`. -/ @[to_additive "If `add_comm_monoid` homs `g : M →+ P, l : P →+ A` induce maps of localizations, the composition of the induced maps equals the map of localizations induced by `l ∘ g`."] lemma map_map {A : Type*} [comm_monoid A] {U : submonoid A} {R} [comm_monoid R] (j : localization_map U R) {l : P →* A} (hl : ∀ w : T, l w ∈ U) (x) : k.map hl j (f.map hy k x) = f.map (λ x, show l.comp g x ∈ U, from hl ⟨g x, hy x⟩) j x := by rw ←f.map_comp_map hy j hl; refl section away_map variables (x : M) /-- Given `x : M`, the type of `comm_monoid` homomorphisms `f : M →* N` such that `N` is isomorphic to the localization of `M` at the submonoid generated by `x`. -/ @[reducible, to_additive "Given `x : M`, the type of `add_comm_monoid` homomorphisms `f : M →+ N` such that `N` is isomorphic to the localization of `M` at the submonoid generated by `x`."] def away_map (N' : Type*) [comm_monoid N'] := localization_map (powers x) N' variables (F : away_map x N) /-- Given `x : M` and a localization map `F : M →* N` away from `x`, `inv_self` is `(F x)⁻¹`. -/ noncomputable def away_map.inv_self : N := F.mk' 1 ⟨x, mem_powers _⟩ /-- Given `x : M`, a localization map `F : M →* N` away from `x`, and a map of `comm_monoid`s `g : M →* P` such that `g x` is invertible, the homomorphism induced from `N` to `P` sending `z : N` to `g y * (g x)⁻ⁿ`, where `y : M, n : ℕ` are such that `z = F y * (F x)⁻ⁿ`. -/ noncomputable def away_map.lift (hg : is_unit (g x)) : N →* P := F.lift $ λ y, show is_unit (g y.1), begin obtain ⟨n, hn⟩ := y.2, rw [←hn, g.map_pow], exact is_unit.pow n hg, end @[simp] lemma away_map.lift_eq (hg : is_unit (g x)) (a : M) : F.lift x hg (F.to_map a) = g a := lift_eq _ _ _ @[simp] lemma away_map.lift_comp (hg : is_unit (g x)) : (F.lift x hg).comp F.to_map = g := lift_comp _ _ /-- Given `x y : M` and localization maps `F : M →* N, G : M →* P` away from `x` and `x * y` respectively, the homomorphism induced from `N` to `P`. -/ noncomputable def away_to_away_right (y : M) (G : away_map (x * y) P) : N →* P := F.lift x $ show is_unit (G.to_map x), from is_unit_of_mul_eq_one (G.to_map x) (G.mk' y ⟨x * y, mem_powers _⟩) $ by rw [mul_mk'_eq_mk'_of_mul, mk'_self] end away_map end localization_map end submonoid namespace add_submonoid namespace localization_map section away_map variables {A : Type*} [add_comm_monoid A] (x : A) {B : Type*} [add_comm_monoid B] (F : away_map x B) {C : Type*} [add_comm_monoid C] {g : A →+ C} /-- Given `x : A` and a localization map `F : A →+ B` away from `x`, `neg_self` is `- (F x)`. -/ noncomputable def away_map.neg_self : B := F.mk' 0 ⟨x, mem_multiples _⟩ /-- Given `x : A`, a localization map `F : A →+ B` away from `x`, and a map of `add_comm_monoid`s `g : A →+ C` such that `g x` is invertible, the homomorphism induced from `B` to `C` sending `z : B` to `g y - n • g x`, where `y : A, n : ℕ` are such that `z = F y - n • F x`. -/ noncomputable def away_map.lift (hg : is_add_unit (g x)) : B →+ C := F.lift $ λ y, show is_add_unit (g y.1), begin obtain ⟨n, hn⟩ := y.2, rw ← hn, dsimp, rw [g.map_nsmul], exact is_add_unit.map (nsmul_add_monoid_hom n) hg, end @[simp] lemma away_map.lift_eq (hg : is_add_unit (g x)) (a : A) : F.lift x hg (F.to_map a) = g a := lift_eq _ _ _ @[simp] lemma away_map.lift_comp (hg : is_add_unit (g x)) : (F.lift x hg).comp F.to_map = g := lift_comp _ _ /-- Given `x y : A` and localization maps `F : A →+ B, G : A →+ C` away from `x` and `x + y` respectively, the homomorphism induced from `B` to `C`. -/ noncomputable def away_to_away_right (y : A) (G : away_map (x + y) C) : B →+ C := F.lift x $ show is_add_unit (G.to_map x), from is_add_unit_of_add_eq_zero (G.to_map x) (G.mk' y ⟨x + y, mem_multiples _⟩) $ by rw [add_mk'_eq_mk'_of_add, mk'_self] end away_map end localization_map end add_submonoid namespace submonoid namespace localization_map variables (f : S.localization_map N) {g : M →* P} (hg : ∀ (y : S), is_unit (g y)) {T : submonoid P} {Q : Type*} [comm_monoid Q] /-- If `f : M →* N` and `k : M →* P` are localization maps for a submonoid `S`, we get an isomorphism of `N` and `P`. -/ @[to_additive "If `f : M →+ N` and `k : M →+ R` are localization maps for a submonoid `S`, we get an isomorphism of `N` and `R`."] noncomputable def mul_equiv_of_localizations (k : localization_map S P) : N ≃* P := ⟨f.lift k.map_units, k.lift f.map_units, f.lift_left_inverse, k.lift_left_inverse, monoid_hom.map_mul _⟩ @[simp, to_additive] lemma mul_equiv_of_localizations_apply {k : localization_map S P} {x} : f.mul_equiv_of_localizations k x = f.lift k.map_units x := rfl @[simp, to_additive] lemma mul_equiv_of_localizations_symm_apply {k : localization_map S P} {x} : (f.mul_equiv_of_localizations k).symm x = k.lift f.map_units x := rfl @[to_additive] lemma mul_equiv_of_localizations_symm_eq_mul_equiv_of_localizations {k : localization_map S P} : (k.mul_equiv_of_localizations f).symm = f.mul_equiv_of_localizations k := rfl /-- If `f : M →* N` is a localization map for a submonoid `S` and `k : N ≃* P` is an isomorphism of `comm_monoid`s, `k ∘ f` is a localization map for `M` at `S`. -/ @[to_additive "If `f : M →+ N` is a localization map for a submonoid `S` and `k : N ≃+ P` is an isomorphism of `add_comm_monoid`s, `k ∘ f` is a localization map for `M` at `S`."] def of_mul_equiv_of_localizations (k : N ≃* P) : localization_map S P := (k.to_monoid_hom.comp f.to_map).to_localization_map (λ y, is_unit_comp f k.to_monoid_hom y) (λ v, let ⟨z, hz⟩ := k.to_equiv.surjective v in let ⟨x, hx⟩ := f.surj z in ⟨x, show v * k _ = k _, by rw [←hx, k.map_mul, ←hz]; refl⟩) (λ x y, k.apply_eq_iff_eq.trans f.eq_iff_exists) @[simp, to_additive] lemma of_mul_equiv_of_localizations_apply {k : N ≃* P} (x) : (f.of_mul_equiv_of_localizations k).to_map x = k (f.to_map x) := rfl @[to_additive] lemma of_mul_equiv_of_localizations_eq {k : N ≃* P} : (f.of_mul_equiv_of_localizations k).to_map = k.to_monoid_hom.comp f.to_map := rfl @[to_additive] lemma symm_comp_of_mul_equiv_of_localizations_apply {k : N ≃* P} (x) : k.symm ((f.of_mul_equiv_of_localizations k).to_map x) = f.to_map x := k.symm_apply_apply (f.to_map x) @[to_additive] lemma symm_comp_of_mul_equiv_of_localizations_apply' {k : P ≃* N} (x) : k ((f.of_mul_equiv_of_localizations k.symm).to_map x) = f.to_map x := k.apply_symm_apply (f.to_map x) @[to_additive] lemma of_mul_equiv_of_localizations_eq_iff_eq {k : N ≃* P} {x y} : (f.of_mul_equiv_of_localizations k).to_map x = y ↔ f.to_map x = k.symm y := k.to_equiv.eq_symm_apply.symm @[to_additive add_equiv_of_localizations_right_inv] lemma mul_equiv_of_localizations_right_inv (k : localization_map S P) : f.of_mul_equiv_of_localizations (f.mul_equiv_of_localizations k) = k := to_map_injective $ f.lift_comp k.map_units @[simp, to_additive add_equiv_of_localizations_right_inv_apply] lemma mul_equiv_of_localizations_right_inv_apply {k : localization_map S P} {x} : (f.of_mul_equiv_of_localizations (f.mul_equiv_of_localizations k)).to_map x = k.to_map x := ext_iff.1 (f.mul_equiv_of_localizations_right_inv k) x @[to_additive] lemma mul_equiv_of_localizations_left_inv (k : N ≃* P) : f.mul_equiv_of_localizations (f.of_mul_equiv_of_localizations k) = k := mul_equiv.ext $ monoid_hom.ext_iff.1 $ f.lift_of_comp k.to_monoid_hom @[simp, to_additive] lemma mul_equiv_of_localizations_left_inv_apply {k : N ≃* P} (x) : f.mul_equiv_of_localizations (f.of_mul_equiv_of_localizations k) x = k x := by rw mul_equiv_of_localizations_left_inv @[simp, to_additive] lemma of_mul_equiv_of_localizations_id : f.of_mul_equiv_of_localizations (mul_equiv.refl N) = f := by ext; refl @[to_additive] lemma of_mul_equiv_of_localizations_comp {k : N ≃* P} {j : P ≃* Q} : (f.of_mul_equiv_of_localizations (k.trans j)).to_map = j.to_monoid_hom.comp (f.of_mul_equiv_of_localizations k).to_map := by ext; refl /-- Given `comm_monoid`s `M, P` and submonoids `S ⊆ M, T ⊆ P`, if `f : M →* N` is a localization map for `S` and `k : P ≃* M` is an isomorphism of `comm_monoid`s such that `k(T) = S`, `f ∘ k` is a localization map for `T`. -/ @[to_additive "Given `comm_monoid`s `M, P` and submonoids `S ⊆ M, T ⊆ P`, if `f : M →* N` is a localization map for `S` and `k : P ≃* M` is an isomorphism of `comm_monoid`s such that `k(T) = S`, `f ∘ k` is a localization map for `T`."] def of_mul_equiv_of_dom {k : P ≃* M} (H : T.map k.to_monoid_hom = S) : localization_map T N := let H' : S.comap k.to_monoid_hom = T := H ▸ (set_like.coe_injective $ T.1.preimage_image_eq k.to_equiv.injective) in (f.to_map.comp k.to_monoid_hom).to_localization_map (λ y, let ⟨z, hz⟩ := f.map_units ⟨k y, H ▸ set.mem_image_of_mem k y.2⟩ in ⟨z, hz⟩) (λ z, let ⟨x, hx⟩ := f.surj z in let ⟨v, hv⟩ := k.to_equiv.surjective x.1 in let ⟨w, hw⟩ := k.to_equiv.surjective x.2 in ⟨(v, ⟨w, H' ▸ show k w ∈ S, from hw.symm ▸ x.2.2⟩), show z * f.to_map (k.to_equiv w) = f.to_map (k.to_equiv v), by erw [hv, hw, hx]; refl⟩) (λ x y, show f.to_map _ = f.to_map _ ↔ _, by erw f.eq_iff_exists; exact ⟨λ ⟨c, hc⟩, let ⟨d, hd⟩ := k.to_equiv.surjective c in ⟨⟨d, H' ▸ show k d ∈ S, from hd.symm ▸ c.2⟩, by erw [←hd, ←k.map_mul, ←k.map_mul] at hc; exact k.to_equiv.injective hc⟩, λ ⟨c, hc⟩, ⟨⟨k c, H ▸ set.mem_image_of_mem k c.2⟩, by erw ←k.map_mul; rw [hc, k.map_mul]; refl⟩⟩) @[simp, to_additive] lemma of_mul_equiv_of_dom_apply {k : P ≃* M} (H : T.map k.to_monoid_hom = S) (x) : (f.of_mul_equiv_of_dom H).to_map x = f.to_map (k x) := rfl @[to_additive] lemma of_mul_equiv_of_dom_eq {k : P ≃* M} (H : T.map k.to_monoid_hom = S) : (f.of_mul_equiv_of_dom H).to_map = f.to_map.comp k.to_monoid_hom := rfl @[to_additive] lemma of_mul_equiv_of_dom_comp_symm {k : P ≃* M} (H : T.map k.to_monoid_hom = S) (x) : (f.of_mul_equiv_of_dom H).to_map (k.symm x) = f.to_map x := congr_arg f.to_map $ k.apply_symm_apply x @[to_additive] lemma of_mul_equiv_of_dom_comp {k : M ≃* P} (H : T.map k.symm.to_monoid_hom = S) (x) : (f.of_mul_equiv_of_dom H).to_map (k x) = f.to_map x := congr_arg f.to_map $ k.symm_apply_apply x /-- A special case of `f ∘ id = f`, `f` a localization map. -/ @[simp, to_additive "A special case of `f ∘ id = f`, `f` a localization map."] lemma of_mul_equiv_of_dom_id : f.of_mul_equiv_of_dom (show S.map (mul_equiv.refl M).to_monoid_hom = S, from submonoid.ext $ λ x, ⟨λ ⟨y, hy, h⟩, h ▸ hy, λ h, ⟨x, h, rfl⟩⟩) = f := by ext; refl /-- Given localization maps `f : M →* N, k : P →* U` for submonoids `S, T` respectively, an isomorphism `j : M ≃* P` such that `j(S) = T` induces an isomorphism of localizations `N ≃* U`. -/ @[to_additive "Given localization maps `f : M →+ N, k : P →+ U` for submonoids `S, T` respectively, an isomorphism `j : M ≃+ P` such that `j(S) = T` induces an isomorphism of localizations `N ≃+ U`."] noncomputable def mul_equiv_of_mul_equiv (k : localization_map T Q) {j : M ≃* P} (H : S.map j.to_monoid_hom = T) : N ≃* Q := f.mul_equiv_of_localizations $ k.of_mul_equiv_of_dom H @[simp, to_additive] lemma mul_equiv_of_mul_equiv_eq_map_apply {k : localization_map T Q} {j : M ≃* P} (H : S.map j.to_monoid_hom = T) (x) : f.mul_equiv_of_mul_equiv k H x = f.map (λ y : S, show j.to_monoid_hom y ∈ T, from H ▸ set.mem_image_of_mem j y.2) k x := rfl @[to_additive] lemma mul_equiv_of_mul_equiv_eq_map {k : localization_map T Q} {j : M ≃* P} (H : S.map j.to_monoid_hom = T) : (f.mul_equiv_of_mul_equiv k H).to_monoid_hom = f.map (λ y : S, show j.to_monoid_hom y ∈ T, from H ▸ set.mem_image_of_mem j y.2) k := rfl @[simp, to_additive] lemma mul_equiv_of_mul_equiv_eq {k : localization_map T Q} {j : M ≃* P} (H : S.map j.to_monoid_hom = T) (x) : f.mul_equiv_of_mul_equiv k H (f.to_map x) = k.to_map (j x) := f.map_eq (λ y : S, H ▸ set.mem_image_of_mem j y.2) _ @[simp, to_additive] lemma mul_equiv_of_mul_equiv_mk' {k : localization_map T Q} {j : M ≃* P} (H : S.map j.to_monoid_hom = T) (x y) : f.mul_equiv_of_mul_equiv k H (f.mk' x y) = k.mk' (j x) ⟨j y, H ▸ set.mem_image_of_mem j y.2⟩ := f.map_mk' (λ y : S, H ▸ set.mem_image_of_mem j y.2) _ _ @[simp, to_additive] lemma of_mul_equiv_of_mul_equiv_apply {k : localization_map T Q} {j : M ≃* P} (H : S.map j.to_monoid_hom = T) (x) : (f.of_mul_equiv_of_localizations (f.mul_equiv_of_mul_equiv k H)).to_map x = k.to_map (j x) := ext_iff.1 (f.mul_equiv_of_localizations_right_inv (k.of_mul_equiv_of_dom H)) x @[to_additive] lemma of_mul_equiv_of_mul_equiv {k : localization_map T Q} {j : M ≃* P} (H : S.map j.to_monoid_hom = T) : (f.of_mul_equiv_of_localizations (f.mul_equiv_of_mul_equiv k H)).to_map = k.to_map.comp j.to_monoid_hom := monoid_hom.ext $ f.of_mul_equiv_of_mul_equiv_apply H end localization_map end submonoid namespace localization variables (S) /-- Natural hom sending `x : M`, `M` a `comm_monoid`, to the equivalence class of `(x, 1)` in the localization of `M` at a submonoid. -/ @[to_additive "Natural homomorphism sending `x : M`, `M` an `add_comm_monoid`, to the equivalence class of `(x, 0)` in the localization of `M` at a submonoid."] def monoid_of : submonoid.localization_map S (localization S) := { to_fun := λ x, mk x 1, map_one' := mk_one, map_mul' := λ x y, by rw [mk_mul, mul_one], map_units' := λ y, is_unit_iff_exists_inv.2 ⟨mk 1 y, by rw [mk_mul, mul_one, one_mul, mk_self]⟩, surj' := λ z, induction_on z $ λ x, ⟨x, by rw [mk_mul, mul_comm x.fst, ← mk_mul, mk_self, one_mul]⟩, eq_iff_exists' := λ x y, mk_eq_mk_iff.trans $ r_iff_exists.trans $ show (∃ (c : S), x * 1 * c = y * 1 * c) ↔ _, by rw [mul_one, mul_one], ..(r S).mk'.comp $ monoid_hom.inl M S } variables {S} @[to_additive] lemma mk_one_eq_monoid_of_mk (x) : mk x 1 = (monoid_of S).to_map x := rfl @[to_additive] lemma mk_eq_monoid_of_mk'_apply (x y) : mk x y = (monoid_of S).mk' x y := show _ = _ * _, from (submonoid.localization_map.mul_inv_right (monoid_of S).map_units _ _ _).2 $ begin rw [←mk_one_eq_monoid_of_mk, ←mk_one_eq_monoid_of_mk, show mk x y * mk y 1 = mk (x * y) (1 * y), by rw [mul_comm 1 y, mk_mul], show mk x 1 = mk (x * 1) ((1 : S) * 1), by rw [mul_one, mul_one]], exact mk_eq_mk_iff.2 (con.symm _ $ (localization.r S).mul (con.refl _ (x, 1)) $ one_rel _), end @[simp, to_additive] lemma mk_eq_monoid_of_mk' : mk = (monoid_of S).mk' := funext $ λ _, funext $ λ _, mk_eq_monoid_of_mk'_apply _ _ universes u @[simp, to_additive] lemma lift_on_mk' {p : Sort u} (f : ∀ (a : M) (b : S), p) (H) (a : M) (b : S) : lift_on ((monoid_of S).mk' a b) f H = f a b := by rw [← mk_eq_monoid_of_mk', lift_on_mk] @[simp, to_additive] lemma lift_on₂_mk' {p : Sort*} (f : M → S → M → S → p) (H) (a c : M) (b d : S) : lift_on₂ ((monoid_of S).mk' a b) ((monoid_of S).mk' c d) f H = f a b c d := by rw [← mk_eq_monoid_of_mk', lift_on₂_mk] variables (f : submonoid.localization_map S N) /-- Given a localization map `f : M →* N` for a submonoid `S`, we get an isomorphism between the localization of `M` at `S` as a quotient type and `N`. -/ @[to_additive "Given a localization map `f : M →+ N` for a submonoid `S`, we get an isomorphism between the localization of `M` at `S` as a quotient type and `N`."] noncomputable def mul_equiv_of_quotient (f : submonoid.localization_map S N) : localization S ≃* N := (monoid_of S).mul_equiv_of_localizations f variables {f} @[simp, to_additive] lemma mul_equiv_of_quotient_apply (x) : mul_equiv_of_quotient f x = (monoid_of S).lift f.map_units x := rfl @[simp, to_additive] lemma mul_equiv_of_quotient_mk' (x y) : mul_equiv_of_quotient f ((monoid_of S).mk' x y) = f.mk' x y := (monoid_of S).lift_mk' _ _ _ @[to_additive] lemma mul_equiv_of_quotient_mk (x y) : mul_equiv_of_quotient f (mk x y) = f.mk' x y := by rw mk_eq_monoid_of_mk'_apply; exact mul_equiv_of_quotient_mk' _ _ @[simp, to_additive] lemma mul_equiv_of_quotient_monoid_of (x) : mul_equiv_of_quotient f ((monoid_of S).to_map x) = f.to_map x := (monoid_of S).lift_eq _ _ @[simp, to_additive] lemma mul_equiv_of_quotient_symm_mk' (x y) : (mul_equiv_of_quotient f).symm (f.mk' x y) = (monoid_of S).mk' x y := f.lift_mk' _ _ _ @[to_additive] lemma mul_equiv_of_quotient_symm_mk (x y) : (mul_equiv_of_quotient f).symm (f.mk' x y) = mk x y := by rw mk_eq_monoid_of_mk'_apply; exact mul_equiv_of_quotient_symm_mk' _ _ @[simp, to_additive] lemma mul_equiv_of_quotient_symm_monoid_of (x) : (mul_equiv_of_quotient f).symm (f.to_map x) = (monoid_of S).to_map x := f.lift_eq _ _ section away variables (x : M) /-- Given `x : M`, the localization of `M` at the submonoid generated by `x`, as a quotient. -/ @[reducible, to_additive "Given `x : M`, the localization of `M` at the submonoid generated by `x`, as a quotient."] def away := localization (submonoid.powers x) /-- Given `x : M`, `inv_self` is `x⁻¹` in the localization (as a quotient type) of `M` at the submonoid generated by `x`. -/ @[to_additive "Given `x : M`, `neg_self` is `-x` in the localization (as a quotient type) of `M` at the submonoid generated by `x`."] def away.inv_self : away x := mk 1 ⟨x, submonoid.mem_powers _⟩ /-- Given `x : M`, the natural hom sending `y : M`, `M` a `comm_monoid`, to the equivalence class of `(y, 1)` in the localization of `M` at the submonoid generated by `x`. -/ @[reducible, to_additive "Given `x : M`, the natural hom sending `y : M`, `M` an `add_comm_monoid`, to the equivalence class of `(y, 0)` in the localization of `M` at the submonoid generated by `x`."] def away.monoid_of : submonoid.localization_map.away_map x (away x) := monoid_of (submonoid.powers x) @[simp, to_additive] lemma away.mk_eq_monoid_of_mk' : mk = (away.monoid_of x).mk' := mk_eq_monoid_of_mk' /-- Given `x : M` and a localization map `f : M →* N` away from `x`, we get an isomorphism between the localization of `M` at the submonoid generated by `x` as a quotient type and `N`. -/ @[to_additive "Given `x : M` and a localization map `f : M →+ N` away from `x`, we get an isomorphism between the localization of `M` at the submonoid generated by `x` as a quotient type and `N`."] noncomputable def away.mul_equiv_of_quotient (f : submonoid.localization_map.away_map x N) : away x ≃* N := mul_equiv_of_quotient f end away end localization
676f54ab868be466ed671555fe77191dedba46a8
491068d2ad28831e7dade8d6dff871c3e49d9431
/tests/lean/run/export2.lean
d81d5f0ed82bbb3a778f63f48ffb0f443494316c
[ "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
150
lean
open nat definition foo := 30 namespace foo definition x : nat := 10 definition y : nat := 20 end foo export foo example : x + y = foo := rfl
ead9972f81402ad9cfc18a48074cf66c4439cbc7
9dc8cecdf3c4634764a18254e94d43da07142918
/src/ring_theory/adjoin/power_basis.lean
a2b132111e4d0141eee5eb2b4b7534dcf6a9342b
[ "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
8,012
lean
/- Copyright (c) 2021 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen -/ import ring_theory.adjoin.basic import ring_theory.power_basis import linear_algebra.matrix.basis /-! # Power basis for `algebra.adjoin R {x}` This file defines the canonical power basis on `algebra.adjoin R {x}`, where `x` is an integral element over `R`. -/ variables {K S : Type*} [field K] [comm_ring S] [algebra K S] namespace algebra open polynomial open power_basis open_locale big_operators /-- The elements `1, x, ..., x ^ (d - 1)` for a basis for the `K`-module `K[x]`, where `d` is the degree of the minimal polynomial of `x`. -/ noncomputable def adjoin.power_basis_aux {x : S} (hx : _root_.is_integral K x) : basis (fin (minpoly K x).nat_degree) K (adjoin K ({x} : set S)) := begin have hST : function.injective (algebra_map (adjoin K ({x} : set S)) S) := subtype.coe_injective, have hx' : _root_.is_integral K (show adjoin K ({x} : set S), from ⟨x, subset_adjoin (set.mem_singleton x)⟩), { apply (is_integral_algebra_map_iff hST).mp, convert hx, apply_instance }, have minpoly_eq := minpoly.eq_of_algebra_map_eq hST hx' rfl, apply @basis.mk (fin (minpoly K x).nat_degree) _ (adjoin K {x}) (λ i, ⟨x, subset_adjoin (set.mem_singleton x)⟩ ^ (i : ℕ)), { have := hx'.linear_independent_pow, rwa minpoly_eq at this }, { rintros ⟨y, hy⟩ _, have := hx'.mem_span_pow, rw minpoly_eq at this, apply this, { rw [adjoin_singleton_eq_range_aeval] at hy, obtain ⟨f, rfl⟩ := (aeval x).mem_range.mp hy, use f, ext, exact (is_scalar_tower.algebra_map_aeval K (adjoin K {x}) S ⟨x, _⟩ _).symm } } end /-- The power basis `1, x, ..., x ^ (d - 1)` for `K[x]`, where `d` is the degree of the minimal polynomial of `x`. See `algebra.adjoin.power_basis'` for a version over a more general base ring. -/ @[simps gen dim] noncomputable def adjoin.power_basis {x : S} (hx : _root_.is_integral K x) : power_basis K (adjoin K ({x} : set S)) := { gen := ⟨x, subset_adjoin (set.mem_singleton x)⟩, dim := (minpoly K x).nat_degree, basis := adjoin.power_basis_aux hx, basis_eq_pow := basis.mk_apply _ _ } end algebra open algebra /-- The power basis given by `x` if `B.gen ∈ adjoin K {x}`. See `power_basis.of_gen_mem_adjoin'` for a version over a more general base ring. -/ @[simps] noncomputable def power_basis.of_gen_mem_adjoin {x : S} (B : power_basis K S) (hint : _root_.is_integral K x) (hx : B.gen ∈ adjoin K ({x} : set S)) : power_basis K S := (algebra.adjoin.power_basis hint).map $ (subalgebra.equiv_of_eq _ _ $ power_basis.adjoin_eq_top_of_gen_mem_adjoin hx).trans subalgebra.top_equiv section is_integral namespace power_basis open polynomial open_locale polynomial variables {R : Type*} [comm_ring R] [algebra R S] [algebra R K] [is_scalar_tower R K S] variables {A : Type*} [comm_ring A] [algebra R A] [algebra S A] variables [is_scalar_tower R S A] {B : power_basis S A} (hB : is_integral R B.gen) include hB /-- If `B : power_basis S A` is such that `is_integral R B.gen`, then `is_integral R (B.basis.repr (B.gen ^ n) i)` for all `i` if `minpoly S B.gen = (minpoly R B.gen).map (algebra_map R S)`. This is the case if `R` is a GCD domain and `S` is its fraction ring. -/ lemma repr_gen_pow_is_integral [is_domain S] (hmin : minpoly S B.gen = (minpoly R B.gen).map (algebra_map R S)) (n : ℕ) : ∀ i, is_integral R (B.basis.repr (B.gen ^ n) i) := begin intro i, let Q := (X ^ n) %ₘ (minpoly R B.gen), have : B.gen ^ n = aeval B.gen Q, { rw [← @aeval_X_pow R _ _ _ _ B.gen, ← mod_by_monic_add_div (X ^ n) (minpoly.monic hB)], simp }, by_cases hQ : Q = 0, { simp [this, hQ, is_integral_zero] }, have hlt : Q.nat_degree < B.dim, { rw [← B.nat_degree_minpoly, hmin, (minpoly.monic hB).nat_degree_map, nat_degree_lt_nat_degree_iff hQ], letI : nontrivial R := nontrivial.of_polynomial_ne hQ, exact degree_mod_by_monic_lt _ (minpoly.monic hB), apply_instance }, rw [this, aeval_eq_sum_range' hlt], simp only [linear_equiv.map_sum, linear_equiv.map_smulₛₗ, ring_hom.id_apply, finset.sum_apply'], refine is_integral.sum _ (λ j hj, _), replace hj := finset.mem_range.1 hj, rw [← fin.coe_mk hj, ← B.basis_eq_pow, algebra.smul_def, is_scalar_tower.algebra_map_apply R S A, ← algebra.smul_def, linear_equiv.map_smul], simp only [algebra_map_smul, finsupp.coe_smul, pi.smul_apply, B.basis.repr_self_apply], by_cases hij : (⟨j, hj⟩ : fin _) = i, { simp only [hij, eq_self_iff_true, if_true], rw [algebra.smul_def, mul_one], exact is_integral_algebra_map }, { simp [hij, is_integral_zero] } end variable {B} /-- Let `B : power_basis S A` be such that `is_integral R B.gen`, and let `x y : A` be elements with integral coordinates in the base `B.basis`. Then `is_integral R ((B.basis.repr (x * y) i)` for all `i` if `minpoly S B.gen = (minpoly R B.gen).map (algebra_map R S)`. This is the case if `R` is a GCD domain and `S` is its fraction ring. -/ lemma repr_mul_is_integral [is_domain S] {x y : A} (hx : ∀ i, is_integral R (B.basis.repr x i)) (hy : ∀ i, is_integral R (B.basis.repr y i)) (hmin : minpoly S B.gen = (minpoly R B.gen).map (algebra_map R S)) : ∀ i, is_integral R ((B.basis.repr (x * y) i)) := begin intro i, rw [← B.basis.sum_repr x, ← B.basis.sum_repr y, finset.sum_mul_sum, linear_equiv.map_sum, finset.sum_apply'], refine is_integral.sum _ (λ I hI, _), simp only [algebra.smul_mul_assoc, algebra.mul_smul_comm, linear_equiv.map_smulₛₗ, ring_hom.id_apply, finsupp.coe_smul, pi.smul_apply, id.smul_eq_mul], refine is_integral_mul (hy _) (is_integral_mul (hx _) _), simp only [coe_basis, ← pow_add], refine repr_gen_pow_is_integral hB hmin _ _, end /-- Let `B : power_basis S A` be such that `is_integral R B.gen`, and let `x : A` be and element with integral coordinates in the base `B.basis`. Then `is_integral R ((B.basis.repr (x ^ n) i)` for all `i` and all `n` if `minpoly S B.gen = (minpoly R B.gen).map (algebra_map R S)`. This is the case if `R` is a GCD domain and `S` is its fraction ring. -/ lemma repr_pow_is_integral [is_domain S] {x : A} (hx : ∀ i, is_integral R (B.basis.repr x i)) (hmin : minpoly S B.gen = (minpoly R B.gen).map (algebra_map R S)) (n : ℕ) : ∀ i, is_integral R ((B.basis.repr (x ^ n) i)) := begin nontriviality A using [subsingleton.elim (x ^ n) 0, is_integral_zero], revert hx, refine nat.case_strong_induction_on n _ (λ n hn, _), { intros hx i, rw [pow_zero, ← pow_zero B.gen, ← fin.coe_mk B.dim_pos, ← B.basis_eq_pow, B.basis.repr_self_apply], split_ifs, { exact is_integral_one }, { exact is_integral_zero } }, { intros hx, rw [pow_succ], exact repr_mul_is_integral hB hx (λ _, hn _ le_rfl (λ _, hx _) _) hmin } end /-- Let `B B' : power_basis K S` be such that `is_integral R B.gen`, and let `P : R[X]` be such that `aeval B.gen P = B'.gen`. Then `is_integral R (B.basis.to_matrix B'.basis i j)` for all `i` and `j` if `minpoly K B.gen = (minpoly R B.gen).map (algebra_map R L)`. This is the case if `R` is a GCD domain and `K` is its fraction ring. -/ lemma to_matrix_is_integral {B B' : power_basis K S} {P : R[X]} (h : aeval B.gen P = B'.gen) (hB : is_integral R B.gen) (hmin : minpoly K B.gen = (minpoly R B.gen).map (algebra_map R K)) : ∀ i j, _root_.is_integral R (B.basis.to_matrix B'.basis i j) := begin intros i j, rw [B.basis.to_matrix_apply, B'.coe_basis], refine repr_pow_is_integral hB (λ i, _) hmin _ _, rw [← h, aeval_eq_sum_range, linear_equiv.map_sum, finset.sum_apply'], refine is_integral.sum _ (λ n hn, _), rw [algebra.smul_def, is_scalar_tower.algebra_map_apply R K S, ← algebra.smul_def, linear_equiv.map_smul, algebra_map_smul], exact is_integral_smul _ (repr_gen_pow_is_integral hB hmin _ _), end end power_basis end is_integral
19e4805c5286a6d6cbc74824da816e84e224c0d0
8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3
/src/logic/embedding.lean
0aa6294d9f742228119b6033c1c4b569f95f71f0
[ "Apache-2.0" ]
permissive
troyjlee/mathlib
e18d4b8026e32062ab9e89bc3b003a5d1cfec3f5
45e7eb8447555247246e3fe91c87066506c14875
refs/heads/master
1,689,248,035,046
1,629,470,528,000
1,629,470,528,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
15,474
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro -/ import data.equiv.basic import data.sigma.basic /-! # Injective functions -/ universes u v w x namespace function /-- `α ↪ β` is a bundled injective function. -/ @[nolint has_inhabited_instance] -- depending on cardinalities, an injective function may not exist structure embedding (α : Sort*) (β : Sort*) := (to_fun : α → β) (inj' : injective to_fun) infixr ` ↪ `:25 := embedding instance {α : Sort u} {β : Sort v} : has_coe_to_fun (α ↪ β) := ⟨_, embedding.to_fun⟩ initialize_simps_projections embedding (to_fun → apply) end function section equiv variables {α : Sort u} {β : Sort v} (f : α ≃ β) /-- Convert an `α ≃ β` to `α ↪ β`. This is also available as a coercion `equiv.coe_embedding`. The explicit `equiv.to_embedding` version is preferred though, since the coercion can have issues inferring the type of the resulting embedding. For example: ```lean -- Works: example (s : finset (fin 3)) (f : equiv.perm (fin 3)) : s.map f.to_embedding = s.map f := by simp -- Error, `f` has type `fin 3 ≃ fin 3` but is expected to have type `fin 3 ↪ ?m_1 : Type ?` example (s : finset (fin 3)) (f : equiv.perm (fin 3)) : s.map f = s.map f.to_embedding := by simp ``` -/ @[simps] protected def equiv.to_embedding : α ↪ β := ⟨f, f.injective⟩ instance equiv.coe_embedding : has_coe (α ≃ β) (α ↪ β) := ⟨equiv.to_embedding⟩ @[reducible] instance equiv.perm.coe_embedding : has_coe (equiv.perm α) (α ↪ α) := equiv.coe_embedding @[simp] lemma equiv.coe_eq_to_embedding : ↑f = f.to_embedding := rfl /-- Given an equivalence to a subtype, produce an embedding to the elements of the corresponding set. -/ @[simps] def equiv.as_embedding {p : β → Prop} (e : α ≃ subtype p) : α ↪ β := ⟨coe ∘ e, subtype.coe_injective.comp e.injective⟩ @[simp] lemma equiv.as_embedding_range {α β : Sort*} {p : β → Prop} (e : α ≃ subtype p) : set.range e.as_embedding = set_of p := set.ext $ λ x, ⟨λ ⟨y, h⟩, h ▸ subtype.coe_prop (e y), λ hs, ⟨e.symm ⟨x, hs⟩, by simp⟩⟩ end equiv namespace function namespace embedding lemma coe_injective {α β} : @function.injective (α ↪ β) (α → β) coe_fn | ⟨x, _⟩ ⟨y, _⟩ rfl := rfl @[ext] lemma ext {α β} {f g : embedding α β} (h : ∀ x, f x = g x) : f = g := coe_injective (funext h) lemma ext_iff {α β} {f g : embedding α β} : (∀ x, f x = g x) ↔ f = g := ⟨ext, λ h _, by rw h⟩ @[simp] theorem to_fun_eq_coe {α β} (f : α ↪ β) : to_fun f = f := rfl @[simp] theorem coe_fn_mk {α β} (f : α → β) (i) : (@mk _ _ f i : α → β) = f := rfl @[simp] lemma mk_coe {α β : Type*} (f : α ↪ β) (inj) : (⟨f, inj⟩ : α ↪ β) = f := by { ext, simp } theorem injective {α β} (f : α ↪ β) : injective f := f.inj' @[simp] lemma apply_eq_iff_eq {α β : Type*} (f : α ↪ β) (x y : α) : f x = f y ↔ x = y := f.injective.eq_iff @[refl, simps {simp_rhs := tt}] protected def refl (α : Sort*) : α ↪ α := ⟨id, injective_id⟩ @[trans, simps {simp_rhs := tt}] protected def trans {α β γ} (f : α ↪ β) (g : β ↪ γ) : α ↪ γ := ⟨g ∘ f, g.injective.comp f.injective⟩ @[simp] lemma equiv_to_embedding_trans_symm_to_embedding {α β : Sort*} (e : α ≃ β) : e.to_embedding.trans e.symm.to_embedding = embedding.refl _ := by { ext, simp, } @[simp] lemma equiv_symm_to_embedding_trans_to_embedding {α β : Sort*} (e : α ≃ β) : e.symm.to_embedding.trans e.to_embedding = embedding.refl _ := by { ext, simp, } protected def congr {α : Sort u} {β : Sort v} {γ : Sort w} {δ : Sort x} (e₁ : α ≃ β) (e₂ : γ ≃ δ) (f : α ↪ γ) : (β ↪ δ) := (equiv.to_embedding e₁.symm).trans (f.trans e₂.to_embedding) /-- A right inverse `surj_inv` of a surjective function as an `embedding`. -/ protected noncomputable def of_surjective {α β} (f : β → α) (hf : surjective f) : α ↪ β := ⟨surj_inv hf, injective_surj_inv _⟩ /-- Convert a surjective `embedding` to an `equiv` -/ protected noncomputable def equiv_of_surjective {α β} (f : α ↪ β) (hf : surjective f) : α ≃ β := equiv.of_bijective f ⟨f.injective, hf⟩ /-- There is always an embedding from an empty type. --/ protected def of_is_empty {α β} [is_empty α] : α ↪ β := ⟨is_empty_elim, is_empty_elim⟩ protected def of_not_nonempty {α β} (hα : ¬ nonempty α) : α ↪ β := ⟨λa, (hα ⟨a⟩).elim, assume a, (hα ⟨a⟩).elim⟩ /-- Change the value of an embedding `f` at one point. If the prescribed image is already occupied by some `f a'`, then swap the values at these two points. -/ def set_value {α β} (f : α ↪ β) (a : α) (b : β) [∀ a', decidable (a' = a)] [∀ a', decidable (f a' = b)] : α ↪ β := ⟨λ a', if a' = a then b else if f a' = b then f a else f a', begin intros x y h, dsimp at h, split_ifs at h; try { substI b }; try { simp only [f.injective.eq_iff] at * }; cc end⟩ theorem set_value_eq {α β} (f : α ↪ β) (a : α) (b : β) [∀ a', decidable (a' = a)] [∀ a', decidable (f a' = b)] : set_value f a b a = b := by simp [set_value] /-- Embedding into `option` -/ protected def some {α} : α ↪ option α := ⟨some, option.some_injective α⟩ /-- Embedding of a `subtype`. -/ def subtype {α} (p : α → Prop) : subtype p ↪ α := ⟨coe, λ _ _, subtype.ext_val⟩ @[simp] lemma coe_subtype {α} (p : α → Prop) : ⇑(subtype p) = coe := rfl /-- Choosing an element `b : β` gives an embedding of `punit` into `β`. -/ def punit {β : Sort*} (b : β) : punit ↪ β := ⟨λ _, b, by { rintros ⟨⟩ ⟨⟩ _, refl, }⟩ /-- Fixing an element `b : β` gives an embedding `α ↪ α × β`. -/ def sectl (α : Sort*) {β : Sort*} (b : β) : α ↪ α × β := ⟨λ a, (a, b), λ a a' h, congr_arg prod.fst h⟩ /-- Fixing an element `a : α` gives an embedding `β ↪ α × β`. -/ def sectr {α : Sort*} (a : α) (β : Sort*): β ↪ α × β := ⟨λ b, (a, b), λ b b' h, congr_arg prod.snd h⟩ /-- Restrict the codomain of an embedding. -/ def cod_restrict {α β} (p : set β) (f : α ↪ β) (H : ∀ a, f a ∈ p) : α ↪ p := ⟨λ a, ⟨f a, H a⟩, λ a b h, f.injective (@congr_arg _ _ _ _ subtype.val h)⟩ @[simp] theorem cod_restrict_apply {α β} (p) (f : α ↪ β) (H a) : cod_restrict p f H a = ⟨f a, H a⟩ := rfl /-- If `e₁` and `e₂` are embeddings, then so is `prod.map e₁ e₂ : (a, b) ↦ (e₁ a, e₂ b)`. -/ def prod_map {α β γ δ : Type*} (e₁ : α ↪ β) (e₂ : γ ↪ δ) : α × γ ↪ β × δ := ⟨prod.map e₁ e₂, e₁.injective.prod_map e₂.injective⟩ @[simp] lemma coe_prod_map {α β γ δ : Type*} (e₁ : α ↪ β) (e₂ : γ ↪ δ) : ⇑(e₁.prod_map e₂) = prod.map e₁ e₂ := rfl section sum open sum /-- If `e₁` and `e₂` are embeddings, then so is `sum.map e₁ e₂`. -/ def sum_map {α β γ δ : Type*} (e₁ : α ↪ β) (e₂ : γ ↪ δ) : α ⊕ γ ↪ β ⊕ δ := ⟨sum.map e₁ e₂, assume s₁ s₂ h, match s₁, s₂, h with | inl a₁, inl a₂, h := congr_arg inl $ e₁.injective $ inl.inj h | inr b₁, inr b₂, h := congr_arg inr $ e₂.injective $ inr.inj h end⟩ @[simp] theorem coe_sum_map {α β γ δ} (e₁ : α ↪ β) (e₂ : γ ↪ δ) : ⇑(sum_map e₁ e₂) = sum.map e₁ e₂ := rfl /-- The embedding of `α` into the sum `α ⊕ β`. -/ @[simps] def inl {α β : Type*} : α ↪ α ⊕ β := ⟨sum.inl, λ a b, sum.inl.inj⟩ /-- The embedding of `β` into the sum `α ⊕ β`. -/ @[simps] def inr {α β : Type*} : β ↪ α ⊕ β := ⟨sum.inr, λ a b, sum.inr.inj⟩ end sum section sigma variables {α α' : Type*} {β : α → Type*} {β' : α' → Type*} /-- `sigma.mk` as an `function.embedding`. -/ @[simps apply] def sigma_mk (a : α) : β a ↪ Σ x, β x := ⟨sigma.mk a, sigma_mk_injective⟩ /-- If `f : α ↪ α'` is an embedding and `g : Π a, β α ↪ β' (f α)` is a family of embeddings, then `sigma.map f g` is an embedding. -/ @[simps apply] def sigma_map (f : α ↪ α') (g : Π a, β a ↪ β' (f a)) : (Σ a, β a) ↪ Σ a', β' a' := ⟨sigma.map f (λ a, g a), f.injective.sigma_map (λ a, (g a).injective)⟩ end sigma def Pi_congr_right {α : Sort*} {β γ : α → Sort*} (e : ∀ a, β a ↪ γ a) : (Π a, β a) ↪ (Π a, γ a) := ⟨λf a, e a (f a), λ f₁ f₂ h, funext $ λ a, (e a).injective (congr_fun h a)⟩ def arrow_congr_left {α : Sort u} {β : Sort v} {γ : Sort w} (e : α ↪ β) : (γ → α) ↪ (γ → β) := Pi_congr_right (λ _, e) noncomputable def arrow_congr_right {α : Sort u} {β : Sort v} {γ : Sort w} [inhabited γ] (e : α ↪ β) : (α → γ) ↪ (β → γ) := by haveI := classical.prop_decidable; exact let f' : (α → γ) → (β → γ) := λf b, if h : ∃c, e c = b then f (classical.some h) else default γ in ⟨f', assume f₁ f₂ h, funext $ assume c, have ∃c', e c' = e c, from ⟨c, rfl⟩, have eq' : f' f₁ (e c) = f' f₂ (e c), from congr_fun h _, have eq_b : classical.some this = c, from e.injective $ classical.some_spec this, by simp [f', this, if_pos, eq_b] at eq'; assumption⟩ protected def subtype_map {α β} {p : α → Prop} {q : β → Prop} (f : α ↪ β) (h : ∀{{x}}, p x → q (f x)) : {x : α // p x} ↪ {y : β // q y} := ⟨subtype.map f h, subtype.map_injective h f.2⟩ open set /-- `set.image` as an embedding `set α ↪ set β`. -/ @[simps apply] protected def image {α β} (f : α ↪ β) : set α ↪ set β := ⟨image f, f.2.image_injective⟩ lemma swap_apply {α β : Type*} [decidable_eq α] [decidable_eq β] (f : α ↪ β) (x y z : α) : equiv.swap (f x) (f y) (f z) = f (equiv.swap x y z) := f.injective.swap_apply x y z lemma swap_comp {α β : Type*} [decidable_eq α] [decidable_eq β] (f : α ↪ β) (x y : α) : equiv.swap (f x) (f y) ∘ f = f ∘ equiv.swap x y := f.injective.swap_comp x y end embedding end function namespace equiv open function.embedding /-- The type of embeddings `α ↪ β` is equivalent to the subtype of all injective functions `α → β`. -/ def subtype_injective_equiv_embedding (α β : Sort*) : {f : α → β // function.injective f} ≃ (α ↪ β) := { to_fun := λ f, ⟨f.val, f.property⟩, inv_fun := λ f, ⟨f, f.injective⟩, left_inv := λ f, by simp, right_inv := λ f, by {ext, refl} } /-- If `α₁ ≃ α₂` and `β₁ ≃ β₂`, then the type of embeddings `α₁ ↪ β₁` is equivalent to the type of embeddings `α₂ ↪ β₂`. -/ @[congr, simps apply] def embedding_congr {α β γ δ : Sort*} (h : α ≃ β) (h' : γ ≃ δ) : (α ↪ γ) ≃ (β ↪ δ) := { to_fun := λ f, h.symm.to_embedding.trans $ f.trans $ h'.to_embedding, inv_fun := λ f, h.to_embedding.trans $ f.trans $ h'.symm.to_embedding, left_inv := λ x, by {ext, simp}, right_inv := λ x, by {ext, simp} } @[simp] lemma embedding_congr_refl {α β : Sort*} : embedding_congr (equiv.refl α) (equiv.refl β) = equiv.refl (α ↪ β) := by {ext, refl} @[simp] lemma embedding_congr_trans {α₁ β₁ α₂ β₂ α₃ β₃ : Sort*} (e₁ : α₁ ≃ α₂) (e₁' : β₁ ≃ β₂) (e₂ : α₂ ≃ α₃) (e₂' : β₂ ≃ β₃) : embedding_congr (e₁.trans e₂) (e₁'.trans e₂') = (embedding_congr e₁ e₁').trans (embedding_congr e₂ e₂') := rfl @[simp] lemma embedding_congr_symm {α₁ β₁ α₂ β₂ : Sort*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂) : (embedding_congr e₁ e₂).symm = embedding_congr e₁.symm e₂.symm := rfl lemma embedding_congr_apply_trans {α₁ β₁ γ₁ α₂ β₂ γ₂ : Sort*} (ea : α₁ ≃ α₂) (eb : β₁ ≃ β₂) (ec : γ₁ ≃ γ₂) (f : α₁ ↪ β₁) (g : β₁ ↪ γ₁) : equiv.embedding_congr ea ec (f.trans g) = (equiv.embedding_congr ea eb f).trans (equiv.embedding_congr eb ec g) := by {ext, simp} @[simp] lemma refl_to_embedding {α : Type*} : (equiv.refl α).to_embedding = function.embedding.refl α := rfl @[simp] lemma trans_to_embedding {α β γ : Type*} (e : α ≃ β) (f : β ≃ γ) : (e.trans f).to_embedding = e.to_embedding.trans f.to_embedding := rfl end equiv namespace set /-- The injection map is an embedding between subsets. -/ @[simps apply] def embedding_of_subset {α} (s t : set α) (h : s ⊆ t) : s ↪ t := ⟨λ x, ⟨x.1, h x.2⟩, λ ⟨x, hx⟩ ⟨y, hy⟩ h, by { congr, injection h }⟩ end set section subtype variable {α : Type*} /-- A subtype `{x // p x ∨ q x}` over a disjunction of `p q : α → Prop` can be injectively split into a sum of subtypes `{x // p x} ⊕ {x // q x}` such that `¬ p x` is sent to the right. -/ def subtype_or_left_embedding (p q : α → Prop) [decidable_pred p] : {x // p x ∨ q x} ↪ {x // p x} ⊕ {x // q x} := ⟨λ x, if h : p x then sum.inl ⟨x, h⟩ else sum.inr ⟨x, x.prop.resolve_left h⟩, begin intros x y, dsimp only, split_ifs; simp [subtype.ext_iff] end⟩ lemma subtype_or_left_embedding_apply_left {p q : α → Prop} [decidable_pred p] (x : {x // p x ∨ q x}) (hx : p x) : subtype_or_left_embedding p q x = sum.inl ⟨x, hx⟩ := dif_pos hx lemma subtype_or_left_embedding_apply_right {p q : α → Prop} [decidable_pred p] (x : {x // p x ∨ q x}) (hx : ¬ p x) : subtype_or_left_embedding p q x = sum.inr ⟨x, x.prop.resolve_left hx⟩ := dif_neg hx /-- A subtype `{x // p x}` can be injectively sent to into a subtype `{x // q x}`, if `p x → q x` for all `x : α`. -/ @[simps] def subtype.imp_embedding (p q : α → Prop) (h : p ≤ q) : {x // p x} ↪ {x // q x} := ⟨λ x, ⟨x, h x x.prop⟩, λ x y, by simp [subtype.ext_iff]⟩ /-- A subtype `{x // p x ∨ q x}` over a disjunction of `p q : α → Prop` is equivalent to a sum of subtypes `{x // p x} ⊕ {x // q x}` such that `¬ p x` is sent to the right, when `disjoint p q`. See also `equiv.sum_compl`, for when `is_compl p q`. -/ @[simps apply] def subtype_or_equiv (p q : α → Prop) [decidable_pred p] (h : disjoint p q) : {x // p x ∨ q x} ≃ {x // p x} ⊕ {x // q x} := { to_fun := subtype_or_left_embedding p q, inv_fun := sum.elim (subtype.imp_embedding _ _ (λ x hx, (or.inl hx : p x ∨ q x))) (subtype.imp_embedding _ _ (λ x hx, (or.inr hx : p x ∨ q x))), left_inv := λ x, begin by_cases hx : p x, { rw subtype_or_left_embedding_apply_left _ hx, simp [subtype.ext_iff] }, { rw subtype_or_left_embedding_apply_right _ hx, simp [subtype.ext_iff] }, end, right_inv := λ x, begin cases x, { simp only [sum.elim_inl], rw subtype_or_left_embedding_apply_left, { simp }, { simpa using x.prop } }, { simp only [sum.elim_inr], rw subtype_or_left_embedding_apply_right, { simp }, { suffices : ¬ p x, { simpa }, intro hp, simpa using h x ⟨hp, x.prop⟩ } } end } @[simp] lemma subtype_or_equiv_symm_inl (p q : α → Prop) [decidable_pred p] (h : disjoint p q) (x : {x // p x}) : (subtype_or_equiv p q h).symm (sum.inl x) = ⟨x, or.inl x.prop⟩ := rfl @[simp] lemma subtype_or_equiv_symm_inr (p q : α → Prop) [decidable_pred p] (h : disjoint p q) (x : {x // q x}) : (subtype_or_equiv p q h).symm (sum.inr x) = ⟨x, or.inr x.prop⟩ := rfl end subtype
ca3c3826edc77a99e4211fa321630b305a154e66
64874bd1010548c7f5a6e3e8902efa63baaff785
/library/data/int/basic.lean
1ed9d592813caa9954ecbbe6df4441d57bff8c89
[ "Apache-2.0" ]
permissive
tjiaqi/lean
4634d729795c164664d10d093f3545287c76628f
d0ce4cf62f4246b0600c07e074d86e51f2195e30
refs/heads/master
1,622,323,796,480
1,422,643,069,000
1,422,643,069,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
33,211
lean
/- Copyright (c) 2014 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Module: int.basic Authors: Floris van Doorn, Jeremy Avigad The integers, with addition, multiplication, and subtraction. The representation of the integers is chosen to compute efficiently. To faciliate proving things about these operations, we show that the integers are a quotient of ℕ × ℕ with the usual equivalence relation, ≡, and functions abstr : ℕ × ℕ → ℤ repr : ℤ → ℕ × ℕ satisfying: abstr_repr (a : ℤ) : abstr (repr a) = a repr_abstr (p : ℕ × ℕ) : repr (abstr p) ≡ p abstr_eq (p q : ℕ × ℕ) : p ≡ q → abstr p = abstr q For example, to "lift" statements about add to statements about padd, we need to prove the following: repr_add (a b : ℤ) : repr (a + b) = padd (repr a) (repr b) padd_congr (p p' q q' : ℕ × ℕ) (H1 : p ≡ p') (H2 : q ≡ q') : padd p q ≡ p' q' -/ import data.nat.basic data.nat.order data.nat.sub data.prod import algebra.relation algebra.binary algebra.ordered_ring import tools.fake_simplifier open eq.ops open prod relation nat open decidable binary fake_simplifier /- the type of integers -/ inductive int : Type := of_nat : nat → int, neg_succ_of_nat : nat → int notation `ℤ` := int attribute int.of_nat [coercion] definition int.of_num [coercion] [reducible] (n : num) : ℤ := int.of_nat (nat.of_num n) namespace int /- definitions of basic functions -/ definition neg_of_nat (m : ℕ) : ℤ := nat.cases_on m 0 (take m', neg_succ_of_nat m') definition sub_nat_nat (m n : ℕ) : ℤ := nat.cases_on (n - m) (of_nat (m - n)) -- m ≥ n (take k, neg_succ_of_nat k) -- m < n, and n - m = succ k definition neg (a : ℤ) : ℤ := cases_on a (take m, -- a = of_nat m nat.cases_on m 0 (take m', neg_succ_of_nat m')) (take m, of_nat (succ m)) -- a = neg_succ_of_nat m definition add (a b : ℤ) : ℤ := cases_on a (take m, -- a = of_nat m cases_on b (take n, of_nat (m + n)) -- b = of_nat n (take n, sub_nat_nat m (succ n))) -- b = neg_succ_of_nat n (take m, -- a = neg_succ_of_nat m cases_on b (take n, sub_nat_nat n (succ m)) -- b = of_nat n (take n, neg_of_nat (succ m + succ n))) -- b = neg_succ_of_nat n definition mul (a b : ℤ) : ℤ := cases_on a (take m, -- a = of_nat m cases_on b (take n, of_nat (m * n)) -- b = of_nat n (take n, neg_of_nat (m * succ n))) -- b = neg_succ_of_nat n (take m, -- a = neg_succ_of_nat m cases_on b (take n, neg_of_nat (succ m * n)) -- b = of_nat n (take n, of_nat (succ m * succ n))) -- b = neg_succ_of_nat n /- notation -/ notation `-[` n `+1]` := int.neg_succ_of_nat n -- for pretty-printing output prefix - := int.neg infix + := int.add infix * := int.mul /- some basic functions and properties -/ theorem of_nat_inj {m n : ℕ} (H : of_nat m = of_nat n) : m = n := no_confusion H (λe, e) theorem neg_succ_of_nat_inj {m n : ℕ} (H : neg_succ_of_nat m = neg_succ_of_nat n) : m = n := no_confusion H (λe, e) definition has_decidable_eq [instance] : decidable_eq ℤ := take a b, cases_on a (take m, cases_on b (take n, if H : m = n then inl (congr_arg of_nat H) else inr (take H1, H (of_nat_inj H1))) (take n', inr (assume H, no_confusion H))) (take m', cases_on b (take n, inr (assume H, no_confusion H)) (take n', (if H : m' = n' then inl (congr_arg neg_succ_of_nat H) else inr (take H1, H (neg_succ_of_nat_inj H1))))) theorem add_of_nat (n m : nat) : of_nat n + of_nat m = #nat n + m := rfl theorem of_nat_succ (n : ℕ) : of_nat (succ n) = of_nat n + 1 := rfl theorem mul_of_nat (n m : ℕ) : of_nat n * of_nat m = n * m := rfl theorem sub_nat_nat_of_ge {m n : ℕ} (H : m ≥ n) : sub_nat_nat m n = of_nat (m - n) := have H1 : n - m = 0, from sub_eq_zero_of_le H, calc sub_nat_nat m n = nat.cases_on 0 (of_nat (m - n)) _ : H1 ▸ rfl ... = of_nat (m - n) : rfl context attribute sub_nat_nat [reducible] theorem sub_nat_nat_of_lt {m n : ℕ} (H : m < n) : sub_nat_nat m n = neg_succ_of_nat (pred (n - m)) := have H1 : n - m = succ (pred (n - m)), from (succ_pred_of_pos (sub_pos_of_lt H))⁻¹, calc sub_nat_nat m n = nat.cases_on (succ (pred (n - m))) (of_nat (m - n)) (take k, neg_succ_of_nat k) : H1 ▸ rfl ... = neg_succ_of_nat (pred (n - m)) : rfl end definition nat_abs (a : ℤ) : ℕ := cases_on a (take n, n) (take n', succ n') theorem nat_abs_of_nat (n : ℕ) : nat_abs (of_nat n) = n := rfl theorem nat_abs_eq_zero {a : ℤ} : nat_abs a = 0 → a = 0 := cases_on a (take m, assume H : nat_abs (of_nat m) = 0, congr_arg of_nat H) (take m', assume H : nat_abs (neg_succ_of_nat m') = 0, absurd H (succ_ne_zero _)) /- int is a quotient of ordered pairs of natural numbers -/ definition equiv (p q : ℕ × ℕ) : Prop := pr1 p + pr2 q = pr2 p + pr1 q local notation p `≡` q := equiv p q theorem equiv.refl {p : ℕ × ℕ} : p ≡ p := !add.comm theorem equiv.symm {p q : ℕ × ℕ} (H : p ≡ q) : q ≡ p := calc pr1 q + pr2 p = pr2 p + pr1 q : !add.comm ... = pr1 p + pr2 q : H⁻¹ ... = pr2 q + pr1 p : !add.comm theorem equiv.trans {p q r : ℕ × ℕ} (H1 : p ≡ q) (H2 : q ≡ r) : p ≡ r := have H3 : pr1 p + pr2 r + pr2 q = pr2 p + pr1 r + pr2 q, from calc pr1 p + pr2 r + pr2 q = pr1 p + pr2 q + pr2 r : by simp ... = pr2 p + pr1 q + pr2 r : {H1} ... = pr2 p + (pr1 q + pr2 r) : by simp ... = pr2 p + (pr2 q + pr1 r) : {H2} ... = pr2 p + pr1 r + pr2 q : by simp, show pr1 p + pr2 r = pr2 p + pr1 r, from add.cancel_right H3 theorem equiv_equiv : is_equivalence equiv := is_equivalence.mk @equiv.refl @equiv.symm @equiv.trans theorem equiv_cases {p q : ℕ × ℕ} (H : equiv p q) : (pr1 p ≥ pr2 p ∧ pr1 q ≥ pr2 q) ∨ (pr1 p < pr2 p ∧ pr1 q < pr2 q) := or.elim (@le_or_gt (pr2 p) (pr1 p)) (assume H1: pr1 p ≥ pr2 p, have H2 : pr2 p + pr1 q ≥ pr2 p + pr2 q, from H ▸ add_le_add_right H1 (pr2 q), or.inl (and.intro H1 (le_of_add_le_add_left H2))) (assume H1: pr1 p < pr2 p, have H2 : pr2 p + pr1 q < pr2 p + pr2 q, from H ▸ add_lt_add_right H1 (pr2 q), or.inr (and.intro H1 (lt_of_add_lt_add_left H2))) theorem equiv_of_eq {p q : ℕ × ℕ} (H : p = q) : p ≡ q := H ▸ equiv.refl theorem equiv_of_eq_of_equiv {p q r : ℕ × ℕ} (H1 : p = q) (H2 : q ≡ r) : p ≡ r := H1⁻¹ ▸ H2 theorem equiv_of_equiv_of_eq {p q r : ℕ × ℕ} (H1 : p ≡ q) (H2 : q = r) : p ≡ r := H2 ▸ H1 calc_trans equiv.trans calc_refl equiv.refl calc_symm equiv.symm calc_trans equiv_of_eq_of_equiv calc_trans equiv_of_equiv_of_eq /- the representation and abstraction functions -/ definition abstr (a : ℕ × ℕ) : ℤ := sub_nat_nat (pr1 a) (pr2 a) theorem abstr_of_ge {p : ℕ × ℕ} (H : pr1 p ≥ pr2 p) : abstr p = of_nat (pr1 p - pr2 p) := sub_nat_nat_of_ge H theorem abstr_of_lt {p : ℕ × ℕ} (H : pr1 p < pr2 p) : abstr p = neg_succ_of_nat (pred (pr2 p - pr1 p)) := sub_nat_nat_of_lt H definition repr (a : ℤ) : ℕ × ℕ := cases_on a (take m, (m, 0)) (take m, (0, succ m)) theorem abstr_repr (a : ℤ) : abstr (repr a) = a := cases_on a (take m, (sub_nat_nat_of_ge (zero_le m))) (take m, rfl) theorem repr_sub_nat_nat (m n : ℕ) : repr (sub_nat_nat m n) ≡ (m, n) := or.elim (@le_or_gt n m) (take H : m ≥ n, have H1 : repr (sub_nat_nat m n) = (m - n, 0), from sub_nat_nat_of_ge H ▸ rfl, H1⁻¹ ▸ (calc m - n + n = m : sub_add_cancel H ... = 0 + m : zero_add)) (take H : m < n, have H1 : repr (sub_nat_nat m n) = (0, succ (pred (n - m))), from sub_nat_nat_of_lt H ▸ rfl, H1⁻¹ ▸ (calc 0 + n = n : zero_add ... = n - m + m : sub_add_cancel (le_of_lt H) ... = succ (pred (n - m)) + m : (succ_pred_of_pos (sub_pos_of_lt H))⁻¹)) theorem repr_abstr (p : ℕ × ℕ) : repr (abstr p) ≡ p := !prod.eta ▸ !repr_sub_nat_nat theorem abstr_eq {p q : ℕ × ℕ} (Hequiv : p ≡ q) : abstr p = abstr q := or.elim (equiv_cases Hequiv) (assume H2, have H3 : pr1 p ≥ pr2 p, from and.elim_left H2, have H4 : pr1 q ≥ pr2 q, from and.elim_right H2, have H5 : pr1 p = pr1 q - pr2 q + pr2 p, from calc pr1 p = pr1 p + pr2 q - pr2 q : add_sub_cancel ... = pr2 p + pr1 q - pr2 q : Hequiv ... = pr2 p + (pr1 q - pr2 q) : add_sub_assoc H4 ... = pr1 q - pr2 q + pr2 p : add.comm, have H6 : pr1 p - pr2 p = pr1 q - pr2 q, from calc pr1 p - pr2 p = pr1 q - pr2 q + pr2 p - pr2 p : H5 ... = pr1 q - pr2 q : add_sub_cancel, abstr_of_ge H3 ⬝ congr_arg of_nat H6 ⬝ (abstr_of_ge H4)⁻¹) (assume H2, have H3 : pr1 p < pr2 p, from and.elim_left H2, have H4 : pr1 q < pr2 q, from and.elim_right H2, have H5 : pr2 p = pr2 q - pr1 q + pr1 p, from calc pr2 p = pr2 p + pr1 q - pr1 q : add_sub_cancel ... = pr1 p + pr2 q - pr1 q : Hequiv ... = pr1 p + (pr2 q - pr1 q) : add_sub_assoc (le_of_lt H4) ... = pr2 q - pr1 q + pr1 p : add.comm, have H6 : pr2 p - pr1 p = pr2 q - pr1 q, from calc pr2 p - pr1 p = pr2 q - pr1 q + pr1 p - pr1 p : H5 ... = pr2 q - pr1 q : add_sub_cancel, abstr_of_lt H3 ⬝ congr_arg neg_succ_of_nat (congr_arg pred H6)⬝ (abstr_of_lt H4)⁻¹) theorem equiv_iff (p q : ℕ × ℕ) : (p ≡ q) ↔ ((p ≡ p) ∧ (q ≡ q) ∧ (abstr p = abstr q)) := iff.intro (assume H : equiv p q, and.intro !equiv.refl (and.intro !equiv.refl (abstr_eq H))) (assume H : equiv p p ∧ equiv q q ∧ abstr p = abstr q, have H1 : abstr p = abstr q, from and.elim_right (and.elim_right H), equiv.trans (H1 ▸ equiv.symm (repr_abstr p)) (repr_abstr q)) theorem eq_abstr_of_equiv_repr {a : ℤ} {p : ℕ × ℕ} (Hequiv : repr a ≡ p) : a = abstr p := calc a = abstr (repr a) : abstr_repr ... = abstr p : abstr_eq Hequiv theorem eq_of_repr_equiv_repr {a b : ℤ} (H : repr a ≡ repr b) : a = b := calc a = abstr (repr a) : abstr_repr ... = abstr (repr b) : abstr_eq H ... = b : abstr_repr context attribute abstr [reducible] attribute dist [reducible] theorem nat_abs_abstr (p : ℕ × ℕ) : nat_abs (abstr p) = dist (pr1 p) (pr2 p) := let m := pr1 p, n := pr2 p in or.elim (@le_or_gt n m) (assume H : m ≥ n, calc nat_abs (abstr (m, n)) = nat_abs (of_nat (m - n)) : int.abstr_of_ge H ... = dist m n : dist_eq_sub_of_ge H) (assume H : m < n, calc nat_abs (abstr (m, n)) = nat_abs (neg_succ_of_nat (pred (n - m))) : int.abstr_of_lt H ... = succ (pred (n - m)) : rfl ... = n - m : succ_pred_of_pos (sub_pos_of_lt H) ... = dist m n : dist_eq_sub_of_le (le_of_lt H)) end theorem cases_of_nat (a : ℤ) : (∃n : ℕ, a = of_nat n) ∨ (∃n : ℕ, a = - of_nat n) := cases_on a (take n, or.inl (exists.intro n rfl)) (take n', or.inr (exists.intro (succ n') rfl)) theorem cases_of_nat_succ (a : ℤ) : (∃n : ℕ, a = of_nat n) ∨ (∃n : ℕ, a = - (of_nat (succ n))) := int.cases_on a (take m, or.inl (exists.intro _ rfl)) (take m, or.inr (exists.intro _ rfl)) theorem by_cases_of_nat {P : ℤ → Prop} (a : ℤ) (H1 : ∀n : ℕ, P (of_nat n)) (H2 : ∀n : ℕ, P (- of_nat n)) : P a := or.elim (cases_of_nat a) (assume H, obtain (n : ℕ) (H3 : a = n), from H, H3⁻¹ ▸ H1 n) (assume H, obtain (n : ℕ) (H3 : a = -n), from H, H3⁻¹ ▸ H2 n) theorem by_cases_of_nat_succ {P : ℤ → Prop} (a : ℤ) (H1 : ∀n : ℕ, P (of_nat n)) (H2 : ∀n : ℕ, P (- of_nat (succ n))) : P a := or.elim (cases_of_nat_succ a) (assume H, obtain (n : ℕ) (H3 : a = n), from H, H3⁻¹ ▸ H1 n) (assume H, obtain (n : ℕ) (H3 : a = -(succ n)), from H, H3⁻¹ ▸ H2 n) /- int is a ring -/ /- addition -/ definition padd (p q : ℕ × ℕ) : ℕ × ℕ := map_pair2 nat.add p q theorem repr_add (a b : ℤ) : repr (add a b) ≡ padd (repr a) (repr b) := cases_on a (take m, cases_on b (take n, !equiv.refl) (take n', have H1 : equiv (repr (add (of_nat m) (neg_succ_of_nat n'))) (m, succ n'), from !repr_sub_nat_nat, have H2 : padd (repr (of_nat m)) (repr (neg_succ_of_nat n')) = (m, 0 + succ n'), from rfl, (!zero_add ▸ H2)⁻¹ ▸ H1)) (take m', cases_on b (take n, have H1 : equiv (repr (add (neg_succ_of_nat m') (of_nat n))) (n, succ m'), from !repr_sub_nat_nat, have H2 : padd (repr (neg_succ_of_nat m')) (repr (of_nat n)) = (0 + n, succ m'), from rfl, (!zero_add ▸ H2)⁻¹ ▸ H1) (take n',!repr_sub_nat_nat)) theorem padd_congr {p p' q q' : ℕ × ℕ} (Ha : p ≡ p') (Hb : q ≡ q') : padd p q ≡ padd p' q' := calc pr1 (padd p q) + pr2 (padd p' q') = pr1 p + pr2 p' + (pr1 q + pr2 q') : by simp ... = pr2 p + pr1 p' + (pr1 q + pr2 q') : {Ha} ... = pr2 p + pr1 p' + (pr2 q + pr1 q') : {Hb} ... = pr2 (padd p q) + pr1 (padd p' q') : by simp theorem padd_comm (p q : ℕ × ℕ) : padd p q = padd q p := calc padd p q = (pr1 p + pr1 q, pr2 p + pr2 q) : rfl ... = (pr1 q + pr1 p, pr2 p + pr2 q) : add.comm ... = (pr1 q + pr1 p, pr2 q + pr2 p) : add.comm ... = padd q p : rfl theorem padd_assoc (p q r : ℕ × ℕ) : padd (padd p q) r = padd p (padd q r) := calc padd (padd p q) r = (pr1 p + pr1 q + pr1 r, pr2 p + pr2 q + pr2 r) : rfl ... = (pr1 p + (pr1 q + pr1 r), pr2 p + pr2 q + pr2 r) : add.assoc ... = (pr1 p + (pr1 q + pr1 r), pr2 p + (pr2 q + pr2 r)) : add.assoc ... = padd p (padd q r) : rfl theorem add.comm (a b : ℤ) : a + b = b + a := begin apply eq_of_repr_equiv_repr, apply equiv.trans, apply repr_add, apply equiv.symm, apply (eq.subst (padd_comm (repr b) (repr a))), apply repr_add end theorem add.assoc (a b c : ℤ) : a + b + c = a + (b + c) := have H1 [visible]: repr (a + b + c) ≡ padd (padd (repr a) (repr b)) (repr c), from equiv.trans (repr_add (a + b) c) (padd_congr !repr_add !equiv.refl), have H2 [visible]: repr (a + (b + c)) ≡ padd (repr a) (padd (repr b) (repr c)), from equiv.trans (repr_add a (b + c)) (padd_congr !equiv.refl !repr_add), begin apply eq_of_repr_equiv_repr, apply equiv.trans, apply H1, apply (eq.subst ((padd_assoc _ _ _)⁻¹)), apply equiv.symm, apply H2 end theorem add_zero (a : ℤ) : a + 0 = a := cases_on a (take m, rfl) (take m', rfl) theorem zero_add (a : ℤ) : 0 + a = a := add.comm a 0 ▸ add_zero a /- negation -/ definition pneg (p : ℕ × ℕ) : ℕ × ℕ := (pr2 p, pr1 p) -- note: this is =, not just ≡ theorem repr_neg (a : ℤ) : repr (- a) = pneg (repr a) := cases_on a (take m, nat.cases_on m rfl (take m', rfl)) (take m', rfl) theorem pneg_congr {p p' : ℕ × ℕ} (H : p ≡ p') : pneg p ≡ pneg p' := eq.symm H theorem pneg_pneg (p : ℕ × ℕ) : pneg (pneg p) = p := !prod.eta theorem nat_abs_neg (a : ℤ) : nat_abs (-a) = nat_abs a := calc nat_abs (-a) = nat_abs (abstr (repr (-a))) : abstr_repr ... = nat_abs (abstr (pneg (repr a))) : repr_neg ... = dist (pr1 (pneg (repr a))) (pr2 (pneg (repr a))) : nat_abs_abstr ... = dist (pr2 (pneg (repr a))) (pr1 (pneg (repr a))) : dist.comm ... = nat_abs (abstr (repr a)) : nat_abs_abstr ... = nat_abs a : abstr_repr theorem padd_pneg (p : ℕ × ℕ) : padd p (pneg p) ≡ (0, 0) := show pr1 p + pr2 p + 0 = pr2 p + pr1 p + 0, from !nat.add.comm ▸ rfl theorem padd_padd_pneg (p q : ℕ × ℕ) : padd (padd p q) (pneg q) ≡ p := show pr1 p + pr1 q + pr2 q + pr2 p = pr2 p + pr2 q + pr1 q + pr1 p, from by simp theorem add.left_inv (a : ℤ) : -a + a = 0 := have H : repr (-a + a) ≡ repr 0, from calc repr (-a + a) ≡ padd (repr (neg a)) (repr a) : repr_add ... = padd (pneg (repr a)) (repr a) : repr_neg ... ≡ repr 0 : padd_pneg, eq_of_repr_equiv_repr H /- nat abs -/ definition pabs (p : ℕ × ℕ) : ℕ := dist (pr1 p) (pr2 p) theorem pabs_congr {p q : ℕ × ℕ} (H : p ≡ q) : pabs p = pabs q := calc pabs p = nat_abs (abstr p) : nat_abs_abstr ... = nat_abs (abstr q) : abstr_eq H ... = pabs q : nat_abs_abstr theorem nat_abs_eq_pabs_repr (a : ℤ) : nat_abs a = pabs (repr a) := calc nat_abs a = nat_abs (abstr (repr a)) : abstr_repr ... = pabs (repr a) : nat_abs_abstr theorem nat_abs_add_le (a b : ℤ) : nat_abs (a + b) ≤ nat_abs a + nat_abs b := have H : nat_abs (a + b) = pabs (padd (repr a) (repr b)), from calc nat_abs (a + b) = pabs (repr (a + b)) : nat_abs_eq_pabs_repr ... = pabs (padd (repr a) (repr b)) : pabs_congr !repr_add, have H1 : nat_abs a = pabs (repr a), from !nat_abs_eq_pabs_repr, have H2 : nat_abs b = pabs (repr b), from !nat_abs_eq_pabs_repr, have H3 : pabs (padd (repr a) (repr b)) ≤ pabs (repr a) + pabs (repr b), from !dist_add_add_le_add_dist_dist, H⁻¹ ▸ H1⁻¹ ▸ H2⁻¹ ▸ H3 context attribute nat_abs [reducible] theorem mul_nat_abs (a b : ℤ) : nat_abs (a * b) = #nat (nat_abs a) * (nat_abs b) := cases_on a (take m, cases_on b (take n, rfl) (take n', !nat_abs_neg ▸ rfl)) (take m', cases_on b (take n, !nat_abs_neg ▸ rfl) (take n', rfl)) end /- multiplication -/ definition pmul (p q : ℕ × ℕ) : ℕ × ℕ := (pr1 p * pr1 q + pr2 p * pr2 q, pr1 p * pr2 q + pr2 p * pr1 q) theorem repr_neg_of_nat (m : ℕ) : repr (neg_of_nat m) = (0, m) := nat.cases_on m rfl (take m', rfl) -- note: we have =, not just ≡ theorem repr_mul (a b : ℤ) : repr (mul a b) = pmul (repr a) (repr b) := cases_on a (take m, cases_on b (take n, (calc pmul (repr m) (repr n) = (m * n + 0 * 0, m * 0 + 0 * n) : rfl ... = (m * n + 0 * 0, m * 0 + 0) : zero_mul)⁻¹) (take n', (calc pmul (repr m) (repr (neg_succ_of_nat n')) = (m * 0 + 0 * succ n', m * succ n' + 0 * 0) : rfl ... = (m * 0 + 0, m * succ n' + 0 * 0) : zero_mul ... = repr (mul m (neg_succ_of_nat n')) : repr_neg_of_nat)⁻¹)) (take m', cases_on b (take n, (calc pmul (repr (neg_succ_of_nat m')) (repr n) = (0 * n + succ m' * 0, 0 * 0 + succ m' * n) : rfl ... = (0 + succ m' * 0, 0 * 0 + succ m' * n) : zero_mul ... = (0 + succ m' * 0, succ m' * n) : {!nat.zero_add} ... = repr (mul (neg_succ_of_nat m') n) : repr_neg_of_nat)⁻¹) (take n', (calc pmul (repr (neg_succ_of_nat m')) (repr (neg_succ_of_nat n')) = (0 + succ m' * succ n', 0 * succ n') : rfl ... = (succ m' * succ n', 0 * succ n') : nat.zero_add ... = (succ m' * succ n', 0) : zero_mul ... = repr (mul (neg_succ_of_nat m') (neg_succ_of_nat n')) : rfl)⁻¹)) theorem equiv_mul_prep {xa ya xb yb xn yn xm ym : ℕ} (H1 : xa + yb = ya + xb) (H2 : xn + ym = yn + xm) : xa * xn + ya * yn + (xb * ym + yb * xm) = xa * yn + ya * xn + (xb * xm + yb * ym) := have H3 : xa * xn + ya * yn + (xb * ym + yb * xm) + (yb * xn + xb * yn + (xb * xn + yb * yn)) = xa * yn + ya * xn + (xb * xm + yb * ym) + (yb * xn + xb * yn + (xb * xn + yb * yn)), from calc xa * xn + ya * yn + (xb * ym + yb * xm) + (yb * xn + xb * yn + (xb * xn + yb * yn)) = xa * xn + yb * xn + (ya * yn + xb * yn) + (xb * xn + xb * ym + (yb * yn + yb * xm)) : by simp ... = (xa + yb) * xn + (ya + xb) * yn + (xb * (xn + ym) + yb * (yn + xm)) : by simp ... = (ya + xb) * xn + (xa + yb) * yn + (xb * (yn + xm) + yb * (xn + ym)) : by simp ... = ya * xn + xb * xn + (xa * yn + yb * yn) + (xb * yn + xb * xm + (yb*xn + yb*ym)) : by simp ... = xa * yn + ya * xn + (xb * xm + yb * ym) + (yb * xn + xb * yn + (xb * xn + yb * yn)) : by simp, nat.add.cancel_right H3 theorem pmul_congr {p p' q q' : ℕ × ℕ} (H1 : p ≡ p') (H2 : q ≡ q') : pmul p q ≡ pmul p' q' := equiv_mul_prep H1 H2 theorem pmul_comm (p q : ℕ × ℕ) : pmul p q = pmul q p := calc (pr1 p * pr1 q + pr2 p * pr2 q, pr1 p * pr2 q + pr2 p * pr1 q) = (pr1 q * pr1 p + pr2 p * pr2 q, pr1 p * pr2 q + pr2 p * pr1 q) : mul.comm ... = (pr1 q * pr1 p + pr2 q * pr2 p, pr1 p * pr2 q + pr2 p * pr1 q) : mul.comm ... = (pr1 q * pr1 p + pr2 q * pr2 p, pr2 q * pr1 p + pr2 p * pr1 q) : mul.comm ... = (pr1 q * pr1 p + pr2 q * pr2 p, pr2 q * pr1 p + pr1 q * pr2 p) : mul.comm ... = (pr1 q * pr1 p + pr2 q * pr2 p, pr1 q * pr2 p + pr2 q * pr1 p) : nat.add.comm theorem mul.comm (a b : ℤ) : a * b = b * a := eq_of_repr_equiv_repr ((calc repr (a * b) = pmul (repr a) (repr b) : repr_mul ... = pmul (repr b) (repr a) : pmul_comm ... = repr (b * a) : repr_mul) ▸ !equiv.refl) theorem pmul_assoc (p q r: ℕ × ℕ) : pmul (pmul p q) r = pmul p (pmul q r) := by simp theorem mul.assoc (a b c : ℤ) : (a * b) * c = a * (b * c) := eq_of_repr_equiv_repr ((calc repr (a * b * c) = pmul (repr (a * b)) (repr c) : repr_mul ... = pmul (pmul (repr a) (repr b)) (repr c) : repr_mul ... = pmul (repr a) (pmul (repr b) (repr c)) : pmul_assoc ... = pmul (repr a) (repr (b * c)) : repr_mul ... = repr (a * (b * c)) : repr_mul) ▸ !equiv.refl) theorem mul_one (a : ℤ) : a * 1 = a := eq_of_repr_equiv_repr (equiv_of_eq ((calc repr (a * 1) = pmul (repr a) (repr 1) : repr_mul ... = (pr1 (repr a), pr2 (repr a)) : by simp ... = repr a : prod.eta))) theorem one_mul (a : ℤ) : 1 * a = a := mul.comm a 1 ▸ mul_one a theorem mul.right_distrib (a b c : ℤ) : (a + b) * c = a * c + b * c := eq_of_repr_equiv_repr (calc repr ((a + b) * c) = pmul (repr (a + b)) (repr c) : repr_mul ... ≡ pmul (padd (repr a) (repr b)) (repr c) : pmul_congr !repr_add equiv.refl ... = padd (pmul (repr a) (repr c)) (pmul (repr b) (repr c)) : by simp ... = padd (repr (a * c)) (pmul (repr b) (repr c)) : {(repr_mul a c)⁻¹} ... = padd (repr (a * c)) (repr (b * c)) : repr_mul ... ≡ repr (a * c + b * c) : equiv.symm !repr_add) theorem mul.left_distrib (a b c : ℤ) : a * (b + c) = a * b + a * c := calc a * (b + c) = (b + c) * a : mul.comm a (b + c) ... = b * a + c * a : mul.right_distrib b c a ... = a * b + c * a : {mul.comm b a} ... = a * b + a * c : {mul.comm c a} theorem zero_ne_one : (typeof 0 : int) ≠ 1 := assume H : 0 = 1, show false, from succ_ne_zero 0 ((of_nat_inj H)⁻¹) theorem eq_zero_or_eq_zero_of_mul_eq_zero {a b : ℤ} (H : a * b = 0) : a = 0 ∨ b = 0 := have H2 : (nat_abs a) * (nat_abs b) = nat.zero, from calc (nat_abs a) * (nat_abs b) = (nat_abs (a * b)) : (mul_nat_abs a b)⁻¹ ... = (nat_abs 0) : {H} ... = nat.zero : nat_abs_of_nat nat.zero, have H3 : (nat_abs a) = nat.zero ∨ (nat_abs b) = nat.zero, from eq_zero_or_eq_zero_of_mul_eq_zero H2, or_of_or_of_imp_of_imp H3 (assume H : (nat_abs a) = nat.zero, nat_abs_eq_zero H) (assume H : (nat_abs b) = nat.zero, nat_abs_eq_zero H) section open [classes] algebra protected definition integral_domain [instance] [reducible] : algebra.integral_domain int := algebra.integral_domain.mk add add.assoc zero zero_add add_zero neg add.left_inv add.comm mul mul.assoc (of_num 1) one_mul mul_one mul.left_distrib mul.right_distrib zero_ne_one mul.comm @eq_zero_or_eq_zero_of_mul_eq_zero end /- instantiate ring theorems to int -/ section port_algebra theorem mul.left_comm : ∀a b c : ℤ, a * (b * c) = b * (a * c) := algebra.mul.left_comm theorem mul.right_comm : ∀a b c : ℤ, (a * b) * c = (a * c) * b := algebra.mul.right_comm theorem add.left_comm : ∀a b c : ℤ, a + (b + c) = b + (a + c) := algebra.add.left_comm theorem add.right_comm : ∀a b c : ℤ, (a + b) + c = (a + c) + b := algebra.add.right_comm theorem add.left_cancel : ∀{a b c : ℤ}, a + b = a + c → b = c := @algebra.add.left_cancel _ _ theorem add.right_cancel : ∀{a b c : ℤ}, a + b = c + b → a = c := @algebra.add.right_cancel _ _ theorem neg_add_cancel_left : ∀a b : ℤ, -a + (a + b) = b := algebra.neg_add_cancel_left theorem neg_add_cancel_right : ∀a b : ℤ, a + -b + b = a := algebra.neg_add_cancel_right theorem neg_eq_of_add_eq_zero : ∀{a b : ℤ}, a + b = 0 → -a = b := @algebra.neg_eq_of_add_eq_zero _ _ theorem neg_zero : -0 = 0 := algebra.neg_zero theorem neg_neg : ∀a : ℤ, -(-a) = a := algebra.neg_neg theorem neg.inj : ∀{a b : ℤ}, -a = -b → a = b := @algebra.neg.inj _ _ theorem neg_eq_neg_iff_eq : ∀a b : ℤ, -a = -b ↔ a = b := algebra.neg_eq_neg_iff_eq theorem neg_eq_zero_iff_eq_zero : ∀a : ℤ, -a = 0 ↔ a = 0 := algebra.neg_eq_zero_iff_eq_zero theorem eq_neg_of_eq_neg : ∀{a b : ℤ}, a = -b → b = -a := @algebra.eq_neg_of_eq_neg _ _ theorem eq_neg_iff_eq_neg : ∀{a b : ℤ}, a = -b ↔ b = -a := @algebra.eq_neg_iff_eq_neg _ _ theorem add.right_inv : ∀a : ℤ, a + -a = 0 := algebra.add.right_inv theorem add_neg_cancel_left : ∀a b : ℤ, a + (-a + b) = b := algebra.add_neg_cancel_left theorem add_neg_cancel_right : ∀a b : ℤ, a + b + -b = a := algebra.add_neg_cancel_right theorem neg_add_rev : ∀a b : ℤ, -(a + b) = -b + -a := algebra.neg_add_rev theorem eq_add_neg_of_add_eq : ∀{a b c : ℤ}, a + b = c → a = c + -b := @algebra.eq_add_neg_of_add_eq _ _ theorem eq_neg_add_of_add_eq : ∀{a b c : ℤ}, a + b = c → b = -a + c := @algebra.eq_neg_add_of_add_eq _ _ theorem neg_add_eq_of_eq_add : ∀{a b c : ℤ}, a = b + c → -b + a = c := @algebra.neg_add_eq_of_eq_add _ _ theorem add_neg_eq_of_eq_add : ∀{a b c : ℤ}, a = b + c → a + -c = b := @algebra.add_neg_eq_of_eq_add _ _ theorem eq_add_of_add_neg_eq : ∀{a b c : ℤ}, a + -b = c → a = c + b := @algebra.eq_add_of_add_neg_eq _ _ theorem eq_add_of_neg_add_eq : ∀{a b c : ℤ}, -a + b = c → b = a + c := @algebra.eq_add_of_neg_add_eq _ _ theorem add_eq_of_eq_neg_add : ∀{a b c : ℤ}, a = -b + c → b + a = c := @algebra.add_eq_of_eq_neg_add _ _ theorem add_eq_of_eq_add_neg : ∀{a b c : ℤ}, a = b + -c → a + c = b := @algebra.add_eq_of_eq_add_neg _ _ theorem add_eq_iff_eq_neg_add : ∀a b c : ℤ, a + b = c ↔ b = -a + c := @algebra.add_eq_iff_eq_neg_add _ _ theorem add_eq_iff_eq_add_neg : ∀a b c : ℤ, a + b = c ↔ a = c + -b := @algebra.add_eq_iff_eq_add_neg _ _ definition sub (a b : ℤ) : ℤ := algebra.sub a b infix - := int.sub theorem sub_self : ∀a : ℤ, a - a = 0 := algebra.sub_self theorem sub_add_cancel : ∀a b : ℤ, a - b + b = a := algebra.sub_add_cancel theorem add_sub_cancel : ∀a b : ℤ, a + b - b = a := algebra.add_sub_cancel theorem eq_of_sub_eq_zero : ∀{a b : ℤ}, a - b = 0 → a = b := @algebra.eq_of_sub_eq_zero _ _ theorem eq_iff_sub_eq_zero : ∀a b : ℤ, a = b ↔ a - b = 0 := algebra.eq_iff_sub_eq_zero theorem zero_sub : ∀a : ℤ, 0 - a = -a := algebra.zero_sub theorem sub_zero : ∀a : ℤ, a - 0 = a := algebra.sub_zero theorem sub_neg_eq_add : ∀a b : ℤ, a - (-b) = a + b := algebra.sub_neg_eq_add theorem neg_sub : ∀a b : ℤ, -(a - b) = b - a := algebra.neg_sub theorem add_sub : ∀a b c : ℤ, a + (b - c) = a + b - c := algebra.add_sub theorem sub_add_eq_sub_sub_swap : ∀a b c : ℤ, a - (b + c) = a - c - b := algebra.sub_add_eq_sub_sub_swap theorem sub_eq_iff_eq_add : ∀a b c : ℤ, a - b = c ↔ a = c + b := algebra.sub_eq_iff_eq_add theorem eq_sub_iff_add_eq : ∀a b c : ℤ, a = b - c ↔ a + c = b := algebra.eq_sub_iff_add_eq theorem eq_iff_eq_of_sub_eq_sub : ∀{a b c d : ℤ}, a - b = c - d → a = b ↔ c = d := @algebra.eq_iff_eq_of_sub_eq_sub _ _ theorem sub_add_eq_sub_sub : ∀a b c : ℤ, a - (b + c) = a - b - c := algebra.sub_add_eq_sub_sub theorem neg_add_eq_sub : ∀a b : ℤ, -a + b = b - a := algebra.neg_add_eq_sub theorem neg_add : ∀a b : ℤ, -(a + b) = -a + -b := algebra.neg_add theorem sub_add_eq_add_sub : ∀a b c : ℤ, a - b + c = a + c - b := algebra.sub_add_eq_add_sub theorem sub_sub_ : ∀a b c : ℤ, a - b - c = a - (b + c) := algebra.sub_sub theorem add_sub_add_left_eq_sub : ∀a b c : ℤ, (c + a) - (c + b) = a - b := algebra.add_sub_add_left_eq_sub theorem ne_zero_of_mul_ne_zero_right : ∀{a b : ℤ}, a * b ≠ 0 → a ≠ 0 := @algebra.ne_zero_of_mul_ne_zero_right _ _ theorem ne_zero_of_mul_ne_zero_left : ∀{a b : ℤ}, a * b ≠ 0 → b ≠ 0 := @algebra.ne_zero_of_mul_ne_zero_left _ _ definition dvd (a b : ℤ) : Prop := algebra.dvd a b infix `|` := dvd theorem dvd.intro : ∀{a b c : ℤ} (H : a * b = c), a | c := @algebra.dvd.intro _ _ theorem dvd.intro_right : ∀{a b c : ℤ} (H : a * b = c), b | c := @algebra.dvd.intro_right _ _ theorem dvd.ex : ∀{a b : ℤ} (H : a | b), ∃c, a * c = b := @algebra.dvd.ex _ _ theorem dvd.elim : ∀{P : Prop} {a b : ℤ} (H₁ : a | b) (H₂ : ∀c, a * c = b → P), P := @algebra.dvd.elim _ _ theorem dvd.refl : ∀a : ℤ, a | a := algebra.dvd.refl theorem dvd.trans : ∀{a b c : ℤ} (H₁ : a | b) (H₂ : b | c), a | c := @algebra.dvd.trans _ _ theorem eq_zero_of_zero_dvd : ∀{a : ℤ} (H : 0 | a), a = 0 := @algebra.eq_zero_of_zero_dvd _ _ theorem dvd_zero : ∀a : ℤ, a | 0 := algebra.dvd_zero theorem one_dvd : ∀a : ℤ, 1 | a := algebra.one_dvd theorem dvd_mul_right : ∀a b : ℤ, a | a * b := algebra.dvd_mul_right theorem dvd_mul_left : ∀a b : ℤ, a | b * a := algebra.dvd_mul_left theorem dvd_mul_of_dvd_left : ∀{a b : ℤ} (H : a | b) (c : ℤ), a | b * c := @algebra.dvd_mul_of_dvd_left _ _ theorem dvd_mul_of_dvd_right : ∀{a b : ℤ} (H : a | b) (c : ℤ), a | c * b := @algebra.dvd_mul_of_dvd_right _ _ theorem mul_dvd_mul : ∀{a b c d : ℤ}, a | b → c | d → a * c | b * d := @algebra.mul_dvd_mul _ _ theorem dvd_of_mul_right_dvd : ∀{a b c : ℤ}, a * b | c → a | c := @algebra.dvd_of_mul_right_dvd _ _ theorem dvd_of_mul_left_dvd : ∀{a b c : ℤ}, a * b | c → b | c := @algebra.dvd_of_mul_left_dvd _ _ theorem dvd_add : ∀{a b c : ℤ}, a | b → a | c → a | b + c := @algebra.dvd_add _ _ theorem zero_mul : ∀a : ℤ, 0 * a = 0 := algebra.zero_mul theorem mul_zero : ∀a : ℤ, a * 0 = 0 := algebra.mul_zero theorem neg_mul_eq_neg_mul : ∀a b : ℤ, -(a * b) = -a * b := algebra.neg_mul_eq_neg_mul theorem neg_mul_eq_mul_neg : ∀a b : ℤ, -(a * b) = a * -b := algebra.neg_mul_eq_mul_neg theorem neg_mul_neg : ∀a b : ℤ, -a * -b = a * b := algebra.neg_mul_neg theorem neg_mul_comm : ∀a b : ℤ, -a * b = a * -b := algebra.neg_mul_comm theorem neg_eq_neg_one_mul : ∀a : ℤ, -a = -1 * a := algebra.neg_eq_neg_one_mul theorem mul_sub_left_distrib : ∀a b c : ℤ, a * (b - c) = a * b - a * c := algebra.mul_sub_left_distrib theorem mul_sub_right_distrib : ∀a b c : ℤ, (a - b) * c = a * c - b * c := algebra.mul_sub_right_distrib theorem mul_add_eq_mul_add_iff_sub_mul_add_eq : ∀a b c d e : ℤ, a * e + c = b * e + d ↔ (a - b) * e + c = d := algebra.mul_add_eq_mul_add_iff_sub_mul_add_eq theorem mul_self_sub_mul_self_eq : ∀a b : ℤ, a * a - b * b = (a + b) * (a - b) := algebra.mul_self_sub_mul_self_eq theorem mul_self_sub_one_eq : ∀a : ℤ, a * a - 1 = (a + 1) * (a - 1) := algebra.mul_self_sub_one_eq theorem dvd_neg_iff_dvd : ∀a b : ℤ, a | -b ↔ a | b := algebra.dvd_neg_iff_dvd theorem neg_dvd_iff_dvd : ∀a b : ℤ, -a | b ↔ a | b := algebra.neg_dvd_iff_dvd theorem dvd_sub : ∀a b c : ℤ, a | b → a | c → a | (b - c) := algebra.dvd_sub theorem mul_ne_zero : ∀{a b : ℤ}, a ≠ 0 → b ≠ 0 → a * b ≠ 0 := @algebra.mul_ne_zero _ _ theorem mul.cancel_right : ∀{a b c : ℤ}, a ≠ 0 → b * a = c * a → b = c := @algebra.mul.cancel_right _ _ theorem mul.cancel_left : ∀{a b c : ℤ}, a ≠ 0 → a * b = a * c → b = c := @algebra.mul.cancel_left _ _ theorem mul_self_eq_mul_self_iff : ∀a b : ℤ, a * a = b * b ↔ a = b ∨ a = -b := algebra.mul_self_eq_mul_self_iff theorem mul_self_eq_one_iff : ∀a : ℤ, a * a = 1 ↔ a = 1 ∨ a = -1 := algebra.mul_self_eq_one_iff theorem dvd_of_mul_dvd_mul_left : ∀{a b c : ℤ}, a ≠ 0 → a * b | a * c → b | c := @algebra.dvd_of_mul_dvd_mul_left _ _ theorem dvd_of_mul_dvd_mul_right : ∀{a b c : ℤ}, a ≠ 0 → b * a | c * a → b | c := @algebra.dvd_of_mul_dvd_mul_right _ _ end port_algebra end int
309dd765a5d1fb66662167678cf5c7f69930a25e
4727251e0cd73359b15b664c3170e5d754078599
/src/tactic/subtype_instance.lean
e8209db65bb7764153f47e9fdaaeb572900fbe92
[ "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,438
lean
/- Copyright (c) 2018 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon Provides a `subtype_instance` tactic which builds instances for algebraic substructures (sub-groups, sub-rings...). -/ import tactic.basic open tactic expr name list namespace tactic setup_tactic_parser open tactic.interactive (get_current_field refine_struct) /-- makes the substructure axiom name from field name, by postfacing with `_mem`-/ def mk_mem_name (sub : name) : name → name | (mk_string n _) := mk_string (n ++ "_mem") sub | n := n meta def derive_field_subtype : tactic unit := do field ← get_current_field, b ← target >>= is_prop, if b then do `[simp [subtype.ext_iff_val], dsimp [set.set_coe_eq_subtype]], intros, applyc field; assumption else do s ← find_local ``(set _), `(set %%α) ← infer_type s, e ← mk_const field, expl_arity ← get_expl_arity $ e α, xs ← (iota expl_arity).mmap $ λ _, intro1, args ← xs.mmap $ λ x, mk_app `subtype.val [x], hyps ← xs.mmap $ λ x, mk_app `subtype.property [x], val ← mk_app field args, subname ← local_context >>= list.mfirst (λ h, do (expr.const n _, args) ← get_app_fn_args <$> infer_type h, is_def_eq s args.ilast reducible, return n), mem_field ← resolve_constant $ mk_mem_name subname field, val_mem ← mk_app mem_field hyps, `(coe_sort %%s) <- target >>= instantiate_mvars, tactic.refine ``(@subtype.mk _ %%s %%val %%val_mem) namespace interactive /-- builds instances for algebraic substructures Example: ```lean variables {α : Type*} [monoid α] {s : set α} class is_submonoid (s : set α) : Prop := (one_mem : (1:α) ∈ s) (mul_mem {a b} : a ∈ s → b ∈ s → a * b ∈ s) instance subtype.monoid {s : set α} [is_submonoid s] : monoid s := by subtype_instance ``` -/ meta def subtype_instance := do t ← target, let cl := t.get_app_fn.const_name, src ← find_ancestors cl t.app_arg, let inst := pexpr.mk_structure_instance { struct := cl, field_values := [], field_names := [], sources := src.map to_pexpr }, refine_struct inst ; derive_field_subtype add_tactic_doc { name := "subtype_instance", category := doc_category.tactic, decl_names := [``subtype_instance], tags := ["type class", "structures"] } end interactive end tactic
b397b2e19b7c93a7f8ddea71aecbcb54bc434bd5
75bd9c50a345718d735a7533c007cf45f9da9a83
/src/ring_theory/discrete_valuation_ring.lean
a3e9418d3c24f72bb866a53ecf8d846a64abeb19
[ "Apache-2.0" ]
permissive
jtbarker/mathlib
a1a3b1ddc16179826260578410746756ef18032c
392d3e376b44265ef2dedbd92231d3177acc1fd0
refs/heads/master
1,671,246,411,096
1,600,801,712,000
1,600,801,712,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
13,146
lean
/- Copyright (c) 2020 Kevin Buzzard. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Buzzard -/ import ring_theory.principal_ideal_domain order.conditionally_complete_lattice import ring_theory.multiplicity import ring_theory.valuation.basic import tactic /-! # Discrete valuation rings This file defines discrete valuation rings (DVRs) and develops a basic interface for them. ## Important definitions There are various definitions of a DVR in the literature; we define a DVR to be a local PID which is not a field (the first definition in Wikipedia) and prove that this is equivalent to being a PID with a unique non-zero prime ideal (the definition in Serre's book "Local Fields"). Let R be an integral domain, assumed to be a principal ideal ring and a local ring. * `discrete_valuation_ring R` : a predicate expressing that R is a DVR ### Definitions ## Implementation notes It's a theorem that an element of a DVR is a uniformizer if and only if it's irreducible. We do not hence define `uniformizer` at all, because we can use `irreducible` instead. ## Tags discrete valuation ring -/ open_locale classical universe u open ideal local_ring /-- An integral domain is a discrete valuation ring if it's a local PID which is not a field -/ class discrete_valuation_ring (R : Type u) [integral_domain R] extends is_principal_ideal_ring R, local_ring R : Prop := (not_a_field' : maximal_ideal R ≠ ⊥) namespace discrete_valuation_ring variables (R : Type u) [integral_domain R] [discrete_valuation_ring R] lemma not_a_field : maximal_ideal R ≠ ⊥ := not_a_field' variable {R} open principal_ideal_ring /-- An element of a DVR is irreducible iff it is a uniformizer, that is, generates the maximal ideal of R -/ theorem irreducible_iff_uniformizer (ϖ : R) : irreducible ϖ ↔ maximal_ideal R = ideal.span {ϖ} := ⟨λ hϖ, (eq_maximal_ideal (is_maximal_of_irreducible hϖ)).symm, begin intro h, have h2 : ¬(is_unit ϖ) := show ϖ ∈ maximal_ideal R, from h.symm ▸ submodule.mem_span_singleton_self ϖ, refine ⟨h2, _⟩, intros a b hab, by_contra h, push_neg at h, obtain ⟨ha : a ∈ maximal_ideal R, hb : b ∈ maximal_ideal R⟩ := h, rw h at ha hb, rw mem_span_singleton' at ha hb, rcases ha with ⟨a, rfl⟩, rcases hb with ⟨b, rfl⟩, rw (show a * ϖ * (b * ϖ) = ϖ * (ϖ * (a * b)), by ring) at hab, have h3 := eq_zero_of_mul_eq_self_right _ hab.symm, { apply not_a_field R, simp [h, h3] }, { intro hh, apply h2, refine is_unit_of_dvd_one ϖ _, use a * b, exact hh.symm } end⟩ variable (R) /-- Uniformisers exist in a DVR -/ theorem exists_irreducible : ∃ ϖ : R, irreducible ϖ := by {simp_rw [irreducible_iff_uniformizer], exact (is_principal_ideal_ring.principal $ maximal_ideal R).principal} /-- an integral domain is a DVR iff it's a PID with a unique non-zero prime ideal -/ theorem iff_pid_with_one_nonzero_prime (R : Type u) [integral_domain R] : discrete_valuation_ring R ↔ is_principal_ideal_ring R ∧ ∃! P : ideal R, P ≠ ⊥ ∧ is_prime P := begin split, { intro RDVR, rcases id RDVR with ⟨RPID, Rlocal, Rnotafield⟩, split, assumption, resetI, use local_ring.maximal_ideal R, split, split, { assumption }, { apply_instance } , { rintro Q ⟨hQ1, hQ2⟩, obtain ⟨q, rfl⟩ := (is_principal_ideal_ring.principal Q).1, have hq : q ≠ 0, { rintro rfl, apply hQ1, simp }, erw span_singleton_prime hq at hQ2, replace hQ2 := irreducible_of_prime hQ2, rw irreducible_iff_uniformizer at hQ2, exact hQ2.symm } }, { rintro ⟨RPID, Punique⟩, haveI : local_ring R := local_of_unique_nonzero_prime R Punique, refine {not_a_field' := _}, rcases Punique with ⟨P, ⟨hP1, hP2⟩, hP3⟩, have hPM : P ≤ maximal_ideal R := le_maximal_ideal (hP2.1), intro h, rw [h, le_bot_iff] at hPM, exact hP1 hPM } end lemma associated_of_irreducible {a b : R} (ha : irreducible a) (hb : irreducible b) : associated a b := begin rw irreducible_iff_uniformizer at ha hb, rw [←span_singleton_eq_span_singleton, ←ha, hb], end end discrete_valuation_ring namespace discrete_valuation_ring variable (R : Type*) /-- Alternative characterisation of discrete valuation rings. -/ def has_unit_mul_pow_irreducible_factorization [integral_domain R] : Prop := ∃ p : R, irreducible p ∧ ∀ {x : R}, x ≠ 0 → ∃ (n : ℕ), associated (p ^ n) x namespace has_unit_mul_pow_irreducible_factorization variables {R} [integral_domain R] (hR : has_unit_mul_pow_irreducible_factorization R) include hR lemma unique_irreducible ⦃p q : R⦄ (hp : irreducible p) (hq : irreducible q) : associated p q := begin rcases hR with ⟨ϖ, hϖ, hR⟩, suffices : ∀ {p : R} (hp : irreducible p), associated p ϖ, { apply associated.trans (this hp) (this hq).symm, }, clear hp hq p q, intros p hp, obtain ⟨n, hn⟩ := hR hp.ne_zero, have : irreducible (ϖ ^ n) := irreducible_of_associated hn.symm hp, rcases lt_trichotomy n 1 with (H|rfl|H), { obtain rfl : n = 0, { clear hn this, revert H n, exact dec_trivial }, simpa only [not_irreducible_one, pow_zero] using this, }, { simpa only [pow_one] using hn.symm, }, { obtain ⟨n, rfl⟩ : ∃ k, n = 1 + k + 1 := nat.exists_eq_add_of_lt H, rw pow_succ at this, rcases this.2 _ _ rfl with H0|H0, { exact (hϖ.not_unit H0).elim, }, { rw [add_comm, pow_succ] at H0, exact (hϖ.not_unit (is_unit_of_mul_is_unit_left H0)).elim } } end /-- Implementation detail: an integral domain in which there is a unit `p` such that every nonzero element is associated to a power of `p` is a unique factorization domain. See `discrete_valuation_ring.of_has_unit_mul_pow_irreducible_factorization`. -/ theorem ufd : unique_factorization_monoid R := let p := classical.some hR in let spec := classical.some_spec hR in unique_factorization_monoid_of_exists_prime_of_factor $ λ x hx, begin use multiset.repeat p (classical.some (spec.2 hx)), split, { intros q hq, have hpq := multiset.eq_of_mem_repeat hq, rw hpq, refine ⟨spec.1.ne_zero, spec.1.not_unit, _⟩, intros a b h, by_cases ha : a = 0, { rw ha, simp only [true_or, dvd_zero], }, by_cases hb : b = 0, { rw hb, simp only [or_true, dvd_zero], }, obtain ⟨m, u, rfl⟩ := spec.2 ha, rw [mul_assoc, mul_left_comm, is_unit.dvd_mul_left _ _ _ (is_unit_unit _)] at h, rw is_unit.dvd_mul_right (is_unit_unit _), by_cases hm : m = 0, { simp only [hm, one_mul, pow_zero] at h ⊢, right, exact h }, left, obtain ⟨m, rfl⟩ := nat.exists_eq_succ_of_ne_zero hm, apply dvd_mul_of_dvd_left (dvd_refl _) _ }, { rw [multiset.prod_repeat], exact (classical.some_spec (spec.2 hx)), } end omit hR lemma of_ufd_of_unique_irreducible [unique_factorization_monoid R] (h₁ : ∃ p : R, irreducible p) (h₂ : ∀ ⦃p q : R⦄, irreducible p → irreducible q → associated p q) : has_unit_mul_pow_irreducible_factorization R := begin obtain ⟨p, hp⟩ := h₁, refine ⟨p, hp, _⟩, intros x hx, cases wf_dvd_monoid.exists_factors x hx with fx hfx, refine ⟨fx.card, _⟩, have H := hfx.2, rw ← associates.mk_eq_mk_iff_associated at H ⊢, rw [← H, ← associates.prod_mk, associates.mk_pow, ← multiset.prod_repeat], congr' 1, symmetry, rw multiset.eq_repeat, simp only [true_and, and_imp, multiset.card_map, eq_self_iff_true, multiset.mem_map, exists_imp_distrib], rintros _ q hq rfl, rw associates.mk_eq_mk_iff_associated, apply h₂ (hfx.1 _ hq) hp, end end has_unit_mul_pow_irreducible_factorization lemma aux_pid_of_ufd_of_unique_irreducible (R : Type u) [integral_domain R] [unique_factorization_monoid R] (h₁ : ∃ p : R, irreducible p) (h₂ : ∀ ⦃p q : R⦄, irreducible p → irreducible q → associated p q) : is_principal_ideal_ring R := begin constructor, intro I, by_cases I0 : I = ⊥, { rw I0, use 0, simp only [set.singleton_zero, submodule.span_zero], }, obtain ⟨x, hxI, hx0⟩ : ∃ x ∈ I, x ≠ (0:R) := I.ne_bot_iff.mp I0, obtain ⟨p, hp, H⟩ := has_unit_mul_pow_irreducible_factorization.of_ufd_of_unique_irreducible h₁ h₂, have ex : ∃ n : ℕ, p ^ n ∈ I, { obtain ⟨n, u, rfl⟩ := H hx0, refine ⟨n, _⟩, simpa only [units.mul_inv_cancel_right] using @ideal.mul_mem_right _ _ I _ ↑u⁻¹ hxI, }, constructor, use p ^ (nat.find ex), show I = ideal.span _, apply le_antisymm, { intros r hr, by_cases hr0 : r = 0, { simp only [hr0, submodule.zero_mem], }, obtain ⟨n, u, rfl⟩ := H hr0, simp only [mem_span_singleton, is_unit_unit, is_unit.dvd_mul_right], apply pow_dvd_pow, apply nat.find_min', simpa only [units.mul_inv_cancel_right] using @ideal.mul_mem_right _ _ I _ ↑u⁻¹ hr, }, { erw submodule.span_singleton_le_iff_mem, exact nat.find_spec ex, }, end /-- A unique factorization domain with at least one irreducible element in which all irreducible elements are associated is a discrete valuation ring. -/ lemma of_ufd_of_unique_irreducible {R : Type u} [integral_domain R] [unique_factorization_monoid R] (h₁ : ∃ p : R, irreducible p) (h₂ : ∀ ⦃p q : R⦄, irreducible p → irreducible q → associated p q) : discrete_valuation_ring R := begin rw iff_pid_with_one_nonzero_prime, haveI PID : is_principal_ideal_ring R := aux_pid_of_ufd_of_unique_irreducible R h₁ h₂, obtain ⟨p, hp⟩ := h₁, refine ⟨PID, ⟨ideal.span {p}, ⟨_, _⟩, _⟩⟩, { rw submodule.ne_bot_iff, refine ⟨p, ideal.mem_span_singleton.mpr (dvd_refl p), hp.ne_zero⟩, }, { rwa [ideal.span_singleton_prime hp.ne_zero, ← unique_factorization_monoid.irreducible_iff_prime], }, { intro I, rw ← submodule.is_principal.span_singleton_generator I, rintro ⟨I0, hI⟩, apply span_singleton_eq_span_singleton.mpr, apply h₂ _ hp, erw [ne.def, span_singleton_eq_bot] at I0, rwa [unique_factorization_monoid.irreducible_iff_prime, ← ideal.span_singleton_prime I0], }, end /-- An integral domain in which there is a unit `p` such that every nonzero element is associated to a power of `p` is a discrete valuation ring. -/ lemma of_has_unit_mul_pow_irreducible_factorization {R : Type u} [integral_domain R] (hR : has_unit_mul_pow_irreducible_factorization R) : discrete_valuation_ring R := begin letI : unique_factorization_monoid R := hR.ufd, apply of_ufd_of_unique_irreducible _ hR.unique_irreducible, unfreezingI { obtain ⟨p, hp, H⟩ := hR, exact ⟨p, hp⟩, }, end section variables [integral_domain R] [discrete_valuation_ring R] variable {R} lemma associated_pow_irreducible {x : R} (hx : x ≠ 0) {ϖ : R} (hirr : irreducible ϖ) : ∃ (n : ℕ), associated x (ϖ ^ n) := begin have : wf_dvd_monoid R := is_noetherian_ring.wf_dvd_monoid, cases wf_dvd_monoid.exists_factors x hx with fx hfx, unfreezingI { use fx.card }, have H := hfx.2, rw ← associates.mk_eq_mk_iff_associated at H ⊢, rw [← H, ← associates.prod_mk, associates.mk_pow, ← multiset.prod_repeat], congr' 1, rw multiset.eq_repeat, simp only [true_and, and_imp, multiset.card_map, eq_self_iff_true, multiset.mem_map, exists_imp_distrib], rintros _ _ _ rfl, rw associates.mk_eq_mk_iff_associated, refine associated_of_irreducible _ _ hirr, apply hfx.1, assumption end open submodule.is_principal lemma ideal_eq_span_pow_irreducible {s : ideal R} (hs : s ≠ ⊥) {ϖ : R} (hirr : irreducible ϖ) : ∃ n : ℕ, s = ideal.span {ϖ ^ n} := begin have gen_ne_zero : generator s ≠ 0, { rw [ne.def, ← eq_bot_iff_generator_eq_zero], assumption }, rcases associated_pow_irreducible gen_ne_zero hirr with ⟨n, u, hnu⟩, use n, have : span _ = _ := span_singleton_generator s, rw [← this, ← hnu, span_singleton_eq_span_singleton], use u end lemma unit_mul_pow_congr_pow {p q : R} (hp : irreducible p) (hq : irreducible q) (u v : units R) (m n : ℕ) (h : ↑u * p ^ m = v * q ^ n) : m = n := begin have key : associated (multiset.repeat p m).prod (multiset.repeat q n).prod, { rw [multiset.prod_repeat, multiset.prod_repeat, associated], refine ⟨u * v⁻¹, _⟩, simp only [units.coe_mul], rw [mul_left_comm, ← mul_assoc, h, mul_right_comm, units.mul_inv, one_mul], }, have := multiset.card_eq_card_of_rel (unique_factorization_monoid.factors_unique _ _ key), { simpa only [multiset.card_repeat] }, all_goals { intros x hx, replace hx := multiset.eq_of_mem_repeat hx, unfreezingI { subst hx, assumption } }, end lemma unit_mul_pow_congr_unit {ϖ : R} (hirr : irreducible ϖ) (u v : units R) (m n : ℕ) (h : ↑u * ϖ ^ m = v * ϖ ^ n) : u = v := begin obtain rfl : m = n := unit_mul_pow_congr_pow hirr hirr u v m n h, rw ← sub_eq_zero at h, rw [← sub_mul, mul_eq_zero] at h, cases h, { rw sub_eq_zero at h, exact_mod_cast h }, { apply (hirr.ne_zero (pow_eq_zero h)).elim, } end end end discrete_valuation_ring
b2e7fb56166cb2487cd36e45410c990d0a6d2c95
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/algebra/category/Group/images.lean
ab486f0bf88af08226c40b354a317211f91e3211
[ "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,303
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import algebra.category.Group.abelian import category_theory.limits.shapes.images import category_theory.limits.types /-! # The category of commutative additive groups has images. Note that we don't need to register any of the constructions here as instances, because we get them from the fact that `AddCommGroup` is an abelian category. -/ open category_theory open category_theory.limits universe u namespace AddCommGroup -- Note that because `injective_of_mono` is currently only proved in `Type 0`, -- we restrict to the lowest universe here for now. variables {G H : AddCommGroup.{0}} (f : G ⟶ H) local attribute [ext] subtype.ext_val section -- implementation details of `has_image` for AddCommGroup; use the API, not these /-- the image of a morphism in AddCommGroup is just the bundling of `add_monoid_hom.range f` -/ def image : AddCommGroup := AddCommGroup.of (add_monoid_hom.range f) /-- the inclusion of `image f` into the target -/ def image.ι : image f ⟶ H := f.range.subtype instance : mono (image.ι f) := concrete_category.mono_of_injective (image.ι f) subtype.val_injective /-- the corestriction map to the image -/ def factor_thru_image : G ⟶ image f := f.range_restrict lemma image.fac : factor_thru_image f ≫ image.ι f = f := by { ext, refl, } local attribute [simp] image.fac variables {f} /-- the universal property for the image factorisation -/ noncomputable def image.lift (F' : mono_factorisation f) : image f ⟶ F'.I := { to_fun := (λ x, F'.e (classical.indefinite_description _ x.2).1 : image f → F'.I), map_zero' := begin haveI := F'.m_mono, apply injective_of_mono F'.m, change (F'.e ≫ F'.m) _ = _, rw [F'.fac, add_monoid_hom.map_zero], exact (classical.indefinite_description (λ y, f y = 0) _).2, end, map_add' := begin intros x y, haveI := F'.m_mono, apply injective_of_mono F'.m, rw [add_monoid_hom.map_add], change (F'.e ≫ F'.m) _ = (F'.e ≫ F'.m) _ + (F'.e ≫ F'.m) _, rw [F'.fac], rw (classical.indefinite_description (λ z, f z = _) _).2, rw (classical.indefinite_description (λ z, f z = _) _).2, rw (classical.indefinite_description (λ z, f z = _) _).2, refl, end, } lemma image.lift_fac (F' : mono_factorisation f) : image.lift F' ≫ F'.m = image.ι f := begin ext x, change (F'.e ≫ F'.m) _ = _, rw [F'.fac, (classical.indefinite_description _ x.2).2], refl, end end /-- the factorisation of any morphism in AddCommGroup through a mono. -/ def mono_factorisation : mono_factorisation f := { I := image f, m := image.ι f, e := factor_thru_image f } /-- the factorisation of any morphism in AddCommGroup through a mono has the universal property of the image. -/ noncomputable def is_image : is_image (mono_factorisation f) := { lift := image.lift, lift_fac' := image.lift_fac } /-- The categorical image of a morphism in `AddCommGroup` agrees with the usual group-theoretical range. -/ noncomputable def image_iso_range {G H : AddCommGroup.{0}} (f : G ⟶ H) : limits.image f ≅ AddCommGroup.of f.range := is_image.iso_ext (image.is_image f) (is_image f) end AddCommGroup
e5c224c86202568301c37326c0607335785d190c
2eab05920d6eeb06665e1a6df77b3157354316ad
/src/measure_theory/measure/finite_measure_weak_convergence.lean
c9179fdea0356a1688ab425e0cbddda01cb6e20d
[ "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
8,334
lean
/- Copyright (c) 2021 Kalle Kytölä. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kalle Kytölä -/ import measure_theory.measure.measure_space /-! # Weak convergence of (finite) measures This file will define the topology of weak convergence of finite measures and probability measures on topological spaces. The topology of weak convergence is the coarsest topology w.r.t. which for every bounded continuous `ℝ≥0`-valued function `f`, the integration of `f` against the measure is continuous. TODOs: * Define the topologies (the current version only defines the types) via `weak_dual ℝ≥0 (α →ᵇ ℝ≥0)`. * Prove that an equivalent definition of the topologies is obtained requiring continuity of integration of bounded continuous `ℝ`-valued functions instead. * Include the portmanteau theorem on characterizations of weak convergence of (Borel) probability measures. ## Main definitions The main definitions are the types `finite_measure α` and `probability_measure α`. TODO: * Define the topologies on the above types. ## Main results None yet. TODO: * Portmanteau theorem. ## Notations No new notation is introduced. ## Implementation notes The topology of weak convergence of finite Borel measures will be defined using a mapping from `finite_measure α` to `weak_dual ℝ≥0 (α →ᵇ ℝ≥0)`, inheriting the topology from the latter. The current implementation of `finite_measure α` and `probability_measure α` is directly as subtypes of `measure α`, and the coercion to a function is the composition `ennreal.to_nnreal` and the coercion to function of `measure α`. Another alternative would be to use a bijection with `vector_measure α ℝ≥0` as an intermediate step. The choice of implementation should not have drastic downstream effects, so it can be changed later if appropriate. Potential advantages of using the `nnreal`-valued vector measure alternative: * The coercion to function would avoid need to compose with `ennreal.to_nnreal`, the `nnreal`-valued API could be more directly available. Potential drawbacks of the vector measure alternative: * The coercion to function would lose monotonicity, as non-measurable sets would be defined to have measure 0. * No integration theory directly. E.g., the topology definition requires `lintegral` w.r.t. a coercion to `measure α` in any case. ## References * [Billingsley, *Convergence of probability measures*][billingsley1999] ## Tags weak convergence of measures, finite measure, probability measure -/ noncomputable theory open measure_theory open set open filter open_locale topological_space ennreal nnreal namespace measure_theory variables {α : Type*} [measurable_space α] /-- Finite measures are defined as the subtype of measures that have the property of being finite measures (i.e., their total mass is finite). -/ def finite_measure (α : Type*) [measurable_space α] : Type* := {μ : measure α // is_finite_measure μ} namespace finite_measure /-- A finite measure can be interpreted as a measure. -/ instance : has_coe (finite_measure α) (measure_theory.measure α) := coe_subtype instance is_finite_measure (μ : finite_measure α) : is_finite_measure (μ : measure α) := μ.prop instance : has_coe_to_fun (finite_measure α) (λ _, set α → ℝ≥0) := ⟨λ μ s, (μ s).to_nnreal⟩ lemma coe_fn_eq_to_nnreal_coe_fn_to_measure (ν : finite_measure α) : (ν : set α → ℝ≥0) = λ s, ((ν : measure α) s).to_nnreal := rfl @[simp] lemma ennreal_coe_fn_eq_coe_fn_to_measure (ν : finite_measure α) (s : set α) : (ν s : ℝ≥0∞) = (ν : measure α) s := ennreal.coe_to_nnreal (measure_lt_top ↑ν s).ne @[simp] lemma val_eq_to_measure (ν : finite_measure α) : ν.val = (ν : measure α) := rfl lemma coe_injective : function.injective (coe : finite_measure α → measure α) := subtype.coe_injective /-- The (total) mass of a finite measure `μ` is `μ univ`, i.e., the cast to `nnreal` of `(μ : measure α) univ`. -/ def mass (μ : finite_measure α) : ℝ≥0 := μ univ @[simp] lemma ennreal_mass {μ : finite_measure α} : (μ.mass : ℝ≥0∞) = (μ : measure α) univ := ennreal_coe_fn_eq_coe_fn_to_measure μ set.univ instance has_zero : has_zero (finite_measure α) := { zero := ⟨0, measure_theory.is_finite_measure_zero⟩ } instance : inhabited (finite_measure α) := ⟨0⟩ instance : has_add (finite_measure α) := { add := λ μ ν, ⟨μ + ν, measure_theory.is_finite_measure_add⟩ } instance : has_scalar ℝ≥0 (finite_measure α) := { smul := λ (c : ℝ≥0) μ, ⟨c • μ, measure_theory.is_finite_measure_smul_nnreal⟩, } @[simp, norm_cast] lemma coe_zero : (coe : finite_measure α → measure α) 0 = 0 := rfl @[simp, norm_cast] lemma coe_add (μ ν : finite_measure α) : ↑(μ + ν) = (↑μ + ↑ν : measure α) := rfl @[simp, norm_cast] lemma coe_smul (c : ℝ≥0) (μ : finite_measure α) : ↑(c • μ) = (c • ↑μ : measure α) := rfl @[simp, norm_cast] lemma coe_fn_zero : (⇑(0 : finite_measure α) : set α → ℝ≥0) = (0 : set α → ℝ≥0) := by { funext, refl, } @[simp, norm_cast] lemma coe_fn_add (μ ν : finite_measure α) : (⇑(μ + ν) : set α → ℝ≥0) = (⇑μ + ⇑ν : set α → ℝ≥0) := by { funext, simp [← ennreal.coe_eq_coe], } @[simp, norm_cast] lemma coe_fn_smul (c : ℝ≥0) (μ : finite_measure α) : (⇑(c • μ) : set α → ℝ≥0) = c • (⇑μ : set α → ℝ≥0) := by { funext, simp [← ennreal.coe_eq_coe], refl, } instance : add_comm_monoid (finite_measure α) := finite_measure.coe_injective.add_comm_monoid (coe : finite_measure α → measure α) finite_measure.coe_zero finite_measure.coe_add /-- Coercion is an `add_monoid_hom`. -/ @[simps] def coe_add_monoid_hom : finite_measure α →+ measure α := { to_fun := coe, map_zero' := coe_zero, map_add' := coe_add } instance {α : Type*} [measurable_space α] : module ℝ≥0 (finite_measure α) := function.injective.module _ coe_add_monoid_hom finite_measure.coe_injective coe_smul end finite_measure /-- Probability measures are defined as the subtype of measures that have the property of being probability measures (i.e., their total mass is one). -/ def probability_measure (α : Type*) [measurable_space α] : Type* := {μ : measure α // is_probability_measure μ} namespace probability_measure instance [inhabited α] : inhabited (probability_measure α) := ⟨⟨measure.dirac (default α), measure.dirac.is_probability_measure⟩⟩ /-- A probability measure can be interpreted as a measure. -/ instance : has_coe (probability_measure α) (measure_theory.measure α) := coe_subtype instance : has_coe_to_fun (probability_measure α) (λ _, set α → ℝ≥0) := ⟨λ μ s, (μ s).to_nnreal⟩ instance (μ : probability_measure α) : is_probability_measure (μ : measure α) := μ.prop lemma coe_fn_eq_to_nnreal_coe_fn_to_measure (ν : probability_measure α) : (ν : set α → ℝ≥0) = λ s, ((ν : measure α) s).to_nnreal := rfl @[simp] lemma val_eq_to_measure (ν : probability_measure α) : ν.val = (ν : measure α) := rfl lemma coe_injective : function.injective (coe : probability_measure α → measure α) := subtype.coe_injective @[simp] lemma coe_fn_univ (ν : probability_measure α) : ν univ = 1 := congr_arg ennreal.to_nnreal ν.prop.measure_univ /-- A probability measure can be interpreted as a finite measure. -/ def to_finite_measure (μ : probability_measure α) : finite_measure α := ⟨μ, infer_instance⟩ @[simp] lemma coe_comp_to_finite_measure_eq_coe (ν : probability_measure α) : (ν.to_finite_measure : measure α) = (ν : measure α) := rfl @[simp] lemma coe_fn_comp_to_finite_measure_eq_coe_fn (ν : probability_measure α) : (ν.to_finite_measure : set α → ℝ≥0) = (ν : set α → ℝ≥0) := rfl @[simp] lemma ennreal_coe_fn_eq_coe_fn_to_measure (ν : probability_measure α) (s : set α) : (ν s : ℝ≥0∞) = (ν : measure α) s := by { rw [← coe_fn_comp_to_finite_measure_eq_coe_fn, finite_measure.ennreal_coe_fn_eq_coe_fn_to_measure], refl, } @[simp] lemma mass_to_finite_measure (μ : probability_measure α) : μ.to_finite_measure.mass = 1 := μ.coe_fn_univ end probability_measure end measure_theory
29bbc42fa41c82c7b5bc0944067be9441427b9cd
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/linear_algebra/trace.lean
8872d7e83904428ca67e468754f508081ede7f0f
[ "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,499
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, Patrick Massot, Casper Putz, Anne Baanen -/ import linear_algebra.matrix.to_lin import linear_algebra.matrix.trace /-! # Trace of a matrix This file defines the trace of a linear map. See also `linear_algebra/matrix/trace.lean` for the trace of a matrix. ## Tags linear_map, trace, diagonal -/ noncomputable theory universes u v w namespace linear_map open_locale big_operators open_locale matrix variables (R : Type u) [comm_ring R] {M : Type v} [add_comm_group M] [module R M] variables {ι : Type w} [decidable_eq ι] [fintype ι] variables {κ : Type*} [decidable_eq κ] [fintype κ] variables (b : basis ι R M) (c : basis κ R M) /-- The trace of an endomorphism given a basis. -/ def trace_aux : (M →ₗ[R] M) →ₗ[R] R := (matrix.trace ι R R).comp $ linear_map.to_matrix b b -- Can't be `simp` because it would cause a loop. lemma trace_aux_def (b : basis ι R M) (f : M →ₗ[R] M) : trace_aux R b f = matrix.trace ι R R (linear_map.to_matrix b b f) := rfl theorem trace_aux_eq : trace_aux R b = trace_aux R c := linear_map.ext $ λ f, calc matrix.trace ι R R (linear_map.to_matrix b b f) = matrix.trace ι R R (linear_map.to_matrix b b ((linear_map.id.comp f).comp linear_map.id)) : by rw [linear_map.id_comp, linear_map.comp_id] ... = matrix.trace ι R R (linear_map.to_matrix c b linear_map.id ⬝ linear_map.to_matrix c c f ⬝ linear_map.to_matrix b c linear_map.id) : by rw [linear_map.to_matrix_comp _ c, linear_map.to_matrix_comp _ c] ... = matrix.trace κ R R (linear_map.to_matrix c c f ⬝ linear_map.to_matrix b c linear_map.id ⬝ linear_map.to_matrix c b linear_map.id) : by rw [matrix.mul_assoc, matrix.trace_mul_comm] ... = matrix.trace κ R R (linear_map.to_matrix c c ((f.comp linear_map.id).comp linear_map.id)) : by rw [linear_map.to_matrix_comp _ b, linear_map.to_matrix_comp _ c] ... = matrix.trace κ R R (linear_map.to_matrix c c f) : by rw [linear_map.comp_id, linear_map.comp_id] open_locale classical variables (R) (M) /-- Trace of an endomorphism independent of basis. -/ def trace : (M →ₗ[R] M) →ₗ[R] R := if H : ∃ (s : finset M), nonempty (basis s R M) then trace_aux R H.some_spec.some else 0 variables (R) {M} /-- Auxiliary lemma for `trace_eq_matrix_trace`. -/ theorem trace_eq_matrix_trace_of_finset {s : finset M} (b : basis s R M) (f : M →ₗ[R] M) : trace R M f = matrix.trace s R R (linear_map.to_matrix b b f) := have ∃ (s : finset M), nonempty (basis s R M), from ⟨s, ⟨b⟩⟩, by { rw [trace, dif_pos this, ← trace_aux_def], congr' 1, apply trace_aux_eq } theorem trace_eq_matrix_trace (f : M →ₗ[R] M) : trace R M f = matrix.trace ι R R (linear_map.to_matrix b b f) := if hR : nontrivial R then by haveI := hR; rw [trace_eq_matrix_trace_of_finset R b.reindex_finset_range, ← trace_aux_def, ← trace_aux_def, trace_aux_eq R b] else @subsingleton.elim _ (not_nontrivial_iff_subsingleton.mp hR) _ _ theorem trace_mul_comm (f g : M →ₗ[R] M) : trace R M (f * g) = trace R M (g * f) := if H : ∃ (s : finset M), nonempty (basis s R M) then let ⟨s, ⟨b⟩⟩ := H in by { simp_rw [trace_eq_matrix_trace R b, linear_map.to_matrix_mul], apply matrix.trace_mul_comm } else by rw [trace, dif_neg H, linear_map.zero_apply, linear_map.zero_apply] end linear_map
a562cd2f230052aa4b442f8e159cd85cb879f072
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/library/data/int/gcd.lean
32cd10376299a6933e674ff72694633bff74ec1d
[ "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
14,561
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura Definitions and properties of gcd, lcm, and coprime. -/ import .div data.nat.gcd open eq.ops namespace int /- gcd -/ definition gcd (a b : ℤ) : ℤ := of_nat (nat.gcd (nat_abs a) (nat_abs b)) theorem gcd_nonneg (a b : ℤ) : gcd a b ≥ 0 := of_nat_nonneg (nat.gcd (nat_abs a) (nat_abs b)) theorem gcd.comm (a b : ℤ) : gcd a b = gcd b a := by rewrite [↑gcd, nat.gcd.comm] theorem gcd_zero_right (a : ℤ) : gcd a 0 = abs a := by rewrite [↑gcd, nat_abs_zero, nat.gcd_zero_right, of_nat_nat_abs] theorem gcd_zero_left (a : ℤ) : gcd 0 a = abs a := by rewrite [gcd.comm, gcd_zero_right] theorem gcd_one_right (a : ℤ) : gcd a 1 = 1 := by rewrite [↑gcd, nat_abs_one, nat.gcd_one_right] theorem gcd_one_left (a : ℤ) : gcd 1 a = 1 := by rewrite [gcd.comm, gcd_one_right] theorem gcd_abs_left (a b : ℤ) : gcd (abs a) b = gcd a b := by rewrite [↑gcd, *nat_abs_abs] theorem gcd_abs_right (a b : ℤ) : gcd (abs a) b = gcd a b := by rewrite [↑gcd, *nat_abs_abs] theorem gcd_abs_abs (a b : ℤ) : gcd (abs a) (abs b) = gcd a b := by rewrite [↑gcd, *nat_abs_abs] section open nat theorem gcd_of_ne_zero (a : ℤ) {b : ℤ} (H : b ≠ 0) : gcd a b = gcd b (abs a % abs b) := have nat_abs b ≠ 0, from assume H', H (eq_zero_of_nat_abs_eq_zero H'), have nat_abs b > 0, from pos_of_ne_zero this, have nat.gcd (nat_abs a) (nat_abs b) = (nat.gcd (nat_abs b) (nat_abs a % nat_abs b)), from @nat.gcd_of_pos (nat_abs a) (nat_abs b) this, calc gcd a b = nat.gcd (nat_abs b) (nat_abs a % nat_abs b) : by rewrite [↑gcd, this] ... = gcd (abs b) (abs a % abs b) : by rewrite [↑gcd, -*of_nat_nat_abs, of_nat_mod] ... = gcd b (abs a % abs b) : by rewrite [↑gcd, *nat_abs_abs] end theorem gcd_of_pos (a : ℤ) {b : ℤ} (H : b > 0) : gcd a b = gcd b (abs a % b) := by rewrite [!gcd_of_ne_zero (ne_of_gt H), abs_of_pos H] theorem gcd_of_nonneg_of_pos {a b : ℤ} (H1 : a ≥ 0) (H2 : b > 0) : gcd a b = gcd b (a % b) := by rewrite [!gcd_of_pos H2, abs_of_nonneg H1] theorem gcd_self (a : ℤ) : gcd a a = abs a := by rewrite [↑gcd, nat.gcd_self, of_nat_nat_abs] theorem gcd_dvd_left (a b : ℤ) : gcd a b ∣ a := have gcd a b ∣ abs a, by rewrite [↑gcd, -of_nat_nat_abs, of_nat_dvd_of_nat_iff]; apply nat.gcd_dvd_left, iff.mp !dvd_abs_iff this theorem gcd_dvd_right (a b : ℤ) : gcd a b ∣ b := by rewrite gcd.comm; apply gcd_dvd_left theorem dvd_gcd {a b c : ℤ} : a ∣ b → a ∣ c → a ∣ gcd b c := begin rewrite [↑gcd, -*(abs_dvd_iff a), -(dvd_abs_iff _ b), -(dvd_abs_iff _ c), -*of_nat_nat_abs], rewrite [*of_nat_dvd_of_nat_iff] , apply nat.dvd_gcd end theorem gcd.assoc (a b c : ℤ) : gcd (gcd a b) c = gcd a (gcd b c) := dvd.antisymm !gcd_nonneg !gcd_nonneg (dvd_gcd (dvd.trans !gcd_dvd_left !gcd_dvd_left) (dvd_gcd (dvd.trans !gcd_dvd_left !gcd_dvd_right) !gcd_dvd_right)) (dvd_gcd (dvd_gcd !gcd_dvd_left (dvd.trans !gcd_dvd_right !gcd_dvd_left)) (dvd.trans !gcd_dvd_right !gcd_dvd_right)) theorem gcd_mul_left (a b c : ℤ) : gcd (a * b) (a * c) = abs a * gcd b c := by rewrite [↑gcd, *nat_abs_mul, nat.gcd_mul_left, of_nat_mul, of_nat_nat_abs] theorem gcd_mul_right (a b c : ℤ) : gcd (a * b) (c * b) = gcd a c * abs b := by rewrite [mul.comm a, mul.comm c, mul.comm (gcd a c), gcd_mul_left] theorem gcd_pos_of_ne_zero_left {a : ℤ} (b : ℤ) (H : a ≠ 0) : gcd a b > 0 := have gcd a b ≠ 0, from suppose gcd a b = 0, have 0 ∣ a, from this ▸ gcd_dvd_left a b, show false, from H (eq_zero_of_zero_dvd this), lt_of_le_of_ne (gcd_nonneg a b) (ne.symm this) theorem gcd_pos_of_ne_zero_right (a : ℤ) {b : ℤ} (H : b ≠ 0) : gcd a b > 0 := by rewrite gcd.comm; apply !gcd_pos_of_ne_zero_left H theorem eq_zero_of_gcd_eq_zero_left {a b : ℤ} (H : gcd a b = 0) : a = 0 := decidable.by_contradiction (suppose a ≠ 0, have gcd a b > 0, from !gcd_pos_of_ne_zero_left this, ne_of_lt this H⁻¹) theorem eq_zero_of_gcd_eq_zero_right {a b : ℤ} (H : gcd a b = 0) : b = 0 := by rewrite gcd.comm at H; apply !eq_zero_of_gcd_eq_zero_left H theorem gcd_div {a b c : ℤ} (H1 : c ∣ a) (H2 : c ∣ b) : gcd (a / c) (b / c) = gcd a b / (abs c) := decidable.by_cases (suppose c = 0, calc gcd (a / c) (b / c) = gcd 0 0 : by subst c; rewrite *int.div_zero ... = 0 : gcd_zero_left ... = gcd a b / 0 : int.div_zero ... = gcd a b / (abs c) : by subst c) (suppose c ≠ 0, have abs c ≠ 0, from assume H', this (eq_zero_of_abs_eq_zero H'), eq.symm (int.div_eq_of_eq_mul_left this (eq.symm (calc gcd (a / c) (b / c) * abs c = gcd (a / c * c) (b / c * c) : gcd_mul_right ... = gcd a (b / c * c) : int.div_mul_cancel H1 ... = gcd a b : int.div_mul_cancel H2)))) theorem gcd_dvd_gcd_mul_left (a b c : ℤ) : gcd a b ∣ gcd (c * a) b := dvd_gcd (dvd.trans !gcd_dvd_left !dvd_mul_left) !gcd_dvd_right theorem gcd_dvd_gcd_mul_right (a b c : ℤ) : gcd a b ∣ gcd (a * c) b := !mul.comm ▸ !gcd_dvd_gcd_mul_left theorem div_gcd_eq_div_gcd_of_nonneg {a₁ b₁ a₂ b₂ : ℤ} (H : a₁ * b₂ = a₂ * b₁) (H1 : b₁ ≠ 0) (H2 : b₂ ≠ 0) (H3 : a₁ ≥ 0) (H4 : a₂ ≥ 0) : a₁ / (gcd a₁ b₁) = a₂ / (gcd a₂ b₂) := begin apply div_eq_div_of_dvd_of_dvd, repeat (apply gcd_dvd_left), intro H', apply H1, apply eq_zero_of_gcd_eq_zero_right H', intro H', apply H2, apply eq_zero_of_gcd_eq_zero_right H', rewrite [-abs_of_nonneg H3 at {1}, -abs_of_nonneg H4 at {2}], rewrite [-gcd_mul_left, -gcd_mul_right, H, mul.comm b₁] end theorem div_gcd_eq_div_gcd {a₁ b₁ a₂ b₂ : ℤ} (H : a₁ * b₂ = a₂ * b₁) (H1 : b₁ > 0) (H2 : b₂ > 0) : a₁ / (gcd a₁ b₁) = a₂ / (gcd a₂ b₂) := or.elim (le_or_gt 0 a₁) (assume H3 : a₁ ≥ 0, have H4 : a₂ * b₁ ≥ 0, by rewrite -H; apply mul_nonneg H3 (le_of_lt H2), have H5 : a₂ ≥ 0, from nonneg_of_mul_nonneg_right H4 H1, div_gcd_eq_div_gcd_of_nonneg H (ne_of_gt H1) (ne_of_gt H2) H3 H5) (assume H3 : a₁ < 0, have H4 : a₂ * b₁ < 0, by rewrite -H; apply mul_neg_of_neg_of_pos H3 H2, have H5 : a₂ < 0, from neg_of_mul_neg_right H4 (le_of_lt H1), have H6 : abs a₁ / (gcd (abs a₁) (abs b₁)) = abs a₂ / (gcd (abs a₂) (abs b₂)), begin apply div_gcd_eq_div_gcd_of_nonneg, rewrite [abs_of_pos H1, abs_of_pos H2, abs_of_neg H3, abs_of_neg H5], rewrite [-*neg_mul_eq_neg_mul, H], apply ne_of_gt (abs_pos_of_pos H1), apply ne_of_gt (abs_pos_of_pos H2), repeat (apply abs_nonneg) end, have H7 : -a₁ / (gcd a₁ b₁) = -a₂ / (gcd a₂ b₂), begin rewrite [-abs_of_neg H3, -abs_of_neg H5, -gcd_abs_abs a₁], rewrite [-gcd_abs_abs a₂ b₂], exact H6 end, calc a₁ / (gcd a₁ b₁) = -(-a₁ / (gcd a₁ b₁)) : by rewrite [neg_div_of_dvd !gcd_dvd_left, neg_neg] ... = -(-a₂ / (gcd a₂ b₂)) : H7 ... = a₂ / (gcd a₂ b₂) : by rewrite [neg_div_of_dvd !gcd_dvd_left, neg_neg]) /- lcm -/ definition lcm (a b : ℤ) : ℤ := of_nat (nat.lcm (nat_abs a) (nat_abs b)) theorem lcm_nonneg (a b : ℤ) : lcm a b ≥ 0 := of_nat_nonneg (nat.lcm (nat_abs a) (nat_abs b)) theorem lcm.comm (a b : ℤ) : lcm a b = lcm b a := by rewrite [↑lcm, nat.lcm.comm] theorem lcm_zero_left (a : ℤ) : lcm 0 a = 0 := by rewrite [↑lcm, nat_abs_zero, nat.lcm_zero_left] theorem lcm_zero_right (a : ℤ) : lcm a 0 = 0 := !lcm.comm ▸ !lcm_zero_left theorem lcm_one_left (a : ℤ) : lcm 1 a = abs a := by rewrite [↑lcm, nat_abs_one, nat.lcm_one_left, of_nat_nat_abs] theorem lcm_one_right (a : ℤ) : lcm a 1 = abs a := !lcm.comm ▸ !lcm_one_left theorem lcm_abs_left (a b : ℤ) : lcm (abs a) b = lcm a b := by rewrite [↑lcm, *nat_abs_abs] theorem lcm_abs_right (a b : ℤ) : lcm (abs a) b = lcm a b := by rewrite [↑lcm, *nat_abs_abs] theorem lcm_abs_abs (a b : ℤ) : lcm (abs a) (abs b) = lcm a b := by rewrite [↑lcm, *nat_abs_abs] theorem lcm_self (a : ℤ) : lcm a a = abs a := by rewrite [↑lcm, nat.lcm_self, of_nat_nat_abs] theorem dvd_lcm_left (a b : ℤ) : a ∣ lcm a b := by rewrite [↑lcm, -abs_dvd_iff, -of_nat_nat_abs, of_nat_dvd_of_nat_iff]; apply nat.dvd_lcm_left theorem dvd_lcm_right (a b : ℤ) : b ∣ lcm a b := !lcm.comm ▸ !dvd_lcm_left theorem gcd_mul_lcm (a b : ℤ) : gcd a b * lcm a b = abs (a * b) := begin rewrite [↑gcd, ↑lcm, -of_nat_nat_abs, -of_nat_mul, of_nat_eq_of_nat_iff, nat_abs_mul], apply nat.gcd_mul_lcm end theorem lcm_dvd {a b c : ℤ} : a ∣ c → b ∣ c → lcm a b ∣ c := begin rewrite [↑lcm, -(abs_dvd_iff a), -(abs_dvd_iff b), -*(dvd_abs_iff _ c), -*of_nat_nat_abs], rewrite [*of_nat_dvd_of_nat_iff] , apply nat.lcm_dvd end theorem lcm_assoc (a b c : ℤ) : lcm (lcm a b) c = lcm a (lcm b c) := dvd.antisymm !lcm_nonneg !lcm_nonneg (lcm_dvd (lcm_dvd !dvd_lcm_left (dvd.trans !dvd_lcm_left !dvd_lcm_right)) (dvd.trans !dvd_lcm_right !dvd_lcm_right)) (lcm_dvd (dvd.trans !dvd_lcm_left !dvd_lcm_left) (lcm_dvd (dvd.trans !dvd_lcm_right !dvd_lcm_left) !dvd_lcm_right)) /- coprime -/ abbreviation coprime (a b : ℤ) : Prop := gcd a b = 1 theorem coprime_swap {a b : ℤ} (H : coprime b a) : coprime a b := !gcd.comm ▸ H theorem dvd_of_coprime_of_dvd_mul_right {a b c : ℤ} (H1 : coprime c b) (H2 : c ∣ a * b) : c ∣ a := have H3 : gcd (a * c) (a * b) = abs a, from calc gcd (a * c) (a * b) = abs a * gcd c b : gcd_mul_left ... = abs a * 1 : H1 ... = abs a : mul_one, have H4 : (c ∣ gcd (a * c) (a * b)), from dvd_gcd !dvd_mul_left H2, by rewrite [-dvd_abs_iff, -H3]; apply H4 theorem dvd_of_coprime_of_dvd_mul_left {a b c : ℤ} (H1 : coprime c a) (H2 : c ∣ a * b) : c ∣ b := dvd_of_coprime_of_dvd_mul_right H1 (!mul.comm ▸ H2) theorem gcd_mul_left_cancel_of_coprime {c : ℤ} (a : ℤ) {b : ℤ} (H : coprime c b) : gcd (c * a) b = gcd a b := begin revert H, unfold [coprime, gcd], rewrite [-of_nat_one], rewrite [+of_nat_eq_of_nat_iff, nat_abs_mul], apply nat.gcd_mul_left_cancel_of_coprime, end theorem gcd_mul_right_cancel_of_coprime (a : ℤ) {c b : ℤ} (H : coprime c b) : gcd (a * c) b = gcd a b := !mul.comm ▸ !gcd_mul_left_cancel_of_coprime H theorem gcd_mul_left_cancel_of_coprime_right {c a : ℤ} (b : ℤ) (H : coprime c a) : gcd a (c * b) = gcd a b := !gcd.comm ▸ !gcd.comm ▸ !gcd_mul_left_cancel_of_coprime H theorem gcd_mul_right_cancel_of_coprime_right {c a : ℤ} (b : ℤ) (H : coprime c a) : gcd a (b * c) = gcd a b := !gcd.comm ▸ !gcd.comm ▸ !gcd_mul_right_cancel_of_coprime H theorem coprime_div_gcd_div_gcd {a b : ℤ} (H : gcd a b ≠ 0) : coprime (a / gcd a b) (b / gcd a b) := calc gcd (a / gcd a b) (b / gcd a b) = gcd a b / abs (gcd a b) : gcd_div !gcd_dvd_left !gcd_dvd_right ... = 1 : by rewrite [abs_of_nonneg !gcd_nonneg, int.div_self H] theorem not_coprime_of_dvd_of_dvd {m n d : ℤ} (dgt1 : d > 1) (Hm : d ∣ m) (Hn : d ∣ n) : ¬ coprime m n := assume co : coprime m n, have d ∣ gcd m n, from dvd_gcd Hm Hn, have d ∣ 1, by rewrite [↑coprime at co, co at this]; apply this, have d ≤ 1, from le_of_dvd dec_trivial this, show false, from not_lt_of_ge `d ≤ 1` `d > 1` theorem exists_coprime {a b : ℤ} (H : gcd a b ≠ 0) : exists a' b', coprime a' b' ∧ a = a' * gcd a b ∧ b = b' * gcd a b := have H1 : a = (a / gcd a b) * gcd a b, from (int.div_mul_cancel !gcd_dvd_left)⁻¹, have H2 : b = (b / gcd a b) * gcd a b, from (int.div_mul_cancel !gcd_dvd_right)⁻¹, exists.intro _ (exists.intro _ (and.intro (coprime_div_gcd_div_gcd H) (and.intro H1 H2))) theorem coprime_mul {a b c : ℤ} (H1 : coprime a c) (H2 : coprime b c) : coprime (a * b) c := calc gcd (a * b) c = gcd b c : !gcd_mul_left_cancel_of_coprime H1 ... = 1 : H2 theorem coprime_mul_right {c a b : ℤ} (H1 : coprime c a) (H2 : coprime c b) : coprime c (a * b) := coprime_swap (coprime_mul (coprime_swap H1) (coprime_swap H2)) theorem coprime_of_coprime_mul_left {c a b : ℤ} (H : coprime (c * a) b) : coprime a b := have H1 : (gcd a b ∣ gcd (c * a) b), from !gcd_dvd_gcd_mul_left, eq_one_of_dvd_one !gcd_nonneg (H ▸ H1) theorem coprime_of_coprime_mul_right {c a b : ℤ} (H : coprime (a * c) b) : coprime a b := coprime_of_coprime_mul_left (!mul.comm ▸ H) theorem coprime_of_coprime_mul_left_right {c a b : ℤ} (H : coprime a (c * b)) : coprime a b := coprime_swap (coprime_of_coprime_mul_left (coprime_swap H)) theorem coprime_of_coprime_mul_right_right {c a b : ℤ} (H : coprime a (b * c)) : coprime a b := coprime_of_coprime_mul_left_right (!mul.comm ▸ H) theorem exists_eq_prod_and_dvd_and_dvd {a b c : ℤ} (H : c ∣ a * b) : ∃ a' b', c = a' * b' ∧ a' ∣ a ∧ b' ∣ b := decidable.by_cases (suppose gcd c a = 0, have c = 0, from eq_zero_of_gcd_eq_zero_left `gcd c a = 0`, have a = 0, from eq_zero_of_gcd_eq_zero_right `gcd c a = 0`, have c = 0 * b, from `c = 0` ⬝ !zero_mul⁻¹, have 0 ∣ a, from `a = 0`⁻¹ ▸ !dvd.refl, have b ∣ b, from !dvd.refl, exists.intro _ (exists.intro _ (and.intro `c = 0 * b` (and.intro `0 ∣ a` `b ∣ b`)))) (suppose gcd c a ≠ 0, have gcd c a ∣ c, from !gcd_dvd_left, have H3 : c / gcd c a ∣ (a * b) / gcd c a, from div_dvd_div this H, have H4 : (a * b) / gcd c a = (a / gcd c a) * b, from calc a * b / gcd c a = b * a / gcd c a : mul.comm ... = b * (a / gcd c a) : !int.mul_div_assoc !gcd_dvd_right ... = a / gcd c a * b : mul.comm, have H5 : c / gcd c a ∣ (a / gcd c a) * b, from H4 ▸ H3, have H6 : coprime (c / gcd c a) (a / gcd c a), from coprime_div_gcd_div_gcd `gcd c a ≠ 0`, have H7 : c / gcd c a ∣ b, from dvd_of_coprime_of_dvd_mul_left H6 H5, have H8 : c = gcd c a * (c / gcd c a), from (int.mul_div_cancel' `gcd c a ∣ c`)⁻¹, exists.intro _ (exists.intro _ (and.intro H8 (and.intro !gcd_dvd_right H7)))) end int
d376d7e03253ee0e26c614062da9dd0ebe5508f3
f5f7e6fae601a5fe3cac7cc3ed353ed781d62419
/src/data/set/basic.lean
a6db4fd9e3851b32519bea44d19f97912de21e96
[ "Apache-2.0" ]
permissive
EdAyers/mathlib
9ecfb2f14bd6caad748b64c9c131befbff0fb4e0
ca5d4c1f16f9c451cf7170b10105d0051db79e1b
refs/heads/master
1,626,189,395,845
1,555,284,396,000
1,555,284,396,000
144,004,030
0
0
Apache-2.0
1,533,727,664,000
1,533,727,663,000
null
UTF-8
Lean
false
false
49,132
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jeremy Avigad, Leonardo de Moura -/ import tactic.ext tactic.finish data.subtype tactic.interactive open function /- set coercion to a type -/ namespace set instance {α : Type*} : has_coe_to_sort (set α) := ⟨_, λ s, {x // x ∈ s}⟩ end set section set_coe universe u variables {α : Type u} theorem set.set_coe_eq_subtype (s : set α) : coe_sort.{(u+1) (u+2)} s = {x // x ∈ s} := rfl @[simp] theorem set_coe.forall {s : set α} {p : s → Prop} : (∀ x : s, p x) ↔ (∀ x (h : x ∈ s), p ⟨x, h⟩) := subtype.forall @[simp] theorem set_coe.exists {s : set α} {p : s → Prop} : (∃ x : s, p x) ↔ (∃ x (h : x ∈ s), p ⟨x, h⟩) := subtype.exists @[simp] theorem set_coe_cast : ∀ {s t : set α} (H' : s = t) (H : @eq (Type u) s t) (x : s), cast H x = ⟨x.1, H' ▸ x.2⟩ | s _ rfl _ ⟨x, h⟩ := rfl theorem set_coe.ext {s : set α} {a b : s} : (↑a : α) = ↑b → a = b := subtype.eq theorem set_coe.ext_iff {s : set α} {a b : s} : (↑a : α) = ↑b ↔ a = b := iff.intro set_coe.ext (assume h, h ▸ rfl) end set_coe lemma subtype.mem {α : Type*} {s : set α} (p : s) : (p : α) ∈ s := p.property namespace set universes u v w x variables {α : Type u} {β : Type v} {γ : Type w} {ι : Sort x} {a : α} {s t : set α} instance : inhabited (set α) := ⟨∅⟩ @[extensionality] theorem ext {a b : set α} (h : ∀ x, x ∈ a ↔ x ∈ b) : a = b := funext (assume x, propext (h x)) theorem ext_iff (s t : set α) : s = t ↔ ∀ x, x ∈ s ↔ x ∈ t := ⟨λ h x, by rw h, ext⟩ @[trans] theorem mem_of_mem_of_subset {α : Type u} {x : α} {s t : set α} (hx : x ∈ s) (h : s ⊆ t) : x ∈ t := h hx /- mem and set_of -/ @[simp] theorem mem_set_of_eq {a : α} {p : α → Prop} : a ∈ {a | p a} = p a := rfl @[simp] theorem nmem_set_of_eq {a : α} {P : α → Prop} : a ∉ {a : α | P a} = ¬ P a := rfl @[simp] theorem set_of_mem_eq {s : set α} : {x | x ∈ s} = s := rfl theorem mem_def {a : α} {s : set α} : a ∈ s ↔ s a := iff.rfl instance decidable_mem (s : set α) [H : decidable_pred s] : ∀ a, decidable (a ∈ s) := H instance decidable_set_of (p : α → Prop) [H : decidable_pred p] : decidable_pred {a | p a} := H @[simp] theorem set_of_subset_set_of {p q : α → Prop} : {a | p a} ⊆ {a | q a} ↔ (∀a, p a → q a) := iff.rfl @[simp] lemma sep_set_of {α} {p q : α → Prop} : {a ∈ {a | p a } | q a} = {a | p a ∧ q a} := rfl @[simp] lemma set_of_mem {α} {s : set α} : {a | a ∈ s} = s := rfl /- subset -/ -- TODO(Jeremy): write a tactic to unfold specific instances of generic notation? theorem subset_def {s t : set α} : (s ⊆ t) = ∀ x, x ∈ s → x ∈ t := rfl @[refl] theorem subset.refl (a : set α) : a ⊆ a := assume x, id @[trans] theorem subset.trans {a b c : set α} (ab : a ⊆ b) (bc : b ⊆ c) : a ⊆ c := assume x h, bc (ab h) @[trans] theorem mem_of_eq_of_mem {α : Type u} {x y : α} {s : set α} (hx : x = y) (h : y ∈ s) : x ∈ s := hx.symm ▸ h theorem subset.antisymm {a b : set α} (h₁ : a ⊆ b) (h₂ : b ⊆ a) : a = b := ext (λ x, iff.intro (λ ina, h₁ ina) (λ inb, h₂ inb)) theorem subset.antisymm_iff {a b : set α} : a = b ↔ a ⊆ b ∧ b ⊆ a := ⟨λ e, e ▸ ⟨subset.refl _, subset.refl _⟩, λ ⟨h₁, h₂⟩, subset.antisymm h₁ h₂⟩ -- an alterantive name theorem eq_of_subset_of_subset {a b : set α} (h₁ : a ⊆ b) (h₂ : b ⊆ a) : a = b := subset.antisymm h₁ h₂ theorem mem_of_subset_of_mem {s₁ s₂ : set α} {a : α} : s₁ ⊆ s₂ → a ∈ s₁ → a ∈ s₂ := assume h₁ h₂, h₁ h₂ theorem not_subset : (¬ s ⊆ t) ↔ ∃a, a ∈ s ∧ a ∉ t := by simp [subset_def, classical.not_forall] /- strict subset -/ /-- `s ⊂ t` means that `s` is a strict subset of `t`, that is, `s ⊆ t` but `s ≠ t`. -/ def strict_subset (s t : set α) := s ⊆ t ∧ s ≠ t instance : has_ssubset (set α) := ⟨strict_subset⟩ theorem ssubset_def : (s ⊂ t) = (s ⊆ t ∧ s ≠ t) := rfl lemma exists_of_ssubset {α : Type u} {s t : set α} (h : s ⊂ t) : (∃x∈t, x ∉ s) := classical.by_contradiction $ assume hn, have t ⊆ s, from assume a hat, classical.by_contradiction $ assume has, hn ⟨a, hat, has⟩, h.2 $ subset.antisymm h.1 this lemma ssubset_iff_subset_not_subset {s t : set α} : s ⊂ t ↔ s ⊆ t ∧ ¬ t ⊆ s := by split; simp [set.ssubset_def, ne.def, set.subset.antisymm_iff] {contextual := tt} theorem not_mem_empty (x : α) : ¬ (x ∈ (∅ : set α)) := assume h : x ∈ ∅, h @[simp] theorem not_not_mem [decidable (a ∈ s)] : ¬ (a ∉ s) ↔ a ∈ s := not_not /- empty set -/ theorem empty_def : (∅ : set α) = {x | false} := rfl @[simp] theorem mem_empty_eq (x : α) : x ∈ (∅ : set α) = false := rfl @[simp] theorem set_of_false : {a : α | false} = ∅ := rfl theorem eq_empty_iff_forall_not_mem {s : set α} : s = ∅ ↔ ∀ x, x ∉ s := by simp [ext_iff] theorem ne_empty_of_mem {s : set α} {x : α} (h : x ∈ s) : s ≠ ∅ := by { intro hs, rw hs at h, apply not_mem_empty _ h } @[simp] theorem empty_subset (s : set α) : ∅ ⊆ s := assume x, assume h, false.elim h theorem subset_empty_iff {s : set α} : s ⊆ ∅ ↔ s = ∅ := by simp [subset.antisymm_iff] theorem eq_empty_of_subset_empty {s : set α} : s ⊆ ∅ → s = ∅ := subset_empty_iff.1 theorem ne_empty_iff_exists_mem {s : set α} : s ≠ ∅ ↔ ∃ x, x ∈ s := by haveI := classical.prop_decidable; simp [eq_empty_iff_forall_not_mem] theorem exists_mem_of_ne_empty {s : set α} : s ≠ ∅ → ∃ x, x ∈ s := ne_empty_iff_exists_mem.1 theorem coe_nonempty_iff_ne_empty {s : set α} : nonempty s ↔ s ≠ ∅ := nonempty_subtype.trans ne_empty_iff_exists_mem.symm -- TODO: remove when simplifier stops rewriting `a ≠ b` to `¬ a = b` theorem not_eq_empty_iff_exists {s : set α} : ¬ (s = ∅) ↔ ∃ x, x ∈ s := ne_empty_iff_exists_mem theorem subset_eq_empty {s t : set α} (h : t ⊆ s) (e : s = ∅) : t = ∅ := subset_empty_iff.1 $ e ▸ h theorem subset_ne_empty {s t : set α} (h : t ⊆ s) : t ≠ ∅ → s ≠ ∅ := mt (subset_eq_empty h) theorem ball_empty_iff {p : α → Prop} : (∀ x ∈ (∅ : set α), p x) ↔ true := by simp [iff_def] /- universal set -/ theorem univ_def : @univ α = {x | true} := rfl @[simp] theorem mem_univ (x : α) : x ∈ @univ α := trivial theorem empty_ne_univ [h : inhabited α] : (∅ : set α) ≠ univ := by simp [ext_iff] @[simp] theorem subset_univ (s : set α) : s ⊆ univ := λ x H, trivial theorem univ_subset_iff {s : set α} : univ ⊆ s ↔ s = univ := by simp [subset.antisymm_iff] theorem eq_univ_of_univ_subset {s : set α} : univ ⊆ s → s = univ := univ_subset_iff.1 theorem eq_univ_iff_forall {s : set α} : s = univ ↔ ∀ x, x ∈ s := by simp [ext_iff] theorem eq_univ_of_forall {s : set α} : (∀ x, x ∈ s) → s = univ := eq_univ_iff_forall.2 @[simp] lemma univ_eq_empty_iff {α : Type*} : (univ : set α) = ∅ ↔ ¬ nonempty α := eq_empty_iff_forall_not_mem.trans ⟨λ H ⟨x⟩, H x trivial, λ H x _, H ⟨x⟩⟩ lemma nonempty_iff_univ_ne_empty {α : Type*} : nonempty α ↔ (univ : set α) ≠ ∅ := by classical; exact iff_not_comm.1 univ_eq_empty_iff lemma exists_mem_of_nonempty (α) : ∀ [nonempty α], ∃x:α, x ∈ (univ : set α) | ⟨x⟩ := ⟨x, trivial⟩ @[simp] lemma univ_ne_empty {α} [h : nonempty α] : (univ : set α) ≠ ∅ := λ e, univ_eq_empty_iff.1 e h instance univ_decidable : decidable_pred (@set.univ α) := λ x, is_true trivial /- union -/ theorem union_def {s₁ s₂ : set α} : s₁ ∪ s₂ = {a | a ∈ s₁ ∨ a ∈ s₂} := rfl theorem mem_union_left {x : α} {a : set α} (b : set α) : x ∈ a → x ∈ a ∪ b := or.inl theorem mem_union_right {x : α} {b : set α} (a : set α) : x ∈ b → x ∈ a ∪ b := or.inr theorem mem_or_mem_of_mem_union {x : α} {a b : set α} (H : x ∈ a ∪ b) : x ∈ a ∨ x ∈ b := H theorem mem_union.elim {x : α} {a b : set α} {P : Prop} (H₁ : x ∈ a ∪ b) (H₂ : x ∈ a → P) (H₃ : x ∈ b → P) : P := or.elim H₁ H₂ H₃ theorem mem_union (x : α) (a b : set α) : x ∈ a ∪ b ↔ x ∈ a ∨ x ∈ b := iff.rfl @[simp] theorem mem_union_eq (x : α) (a b : set α) : x ∈ a ∪ b = (x ∈ a ∨ x ∈ b) := rfl @[simp] theorem union_self (a : set α) : a ∪ a = a := ext (assume x, or_self _) @[simp] theorem union_empty (a : set α) : a ∪ ∅ = a := ext (assume x, or_false _) @[simp] theorem empty_union (a : set α) : ∅ ∪ a = a := ext (assume x, false_or _) theorem union_comm (a b : set α) : a ∪ b = b ∪ a := ext (assume x, or.comm) theorem union_assoc (a b c : set α) : (a ∪ b) ∪ c = a ∪ (b ∪ c) := ext (assume x, or.assoc) instance union_is_assoc : is_associative (set α) (∪) := ⟨union_assoc⟩ instance union_is_comm : is_commutative (set α) (∪) := ⟨union_comm⟩ theorem union_left_comm (s₁ s₂ s₃ : set α) : s₁ ∪ (s₂ ∪ s₃) = s₂ ∪ (s₁ ∪ s₃) := by finish theorem union_right_comm (s₁ s₂ s₃ : set α) : (s₁ ∪ s₂) ∪ s₃ = (s₁ ∪ s₃) ∪ s₂ := by finish theorem union_eq_self_of_subset_left {s t : set α} (h : s ⊆ t) : s ∪ t = t := by finish [subset_def, ext_iff, iff_def] theorem union_eq_self_of_subset_right {s t : set α} (h : t ⊆ s) : s ∪ t = s := by finish [subset_def, ext_iff, iff_def] @[simp] theorem subset_union_left (s t : set α) : s ⊆ s ∪ t := λ x, or.inl @[simp] theorem subset_union_right (s t : set α) : t ⊆ s ∪ t := λ x, or.inr theorem union_subset {s t r : set α} (sr : s ⊆ r) (tr : t ⊆ r) : s ∪ t ⊆ r := by finish [subset_def, union_def] @[simp] theorem union_subset_iff {s t u : set α} : s ∪ t ⊆ u ↔ s ⊆ u ∧ t ⊆ u := by finish [iff_def, subset_def] theorem union_subset_union {s₁ s₂ t₁ t₂ : set α} (h₁ : s₁ ⊆ s₂) (h₂ : t₁ ⊆ t₂) : s₁ ∪ t₁ ⊆ s₂ ∪ t₂ := by finish [subset_def] theorem union_subset_union_left {s₁ s₂ : set α} (t) (h : s₁ ⊆ s₂) : s₁ ∪ t ⊆ s₂ ∪ t := union_subset_union h (by refl) theorem union_subset_union_right (s) {t₁ t₂ : set α} (h : t₁ ⊆ t₂) : s ∪ t₁ ⊆ s ∪ t₂ := union_subset_union (by refl) h @[simp] theorem union_empty_iff {s t : set α} : s ∪ t = ∅ ↔ s = ∅ ∧ t = ∅ := ⟨by finish [ext_iff], by finish [ext_iff]⟩ /- intersection -/ theorem inter_def {s₁ s₂ : set α} : s₁ ∩ s₂ = {a | a ∈ s₁ ∧ a ∈ s₂} := rfl theorem mem_inter_iff (x : α) (a b : set α) : x ∈ a ∩ b ↔ x ∈ a ∧ x ∈ b := iff.rfl @[simp] theorem mem_inter_eq (x : α) (a b : set α) : x ∈ a ∩ b = (x ∈ a ∧ x ∈ b) := rfl theorem mem_inter {x : α} {a b : set α} (ha : x ∈ a) (hb : x ∈ b) : x ∈ a ∩ b := ⟨ha, hb⟩ theorem mem_of_mem_inter_left {x : α} {a b : set α} (h : x ∈ a ∩ b) : x ∈ a := h.left theorem mem_of_mem_inter_right {x : α} {a b : set α} (h : x ∈ a ∩ b) : x ∈ b := h.right @[simp] theorem inter_self (a : set α) : a ∩ a = a := ext (assume x, and_self _) @[simp] theorem inter_empty (a : set α) : a ∩ ∅ = ∅ := ext (assume x, and_false _) @[simp] theorem empty_inter (a : set α) : ∅ ∩ a = ∅ := ext (assume x, false_and _) theorem inter_comm (a b : set α) : a ∩ b = b ∩ a := ext (assume x, and.comm) theorem inter_assoc (a b c : set α) : (a ∩ b) ∩ c = a ∩ (b ∩ c) := ext (assume x, and.assoc) instance inter_is_assoc : is_associative (set α) (∩) := ⟨inter_assoc⟩ instance inter_is_comm : is_commutative (set α) (∩) := ⟨inter_comm⟩ theorem inter_left_comm (s₁ s₂ s₃ : set α) : s₁ ∩ (s₂ ∩ s₃) = s₂ ∩ (s₁ ∩ s₃) := by finish theorem inter_right_comm (s₁ s₂ s₃ : set α) : (s₁ ∩ s₂) ∩ s₃ = (s₁ ∩ s₃) ∩ s₂ := by finish @[simp] theorem inter_subset_left (s t : set α) : s ∩ t ⊆ s := λ x H, and.left H @[simp] theorem inter_subset_right (s t : set α) : s ∩ t ⊆ t := λ x H, and.right H theorem subset_inter {s t r : set α} (rs : r ⊆ s) (rt : r ⊆ t) : r ⊆ s ∩ t := by finish [subset_def, inter_def] @[simp] theorem subset_inter_iff {s t r : set α} : r ⊆ s ∩ t ↔ r ⊆ s ∧ r ⊆ t := ⟨λ h, ⟨subset.trans h (inter_subset_left _ _), subset.trans h (inter_subset_right _ _)⟩, λ ⟨h₁, h₂⟩, subset_inter h₁ h₂⟩ @[simp] theorem inter_univ (a : set α) : a ∩ univ = a := ext (assume x, and_true _) @[simp] theorem univ_inter (a : set α) : univ ∩ a = a := ext (assume x, true_and _) theorem inter_subset_inter_left {s t : set α} (u : set α) (H : s ⊆ t) : s ∩ u ⊆ t ∩ u := by finish [subset_def] theorem inter_subset_inter_right {s t : set α} (u : set α) (H : s ⊆ t) : u ∩ s ⊆ u ∩ t := by finish [subset_def] theorem inter_subset_inter {s₁ s₂ t₁ t₂ : set α} (h₁ : s₁ ⊆ t₁) (h₂ : s₂ ⊆ t₂) : s₁ ∩ s₂ ⊆ t₁ ∩ t₂ := by finish [subset_def] theorem inter_eq_self_of_subset_left {s t : set α} (h : s ⊆ t) : s ∩ t = s := by finish [subset_def, ext_iff, iff_def] theorem inter_eq_self_of_subset_right {s t : set α} (h : t ⊆ s) : s ∩ t = t := by finish [subset_def, ext_iff, iff_def] theorem union_inter_cancel_left {s t : set α} : (s ∪ t) ∩ s = s := by finish [ext_iff, iff_def] theorem union_inter_cancel_right {s t : set α} : (s ∪ t) ∩ t = t := by finish [ext_iff, iff_def] -- TODO(Mario): remove? theorem nonempty_of_inter_nonempty_right {s t : set α} (h : s ∩ t ≠ ∅) : t ≠ ∅ := by finish [ext_iff, iff_def] theorem nonempty_of_inter_nonempty_left {s t : set α} (h : s ∩ t ≠ ∅) : s ≠ ∅ := by finish [ext_iff, iff_def] /- distributivity laws -/ theorem inter_distrib_left (s t u : set α) : s ∩ (t ∪ u) = (s ∩ t) ∪ (s ∩ u) := ext (assume x, and_or_distrib_left) theorem inter_distrib_right (s t u : set α) : (s ∪ t) ∩ u = (s ∩ u) ∪ (t ∩ u) := ext (assume x, or_and_distrib_right) theorem union_distrib_left (s t u : set α) : s ∪ (t ∩ u) = (s ∪ t) ∩ (s ∪ u) := ext (assume x, or_and_distrib_left) theorem union_distrib_right (s t u : set α) : (s ∩ t) ∪ u = (s ∪ u) ∩ (t ∪ u) := ext (assume x, and_or_distrib_right) /- insert -/ theorem insert_def (x : α) (s : set α) : insert x s = { y | y = x ∨ y ∈ s } := rfl @[simp] theorem insert_of_has_insert (x : α) (s : set α) : has_insert.insert x s = insert x s := rfl @[simp] theorem subset_insert (x : α) (s : set α) : s ⊆ insert x s := assume y ys, or.inr ys theorem mem_insert (x : α) (s : set α) : x ∈ insert x s := or.inl rfl theorem mem_insert_of_mem {x : α} {s : set α} (y : α) : x ∈ s → x ∈ insert y s := or.inr theorem eq_or_mem_of_mem_insert {x a : α} {s : set α} : x ∈ insert a s → x = a ∨ x ∈ s := id theorem mem_of_mem_insert_of_ne {x a : α} {s : set α} (xin : x ∈ insert a s) : x ≠ a → x ∈ s := by finish [insert_def] @[simp] theorem mem_insert_iff {x a : α} {s : set α} : x ∈ insert a s ↔ (x = a ∨ x ∈ s) := iff.rfl @[simp] theorem insert_eq_of_mem {a : α} {s : set α} (h : a ∈ s) : insert a s = s := by finish [ext_iff, iff_def] theorem insert_subset : insert a s ⊆ t ↔ (a ∈ t ∧ s ⊆ t) := by simp [subset_def, or_imp_distrib, forall_and_distrib] theorem insert_subset_insert (h : s ⊆ t) : insert a s ⊆ insert a t := assume a', or.imp_right (@h a') theorem ssubset_insert {s : set α} {a : α} (h : a ∉ s) : s ⊂ insert a s := by finish [ssubset_def, ext_iff] theorem insert_comm (a b : α) (s : set α) : insert a (insert b s) = insert b (insert a s) := ext $ by simp [or.left_comm] theorem insert_union : insert a s ∪ t = insert a (s ∪ t) := ext $ assume a, by simp [or.comm, or.left_comm] @[simp] theorem union_insert : s ∪ insert a t = insert a (s ∪ t) := ext $ assume a, by simp [or.comm, or.left_comm] -- TODO(Jeremy): make this automatic theorem insert_ne_empty (a : α) (s : set α) : insert a s ≠ ∅ := by safe [ext_iff, iff_def]; have h' := a_1 a; finish -- useful in proofs by induction theorem forall_of_forall_insert {P : α → Prop} {a : α} {s : set α} (h : ∀ x, x ∈ insert a s → P x) : ∀ x, x ∈ s → P x := by finish theorem forall_insert_of_forall {P : α → Prop} {a : α} {s : set α} (h : ∀ x, x ∈ s → P x) (ha : P a) : ∀ x, x ∈ insert a s → P x := by finish theorem ball_insert_iff {P : α → Prop} {a : α} {s : set α} : (∀ x ∈ insert a s, P x) ↔ P a ∧ (∀x ∈ s, P x) := by finish [iff_def] /- singletons -/ theorem singleton_def (a : α) : ({a} : set α) = insert a ∅ := rfl @[simp] theorem mem_singleton_iff {a b : α} : a ∈ ({b} : set α) ↔ a = b := by finish [singleton_def] lemma set_of_eq_eq_singleton {a : α} : {n | n = a} = {a} := set.ext $ λ n, (set.mem_singleton_iff).symm -- TODO: again, annotation needed @[simp] theorem mem_singleton (a : α) : a ∈ ({a} : set α) := by finish theorem eq_of_mem_singleton {x y : α} (h : x ∈ ({y} : set α)) : x = y := by finish @[simp] theorem singleton_eq_singleton_iff {x y : α} : {x} = ({y} : set α) ↔ x = y := by finish [ext_iff, iff_def] theorem mem_singleton_of_eq {x y : α} (H : x = y) : x ∈ ({y} : set α) := by finish theorem insert_eq (x : α) (s : set α) : insert x s = ({x} : set α) ∪ s := by finish [ext_iff, or_comm] @[simp] theorem pair_eq_singleton (a : α) : ({a, a} : set α) = {a} := by finish @[simp] theorem singleton_ne_empty (a : α) : ({a} : set α) ≠ ∅ := insert_ne_empty _ _ @[simp] theorem singleton_subset_iff {a : α} {s : set α} : {a} ⊆ s ↔ a ∈ s := ⟨λh, h (by simp), λh b e, by simp at e; simp [*]⟩ theorem set_compr_eq_eq_singleton {a : α} : {b | b = a} = {a} := ext $ by simp @[simp] theorem union_singleton : s ∪ {a} = insert a s := by simp [singleton_def] @[simp] theorem singleton_union : {a} ∪ s = insert a s := by rw [union_comm, union_singleton] theorem singleton_inter_eq_empty : {a} ∩ s = ∅ ↔ a ∉ s := by simp [eq_empty_iff_forall_not_mem] theorem inter_singleton_eq_empty : s ∩ {a} = ∅ ↔ a ∉ s := by rw [inter_comm, singleton_inter_eq_empty] /- separation -/ theorem mem_sep {s : set α} {p : α → Prop} {x : α} (xs : x ∈ s) (px : p x) : x ∈ {x ∈ s | p x} := ⟨xs, px⟩ @[simp] theorem mem_sep_eq {s : set α} {p : α → Prop} {x : α} : x ∈ {x ∈ s | p x} = (x ∈ s ∧ p x) := rfl theorem mem_sep_iff {s : set α} {p : α → Prop} {x : α} : x ∈ {x ∈ s | p x} ↔ x ∈ s ∧ p x := iff.rfl theorem eq_sep_of_subset {s t : set α} (ssubt : s ⊆ t) : s = {x ∈ t | x ∈ s} := by finish [ext_iff, iff_def, subset_def] theorem sep_subset (s : set α) (p : α → Prop) : {x ∈ s | p x} ⊆ s := assume x, and.left theorem forall_not_of_sep_empty {s : set α} {p : α → Prop} (h : {x ∈ s | p x} = ∅) : ∀ x ∈ s, ¬ p x := by finish [ext_iff] @[simp] lemma sep_univ {α} {p : α → Prop} : {a ∈ (univ : set α) | p a} = {a | p a} := set.ext $ by simp /- complement -/ theorem mem_compl {s : set α} {x : α} (h : x ∉ s) : x ∈ -s := h lemma compl_set_of {α} (p : α → Prop) : - {a | p a} = { a | ¬ p a } := rfl theorem not_mem_of_mem_compl {s : set α} {x : α} (h : x ∈ -s) : x ∉ s := h @[simp] theorem mem_compl_eq (s : set α) (x : α) : x ∈ -s = (x ∉ s) := rfl theorem mem_compl_iff (s : set α) (x : α) : x ∈ -s ↔ x ∉ s := iff.rfl @[simp] theorem inter_compl_self (s : set α) : s ∩ -s = ∅ := by finish [ext_iff] @[simp] theorem compl_inter_self (s : set α) : -s ∩ s = ∅ := by finish [ext_iff] @[simp] theorem compl_empty : -(∅ : set α) = univ := by finish [ext_iff] @[simp] theorem compl_union (s t : set α) : -(s ∪ t) = -s ∩ -t := by finish [ext_iff] @[simp] theorem compl_compl (s : set α) : -(-s) = s := by finish [ext_iff] -- ditto theorem compl_inter (s t : set α) : -(s ∩ t) = -s ∪ -t := by finish [ext_iff] @[simp] theorem compl_univ : -(univ : set α) = ∅ := by finish [ext_iff] theorem union_eq_compl_compl_inter_compl (s t : set α) : s ∪ t = -(-s ∩ -t) := by simp [compl_inter, compl_compl] theorem inter_eq_compl_compl_union_compl (s t : set α) : s ∩ t = -(-s ∪ -t) := by simp [compl_compl] @[simp] theorem union_compl_self (s : set α) : s ∪ -s = univ := by finish [ext_iff] @[simp] theorem compl_union_self (s : set α) : -s ∪ s = univ := by finish [ext_iff] theorem compl_comp_compl : compl ∘ compl = @id (set α) := funext compl_compl theorem compl_subset_comm {s t : set α} : -s ⊆ t ↔ -t ⊆ s := by haveI := classical.prop_decidable; exact forall_congr (λ a, not_imp_comm) lemma compl_subset_compl {s t : set α} : -s ⊆ -t ↔ t ⊆ s := by rw [compl_subset_comm, compl_compl] theorem compl_subset_iff_union {s t : set α} : -s ⊆ t ↔ s ∪ t = univ := iff.symm $ eq_univ_iff_forall.trans $ forall_congr $ λ a, by haveI := classical.prop_decidable; exact or_iff_not_imp_left theorem subset_compl_comm {s t : set α} : s ⊆ -t ↔ t ⊆ -s := forall_congr $ λ a, imp_not_comm theorem subset_compl_iff_disjoint {s t : set α} : s ⊆ -t ↔ s ∩ t = ∅ := iff.trans (forall_congr $ λ a, and_imp.symm) subset_empty_iff theorem inter_subset (a b c : set α) : a ∩ b ⊆ c ↔ a ⊆ -b ∪ c := begin haveI := classical.prop_decidable, split, { intros h x xa, by_cases h' : x ∈ b, simp [h ⟨xa, h'⟩], simp [h'] }, intros h x, rintro ⟨xa, xb⟩, cases h xa, contradiction, assumption end /- set difference -/ theorem diff_eq (s t : set α) : s \ t = s ∩ -t := rfl @[simp] theorem mem_diff {s t : set α} (x : α) : x ∈ s \ t ↔ x ∈ s ∧ x ∉ t := iff.rfl theorem mem_diff_of_mem {s t : set α} {x : α} (h1 : x ∈ s) (h2 : x ∉ t) : x ∈ s \ t := ⟨h1, h2⟩ theorem mem_of_mem_diff {s t : set α} {x : α} (h : x ∈ s \ t) : x ∈ s := h.left theorem not_mem_of_mem_diff {s t : set α} {x : α} (h : x ∈ s \ t) : x ∉ t := h.right theorem union_diff_cancel {s t : set α} (h : s ⊆ t) : s ∪ (t \ s) = t := by finish [ext_iff, iff_def, subset_def] theorem union_diff_cancel_left {s t : set α} (h : s ∩ t ⊆ ∅) : (s ∪ t) \ s = t := by finish [ext_iff, iff_def, subset_def] theorem union_diff_cancel_right {s t : set α} (h : s ∩ t ⊆ ∅) : (s ∪ t) \ t = s := by finish [ext_iff, iff_def, subset_def] theorem union_diff_left {s t : set α} : (s ∪ t) \ s = t \ s := by finish [ext_iff, iff_def] theorem union_diff_right {s t : set α} : (s ∪ t) \ t = s \ t := by finish [ext_iff, iff_def] theorem union_diff_distrib {s t u : set α} : (s ∪ t) \ u = s \ u ∪ t \ u := inter_distrib_right _ _ _ theorem inter_diff_assoc (a b c : set α) : (a ∩ b) \ c = a ∩ (b \ c) := inter_assoc _ _ _ theorem inter_diff_self (a b : set α) : a ∩ (b \ a) = ∅ := by finish [ext_iff] theorem inter_union_diff (s t : set α) : (s ∩ t) ∪ (s \ t) = s := by finish [ext_iff, iff_def] theorem diff_subset (s t : set α) : s \ t ⊆ s := by finish [subset_def] theorem diff_subset_diff {s₁ s₂ t₁ t₂ : set α} : s₁ ⊆ s₂ → t₂ ⊆ t₁ → s₁ \ t₁ ⊆ s₂ \ t₂ := by finish [subset_def] theorem diff_subset_diff_left {s₁ s₂ t : set α} (h : s₁ ⊆ s₂) : s₁ \ t ⊆ s₂ \ t := diff_subset_diff h (by refl) theorem diff_subset_diff_right {s t u : set α} (h : t ⊆ u) : s \ u ⊆ s \ t := diff_subset_diff (subset.refl s) h theorem compl_eq_univ_diff (s : set α) : -s = univ \ s := by finish [ext_iff] @[simp] lemma empty_diff {α : Type*} (s : set α) : (∅ \ s : set α) = ∅ := eq_empty_of_subset_empty $ assume x ⟨hx, _⟩, hx theorem diff_eq_empty {s t : set α} : s \ t = ∅ ↔ s ⊆ t := ⟨assume h x hx, classical.by_contradiction $ assume : x ∉ t, show x ∈ (∅ : set α), from h ▸ ⟨hx, this⟩, assume h, eq_empty_of_subset_empty $ assume x ⟨hx, hnx⟩, hnx $ h hx⟩ @[simp] theorem diff_empty {s : set α} : s \ ∅ = s := ext $ assume x, ⟨assume ⟨hx, _⟩, hx, assume h, ⟨h, not_false⟩⟩ theorem diff_diff {u : set α} : s \ t \ u = s \ (t ∪ u) := ext $ by simp [not_or_distrib, and.comm, and.left_comm] lemma diff_subset_iff {s t u : set α} : s \ t ⊆ u ↔ s ⊆ t ∪ u := ⟨assume h x xs, classical.by_cases or.inl (assume nxt, or.inr (h ⟨xs, nxt⟩)), assume h x ⟨xs, nxt⟩, or.resolve_left (h xs) nxt⟩ lemma diff_subset_comm {s t u : set α} : s \ t ⊆ u ↔ s \ u ⊆ t := by rw [diff_subset_iff, diff_subset_iff, union_comm] @[simp] theorem insert_diff (h : a ∈ t) : insert a s \ t = s \ t := ext $ by intro; constructor; simp [or_imp_distrib, h] {contextual := tt} theorem union_diff_self {s t : set α} : s ∪ (t \ s) = s ∪ t := by finish [ext_iff, iff_def] theorem diff_union_self {s t : set α} : (s \ t) ∪ t = s ∪ t := by rw [union_comm, union_diff_self, union_comm] theorem diff_inter_self {a b : set α} : (b \ a) ∩ a = ∅ := ext $ by simp [iff_def] {contextual:=tt} theorem diff_eq_self {s t : set α} : s \ t = s ↔ t ∩ s ⊆ ∅ := by finish [ext_iff, iff_def, subset_def] @[simp] theorem diff_singleton_eq_self {a : α} {s : set α} (h : a ∉ s) : s \ {a} = s := diff_eq_self.2 $ by simp [singleton_inter_eq_empty.2 h] @[simp] theorem insert_diff_singleton {a : α} {s : set α} : insert a (s \ {a}) = insert a s := by simp [insert_eq, union_diff_self, -union_singleton, -singleton_union] @[simp] lemma diff_self {s : set α} : s \ s = ∅ := ext $ by simp /- powerset -/ theorem mem_powerset {x s : set α} (h : x ⊆ s) : x ∈ powerset s := h theorem subset_of_mem_powerset {x s : set α} (h : x ∈ powerset s) : x ⊆ s := h theorem mem_powerset_iff (x s : set α) : x ∈ powerset s ↔ x ⊆ s := iff.rfl /- inverse image -/ /-- The preimage of `s : set β` by `f : α → β`, written `f ⁻¹' s`, is the set of `x : α` such that `f x ∈ s`. -/ def preimage {α : Type u} {β : Type v} (f : α → β) (s : set β) : set α := {x | f x ∈ s} infix ` ⁻¹' `:80 := preimage section preimage variables {f : α → β} {g : β → γ} @[simp] theorem preimage_empty : f ⁻¹' ∅ = ∅ := rfl @[simp] theorem mem_preimage_eq {s : set β} {a : α} : (a ∈ f ⁻¹' s) = (f a ∈ s) := rfl theorem preimage_mono {s t : set β} (h : s ⊆ t) : f ⁻¹' s ⊆ f ⁻¹' t := assume x hx, h hx @[simp] theorem preimage_univ : f ⁻¹' univ = univ := rfl @[simp] theorem preimage_inter {s t : set β} : f ⁻¹' (s ∩ t) = f ⁻¹' s ∩ f ⁻¹' t := rfl @[simp] theorem preimage_union {s t : set β} : f ⁻¹' (s ∪ t) = f ⁻¹' s ∪ f ⁻¹' t := rfl @[simp] theorem preimage_compl {s : set β} : f ⁻¹' (- s) = - (f ⁻¹' s) := rfl @[simp] theorem preimage_diff (f : α → β) (s t : set β) : f ⁻¹' (s \ t) = f ⁻¹' s \ f ⁻¹' t := rfl @[simp] theorem preimage_set_of_eq {p : α → Prop} {f : β → α} : f ⁻¹' {a | p a} = {a | p (f a)} := rfl theorem preimage_id {s : set α} : id ⁻¹' s = s := rfl theorem preimage_comp {s : set γ} : (g ∘ f) ⁻¹' s = f ⁻¹' (g ⁻¹' s) := rfl theorem eq_preimage_subtype_val_iff {p : α → Prop} {s : set (subtype p)} {t : set α} : s = subtype.val ⁻¹' t ↔ (∀x (h : p x), (⟨x, h⟩ : subtype p) ∈ s ↔ x ∈ t) := ⟨assume s_eq x h, by rw [s_eq]; simp, assume h, ext $ assume ⟨x, hx⟩, by simp [h]⟩ end preimage /- function image -/ section image infix ` '' `:80 := image /-- Two functions `f₁ f₂ : α → β` are equal on `s` if `f₁ x = f₂ x` for all `x ∈ a`. -/ @[reducible] def eq_on (f1 f2 : α → β) (a : set α) : Prop := ∀ x ∈ a, f1 x = f2 x -- TODO(Jeremy): use bounded exists in image theorem mem_image_iff_bex {f : α → β} {s : set α} {y : β} : y ∈ f '' s ↔ ∃ x (_ : x ∈ s), f x = y := bex_def.symm theorem mem_image_eq (f : α → β) (s : set α) (y: β) : y ∈ f '' s = ∃ x, x ∈ s ∧ f x = y := rfl @[simp] theorem mem_image (f : α → β) (s : set α) (y : β) : y ∈ f '' s ↔ ∃ x, x ∈ s ∧ f x = y := iff.rfl theorem mem_image_of_mem (f : α → β) {x : α} {a : set α} (h : x ∈ a) : f x ∈ f '' a := ⟨_, h, rfl⟩ theorem mem_image_of_injective {f : α → β} {a : α} {s : set α} (hf : injective f) : f a ∈ f '' s ↔ a ∈ s := iff.intro (assume ⟨b, hb, eq⟩, (hf eq) ▸ hb) (assume h, mem_image_of_mem _ h) theorem ball_image_of_ball {f : α → β} {s : set α} {p : β → Prop} (h : ∀ x ∈ s, p (f x)) : ∀ y ∈ f '' s, p y := by finish [mem_image_eq] @[simp] theorem ball_image_iff {f : α → β} {s : set α} {p : β → Prop} : (∀ y ∈ f '' s, p y) ↔ (∀ x ∈ s, p (f x)) := iff.intro (assume h a ha, h _ $ mem_image_of_mem _ ha) (assume h b ⟨a, ha, eq⟩, eq ▸ h a ha) theorem mono_image {f : α → β} {s t : set α} (h : s ⊆ t) : f '' s ⊆ f '' t := assume x ⟨y, hy, y_eq⟩, y_eq ▸ mem_image_of_mem _ $ h hy theorem mem_image_elim {f : α → β} {s : set α} {C : β → Prop} (h : ∀ (x : α), x ∈ s → C (f x)) : ∀{y : β}, y ∈ f '' s → C y | ._ ⟨a, a_in, rfl⟩ := h a a_in theorem mem_image_elim_on {f : α → β} {s : set α} {C : β → Prop} {y : β} (h_y : y ∈ f '' s) (h : ∀ (x : α), x ∈ s → C (f x)) : C y := mem_image_elim h h_y @[congr] lemma image_congr {f g : α → β} {s : set α} (h : ∀a∈s, f a = g a) : f '' s = g '' s := by safe [ext_iff, iff_def] theorem image_eq_image_of_eq_on {f₁ f₂ : α → β} {s : set α} (heq : eq_on f₁ f₂ s) : f₁ '' s = f₂ '' s := image_congr heq theorem image_comp (f : β → γ) (g : α → β) (a : set α) : (f ∘ g) '' a = f '' (g '' a) := subset.antisymm (ball_image_of_ball $ assume a ha, mem_image_of_mem _ $ mem_image_of_mem _ ha) (ball_image_of_ball $ ball_image_of_ball $ assume a ha, mem_image_of_mem _ ha) /- Proof is removed as it uses generated names TODO(Jeremy): make automatic, begin safe [ext_iff, iff_def, mem_image, (∘)], have h' := h_2 (g a_2), finish end -/ theorem image_subset {a b : set α} (f : α → β) (h : a ⊆ b) : f '' a ⊆ f '' b := by finish [subset_def, mem_image_eq] theorem image_union (f : α → β) (s t : set α) : f '' (s ∪ t) = f '' s ∪ f '' t := by finish [ext_iff, iff_def, mem_image_eq] @[simp] theorem image_empty (f : α → β) : f '' ∅ = ∅ := ext $ by simp theorem image_inter_on {f : α → β} {s t : set α} (h : ∀x∈t, ∀y∈s, f x = f y → x = y) : f '' s ∩ f '' t = f '' (s ∩ t) := subset.antisymm (assume b ⟨⟨a₁, ha₁, h₁⟩, ⟨a₂, ha₂, h₂⟩⟩, have a₂ = a₁, from h _ ha₂ _ ha₁ (by simp *), ⟨a₁, ⟨ha₁, this ▸ ha₂⟩, h₁⟩) (subset_inter (mono_image $ inter_subset_left _ _) (mono_image $ inter_subset_right _ _)) theorem image_inter {f : α → β} {s t : set α} (H : injective f) : f '' s ∩ f '' t = f '' (s ∩ t) := image_inter_on (assume x _ y _ h, H h) theorem image_univ_of_surjective {ι : Type*} {f : ι → β} (H : surjective f) : f '' univ = univ := eq_univ_of_forall $ by simp [image]; exact H @[simp] theorem image_singleton {f : α → β} {a : α} : f '' {a} = {f a} := ext $ λ x, by simp [image]; rw eq_comm @[simp] lemma image_eq_empty {α β} {f : α → β} {s : set α} : f '' s = ∅ ↔ s = ∅ := by simp only [eq_empty_iff_forall_not_mem]; exact ⟨λ H a ha, H _ ⟨_, ha, rfl⟩, λ H b ⟨_, ha, _⟩, H _ ha⟩ lemma inter_singleton_ne_empty {α : Type*} {s : set α} {a : α} : s ∩ {a} ≠ ∅ ↔ a ∈ s := by finish [set.inter_singleton_eq_empty] theorem fix_set_compl (t : set α) : compl t = - t := rfl -- TODO(Jeremy): there is an issue with - t unfolding to compl t theorem mem_compl_image (t : set α) (S : set (set α)) : t ∈ compl '' S ↔ -t ∈ S := begin suffices : ∀ x, -x = t ↔ -t = x, {simp [fix_set_compl, this]}, intro x, split; { intro e, subst e, simp } end @[simp] theorem image_id (s : set α) : id '' s = s := ext $ by simp theorem compl_compl_image (S : set (set α)) : compl '' (compl '' S) = S := by rw [← image_comp, compl_comp_compl, image_id] theorem image_insert_eq {f : α → β} {a : α} {s : set α} : f '' (insert a s) = insert (f a) (f '' s) := ext $ by simp [and_or_distrib_left, exists_or_distrib, eq_comm, or_comm, and_comm] theorem image_subset_preimage_of_inverse {f : α → β} {g : β → α} (I : left_inverse g f) (s : set α) : f '' s ⊆ g ⁻¹' s := λ b ⟨a, h, e⟩, e ▸ ((I a).symm ▸ h : g (f a) ∈ s) theorem preimage_subset_image_of_inverse {f : α → β} {g : β → α} (I : left_inverse g f) (s : set β) : f ⁻¹' s ⊆ g '' s := λ b h, ⟨f b, h, I b⟩ theorem image_eq_preimage_of_inverse {f : α → β} {g : β → α} (h₁ : left_inverse g f) (h₂ : right_inverse g f) : image f = preimage g := funext $ λ s, subset.antisymm (image_subset_preimage_of_inverse h₁ s) (preimage_subset_image_of_inverse h₂ s) theorem mem_image_iff_of_inverse {f : α → β} {g : β → α} {b : β} {s : set α} (h₁ : left_inverse g f) (h₂ : right_inverse g f) : b ∈ f '' s ↔ g b ∈ s := by rw image_eq_preimage_of_inverse h₁ h₂; refl theorem image_compl_subset {f : α → β} {s : set α} (H : injective f) : f '' -s ⊆ -(f '' s) := subset_compl_iff_disjoint.2 $ by simp [image_inter H] theorem subset_image_compl {f : α → β} {s : set α} (H : surjective f) : -(f '' s) ⊆ f '' -s := compl_subset_iff_union.2 $ by rw ← image_union; simp [image_univ_of_surjective H] theorem image_compl_eq {f : α → β} {s : set α} (H : bijective f) : f '' -s = -(f '' s) := subset.antisymm (image_compl_subset H.1) (subset_image_compl H.2) /- image and preimage are a Galois connection -/ theorem image_subset_iff {s : set α} {t : set β} {f : α → β} : f '' s ⊆ t ↔ s ⊆ f ⁻¹' t := ball_image_iff theorem image_preimage_subset (f : α → β) (s : set β) : f '' (f ⁻¹' s) ⊆ s := image_subset_iff.2 (subset.refl _) theorem subset_preimage_image (f : α → β) (s : set α) : s ⊆ f ⁻¹' (f '' s) := λ x, mem_image_of_mem f theorem preimage_image_eq {f : α → β} (s : set α) (h : injective f) : f ⁻¹' (f '' s) = s := subset.antisymm (λ x ⟨y, hy, e⟩, h e ▸ hy) (subset_preimage_image f s) theorem image_preimage_eq {f : α → β} {s : set β} (h : surjective f) : f '' (f ⁻¹' s) = s := subset.antisymm (image_preimage_subset f s) (λ x hx, let ⟨y, e⟩ := h x in ⟨y, (e.symm ▸ hx : f y ∈ s), e⟩) lemma preimage_eq_preimage {f : β → α} (hf : surjective f) : f ⁻¹' s = preimage f t ↔ s = t := iff.intro (assume eq, by rw [← @image_preimage_eq β α f s hf, ← @image_preimage_eq β α f t hf, eq]) (assume eq, eq ▸ rfl) lemma surjective_preimage {f : β → α} (hf : surjective f) : injective (preimage f) := assume s t, (preimage_eq_preimage hf).1 theorem compl_image : image (@compl α) = preimage compl := image_eq_preimage_of_inverse compl_compl compl_compl theorem compl_image_set_of {α : Type u} {p : set α → Prop} : compl '' {x | p x} = {x | p (- x)} := congr_fun compl_image p theorem inter_preimage_subset (s : set α) (t : set β) (f : α → β) : s ∩ f ⁻¹' t ⊆ f ⁻¹' (f '' s ∩ t) := λ x h, ⟨mem_image_of_mem _ h.left, h.right⟩ theorem union_preimage_subset (s : set α) (t : set β) (f : α → β) : s ∪ f ⁻¹' t ⊆ f ⁻¹' (f '' s ∪ t) := λ x h, or.elim h (λ l, or.inl $ mem_image_of_mem _ l) (λ r, or.inr r) theorem subset_image_union (f : α → β) (s : set α) (t : set β) : f '' (s ∪ f ⁻¹' t) ⊆ f '' s ∪ t := image_subset_iff.2 (union_preimage_subset _ _ _) lemma preimage_subset_iff {A : set α} {B : set β} {f : α → β} : f⁻¹' B ⊆ A ↔ (∀ a : α, f a ∈ B → a ∈ A) := iff.rfl lemma image_eq_image {f : α → β} (hf : injective f) : f '' s = f '' t ↔ s = t := iff.symm $ iff.intro (assume eq, eq ▸ rfl) $ assume eq, by rw [← preimage_image_eq s hf, ← preimage_image_eq t hf, eq] lemma image_subset_image_iff {f : α → β} (hf : injective f) : f '' s ⊆ f '' t ↔ s ⊆ t := begin refine (iff.symm $ iff.intro (image_subset f) $ assume h, _), rw [← preimage_image_eq s hf, ← preimage_image_eq t hf], exact preimage_mono h end lemma injective_image {f : α → β} (hf : injective f) : injective (('') f) := assume s t, (image_eq_image hf).1 lemma prod_quotient_preimage_eq_image [s : setoid α] (g : quotient s → β) {h : α → β} (Hh : h = g ∘ quotient.mk) (r : set (β × β)) : {x : quotient s × quotient s | (g x.1, g x.2) ∈ r} = (λ a : α × α, (⟦a.1⟧, ⟦a.2⟧)) '' ((λ a : α × α, (h a.1, h a.2)) ⁻¹' r) := Hh.symm ▸ set.ext (λ ⟨a₁, a₂⟩, ⟨quotient.induction_on₂ a₁ a₂ (λ a₁ a₂ h, ⟨(a₁, a₂), h, rfl⟩), λ ⟨⟨b₁, b₂⟩, h₁, h₂⟩, show (g a₁, g a₂) ∈ r, from have h₃ : ⟦b₁⟧ = a₁ ∧ ⟦b₂⟧ = a₂ := prod.ext_iff.1 h₂, h₃.1 ▸ h₃.2 ▸ h₁⟩) def image_factorization (f : α → β) (s : set α) : s → f '' s := λ p, ⟨f p.1, mem_image_of_mem f p.2⟩ lemma image_factorization_eq {f : α → β} {s : set α} : subtype.val ∘ image_factorization f s = f ∘ subtype.val := funext $ λ p, rfl lemma surjective_onto_image {f : α → β} {s : set α} : surjective (image_factorization f s) := λ ⟨_, ⟨a, ha, rfl⟩⟩, ⟨⟨a, ha⟩, rfl⟩ end image theorem univ_eq_true_false : univ = ({true, false} : set Prop) := eq.symm $ eq_univ_of_forall $ classical.cases (by simp) (by simp) section range variables {f : ι → α} open function /-- Range of a function. This function is more flexible than `f '' univ`, as the image requires that the domain is in Type and not an arbitrary Sort. -/ def range (f : ι → α) : set α := {x | ∃y, f y = x} @[simp] theorem mem_range {x : α} : x ∈ range f ↔ ∃ y, f y = x := iff.rfl theorem mem_range_self (i : ι) : f i ∈ range f := ⟨i, rfl⟩ theorem forall_range_iff {p : α → Prop} : (∀ a ∈ range f, p a) ↔ (∀ i, p (f i)) := ⟨assume h i, h (f i) (mem_range_self _), assume h a ⟨i, (hi : f i = a)⟩, hi ▸ h i⟩ theorem exists_range_iff {p : α → Prop} : (∃ a ∈ range f, p a) ↔ (∃ i, p (f i)) := ⟨assume ⟨a, ⟨i, eq⟩, h⟩, ⟨i, eq.symm ▸ h⟩, assume ⟨i, h⟩, ⟨f i, mem_range_self _, h⟩⟩ theorem range_iff_surjective : range f = univ ↔ surjective f := eq_univ_iff_forall @[simp] theorem range_id : range (@id α) = univ := range_iff_surjective.2 surjective_id @[simp] theorem image_univ {ι : Type*} {f : ι → β} : f '' univ = range f := ext $ by simp [image, range] theorem image_subset_range {ι : Type*} (f : ι → β) (s : set ι) : f '' s ⊆ range f := by rw ← image_univ; exact image_subset _ (subset_univ _) theorem range_comp {g : α → β} : range (g ∘ f) = g '' range f := subset.antisymm (forall_range_iff.mpr $ assume i, mem_image_of_mem g (mem_range_self _)) (ball_image_iff.mpr $ forall_range_iff.mpr mem_range_self) theorem range_subset_iff {ι : Type*} {f : ι → β} {s : set β} : range f ⊆ s ↔ ∀ y, f y ∈ s := forall_range_iff lemma nonempty_of_nonempty_range {α : Type*} {β : Type*} {f : α → β} (H : ¬range f = ∅) : nonempty α := begin cases exists_mem_of_ne_empty H with x h, cases mem_range.1 h with y _, exact ⟨y⟩ end @[simp] lemma range_eq_empty {α : Type u} {β : Type v} {f : α → β} : range f = ∅ ↔ ¬ nonempty α := by rw ← set.image_univ; simp [-set.image_univ] theorem image_preimage_eq_inter_range {f : α → β} {t : set β} : f '' (f ⁻¹' t) = t ∩ range f := ext $ assume x, ⟨assume ⟨x, hx, heq⟩, heq ▸ ⟨hx, mem_range_self _⟩, assume ⟨hx, ⟨y, h_eq⟩⟩, h_eq ▸ mem_image_of_mem f $ show y ∈ f ⁻¹' t, by simp [preimage, h_eq, hx]⟩ lemma image_preimage_eq_of_subset {f : α → β} {s : set β} (hs : s ⊆ range f) : f '' (f ⁻¹' s) = s := by rw [image_preimage_eq_inter_range, inter_eq_self_of_subset_left hs] theorem preimage_inter_range {f : α → β} {s : set β} : f ⁻¹' (s ∩ range f) = f ⁻¹' s := set.ext $ λ x, and_iff_left ⟨x, rfl⟩ theorem preimage_image_preimage {f : α → β} {s : set β} : f ⁻¹' (f '' (f ⁻¹' s)) = f ⁻¹' s := by rw [image_preimage_eq_inter_range, preimage_inter_range] @[simp] theorem quot_mk_range_eq [setoid α] : range (λx : α, ⟦x⟧) = univ := range_iff_surjective.2 quot.exists_rep lemma range_const_subset {c : β} : range (λx:α, c) ⊆ {c} := range_subset_iff.2 $ λ x, or.inl rfl @[simp] lemma range_const [h : nonempty α] {c : β} : range (λx:α, c) = {c} := begin refine subset.antisymm range_const_subset (λy hy, _), rw set.mem_singleton_iff.1 hy, rcases exists_mem_of_nonempty α with ⟨x, _⟩, exact mem_range_self x end def range_factorization (f : ι → β) : ι → range f := λ i, ⟨f i, mem_range_self i⟩ lemma range_factorization_eq {f : ι → β} : subtype.val ∘ range_factorization f = f := funext $ λ i, rfl lemma surjective_onto_range : surjective (range_factorization f) := λ ⟨_, ⟨i, rfl⟩⟩, ⟨i, rfl⟩ end range /-- The set `s` is pairwise `r` if `r x y` for all *distinct* `x y ∈ s`. -/ def pairwise_on (s : set α) (r : α → α → Prop) := ∀ x ∈ s, ∀ y ∈ s, x ≠ y → r x y theorem pairwise_on.mono {s t : set α} {r} (h : t ⊆ s) (hp : pairwise_on s r) : pairwise_on t r := λ x xt y yt, hp x (h xt) y (h yt) theorem pairwise_on.mono' {s : set α} {r r' : α → α → Prop} (H : ∀ a b, r a b → r' a b) (hp : pairwise_on s r) : pairwise_on s r' := λ x xs y ys h, H _ _ (hp x xs y ys h) end set /- image and preimage on subtypes -/ namespace subtype variable {α : Type*} lemma val_image {p : α → Prop} {s : set (subtype p)} : subtype.val '' s = {x | ∃h : p x, (⟨x, h⟩ : subtype p) ∈ s} := set.ext $ assume a, ⟨assume ⟨⟨a', ha'⟩, in_s, h_eq⟩, h_eq ▸ ⟨ha', in_s⟩, assume ⟨ha, in_s⟩, ⟨⟨a, ha⟩, in_s, rfl⟩⟩ @[simp] lemma val_range {p : α → Prop} : set.range (@subtype.val _ p) = {x | p x} := by rw ← set.image_univ; simp [-set.image_univ, val_image] theorem val_image_subset (s : set α) (t : set (subtype s)) : t.image val ⊆ s := λ x ⟨y, yt, yvaleq⟩, by rw ←yvaleq; exact y.property theorem val_image_univ (s : set α) : @val _ s '' set.univ = s := set.eq_of_subset_of_subset (val_image_subset _ _) (λ x xs, ⟨⟨x, xs⟩, ⟨set.mem_univ _, rfl⟩⟩) theorem image_preimage_val (s t : set α) : (@subtype.val _ s) '' ((@subtype.val _ s) ⁻¹' t) = t ∩ s := begin ext x, simp, split, { rintros ⟨y, ys, yt, yx⟩, rw ←yx, exact ⟨yt, ys⟩ }, rintros ⟨xt, xs⟩, exact ⟨x, xs, xt, rfl⟩ end theorem preimage_val_eq_preimage_val_iff (s t u : set α) : ((@subtype.val _ s) ⁻¹' t = (@subtype.val _ s) ⁻¹' u) ↔ (t ∩ s = u ∩ s) := begin rw [←image_preimage_val, ←image_preimage_val], split, { intro h, rw h }, intro h, exact set.injective_image (val_injective) h end end subtype namespace set section range variable {α : Type*} @[simp] lemma subtype.val_range {p : α → Prop} : range (@subtype.val _ p) = {x | p x} := by rw ← image_univ; simp [-image_univ, subtype.val_image] @[simp] lemma range_coe_subtype (s : set α): range (coe : s → α) = s := subtype.val_range end range section prod variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} variables {s s₁ s₂ : set α} {t t₁ t₂ : set β} /-- The cartesian product `prod s t` is the set of `(a, b)` such that `a ∈ s` and `b ∈ t`. -/ protected def prod (s : set α) (t : set β) : set (α × β) := {p | p.1 ∈ s ∧ p.2 ∈ t} lemma prod_eq (s : set α) (t : set β) : set.prod s t = prod.fst ⁻¹' s ∩ prod.snd ⁻¹' t := rfl theorem mem_prod_eq {p : α × β} : p ∈ set.prod s t = (p.1 ∈ s ∧ p.2 ∈ t) := rfl @[simp] theorem mem_prod {p : α × β} : p ∈ set.prod s t ↔ p.1 ∈ s ∧ p.2 ∈ t := iff.rfl lemma mk_mem_prod {a : α} {b : β} (a_in : a ∈ s) (b_in : b ∈ t) : (a, b) ∈ set.prod s t := ⟨a_in, b_in⟩ @[simp] theorem prod_empty {s : set α} : set.prod s ∅ = (∅ : set (α × β)) := ext $ by simp [set.prod] @[simp] theorem empty_prod {t : set β} : set.prod ∅ t = (∅ : set (α × β)) := ext $ by simp [set.prod] theorem insert_prod {a : α} {s : set α} {t : set β} : set.prod (insert a s) t = (prod.mk a '' t) ∪ set.prod s t := ext begin simp [set.prod, image, iff_def, or_imp_distrib] {contextual := tt}; cc end theorem prod_insert {b : β} {s : set α} {t : set β} : set.prod s (insert b t) = ((λa, (a, b)) '' s) ∪ set.prod s t := ext begin simp [set.prod, image, iff_def, or_imp_distrib] {contextual := tt}; cc end theorem prod_preimage_eq {f : γ → α} {g : δ → β} : set.prod (preimage f s) (preimage g t) = preimage (λp, (f p.1, g p.2)) (set.prod s t) := rfl theorem prod_mono {s₁ s₂ : set α} {t₁ t₂ : set β} (hs : s₁ ⊆ s₂) (ht : t₁ ⊆ t₂) : set.prod s₁ t₁ ⊆ set.prod s₂ t₂ := assume x ⟨h₁, h₂⟩, ⟨hs h₁, ht h₂⟩ theorem prod_inter_prod : set.prod s₁ t₁ ∩ set.prod s₂ t₂ = set.prod (s₁ ∩ s₂) (t₁ ∩ t₂) := subset.antisymm (assume ⟨a, b⟩ ⟨⟨ha₁, hb₁⟩, ⟨ha₂, hb₂⟩⟩, ⟨⟨ha₁, ha₂⟩, ⟨hb₁, hb₂⟩⟩) (subset_inter (prod_mono (inter_subset_left _ _) (inter_subset_left _ _)) (prod_mono (inter_subset_right _ _) (inter_subset_right _ _))) theorem image_swap_prod : (λp:β×α, (p.2, p.1)) '' set.prod t s = set.prod s t := ext $ assume ⟨a, b⟩, by simp [mem_image_eq, set.prod, and_comm]; exact ⟨ assume ⟨b', a', ⟨h_a, h_b⟩, h⟩, by subst a'; subst b'; assumption, assume h, ⟨b, a, ⟨rfl, rfl⟩, h⟩⟩ theorem image_swap_eq_preimage_swap : image (@prod.swap α β) = preimage prod.swap := image_eq_preimage_of_inverse prod.swap_left_inverse prod.swap_right_inverse theorem prod_image_image_eq {m₁ : α → γ} {m₂ : β → δ} : set.prod (image m₁ s) (image m₂ t) = image (λp:α×β, (m₁ p.1, m₂ p.2)) (set.prod s t) := ext $ by simp [-exists_and_distrib_right, exists_and_distrib_right.symm, and.left_comm, and.assoc, and.comm] theorem prod_range_range_eq {α β γ δ} {m₁ : α → γ} {m₂ : β → δ} : set.prod (range m₁) (range m₂) = range (λp:α×β, (m₁ p.1, m₂ p.2)) := ext $ by simp [range] @[simp] theorem prod_singleton_singleton {a : α} {b : β} : set.prod {a} {b} = ({(a, b)} : set (α×β)) := ext $ by simp [set.prod] theorem prod_neq_empty_iff {s : set α} {t : set β} : set.prod s t ≠ ∅ ↔ (s ≠ ∅ ∧ t ≠ ∅) := by simp [not_eq_empty_iff_exists] theorem prod_eq_empty_iff {s : set α} {t : set β} : set.prod s t = ∅ ↔ (s = ∅ ∨ t = ∅) := suffices (¬ set.prod s t ≠ ∅) ↔ (¬ s ≠ ∅ ∨ ¬ t ≠ ∅), by simpa only [(≠), classical.not_not], by classical; rw [prod_neq_empty_iff, not_and_distrib] @[simp] theorem prod_mk_mem_set_prod_eq {a : α} {b : β} {s : set α} {t : set β} : (a, b) ∈ set.prod s t = (a ∈ s ∧ b ∈ t) := rfl @[simp] theorem univ_prod_univ : set.prod (@univ α) (@univ β) = univ := ext $ assume ⟨a, b⟩, by simp lemma prod_sub_preimage_iff {W : set γ} {f : α × β → γ} : set.prod s t ⊆ f ⁻¹' W ↔ ∀ a b, a ∈ s → b ∈ t → f (a, b) ∈ W := by simp [subset_def] end prod section pi variables {α : Type*} {π : α → Type*} def pi (i : set α) (s : Πa, set (π a)) : set (Πa, π a) := { f | ∀a∈i, f a ∈ s a } @[simp] lemma pi_empty_index (s : Πa, set (π a)) : pi ∅ s = univ := by ext; simp [pi] @[simp] lemma pi_insert_index (a : α) (i : set α) (s : Πa, set (π a)) : pi (insert a i) s = ((λf, f a) ⁻¹' s a) ∩ pi i s := by ext; simp [pi, or_imp_distrib, forall_and_distrib] @[simp] lemma pi_singleton_index (a : α) (s : Πa, set (π a)) : pi {a} s = ((λf:(Πa, π a), f a) ⁻¹' s a) := by ext; simp [pi] lemma pi_if {p : α → Prop} [h : decidable_pred p] (i : set α) (s t : Πa, set (π a)) : pi i (λa, if p a then s a else t a) = pi {a ∈ i | p a} s ∩ pi {a ∈ i | ¬ p a} t := begin ext f, split, { assume h, split; { rintros a ⟨hai, hpa⟩, simpa [*] using h a } }, { rintros ⟨hs, ht⟩ a hai, by_cases p a; simp [*, pi] at * } end end pi section inclusion variable {α : Type*} /-- `inclusion` is the "identity" function between two subsets `s` and `t`, where `s ⊆ t` -/ def inclusion {s t : set α} (h : s ⊆ t) : s → t := λ x : s, (⟨x, h x.2⟩ : t) @[simp] lemma inclusion_self {s : set α} (x : s) : inclusion (set.subset.refl _) x = x := by cases x; refl @[simp] lemma inclusion_inclusion {s t u : set α} (hst : s ⊆ t) (htu : t ⊆ u) (x : s) : inclusion htu (inclusion hst x) = inclusion (set.subset.trans hst htu) x := by cases x; refl lemma inclusion_injective {s t : set α} (h : s ⊆ t) : function.injective (inclusion h) | ⟨_, _⟩ ⟨_, _⟩ := subtype.ext.2 ∘ subtype.ext.1 end inclusion end set
ea69a083c3df00d055048f4e0ec68496b5bbde70
7cdf3413c097e5d36492d12cdd07030eb991d394
/src/game/world6/level5.lean
2fbf8f1f750b703a769115e413aee4913b4230dc
[]
no_license
alreadydone/natural_number_game
3135b9385a9f43e74cfbf79513fc37e69b99e0b3
1a39e693df4f4e871eb449890d3c7715a25c2ec9
refs/heads/master
1,599,387,390,105
1,573,200,587,000
1,573,200,691,000
220,397,084
0
0
null
1,573,192,734,000
1,573,192,733,000
null
UTF-8
Lean
false
false
2,336
lean
/- # Proposition world. ## Level 5 : `P → (Q → P)`. In this level, our goal is to construct an implication, like in level 2. ``` ⊢ P → (Q → P) ``` So $P$ and $Q$ are propositions, and our goal is to prove that $P\implies(Q\implies P)$. We don't know whether $P$, $Q$ are true or false, so initially this seems like a bit of a tall order. But let's give it a go. Delete the `sorry` and let's think about how to proceed. Our goal is `P → X` for some true/false statement $X$, and if our goal is to construct an implication then we almost always want to use the `intro` tactic from level 2, Lean's version of "assume $P$ is true". So let's start with `intro p,` and we then find ourselves in this state: ``` P Q : Prop, p : P ⊢ Q → P ``` We now have a proof $p$ of $P$ and we are supposed to be constructing a proof of $Q\implies P$. So let's assume that $Q$ is true and try and prove that $P$ is true. We assume $Q$ like this: `intro q,` and now we have to prove $P$, but have a proof handy: `exact p,` -/ /- Lemma : no-side-bar For any propositions $P$ and $Q$, we always have $P\implies(Q\implies P)$. -/ example (P Q : Prop) : P → (Q → P) := begin intro p, intro q, exact p, end /- A mathematician would treat $P\implies(Q\implies P)$ as the same as the set $P\land Q\implies P$, because to give a proof of either of these is just to give a method which takes a proof of $P$ and a proof of $Q$, and returns a proof of $P$. Thinking of the goal as $P\land Q\implies P$ we see why it is provable. ## Did you notice? I wrote `P → (Q → P)` but Lean just writes `P → Q → P`. This is because computer scientists adopt the convention that `→` is *right associative*, which is a fancy way of saying "when we write `P → Q → R`, we mean `P → (Q → R)`. Mathematicians would never dream of writing something as ambiguous as $P\implies Q\implies R$ (they are not really interested in proving abstract propositions, they would rather work with concrete ones such as Fermat's Last Theorem), so they do not have a convention for where the brackets go. It's important to remember Lean's convention though, or else you will get confused. If your goal is `P → Q → R` then you need to know whether `intro h` will create `h : P` or `h : P → Q`. Make sure you understand which one. -/
914fa26e40e4bc090029315bbb74b31c24c00dc3
2bafba05c98c1107866b39609d15e849a4ca2bb8
/src/week_7/Part_C_back_to_Z.lean
f0b659bbfd6da186a820d5f4babacdf3d6cd7679
[ "Apache-2.0" ]
permissive
ImperialCollegeLondon/formalising-mathematics
b54c83c94b5c315024ff09997fcd6b303892a749
7cf1d51c27e2038d2804561d63c74711924044a1
refs/heads/master
1,651,267,046,302
1,638,888,459,000
1,638,888,459,000
331,592,375
284
24
Apache-2.0
1,669,593,705,000
1,611,224,849,000
Lean
UTF-8
Lean
false
false
1,167
lean
import week_7.Part_A_quotients import week_7.Part_B_universal_property /- # `Z ≃ ℤ` Let's use the previous parts to show that Z and ℤ are isomorphic. -/ -- Let's define pℤ to be the usual subtraction function ℕ² → ℤ def pℤ (ab : N2) : ℤ := (ab.1 : ℤ) - ab.2 @[simp] lemma pℤ_def (a b : ℕ) : pℤ (a, b) = (a : ℤ) - b := rfl -- Start with `intro z, apply int.induction_on z` to prove this. theorem pℤsurj : function.surjective pℤ := begin sorry, end -- The fibres of pℤ are equivalence classes. theorem pℤequiv (ab cd : N2) : ab ≈ cd ↔ pℤ ab = pℤ cd := begin sorry, end -- It's helpful to have a random one-sided inverse coming from surjectivity noncomputable def invp : ℤ → N2 := λ z, classical.some (pℤsurj z) -- Here's the proof that it is an inverse. @[simp] theorem invp_inv (z : ℤ) : pℤ (invp z) = z := classical.some_spec (pℤsurj z) -- Now we can prove that ℤ and pℤ are universal. theorem int_is_universal : is_universal ℤ pℤ := begin sorry, end -- and now we can prove they're in bijection noncomputable example : ℤ ≃ Z := universal_equiv_quotient _ _ _ int_is_universal
a0bfe3f250e6d506fda1979aa156278d5d37b9e4
9cba98daa30c0804090f963f9024147a50292fa0
/demo/demo.lean
41752bc3a2fc42f35a498b3ccd46b9af40837dc2
[]
no_license
kevinsullivan/phys
dcb192f7b3033797541b980f0b4a7e75d84cea1a
ebc2df3779d3605ff7a9b47eeda25c2a551e011f
refs/heads/master
1,637,490,575,500
1,629,899,064,000
1,629,899,064,000
168,012,884
0
3
null
1,629,644,436,000
1,548,699,832,000
Lean
UTF-8
Lean
false
false
1,323
lean
import data.real.basic import ..phys /- On standard_space impose a temporal coordinate system with t=0 at launch time and the unit of time is one second. -/ def launch_time /-: time (time_std_space ℚ)-/ := mk_time (time_std_space ℚ) 0 def one_second /-: duration (time_std_space ℚ)-/ := mk_duration (time_std_space ℚ) 1 def time_frame := mk_frame launch_time.to_point one_second.to_vectr -- TODO: Fix this API glitch def mission_time := mk_space ℚ time_frame -- TODO: should infer Q at this point in API /- Do some coordinate-free computations in this affine coordinate space -/ def ego_launch_time := mk_time mission_time 0 def t_plus_one_minute := mk_time mission_time 60 def one_minute := mk_duration mission_time 60 def t_plus_one_minute' := one_minute +ᵥ ego_launch_time -- coordinate free in coordinate space def t_plus_one_second := one_second +ᵥ ego_launch_time -- frame error /- Now make sure geom stuff is working equally well -/ /- Then model a dynamic system, e.g., in which a position varies with time, e.g., pos(t) = t. -/
8ac4aa5c0954f20e04d4ebf55e227b9a49f6142c
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/tactic/omega/eq_elim_auto.lean
40f46a0fb93c0e9ce76112ffb3560785944b7f27
[]
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
4,026
lean
/- Copyright (c) 2019 Seul Baek. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Seul Baek -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.tactic.omega.clause import Mathlib.PostPort universes l namespace Mathlib /- Correctness lemmas for equality elimination. See 5.5 of <http://www.decision-procedures.org/> for details. -/ namespace omega def symdiv (i : ℤ) (j : ℤ) : ℤ := ite (bit0 1 * (i % j) < j) (i / j) (i / j + 1) def symmod (i : ℤ) (j : ℤ) : ℤ := ite (bit0 1 * (i % j) < j) (i % j) (i % j - j) theorem symmod_add_one_self {i : ℤ} : 0 < i → symmod i (i + 1) = -1 := sorry theorem mul_symdiv_eq {i : ℤ} {j : ℤ} : j * symdiv i j = i - symmod i j := sorry theorem symmod_eq {i : ℤ} {j : ℤ} : symmod i j = i - j * symdiv i j := eq.mpr (id (Eq._oldrec (Eq.refl (symmod i j = i - j * symdiv i j)) mul_symdiv_eq)) (eq.mpr (id (Eq._oldrec (Eq.refl (symmod i j = i - (i - symmod i j))) (sub_sub_cancel i (symmod i j)))) (Eq.refl (symmod i j))) /-- (sgm v b as n) is the new value assigned to the nth variable after a single step of equality elimination using valuation v, term ⟨b, as⟩, and variable index n. If v satisfies the initial constraint set, then (v ⟨n ↦ sgm v b as n⟩) satisfies the new constraint set after equality elimination. -/ def sgm (v : ℕ → ℤ) (b : ℤ) (as : List ℤ) (n : ℕ) : ℤ := let a_n : ℤ := list.func.get n as; let m : ℤ := a_n + 1; (symmod b m + coeffs.val v (list.map (fun (x : ℤ) => symmod x m) as)) / m def rhs : ℕ → ℤ → List ℤ → term := sorry theorem rhs_correct_aux {v : ℕ → ℤ} {m : ℤ} {as : List ℤ} {k : ℕ} : ∃ (d : ℤ), m * d + coeffs.val_between v (list.map (fun (x : ℤ) => symmod x m) as) 0 k = coeffs.val_between v as 0 k := sorry theorem rhs_correct {v : ℕ → ℤ} {b : ℤ} {as : List ℤ} (n : ℕ) : 0 < list.func.get n as → 0 = term.val v (b, as) → v n = term.val (update n (sgm v b as n) v) (rhs n b as) := sorry def sym_sym (m : ℤ) (b : ℤ) : ℤ := symdiv b m + symmod b m def coeffs_reduce : ℕ → ℤ → List ℤ → term := sorry theorem coeffs_reduce_correct {v : ℕ → ℤ} {b : ℤ} {as : List ℤ} {n : ℕ} : 0 < list.func.get n as → 0 = term.val v (b, as) → 0 = term.val (update n (sgm v b as n) v) (coeffs_reduce n b as) := sorry -- Requires : t1.coeffs[m] = 1 def cancel (m : ℕ) (t1 : term) (t2 : term) : term := term.add (term.mul (-list.func.get m (prod.snd t2)) t1) t2 def subst (n : ℕ) (t1 : term) (t2 : term) : term := term.add (term.mul (list.func.get n (prod.snd t2)) t1) (prod.fst t2, list.func.set 0 (prod.snd t2) n) theorem subst_correct {v : ℕ → ℤ} {b : ℤ} {as : List ℤ} {t : term} {n : ℕ} : 0 < list.func.get n as → 0 = term.val v (b, as) → term.val v t = term.val (update n (sgm v b as n) v) (subst n (rhs n b as) t) := sorry /-- The type of equality elimination rules. -/ inductive ee where | drop : ee | nondiv : ℤ → ee | factor : ℤ → ee | neg : ee | reduce : ℕ → ee | cancel : ℕ → ee namespace ee def repr : ee → string := sorry protected instance has_repr : has_repr ee := has_repr.mk repr end ee /-- Apply a given sequence of equality elimination steps to a clause. -/ def eq_elim : List ee → clause → clause := sorry theorem sat_empty : clause.sat ([], []) := Exists.intro (fun (_x : ℕ) => 0) { left := of_as_true trivial, right := of_as_true trivial } theorem sat_eq_elim {es : List ee} {c : clause} : clause.sat c → clause.sat (eq_elim es c) := sorry /-- If the result of equality elimination is unsatisfiable, the original clause is unsatisfiable. -/ theorem unsat_of_unsat_eq_elim (ee : List ee) (c : clause) : clause.unsat (eq_elim ee c) → clause.unsat c := fun (h1 : clause.unsat (eq_elim ee c)) => id fun (h2 : clause.sat c) => h1 (sat_eq_elim h2) end Mathlib
74418732fb9e16fe7a2cee66d8ccd44ded0d1d68
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/eq11.lean
185db76bc99b8d428b168902058e637fe9899bbc
[ "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
841
lean
inductive day | monday | tuesday | wednesday | thursday | friday | saturday | sunday open day definition next_weekday : day → day | monday := tuesday | tuesday := wednesday | wednesday := thursday | thursday := friday | _ := monday theorem next_weekday_monday : next_weekday monday = tuesday := rfl theorem next_weekday_tuesday : next_weekday tuesday = wednesday := rfl theorem next_weekday_wednesday : next_weekday wednesday = thursday := rfl theorem next_weekday_thursday : next_weekday thursday = friday := rfl theorem next_weekday_friday : next_weekday friday = monday := rfl theorem next_weekday_sat : next_weekday saturday = monday := rfl theorem next_weekday_sunday : next_weekday sunday = monday := rfl example : next_weekday (next_weekday monday) = wednesday := rfl
4a42ab363052ff4406ab3d50fc6d2e06e0457b25
592ee40978ac7604005a4e0d35bbc4b467389241
/Library/generated/mathscheme-lean/RightInverse.lean
f0107cf28b9abaf9257e1da71058f7baef553e98
[]
no_license
ysharoda/Deriving-Definitions
3e149e6641fae440badd35ac110a0bd705a49ad2
dfecb27572022de3d4aa702cae8db19957523a59
refs/heads/master
1,679,127,857,700
1,615,939,007,000
1,615,939,007,000
229,785,731
4
0
null
null
null
null
UTF-8
Lean
false
false
8,833
lean
import init.data.nat.basic import init.data.fin.basic import data.vector import .Prelude open Staged open nat open fin open vector section RightInverse structure RightInverse (A : Type) : Type := (inv : (A → A)) (e : A) (op : (A → (A → A))) (rightInverse_inv_op_e : (∀ {x : A} , (op (inv x) x) = e)) open RightInverse structure Sig (AS : Type) : Type := (invS : (AS → AS)) (eS : AS) (opS : (AS → (AS → AS))) structure Product (A : Type) : Type := (invP : ((Prod A A) → (Prod A A))) (eP : (Prod A A)) (opP : ((Prod A A) → ((Prod A A) → (Prod A A)))) (rightInverse_inv_op_eP : (∀ {xP : (Prod A A)} , (opP (invP xP) xP) = eP)) structure Hom {A1 : Type} {A2 : Type} (Ri1 : (RightInverse A1)) (Ri2 : (RightInverse A2)) : Type := (hom : (A1 → A2)) (pres_inv : (∀ {x1 : A1} , (hom ((inv Ri1) x1)) = ((inv Ri2) (hom x1)))) (pres_e : (hom (e Ri1)) = (e Ri2)) (pres_op : (∀ {x1 x2 : A1} , (hom ((op Ri1) x1 x2)) = ((op Ri2) (hom x1) (hom x2)))) structure RelInterp {A1 : Type} {A2 : Type} (Ri1 : (RightInverse A1)) (Ri2 : (RightInverse A2)) : Type 1 := (interp : (A1 → (A2 → Type))) (interp_inv : (∀ {x1 : A1} {y1 : A2} , ((interp x1 y1) → (interp ((inv Ri1) x1) ((inv Ri2) y1))))) (interp_e : (interp (e Ri1) (e Ri2))) (interp_op : (∀ {x1 x2 : A1} {y1 y2 : A2} , ((interp x1 y1) → ((interp x2 y2) → (interp ((op Ri1) x1 x2) ((op Ri2) y1 y2)))))) inductive RightInverseLTerm : Type | invL : (RightInverseLTerm → RightInverseLTerm) | eL : RightInverseLTerm | opL : (RightInverseLTerm → (RightInverseLTerm → RightInverseLTerm)) open RightInverseLTerm inductive ClRightInverseClTerm (A : Type) : Type | sing : (A → ClRightInverseClTerm) | invCl : (ClRightInverseClTerm → ClRightInverseClTerm) | eCl : ClRightInverseClTerm | opCl : (ClRightInverseClTerm → (ClRightInverseClTerm → ClRightInverseClTerm)) open ClRightInverseClTerm inductive OpRightInverseOLTerm (n : ℕ) : Type | v : ((fin n) → OpRightInverseOLTerm) | invOL : (OpRightInverseOLTerm → OpRightInverseOLTerm) | eOL : OpRightInverseOLTerm | opOL : (OpRightInverseOLTerm → (OpRightInverseOLTerm → OpRightInverseOLTerm)) open OpRightInverseOLTerm inductive OpRightInverseOL2Term2 (n : ℕ) (A : Type) : Type | v2 : ((fin n) → OpRightInverseOL2Term2) | sing2 : (A → OpRightInverseOL2Term2) | invOL2 : (OpRightInverseOL2Term2 → OpRightInverseOL2Term2) | eOL2 : OpRightInverseOL2Term2 | opOL2 : (OpRightInverseOL2Term2 → (OpRightInverseOL2Term2 → OpRightInverseOL2Term2)) open OpRightInverseOL2Term2 def simplifyCl {A : Type} : ((ClRightInverseClTerm A) → (ClRightInverseClTerm A)) | (invCl x1) := (invCl (simplifyCl x1)) | eCl := eCl | (opCl x1 x2) := (opCl (simplifyCl x1) (simplifyCl x2)) | (sing x1) := (sing x1) def simplifyOpB {n : ℕ} : ((OpRightInverseOLTerm n) → (OpRightInverseOLTerm n)) | (invOL x1) := (invOL (simplifyOpB x1)) | eOL := eOL | (opOL x1 x2) := (opOL (simplifyOpB x1) (simplifyOpB x2)) | (v x1) := (v x1) def simplifyOp {n : ℕ} {A : Type} : ((OpRightInverseOL2Term2 n A) → (OpRightInverseOL2Term2 n A)) | (invOL2 x1) := (invOL2 (simplifyOp x1)) | eOL2 := eOL2 | (opOL2 x1 x2) := (opOL2 (simplifyOp x1) (simplifyOp x2)) | (v2 x1) := (v2 x1) | (sing2 x1) := (sing2 x1) def evalB {A : Type} : ((RightInverse A) → (RightInverseLTerm → A)) | Ri (invL x1) := ((inv Ri) (evalB Ri x1)) | Ri eL := (e Ri) | Ri (opL x1 x2) := ((op Ri) (evalB Ri x1) (evalB Ri x2)) def evalCl {A : Type} : ((RightInverse A) → ((ClRightInverseClTerm A) → A)) | Ri (sing x1) := x1 | Ri (invCl x1) := ((inv Ri) (evalCl Ri x1)) | Ri eCl := (e Ri) | Ri (opCl x1 x2) := ((op Ri) (evalCl Ri x1) (evalCl Ri x2)) def evalOpB {A : Type} {n : ℕ} : ((RightInverse A) → ((vector A n) → ((OpRightInverseOLTerm n) → A))) | Ri vars (v x1) := (nth vars x1) | Ri vars (invOL x1) := ((inv Ri) (evalOpB Ri vars x1)) | Ri vars eOL := (e Ri) | Ri vars (opOL x1 x2) := ((op Ri) (evalOpB Ri vars x1) (evalOpB Ri vars x2)) def evalOp {A : Type} {n : ℕ} : ((RightInverse A) → ((vector A n) → ((OpRightInverseOL2Term2 n A) → A))) | Ri vars (v2 x1) := (nth vars x1) | Ri vars (sing2 x1) := x1 | Ri vars (invOL2 x1) := ((inv Ri) (evalOp Ri vars x1)) | Ri vars eOL2 := (e Ri) | Ri vars (opOL2 x1 x2) := ((op Ri) (evalOp Ri vars x1) (evalOp Ri vars x2)) def inductionB {P : (RightInverseLTerm → Type)} : ((∀ (x1 : RightInverseLTerm) , ((P x1) → (P (invL x1)))) → ((P eL) → ((∀ (x1 x2 : RightInverseLTerm) , ((P x1) → ((P x2) → (P (opL x1 x2))))) → (∀ (x : RightInverseLTerm) , (P x))))) | pinvl pel popl (invL x1) := (pinvl _ (inductionB pinvl pel popl x1)) | pinvl pel popl eL := pel | pinvl pel popl (opL x1 x2) := (popl _ _ (inductionB pinvl pel popl x1) (inductionB pinvl pel popl x2)) def inductionCl {A : Type} {P : ((ClRightInverseClTerm A) → Type)} : ((∀ (x1 : A) , (P (sing x1))) → ((∀ (x1 : (ClRightInverseClTerm A)) , ((P x1) → (P (invCl x1)))) → ((P eCl) → ((∀ (x1 x2 : (ClRightInverseClTerm A)) , ((P x1) → ((P x2) → (P (opCl x1 x2))))) → (∀ (x : (ClRightInverseClTerm A)) , (P x)))))) | psing pinvcl pecl popcl (sing x1) := (psing x1) | psing pinvcl pecl popcl (invCl x1) := (pinvcl _ (inductionCl psing pinvcl pecl popcl x1)) | psing pinvcl pecl popcl eCl := pecl | psing pinvcl pecl popcl (opCl x1 x2) := (popcl _ _ (inductionCl psing pinvcl pecl popcl x1) (inductionCl psing pinvcl pecl popcl x2)) def inductionOpB {n : ℕ} {P : ((OpRightInverseOLTerm n) → Type)} : ((∀ (fin : (fin n)) , (P (v fin))) → ((∀ (x1 : (OpRightInverseOLTerm n)) , ((P x1) → (P (invOL x1)))) → ((P eOL) → ((∀ (x1 x2 : (OpRightInverseOLTerm n)) , ((P x1) → ((P x2) → (P (opOL x1 x2))))) → (∀ (x : (OpRightInverseOLTerm n)) , (P x)))))) | pv pinvol peol popol (v x1) := (pv x1) | pv pinvol peol popol (invOL x1) := (pinvol _ (inductionOpB pv pinvol peol popol x1)) | pv pinvol peol popol eOL := peol | pv pinvol peol popol (opOL x1 x2) := (popol _ _ (inductionOpB pv pinvol peol popol x1) (inductionOpB pv pinvol peol popol x2)) def inductionOp {n : ℕ} {A : Type} {P : ((OpRightInverseOL2Term2 n A) → Type)} : ((∀ (fin : (fin n)) , (P (v2 fin))) → ((∀ (x1 : A) , (P (sing2 x1))) → ((∀ (x1 : (OpRightInverseOL2Term2 n A)) , ((P x1) → (P (invOL2 x1)))) → ((P eOL2) → ((∀ (x1 x2 : (OpRightInverseOL2Term2 n A)) , ((P x1) → ((P x2) → (P (opOL2 x1 x2))))) → (∀ (x : (OpRightInverseOL2Term2 n A)) , (P x))))))) | pv2 psing2 pinvol2 peol2 popol2 (v2 x1) := (pv2 x1) | pv2 psing2 pinvol2 peol2 popol2 (sing2 x1) := (psing2 x1) | pv2 psing2 pinvol2 peol2 popol2 (invOL2 x1) := (pinvol2 _ (inductionOp pv2 psing2 pinvol2 peol2 popol2 x1)) | pv2 psing2 pinvol2 peol2 popol2 eOL2 := peol2 | pv2 psing2 pinvol2 peol2 popol2 (opOL2 x1 x2) := (popol2 _ _ (inductionOp pv2 psing2 pinvol2 peol2 popol2 x1) (inductionOp pv2 psing2 pinvol2 peol2 popol2 x2)) def stageB : (RightInverseLTerm → (Staged RightInverseLTerm)) | (invL x1) := (stage1 invL (codeLift1 invL) (stageB x1)) | eL := (Now eL) | (opL x1 x2) := (stage2 opL (codeLift2 opL) (stageB x1) (stageB x2)) def stageCl {A : Type} : ((ClRightInverseClTerm A) → (Staged (ClRightInverseClTerm A))) | (sing x1) := (Now (sing x1)) | (invCl x1) := (stage1 invCl (codeLift1 invCl) (stageCl x1)) | eCl := (Now eCl) | (opCl x1 x2) := (stage2 opCl (codeLift2 opCl) (stageCl x1) (stageCl x2)) def stageOpB {n : ℕ} : ((OpRightInverseOLTerm n) → (Staged (OpRightInverseOLTerm n))) | (v x1) := (const (code (v x1))) | (invOL x1) := (stage1 invOL (codeLift1 invOL) (stageOpB x1)) | eOL := (Now eOL) | (opOL x1 x2) := (stage2 opOL (codeLift2 opOL) (stageOpB x1) (stageOpB x2)) def stageOp {n : ℕ} {A : Type} : ((OpRightInverseOL2Term2 n A) → (Staged (OpRightInverseOL2Term2 n A))) | (sing2 x1) := (Now (sing2 x1)) | (v2 x1) := (const (code (v2 x1))) | (invOL2 x1) := (stage1 invOL2 (codeLift1 invOL2) (stageOp x1)) | eOL2 := (Now eOL2) | (opOL2 x1 x2) := (stage2 opOL2 (codeLift2 opOL2) (stageOp x1) (stageOp x2)) structure StagedRepr (A : Type) (Repr : (Type → Type)) : Type := (invT : ((Repr A) → (Repr A))) (eT : (Repr A)) (opT : ((Repr A) → ((Repr A) → (Repr A)))) end RightInverse
fa46cdce3c7163894a0356bf925a7a27bbe04227
a721fe7446524f18ba361625fc01033d9c8b7a78
/src/principia/real/completeness.lean
5d0dc9ba8188ddbed824c179a098c8c51cd9f337
[]
no_license
Sterrs/leaning
8fd80d1f0a6117a220bb2e57ece639b9a63deadc
3901cc953694b33adda86cb88ca30ba99594db31
refs/heads/master
1,627,023,822,744
1,616,515,221,000
1,616,515,221,000
245,512,190
2
0
null
1,616,429,050,000
1,583,527,118,000
Lean
UTF-8
Lean
false
false
884
lean
import .le import ..myset.basic namespace hidden def is_upper_bound (S : myset real) (a : real) := ∀ x : real, x ∈ S → x ≤ a def is_least (S : myset real) (a : real) := a ∈ S ∧ ∀ x : real, x ∈ S → a ≤ x def upper_bounds (S : myset real) : myset real := λ x, is_upper_bound S x def is_least_upper_bound (S : myset real) (a : real) := is_least (upper_bounds S) a def bounded_above (S : myset real) := ∃ a : real, is_upper_bound S a theorem least_upper_bound_property (S : myset real) : bounded_above S → ∃ x : real, is_least_upper_bound S x := sorry open classical noncomputable def sup (S : myset real) (h : bounded_above S) := some (least_upper_bound_property S h) variables {S : myset real} (h : bounded_above S) theorem sup_is_ub : is_upper_bound S (sup S h) := sorry theorem sup_is_lub : is_least_upper_bound S (sup S h) := sorry end hidden
b3d1138c4b43bfdbcdbfc5ecc0a5276e6330d45f
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/number_theory/modular_forms/congruence_subgroups.lean
aa1a2c3e4ba21f6b9dbf4d0040d437953abbc614
[ "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,084
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 FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. 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
22378960bb5dd4d1d53a967f678a43594fcc6b76
bdb33f8b7ea65f7705fc342a178508e2722eb851
/data/hash_map.lean
de00d46e5f3860c29e332f9b4fcb32edc8adcbe9
[ "Apache-2.0" ]
permissive
rwbarton/mathlib
939ae09bf8d6eb1331fc2f7e067d39567e10e33d
c13c5ea701bb1eec057e0a242d9f480a079105e9
refs/heads/master
1,584,015,335,862
1,524,142,167,000
1,524,142,167,000
130,614,171
0
0
Apache-2.0
1,548,902,667,000
1,524,437,371,000
Lean
UTF-8
Lean
false
false
27,772
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, Mario Carneiro -/ import data.list.basic data.pnat data.array.lemmas universes u v w /-- `bucket_array α β` is the underlying data type for `hash_map α β`, an array of linked lists of key-value pairs. -/ def bucket_array (α : Type u) (β : α → Type v) (n : ℕ+) := array n.1 (list Σ a, β a) /-- Make a hash_map index from a `nat` hash value and a (positive) buffer size -/ def hash_map.mk_idx (n : ℕ+) (i : nat) : fin n.1 := ⟨i % n.1, nat.mod_lt _ n.2⟩ namespace bucket_array section parameters {α : Type u} {β : α → Type v} (hash_fn : α → nat) variables {n : ℕ+} (data : bucket_array α β n) /-- Read the bucket corresponding to an element -/ def read (a : α) : list Σ a, β a := let bidx := hash_map.mk_idx n (hash_fn a) in data.read bidx /-- Write the bucket corresponding to an element -/ def write (a : α) (l : list Σ a, β a) : bucket_array α β n := let bidx := hash_map.mk_idx n (hash_fn a) in data.write bidx l /-- Modify (read, apply `f`, and write) the bucket corresponding to an element -/ def modify (a : α) (f : list (Σ a, β a) → list (Σ a, β a)) : bucket_array α β n := let bidx := hash_map.mk_idx n (hash_fn a) in array.write data bidx (f (array.read data bidx)) /-- The list of all key-value pairs in the bucket list -/ def as_list : list Σ a, β a := data.to_list.join theorem mem_as_list {a : Σ a, β a} : a ∈ data.as_list ↔ ∃i, a ∈ array.read data i := have (∃ (l : list (Σ (a : α), β a)) (i : fin (n.val)), a ∈ l ∧ array.read data i = l) ↔ ∃ (i : fin (n.val)), a ∈ array.read data i, by rw exists_swap; exact exists_congr (λ i, by simp), by simp [as_list]; simpa [array.mem.def, and_comm] /-- Fold a function `f` over the key-value pairs in the bucket list -/ def foldl {δ : Type w} (d : δ) (f : δ → Π a, β a → δ) : δ := data.foldl d (λ b d, b.foldl (λ r a, f r a.1 a.2) d) theorem foldl_eq {δ : Type w} (d : δ) (f : δ → Π a, β a → δ) : data.foldl d f = data.as_list.foldl (λ r a, f r a.1 a.2) d := by rw [foldl, as_list, list.foldl_join, ← array.to_list_foldl] end end bucket_array namespace hash_map section parameters {α : Type u} {β : α → Type v} (hash_fn : α → nat) /-- Insert the pair `⟨a, b⟩` into the correct location in the bucket array (without checking for duplication) -/ def reinsert_aux {n} (data : bucket_array α β n) (a : α) (b : β a) : bucket_array α β n := data.modify hash_fn a (λl, ⟨a, b⟩ :: l) parameter [decidable_eq α] /-- Search a bucket for a key `a` and return the value -/ def find_aux (a : α) : list (Σ a, β a) → option (β a) | [] := none | (⟨a',b⟩::t) := if h : a' = a then some (eq.rec_on h b) else find_aux t theorem find_aux_iff {a : α} {b : β a} : Π {l : list Σ a, β a}, (l.map sigma.fst).nodup → (find_aux a l = some b ↔ sigma.mk a b ∈ l) | [] nd := ⟨λn, by injection n, false.elim⟩ | (⟨a',b'⟩::t) nd := begin by_cases a' = a, { clear find_aux_iff, subst h, suffices : b' = b ↔ b' = b ∨ sigma.mk a' b ∈ t, {simpa [find_aux, eq_comm]}, refine (or_iff_left_of_imp (λ m, _)).symm, have : a' ∉ t.map sigma.fst, from list.not_mem_of_nodup_cons nd, exact this.elim (list.mem_map_of_mem sigma.fst m) }, { have : sigma.mk a b ≠ ⟨a', b'⟩, { intro e, injection e with e, exact h e.symm }, simp at nd, simp [find_aux, h, ne.symm h, find_aux_iff, nd] } end /-- Returns `tt` if the bucket `l` contains the key `a` -/ def contains_aux (a : α) (l : list Σ a, β a) : bool := (find_aux a l).is_some theorem contains_aux_iff {a : α} {l : list Σ a, β a} (nd : (l.map sigma.fst).nodup) : contains_aux a l ↔ a ∈ l.map sigma.fst := begin unfold contains_aux, cases h : find_aux a l with b; simp [option.is_some], { assume (b : β a) (m : sigma.mk a b ∈ l), rw (find_aux_iff nd).2 m at h, contradiction }, { show ∃ (b : β a), sigma.mk a b ∈ l, exact ⟨_, (find_aux_iff nd).1 h⟩ }, end /-- Modify a bucket to replace a value in the list. Leaves the list unchanged if the key is not found. -/ def replace_aux (a : α) (b : β a) : list (Σ a, β a) → list (Σ a, β a) | [] := [] | (⟨a', b'⟩::t) := if a' = a then ⟨a, b⟩::t else ⟨a', b'⟩ :: replace_aux t /-- Modify a bucket to remove a key, if it exists. -/ def erase_aux (a : α) : list (Σ a, β a) → list (Σ a, β a) | [] := [] | (⟨a', b'⟩::t) := if a' = a then t else ⟨a', b'⟩ :: erase_aux t /-- The predicate `valid bkts sz` means that `bkts` satisfies the `hash_map` invariants: There are exactly `sz` elements in it, every pair is in the bucket determined by its key and the hash function, and no key appears multiple times in the list. -/ structure valid {n} (bkts : bucket_array α β n) (sz : nat) : Prop := (len : bkts.as_list.length = sz) (idx : ∀ {i} {a : Σ a, β a}, a ∈ array.read bkts i → mk_idx n (hash_fn a.1) = i) (nodup : ∀i, ((array.read bkts i).map sigma.fst).nodup) theorem valid.idx_enum {n} {bkts : bucket_array α β n} {sz : nat} (v : valid bkts sz) {i l} (he : (i, l) ∈ bkts.to_list.enum) {a} {b : β a} (hl : sigma.mk a b ∈ l) : ∃ h, mk_idx n (hash_fn a) = ⟨i, h⟩ := (bkts.mem_to_list_enum.1 he).imp (λ h e, by subst e; exact v.idx hl) theorem valid.idx_enum_1 {n} {bkts : bucket_array α β n} {sz : nat} (v : valid bkts sz) {i l} (he : (i, l) ∈ bkts.to_list.enum) {a} {b : β a} (hl : sigma.mk a b ∈ l) : (mk_idx n (hash_fn a)).1 = i := let ⟨h, e⟩ := v.idx_enum _ he hl in by rw e; refl theorem valid.as_list_nodup {n} {bkts : bucket_array α β n} {sz : nat} (v : valid bkts sz) : (bkts.as_list.map sigma.fst).nodup := begin suffices : (bkts.to_list.map (list.map sigma.fst)).pairwise list.disjoint, { simp [bucket_array.as_list, list.nodup_join, this], change ∀ l s, array.mem s bkts → list.map sigma.fst s = l → l.nodup, introv m e, subst e, cases m with i e, subst e, apply v.nodup }, rw [← list.enum_map_snd bkts.to_list, list.pairwise_map, list.pairwise_map], have : (bkts.to_list.enum.map prod.fst).nodup := by simp [list.nodup_range], refine list.pairwise.imp_of_mem _ ((list.pairwise_map _).1 this), rw prod.forall, intros i l₁, rw prod.forall, intros j l₂ me₁ me₂ ij, simp [list.disjoint], intros a b ml₁ b' ml₂, apply ij, rwa [← v.idx_enum_1 _ me₁ ml₁, ← v.idx_enum_1 _ me₂ ml₂] end theorem mk_as_list (n : ℕ+) : bucket_array.as_list (mk_array n.1 [] : bucket_array α β n) = [] := list.eq_nil_of_forall_not_mem $ λ x m, let ⟨i, h⟩ := (bucket_array.mem_as_list _).1 m in h theorem mk_valid (n : ℕ+) : @valid n (mk_array n.1 []) 0 := ⟨by simp [mk_as_list], λ i a h, by cases h, λ i, list.nodup_nil⟩ theorem valid.find_aux_iff {n} {bkts : bucket_array α β n} {sz : nat} (v : valid bkts sz) {a : α} {b : β a} : find_aux a (bkts.read hash_fn a) = some b ↔ sigma.mk a b ∈ bkts.as_list := (find_aux_iff (v.nodup _)).trans $ by rw bkts.mem_as_list; exact ⟨λ h, ⟨_, h⟩, λ ⟨i, h⟩, (v.idx h).symm ▸ h⟩ theorem valid.contains_aux_iff {n} {bkts : bucket_array α β n} {sz : nat} (v : valid bkts sz) (a : α) : contains_aux a (bkts.read hash_fn a) ↔ a ∈ bkts.as_list.map sigma.fst := by simp [contains_aux, option.is_some_iff_exists, v.find_aux_iff hash_fn] section parameters {n : ℕ+} {bkts : bucket_array α β n} {bidx : fin n.1} {f : list (Σ a, β a) → list (Σ a, β a)} (u v1 v2 w : list Σ a, β a) local notation `L` := array.read bkts bidx private def bkts' : bucket_array α β n := array.write bkts bidx (f L) variables (hl : L = u ++ v1 ++ w) (hfl : f L = u ++ v2 ++ w) include hl hfl theorem append_of_modify : ∃ u' w', bkts.as_list = u' ++ v1 ++ w' ∧ bkts'.as_list = u' ++ v2 ++ w' := begin unfold bucket_array.as_list, have h : bidx.1 < bkts.to_list.length, {simp [bidx.2]}, refine ⟨(bkts.to_list.take bidx.1).join ++ u, w ++ (bkts.to_list.drop (bidx.1+1)).join, _, _⟩, { conv { to_lhs, rw [← list.take_append_drop bidx.1 bkts.to_list, list.drop_eq_nth_le_cons h], simp [hl] }, simp }, { conv { to_lhs, rw [bkts', array.write_to_list, list.update_nth_eq_take_cons_drop _ h], simp [hfl] }, simp } end variables (hvnd : (v2.map sigma.fst).nodup) (hal : ∀ (a : Σ a, β a), a ∈ v2 → mk_idx n (hash_fn a.1) = bidx) (djuv : (u.map sigma.fst).disjoint (v2.map sigma.fst)) (djwv : (w.map sigma.fst).disjoint (v2.map sigma.fst)) include hvnd hal djuv djwv theorem valid.modify {sz : ℕ} (v : valid bkts sz) : sz + v2.length ≥ v1.length ∧ valid bkts' (sz + v2.length - v1.length) := begin rcases append_of_modify u v1 v2 w hl hfl with ⟨u', w', e₁, e₂⟩, rw [← v.len, e₁], suffices : valid bkts' (u' ++ v2 ++ w').length, { simpa [ge, nat.le_add_right, nat.add_sub_cancel_left] }, refine ⟨congr_arg _ e₂, λ i a, _, λ i, _⟩, { by_cases bidx = i, { subst i, rw [bkts', array.read_write, hfl], have := @valid.idx _ _ _ v bidx a, simp only [hl, list.mem_append, or_imp_distrib, forall_and_distrib] at this ⊢, exact ⟨⟨this.1.1, hal _⟩, this.2⟩ }, { rw [bkts', array.read_write_of_ne _ _ h], apply v.idx } }, { by_cases bidx = i, { subst i, rw [bkts', array.read_write, hfl], have := @valid.nodup _ _ _ v bidx, simp [hl, list.nodup_append] at this, simp [list.nodup_append, this, hvnd, djuv, djwv.symm] }, { rw [bkts', array.read_write_of_ne _ _ h], apply v.nodup } } end end theorem valid.replace_aux (a : α) (b : β a) : Π (l : list (Σ a, β a)), a ∈ l.map sigma.fst → ∃ (u w : list Σ a, β a) b', l = u ++ [⟨a, b'⟩] ++ w ∧ replace_aux a b l = u ++ [⟨a, b⟩] ++ w | [] := false.elim | (⟨a', b'⟩::t) := begin by_cases e : a' = a, { subst a', suffices : ∃ u w (b'' : β a), sigma.mk a b' :: t = u ++ ⟨a, b''⟩ :: w ∧ replace_aux a b (⟨a, b'⟩ :: t) = u ++ ⟨a, b⟩ :: w, {simpa}, refine ⟨[], t, b', _⟩, simp [replace_aux] }, { suffices : ∀ (x : β a) (_ : sigma.mk a x ∈ t), ∃ u w (b'' : β a), sigma.mk a' b' :: t = u ++ ⟨a, b''⟩ :: w ∧ sigma.mk a' b' :: replace_aux a b t = u ++ ⟨a, b⟩ :: w, { simpa [replace_aux, ne.symm e, e] }, intros x m, have IH : ∀ (x : β a) (_ : sigma.mk a x ∈ t), ∃ u w (b'' : β a), t = u ++ ⟨a, b''⟩ :: w ∧ replace_aux a b t = u ++ ⟨a, b⟩ :: w, { simpa using valid.replace_aux t }, rcases IH x m with ⟨u, w, b'', hl, hfl⟩, exact ⟨⟨a', b'⟩ :: u, w, b'', by simp [hl, hfl.symm, ne.symm e]⟩ } end theorem valid.replace {n : ℕ+} {bkts : bucket_array α β n} {sz : ℕ} (a : α) (b : β a) (Hc : contains_aux a (bkts.read hash_fn a)) (v : valid bkts sz) : valid (bkts.modify hash_fn a (replace_aux a b)) sz := begin have nd := v.nodup (mk_idx n (hash_fn a)), rcases hash_map.valid.replace_aux a b (array.read bkts (mk_idx n (hash_fn a))) ((contains_aux_iff nd).1 Hc) with ⟨u, w, b', hl, hfl⟩, simp [hl, list.nodup_append] at nd, refine (v.modify hash_fn u [⟨a, b'⟩] [⟨a, b⟩] w hl hfl (list.nodup_singleton _) (λa' e, by simp at e; rw e) (λa' e1 e2, _) (λa' e1 e2, _)).2; { revert e1, simp [-sigma.exists] at e2, subst a', simp [nd] } end theorem valid.insert {n : ℕ+} {bkts : bucket_array α β n} {sz : ℕ} (a : α) (b : β a) (Hnc : ¬ contains_aux a (bkts.read hash_fn a)) (v : valid bkts sz) : valid (reinsert_aux bkts a b) (sz+1) := begin have nd := v.nodup (mk_idx n (hash_fn a)), refine (v.modify hash_fn [] [] [⟨a, b⟩] (bkts.read hash_fn a) rfl rfl (list.nodup_singleton _) (λa' e, by simp at e; rw e) (λa', false.elim) (λa' e1 e2, _)).2, simp [-sigma.exists] at e2, subst a', exact Hnc ((contains_aux_iff nd).2 e1) end theorem valid.erase_aux (a : α) : Π (l : list (Σ a, β a)), a ∈ l.map sigma.fst → ∃ (u w : list Σ a, β a) b, l = u ++ [⟨a, b⟩] ++ w ∧ erase_aux a l = u ++ [] ++ w | [] := false.elim | (⟨a', b'⟩::t) := begin by_cases e : a' = a, { subst a', simpa [erase_aux, and_comm] using show ∃ u w (x : β a), t = u ++ w ∧ sigma.mk a b' :: t = u ++ ⟨a, x⟩ :: w, from ⟨[], t, b', by simp⟩ }, { simp [erase_aux, e, ne.symm e], suffices : ∀ (b : β a) (_ : sigma.mk a b ∈ t), ∃ u w (x : β a), sigma.mk a' b' :: t = u ++ ⟨a, x⟩ :: w ∧ sigma.mk a' b' :: erase_aux a t = u ++ w, { simpa [replace_aux, ne.symm e, e] }, intros b m, have IH : ∀ (x : β a) (_ : sigma.mk a x ∈ t), ∃ u w (x : β a), t = u ++ ⟨a, x⟩ :: w ∧ erase_aux a t = u ++ w, { simpa using valid.erase_aux t }, rcases IH b m with ⟨u, w, b'', hl, hfl⟩, exact ⟨⟨a', b'⟩ :: u, w, b'', by simp [hl, hfl.symm]⟩ } end theorem valid.erase {n} {bkts : bucket_array α β n} {sz} (a : α) (Hc : contains_aux a (bkts.read hash_fn a)) (v : valid bkts sz) : valid (bkts.modify hash_fn a (erase_aux a)) (sz-1) := begin have nd := v.nodup (mk_idx n (hash_fn a)), rcases hash_map.valid.erase_aux a (array.read bkts (mk_idx n (hash_fn a))) ((contains_aux_iff nd).1 Hc) with ⟨u, w, b, hl, hfl⟩, refine (v.modify hash_fn u [⟨a, b⟩] [] w hl hfl list.nodup_nil _ _ _).2; { intros, simp at *; contradiction } end end end hash_map /-- A hash map data structure, representing a finite key-value map with key type `α` and value type `β` (which may depend on `α`). -/ structure hash_map (α : Type u) [decidable_eq α] (β : α → Type v) := (hash_fn : α → nat) (size : ℕ) (nbuckets : ℕ+) (buckets : bucket_array α β nbuckets) (is_valid : hash_map.valid hash_fn buckets size) /-- Construct an empty hash map with buffer size `nbuckets` (default 8). -/ def mk_hash_map {α : Type u} [decidable_eq α] {β : α → Type v} (hash_fn : α → nat) (nbuckets := 8) : hash_map α β := let n := if nbuckets = 0 then 8 else nbuckets in let nz : n > 0 := by abstract { cases nbuckets, {simp, tactic.comp_val}, simp [if_pos, nat.succ_ne_zero], apply nat.zero_lt_succ} in { hash_fn := hash_fn, size := 0, nbuckets := ⟨n, nz⟩, buckets := mk_array n [], is_valid := hash_map.mk_valid _ _ } namespace hash_map variables {α : Type u} {β : α → Type v} [decidable_eq α] /-- Return the value corresponding to a key, or `none` if not found -/ def find (m : hash_map α β) (a : α) : option (β a) := find_aux a (m.buckets.read m.hash_fn a) /-- Return `tt` if the key exists in the map -/ def contains (m : hash_map α β) (a : α) : bool := (m.find a).is_some instance : has_mem α (hash_map α β) := ⟨λa m, m.contains a⟩ /-- Fold a function over the key-value pairs in the map -/ def fold {δ : Type w} (m : hash_map α β) (d : δ) (f : δ → Π a, β a → δ) : δ := m.buckets.foldl d f /-- The list of key-value pairs in the map -/ def entries (m : hash_map α β) : list Σ a, β a := m.buckets.as_list /-- The list of keys in the map -/ def keys (m : hash_map α β) : list α := m.entries.map sigma.fst theorem find_iff (m : hash_map α β) (a : α) (b : β a) : m.find a = some b ↔ sigma.mk a b ∈ m.entries := m.is_valid.find_aux_iff _ theorem contains_iff (m : hash_map α β) (a : α) : m.contains a ↔ a ∈ m.keys := m.is_valid.contains_aux_iff _ _ theorem entries_empty (hash_fn : α → nat) (n) : (@mk_hash_map α _ β hash_fn n).entries = [] := by dsimp [entries, mk_hash_map]; rw mk_as_list theorem keys_empty (hash_fn : α → nat) (n) : (@mk_hash_map α _ β hash_fn n).keys = [] := by dsimp [keys]; rw entries_empty; refl theorem find_empty (hash_fn : α → nat) (n a) : (@mk_hash_map α _ β hash_fn n).find a = none := by induction h : (@mk_hash_map α _ β hash_fn n).find a; [refl, { have := (find_iff _ _ _).1 h, rw entries_empty at this, contradiction }] theorem not_contains_empty (hash_fn : α → nat) (n a) : ¬ (@mk_hash_map α _ β hash_fn n).contains a := by apply bool_iff_false.2; dsimp [contains]; rw [find_empty]; refl theorem insert_lemma (hash_fn : α → nat) {n n'} {bkts : bucket_array α β n} {sz} (v : valid hash_fn bkts sz) : valid hash_fn (bkts.foldl (mk_array _ [] : bucket_array α β n') (reinsert_aux hash_fn)) sz := begin suffices : ∀ (l : list Σ a, β a) (t : bucket_array α β n') sz, valid hash_fn t sz → ((l ++ t.as_list).map sigma.fst).nodup → valid hash_fn (l.foldl (λr (a : Σ a, β a), reinsert_aux hash_fn r a.1 a.2) t) (sz + l.length), { have p := this bkts.as_list _ _ (mk_valid _ _), rw [mk_as_list, list.append_nil, zero_add, v.len] at p, rw bucket_array.foldl_eq, exact p (v.as_list_nodup _) }, intro l, induction l with c l IH; intros t sz v nd, {exact v}, rw show sz + (c :: l).length = sz + 1 + l.length, by simp, rcases (show (l.map sigma.fst).nodup ∧ ((bucket_array.as_list t).map sigma.fst).nodup ∧ c.fst ∉ l.map sigma.fst ∧ c.fst ∉ (bucket_array.as_list t).map sigma.fst ∧ (l.map sigma.fst).disjoint ((bucket_array.as_list t).map sigma.fst), by simpa [list.nodup_append, not_or_distrib, and_comm, and.left_comm] using nd) with ⟨nd1, nd2, nm1, nm2, dj⟩, have v' := v.insert _ _ c.2 (λHc, nm2 $ (v.contains_aux_iff _ c.1).1 Hc), apply IH _ _ v', suffices : ∀ ⦃a : α⦄ (b : β a), sigma.mk a b ∈ l → ∀ (b' : β a), sigma.mk a b' ∈ (reinsert_aux hash_fn t c.1 c.2).as_list → false, { simpa [list.nodup_append, nd1, v'.as_list_nodup _, list.disjoint] }, intros a b m1 b' m2, rcases (reinsert_aux hash_fn t c.1 c.2).mem_as_list.1 m2 with ⟨i, im⟩, have : sigma.mk a b' ∉ array.read t i, { intro m3, have : a ∈ list.map sigma.fst t.as_list := list.mem_map_of_mem sigma.fst (t.mem_as_list.2 ⟨_, m3⟩), exact dj (list.mem_map_of_mem sigma.fst m1) this }, by_cases h : mk_idx n' (hash_fn c.1) = i, { subst h, have e : sigma.mk a b' = ⟨c.1, c.2⟩, { simpa [reinsert_aux, bucket_array.modify, array.read_write, this] using im }, injection e with e, subst a, exact nm1.elim (@list.mem_map_of_mem _ _ sigma.fst _ _ m1) }, { apply this, simpa [reinsert_aux, bucket_array.modify, array.read_write_of_ne _ _ h] using im } end /-- Insert a key-value pair into the map. (Modifies `m` in-place when applicable) -/ def insert : Π (m : hash_map α β) (a : α) (b : β a), hash_map α β | ⟨hash_fn, size, n, buckets, v⟩ a b := let bkt := buckets.read hash_fn a in if hc : contains_aux a bkt then { hash_fn := hash_fn, size := size, nbuckets := n, buckets := buckets.modify hash_fn a (replace_aux a b), is_valid := v.replace _ a b hc } else let size' := size + 1, buckets' := buckets.modify hash_fn a (λl, ⟨a, b⟩::l), valid' := v.insert _ a b hc in if size' ≤ n.1 then { hash_fn := hash_fn, size := size', nbuckets := n, buckets := buckets', is_valid := valid' } else let n' : ℕ+ := ⟨n.1 * 2, mul_pos n.2 dec_trivial⟩, buckets'' : bucket_array α β n' := buckets'.foldl (mk_array _ []) (reinsert_aux hash_fn) in { hash_fn := hash_fn, size := size', nbuckets := n', buckets := buckets'', is_valid := insert_lemma _ valid' } theorem mem_insert : Π (m : hash_map α β) (a b a' b'), (sigma.mk a' b' : sigma β) ∈ (m.insert a b).entries ↔ if a = a' then b == b' else sigma.mk a' b' ∈ m.entries | ⟨hash_fn, size, n, bkts, v⟩ a b a' b' := begin let bkt := bkts.read hash_fn a, have nd : (bkt.map sigma.fst).nodup := v.nodup (mk_idx n (hash_fn a)), have lem : Π (bkts' : bucket_array α β n) (v1 u w) (hl : bucket_array.as_list bkts = u ++ v1 ++ w) (hfl : bucket_array.as_list bkts' = u ++ [⟨a, b⟩] ++ w) (veq : (v1 = [] ∧ ¬ contains_aux a bkt) ∨ ∃b'', v1 = [⟨a, b''⟩]), sigma.mk a' b' ∈ bkts'.as_list ↔ if a = a' then b == b' else sigma.mk a' b' ∈ bkts.as_list, { intros bkts' v1 u w hl hfl veq, rw [hl, hfl], by_cases h : a = a', { subst a', suffices : b = b' ∨ sigma.mk a b' ∈ u ∨ sigma.mk a b' ∈ w ↔ b = b', { simpa [eq_comm, or.left_comm] }, refine or_iff_left_of_imp (not.elim $ not_or_distrib.2 _), rcases veq with ⟨rfl, Hnc⟩ | ⟨b'', rfl⟩, { have na := (not_iff_not_of_iff $ v.contains_aux_iff _ _).1 Hnc, simp [hl, not_or_distrib] at na, simp [na] }, { have nd' := v.as_list_nodup _, simp [hl, list.nodup_append] at nd', simp [nd'] } }, { suffices : sigma.mk a' b' ∉ v1, {simp [h, ne.symm h, this]}, rcases veq with ⟨rfl, Hnc⟩ | ⟨b'', rfl⟩; simp [ne.symm h] } }, by_cases Hc : (contains_aux a bkt : Prop), { rcases hash_map.valid.replace_aux a b (array.read bkts (mk_idx n (hash_fn a))) ((contains_aux_iff nd).1 Hc) with ⟨u', w', b'', hl', hfl'⟩, rcases (append_of_modify u' [⟨a, b''⟩] [⟨a, b⟩] w' hl' hfl') with ⟨u, w, hl, hfl⟩, simpa [insert, @dif_pos (contains_aux a bkt) _ Hc] using lem _ _ u w hl hfl (or.inr ⟨b'', rfl⟩) }, { let size' := size + 1, let bkts' := bkts.modify hash_fn a (λl, ⟨a, b⟩::l), have mi : sigma.mk a' b' ∈ bkts'.as_list ↔ if a = a' then b == b' else sigma.mk a' b' ∈ bkts.as_list := let ⟨u, w, hl, hfl⟩ := append_of_modify [] [] [⟨a, b⟩] _ rfl rfl in lem bkts' _ u w hl hfl $ or.inl ⟨rfl, Hc⟩, simp [insert, @dif_neg (contains_aux a bkt) _ Hc], by_cases h : size' ≤ n.1, -- TODO(Mario): Why does the by_cases assumption look different than the stated one? { simpa [show size' ≤ n.1, from h] using mi }, { let n' : ℕ+ := ⟨n.1 * 2, mul_pos n.2 dec_trivial⟩, let bkts'' : bucket_array α β n' := bkts'.foldl (mk_array _ []) (reinsert_aux hash_fn), suffices : sigma.mk a' b' ∈ bkts''.as_list ↔ sigma.mk a' b' ∈ bkts'.as_list.reverse, { simpa [show ¬ size' ≤ n.1, from h, mi] }, rw [show bkts'' = bkts'.as_list.foldl _ _, from bkts'.foldl_eq _ _, ← list.foldr_reverse], induction bkts'.as_list.reverse with a l IH, { simp [mk_as_list] }, { cases a with a'' b'', let B := l.foldr (λ (y : sigma β) (x : bucket_array α β n'), reinsert_aux hash_fn x y.1 y.2) (mk_array n'.1 []), rcases append_of_modify [] [] [⟨a'', b''⟩] _ rfl rfl with ⟨u, w, hl, hfl⟩, simp [IH.symm, or.left_comm, show B.as_list = _, from hl, show (reinsert_aux hash_fn B a'' b'').as_list = _, from hfl] } } } end theorem find_insert_eq (m : hash_map α β) (a : α) (b : β a) : (m.insert a b).find a = some b := (find_iff (m.insert a b) a b).2 $ (mem_insert m a b a b).2 $ by rw if_pos rfl theorem find_insert_ne (m : hash_map α β) (a a' : α) (b : β a) (h : a ≠ a') : (m.insert a b).find a' = m.find a' := option.eq_of_eq_some $ λb', let t := mem_insert m a b a' b' in (find_iff _ _ _).trans $ iff.trans (by rwa if_neg h at t) (find_iff _ _ _).symm theorem find_insert (m : hash_map α β) (a' a : α) (b : β a) : (m.insert a b).find a' = if h : a = a' then some (eq.rec_on h b) else m.find a' := if h : a = a' then by rw dif_pos h; exact match a', h with ._, rfl := find_insert_eq m a b end else by rw dif_neg h; exact find_insert_ne m a a' b h /-- Insert a list of key-value pairs into the map. (Modifies `m` in-place when applicable) -/ def insert_all (l : list (Σ a, β a)) (m : hash_map α β) : hash_map α β := l.foldl (λ m ⟨a, b⟩, insert m a b) m /-- Construct a hash map from a list of key-value pairs. -/ def of_list (l : list (Σ a, β a)) (hash_fn) : hash_map α β := insert_all l (mk_hash_map hash_fn (2 * l.length)) /-- Remove a key from the map. (Modifies `m` in-place when applicable) -/ def erase (m : hash_map α β) (a : α) : hash_map α β := match m with ⟨hash_fn, size, n, buckets, v⟩ := if hc : contains_aux a (buckets.read hash_fn a) then { hash_fn := hash_fn, size := size - 1, nbuckets := n, buckets := buckets.modify hash_fn a (erase_aux a), is_valid := v.erase _ a hc } else m end theorem mem_erase : Π (m : hash_map α β) (a a' b'), (sigma.mk a' b' : sigma β) ∈ (m.erase a).entries ↔ a ≠ a' ∧ sigma.mk a' b' ∈ m.entries | ⟨hash_fn, size, n, bkts, v⟩ a a' b' := begin let bkt := bkts.read hash_fn a, by_cases Hc : (contains_aux a bkt : Prop), { let bkts' := bkts.modify hash_fn a (erase_aux a), suffices : sigma.mk a' b' ∈ bkts'.as_list ↔ a ≠ a' ∧ sigma.mk a' b' ∈ bkts.as_list, { simpa [erase, @dif_pos (contains_aux a bkt) _ Hc] }, have nd := v.nodup (mk_idx n (hash_fn a)), rcases valid.erase_aux a bkt ((contains_aux_iff nd).1 Hc) with ⟨u', w', b, hl', hfl'⟩, rcases append_of_modify u' [⟨a, b⟩] [] _ hl' hfl' with ⟨u, w, hl, hfl⟩, suffices : ∀_:sigma.mk a' b' ∈ u ∨ sigma.mk a' b' ∈ w, a ≠ a', { have : sigma.mk a' b' ∈ u ∨ sigma.mk a' b' ∈ w ↔ (¬a = a' ∧ a' = a) ∧ b' == b ∨ ¬a = a' ∧ (sigma.mk a' b' ∈ u ∨ sigma.mk a' b' ∈ w), { simp [eq_comm, not_and_self_iff, and_iff_right_of_imp this] }, simpa [hl, show bkts'.as_list = _, from hfl, and_or_distrib_left, and_comm, and.left_comm, or.left_comm] }, intros m e, subst a', revert m, apply not_or_distrib.2, have nd' := v.as_list_nodup _, simp [hl, list.nodup_append] at nd', simp [nd'] }, { suffices : ∀_:sigma.mk a' b' ∈ bucket_array.as_list bkts, a ≠ a', { simp [erase, @dif_neg (contains_aux a bkt) _ Hc, entries, and_iff_right_of_imp this] }, intros m e, subst a', exact Hc ((v.contains_aux_iff _ _).2 (list.mem_map_of_mem sigma.fst m)) } end theorem find_erase_eq (m : hash_map α β) (a : α) : (m.erase a).find a = none := begin cases h : (m.erase a).find a with b, {refl}, exact absurd rfl ((mem_erase m a a b).1 ((find_iff (m.erase a) a b).1 h)).left end theorem find_erase_ne (m : hash_map α β) (a a' : α) (h : a ≠ a') : (m.erase a).find a' = m.find a' := option.eq_of_eq_some $ λb', (find_iff _ _ _).trans $ (mem_erase m a a' b').trans $ (and_iff_right h).trans (find_iff _ _ _).symm theorem find_erase (m : hash_map α β) (a' a : α) : (m.erase a).find a' = if a = a' then none else m.find a' := if h : a = a' then by subst a'; simp [find_erase_eq m a] else by rw if_neg h; exact find_erase_ne m a a' h section string variables [has_to_string α] [∀ a, has_to_string (β a)] open prod private def key_data_to_string (a : α) (b : β a) (first : bool) : string := (if first then "" else ", ") ++ sformat!"{a} ← {b}" private def to_string (m : hash_map α β) : string := "⟨" ++ (fst (fold m ("", tt) (λ p a b, (fst p ++ key_data_to_string a b (snd p), ff)))) ++ "⟩" instance : has_to_string (hash_map α β) := ⟨to_string⟩ end string section format open format prod variables [has_to_format α] [∀ a, has_to_format (β a)] private meta def format_key_data (a : α) (b : β a) (first : bool) : format := (if first then to_fmt "" else to_fmt "," ++ line) ++ to_fmt a ++ space ++ to_fmt "←" ++ space ++ to_fmt b private meta def to_format (m : hash_map α β) : format := group $ to_fmt "⟨" ++ nest 1 (fst (fold m (to_fmt "", tt) (λ p a b, (fst p ++ format_key_data a b (snd p), ff)))) ++ to_fmt "⟩" meta instance : has_to_format (hash_map α β) := ⟨to_format⟩ end format end hash_map
9447a4166f3174cea4d58ce591ccaeb6800347dc
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/data/nat/bitwise.lean
e16ac1883cf3837360e993d71acdd7ab52264eda
[ "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
8,551
lean
/- Copyright (c) 2020 Markus Himmel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Markus Himmel -/ import tactic.linarith /-! # Bitwise operations on natural numbers In the first half of this file, we provide theorems for reasoning about natural numbers from their bitwise properties. In the second half of this file, we show properties of the bitwise operations `lor`, `land` and `lxor`, which are defined in core. ## Main results * `eq_of_test_bit_eq`: two natural numbers are equal if they have equal bits at every position. * `exists_most_significant_bit`: if `n ≠ 0`, then there is some position `i` that contains the most significant `1`-bit of `n`. * `lt_of_test_bit`: if `n` and `m` are numbers and `i` is a position such that the `i`-th bit of of `n` is zero, the `i`-th bit of `m` is one, and all more significant bits are equal, then `n < m`. ## Future work There is another way to express bitwise properties of natural number: `digits 2`. The two ways should be connected. ## Keywords bitwise, and, or, xor -/ open function namespace nat @[simp] lemma bit_ff : bit ff = bit0 := rfl @[simp] lemma bit_tt : bit tt = bit1 := rfl @[simp] lemma bit_eq_zero {n : ℕ} {b : bool} : n.bit b = 0 ↔ n = 0 ∧ b = ff := by { cases b; norm_num [bit0_eq_zero, nat.bit1_ne_zero] } lemma zero_of_test_bit_eq_ff {n : ℕ} (h : ∀ i, test_bit n i = ff) : n = 0 := begin induction n using nat.binary_rec with b n hn, { refl }, { have : b = ff := by simpa using h 0, rw [this, bit_ff, bit0_val, hn (λ i, by rw [←h (i + 1), test_bit_succ]), mul_zero] } end @[simp] lemma zero_test_bit (i : ℕ) : test_bit 0 i = ff := by simp [test_bit] /-- Bitwise extensionality: Two numbers agree if they agree at every bit position. -/ lemma eq_of_test_bit_eq {n m : ℕ} (h : ∀ i, test_bit n i = test_bit m i) : n = m := begin induction n using nat.binary_rec with b n hn generalizing m, { simp only [zero_test_bit] at h, exact (zero_of_test_bit_eq_ff (λ i, (h i).symm)).symm }, induction m using nat.binary_rec with b' m hm, { simp only [zero_test_bit] at h, exact zero_of_test_bit_eq_ff h }, suffices h' : n = m, { rw [h', show b = b', by simpa using h 0] }, exact hn (λ i, by convert h (i + 1) using 1; rw test_bit_succ) end lemma exists_most_significant_bit {n : ℕ} (h : n ≠ 0) : ∃ i, test_bit n i = tt ∧ ∀ j, i < j → test_bit n j = ff := begin induction n using nat.binary_rec with b n hn, { exact false.elim (h rfl) }, by_cases h' : n = 0, { subst h', rw (show b = tt, by { revert h, cases b; simp }), refine ⟨0, ⟨by rw [test_bit_zero], λ j hj, _⟩⟩, obtain ⟨j', rfl⟩ := exists_eq_succ_of_ne_zero (ne_of_gt hj), rw [test_bit_succ, zero_test_bit] }, { obtain ⟨k, ⟨hk, hk'⟩⟩ := hn h', refine ⟨k + 1, ⟨by rw [test_bit_succ, hk], λ j hj, _⟩⟩, obtain ⟨j', rfl⟩ := exists_eq_succ_of_ne_zero (show j ≠ 0, by linarith), exact (test_bit_succ _ _ _).trans (hk' _ (lt_of_succ_lt_succ hj)) } end lemma lt_of_test_bit {n m : ℕ} (i : ℕ) (hn : test_bit n i = ff) (hm : test_bit m i = tt) (hnm : ∀ j, i < j → test_bit n j = test_bit m j) : n < m := begin induction n using nat.binary_rec with b n hn' generalizing i m, { contrapose! hm, rw le_zero_iff at hm, simp [hm] }, induction m using nat.binary_rec with b' m hm' generalizing i, { exact false.elim (bool.ff_ne_tt ((zero_test_bit i).symm.trans hm)) }, by_cases hi : i = 0, { subst hi, simp only [test_bit_zero] at hn hm, have : n = m, { exact eq_of_test_bit_eq (λ i, by convert hnm (i + 1) dec_trivial using 1; rw test_bit_succ) }, rw [hn, hm, this, bit_ff, bit_tt, bit0_val, bit1_val], exact lt_add_one _ }, { obtain ⟨i', rfl⟩ := exists_eq_succ_of_ne_zero hi, simp only [test_bit_succ] at hn hm, have := hn' _ hn hm (λ j hj, by convert hnm j.succ (succ_lt_succ hj) using 1; rw test_bit_succ), cases b; cases b'; simp only [bit_ff, bit_tt, bit0_val n, bit1_val n, bit0_val m, bit1_val m]; linarith } end @[simp] lemma test_bit_two_pow_self (n : ℕ) : test_bit (2 ^ n) n = tt := by rw [test_bit, shiftr_eq_div_pow, nat.div_self (pow_pos zero_lt_two n), bodd_one] lemma test_bit_two_pow_of_ne {n m : ℕ} (hm : n ≠ m) : test_bit (2 ^ n) m = ff := begin rw [test_bit, shiftr_eq_div_pow], cases hm.lt_or_lt with hm hm, { rw [nat.div_eq_zero, bodd_zero], exact nat.pow_lt_pow_of_lt_right one_lt_two hm }, { rw [pow_div hm.le zero_lt_two, ←nat.sub_add_cancel (nat.sub_pos_of_lt hm), pow_succ], simp } end lemma test_bit_two_pow (n m : ℕ) : test_bit (2 ^ n) m = (n = m) := begin by_cases n = m, { cases h, simp }, { rw test_bit_two_pow_of_ne h, simp [h] } end /-- If `f` is a commutative operation on bools such that `f ff ff = ff`, then `bitwise f` is also commutative. -/ lemma bitwise_comm {f : bool → bool → bool} (hf : ∀ b b', f b b' = f b' b) (hf' : f ff ff = ff) (n m : ℕ) : bitwise f n m = bitwise f m n := suffices bitwise f = swap (bitwise f), by conv_lhs { rw this }, calc bitwise f = bitwise (swap f) : congr_arg _ $ funext $ λ _, funext $ hf _ ... = swap (bitwise f) : bitwise_swap hf' lemma lor_comm (n m : ℕ) : lor n m = lor m n := bitwise_comm bool.bor_comm rfl n m lemma land_comm (n m : ℕ) : land n m = land m n := bitwise_comm bool.band_comm rfl n m lemma lxor_comm (n m : ℕ) : lxor n m = lxor m n := bitwise_comm bool.bxor_comm rfl n m @[simp] lemma zero_lxor (n : ℕ) : lxor 0 n = n := by simp [lxor] @[simp] lemma lxor_zero (n : ℕ) : lxor n 0 = n := by simp [lxor] @[simp] lemma zero_land (n : ℕ) : land 0 n = 0 := by simp [land] @[simp] lemma land_zero (n : ℕ) : land n 0 = 0 := by simp [land] @[simp] lemma zero_lor (n : ℕ) : lor 0 n = n := by simp [lor] @[simp] lemma lor_zero (n : ℕ) : lor n 0 = n := by simp [lor] /-- Proving associativity of bitwise operations in general essentially boils down to a huge case distinction, so it is shorter to use this tactic instead of proving it in the general case. -/ meta def bitwise_assoc_tac : tactic unit := `[induction n using nat.binary_rec with b n hn generalizing m k, { simp }, induction m using nat.binary_rec with b' m hm, { simp }, induction k using nat.binary_rec with b'' k hk; simp [hn]] lemma lxor_assoc (n m k : ℕ) : lxor (lxor n m) k = lxor n (lxor m k) := by bitwise_assoc_tac lemma land_assoc (n m k : ℕ) : land (land n m) k = land n (land m k) := by bitwise_assoc_tac lemma lor_assoc (n m k : ℕ) : lor (lor n m) k = lor n (lor m k) := by bitwise_assoc_tac @[simp] lemma lxor_self (n : ℕ) : lxor n n = 0 := zero_of_test_bit_eq_ff $ λ i, by simp lemma lxor_right_inj {n m m' : ℕ} (h : lxor n m = lxor n m') : m = m' := calc m = lxor n (lxor n m') : by simp [←lxor_assoc, ←h] ... = m' : by simp [←lxor_assoc] lemma lxor_left_inj {n n' m : ℕ} (h : lxor n m = lxor n' m) : n = n' := by { rw [lxor_comm n m, lxor_comm n' m] at h, exact lxor_right_inj h } lemma lxor_eq_zero {n m : ℕ} : lxor n m = 0 ↔ n = m := ⟨by { rw ←lxor_self m, exact lxor_left_inj }, by { rintro rfl, exact lxor_self _ }⟩ lemma lxor_trichotomy {a b c : ℕ} (h : lxor a (lxor b c) ≠ 0) : lxor b c < a ∨ lxor a c < b ∨ lxor a b < c := begin set v := lxor a (lxor b c) with hv, -- The xor of any two of `a`, `b`, `c` is the xor of `v` and the third. have hab : lxor a b = lxor c v, { rw hv, conv_rhs { rw lxor_comm, simp [lxor_assoc] } }, have hac : lxor a c = lxor b v, { rw hv, conv_rhs { congr, skip, rw lxor_comm }, rw [←lxor_assoc, ←lxor_assoc, lxor_self, zero_lxor, lxor_comm] }, have hbc : lxor b c = lxor a v, { simp [hv, ←lxor_assoc] }, -- If `i` is the position of the most significant bit of `v`, then at least one of `a`, `b`, `c` -- has a one bit at position `i`. obtain ⟨i, ⟨hi, hi'⟩⟩ := exists_most_significant_bit h, have : test_bit a i = tt ∨ test_bit b i = tt ∨ test_bit c i = tt, { contrapose! hi, simp only [eq_ff_eq_not_eq_tt, ne, test_bit_lxor] at ⊢ hi, rw [hi.1, hi.2.1, hi.2.2, bxor_ff, bxor_ff] }, -- If, say, `a` has a one bit at position `i`, then `a xor v` has a zero bit at position `i`, but -- the same bits as `a` in positions greater than `j`, so `a xor v < a`. rcases this with h|h|h; [{ left, rw hbc }, { right, left, rw hac }, { right, right, rw hab }]; exact lt_of_test_bit i (by simp [h, hi]) h (λ j hj, by simp [hi' _ hj]) end end nat
551dffb3536d0890ee3797e5df54788d4941bcc7
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/number_theory/primorial_auto.lean
5450e6ff6650518636a78253e54f742d1d8a93be
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
1,464
lean
/- Copyright (c) 2020 Patrick Stevens. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Stevens -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.tactic.ring_exp import Mathlib.data.nat.parity import Mathlib.data.nat.choose.sum import Mathlib.PostPort namespace Mathlib /-! # Primorial This file defines the primorial function (the product of primes less than or equal to some bound), and proves that `primorial n ≤ 4 ^ n`. ## Notations We use the local notation `n#` for the primorial of `n`: that is, the product of the primes less than or equal to `n`. -/ /-- The primorial `n#` of `n` is the product of the primes less than or equal to `n`. -/ def primorial (n : ℕ) : ℕ := finset.prod (finset.filter nat.prime (finset.range (n + 1))) fun (p : ℕ) => p theorem primorial_succ {n : ℕ} (n_big : 1 < n) (r : n % bit0 1 = 1) : primorial (n + 1) = primorial n := sorry theorem dvd_choose_of_middling_prime (p : ℕ) (is_prime : nat.prime p) (m : ℕ) (p_big : m + 1 < p) (p_small : p ≤ bit0 1 * m + 1) : p ∣ nat.choose (bit0 1 * m + 1) (m + 1) := sorry theorem prod_primes_dvd {s : finset ℕ} (n : ℕ) (h : ∀ (a : ℕ), a ∈ s → nat.prime a) (div : ∀ (a : ℕ), a ∈ s → a ∣ n) : (finset.prod s fun (p : ℕ) => p) ∣ n := sorry theorem primorial_le_4_pow (n : ℕ) : primorial n ≤ bit0 (bit0 1) ^ n := sorry end Mathlib
e432fe5f642ee8563a88be69adc876cc1ca5c761
de91c42b87530c3bdcc2b138ef1a3c3d9bee0d41
/old/expressions/classical_time.lean
05f579f0d909f3b1094c46444367583aa249ece5
[]
no_license
kevinsullivan/lang
d3e526ba363dc1ddf5ff1c2f36607d7f891806a7
e9d869bff94fb13ad9262222a6f3c4aafba82d5e
refs/heads/master
1,687,840,064,795
1,628,047,969,000
1,628,047,969,000
282,210,749
0
1
null
1,608,153,830,000
1,595,592,637,000
Lean
UTF-8
Lean
false
false
3,441
lean
import ...phys.src.classical_time import .expression namespace lang.classicalTime structure spaceVar extends var def myvar : spaceVar := ⟨⟨2⟩⟩ def p : spaceVar := ⟨⟨1⟩⟩ inductive spaceExpr | lit (v : classicalTime) | var (v : spaceVar) abbreviation spaceEnv := spaceVar → classicalTime abbreviation spaceEval := spaceExpr → spaceEnv → classicalTime structure frameVar extends var inductive frameExpr | lit (f : classicalTimeFrame) | var (v : frameVar) abbreviation frameEnv := frameVar → classicalTimeFrame abbreviation frameEval := frameExpr → frameEnv → classicalTimeFrame structure transformVar extends var inductive transformExpr | lit (t : classicalTimeTransform) | var (v : transformVar) abbreviation transformEnv := transformVar → classicalTimeTransform abbreviation transformEval := transformExpr → transformEnv → classicalTimeTransform structure ScalarVar extends var inductive ScalarExpr | lit (f : classicalTimeScalar) | var (v : ScalarVar) abbreviation scalarEnv := ScalarVar → classicalTimeScalar abbreviation scalarEval := ScalarExpr → scalarEnv → classicalTimeScalar --abbreviation structure CoordinateVectorVar extends var inductive CoordinateVectorExpr | lit (f : classicalTimeCoordinateVector) | var (v : CoordinateVectorVar) abbreviation CoordinateVectorEnv := CoordinateVectorVar → classicalTimeCoordinateVector abbreviation CoordinateVectorEval := CoordinateVectorExpr → CoordinateVectorEnv → classicalTimeCoordinateVector structure CoordinatePointVar extends var inductive CoordinatePointExpr | lit (f : classicalTimeCoordinatePoint) | var(v : CoordinatePointVar ) abbreviation pointEnv := CoordinatePointVar → classicalTimeCoordinatePoint abbreviation pointEval := CoordinatePointExpr → pointEnv → classicalTimeCoordinatePoint def spaceVarEq : spaceVar → spaceVar → bool | v1 v2 := v1.num=v2.num def CoordinateVectorVarEq : CoordinateVectorVar → CoordinateVectorVar → bool | v1 v2 := v1.num=v2.num def pointVarEq : CoordinatePointVar → CoordinatePointVar → bool | v1 v2 := v1.num=v2.num def frameVarEq : frameVar → frameVar → bool | v1 v2 := v1.num=v2.num def transformVarEq : transformVar → transformVar → bool | v1 v2 := v1.num=v2.num def scalarVarEq : ScalarVar → ScalarVar → bool | v1 v2 := v1.num=v2.num structure env : Type := (sp : spaceEnv) (fr : frameEnv ) (tr : transformEnv) (vec : CoordinateVectorEnv) (pt : pointEnv) (s : scalarEnv) noncomputable def initSp := λ v : spaceVar, classicalTime.build 9999 noncomputable def initFr := λ v : frameVar, classicalTime.stdFrame (initSp ⟨⟨9999⟩⟩) noncomputable def initVec := λ v : CoordinateVectorVar, classicalTimeCoordinateVector.build (initSp ⟨⟨9999⟩⟩) (initFr ⟨⟨9999⟩⟩) ⟨[1], by refl⟩ noncomputable def initPt := λ v : CoordinatePointVar, classicalTimeCoordinatePoint.build (initSp ⟨⟨9999⟩⟩) (initFr ⟨⟨9999⟩⟩) ⟨[1], by refl⟩ noncomputable def initScalar := λ v : ScalarVar, classicalTimeScalar.build (initSp ⟨⟨9999⟩⟩) ⟨[1],rfl⟩ noncomputable def initTransform := λ v : transformVar, classicalTimeTransform.mk (initSp ⟨⟨9999⟩⟩) (initFr ⟨⟨9999⟩⟩) (initFr ⟨⟨9999⟩⟩) noncomputable def initEnv : env := ⟨initSp, initFr, initTransform, initVec, initPt, initScalar⟩ end lang.classicalTime
f0e77e948034c4222eaa6747c3e06c4212c8981f
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/linear_algebra/matrix/basis.lean
195d49b6c7cc61cbe1e242189a2e2d088bb62150
[ "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
9,535
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, Patrick Massot, Casper Putz, Anne Baanen -/ import linear_algebra.matrix.reindex import linear_algebra.matrix.to_lin /-! # Bases and matrices This file defines the map `basis.to_matrix` that sends a family of vectors to the matrix of their coordinates with respect to some basis. ## Main definitions * `basis.to_matrix e v` is the matrix whose `i, j`th entry is `e.repr (v j) i` * `basis.to_matrix_equiv` is `basis.to_matrix` bundled as a linear equiv ## Main results * `linear_map.to_matrix_id_eq_basis_to_matrix`: `linear_map.to_matrix b c id` is equal to `basis.to_matrix b c` * `basis.to_matrix_mul_to_matrix`: multiplying `basis.to_matrix` with another `basis.to_matrix` gives a `basis.to_matrix` ## Tags matrix, basis -/ noncomputable theory open linear_map matrix set submodule open_locale big_operators open_locale matrix section basis_to_matrix variables {ι ι' κ κ' : Type*} variables {R M : Type*} [comm_semiring R] [add_comm_monoid M] [module R M] variables {R₂ M₂ : Type*} [comm_ring R₂] [add_comm_group M₂] [module R₂ M₂] open function matrix /-- From a basis `e : ι → M` and a family of vectors `v : ι' → M`, make the matrix whose columns are the vectors `v i` written in the basis `e`. -/ def basis.to_matrix (e : basis ι R M) (v : ι' → M) : matrix ι ι' R := λ i j, e.repr (v j) i variables (e : basis ι R M) (v : ι' → M) (i : ι) (j : ι') namespace basis lemma to_matrix_apply : e.to_matrix v i j = e.repr (v j) i := rfl lemma to_matrix_transpose_apply : (e.to_matrix v)ᵀ j = e.repr (v j) := funext $ (λ _, rfl) lemma to_matrix_eq_to_matrix_constr [fintype ι] [decidable_eq ι] (v : ι → M) : e.to_matrix v = linear_map.to_matrix e e (e.constr ℕ v) := by { ext, rw [basis.to_matrix_apply, linear_map.to_matrix_apply, basis.constr_basis] } -- TODO (maybe) Adjust the definition of `basis.to_matrix` to eliminate the transpose. lemma coe_pi_basis_fun.to_matrix_eq_transpose [fintype ι] : ((pi.basis_fun R ι).to_matrix : matrix ι ι R → matrix ι ι R) = matrix.transpose := by { ext M i j, refl, } @[simp] lemma to_matrix_self [decidable_eq ι] : e.to_matrix e = 1 := begin rw basis.to_matrix, ext i j, simp [basis.equiv_fun, matrix.one_apply, finsupp.single, eq_comm] end lemma to_matrix_update [decidable_eq ι'] (x : M) : e.to_matrix (function.update v j x) = matrix.update_column (e.to_matrix v) j (e.repr x) := begin ext i' k, rw [basis.to_matrix, matrix.update_column_apply, e.to_matrix_apply], split_ifs, { rw [h, update_same j x v] }, { rw update_noteq h }, end /-- The basis constructed by `units_smul` has vectors given by a diagonal matrix. -/ @[simp] lemma to_matrix_units_smul [decidable_eq ι] (e : basis ι R₂ M₂) (w : ι → R₂ˣ) : e.to_matrix (e.units_smul w) = diagonal (coe ∘ w) := begin ext i j, by_cases h : i = j, { simp [h, to_matrix_apply, units_smul_apply, units.smul_def] }, { simp [h, to_matrix_apply, units_smul_apply, units.smul_def, ne.symm h] } end /-- The basis constructed by `is_unit_smul` has vectors given by a diagonal matrix. -/ @[simp] lemma to_matrix_is_unit_smul [decidable_eq ι] (e : basis ι R₂ M₂) {w : ι → R₂} (hw : ∀ i, is_unit (w i)) : e.to_matrix (e.is_unit_smul hw) = diagonal w := e.to_matrix_units_smul _ @[simp] lemma sum_to_matrix_smul_self [fintype ι] : ∑ (i : ι), e.to_matrix v i j • e i = v j := by simp_rw [e.to_matrix_apply, e.sum_repr] lemma to_matrix_map_vec_mul {S : Type*} [ring S] [algebra R S] [fintype ι] (b : basis ι R S) (v : ι' → S) : ((b.to_matrix v).map $ algebra_map R S).vec_mul b = v := begin ext i, simp_rw [vec_mul, dot_product, matrix.map_apply, ← algebra.commutes, ← algebra.smul_def, sum_to_matrix_smul_self], end @[simp] lemma to_lin_to_matrix [fintype ι] [fintype ι'] [decidable_eq ι'] (v : basis ι' R M) : matrix.to_lin v e (e.to_matrix v) = id := v.ext (λ i, by rw [to_lin_self, id_apply, e.sum_to_matrix_smul_self]) /-- From a basis `e : ι → M`, build a linear equivalence between families of vectors `v : ι → M`, and matrices, making the matrix whose columns are the vectors `v i` written in the basis `e`. -/ def to_matrix_equiv [fintype ι] (e : basis ι R M) : (ι → M) ≃ₗ[R] matrix ι ι R := { to_fun := e.to_matrix, map_add' := λ v w, begin ext i j, change _ = _ + _, rw [e.to_matrix_apply, pi.add_apply, linear_equiv.map_add], refl end, map_smul' := begin intros c v, ext i j, rw [e.to_matrix_apply, pi.smul_apply, linear_equiv.map_smul], refl end, inv_fun := λ m j, ∑ i, (m i j) • e i, left_inv := begin intro v, ext j, exact e.sum_to_matrix_smul_self v j end, right_inv := begin intros m, ext k l, simp only [e.to_matrix_apply, ← e.equiv_fun_apply, ← e.equiv_fun_symm_apply, linear_equiv.apply_symm_apply], end } end basis section mul_linear_map_to_matrix variables {N : Type*} [add_comm_monoid N] [module R N] variables (b : basis ι R M) (b' : basis ι' R M) (c : basis κ R N) (c' : basis κ' R N) variables (f : M →ₗ[R] N) open linear_map section fintype variables [fintype ι'] [fintype κ] [fintype κ'] @[simp] lemma basis_to_matrix_mul_linear_map_to_matrix [decidable_eq ι'] : c.to_matrix c' ⬝ linear_map.to_matrix b' c' f = linear_map.to_matrix b' c f := (matrix.to_lin b' c).injective (by haveI := classical.dec_eq κ'; rw [to_lin_to_matrix, to_lin_mul b' c' c, to_lin_to_matrix, c.to_lin_to_matrix, id_comp]) variable [fintype ι] @[simp] lemma linear_map_to_matrix_mul_basis_to_matrix [decidable_eq ι] [decidable_eq ι'] : linear_map.to_matrix b' c' f ⬝ b'.to_matrix b = linear_map.to_matrix b c' f := (matrix.to_lin b c').injective (by rw [to_lin_to_matrix, to_lin_mul b b' c', to_lin_to_matrix, b'.to_lin_to_matrix, comp_id]) lemma basis_to_matrix_mul_linear_map_to_matrix_mul_basis_to_matrix [decidable_eq ι] [decidable_eq ι'] : c.to_matrix c' ⬝ linear_map.to_matrix b' c' f ⬝ b'.to_matrix b = linear_map.to_matrix b c f := by rw [basis_to_matrix_mul_linear_map_to_matrix, linear_map_to_matrix_mul_basis_to_matrix] lemma basis_to_matrix_mul [decidable_eq κ] (b₁ : basis ι R M) (b₂ : basis ι' R M) (b₃ : basis κ R N) (A : matrix ι' κ R) : b₁.to_matrix b₂ ⬝ A = linear_map.to_matrix b₃ b₁ (to_lin b₃ b₂ A) := begin have := basis_to_matrix_mul_linear_map_to_matrix b₃ b₁ b₂ (matrix.to_lin b₃ b₂ A), rwa [linear_map.to_matrix_to_lin] at this end lemma mul_basis_to_matrix [decidable_eq ι] [decidable_eq ι'] (b₁ : basis ι R M) (b₂ : basis ι' R M) (b₃ : basis κ R N) (A : matrix κ ι R) : A ⬝ b₁.to_matrix b₂ = linear_map.to_matrix b₂ b₃ (to_lin b₁ b₃ A) := begin have := linear_map_to_matrix_mul_basis_to_matrix b₂ b₁ b₃ (matrix.to_lin b₁ b₃ A), rwa [linear_map.to_matrix_to_lin] at this end lemma basis_to_matrix_basis_fun_mul (b : basis ι R (ι → R)) (A : matrix ι ι R) : b.to_matrix (pi.basis_fun R ι) ⬝ A = of (λ i j, b.repr (Aᵀ j) i) := begin classical, simp only [basis_to_matrix_mul _ _ (pi.basis_fun R ι), matrix.to_lin_eq_to_lin'], ext i j, rw [linear_map.to_matrix_apply, matrix.to_lin'_apply, pi.basis_fun_apply, matrix.mul_vec_std_basis_apply, matrix.of_apply] end /-- A generalization of `linear_map.to_matrix_id`. -/ @[simp] lemma linear_map.to_matrix_id_eq_basis_to_matrix [decidable_eq ι] : linear_map.to_matrix b b' id = b'.to_matrix b := by { haveI := classical.dec_eq ι', rw [←@basis_to_matrix_mul_linear_map_to_matrix _ _ ι, to_matrix_id, matrix.mul_one] } /-- See also `basis.to_matrix_reindex` which gives the `simp` normal form of this result. -/ lemma basis.to_matrix_reindex' [decidable_eq ι] [decidable_eq ι'] (b : basis ι R M) (v : ι' → M) (e : ι ≃ ι') : (b.reindex e).to_matrix v = matrix.reindex_alg_equiv _ e (b.to_matrix (v ∘ e)) := by { ext, simp only [basis.to_matrix_apply, basis.reindex_repr, matrix.reindex_alg_equiv_apply, matrix.reindex_apply, matrix.submatrix_apply, function.comp_app, e.apply_symm_apply] } end fintype /-- A generalization of `basis.to_matrix_self`, in the opposite direction. -/ @[simp] lemma basis.to_matrix_mul_to_matrix {ι'' : Type*} [fintype ι'] (b'' : ι'' → M) : b.to_matrix b' ⬝ b'.to_matrix b'' = b.to_matrix b'' := begin have := classical.dec_eq ι, have := classical.dec_eq ι', haveI := classical.dec_eq ι'', ext i j, simp only [matrix.mul_apply, basis.to_matrix_apply, basis.sum_repr_mul_repr], end /-- `b.to_matrix b'` and `b'.to_matrix b` are inverses. -/ lemma basis.to_matrix_mul_to_matrix_flip [decidable_eq ι] [fintype ι'] : b.to_matrix b' ⬝ b'.to_matrix b = 1 := by rw [basis.to_matrix_mul_to_matrix, basis.to_matrix_self] @[simp] lemma basis.to_matrix_reindex (b : basis ι R M) (v : ι' → M) (e : ι ≃ ι') : (b.reindex e).to_matrix v = (b.to_matrix v).submatrix e.symm id := by { ext, simp only [basis.to_matrix_apply, basis.reindex_repr, matrix.submatrix_apply, id.def] } @[simp] lemma basis.to_matrix_map (b : basis ι R M) (f : M ≃ₗ[R] N) (v : ι → N) : (b.map f).to_matrix v = b.to_matrix (f.symm ∘ v) := by { ext, simp only [basis.to_matrix_apply, basis.map, linear_equiv.trans_apply] } end mul_linear_map_to_matrix end basis_to_matrix
45367cadd55b25cfa2ae569757a64d8f1f817113
4727251e0cd73359b15b664c3170e5d754078599
/src/number_theory/prime_counting.lean
1054b077f7add7f163eeacce5cffbab97fddedd7
[ "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,359
lean
/- Copyright (c) 2021 Bolton Bailey. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bolton Bailey -/ import data.nat.prime import data.nat.totient import algebra.periodic import data.finset.locally_finite import data.nat.count import data.nat.nth /-! # The Prime Counting Function In this file we define the prime counting function: the function on natural numbers that returns the number of primes less than or equal to its input. ## Main Results The main definitions for this file are - `nat.prime_counting`: The prime counting function π - `nat.prime_counting'`: π(n - 1) We then prove that these are monotone in `nat.monotone_prime_counting` and `nat.monotone_prime_counting'`. The last main theorem `nat.prime_counting'_add_le` is an upper bound on `π'` which arises by observing that all numbers greater than `k` and not coprime to `k` are not prime, and so only at most `φ(k)/k` fraction of the numbers from `k` to `n` are prime. ## Notation We use the standard notation `π` to represent the prime counting function (and `π'` to represent the reindexed version). -/ namespace nat open finset /-- A variant of the traditional prime counting function which gives the number of primes *strictly* less than the input. More convenient for avoiding off-by-one errors. -/ def prime_counting' : ℕ → ℕ := nat.count prime /-- The prime counting function: Returns the number of primes less than or equal to the input. -/ def prime_counting (n : ℕ) : ℕ := prime_counting' (n + 1) localized "notation `π` := nat.prime_counting" in nat localized "notation `π'` := nat.prime_counting'" in nat lemma monotone_prime_counting' : monotone prime_counting' := count_monotone prime lemma monotone_prime_counting : monotone prime_counting := λ a b a_le_b, monotone_prime_counting' (add_le_add_right a_le_b 1) @[simp] lemma prime_counting'_nth_eq (n : ℕ) : π' (nth prime n) = n := count_nth_of_infinite _ infinite_set_of_prime _ @[simp] lemma prime_nth_prime (n : ℕ) : prime (nth prime n) := nth_mem_of_infinite _ infinite_set_of_prime _ /-- A linear upper bound on the size of the `prime_counting'` function -/ lemma prime_counting'_add_le {a k : ℕ} (h0 : 0 < a) (h1 : a < k) (n : ℕ) : π' (k + n) ≤ π' k + nat.totient a * (n / a + 1) := calc π' (k + n) ≤ ((range k).filter (prime)).card + ((Ico k (k + n)).filter (prime)).card : begin rw [prime_counting', count_eq_card_filter_range, range_eq_Ico, ←Ico_union_Ico_eq_Ico (zero_le k) (le_self_add), filter_union], apply card_union_le, end ... ≤ π' k + ((Ico k (k + n)).filter (prime)).card : by rw [prime_counting', count_eq_card_filter_range] ... ≤ π' k + ((Ico k (k + n)).filter (coprime a)).card : begin refine add_le_add_left (card_le_of_subset _) k.prime_counting', simp only [subset_iff, and_imp, mem_filter, mem_Ico], intros p succ_k_le_p p_lt_n p_prime, split, { exact ⟨succ_k_le_p, p_lt_n⟩, }, { rw coprime_comm, exact coprime_of_lt_prime h0 (gt_of_ge_of_gt succ_k_le_p h1) p_prime, }, end ... ≤ π' k + totient a * (n / a + 1) : begin rw [add_le_add_iff_left], exact Ico_filter_coprime_le k n h0, end end nat
d149bcd991f5211c7763242da57e35ed5700369f
b7f22e51856f4989b970961f794f1c435f9b8f78
/library/data/nat/bigops.lean
2aa1ab72e8fd912cc42bea742a5a7a26a88ffac0
[ "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
7,384
lean
/- Copyright (c) 2015 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jeremy Avigad Finite sums and products over intervals of natural numbers. -/ import data.nat.order algebra.group_bigops algebra.interval namespace nat /- sums -/ section add_monoid variables {A : Type} [add_monoid A] definition sum_up_to (n : ℕ) (f : ℕ → A) : A := nat.rec_on n 0 (λ n a, a + f n) notation `∑` binders ` < ` n `, ` r:(scoped f, sum_up_to n f) := r proposition sum_up_to_zero (f : ℕ → A) : (∑ i < 0, f i) = 0 := rfl proposition sum_up_to_succ (n : ℕ) (f : ℕ → A) : (∑ i < succ n, f i) = (∑ i < n, f i) + f n := rfl proposition sum_up_to_one (f : ℕ → A) : (∑ i < 1, f i) = f 0 := zero_add (f 0) definition sum_range (m n : ℕ) (f : ℕ → A) : A := sum_up_to (succ n - m) (λ i, f (i + m)) notation `∑` binders `=` m `...` n `, ` r:(scoped f, sum_range m n f) := r proposition sum_range_def (m n : ℕ) (f : ℕ → A) : (∑ i = m...n, f i) = (∑ i < (succ n - m), f (i + m)) := rfl proposition sum_range_self (m : ℕ) (f : ℕ → A) : (∑ i = m...m, f i) = f m := by krewrite [↑sum_range, succ_sub !le.refl, nat.sub_self, sum_up_to_one, zero_add] proposition sum_range_succ {m n : ℕ} (f : ℕ → A) (H : m ≤ succ n) : (∑ i = m...succ n, f i) = (∑ i = m...n, f i) + f (succ n) := by rewrite [↑sum_range, succ_sub H, sum_up_to_succ, nat.sub_add_cancel H] proposition sum_up_to_succ_eq_sum_range_zero (n : ℕ) (f : ℕ → A) : (∑ i < succ n, f i) = (∑ i = 0...n, f i) := rfl end add_monoid section finset variables {A : Type} [add_comm_monoid A] open finset proposition sum_up_to_eq_Sum_upto (n : ℕ) (f : ℕ → A) : (∑ i < n, f i) = (∑ i ∈ upto n, f i) := begin induction n with n ih, {exact rfl}, have H : upto n ∩ '{n} = ∅, from inter_eq_empty (take x, suppose x ∈ upto n, have x < n, from lt_of_mem_upto this, suppose x ∈ '{n}, have x = n, by rewrite -mem_singleton_iff; apply this, have n < n, from eq.subst this `x < n`, show false, from !lt.irrefl this), rewrite [sum_up_to_succ, ih, upto_succ, Sum_union _ H, Sum_singleton] end end finset section set variables {A : Type} [add_comm_monoid A] open set interval proposition sum_range_eq_sum_interval_aux (m n : ℕ) (f : ℕ → A) : (∑ i = m...m+n, f i) = (∑ i ∈ '[m, m + n], f i) := begin induction n with n ih, {krewrite [nat.add_zero, sum_range_self, Icc_self, Sum_singleton]}, have H : m ≤ succ (m + n), from le_of_lt (lt_of_le_of_lt !le_add_right !lt_succ_self), have H' : '[m, m + n] ∩ '{succ (m + n)} = ∅, from eq_empty_of_forall_not_mem (take x, assume H1, have x = succ (m + n), from eq_of_mem_singleton (and.right H1), have succ (m + n) ≤ m + n, from eq.subst this (and.right (and.left H1)), show false, from not_lt_of_ge this !lt_succ_self), rewrite [add_succ, sum_range_succ f H, Icc_eq_Icc_union_Ioc !le_add_right !le_succ, nat.Ioc_eq_Icc_succ, Icc_self, Sum_union f H', Sum_singleton, ih] end proposition sum_range_eq_sum_interval {m n : ℕ} (f : ℕ → A) (H : m ≤ n) : (∑ i = m...n, f i) = (∑ i ∈ '[m, n], f i) := have n = m + (n - m), by rewrite [add.comm, nat.sub_add_cancel H], using this, by rewrite this; apply sum_range_eq_sum_interval_aux proposition sum_range_offset (m n : ℕ) (f : ℕ → A) : (∑ i = m...m+n, f i) = (∑ i = 0...n, f (m + i)) := have bij_on (add m) ('[0, n]) ('[m, m+n]), from !nat.bij_on_add_Icc_zero, by rewrite [-zero_add n at {2}, *sum_range_eq_sum_interval_aux, Sum_eq_of_bij_on f this, zero_add] end set /- products -/ section monoid variables {A : Type} [monoid A] definition prod_up_to (n : ℕ) (f : ℕ → A) : A := nat.rec_on n 1 (λ n a, a * f n) notation `∏` binders ` < ` n `, ` r:(scoped f, prod_up_to n f) := r proposition prod_up_to_zero (f : ℕ → A) : (∏ i < 0, f i) = 1 := rfl proposition prod_up_to_succ (n : ℕ) (f : ℕ → A) : (∏ i < succ n, f i) = (∏ i < n, f i) * f n := rfl proposition prod_up_to_one (f : ℕ → A) : (∏ i < 1, f i) = f 0 := one_mul (f 0) definition prod_range (m n : ℕ) (f : ℕ → A) : A := prod_up_to (succ n - m) (λ i, f (i + m)) notation `∏` binders `=` m `...` n `, ` r:(scoped f, prod_range m n f) := r proposition prod_range_def (m n : ℕ) (f : ℕ → A) : (∏ i = m...n, f i) = (∏ i < (succ n - m), f (i + m)) := rfl proposition prod_range_self (m : ℕ) (f : ℕ → A) : (∏ i = m...m, f i) = f m := by krewrite [↑prod_range, succ_sub !le.refl, nat.sub_self, prod_up_to_one, zero_add] proposition prod_range_succ {m n : ℕ} (f : ℕ → A) (H : m ≤ succ n) : (∏ i = m...succ n, f i) = (∏ i = m...n, f i) * f (succ n) := by rewrite [↑prod_range, succ_sub H, prod_up_to_succ, nat.sub_add_cancel H] proposition prod_up_to_succ_eq_prod_range_zero (n : ℕ) (f : ℕ → A) : (∏ i < succ n, f i) = (∏ i = 0...n, f i) := rfl end monoid section finset variables {A : Type} [comm_monoid A] open finset proposition prod_up_to_eq_Prod_upto (n : ℕ) (f : ℕ → A) : (∏ i < n, f i) = (∏ i ∈ upto n, f i) := begin induction n with n ih, {exact rfl}, have H : upto n ∩ '{n} = ∅, from inter_eq_empty (take x, suppose x ∈ upto n, have x < n, from lt_of_mem_upto this, suppose x ∈ '{n}, have x = n, by rewrite -mem_singleton_iff; apply this, have n < n, from eq.subst this `x < n`, show false, from !lt.irrefl this), rewrite [prod_up_to_succ, ih, upto_succ, Prod_union _ H, Prod_singleton] end end finset section set variables {A : Type} [comm_monoid A] open set interval proposition prod_range_eq_prod_interval_aux (m n : ℕ) (f : ℕ → A) : (∏ i = m...m+n, f i) = (∏ i ∈ '[m, m + n], f i) := begin induction n with n ih, {krewrite [nat.add_zero, prod_range_self, Icc_self, Prod_singleton]}, have H : m ≤ succ (m + n), from le_of_lt (lt_of_le_of_lt !le_add_right !lt_succ_self), have H' : '[m, m + n] ∩ '{succ (m + n)} = ∅, from eq_empty_of_forall_not_mem (take x, assume H1, have x = succ (m + n), from eq_of_mem_singleton (and.right H1), have succ (m + n) ≤ m + n, from eq.subst this (and.right (and.left H1)), show false, from not_lt_of_ge this !lt_succ_self), rewrite [add_succ, prod_range_succ f H, Icc_eq_Icc_union_Ioc !le_add_right !le_succ, nat.Ioc_eq_Icc_succ, Icc_self, Prod_union f H', Prod_singleton, ih] end proposition prod_range_eq_prod_interval {m n : ℕ} (f : ℕ → A) (H : m ≤ n) : (∏ i = m...n, f i) = (∏ i ∈ '[m, n], f i) := have n = m + (n - m), by rewrite [add.comm, nat.sub_add_cancel H], using this, by rewrite this; apply prod_range_eq_prod_interval_aux proposition prod_range_offset (m n : ℕ) (f : ℕ → A) : (∏ i = m...m+n, f i) = (∏ i = 0...n, f (m + i)) := have bij_on (add m) ('[0, n]) ('[m, m+n]), from !nat.bij_on_add_Icc_zero, by rewrite [-zero_add n at {2}, *prod_range_eq_prod_interval_aux, Prod_eq_of_bij_on f this, zero_add] end set end nat
8536bb1dfd520ebd06ef892be392306feba817a2
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/topology/instances/int.lean
dfadfde9aebd3a216df1f307e28057776c93027f
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
2,288
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro -/ import data.int.interval import topology.metric_space.basic import order.filter.archimedean /-! # Topology on the integers The structure of a metric space on `ℤ` is introduced in this file, induced from `ℝ`. -/ noncomputable theory open metric set filter namespace int instance : has_dist ℤ := ⟨λ x y, dist (x : ℝ) y⟩ theorem dist_eq (x y : ℤ) : dist x y = |x - y| := rfl @[norm_cast, simp] theorem dist_cast_real (x y : ℤ) : dist (x : ℝ) y = dist x y := rfl lemma pairwise_one_le_dist : pairwise (λ m n : ℤ, 1 ≤ dist m n) := begin intros m n hne, rw dist_eq, norm_cast, rwa [← zero_add (1 : ℤ), int.add_one_le_iff, abs_pos, sub_ne_zero] end lemma uniform_embedding_coe_real : uniform_embedding (coe : ℤ → ℝ) := uniform_embedding_bot_of_pairwise_le_dist zero_lt_one pairwise_one_le_dist lemma closed_embedding_coe_real : closed_embedding (coe : ℤ → ℝ) := closed_embedding_of_pairwise_le_dist zero_lt_one pairwise_one_le_dist instance : metric_space ℤ := int.uniform_embedding_coe_real.comap_metric_space _ theorem preimage_ball (x : ℤ) (r : ℝ) : coe ⁻¹' (ball (x : ℝ) r) = ball x r := rfl theorem preimage_closed_ball (x : ℤ) (r : ℝ) : coe ⁻¹' (closed_ball (x : ℝ) r) = closed_ball x r := rfl theorem ball_eq_Ioo (x : ℤ) (r : ℝ) : ball x r = Ioo ⌊↑x - r⌋ ⌈↑x + r⌉ := by rw [← preimage_ball, real.ball_eq_Ioo, preimage_Ioo] theorem closed_ball_eq_Icc (x : ℤ) (r : ℝ) : closed_ball x r = Icc ⌈↑x - r⌉ ⌊↑x + r⌋ := by rw [← preimage_closed_ball, real.closed_ball_eq_Icc, preimage_Icc] instance : proper_space ℤ := ⟨ begin intros x r, rw closed_ball_eq_Icc, exact (set.finite_Icc _ _).is_compact, end ⟩ @[simp] lemma cocompact_eq : cocompact ℤ = at_bot ⊔ at_top := by simp only [← comap_dist_right_at_top_eq_cocompact (0 : ℤ), dist_eq, sub_zero, cast_zero, ← cast_abs, ← @comap_comap _ _ _ _ abs, int.comap_coe_at_top, comap_abs_at_top] @[simp] lemma cofinite_eq : (cofinite : filter ℤ) = at_bot ⊔ at_top := by rw [← cocompact_eq_cofinite, cocompact_eq] end int
750f58a2c7b9cdc8efa06728745df883bee54b97
9dc8cecdf3c4634764a18254e94d43da07142918
/src/probability/conditional_expectation.lean
b0d2ad785791960754fa1cbadde7fbdaadf7f5fe
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
3,410
lean
/- Copyright (c) 2022 Kexing Ying. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kexing Ying -/ import probability.notation import probability.independence /-! # Probabilistic properties of the conditional expectation This file contains some properties about the conditional expectation which does not belong in the main conditional expectation file. ## Main result * `measure_theory.condexp_indep_eq`: If `m₁, m₂` are independent σ-algebras and `f` is a `m₁`-measurable function, then `𝔼[f | m₂] = 𝔼[f]` almost everywhere. -/ open topological_space filter open_locale nnreal ennreal measure_theory probability_theory big_operators namespace measure_theory open probability_theory variables {Ω E : Type*} [normed_add_comm_group E] [normed_space ℝ E] [complete_space E] {m₁ m₂ m : measurable_space Ω} {μ : measure Ω} {f : Ω → E} /-- If `m₁, m₂` are independent σ-algebras and `f` is `m₁`-measurable, then `𝔼[f | m₂] = 𝔼[f]` almost everywhere. -/ lemma condexp_indep_eq (hle₁ : m₁ ≤ m) (hle₂ : m₂ ≤ m) [sigma_finite (μ.trim hle₂)] (hf : strongly_measurable[m₁] f) (hindp : indep m₁ m₂ μ) : μ[f | m₂] =ᵐ[μ] λ x, μ[f] := begin by_cases hfint : integrable f μ, swap, { rw [condexp_undef hfint, integral_undef hfint], refl, }, have hfint₁ := hfint.trim hle₁ hf, refine (ae_eq_condexp_of_forall_set_integral_eq hle₂ hfint (λ s _ hs, integrable_on_const.2 (or.inr hs)) (λ s hms hs, _) strongly_measurable_const.ae_strongly_measurable').symm, rw set_integral_const, rw ← mem_ℒp_one_iff_integrable at hfint, refine hfint.induction_strongly_measurable hle₁ ennreal.one_ne_top _ _ _ _ _ _, { intros c t hmt ht, rw [integral_indicator (hle₁ _ hmt), set_integral_const, smul_smul, ← ennreal.to_real_mul, mul_comm, ← hindp _ _ hmt hms, set_integral_indicator (hle₁ _ hmt), set_integral_const, set.inter_comm] }, { intros u v hdisj huint hvint hu hv hu_eq hv_eq, rw mem_ℒp_one_iff_integrable at huint hvint, rw [integral_add' huint hvint, smul_add, hu_eq, hv_eq, integral_add' huint.integrable_on hvint.integrable_on], }, { have heq₁ : (λ f : Lp_meas E ℝ m₁ 1 μ, ∫ x, f x ∂μ) = (λ f : Lp E 1 μ, ∫ x, f x ∂μ) ∘ (submodule.subtypeL _), { refine funext (λ f, integral_congr_ae _), simp_rw [submodule.coe_subtypeL', submodule.coe_subtype, ← coe_fn_coe_base], }, have heq₂ : (λ f : Lp_meas E ℝ m₁ 1 μ, ∫ x in s, f x ∂μ) = (λ f : Lp E 1 μ, ∫ x in s, f x ∂μ) ∘ (submodule.subtypeL _), { refine funext (λ f, integral_congr_ae (ae_restrict_of_ae _)), simp_rw [submodule.coe_subtypeL', submodule.coe_subtype, ← coe_fn_coe_base], exact eventually_of_forall (λ _, rfl), }, refine is_closed_eq (continuous.const_smul _ _) _, { rw heq₁, exact continuous_integral.comp (continuous_linear_map.continuous _), }, { rw heq₂, exact (continuous_set_integral _).comp (continuous_linear_map.continuous _), }, }, { intros u v huv huint hueq, rwa [← integral_congr_ae huv, ← (set_integral_congr_ae (hle₂ _ hms) _ : ∫ x in s, u x ∂μ = ∫ x in s, v x ∂μ)], filter_upwards [huv] with x hx _ using hx, }, { exact ⟨f, hf, eventually_eq.rfl⟩, }, end end measure_theory
cc0e5e2d52fb86cc8d8496874a5b484dea4cd1ff
94e33a31faa76775069b071adea97e86e218a8ee
/src/algebra/order/monoid_lemmas_zero_lt.lean
3b2b2a4fe31be1b8d89cc1a3f389d244ce773b20
[ "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
41,521
lean
/- Copyright (c) 2022 Damiano Testa. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Damiano Testa, Yuyang Zhao -/ import algebra.covariant_and_contravariant import algebra.group_with_zero.defs /-! # Multiplication by ·positive· elements is monotonic Let `α` be a type with `<` and `0`. We use the type `{x : α // 0 < x}` of positive elements of `α` to prove results about monotonicity of multiplication. We also introduce the local notation `α>0` for the subtype `{x : α // 0 < x}`: * the notation `α>0` to stands for `{x : α // 0 < x}`. If the type `α` also has a multiplication, then we combine this with (`contravariant_`) `covariant_class`es to assume that multiplication by positive elements is (strictly) monotone on a `mul_zero_class`, `monoid_with_zero`,... More specifically, we use extensively the following typeclasses: * monotone left * * `covariant_class α>0 α (λ x y, x * y) (≤)`, abbreviated `pos_mul_mono α`, expressing that multiplication by positive elements on the left is monotone; * * `covariant_class α>0 α (λ x y, x * y) (<)`, abbreviated `pos_mul_strict_mono α`, expressing that multiplication by positive elements on the left is strictly monotone; * monotone right * * `covariant_class α>0 α (λ x y, y * x) (≤)`, abbreviated `mul_pos_mono α`, expressing that multiplication by positive elements on the right is monotone; * * `covariant_class α>0 α (λ x y, y * x) (<)`, abbreviated `mul_pos_strict_mono α`, expressing that multiplication by positive elements on the right is strictly monotone. * reverse monotone left * * `contravariant_class α>0 α (λ x y, x * y) (≤)`, abbreviated `pos_mul_mono_rev α`, expressing that multiplication by positive elements on the left is reverse monotone; * * `contravariant_class α>0 α (λ x y, x * y) (<)`, abbreviated `pos_mul_reflect_lt α`, expressing that multiplication by positive elements on the left is strictly reverse monotone; * reverse reverse monotone right * * `contravariant_class α>0 α (λ x y, y * x) (≤)`, abbreviated `mul_pos_mono_rev α`, expressing that multiplication by positive elements on the right is reverse monotone; * * `contravariant_class α>0 α (λ x y, y * x) (<)`, abbreviated `mul_pos_reflect_lt α`, expressing that multiplication by positive elements on the right is strictly reverse monotone. ## Formalization comments We use `α>0 = {x : α // 0 < x}` with a strict inequality since in most cases what happens with `0` is clear. This creates a few bumps in the first couple of proofs, where we have to split cases on whether an element is `0` or not, but goes smoothly after that. A further advantage is that we only introduce notation for the positive elements and we do not need also the non-negative ones. Some lemmas for `partial_order` also have a variant for `preorder`, where the `preorder` version has stronger hypotheses. In this case we put the `preorder` lemma in the `preorder` namespace. -/ /- I am changing the file `algebra/order/monoid_lemmas` incrementally, with the idea of reproducing almost all of the proofs in `algebra/order/ring` with weaker assumptions. -/ universe u variable {α : Type u} /- Notation for positive elements https:// leanprover.zulipchat.com/#narrow/stream/113488-general/topic/notation.20for.20positive.20elements -/ local notation `α>0` := {x : α // 0 < x} namespace zero_lt section abbreviations_strict_mono variables (X : Type u) [has_mul X] [has_zero X] [has_lt X] /-- `zero_lt.pos_mul_strict_mono α` is an abbreviation for `covariant_class α>0 α (λ x y, x * y) (<)`, expressing that multiplication by positive elements on the left is strictly monotone. -/ abbreviation pos_mul_strict_mono : Prop := covariant_class {x : X // 0 < x} X (λ x y, x * y) (<) /-- `zero_lt.mul_pos_strict_mono α` is an abbreviation for `covariant_class α>0 α (λ x y, y * x) (<)`, expressing that multiplication by positive elements on the right is strictly monotone. -/ abbreviation mul_pos_strict_mono : Prop := covariant_class {x : X // 0 < x} X (λ x y, y * x) (<) /-- `zero_lt.pos_mul_reflect_lt α` is an abbreviation for `contravariant_class α>0 α (λ x y, x * y) (<)`, expressing that multiplication by positive elements on the left is strictly reverse monotone. -/ abbreviation pos_mul_reflect_lt : Prop := contravariant_class {x : X // 0 < x} X (λ x y, x * y) (<) /-- `zero_lt.mul_pos_reflect_lt α` is an abbreviation for `contravariant_class α>0 α (λ x y, y * x) (<)`, expressing that multiplication by positive elements on the right is strictly reverse monotone. -/ abbreviation mul_pos_reflect_lt : Prop := contravariant_class {x : X // 0 < x} X (λ x y, y * x) (<) end abbreviations_strict_mono section abbreviations_mono variables (X : Type*) [has_mul X] [has_zero X] [has_lt X] [has_le X] /-- `zero_lt.pos_mul_mono α` is an abbreviation for `covariant_class α>0 α (λ x y, x * y) (≤)`, expressing that multiplication by positive elements on the left is monotone. -/ abbreviation pos_mul_mono : Prop := covariant_class {x : X // 0 < x} X (λ x y, x * y) (≤) /-- `zero_lt.mul_pos_mono α` is an abbreviation for `covariant_class α>0 α (λ x y, y * x) (≤)`, expressing that multiplication by positive elements on the right is monotone. -/ abbreviation mul_pos_mono : Prop := covariant_class {x : X // 0 < x} X (λ x y, y * x) (≤) /-- `zero_lt.pos_mul_mono_rev α` is an abbreviation for `contravariant_class α>0 α (λ x y, x * y) (≤)`, expressing that multiplication by positive elements on the left is reverse monotone. -/ abbreviation pos_mul_mono_rev : Prop := contravariant_class {x : X // 0 < x} X (λ x y, x * y) (≤) /-- `zero_lt.mul_pos_mono_rev α` is an abbreviation for `contravariant_class α>0 α (λ x y, y * x) (≤)`, expressing that multiplication by positive elements on the right is reverse monotone. -/ abbreviation mul_pos_mono_rev : Prop := contravariant_class {x : X // 0 < x} X (λ x y, y * x) (≤) end abbreviations_mono variables {a b c d : α} section has_mul_zero variables [has_mul α] [has_zero α] section has_lt variables [has_lt α] lemma mul_lt_mul_left' [pos_mul_strict_mono α] (bc : b < c) (a0 : 0 < a) : a * b < a * c := @covariant_class.elim α>0 α (λ x y, x * y) (<) _ ⟨a, a0⟩ _ _ bc lemma mul_lt_mul_right' [mul_pos_strict_mono α] (bc : b < c) (a0 : 0 < a) : b * a < c * a := @covariant_class.elim α>0 α (λ x y, y * x) (<) _ ⟨a, a0⟩ _ _ bc -- proven with `a0 : 0 ≤ a` as `lt_of_mul_lt_mul_left` lemma lt_of_mul_lt_mul_left' [pos_mul_reflect_lt α] (bc : a * b < a * c) (a0 : 0 < a) : b < c := @contravariant_class.elim α>0 α (λ x y, x * y) (<) _ ⟨a, a0⟩ _ _ bc -- proven with `a0 : 0 ≤ a` as `lt_of_mul_lt_mul_right` lemma lt_of_mul_lt_mul_right' [mul_pos_reflect_lt α] (bc : b * a < c * a) (a0 : 0 < a) : b < c := @contravariant_class.elim α>0 α (λ x y, y * x) (<) _ ⟨a, a0⟩ _ _ bc @[simp] lemma mul_lt_mul_iff_left [pos_mul_strict_mono α] [pos_mul_reflect_lt α] (a0 : 0 < a) : a * b < a * c ↔ b < c := @rel_iff_cov α>0 α (λ x y, x * y) (<) _ _ ⟨a, a0⟩ _ _ @[simp] lemma mul_lt_mul_iff_right [mul_pos_strict_mono α] [mul_pos_reflect_lt α] (a0 : 0 < a) : b * a < c * a ↔ b < c := @rel_iff_cov α>0 α (λ x y, y * x) (<) _ _ ⟨a, a0⟩ _ _ end has_lt section has_lt_le variables [has_lt α] [has_le α] -- proven with `a0 : 0 ≤ a` as `mul_le_mul_left` lemma mul_le_mul_left' [pos_mul_mono α] (bc : b ≤ c) (a0 : 0 < a) : a * b ≤ a * c := @covariant_class.elim α>0 α (λ x y, x * y) (≤) _ ⟨a, a0⟩ _ _ bc -- proven with `a0 : 0 ≤ a` as `mul_le_mul_right` lemma mul_le_mul_right' [mul_pos_mono α] (bc : b ≤ c) (a0 : 0 < a) : b * a ≤ c * a := @covariant_class.elim α>0 α (λ x y, y * x) (≤) _ ⟨a, a0⟩ _ _ bc lemma le_of_mul_le_mul_left' [pos_mul_mono_rev α] (bc : a * b ≤ a * c) (a0 : 0 < a) : b ≤ c := @contravariant_class.elim α>0 α (λ x y, x * y) (≤) _ ⟨a, a0⟩ _ _ bc lemma le_of_mul_le_mul_right' [mul_pos_mono_rev α] (bc : b * a ≤ c * a) (a0 : 0 < a) : b ≤ c := @contravariant_class.elim α>0 α (λ x y, y * x) (≤) _ ⟨a, a0⟩ _ _ bc @[simp] lemma mul_le_mul_iff_left [pos_mul_mono α] [pos_mul_mono_rev α] (a0 : 0 < a) : a * b ≤ a * c ↔ b ≤ c := @rel_iff_cov α>0 α (λ x y, x * y) (≤) _ _ ⟨a, a0⟩ _ _ @[simp] lemma mul_le_mul_iff_right [mul_pos_mono α] [mul_pos_mono_rev α] (a0 : 0 < a) : b * a ≤ c * a ↔ b ≤ c := @rel_iff_cov α>0 α (λ x y, y * x) (≤) _ _ ⟨a, a0⟩ _ _ end has_lt_le section preorder variables [preorder α] -- proven with `a0 : 0 ≤ a` `d0 : 0 ≤ d` as `mul_le_mul_of_le_of_le` lemma preorder.mul_le_mul_of_le_of_le [pos_mul_mono α] [mul_pos_mono α] (h₁ : a ≤ b) (h₂ : c ≤ d) (a0 : 0 < a) (d0 : 0 < d) : a * c ≤ b * d := (mul_le_mul_left' h₂ a0).trans (mul_le_mul_right' h₁ d0) -- proven with `b0 : 0 ≤ b` `c0 : 0 ≤ c` as `mul_le_mul_of_le_of_le'` lemma preorder.mul_le_mul_of_le_of_le' [pos_mul_mono α] [mul_pos_mono α] (h₁ : a ≤ b) (h₂ : c ≤ d) (b0 : 0 < b) (c0 : 0 < c) : a * c ≤ b * d := (mul_le_mul_right' h₁ c0).trans (mul_le_mul_left' h₂ b0) lemma mul_lt_mul_of_le_of_lt [pos_mul_strict_mono α] [mul_pos_mono α] (h₁ : a ≤ b) (h₂ : c < d) (a0 : 0 < a) (d0 : 0 < d) : a * c < b * d := (mul_lt_mul_left' h₂ a0).trans_le (mul_le_mul_right' h₁ d0) lemma mul_lt_mul_of_le_of_lt' [pos_mul_strict_mono α] [mul_pos_mono α] (h₁ : a ≤ b) (h₂ : c < d) (b0 : 0 < b) (c0 : 0 < c) : a * c < b * d := (mul_le_mul_right' h₁ c0).trans_lt (mul_lt_mul_left' h₂ b0) lemma mul_lt_mul_of_lt_of_le [pos_mul_mono α] [mul_pos_strict_mono α] (h₁ : a < b) (h₂ : c ≤ d) (a0 : 0 < a) (d0 : 0 < d) : a * c < b * d := (mul_le_mul_left' h₂ a0).trans_lt (mul_lt_mul_right' h₁ d0) lemma mul_lt_mul_of_lt_of_le' [pos_mul_mono α] [mul_pos_strict_mono α] (h₁ : a < b) (h₂ : c ≤ d) (b0 : 0 < b) (c0 : 0 < c) : a * c < b * d := (mul_lt_mul_right' h₁ c0).trans_le (mul_le_mul_left' h₂ b0) lemma mul_lt_mul_of_lt_of_lt [pos_mul_strict_mono α] [mul_pos_strict_mono α] (h₁ : a < b) (h₂ : c < d) (a0 : 0 < a) (d0 : 0 < d) : a * c < b * d := (mul_lt_mul_left' h₂ a0).trans (mul_lt_mul_right' h₁ d0) lemma mul_lt_mul_of_lt_of_lt' [pos_mul_strict_mono α] [mul_pos_strict_mono α] (h₁ : a < b) (h₂ : c < d) (b0 : 0 < b) (c0 : 0 < c) : a * c < b * d := (mul_lt_mul_right' h₁ c0).trans (mul_lt_mul_left' h₂ b0) -- proven with `a0 : 0 ≤ a` as `mul_le_of_mul_le_left` lemma preorder.mul_le_of_mul_le_left [pos_mul_mono α] (h : a * b ≤ c) (hle : d ≤ b) (a0 : 0 < a) : a * d ≤ c := (mul_le_mul_left' hle a0).trans h lemma mul_lt_of_mul_lt_left [pos_mul_mono α] (h : a * b < c) (hle : d ≤ b) (a0 : 0 < a) : a * d < c := (mul_le_mul_left' hle a0).trans_lt h -- proven with `b0 : 0 ≤ b` as `le_mul_of_le_mul_left` lemma preorder.le_mul_of_le_mul_left [pos_mul_mono α] (h : a ≤ b * c) (hle : c ≤ d) (b0 : 0 < b) : a ≤ b * d := h.trans (mul_le_mul_left' hle b0) lemma lt_mul_of_lt_mul_left [pos_mul_mono α] (h : a < b * c) (hle : c ≤ d) (b0 : 0 < b) : a < b * d := h.trans_le (mul_le_mul_left' hle b0) -- proven with `b0 : 0 ≤ b` as `mul_le_of_mul_le_right` lemma preorder.mul_le_of_mul_le_right [mul_pos_mono α] (h : a * b ≤ c) (hle : d ≤ a) (b0 : 0 < b) : d * b ≤ c := (mul_le_mul_right' hle b0).trans h lemma mul_lt_of_mul_lt_right [mul_pos_mono α] (h : a * b < c) (hle : d ≤ a) (b0 : 0 < b) : d * b < c := (mul_le_mul_right' hle b0).trans_lt h -- proven with `c0 : 0 ≤ c` as `le_mul_of_le_mul_right` lemma preorder.le_mul_of_le_mul_right [mul_pos_mono α] (h : a ≤ b * c) (hle : b ≤ d) (c0 : 0 < c) : a ≤ d * c := h.trans (mul_le_mul_right' hle c0) lemma lt_mul_of_lt_mul_right [mul_pos_mono α] (h : a < b * c) (hle : b ≤ d) (c0 : 0 < c) : a < d * c := h.trans_le (mul_le_mul_right' hle c0) end preorder section partial_order variables [partial_order α] @[priority 100] -- see Note [lower instance priority] instance pos_mul_strict_mono.to_pos_mul_mono [pos_mul_strict_mono α] : pos_mul_mono α := ⟨λ x a b h, h.eq_or_lt.elim (λ h', h' ▸ le_rfl) (λ h', (mul_lt_mul_left' h' x.prop).le)⟩ @[priority 100] -- see Note [lower instance priority] instance mul_pos_strict_mono.to_mul_pos_mono [mul_pos_strict_mono α] : mul_pos_mono α := ⟨λ x a b h, h.eq_or_lt.elim (λ h', h' ▸ le_rfl) (λ h', (mul_lt_mul_right' h' x.prop).le)⟩ @[priority 100] -- see Note [lower instance priority] instance pos_mul_mono_rev.to_pos_mul_reflect_lt [pos_mul_mono_rev α] : pos_mul_reflect_lt α := ⟨λ x a b h, lt_of_le_of_ne (le_of_mul_le_mul_left' h.le x.prop) (λ h', by simpa [h'] using h)⟩ @[priority 100] -- see Note [lower instance priority] instance mul_pos_mono_rev.to_mul_pos_reflect_lt [mul_pos_mono_rev α] : mul_pos_reflect_lt α := ⟨λ x a b h, lt_of_le_of_ne (le_of_mul_le_mul_right' h.le x.prop) (λ h', by simpa [h'] using h)⟩ end partial_order section linear_order variables [linear_order α] @[priority 100] -- see Note [lower instance priority] instance pos_mul_strict_mono.to_pos_mul_mono_rev [pos_mul_strict_mono α] : pos_mul_mono_rev α := ⟨λ x a b h, le_of_not_lt $ λ h', h.not_lt (mul_lt_mul_left' h' x.prop)⟩ @[priority 100] -- see Note [lower instance priority] instance mul_pos_strict_mono.to_mul_pos_mono_rev [mul_pos_strict_mono α] : mul_pos_mono_rev α := ⟨λ x a b h, le_of_not_lt $ λ h', h.not_lt (mul_lt_mul_right' h' x.prop)⟩ lemma pos_mul_mono_rev.to_pos_mul_strict_mono [pos_mul_mono_rev α] : pos_mul_strict_mono α := ⟨λ x a b h, lt_of_not_le $ λ h', h.not_le (le_of_mul_le_mul_left' h' x.prop)⟩ lemma mul_pos_mono_rev.to_mul_pos_strict_mono [mul_pos_mono_rev α] : mul_pos_strict_mono α := ⟨λ x a b h, lt_of_not_le $ λ h', h.not_le (le_of_mul_le_mul_right' h' x.prop)⟩ lemma pos_mul_strict_mono_iff_pos_mul_mono_rev : pos_mul_strict_mono α ↔ pos_mul_mono_rev α := ⟨@zero_lt.pos_mul_strict_mono.to_pos_mul_mono_rev _ _ _ _, @pos_mul_mono_rev.to_pos_mul_strict_mono _ _ _ _⟩ lemma mul_pos_strict_mono_iff_mul_pos_mono_rev : mul_pos_strict_mono α ↔ mul_pos_mono_rev α := ⟨@zero_lt.mul_pos_strict_mono.to_mul_pos_mono_rev _ _ _ _, @mul_pos_mono_rev.to_mul_pos_strict_mono _ _ _ _⟩ lemma pos_mul_reflect_lt.to_pos_mul_mono [pos_mul_reflect_lt α] : pos_mul_mono α := ⟨λ x a b h, le_of_not_lt $ λ h', h.not_lt (lt_of_mul_lt_mul_left' h' x.prop)⟩ lemma mul_pos_reflect_lt.to_mul_pos_mono [mul_pos_reflect_lt α] : mul_pos_mono α := ⟨λ x a b h, le_of_not_lt $ λ h', h.not_lt (lt_of_mul_lt_mul_right' h' x.prop)⟩ lemma pos_mul_mono.to_pos_mul_reflect_lt [pos_mul_mono α] : pos_mul_reflect_lt α := ⟨λ x a b h, lt_of_not_le $ λ h', h.not_le (mul_le_mul_left' h' x.prop)⟩ lemma mul_pos_mono.to_mul_pos_reflect_lt [mul_pos_mono α] : mul_pos_reflect_lt α := ⟨λ x a b h, lt_of_not_le $ λ h', h.not_le (mul_le_mul_right' h' x.prop)⟩ lemma pos_mul_mono_iff_pos_mul_reflect_lt : pos_mul_mono α ↔ pos_mul_reflect_lt α := ⟨@pos_mul_mono.to_pos_mul_reflect_lt _ _ _ _, @pos_mul_reflect_lt.to_pos_mul_mono _ _ _ _⟩ lemma mul_pos_mono_iff_mul_pos_reflect_lt : mul_pos_mono α ↔ mul_pos_reflect_lt α := ⟨@mul_pos_mono.to_mul_pos_reflect_lt _ _ _ _, @mul_pos_reflect_lt.to_mul_pos_mono _ _ _ _⟩ end linear_order end has_mul_zero section mul_zero_class variables [mul_zero_class α] section preorder variables [preorder α] /-- Assumes left covariance. -/ lemma left.mul_pos [pos_mul_strict_mono α] (ha : 0 < a) (hb : 0 < b) : 0 < a * b := have h : a * 0 < a * b, from mul_lt_mul_left' hb ha, by rwa [mul_zero] at h lemma mul_neg_of_pos_of_neg [pos_mul_strict_mono α] (ha : 0 < a) (hb : b < 0) : a * b < 0 := have h : a * b < a * 0, from mul_lt_mul_left' hb ha, by rwa [mul_zero] at h /-- Assumes right covariance. -/ lemma right.mul_pos [mul_pos_strict_mono α] (ha : 0 < a) (hb : 0 < b) : 0 < a * b := have h : 0 * b < a * b, from mul_lt_mul_right' ha hb, by rwa [zero_mul] at h lemma mul_neg_of_neg_of_pos [mul_pos_strict_mono α] (ha : a < 0) (hb : 0 < b) : a * b < 0 := have h : a * b < 0 * b, from mul_lt_mul_right' ha hb, by rwa [zero_mul] at h end preorder section partial_order variables [partial_order α] lemma mul_le_mul_left [pos_mul_mono α] (bc : b ≤ c) (a0 : 0 ≤ a) : a * b ≤ a * c := a0.lt_or_eq.elim (mul_le_mul_left' bc) (λ h, by simp only [← h, zero_mul]) lemma mul_le_mul_right [mul_pos_mono α] (bc : b ≤ c) (a0 : 0 ≤ a) : b * a ≤ c * a := a0.lt_or_eq.elim (mul_le_mul_right' bc) (λ h, by simp only [← h, mul_zero]) /-- Assumes left covariance. -/ lemma left.mul_nonneg [pos_mul_mono α] (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ a * b := have h : a * 0 ≤ a * b, from mul_le_mul_left hb ha, by rwa [mul_zero] at h lemma mul_nonpos_of_nonneg_of_nonpos [pos_mul_mono α] (ha : 0 ≤ a) (hb : b ≤ 0) : a * b ≤ 0 := have h : a * b ≤ a * 0, from mul_le_mul_left hb ha, by rwa [mul_zero] at h /-- Assumes right covariance. -/ lemma right.mul_nonneg [mul_pos_mono α] (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ a * b := have h : 0 * b ≤ a * b, from mul_le_mul_right ha hb, by rwa [zero_mul] at h lemma mul_nonpos_of_nonpos_of_nonneg [mul_pos_mono α] (ha : a ≤ 0) (hb : 0 ≤ b) : a * b ≤ 0 := have h : a * b ≤ 0 * b, from mul_le_mul_right ha hb, by rwa [zero_mul] at h lemma lt_of_mul_lt_mul_left [pos_mul_reflect_lt α] (bc : a * b < a * c) (a0 : 0 ≤ a) : b < c := begin by_cases a₀ : a = 0, { exact (lt_irrefl (0 : α) (by simpa only [a₀, zero_mul] using bc)).elim }, { exact lt_of_mul_lt_mul_left' bc ((ne.symm a₀).le_iff_lt.mp a0) } end lemma pos_of_mul_pos_right [pos_mul_reflect_lt α] (h : 0 < a * b) (ha : 0 ≤ a) : 0 < b := lt_of_mul_lt_mul_left ((mul_zero a).symm ▸ h : a * 0 < a * b) ha lemma lt_of_mul_lt_mul_right [mul_pos_reflect_lt α] (bc : b * a < c * a) (a0 : 0 ≤ a) : b < c := begin by_cases a₀ : a = 0, { exact (lt_irrefl (0 : α) (by simpa only [a₀, mul_zero] using bc)).elim }, { exact lt_of_mul_lt_mul_right' bc ((ne.symm a₀).le_iff_lt.mp a0) } end lemma pos_of_mul_pos_left [mul_pos_reflect_lt α] (h : 0 < a * b) (hb : 0 ≤ b) : 0 < a := lt_of_mul_lt_mul_right ((zero_mul b).symm ▸ h : 0 * b < a * b) hb lemma pos_iff_pos_of_mul_pos [pos_mul_reflect_lt α] [mul_pos_reflect_lt α] (hab : 0 < a * b) : 0 < a ↔ 0 < b := ⟨pos_of_mul_pos_right hab ∘ le_of_lt, pos_of_mul_pos_left hab ∘ le_of_lt⟩ lemma mul_le_mul_of_le_of_le [pos_mul_mono α] [mul_pos_mono α] (h₁ : a ≤ b) (h₂ : c ≤ d) (a0 : 0 ≤ a) (d0 : 0 ≤ d) : a * c ≤ b * d := (mul_le_mul_left h₂ a0).trans $ mul_le_mul_right h₁ d0 lemma mul_le_mul_of_le_of_le' [pos_mul_mono α] [mul_pos_mono α] (h₁ : a ≤ b) (h₂ : c ≤ d) (b0 : 0 ≤ b) (c0 : 0 ≤ c) : a * c ≤ b * d := (mul_le_mul_right h₁ c0).trans $ mul_le_mul_left h₂ b0 lemma mul_le_of_mul_le_left [pos_mul_mono α] (h : a * b ≤ c) (hle : d ≤ b) (a0 : 0 ≤ a) : a * d ≤ c := (mul_le_mul_left hle a0).trans h lemma le_mul_of_le_mul_left [pos_mul_mono α] (h : a ≤ b * c) (hle : c ≤ d) (b0 : 0 ≤ b) : a ≤ b * d := h.trans (mul_le_mul_left hle b0) lemma mul_le_of_mul_le_right [mul_pos_mono α] (h : a * b ≤ c) (hle : d ≤ a) (b0 : 0 ≤ b) : d * b ≤ c := (mul_le_mul_right hle b0).trans h lemma le_mul_of_le_mul_right [mul_pos_mono α] (h : a ≤ b * c) (hle : b ≤ d) (c0 : 0 ≤ c) : a ≤ d * c := h.trans (mul_le_mul_right hle c0) lemma mul_left_cancel_iff [pos_mul_mono_rev α] (a0 : 0 < a) : a * b = a * c ↔ b = c := ⟨λ h, (le_of_mul_le_mul_left' h.le a0).antisymm (le_of_mul_le_mul_left' h.ge a0), congr_arg _⟩ lemma mul_right_cancel_iff [mul_pos_mono_rev α] (b0 : 0 < b) : a * b = c * b ↔ a = c := ⟨λ h, (le_of_mul_le_mul_right' h.le b0).antisymm (le_of_mul_le_mul_right' h.ge b0), congr_arg _⟩ lemma mul_eq_mul_iff_eq_and_eq [pos_mul_strict_mono α] [mul_pos_strict_mono α] [pos_mul_mono_rev α] [mul_pos_mono_rev α] (hac : a ≤ b) (hbd : c ≤ d) (a0 : 0 < a) (d0 : 0 < d) : a * c = b * d ↔ a = b ∧ c = d := begin refine ⟨λ h, _, λ h, congr_arg2 (*) h.1 h.2⟩, rcases hac.eq_or_lt with rfl | hac, { exact ⟨rfl, (mul_left_cancel_iff a0).mp h⟩ }, rcases eq_or_lt_of_le hbd with rfl | hbd, { exact ⟨(mul_right_cancel_iff d0).mp h, rfl⟩ }, exact ((mul_lt_mul_of_lt_of_lt hac hbd a0 d0).ne h).elim, end lemma mul_eq_mul_iff_eq_and_eq' [pos_mul_strict_mono α] [mul_pos_strict_mono α] [pos_mul_mono_rev α] [mul_pos_mono_rev α] (hac : a ≤ b) (hbd : c ≤ d) (b0 : 0 < b) (c0 : 0 < c) : a * c = b * d ↔ a = b ∧ c = d := begin refine ⟨λ h, _, λ h, congr_arg2 (*) h.1 h.2⟩, rcases hac.eq_or_lt with rfl | hac, { exact ⟨rfl, (mul_left_cancel_iff b0).mp h⟩ }, rcases eq_or_lt_of_le hbd with rfl | hbd, { exact ⟨(mul_right_cancel_iff c0).mp h, rfl⟩ }, exact ((mul_lt_mul_of_lt_of_lt' hac hbd b0 c0).ne h).elim, end end partial_order section linear_order variables [linear_order α] lemma pos_and_pos_or_neg_and_neg_of_mul_pos [pos_mul_mono α] [mul_pos_mono α] (hab : 0 < a * b) : (0 < a ∧ 0 < b) ∨ (a < 0 ∧ b < 0) := begin rcases lt_trichotomy 0 a with ha | rfl | ha, { refine or.inl ⟨ha, lt_imp_lt_of_le_imp_le (λ hb, _) hab⟩, exact mul_nonpos_of_nonneg_of_nonpos ha.le hb }, { rw [zero_mul] at hab, exact hab.false.elim }, { refine or.inr ⟨ha, lt_imp_lt_of_le_imp_le (λ hb, _) hab⟩, exact mul_nonpos_of_nonpos_of_nonneg ha.le hb } end lemma neg_of_mul_pos_right [pos_mul_mono α] [mul_pos_mono α] (h : 0 < a * b) (ha : a ≤ 0) : b < 0 := ((pos_and_pos_or_neg_and_neg_of_mul_pos h).resolve_left $ λ h, h.1.not_le ha).2 lemma neg_of_mul_pos_left [pos_mul_mono α] [mul_pos_mono α] (h : 0 < a * b) (ha : b ≤ 0) : a < 0 := ((pos_and_pos_or_neg_and_neg_of_mul_pos h).resolve_left $ λ h, h.2.not_le ha).1 lemma neg_iff_neg_of_mul_pos [pos_mul_mono α] [mul_pos_mono α] (hab : 0 < a * b) : a < 0 ↔ b < 0 := ⟨neg_of_mul_pos_right hab ∘ le_of_lt, neg_of_mul_pos_left hab ∘ le_of_lt⟩ lemma left.neg_of_mul_neg_left [pos_mul_mono α] (h : a * b < 0) (h1 : 0 ≤ a) : b < 0 := lt_of_not_ge (assume h2 : b ≥ 0, (left.mul_nonneg h1 h2).not_lt h) lemma right.neg_of_mul_neg_left [mul_pos_mono α] (h : a * b < 0) (h1 : 0 ≤ a) : b < 0 := lt_of_not_ge (assume h2 : b ≥ 0, (right.mul_nonneg h1 h2).not_lt h) lemma left.neg_of_mul_neg_right [pos_mul_mono α] (h : a * b < 0) (h1 : 0 ≤ b) : a < 0 := lt_of_not_ge (assume h2 : a ≥ 0, (left.mul_nonneg h2 h1).not_lt h) lemma right.neg_of_mul_neg_right [mul_pos_mono α] (h : a * b < 0) (h1 : 0 ≤ b) : a < 0 := lt_of_not_ge (assume h2 : a ≥ 0, (right.mul_nonneg h2 h1).not_lt h) end linear_order end mul_zero_class section mul_one_class variables [mul_one_class α] [has_zero α] section preorder variables [preorder α] /-! Lemmas of the form `a ≤ a * b ↔ 1 ≤ b` and `a * b ≤ a ↔ b ≤ 1`, which assume left covariance. -/ @[simp] lemma le_mul_iff_one_le_right [pos_mul_mono α] [pos_mul_mono_rev α] (a0 : 0 < a) : a ≤ a * b ↔ 1 ≤ b := iff.trans (by rw [mul_one]) (mul_le_mul_iff_left a0) @[simp] lemma lt_mul_iff_one_lt_right [pos_mul_strict_mono α] [pos_mul_reflect_lt α] (a0 : 0 < a) : a < a * b ↔ 1 < b := iff.trans (by rw [mul_one]) (mul_lt_mul_iff_left a0) @[simp] lemma mul_le_iff_le_one_right [pos_mul_mono α] [pos_mul_mono_rev α] (a0 : 0 < a) : a * b ≤ a ↔ b ≤ 1 := iff.trans (by rw [mul_one]) (mul_le_mul_iff_left a0) @[simp] lemma mul_lt_iff_lt_one_right [pos_mul_strict_mono α] [pos_mul_reflect_lt α] (a0 : 0 < a) : a * b < a ↔ b < 1 := iff.trans (by rw [mul_one]) (mul_lt_mul_iff_left a0) /-! Lemmas of the form `a ≤ b * a ↔ 1 ≤ b` and `a * b ≤ b ↔ a ≤ 1`, which assume right covariance. -/ @[simp] lemma le_mul_iff_one_le_left [mul_pos_mono α] [mul_pos_mono_rev α] (a0 : 0 < a) : a ≤ b * a ↔ 1 ≤ b := iff.trans (by rw [one_mul]) (mul_le_mul_iff_right a0) @[simp] lemma lt_mul_iff_one_lt_left [mul_pos_strict_mono α] [mul_pos_reflect_lt α] (a0 : 0 < a) : a < b * a ↔ 1 < b := iff.trans (by rw [one_mul]) (mul_lt_mul_iff_right a0) @[simp] lemma mul_le_iff_le_one_left [mul_pos_mono α] [mul_pos_mono_rev α] (b0 : 0 < b) : a * b ≤ b ↔ a ≤ 1 := iff.trans (by rw [one_mul]) (mul_le_mul_iff_right b0) @[simp] lemma mul_lt_iff_lt_one_left [mul_pos_strict_mono α] [mul_pos_reflect_lt α] (b0 : 0 < b) : a * b < b ↔ a < 1 := iff.trans (by rw [one_mul]) (mul_lt_mul_iff_right b0) /-! Lemmas of the form `b ≤ c → a ≤ 1 → 0 < b → b * a ≤ c`, which assume left covariance. -/ -- proven with `b0 : 0 ≤ b` as `mul_le_of_le_of_le_one` lemma preorder.mul_le_of_le_of_le_one [pos_mul_mono α] (bc : b ≤ c) (ha : a ≤ 1) (b0 : 0 < b) : b * a ≤ c := calc b * a ≤ b * 1 : mul_le_mul_left' ha b0 ... = b : mul_one b ... ≤ c : bc lemma mul_lt_of_le_of_lt_one [pos_mul_strict_mono α] (bc : b ≤ c) (ha : a < 1) (b0 : 0 < b) : b * a < c := calc b * a < b * 1 : mul_lt_mul_left' ha b0 ... = b : mul_one b ... ≤ c : bc lemma mul_lt_of_lt_of_le_one [pos_mul_mono α] (bc : b < c) (ha : a ≤ 1) (b0 : 0 < b) : b * a < c := calc b * a ≤ b * 1 : mul_le_mul_left' ha b0 ... = b : mul_one b ... < c : bc lemma mul_lt_of_lt_of_lt_one [pos_mul_strict_mono α] (bc : b < c) (ha : a < 1) (b0 : 0 < b) : b * a < c := calc b * a < b * 1 : mul_lt_mul_left' ha b0 ... = b : mul_one b ... < c : bc -- proven with `a0 : 0 ≤ a` as `left.mul_le_one_of_le_of_le` /-- Assumes left covariance. -/ lemma preorder.left.mul_le_one_of_le_of_le' [pos_mul_mono α] (ha : a ≤ 1) (hb : b ≤ 1) (a0 : 0 < a) : a * b ≤ 1 := preorder.mul_le_of_le_of_le_one ha hb a0 /-- Assumes left covariance. -/ lemma left.mul_lt_one_of_le_of_lt [pos_mul_strict_mono α] (ha : a ≤ 1) (hb : b < 1) (a0 : 0 < a) : a * b < 1 := mul_lt_of_le_of_lt_one ha hb a0 /-- Assumes left covariance. -/ lemma left.mul_lt_one_of_lt_of_le [pos_mul_mono α] (ha : a < 1) (hb : b ≤ 1) (a0 : 0 < a) : a * b < 1 := mul_lt_of_lt_of_le_one ha hb a0 /-- Assumes left covariance. -/ lemma left.mul_lt_one_of_lt_of_lt [pos_mul_strict_mono α] (ha : a < 1) (hb : b < 1) (a0 : 0 < a) : a * b < 1 := mul_lt_of_lt_of_lt_one ha hb a0 /-! Lemmas of the form `b ≤ c → 1 ≤ a → 0 < c → b ≤ c * a`, which assume left covariance. -/ -- proven with `c0 : 0 ≤ c` as `le_mul_of_le_of_one_le` lemma preorder.le_mul_of_le_of_one_le [pos_mul_mono α] (bc : b ≤ c) (ha : 1 ≤ a) (c0 : 0 < c) : b ≤ c * a := calc b ≤ c : bc ... = c * 1 : (mul_one c).symm ... ≤ c * a : mul_le_mul_left' ha c0 lemma lt_mul_of_le_of_one_lt [pos_mul_strict_mono α] (bc : b ≤ c) (ha : 1 < a) (c0 : 0 < c) : b < c * a := calc b ≤ c : bc ... = c * 1 : (mul_one c).symm ... < c * a : mul_lt_mul_left' ha c0 lemma lt_mul_of_lt_of_one_le [pos_mul_mono α] (bc : b < c) (ha : 1 ≤ a) (c0 : 0 < c) : b < c * a := calc b < c : bc ... = c * 1 : (mul_one c).symm ... ≤ c * a : mul_le_mul_left' ha c0 lemma lt_mul_of_lt_of_one_lt [pos_mul_strict_mono α] (bc : b < c) (ha : 1 < a) (c0 : 0 < c) : b < c * a := calc b < c : bc ... = c * 1 : (mul_one _).symm ... < c * a : mul_lt_mul_left' ha c0 -- proven with `a0 : 0 ≤ a` as `left.one_le_mul_of_le_of_le` /-- Assumes left covariance. -/ lemma preorder.left.one_le_mul_of_le_of_le [pos_mul_mono α] (ha : 1 ≤ a) (hb : 1 ≤ b) (a0 : 0 < a) : 1 ≤ a * b := preorder.le_mul_of_le_of_one_le ha hb a0 /-- Assumes left covariance. -/ lemma left.one_lt_mul_of_le_of_lt [pos_mul_strict_mono α] (ha : 1 ≤ a) (hb : 1 < b) (a0 : 0 < a) : 1 < a * b := lt_mul_of_le_of_one_lt ha hb a0 /-- Assumes left covariance. -/ lemma left.one_lt_mul_of_lt_of_le [pos_mul_mono α] (ha : 1 < a) (hb : 1 ≤ b) (a0 : 0 < a) : 1 < a * b := lt_mul_of_lt_of_one_le ha hb a0 /-- Assumes left covariance. -/ lemma left.one_lt_mul_of_lt_of_lt [pos_mul_strict_mono α] (ha : 1 < a) (hb : 1 < b) (a0 : 0 < a) : 1 < a * b := lt_mul_of_lt_of_one_lt ha hb a0 /-! Lemmas of the form `a ≤ 1 → b ≤ c → 0 < b → a * b ≤ c`, which assume right covariance. -/ -- proven with `b0 : 0 ≤ b` as `mul_le_of_le_one_of_le` lemma preorder.mul_le_of_le_one_of_le [mul_pos_mono α] (ha : a ≤ 1) (bc : b ≤ c) (b0 : 0 < b) : a * b ≤ c := calc a * b ≤ 1 * b : mul_le_mul_right' ha b0 ... = b : one_mul b ... ≤ c : bc lemma mul_lt_of_lt_one_of_le [mul_pos_strict_mono α] (ha : a < 1) (bc : b ≤ c) (b0 : 0 < b) : a * b < c := calc a * b < 1 * b : mul_lt_mul_right' ha b0 ... = b : one_mul b ... ≤ c : bc lemma mul_lt_of_le_one_of_lt [mul_pos_mono α] (ha : a ≤ 1) (hb : b < c) (b0 : 0 < b) : a * b < c := calc a * b ≤ 1 * b : mul_le_mul_right' ha b0 ... = b : one_mul b ... < c : hb lemma mul_lt_of_lt_one_of_lt [mul_pos_strict_mono α] (ha : a < 1) (bc : b < c) (b0 : 0 < b) : a * b < c := calc a * b < 1 * b : mul_lt_mul_right' ha b0 ... = b : one_mul b ... < c : bc -- proven with `b0 : 0 ≤ b` as `right.mul_le_one_of_le_of_le` /-- Assumes right covariance. -/ lemma preorder.right.mul_le_one_of_le_of_le [mul_pos_mono α] (ha : a ≤ 1) (hb : b ≤ 1) (b0 : 0 < b) : a * b ≤ 1 := preorder.mul_le_of_le_one_of_le ha hb b0 /-- Assumes right covariance. -/ lemma right.mul_lt_one_of_lt_of_le [mul_pos_strict_mono α] (ha : a < 1) (hb : b ≤ 1) (b0 : 0 < b) : a * b < 1 := mul_lt_of_lt_one_of_le ha hb b0 /-- Assumes right covariance. -/ lemma right.mul_lt_one_of_le_of_lt [mul_pos_mono α] (ha : a ≤ 1) (hb : b < 1) (b0 : 0 < b) : a * b < 1 := mul_lt_of_le_one_of_lt ha hb b0 /-- Assumes right covariance. -/ lemma right.mul_lt_one_of_lt_of_lt [mul_pos_strict_mono α] (ha : a < 1) (hb : b < 1) (b0 : 0 < b) : a * b < 1 := mul_lt_of_lt_one_of_lt ha hb b0 /-! Lemmas of the form `1 ≤ a → b ≤ c → 0 < c → b ≤ a * c`, which assume right covariance. -/ -- proven with `c0 : 0 ≤ c` as `le_mul_of_one_le_of_le` lemma preorder.le_mul_of_one_le_of_le [mul_pos_mono α] (ha : 1 ≤ a) (bc : b ≤ c) (c0 : 0 < c) : b ≤ a * c := calc b ≤ c : bc ... = 1 * c : (one_mul c).symm ... ≤ a * c : mul_le_mul_right' ha c0 lemma lt_mul_of_one_lt_of_le [mul_pos_strict_mono α] (ha : 1 < a) (bc : b ≤ c) (c0 : 0 < c) : b < a * c := calc b ≤ c : bc ... = 1 * c : (one_mul c).symm ... < a * c : mul_lt_mul_right' ha c0 lemma lt_mul_of_one_le_of_lt [mul_pos_mono α] (ha : 1 ≤ a) (bc : b < c) (c0 : 0 < c) : b < a * c := calc b < c : bc ... = 1 * c : (one_mul c).symm ... ≤ a * c : mul_le_mul_right' ha c0 lemma lt_mul_of_one_lt_of_lt [mul_pos_strict_mono α] (ha : 1 < a) (bc : b < c) (c0 : 0 < c) : b < a * c := calc b < c : bc ... = 1 * c : (one_mul c).symm ... < a * c : mul_lt_mul_right' ha c0 -- proven with `b0 : 0 ≤ b` as `right.one_le_mul_of_le_of_le` /-- Assumes right covariance. -/ lemma preorder.right.one_le_mul_of_le_of_le [mul_pos_mono α] (ha : 1 ≤ a) (hb : 1 ≤ b) (b0 : 0 < b) : 1 ≤ a * b := preorder.le_mul_of_one_le_of_le ha hb b0 /-- Assumes right covariance. -/ lemma right.one_lt_mul_of_lt_of_le [mul_pos_strict_mono α] (ha : 1 < a) (hb : 1 ≤ b) (b0 : 0 < b) : 1 < a * b := lt_mul_of_one_lt_of_le ha hb b0 /-- Assumes right covariance. -/ lemma right.one_lt_mul_of_le_of_lt [mul_pos_mono α] (ha : 1 ≤ a) (hb : 1 < b) (b0 : 0 < b) : 1 < a * b := lt_mul_of_one_le_of_lt ha hb b0 /-- Assumes right covariance. -/ lemma right.one_lt_mul_of_lt_of_lt [mul_pos_strict_mono α] (ha : 1 < a) (hb : 1 < b) (b0 : 0 < b) : 1 < a * b := lt_mul_of_one_lt_of_lt ha hb b0 -- proven with `a0 : 0 ≤ a` as `mul_le_of_le_one_right` lemma preorder.mul_le_of_le_one_right [pos_mul_mono α] (h : b ≤ 1) (a0 : 0 < a) : a * b ≤ a := preorder.mul_le_of_le_of_le_one le_rfl h a0 -- proven with `a0 : 0 ≤ a` as `le_mul_of_one_le_right` lemma preorder.le_mul_of_one_le_right [pos_mul_mono α] (h : 1 ≤ b) (a0 : 0 < a) : a ≤ a * b := preorder.le_mul_of_le_of_one_le le_rfl h a0 -- proven with `b0 : 0 ≤ b` as `mul_le_of_le_one_left` lemma preorder.mul_le_of_le_one_left [mul_pos_mono α] (h : a ≤ 1) (b0 : 0 < b) : a * b ≤ b := preorder.mul_le_of_le_one_of_le h le_rfl b0 -- proven with `b0 : 0 ≤ b` as `le_mul_of_one_le_left` lemma preorder.le_mul_of_one_le_left [mul_pos_mono α] (h : 1 ≤ a) (b0 : 0 < b) : b ≤ a * b := preorder.le_mul_of_one_le_of_le h le_rfl b0 lemma mul_lt_of_lt_one_right [pos_mul_strict_mono α] (h : b < 1) (a0 : 0 < a) : a * b < a := mul_lt_of_le_of_lt_one le_rfl h a0 lemma lt_mul_of_one_lt_right [pos_mul_strict_mono α] (h : 1 < b) (a0 : 0 < a) : a < a * b := lt_mul_of_le_of_one_lt le_rfl h a0 lemma mul_lt_of_lt_one_left [mul_pos_strict_mono α] (h : a < 1) (b0 : 0 < b) : a * b < b := mul_lt_of_lt_one_of_le h le_rfl b0 lemma lt_mul_of_one_lt_left [mul_pos_strict_mono α] (h : 1 < a) (b0 : 0 < b) : b < a * b := lt_mul_of_one_lt_of_le h le_rfl b0 -- proven with `a0 : 0 ≤ a` as `le_of_mul_le_of_one_le_left` lemma preorder.le_of_mul_le_of_one_le_left [pos_mul_mono α] (h : a * b ≤ c) (hle : 1 ≤ b) (a0 : 0 < a) : a ≤ c := (preorder.le_mul_of_one_le_right hle a0).trans h lemma lt_of_mul_lt_of_one_le_left [pos_mul_mono α] (h : a * b < c) (hle : 1 ≤ b) (a0 : 0 < a) : a < c := (preorder.le_mul_of_one_le_right hle a0).trans_lt h -- proven with `b0 : 0 ≤ b` as `le_of_le_mul_of_le_one_left` lemma preorder.le_of_le_mul_of_le_one_left [pos_mul_mono α] (h : a ≤ b * c) (hle : c ≤ 1) (b0 : 0 < b) : a ≤ b := h.trans (preorder.mul_le_of_le_one_right hle b0) lemma lt_of_lt_mul_of_le_one_left [pos_mul_mono α] (h : a < b * c) (hle : c ≤ 1) (b0 : 0 < b) : a < b := h.trans_le (preorder.mul_le_of_le_one_right hle b0) -- proven with `b0 : 0 ≤ b` as `le_of_mul_le_of_one_le_right` lemma preorder.le_of_mul_le_of_one_le_right [mul_pos_mono α] (h : a * b ≤ c) (hle : 1 ≤ a) (b0 : 0 < b) : b ≤ c := (preorder.le_mul_of_one_le_left hle b0).trans h lemma lt_of_mul_lt_of_one_le_right [mul_pos_mono α] (h : a * b < c) (hle : 1 ≤ a) (b0 : 0 < b) : b < c := (preorder.le_mul_of_one_le_left hle b0).trans_lt h -- proven with `c0 : 0 ≤ b` as `le_of_le_mul_of_le_one_right` lemma preorder.le_of_le_mul_of_le_one_right [mul_pos_mono α] (h : a ≤ b * c) (hle : b ≤ 1) (c0 : 0 < c) : a ≤ c := h.trans (preorder.mul_le_of_le_one_left hle c0) lemma lt_of_lt_mul_of_le_one_right [mul_pos_mono α] (h : a < b * c) (hle : b ≤ 1) (c0 : 0 < c) : a < c := h.trans_le (preorder.mul_le_of_le_one_left hle c0) end preorder section linear_order variables [linear_order α] -- proven with `a0 : 0 ≤ a` as `exists_square_le` lemma exists_square_le' [pos_mul_strict_mono α] (a0 : 0 < a) : ∃ (b : α), b * b ≤ a := begin by_cases h : a < 1, { use a, have : a*a < a*1, exact mul_lt_mul_left' h a0, rw mul_one at this, exact le_of_lt this }, { use 1, push_neg at h, rwa mul_one } end end linear_order end mul_one_class section mul_zero_one_class variables [mul_zero_one_class α] section partial_order variables [partial_order α] lemma mul_le_of_le_of_le_one [pos_mul_mono α] (bc : b ≤ c) (ha : a ≤ 1) (b0 : 0 ≤ b) : b * a ≤ c := b0.lt_or_eq.elim (preorder.mul_le_of_le_of_le_one bc ha) (λ h, by rw [← h, zero_mul]; exact b0.trans bc) /-- Assumes left covariance. -/ lemma left.mul_le_one_of_le_of_le [pos_mul_mono α] (ha : a ≤ 1) (hb : b ≤ 1) (a0 : 0 ≤ a) : a * b ≤ 1 := mul_le_of_le_of_le_one ha hb a0 lemma le_mul_of_le_of_one_le [pos_mul_mono α] (bc : b ≤ c) (ha : 1 ≤ a) (c0 : 0 ≤ c) : b ≤ c * a := c0.lt_or_eq.elim (preorder.le_mul_of_le_of_one_le bc ha) (λ h, by rw [← h, zero_mul] at *; exact bc) /-- Assumes left covariance. -/ lemma left.one_le_mul_of_le_of_le [pos_mul_mono α] (ha : 1 ≤ a) (hb : 1 ≤ b) (a0 : 0 ≤ a) : 1 ≤ a * b := le_mul_of_le_of_one_le ha hb a0 lemma mul_le_of_le_one_of_le [mul_pos_mono α] (ha : a ≤ 1) (bc : b ≤ c) (b0 : 0 ≤ b) : a * b ≤ c := b0.lt_or_eq.elim (preorder.mul_le_of_le_one_of_le ha bc) (λ h, by rw [← h, mul_zero] at *; exact bc) /-- Assumes right covariance. -/ lemma right.mul_le_one_of_le_of_le [mul_pos_mono α] (ha : a ≤ 1) (hb : b ≤ 1) (b0 : 0 < b) : a * b ≤ 1 := preorder.mul_le_of_le_one_of_le ha hb b0 lemma le_mul_of_one_le_of_le [mul_pos_mono α] (ha : 1 ≤ a) (bc : b ≤ c) (c0 : 0 ≤ c) : b ≤ a * c := c0.lt_or_eq.elim (preorder.le_mul_of_one_le_of_le ha bc) (λ h, by rw [← h, mul_zero] at *; exact bc) /-- Assumes right covariance. -/ lemma right.one_le_mul_of_le_of_le [mul_pos_mono α] (ha : 1 ≤ a) (hb : 1 ≤ b) (b0 : 0 ≤ b) : 1 ≤ a * b := le_mul_of_one_le_of_le ha hb b0 lemma mul_le_of_le_one_right [pos_mul_mono α] (h : b ≤ 1) (a0 : 0 ≤ a) : a * b ≤ a := mul_le_of_le_of_le_one le_rfl h a0 lemma le_mul_of_one_le_right [pos_mul_mono α] (h : 1 ≤ b) (a0 : 0 ≤ a) : a ≤ a * b := le_mul_of_le_of_one_le le_rfl h a0 lemma mul_le_of_le_one_left [mul_pos_mono α] (h : a ≤ 1) (b0 : 0 ≤ b) : a * b ≤ b := mul_le_of_le_one_of_le h le_rfl b0 lemma le_mul_of_one_le_left [mul_pos_mono α] (h : 1 ≤ a) (b0 : 0 ≤ b) : b ≤ a * b := le_mul_of_one_le_of_le h le_rfl b0 lemma le_of_mul_le_of_one_le_left [pos_mul_mono α] (h : a * b ≤ c) (hle : 1 ≤ b) (a0 : 0 ≤ a) : a ≤ c := a0.lt_or_eq.elim (preorder.le_of_mul_le_of_one_le_left h hle) (λ ha, by simpa only [← ha, zero_mul] using h) lemma le_of_le_mul_of_le_one_left [pos_mul_mono α] (h : a ≤ b * c) (hle : c ≤ 1) (b0 : 0 ≤ b) : a ≤ b := b0.lt_or_eq.elim (preorder.le_of_le_mul_of_le_one_left h hle) (λ hb, by simpa only [← hb, zero_mul] using h) lemma le_of_mul_le_of_one_le_right [mul_pos_mono α] (h : a * b ≤ c) (hle : 1 ≤ a) (b0 : 0 ≤ b) : b ≤ c := b0.lt_or_eq.elim (preorder.le_of_mul_le_of_one_le_right h hle) (λ ha, by simpa only [← ha, mul_zero] using h) lemma le_of_le_mul_of_le_one_right [mul_pos_mono α] (h : a ≤ b * c) (hle : b ≤ 1) (c0 : 0 ≤ c) : a ≤ c := c0.lt_or_eq.elim (preorder.le_of_le_mul_of_le_one_right h hle) (λ ha, by simpa only [← ha, mul_zero] using h) end partial_order section linear_order variables [linear_order α] lemma exists_square_le [pos_mul_strict_mono α] (a0 : 0 ≤ a) : ∃ (b : α), b * b ≤ a := a0.lt_or_eq.elim exists_square_le' (λ h, by rw [← h]; exact ⟨0, by simp⟩) end linear_order end mul_zero_one_class section cancel_monoid_with_zero variables [cancel_monoid_with_zero α] section partial_order variables [partial_order α] lemma pos_mul_mono.to_pos_mul_strict_mono [pos_mul_mono α] : pos_mul_strict_mono α := ⟨λ x a b h, lt_of_le_of_ne (mul_le_mul_left' h.le x.2) (h.ne ∘ mul_left_cancel₀ x.2.ne.symm)⟩ lemma pos_mul_mono_iff_pos_mul_strict_mono : pos_mul_mono α ↔ pos_mul_strict_mono α := ⟨@pos_mul_mono.to_pos_mul_strict_mono α _ _, @zero_lt.pos_mul_strict_mono.to_pos_mul_mono α _ _ _⟩ lemma mul_pos_mono.to_mul_pos_strict_mono [mul_pos_mono α] : mul_pos_strict_mono α := ⟨λ x a b h, lt_of_le_of_ne (mul_le_mul_right' h.le x.2) (h.ne ∘ mul_right_cancel₀ x.2.ne.symm)⟩ lemma mul_pos_mono_iff_mul_pos_strict_mono : mul_pos_mono α ↔ mul_pos_strict_mono α := ⟨@mul_pos_mono.to_mul_pos_strict_mono α _ _, @zero_lt.mul_pos_strict_mono.to_mul_pos_mono α _ _ _⟩ lemma pos_mul_reflect_lt.to_pos_mul_mono_rev [pos_mul_reflect_lt α] : pos_mul_mono_rev α := ⟨λ x a b h, h.eq_or_lt.elim (le_of_eq ∘ mul_left_cancel₀ x.2.ne.symm) (λ h', (lt_of_mul_lt_mul_left' h' x.2).le)⟩ lemma pos_mul_mono_rev_iff_pos_mul_reflect_lt : pos_mul_mono_rev α ↔ pos_mul_reflect_lt α := ⟨@zero_lt.pos_mul_mono_rev.to_pos_mul_reflect_lt α _ _ _, @pos_mul_reflect_lt.to_pos_mul_mono_rev α _ _⟩ lemma mul_pos_reflect_lt.to_mul_pos_mono_rev [mul_pos_reflect_lt α] : mul_pos_mono_rev α := ⟨λ x a b h, h.eq_or_lt.elim (le_of_eq ∘ mul_right_cancel₀ x.2.ne.symm) (λ h', (lt_of_mul_lt_mul_right' h' x.2).le)⟩ lemma mul_pos_mono_rev_iff_mul_pos_reflect_lt : mul_pos_mono_rev α ↔ mul_pos_reflect_lt α := ⟨@zero_lt.mul_pos_mono_rev.to_mul_pos_reflect_lt α _ _ _, @mul_pos_reflect_lt.to_mul_pos_mono_rev α _ _⟩ end partial_order end cancel_monoid_with_zero section comm_semigroup_has_zero variables [comm_semigroup α] [has_zero α] variables [has_lt α] lemma pos_mul_strict_mono_iff_mul_pos_strict_mono : pos_mul_strict_mono α ↔ mul_pos_strict_mono α := by simp ! only [mul_comm] lemma pos_mul_reflect_lt_iff_mul_pos_reflect_lt : pos_mul_reflect_lt α ↔ mul_pos_reflect_lt α := by simp ! only [mul_comm] variables [has_le α] lemma pos_mul_mono_iff_mul_pos_mono : pos_mul_mono α ↔ mul_pos_mono α := by simp ! only [mul_comm] lemma pos_mul_mono_rev_iff_mul_pos_mono_rev : pos_mul_mono_rev α ↔ mul_pos_mono_rev α := by simp ! only [mul_comm] end comm_semigroup_has_zero end zero_lt
a2cbfd23fc7c0c5c24bef4a1c340579c0436d638
3446e92e64a5de7ed1f2109cfb024f83cd904c34
/src/game/world5/level1.lean
84dcbcd705ba294f2d7257d4e3c0ab03fdc9586b
[]
no_license
kckennylau/natural_number_game
019f4a5f419c9681e65234ecd124c564f9a0a246
ad8c0adaa725975be8a9f978c8494a39311029be
refs/heads/master
1,598,784,137,722
1,571,905,156,000
1,571,905,156,000
218,354,686
0
0
null
1,572,373,319,000
1,572,373,318,000
null
UTF-8
Lean
false
false
2,924
lean
import game.world4.level7 -- hide import mynat.le namespace mynat -- hide /- A new level, a new import. You can take a look at the actual Leo `an files being imported by going to the source code for this game, which is available at <a href="https://github.com/ImperialCollegeLondon/natural_number_game" target="blank"> GitHub</a>. Here's what you get from the import: 1) The following data: * a binary relation called mynat.le, and notation a ≤ b for this relation. The definition is: a ≤ b ↔ ∃ c : mynat, b = a + c 2) The following axiom: * `le_def (a b : mynat) : a ≤ b ↔ ∃ (c : mynat), b = a + c` So `rw le_def` will change a ≤ b to `∃ c : mynat b = a + c`. You'll now have to know what to do with terms which have an ∃ in them! There are two new tactics you'll need. 1) If your *goal* is of the form `∃ c, ...` then to make progress you can use the `use` tactic. For example if your local context is this: ``` x y : mynat ⊢ ∃ t : mynat, t + y = x + y ``` then we want to set `t = x` so we write `use x`, and this will remove the `∃` and change `t` to `x`. 2) If you have a *hypothesis* of the form `h : ∃ c, P` where `P` is some proposition (which probably depends on `c`) then to extract `c` from `h` you can use the `cases` tactic. For example, if `h` is as above, then `cases h with c hc` will create your term `c` as well as creating a proof `hc` of `P`, i.e., `hc` is the proof that `c` satisfies `P`. For example, if we have ``` h : ∃ c : mynat, c + c = 12 ``` then `cases h with c hc` will turn it into ``` c : mynat, hc : c + c = 12 ``` Of course if you don't want it to be called `c`, you can do `cases h with n hn` and if you want `n + n = 12` to be called H12 you can do `cases h with n H12`. -/ /- Tactic : cases If you have a hypothesis `h : ∃ n, P(n)` where `P(n)` is a proposition depending on `n`, then `cases h with d hd` will produce a new term `d` and also a proof `hd` of `P(d)`. ## Example If the local context contains ``` h : ∃ c : mynat, c + c = 12 ``` then `cases h with c hc` will turn it into ``` c : mynat, hc : c + c = 12 ``` -/ /- Tactic : use If your goal is of the form ``` ⊢ ∃ c : P(c)` ``` where `P` is some proposition which depends on `c`, then you might want to prove it by coming up with an explicit value of `c` for which you can prove `P(c)`. The way you supply this value is with the `use` tactic. For example if the goal is ``` ⊢ ∃ c : c = 6 ``` then you can prove this with ``` use 6, refl, ``` -/ /- ## Level 1/30 or so: `le_refl` To get started on this level, you can `rw le_def`. Once you have proved it, the `refl` tactic will close all goals of the form `a ≤ a`. -/ /- Lemma For all naturals $a$, $a \leq a$. -/ theorem le_refl (a : mynat) : a ≤ a := begin [less_leaky] rw le_def, use 0, rw add_zero, refl, end attribute [_refl_lemma] le_refl -- hide end mynat --hide
ab8baa3824b4425016263b39809ac17bbaadeae1
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/src/Init/Lean/Compiler/ClosedTermCache.lean
36d18f3517133c352878b97aa5d92a84c7134ca7
[ "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
1,110
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import Init.Lean.Environment namespace Lean abbrev ClosedTermCache := SMap Expr Name def mkClosedTermCacheExtension : IO (SimplePersistentEnvExtension (Expr × Name) ClosedTermCache) := registerSimplePersistentEnvExtension { name := `closedTermCache, addImportedFn := fun as => let cache : ClosedTermCache := mkStateFromImportedEntries (fun s (p : Expr × Name) => s.insert p.1 p.2) {} as; cache.switch, addEntryFn := fun s ⟨e, n⟩ => s.insert e n } @[init mkClosedTermCacheExtension] constant closedTermCacheExt : SimplePersistentEnvExtension (Expr × Name) ClosedTermCache := arbitrary _ @[export lean_cache_closed_term_name] def cacheClosedTermName (env : Environment) (e : Expr) (n : Name) : Environment := closedTermCacheExt.addEntry env (e, n) @[export lean_get_closed_term_name] def getClosedTermName? (env : Environment) (e : Expr) : Option Name := (closedTermCacheExt.getState env).find? e end Lean
21cde42471c8861380dd211d451d14004869eca3
abd85493667895c57a7507870867b28124b3998f
/src/analysis/normed_space/real_inner_product.lean
9a6093ef02f863966e49083cac2a6dcd146f1c37
[ "Apache-2.0" ]
permissive
pechersky/mathlib
d56eef16bddb0bfc8bc552b05b7270aff5944393
f1df14c2214ee114c9738e733efd5de174deb95d
refs/heads/master
1,666,714,392,571
1,591,747,567,000
1,591,747,567,000
270,557,274
0
0
Apache-2.0
1,591,597,975,000
1,591,597,974,000
null
UTF-8
Lean
false
false
33,441
lean
/- Copyright (c) 2019 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou -/ import algebra.quadratic_discriminant import analysis.special_functions.pow import tactic.apply_fun import tactic.monotonicity /-! # Inner Product Space This file defines real inner product space and proves its basic properties. An inner product space is a vector space endowed with an inner product. It generalizes the notion of dot product in `ℝ^n` and provides the means of defining the length of a vector and the angle between two vectors. In particular vectors `x` and `y` are orthogonal if their inner product equals zero. ## Main statements Existence of orthogonal projection onto nonempty complete subspace: Let `u` be a point in an inner product space, and let `K` be a nonempty complete subspace. Then there exists a unique `v` in `K` that minimizes the distance `∥u - v∥` to `u`. The point `v` is usually called the orthogonal projection of `u` onto `K`. ## Implementation notes We decide to develop the theory of real inner product spaces and that of complex inner product spaces separately. ## Tags inner product space, norm, orthogonal projection ## References * [Clément & Martin, *The Lax-Milgram Theorem. A detailed proof to be formalized in Coq*] * [Clément & Martin, *A Coq formal proof of the Lax–Milgram theorem*] The Coq code is available at the following address: <http://www.lri.fr/~sboldo/elfic/index.html> -/ noncomputable theory open real set open_locale big_operators open_locale topological_space universes u v w variables {α : Type u} {F : Type v} {G : Type w} class has_inner (α : Type*) := (inner : α → α → ℝ) export has_inner (inner) section prio set_option default_priority 100 -- see Note [default priority] -- see Note[vector space definition] for why we extend `module`. /-- An inner product space is a real vector space with an additional operation called inner product. Inner product spaces over complex vector space will be defined in another file. -/ class inner_product_space (α : Type*) extends add_comm_group α, semimodule ℝ α, has_inner α := (comm : ∀ x y, inner x y = inner y x) (nonneg : ∀ x, 0 ≤ inner x x) (definite : ∀ x, inner x x = 0 → x = 0) (add_left : ∀ x y z, inner (x + y) z = inner x z + inner y z) (smul_left : ∀ x y r, inner (r • x) y = r * inner x y) end prio variables [inner_product_space α] section basic_properties lemma inner_comm (x y : α) : inner x y = inner y x := inner_product_space.comm x y lemma inner_self_nonneg {x : α} : 0 ≤ inner x x := inner_product_space.nonneg _ lemma inner_add_left {x y z : α} : inner (x + y) z = inner x z + inner y z := inner_product_space.add_left _ _ _ lemma inner_add_right {x y z : α} : inner x (y + z) = inner x y + inner x z := by { rw [inner_comm, inner_add_left], simp [inner_comm] } lemma inner_smul_left {x y : α} {r : ℝ} : inner (r • x) y = r * inner x y := inner_product_space.smul_left _ _ _ lemma inner_smul_right {x y : α} {r : ℝ} : inner x (r • y) = r * inner x y := by { rw [inner_comm, inner_smul_left, inner_comm] } @[simp] lemma inner_zero_left {x : α} : inner 0 x = 0 := by { rw [← zero_smul ℝ (0:α), inner_smul_left, zero_mul] } @[simp] lemma inner_zero_right {x : α} : inner x 0 = 0 := by { rw [inner_comm, inner_zero_left] } @[simp] lemma inner_self_eq_zero {x : α} : inner x x = 0 ↔ x = 0 := iff.intro (inner_product_space.definite _) (by { rintro rfl, exact inner_zero_left }) @[simp] lemma inner_self_nonpos {x : α} : inner x x ≤ 0 ↔ x = 0 := ⟨λ h, inner_self_eq_zero.1 (le_antisymm h inner_self_nonneg), λ h, h.symm ▸ le_of_eq inner_zero_left⟩ @[simp] lemma inner_neg_left {x y : α} : inner (-x) y = -inner x y := by { rw [← neg_one_smul ℝ x, inner_smul_left], simp } @[simp] lemma inner_neg_right {x y : α} : inner x (-y) = -inner x y := by { rw [inner_comm, inner_neg_left, inner_comm] } @[simp] lemma inner_neg_neg {x y : α} : inner (-x) (-y) = inner x y := by simp lemma inner_sub_left {x y z : α} : inner (x - y) z = inner x z - inner y z := by { simp [sub_eq_add_neg, inner_add_left] } lemma inner_sub_right {x y z : α} : inner x (y - z) = inner x y - inner x z := by { simp [sub_eq_add_neg, inner_add_right] } /-- Expand `inner (x + y) (x + y)` -/ lemma inner_add_add_self {x y : α} : inner (x + y) (x + y) = inner x x + 2 * inner x y + inner y y := by simpa [inner_add_left, inner_add_right, two_mul, add_assoc] using inner_comm _ _ /-- Expand `inner (x - y) (x - y)` -/ lemma inner_sub_sub_self {x y : α} : inner (x - y) (x - y) = inner x x - 2 * inner x y + inner y y := begin simp only [inner_sub_left, inner_sub_right, two_mul], simpa [sub_eq_add_neg, add_comm, add_left_comm] using inner_comm _ _ end /-- Parallelogram law -/ lemma parallelogram_law {x y : α} : inner (x + y) (x + y) + inner (x - y) (x - y) = 2 * (inner x x + inner y y) := by simp [inner_add_add_self, inner_sub_sub_self, two_mul, sub_eq_add_neg, add_comm, add_left_comm] /-- Cauchy–Schwarz inequality -/ lemma inner_mul_inner_self_le (x y : α) : inner x y * inner x y ≤ inner x x * inner y y := begin have : ∀ t, 0 ≤ inner y y * t * t + 2 * inner x y * t + inner x x, from assume t, calc 0 ≤ inner (x+t•y) (x+t•y) : inner_self_nonneg ... = inner y y * t * t + 2 * inner x y * t + inner x x : by { simp only [inner_add_add_self, inner_smul_right, inner_smul_left], ring }, have := discriminant_le_zero this, rw discrim at this, have h : (2 * inner x y)^2 - 4 * inner y y * inner x x = 4 * (inner x y * inner x y - inner x x * inner y y) := by ring, rw h at this, linarith end end basic_properties section norm /-- An inner product naturally induces a norm. -/ @[priority 100] -- see Note [lower instance priority] instance inner_product_space_has_norm : has_norm α := ⟨λx, sqrt (inner x x)⟩ lemma norm_eq_sqrt_inner {x : α} : ∥x∥ = sqrt (inner x x) := rfl lemma inner_self_eq_norm_square (x : α) : inner x x = ∥x∥ * ∥x∥ := (mul_self_sqrt inner_self_nonneg).symm /-- Expand the square -/ lemma norm_add_pow_two {x y : α} : ∥x + y∥^2 = ∥x∥^2 + 2 * inner x y + ∥y∥^2 := by { repeat {rw [pow_two, ← inner_self_eq_norm_square]}, exact inner_add_add_self } /-- Same lemma as above but in a different form -/ lemma norm_add_mul_self {x y : α} : ∥x + y∥ * ∥x + y∥ = ∥x∥ * ∥x∥ + 2 * inner x y + ∥y∥ * ∥y∥ := by { repeat {rw [← pow_two]}, exact norm_add_pow_two } /-- Expand the square -/ lemma norm_sub_pow_two {x y : α} : ∥x - y∥^2 = ∥x∥^2 - 2 * inner x y + ∥y∥^2 := by { repeat {rw [pow_two, ← inner_self_eq_norm_square]}, exact inner_sub_sub_self } /-- Same lemma as above but in a different form -/ lemma norm_sub_mul_self {x y : α} : ∥x - y∥ * ∥x - y∥ = ∥x∥ * ∥x∥ - 2 * inner x y + ∥y∥ * ∥y∥ := by { repeat {rw [← pow_two]}, exact norm_sub_pow_two } /-- Cauchy–Schwarz inequality with norm -/ lemma abs_inner_le_norm (x y : α) : abs (inner x y) ≤ ∥x∥ * ∥y∥ := nonneg_le_nonneg_of_squares_le (mul_nonneg (sqrt_nonneg _) (sqrt_nonneg _)) begin rw abs_mul_abs_self, have : ∥x∥ * ∥y∥ * (∥x∥ * ∥y∥) = inner x x * inner y y, simp only [inner_self_eq_norm_square], ring, rw this, exact inner_mul_inner_self_le _ _ end lemma parallelogram_law_with_norm {x y : α} : ∥x + y∥ * ∥x + y∥ + ∥x - y∥ * ∥x - y∥ = 2 * (∥x∥ * ∥x∥ + ∥y∥ * ∥y∥) := by { simp only [(inner_self_eq_norm_square _).symm], exact parallelogram_law } /-- The inner product, in terms of the norm. -/ lemma inner_eq_norm_add_mul_self_sub_norm_mul_self_sub_norm_mul_self_div_two (x y : α) : inner x y = (∥x + y∥ * ∥x + y∥ - ∥x∥ * ∥x∥ - ∥y∥ * ∥y∥) / 2 := begin rw norm_add_mul_self, ring end /-- The inner product, in terms of the norm. -/ lemma inner_eq_norm_mul_self_add_norm_mul_self_sub_norm_sub_mul_self_div_two (x y : α) : inner x y = (∥x∥ * ∥x∥ + ∥y∥ * ∥y∥ - ∥x - y∥ * ∥x - y∥) / 2 := begin rw norm_sub_mul_self, ring end /-- The inner product, in terms of the norm. -/ lemma inner_eq_norm_add_mul_self_sub_norm_sub_mul_self_div_four (x y : α) : inner x y = (∥x + y∥ * ∥x + y∥ - ∥x - y∥ * ∥x - y∥) / 4 := begin rw [norm_add_mul_self, norm_sub_mul_self], ring end /-- Pythagorean theorem, if-and-only-if vector inner product form. -/ lemma norm_add_square_eq_norm_square_add_norm_square_iff_inner_eq_zero (x y : α) : ∥x + y∥ * ∥x + y∥ = ∥x∥ * ∥x∥ + ∥y∥ * ∥y∥ ↔ inner x y = 0 := begin rw [norm_add_mul_self, add_right_cancel_iff, add_right_eq_self, mul_eq_zero], norm_num end /-- Pythagorean theorem, vector inner product form. -/ lemma norm_add_square_eq_norm_square_add_norm_square {x y : α} (h : inner x y = 0) : ∥x + y∥ * ∥x + y∥ = ∥x∥ * ∥x∥ + ∥y∥ * ∥y∥ := (norm_add_square_eq_norm_square_add_norm_square_iff_inner_eq_zero x y).2 h /-- Pythagorean theorem, subtracting vectors, if-and-only-if vector inner product form. -/ lemma norm_sub_square_eq_norm_square_add_norm_square_iff_inner_eq_zero (x y : α) : ∥x - y∥ * ∥x - y∥ = ∥x∥ * ∥x∥ + ∥y∥ * ∥y∥ ↔ inner x y = 0 := begin rw [norm_sub_mul_self, add_right_cancel_iff, sub_eq_add_neg, add_right_eq_self, neg_eq_zero, mul_eq_zero], norm_num end /-- Pythagorean theorem, subtracting vectors, vector inner product form. -/ lemma norm_sub_square_eq_norm_square_add_norm_square {x y : α} (h : inner x y = 0) : ∥x - y∥ * ∥x - y∥ = ∥x∥ * ∥x∥ + ∥y∥ * ∥y∥ := (norm_sub_square_eq_norm_square_add_norm_square_iff_inner_eq_zero x y).2 h /-- An inner product space forms a normed group w.r.t. its associated norm. -/ @[priority 100] -- see Note [lower instance priority] instance inner_product_space_is_normed_group : normed_group α := normed_group.of_core α { norm_eq_zero_iff := assume x, iff.intro (λ h : sqrt (inner x x) = 0, inner_self_eq_zero.1 $ (sqrt_eq_zero inner_self_nonneg).1 h ) (by {rintro rfl, show sqrt (inner (0:α) 0) = 0, simp }), triangle := assume x y, begin have := calc ∥x + y∥ * ∥x + y∥ = inner (x + y) (x + y) : (inner_self_eq_norm_square _).symm ... = inner x x + 2 * inner x y + inner y y : inner_add_add_self ... ≤ inner x x + 2 * (∥x∥ * ∥y∥) + inner y y : by linarith [abs_inner_le_norm x y, le_abs_self (inner x y)] ... = (∥x∥ + ∥y∥) * (∥x∥ + ∥y∥) : by { simp only [inner_self_eq_norm_square], ring }, exact nonneg_le_nonneg_of_squares_le (add_nonneg (sqrt_nonneg _) (sqrt_nonneg _)) this end, norm_neg := λx, show sqrt (inner (-x) (-x)) = sqrt (inner x x), by simp } /-- An inner product space forms a normed space over reals w.r.t. its associated norm. -/ instance inner_product_space_is_normed_space : normed_space ℝ α := { norm_smul_le := assume r x, le_of_eq $ begin rw [norm_eq_sqrt_inner, sqrt_eq_iff_mul_self_eq, inner_smul_left, inner_smul_right, inner_self_eq_norm_square], exact calc abs(r) * ∥x∥ * (abs(r) * ∥x∥) = (abs(r) * abs(r)) * (∥x∥ * ∥x∥) : by ring ... = r * (r * (∥x∥ * ∥x∥)) : by { rw abs_mul_abs_self, ring }, exact inner_self_nonneg, exact mul_nonneg (abs_nonneg _) (sqrt_nonneg _) end } /-- The inner product of two vectors, divided by the product of their norms, has absolute value at most 1. -/ lemma abs_inner_div_norm_mul_norm_le_one (x y : α) : abs (inner x y / (∥x∥ * ∥y∥)) ≤ 1 := begin rw abs_div, by_cases h : 0 = abs (∥x∥ * ∥y∥), { rw [←h, div_zero], norm_num }, { apply div_le_of_le_mul (lt_of_le_of_ne (ge_iff_le.mp (abs_nonneg (∥x∥ * ∥y∥))) h), convert abs_inner_le_norm x y using 1, rw [abs_mul, abs_of_nonneg (norm_nonneg x), abs_of_nonneg (norm_nonneg y), mul_one] } end /-- The inner product of a vector with a multiple of itself. -/ lemma inner_smul_self_left (x : α) (r : ℝ) : inner (r • x) x = r * (∥x∥ * ∥x∥) := by rw [inner_smul_left, ←inner_self_eq_norm_square] /-- The inner product of a vector with a multiple of itself. -/ lemma inner_smul_self_right (x : α) (r : ℝ) : inner x (r • x) = r * (∥x∥ * ∥x∥) := by rw [inner_smul_right, ←inner_self_eq_norm_square] /-- The inner product of a nonzero vector with a nonzero multiple of itself, divided by the product of their norms, has absolute value 1. -/ lemma abs_inner_div_norm_mul_norm_eq_one_of_ne_zero_of_ne_zero_mul {x : α} {r : ℝ} (hx : x ≠ 0) (hr : r ≠ 0) : abs (inner x (r • x) / (∥x∥ * ∥r • x∥)) = 1 := begin rw [inner_smul_self_right, norm_smul, real.norm_eq_abs, ←mul_assoc ∥x∥, mul_comm _ (abs r), mul_assoc, abs_div, abs_mul r, abs_mul (abs r), abs_abs, div_self], exact mul_ne_zero (λ h, hr (eq_zero_of_abs_eq_zero h)) (λ h, hx (norm_eq_zero.1 (eq_zero_of_mul_self_eq_zero (eq_zero_of_abs_eq_zero h)))) end /-- The inner product of a nonzero vector with a positive multiple of itself, divided by the product of their norms, has value 1. -/ lemma inner_div_norm_mul_norm_eq_one_of_ne_zero_of_pos_mul {x : α} {r : ℝ} (hx : x ≠ 0) (hr : 0 < r) : inner x (r • x) / (∥x∥ * ∥r • x∥) = 1 := begin rw [inner_smul_self_right, norm_smul, real.norm_eq_abs, ←mul_assoc ∥x∥, mul_comm _ (abs r), mul_assoc, abs_of_nonneg (le_of_lt hr), div_self], exact mul_ne_zero (ne_of_gt hr) (λ h, hx (norm_eq_zero.1 (eq_zero_of_mul_self_eq_zero h))) end /-- The inner product of a nonzero vector with a negative multiple of itself, divided by the product of their norms, has value -1. -/ lemma inner_div_norm_mul_norm_eq_neg_one_of_ne_zero_of_neg_mul {x : α} {r : ℝ} (hx : x ≠ 0) (hr : r < 0) : inner x (r • x) / (∥x∥ * ∥r • x∥) = -1 := begin rw [inner_smul_self_right, norm_smul, real.norm_eq_abs, ←mul_assoc ∥x∥, mul_comm _ (abs r), mul_assoc, abs_of_neg hr, ←neg_mul_eq_neg_mul, div_neg_eq_neg_div, div_self], exact mul_ne_zero (ne_of_lt hr) (λ h, hx (norm_eq_zero.1 (eq_zero_of_mul_self_eq_zero h))) end /-- The inner product of two vectors, divided by the product of their norms, has absolute value 1 if and only if they are nonzero and one is a multiple of the other. One form of equality case for Cauchy-Schwarz. -/ lemma abs_inner_div_norm_mul_norm_eq_one_iff (x y : α) : abs (inner x y / (∥x∥ * ∥y∥)) = 1 ↔ (x ≠ 0 ∧ ∃ (r : ℝ), r ≠ 0 ∧ y = r • x) := begin split, { intro h, have hx0 : x ≠ 0, { intro hx0, rw [hx0, inner_zero_left, zero_div] at h, norm_num at h, exact h }, refine and.intro hx0 _, set r := inner x y / (∥x∥ * ∥x∥) with hr, use r, set t := y - r • x with ht, have ht0 : inner x t = 0, { rw [ht, inner_sub_right, inner_smul_right, hr, ←inner_self_eq_norm_square, div_mul_cancel _ (λ h, hx0 (inner_self_eq_zero.1 h)), sub_self] }, rw [←sub_add_cancel y (r • x), ←ht, inner_add_right, ht0, zero_add, inner_smul_right, inner_self_eq_norm_square, ←mul_assoc, mul_comm, mul_div_mul_left _ _ (λ h, hx0 (norm_eq_zero.1 h)), abs_div, abs_mul, abs_of_nonneg (norm_nonneg _), abs_of_nonneg (norm_nonneg _), ←real.norm_eq_abs, ←norm_smul] at h, have hr0 : r ≠ 0, { intro hr0, rw [hr0, zero_smul, norm_zero, zero_div] at h, norm_num at h }, refine and.intro hr0 _, have h2 : ∥r • x∥ ^ 2 = ∥t + r • x∥ ^ 2, { congr' 1, refine eq_of_div_eq_one _ _ h, intro h0, rw [h0, div_zero] at h, norm_num at h }, rw [pow_two, pow_two, ←inner_self_eq_norm_square, ←inner_self_eq_norm_square, inner_add_add_self] at h2, conv_rhs at h2 { congr, congr, skip, rw [inner_smul_right, inner_comm, ht0, mul_zero, mul_zero] }, symmetry' at h2, rw [add_zero, add_left_eq_self, inner_self_eq_zero] at h2, rw h2 at ht, exact eq_of_sub_eq_zero ht.symm }, { intro h, rcases h with ⟨hx, ⟨r, ⟨hr, hy⟩⟩⟩, rw hy, exact abs_inner_div_norm_mul_norm_eq_one_of_ne_zero_of_ne_zero_mul hx hr } end /-- The inner product of two vectors, divided by the product of their norms, has value 1 if and only if they are nonzero and one is a positive multiple of the other. -/ lemma inner_div_norm_mul_norm_eq_one_iff (x y : α) : inner x y / (∥x∥ * ∥y∥) = 1 ↔ (x ≠ 0 ∧ ∃ (r : ℝ), 0 < r ∧ y = r • x) := begin split, { intro h, have ha := h, apply_fun abs at ha, norm_num at ha, rcases (abs_inner_div_norm_mul_norm_eq_one_iff x y).1 ha with ⟨hx, ⟨r, ⟨hr, hy⟩⟩⟩, use [hx, r], refine and.intro _ hy, by_contradiction hrneg, rw hy at h, rw inner_div_norm_mul_norm_eq_neg_one_of_ne_zero_of_neg_mul hx (lt_of_le_of_ne' (le_of_not_lt hrneg) hr) at h, norm_num at h }, { intro h, rcases h with ⟨hx, ⟨r, ⟨hr, hy⟩⟩⟩, rw hy, exact inner_div_norm_mul_norm_eq_one_of_ne_zero_of_pos_mul hx hr } end /-- The inner product of two vectors, divided by the product of their norms, has value -1 if and only if they are nonzero and one is a negative multiple of the other. -/ lemma inner_div_norm_mul_norm_eq_neg_one_iff (x y : α) : inner x y / (∥x∥ * ∥y∥) = -1 ↔ (x ≠ 0 ∧ ∃ (r : ℝ), r < 0 ∧ y = r • x) := begin split, { intro h, have ha := h, apply_fun abs at ha, norm_num at ha, rcases (abs_inner_div_norm_mul_norm_eq_one_iff x y).1 ha with ⟨hx, ⟨r, ⟨hr, hy⟩⟩⟩, use [hx, r], refine and.intro _ hy, by_contradiction hrpos, rw hy at h, rw inner_div_norm_mul_norm_eq_one_of_ne_zero_of_pos_mul hx (lt_of_le_of_ne' (le_of_not_lt hrpos) hr.symm) at h, norm_num at h }, { intro h, rcases h with ⟨hx, ⟨r, ⟨hr, hy⟩⟩⟩, rw hy, exact inner_div_norm_mul_norm_eq_neg_one_of_ne_zero_of_neg_mul hx hr } end end norm -- TODO [Lean 3.15]: drop some of these `show`s /-- If `ι` is a finite type and each space `f i`, `i : ι`, is an inner product space, then `Π i, f i` is an inner product space as well. This is not an instance to avoid conflict with the default instance for the norm on `Π i, f i`. -/ def pi.inner_product_space (ι : Type*) [fintype ι] (f : ι → Type*) [Π i, inner_product_space (f i)] : inner_product_space (Π i, f i) := { inner := λ x y, ∑ i, inner (x i) (y i), comm := λ x y, finset.sum_congr rfl $ λ i hi, inner_comm (x i) (y i), nonneg := λ x, show (0:ℝ) ≤ ∑ i, inner (x i) (x i), from finset.sum_nonneg (λ i hi, inner_self_nonneg), definite := λ x h, begin have : ∀ i ∈ (finset.univ : finset ι), 0 ≤ inner (x i) (x i) := λ i hi, inner_self_nonneg, simpa [inner, finset.sum_eq_zero_iff_of_nonneg this, function.funext_iff] using h, end, add_left := λ x y z, show ∑ i, inner (x i + y i) (z i) = ∑ i, inner (x i) (z i) + ∑ i, inner (y i) (z i), by simp only [inner_add_left, finset.sum_add_distrib], smul_left := λ x y r, show ∑ (i : ι), inner (r • x i) (y i) = r * ∑ i, inner (x i) (y i), by simp only [finset.mul_sum, inner_smul_left] } /-- The set of real numbers is an inner product space. While the norm given by this definition is equal to the default norm `∥x∥ = abs x`, it is not definitionally equal, so we don't turn this definition into an instance. TODO: do the same trick as with `metric_space` and `emetric_space`? -/ def real.inner_product_space : inner_product_space ℝ := { inner := (*), comm := mul_comm, nonneg := mul_self_nonneg, definite := λ x, mul_self_eq_zero.1, add_left := add_mul, smul_left := λ _ _ _, mul_assoc _ _ _ } section instances /-- The standard Euclidean space, functions on a finite type. For an `n`-dimensional space use `euclidean_space (fin n)`. -/ @[derive add_comm_group, nolint unused_arguments] def euclidean_space (n : Type*) [fintype n] : Type* := n → ℝ variables {n : Type*} [fintype n] instance : inhabited (euclidean_space n) := ⟨0⟩ local attribute [instance] real.inner_product_space instance : inner_product_space (euclidean_space n) := pi.inner_product_space n (λ _, ℝ) lemma euclidean_space.inner_def (x y : euclidean_space n) : inner x y = ∑ i, x i * y i := rfl end instances section orthogonal open filter /-- Existence of minimizers Let `u` be a point in an inner product space, and let `K` be a nonempty complete convex subset. Then there exists a unique `v` in `K` that minimizes the distance `∥u - v∥` to `u`. -/ -- FIXME this monolithic proof causes a deterministic timeout with `-T50000` -- It should be broken in a sequence of more manageable pieces, -- perhaps with individual statements for the three steps below. theorem exists_norm_eq_infi_of_complete_convex {K : set α} (ne : K.nonempty) (h₁ : is_complete K) (h₂ : convex K) : ∀ u : α, ∃ v ∈ K, ∥u - v∥ = ⨅ w : K, ∥u - w∥ := assume u, begin let δ := ⨅ w : K, ∥u - w∥, letI : nonempty K := ne.to_subtype, have zero_le_δ : 0 ≤ δ := le_cinfi (λ _, norm_nonneg _), have δ_le : ∀ w : K, δ ≤ ∥u - w∥, from cinfi_le ⟨0, forall_range_iff.2 $ λ _, norm_nonneg _⟩, have δ_le' : ∀ w ∈ K, δ ≤ ∥u - w∥ := assume w hw, δ_le ⟨w, hw⟩, -- Step 1: since `δ` is the infimum, can find a sequence `w : ℕ → K` in `K` -- such that `∥u - w n∥ < δ + 1 / (n + 1)` (which implies `∥u - w n∥ --> δ`); -- maybe this should be a separate lemma have exists_seq : ∃ w : ℕ → K, ∀ n, ∥u - w n∥ < δ + 1 / (n + 1), { have hδ : ∀n:ℕ, δ < δ + 1 / (n + 1), from λ n, lt_add_of_le_of_pos (le_refl _) nat.one_div_pos_of_nat, have h := λ n, exists_lt_of_cinfi_lt (hδ n), let w : ℕ → K := λ n, classical.some (h n), exact ⟨w, λ n, classical.some_spec (h n)⟩ }, rcases exists_seq with ⟨w, hw⟩, have norm_tendsto : tendsto (λ n, ∥u - w n∥) at_top (𝓝 δ), { have h : tendsto (λ n:ℕ, δ) at_top (𝓝 δ) := tendsto_const_nhds, have h' : tendsto (λ n:ℕ, δ + 1 / (n + 1)) at_top (𝓝 δ), { convert h.add tendsto_one_div_add_at_top_nhds_0_nat, simp only [add_zero] }, exact tendsto_of_tendsto_of_tendsto_of_le_of_le h h' (λ x, δ_le _) (λ x, le_of_lt (hw _)) }, -- Step 2: Prove that the sequence `w : ℕ → K` is a Cauchy sequence have seq_is_cauchy : cauchy_seq (λ n, ((w n):α)), { rw cauchy_seq_iff_le_tendsto_0, -- splits into three goals let b := λ n:ℕ, (8 * δ * (1/(n+1)) + 4 * (1/(n+1)) * (1/(n+1))), use (λn, sqrt (b n)), split, -- first goal : `∀ (n : ℕ), 0 ≤ sqrt (b n)` assume n, exact sqrt_nonneg _, split, -- second goal : `∀ (n m N : ℕ), N ≤ n → N ≤ m → dist ↑(w n) ↑(w m) ≤ sqrt (b N)` assume p q N hp hq, let wp := ((w p):α), let wq := ((w q):α), let a := u - wq, let b := u - wp, let half := 1 / (2:ℝ), let div := 1 / ((N:ℝ) + 1), have : 4 * ∥u - half • (wq + wp)∥ * ∥u - half • (wq + wp)∥ + ∥wp - wq∥ * ∥wp - wq∥ = 2 * (∥a∥ * ∥a∥ + ∥b∥ * ∥b∥) := calc 4 * ∥u - half•(wq + wp)∥ * ∥u - half•(wq + wp)∥ + ∥wp - wq∥ * ∥wp - wq∥ = (2*∥u - half•(wq + wp)∥) * (2 * ∥u - half•(wq + wp)∥) + ∥wp-wq∥*∥wp-wq∥ : by ring ... = (abs((2:ℝ)) * ∥u - half•(wq + wp)∥) * (abs((2:ℝ)) * ∥u - half•(wq+wp)∥) + ∥wp-wq∥*∥wp-wq∥ : by { rw abs_of_nonneg, exact add_nonneg zero_le_one zero_le_one } ... = ∥(2:ℝ) • (u - half • (wq + wp))∥ * ∥(2:ℝ) • (u - half • (wq + wp))∥ + ∥wp-wq∥ * ∥wp-wq∥ : by { rw [norm_smul], refl } ... = ∥a + b∥ * ∥a + b∥ + ∥a - b∥ * ∥a - b∥ : begin rw [smul_sub, smul_smul, mul_one_div_cancel (two_ne_zero : (2 : ℝ) ≠ 0), ← one_add_one_eq_two, add_smul], simp only [one_smul], have eq₁ : wp - wq = a - b, show wp - wq = (u - wq) - (u - wp), abel, have eq₂ : u + u - (wq + wp) = a + b, show u + u - (wq + wp) = (u - wq) + (u - wp), abel, rw [eq₁, eq₂], end ... = 2 * (∥a∥ * ∥a∥ + ∥b∥ * ∥b∥) : parallelogram_law_with_norm, have eq : δ ≤ ∥u - half • (wq + wp)∥, { rw smul_add, apply δ_le', apply h₂, repeat {exact subtype.mem _}, repeat {exact le_of_lt one_half_pos}, exact add_halves 1 }, have eq₁ : 4 * δ * δ ≤ 4 * ∥u - half • (wq + wp)∥ * ∥u - half • (wq + wp)∥, { mono, mono, norm_num, apply mul_nonneg, norm_num, exact norm_nonneg _ }, have eq₂ : ∥a∥ * ∥a∥ ≤ (δ + div) * (δ + div) := mul_self_le_mul_self (norm_nonneg _) (le_trans (le_of_lt $ hw q) (add_le_add_left (nat.one_div_le_one_div hq) _)), have eq₂' : ∥b∥ * ∥b∥ ≤ (δ + div) * (δ + div) := mul_self_le_mul_self (norm_nonneg _) (le_trans (le_of_lt $ hw p) (add_le_add_left (nat.one_div_le_one_div hp) _)), rw dist_eq_norm, apply nonneg_le_nonneg_of_squares_le, { exact sqrt_nonneg _ }, rw mul_self_sqrt, exact calc ∥wp - wq∥ * ∥wp - wq∥ = 2 * (∥a∥*∥a∥ + ∥b∥*∥b∥) - 4 * ∥u - half • (wq+wp)∥ * ∥u - half • (wq+wp)∥ : by { rw ← this, simp } ... ≤ 2 * (∥a∥ * ∥a∥ + ∥b∥ * ∥b∥) - 4 * δ * δ : sub_le_sub_left eq₁ _ ... ≤ 2 * ((δ + div) * (δ + div) + (δ + div) * (δ + div)) - 4 * δ * δ : sub_le_sub_right (mul_le_mul_of_nonneg_left (add_le_add eq₂ eq₂') (by norm_num)) _ ... = 8 * δ * div + 4 * div * div : by ring, exact add_nonneg (mul_nonneg (mul_nonneg (by norm_num) zero_le_δ) (le_of_lt nat.one_div_pos_of_nat)) (mul_nonneg (mul_nonneg (by norm_num) (le_of_lt nat.one_div_pos_of_nat)) (le_of_lt nat.one_div_pos_of_nat)), -- third goal : `tendsto (λ (n : ℕ), sqrt (b n)) at_top (𝓝 0)` apply tendsto.comp, { convert continuous_sqrt.continuous_at, exact sqrt_zero.symm }, have eq₁ : tendsto (λ (n : ℕ), 8 * δ * (1 / (n + 1))) at_top (𝓝 (0:ℝ)), { convert (@tendsto_const_nhds _ _ _ (8 * δ) _).mul tendsto_one_div_add_at_top_nhds_0_nat, simp only [mul_zero] }, have : tendsto (λ (n : ℕ), (4:ℝ) * (1 / (n + 1))) at_top (𝓝 (0:ℝ)), { convert (@tendsto_const_nhds _ _ _ (4:ℝ) _).mul tendsto_one_div_add_at_top_nhds_0_nat, simp only [mul_zero] }, have eq₂ : tendsto (λ (n : ℕ), (4:ℝ) * (1 / (n + 1)) * (1 / (n + 1))) at_top (𝓝 (0:ℝ)), { convert this.mul tendsto_one_div_add_at_top_nhds_0_nat, simp only [mul_zero] }, convert eq₁.add eq₂, simp only [add_zero] }, -- Step 3: By completeness of `K`, let `w : ℕ → K` converge to some `v : K`. -- Prove that it satisfies all requirements. rcases cauchy_seq_tendsto_of_is_complete h₁ (λ n, _) seq_is_cauchy with ⟨v, hv, w_tendsto⟩, use v, use hv, have h_cont : continuous (λ v, ∥u - v∥) := continuous.comp continuous_norm (continuous.sub continuous_const continuous_id), have : tendsto (λ n, ∥u - w n∥) at_top (𝓝 ∥u - v∥), convert (tendsto.comp h_cont.continuous_at w_tendsto), exact tendsto_nhds_unique at_top_ne_bot this norm_tendsto, exact subtype.mem _ end /-- Characterization of minimizers in the above theorem -/ theorem norm_eq_infi_iff_inner_le_zero {K : set α} (h : convex K) {u : α} {v : α} (hv : v ∈ K) : ∥u - v∥ = (⨅ w : K, ∥u - w∥) ↔ ∀ w ∈ K, inner (u - v) (w - v) ≤ 0 := iff.intro begin assume eq w hw, let δ := ⨅ w : K, ∥u - w∥, let p := inner (u - v) (w - v), let q := ∥w - v∥^2, letI : nonempty K := ⟨⟨v, hv⟩⟩, have zero_le_δ : 0 ≤ δ, apply le_cinfi, intro, exact norm_nonneg _, have δ_le : ∀ w : K, δ ≤ ∥u - w∥, assume w, apply cinfi_le, use (0:ℝ), rintros _ ⟨_, rfl⟩, exact norm_nonneg _, have δ_le' : ∀ w ∈ K, δ ≤ ∥u - w∥ := assume w hw, δ_le ⟨w, hw⟩, have : ∀θ:ℝ, 0 < θ → θ ≤ 1 → 2 * p ≤ θ * q, assume θ hθ₁ hθ₂, have : ∥u - v∥^2 ≤ ∥u - v∥^2 - 2 * θ * inner (u - v) (w - v) + θ*θ*∥w - v∥^2 := calc ∥u - v∥^2 ≤ ∥u - (θ•w + (1-θ)•v)∥^2 : begin simp only [pow_two], apply mul_self_le_mul_self (norm_nonneg _), rw [eq], apply δ_le', apply h hw hv, exacts [le_of_lt hθ₁, sub_nonneg.2 hθ₂, add_sub_cancel'_right _ _], end ... = ∥(u - v) - θ • (w - v)∥^2 : begin have : u - (θ•w + (1-θ)•v) = (u - v) - θ • (w - v), { rw [smul_sub, sub_smul, one_smul], simp only [sub_eq_add_neg, add_comm, add_left_comm, add_assoc, neg_add_rev] }, rw this end ... = ∥u - v∥^2 - 2 * θ * inner (u - v) (w - v) + θ*θ*∥w - v∥^2 : begin rw [norm_sub_pow_two, inner_smul_right, norm_smul], simp only [pow_two], show ∥u-v∥*∥u-v∥-2*(θ*inner(u-v)(w-v))+abs(θ)*∥w-v∥*(abs(θ)*∥w-v∥)= ∥u-v∥*∥u-v∥-2*θ*inner(u-v)(w-v)+θ*θ*(∥w-v∥*∥w-v∥), rw abs_of_pos hθ₁, ring end, have eq₁ : ∥u-v∥^2-2*θ*inner(u-v)(w-v)+θ*θ*∥w-v∥^2=∥u-v∥^2+(θ*θ*∥w-v∥^2-2*θ*inner(u-v)(w-v)), abel, rw [eq₁, le_add_iff_nonneg_right] at this, have eq₂ : θ*θ*∥w-v∥^2-2*θ*inner(u-v)(w-v)=θ*(θ*∥w-v∥^2-2*inner(u-v)(w-v)), ring, rw eq₂ at this, have := le_of_sub_nonneg (nonneg_of_mul_nonneg_left this hθ₁), exact this, by_cases hq : q = 0, { rw hq at this, have : p ≤ 0, have := this (1:ℝ) (by norm_num) (by norm_num), linarith, exact this }, { have q_pos : 0 < q, apply lt_of_le_of_ne, exact pow_two_nonneg _, intro h, exact hq h.symm, by_contradiction hp, rw not_le at hp, let θ := min (1:ℝ) (p / q), have eq₁ : θ*q ≤ p := calc θ*q ≤ (p/q) * q : mul_le_mul_of_nonneg_right (min_le_right _ _) (pow_two_nonneg _) ... = p : div_mul_cancel _ hq, have : 2 * p ≤ p := calc 2 * p ≤ θ*q : by { refine this θ (lt_min (by norm_num) (div_pos hp q_pos)) (by norm_num) } ... ≤ p : eq₁, linarith } end begin assume h, letI : nonempty K := ⟨⟨v, hv⟩⟩, apply le_antisymm, { apply le_cinfi, assume w, apply nonneg_le_nonneg_of_squares_le (norm_nonneg _), have := h w w.2, exact calc ∥u - v∥ * ∥u - v∥ ≤ ∥u - v∥ * ∥u - v∥ - 2 * inner (u - v) ((w:α) - v) : by linarith ... ≤ ∥u - v∥^2 - 2 * inner (u - v) ((w:α) - v) + ∥(w:α) - v∥^2 : by { rw pow_two, refine le_add_of_nonneg_right _, exact pow_two_nonneg _ } ... = ∥(u - v) - (w - v)∥^2 : norm_sub_pow_two.symm ... = ∥u - w∥ * ∥u - w∥ : by { have : (u - v) - (w - v) = u - w, abel, rw [this, pow_two] } }, { show (⨅ (w : K), ∥u - w∥) ≤ (λw:K, ∥u - w∥) ⟨v, hv⟩, apply cinfi_le, use 0, rintros y ⟨z, rfl⟩, exact norm_nonneg _ } end /-- Existence of minimizers. Let `u` be a point in an inner product space, and let `K` be a nonempty complete subspace. Then there exists a unique `v` in `K` that minimizes the distance `∥u - v∥` to `u`. This point `v` is usually called the orthogonal projection of `u` onto `K`. -/ theorem exists_norm_eq_infi_of_complete_subspace (K : subspace ℝ α) (h : is_complete (↑K : set α)) : ∀ u : α, ∃ v ∈ K, ∥u - v∥ = ⨅ w : (↑K : set α), ∥u - w∥ := exists_norm_eq_infi_of_complete_convex ⟨0, K.zero⟩ h K.convex /-- Characterization of minimizers in the above theorem. Let `u` be a point in an inner product space, and let `K` be a nonempty subspace. Then point `v` minimizes the distance `∥u - v∥` if and only if for all `w ∈ K`, `inner (u - v) w = 0` (i.e., `u - v` is orthogonal to the subspace `K`) -/ theorem norm_eq_infi_iff_inner_eq_zero (K : subspace ℝ α) {u : α} {v : α} (hv : v ∈ K) : ∥u - v∥ = (⨅ w : (↑K : set α), ∥u - w∥) ↔ ∀ w ∈ K, inner (u - v) w = 0 := iff.intro begin assume h, have h : ∀ w ∈ K, inner (u - v) (w - v) ≤ 0, { rwa [norm_eq_infi_iff_inner_le_zero] at h, exacts [K.convex, hv] }, assume w hw, have le : inner (u - v) w ≤ 0, let w' := w + v, have : w' ∈ K := submodule.add_mem _ hw hv, have h₁ := h w' this, have h₂ : w' - v = w, simp only [add_neg_cancel_right, sub_eq_add_neg], rw h₂ at h₁, exact h₁, have ge : inner (u - v) w ≥ 0, let w'' := -w + v, have : w'' ∈ K := submodule.add_mem _ (submodule.neg_mem _ hw) hv, have h₁ := h w'' this, have h₂ : w'' - v = -w, simp only [neg_inj', add_neg_cancel_right, sub_eq_add_neg], rw [h₂, inner_neg_right] at h₁, linarith, exact le_antisymm le ge end begin assume h, have : ∀ w ∈ K, inner (u - v) (w - v) ≤ 0, assume w hw, let w' := w - v, have : w' ∈ K := submodule.sub_mem _ hw hv, have h₁ := h w' this, exact le_of_eq h₁, rwa norm_eq_infi_iff_inner_le_zero, exacts [submodule.convex _, hv] end end orthogonal
d0ad26d32b2cff4588f2dc29881555d4df65e7e2
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/constr_tac.lean
1c94e9dd0884d3eccf603e9afdc89a3d72a86d02
[ "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
854
lean
import data.list example (a b c : Prop) : a → b → c → a ∧ b ∧ c := begin intro Ha Hb Hc, split, assumption, split, repeat assumption end example (a b c : Type) : a → b → c → a × b × c := begin intro Ha Hb Hc, repeat (split | assumption) end example (a b : Type) : a → sum a b := begin intro Ha, left, assumption end example (a b : Type) : b → sum a b := begin intro Ha, right, assumption end example (a b : Prop) : a → a ∨ b := begin intro Ha, left, assumption end example (a b : Prop) : b → a ∨ b := begin intro Ha, right, assumption end open nat example (a : nat) : a > 0 → ∃ x : nat, x > 0 := begin intro Ha, existsi a, apply Ha end example : list nat := begin constructor 1 end example : list nat := begin constructor 2, constructor 1, constructor 1 end
100a8f05ab664e390e8e6872c4e7a58e72c0cfb3
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/polynomial/induction_auto.lean
f880ca3f06a1690576662e99bcf5f8bb5aa7e3c1
[]
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,674
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.polynomial.coeff import Mathlib.PostPort universes u namespace Mathlib /-! # Theory of univariate polynomials The main results are `induction_on` and `as_sum`. -/ namespace polynomial theorem sum_C_mul_X_eq {R : Type u} [semiring R] (p : polynomial R) : (finsupp.sum p fun (n : ℕ) (a : R) => coe_fn C a * X ^ n) = p := Eq.trans (finset.sum_congr rfl fun (n : ℕ) (hn : n ∈ finsupp.support p) => Eq.symm single_eq_C_mul_X) (finsupp.sum_single p) theorem sum_monomial_eq {R : Type u} [semiring R] (p : polynomial R) : (finsupp.sum p fun (n : ℕ) (a : R) => coe_fn (monomial n) a) = p := sorry protected theorem induction_on {R : Type u} [semiring R] {M : polynomial R → Prop} (p : polynomial R) (h_C : ∀ (a : R), M (coe_fn C a)) (h_add : ∀ (p q : polynomial R), M p → M q → M (p + q)) (h_monomial : ∀ (n : ℕ) (a : R), M (coe_fn C a * X ^ n) → M (coe_fn C a * X ^ (n + 1))) : M p := sorry /-- To prove something about polynomials, it suffices to show the condition is closed under taking sums, and it holds for monomials. -/ protected theorem induction_on' {R : Type u} [semiring R] {M : polynomial R → Prop} (p : polynomial R) (h_add : ∀ (p q : polynomial R), M p → M q → M (p + q)) (h_monomial : ∀ (n : ℕ) (a : R), M (coe_fn (monomial n) a)) : M p := polynomial.induction_on p (h_monomial 0) h_add fun (n : ℕ) (a : R) (h : M (coe_fn C a * X ^ n)) => eq.mpr (id (Eq._oldrec (Eq.refl (M (coe_fn C a * X ^ (n + 1)))) (Eq.symm single_eq_C_mul_X))) (h_monomial (n + 1) a) theorem coeff_mul_monomial {R : Type u} [semiring R] (p : polynomial R) (n : ℕ) (d : ℕ) (r : R) : coeff (p * coe_fn (monomial n) r) (d + n) = coeff p d * r := sorry theorem coeff_monomial_mul {R : Type u} [semiring R] (p : polynomial R) (n : ℕ) (d : ℕ) (r : R) : coeff (coe_fn (monomial n) r * p) (d + n) = r * coeff p d := sorry -- This can already be proved by `simp`. theorem coeff_mul_monomial_zero {R : Type u} [semiring R] (p : polynomial R) (d : ℕ) (r : R) : coeff (p * coe_fn (monomial 0) r) d = coeff p d * r := coeff_mul_monomial p 0 d r -- This can already be proved by `simp`. theorem coeff_monomial_zero_mul {R : Type u} [semiring R] (p : polynomial R) (d : ℕ) (r : R) : coeff (coe_fn (monomial 0) r * p) d = r * coeff p d := coeff_monomial_mul p 0 d r end Mathlib
082a86397a188f79017f918d4fa12c497f1e4aee
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/tactic/has_variable_names_auto.lean
f4e7260ea86e541e1e32d90f649f16d0f0ebf86d
[]
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
10,066
lean
/- Copyright (c) 2020 Jannis Limperg. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jannis Limperg -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.tactic.core import Mathlib.PostPort universes u l v u_1 u_2 namespace Mathlib /-! # A tactic for type-based naming of variables When we name hypotheses or variables, we often do so in a type-directed fashion: a hypothesis of type `ℕ` is called `n` or `m`; a hypothesis of type `list ℕ` is called `ns`; etc. This module provides a tactic, `tactic.typical_variable_names`, which looks up typical variable names for a given type. Typical variable names are registered by giving an instance of the type class `has_variable_names`. This file provides `has_variable_names` instances for many of the core Lean types. If you want to override these, you can declare a high-priority instance (perhaps localised) of `has_variable_names`. E.g. to change the names given to natural numbers: ```lean def foo : has_variable_names ℕ := ⟨[`i, `j, `k]⟩ local attribute [instance, priority 1000] foo ``` -/ /-- Type class for associating a type `α` with typical variable names for elements of `α`. See `tactic.typical_variable_names`. -/ class has_variable_names (α : Sort u) where names : List name names_nonempty : autoParam (0 < list.length names) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.tactic.exact_dec_trivial") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "tactic") "exact_dec_trivial") []) namespace tactic /-- `typical_variable_names t` obtains typical names for variables of type `t`. The returned list is guaranteed to be nonempty. Fails if there is no instance `has_typical_variable_names t`. ``` typical_variable_names `(ℕ) = [`n, `m, `o] ``` -/ end tactic namespace has_variable_names /-- `@make_listlike_instance α _ β` creates an instance `has_variable_names β` from an instance `has_variable_names α`. If `α` has associated names `a`, `b`, ..., the generated instance for `β` has names `as`, `bs`, ... This can be used to create instances for 'containers' such as lists or sets. -/ def make_listlike_instance (α : Sort u) [has_variable_names α] {β : Sort v} : has_variable_names β := mk (list.map (fun (n : name) => name.append_suffix n (string.str string.empty (char.of_nat (bit1 (bit1 (bit0 (bit0 (bit1 (bit1 1))))))))) (names α)) /-- `@make_inheriting_instance α _ β` creates an instance `has_variable_names β` from an instance `has_variable_names α`. The generated instance contains the same variable names as that of `α`. This can be used to create instances for 'wrapper' types like `option` and `subtype`. -/ def make_inheriting_instance (α : Sort u) [has_variable_names α] {β : Sort v} : has_variable_names β := mk (names α) end has_variable_names protected instance d_array.has_variable_names {n : ℕ} {α : Type u_1} [has_variable_names α] : has_variable_names (d_array n fun (_x : fin n) => α) := has_variable_names.make_listlike_instance α protected instance bool.has_variable_names : has_variable_names Bool := has_variable_names.mk [name.mk_string (string.str string.empty (char.of_nat (bit0 (bit1 (bit0 (bit0 (bit0 (bit1 1)))))))) name.anonymous] protected instance char.has_variable_names : has_variable_names char := has_variable_names.mk [name.mk_string (string.str string.empty (char.of_nat (bit1 (bit1 (bit0 (bit0 (bit0 (bit1 1)))))))) name.anonymous] protected instance fin.has_variable_names {n : ℕ} : has_variable_names (fin n) := has_variable_names.mk [name.mk_string (string.str string.empty (char.of_nat (bit0 (bit1 (bit1 (bit1 (bit0 (bit1 1)))))))) name.anonymous, name.mk_string (string.str string.empty (char.of_nat (bit1 (bit0 (bit1 (bit1 (bit0 (bit1 1)))))))) name.anonymous, name.mk_string (string.str string.empty (char.of_nat (bit1 (bit1 (bit1 (bit1 (bit0 (bit1 1)))))))) name.anonymous] protected instance int.has_variable_names : has_variable_names ℤ := has_variable_names.mk [name.mk_string (string.str string.empty (char.of_nat (bit0 (bit1 (bit1 (bit1 (bit0 (bit1 1)))))))) name.anonymous, name.mk_string (string.str string.empty (char.of_nat (bit1 (bit0 (bit1 (bit1 (bit0 (bit1 1)))))))) name.anonymous, name.mk_string (string.str string.empty (char.of_nat (bit1 (bit1 (bit1 (bit1 (bit0 (bit1 1)))))))) name.anonymous] protected instance list.has_variable_names {α : Type u_1} [has_variable_names α] : has_variable_names (List α) := has_variable_names.make_listlike_instance α protected instance nat.has_variable_names : has_variable_names ℕ := has_variable_names.mk [name.mk_string (string.str string.empty (char.of_nat (bit0 (bit1 (bit1 (bit1 (bit0 (bit1 1)))))))) name.anonymous, name.mk_string (string.str string.empty (char.of_nat (bit1 (bit0 (bit1 (bit1 (bit0 (bit1 1)))))))) name.anonymous, name.mk_string (string.str string.empty (char.of_nat (bit1 (bit1 (bit1 (bit1 (bit0 (bit1 1)))))))) name.anonymous] protected instance sort.has_variable_names : has_variable_names Prop := has_variable_names.mk [name.mk_string (string.str string.empty (char.of_nat (bit0 (bit0 (bit0 (bit0 (bit1 (bit0 1)))))))) name.anonymous, name.mk_string (string.str string.empty (char.of_nat (bit1 (bit0 (bit0 (bit0 (bit1 (bit0 1)))))))) name.anonymous, name.mk_string (string.str string.empty (char.of_nat (bit0 (bit1 (bit0 (bit0 (bit1 (bit0 1)))))))) name.anonymous] protected instance thunk.has_variable_names {α : Type u_1} [has_variable_names α] : has_variable_names (thunk α) := has_variable_names.make_inheriting_instance α protected instance prod.has_variable_names {α : Type u_1} {β : Type u_2} : has_variable_names (α × β) := has_variable_names.mk [name.mk_string (string.str string.empty (char.of_nat (bit0 (bit0 (bit0 (bit0 (bit1 (bit1 1)))))))) name.anonymous] protected instance pprod.has_variable_names {α : Sort u_1} {β : Sort u_2} : has_variable_names (PProd α β) := has_variable_names.mk [name.mk_string (string.str string.empty (char.of_nat (bit0 (bit0 (bit0 (bit0 (bit1 (bit1 1)))))))) name.anonymous] protected instance sigma.has_variable_names {α : Type u_1} {β : α → Type u_2} : has_variable_names (sigma β) := has_variable_names.mk [name.mk_string (string.str string.empty (char.of_nat (bit0 (bit0 (bit0 (bit0 (bit1 (bit1 1)))))))) name.anonymous] protected instance psigma.has_variable_names {α : Sort u_1} {β : α → Sort u_2} : has_variable_names (psigma β) := has_variable_names.mk [name.mk_string (string.str string.empty (char.of_nat (bit0 (bit0 (bit0 (bit0 (bit1 (bit1 1)))))))) name.anonymous] protected instance subtype.has_variable_names {α : Sort u_1} [has_variable_names α] {p : α → Prop} : has_variable_names (Subtype p) := has_variable_names.make_inheriting_instance α protected instance option.has_variable_names {α : Type u_1} [has_variable_names α] : has_variable_names (Option α) := has_variable_names.make_inheriting_instance α protected instance bin_tree.has_variable_names {α : Type u_1} : has_variable_names (bin_tree α) := has_variable_names.mk [name.mk_string (string.str string.empty (char.of_nat (bit0 (bit0 (bit1 (bit0 (bit1 (bit1 1)))))))) name.anonymous] protected instance rbtree.has_variable_names {α : Type u_1} [has_variable_names α] {lt : α → α → Prop} : has_variable_names (rbtree α) := has_variable_names.make_listlike_instance α protected instance set.has_variable_names {α : Type u_1} [has_variable_names α] : has_variable_names (set α) := has_variable_names.make_listlike_instance α protected instance string.has_variable_names : has_variable_names string := has_variable_names.mk [name.mk_string (string.str string.empty (char.of_nat (bit1 (bit1 (bit0 (bit0 (bit1 (bit1 1)))))))) name.anonymous] protected instance unsigned.has_variable_names : has_variable_names unsigned := has_variable_names.mk [name.mk_string (string.str string.empty (char.of_nat (bit0 (bit1 (bit1 (bit1 (bit0 (bit1 1)))))))) name.anonymous, name.mk_string (string.str string.empty (char.of_nat (bit1 (bit0 (bit1 (bit1 (bit0 (bit1 1)))))))) name.anonymous, name.mk_string (string.str string.empty (char.of_nat (bit1 (bit1 (bit1 (bit1 (bit0 (bit1 1)))))))) name.anonymous] protected instance pi.has_variable_names {α : Sort u_1} {β : α → Sort u_2} : has_variable_names ((a : α) → β a) := has_variable_names.mk [name.mk_string (string.str string.empty (char.of_nat (bit0 (bit1 (bit1 (bit0 (bit0 (bit1 1)))))))) name.anonymous, name.mk_string (string.str string.empty (char.of_nat (bit1 (bit1 (bit1 (bit0 (bit0 (bit1 1)))))))) name.anonymous, name.mk_string (string.str string.empty (char.of_nat (bit0 (bit0 (bit0 (bit1 (bit0 (bit1 1)))))))) name.anonymous] protected instance name.has_variable_names : has_variable_names name := has_variable_names.mk [name.mk_string (string.str string.empty (char.of_nat (bit0 (bit1 (bit1 (bit1 (bit0 (bit1 1)))))))) name.anonymous] protected instance binder_info.has_variable_names : has_variable_names binder_info := has_variable_names.mk [name.mk_string (string.str (string.str string.empty (char.of_nat (bit0 (bit1 (bit0 (bit0 (bit0 (bit1 1)))))))) (char.of_nat (bit1 (bit0 (bit0 (bit1 (bit0 (bit1 1)))))))) name.anonymous] end Mathlib
681c10a2f62ce398e55057d82cef1b6a95d4274d
65fd5af7cdd47a9bb08328fa38e9ca296954e897
/src/call_cc.lean
8eb08f4f7b4985db2227e2c52eba852b101a8bc4
[]
no_license
cipher1024/call-cc
f5961409d1a83229c386699ce148cdc9379a94e9
a287a0b1813279a76dc6ac9eeb1e0629b45e3e23
refs/heads/master
1,586,545,631,568
1,544,468,876,000
1,544,468,876,000
161,223,643
0
0
null
null
null
null
UTF-8
Lean
false
false
9,418
lean
-- import data.array.lemmas import tactic import tactic.squeeze import data.list.basic -- import tactic.linarith -- import data.finmap universes u v w structure label (α : Type w) (m : Type u → Type v) (β : Type u) := (apply : α → m β) def goto {α β} {m : Type u → Type v} (f : label α m β) (x : α) := f.apply x class monad_cont (m : Type u → Type v) extends monad m := (call_cc : Π {α β}, ((label α m β) → m α) → m α) open monad_cont class is_lawful_monad_cont (m : Type u → Type v) [monad_cont m] extends is_lawful_monad m := (call_cc_bind_right {α ω γ} (cmd : m α) (next : (label ω m γ) → α → m ω) : call_cc (λ f, cmd >>= next f) = cmd >>= λ x, call_cc (λ f, next f x)) (call_cc_bind_left {α} (β) (x : α) (dead : label α m β → β → m α) : call_cc (λ f : label α m β, goto f x >>= dead f) = pure x) (call_cc_dummy {α β} (dummy : m α) : call_cc (λ f : label α m β, dummy) = dummy) export is_lawful_monad_cont def cont_t (r : Type u) (m : Type u → Type v) (α : Type w) := (α → m r) → m r namespace cont_t variables {r : Type u} {m : Type u → Type v} {α β γ ω : Type w} def run : cont_t r m α → (α → m r) → m r := id def map (f : m r → m r) (x : cont_t r m α) : cont_t r m α := f ∘ x lemma run_cont_t_map_cont_t (f : m r → m r) (x : cont_t r m α) : run (map f x) = f ∘ run x := rfl def with_cont_t (f : (β → m r) → α → m r) (x : cont_t r m α) : cont_t r m β := λ g, x $ f g lemma run_with_cont_t (f : (β → m r) → α → m r) (x : cont_t r m α) : run (with_cont_t f x) = run x ∘ f := rfl -- def call_cc (f : (α → cont_t r m β) → cont_t r m α) : cont_t r m α := -- λ g, f (λ x h, g x) g instance : monad (cont_t r m) := { pure := λ α x f, f x, bind := λ α β x f g, x $ λ i, f i g } -- variables [is_lawful_monad m] instance : is_lawful_monad (cont_t r m) := { id_map := by { intros, refl }, pure_bind := by { intros, ext, refl }, bind_assoc := by { intros, ext, refl } } instance [monad m] : has_monad_lift m (cont_t r m) := { monad_lift := λ a x f, x >>= f } lemma monad_lift_bind [monad m] [is_lawful_monad m] {α β} (x : m α) (f : α → m β) : (monad_lift (x >>= f) : cont_t r m β) = monad_lift x >>= monad_lift ∘ f := by { ext, simp only [monad_lift,has_monad_lift.monad_lift,(∘),(>>=),bind_assoc,id.def] } instance : monad_cont (cont_t r m) := { call_cc := λ α β f g, f ⟨λ x h, g x⟩ g } instance : is_lawful_monad_cont (cont_t r m) := { call_cc_bind_right := by intros; ext; refl, call_cc_bind_left := by intros; ext; refl, call_cc_dummy := by intros; ext; refl } end cont_t section take_while open list (hiding take_while) variables {r : Type u} {m : Type u → Type v} {α β γ ω : Type w} def take_while' [monad_cont m] {α} (p : α → Prop) [decidable_pred p] (xs : list α) : m (list α) := call_cc $ λ leave, do rs ← mfoldl (λ rs x, if p x then pure $ x :: rs else goto leave rs.reverse ) [] xs, pure rs.reverse def take_while (p : α → Prop) [decidable_pred p] (xs : list α) : list α := cont_t.run (take_while' p xs) id meta def assigned : tactic unit := do tactic.cleanup lemma take_while_eq' [monad_cont m] [is_lawful_monad_cont m] {α} (p : α → Prop) [decidable_pred p] (xs : list α) : take_while' p xs = (pure $ list.take_while p xs : m (list α)) := begin suffices : take_while' p xs = (pure $ [].reverse ++ list.take_while p xs : m (list α)), { revert this, simp only [imp_self, list.nil_append, list.reverse_nil] }, dunfold take_while', generalize : list.nil = rs, induction xs with x xs generalizing rs, { simp only [list.take_while, call_cc_dummy, mfoldl, list.append_nil, pure_bind], }, { simp [list.take_while, list.mfoldl_cons], split_ifs, { simp only [xs_ih,reverse_cons,cons_append,eq_self_iff_true,nil_append,pure_bind,append_assoc] }, { simp only [bind_assoc, list.append_nil, call_cc_bind_left] } }, end lemma take_while_eq (p : α → Prop) [decidable_pred p] (xs : list α) : take_while p xs = list.take_while p xs := by rw [take_while,take_while_eq']; refl end take_while section duff -- not the beer open nat variables {m : Type u → Type v} {α β γ ω : Type u} variables (f : α → m α) section iter variables [monad m] def iterate' : ℕ → α → m α | 0 := pure | (succ n) := λ x, f x >>= iterate' n open list lemma iterate_eq_foldl (n : ℕ) (x : α) : iterate' f n x = mfoldl (λ i _, f i) x (iota n) := begin induction n with n generalizing x, refl, dsimp [iterate',mfoldl,iota], congr, ext, apply n_ih, end variables [is_lawful_monad m] lemma iterate_succ' (n : ℕ) (x : α) : iterate' f (succ n) x = iterate' f n x >>= f := begin dsimp [iterate'], induction n with n generalizing x; simp only [iterate', *, bind_pure, nat.nat_zero_eq_zero, pure_bind], rw [← bind_assoc,n_ih], end lemma iterate_succ'' (n : ℕ) : iterate' f (succ n) = λ x, iterate' f n x >>= f := by ext; rw _root_.iterate_succ' lemma iterate_bind_iterate (x : α) (n k : ℕ) : iterate' f k x >>= iterate' f n = iterate' f (k + n) x := by induction k generalizing x; simp only [iterate',*,bind_assoc,pure_bind,zero_add,succ_add] lemma iterate_iterate (n k : ℕ) : iterate' (iterate' f n) k = iterate' f (n*k) := by induction k; simp only [iterate',*,iterate_bind_iterate,mul_succ,mul_zero,eq_self_iff_true,add_comm] end iter variables [monad_cont m] def iterate_duff' : Π n, (array n (label α m α) → α → m α) → α → m α | 0 f x := f array.nil x | (succ n) g x := call_cc $ λ goto_n, do y ← iterate_duff' n (λ a, g $ a.push_back goto_n) x, f y def duff_idx {n} : fin n → fin n | ⟨i,hi⟩ := have h : n - (i + 1) < n, from nat.sub_lt_self (lt_of_le_of_lt (nat.zero_le _) hi) (zero_lt_succ _), ⟨n - (i+1), h⟩ def iterate_duff {n} (i : fin n) : α → m α := iterate_duff' f n $ λ a, goto (a.read (duff_idx i)) variables [is_lawful_monad_cont m] @[simp] lemma iterate_duff_dummy (n : ℕ) (goto_n : α → m α) (x : α) : iterate_duff' f n (λ (a : array n (label α m α)), goto_n) x = goto_n x >>= iterate' f n := begin induction n generalizing goto_n; simp only [iterate_duff',*,_root_.iterate_succ'',call_cc_dummy,bind_assoc], simp only [iterate', bind_pure], end lemma read_push_back {n} (a : array n α) (x : α) (i : fin $ n+1) : (a.push_back x).read (duff_idx i) = if h' : i = 0 then x else a.read (duff_idx $ i.pred h') := by { cases i, dsimp [array.push_back,array.read,duff_idx,d_array.read], apply dif_ctx_congr, intros, refl, { intros, dsimp [duff_idx,fin.pred], congr' 2, cases i_val, contradiction, dsimp [pred], rw nat.add_sub_add_right, }, { rw [nat.add_sub_add_right,nat.sub_eq_iff_eq_add], conv { to_lhs, to_lhs, rw ← add_zero n }, rw add_left_inj, split; intro h; try { injection h }; subst i_val; refl, apply le_of_lt_succ i_is_lt, } } lemma coe_pred {n} (i : fin $ succ n) (h : i ≠ 0) : ↑(i.pred h) = nat.pred i := by cases i; refl lemma iterate_duff_def {n} (i : fin n) (x : α) : iterate_duff f i x = iterate' f i x := begin dsimp [iterate_duff], induction n generalizing i, { cases not_lt_zero _ i.is_lt }, { simp only [iterate_duff', read_push_back], split_ifs, { subst i, unfold_coes, unfold has_zero.zero, simp only [iterate',call_cc_bind_left,bind_assoc, iterate_duff_dummy] }, { specialize n_ih (i.pred h), simp only [n_ih, call_cc_dummy, coe_pred], cases i with i, cases i, contradiction, unfold_coes, dsimp [fin.val], rw [_root_.iterate_succ'] } } end def push_each : Π (l : list α) {n}, array n α → array (n + l.length) α | [] n a := a | (x :: xs) n a := have n + 1 + xs.length = (n + list.length (x :: xs)), by simp only [list.length, add_comm, add_left_comm], cast (by rw this) $ push_each xs $ a.push_back x lemma iterate_duff_def_4 (i : fin 4) (x : α) : iterate' f i x = call_cc (λ goto_n_0 : label α m α, call_cc (λ goto_n_1, call_cc (λ goto_n_2, call_cc (λ goto_n_3, let a := push_each [goto_n_3,goto_n_2,goto_n_1,goto_n_0] array.nil in goto (a.read (duff_idx i)) x >>= λ (y₄ : α), f y₄) >>= λ (y₃ : α), f y₃) >>= λ (y₂ : α), f y₂) >>= λ (y₁ : α), f y₁) := begin rw ← iterate_duff_def, dsimp [iterate_duff,iterate_duff'], refl, end lemma unroll_iterate_def (n c : ℕ) (h : n > 0) (x : α) : iterate' f c x = iterate_duff f ⟨c % n, mod_lt c h ⟩ x >>= iterate' (iterate' f n) (c / n) := begin rw iterate_duff_def, unfold_coes, dsimp [fin.val], simp only [iterate_iterate,iterate_bind_iterate,mod_add_div], end lemma unroll_iterate_def_4 (c : ℕ) (x : α) : iterate' f c x = call_cc (λ goto_n_0 : label α m α, call_cc (λ goto_n_1, call_cc (λ goto_n_2, call_cc (λ goto_n_3, let a := push_each [goto_n_3,goto_n_2,goto_n_1,goto_n_0] array.nil in goto (a.read $ duff_idx (⟨c % 4,mod_lt c dec_trivial⟩)) x >>= λ (y₄ : α), f y₄) >>= λ (y₃ : α), f y₃) >>= λ (y₂ : α), f y₂) >>= λ (y₁ : α), f y₁) >>= iterate' (λ x, f x >>= f >>= f >>= f) (c / 4) := begin rw unroll_iterate_def _ 4, dsimp [iterate_duff,iterate',iterate_duff'], congr, ext, simp only [bind_assoc,bind_pure], apply_instance end end duff
8145937c3ae0228ee801e09a90b0bfc0e22ecffb
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/measure_theory/integral/interval_average.lean
14e8f186367c266195bd636ea18c87fb26d87350
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
2,084
lean
/- Copyright (c) 2022 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import measure_theory.integral.interval_integral import measure_theory.integral.average /-! # Integral average over an interval > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file we introduce notation `⨍ x in a..b, f x` for the average `⨍ x in Ι a b, f x` of `f` over the interval `Ι a b = set.Ioc (min a b) (max a b)` w.r.t. the Lebesgue measure, then prove formulas for this average: * `interval_average_eq`: `⨍ x in a..b, f x = (b - a)⁻¹ • ∫ x in a..b, f x`; * `interval_average_eq_div`: `⨍ x in a..b, f x = (∫ x in a..b, f x) / (b - a)`. We also prove that `⨍ x in a..b, f x = ⨍ x in b..a, f x`, see `interval_average_symm`. ## Notation `⨍ x in a..b, f x`: average of `f` over the interval `Ι a b` w.r.t. the Lebesgue measure. -/ open measure_theory set topological_space open_locale interval variables {E : Type*} [normed_add_comm_group E] [normed_space ℝ E] [complete_space E] notation `⨍` binders ` in ` a `..` b `, ` r:(scoped:60 f, average (measure.restrict volume (Ι a b)) f) := r lemma interval_average_symm (f : ℝ → E) (a b : ℝ) : ⨍ x in a..b, f x = ⨍ x in b..a, f x := by rw [set_average_eq, set_average_eq, uIoc_swap] lemma interval_average_eq (f : ℝ → E) (a b : ℝ) : ⨍ x in a..b, f x = (b - a)⁻¹ • ∫ x in a..b, f x := begin cases le_or_lt a b with h h, { rw [set_average_eq, uIoc_of_le h, real.volume_Ioc, interval_integral.integral_of_le h, ennreal.to_real_of_real (sub_nonneg.2 h)] }, { rw [set_average_eq, uIoc_of_lt h, real.volume_Ioc, interval_integral.integral_of_ge h.le, ennreal.to_real_of_real (sub_nonneg.2 h.le), smul_neg, ← neg_smul, ← inv_neg, neg_sub] } end lemma interval_average_eq_div (f : ℝ → ℝ) (a b : ℝ) : ⨍ x in a..b, f x = (∫ x in a..b, f x) / (b - a) := by rw [interval_average_eq, smul_eq_mul, div_eq_inv_mul]
2d071d52c8b6dedd30750f609b4a9392d32b038e
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/number_theory/sum_four_squares_auto.lean
ab34da85e3879ba4c8ec2ac34abcf9f6211a78bf
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
1,303
lean
/- Copyright (c) 2019 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes ## Lagrange's four square theorem The main result in this file is `sum_four_squares`, a proof that every natural number is the sum of four square numbers. # Implementation Notes The proof used is close to Lagrange's original proof. -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.algebra.group_power.identities import Mathlib.data.zmod.basic import Mathlib.field_theory.finite.basic import Mathlib.data.int.parity import Mathlib.data.fintype.card import Mathlib.PostPort namespace Mathlib namespace int theorem sum_two_squares_of_two_mul_sum_two_squares {m : ℤ} {x : ℤ} {y : ℤ} (h : bit0 1 * m = x ^ bit0 1 + y ^ bit0 1) : m = ((x - y) / bit0 1) ^ bit0 1 + ((x + y) / bit0 1) ^ bit0 1 := sorry theorem exists_sum_two_squares_add_one_eq_k (p : ℕ) [hp : fact (nat.prime p)] : ∃ (a : ℤ), ∃ (b : ℤ), ∃ (k : ℕ), a ^ bit0 1 + b ^ bit0 1 + 1 = ↑k * ↑p ∧ k < p := sorry end int namespace nat theorem sum_four_squares (n : ℕ) : ∃ (a : ℕ), ∃ (b : ℕ), ∃ (c : ℕ), ∃ (d : ℕ), a ^ bit0 1 + b ^ bit0 1 + c ^ bit0 1 + d ^ bit0 1 = n := sorry end Mathlib
759eb62bca6ffbfc50b1346f98d30b55cf696e20
9028d228ac200bbefe3a711342514dd4e4458bff
/src/order/rel_classes.lean
22a83848448377bd702699b2b447cb1e904a6eed
[ "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
13,708
lean
/- Copyright (c) 2020 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Mario Carneiro, Yury G. Kudryashov -/ import order.basic /-! # Unbundled relation classes In this file we prove some properties of `is_*` classes defined in `init.algebra.classes`. The main difference between these classes and the usual order classes (`preorder` etc) is that usual classes extend `has_le` and/or `has_lt` while these classes take a relation as an explicit argument. -/ universes u v variables {α : Type u} {β : Type v} {r : α → α → Prop} {s : β → β → Prop} open function theorem is_refl.swap (r) [is_refl α r] : is_refl α (swap r) := ⟨refl_of r⟩ theorem is_irrefl.swap (r) [is_irrefl α r] : is_irrefl α (swap r) := ⟨irrefl_of r⟩ theorem is_trans.swap (r) [is_trans α r] : is_trans α (swap r) := ⟨λ a b c h₁ h₂, trans_of r h₂ h₁⟩ theorem is_antisymm.swap (r) [is_antisymm α r] : is_antisymm α (swap r) := ⟨λ a b h₁ h₂, antisymm h₂ h₁⟩ theorem is_asymm.swap (r) [is_asymm α r] : is_asymm α (swap r) := ⟨λ a b h₁ h₂, asymm_of r h₂ h₁⟩ theorem is_total.swap (r) [is_total α r] : is_total α (swap r) := ⟨λ a b, (total_of r a b).swap⟩ theorem is_trichotomous.swap (r) [is_trichotomous α r] : is_trichotomous α (swap r) := ⟨λ a b, by simpa [swap, or.comm, or.left_comm] using trichotomous_of r a b⟩ theorem is_preorder.swap (r) [is_preorder α r] : is_preorder α (swap r) := {..@is_refl.swap α r _, ..@is_trans.swap α r _} theorem is_strict_order.swap (r) [is_strict_order α r] : is_strict_order α (swap r) := {..@is_irrefl.swap α r _, ..@is_trans.swap α r _} theorem is_partial_order.swap (r) [is_partial_order α r] : is_partial_order α (swap r) := {..@is_preorder.swap α r _, ..@is_antisymm.swap α r _} theorem is_total_preorder.swap (r) [is_total_preorder α r] : is_total_preorder α (swap r) := {..@is_preorder.swap α r _, ..@is_total.swap α r _} theorem is_linear_order.swap (r) [is_linear_order α r] : is_linear_order α (swap r) := {..@is_partial_order.swap α r _, ..@is_total.swap α r _} protected theorem is_asymm.is_antisymm (r) [is_asymm α r] : is_antisymm α r := ⟨λ x y h₁ h₂, (asymm h₁ h₂).elim⟩ protected theorem is_asymm.is_irrefl [is_asymm α r] : is_irrefl α r := ⟨λ a h, asymm h h⟩ /- Convert algebraic structure style to explicit relation style typeclasses -/ instance [preorder α] : is_refl α (≤) := ⟨le_refl⟩ instance [preorder α] : is_refl α (≥) := is_refl.swap _ instance [preorder α] : is_trans α (≤) := ⟨@le_trans _ _⟩ instance [preorder α] : is_trans α (≥) := is_trans.swap _ instance [preorder α] : is_preorder α (≤) := {} instance [preorder α] : is_preorder α (≥) := {} instance [preorder α] : is_irrefl α (<) := ⟨lt_irrefl⟩ instance [preorder α] : is_irrefl α (>) := is_irrefl.swap _ instance [preorder α] : is_trans α (<) := ⟨@lt_trans _ _⟩ instance [preorder α] : is_trans α (>) := is_trans.swap _ instance [preorder α] : is_asymm α (<) := ⟨@lt_asymm _ _⟩ instance [preorder α] : is_asymm α (>) := is_asymm.swap _ instance [preorder α] : is_antisymm α (<) := is_asymm.is_antisymm _ instance [preorder α] : is_antisymm α (>) := is_asymm.is_antisymm _ instance [preorder α] : is_strict_order α (<) := {} instance [preorder α] : is_strict_order α (>) := {} instance preorder.is_total_preorder [preorder α] [is_total α (≤)] : is_total_preorder α (≤) := {} instance [partial_order α] : is_antisymm α (≤) := ⟨@le_antisymm _ _⟩ instance [partial_order α] : is_antisymm α (≥) := is_antisymm.swap _ instance [partial_order α] : is_partial_order α (≤) := {} instance [partial_order α] : is_partial_order α (≥) := {} instance [linear_order α] : is_total α (≤) := ⟨le_total⟩ instance [linear_order α] : is_total α (≥) := is_total.swap _ instance linear_order.is_total_preorder [linear_order α] : is_total_preorder α (≤) := by apply_instance instance [linear_order α] : is_total_preorder α (≥) := {} instance [linear_order α] : is_linear_order α (≤) := {} instance [linear_order α] : is_linear_order α (≥) := {} instance [linear_order α] : is_trichotomous α (<) := ⟨lt_trichotomy⟩ instance [linear_order α] : is_trichotomous α (>) := is_trichotomous.swap _ instance order_dual.is_total_le [has_le α] [is_total α (≤)] : is_total (order_dual α) (≤) := @is_total.swap α _ _ lemma trans_trichotomous_left [is_trans α r] [is_trichotomous α r] {a b c : α} : ¬r b a → r b c → r a c := begin intros h₁ h₂, rcases trichotomous_of r a b with h₃|h₃|h₃, exact trans h₃ h₂, rw h₃, exact h₂, exfalso, exact h₁ h₃ end lemma trans_trichotomous_right [is_trans α r] [is_trichotomous α r] {a b c : α} : r a b → ¬r c b → r a c := begin intros h₁ h₂, rcases trichotomous_of r b c with h₃|h₃|h₃, exact trans h₁ h₃, rw ←h₃, exact h₁, exfalso, exact h₂ h₃ end /-- Construct a partial order from a `is_strict_order` relation -/ def partial_order_of_SO (r) [is_strict_order α r] : partial_order α := { le := λ x y, x = y ∨ r x y, lt := r, le_refl := λ x, or.inl rfl, le_trans := λ x y z h₁ h₂, match y, z, h₁, h₂ with | _, _, or.inl rfl, h₂ := h₂ | _, _, h₁, or.inl rfl := h₁ | _, _, or.inr h₁, or.inr h₂ := or.inr (trans h₁ h₂) end, le_antisymm := λ x y h₁ h₂, match y, h₁, h₂ with | _, or.inl rfl, h₂ := rfl | _, h₁, or.inl rfl := rfl | _, or.inr h₁, or.inr h₂ := (asymm h₁ h₂).elim end, lt_iff_le_not_le := λ x y, ⟨λ h, ⟨or.inr h, not_or (λ e, by rw e at h; exact irrefl _ h) (asymm h)⟩, λ ⟨h₁, h₂⟩, h₁.resolve_left (λ e, h₂ $ e ▸ or.inl rfl)⟩ } /-- This is basically the same as `is_strict_total_order`, but that definition is in Type (probably by mistake) and also has redundant assumptions. -/ @[algebra] class is_strict_total_order' (α : Type u) (lt : α → α → Prop) extends is_trichotomous α lt, is_strict_order α lt : Prop. /-- Construct a linear order from a `is_strict_total_order'` relation -/ def linear_order_of_STO' (r) [is_strict_total_order' α r] : linear_order α := { le_total := λ x y, match y, trichotomous_of r x y with | y, or.inl h := or.inl (or.inr h) | _, or.inr (or.inl rfl) := or.inl (or.inl rfl) | _, or.inr (or.inr h) := or.inr (or.inr h) end, ..partial_order_of_SO r } /-- Construct a decidable linear order from a `is_strict_total_order'` relation -/ def decidable_linear_order_of_STO' (r) [is_strict_total_order' α r] [decidable_rel r] : decidable_linear_order α := by letI LO := linear_order_of_STO' r; exact { decidable_le := λ x y, decidable_of_iff (¬ r y x) (@not_lt _ _ y x), ..LO } theorem is_strict_total_order'.swap (r) [is_strict_total_order' α r] : is_strict_total_order' α (swap r) := {..is_trichotomous.swap r, ..is_strict_order.swap r} instance [linear_order α] : is_strict_total_order' α (<) := {} /-- A connected order is one satisfying the condition `a < c → a < b ∨ b < c`. This is recognizable as an intuitionistic substitute for `a ≤ b ∨ b ≤ a` on the constructive reals, and is also known as negative transitivity, since the contrapositive asserts transitivity of the relation `¬ a < b`. -/ @[algebra] class is_order_connected (α : Type u) (lt : α → α → Prop) : Prop := (conn : ∀ a b c, lt a c → lt a b ∨ lt b c) theorem is_order_connected.neg_trans {r : α → α → Prop} [is_order_connected α r] {a b c} (h₁ : ¬ r a b) (h₂ : ¬ r b c) : ¬ r a c := mt (is_order_connected.conn a b c) $ by simp [h₁, h₂] theorem is_strict_weak_order_of_is_order_connected [is_asymm α r] [is_order_connected α r] : is_strict_weak_order α r := { trans := λ a b c h₁ h₂, (is_order_connected.conn _ c _ h₁).resolve_right (asymm h₂), incomp_trans := λ a b c ⟨h₁, h₂⟩ ⟨h₃, h₄⟩, ⟨is_order_connected.neg_trans h₁ h₃, is_order_connected.neg_trans h₄ h₂⟩, ..@is_asymm.is_irrefl α r _ } @[priority 100] -- see Note [lower instance priority] instance is_order_connected_of_is_strict_total_order' [is_strict_total_order' α r] : is_order_connected α r := ⟨λ a b c h, (trichotomous _ _).imp_right (λ o, o.elim (λ e, e ▸ h) (λ h', trans h' h))⟩ @[priority 100] -- see Note [lower instance priority] instance is_strict_total_order_of_is_strict_total_order' [is_strict_total_order' α r] : is_strict_total_order α r := {..is_strict_weak_order_of_is_order_connected} instance [linear_order α] : is_strict_total_order α (<) := by apply_instance instance [linear_order α] : is_order_connected α (<) := by apply_instance instance [linear_order α] : is_incomp_trans α (<) := by apply_instance instance [linear_order α] : is_strict_weak_order α (<) := by apply_instance /-- An extensional relation is one in which an element is determined by its set of predecessors. It is named for the `x ∈ y` relation in set theory, whose extensionality is one of the first axioms of ZFC. -/ @[algebra] class is_extensional (α : Type u) (r : α → α → Prop) : Prop := (ext : ∀ a b, (∀ x, r x a ↔ r x b) → a = b) @[priority 100] -- see Note [lower instance priority] instance is_extensional_of_is_strict_total_order' [is_strict_total_order' α r] : is_extensional α r := ⟨λ a b H, ((@trichotomous _ r _ a b) .resolve_left $ mt (H _).2 (irrefl a)) .resolve_right $ mt (H _).1 (irrefl b)⟩ /-- A well order is a well-founded linear order. -/ @[algebra] class is_well_order (α : Type u) (r : α → α → Prop) extends is_strict_total_order' α r : Prop := (wf : well_founded r) @[priority 100] -- see Note [lower instance priority] instance is_well_order.is_strict_total_order {α} (r : α → α → Prop) [is_well_order α r] : is_strict_total_order α r := by apply_instance @[priority 100] -- see Note [lower instance priority] instance is_well_order.is_extensional {α} (r : α → α → Prop) [is_well_order α r] : is_extensional α r := by apply_instance @[priority 100] -- see Note [lower instance priority] instance is_well_order.is_trichotomous {α} (r : α → α → Prop) [is_well_order α r] : is_trichotomous α r := by apply_instance @[priority 100] -- see Note [lower instance priority] instance is_well_order.is_trans {α} (r : α → α → Prop) [is_well_order α r] : is_trans α r := by apply_instance @[priority 100] -- see Note [lower instance priority] instance is_well_order.is_irrefl {α} (r : α → α → Prop) [is_well_order α r] : is_irrefl α r := by apply_instance @[priority 100] -- see Note [lower instance priority] instance is_well_order.is_asymm {α} (r : α → α → Prop) [is_well_order α r] : is_asymm α r := by apply_instance /-- Construct a decidable linear order from a well-founded linear order. -/ noncomputable def is_well_order.decidable_linear_order (r : α → α → Prop) [is_well_order α r] : decidable_linear_order α := by { haveI := linear_order_of_STO' r, exact classical.DLO α } instance empty_relation.is_well_order [subsingleton α] : is_well_order α empty_relation := { trichotomous := λ a b, or.inr $ or.inl $ subsingleton.elim _ _, irrefl := λ a, id, trans := λ a b c, false.elim, wf := ⟨λ a, ⟨_, λ y, false.elim⟩⟩ } instance nat.lt.is_well_order : is_well_order ℕ (<) := ⟨nat.lt_wf⟩ instance sum.lex.is_well_order [is_well_order α r] [is_well_order β s] : is_well_order (α ⊕ β) (sum.lex r s) := { trichotomous := λ a b, by cases a; cases b; simp; apply trichotomous, irrefl := λ a, by cases a; simp; apply irrefl, trans := λ a b c, by cases a; cases b; simp; cases c; simp; apply trans, wf := sum.lex_wf is_well_order.wf is_well_order.wf } instance prod.lex.is_well_order [is_well_order α r] [is_well_order β s] : is_well_order (α × β) (prod.lex r s) := { trichotomous := λ ⟨a₁, a₂⟩ ⟨b₁, b₂⟩, match @trichotomous _ r _ a₁ b₁ with | or.inl h₁ := or.inl $ prod.lex.left _ _ h₁ | or.inr (or.inr h₁) := or.inr $ or.inr $ prod.lex.left _ _ h₁ | or.inr (or.inl e) := e ▸ match @trichotomous _ s _ a₂ b₂ with | or.inl h := or.inl $ prod.lex.right _ h | or.inr (or.inr h) := or.inr $ or.inr $ prod.lex.right _ h | or.inr (or.inl e) := e ▸ or.inr $ or.inl rfl end end, irrefl := λ ⟨a₁, a₂⟩ h, by cases h with _ _ _ _ h _ _ _ h; [exact irrefl _ h, exact irrefl _ h], trans := λ a b c h₁ h₂, begin cases h₁ with a₁ a₂ b₁ b₂ ab a₁ b₁ b₂ ab; cases h₂ with _ _ c₁ c₂ bc _ _ c₂ bc, { exact prod.lex.left _ _ (trans ab bc) }, { exact prod.lex.left _ _ ab }, { exact prod.lex.left _ _ bc }, { exact prod.lex.right _ (trans ab bc) } end, wf := prod.lex_wf is_well_order.wf is_well_order.wf } /-- An unbounded or cofinal set -/ def unbounded (r : α → α → Prop) (s : set α) : Prop := ∀ a, ∃ b ∈ s, ¬ r b a /-- A bounded or final set -/ def bounded (r : α → α → Prop) (s : set α) : Prop := ∃a, ∀ b ∈ s, r b a @[simp] lemma not_bounded_iff {r : α → α → Prop} (s : set α) : ¬bounded r s ↔ unbounded r s := begin classical, simp only [bounded, unbounded, not_forall, not_exists, exists_prop, not_and, not_not] end @[simp] lemma not_unbounded_iff {r : α → α → Prop} (s : set α) : ¬unbounded r s ↔ bounded r s := by { classical, rw [not_iff_comm, not_bounded_iff] }
cf73faffdba2963ab3bf108a81cf1c3b0dccadee
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/order/filter/lift.lean
c8d7f8a1e8605f0e53ff0c0712397bf8d05d97fe
[ "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
19,441
lean
/- Copyright (c) 2019 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl -/ import order.filter.bases /-! # Lift filters along filter and set functions > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. -/ open set open_locale classical filter namespace filter variables {α : Type*} {β : Type*} {γ : Type*} {ι : Sort*} section lift /-- A variant on `bind` using a function `g` taking a set instead of a member of `α`. This is essentially a push-forward along a function mapping each set to a filter. -/ protected def lift (f : filter α) (g : set α → filter β) := ⨅s ∈ f, g s variables {f f₁ f₂ : filter α} {g g₁ g₂ : set α → filter β} @[simp] lemma lift_top (g : set α → filter β) : (⊤ : filter α).lift g = g univ := by simp [filter.lift] /-- If `(p : ι → Prop, s : ι → set α)` is a basis of a filter `f`, `g` is a monotone function `set α → filter γ`, and for each `i`, `(pg : β i → Prop, sg : β i → set α)` is a basis of the filter `g (s i)`, then `(λ (i : ι) (x : β i), p i ∧ pg i x, λ (i : ι) (x : β i), sg i x)` is a basis of the filter `f.lift g`. This basis is parametrized by `i : ι` and `x : β i`, so in order to formulate this fact using `has_basis` one has to use `Σ i, β i` as the index type, see `filter.has_basis.lift`. This lemma states the corresponding `mem_iff` statement without using a sigma type. -/ lemma has_basis.mem_lift_iff {ι} {p : ι → Prop} {s : ι → set α} {f : filter α} (hf : f.has_basis p s) {β : ι → Type*} {pg : Π i, β i → Prop} {sg : Π i, β i → set γ} {g : set α → filter γ} (hg : ∀ i, (g $ s i).has_basis (pg i) (sg i)) (gm : monotone g) {s : set γ} : s ∈ f.lift g ↔ ∃ (i : ι) (hi : p i) (x : β i) (hx : pg i x), sg i x ⊆ s := begin refine (mem_binfi_of_directed _ ⟨univ, univ_sets _⟩).trans _, { intros t₁ ht₁ t₂ ht₂, exact ⟨t₁ ∩ t₂, inter_mem ht₁ ht₂, gm $ inter_subset_left _ _, gm $ inter_subset_right _ _⟩ }, { simp only [← (hg _).mem_iff], exact hf.exists_iff (λ t₁ t₂ ht H, gm ht H) } end /-- If `(p : ι → Prop, s : ι → set α)` is a basis of a filter `f`, `g` is a monotone function `set α → filter γ`, and for each `i`, `(pg : β i → Prop, sg : β i → set α)` is a basis of the filter `g (s i)`, then `(λ (i : ι) (x : β i), p i ∧ pg i x, λ (i : ι) (x : β i), sg i x)` is a basis of the filter `f.lift g`. This basis is parametrized by `i : ι` and `x : β i`, so in order to formulate this fact using `has_basis` one has to use `Σ i, β i` as the index type. See also `filter.has_basis.mem_lift_iff` for the corresponding `mem_iff` statement formulated without using a sigma type. -/ lemma has_basis.lift {ι} {p : ι → Prop} {s : ι → set α} {f : filter α} (hf : f.has_basis p s) {β : ι → Type*} {pg : Π i, β i → Prop} {sg : Π i, β i → set γ} {g : set α → filter γ} (hg : ∀ i, (g $ s i).has_basis (pg i) (sg i)) (gm : monotone g) : (f.lift g).has_basis (λ i : Σ i, β i, p i.1 ∧ pg i.1 i.2) (λ i : Σ i, β i, sg i.1 i.2) := begin refine ⟨λ t, (hf.mem_lift_iff hg gm).trans _⟩, simp [sigma.exists, and_assoc, exists_and_distrib_left] end lemma mem_lift_sets (hg : monotone g) {s : set β} : s ∈ f.lift g ↔ ∃t∈f, s ∈ g t := (f.basis_sets.mem_lift_iff (λ s, (g s).basis_sets) hg).trans $ by simp only [id, exists_mem_subset_iff] lemma sInter_lift_sets (hg : monotone g) : ⋂₀ {s | s ∈ f.lift g} = ⋂ s ∈ f, ⋂₀ {t | t ∈ g s} := by simp only [sInter_eq_bInter, mem_set_of_eq, filter.mem_sets, mem_lift_sets hg, Inter_exists, @Inter_comm _ (set β)] lemma mem_lift {s : set β} {t : set α} (ht : t ∈ f) (hs : s ∈ g t) : s ∈ f.lift g := le_principal_iff.mp $ show f.lift g ≤ 𝓟 s, from infi_le_of_le t $ infi_le_of_le ht $ le_principal_iff.mpr hs lemma lift_le {f : filter α} {g : set α → filter β} {h : filter β} {s : set α} (hs : s ∈ f) (hg : g s ≤ h) : f.lift g ≤ h := infi₂_le_of_le s hs hg lemma le_lift {f : filter α} {g : set α → filter β} {h : filter β} : h ≤ f.lift g ↔ ∀ s ∈ f, h ≤ g s := le_infi₂_iff lemma lift_mono (hf : f₁ ≤ f₂) (hg : g₁ ≤ g₂) : f₁.lift g₁ ≤ f₂.lift g₂ := infi_mono $ λ s, infi_mono' $ λ hs, ⟨hf hs, hg s⟩ lemma lift_mono' (hg : ∀s ∈ f, g₁ s ≤ g₂ s) : f.lift g₁ ≤ f.lift g₂ := infi₂_mono hg lemma tendsto_lift {m : γ → β} {l : filter γ} : tendsto m l (f.lift g) ↔ ∀ s ∈ f, tendsto m l (g s) := by simp only [filter.lift, tendsto_infi] lemma map_lift_eq {m : β → γ} (hg : monotone g) : map m (f.lift g) = f.lift (map m ∘ g) := have monotone (map m ∘ g), from map_mono.comp hg, filter.ext $ λ s, by simp only [mem_lift_sets hg, mem_lift_sets this, exists_prop, mem_map, function.comp_app] lemma comap_lift_eq {m : γ → β} : comap m (f.lift g) = f.lift (comap m ∘ g) := by simp only [filter.lift, comap_infi] theorem comap_lift_eq2 {m : β → α} {g : set β → filter γ} (hg : monotone g) : (comap m f).lift g = f.lift (g ∘ preimage m) := le_antisymm (le_infi₂ $ λ s hs, infi₂_le (m ⁻¹' s) ⟨s, hs, subset.rfl⟩) (le_infi₂ $ λ s ⟨s', hs', (h_sub : m ⁻¹' s' ⊆ s)⟩, infi₂_le_of_le s' hs' $ hg h_sub) lemma lift_map_le {g : set β → filter γ} {m : α → β} : (map m f).lift g ≤ f.lift (g ∘ image m) := le_lift.2 $ λ s hs, lift_le (image_mem_map hs) le_rfl lemma map_lift_eq2 {g : set β → filter γ} {m : α → β} (hg : monotone g) : (map m f).lift g = f.lift (g ∘ image m) := lift_map_le.antisymm $ le_lift.2 $ λ s hs, lift_le hs $ hg $ image_preimage_subset _ _ 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_lift.2 $ λ s hs, lift_le hs $ lift_le hs le_rfl 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) := lift_lift_same_le_lift.antisymm $ le_lift.2 $ λ s hs, le_lift.2 $ λ t ht, lift_le (inter_mem 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) : (𝓟 s).lift g = g s := (lift_le (mem_principal_self _) le_rfl).antisymm (le_lift.2 $ λ t ht, hg ht) 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_ne_bot_iff (hm : monotone g) : (ne_bot $ f.lift g) ↔ (∀s∈f, ne_bot (g s)) := by simp only [ne_bot_iff, ne.def, ← empty_mem_iff_bot, mem_lift_sets hm, not_exists] @[simp] lemma lift_const {f : filter α} {g : filter β} : f.lift (λx, g) = g := infi_subtype'.trans infi_const @[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 only [filter.lift, infi_inf_eq] @[simp] lemma lift_principal2 {f : filter α} : f.lift 𝓟 = f := le_antisymm (assume s hs, mem_lift hs (mem_principal_self s)) (le_infi $ assume s, le_infi $ assume hs, by simp only [hs, le_principal_iff]) lemma lift_infi_le {f : ι → filter α} {g : set α → filter β} : (infi f).lift g ≤ ⨅ i, (f i).lift g := le_infi $ λ i, lift_mono (infi_le _ _) le_rfl lemma lift_infi [nonempty ι] {f : ι → filter α} {g : set α → filter β} (hg : ∀ s t, g (s ∩ t) = g s ⊓ g t) : (infi f).lift g = (⨅i, (f i).lift g) := begin refine lift_infi_le.antisymm (λ s, _), have H : ∀ t ∈ infi f, (⨅ i, (f i).lift g) ≤ g t, { intros t ht, refine infi_sets_induct ht _ (λ i s t hs ht, _), { inhabit ι, exact infi₂_le_of_le default univ (infi_le _ univ_mem) }, { rw hg, exact le_inf (infi₂_le_of_le i s $ infi_le _ hs) ht } }, simp only [mem_lift_sets (monotone.of_map_inf hg), exists_imp_distrib], exact λ t ht hs, H t ht hs end lemma lift_infi_of_directed [nonempty ι] {f : ι → filter α} {g : set α → filter β} (hf : directed (≥) f) (hg : monotone g) : (infi f).lift g = (⨅i, (f i).lift g) := lift_infi_le.antisymm $ λ s, begin simp only [mem_lift_sets hg, exists_imp_distrib, mem_infi_of_directed hf], exact assume t i ht hs, mem_infi_of_mem i $ mem_lift ht hs end lemma lift_infi_of_map_univ {f : ι → filter α} {g : set α → filter β} (hg : ∀ s t, g (s ∩ t) = g s ⊓ g t) (hg' : g univ = ⊤) : (infi f).lift g = (⨅i, (f i).lift g) := begin casesI is_empty_or_nonempty ι, { simp [infi_of_empty, hg'] }, { exact lift_infi hg } end end lift section lift' /-- Specialize `lift` to functions `set α → set β`. This can be viewed as a generalization of `map`. This is essentially a push-forward along a function mapping each set to a set. -/ protected def lift' (f : filter α) (h : set α → set β) := f.lift (𝓟 ∘ h) variables {f f₁ f₂ : filter α} {h h₁ h₂ : set α → set β} @[simp] lemma lift'_top (h : set α → set β) : (⊤ : filter α).lift' h = 𝓟 (h univ) := lift_top _ lemma mem_lift' {t : set α} (ht : t ∈ f) : h t ∈ (f.lift' h) := le_principal_iff.mp $ show f.lift' h ≤ 𝓟 (h t), from infi_le_of_le t $ infi_le_of_le ht $ le_rfl lemma tendsto_lift' {m : γ → β} {l : filter γ} : tendsto m l (f.lift' h) ↔ ∀ s ∈ f, ∀ᶠ a in l, m a ∈ h s := by simp only [filter.lift', tendsto_lift, tendsto_principal] lemma has_basis.lift' {ι} {p : ι → Prop} {s} (hf : f.has_basis p s) (hh : monotone h) : (f.lift' h).has_basis p (h ∘ s) := begin refine ⟨λ t, (hf.mem_lift_iff _ (monotone_principal.comp hh)).trans _⟩, show ∀ i, (𝓟 (h (s i))).has_basis (λ j : unit, true) (λ (j : unit), h (s i)), from λ i, has_basis_principal _, simp only [exists_const] end lemma mem_lift'_sets (hh : monotone h) {s : set β} : s ∈ f.lift' h ↔ ∃ t ∈ f, h t ⊆ s := mem_lift_sets $ monotone_principal.comp hh lemma eventually_lift'_iff (hh : monotone h) {p : β → Prop} : (∀ᶠ y in f.lift' h, p y) ↔ (∃ t ∈ f, ∀ y ∈ h t, p y) := mem_lift'_sets hh lemma sInter_lift'_sets (hh : monotone h) : ⋂₀ {s | s ∈ f.lift' h} = ⋂ s ∈ f, h s := (sInter_lift_sets (monotone_principal.comp hh)).trans $ Inter₂_congr $ λ s hs, cInf_Ici lemma lift'_le {f : filter α} {g : set α → set β} {h : filter β} {s : set α} (hs : s ∈ f) (hg : 𝓟 (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, h₁ s ⊆ h₂ s) : f.lift' h₁ ≤ f.lift' h₂ := infi₂_mono $ λ s hs, principal_mono.mpr $ hh s hs lemma lift'_cong (hh : ∀s∈f, 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 ∘ 𝓟 ∘ h) : map_lift_eq $ monotone_principal.comp hh ... = f.lift' (image m ∘ h) : by simp only [(∘), filter.lift', map_principal, eq_self_iff_true] lemma lift'_map_le {g : set β → set γ} {m : α → β} : (map m f).lift' g ≤ f.lift' (g ∘ image m) := lift_map_le 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_principal.comp hg theorem comap_lift'_eq {m : γ → β} : comap m (f.lift' h) = f.lift' (preimage m ∘ h) := by simp only [filter.lift', comap_lift_eq, (∘), comap_principal] theorem comap_lift'_eq2 {m : β → α} {g : set β → set γ} (hg : monotone g) : (comap m f).lift' g = f.lift' (g ∘ preimage m) := comap_lift_eq2 $ monotone_principal.comp hg lemma lift'_principal {s : set α} (hh : monotone h) : (𝓟 s).lift' h = 𝓟 (h s) := lift_principal $ monotone_principal.comp hh lemma lift'_pure {a : α} (hh : monotone h) : (pure a : filter α).lift' h = 𝓟 (h {a}) := by rw [← principal_singleton, lift'_principal hh] lemma lift'_bot (hh : monotone h) : (⊥ : filter α).lift' h = 𝓟 (h ∅) := by rw [← principal_empty, lift'_principal hh] lemma le_lift' {f : filter α} {h : set α → set β} {g : filter β} : g ≤ f.lift' h ↔ ∀ s ∈ f, h s ∈ g := le_lift.trans $ forall₂_congr $ λ s hs, le_principal_iff lemma principal_le_lift' {t : set β} : 𝓟 t ≤ f.lift' h ↔ ∀ s ∈ f, t ⊆ h s := le_lift' 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, (𝓟 (g s)).lift h) : lift_assoc (monotone_principal.comp hg) ... = f.lift (λs, h (g s)) : by simp only [lift_principal, hh, eq_self_iff_true] 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_principal.comp hh) 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_principal.comp (hg₁ s)) (assume t, monotone_principal.comp (hg₂ t)) lemma lift'_inf_principal_eq {h : set α → set β} {s : set β} : f.lift' h ⊓ 𝓟 s = f.lift' (λt, h t ∩ s) := by simp only [filter.lift', filter.lift, (∘), ← inf_principal, infi_subtype', ← infi_inf] lemma lift'_ne_bot_iff (hh : monotone h) : (ne_bot (f.lift' h)) ↔ (∀s∈f, (h s).nonempty) := calc (ne_bot (f.lift' h)) ↔ (∀s∈f, ne_bot (𝓟 (h s))) : lift_ne_bot_iff (monotone_principal.comp hh) ... ↔ (∀s∈f, (h s).nonempty) : by simp only [principal_ne_bot_iff] @[simp] lemma lift'_id {f : filter α} : f.lift' id = f := lift_principal2 lemma lift'_infi [nonempty ι] {f : ι → filter α} {g : set α → set β} (hg : ∀ s t, g (s ∩ t) = g s ∩ g t) : (infi f).lift' g = (⨅ i, (f i).lift' g) := lift_infi $ λ s t, by rw [inf_principal, (∘), ← hg] lemma lift'_infi_of_map_univ {f : ι → filter α} {g : set α → set β} (hg : ∀{s t}, g (s ∩ t) = g s ∩ g t) (hg' : g univ = univ) : (infi f).lift' g = (⨅ i, (f i).lift' g) := lift_infi_of_map_univ (λ s t, by rw [inf_principal, (∘), ← hg]) (by rw [function.comp_app, hg', principal_univ]) lemma lift'_inf (f g : filter α) {s : set α → set β} (hs : ∀ t₁ t₂, s (t₁ ∩ t₂) = s t₁ ∩ s t₂) : (f ⊓ g).lift' s = f.lift' s ⊓ g.lift' s := have (⨅ b : bool, cond b f g).lift' s = ⨅ b : bool, (cond b f g).lift' s := lift'_infi @hs, by simpa only [infi_bool_eq] lemma lift'_inf_le (f g : filter α) (s : set α → set β) : (f ⊓ g).lift' s ≤ f.lift' s ⊓ g.lift' s := le_inf (lift'_mono inf_le_left le_rfl) (lift'_mono inf_le_right le_rfl) theorem comap_eq_lift' {f : filter β} {m : α → β} : comap m f = f.lift' (preimage m) := filter.ext $ λ s, (mem_lift'_sets monotone_preimage).symm end lift' section prod variables {f : filter α} lemma prod_def {f : filter α} {g : filter β} : f ×ᶠ g = (f.lift $ λ s, g.lift' $ λ t, s ×ˢ t) := have ∀(s:set α) (t : set β), 𝓟 (s ×ˢ t) = (𝓟 s).comap prod.fst ⊓ (𝓟 t).comap prod.snd, by simp only [principal_eq_iff_eq, comap_principal, inf_principal]; intros; refl, begin simp only [filter.lift', function.comp, this, lift_inf, lift_const, lift_inf], rw [← comap_lift_eq, ← comap_lift_eq], simp only [filter.prod, lift_principal2] end lemma prod_same_eq : f ×ᶠ f = f.lift' (λ t : set α, t ×ˢ t) := prod_def.trans $ lift_lift'_same_eq_lift' (λ s, monotone_const.set_prod monotone_id) (λ t, monotone_id.set_prod monotone_const) lemma mem_prod_same_iff {s : set (α×α)} : s ∈ f ×ᶠ f ↔ (∃t∈f, t ×ˢ t ⊆ s) := by { rw [prod_same_eq, mem_lift'_sets], exact monotone_id.set_prod monotone_id } lemma tendsto_prod_self_iff {f : α × α → β} {x : filter α} {y : filter β} : filter.tendsto f (x ×ᶠ x) y ↔ ∀ W ∈ y, ∃ U ∈ x, ∀ (x x' : α), x ∈ U → x' ∈ U → f (x, x') ∈ W := by simp only [tendsto_def, mem_prod_same_iff, prod_sub_preimage_iff, exists_prop, iff_self] variables {α₁ : Type*} {α₂ : Type*} {β₁ : Type*} {β₂ : Type*} lemma prod_lift_lift {f₁ : filter α₁} {f₂ : filter α₂} {g₁ : set α₁ → filter β₁} {g₂ : set α₂ → filter β₂} (hg₁ : monotone g₁) (hg₂ : monotone g₂) : (f₁.lift g₁) ×ᶠ (f₂.lift g₂) = f₁.lift (λs, f₂.lift (λt, g₁ s ×ᶠ g₂ t)) := begin simp only [prod_def, lift_assoc hg₁], apply congr_arg, funext x, rw [lift_comm], apply congr_arg, funext y, apply lift'_lift_assoc hg₂ end lemma prod_lift'_lift' {f₁ : filter α₁} {f₂ : filter α₂} {g₁ : set α₁ → set β₁} {g₂ : set α₂ → set β₂} (hg₁ : monotone g₁) (hg₂ : monotone g₂) : f₁.lift' g₁ ×ᶠ f₂.lift' g₂ = f₁.lift (λ s, f₂.lift' (λ t, g₁ s ×ˢ g₂ t)) := calc f₁.lift' g₁ ×ᶠ f₂.lift' g₂ = f₁.lift (λ s, f₂.lift (λ t, 𝓟 (g₁ s) ×ᶠ 𝓟 (g₂ t))) : prod_lift_lift (monotone_principal.comp hg₁) (monotone_principal.comp hg₂) ... = f₁.lift (λ s, f₂.lift (λ t, 𝓟 (g₁ s ×ˢ g₂ t))) : by simp only [prod_principal_principal] end prod end filter
1d1f4e9598442fe3426b909d69f3e4b29e4d96ba
9dc8cecdf3c4634764a18254e94d43da07142918
/src/measure_theory/function/convergence_in_measure.lean
060a8a5bb4a5454a69b4e2e6c3087e80df3fad79
[ "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
16,891
lean
/- Copyright (c) 2022 Rémy Degenne, Kexing Ying. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Rémy Degenne, Kexing Ying -/ import measure_theory.function.egorov /-! # Convergence in measure We define convergence in measure which is one of the many notions of convergence in probability. A sequence of functions `f` is said to converge in measure to some function `g` if for all `ε > 0`, the measure of the set `{x | ε ≤ dist (f i x) (g x)}` tends to 0 as `i` converges along some given filter `l`. Convergence in measure is most notably used in the formulation of the weak law of large numbers and is also useful in theorems such as the Vitali convergence theorem. This file provides some basic lemmas for working with convergence in measure and establishes some relations between convergence in measure and other notions of convergence. ## Main definitions * `measure_theory.tendsto_in_measure (μ : measure α) (f : ι → α → E) (g : α → E)`: `f` converges in `μ`-measure to `g`. ## Main results * `measure_theory.tendsto_in_measure_of_tendsto_ae`: convergence almost everywhere in a finite measure space implies convergence in measure. * `measure_theory.tendsto_in_measure.exists_seq_tendsto_ae`: if `f` is a sequence of functions which converges in measure to `g`, then `f` has a subsequence which convergence almost everywhere to `g`. * `measure_theory.tendsto_in_measure_of_tendsto_snorm`: convergence in Lp implies convergence in measure. -/ open topological_space filter open_locale nnreal ennreal measure_theory topological_space namespace measure_theory variables {α ι E : Type*} {m : measurable_space α} {μ : measure α} /-- A sequence of functions `f` is said to converge in measure to some function `g` if for all `ε > 0`, the measure of the set `{x | ε ≤ dist (f i x) (g x)}` tends to 0 as `i` converges along some given filter `l`. -/ def tendsto_in_measure [has_dist E] {m : measurable_space α} (μ : measure α) (f : ι → α → E) (l : filter ι) (g : α → E) : Prop := ∀ ε (hε : 0 < ε), tendsto (λ i, μ {x | ε ≤ dist (f i x) (g x)}) l (𝓝 0) lemma tendsto_in_measure_iff_norm [seminormed_add_comm_group E] {l : filter ι} {f : ι → α → E} {g : α → E} : tendsto_in_measure μ f l g ↔ ∀ ε (hε : 0 < ε), tendsto (λ i, μ {x | ε ≤ ∥f i x - g x∥}) l (𝓝 0) := by simp_rw [tendsto_in_measure, dist_eq_norm] namespace tendsto_in_measure variables [has_dist E] {l : filter ι} {f f' : ι → α → E} {g g' : α → E} protected lemma congr' (h_left : ∀ᶠ i in l, f i =ᵐ[μ] f' i) (h_right : g =ᵐ[μ] g') (h_tendsto : tendsto_in_measure μ f l g) : tendsto_in_measure μ f' l g' := begin intros ε hε, suffices : (λ i, μ {x | ε ≤ dist (f' i x) (g' x)}) =ᶠ[l] (λ i, μ {x | ε ≤ dist (f i x) (g x)}), { rw tendsto_congr' this, exact h_tendsto ε hε, }, filter_upwards [h_left] with i h_ae_eq, refine measure_congr _, filter_upwards [h_ae_eq, h_right] with x hxf hxg, rw eq_iff_iff, change ε ≤ dist (f' i x) (g' x) ↔ ε ≤ dist (f i x) (g x), rw [hxg, hxf], end protected lemma congr (h_left : ∀ i, f i =ᵐ[μ] f' i) (h_right : g =ᵐ[μ] g') (h_tendsto : tendsto_in_measure μ f l g) : tendsto_in_measure μ f' l g' := tendsto_in_measure.congr' (eventually_of_forall h_left) h_right h_tendsto lemma congr_left (h : ∀ i, f i =ᵐ[μ] f' i) (h_tendsto : tendsto_in_measure μ f l g) : tendsto_in_measure μ f' l g := h_tendsto.congr h (eventually_eq.rfl) lemma congr_right (h : g =ᵐ[μ] g') (h_tendsto : tendsto_in_measure μ f l g) : tendsto_in_measure μ f l g' := h_tendsto.congr (λ i, eventually_eq.rfl) h end tendsto_in_measure section exists_seq_tendsto_ae variables [metric_space E] variables {f : ℕ → α → E} {g : α → E} /-- Auxiliary lemma for `tendsto_in_measure_of_tendsto_ae`. -/ lemma tendsto_in_measure_of_tendsto_ae_of_strongly_measurable [is_finite_measure μ] (hf : ∀ n, strongly_measurable (f n)) (hg : strongly_measurable g) (hfg : ∀ᵐ x ∂μ, tendsto (λ n, f n x) at_top (𝓝 (g x))) : tendsto_in_measure μ f at_top g := begin refine λ ε hε, ennreal.tendsto_at_top_zero.mpr (λ δ hδ, _), by_cases hδi : δ = ∞, { simp only [hδi, implies_true_iff, le_top, exists_const], }, lift δ to ℝ≥0 using hδi, rw [gt_iff_lt, ennreal.coe_pos, ← nnreal.coe_pos] at hδ, obtain ⟨t, htm, ht, hunif⟩ := tendsto_uniformly_on_of_ae_tendsto' hf hg hfg hδ, rw ennreal.of_real_coe_nnreal at ht, rw metric.tendsto_uniformly_on_iff at hunif, obtain ⟨N, hN⟩ := eventually_at_top.1 (hunif ε hε), refine ⟨N, λ n hn, _⟩, suffices : {x : α | ε ≤ dist (f n x) (g x)} ⊆ t, from (measure_mono this).trans ht, rw ← set.compl_subset_compl, intros x hx, rw [set.mem_compl_eq, set.nmem_set_of_eq, dist_comm, not_le], exact hN n hn x hx, end /-- Convergence a.e. implies convergence in measure in a finite measure space. -/ lemma tendsto_in_measure_of_tendsto_ae [is_finite_measure μ] (hf : ∀ n, ae_strongly_measurable (f n) μ) (hfg : ∀ᵐ x ∂μ, tendsto (λ n, f n x) at_top (𝓝 (g x))) : tendsto_in_measure μ f at_top g := begin have hg : ae_strongly_measurable g μ, from ae_strongly_measurable_of_tendsto_ae _ hf hfg, refine tendsto_in_measure.congr (λ i, (hf i).ae_eq_mk.symm) hg.ae_eq_mk.symm _, refine tendsto_in_measure_of_tendsto_ae_of_strongly_measurable (λ i, (hf i).strongly_measurable_mk) hg.strongly_measurable_mk _, have hf_eq_ae : ∀ᵐ x ∂μ, ∀ n, (hf n).mk (f n) x = f n x, from ae_all_iff.mpr (λ n, (hf n).ae_eq_mk.symm), filter_upwards [hf_eq_ae, hg.ae_eq_mk, hfg] with x hxf hxg hxfg, rw [← hxg, funext (λ n, hxf n)], exact hxfg, end namespace exists_seq_tendsto_ae lemma exists_nat_measure_lt_two_inv (hfg : tendsto_in_measure μ f at_top g) (n : ℕ) : ∃ N, ∀ m ≥ N, μ {x | 2⁻¹ ^ n ≤ dist (f m x) (g x)} ≤ 2⁻¹ ^ n := begin specialize hfg (2⁻¹ ^ n) (by simp only [zero_lt_bit0, pow_pos, zero_lt_one, inv_pos]), rw ennreal.tendsto_at_top_zero at hfg, exact hfg (2⁻¹ ^ n) (pos_iff_ne_zero.mpr (λ h_zero, by simpa using pow_eq_zero h_zero)) end /-- Given a sequence of functions `f` which converges in measure to `g`, `seq_tendsto_ae_seq_aux` is a sequence such that `∀ m ≥ seq_tendsto_ae_seq_aux n, μ {x | 2⁻¹ ^ n ≤ dist (f m x) (g x)} ≤ 2⁻¹ ^ n`. -/ noncomputable def seq_tendsto_ae_seq_aux (hfg : tendsto_in_measure μ f at_top g) (n : ℕ) := classical.some (exists_nat_measure_lt_two_inv hfg n) /-- Transformation of `seq_tendsto_ae_seq_aux` to makes sure it is strictly monotone. -/ noncomputable def seq_tendsto_ae_seq (hfg : tendsto_in_measure μ f at_top g) : ℕ → ℕ | 0 := seq_tendsto_ae_seq_aux hfg 0 | (n + 1) := max (seq_tendsto_ae_seq_aux hfg (n + 1)) (seq_tendsto_ae_seq n + 1) lemma seq_tendsto_ae_seq_succ (hfg : tendsto_in_measure μ f at_top g) {n : ℕ} : seq_tendsto_ae_seq hfg (n + 1) = max (seq_tendsto_ae_seq_aux hfg (n + 1)) (seq_tendsto_ae_seq hfg n + 1) := by rw seq_tendsto_ae_seq lemma seq_tendsto_ae_seq_spec (hfg : tendsto_in_measure μ f at_top g) (n k : ℕ) (hn : seq_tendsto_ae_seq hfg n ≤ k) : μ {x | 2⁻¹ ^ n ≤ dist (f k x) (g x)} ≤ 2⁻¹ ^ n := begin cases n, { exact classical.some_spec (exists_nat_measure_lt_two_inv hfg 0) k hn }, { exact classical.some_spec (exists_nat_measure_lt_two_inv hfg _) _ (le_trans (le_max_left _ _) hn) } end lemma seq_tendsto_ae_seq_strict_mono (hfg : tendsto_in_measure μ f at_top g) : strict_mono (seq_tendsto_ae_seq hfg) := begin refine strict_mono_nat_of_lt_succ (λ n, _), rw seq_tendsto_ae_seq_succ, exact lt_of_lt_of_le (lt_add_one $ seq_tendsto_ae_seq hfg n) (le_max_right _ _), end end exists_seq_tendsto_ae /-- If `f` is a sequence of functions which converges in measure to `g`, then there exists a subsequence of `f` which converges a.e. to `g`. -/ lemma tendsto_in_measure.exists_seq_tendsto_ae (hfg : tendsto_in_measure μ f at_top g) : ∃ ns : ℕ → ℕ, strict_mono ns ∧ ∀ᵐ x ∂μ, tendsto (λ i, f (ns i) x) at_top (𝓝 (g x)) := begin /- Since `f` tends to `g` in measure, it has a subsequence `k ↦ f (ns k)` such that `μ {|f (ns k) - g| ≥ 2⁻ᵏ} ≤ 2⁻ᵏ` for all `k`. Defining `s := ⋂ k, ⋃ i ≥ k, {|f (ns k) - g| ≥ 2⁻ᵏ}`, we see that `μ s = 0` by the first Borel-Cantelli lemma. On the other hand, as `s` is precisely the set for which `f (ns k)` doesn't converge to `g`, `f (ns k)` converges almost everywhere to `g` as required. -/ have h_lt_ε_real : ∀ (ε : ℝ) (hε : 0 < ε), ∃ k : ℕ, 2 * 2⁻¹ ^ k < ε, { intros ε hε, obtain ⟨k, h_k⟩ : ∃ (k : ℕ), 2⁻¹ ^ k < ε := exists_pow_lt_of_lt_one hε (by norm_num), refine ⟨k + 1, (le_of_eq _).trans_lt h_k⟩, rw pow_add, ring }, set ns := exists_seq_tendsto_ae.seq_tendsto_ae_seq hfg, use ns, let S := λ k, {x | 2⁻¹ ^ k ≤ dist (f (ns k) x) (g x)}, have hμS_le : ∀ k, μ (S k) ≤ 2⁻¹ ^ k := λ k, exists_seq_tendsto_ae.seq_tendsto_ae_seq_spec hfg k (ns k) (le_rfl), set s := filter.at_top.limsup S with hs, have hμs : μ s = 0, { refine measure_limsup_eq_zero (ne_of_lt $ lt_of_le_of_lt (ennreal.tsum_le_tsum hμS_le) _), simp only [ennreal.tsum_geometric, ennreal.one_sub_inv_two, inv_inv], dec_trivial }, have h_tendsto : ∀ x ∈ sᶜ, tendsto (λ i, f (ns i) x) at_top (𝓝 (g x)), { refine λ x hx, metric.tendsto_at_top.mpr (λ ε hε, _), rw [hs, limsup_eq_infi_supr_of_nat] at hx, simp only [set.supr_eq_Union, set.infi_eq_Inter, set.compl_Inter, set.compl_Union, set.mem_Union, set.mem_Inter, set.mem_compl_eq, set.mem_set_of_eq, not_le] at hx, obtain ⟨N, hNx⟩ := hx, obtain ⟨k, hk_lt_ε⟩ := h_lt_ε_real ε hε, refine ⟨max N (k - 1), λ n hn_ge, lt_of_le_of_lt _ hk_lt_ε⟩, specialize hNx n ((le_max_left _ _).trans hn_ge), have h_inv_n_le_k : (2 : ℝ)⁻¹ ^ n ≤ 2 * 2⁻¹ ^ k, { rw [mul_comm, ← inv_mul_le_iff' (@two_pos ℝ _ _)], conv_lhs { congr, rw ← pow_one (2 : ℝ)⁻¹ }, rw [← pow_add, add_comm], exact pow_le_pow_of_le_one ((one_div (2 : ℝ)) ▸ one_half_pos.le) (inv_le_one one_le_two) ((le_tsub_add.trans (add_le_add_right (le_max_right _ _) 1)).trans (add_le_add_right hn_ge 1)) }, exact le_trans hNx.le h_inv_n_le_k }, rw ae_iff, refine ⟨exists_seq_tendsto_ae.seq_tendsto_ae_seq_strict_mono hfg, measure_mono_null (λ x, _) hμs⟩, rw [set.mem_set_of_eq, ← @not_not (x ∈ s), not_imp_not], exact h_tendsto x, end lemma tendsto_in_measure.exists_seq_tendsto_in_measure_at_top {u : filter ι} [ne_bot u] [is_countably_generated u] {f : ι → α → E} {g : α → E} (hfg : tendsto_in_measure μ f u g) : ∃ ns : ℕ → ι, tendsto_in_measure μ (λ n, f (ns n)) at_top g := begin obtain ⟨ns, h_tendsto_ns⟩ : ∃ (ns : ℕ → ι), tendsto ns at_top u := exists_seq_tendsto u, exact ⟨ns, λ ε hε, (hfg ε hε).comp h_tendsto_ns⟩, end lemma tendsto_in_measure.exists_seq_tendsto_ae' {u : filter ι} [ne_bot u] [is_countably_generated u] {f : ι → α → E} {g : α → E} (hfg : tendsto_in_measure μ f u g) : ∃ ns : ℕ → ι, ∀ᵐ x ∂μ, tendsto (λ i, f (ns i) x) at_top (𝓝 (g x)) := begin obtain ⟨ms, hms⟩ := hfg.exists_seq_tendsto_in_measure_at_top, obtain ⟨ns, -, hns⟩ := hms.exists_seq_tendsto_ae, exact ⟨ms ∘ ns, hns⟩, end end exists_seq_tendsto_ae section ae_measurable_of variables [measurable_space E] [normed_add_comm_group E] [borel_space E] lemma tendsto_in_measure.ae_measurable {u : filter ι} [ne_bot u] [is_countably_generated u] {f : ι → α → E} {g : α → E} (hf : ∀ n, ae_measurable (f n) μ) (h_tendsto : tendsto_in_measure μ f u g) : ae_measurable g μ := begin obtain ⟨ns, hns⟩ := h_tendsto.exists_seq_tendsto_ae', exact ae_measurable_of_tendsto_metrizable_ae at_top (λ n, hf (ns n)) hns, end end ae_measurable_of section tendsto_in_measure_of variables [normed_add_comm_group E] {p : ℝ≥0∞} variables {f : ι → α → E} {g : α → E} /-- This lemma is superceded by `measure_theory.tendsto_in_measure_of_tendsto_snorm` where we allow `p = ∞` and only require `ae_strongly_measurable`. -/ lemma tendsto_in_measure_of_tendsto_snorm_of_strongly_measurable (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) (hf : ∀ n, strongly_measurable (f n)) (hg : strongly_measurable g) {l : filter ι} (hfg : tendsto (λ n, snorm (f n - g) p μ) l (𝓝 0)) : tendsto_in_measure μ f l g := begin intros ε hε, replace hfg := ennreal.tendsto.const_mul (tendsto.ennrpow_const p.to_real hfg) (or.inr $ @ennreal.of_real_ne_top (1 / ε ^ (p.to_real))), simp only [mul_zero, ennreal.zero_rpow_of_pos (ennreal.to_real_pos hp_ne_zero hp_ne_top)] at hfg, rw ennreal.tendsto_nhds_zero at hfg ⊢, intros δ hδ, refine (hfg δ hδ).mono (λ n hn, _), refine le_trans _ hn, rw [ennreal.of_real_div_of_pos (real.rpow_pos_of_pos hε _), ennreal.of_real_one, mul_comm, mul_one_div, ennreal.le_div_iff_mul_le _ (or.inl (ennreal.of_real_ne_top)), mul_comm], { convert mul_meas_ge_le_pow_snorm' μ hp_ne_zero hp_ne_top ((hf n).sub hg).ae_strongly_measurable (ennreal.of_real ε), { exact (ennreal.of_real_rpow_of_pos hε).symm }, { ext x, rw [dist_eq_norm, ← ennreal.of_real_le_of_real_iff (norm_nonneg _), of_real_norm_eq_coe_nnnorm], exact iff.rfl } }, { rw [ne, ennreal.of_real_eq_zero, not_le], exact or.inl (real.rpow_pos_of_pos hε _) }, end /-- This lemma is superceded by `measure_theory.tendsto_in_measure_of_tendsto_snorm` where we allow `p = ∞`. -/ lemma tendsto_in_measure_of_tendsto_snorm_of_ne_top (hp_ne_zero : p ≠ 0) (hp_ne_top : p ≠ ∞) (hf : ∀ n, ae_strongly_measurable (f n) μ) (hg : ae_strongly_measurable g μ) {l : filter ι} (hfg : tendsto (λ n, snorm (f n - g) p μ) l (𝓝 0)) : tendsto_in_measure μ f l g := begin refine tendsto_in_measure.congr (λ i, (hf i).ae_eq_mk.symm) hg.ae_eq_mk.symm _, refine tendsto_in_measure_of_tendsto_snorm_of_strongly_measurable hp_ne_zero hp_ne_top (λ i, (hf i).strongly_measurable_mk) hg.strongly_measurable_mk _, have : (λ n, snorm ((hf n).mk (f n) - hg.mk g) p μ) = (λ n, snorm (f n - g) p μ), { ext1 n, refine snorm_congr_ae (eventually_eq.sub (hf n).ae_eq_mk.symm hg.ae_eq_mk.symm), }, rw this, exact hfg, end /-- See also `measure_theory.tendsto_in_measure_of_tendsto_snorm` which work for general Lp-convergence for all `p ≠ 0`. -/ lemma tendsto_in_measure_of_tendsto_snorm_top {E} [normed_add_comm_group E] {f : ι → α → E} {g : α → E} {l : filter ι} (hfg : tendsto (λ n, snorm (f n - g) ∞ μ) l (𝓝 0)) : tendsto_in_measure μ f l g := begin intros δ hδ, simp only [snorm_exponent_top, snorm_ess_sup] at hfg, rw ennreal.tendsto_nhds_zero at hfg ⊢, intros ε hε, specialize hfg ((ennreal.of_real δ) / 2) (ennreal.div_pos_iff.2 ⟨(ennreal.of_real_pos.2 hδ).ne.symm, ennreal.two_ne_top⟩), refine hfg.mono (λ n hn, _), simp only [true_and, gt_iff_lt, ge_iff_le, zero_tsub, zero_le, zero_add, set.mem_Icc, pi.sub_apply] at *, have : ess_sup (λ (x : α), (∥f n x - g x∥₊ : ℝ≥0∞)) μ < ennreal.of_real δ := lt_of_le_of_lt hn (ennreal.half_lt_self (ennreal.of_real_pos.2 hδ).ne.symm ennreal.of_real_lt_top.ne), refine ((le_of_eq _).trans (ae_lt_of_ess_sup_lt this).le).trans hε.le, congr' with x, simp only [ennreal.of_real_le_iff_le_to_real ennreal.coe_lt_top.ne, ennreal.coe_to_real, not_lt, coe_nnnorm, set.mem_set_of_eq, set.mem_compl_eq], rw ← dist_eq_norm (f n x) (g x), refl end /-- Convergence in Lp implies convergence in measure. -/ lemma tendsto_in_measure_of_tendsto_snorm {l : filter ι} (hp_ne_zero : p ≠ 0) (hf : ∀ n, ae_strongly_measurable (f n) μ) (hg : ae_strongly_measurable g μ) (hfg : tendsto (λ n, snorm (f n - g) p μ) l (𝓝 0)) : tendsto_in_measure μ f l g := begin by_cases hp_ne_top : p = ∞, { subst hp_ne_top, exact tendsto_in_measure_of_tendsto_snorm_top hfg }, { exact tendsto_in_measure_of_tendsto_snorm_of_ne_top hp_ne_zero hp_ne_top hf hg hfg } end /-- Convergence in Lp implies convergence in measure. -/ lemma tendsto_in_measure_of_tendsto_Lp [hp : fact (1 ≤ p)] {f : ι → Lp E p μ} {g : Lp E p μ} {l : filter ι} (hfg : tendsto f l (𝓝 g)) : tendsto_in_measure μ (λ n, f n) l g := tendsto_in_measure_of_tendsto_snorm (ennreal.zero_lt_one.trans_le hp.elim).ne.symm (λ n, Lp.ae_strongly_measurable _) (Lp.ae_strongly_measurable _) ((Lp.tendsto_Lp_iff_tendsto_ℒp' _ _).mp hfg) end tendsto_in_measure_of end measure_theory
9a0034fbc40145ed2e7df748bf8177562ee84e94
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/interactive/macroGoalIssue.lean
5371388aeb98437acbfa4490a7d6a436fa283574
[ "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
124
lean
theorem ex (n : Nat) : True := by have : n = 0 + n := by rw [Nat.zero_add] skip --^ $/lean/plainGoal exact True.intro
71bce0f558637a5d3415c53d378740d513c2e495
432d948a4d3d242fdfb44b81c9e1b1baacd58617
/src/data/polynomial/integral_normalization.lean
33fa41b18c1a0919fbfdfe1a42763c0cbf3daabd
[ "Apache-2.0" ]
permissive
JLimperg/aesop3
306cc6570c556568897ed2e508c8869667252e8a
a4a116f650cc7403428e72bd2e2c4cda300fe03f
refs/heads/master
1,682,884,916,368
1,620,320,033,000
1,620,320,033,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
5,555
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker -/ import data.polynomial.algebra_map import data.polynomial.monic /-! # Theory of monic polynomials We define `integral_normalization`, which relate arbitrary polynomials to monic ones. -/ open_locale big_operators namespace polynomial universes u v y variables {R : Type u} {S : Type v} {a b : R} {m n : ℕ} {ι : Type y} section integral_normalization section semiring variables [semiring R] /-- If `f : polynomial R` is a nonzero polynomial with root `z`, `integral_normalization f` is a monic polynomial with root `leading_coeff f * z`. Moreover, `integral_normalization 0 = 0`. -/ noncomputable def integral_normalization (f : polynomial R) : polynomial R := ∑ i in f.support, monomial i (if f.degree = i then 1 else coeff f i * f.leading_coeff ^ (f.nat_degree - 1 - i)) @[simp] lemma integral_normalization_zero : integral_normalization (0 : polynomial R) = 0 := by simp [integral_normalization] lemma integral_normalization_coeff {f : polynomial R} {i : ℕ} : (integral_normalization f).coeff i = if f.degree = i then 1 else coeff f i * f.leading_coeff ^ (f.nat_degree - 1 - i) := have f.coeff i = 0 → f.degree ≠ i, from λ hc hd, coeff_ne_zero_of_eq_degree hd hc, by simp [integral_normalization, coeff_monomial, this, mem_support_iff] {contextual := tt} lemma integral_normalization_support {f : polynomial R} : (integral_normalization f).support ⊆ f.support := by { intro, simp [integral_normalization, coeff_monomial, mem_support_iff] {contextual := tt} } lemma integral_normalization_coeff_degree {f : polynomial R} {i : ℕ} (hi : f.degree = i) : (integral_normalization f).coeff i = 1 := by rw [integral_normalization_coeff, if_pos hi] lemma integral_normalization_coeff_nat_degree {f : polynomial R} (hf : f ≠ 0) : (integral_normalization f).coeff (nat_degree f) = 1 := integral_normalization_coeff_degree (degree_eq_nat_degree hf) lemma integral_normalization_coeff_ne_degree {f : polynomial R} {i : ℕ} (hi : f.degree ≠ i) : coeff (integral_normalization f) i = coeff f i * f.leading_coeff ^ (f.nat_degree - 1 - i) := by rw [integral_normalization_coeff, if_neg hi] lemma integral_normalization_coeff_ne_nat_degree {f : polynomial R} {i : ℕ} (hi : i ≠ nat_degree f) : coeff (integral_normalization f) i = coeff f i * f.leading_coeff ^ (f.nat_degree - 1 - i) := integral_normalization_coeff_ne_degree (degree_ne_of_nat_degree_ne hi.symm) lemma monic_integral_normalization {f : polynomial R} (hf : f ≠ 0) : monic (integral_normalization f) := monic_of_degree_le f.nat_degree (finset.sup_le $ λ i h, with_bot.coe_le_coe.2 $ le_nat_degree_of_mem_supp i $ integral_normalization_support h) (integral_normalization_coeff_nat_degree hf) end semiring section domain variables [integral_domain R] @[simp] lemma support_integral_normalization {f : polynomial R} : (integral_normalization f).support = f.support := begin by_cases hf : f = 0, {simp [hf]}, ext i, refine ⟨λ h, integral_normalization_support h, _⟩, simp only [integral_normalization_coeff, mem_support_iff], intro hfi, split_ifs with hi; simp [hfi, hi, pow_ne_zero _ (leading_coeff_ne_zero.mpr hf)] end variables [comm_ring S] lemma integral_normalization_eval₂_eq_zero {p : polynomial R} (f : R →+* S) {z : S} (hz : eval₂ f z p = 0) (inj : ∀ (x : R), f x = 0 → x = 0) : eval₂ f (z * f p.leading_coeff) (integral_normalization p) = 0 := calc eval₂ f (z * f p.leading_coeff) (integral_normalization p) = p.support.attach.sum (λ i, f (coeff (integral_normalization p) i.1 * p.leading_coeff ^ i.1) * z ^ i.1) : by { rw [eval₂, sum_def, support_integral_normalization], simp only [mul_comm z, mul_pow, mul_assoc, ring_hom.map_pow, ring_hom.map_mul], exact finset.sum_attach.symm } ... = p.support.attach.sum (λ i, f (coeff p i.1 * p.leading_coeff ^ (nat_degree p - 1)) * z ^ i.1) : begin by_cases hp : p = 0, {simp [hp]}, have one_le_deg : 1 ≤ nat_degree p := nat.succ_le_of_lt (nat_degree_pos_of_eval₂_root hp f hz inj), congr' with i, congr' 2, by_cases hi : i.1 = nat_degree p, { rw [hi, integral_normalization_coeff_degree, one_mul, leading_coeff, ←pow_succ, nat.sub_add_cancel one_le_deg], exact degree_eq_nat_degree hp }, { have : i.1 ≤ p.nat_degree - 1 := nat.le_pred_of_lt (lt_of_le_of_ne (le_nat_degree_of_ne_zero (mem_support_iff.mp i.2)) hi), rw [integral_normalization_coeff_ne_nat_degree hi, mul_assoc, ←pow_add, nat.sub_add_cancel this] } end ... = f p.leading_coeff ^ (nat_degree p - 1) * eval₂ f z p : by { simp_rw [eval₂, sum_def, λ i, mul_comm (coeff p i), ring_hom.map_mul, ring_hom.map_pow, mul_assoc, ←finset.mul_sum], congr' 1, exact @finset.sum_attach _ _ p.support _ (λ i, f (p.coeff i) * z ^ i) } ... = 0 : by rw [hz, _root_.mul_zero] lemma integral_normalization_aeval_eq_zero [algebra R S] {f : polynomial R} {z : S} (hz : aeval z f = 0) (inj : ∀ (x : R), algebra_map R S x = 0 → x = 0) : aeval (z * algebra_map R S f.leading_coeff) (integral_normalization f) = 0 := integral_normalization_eval₂_eq_zero (algebra_map R S) hz inj end domain end integral_normalization end polynomial
8ca98581fb97a77e9f77995eca328b78bf5b6301
8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3
/src/algebra/gcd_monoid/finset.lean
43fb0af2ed3b9b3a3f846868e43aafd8fa7932e6
[ "Apache-2.0" ]
permissive
troyjlee/mathlib
e18d4b8026e32062ab9e89bc3b003a5d1cfec3f5
45e7eb8447555247246e3fe91c87066506c14875
refs/heads/master
1,689,248,035,046
1,629,470,528,000
1,629,470,528,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
6,899
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.finset.fold import algebra.gcd_monoid.multiset /-! # GCD and LCM operations on finsets ## Main definitions - `finset.gcd` - the greatest common denominator of a `finset` of elements of a `gcd_monoid` - `finset.lcm` - the least common multiple of a `finset` of elements of a `gcd_monoid` ## Implementation notes Many of the proofs use the lemmas `gcd.def` and `lcm.def`, which relate `finset.gcd` and `finset.lcm` to `multiset.gcd` and `multiset.lcm`. TODO: simplify with a tactic and `data.finset.lattice` ## Tags finset, gcd -/ variables {α β γ : Type*} namespace finset open multiset variables [comm_cancel_monoid_with_zero α] [nontrivial α] [gcd_monoid α] /-! ### lcm -/ section lcm /-- Least common multiple of a finite set -/ def lcm (s : finset β) (f : β → α) : α := s.fold gcd_monoid.lcm 1 f variables {s s₁ s₂ : finset β} {f : β → α} lemma lcm_def : s.lcm f = (s.1.map f).lcm := rfl @[simp] lemma lcm_empty : (∅ : finset β).lcm f = 1 := fold_empty @[simp] lemma lcm_dvd_iff {a : α} : s.lcm f ∣ a ↔ (∀b ∈ s, f b ∣ a) := begin apply iff.trans multiset.lcm_dvd, simp only [multiset.mem_map, and_imp, exists_imp_distrib], exact ⟨λ k b hb, k _ _ hb rfl, λ k a' b hb h, h ▸ k _ hb⟩, end lemma lcm_dvd {a : α} : (∀b ∈ s, f b ∣ a) → s.lcm f ∣ a := lcm_dvd_iff.2 lemma dvd_lcm {b : β} (hb : b ∈ s) : f b ∣ s.lcm f := lcm_dvd_iff.1 (dvd_refl _) _ hb @[simp] lemma lcm_insert [decidable_eq β] {b : β} : (insert b s : finset β).lcm f = gcd_monoid.lcm (f b) (s.lcm f) := begin by_cases h : b ∈ s, { rw [insert_eq_of_mem h, (lcm_eq_right_iff (f b) (s.lcm f) (multiset.normalize_lcm (s.1.map f))).2 (dvd_lcm h)] }, apply fold_insert h, end @[simp] lemma lcm_singleton {b : β} : ({b} : finset β).lcm f = normalize (f b) := multiset.lcm_singleton @[simp] lemma normalize_lcm : normalize (s.lcm f) = s.lcm f := by simp [lcm_def] lemma lcm_union [decidable_eq β] : (s₁ ∪ s₂).lcm f = gcd_monoid.lcm (s₁.lcm f) (s₂.lcm f) := finset.induction_on s₁ (by rw [empty_union, lcm_empty, lcm_one_left, normalize_lcm]) $ λ a s has ih, by rw [insert_union, lcm_insert, lcm_insert, ih, lcm_assoc] theorem lcm_congr {f g : β → α} (hs : s₁ = s₂) (hfg : ∀a ∈ s₂, f a = g a) : s₁.lcm f = s₂.lcm g := by { subst hs, exact finset.fold_congr hfg } lemma lcm_mono_fun {g : β → α} (h : ∀ b ∈ s, f b ∣ g b) : s.lcm f ∣ s.lcm g := lcm_dvd (λ b hb, dvd_trans (h b hb) (dvd_lcm hb)) lemma lcm_mono (h : s₁ ⊆ s₂) : s₁.lcm f ∣ s₂.lcm f := lcm_dvd $ assume b hb, dvd_lcm (h hb) end lcm /-! ### gcd -/ section gcd /-- Greatest common divisor of a finite set -/ def gcd (s : finset β) (f : β → α) : α := s.fold gcd_monoid.gcd 0 f variables {s s₁ s₂ : finset β} {f : β → α} lemma gcd_def : s.gcd f = (s.1.map f).gcd := rfl @[simp] lemma gcd_empty : (∅ : finset β).gcd f = 0 := fold_empty lemma dvd_gcd_iff {a : α} : a ∣ s.gcd f ↔ ∀b ∈ s, a ∣ f b := begin apply iff.trans multiset.dvd_gcd, simp only [multiset.mem_map, and_imp, exists_imp_distrib], exact ⟨λ k b hb, k _ _ hb rfl, λ k a' b hb h, h ▸ k _ hb⟩, end lemma gcd_dvd {b : β} (hb : b ∈ s) : s.gcd f ∣ f b := dvd_gcd_iff.1 (dvd_refl _) _ hb lemma dvd_gcd {a : α} : (∀b ∈ s, a ∣ f b) → a ∣ s.gcd f := dvd_gcd_iff.2 @[simp] lemma gcd_insert [decidable_eq β] {b : β} : (insert b s : finset β).gcd f = gcd_monoid.gcd (f b) (s.gcd f) := begin by_cases h : b ∈ s, { rw [insert_eq_of_mem h, (gcd_eq_right_iff (f b) (s.gcd f) (multiset.normalize_gcd (s.1.map f))).2 (gcd_dvd h)] ,}, apply fold_insert h, end @[simp] lemma gcd_singleton {b : β} : ({b} : finset β).gcd f = normalize (f b) := multiset.gcd_singleton @[simp] lemma normalize_gcd : normalize (s.gcd f) = s.gcd f := by simp [gcd_def] lemma gcd_union [decidable_eq β] : (s₁ ∪ s₂).gcd f = gcd_monoid.gcd (s₁.gcd f) (s₂.gcd f) := finset.induction_on s₁ (by rw [empty_union, gcd_empty, gcd_zero_left, normalize_gcd]) $ λ a s has ih, by rw [insert_union, gcd_insert, gcd_insert, ih, gcd_assoc] theorem gcd_congr {f g : β → α} (hs : s₁ = s₂) (hfg : ∀a ∈ s₂, f a = g a) : s₁.gcd f = s₂.gcd g := by { subst hs, exact finset.fold_congr hfg } lemma gcd_mono_fun {g : β → α} (h : ∀ b ∈ s, f b ∣ g b) : s.gcd f ∣ s.gcd g := dvd_gcd (λ b hb, dvd_trans (gcd_dvd hb) (h b hb)) lemma gcd_mono (h : s₁ ⊆ s₂) : s₂.gcd f ∣ s₁.gcd f := dvd_gcd $ assume b hb, gcd_dvd (h hb) theorem gcd_eq_zero_iff : s.gcd f = 0 ↔ ∀ (x : β), x ∈ s → f x = 0 := begin rw [gcd_def, multiset.gcd_eq_zero_iff], split; intro h, { intros b bs, apply h (f b), simp only [multiset.mem_map, mem_def.1 bs], use b, simp [mem_def.1 bs] }, { intros a as, rw multiset.mem_map at as, rcases as with ⟨b, ⟨bs, rfl⟩⟩, apply h b (mem_def.1 bs) } end lemma gcd_eq_gcd_filter_ne_zero [decidable_pred (λ (x : β), f x = 0)] : s.gcd f = (s.filter (λ x, f x ≠ 0)).gcd f := begin classical, transitivity ((s.filter (λ x, f x = 0)) ∪ (s.filter (λ x, f x ≠ 0))).gcd f, { rw filter_union_filter_neg_eq }, rw gcd_union, transitivity gcd_monoid.gcd (0 : α) _, { refine congr (congr rfl _) rfl, apply s.induction_on, { simp }, intros a s has h, rw filter_insert, split_ifs with h1; simp [h, h1], }, simp [gcd_zero_left, normalize_gcd], end lemma gcd_mul_left {a : α} : s.gcd (λ x, a * f x) = normalize a * s.gcd f := begin classical, apply s.induction_on, { simp }, intros b t hbt h, rw [gcd_insert, gcd_insert, h, ← gcd_mul_left], apply ((normalize_associated a).mul_right _).gcd_eq_right end lemma gcd_mul_right {a : α} : s.gcd (λ x, f x * a) = s.gcd f * normalize a := begin classical, apply s.induction_on, { simp }, intros b t hbt h, rw [gcd_insert, gcd_insert, h, ← gcd_mul_right], apply ((normalize_associated a).mul_left _).gcd_eq_right end end gcd end finset namespace finset section integral_domain variables [nontrivial β] [integral_domain α] [gcd_monoid α] lemma gcd_eq_of_dvd_sub {s : finset β} {f g : β → α} {a : α} (h : ∀ x : β, x ∈ s → a ∣ f x - g x) : gcd_monoid.gcd a (s.gcd f) = gcd_monoid.gcd a (s.gcd g) := begin classical, revert h, apply s.induction_on, { simp }, intros b s bs hi h, rw [gcd_insert, gcd_insert, gcd_comm (f b), ← gcd_assoc, hi (λ x hx, h _ (mem_insert_of_mem hx)), gcd_comm a, gcd_assoc, gcd_comm a (gcd_monoid.gcd _ _), gcd_comm (g b), gcd_assoc _ _ a, gcd_comm _ a], exact congr_arg _ (gcd_eq_of_dvd_sub_right (h _ (mem_insert_self _ _))) end end integral_domain end finset
89e21ebb46300a5ab4ba6dfc6f0aa7c26eb00bf4
41e069072396dcd54bd9fdadb27cfd35fd07016a
/src/defs.lean
a9b66204d339469a3942facd31384dc66def1bc5
[ "MIT" ]
permissive
semorrison/ModalTab
438ad601bd2631ab9cfe1e61f0d1337a36e2367e
cc94099194a2b69f84eb7a770b7aac711825179f
refs/heads/master
1,585,939,884,891
1,540,961,947,000
1,540,961,947,000
155,500,181
0
0
MIT
1,540,961,175,000
1,540,961,175,000
null
UTF-8
Lean
false
false
3,331
lean
/- Copyright (c) 2018 Minchao Wu. All rights reserved. Released under MIT license as described in the file LICENSE. Author: Minchao Wu -/ import data.list open nat tactic subtype inductive nnf : Type | var (n : nat) | neg (n : nat) | and (φ ψ : nnf) | or (φ ψ : nnf) | box (φ : nnf) | dia (φ : nnf) open nnf instance : inhabited nnf := ⟨nnf.var 0⟩ class no_literals (Γ : list nnf) := (no_var : ∀ {n}, var n ∉ Γ) (no_neg : ∀ {n}, neg n ∉ Γ) class saturated (Γ : list nnf) := (no_and : ∀ {φ ψ}, nnf.and φ ψ ∉ Γ) (no_or : ∀ {φ ψ}, nnf.or φ ψ ∉ Γ) class box_only (Γ : list nnf) extends no_literals Γ, saturated Γ := (no_dia : ∀ {φ}, nnf.dia φ ∉ Γ) def nnf.to_string : nnf → string | (var n) := "P" ++ n.repr | (neg n) := "¬P" ++ n.repr | (and φ ψ) := nnf.to_string φ ++ "∧" ++ nnf.to_string ψ | (or φ ψ) := nnf.to_string φ ++ "∨" ++ nnf.to_string ψ | (box φ) := "□" ++ "(" ++ nnf.to_string φ ++ ")" | (dia φ) := "◇" ++ "(" ++ nnf.to_string φ ++ ")" instance nnf_repr : has_repr nnf := ⟨nnf.to_string⟩ instance dec_eq_nnf : decidable_eq nnf := by mk_dec_eq_instance structure kripke (states : Type) := (val : ℕ → states → Prop) (rel : states → states → Prop) instance inhabited_kripke : inhabited (kripke ℕ) := ⟨{ val := λ a b, tt, rel := λ a b, tt }⟩ open nnf /- This has a better computaional behaviour than a forcing relation defined explicitly as an inductive predicate. -/ @[simp] def force {states : Type} (k : kripke states) : states → nnf → Prop | s (var n) := k.val n s | s (neg n) := ¬ k.val n s | s (and φ ψ) := force s φ ∧ force s ψ | s (or φ ψ) := force s φ ∨ force s ψ | s (box φ) := ∀ s', k.rel s s' → force s' φ | s (dia φ) := ∃ s', k.rel s s' ∧ force s' φ def sat {st} (k : kripke st) (s) (Γ : list nnf) : Prop := ∀ φ ∈ Γ, force k s φ def unsatisfiable (Γ : list nnf) : Prop := ∀ (st) (k : kripke st) s, ¬ sat k s Γ theorem unsat_singleton {φ} : unsatisfiable [φ] → ∀ (st) (k : kripke st) s, ¬ force k s φ := begin intro h, intros, intro hf, apply h, intros ψ hψ, rw list.mem_singleton at hψ, rw hψ, exact hf end theorem sat_of_empty {st} (k : kripke st) (s) : sat k s [] := λ φ h, absurd h $ list.not_mem_nil _ theorem ne_empty_of_unsat {Γ} (h : unsatisfiable Γ): Γ ≠ [] := begin intro heq, rw heq at h, apply h, apply sat_of_empty, exact nat, apply inhabited_kripke.1, exact 0 end /- Do not use map -/ @[simp] def node_size : list nnf → ℕ | [] := 0 | (hd :: tl) := sizeof hd + node_size tl inductive close_instance (Γ : list nnf) | cons : Π {n}, var n ∈ Γ → neg n ∈ Γ → close_instance inductive and_instance (Γ : list nnf) : list nnf → Type | cons : Π {φ ψ}, nnf.and φ ψ ∈ Γ → and_instance (φ :: ψ :: Γ.erase (nnf.and φ ψ)) inductive or_instance (Γ : list nnf) : list nnf → list nnf → Type | cons : Π {φ ψ}, nnf.or φ ψ ∈ Γ → or_instance (φ :: Γ.erase (nnf.or φ ψ)) (ψ :: Γ.erase (nnf.or φ ψ)) def left_prcp : Π {Γ₁ Γ₂ Δ : list nnf} (i : or_instance Δ Γ₁ Γ₂), nnf | Γ₁ Γ₂ Δ (@or_instance.cons _ φ ψ _) := φ
186e23859fff2d969e4abd92a5c6c17896b0a7f0
dd0f5513e11c52db157d2fcc8456d9401a6cd9da
/12_Axioms.org.1.lean
585ecffcfb1a9592654a08efe3f9fdc030dc1e9e
[]
no_license
cjmazey/lean-tutorial
ba559a49f82aa6c5848b9bf17b7389bf7f4ba645
381f61c9fcac56d01d959ae0fa6e376f2c4e3b34
refs/heads/master
1,610,286,098,832
1,447,124,923,000
1,447,124,923,000
43,082,433
0
0
null
null
null
null
UTF-8
Lean
false
false
107
lean
import standard namespace hide -- BEGIN axiom propext {a b : Prop} : (a ↔ b) → a = b -- END end hide
4b2e11e0b24ad10d6ef5869bb23fddc5b8288728
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/category_theory/limits/types.lean
f26d80d8efcccb02b0c888f3a68137a5cebf970f
[ "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
15,192
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Reid Barton -/ import category_theory.limits.shapes.images import category_theory.filtered import tactic.equiv_rw universes u open category_theory open category_theory.limits namespace category_theory.limits.types variables {J : Type u} [small_category J] /-- (internal implementation) the limit cone of a functor, implemented as flat sections of a pi type -/ def limit_cone (F : J ⥤ Type u) : cone F := { X := F.sections, π := { app := λ j u, u.val j } } local attribute [elab_simple] congr_fun /-- (internal implementation) the fact that the proposed limit cone is the limit -/ def limit_cone_is_limit (F : J ⥤ Type u) : is_limit (limit_cone F) := { lift := λ s v, ⟨λ j, s.π.app j v, λ j j' f, congr_fun (cone.w s f) _⟩, uniq' := by { intros, ext x j, exact congr_fun (w j) x } } /-- The category of types has all limits. See https://stacks.math.columbia.edu/tag/002U. -/ instance : has_limits (Type u) := { has_limits_of_shape := λ J 𝒥, by exactI { has_limit := λ F, has_limit.mk { cone := limit_cone F, is_limit := limit_cone_is_limit F } } } /-- The equivalence between a limiting cone of `F` in `Type u` and the "concrete" definition as the sections of `F`. -/ def is_limit_equiv_sections {F : J ⥤ Type u} {c : cone F} (t : is_limit c) : c.X ≃ F.sections := (is_limit.cone_point_unique_up_to_iso t (limit_cone_is_limit F)).to_equiv @[simp] lemma is_limit_equiv_sections_apply {F : J ⥤ Type u} {c : cone F} (t : is_limit c) (j : J) (x : c.X) : (((is_limit_equiv_sections t) x) : Π j, F.obj j) j = c.π.app j x := rfl @[simp] lemma is_limit_equiv_sections_symm_apply {F : J ⥤ Type u} {c : cone F} (t : is_limit c) (x : F.sections) (j : J) : c.π.app j ((is_limit_equiv_sections t).symm x) = (x : Π j, F.obj j) j := begin equiv_rw (is_limit_equiv_sections t).symm at x, simp, end /-- The equivalence between the abstract limit of `F` in `Type u` and the "concrete" definition as the sections of `F`. -/ noncomputable def limit_equiv_sections (F : J ⥤ Type u) : (limit F : Type u) ≃ F.sections := is_limit_equiv_sections (limit.is_limit _) @[simp] lemma limit_equiv_sections_apply (F : J ⥤ Type u) (x : limit F) (j : J) : (((limit_equiv_sections F) x) : Π j, F.obj j) j = limit.π F j x := rfl @[simp] lemma limit_equiv_sections_symm_apply (F : J ⥤ Type u) (x : F.sections) (j : J) : limit.π F j ((limit_equiv_sections F).symm x) = (x : Π j, F.obj j) j := is_limit_equiv_sections_symm_apply _ _ _ /-- Construct a term of `limit F : Type u` from a family of terms `x : Π j, F.obj j` which are "coherent": `∀ (j j') (f : j ⟶ j'), F.map f (x j) = x j'`. -/ @[ext] noncomputable def limit.mk (F : J ⥤ Type u) (x : Π j, F.obj j) (h : ∀ (j j') (f : j ⟶ j'), F.map f (x j) = x j') : (limit F : Type u) := (limit_equiv_sections F).symm ⟨x, h⟩ @[simp] lemma limit.π_mk (F : J ⥤ Type u) (x : Π j, F.obj j) (h : ∀ (j j') (f : j ⟶ j'), F.map f (x j) = x j') (j) : limit.π F j (limit.mk F x h) = x j := by { dsimp [limit.mk], simp, } -- PROJECT: prove this for concrete categories where the forgetful functor preserves limits @[ext] lemma limit_ext (F : J ⥤ Type u) (x y : limit F) (w : ∀ j, limit.π F j x = limit.π F j y) : x = y := begin apply (limit_equiv_sections F).injective, ext j, simp [w j], end lemma limit_ext_iff (F : J ⥤ Type u) (x y : limit F) : x = y ↔ (∀ j, limit.π F j x = limit.π F j y) := ⟨λ t _, t ▸ rfl, limit_ext _ _ _⟩ -- TODO: are there other limits lemmas that should have `_apply` versions? -- Can we generate these like with `@[reassoc]`? -- PROJECT: prove these for any concrete category where the forgetful functor preserves limits? @[simp] lemma limit.w_apply {F : J ⥤ Type u} {j j' : J} {x : limit F} (f : j ⟶ j') : F.map f (limit.π F j x) = limit.π F j' x := congr_fun (limit.w F f) x @[simp] lemma limit.lift_π_apply (F : J ⥤ Type u) (s : cone F) (j : J) (x : s.X) : limit.π F j (limit.lift F s x) = s.π.app j x := congr_fun (limit.lift_π s j) x @[simp] lemma limit.map_π_apply {F G : J ⥤ Type u} (α : F ⟶ G) (j : J) (x) : limit.π G j (lim_map α x) = α.app j (limit.π F j x) := congr_fun (lim_map_π α j) x /-- The relation defining the quotient type which implements the colimit of a functor `F : J ⥤ Type u`. See `category_theory.limits.types.quot`. -/ def quot.rel (F : J ⥤ Type u) : (Σ j, F.obj j) → (Σ j, F.obj j) → Prop := (λ p p', ∃ f : p.1 ⟶ p'.1, p'.2 = F.map f p.2) /-- A quotient type implementing the colimit of a functor `F : J ⥤ Type u`, as pairs `⟨j, x⟩` where `x : F.obj j`, modulo the equivalence relation generated by `⟨j, x⟩ ~ ⟨j', x'⟩` whenever there is a morphism `f : j ⟶ j'` so `F.map f x = x'`. -/ @[nolint has_inhabited_instance] def quot (F : J ⥤ Type u) : Type u := @quot (Σ j, F.obj j) (quot.rel F) /-- (internal implementation) the colimit cocone of a functor, implemented as a quotient of a sigma type -/ def colimit_cocone (F : J ⥤ Type u) : cocone F := { X := quot F, ι := { app := λ j x, quot.mk _ ⟨j, x⟩, naturality' := λ j j' f, funext $ λ x, eq.symm (quot.sound ⟨f, rfl⟩) } } local attribute [elab_with_expected_type] quot.lift /-- (internal implementation) the fact that the proposed colimit cocone is the colimit -/ def colimit_cocone_is_colimit (F : J ⥤ Type u) : is_colimit (colimit_cocone F) := { desc := λ s, quot.lift (λ (p : Σ j, F.obj j), s.ι.app p.1 p.2) (assume ⟨j, x⟩ ⟨j', x'⟩ ⟨f, hf⟩, by rw hf; exact (congr_fun (cocone.w s f) x).symm) } /-- The category of types has all colimits. See https://stacks.math.columbia.edu/tag/002U. -/ instance : has_colimits (Type u) := { has_colimits_of_shape := λ J 𝒥, by exactI { has_colimit := λ F, has_colimit.mk { cocone := colimit_cocone F, is_colimit := colimit_cocone_is_colimit F } } } /-- The equivalence between the abstract colimit of `F` in `Type u` and the "concrete" definition as a quotient. -/ noncomputable def colimit_equiv_quot (F : J ⥤ Type u) : (colimit F : Type u) ≃ quot F := (is_colimit.cocone_point_unique_up_to_iso (colimit.is_colimit F) (colimit_cocone_is_colimit F)).to_equiv @[simp] lemma colimit_equiv_quot_symm_apply (F : J ⥤ Type u) (j : J) (x : F.obj j) : (colimit_equiv_quot F).symm (quot.mk _ ⟨j, x⟩) = colimit.ι F j x := rfl @[simp] lemma colimit_equiv_quot_apply (F : J ⥤ Type u) (j : J) (x : F.obj j) : (colimit_equiv_quot F) (colimit.ι F j x) = quot.mk _ ⟨j, x⟩ := begin apply (colimit_equiv_quot F).symm.injective, simp, end @[simp] lemma colimit.w_apply {F : J ⥤ Type u} {j j' : J} {x : F.obj j} (f : j ⟶ j') : colimit.ι F j' (F.map f x) = colimit.ι F j x := congr_fun (colimit.w F f) x @[simp] lemma colimit.ι_desc_apply (F : J ⥤ Type u) (s : cocone F) (j : J) (x : F.obj j) : colimit.desc F s (colimit.ι F j x) = s.ι.app j x := congr_fun (colimit.ι_desc s j) x @[simp] lemma colimit.ι_map_apply {F G : J ⥤ Type u} (α : F ⟶ G) (j : J) (x) : colim.map α (colimit.ι F j x) = colimit.ι G j (α.app j x) := congr_fun (colimit.ι_map α j) x lemma colimit_sound {F : J ⥤ Type u} {j j' : J} {x : F.obj j} {x' : F.obj j'} (f : j ⟶ j') (w : F.map f x = x') : colimit.ι F j x = colimit.ι F j' x' := begin rw [←w], simp, end lemma colimit_sound' {F : J ⥤ Type u} {j j' : J} {x : F.obj j} {x' : F.obj j'} {j'' : J} (f : j ⟶ j'') (f' : j' ⟶ j'') (w : F.map f x = F.map f' x') : colimit.ι F j x = colimit.ι F j' x' := begin rw [←colimit.w _ f, ←colimit.w _ f'], rw [types_comp_apply, types_comp_apply, w], end lemma colimit_eq {F : J ⥤ Type u } {j j' : J} {x : F.obj j} {x' : F.obj j'} (w : colimit.ι F j x = colimit.ι F j' x') : eqv_gen (quot.rel F) ⟨j, x⟩ ⟨j', x'⟩ := begin apply quot.eq.1, simpa using congr_arg (colimit_equiv_quot F) w, end lemma jointly_surjective (F : J ⥤ Type u) {t : cocone F} (h : is_colimit t) (x : t.X) : ∃ j y, t.ι.app j y = x := begin suffices : (λ (x : t.X), ulift.up (∃ j y, t.ι.app j y = x)) = (λ _, ulift.up true), { have := congr_fun this x, have H := congr_arg ulift.down this, dsimp at H, rwa eq_true at H }, refine h.hom_ext _, intro j, ext y, erw iff_true, exact ⟨j, y, rfl⟩ end /-- A variant of `jointly_surjective` for `x : colimit F`. -/ lemma jointly_surjective' {F : J ⥤ Type u} (x : colimit F) : ∃ j y, colimit.ι F j y = x := jointly_surjective F (colimit.is_colimit _) x namespace filtered_colimit /- For filtered colimits of types, we can give an explicit description of the equivalence relation generated by the relation used to form the colimit. -/ variables (F : J ⥤ Type u) /-- An alternative relation on `Σ j, F.obj j`, which generates the same equivalence relation as we use to define the colimit in `Type` above, but that is more convenient when working with filtered colimits. Elements in `F.obj j` and `F.obj j'` are equivalent if there is some `k : J` to the right where their images are equal. -/ protected def rel (x y : Σ j, F.obj j) : Prop := ∃ k (f : x.1 ⟶ k) (g : y.1 ⟶ k), F.map f x.2 = F.map g y.2 lemma rel_of_quot_rel (x y : Σ j, F.obj j) : quot.rel F x y → filtered_colimit.rel F x y := λ ⟨f, h⟩, ⟨y.1, f, 𝟙 y.1, by rw [← h, functor_to_types.map_id_apply]⟩ lemma eqv_gen_quot_rel_of_rel (x y : Σ j, F.obj j) : filtered_colimit.rel F x y → eqv_gen (quot.rel F) x y := λ ⟨k, f, g, h⟩, eqv_gen.trans _ ⟨k, F.map f x.2⟩ _ (eqv_gen.rel _ _ ⟨f, rfl⟩) (eqv_gen.symm _ _ (eqv_gen.rel _ _ ⟨g, h⟩)) local attribute [elab_simple] nat_trans.app /-- Recognizing filtered colimits of types. -/ noncomputable def is_colimit_of (t : cocone F) (hsurj : ∀ (x : t.X), ∃ i xi, x = t.ι.app i xi) (hinj : ∀ i j xi xj, t.ι.app i xi = t.ι.app j xj → ∃ k (f : i ⟶ k) (g : j ⟶ k), F.map f xi = F.map g xj) : is_colimit t := -- Strategy: Prove that the map from "the" colimit of F (defined above) to t.X -- is a bijection. begin apply is_colimit.of_iso_colimit (colimit.is_colimit F), refine cocones.ext (equiv.to_iso (equiv.of_bijective _ _)) _, { exact colimit.desc F t }, { split, { show function.injective _, intros a b h, rcases jointly_surjective F (colimit.is_colimit F) a with ⟨i, xi, rfl⟩, rcases jointly_surjective F (colimit.is_colimit F) b with ⟨j, xj, rfl⟩, change (colimit.ι F i ≫ colimit.desc F t) xi = (colimit.ι F j ≫ colimit.desc F t) xj at h, rw [colimit.ι_desc, colimit.ι_desc] at h, rcases hinj i j xi xj h with ⟨k, f, g, h'⟩, change colimit.ι F i xi = colimit.ι F j xj, rw [←colimit.w F f, ←colimit.w F g], change colimit.ι F k (F.map f xi) = colimit.ι F k (F.map g xj), rw h' }, { show function.surjective _, intro x, rcases hsurj x with ⟨i, xi, rfl⟩, use colimit.ι F i xi, simp } }, { intro j, apply colimit.ι_desc } end variables [is_filtered_or_empty J] protected lemma rel_equiv : equivalence (filtered_colimit.rel F) := ⟨λ x, ⟨x.1, 𝟙 x.1, 𝟙 x.1, rfl⟩, λ x y ⟨k, f, g, h⟩, ⟨k, g, f, h.symm⟩, λ x y z ⟨k, f, g, h⟩ ⟨k', f', g', h'⟩, let ⟨l, fl, gl, _⟩ := is_filtered_or_empty.cocone_objs k k', ⟨m, n, hn⟩ := is_filtered_or_empty.cocone_maps (g ≫ fl) (f' ≫ gl) in ⟨m, f ≫ fl ≫ n, g' ≫ gl ≫ n, calc F.map (f ≫ fl ≫ n) x.2 = F.map (fl ≫ n) (F.map f x.2) : by simp ... = F.map (fl ≫ n) (F.map g y.2) : by rw h ... = F.map ((g ≫ fl) ≫ n) y.2 : by simp ... = F.map ((f' ≫ gl) ≫ n) y.2 : by rw hn ... = F.map (gl ≫ n) (F.map f' y.2) : by simp ... = F.map (gl ≫ n) (F.map g' z.2) : by rw h' ... = F.map (g' ≫ gl ≫ n) z.2 : by simp⟩⟩ protected lemma rel_eq_eqv_gen_quot_rel : filtered_colimit.rel F = eqv_gen (quot.rel F) := begin ext ⟨j, x⟩ ⟨j', y⟩, split, { apply eqv_gen_quot_rel_of_rel }, { rw ←(filtered_colimit.rel_equiv F).eqv_gen_iff, exact eqv_gen.mono (rel_of_quot_rel F) } end lemma colimit_eq_iff_aux {i j : J} {xi : F.obj i} {xj : F.obj j} : (colimit_cocone F).ι.app i xi = (colimit_cocone F).ι.app j xj ↔ filtered_colimit.rel F ⟨i, xi⟩ ⟨j, xj⟩ := begin change quot.mk _ _ = quot.mk _ _ ↔ _, rw [quot.eq, filtered_colimit.rel_eq_eqv_gen_quot_rel], end lemma is_colimit_eq_iff {t : cocone F} (ht : is_colimit t) {i j : J} {xi : F.obj i} {xj : F.obj j} : t.ι.app i xi = t.ι.app j xj ↔ ∃ k (f : i ⟶ k) (g : j ⟶ k), F.map f xi = F.map g xj := let t' := colimit_cocone F, e : t' ≅ t := is_colimit.unique_up_to_iso (colimit_cocone_is_colimit F) ht, e' : t'.X ≅ t.X := (cocones.forget _).map_iso e in begin refine iff.trans _ (colimit_eq_iff_aux F), convert e'.to_equiv.apply_eq_iff_eq; rw ←e.hom.w; refl end lemma colimit_eq_iff {i j : J} {xi : F.obj i} {xj : F.obj j} : colimit.ι F i xi = colimit.ι F j xj ↔ ∃ k (f : i ⟶ k) (g : j ⟶ k), F.map f xi = F.map g xj := is_colimit_eq_iff _ (colimit.is_colimit F) end filtered_colimit variables {α β : Type u} (f : α ⟶ β) section -- implementation of `has_image` /-- the image of a morphism in Type is just `set.range f` -/ def image : Type u := set.range f instance [inhabited α] : inhabited (image f) := { default := ⟨f (default α), ⟨_, rfl⟩⟩ } /-- the inclusion of `image f` into the target -/ def image.ι : image f ⟶ β := subtype.val instance : mono (image.ι f) := (mono_iff_injective _).2 subtype.val_injective variables {f} /-- the universal property for the image factorisation -/ noncomputable def image.lift (F' : mono_factorisation f) : image f ⟶ F'.I := (λ x, F'.e (classical.indefinite_description _ x.2).1 : image f → F'.I) lemma image.lift_fac (F' : mono_factorisation f) : image.lift F' ≫ F'.m = image.ι f := begin ext x, change (F'.e ≫ F'.m) _ = _, rw [F'.fac, (classical.indefinite_description _ x.2).2], refl, end end /-- the factorisation of any morphism in Type through a mono. -/ def mono_factorisation : mono_factorisation f := { I := image f, m := image.ι f, e := set.range_factorization f } /-- the facorisation through a mono has the universal property of the image. -/ noncomputable def is_image : is_image (mono_factorisation f) := { lift := image.lift, lift_fac' := image.lift_fac } instance : has_image f := has_image.mk ⟨_, is_image f⟩ instance : has_images (Type u) := { has_image := by apply_instance } instance : has_image_maps (Type u) := { has_image_map := λ f g st, has_image_map.transport st (mono_factorisation f.hom) (is_image g.hom) (λ x, ⟨st.right x.1, ⟨st.left (classical.some x.2), begin have p := st.w, replace p := congr_fun p (classical.some x.2), simp only [functor.id_map, types_comp_apply, subtype.val_eq_coe] at p, erw [p, classical.some_spec x.2], end⟩⟩) rfl } end category_theory.limits.types
6ae52d794278c94cd0daf33cb9acfb1e4d56ed5e
3dd1b66af77106badae6edb1c4dea91a146ead30
/tests/lean/run/ind7.lean
4d699c56137fe0ba45ac6a6f59c0159038147eee
[ "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
189
lean
namespace list inductive list (A : Type) : Type := | nil : list A | cons : A → list A → list A check list.{1} check cons.{1} check list_rec.{1 1} end check list.list.{1}
e4d2cd59e27ef26adf25e5d7b2023b7f242be687
4727251e0cd73359b15b664c3170e5d754078599
/src/deprecated/ring.lean
1d36df151c1d1bdb559109d03c318eed87c54136
[ "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,845
lean
/- Copyright (c) 2020 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import deprecated.group /-! # Unbundled semiring and ring homomorphisms (deprecated) This file defines structures for unbundled semiring and ring homomorphisms. Though bundled morphisms are now preferred, the unbundled structures are still occasionally used in mathlib, and probably will not go away before Lean 4 because Lean 3 often fails to coerce a bundled homomorphism to a function. ## Main Definitions `is_semiring_hom` (deprecated), `is_ring_hom` (deprecated) ## Tags is_semiring_hom, is_ring_hom -/ universes u v w variable {α : Type u} /-- Predicate for semiring homomorphisms (deprecated -- use the bundled `ring_hom` version). -/ structure is_semiring_hom {α : Type u} {β : Type v} [semiring α] [semiring β] (f : α → β) : Prop := (map_zero [] : f 0 = 0) (map_one [] : f 1 = 1) (map_add [] : ∀ {x y}, f (x + y) = f x + f y) (map_mul [] : ∀ {x y}, f (x * y) = f x * f y) namespace is_semiring_hom variables {β : Type v} [semiring α] [semiring β] variables {f : α → β} (hf : is_semiring_hom f) {x y : α} /-- The identity map is a semiring homomorphism. -/ lemma id : is_semiring_hom (@id α) := by refine {..}; intros; refl /-- The composition of two semiring homomorphisms is a semiring homomorphism. -/ lemma comp (hf : is_semiring_hom f) {γ} [semiring γ] {g : β → γ} (hg : is_semiring_hom g) : is_semiring_hom (g ∘ f) := { map_zero := by simpa [map_zero hf] using map_zero hg, map_one := by simpa [map_one hf] using map_one hg, map_add := λ x y, by simp [map_add hf, map_add hg], map_mul := λ x y, by simp [map_mul hf, map_mul hg] } /-- A semiring homomorphism is an additive monoid homomorphism. -/ lemma to_is_add_monoid_hom (hf : is_semiring_hom f) : is_add_monoid_hom f := { ..‹is_semiring_hom f› } /-- A semiring homomorphism is a monoid homomorphism. -/ lemma to_is_monoid_hom (hf : is_semiring_hom f) : is_monoid_hom f := { ..‹is_semiring_hom f› } end is_semiring_hom /-- Predicate for ring homomorphisms (deprecated -- use the bundled `ring_hom` version). -/ structure is_ring_hom {α : Type u} {β : Type v} [ring α] [ring β] (f : α → β) : Prop := (map_one [] : f 1 = 1) (map_mul [] : ∀ {x y}, f (x * y) = f x * f y) (map_add [] : ∀ {x y}, f (x + y) = f x + f y) namespace is_ring_hom variables {β : Type v} [ring α] [ring β] /-- A map of rings that is a semiring homomorphism is also a ring homomorphism. -/ lemma of_semiring {f : α → β} (H : is_semiring_hom f) : is_ring_hom f := {..H} variables {f : α → β} (hf : is_ring_hom f) {x y : α} /-- Ring homomorphisms map zero to zero. -/ lemma map_zero (hf : is_ring_hom f) : f 0 = 0 := calc f 0 = f (0 + 0) - f 0 : by rw [hf.map_add]; simp ... = 0 : by simp /-- Ring homomorphisms preserve additive inverses. -/ lemma map_neg (hf : is_ring_hom f) : f (-x) = -f x := calc f (-x) = f (-x + x) - f x : by rw [hf.map_add]; simp ... = -f x : by simp [hf.map_zero] /-- Ring homomorphisms preserve subtraction. -/ lemma map_sub (hf : is_ring_hom f) : f (x - y) = f x - f y := by simp [sub_eq_add_neg, hf.map_add, hf.map_neg] /-- The identity map is a ring homomorphism. -/ lemma id : is_ring_hom (@id α) := by refine {..}; intros; refl /-- The composition of two ring homomorphisms is a ring homomorphism. -/ -- see Note [no instance on morphisms] lemma comp (hf : is_ring_hom f) {γ} [ring γ] {g : β → γ} (hg : is_ring_hom g) : is_ring_hom (g ∘ f) := { map_add := λ x y, by simp [map_add hf]; rw map_add hg; refl, map_mul := λ x y, by simp [map_mul hf]; rw map_mul hg; refl, map_one := by simp [map_one hf]; exact map_one hg } /-- A ring homomorphism is also a semiring homomorphism. -/ lemma to_is_semiring_hom (hf : is_ring_hom f) : is_semiring_hom f := { map_zero := map_zero hf, ..‹is_ring_hom f› } lemma to_is_add_group_hom (hf : is_ring_hom f) : is_add_group_hom f := { map_add := hf.map_add } end is_ring_hom variables {β : Type v} {γ : Type w} [rα : semiring α] [rβ : semiring β] namespace ring_hom section include rα rβ /-- Interpret `f : α → β` with `is_semiring_hom f` as a ring homomorphism. -/ def of {f : α → β} (hf : is_semiring_hom f) : α →+* β := { to_fun := f, .. monoid_hom.of hf.to_is_monoid_hom, .. add_monoid_hom.of hf.to_is_add_monoid_hom } @[simp] lemma coe_of {f : α → β} (hf : is_semiring_hom f) : ⇑(of hf) = f := rfl lemma to_is_semiring_hom (f : α →+* β) : is_semiring_hom f := { map_zero := f.map_zero, map_one := f.map_one, map_add := f.map_add, map_mul := f.map_mul } end lemma to_is_ring_hom {α γ} [ring α] [ring γ] (g : α →+* γ) : is_ring_hom g := is_ring_hom.of_semiring g.to_is_semiring_hom end ring_hom
bc81e4c4680b339d45bf5d653f3a65bc8b64ea0d
4efff1f47634ff19e2f786deadd394270a59ecd2
/src/data/nat/multiplicity.lean
2bed5c0b72f6e39c7bfb2c7c793b3c847170ba2a
[ "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
9,478
lean
/- Copyright (c) 2019 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import data.nat.choose import ring_theory.multiplicity import data.nat.modeq import algebra.gcd_monoid /-! # Natural number multiplicity This file contains lemmas about the multiplicity function (the maximum prime power divding a number). # Main results There are natural number versions of some basic lemmas about multiplicity. There are also lemmas about the multiplicity of primes in factorials and in binomial coefficients. -/ open finset nat multiplicity open_locale big_operators namespace nat /-- The multiplicity of a divisor `m` of `n`, is the cardinality of the set of positive natural numbers `i` such that `p ^ i` divides `n`. The set is expressed by filtering `Ico 1 b` where `b` is any bound at least `n` -/ lemma multiplicity_eq_card_pow_dvd {m n b : ℕ} (hm1 : m ≠ 1) (hn0 : 0 < n) (hb : n ≤ b): multiplicity m n = ↑((finset.Ico 1 b).filter (λ i, m ^ i ∣ n)).card := calc multiplicity m n = ↑(Ico 1 $ ((multiplicity m n).get (finite_nat_iff.2 ⟨hm1, hn0⟩) + 1)).card : by simp ... = ↑((finset.Ico 1 b).filter (λ i, m ^ i ∣ n)).card : congr_arg coe $ congr_arg card $ finset.ext $ λ i, have hmn : ¬ m ^ n ∣ n, from if hm0 : m = 0 then λ _, by cases n; simp [*, lt_irrefl, nat.pow_succ] at * else mt (le_of_dvd hn0) (not_le_of_gt $ lt_pow_self (lt_of_le_of_ne (nat.pos_of_ne_zero hm0) hm1.symm) _), ⟨λ hi, begin simp only [Ico.mem, mem_filter, lt_succ_iff] at *, exact ⟨⟨hi.1, lt_of_le_of_lt hi.2 $ lt_of_lt_of_le (by rw [← enat.coe_lt_coe, enat.coe_get, multiplicity_lt_iff_neg_dvd, nat.pow_eq_pow]; exact hmn) hb⟩, by rw [← nat.pow_eq_pow, pow_dvd_iff_le_multiplicity]; rw [← @enat.coe_le_coe i, enat.coe_get] at hi; exact hi.2⟩ end, begin simp only [Ico.mem, mem_filter, lt_succ_iff, and_imp, true_and] { contextual := tt }, assume h1i hib hmin, rwa [← enat.coe_le_coe, enat.coe_get, ← pow_dvd_iff_le_multiplicity, nat.pow_eq_pow] end⟩ namespace prime lemma multiplicity_one {p : ℕ} (hp : p.prime) : multiplicity p 1 = 0 := by rw [multiplicity.one_right (mt nat.is_unit_iff.mp (ne_of_gt hp.one_lt))] lemma multiplicity_mul {p m n : ℕ} (hp : p.prime) : multiplicity p (m * n) = multiplicity p m + multiplicity p n := by rw [← int.coe_nat_multiplicity, ← int.coe_nat_multiplicity, ← int.coe_nat_multiplicity, int.coe_nat_mul, multiplicity.mul (nat.prime_iff_prime_int.1 hp)] lemma multiplicity_pow {p m n : ℕ} (hp : p.prime) : multiplicity p (m ^ n) = n •ℕ (multiplicity p m) := by induction n; simp [nat.pow_succ, hp.multiplicity_mul, *, hp.multiplicity_one, succ_nsmul, add_comm] lemma multiplicity_self {p : ℕ} (hp : p.prime) : multiplicity p p = 1 := have h₁ : ¬ is_unit (p : ℤ), from mt is_unit_int.1 (ne_of_gt hp.one_lt), have h₂ : (p : ℤ) ≠ 0, from int.coe_nat_ne_zero.2 hp.ne_zero, by rw [← int.coe_nat_multiplicity, multiplicity_self h₁ h₂] lemma multiplicity_pow_self {p n : ℕ} (hp : p.prime) : multiplicity p (p ^ n) = n := by induction n; simp [hp.multiplicity_one, nat.pow_succ, hp.multiplicity_mul, *, hp.multiplicity_self, succ_eq_add_one] /-- The multiplicity of a prime in `fact n` is the sum of the quotients `n / p ^ i`. This sum is expressed over the set `Ico 1 b` where `b` is any bound at least `n` -/ lemma multiplicity_fact {p : ℕ} (hp : p.prime) : ∀ {n b : ℕ}, n ≤ b → multiplicity p n.fact = (∑ i in Ico 1 b, n / p ^ i : ℕ) | 0 b hb := by simp [Ico, hp.multiplicity_one] | (n+1) b hb := calc multiplicity p (n+1).fact = multiplicity p n.fact + multiplicity p (n+1) : by rw [fact_succ, hp.multiplicity_mul, add_comm] ... = (∑ i in Ico 1 b, n / p ^ i : ℕ) + ((finset.Ico 1 b).filter (λ i, p ^ i ∣ n+1)).card : by rw [multiplicity_fact (le_of_succ_le hb), ← multiplicity_eq_card_pow_dvd (ne_of_gt hp.one_lt) (succ_pos _) hb] ... = (∑ i in Ico 1 b, (n / p ^ i + if p^i ∣ n+1 then 1 else 0) : ℕ) : by rw [sum_add_distrib, sum_boole]; simp ... = (∑ i in Ico 1 b, (n + 1) / p ^ i : ℕ) : congr_arg coe $ finset.sum_congr rfl (by intros; simp [nat.succ_div]; congr) /-- A prime power divides `fact n` iff it is at most the sum of the quotients `n / p ^ i`. This sum is expressed over the set `Ico 1 b` where `b` is any bound at least `n` -/ lemma pow_dvd_fact_iff {p : ℕ} {n r b : ℕ} (hp : p.prime) (hbn : n ≤ b) : p ^ r ∣ fact n ↔ r ≤ ∑ i in Ico 1 b, n / p ^ i := by rw [← enat.coe_le_coe, ← hp.multiplicity_fact hbn, ← pow_dvd_iff_le_multiplicity, nat.pow_eq_pow] lemma multiplicity_choose_aux {p n b k : ℕ} (hp : p.prime) (hkn : k ≤ n) : ∑ i in finset.Ico 1 b, n / p ^ i = ∑ i in finset.Ico 1 b, k / p ^ i + ∑ i in finset.Ico 1 b, (n - k) / p ^ i + ((finset.Ico 1 b).filter (λ i, p ^ i ≤ k % p ^ i + (n - k) % p ^ i)).card := calc ∑ i in finset.Ico 1 b, n / p ^ i = ∑ i in finset.Ico 1 b, (k + (n - k)) / p ^ i : by simp only [nat.add_sub_cancel' hkn] ... = ∑ i in finset.Ico 1 b, (k / p ^ i + (n - k) / p ^ i + if p ^ i ≤ k % p ^ i + (n - k) % p ^ i then 1 else 0) : by simp only [nat.add_div (nat.pow_pos hp.pos _)] ... = _ : begin simp only [sum_add_distrib], simp [sum_boole], end -- we have to use `sum_add_distrib` before `add_ite` fires. /-- The multiplity of `p` in `choose n k` is the number of carries when `k` and `n - k` are added in base `p`. The set is expressed by filtering `Ico 1 b` where `b` is any bound at least `n`. -/ lemma multiplicity_choose {p n k b : ℕ} (hp : p.prime) (hkn : k ≤ n) (hnb : n ≤ b) : multiplicity p (choose n k) = ((Ico 1 b).filter (λ i, p ^ i ≤ k % p ^ i + (n - k) % p ^ i)).card := have h₁ : multiplicity p (choose n k) + multiplicity p (k.fact * (n - k).fact) = ((finset.Ico 1 b).filter (λ i, p ^ i ≤ k % p ^ i + (n - k) % p ^ i)).card + multiplicity p (k.fact * (n - k).fact), begin rw [← hp.multiplicity_mul, ← mul_assoc, choose_mul_fact_mul_fact hkn, hp.multiplicity_fact hnb, hp.multiplicity_mul, hp.multiplicity_fact (le_trans hkn hnb), hp.multiplicity_fact (le_trans (nat.sub_le_self _ _) hnb), multiplicity_choose_aux hp hkn], simp [add_comm], end, (enat.add_right_cancel_iff (enat.ne_top_iff_dom.2 $ by exact finite_nat_iff.2 ⟨ne_of_gt hp.one_lt, mul_pos (fact_pos k) (fact_pos (n - k))⟩)).1 h₁ /-- A lower bound on the multiplicity of `p` in `choose n k`. -/ lemma multiplicity_le_multiplicity_choose_add {p : ℕ} (hp : p.prime) (n k : ℕ) : multiplicity p n ≤ multiplicity p (choose n k) + multiplicity p k := if hkn : n < k then by simp [choose_eq_zero_of_lt hkn] else if hk0 : k = 0 then by simp [hk0] else if hn0 : n = 0 then by cases k; simp [hn0, *] at * else begin rw [multiplicity_choose hp (le_of_not_gt hkn) (le_refl _), multiplicity_eq_card_pow_dvd (ne_of_gt hp.one_lt) (nat.pos_of_ne_zero hk0) (le_of_not_gt hkn), multiplicity_eq_card_pow_dvd (ne_of_gt hp.one_lt) (nat.pos_of_ne_zero hn0) (le_refl _), ← enat.coe_add, enat.coe_le_coe], calc ((Ico 1 n).filter (λ i, p ^ i ∣ n)).card ≤ ((Ico 1 n).filter (λ i, p ^ i ≤ k % p ^ i + (n - k) % p ^ i) ∪ (Ico 1 n).filter (λ i, p ^ i ∣ k) ).card : card_le_of_subset $ λ i, begin have := @le_mod_add_mod_of_dvd_add_of_not_dvd k (n - k) (p ^ i), simp [nat.add_sub_cancel' (le_of_not_gt hkn)] at * {contextual := tt}, tauto end ... ≤ ((Ico 1 n).filter (λ i, p ^ i ≤ k % p ^ i + (n - k) % p ^ i)).card + ((Ico 1 n).filter (λ i, p ^ i ∣ k)).card : card_union_le _ _ end lemma multiplicity_choose_prime_pow {p n k : ℕ} (hp : p.prime) (hkn : k ≤ p ^ n) (hk0 : 0 < k) : multiplicity p (choose (p ^ n) k) + multiplicity p k = n := le_antisymm (have hdisj : disjoint ((Ico 1 (p ^ n)).filter (λ i, p ^ i ≤ k % p ^ i + (p ^ n - k) % p ^ i)) ((Ico 1 (p ^ n)).filter (λ i, p ^ i ∣ k)), by simp [disjoint_right, *, dvd_iff_mod_eq_zero, nat.mod_lt _ (nat.pow_pos hp.pos _)] {contextual := tt}, have filter_subset_Ico : filter (λ i, p ^ i ≤ k % p ^ i + (p ^ n - k) % p ^ i ∨ p ^ i ∣ k) (Ico 1 (p ^ n)) ⊆ Ico 1 n.succ, from begin simp only [finset.subset_iff, Ico.mem, mem_filter, and_imp, true_and] {contextual := tt}, assume i h1i hip h, refine lt_succ_of_le (le_of_not_gt (λ hin, _)), have hpik : ¬ p ^ i ∣ k, from mt (le_of_dvd hk0) (not_le_of_gt (lt_of_le_of_lt hkn (pow_right_strict_mono hp.two_le hin))), have hpn : k % p ^ i + (p ^ n - k) % p ^ i < p ^ i, from calc k % p ^ i + (p ^ n - k) % p ^ i ≤ k + (p ^ n - k) : add_le_add (mod_le _ _) (mod_le _ _) ... = p ^ n : nat.add_sub_cancel' hkn ... < p ^ i : pow_right_strict_mono hp.two_le hin, simpa [hpik, not_le_of_gt hpn] using h end, begin rw [multiplicity_choose hp hkn (le_refl _), multiplicity_eq_card_pow_dvd (ne_of_gt hp.one_lt) hk0 hkn, ← enat.coe_add, enat.coe_le_coe, ← card_disjoint_union hdisj, filter_union_right], exact le_trans (card_le_of_subset filter_subset_Ico) (by simp) end) (by rw [← hp.multiplicity_pow_self]; exact multiplicity_le_multiplicity_choose_add hp _ _) end prime end nat
c510e44ecf34419dad6b84a1b2e6cb8e5377dd21
6b02ce66658141f3e0aa3dfa88cd30bbbb24d17b
/stage0/src/Lean/Meta/SizeOf.lean
93bf664cbb7586f0ce4ec68aacfcde54bf4f568e
[ "Apache-2.0" ]
permissive
pbrinkmeier/lean4
d31991fd64095e64490cb7157bcc6803f9c48af4
32fd82efc2eaf1232299e930ec16624b370eac39
refs/heads/master
1,681,364,001,662
1,618,425,427,000
1,618,425,427,000
358,314,562
0
0
Apache-2.0
1,618,504,558,000
1,618,501,999,000
null
UTF-8
Lean
false
false
21,981
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Meta.AppBuilder import Lean.Meta.Instances namespace Lean.Meta /-- Create `SizeOf` local instances for applicable parameters, and execute `k` using them. -/ private partial def mkLocalInstances {α} (params : Array Expr) (k : Array Expr → MetaM α) : MetaM α := loop 0 #[] where loop (i : Nat) (insts : Array Expr) : MetaM α := do if i < params.size then let param := params[i] let paramType ← inferType param let instType? ← forallTelescopeReducing paramType fun xs _ => do let type ← mkAppN param xs try let sizeOf ← mkAppM `SizeOf #[type] let instType ← mkForallFVars xs sizeOf return some instType catch _ => return none match instType? with | none => loop (i+1) insts | some instType => let instName ← mkFreshUserName `inst withLocalDecl instName BinderInfo.instImplicit instType fun inst => loop (i+1) (insts.push inst) else k insts /-- Return `some x` if `fvar` has type of the form `... -> motive ... fvar` where `motive` in `motiveFVars`. That is, `x` "produces" one of the recursor motives. -/ private def isInductiveHypothesis? (motiveFVars : Array Expr) (fvar : Expr) : MetaM (Option Expr) := do forallTelescopeReducing (← inferType fvar) fun _ type => if type.isApp && motiveFVars.contains type.getAppFn then return some type.appArg! else return none private def isInductiveHypothesis (motiveFVars : Array Expr) (fvar : Expr) : MetaM Bool := return (← isInductiveHypothesis? motiveFVars fvar).isSome /-- Let `motiveFVars` be free variables for each motive in a kernel recursor, and `minorFVars` the free variables for a minor premise. Then, return `some idx` if `minorFVars[idx]` has a type of the form `... -> motive ... fvar` for some `motive` in `motiveFVars`. -/ private def isRecField? (motiveFVars : Array Expr) (minorFVars : Array Expr) (fvar : Expr) : MetaM (Option Nat) := do let mut idx := 0 for minorFVar in minorFVars do if let some fvar' ← isInductiveHypothesis? motiveFVars minorFVar then if fvar == fvar' then return some idx idx := idx + 1 return none private partial def mkSizeOfMotives {α} (motiveFVars : Array Expr) (k : Array Expr → MetaM α) : MetaM α := loop 0 #[] where loop (i : Nat) (motives : Array Expr) : MetaM α := do if i < motiveFVars.size then let type ← inferType motiveFVars[i] let motive ← forallTelescopeReducing type fun xs _ => do mkLambdaFVars xs <| mkConst ``Nat trace[Meta.sizeOf] "motive: {motive}" loop (i+1) (motives.push motive) else k motives private partial def mkSizeOfMinors {α} (motiveFVars : Array Expr) (minorFVars : Array Expr) (minorFVars' : Array Expr) (k : Array Expr → MetaM α) : MetaM α := assert! minorFVars.size == minorFVars'.size loop 0 #[] where loop (i : Nat) (minors : Array Expr) : MetaM α := do if i < minorFVars.size then forallTelescopeReducing (← inferType minorFVars[i]) fun xs _ => forallBoundedTelescope (← inferType minorFVars'[i]) xs.size fun xs' _ => do let mut minor ← mkNumeral (mkConst ``Nat) 1 for x in xs, x' in xs' do unless (← isInductiveHypothesis motiveFVars x) do unless (← whnf (← inferType x)).isForall do -- we suppress higher-order fields match (← isRecField? motiveFVars xs x) with | some idx => minor ← mkAdd minor xs'[idx] | none => minor ← mkAdd minor (← mkAppM ``SizeOf.sizeOf #[x']) minor ← mkLambdaFVars xs' minor trace[Meta.sizeOf] "minor: {minor}" loop (i+1) (minors.push minor) else k minors /-- Create a "sizeOf" function with name `declName` using the recursor `recName`. -/ partial def mkSizeOfFn (recName : Name) (declName : Name): MetaM Unit := do trace[Meta.sizeOf] "recName: {recName}" let recInfo : RecursorVal ← getConstInfoRec recName forallTelescopeReducing recInfo.type fun xs type => let levelParams := recInfo.levelParams.tail! -- universe parameters for declaration being defined let params := xs[:recInfo.numParams] let motiveFVars := xs[recInfo.numParams : recInfo.numParams + recInfo.numMotives] let minorFVars := xs[recInfo.getFirstMinorIdx : recInfo.getFirstMinorIdx + recInfo.numMinors] let indices := xs[recInfo.getFirstIndexIdx : recInfo.getFirstIndexIdx + recInfo.numIndices] let major := xs[recInfo.getMajorIdx] let nat := mkConst ``Nat mkLocalInstances params fun localInsts => mkSizeOfMotives motiveFVars fun motives => do let us := levelOne :: levelParams.map mkLevelParam -- universe level parameters for `rec`-application let recFn := mkConst recName us let val := mkAppN recFn (params ++ motives) forallBoundedTelescope (← inferType val) recInfo.numMinors fun minorFVars' _ => mkSizeOfMinors motiveFVars minorFVars minorFVars' fun minors => do let sizeOfParams := params ++ localInsts ++ indices ++ #[major] let sizeOfType ← mkForallFVars sizeOfParams nat let val := mkAppN val (minors ++ indices ++ #[major]) trace[Meta.sizeOf] "val: {val}" let sizeOfValue ← mkLambdaFVars sizeOfParams val addDecl <| Declaration.defnDecl { name := declName levelParams := levelParams type := sizeOfType value := sizeOfValue safety := DefinitionSafety.safe hints := ReducibilityHints.abbrev } /-- Create `sizeOf` functions for all inductive datatypes in the mutual inductive declaration containing `typeName` The resulting array contains the generated functions names. The `NameMap` maps recursor names into the generated function names. There is a function for each element of the mutual inductive declaration, and for auxiliary recursors for nested inductive types. -/ def mkSizeOfFns (typeName : Name) : MetaM (Array Name × NameMap Name) := do let indInfo ← getConstInfoInduct typeName let recInfo ← getConstInfoRec (mkRecName typeName) let numExtra := recInfo.numMotives - indInfo.all.length -- numExtra > 0 for nested inductive types let mut result := #[] let baseName := indInfo.all.head! ++ `_sizeOf -- we use the first inductive type as the base name for `sizeOf` functions let mut i := 1 let mut recMap : NameMap Name := {} for indTypeName in indInfo.all do let sizeOfName := baseName.appendIndexAfter i let recName := mkRecName indTypeName mkSizeOfFn recName sizeOfName recMap := recMap.insert recName sizeOfName result := result.push sizeOfName i := i + 1 for j in [:numExtra] do let recName := (mkRecName indInfo.all.head!).appendIndexAfter (j+1) let sizeOfName := baseName.appendIndexAfter i mkSizeOfFn recName sizeOfName recMap := recMap.insert recName sizeOfName result := result.push sizeOfName i := i + 1 return (result, recMap) def mkSizeOfSpecLemmaName (ctorName : Name) : Name := ctorName ++ `sizeOf_spec def mkSizeOfSpecLemmaInstance (ctorApp : Expr) : MetaM Expr := matchConstCtor ctorApp.getAppFn (fun _ => throwError "failed to apply 'sizeOf' spec, constructor expected{indentExpr ctorApp}") fun ctorInfo ctorLevels => do let ctorArgs := ctorApp.getAppArgs let ctorFields := ctorArgs[ctorArgs.size - ctorInfo.numFields:] let lemmaName := mkSizeOfSpecLemmaName ctorInfo.name let lemmaInfo ← getConstInfo lemmaName let lemmaArity ← forallTelescopeReducing lemmaInfo.type fun xs _ => return xs.size let lemmaArgMask := mkArray (lemmaArity - ctorInfo.numFields) (none (α := Expr)) let lemmaArgMask := lemmaArgMask ++ ctorFields.toArray.map some mkAppOptM lemmaName lemmaArgMask /- SizeOf spec theorem for nested inductive types -/ namespace SizeOfSpecNested structure Context where indInfo : InductiveVal sizeOfFns : Array Name ctorName : Name params : Array Expr localInsts : Array Expr recMap : NameMap Name -- mapping from recursor name into `_sizeOf_<idx>` function name (see `mkSizeOfFns`) abbrev M := ReaderT Context MetaM def throwUnexpected {α} (msg : MessageData) : M α := do throwError "failed to generate sizeOf lemma for {(← read).ctorName} (use `set_option genSizeOfSpec false` to disable lemma generation), {msg}" def throwFailed {α} : M α := do throwError "failed to generate sizeOf lemma for {(← read).ctorName}, (use `set_option genSizeOfSpec false` to disable lemma generation)" /-- Convert a recursor application into a `_sizeOf_<idx>` application. -/ private def recToSizeOf (e : Expr) : M Expr := do matchConstRec e.getAppFn (fun _ => throwFailed) fun info us => do match (← read).recMap.find? info.name with | none => throwUnexpected m!"expected recursor application {indentExpr e}" | some sizeOfName => let args := e.getAppArgs let indices := args[info.getFirstIndexIdx : info.getFirstIndexIdx + info.numIndices] let major := args[info.getMajorIdx] return mkAppN (mkConst sizeOfName us.tail!) ((← read).params ++ (← read).localInsts ++ indices ++ #[major]) mutual /-- Construct minor premise proof for `mkSizeOfAuxLemmaProof`. `ys` contains fields and inductive hypotheses for the minor premise. -/ private partial def mkMinorProof (ys : Array Expr) (lhs rhs : Expr) : M Expr := do trace[Meta.sizeOf.minor] "{lhs} =?= {rhs}" if (← isDefEq lhs rhs) then mkEqRefl rhs else match (← whnfI lhs).natAdd?, (← whnfI rhs).natAdd? with | some (a₁, b₁), some (a₂, b₂) => let p₁ ← mkMinorProof ys a₁ a₂ let p₂ ← mkMinorProofStep ys b₁ b₂ mkCongr (← mkCongrArg (mkConst ``Nat.add) p₁) p₂ | _, _ => throwUnexpected m!"expected 'Nat.add' application, lhs is {indentExpr lhs}\nrhs is{indentExpr rhs}" /-- Helper method for `mkMinorProof`. The proof step is one of the following - Reflexivity - Assumption (i.e., using an inductive hypotheses from `ys`) - `mkSizeOfAuxLemma` application. This case happens when we have multiple levels of nesting -/ private partial def mkMinorProofStep (ys : Array Expr) (lhs rhs : Expr) : M Expr := do if (← isDefEq lhs rhs) then mkEqRefl rhs else let lhs ← recToSizeOf lhs trace[Meta.sizeOf.minor.step] "{lhs} =?= {rhs}" let target ← mkEq lhs rhs for y in ys do if (← isDefEq (← inferType y) target) then return y mkSizeOfAuxLemma lhs rhs /-- Construct proof of auxiliary lemma. See `mkSizeOfAuxLemma` -/ private partial def mkSizeOfAuxLemmaProof (info : InductiveVal) (lhs rhs : Expr) : M Expr := do let lhsArgs := lhs.getAppArgs let sizeOfBaseArgs := lhsArgs[:lhsArgs.size - info.numIndices - 1] let indicesMajor := lhsArgs[lhsArgs.size - info.numIndices - 1:] let sizeOfLevels := lhs.getAppFn.constLevels! /- Auxiliary function for constructing an `_sizeOf_<idx>` for `ys`, where `ys` are the indices + major. Recall that if `info.name` is part of a mutually inductive declaration, then the resulting application is not necessarily a `lhs.getAppFn` application. The result is an application of one of the `(← read),sizeOfFns` functions. We use this auxiliary function to builtin the motive of the recursor. -/ let rec mkSizeOf (ys : Array Expr) : M Expr := do for sizeOfFn in (← read).sizeOfFns do let candidate := mkAppN (mkAppN (mkConst sizeOfFn sizeOfLevels) sizeOfBaseArgs) ys if (← isTypeCorrect candidate) then return candidate throwFailed let major := lhs.appArg! let majorType ← whnf (← inferType major) let majorTypeArgs := majorType.getAppArgs match majorType.getAppFn.const? with | none => throwFailed | some (_, us) => let recName := mkRecName info.name let recInfo ← getConstInfoRec recName let r := mkConst recName (levelZero :: us) let r := mkAppN r majorTypeArgs[:info.numParams] forallBoundedTelescope (← inferType r) recInfo.numMotives fun motiveFVars _ => do let mut r := r -- Add motives for motiveFVar in motiveFVars do let motive ← forallTelescopeReducing (← inferType motiveFVar) fun ys _ => do let lhs ← mkSizeOf ys let rhs ← mkAppM ``SizeOf.sizeOf #[ys.back] mkLambdaFVars ys (← mkEq lhs rhs) r := mkApp r motive forallBoundedTelescope (← inferType r) recInfo.numMinors fun minorFVars _ => do let mut r := r -- Add minors for minorFVar in minorFVars do let minor ← forallTelescopeReducing (← inferType minorFVar) fun ys target => do let target ← whnf target match target.eq? with | none => throwFailed | some (_, lhs, rhs) => if (← isDefEq lhs rhs) then mkLambdaFVars ys (← mkEqRefl rhs) else let lhs ← unfoldDefinition lhs -- Unfold `_sizeOf_<idx>` -- rhs is of the form `sizeOf (ctor ...)` let ctorApp := rhs.appArg! let specLemma ← mkSizeOfSpecLemmaInstance ctorApp let specEq ← whnf (← inferType specLemma) match specEq.eq? with | none => throwFailed | some (_, rhs, rhsExpanded) => let lhs_eq_rhsExpanded ← mkMinorProof ys lhs rhsExpanded let rhsExpanded_eq_rhs ← mkEqSymm specLemma mkLambdaFVars ys (← mkEqTrans lhs_eq_rhsExpanded rhsExpanded_eq_rhs) r := mkApp r minor -- Add indices and major return mkAppN r indicesMajor /-- Generate proof for `C._sizeOf_<idx> t = sizeOf t` where `C._sizeOf_<idx>` is a auxiliary function generated for a nested inductive type in `C`. For example, given ```lean inductive Expr where | app (f : String) (args : List Expr) ``` We generate the auxiliary function `Expr._sizeOf_1 : List Expr → Nat`. To generate the `sizeOf` spec lemma ``` sizeOf (Expr.app f args) = 1 + sizeOf f + sizeOf args ``` we need an auxiliary lemma for showing `Expr._sizeOf_1 args = sizeOf args`. Recall that `sizeOf (Expr.app f args)` is definitionally equal to `1 + sizeOf f + Expr._sizeOf_1 args`, but `Expr._sizeOf_1 args` is **not** definitionally equal to `sizeOf args`. We need a proof by induction. -/ private partial def mkSizeOfAuxLemma (lhs rhs : Expr) : M Expr := do trace[Meta.sizeOf.aux] "{lhs} =?= {rhs}" match lhs.getAppFn.const? with | none => throwFailed | some (fName, us) => let thmLevelParams ← us.mapM fun | Level.param n _ => return n | _ => throwFailed let thmName := fName.appendAfter "_eq" if (← getEnv).contains thmName then -- Auxiliary lemma has already been defined return mkAppN (mkConst thmName us) lhs.getAppArgs else -- Define auxiliary lemma -- First, generalize indices let x := lhs.appArg! let xType ← whnf (← inferType x) matchConstInduct xType.getAppFn (fun _ => throwFailed) fun info _ => do let params := xType.getAppArgs[:info.numParams] forallTelescopeReducing (← inferType (mkAppN xType.getAppFn params)) fun indices _ => do let majorType := mkAppN (mkAppN xType.getAppFn params) indices withLocalDeclD `x majorType fun major => do let lhsArgs := lhs.getAppArgs let lhsArgsNew := lhsArgs[:lhsArgs.size - 1 - indices.size] ++ indices ++ #[major] let lhsNew := mkAppN lhs.getAppFn lhsArgsNew let rhsNew ← mkAppM ``SizeOf.sizeOf #[major] let eq ← mkEq lhsNew rhsNew let thmParams := lhsArgsNew let thmType ← mkForallFVars thmParams eq let thmValue ← mkSizeOfAuxLemmaProof info lhsNew rhsNew let thmValue ← mkLambdaFVars thmParams thmValue trace[Meta.sizeOf] "thmValue: {thmValue}" addDecl <| Declaration.thmDecl { name := thmName levelParams := thmLevelParams type := thmType value := thmValue } return mkAppN (mkConst thmName us) lhs.getAppArgs end /- Prove SizeOf spec lemma of the form `sizeOf <ctor-application> = 1 + sizeOf <field_1> + ... + sizeOf <field_n> -/ partial def main (lhs rhs : Expr) : M Expr := do if (← isDefEq lhs rhs) then mkEqRefl rhs else /- Expand lhs and rhs to obtain `Nat.add` applications -/ let lhs ← whnfI lhs -- Expand `sizeOf (ctor ...)` into `_sizeOf_<idx>` application let lhs ← unfoldDefinition lhs -- Unfold `_sizeOf_<idx>` application into `HAdd.hAdd` application loop lhs rhs where loop (lhs rhs : Expr) : M Expr := do trace[Meta.sizeOf.loop] "{lhs} =?= {rhs}" if (← isDefEq lhs rhs) then mkEqRefl rhs else match (← whnfI lhs).natAdd?, (← whnfI rhs).natAdd? with | some (a₁, b₁), some (a₂, b₂) => let p₁ ← loop a₁ a₂ let p₂ ← step b₁ b₂ mkCongr (← mkCongrArg (mkConst ``Nat.add) p₁) p₂ | _, _ => throwUnexpected m!"expected 'Nat.add' application, lhs is {indentExpr lhs}\nrhs is{indentExpr rhs}" step (lhs rhs : Expr) : M Expr := do if (← isDefEq lhs rhs) then mkEqRefl rhs else let lhs ← recToSizeOf lhs mkSizeOfAuxLemma lhs rhs end SizeOfSpecNested private def mkSizeOfSpecTheorem (indInfo : InductiveVal) (sizeOfFns : Array Name) (recMap : NameMap Name) (ctorName : Name) : MetaM Unit := do let ctorInfo ← getConstInfoCtor ctorName let us := ctorInfo.levelParams.map mkLevelParam forallTelescopeReducing ctorInfo.type fun xs _ => do let params := xs[:ctorInfo.numParams] let fields := xs[ctorInfo.numParams:] let ctorApp := mkAppN (mkConst ctorName us) xs mkLocalInstances params fun localInsts => do let lhs ← mkAppM ``SizeOf.sizeOf #[ctorApp] let mut rhs ← mkNumeral (mkConst ``Nat) 1 for field in fields do unless (← whnf (← inferType field)).isForall do rhs ← mkAdd rhs (← mkAppM ``SizeOf.sizeOf #[field]) let target ← mkEq lhs rhs let thmName := mkSizeOfSpecLemmaName ctorName let thmParams := params ++ localInsts ++ fields let thmType ← mkForallFVars thmParams target let thmValue ← if indInfo.isNested then SizeOfSpecNested.main lhs rhs |>.run { indInfo := indInfo, sizeOfFns := sizeOfFns, ctorName := ctorName, params := params, localInsts := localInsts, recMap := recMap } else mkEqRefl rhs let thmValue ← mkLambdaFVars thmParams thmValue addDecl <| Declaration.thmDecl { name := thmName levelParams := ctorInfo.levelParams type := thmType value := thmValue } private def mkSizeOfSpecTheorems (indTypeNames : Array Name) (sizeOfFns : Array Name) (recMap : NameMap Name) : MetaM Unit := do for indTypeName in indTypeNames do let indInfo ← getConstInfoInduct indTypeName for ctorName in indInfo.ctors do mkSizeOfSpecTheorem indInfo sizeOfFns recMap ctorName return () register_builtin_option genSizeOf : Bool := { defValue := true descr := "generate `SizeOf` instance for inductive types and structures" } register_builtin_option genSizeOfSpec : Bool := { defValue := true descr := "generate `SizeOf` specificiation theorems for automatically generated instances" } def mkSizeOfInstances (typeName : Name) : MetaM Unit := do if (← getEnv).contains ``SizeOf && genSizeOf.get (← getOptions) && !(← isInductivePredicate typeName) then let indInfo ← getConstInfoInduct typeName unless indInfo.isUnsafe do let (fns, recMap) ← mkSizeOfFns typeName for indTypeName in indInfo.all, fn in fns do let indInfo ← getConstInfoInduct indTypeName forallTelescopeReducing indInfo.type fun xs _ => let params := xs[:indInfo.numParams] let indices := xs[indInfo.numParams:] mkLocalInstances params fun localInsts => do let us := indInfo.levelParams.map mkLevelParam let indType := mkAppN (mkConst indTypeName us) xs let sizeOfIndType ← mkAppM ``SizeOf #[indType] withLocalDeclD `m indType fun m => do let v ← mkLambdaFVars #[m] <| mkAppN (mkConst fn us) (params ++ localInsts ++ indices ++ #[m]) let sizeOfMk ← mkAppM ``SizeOf.mk #[v] let instDeclName := indTypeName ++ `_sizeOf_inst let instDeclType ← mkForallFVars (xs ++ localInsts) sizeOfIndType let instDeclValue ← mkLambdaFVars (xs ++ localInsts) sizeOfMk addDecl <| Declaration.defnDecl { name := instDeclName levelParams := indInfo.levelParams type := instDeclType value := instDeclValue safety := DefinitionSafety.safe hints := ReducibilityHints.abbrev } addInstance instDeclName AttributeKind.global (eval_prio default) if genSizeOfSpec.get (← getOptions) then mkSizeOfSpecTheorems indInfo.all.toArray fns recMap builtin_initialize registerTraceClass `Meta.sizeOf end Lean.Meta
98b0214c903bb2633abd41365ea9d61716b83a1d
cc060cf567f81c404a13ee79bf21f2e720fa6db0
/lean/category-theory-parameterized.lean
6bc6688417b88c51cb500221bd3a02ee316e443a
[ "Apache-2.0" ]
permissive
semorrison/proof
cf0a8c6957153bdb206fd5d5a762a75958a82bca
5ee398aa239a379a431190edbb6022b1a0aa2c70
refs/heads/master
1,610,414,502,842
1,518,696,851,000
1,518,696,851,000
78,375,937
2
1
null
null
null
null
UTF-8
Lean
false
false
12,211
lean
import standard meta def blast : tactic unit := using_smt $ return () set_option pp.implicit true structure Category { Obj : Type } (Hom : Obj -> Obj -> Type) := (identity : Π A : Obj, Hom A A) (compose : Π ⦃A B C : Obj⦄, Hom A B → Hom B C → Hom A C) (left_identity : Π ⦃A B : Obj⦄ (f : Hom A B), compose (identity _) f = f) (right_identity : Π ⦃A B : Obj⦄ (f : Hom A B), compose f (identity _) = f) (associativity : Π ⦃A B C D : Obj⦄ (f : Hom A B) (g : Hom B C) (h : Hom C D), compose (compose f g) h = compose f (compose g h)) class Category' { Obj : Type } (Hom : Obj -> Obj -> Type) := (identity : Π A : Obj, Hom A A) (compose : Π ⦃A B C : Obj⦄, Hom A B → Hom B C → Hom A C) (left_identity : Π ⦃A B : Obj⦄ (f : Hom A B), compose (identity _) f = f) (right_identity : Π ⦃A B : Obj⦄ (f : Hom A B), compose f (identity _) = f) (associativity : Π ⦃A B C D : Obj⦄ (f : Hom A B) (g : Hom B C) (h : Hom C D), compose (compose f g) h = compose f (compose g h)) print Category print Category' attribute [class] Category -- declaring it as a class from the beginning results in an insane -- signature ... This really seems like it must be a bug, or at least -- a rough edge. -- print Category instance ℕCategory : Category (λ a b : unit, ℕ) := { identity := λ a, 0, compose := λ a b c, add, left_identity := λ a b, zero_add, -- Sadly, "by blast" doesn't work here. I think this is because -- "by blast" checks if the heads of the expressions match, -- which is the case for right_identity and associativity, but -- not left_identity. right_identity := by blast, --λ a b, add_zero, associativity := by blast --λ a b c d, add_assoc } open Category variables { Obj₁ Obj₂ : Type } variables { Hom₁ : Obj₁ → Obj₁ → Type } { Hom₂ : Obj₂ → Obj₂ → Type } structure Functor (C₁ : Category Hom₁) (C₂ : Category Hom₂) := (onObjects : Obj₁ → Obj₂) (onMorphisms : Π ⦃A B : Obj₁⦄, Hom₁ A B → Hom₂ (onObjects A) (onObjects B)) (identities : Π (A : Obj₁), onMorphisms (identity C₁ A) = identity C₂ (onObjects A)) (functoriality : Π ⦃A B C : Obj₁⦄ (f : Hom₁ A B) (g : Hom₁ B C), onMorphisms (compose C₁ f g) = compose C₂ (onMorphisms f) (onMorphisms g)) attribute [class] Functor -- (Scott:) I wish it were possible to define coercions of Functor to either -- onObjects or onMorphisms, so we could just write F A and F f. -- (Scott:) I'm not sure what purpose these notations serve: is 'F <$> A' that -- much better than 'onObjects F A' that it warrants introducing notation? namespace Functor -- Lean complains about the use of local variables in -- notation. There must be a way around that. infix `<$>`:50 := λ {Obj₁ Obj₂ : Type} {Hom₁ : Obj₁ → Obj₁ → Type} {Hom₂ : Obj₂ → Obj₂ → Type} {C₁ : Category Hom₁} {C₂ : Category Hom₂} (F : Functor C₁ C₂) (A : Obj₁), onObjects F A infix `<$>m`:50 := λ {Obj₁ Obj₂ : Type} {Hom₁ : Obj₁ → Obj₁ → Type} {Hom₂ : Obj₂ → Obj₂ → Type} {C₁ : Category Hom₁} {C₂ : Category Hom₂} (F : Functor C₁ C₂) {A B : Obj₁} (f : Hom₁ A B), onMorphisms F f end Functor --print Functor instance DoublingAsFunctor : Functor ℕCategory ℕCategory := { onObjects := id, onMorphisms := (λ A B n, n + n), -- Sadly, "by blast" doesn't work below if we replace "n + n" -- with "2 * n". Again, I think this is because the heads don't -- match. If you use "n * 2", then identities works by blast, -- but functoriality still doesn't. identities := by blast, functoriality := by blast } variables { Obj_C Obj_D : Type } variables { Hom_C : Obj_C -> Obj_C -> Type } { Hom_D : Obj_D -> Obj_D -> Type } open prod -- TODO(?) Can these proofs be simplified? -- Stephen's earlier versions (for Lean 2?) were perhaps better. instance ProductCategory (C : Category Hom_C) (D : Category Hom_D) : Category (λ a b : Obj_C × Obj_D, Hom_C (fst a) (fst b) × Hom_D (snd a) (snd b)) := { identity := λ a, (identity C (fst a), identity D (snd a)), compose := λ a b c f g, (compose C (fst f) (fst g), compose D (snd f) (snd g)), left_identity := begin intros, pose x := left_identity C (fst f), pose y := left_identity D (snd f), induction f, blast end, right_identity := begin intros, pose x := right_identity C (fst f), pose y := right_identity D (snd f), induction f, blast end, associativity := begin intros, pose x := associativity C (fst f) (fst g) (fst h), pose y := associativity D (snd f) (snd g) (snd h), induction f, blast end } namespace ProductCategory notation C `×c` D := ProductCategory C D end ProductCategory def ℕTensorProduct : Functor (ℕCategory ×c ℕCategory) ℕCategory := { onObjects := fst, onMorphisms := λ A B n, fst n + snd n, identities := by blast, functoriality := by blast } structure LaxMonoidalCategory { Obj : Type } (Hom : Obj → Obj → Type) extends carrier : Category Hom := (tensor : Functor (carrier ×c carrier) carrier) (tensor_unit : Obj) (associator : Π (A B C : Obj), Hom (tensor <$> (tensor <$> (A, B), C)) (tensor <$> (A, tensor <$> (B, C)))) -- TODO actually, express the associator as a natural transformation! /- I tried writing the pentagon, but it doesn't type check. :-( (pentagon : Π (A B C D : Obj), -- we need to compare: -- ((AB)C)D ---> (A(BC))D ---> A((BC)D) ---> A(B(CD)) -- ((AB)C)D ---> (AB)(CD) ---> A(B(CD)) compose (compose (tensor <$>m (associator A B C, identity D)) (associator A (tensor <$> (B, C)) D) ) (tensor <$>m (identity A, associator B C D)) = compose (associator (tensor <$> (A, B)) C D) (associator A B (tensor <$> (C, D))) ) -/ /- -- TODO(far future) -- One should prove the first substantial result of this theory: that any two ways to reparenthesize are equal. -- It requires introducing a representation of a reparathesization, but the proof should then be an easy induction. -- It's a good example of something that is so easy for humans, that is better eventually be easy for the computer too! -/ -- Notice that LaxMonoidalCategory.tensor has a horrible signature... -- It sure would be nice if it read ... Functor (carrier ×c carrier) carrier -- print LaxMonoidalCategory attribute [class] LaxMonoidalCategory attribute [instance] LaxMonoidalCategory.to_Category instance LaxMonoidalCategory_coercion { Obj : Type } { Hom : Obj -> Obj -> Type } : has_coe (LaxMonoidalCategory Hom) (Category Hom) := ⟨LaxMonoidalCategory.to_Category⟩ namespace LaxMonoidalCategory infix `⊗`:70 := λ {Obj : Type} {Hom : Obj → Obj → Type} {C : LaxMonoidalCategory Obj Hom} (A B : Obj), tensor C <$> (A, B) infix `⊗m`:70 := λ {Obj : Type} {Hom : Obj → Obj → Type} {C : LaxMonoidalCategory Obj Hom} {A B C D : Obj} (f : Hom A B) (g : Hom C D), tensor C <$>m (f, g) end LaxMonoidalCategory open LaxMonoidalCategory --theorem LiftForgetLax {Obj : Type} {Hom : Obj → Obj → Type} (C : Category Obj Hom) : -- Π {tensor : Functor (C ×c C) C} {unit : Obj} -- {associator : Π (A B C : Obj), Hom ((A ⊗ B) ⊗ C) (A ⊗ (B ⊗ C))}, -- LaxAsCategory.mk def ℕLaxMonoidalCategory : LaxMonoidalCategory (λ A B : unit, ℕ) := { ℕCategory with tensor := ℕTensorProduct, tensor_unit := (), associator := λ A B C, Category.identity ℕCategory () } -- casting is not working: we really want to be able to write the following: instance DoublingAsFunctor' : Functor ℕLaxMonoidalCategory ℕLaxMonoidalCategory := { onObjects := id, onMorphisms := (λ A B n, n + n), identities := by blast, functoriality := by blast } structure OplaxMonoidalCategory {Obj : Type} (Hom : Obj → Obj → Type) extends carrier : Category Hom := (tensor : Functor (carrier ×c carrier) carrier) (tensor_unit : Obj) -- TODO better name? unfortunately it doesn't yet make sense to say 'inverse_associator'. (backwards_associator : Π (A B C : Obj), Hom (tensor <$> (A, tensor <$> (B, C))) (tensor <$> (tensor <$> (A, B), C))) attribute [class] OplaxMonoidalCategory attribute [instance] OplaxMonoidalCategory.to_Category instance OplaxMonoidalCategory_coercion { Obj : Type } { Hom : Obj -> Obj -> Type } : has_coe (OplaxMonoidalCategory Hom) (Category Hom) := ⟨OplaxMonoidalCategory.to_Category⟩ structure MonoidalCategory {Obj : Type} (Hom : Obj -> Obj -> Type) extends LaxMonoidalCategory Hom, OplaxMonoidalCategory Hom := (associators_inverses_1: Π (A B C : Obj), compose (associator A B C) (backwards_associator A B C) = identity (tensor <$> (tensor <$> (A, B), C))) (associators_inverses_2: Π (A B C : Obj), compose (backwards_associator A B C) (associator A B C) = identity (tensor <$> (A, tensor <$> (B, C)))) attribute [class] MonoidalCategory attribute [instance] MonoidalCategory.to_LaxMonoidalCategory instance MonoidalCategory_coercion_to_LaxMonoidalCategory { Obj : Type } { Hom : Obj -> Obj -> Type } : has_coe (MonoidalCategory Hom) (LaxMonoidalCategory Hom) := ⟨MonoidalCategory.to_LaxMonoidalCategory⟩ --instance MonoidalCategory_coercion_to_OplaxMonoidalCategory { Obj : Type } { Hom : Obj -> Obj -> Type } : has_coe (MonoidalCategory Hom) (OplaxMonoidalCategory Hom) := ⟨MonoidalCategory.to_OplaxMonoidalCategory⟩ definition foo { Obj : Type } { Hom : Obj -> Obj -> Type } (C : MonoidalCategory Hom ) : Category Hom := C definition identity_functor { Obj : Type } { Hom : Obj -> Obj -> Type } (C : MonoidalCategory Hom) : Functor C C := { } open Functor -- Running into the same coercion problem. :-( definition tensor_on_left {Obj: Type} {Hom: Obj -> Obj -> Type} (C: MonoidalCategory Hom) (X: Obj) : @Functor Obj Obj Hom Hom C C := { onObjects := λ a : Obj, onObjects (@tensor Obj Hom C) (X, a), onMorphisms := λ a b : Obj, λ f : Hom a b, onMorphisms (@tensor Obj Hom C) (@Category.identity Obj Hom C X, f), identities := --begin -- intros, -- pose H := identities (@tensor Obj Hom C) (X, A), -- -- sadly, that's not enough -- sorry --end, by sorry, functoriality := by sorry } -- TODO definition tensor_on_right -- TODO define natural transformations between functors -- TODO define a natural isomorphism -- TODO define a braided monoidal category structure BraidedMonoidalCategory {Obj : Type} (Hom: Obj -> Obj -> Type) extends MonoidalCategory Hom := (braiding: Π A : Obj, NaturalIsomorphism (tensor_on_left this A) (tensor_on_right this A)) -- TODO define a symmetric monoidal category structure EnrichedCategory { VObj : Type } { VHom : VObj -> VObj -> Type } (V: MonoidalCategory VHom) { Obj : Type } (Hom : Obj -> Obj -> VObj) := (compose : Π ⦃A B C : Obj⦄, VHom ((tensor V) <$> ((Hom A B), (Hom B C))) (Hom A C)) -- again, the coercion problem -- TODO and so on -- How do you define a structure which extends another, but has no new fields? -- TODO How would we define an additive category, now? We don't want to say: -- Hom : Obj -> Obj -> AdditiveGroup -- instead we want to express something like: -- Hom : Obj -> Obj -> [something coercible to AdditiveGroup]
20622b8ad5ba85e650dfabf654864d4f7dacafb4
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/analysis/special_functions/complex/circle.lean
309987e80bfa5a37b227cb8f7da1a09296a288c3
[ "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
3,538
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 -/ import analysis.complex.circle import analysis.special_functions.complex.log /-! # Maps on the unit circle In this file we prove some basic lemmas about `exp_map_circle` and the restriction of `complex.arg` to the unit circle. These two maps define a local equivalence between `circle` and `ℝ`, see `circle.arg_local_equiv` and `circle.arg_equiv`, that sends the whole circle to `(-π, π]`. -/ open complex function set open_locale real namespace circle lemma injective_arg : injective (λ z : circle, arg z) := λ z w h, subtype.ext $ ext_abs_arg ((abs_eq_of_mem_circle z).trans (abs_eq_of_mem_circle w).symm) h @[simp] lemma arg_eq_arg {z w : circle} : arg z = arg w ↔ z = w := injective_arg.eq_iff end circle lemma arg_exp_map_circle {x : ℝ} (h₁ : -π < x) (h₂ : x ≤ π) : arg (exp_map_circle x) = x := by rw [exp_map_circle_apply, exp_mul_I, arg_cos_add_sin_mul_I h₁ h₂] @[simp] lemma exp_map_circle_arg (z : circle) : exp_map_circle (arg z) = z := circle.injective_arg $ arg_exp_map_circle (neg_pi_lt_arg _) (arg_le_pi _) namespace circle /-- `complex.arg ∘ coe` and `exp_map_circle` define a local equivalence between `circle and `ℝ` with `source = set.univ` and `target = set.Ioc (-π) π`. -/ @[simps { fully_applied := ff }] noncomputable def arg_local_equiv : local_equiv circle ℝ := { to_fun := arg ∘ coe, inv_fun := exp_map_circle, source := univ, target := Ioc (-π) π, map_source' := λ z _, ⟨neg_pi_lt_arg _, arg_le_pi _⟩, map_target' := maps_to_univ _ _, left_inv' := λ z _, exp_map_circle_arg z, right_inv' := λ x hx, arg_exp_map_circle hx.1 hx.2 } /-- `complex.arg` and `exp_map_circle` define an equivalence between `circle and `(-π, π]`. -/ @[simps { fully_applied := ff }] noncomputable def arg_equiv : circle ≃ Ioc (-π) π := { to_fun := λ z, ⟨arg z, neg_pi_lt_arg _, arg_le_pi _⟩, inv_fun := exp_map_circle ∘ coe, left_inv := λ z, arg_local_equiv.left_inv trivial, right_inv := λ x, subtype.ext $ arg_local_equiv.right_inv x.2 } end circle lemma left_inverse_exp_map_circle_arg : left_inverse exp_map_circle (arg ∘ coe) := exp_map_circle_arg lemma inv_on_arg_exp_map_circle : inv_on (arg ∘ coe) exp_map_circle (Ioc (-π) π) univ := circle.arg_local_equiv.symm.inv_on lemma surj_on_exp_map_circle_neg_pi_pi : surj_on exp_map_circle (Ioc (-π) π) univ := circle.arg_local_equiv.symm.surj_on lemma exp_map_circle_eq_exp_map_circle {x y : ℝ} : exp_map_circle x = exp_map_circle y ↔ ∃ m : ℤ, x = y + m * (2 * π) := begin rw [subtype.ext_iff, exp_map_circle_apply, exp_map_circle_apply, exp_eq_exp_iff_exists_int], refine exists_congr (λ n, _), rw [← mul_assoc, ← add_mul, mul_left_inj' I_ne_zero, ← of_real_one, ← of_real_bit0, ← of_real_mul, ← of_real_int_cast, ← of_real_mul, ← of_real_add, of_real_inj] end lemma periodic_exp_map_circle : periodic exp_map_circle (2 * π) := λ z, exp_map_circle_eq_exp_map_circle.2 ⟨1, by rw [int.cast_one, one_mul]⟩ @[simp] lemma exp_map_circle_two_pi : exp_map_circle (2 * π) = 1 := periodic_exp_map_circle.eq.trans exp_map_circle_zero lemma exp_map_circle_sub_two_pi (x : ℝ) : exp_map_circle (x - 2 * π) = exp_map_circle x := periodic_exp_map_circle.sub_eq x lemma exp_map_circle_add_two_pi (x : ℝ) : exp_map_circle (x + 2 * π) = exp_map_circle x := periodic_exp_map_circle x
25ba39e7a8a9b4ba743eaab4718ea21731f73ffb
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/order/atoms_auto.lean
b18adcb1d0baacae3af478738d65456e470ac699
[]
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
17,351
lean
/- Copyright (c) 2020 Aaron Anderson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Aaron Anderson. -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.order.complete_boolean_algebra import Mathlib.order.modular_lattice import Mathlib.data.fintype.basic import Mathlib.PostPort universes u_1 l u_2 namespace Mathlib /-! # Atoms, Coatoms, and Simple Lattices This module defines atoms, which are minimal non-`⊥` elements in bounded lattices, simple lattices, which are lattices with only two elements, and related ideas. ## Main definitions ### Atoms and Coatoms * `is_atom a` indicates that the only element below `a` is `⊥`. * `is_coatom a` indicates that the only element above `a` is `⊤`. ### Atomic and Atomistic Lattices * `is_atomic` indicates that every element other than `⊥` is above an atom. * `is_coatomic` indicates that every element other than `⊤` is below a coatom. * `is_atomistic` indicates that every element is the `Sup` of a set of atoms. * `is_coatomistic` indicates that every element is the `Inf` of a set of coatoms. ### Simple Lattices * `is_simple_lattice` indicates that a bounded lattice has only two elements, `⊥` and `⊤`. * `is_simple_lattice.bounded_distrib_lattice` * Given an instance of `is_simple_lattice`, we provide the following definitions. These are not made global instances as they contain data : * `is_simple_lattice.boolean_algebra` * `is_simple_lattice.complete_lattice` * `is_simple_lattice.complete_boolean_algebra` ## Main results * `is_atom_dual_iff_is_coatom` and `is_coatom_dual_iff_is_atom` express the (definitional) duality of `is_atom` and `is_coatom`. * `is_simple_lattice_iff_is_atom_top` and `is_simple_lattice_iff_is_coatom_bot` express the connection between atoms, coatoms, and simple lattices -/ /-- An atom of an `order_bot` is an element with no other element between it and `⊥`, which is not `⊥`. -/ def is_atom {α : Type u_1} [order_bot α] (a : α) := a ≠ ⊥ ∧ ∀ (b : α), b < a → b = ⊥ theorem eq_bot_or_eq_of_le_atom {α : Type u_1} [order_bot α] {a : α} {b : α} (ha : is_atom a) (hab : b ≤ a) : b = ⊥ ∨ b = a := or.imp_left (and.right ha b) (has_le.le.lt_or_eq hab) theorem is_atom.Iic {α : Type u_1} [order_bot α] {x : α} {a : α} (ha : is_atom a) (hax : a ≤ x) : is_atom { val := a, property := hax } := sorry theorem is_atom.of_is_atom_coe_Iic {α : Type u_1} [order_bot α] {x : α} {a : ↥(set.Iic x)} (ha : is_atom a) : is_atom ↑a := sorry /-- A coatom of an `order_top` is an element with no other element between it and `⊤`, which is not `⊤`. -/ def is_coatom {α : Type u_1} [order_top α] (a : α) := a ≠ ⊤ ∧ ∀ (b : α), a < b → b = ⊤ theorem eq_top_or_eq_of_coatom_le {α : Type u_1} [order_top α] {a : α} {b : α} (ha : is_coatom a) (hab : a ≤ b) : b = ⊤ ∨ b = a := or.imp (and.right ha b) (iff.mpr eq_comm) (has_le.le.lt_or_eq hab) theorem is_coatom.Ici {α : Type u_1} [order_top α] {x : α} {a : α} (ha : is_coatom a) (hax : x ≤ a) : is_coatom { val := a, property := hax } := sorry theorem is_coatom.of_is_coatom_coe_Ici {α : Type u_1} [order_top α] {x : α} {a : ↥(set.Ici x)} (ha : is_coatom a) : is_coatom ↑a := sorry theorem is_atom.inf_eq_bot_of_ne {α : Type u_1} [semilattice_inf_bot α] {a : α} {b : α} (ha : is_atom a) (hb : is_atom b) (hab : a ≠ b) : a ⊓ b = ⊥ := sorry theorem is_atom.disjoint_of_ne {α : Type u_1} [semilattice_inf_bot α] {a : α} {b : α} (ha : is_atom a) (hb : is_atom b) (hab : a ≠ b) : disjoint a b := iff.mpr disjoint_iff (is_atom.inf_eq_bot_of_ne ha hb hab) theorem is_coatom.sup_eq_top_of_ne {α : Type u_1} [semilattice_sup_top α] {a : α} {b : α} (ha : is_coatom a) (hb : is_coatom b) (hab : a ≠ b) : a ⊔ b = ⊤ := sorry @[simp] theorem is_coatom_dual_iff_is_atom {α : Type u_1} [bounded_lattice α] {a : α} : is_coatom (coe_fn order_dual.to_dual a) ↔ is_atom a := iff.refl (is_coatom (coe_fn order_dual.to_dual a)) @[simp] theorem is_atom_dual_iff_is_coatom {α : Type u_1} [bounded_lattice α] {a : α} : is_atom (coe_fn order_dual.to_dual a) ↔ is_coatom a := iff.refl (is_atom (coe_fn order_dual.to_dual a)) /-- A lattice is atomic iff every element other than `⊥` has an atom below it. -/ class is_atomic (α : Type u_1) [bounded_lattice α] where eq_bot_or_exists_atom_le : ∀ (b : α), b = ⊥ ∨ ∃ (a : α), is_atom a ∧ a ≤ b /-- A lattice is coatomic iff every element other than `⊤` has a coatom above it. -/ class is_coatomic (α : Type u_1) [bounded_lattice α] where eq_top_or_exists_le_coatom : ∀ (b : α), b = ⊤ ∨ ∃ (a : α), is_coatom a ∧ b ≤ a @[simp] theorem is_coatomic_dual_iff_is_atomic {α : Type u_1} [bounded_lattice α] : is_coatomic (order_dual α) ↔ is_atomic α := { mp := fun (h : is_coatomic (order_dual α)) => is_atomic.mk fun (b : α) => eq_top_or_exists_le_coatom b, mpr := fun (h : is_atomic α) => is_coatomic.mk fun (b : order_dual α) => eq_bot_or_exists_atom_le b } @[simp] theorem is_atomic_dual_iff_is_coatomic {α : Type u_1} [bounded_lattice α] : is_atomic (order_dual α) ↔ is_coatomic α := { mp := fun (h : is_atomic (order_dual α)) => is_coatomic.mk fun (b : α) => eq_bot_or_exists_atom_le b, mpr := fun (h : is_coatomic α) => is_atomic.mk fun (b : order_dual α) => eq_top_or_exists_le_coatom b } namespace is_atomic protected instance is_coatomic_dual {α : Type u_1} [bounded_lattice α] [h : is_atomic α] : is_coatomic (order_dual α) := iff.mpr is_coatomic_dual_iff_is_atomic h protected instance set.Iic.is_atomic {α : Type u_1} [bounded_lattice α] [is_atomic α] {x : α} : is_atomic ↥(set.Iic x) := mk fun (_x : ↥(set.Iic x)) => sorry end is_atomic namespace is_coatomic protected instance is_coatomic {α : Type u_1} [bounded_lattice α] [h : is_coatomic α] : is_atomic (order_dual α) := iff.mpr is_atomic_dual_iff_is_coatomic h protected instance set.Ici.is_coatomic {α : Type u_1} [bounded_lattice α] [is_coatomic α] {x : α} : is_coatomic ↥(set.Ici x) := mk fun (_x : ↥(set.Ici x)) => sorry end is_coatomic theorem is_atomic_iff_forall_is_atomic_Iic {α : Type u_1} [bounded_lattice α] : is_atomic α ↔ ∀ (x : α), is_atomic ↥(set.Iic x) := sorry theorem is_coatomic_iff_forall_is_coatomic_Ici {α : Type u_1} [bounded_lattice α] : is_coatomic α ↔ ∀ (x : α), is_coatomic ↥(set.Ici x) := iff.trans (iff.symm is_atomic_dual_iff_is_coatomic) (iff.trans is_atomic_iff_forall_is_atomic_Iic (forall_congr fun (x : order_dual α) => iff.trans (iff.symm is_coatomic_dual_iff_is_atomic) iff.rfl)) /-- A lattice is atomistic iff every element is a `Sup` of a set of atoms. -/ class is_atomistic (α : Type u_1) [complete_lattice α] where eq_Sup_atoms : ∀ (b : α), ∃ (s : set α), b = Sup s ∧ ∀ (a : α), a ∈ s → is_atom a /-- A lattice is coatomistic iff every element is an `Inf` of a set of coatoms. -/ class is_coatomistic (α : Type u_1) [complete_lattice α] where eq_Inf_coatoms : ∀ (b : α), ∃ (s : set α), b = Inf s ∧ ∀ (a : α), a ∈ s → is_coatom a @[simp] theorem is_coatomistic_dual_iff_is_atomistic {α : Type u_1} [complete_lattice α] : is_coatomistic (order_dual α) ↔ is_atomistic α := { mp := fun (h : is_coatomistic (order_dual α)) => is_atomistic.mk fun (b : α) => eq_Inf_coatoms b, mpr := fun (h : is_atomistic α) => is_coatomistic.mk fun (b : order_dual α) => eq_Sup_atoms b } @[simp] theorem is_atomistic_dual_iff_is_coatomistic {α : Type u_1} [complete_lattice α] : is_atomistic (order_dual α) ↔ is_coatomistic α := { mp := fun (h : is_atomistic (order_dual α)) => is_coatomistic.mk fun (b : α) => eq_Sup_atoms b, mpr := fun (h : is_coatomistic α) => is_atomistic.mk fun (b : order_dual α) => eq_Inf_coatoms b } namespace is_atomistic protected instance is_coatomistic_dual {α : Type u_1} [complete_lattice α] [h : is_atomistic α] : is_coatomistic (order_dual α) := iff.mpr is_coatomistic_dual_iff_is_atomistic h protected instance is_atomic {α : Type u_1} [complete_lattice α] [is_atomistic α] : is_atomic α := sorry end is_atomistic namespace is_coatomistic protected instance is_atomistic_dual {α : Type u_1} [complete_lattice α] [h : is_coatomistic α] : is_atomistic (order_dual α) := iff.mpr is_atomistic_dual_iff_is_coatomistic h protected instance is_coatomic {α : Type u_1} [complete_lattice α] [is_coatomistic α] : is_coatomic α := sorry end is_coatomistic /-- A lattice is simple iff it has only two elements, `⊥` and `⊤`. -/ class is_simple_lattice (α : Type u_2) [bounded_lattice α] extends nontrivial α where eq_bot_or_eq_top : ∀ (a : α), a = ⊥ ∨ a = ⊤ theorem is_simple_lattice_iff_is_simple_lattice_order_dual {α : Type u_1} [bounded_lattice α] : is_simple_lattice α ↔ is_simple_lattice (order_dual α) := sorry protected instance order_dual.is_simple_lattice {α : Type u_1} [bounded_lattice α] [is_simple_lattice α] : is_simple_lattice (order_dual α) := iff.mp is_simple_lattice_iff_is_simple_lattice_order_dual _inst_2 @[simp] theorem is_atom_top {α : Type u_1} [bounded_lattice α] [is_simple_lattice α] : is_atom ⊤ := { left := top_ne_bot, right := fun (a : α) (ha : a < ⊤) => or.resolve_right (eq_bot_or_eq_top a) (ne_of_lt ha) } @[simp] theorem is_coatom_bot {α : Type u_1} [bounded_lattice α] [is_simple_lattice α] : is_coatom ⊥ := iff.mp is_atom_dual_iff_is_coatom is_atom_top namespace is_simple_lattice /-- A simple `bounded_lattice` is also distributive. -/ protected instance bounded_distrib_lattice {α : Type u_1} [bounded_lattice α] [is_simple_lattice α] : bounded_distrib_lattice α := bounded_distrib_lattice.mk bounded_lattice.sup bounded_lattice.le bounded_lattice.lt sorry sorry sorry sorry sorry sorry bounded_lattice.inf sorry sorry sorry sorry bounded_lattice.top sorry bounded_lattice.bot sorry protected instance is_atomic {α : Type u_1} [bounded_lattice α] [is_simple_lattice α] : is_atomic α := is_atomic.mk fun (b : α) => or.imp_right (fun (h : b = ⊤) => Exists.intro ⊤ { left := is_atom_top, right := ge_of_eq h }) (eq_bot_or_eq_top b) protected instance is_coatomic {α : Type u_1} [bounded_lattice α] [is_simple_lattice α] : is_coatomic α := iff.mp is_atomic_dual_iff_is_coatomic is_simple_lattice.is_atomic /-- Every simple lattice is order-isomorphic to `bool`. -/ def order_iso_bool {α : Type u_1} [bounded_lattice α] [is_simple_lattice α] [DecidableEq α] : α ≃o Bool := rel_iso.mk (equiv.mk (fun (x : α) => to_bool (x = ⊤)) (fun (x : Bool) => cond x ⊤ ⊥) sorry sorry) sorry protected instance fintype {α : Type u_1} [bounded_lattice α] [is_simple_lattice α] [DecidableEq α] : fintype α := fintype.of_equiv Bool (equiv.symm (rel_iso.to_equiv order_iso_bool)) /-- A simple `bounded_lattice` is also a `boolean_algebra`. -/ protected def boolean_algebra {α : Type u_1} [bounded_lattice α] [is_simple_lattice α] [DecidableEq α] : boolean_algebra α := boolean_algebra.mk bounded_distrib_lattice.sup bounded_distrib_lattice.le bounded_distrib_lattice.lt sorry sorry sorry sorry sorry sorry bounded_distrib_lattice.inf sorry sorry sorry sorry bounded_distrib_lattice.top sorry bounded_distrib_lattice.bot sorry (fun (x : α) => ite (x = ⊥) ⊤ ⊥) (fun (x y : α) => ite (x = ⊤ ∧ y = ⊥) ⊤ ⊥) sorry sorry sorry /-- A simple `bounded_lattice` is also complete. -/ protected def complete_lattice {α : Type u_1} [bounded_lattice α] [is_simple_lattice α] : complete_lattice α := complete_lattice.mk bounded_lattice.sup bounded_lattice.le bounded_lattice.lt sorry sorry sorry sorry sorry sorry bounded_lattice.inf sorry sorry sorry bounded_lattice.top sorry bounded_lattice.bot sorry (fun (s : set α) => ite (⊤ ∈ s) ⊤ ⊥) (fun (s : set α) => ite (⊥ ∈ s) ⊥ ⊤) sorry sorry sorry sorry /-- A simple `bounded_lattice` is also a `complete_boolean_algebra`. -/ protected def complete_boolean_algebra {α : Type u_1} [bounded_lattice α] [is_simple_lattice α] : complete_boolean_algebra α := complete_boolean_algebra.mk complete_lattice.sup complete_lattice.le complete_lattice.lt sorry sorry sorry sorry sorry sorry complete_lattice.inf sorry sorry sorry sorry complete_lattice.top sorry complete_lattice.bot sorry boolean_algebra.compl boolean_algebra.sdiff sorry sorry sorry complete_lattice.Sup complete_lattice.Inf sorry sorry sorry sorry sorry sorry end is_simple_lattice namespace is_simple_lattice protected instance is_atomistic {α : Type u_1} [complete_lattice α] [is_simple_lattice α] : is_atomistic α := is_atomistic.mk fun (b : α) => or.elim (eq_bot_or_eq_top b) (fun (h : b = ⊥) => Exists.intro ∅ { left := Eq.trans h (Eq.symm Sup_empty), right := fun (a : α) (ha : a ∈ ∅) => false.elim (set.not_mem_empty a ha) }) fun (h : b = ⊤) => Exists.intro (singleton ⊤) { left := Eq.trans h (Eq.symm Sup_singleton), right := fun (a : α) (ha : a ∈ singleton ⊤) => Eq.symm (iff.mp set.mem_singleton_iff ha) ▸ is_atom_top } protected instance is_coatomistic {α : Type u_1} [complete_lattice α] [is_simple_lattice α] : is_coatomistic α := iff.mp is_atomistic_dual_iff_is_coatomistic is_simple_lattice.is_atomistic end is_simple_lattice namespace fintype namespace is_simple_lattice theorem univ {α : Type u_1} [bounded_lattice α] [is_simple_lattice α] [DecidableEq α] : finset.univ = insert ⊤ (singleton ⊥) := sorry theorem card {α : Type u_1} [bounded_lattice α] [is_simple_lattice α] [DecidableEq α] : card α = bit0 1 := Eq.trans (of_equiv_card (equiv.symm (rel_iso.to_equiv is_simple_lattice.order_iso_bool))) card_bool end is_simple_lattice end fintype namespace bool protected instance is_simple_lattice : is_simple_lattice Bool := is_simple_lattice.mk fun (a : Bool) => eq.mpr (id (Eq._oldrec (Eq.refl (a = ⊥ ∨ a = ⊤)) (Eq.symm (propext finset.mem_singleton)))) (eq.mpr (id (Eq._oldrec (Eq.refl (a ∈ singleton ⊥ ∨ a = ⊤)) (propext or.comm))) (eq.mpr (id (Eq._oldrec (Eq.refl (a = ⊤ ∨ a ∈ singleton ⊥)) (Eq.symm (propext finset.mem_insert)))) (eq.mpr (id (Eq._oldrec (Eq.refl (a ∈ insert ⊤ (singleton ⊥))) top_eq_tt)) (eq.mpr (id (Eq._oldrec (Eq.refl (a ∈ insert tt (singleton ⊥))) bot_eq_ff)) (eq.mpr (id (Eq._oldrec (Eq.refl (a ∈ insert tt (singleton false))) (Eq.symm fintype.univ_bool))) (finset.mem_univ a)))))) end bool theorem is_simple_lattice_iff_is_atom_top {α : Type u_1} [bounded_lattice α] : is_simple_lattice α ↔ is_atom ⊤ := sorry theorem is_simple_lattice_iff_is_coatom_bot {α : Type u_1} [bounded_lattice α] : is_simple_lattice α ↔ is_coatom ⊥ := iff.trans is_simple_lattice_iff_is_simple_lattice_order_dual is_simple_lattice_iff_is_atom_top namespace set theorem is_simple_lattice_Iic_iff_is_atom {α : Type u_1} [bounded_lattice α] {a : α} : is_simple_lattice ↥(Iic a) ↔ is_atom a := sorry theorem is_simple_lattice_Ici_iff_is_coatom {α : Type u_1} [bounded_lattice α] {a : α} : is_simple_lattice ↥(Ici a) ↔ is_coatom a := sorry end set namespace order_iso @[simp] theorem is_atom_iff {α : Type u_1} [bounded_lattice α] {β : Type u_2} [bounded_lattice β] (f : α ≃o β) (a : α) : is_atom (coe_fn f a) ↔ is_atom a := sorry @[simp] theorem is_coatom_iff {α : Type u_1} [bounded_lattice α] {β : Type u_2} [bounded_lattice β] (f : α ≃o β) (a : α) : is_coatom (coe_fn f a) ↔ is_coatom a := is_atom_iff (order_iso.dual f) a theorem is_simple_lattice_iff {α : Type u_1} [bounded_lattice α] {β : Type u_2} [bounded_lattice β] (f : α ≃o β) : is_simple_lattice α ↔ is_simple_lattice β := sorry theorem is_simple_lattice {α : Type u_1} [bounded_lattice α] {β : Type u_2} [bounded_lattice β] [h : is_simple_lattice β] (f : α ≃o β) : is_simple_lattice α := iff.mpr (is_simple_lattice_iff f) h end order_iso namespace is_compl theorem is_atom_iff_is_coatom {α : Type u_1} [bounded_lattice α] [is_modular_lattice α] {a : α} {b : α} (hc : is_compl a b) : is_atom a ↔ is_coatom b := iff.trans (iff.symm set.is_simple_lattice_Iic_iff_is_atom) (iff.trans (order_iso.is_simple_lattice_iff (Iic_order_iso_Ici hc)) set.is_simple_lattice_Ici_iff_is_coatom) theorem is_coatom_iff_is_atom {α : Type u_1} [bounded_lattice α] [is_modular_lattice α] {a : α} {b : α} (hc : is_compl a b) : is_coatom a ↔ is_atom b := iff.symm (is_atom_iff_is_coatom (is_compl.symm hc)) end Mathlib
8e0d4d5527de6058ef862df17860cd34d9fe837e
ff5230333a701471f46c57e8c115a073ebaaa448
/tests/lean/run/psum_wf_rec.lean
8e4e40b991d4d6ce4877a6d32603a151b1ee0417
[ "Apache-2.0" ]
permissive
stanford-cs242/lean
f81721d2b5d00bc175f2e58c57b710d465e6c858
7bd861261f4a37326dcf8d7a17f1f1f330e4548c
refs/heads/master
1,600,957,431,849
1,576,465,093,000
1,576,465,093,000
225,779,423
0
3
Apache-2.0
1,575,433,936,000
1,575,433,935,000
null
UTF-8
Lean
false
false
804
lean
def psum.alt.sizeof {α β} [has_sizeof α] [has_sizeof β] : psum α β → nat | (psum.inl a) := 2*sizeof a + 2 | (psum.inr b) := 2*sizeof b + 1 def sum_has_sizeof_2 {α β} [has_sizeof α] [has_sizeof β] : has_sizeof (psum α β) := ⟨psum.alt.sizeof⟩ local attribute [instance] sum_has_sizeof_2 local attribute [simp] add_comm add_left_comm add_assoc mul_assoc mul_comm mul_left_comm mutual def f, g with f : ℕ → ℕ | n := g n + 1 with g : ℕ → ℕ | 0 := 0 | (n+1) := /- The following is a hint for the equation compiler. We will be able to delete it as soon as we have decision procedures for arithmetic -/ have 2 + n * 2 < 1 + 2 * (n + 1), from begin rw [left_distrib], simp, well_founded_tactics.cancel_nat_add_lt, tactic.comp_val end, f n
c97bd9e14c45eaee73321b07ec32117054245e77
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/linear_algebra/affine_space/affine_equiv.lean
c08341425569b8aea713d14752baf39b9e3785ef
[ "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
16,129
lean
/- Copyright (c) 2020 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury G. Kudryashov -/ import linear_algebra.affine_space.affine_map import algebra.invertible /-! # Affine equivalences In this file we define `affine_equiv k P₁ P₂` (notation: `P₁ ≃ᵃ[k] P₂`) to be the type of affine equivalences between `P₁` and `P₂, i.e., equivalences such that both forward and inverse maps are affine maps. We define the following equivalences: * `affine_equiv.refl k P`: the identity map as an `affine_equiv`; * `e.symm`: the inverse map of an `affine_equiv` as an `affine_equiv`; * `e.trans e'`: composition of two `affine_equiv`s; note that the order follows `mathlib`'s `category_theory` convention (apply `e`, then `e'`), not the convention used in function composition and compositions of bundled morphisms. ## Tags affine space, affine equivalence -/ open function set open_locale affine /-- An affine equivalence is an equivalence between affine spaces such that both forward and inverse maps are affine. We define it using an `equiv` for the map and a `linear_equiv` for the linear part in order to allow affine equivalences with good definitional equalities. -/ @[nolint has_inhabited_instance] structure affine_equiv (k P₁ P₂ : Type*) {V₁ V₂ : Type*} [ring k] [add_comm_group V₁] [module k V₁] [add_torsor V₁ P₁] [add_comm_group V₂] [module k V₂] [add_torsor V₂ P₂] extends P₁ ≃ P₂ := (linear : V₁ ≃ₗ[k] V₂) (map_vadd' : ∀ (p : P₁) (v : V₁), to_equiv (v +ᵥ p) = linear v +ᵥ to_equiv p) notation P₁ ` ≃ᵃ[`:25 k:25 `] `:0 P₂:0 := affine_equiv k P₁ P₂ variables {k V₁ V₂ V₃ V₄ P₁ P₂ P₃ P₄ : Type*} [ring k] [add_comm_group V₁] [module k V₁] [add_torsor V₁ P₁] [add_comm_group V₂] [module k V₂] [add_torsor V₂ P₂] [add_comm_group V₃] [module k V₃] [add_torsor V₃ P₃] [add_comm_group V₄] [module k V₄] [add_torsor V₄ P₄] namespace affine_equiv include V₁ V₂ instance : has_coe_to_fun (P₁ ≃ᵃ[k] P₂) (λ _, P₁ → P₂) := ⟨λ e, e.to_fun⟩ instance : has_coe (P₁ ≃ᵃ[k] P₂) (P₁ ≃ P₂) := ⟨affine_equiv.to_equiv⟩ variables (k P₁) omit V₂ /-- Identity map as an `affine_equiv`. -/ @[refl] def refl : P₁ ≃ᵃ[k] P₁ := { to_equiv := equiv.refl P₁, linear := linear_equiv.refl k V₁, map_vadd' := λ _ _, rfl } @[simp] lemma coe_refl : ⇑(refl k P₁) = id := rfl lemma refl_apply (x : P₁) : refl k P₁ x = x := rfl @[simp] lemma to_equiv_refl : (refl k P₁).to_equiv = equiv.refl P₁ := rfl @[simp] lemma linear_refl : (refl k P₁).linear = linear_equiv.refl k V₁ := rfl variables {k P₁} include V₂ @[simp] lemma map_vadd (e : P₁ ≃ᵃ[k] P₂) (p : P₁) (v : V₁) : e (v +ᵥ p) = e.linear v +ᵥ e p := e.map_vadd' p v @[simp] lemma coe_to_equiv (e : P₁ ≃ᵃ[k] P₂) : ⇑e.to_equiv = e := rfl /-- Reinterpret an `affine_equiv` as an `affine_map`. -/ def to_affine_map (e : P₁ ≃ᵃ[k] P₂) : P₁ →ᵃ[k] P₂ := { to_fun := e, .. e } instance : has_coe (P₁ ≃ᵃ[k] P₂) (P₁ →ᵃ[k] P₂) := ⟨to_affine_map⟩ @[simp] lemma coe_to_affine_map (e : P₁ ≃ᵃ[k] P₂) : (e.to_affine_map : P₁ → P₂) = (e : P₁ → P₂) := rfl @[simp] lemma to_affine_map_mk (f : P₁ ≃ P₂) (f' : V₁ ≃ₗ[k] V₂) (h) : to_affine_map (mk f f' h) = ⟨f, f', h⟩ := rfl @[norm_cast, simp] lemma coe_coe (e : P₁ ≃ᵃ[k] P₂) : ((e : P₁ →ᵃ[k] P₂) : P₁ → P₂) = e := rfl @[simp] lemma linear_to_affine_map (e : P₁ ≃ᵃ[k] P₂) : e.to_affine_map.linear = e.linear := rfl lemma to_affine_map_injective : injective (to_affine_map : (P₁ ≃ᵃ[k] P₂) → (P₁ →ᵃ[k] P₂)) := begin rintros ⟨e, el, h⟩ ⟨e', el', h'⟩ H, simp only [to_affine_map_mk, equiv.coe_inj, linear_equiv.to_linear_map_inj] at H, congr, exacts [H.1, H.2] end @[simp] lemma to_affine_map_inj {e e' : P₁ ≃ᵃ[k] P₂} : e.to_affine_map = e'.to_affine_map ↔ e = e' := to_affine_map_injective.eq_iff @[ext] lemma ext {e e' : P₁ ≃ᵃ[k] P₂} (h : ∀ x, e x = e' x) : e = e' := to_affine_map_injective $ affine_map.ext h lemma coe_fn_injective : @injective (P₁ ≃ᵃ[k] P₂) (P₁ → P₂) coe_fn := λ e e' H, ext $ congr_fun H @[simp, norm_cast] lemma coe_fn_inj {e e' : P₁ ≃ᵃ[k] P₂} : (e : P₁ → P₂) = e' ↔ e = e' := coe_fn_injective.eq_iff lemma to_equiv_injective : injective (to_equiv : (P₁ ≃ᵃ[k] P₂) → (P₁ ≃ P₂)) := λ e e' H, ext $ equiv.ext_iff.1 H @[simp] lemma to_equiv_inj {e e' : P₁ ≃ᵃ[k] P₂} : e.to_equiv = e'.to_equiv ↔ e = e' := to_equiv_injective.eq_iff @[simp] lemma coe_mk (e : P₁ ≃ P₂) (e' : V₁ ≃ₗ[k] V₂) (h) : ((⟨e, e', h⟩ : P₁ ≃ᵃ[k] P₂) : P₁ → P₂) = e := rfl /-- Construct an affine equivalence by verifying the relation between the map and its linear part at one base point. Namely, this function takes a map `e : P₁ → P₂`, a linear equivalence `e' : V₁ ≃ₗ[k] V₂`, and a point `p` such that for any other point `p'` we have `e p' = e' (p' -ᵥ p) +ᵥ e p`. -/ def mk' (e : P₁ → P₂) (e' : V₁ ≃ₗ[k] V₂) (p : P₁) (h : ∀ p' : P₁, e p' = e' (p' -ᵥ p) +ᵥ e p) : P₁ ≃ᵃ[k] P₂ := { to_fun := e, inv_fun := λ q' : P₂, e'.symm (q' -ᵥ e p) +ᵥ p, left_inv := λ p', by simp [h p'], right_inv := λ q', by simp [h (e'.symm (q' -ᵥ e p) +ᵥ p)], linear := e', map_vadd' := λ p' v, by { simp [h p', h (v +ᵥ p'), vadd_vsub_assoc, vadd_vadd] } } @[simp] lemma coe_mk' (e : P₁ ≃ P₂) (e' : V₁ ≃ₗ[k] V₂) (p h) : ⇑(mk' e e' p h) = e := rfl @[simp] lemma linear_mk' (e : P₁ ≃ P₂) (e' : V₁ ≃ₗ[k] V₂) (p h) : (mk' e e' p h).linear = e' := rfl /-- Inverse of an affine equivalence as an affine equivalence. -/ @[symm] def symm (e : P₁ ≃ᵃ[k] P₂) : P₂ ≃ᵃ[k] P₁ := { to_equiv := e.to_equiv.symm, linear := e.linear.symm, map_vadd' := λ v p, e.to_equiv.symm.apply_eq_iff_eq_symm_apply.2 $ by simpa using (e.to_equiv.apply_symm_apply v).symm } @[simp] lemma symm_to_equiv (e : P₁ ≃ᵃ[k] P₂) : e.to_equiv.symm = e.symm.to_equiv := rfl @[simp] lemma symm_linear (e : P₁ ≃ᵃ[k] P₂) : e.linear.symm = e.symm.linear := rfl protected lemma bijective (e : P₁ ≃ᵃ[k] P₂) : bijective e := e.to_equiv.bijective protected lemma surjective (e : P₁ ≃ᵃ[k] P₂) : surjective e := e.to_equiv.surjective protected lemma injective (e : P₁ ≃ᵃ[k] P₂) : injective e := e.to_equiv.injective @[simp] lemma range_eq (e : P₁ ≃ᵃ[k] P₂) : range e = univ := e.surjective.range_eq @[simp] lemma apply_symm_apply (e : P₁ ≃ᵃ[k] P₂) (p : P₂) : e (e.symm p) = p := e.to_equiv.apply_symm_apply p @[simp] lemma symm_apply_apply (e : P₁ ≃ᵃ[k] P₂) (p : P₁) : e.symm (e p) = p := e.to_equiv.symm_apply_apply p lemma apply_eq_iff_eq_symm_apply (e : P₁ ≃ᵃ[k] P₂) {p₁ p₂} : e p₁ = p₂ ↔ p₁ = e.symm p₂ := e.to_equiv.apply_eq_iff_eq_symm_apply @[simp] lemma apply_eq_iff_eq (e : P₁ ≃ᵃ[k] P₂) {p₁ p₂ : P₁} : e p₁ = e p₂ ↔ p₁ = p₂ := e.to_equiv.apply_eq_iff_eq omit V₂ @[simp] lemma symm_refl : (refl k P₁).symm = refl k P₁ := rfl include V₂ V₃ /-- Composition of two `affine_equiv`alences, applied left to right. -/ @[trans] def trans (e : P₁ ≃ᵃ[k] P₂) (e' : P₂ ≃ᵃ[k] P₃) : P₁ ≃ᵃ[k] P₃ := { to_equiv := e.to_equiv.trans e'.to_equiv, linear := e.linear.trans e'.linear, map_vadd' := λ p v, by simp only [linear_equiv.trans_apply, coe_to_equiv, (∘), equiv.coe_trans, map_vadd] } @[simp] lemma coe_trans (e : P₁ ≃ᵃ[k] P₂) (e' : P₂ ≃ᵃ[k] P₃) : ⇑(e.trans e') = e' ∘ e := rfl lemma trans_apply (e : P₁ ≃ᵃ[k] P₂) (e' : P₂ ≃ᵃ[k] P₃) (p : P₁) : e.trans e' p = e' (e p) := rfl include V₄ lemma trans_assoc (e₁ : P₁ ≃ᵃ[k] P₂) (e₂ : P₂ ≃ᵃ[k] P₃) (e₃ : P₃ ≃ᵃ[k] P₄) : (e₁.trans e₂).trans e₃ = e₁.trans (e₂.trans e₃) := ext $ λ _, rfl omit V₃ V₄ @[simp] lemma trans_refl (e : P₁ ≃ᵃ[k] P₂) : e.trans (refl k P₂) = e := ext $ λ _, rfl @[simp] lemma refl_trans (e : P₁ ≃ᵃ[k] P₂) : (refl k P₁).trans e = e := ext $ λ _, rfl @[simp] lemma trans_symm (e : P₁ ≃ᵃ[k] P₂) : e.trans e.symm = refl k P₁ := ext e.symm_apply_apply @[simp] lemma symm_trans (e : P₁ ≃ᵃ[k] P₂) : e.symm.trans e = refl k P₂ := ext e.apply_symm_apply @[simp] lemma apply_line_map (e : P₁ ≃ᵃ[k] P₂) (a b : P₁) (c : k) : e (affine_map.line_map a b c) = affine_map.line_map (e a) (e b) c := e.to_affine_map.apply_line_map a b c omit V₂ instance : group (P₁ ≃ᵃ[k] P₁) := { one := refl k P₁, mul := λ e e', e'.trans e, inv := symm, mul_assoc := λ e₁ e₂ e₃, trans_assoc _ _ _, one_mul := trans_refl, mul_one := refl_trans, mul_left_inv := trans_symm } lemma one_def : (1 : P₁ ≃ᵃ[k] P₁) = refl k P₁ := rfl @[simp] lemma coe_one : ⇑(1 : P₁ ≃ᵃ[k] P₁) = id := rfl lemma mul_def (e e' : P₁ ≃ᵃ[k] P₁) : e * e' = e'.trans e := rfl @[simp] lemma coe_mul (e e' : P₁ ≃ᵃ[k] P₁) : ⇑(e * e') = e ∘ e' := rfl lemma inv_def (e : P₁ ≃ᵃ[k] P₁) : e⁻¹ = e.symm := rfl variable (k) /-- The map `v ↦ v +ᵥ b` as an affine equivalence between a module `V` and an affine space `P` with tangent space `V`. -/ def vadd_const (b : P₁) : V₁ ≃ᵃ[k] P₁ := { to_equiv := equiv.vadd_const b, linear := linear_equiv.refl _ _, map_vadd' := λ p v, add_vadd _ _ _ } @[simp] lemma linear_vadd_const (b : P₁) : (vadd_const k b).linear = linear_equiv.refl k V₁ := rfl @[simp] lemma vadd_const_apply (b : P₁) (v : V₁) : vadd_const k b v = v +ᵥ b := rfl @[simp] lemma vadd_const_symm_apply (b p : P₁) : (vadd_const k b).symm p = p -ᵥ b := rfl /-- `p' ↦ p -ᵥ p'` as an equivalence. -/ def const_vsub (p : P₁) : P₁ ≃ᵃ[k] V₁ := { to_equiv := equiv.const_vsub p, linear := linear_equiv.neg k, map_vadd' := λ p' v, by simp [vsub_vadd_eq_vsub_sub, neg_add_eq_sub] } @[simp] lemma coe_const_vsub (p : P₁) : ⇑(const_vsub k p) = (-ᵥ) p := rfl @[simp] lemma coe_const_vsub_symm (p : P₁) : ⇑(const_vsub k p).symm = λ v, -v +ᵥ p := rfl variable (P₁) /-- The map `p ↦ v +ᵥ p` as an affine automorphism of an affine space. -/ def const_vadd (v : V₁) : P₁ ≃ᵃ[k] P₁ := { to_equiv := equiv.const_vadd P₁ v, linear := linear_equiv.refl _ _, map_vadd' := λ p w, vadd_comm _ _ _ } @[simp] lemma linear_const_vadd (v : V₁) : (const_vadd k P₁ v).linear = linear_equiv.refl _ _ := rfl @[simp] lemma const_vadd_apply (v : V₁) (p : P₁) : const_vadd k P₁ v p = v +ᵥ p := rfl @[simp] lemma const_vadd_symm_apply (v : V₁) (p : P₁) : (const_vadd k P₁ v).symm p = -v +ᵥ p := rfl section homothety omit V₁ variables {R V P : Type*} [comm_ring R] [add_comm_group V] [module R V] [affine_space V P] include V /-- Fixing a point in affine space, homothety about this point gives a group homomorphism from (the centre of) the units of the scalars into the group of affine equivalences. -/ def homothety_units_mul_hom (p : P) : units R →* P ≃ᵃ[R] P := { to_fun := λ t, { to_fun := affine_map.homothety p (t : R), inv_fun := affine_map.homothety p (↑t⁻¹ : R), left_inv := λ p, by simp [← affine_map.comp_apply, ← affine_map.homothety_mul], right_inv := λ p, by simp [← affine_map.comp_apply, ← affine_map.homothety_mul], linear := { inv_fun := linear_map.lsmul R V (↑t⁻¹ : R), left_inv := λ v, by simp [smul_smul], right_inv := λ v, by simp [smul_smul], .. linear_map.lsmul R V t, }, map_vadd' := λ p v, by simp only [vadd_vsub_assoc, smul_add, add_vadd, affine_map.coe_line_map, affine_map.homothety_eq_line_map, equiv.coe_fn_mk, linear_equiv.coe_mk, linear_map.lsmul_apply, linear_map.to_fun_eq_coe], }, map_one' := by { ext, simp, }, map_mul' := λ t₁ t₂, by { ext, simp [← affine_map.comp_apply, ← affine_map.homothety_mul], }, } @[simp] lemma coe_homothety_units_mul_hom_apply (p : P) (t : units R) : (homothety_units_mul_hom p t : P → P) = affine_map.homothety p (t : R) := rfl @[simp] lemma coe_homothety_units_mul_hom_apply_symm (p : P) (t : units R) : ((homothety_units_mul_hom p t).symm : P → P) = affine_map.homothety p (↑t⁻¹ : R) := rfl @[simp] lemma coe_homothety_units_mul_hom_eq_homothety_hom_coe (p : P) : (coe : (P ≃ᵃ[R] P) → P →ᵃ[R] P) ∘ homothety_units_mul_hom p = (affine_map.homothety_hom p) ∘ (coe : units R → R) := by { ext, simp, } end homothety variable {P₁} open function /-- Point reflection in `x` as a permutation. -/ def point_reflection (x : P₁) : P₁ ≃ᵃ[k] P₁ := (const_vsub k x).trans (vadd_const k x) lemma point_reflection_apply (x y : P₁) : point_reflection k x y = x -ᵥ y +ᵥ x := rfl @[simp] lemma point_reflection_symm (x : P₁) : (point_reflection k x).symm = point_reflection k x := to_equiv_injective $ equiv.point_reflection_symm x @[simp] lemma to_equiv_point_reflection (x : P₁) : (point_reflection k x).to_equiv = equiv.point_reflection x := rfl @[simp] lemma point_reflection_self (x : P₁) : point_reflection k x x = x := vsub_vadd _ _ lemma point_reflection_involutive (x : P₁) : involutive (point_reflection k x : P₁ → P₁) := equiv.point_reflection_involutive x /-- `x` is the only fixed point of `point_reflection x`. This lemma requires `x + x = y + y ↔ x = y`. There is no typeclass to use here, so we add it as an explicit argument. -/ lemma point_reflection_fixed_iff_of_injective_bit0 {x y : P₁} (h : injective (bit0 : V₁ → V₁)) : point_reflection k x y = y ↔ y = x := equiv.point_reflection_fixed_iff_of_injective_bit0 h lemma injective_point_reflection_left_of_injective_bit0 (h : injective (bit0 : V₁ → V₁)) (y : P₁) : injective (λ x : P₁, point_reflection k x y) := equiv.injective_point_reflection_left_of_injective_bit0 h y lemma injective_point_reflection_left_of_module [invertible (2:k)]: ∀ y, injective (λ x : P₁, point_reflection k x y) := injective_point_reflection_left_of_injective_bit0 k $ λ x y h, by rwa [bit0, bit0, ← two_smul k x, ← two_smul k y, (is_unit_of_invertible (2:k)).smul_left_cancel] at h lemma point_reflection_fixed_iff_of_module [invertible (2:k)] {x y : P₁} : point_reflection k x y = y ↔ y = x := ((injective_point_reflection_left_of_module k y).eq_iff' (point_reflection_self k y)).trans eq_comm end affine_equiv namespace linear_equiv /-- Interpret a linear equivalence between modules as an affine equivalence. -/ def to_affine_equiv (e : V₁ ≃ₗ[k] V₂) : V₁ ≃ᵃ[k] V₂ := { to_equiv := e.to_equiv, linear := e, map_vadd' := λ p v, e.map_add v p } @[simp] lemma coe_to_affine_equiv (e : V₁ ≃ₗ[k] V₂) : ⇑e.to_affine_equiv = e := rfl end linear_equiv namespace affine_map open affine_equiv include V₁ lemma line_map_vadd (v v' : V₁) (p : P₁) (c : k) : line_map v v' c +ᵥ p = line_map (v +ᵥ p) (v' +ᵥ p) c := (vadd_const k p).apply_line_map v v' c lemma line_map_vsub (p₁ p₂ p₃ : P₁) (c : k) : line_map p₁ p₂ c -ᵥ p₃ = line_map (p₁ -ᵥ p₃) (p₂ -ᵥ p₃) c := (vadd_const k p₃).symm.apply_line_map p₁ p₂ c lemma vsub_line_map (p₁ p₂ p₃ : P₁) (c : k) : p₁ -ᵥ line_map p₂ p₃ c = line_map (p₁ -ᵥ p₂) (p₁ -ᵥ p₃) c := (const_vsub k p₁).apply_line_map p₂ p₃ c lemma vadd_line_map (v : V₁) (p₁ p₂ : P₁) (c : k) : v +ᵥ line_map p₁ p₂ c = line_map (v +ᵥ p₁) (v +ᵥ p₂) c := (const_vadd k P₁ v).apply_line_map p₁ p₂ c variables {R' : Type*} [comm_ring R'] [module R' V₁] lemma homothety_neg_one_apply (c p : P₁) : homothety c (-1:R') p = point_reflection R' c p := by simp [homothety_apply, point_reflection_apply] end affine_map
e2f36a791ff926dd82c54f13c58e7c6f49154ed3
c777c32c8e484e195053731103c5e52af26a25d1
/src/topology/algebra/continuous_affine_map.lean
df8ea46d78521afc5ff35f66866b97d685403b54
[ "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
8,348
lean
/- Copyright (c) 2021 Oliver Nash. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Oliver Nash -/ import linear_algebra.affine_space.affine_map import topology.continuous_function.basic import topology.algebra.module.basic /-! # Continuous affine maps. This file defines a type of bundled continuous affine maps. Note that the definition and basic properties established here require minimal assumptions, and do not even assume compatibility between the topological and algebraic structures. Of course it is necessary to assume some compatibility in order to obtain a useful theory. Such a theory is developed elsewhere for affine spaces modelled on _normed_ vector spaces, but not yet for general topological affine spaces (since we have not defined these yet). ## Main definitions: * `continuous_affine_map` ## Notation: We introduce the notation `P →A[R] Q` for `continuous_affine_map R P Q`. Note that this is parallel to the notation `E →L[R] F` for `continuous_linear_map R E F`. -/ /-- A continuous map of affine spaces. -/ structure continuous_affine_map (R : Type*) {V W : Type*} (P Q : Type*) [ring R] [add_comm_group V] [module R V] [topological_space P] [add_torsor V P] [add_comm_group W] [module R W] [topological_space Q] [add_torsor W Q] extends P →ᵃ[R] Q := (cont : continuous to_fun) notation P ` →A[`:25 R `] ` Q := continuous_affine_map R P Q namespace continuous_affine_map variables {R V W P Q : Type*} [ring R] variables [add_comm_group V] [module R V] [topological_space P] [add_torsor V P] variables [add_comm_group W] [module R W] [topological_space Q] [add_torsor W Q] include V W instance : has_coe (P →A[R] Q) (P →ᵃ[R] Q) := ⟨to_affine_map⟩ lemma to_affine_map_injective {f g : P →A[R] Q} (h : (f : P →ᵃ[R] Q) = (g : P →ᵃ[R] Q)) : f = g := by { cases f, cases g, congr' } instance : continuous_map_class (P →A[R] Q) P Q := { coe := λ f, f.to_affine_map, coe_injective' := λ f g h, to_affine_map_injective $ fun_like.coe_injective h, map_continuous := cont } /-- Helper instance for when there's too many metavariables to apply `fun_like.has_coe_to_fun` directly. -/ instance : has_coe_to_fun (P →A[R] Q) (λ _, P → Q) := fun_like.has_coe_to_fun lemma to_fun_eq_coe (f : P →A[R] Q) : f.to_fun = ⇑f := rfl lemma coe_injective : @function.injective (P →A[R] Q) (P → Q) coe_fn := fun_like.coe_injective @[ext] lemma ext {f g : P →A[R] Q} (h : ∀ x, f x = g x) : f = g := fun_like.ext _ _ h lemma ext_iff {f g : P →A[R] Q} : f = g ↔ ∀ x, f x = g x := fun_like.ext_iff lemma congr_fun {f g : P →A[R] Q} (h : f = g) (x : P) : f x = g x := fun_like.congr_fun h _ /-- Forgetting its algebraic properties, a continuous affine map is a continuous map. -/ def to_continuous_map (f : P →A[R] Q) : C(P, Q) := ⟨f, f.cont⟩ instance : has_coe (P →A[R] Q) (C(P, Q)) := ⟨to_continuous_map⟩ @[simp] lemma to_affine_map_eq_coe (f : P →A[R] Q) : f.to_affine_map = ↑f := rfl @[simp] lemma to_continuous_map_coe (f : P →A[R] Q) : f.to_continuous_map = ↑f := rfl @[simp, norm_cast] lemma coe_to_affine_map (f : P →A[R] Q) : ((f : P →ᵃ[R] Q) : P → Q) = f := rfl @[simp, norm_cast] lemma coe_to_continuous_map (f : P →A[R] Q) : ((f : C(P, Q)) : P → Q) = f := rfl lemma to_continuous_map_injective {f g : P →A[R] Q} (h : (f : C(P, Q)) = (g : C(P, Q))) : f = g := by { ext a, exact continuous_map.congr_fun h a, } @[norm_cast] lemma coe_affine_map_mk (f : P →ᵃ[R] Q) (h) : ((⟨f, h⟩ : P →A[R] Q) : P →ᵃ[R] Q) = f := rfl @[norm_cast] lemma coe_continuous_map_mk (f : P →ᵃ[R] Q) (h) : ((⟨f, h⟩ : P →A[R] Q) : C(P, Q)) = ⟨f, h⟩ := rfl @[simp] lemma coe_mk (f : P →ᵃ[R] Q) (h) : ((⟨f, h⟩ : P →A[R] Q) : P → Q) = f := rfl @[simp] lemma mk_coe (f : P →A[R] Q) (h) : (⟨(f : P →ᵃ[R] Q), h⟩ : P →A[R] Q) = f := by { ext, refl, } @[continuity] protected lemma continuous (f : P →A[R] Q) : continuous f := f.2 variables (R P) /-- The constant map is a continuous affine map. -/ def const (q : Q) : P →A[R] Q := { to_fun := affine_map.const R P q, cont := continuous_const, .. affine_map.const R P q, } @[simp] lemma coe_const (q : Q) : (const R P q : P → Q) = function.const P q := rfl noncomputable instance : inhabited (P →A[R] Q) := ⟨const R P $ nonempty.some (by apply_instance : nonempty Q)⟩ variables {R P} {W₂ Q₂ : Type*} variables [add_comm_group W₂] [module R W₂] [topological_space Q₂] [add_torsor W₂ Q₂] include W₂ /-- The composition of morphisms is a morphism. -/ def comp (f : Q →A[R] Q₂) (g : P →A[R] Q) : P →A[R] Q₂ := { cont := f.cont.comp g.cont, .. (f : Q →ᵃ[R] Q₂).comp (g : P →ᵃ[R] Q), } @[simp, norm_cast] lemma coe_comp (f : Q →A[R] Q₂) (g : P →A[R] Q) : (f.comp g : P → Q₂) = (f : Q → Q₂) ∘ (g : P → Q) := rfl lemma comp_apply (f : Q →A[R] Q₂) (g : P →A[R] Q) (x : P) : f.comp g x = f (g x) := rfl omit W₂ section module_valued_maps variables {S : Type*} variables [topological_space W] instance : has_zero (P →A[R] W) := ⟨continuous_affine_map.const R P 0⟩ @[norm_cast, simp] lemma coe_zero : ((0 : P →A[R] W) : P → W) = 0 := rfl lemma zero_apply (x : P) : (0 : P →A[R] W) x = 0 := rfl section mul_action variables [monoid S] [distrib_mul_action S W] [smul_comm_class R S W] variables [has_continuous_const_smul S W] instance : has_smul S (P →A[R] W) := { smul := λ t f, { cont := f.continuous.const_smul t, .. (t • (f : P →ᵃ[R] W)) } } @[norm_cast, simp] lemma coe_smul (t : S) (f : P →A[R] W) : ⇑(t • f) = t • f := rfl lemma smul_apply (t : S) (f : P →A[R] W) (x : P) : (t • f) x = t • (f x) := rfl instance [distrib_mul_action Sᵐᵒᵖ W] [is_central_scalar S W] : is_central_scalar S (P →A[R] W) := { op_smul_eq_smul := λ t f, ext $ λ _, op_smul_eq_smul _ _ } instance : mul_action S (P →A[R] W) := function.injective.mul_action _ coe_injective coe_smul end mul_action variables [topological_add_group W] instance : has_add (P →A[R] W) := { add := λ f g, { cont := f.continuous.add g.continuous, .. ((f : P →ᵃ[R] W) + (g : P →ᵃ[R] W)) }, } @[norm_cast, simp] lemma coe_add (f g : P →A[R] W) : ⇑(f + g) = f + g := rfl lemma add_apply (f g : P →A[R] W) (x : P) : (f + g) x = f x + g x := rfl instance : has_sub (P →A[R] W) := { sub := λ f g, { cont := f.continuous.sub g.continuous, .. ((f : P →ᵃ[R] W) - (g : P →ᵃ[R] W)) }, } @[norm_cast, simp] lemma coe_sub (f g : P →A[R] W) : ⇑(f - g) = f - g := rfl lemma sub_apply (f g : P →A[R] W) (x : P) : (f - g) x = f x - g x := rfl instance : has_neg (P →A[R] W) := { neg := λ f, { cont := f.continuous.neg, .. (-(f : P →ᵃ[R] W)) }, } @[norm_cast, simp] lemma coe_neg (f : P →A[R] W) : ⇑(-f) = -f := rfl lemma neg_apply (f : P →A[R] W) (x : P) : (-f) x = -(f x) := rfl instance : add_comm_group (P →A[R] W) := coe_injective.add_comm_group _ coe_zero coe_add coe_neg coe_sub (λ _ _, coe_smul _ _) (λ _ _, coe_smul _ _) instance [monoid S] [distrib_mul_action S W] [smul_comm_class R S W] [has_continuous_const_smul S W] : distrib_mul_action S (P →A[R] W) := function.injective.distrib_mul_action ⟨λ f, f.to_affine_map.to_fun, rfl, coe_add⟩ coe_injective coe_smul instance [semiring S] [module S W] [smul_comm_class R S W] [has_continuous_const_smul S W] : module S (P →A[R] W) := function.injective.module S ⟨λ f, f.to_affine_map.to_fun, rfl, coe_add⟩ coe_injective coe_smul end module_valued_maps end continuous_affine_map namespace continuous_linear_map variables {R V W : Type*} [ring R] variables [add_comm_group V] [module R V] [topological_space V] variables [add_comm_group W] [module R W] [topological_space W] /-- A continuous linear map can be regarded as a continuous affine map. -/ def to_continuous_affine_map (f : V →L[R] W) : V →A[R] W := { to_fun := f, linear := f, map_vadd' := by simp, cont := f.cont, } @[simp] lemma coe_to_continuous_affine_map (f : V →L[R] W) : ⇑f.to_continuous_affine_map = f := rfl @[simp] lemma to_continuous_affine_map_map_zero (f : V →L[R] W) : f.to_continuous_affine_map 0 = 0 := by simp end continuous_linear_map
31bdc901081bd3d6d3b691be8b6823be7ff73d2c
82b86ba2ae0d5aed0f01f49c46db5afec0eb2bd7
/tests/lean/run/structInst4.lean
6fa6066fbb9eca12ac868cfb89af6ddcf3e7be89
[ "Apache-2.0" ]
permissive
banksonian/lean4
3a2e6b0f1eb63aa56ff95b8d07b2f851072d54dc
78da6b3aa2840693eea354a41e89fc5b212a5011
refs/heads/master
1,673,703,624,165
1,605,123,551,000
1,605,123,551,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
486
lean
universes u def a : Array ((Nat × Nat) × Bool) := #[] def b : Array Nat := #[] structure Foo := (x : Array ((Nat × Nat) × Bool) := #[]) (y : Nat := 0) #check (b).modifyOp (idx := 1) (fun s => 2) #check { b with [1] := 2 } #check { a with [1].fst.2 := 1 } def foo : Foo := {} #check foo.x[1].1.2 #check { foo with x[1].2 := true } #check { foo with x[1].fst.snd := 1 } #check { foo with x[1].1.fst := 1 } #check { foo with x[1].1.1 := 5 } #check { foo with x[1].1.2 := 5 }
a3a0dd5e90006c0402078f6f19d910761aac55b3
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/group_theory/subgroup/basic.lean
b09bcb9c45d469532b350b8627fa58d8284b870b
[ "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
129,490
lean
/- Copyright (c) 2020 Kexing Ying. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kexing Ying -/ import group_theory.submonoid.pointwise import group_theory.submonoid.membership import group_theory.submonoid.centralizer import algebra.group.conj import algebra.module.basic import order.atoms import order.sup_indep /-! # Subgroups This file defines multiplicative and additive subgroups as an extension of submonoids, in a bundled form (unbundled subgroups are in `deprecated/subgroups.lean`). We prove subgroups of a group form a complete lattice, and results about images and preimages of subgroups under group homomorphisms. The bundled subgroups use bundled monoid homomorphisms. There are also theorems about the subgroups generated by an element or a subset of a group, defined both inductively and as the infimum of the set of subgroups containing a given element/subset. Special thanks goes to Amelia Livingston and Yury Kudryashov for their help and inspiration. ## Main definitions Notation used here: - `G N` are `group`s - `A` is an `add_group` - `H K` are `subgroup`s of `G` or `add_subgroup`s of `A` - `x` is an element of type `G` or type `A` - `f g : N →* G` are group homomorphisms - `s k` are sets of elements of type `G` Definitions in the file: * `subgroup G` : the type of subgroups of a group `G` * `add_subgroup A` : the type of subgroups of an additive group `A` * `complete_lattice (subgroup G)` : the subgroups of `G` form a complete lattice * `subgroup.closure k` : the minimal subgroup that includes the set `k` * `subgroup.subtype` : the natural group homomorphism from a subgroup of group `G` to `G` * `subgroup.gi` : `closure` forms a Galois insertion with the coercion to set * `subgroup.comap H f` : the preimage of a subgroup `H` along the group homomorphism `f` is also a subgroup * `subgroup.map f H` : the image of a subgroup `H` along the group homomorphism `f` is also a subgroup * `subgroup.prod H K` : the product of subgroups `H`, `K` of groups `G`, `N` respectively, `H × K` is a subgroup of `G × N` * `monoid_hom.range f` : the range of the group homomorphism `f` is a subgroup * `monoid_hom.ker f` : the kernel of a group homomorphism `f` is the subgroup of elements `x : G` such that `f x = 1` * `monoid_hom.eq_locus f g` : given group homomorphisms `f`, `g`, the elements of `G` such that `f x = g x` form a subgroup of `G` * `is_simple_group G` : a class indicating that a group has exactly two normal subgroups ## Implementation notes Subgroup inclusion is denoted `≤` rather than `⊆`, although `∈` is defined as membership of a subgroup's underlying set. ## Tags subgroup, subgroups -/ open_locale big_operators pointwise variables {G : Type*} [group G] variables {A : Type*} [add_group A] section subgroup_class /-- `inv_mem_class S G` states `S` is a type of subsets `s ⊆ G` closed under inverses. -/ class inv_mem_class (S G : Type*) [has_inv G] [set_like S G] := (inv_mem : ∀ {s : S} {x}, x ∈ s → x⁻¹ ∈ s) export inv_mem_class (inv_mem) /-- `neg_mem_class S G` states `S` is a type of subsets `s ⊆ G` closed under negation. -/ class neg_mem_class (S G : Type*) [has_neg G] [set_like S G] := (neg_mem : ∀ {s : S} {x}, x ∈ s → -x ∈ s) export neg_mem_class (neg_mem) /-- `subgroup_class S G` states `S` is a type of subsets `s ⊆ G` that are subgroups of `G`. -/ class subgroup_class (S G : Type*) [div_inv_monoid G] [set_like S G] extends submonoid_class S G := (inv_mem : ∀ {s : S} {x}, x ∈ s → x⁻¹ ∈ s) /-- `add_subgroup_class S G` states `S` is a type of subsets `s ⊆ G` that are additive subgroups of `G`. -/ class add_subgroup_class (S G : Type*) [sub_neg_monoid G] [set_like S G] extends add_submonoid_class S G := (neg_mem : ∀ {s : S} {x}, x ∈ s → -x ∈ s) attribute [to_additive] inv_mem_class subgroup_class variables (M S : Type*) [div_inv_monoid M] [set_like S M] [hSM : subgroup_class S M] include hSM @[to_additive, priority 100] -- See note [lower instance priority] instance subgroup_class.to_inv_mem_class : inv_mem_class S M := { .. hSM } variables {S M} {H K : S} /-- A subgroup is closed under division. -/ @[to_additive "An additive subgroup is closed under subtraction."] theorem div_mem {x y : M} (hx : x ∈ H) (hy : y ∈ H) : x / y ∈ H := by rw [div_eq_mul_inv]; exact mul_mem hx (inv_mem hy) @[to_additive] lemma zpow_mem {x : M} (hx : x ∈ K) : ∀ n : ℤ, x ^ n ∈ K | (n : ℕ) := by { rw [zpow_coe_nat], exact pow_mem hx n } | -[1+ n] := by { rw [zpow_neg_succ_of_nat], exact inv_mem (pow_mem hx n.succ) } omit hSM variables [set_like S G] [hSG : subgroup_class S G] include hSG @[simp, to_additive] theorem inv_mem_iff {x : G} : x⁻¹ ∈ H ↔ x ∈ H := ⟨λ h, inv_inv x ▸ inv_mem h, inv_mem⟩ @[to_additive] lemma div_mem_comm_iff {a b : G} : a / b ∈ H ↔ b / a ∈ H := by rw [← inv_mem_iff, div_eq_mul_inv, div_eq_mul_inv, mul_inv_rev, inv_inv] @[simp, to_additive] theorem inv_coe_set : (H : set G)⁻¹ = H := by { ext, simp } @[simp, to_additive] lemma exists_inv_mem_iff_exists_mem {P : G → Prop} : (∃ (x : G), x ∈ H ∧ P x⁻¹) ↔ ∃ x ∈ H, P x := by split; { rintros ⟨x, x_in, hx⟩, exact ⟨x⁻¹, inv_mem x_in, by simp [hx]⟩ } @[to_additive] lemma mul_mem_cancel_right {x y : G} (h : x ∈ H) : y * x ∈ H ↔ y ∈ H := ⟨λ hba, by simpa using mul_mem hba (inv_mem h), λ hb, mul_mem hb h⟩ @[to_additive] lemma mul_mem_cancel_left {x y : G} (h : x ∈ H) : x * y ∈ H ↔ y ∈ H := ⟨λ hab, by simpa using mul_mem (inv_mem h) hab, mul_mem h⟩ namespace subgroup_class omit hSG include hSM /-- A subgroup of a group inherits an inverse. -/ @[to_additive "An additive subgroup of a `add_group` inherits an inverse."] instance has_inv : has_inv H := ⟨λ a, ⟨a⁻¹, inv_mem a.2⟩⟩ /-- A subgroup of a group inherits a division -/ @[to_additive "An additive subgroup of an `add_group` inherits a subtraction."] instance has_div : has_div H := ⟨λ a b, ⟨a / b, div_mem a.2 b.2⟩⟩ omit hSM /-- An additive subgroup of an `add_group` inherits an integer scaling. -/ instance _root_.add_subgroup_class.has_zsmul {M S} [sub_neg_monoid M] [set_like S M] [add_subgroup_class S M] {H : S} : has_smul ℤ H := ⟨λ n a, ⟨n • a, zsmul_mem a.2 n⟩⟩ include hSM /-- A subgroup of a group inherits an integer power. -/ @[to_additive] instance has_zpow : has_pow H ℤ := ⟨λ a n, ⟨a ^ n, zpow_mem a.2 n⟩⟩ @[simp, norm_cast, to_additive] lemma coe_inv (x : H) : ↑(x⁻¹ : H) = (x⁻¹ : M) := rfl @[simp, norm_cast, to_additive] lemma coe_div (x y : H) : (↑(x / y) : M) = ↑x / ↑y := rfl omit hSM variables (H) include hSG /-- A subgroup of a group inherits a group structure. -/ @[to_additive "An additive subgroup of an `add_group` inherits an `add_group` structure.", priority 75] -- Prefer subclasses of `group` over subclasses of `subgroup_class`. instance to_group : group H := subtype.coe_injective.group _ rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) omit hSG /-- A subgroup of a `comm_group` is a `comm_group`. -/ @[to_additive "An additive subgroup of an `add_comm_group` is an `add_comm_group`.", priority 75] -- Prefer subclasses of `comm_group` over subclasses of `subgroup_class`. instance to_comm_group {G : Type*} [comm_group G] [set_like S G] [subgroup_class S G] : comm_group H := subtype.coe_injective.comm_group _ rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) /-- A subgroup of an `ordered_comm_group` is an `ordered_comm_group`. -/ @[to_additive "An additive subgroup of an `add_ordered_comm_group` is an `add_ordered_comm_group`.", priority 75] -- Prefer subclasses of `group` over subclasses of `subgroup_class`. instance to_ordered_comm_group {G : Type*} [ordered_comm_group G] [set_like S G] [subgroup_class S G] : ordered_comm_group H := subtype.coe_injective.ordered_comm_group _ rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) /-- A subgroup of a `linear_ordered_comm_group` is a `linear_ordered_comm_group`. -/ @[to_additive "An additive subgroup of a `linear_ordered_add_comm_group` is a `linear_ordered_add_comm_group`.", priority 75] -- Prefer subclasses of `group` over subclasses of `subgroup_class`. instance to_linear_ordered_comm_group {G : Type*} [linear_ordered_comm_group G] [set_like S G] [subgroup_class S G] : linear_ordered_comm_group H := subtype.coe_injective.linear_ordered_comm_group _ rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) include hSG /-- The natural group hom from a subgroup of group `G` to `G`. -/ @[to_additive "The natural group hom from an additive subgroup of `add_group` `G` to `G`."] def subtype : H →* G := ⟨coe, rfl, λ _ _, rfl⟩ @[simp, to_additive] theorem coe_subtype : (subtype H : H → G) = coe := rfl variables {H} @[simp, norm_cast, to_additive coe_smul] lemma coe_pow (x : H) (n : ℕ) : ((x ^ n : H) : G) = x ^ n := (subtype H : H →* G).map_pow _ _ @[simp, norm_cast, to_additive] lemma coe_zpow (x : H) (n : ℤ) : ((x ^ n : H) : G) = x ^ n := (subtype H : H →* G).map_zpow _ _ /-- The inclusion homomorphism from a subgroup `H` contained in `K` to `K`. -/ @[to_additive "The inclusion homomorphism from a additive subgroup `H` contained in `K` to `K`."] def inclusion {H K : S} (h : H ≤ K) : H →* K := monoid_hom.mk' (λ x, ⟨x, h x.prop⟩) (λ ⟨a, ha⟩ ⟨b, hb⟩, rfl) @[simp, to_additive] lemma inclusion_self (x : H) : inclusion le_rfl x = x := by { cases x, refl } @[simp, to_additive] lemma inclusion_mk {h : H ≤ K} (x : G) (hx : x ∈ H) : inclusion h ⟨x, hx⟩ = ⟨x, h hx⟩ := rfl @[to_additive] lemma inclusion_right (h : H ≤ K) (x : K) (hx : (x : G) ∈ H) : inclusion h ⟨x, hx⟩ = x := by { cases x, refl } @[simp] lemma inclusion_inclusion {L : S} (hHK : H ≤ K) (hKL : K ≤ L) (x : H) : inclusion hKL (inclusion hHK x) = inclusion (hHK.trans hKL) x := by { cases x, refl } @[simp, to_additive] lemma coe_inclusion {H K : S} {h : H ≤ K} (a : H) : (inclusion h a : G) = a := by { cases a, simp only [inclusion, set_like.coe_mk, monoid_hom.mk'_apply] } @[simp, to_additive] lemma subtype_comp_inclusion {H K : S} (hH : H ≤ K) : (subtype K).comp (inclusion hH) = subtype H := by { ext, simp only [monoid_hom.comp_apply, coe_subtype, coe_inclusion] } end subgroup_class end subgroup_class set_option old_structure_cmd true /-- A subgroup of a group `G` is a subset containing 1, closed under multiplication and closed under multiplicative inverse. -/ structure subgroup (G : Type*) [group G] extends submonoid G := (inv_mem' {x} : x ∈ carrier → x⁻¹ ∈ carrier) /-- An additive subgroup of an additive group `G` is a subset containing 0, closed under addition and additive inverse. -/ structure add_subgroup (G : Type*) [add_group G] extends add_submonoid G:= (neg_mem' {x} : x ∈ carrier → -x ∈ carrier) attribute [to_additive] subgroup attribute [to_additive add_subgroup.to_add_submonoid] subgroup.to_submonoid /-- Reinterpret a `subgroup` as a `submonoid`. -/ add_decl_doc subgroup.to_submonoid /-- Reinterpret an `add_subgroup` as an `add_submonoid`. -/ add_decl_doc add_subgroup.to_add_submonoid namespace subgroup @[to_additive] instance : set_like (subgroup G) G := { coe := subgroup.carrier, coe_injective' := λ p q h, by cases p; cases q; congr' } @[to_additive] instance : subgroup_class (subgroup G) G := { mul_mem := subgroup.mul_mem', one_mem := subgroup.one_mem', inv_mem := subgroup.inv_mem' } @[simp, to_additive] lemma mem_carrier {s : subgroup G} {x : G} : x ∈ s.carrier ↔ x ∈ s := iff.rfl @[simp, to_additive] lemma mem_mk {s : set G} {x : G} (h_one) (h_mul) (h_inv) : x ∈ mk s h_one h_mul h_inv ↔ x ∈ s := iff.rfl @[simp, to_additive] lemma coe_set_mk {s : set G} (h_one) (h_mul) (h_inv) : (mk s h_one h_mul h_inv : set G) = s := rfl @[simp, to_additive] lemma mk_le_mk {s t : set G} (h_one) (h_mul) (h_inv) (h_one') (h_mul') (h_inv') : mk s h_one h_mul h_inv ≤ mk t h_one' h_mul' h_inv' ↔ s ⊆ t := iff.rfl /-- See Note [custom simps projection] -/ @[to_additive "See Note [custom simps projection]"] def simps.coe (S : subgroup G) : set G := S initialize_simps_projections subgroup (carrier → coe) initialize_simps_projections add_subgroup (carrier → coe) @[simp, to_additive] lemma coe_to_submonoid (K : subgroup G) : (K.to_submonoid : set G) = K := rfl @[simp, to_additive] lemma mem_to_submonoid (K : subgroup G) (x : G) : x ∈ K.to_submonoid ↔ x ∈ K := iff.rfl @[to_additive] instance (K : subgroup G) [d : decidable_pred (∈ K)] [fintype G] : fintype K := show fintype {g : G // g ∈ K}, from infer_instance @[to_additive] theorem to_submonoid_injective : function.injective (to_submonoid : subgroup G → submonoid G) := λ p q h, set_like.ext'_iff.2 (show _, from set_like.ext'_iff.1 h) @[simp, to_additive] theorem to_submonoid_eq {p q : subgroup G} : p.to_submonoid = q.to_submonoid ↔ p = q := to_submonoid_injective.eq_iff @[to_additive, mono] lemma to_submonoid_strict_mono : strict_mono (to_submonoid : subgroup G → submonoid G) := λ _ _, id attribute [mono] add_subgroup.to_add_submonoid_strict_mono @[to_additive, mono] lemma to_submonoid_mono : monotone (to_submonoid : subgroup G → submonoid G) := to_submonoid_strict_mono.monotone attribute [mono] add_subgroup.to_add_submonoid_mono @[simp, to_additive] lemma to_submonoid_le {p q : subgroup G} : p.to_submonoid ≤ q.to_submonoid ↔ p ≤ q := iff.rfl end subgroup /-! ### Conversion to/from `additive`/`multiplicative` -/ section mul_add /-- Supgroups of a group `G` are isomorphic to additive subgroups of `additive G`. -/ @[simps] def subgroup.to_add_subgroup : subgroup G ≃o add_subgroup (additive G) := { to_fun := λ S, { neg_mem' := S.inv_mem', ..S.to_submonoid.to_add_submonoid }, inv_fun := λ S, { inv_mem' := S.neg_mem', ..S.to_add_submonoid.to_submonoid' }, left_inv := λ x, by cases x; refl, right_inv := λ x, by cases x; refl, map_rel_iff' := λ a b, iff.rfl, } /-- Additive subgroup of an additive group `additive G` are isomorphic to subgroup of `G`. -/ abbreviation add_subgroup.to_subgroup' : add_subgroup (additive G) ≃o subgroup G := subgroup.to_add_subgroup.symm /-- Additive supgroups of an additive group `A` are isomorphic to subgroups of `multiplicative A`. -/ @[simps] def add_subgroup.to_subgroup : add_subgroup A ≃o subgroup (multiplicative A) := { to_fun := λ S, { inv_mem' := S.neg_mem', ..S.to_add_submonoid.to_submonoid }, inv_fun := λ S, { neg_mem' := S.inv_mem', ..S.to_submonoid.to_add_submonoid' }, left_inv := λ x, by cases x; refl, right_inv := λ x, by cases x; refl, map_rel_iff' := λ a b, iff.rfl, } /-- Subgroups of an additive group `multiplicative A` are isomorphic to additive subgroups of `A`. -/ abbreviation subgroup.to_add_subgroup' : subgroup (multiplicative A) ≃o add_subgroup A := add_subgroup.to_subgroup.symm end mul_add namespace subgroup variables (H K : subgroup G) /-- Copy of a subgroup with a new `carrier` equal to the old one. Useful to fix definitional equalities.-/ @[to_additive "Copy of an additive subgroup with a new `carrier` equal to the old one. Useful to fix definitional equalities"] protected def copy (K : subgroup G) (s : set G) (hs : s = K) : subgroup G := { carrier := s, one_mem' := hs.symm ▸ K.one_mem', mul_mem' := hs.symm ▸ K.mul_mem', inv_mem' := hs.symm ▸ K.inv_mem' } @[simp, to_additive] lemma coe_copy (K : subgroup G) (s : set G) (hs : s = ↑K) : (K.copy s hs : set G) = s := rfl @[to_additive] lemma copy_eq (K : subgroup G) (s : set G) (hs : s = ↑K) : K.copy s hs = K := set_like.coe_injective hs /-- Two subgroups are equal if they have the same elements. -/ @[ext, to_additive "Two `add_subgroup`s are equal if they have the same elements."] theorem ext {H K : subgroup G} (h : ∀ x, x ∈ H ↔ x ∈ K) : H = K := set_like.ext h /-- A subgroup contains the group's 1. -/ @[to_additive "An `add_subgroup` contains the group's 0."] protected theorem one_mem : (1 : G) ∈ H := one_mem _ /-- A subgroup is closed under multiplication. -/ @[to_additive "An `add_subgroup` is closed under addition."] protected theorem mul_mem {x y : G} : x ∈ H → y ∈ H → x * y ∈ H := mul_mem /-- A subgroup is closed under inverse. -/ @[to_additive "An `add_subgroup` is closed under inverse."] protected theorem inv_mem {x : G} : x ∈ H → x⁻¹ ∈ H := inv_mem /-- A subgroup is closed under division. -/ @[to_additive "An `add_subgroup` is closed under subtraction."] protected theorem div_mem {x y : G} (hx : x ∈ H) (hy : y ∈ H) : x / y ∈ H := div_mem hx hy @[to_additive] protected theorem inv_mem_iff {x : G} : x⁻¹ ∈ H ↔ x ∈ H := inv_mem_iff @[to_additive] protected lemma div_mem_comm_iff {a b : G} : a / b ∈ H ↔ b / a ∈ H := div_mem_comm_iff @[to_additive] protected theorem inv_coe_set : (H : set G)⁻¹ = H := by { ext, simp } @[to_additive] protected lemma exists_inv_mem_iff_exists_mem (K : subgroup G) {P : G → Prop} : (∃ (x : G), x ∈ K ∧ P x⁻¹) ↔ ∃ x ∈ K, P x := exists_inv_mem_iff_exists_mem @[to_additive] protected lemma mul_mem_cancel_right {x y : G} (h : x ∈ H) : y * x ∈ H ↔ y ∈ H := mul_mem_cancel_right h @[to_additive] protected lemma mul_mem_cancel_left {x y : G} (h : x ∈ H) : x * y ∈ H ↔ y ∈ H := mul_mem_cancel_left h /-- Product of a list of elements in a subgroup is in the subgroup. -/ @[to_additive "Sum of a list of elements in an `add_subgroup` is in the `add_subgroup`."] protected lemma list_prod_mem {l : list G} : (∀ x ∈ l, x ∈ K) → l.prod ∈ K := list_prod_mem /-- Product of a multiset of elements in a subgroup of a `comm_group` is in the subgroup. -/ @[to_additive "Sum of a multiset of elements in an `add_subgroup` of an `add_comm_group` is in the `add_subgroup`."] protected lemma multiset_prod_mem {G} [comm_group G] (K : subgroup G) (g : multiset G) : (∀ a ∈ g, a ∈ K) → g.prod ∈ K := multiset_prod_mem g @[to_additive] lemma multiset_noncomm_prod_mem (K : subgroup G) (g : multiset G) (comm : ∀ (x ∈ g) (y ∈ g), commute x y) : (∀ a ∈ g, a ∈ K) → g.noncomm_prod comm ∈ K := K.to_submonoid.multiset_noncomm_prod_mem g comm /-- Product of elements of a subgroup of a `comm_group` indexed by a `finset` is in the subgroup. -/ @[to_additive "Sum of elements in an `add_subgroup` of an `add_comm_group` indexed by a `finset` is in the `add_subgroup`."] protected lemma prod_mem {G : Type*} [comm_group G] (K : subgroup G) {ι : Type*} {t : finset ι} {f : ι → G} (h : ∀ c ∈ t, f c ∈ K) : ∏ c in t, f c ∈ K := prod_mem h @[to_additive] lemma noncomm_prod_mem (K : subgroup G) {ι : Type*} {t : finset ι} {f : ι → G} (comm : ∀ (x ∈ t) (y ∈ t), commute (f x) (f y)) : (∀ c ∈ t, f c ∈ K) → t.noncomm_prod f comm ∈ K := K.to_submonoid.noncomm_prod_mem t f comm @[to_additive add_subgroup.nsmul_mem] protected lemma pow_mem {x : G} (hx : x ∈ K) : ∀ n : ℕ, x ^ n ∈ K := pow_mem hx @[to_additive] protected lemma zpow_mem {x : G} (hx : x ∈ K) : ∀ n : ℤ, x ^ n ∈ K := zpow_mem hx /-- Construct a subgroup from a nonempty set that is closed under division. -/ @[to_additive "Construct a subgroup from a nonempty set that is closed under subtraction"] def of_div (s : set G) (hsn : s.nonempty) (hs : ∀ x y ∈ s, x * y⁻¹ ∈ s) : subgroup G := have one_mem : (1 : G) ∈ s, from let ⟨x, hx⟩ := hsn in by simpa using hs x hx x hx, have inv_mem : ∀ x, x ∈ s → x⁻¹ ∈ s, from λ x hx, by simpa using hs 1 one_mem x hx, { carrier := s, one_mem' := one_mem, inv_mem' := inv_mem, mul_mem' := λ x y hx hy, by simpa using hs x hx y⁻¹ (inv_mem y hy) } /-- A subgroup of a group inherits a multiplication. -/ @[to_additive "An `add_subgroup` of an `add_group` inherits an addition."] instance has_mul : has_mul H := H.to_submonoid.has_mul /-- A subgroup of a group inherits a 1. -/ @[to_additive "An `add_subgroup` of an `add_group` inherits a zero."] instance has_one : has_one H := H.to_submonoid.has_one /-- A subgroup of a group inherits an inverse. -/ @[to_additive "A `add_subgroup` of a `add_group` inherits an inverse."] instance has_inv : has_inv H := ⟨λ a, ⟨a⁻¹, H.inv_mem a.2⟩⟩ /-- A subgroup of a group inherits a division -/ @[to_additive "An `add_subgroup` of an `add_group` inherits a subtraction."] instance has_div : has_div H := ⟨λ a b, ⟨a / b, H.div_mem a.2 b.2⟩⟩ /-- An `add_subgroup` of an `add_group` inherits a natural scaling. -/ instance _root_.add_subgroup.has_nsmul {G} [add_group G] {H : add_subgroup G} : has_smul ℕ H := ⟨λ n a, ⟨n • a, H.nsmul_mem a.2 n⟩⟩ /-- A subgroup of a group inherits a natural power -/ @[to_additive] instance has_npow : has_pow H ℕ := ⟨λ a n, ⟨a ^ n, H.pow_mem a.2 n⟩⟩ /-- An `add_subgroup` of an `add_group` inherits an integer scaling. -/ instance _root_.add_subgroup.has_zsmul {G} [add_group G] {H : add_subgroup G} : has_smul ℤ H := ⟨λ n a, ⟨n • a, H.zsmul_mem a.2 n⟩⟩ /-- A subgroup of a group inherits an integer power -/ @[to_additive] instance has_zpow : has_pow H ℤ := ⟨λ a n, ⟨a ^ n, H.zpow_mem a.2 n⟩⟩ @[simp, norm_cast, to_additive] lemma coe_mul (x y : H) : (↑(x * y) : G) = ↑x * ↑y := rfl @[simp, norm_cast, to_additive] lemma coe_one : ((1 : H) : G) = 1 := rfl @[simp, norm_cast, to_additive] lemma coe_inv (x : H) : ↑(x⁻¹ : H) = (x⁻¹ : G) := rfl @[simp, norm_cast, to_additive] lemma coe_div (x y : H) : (↑(x / y) : G) = ↑x / ↑y := rfl @[simp, norm_cast, to_additive] lemma coe_mk (x : G) (hx : x ∈ H) : ((⟨x, hx⟩ : H) : G) = x := rfl @[simp, norm_cast, to_additive] lemma coe_pow (x : H) (n : ℕ) : ((x ^ n : H) : G) = x ^ n := rfl @[simp, norm_cast, to_additive] lemma coe_zpow (x : H) (n : ℤ) : ((x ^ n : H) : G) = x ^ n := rfl /-- A subgroup of a group inherits a group structure. -/ @[to_additive "An `add_subgroup` of an `add_group` inherits an `add_group` structure."] instance to_group {G : Type*} [group G] (H : subgroup G) : group H := subtype.coe_injective.group _ rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) /-- A subgroup of a `comm_group` is a `comm_group`. -/ @[to_additive "An `add_subgroup` of an `add_comm_group` is an `add_comm_group`."] instance to_comm_group {G : Type*} [comm_group G] (H : subgroup G) : comm_group H := subtype.coe_injective.comm_group _ rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) /-- A subgroup of an `ordered_comm_group` is an `ordered_comm_group`. -/ @[to_additive "An `add_subgroup` of an `add_ordered_comm_group` is an `add_ordered_comm_group`."] instance to_ordered_comm_group {G : Type*} [ordered_comm_group G] (H : subgroup G) : ordered_comm_group H := subtype.coe_injective.ordered_comm_group _ rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) /-- A subgroup of a `linear_ordered_comm_group` is a `linear_ordered_comm_group`. -/ @[to_additive "An `add_subgroup` of a `linear_ordered_add_comm_group` is a `linear_ordered_add_comm_group`."] instance to_linear_ordered_comm_group {G : Type*} [linear_ordered_comm_group G] (H : subgroup G) : linear_ordered_comm_group H := subtype.coe_injective.linear_ordered_comm_group _ rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) /-- The natural group hom from a subgroup of group `G` to `G`. -/ @[to_additive "The natural group hom from an `add_subgroup` of `add_group` `G` to `G`."] def subtype : H →* G := ⟨coe, rfl, λ _ _, rfl⟩ @[simp, to_additive] theorem coe_subtype : ⇑H.subtype = coe := rfl @[simp, norm_cast, to_additive] theorem coe_list_prod (l : list H) : (l.prod : G) = (l.map coe).prod := submonoid_class.coe_list_prod l @[simp, norm_cast, to_additive] theorem coe_multiset_prod {G} [comm_group G] (H : subgroup G) (m : multiset H) : (m.prod : G) = (m.map coe).prod := submonoid_class.coe_multiset_prod m @[simp, norm_cast, to_additive] theorem coe_finset_prod {ι G} [comm_group G] (H : subgroup G) (f : ι → H) (s : finset ι) : ↑(∏ i in s, f i) = (∏ i in s, f i : G) := submonoid_class.coe_finset_prod f s /-- The inclusion homomorphism from a subgroup `H` contained in `K` to `K`. -/ @[to_additive "The inclusion homomorphism from a additive subgroup `H` contained in `K` to `K`."] def inclusion {H K : subgroup G} (h : H ≤ K) : H →* K := monoid_hom.mk' (λ x, ⟨x, h x.prop⟩) (λ ⟨a, ha⟩ ⟨b, hb⟩, rfl) @[simp, to_additive] lemma coe_inclusion {H K : subgroup G} {h : H ≤ K} (a : H) : (inclusion h a : G) = a := by { cases a, simp only [inclusion, coe_mk, monoid_hom.mk'_apply] } @[to_additive] lemma inclusion_injective {H K : subgroup G} (h : H ≤ K) : function.injective $ inclusion h := set.inclusion_injective h @[simp, to_additive] lemma subtype_comp_inclusion {H K : subgroup G} (hH : H ≤ K) : K.subtype.comp (inclusion hH) = H.subtype := by { ext, simp } /-- The subgroup `G` of the group `G`. -/ @[to_additive "The `add_subgroup G` of the `add_group G`."] instance : has_top (subgroup G) := ⟨{ inv_mem' := λ _ _, set.mem_univ _ , .. (⊤ : submonoid G) }⟩ /-- The top subgroup is isomorphic to the group. This is the group version of `submonoid.top_equiv`. -/ @[to_additive "The top additive subgroup is isomorphic to the additive group. This is the additive group version of `add_submonoid.top_equiv`.", simps] def top_equiv : (⊤ : subgroup G) ≃* G := submonoid.top_equiv /-- The trivial subgroup `{1}` of an group `G`. -/ @[to_additive "The trivial `add_subgroup` `{0}` of an `add_group` `G`."] instance : has_bot (subgroup G) := ⟨{ inv_mem' := λ _, by simp *, .. (⊥ : submonoid G) }⟩ @[to_additive] instance : inhabited (subgroup G) := ⟨⊥⟩ @[simp, to_additive] lemma mem_bot {x : G} : x ∈ (⊥ : subgroup G) ↔ x = 1 := iff.rfl @[simp, to_additive] lemma mem_top (x : G) : x ∈ (⊤ : subgroup G) := set.mem_univ x @[simp, to_additive] lemma coe_top : ((⊤ : subgroup G) : set G) = set.univ := rfl @[simp, to_additive] lemma coe_bot : ((⊥ : subgroup G) : set G) = {1} := rfl @[to_additive] instance : unique (⊥ : subgroup G) := ⟨⟨1⟩, λ g, subtype.ext g.2⟩ @[to_additive] lemma eq_bot_iff_forall : H = ⊥ ↔ ∀ x ∈ H, x = (1 : G) := begin rw set_like.ext'_iff, simp only [coe_bot, set.eq_singleton_iff_unique_mem, set_like.mem_coe, H.one_mem, true_and], end @[to_additive] lemma eq_bot_of_subsingleton [subsingleton H] : H = ⊥ := begin rw subgroup.eq_bot_iff_forall, intros y hy, rw [← subgroup.coe_mk H y hy, subsingleton.elim (⟨y, hy⟩ : H) 1, subgroup.coe_one], end @[to_additive] lemma coe_eq_univ {H : subgroup G} : (H : set G) = set.univ ↔ H = ⊤ := (set_like.ext'_iff.trans (by refl)).symm @[to_additive] lemma coe_eq_singleton {H : subgroup G} : (∃ g : G, (H : set G) = {g}) ↔ H = ⊥ := ⟨λ ⟨g, hg⟩, by { haveI : subsingleton (H : set G) := by { rw hg, apply_instance }, exact H.eq_bot_of_subsingleton }, λ h, ⟨1, set_like.ext'_iff.mp h⟩⟩ @[to_additive] instance fintype_bot : fintype (⊥ : subgroup G) := ⟨{1}, by {rintro ⟨x, ⟨hx⟩⟩, exact finset.mem_singleton_self _}⟩ /- curly brackets `{}` are used here instead of instance brackets `[]` because the instance in a goal is often not the same as the one inferred by type class inference. -/ @[simp, to_additive] lemma card_bot {_ : fintype ↥(⊥ : subgroup G)} : fintype.card (⊥ : subgroup G) = 1 := fintype.card_eq_one_iff.2 ⟨⟨(1 : G), set.mem_singleton 1⟩, λ ⟨y, hy⟩, subtype.eq $ subgroup.mem_bot.1 hy⟩ @[to_additive] lemma eq_top_of_card_eq [fintype H] [fintype G] (h : fintype.card H = fintype.card G) : H = ⊤ := begin haveI : fintype (H : set G) := ‹fintype H›, rw [set_like.ext'_iff, coe_top, ← finset.coe_univ, ← (H : set G).coe_to_finset, finset.coe_inj, ← finset.card_eq_iff_eq_univ, ← h, set.to_finset_card], congr end @[to_additive] lemma eq_top_of_le_card [fintype H] [fintype G] (h : fintype.card G ≤ fintype.card H) : H = ⊤ := eq_top_of_card_eq H (le_antisymm (fintype.card_le_of_injective coe subtype.coe_injective) h) @[to_additive] lemma eq_bot_of_card_le [fintype H] (h : fintype.card H ≤ 1) : H = ⊥ := let _ := fintype.card_le_one_iff_subsingleton.mp h in by exactI eq_bot_of_subsingleton H @[to_additive] lemma eq_bot_of_card_eq [fintype H] (h : fintype.card H = 1) : H = ⊥ := H.eq_bot_of_card_le (le_of_eq h) @[to_additive] lemma nontrivial_iff_exists_ne_one (H : subgroup G) : nontrivial H ↔ ∃ x ∈ H, x ≠ (1:G) := subtype.nontrivial_iff_exists_ne (λ x, x ∈ H) (1 : H) /-- A subgroup is either the trivial subgroup or nontrivial. -/ @[to_additive "A subgroup is either the trivial subgroup or nontrivial."] lemma bot_or_nontrivial (H : subgroup G) : H = ⊥ ∨ nontrivial H := begin classical, by_cases h : ∀ x ∈ H, x = (1 : G), { left, exact H.eq_bot_iff_forall.mpr h }, { right, simp only [not_forall] at h, simpa only [nontrivial_iff_exists_ne_one] } end /-- A subgroup is either the trivial subgroup or contains a non-identity element. -/ @[to_additive "A subgroup is either the trivial subgroup or contains a nonzero element."] lemma bot_or_exists_ne_one (H : subgroup G) : H = ⊥ ∨ ∃ x ∈ H, x ≠ (1:G) := begin convert H.bot_or_nontrivial, rw nontrivial_iff_exists_ne_one end @[to_additive] lemma card_le_one_iff_eq_bot [fintype H] : fintype.card H ≤ 1 ↔ H = ⊥ := ⟨λ h, (eq_bot_iff_forall _).2 (λ x hx, by simpa [subtype.ext_iff] using fintype.card_le_one_iff.1 h ⟨x, hx⟩ 1), λ h, by simp [h]⟩ @[to_additive] lemma one_lt_card_iff_ne_bot [fintype H] : 1 < fintype.card H ↔ H ≠ ⊥ := lt_iff_not_le.trans H.card_le_one_iff_eq_bot.not /-- The inf of two subgroups is their intersection. -/ @[to_additive "The inf of two `add_subgroups`s is their intersection."] instance : has_inf (subgroup G) := ⟨λ H₁ H₂, { inv_mem' := λ _ ⟨hx, hx'⟩, ⟨H₁.inv_mem hx, H₂.inv_mem hx'⟩, .. H₁.to_submonoid ⊓ H₂.to_submonoid }⟩ @[simp, to_additive] lemma coe_inf (p p' : subgroup G) : ((p ⊓ p' : subgroup G) : set G) = p ∩ p' := rfl @[simp, to_additive] lemma mem_inf {p p' : subgroup G} {x : G} : x ∈ p ⊓ p' ↔ x ∈ p ∧ x ∈ p' := iff.rfl @[to_additive] instance : has_Inf (subgroup G) := ⟨λ s, { inv_mem' := λ x hx, set.mem_bInter $ λ i h, i.inv_mem (by apply set.mem_Inter₂.1 hx i h), .. (⨅ S ∈ s, subgroup.to_submonoid S).copy (⋂ S ∈ s, ↑S) (by simp) }⟩ @[simp, norm_cast, to_additive] lemma coe_Inf (H : set (subgroup G)) : ((Inf H : subgroup G) : set G) = ⋂ s ∈ H, ↑s := rfl @[simp, to_additive] lemma mem_Inf {S : set (subgroup G)} {x : G} : x ∈ Inf S ↔ ∀ p ∈ S, x ∈ p := set.mem_Inter₂ @[to_additive] lemma mem_infi {ι : Sort*} {S : ι → subgroup G} {x : G} : (x ∈ ⨅ i, S i) ↔ ∀ i, x ∈ S i := by simp only [infi, mem_Inf, set.forall_range_iff] @[simp, norm_cast, to_additive] lemma coe_infi {ι : Sort*} {S : ι → subgroup G} : (↑(⨅ i, S i) : set G) = ⋂ i, S i := by simp only [infi, coe_Inf, set.bInter_range] /-- Subgroups of a group form a complete lattice. -/ @[to_additive "The `add_subgroup`s of an `add_group` form a complete lattice."] instance : complete_lattice (subgroup G) := { bot := (⊥), bot_le := λ S x hx, (mem_bot.1 hx).symm ▸ S.one_mem, top := (⊤), le_top := λ S x hx, mem_top x, inf := (⊓), le_inf := λ a b c ha hb x hx, ⟨ha hx, hb hx⟩, inf_le_left := λ a b x, and.left, inf_le_right := λ a b x, and.right, .. complete_lattice_of_Inf (subgroup G) $ λ s, is_glb.of_image (λ H K, show (H : set G) ≤ K ↔ H ≤ K, from set_like.coe_subset_coe) is_glb_binfi } @[to_additive] lemma mem_sup_left {S T : subgroup G} : ∀ {x : G}, x ∈ S → x ∈ S ⊔ T := show S ≤ S ⊔ T, from le_sup_left @[to_additive] lemma mem_sup_right {S T : subgroup G} : ∀ {x : G}, x ∈ T → x ∈ S ⊔ T := show T ≤ S ⊔ T, from le_sup_right @[to_additive] lemma mul_mem_sup {S T : subgroup G} {x y : G} (hx : x ∈ S) (hy : y ∈ T) : x * y ∈ S ⊔ T := (S ⊔ T).mul_mem (mem_sup_left hx) (mem_sup_right hy) @[to_additive] lemma mem_supr_of_mem {ι : Sort*} {S : ι → subgroup G} (i : ι) : ∀ {x : G}, x ∈ S i → x ∈ supr S := show S i ≤ supr S, from le_supr _ _ @[to_additive] lemma mem_Sup_of_mem {S : set (subgroup G)} {s : subgroup G} (hs : s ∈ S) : ∀ {x : G}, x ∈ s → x ∈ Sup S := show s ≤ Sup S, from le_Sup hs @[simp, to_additive] lemma subsingleton_iff : subsingleton (subgroup G) ↔ subsingleton G := ⟨ λ h, by exactI ⟨λ x y, have ∀ i : G, i = 1 := λ i, mem_bot.mp $ subsingleton.elim (⊤ : subgroup G) ⊥ ▸ mem_top i, (this x).trans (this y).symm⟩, λ h, by exactI ⟨λ x y, subgroup.ext $ λ i, subsingleton.elim 1 i ▸ by simp [subgroup.one_mem]⟩⟩ @[simp, to_additive] lemma nontrivial_iff : nontrivial (subgroup G) ↔ nontrivial G := not_iff_not.mp ( (not_nontrivial_iff_subsingleton.trans subsingleton_iff).trans not_nontrivial_iff_subsingleton.symm) @[to_additive] instance [subsingleton G] : unique (subgroup G) := ⟨⟨⊥⟩, λ a, @subsingleton.elim _ (subsingleton_iff.mpr ‹_›) a _⟩ @[to_additive] instance [nontrivial G] : nontrivial (subgroup G) := nontrivial_iff.mpr ‹_› @[to_additive] lemma eq_top_iff' : H = ⊤ ↔ ∀ x : G, x ∈ H := eq_top_iff.trans ⟨λ h m, h $ mem_top m, λ h m _, h m⟩ /-- The `subgroup` generated by a set. -/ @[to_additive "The `add_subgroup` generated by a set"] def closure (k : set G) : subgroup G := Inf {K | k ⊆ K} variable {k : set G} @[to_additive] lemma mem_closure {x : G} : x ∈ closure k ↔ ∀ K : subgroup G, k ⊆ K → x ∈ K := mem_Inf /-- The subgroup generated by a set includes the set. -/ @[simp, to_additive "The `add_subgroup` generated by a set includes the set."] lemma subset_closure : k ⊆ closure k := λ x hx, mem_closure.2 $ λ K hK, hK hx @[to_additive] lemma not_mem_of_not_mem_closure {P : G} (hP : P ∉ closure k) : P ∉ k := λ h, hP (subset_closure h) open set /-- A subgroup `K` includes `closure k` if and only if it includes `k`. -/ @[simp, to_additive "An additive subgroup `K` includes `closure k` if and only if it includes `k`"] lemma closure_le : closure k ≤ K ↔ k ⊆ K := ⟨subset.trans subset_closure, λ h, Inf_le h⟩ @[to_additive] lemma closure_eq_of_le (h₁ : k ⊆ K) (h₂ : K ≤ closure k) : closure k = K := le_antisymm ((closure_le $ K).2 h₁) h₂ /-- An induction principle for closure membership. If `p` holds for `1` and all elements of `k`, and is preserved under multiplication and inverse, then `p` holds for all elements of the closure of `k`. -/ @[elab_as_eliminator, to_additive "An induction principle for additive closure membership. If `p` holds for `0` and all elements of `k`, and is preserved under addition and inverses, then `p` holds for all elements of the additive closure of `k`."] lemma closure_induction {p : G → Prop} {x} (h : x ∈ closure k) (Hk : ∀ x ∈ k, p x) (H1 : p 1) (Hmul : ∀ x y, p x → p y → p (x * y)) (Hinv : ∀ x, p x → p x⁻¹) : p x := (@closure_le _ _ ⟨p, Hmul, H1, Hinv⟩ _).2 Hk h /-- A dependent version of `subgroup.closure_induction`. -/ @[elab_as_eliminator, to_additive "A dependent version of `add_subgroup.closure_induction`. "] lemma closure_induction' {p : Π x, x ∈ closure k → Prop} (Hs : ∀ x (h : x ∈ k), p x (subset_closure h)) (H1 : p 1 (one_mem _)) (Hmul : ∀ x hx y hy, p x hx → p y hy → p (x * y) (mul_mem hx hy)) (Hinv : ∀ x hx, p x hx → p x⁻¹ (inv_mem hx)) {x} (hx : x ∈ closure k) : p x hx := begin refine exists.elim _ (λ (hx : x ∈ closure k) (hc : p x hx), hc), exact closure_induction hx (λ x hx, ⟨_, Hs x hx⟩) ⟨_, H1⟩ (λ x y ⟨hx', hx⟩ ⟨hy', hy⟩, ⟨_, Hmul _ _ _ _ hx hy⟩) (λ x ⟨hx', hx⟩, ⟨_, Hinv _ _ hx⟩), end /-- An induction principle for closure membership for predicates with two arguments. -/ @[elab_as_eliminator, to_additive "An induction principle for additive closure membership, for predicates with two arguments."] lemma closure_induction₂ {p : G → G → Prop} {x} {y : G} (hx : x ∈ closure k) (hy : y ∈ closure k) (Hk : ∀ (x ∈ k) (y ∈ k), p x y) (H1_left : ∀ x, p 1 x) (H1_right : ∀ x, p x 1) (Hmul_left : ∀ x₁ x₂ y, p x₁ y → p x₂ y → p (x₁ * x₂) y) (Hmul_right : ∀ x y₁ y₂, p x y₁ → p x y₂ → p x (y₁ * y₂)) (Hinv_left : ∀ x y, p x y → p x⁻¹ y) (Hinv_right : ∀ x y, p x y → p x y⁻¹) : p x y := closure_induction hx (λ x xk, closure_induction hy (Hk x xk) (H1_right x) (Hmul_right x) (Hinv_right x)) (H1_left y) (λ z z', Hmul_left z z' y) (λ z, Hinv_left z y) @[simp, to_additive] lemma closure_closure_coe_preimage {k : set G} : closure ((coe : closure k → G) ⁻¹' k) = ⊤ := eq_top_iff.2 $ λ x, subtype.rec_on x $ λ x hx _, begin refine closure_induction' (λ g hg, _) _ (λ g₁ g₂ hg₁ hg₂, _) (λ g hg, _) hx, { exact subset_closure hg }, { exact one_mem _ }, { exact mul_mem }, { exact inv_mem } end /-- If all the elements of a set `s` commute, then `closure s` is a commutative group. -/ @[to_additive "If all the elements of a set `s` commute, then `closure s` is an additive commutative group."] def closure_comm_group_of_comm {k : set G} (hcomm : ∀ (x ∈ k) (y ∈ k), x * y = y * x) : comm_group (closure k) := { mul_comm := λ x y, begin ext, simp only [subgroup.coe_mul], refine closure_induction₂ x.prop y.prop hcomm (λ x, by simp only [mul_one, one_mul]) (λ x, by simp only [mul_one, one_mul]) (λ x y z h₁ h₂, by rw [mul_assoc, h₂, ←mul_assoc, h₁, mul_assoc]) (λ x y z h₁ h₂, by rw [←mul_assoc, h₁, mul_assoc, h₂, ←mul_assoc]) (λ x y h, by rw [inv_mul_eq_iff_eq_mul, ←mul_assoc, h, mul_assoc, mul_inv_self, mul_one]) (λ x y h, by rw [mul_inv_eq_iff_eq_mul, mul_assoc, h, ←mul_assoc, inv_mul_self, one_mul]) end, ..(closure k).to_group } variable (G) /-- `closure` forms a Galois insertion with the coercion to set. -/ @[to_additive "`closure` forms a Galois insertion with the coercion to set."] protected def gi : galois_insertion (@closure G _) coe := { choice := λ s _, closure s, gc := λ s t, @closure_le _ _ t s, le_l_u := λ s, subset_closure, choice_eq := λ s h, rfl } variable {G} /-- Subgroup closure of a set is monotone in its argument: if `h ⊆ k`, then `closure h ≤ closure k`. -/ @[to_additive "Additive subgroup closure of a set is monotone in its argument: if `h ⊆ k`, then `closure h ≤ closure k`"] lemma closure_mono ⦃h k : set G⦄ (h' : h ⊆ k) : closure h ≤ closure k := (subgroup.gi G).gc.monotone_l h' /-- Closure of a subgroup `K` equals `K`. -/ @[simp, to_additive "Additive closure of an additive subgroup `K` equals `K`"] lemma closure_eq : closure (K : set G) = K := (subgroup.gi G).l_u_eq K @[simp, to_additive] lemma closure_empty : closure (∅ : set G) = ⊥ := (subgroup.gi G).gc.l_bot @[simp, to_additive] lemma closure_univ : closure (univ : set G) = ⊤ := @coe_top G _ ▸ closure_eq ⊤ @[to_additive] lemma closure_union (s t : set G) : closure (s ∪ t) = closure s ⊔ closure t := (subgroup.gi G).gc.l_sup @[to_additive] lemma closure_Union {ι} (s : ι → set G) : closure (⋃ i, s i) = ⨆ i, closure (s i) := (subgroup.gi G).gc.l_supr @[to_additive] lemma closure_eq_bot_iff (G : Type*) [group G] (S : set G) : closure S = ⊥ ↔ S ⊆ {1} := by { rw [← le_bot_iff], exact closure_le _} @[to_additive] lemma supr_eq_closure {ι : Sort*} (p : ι → subgroup G) : (⨆ i, p i) = closure (⋃ i, (p i : set G)) := by simp_rw [closure_Union, closure_eq] /-- The subgroup generated by an element of a group equals the set of integer number powers of the element. -/ @[to_additive /-"The `add_subgroup` generated by an element of an `add_group` equals the set of natural number multiples of the element."-/] lemma mem_closure_singleton {x y : G} : y ∈ closure ({x} : set G) ↔ ∃ n : ℤ, x ^ n = y := begin refine ⟨λ hy, closure_induction hy _ _ _ _, λ ⟨n, hn⟩, hn ▸ zpow_mem (subset_closure $ mem_singleton x) n⟩, { intros y hy, rw [eq_of_mem_singleton hy], exact ⟨1, zpow_one x⟩ }, { exact ⟨0, zpow_zero x⟩ }, { rintros _ _ ⟨n, rfl⟩ ⟨m, rfl⟩, exact ⟨n + m, zpow_add x n m⟩ }, rintros _ ⟨n, rfl⟩, exact ⟨-n, zpow_neg x n⟩ end @[to_additive] lemma closure_singleton_one : closure ({1} : set G) = ⊥ := by simp [eq_bot_iff_forall, mem_closure_singleton] @[simp, to_additive] lemma inv_subset_closure (S : set G) : S⁻¹ ⊆ closure S := begin intros s hs, rw [set_like.mem_coe, ←subgroup.inv_mem_iff], exact subset_closure (mem_inv.mp hs), end @[simp, to_additive] lemma closure_inv (S : set G) : closure S⁻¹ = closure S := begin refine le_antisymm ((subgroup.closure_le _).2 _) ((subgroup.closure_le _).2 _), { exact inv_subset_closure S }, { simpa only [inv_inv] using inv_subset_closure S⁻¹ }, end @[to_additive] lemma closure_to_submonoid (S : set G) : (closure S).to_submonoid = submonoid.closure (S ∪ S⁻¹) := begin refine le_antisymm _ (submonoid.closure_le.2 _), { intros x hx, refine closure_induction hx (λ x hx, submonoid.closure_mono (subset_union_left S S⁻¹) (submonoid.subset_closure hx)) (submonoid.one_mem _) (λ x y hx hy, submonoid.mul_mem _ hx hy) (λ x hx, _), rwa [←submonoid.mem_closure_inv, set.union_inv, inv_inv, set.union_comm] }, { simp only [true_and, coe_to_submonoid, union_subset_iff, subset_closure, inv_subset_closure] } end @[to_additive] lemma closure_induction_left {p : G → Prop} {x : G} (h : x ∈ closure k) (H1 : p 1) (Hmul : ∀ (x ∈ k) y, p y → p (x * y)) (Hinv : ∀ (x ∈ k) y, p y → p (x⁻¹ * y)) : p x := let key := le_of_eq (closure_to_submonoid k) in submonoid.closure_induction_left (key h) H1 (λ x hx, hx.elim (Hmul x) (λ hx y hy, (congr_arg _ (inv_inv x)).mp (Hinv x⁻¹ hx y hy))) @[to_additive] lemma closure_induction_right {p : G → Prop} {x : G} (h : x ∈ closure k) (H1 : p 1) (Hmul : ∀ x (y ∈ k), p x → p (x * y)) (Hinv : ∀ x (y ∈ k), p x → p (x * y⁻¹)) : p x := let key := le_of_eq (closure_to_submonoid k) in submonoid.closure_induction_right (key h) H1 (λ x y hy, hy.elim (Hmul x y) (λ hy hx, (congr_arg _ (inv_inv y)).mp (Hinv x y⁻¹ hy hx))) /-- An induction principle for closure membership. If `p` holds for `1` and all elements of `k` and their inverse, and is preserved under multiplication, then `p` holds for all elements of the closure of `k`. -/ @[to_additive "An induction principle for additive closure membership. If `p` holds for `0` and all elements of `k` and their negation, and is preserved under addition, then `p` holds for all elements of the additive closure of `k`."] lemma closure_induction'' {p : G → Prop} {x} (h : x ∈ closure k) (Hk : ∀ x ∈ k, p x) (Hk_inv : ∀ x ∈ k, p x⁻¹) (H1 : p 1) (Hmul : ∀ x y, p x → p y → p (x * y)) : p x := closure_induction_left h H1 (λ x hx y hy, Hmul x y (Hk x hx) hy) (λ x hx y hy, Hmul x⁻¹ y (Hk_inv x hx) hy) /-- An induction principle for elements of `⨆ i, S i`. If `C` holds for `1` and all elements of `S i` for all `i`, and is preserved under multiplication, then it holds for all elements of the supremum of `S`. -/ @[elab_as_eliminator, to_additive /-" An induction principle for elements of `⨆ i, S i`. If `C` holds for `0` and all elements of `S i` for all `i`, and is preserved under addition, then it holds for all elements of the supremum of `S`. "-/] lemma supr_induction {ι : Sort*} (S : ι → subgroup G) {C : G → Prop} {x : G} (hx : x ∈ ⨆ i, S i) (hp : ∀ i (x ∈ S i), C x) (h1 : C 1) (hmul : ∀ x y, C x → C y → C (x * y)) : C x := begin rw supr_eq_closure at hx, refine closure_induction'' hx (λ x hx, _) (λ x hx, _) h1 hmul, { obtain ⟨i, hi⟩ := set.mem_Union.mp hx, exact hp _ _ hi, }, { obtain ⟨i, hi⟩ := set.mem_Union.mp hx, exact hp _ _ (inv_mem hi), }, end /-- A dependent version of `subgroup.supr_induction`. -/ @[elab_as_eliminator, to_additive /-"A dependent version of `add_subgroup.supr_induction`. "-/] lemma supr_induction' {ι : Sort*} (S : ι → subgroup G) {C : Π x, (x ∈ ⨆ i, S i) → Prop} (hp : ∀ i (x ∈ S i), C x (mem_supr_of_mem i ‹_›)) (h1 : C 1 (one_mem _)) (hmul : ∀ x y hx hy, C x hx → C y hy → C (x * y) (mul_mem ‹_› ‹_›)) {x : G} (hx : x ∈ ⨆ i, S i) : C x hx := begin refine exists.elim _ (λ (hx : x ∈ ⨆ i, S i) (hc : C x hx), hc), refine supr_induction S hx (λ i x hx, _) _ (λ x y, _), { exact ⟨_, hp _ _ hx⟩ }, { exact ⟨_, h1⟩ }, { rintro ⟨_, Cx⟩ ⟨_, Cy⟩, refine ⟨_, hmul _ _ _ _ Cx Cy⟩ }, end @[to_additive] lemma mem_supr_of_directed {ι} [hι : nonempty ι] {K : ι → subgroup G} (hK : directed (≤) K) {x : G} : x ∈ (supr K : subgroup G) ↔ ∃ i, x ∈ K i := begin refine ⟨_, λ ⟨i, hi⟩, (set_like.le_def.1 $ le_supr K i) hi⟩, suffices : x ∈ closure (⋃ i, (K i : set G)) → ∃ i, x ∈ K i, by simpa only [closure_Union, closure_eq (K _)] using this, refine (λ hx, closure_induction hx (λ _, mem_Union.1) _ _ _), { exact hι.elim (λ i, ⟨i, (K i).one_mem⟩) }, { rintros x y ⟨i, hi⟩ ⟨j, hj⟩, rcases hK i j with ⟨k, hki, hkj⟩, exact ⟨k, mul_mem (hki hi) (hkj hj)⟩ }, rintros _ ⟨i, hi⟩, exact ⟨i, inv_mem hi⟩ end @[to_additive] lemma coe_supr_of_directed {ι} [nonempty ι] {S : ι → subgroup G} (hS : directed (≤) S) : ((⨆ i, S i : subgroup G) : set G) = ⋃ i, ↑(S i) := set.ext $ λ x, by simp [mem_supr_of_directed hS] @[to_additive] lemma mem_Sup_of_directed_on {K : set (subgroup G)} (Kne : K.nonempty) (hK : directed_on (≤) K) {x : G} : x ∈ Sup K ↔ ∃ s ∈ K, x ∈ s := begin haveI : nonempty K := Kne.to_subtype, simp only [Sup_eq_supr', mem_supr_of_directed hK.directed_coe, set_coe.exists, subtype.coe_mk] end variables {N : Type*} [group N] {P : Type*} [group P] /-- The preimage of a subgroup along a monoid homomorphism is a subgroup. -/ @[to_additive "The preimage of an `add_subgroup` along an `add_monoid` homomorphism is an `add_subgroup`."] def comap {N : Type*} [group N] (f : G →* N) (H : subgroup N) : subgroup G := { carrier := (f ⁻¹' H), inv_mem' := λ a ha, show f a⁻¹ ∈ H, by rw f.map_inv; exact H.inv_mem ha, .. H.to_submonoid.comap f } @[simp, to_additive] lemma coe_comap (K : subgroup N) (f : G →* N) : (K.comap f : set G) = f ⁻¹' K := rfl @[simp, to_additive] lemma mem_comap {K : subgroup N} {f : G →* N} {x : G} : x ∈ K.comap f ↔ f x ∈ K := iff.rfl @[to_additive] lemma comap_mono {f : G →* N} {K K' : subgroup N} : K ≤ K' → comap f K ≤ comap f K' := preimage_mono @[to_additive] lemma comap_comap (K : subgroup P) (g : N →* P) (f : G →* N) : (K.comap g).comap f = K.comap (g.comp f) := rfl /-- The image of a subgroup along a monoid homomorphism is a subgroup. -/ @[to_additive "The image of an `add_subgroup` along an `add_monoid` homomorphism is an `add_subgroup`."] def map (f : G →* N) (H : subgroup G) : subgroup N := { carrier := (f '' H), inv_mem' := by { rintros _ ⟨x, hx, rfl⟩, exact ⟨x⁻¹, H.inv_mem hx, f.map_inv x⟩ }, .. H.to_submonoid.map f } @[simp, to_additive] lemma coe_map (f : G →* N) (K : subgroup G) : (K.map f : set N) = f '' K := rfl @[simp, to_additive] lemma mem_map {f : G →* N} {K : subgroup G} {y : N} : y ∈ K.map f ↔ ∃ x ∈ K, f x = y := mem_image_iff_bex @[to_additive] lemma mem_map_of_mem (f : G →* N) {K : subgroup G} {x : G} (hx : x ∈ K) : f x ∈ K.map f := mem_image_of_mem f hx @[to_additive] lemma apply_coe_mem_map (f : G →* N) (K : subgroup G) (x : K) : f x ∈ K.map f := mem_map_of_mem f x.prop @[to_additive] lemma map_mono {f : G →* N} {K K' : subgroup G} : K ≤ K' → map f K ≤ map f K' := image_subset _ @[simp, to_additive] lemma map_id : K.map (monoid_hom.id G) = K := set_like.coe_injective $ image_id _ @[to_additive] lemma map_map (g : N →* P) (f : G →* N) : (K.map f).map g = K.map (g.comp f) := set_like.coe_injective $ image_image _ _ _ @[simp, to_additive] lemma map_one_eq_bot : K.map (1 : G →* N) = ⊥ := eq_bot_iff.mpr $ by { rintros x ⟨y, _ , rfl⟩, simp } @[to_additive] lemma mem_map_equiv {f : G ≃* N} {K : subgroup G} {x : N} : x ∈ K.map f.to_monoid_hom ↔ f.symm x ∈ K := @set.mem_image_equiv _ _ ↑K f.to_equiv x @[to_additive] lemma mem_map_iff_mem {f : G →* N} (hf : function.injective f) {K : subgroup G} {x : G} : f x ∈ K.map f ↔ x ∈ K := hf.mem_set_image @[to_additive] lemma map_equiv_eq_comap_symm (f : G ≃* N) (K : subgroup G) : K.map f.to_monoid_hom = K.comap f.symm.to_monoid_hom := set_like.coe_injective (f.to_equiv.image_eq_preimage K) @[to_additive] lemma comap_equiv_eq_map_symm (f : N ≃* G) (K : subgroup G) : K.comap f.to_monoid_hom = K.map f.symm.to_monoid_hom := (map_equiv_eq_comap_symm f.symm K).symm @[to_additive] lemma map_le_iff_le_comap {f : G →* N} {K : subgroup G} {H : subgroup N} : K.map f ≤ H ↔ K ≤ H.comap f := image_subset_iff @[to_additive] lemma gc_map_comap (f : G →* N) : galois_connection (map f) (comap f) := λ _ _, map_le_iff_le_comap @[to_additive] lemma map_sup (H K : subgroup G) (f : G →* N) : (H ⊔ K).map f = H.map f ⊔ K.map f := (gc_map_comap f).l_sup @[to_additive] lemma map_supr {ι : Sort*} (f : G →* N) (s : ι → subgroup G) : (supr s).map f = ⨆ i, (s i).map f := (gc_map_comap f).l_supr @[to_additive] lemma comap_sup_comap_le (H K : subgroup N) (f : G →* N) : comap f H ⊔ comap f K ≤ comap f (H ⊔ K) := monotone.le_map_sup (λ _ _, comap_mono) H K @[to_additive] lemma supr_comap_le {ι : Sort*} (f : G →* N) (s : ι → subgroup N) : (⨆ i, (s i).comap f) ≤ (supr s).comap f := monotone.le_map_supr (λ _ _, comap_mono) @[to_additive] lemma comap_inf (H K : subgroup N) (f : G →* N) : (H ⊓ K).comap f = H.comap f ⊓ K.comap f := (gc_map_comap f).u_inf @[to_additive] lemma comap_infi {ι : Sort*} (f : G →* N) (s : ι → subgroup N) : (infi s).comap f = ⨅ i, (s i).comap f := (gc_map_comap f).u_infi @[to_additive] lemma map_inf_le (H K : subgroup G) (f : G →* N) : map f (H ⊓ K) ≤ map f H ⊓ map f K := le_inf (map_mono inf_le_left) (map_mono inf_le_right) @[to_additive] lemma map_inf_eq (H K : subgroup G) (f : G →* N) (hf : function.injective f) : map f (H ⊓ K) = map f H ⊓ map f K := begin rw ← set_like.coe_set_eq, simp [set.image_inter hf], end @[simp, to_additive] lemma map_bot (f : G →* N) : (⊥ : subgroup G).map f = ⊥ := (gc_map_comap f).l_bot @[simp, to_additive] lemma map_top_of_surjective (f : G →* N) (h : function.surjective f) : subgroup.map f ⊤ = ⊤ := by {rw eq_top_iff, intros x hx, obtain ⟨y, hy⟩ := (h x), exact ⟨y, trivial, hy⟩ } @[simp, to_additive] lemma comap_top (f : G →* N) : (⊤ : subgroup N).comap f = ⊤ := (gc_map_comap f).u_top @[simp, to_additive] lemma comap_subtype_self_eq_top {G : Type*} [group G] {H : subgroup G} : comap H.subtype H = ⊤ := by { ext, simp } @[simp, to_additive] lemma comap_subtype_inf_left {H K : subgroup G} : comap H.subtype (H ⊓ K) = comap H.subtype K := ext $ λ x, and_iff_right_of_imp (λ _, x.prop) @[simp, to_additive] lemma comap_subtype_inf_right {H K : subgroup G} : comap K.subtype (H ⊓ K) = comap K.subtype H := ext $ λ x, and_iff_left_of_imp (λ _, x.prop) /-- If `H ≤ K`, then `H` as a subgroup of `K` is isomorphic to `H`. -/ @[to_additive "If `H ≤ K`, then `H` as a subgroup of `K` is isomorphic to `H`.", simps] def comap_subtype_equiv_of_le {G : Type*} [group G] {H K : subgroup G} (h : H ≤ K) : H.comap K.subtype ≃* H := { to_fun := λ g, ⟨g.1, g.2⟩, inv_fun := λ g, ⟨⟨g.1, h g.2⟩, g.2⟩, left_inv := λ g, subtype.ext (subtype.ext rfl), right_inv := λ g, subtype.ext rfl, map_mul' := λ g h, rfl } /-- For any subgroups `H` and `K`, view `H ⊓ K` as a subgroup of `K`. -/ @[to_additive "For any subgroups `H` and `K`, view `H ⊓ K` as a subgroup of `K`."] def subgroup_of (H K : subgroup G) : subgroup K := H.comap K.subtype @[to_additive] lemma coe_subgroup_of (H K : subgroup G) : (H.subgroup_of K : set K) = K.subtype ⁻¹' H := rfl @[to_additive] lemma mem_subgroup_of {H K : subgroup G} {h : K} : h ∈ H.subgroup_of K ↔ (h : G) ∈ H := iff.rfl @[to_additive] lemma subgroup_of_map_subtype (H K : subgroup G) : (H.subgroup_of K).map K.subtype = H ⊓ K := set_like.ext' begin convert set.image_preimage_eq_inter_range, simp only [subtype.range_coe_subtype, coe_subtype, coe_inf], refl, end @[simp, to_additive] lemma bot_subgroup_of : (⊥ : subgroup G).subgroup_of H = ⊥ := eq.symm (subgroup.ext (λ g, subtype.ext_iff)) @[simp, to_additive] lemma top_subgroup_of : (⊤ : subgroup G).subgroup_of H = ⊤ := rfl @[to_additive] lemma subgroup_of_bot_eq_bot : H.subgroup_of ⊥ = ⊥ := subsingleton.elim _ _ @[to_additive] lemma subgroup_of_bot_eq_top : H.subgroup_of ⊥ = ⊤ := subsingleton.elim _ _ @[simp, to_additive] lemma subgroup_of_self : H.subgroup_of H = ⊤ := top_le_iff.mp (λ g hg, g.2) /-- Given `subgroup`s `H`, `K` of groups `G`, `N` respectively, `H × K` as a subgroup of `G × N`. -/ @[to_additive prod "Given `add_subgroup`s `H`, `K` of `add_group`s `A`, `B` respectively, `H × K` as an `add_subgroup` of `A × B`."] def prod (H : subgroup G) (K : subgroup N) : subgroup (G × N) := { inv_mem' := λ _ hx, ⟨H.inv_mem' hx.1, K.inv_mem' hx.2⟩, .. submonoid.prod H.to_submonoid K.to_submonoid} @[to_additive coe_prod] lemma coe_prod (H : subgroup G) (K : subgroup N) : (H.prod K : set (G × N)) = H ×ˢ K := rfl @[to_additive mem_prod] lemma mem_prod {H : subgroup G} {K : subgroup N} {p : G × N} : p ∈ H.prod K ↔ p.1 ∈ H ∧ p.2 ∈ K := iff.rfl @[to_additive prod_mono] lemma prod_mono : ((≤) ⇒ (≤) ⇒ (≤)) (@prod G _ N _) (@prod G _ N _) := λ s s' hs t t' ht, set.prod_mono hs ht @[to_additive prod_mono_right] lemma prod_mono_right (K : subgroup G) : monotone (λ t : subgroup N, K.prod t) := prod_mono (le_refl K) @[to_additive prod_mono_left] lemma prod_mono_left (H : subgroup N) : monotone (λ K : subgroup G, K.prod H) := λ s₁ s₂ hs, prod_mono hs (le_refl H) @[to_additive prod_top] lemma prod_top (K : subgroup G) : K.prod (⊤ : subgroup N) = K.comap (monoid_hom.fst G N) := ext $ λ x, by simp [mem_prod, monoid_hom.coe_fst] @[to_additive top_prod] lemma top_prod (H : subgroup N) : (⊤ : subgroup G).prod H = H.comap (monoid_hom.snd G N) := ext $ λ x, by simp [mem_prod, monoid_hom.coe_snd] @[simp, to_additive top_prod_top] lemma top_prod_top : (⊤ : subgroup G).prod (⊤ : subgroup N) = ⊤ := (top_prod _).trans $ comap_top _ @[to_additive] lemma bot_prod_bot : (⊥ : subgroup G).prod (⊥ : subgroup N) = ⊥ := set_like.coe_injective $ by simp [coe_prod, prod.one_eq_mk] @[to_additive le_prod_iff] lemma le_prod_iff {H : subgroup G} {K : subgroup N} {J : subgroup (G × N)} : J ≤ H.prod K ↔ map (monoid_hom.fst G N) J ≤ H ∧ map (monoid_hom.snd G N) J ≤ K := by simpa only [← subgroup.to_submonoid_le] using submonoid.le_prod_iff @[to_additive prod_le_iff] lemma prod_le_iff {H : subgroup G} {K : subgroup N} {J : subgroup (G × N)} : H.prod K ≤ J ↔ map (monoid_hom.inl G N) H ≤ J ∧ map (monoid_hom.inr G N) K ≤ J := by simpa only [← subgroup.to_submonoid_le] using submonoid.prod_le_iff @[simp, to_additive prod_eq_bot_iff] lemma prod_eq_bot_iff {H : subgroup G} {K : subgroup N} : H.prod K = ⊥ ↔ H = ⊥ ∧ K = ⊥ := by simpa only [← subgroup.to_submonoid_eq] using submonoid.prod_eq_bot_iff /-- Product of subgroups is isomorphic to their product as groups. -/ @[to_additive prod_equiv "Product of additive subgroups is isomorphic to their product as additive groups"] def prod_equiv (H : subgroup G) (K : subgroup N) : H.prod K ≃* H × K := { map_mul' := λ x y, rfl, .. equiv.set.prod ↑H ↑K } section pi variables {η : Type*} {f : η → Type*} -- defined here and not in group_theory.submonoid.operations to have access to algebra.group.pi /-- A version of `set.pi` for submonoids. Given an index set `I` and a family of submodules `s : Π i, submonoid f i`, `pi I s` is the submonoid of dependent functions `f : Π i, f i` such that `f i` belongs to `pi I s` whenever `i ∈ I`. -/ @[to_additive " A version of `set.pi` for `add_submonoid`s. Given an index set `I` and a family of submodules `s : Π i, add_submonoid f i`, `pi I s` is the `add_submonoid` of dependent functions `f : Π i, f i` such that `f i` belongs to `pi I s` whenever `i ∈ I`. -/ "] def _root_.submonoid.pi [∀ i, mul_one_class (f i)] (I : set η) (s : Π i, submonoid (f i)) : submonoid (Π i, f i) := { carrier := I.pi (λ i, (s i).carrier), one_mem' := λ i _ , (s i).one_mem, mul_mem' := λ p q hp hq i hI, (s i).mul_mem (hp i hI) (hq i hI) } variables [∀ i, group (f i)] /-- A version of `set.pi` for subgroups. Given an index set `I` and a family of submodules `s : Π i, subgroup f i`, `pi I s` is the subgroup of dependent functions `f : Π i, f i` such that `f i` belongs to `pi I s` whenever `i ∈ I`. -/ @[to_additive " A version of `set.pi` for `add_subgroup`s. Given an index set `I` and a family of submodules `s : Π i, add_subgroup f i`, `pi I s` is the `add_subgroup` of dependent functions `f : Π i, f i` such that `f i` belongs to `pi I s` whenever `i ∈ I`. -/ "] def pi (I : set η) (H : Π i, subgroup (f i)) : subgroup (Π i, f i) := { submonoid.pi I (λ i, (H i).to_submonoid) with inv_mem' := λ p hp i hI, (H i).inv_mem (hp i hI) } @[to_additive] lemma coe_pi (I : set η) (H : Π i, subgroup (f i)) : (pi I H : set (Π i, f i)) = set.pi I (λ i, (H i : set (f i))) := rfl @[to_additive] lemma mem_pi (I : set η) {H : Π i, subgroup (f i)} {p : Π i, f i} : p ∈ pi I H ↔ (∀ i : η, i ∈ I → p i ∈ H i) := iff.rfl @[to_additive] lemma pi_top (I : set η) : pi I (λ i, (⊤ : subgroup (f i))) = ⊤ := ext $ λ x, by simp [mem_pi] @[to_additive] lemma pi_empty (H : Π i, subgroup (f i)): pi ∅ H = ⊤ := ext $ λ x, by simp [mem_pi] @[to_additive] lemma pi_bot : pi set.univ (λ i, (⊥ : subgroup (f i))) = ⊥ := (eq_bot_iff_forall _).mpr $ λ p hp, by { simp only [mem_pi, mem_bot] at *, ext j, exact hp j trivial, } @[to_additive] lemma le_pi_iff {I : set η} {H : Π i, subgroup (f i)} {J : subgroup (Π i, f i)} : J ≤ pi I H ↔ (∀ i : η , i ∈ I → map (pi.eval_monoid_hom f i) J ≤ H i) := begin split, { intros h i hi, rintros _ ⟨x, hx, rfl⟩, exact (h hx) _ hi, }, { intros h x hx i hi, refine h i hi ⟨_, hx, rfl⟩, } end @[simp, to_additive] lemma mul_single_mem_pi [decidable_eq η] {I : set η} {H : Π i, subgroup (f i)} (i : η) (x : f i) : pi.mul_single i x ∈ pi I H ↔ (i ∈ I → x ∈ H i) := begin split, { intros h hi, simpa using h i hi, }, { intros h j hj, by_cases heq : j = i, { subst heq, simpa using h hj, }, { simp [heq, one_mem], }, } end @[to_additive] lemma pi_mem_of_mul_single_mem_aux [decidable_eq η] (I : finset η) {H : subgroup (Π i, f i) } (x : Π i, f i) (h1 : ∀ i, i ∉ I → x i = 1) (h2 : ∀ i, i ∈ I → pi.mul_single i (x i) ∈ H ) : x ∈ H := begin induction I using finset.induction_on with i I hnmem ih generalizing x, { convert one_mem H, ext i, exact (h1 i (not_mem_empty i)) }, { have : x = function.update x i 1 * pi.mul_single i (x i), { ext j, by_cases heq : j = i, { subst heq, simp, }, { simp [heq], }, }, rw this, clear this, apply mul_mem, { apply ih; clear ih, { intros j hj, by_cases heq : j = i, { subst heq, simp, }, { simp [heq], apply h1 j, simpa [heq] using hj, } }, { intros j hj, have : j ≠ i, by { rintro rfl, contradiction }, simp [this], exact h2 _ (finset.mem_insert_of_mem hj), }, }, { apply h2, simp, } } end @[to_additive] lemma pi_mem_of_mul_single_mem [finite η] [decidable_eq η] {H : subgroup (Π i, f i)} (x : Π i, f i) (h : ∀ i, pi.mul_single i (x i) ∈ H) : x ∈ H := by { casesI nonempty_fintype η, exact pi_mem_of_mul_single_mem_aux finset.univ x (by simp) (λ i _, h i) } /-- For finite index types, the `subgroup.pi` is generated by the embeddings of the groups. -/ @[to_additive "For finite index types, the `subgroup.pi` is generated by the embeddings of the additive groups."] lemma pi_le_iff [decidable_eq η] [finite η] {H : Π i, subgroup (f i)} {J : subgroup (Π i, f i)} : pi univ H ≤ J ↔ ∀ i : η, map (monoid_hom.single f i) (H i) ≤ J := begin split, { rintros h i _ ⟨x, hx, rfl⟩, apply h, simpa using hx }, { exact λ h x hx, pi_mem_of_mul_single_mem x (λ i, h i (mem_map_of_mem _ (hx i trivial))), } end @[to_additive] lemma pi_eq_bot_iff (H : Π i, subgroup (f i)) : pi set.univ H = ⊥ ↔ ∀ i, H i = ⊥ := begin classical, simp only [eq_bot_iff_forall], split, { intros h i x hx, have : monoid_hom.single f i x = 1 := h (monoid_hom.single f i x) ((mul_single_mem_pi i x).mpr (λ _, hx)), simpa using congr_fun this i, }, { exact λ h x hx, funext (λ i, h _ _ (hx i trivial)), }, end end pi /-- A subgroup is normal if whenever `n ∈ H`, then `g * n * g⁻¹ ∈ H` for every `g : G` -/ structure normal : Prop := (conj_mem : ∀ n, n ∈ H → ∀ g : G, g * n * g⁻¹ ∈ H) attribute [class] normal end subgroup namespace add_subgroup /-- An add_subgroup is normal if whenever `n ∈ H`, then `g + n - g ∈ H` for every `g : G` -/ structure normal (H : add_subgroup A) : Prop := (conj_mem [] : ∀ n, n ∈ H → ∀ g : A, g + n + -g ∈ H) attribute [to_additive add_subgroup.normal] subgroup.normal attribute [class] normal end add_subgroup namespace subgroup variables {H K : subgroup G} @[priority 100, to_additive] instance normal_of_comm {G : Type*} [comm_group G] (H : subgroup G) : H.normal := ⟨by simp [mul_comm, mul_left_comm]⟩ namespace normal variable (nH : H.normal) @[to_additive] lemma mem_comm {a b : G} (h : a * b ∈ H) : b * a ∈ H := have a⁻¹ * (a * b) * a⁻¹⁻¹ ∈ H, from nH.conj_mem (a * b) h a⁻¹, by simpa @[to_additive] lemma mem_comm_iff {a b : G} : a * b ∈ H ↔ b * a ∈ H := ⟨nH.mem_comm, nH.mem_comm⟩ end normal variables (H) /-- A subgroup is characteristic if it is fixed by all automorphisms. Several equivalent conditions are provided by lemmas of the form `characteristic.iff...` -/ structure characteristic : Prop := (fixed : ∀ ϕ : G ≃* G, H.comap ϕ.to_monoid_hom = H) attribute [class] characteristic @[priority 100] instance normal_of_characteristic [h : H.characteristic] : H.normal := ⟨λ a ha b, (set_like.ext_iff.mp (h.fixed (mul_aut.conj b)) a).mpr ha⟩ end subgroup namespace add_subgroup variables (H : add_subgroup A) /-- A add_subgroup is characteristic if it is fixed by all automorphisms. Several equivalent conditions are provided by lemmas of the form `characteristic.iff...` -/ structure characteristic : Prop := (fixed : ∀ ϕ : A ≃+ A, H.comap ϕ.to_add_monoid_hom = H) attribute [to_additive add_subgroup.characteristic] subgroup.characteristic attribute [class] characteristic @[priority 100] instance normal_of_characteristic [h : H.characteristic] : H.normal := ⟨λ a ha b, (set_like.ext_iff.mp (h.fixed (add_aut.conj b)) a).mpr ha⟩ end add_subgroup namespace subgroup variables {H K : subgroup G} @[to_additive] lemma characteristic_iff_comap_eq : H.characteristic ↔ ∀ ϕ : G ≃* G, H.comap ϕ.to_monoid_hom = H := ⟨characteristic.fixed, characteristic.mk⟩ @[to_additive] lemma characteristic_iff_comap_le : H.characteristic ↔ ∀ ϕ : G ≃* G, H.comap ϕ.to_monoid_hom ≤ H := characteristic_iff_comap_eq.trans ⟨λ h ϕ, le_of_eq (h ϕ), λ h ϕ, le_antisymm (h ϕ) (λ g hg, h ϕ.symm ((congr_arg (∈ H) (ϕ.symm_apply_apply g)).mpr hg))⟩ @[to_additive] lemma characteristic_iff_le_comap : H.characteristic ↔ ∀ ϕ : G ≃* G, H ≤ H.comap ϕ.to_monoid_hom := characteristic_iff_comap_eq.trans ⟨λ h ϕ, ge_of_eq (h ϕ), λ h ϕ, le_antisymm (λ g hg, (congr_arg (∈ H) (ϕ.symm_apply_apply g)).mp (h ϕ.symm hg)) (h ϕ)⟩ @[to_additive] lemma characteristic_iff_map_eq : H.characteristic ↔ ∀ ϕ : G ≃* G, H.map ϕ.to_monoid_hom = H := begin simp_rw map_equiv_eq_comap_symm, exact characteristic_iff_comap_eq.trans ⟨λ h ϕ, h ϕ.symm, λ h ϕ, h ϕ.symm⟩, end @[to_additive] lemma characteristic_iff_map_le : H.characteristic ↔ ∀ ϕ : G ≃* G, H.map ϕ.to_monoid_hom ≤ H := begin simp_rw map_equiv_eq_comap_symm, exact characteristic_iff_comap_le.trans ⟨λ h ϕ, h ϕ.symm, λ h ϕ, h ϕ.symm⟩, end @[to_additive] lemma characteristic_iff_le_map : H.characteristic ↔ ∀ ϕ : G ≃* G, H ≤ H.map ϕ.to_monoid_hom := begin simp_rw map_equiv_eq_comap_symm, exact characteristic_iff_le_comap.trans ⟨λ h ϕ, h ϕ.symm, λ h ϕ, h ϕ.symm⟩, end @[to_additive] instance bot_characteristic : characteristic (⊥ : subgroup G) := characteristic_iff_le_map.mpr (λ ϕ, bot_le) @[to_additive] instance top_characteristic : characteristic (⊤ : subgroup G) := characteristic_iff_map_le.mpr (λ ϕ, le_top) variable (G) /-- The center of a group `G` is the set of elements that commute with everything in `G` -/ @[to_additive "The center of an additive group `G` is the set of elements that commute with everything in `G`"] def center : subgroup G := { carrier := set.center G, inv_mem' := λ a, set.inv_mem_center, .. submonoid.center G } @[to_additive] lemma coe_center : ↑(center G) = set.center G := rfl @[simp, to_additive] lemma center_to_submonoid : (center G).to_submonoid = submonoid.center G := rfl variable {G} @[to_additive] lemma mem_center_iff {z : G} : z ∈ center G ↔ ∀ g, g * z = z * g := iff.rfl instance decidable_mem_center [decidable_eq G] [fintype G] : decidable_pred (∈ center G) := λ _, decidable_of_iff' _ mem_center_iff @[to_additive] instance center_characteristic : (center G).characteristic := begin refine characteristic_iff_comap_le.mpr (λ ϕ g hg h, _), rw [←ϕ.injective.eq_iff, ϕ.map_mul, ϕ.map_mul], exact hg (ϕ h), end lemma _root_.comm_group.center_eq_top {G : Type*} [comm_group G] : center G = ⊤ := by { rw [eq_top_iff'], intros x y, exact mul_comm y x } /-- A group is commutative if the center is the whole group -/ def _root_.group.comm_group_of_center_eq_top (h : center G = ⊤) : comm_group G := { mul_comm := by { rw eq_top_iff' at h, intros x y, exact h y x }, .. (_ : group G) } variables {G} (H) section normalizer /-- The `normalizer` of `H` is the largest subgroup of `G` inside which `H` is normal. -/ @[to_additive "The `normalizer` of `H` is the largest subgroup of `G` inside which `H` is normal."] def normalizer : subgroup G := { carrier := {g : G | ∀ n, n ∈ H ↔ g * n * g⁻¹ ∈ H}, one_mem' := by simp, mul_mem' := λ a b (ha : ∀ n, n ∈ H ↔ a * n * a⁻¹ ∈ H) (hb : ∀ n, n ∈ H ↔ b * n * b⁻¹ ∈ H) n, by { rw [hb, ha], simp [mul_assoc] }, inv_mem' := λ a (ha : ∀ n, n ∈ H ↔ a * n * a⁻¹ ∈ H) n, by { rw [ha (a⁻¹ * n * a⁻¹⁻¹)], simp [mul_assoc] } } -- variant for sets. -- TODO should this replace `normalizer`? /-- The `set_normalizer` of `S` is the subgroup of `G` whose elements satisfy `g*S*g⁻¹=S` -/ @[to_additive "The `set_normalizer` of `S` is the subgroup of `G` whose elements satisfy `g+S-g=S`."] def set_normalizer (S : set G) : subgroup G := { carrier := {g : G | ∀ n, n ∈ S ↔ g * n * g⁻¹ ∈ S}, one_mem' := by simp, mul_mem' := λ a b (ha : ∀ n, n ∈ S ↔ a * n * a⁻¹ ∈ S) (hb : ∀ n, n ∈ S ↔ b * n * b⁻¹ ∈ S) n, by { rw [hb, ha], simp [mul_assoc] }, inv_mem' := λ a (ha : ∀ n, n ∈ S ↔ a * n * a⁻¹ ∈ S) n, by { rw [ha (a⁻¹ * n * a⁻¹⁻¹)], simp [mul_assoc] } } lemma mem_normalizer_fintype {S : set G} [finite S] {x : G} (h : ∀ n, n ∈ S → x * n * x⁻¹ ∈ S) : x ∈ subgroup.set_normalizer S := by haveI := classical.prop_decidable; casesI nonempty_fintype S; haveI := set.fintype_image S (λ n, x * n * x⁻¹); exact λ n, ⟨h n, λ h₁, have heq : (λ n, x * n * x⁻¹) '' S = S := set.eq_of_subset_of_card_le (λ n ⟨y, hy⟩, hy.2 ▸ h y hy.1) (by rw set.card_image_of_injective S conj_injective), have x * n * x⁻¹ ∈ (λ n, x * n * x⁻¹) '' S := heq.symm ▸ h₁, let ⟨y, hy⟩ := this in conj_injective hy.2 ▸ hy.1⟩ variable {H} @[to_additive] lemma mem_normalizer_iff {g : G} : g ∈ H.normalizer ↔ ∀ h, h ∈ H ↔ g * h * g⁻¹ ∈ H := iff.rfl @[to_additive] lemma mem_normalizer_iff'' {g : G} : g ∈ H.normalizer ↔ ∀ h : G, h ∈ H ↔ g⁻¹ * h * g ∈ H := by rw [←inv_mem_iff, mem_normalizer_iff, inv_inv] @[to_additive] lemma mem_normalizer_iff' {g : G} : g ∈ H.normalizer ↔ ∀ n, n * g ∈ H ↔ g * n ∈ H := ⟨λ h n, by rw [h, mul_assoc, mul_inv_cancel_right], λ h n, by rw [mul_assoc, ←h, inv_mul_cancel_right]⟩ @[to_additive] lemma le_normalizer : H ≤ normalizer H := λ x xH n, by rw [H.mul_mem_cancel_right (H.inv_mem xH), H.mul_mem_cancel_left xH] @[priority 100, to_additive] instance normal_in_normalizer : (H.comap H.normalizer.subtype).normal := ⟨λ x xH g, by simpa using (g.2 x).1 xH⟩ @[to_additive] lemma normalizer_eq_top : H.normalizer = ⊤ ↔ H.normal := eq_top_iff.trans ⟨λ h, ⟨λ a ha b, (h (mem_top b) a).mp ha⟩, λ h a ha b, ⟨λ hb, h.conj_mem b hb a, λ hb, by rwa [h.mem_comm_iff, inv_mul_cancel_left] at hb⟩⟩ @[to_additive] lemma center_le_normalizer : center G ≤ H.normalizer := λ x hx y, by simp [← mem_center_iff.mp hx y, mul_assoc] open_locale classical @[to_additive] lemma le_normalizer_of_normal [hK : (H.comap K.subtype).normal] (HK : H ≤ K) : K ≤ H.normalizer := λ x hx y, ⟨λ yH, hK.conj_mem ⟨y, HK yH⟩ yH ⟨x, hx⟩, λ yH, by simpa [mem_comap, mul_assoc] using hK.conj_mem ⟨x * y * x⁻¹, HK yH⟩ yH ⟨x⁻¹, K.inv_mem hx⟩⟩ variables {N : Type*} [group N] /-- The preimage of the normalizer is contained in the normalizer of the preimage. -/ @[to_additive "The preimage of the normalizer is contained in the normalizer of the preimage."] lemma le_normalizer_comap (f : N →* G) : H.normalizer.comap f ≤ (H.comap f).normalizer := λ x, begin simp only [mem_normalizer_iff, mem_comap], assume h n, simp [h (f n)] end /-- The image of the normalizer is contained in the normalizer of the image. -/ @[to_additive "The image of the normalizer is contained in the normalizer of the image."] lemma le_normalizer_map (f : G →* N) : H.normalizer.map f ≤ (H.map f).normalizer := λ _, begin simp only [and_imp, exists_prop, mem_map, exists_imp_distrib, mem_normalizer_iff], rintros x hx rfl n, split, { rintros ⟨y, hy, rfl⟩, use [x * y * x⁻¹, (hx y).1 hy], simp }, { rintros ⟨y, hyH, hy⟩, use [x⁻¹ * y * x], rw [hx], simp [hy, hyH, mul_assoc] } end variable (G) /-- Every proper subgroup `H` of `G` is a proper normal subgroup of the normalizer of `H` in `G`. -/ def _root_.normalizer_condition := ∀ (H : subgroup G), H < ⊤ → H < normalizer H variable {G} /-- Alternative phrasing of the normalizer condition: Only the full group is self-normalizing. This may be easier to work with, as it avoids inequalities and negations. -/ lemma _root_.normalizer_condition_iff_only_full_group_self_normalizing : normalizer_condition G ↔ ∀ (H : subgroup G), H.normalizer = H → H = ⊤ := begin apply forall_congr, intro H, simp only [lt_iff_le_and_ne, le_normalizer, true_and, le_top, ne.def], tauto!, end variable (H) /-- In a group that satisifes the normalizer condition, every maximal subgroup is normal -/ lemma normalizer_condition.normal_of_coatom (hnc : normalizer_condition G) (hmax : is_coatom H) : H.normal := normalizer_eq_top.mp (hmax.2 _ (hnc H (lt_top_iff_ne_top.mpr hmax.1))) end normalizer section centralizer /-- The `centralizer` of `H` is the subgroup of `g : G` commuting with every `h : H`. -/ @[to_additive "The `centralizer` of `H` is the additive subgroup of `g : G` commuting with every `h : H`."] def centralizer : subgroup G := { carrier := set.centralizer H, inv_mem' := λ g, set.inv_mem_centralizer, .. submonoid.centralizer ↑H } @[to_additive] lemma mem_centralizer_iff {g : G} : g ∈ H.centralizer ↔ ∀ h ∈ H, h * g = g * h := iff.rfl @[to_additive] lemma mem_centralizer_iff_commutator_eq_one {g : G} : g ∈ H.centralizer ↔ ∀ h ∈ H, h * g * h⁻¹ * g⁻¹ = 1 := by simp only [mem_centralizer_iff, mul_inv_eq_iff_eq_mul, one_mul] @[to_additive] lemma centralizer_top : centralizer ⊤ = center G := set_like.ext' (set.centralizer_univ G) @[to_additive] instance subgroup.centralizer.characteristic [hH : H.characteristic] : H.centralizer.characteristic := begin refine subgroup.characteristic_iff_comap_le.mpr (λ ϕ g hg h hh, ϕ.injective _), rw [map_mul, map_mul], exact hg (ϕ h) (subgroup.characteristic_iff_le_comap.mp hH ϕ hh), end end centralizer /-- Commutivity of a subgroup -/ structure is_commutative : Prop := (is_comm : _root_.is_commutative H (*)) attribute [class] is_commutative /-- Commutivity of an additive subgroup -/ structure _root_.add_subgroup.is_commutative (H : add_subgroup A) : Prop := (is_comm : _root_.is_commutative H (+)) attribute [to_additive add_subgroup.is_commutative] subgroup.is_commutative attribute [class] add_subgroup.is_commutative /-- A commutative subgroup is commutative. -/ @[to_additive "A commutative subgroup is commutative."] instance is_commutative.comm_group [h : H.is_commutative] : comm_group H := { mul_comm := h.is_comm.comm, .. H.to_group } instance center.is_commutative : (center G).is_commutative := ⟨⟨λ a b, subtype.ext (b.2 a)⟩⟩ end subgroup namespace group variables {s : set G} /-- Given a set `s`, `conjugates_of_set s` is the set of all conjugates of the elements of `s`. -/ def conjugates_of_set (s : set G) : set G := ⋃ a ∈ s, conjugates_of a lemma mem_conjugates_of_set_iff {x : G} : x ∈ conjugates_of_set s ↔ ∃ a ∈ s, is_conj a x := set.mem_Union₂ theorem subset_conjugates_of_set : s ⊆ conjugates_of_set s := λ (x : G) (h : x ∈ s), mem_conjugates_of_set_iff.2 ⟨x, h, is_conj.refl _⟩ theorem conjugates_of_set_mono {s t : set G} (h : s ⊆ t) : conjugates_of_set s ⊆ conjugates_of_set t := set.bUnion_subset_bUnion_left h lemma conjugates_subset_normal {N : subgroup G} [tn : N.normal] {a : G} (h : a ∈ N) : conjugates_of a ⊆ N := by { rintros a hc, obtain ⟨c, rfl⟩ := is_conj_iff.1 hc, exact tn.conj_mem a h c } theorem conjugates_of_set_subset {s : set G} {N : subgroup G} [N.normal] (h : s ⊆ N) : conjugates_of_set s ⊆ N := set.Union₂_subset (λ x H, conjugates_subset_normal (h H)) /-- The set of conjugates of `s` is closed under conjugation. -/ lemma conj_mem_conjugates_of_set {x c : G} : x ∈ conjugates_of_set s → (c * x * c⁻¹ ∈ conjugates_of_set s) := λ H, begin rcases (mem_conjugates_of_set_iff.1 H) with ⟨a,h₁,h₂⟩, exact mem_conjugates_of_set_iff.2 ⟨a, h₁, h₂.trans (is_conj_iff.2 ⟨c,rfl⟩)⟩, end end group namespace subgroup open group variable {s : set G} /-- The normal closure of a set `s` is the subgroup closure of all the conjugates of elements of `s`. It is the smallest normal subgroup containing `s`. -/ def normal_closure (s : set G) : subgroup G := closure (conjugates_of_set s) theorem conjugates_of_set_subset_normal_closure : conjugates_of_set s ⊆ normal_closure s := subset_closure theorem subset_normal_closure : s ⊆ normal_closure s := set.subset.trans subset_conjugates_of_set conjugates_of_set_subset_normal_closure theorem le_normal_closure {H : subgroup G} : H ≤ normal_closure ↑H := λ _ h, subset_normal_closure h /-- The normal closure of `s` is a normal subgroup. -/ instance normal_closure_normal : (normal_closure s).normal := ⟨λ n h g, begin refine subgroup.closure_induction h (λ x hx, _) _ (λ x y ihx ihy, _) (λ x ihx, _), { exact (conjugates_of_set_subset_normal_closure (conj_mem_conjugates_of_set hx)) }, { simpa using (normal_closure s).one_mem }, { rw ← conj_mul, exact mul_mem ihx ihy }, { rw ← conj_inv, exact inv_mem ihx } end⟩ /-- The normal closure of `s` is the smallest normal subgroup containing `s`. -/ theorem normal_closure_le_normal {N : subgroup G} [N.normal] (h : s ⊆ N) : normal_closure s ≤ N := begin assume a w, refine closure_induction w (λ x hx, _) _ (λ x y ihx ihy, _) (λ x ihx, _), { exact (conjugates_of_set_subset h hx) }, { exact one_mem _ }, { exact mul_mem ihx ihy }, { exact inv_mem ihx } end lemma normal_closure_subset_iff {N : subgroup G} [N.normal] : s ⊆ N ↔ normal_closure s ≤ N := ⟨normal_closure_le_normal, set.subset.trans (subset_normal_closure)⟩ theorem normal_closure_mono {s t : set G} (h : s ⊆ t) : normal_closure s ≤ normal_closure t := normal_closure_le_normal (set.subset.trans h subset_normal_closure) theorem normal_closure_eq_infi : normal_closure s = ⨅ (N : subgroup G) (_ : normal N) (hs : s ⊆ N), N := le_antisymm (le_infi (λ N, le_infi (λ hN, by exactI le_infi (normal_closure_le_normal)))) (infi_le_of_le (normal_closure s) (infi_le_of_le (by apply_instance) (infi_le_of_le subset_normal_closure le_rfl))) @[simp] theorem normal_closure_eq_self (H : subgroup G) [H.normal] : normal_closure ↑H = H := le_antisymm (normal_closure_le_normal rfl.subset) (le_normal_closure) @[simp] theorem normal_closure_idempotent : normal_closure ↑(normal_closure s) = normal_closure s := normal_closure_eq_self _ theorem closure_le_normal_closure {s : set G} : closure s ≤ normal_closure s := by simp only [subset_normal_closure, closure_le] @[simp] theorem normal_closure_closure_eq_normal_closure {s : set G} : normal_closure ↑(closure s) = normal_closure s := le_antisymm (normal_closure_le_normal closure_le_normal_closure) (normal_closure_mono subset_closure) /-- The normal core of a subgroup `H` is the largest normal subgroup of `G` contained in `H`, as shown by `subgroup.normal_core_eq_supr`. -/ def normal_core (H : subgroup G) : subgroup G := { carrier := {a : G | ∀ b : G, b * a * b⁻¹ ∈ H}, one_mem' := λ a, by rw [mul_one, mul_inv_self]; exact H.one_mem, inv_mem' := λ a h b, (congr_arg (∈ H) conj_inv).mp (H.inv_mem (h b)), mul_mem' := λ a b ha hb c, (congr_arg (∈ H) conj_mul).mp (H.mul_mem (ha c) (hb c)) } lemma normal_core_le (H : subgroup G) : H.normal_core ≤ H := λ a h, by { rw [←mul_one a, ←inv_one, ←one_mul a], exact h 1 } instance normal_core_normal (H : subgroup G) : H.normal_core.normal := ⟨λ a h b c, by rw [mul_assoc, mul_assoc, ←mul_inv_rev, ←mul_assoc, ←mul_assoc]; exact h (c * b)⟩ lemma normal_le_normal_core {H : subgroup G} {N : subgroup G} [hN : N.normal] : N ≤ H.normal_core ↔ N ≤ H := ⟨ge_trans H.normal_core_le, λ h_le n hn g, h_le (hN.conj_mem n hn g)⟩ lemma normal_core_mono {H K : subgroup G} (h : H ≤ K) : H.normal_core ≤ K.normal_core := normal_le_normal_core.mpr (H.normal_core_le.trans h) lemma normal_core_eq_supr (H : subgroup G) : H.normal_core = ⨆ (N : subgroup G) (_ : normal N) (hs : N ≤ H), N := le_antisymm (le_supr_of_le H.normal_core (le_supr_of_le H.normal_core_normal (le_supr_of_le H.normal_core_le le_rfl))) (supr_le (λ N, supr_le (λ hN, supr_le (by exactI normal_le_normal_core.mpr)))) @[simp] lemma normal_core_eq_self (H : subgroup G) [H.normal] : H.normal_core = H := le_antisymm H.normal_core_le (normal_le_normal_core.mpr le_rfl) @[simp] theorem normal_core_idempotent (H : subgroup G) : H.normal_core.normal_core = H.normal_core := H.normal_core.normal_core_eq_self end subgroup namespace monoid_hom variables {N : Type*} {P : Type*} [group N] [group P] (K : subgroup G) open subgroup /-- The range of a monoid homomorphism from a group is a subgroup. -/ @[to_additive "The range of an `add_monoid_hom` from an `add_group` is an `add_subgroup`."] def range (f : G →* N) : subgroup N := subgroup.copy ((⊤ : subgroup G).map f) (set.range f) (by simp [set.ext_iff]) @[to_additive] instance decidable_mem_range (f : G →* N) [fintype G] [decidable_eq N] : decidable_pred (∈ f.range) := λ x, fintype.decidable_exists_fintype @[simp, to_additive] lemma coe_range (f : G →* N) : (f.range : set N) = set.range f := rfl @[simp, to_additive] lemma mem_range {f : G →* N} {y : N} : y ∈ f.range ↔ ∃ x, f x = y := iff.rfl @[to_additive] lemma range_eq_map (f : G →* N) : f.range = (⊤ : subgroup G).map f := by ext; simp /-- The canonical surjective group homomorphism `G →* f(G)` induced by a group homomorphism `G →* N`. -/ @[to_additive "The canonical surjective `add_group` homomorphism `G →+ f(G)` induced by a group homomorphism `G →+ N`."] def range_restrict (f : G →* N) : G →* f.range := cod_restrict f _ $ λ x, ⟨x, rfl⟩ @[simp, to_additive] lemma coe_range_restrict (f : G →* N) (g : G) : (f.range_restrict g : N) = f g := rfl @[to_additive] lemma range_restrict_surjective (f : G →* N) : function.surjective f.range_restrict := λ ⟨_, g, rfl⟩, ⟨g, rfl⟩ @[to_additive] lemma map_range (g : N →* P) (f : G →* N) : f.range.map g = (g.comp f).range := by rw [range_eq_map, range_eq_map]; exact (⊤ : subgroup G).map_map g f @[to_additive] lemma range_top_iff_surjective {N} [group N] {f : G →* N} : f.range = (⊤ : subgroup N) ↔ function.surjective f := set_like.ext'_iff.trans $ iff.trans (by rw [coe_range, coe_top]) set.range_iff_surjective /-- The range of a surjective monoid homomorphism is the whole of the codomain. -/ @[to_additive "The range of a surjective `add_monoid` homomorphism is the whole of the codomain."] lemma range_top_of_surjective {N} [group N] (f : G →* N) (hf : function.surjective f) : f.range = (⊤ : subgroup N) := range_top_iff_surjective.2 hf @[simp, to_additive] lemma range_one : (1 : G →* N).range = ⊥ := set_like.ext $ λ x, by simpa using @comm _ (=) _ 1 x @[simp, to_additive] lemma _root_.subgroup.subtype_range (H : subgroup G) : H.subtype.range = H := by { rw [range_eq_map, ← set_like.coe_set_eq, coe_map, subgroup.coe_subtype], ext, simp } @[simp, to_additive] lemma _root_.subgroup.inclusion_range {H K : subgroup G} (h_le : H ≤ K) : (inclusion h_le).range = H.subgroup_of K := subgroup.ext (λ g, set.ext_iff.mp (set.range_inclusion h_le) g) @[to_additive] lemma subgroup_of_range_eq_of_le {G₁ G₂ : Type*} [group G₁] [group G₂] {K : subgroup G₂} (f : G₁ →* G₂) (h : f.range ≤ K) : f.range.subgroup_of K = (f.cod_restrict K (λ x, h ⟨x, rfl⟩)).range := begin ext k, refine exists_congr _, simp [subtype.ext_iff], end /-- Computable alternative to `monoid_hom.of_injective`. -/ @[to_additive /-"Computable alternative to `add_monoid_hom.of_injective`."-/] def of_left_inverse {f : G →* N} {g : N →* G} (h : function.left_inverse g f) : G ≃* f.range := { to_fun := f.range_restrict, inv_fun := g ∘ f.range.subtype, left_inv := h, right_inv := by { rintros ⟨x, y, rfl⟩, apply subtype.ext, rw [coe_range_restrict, function.comp_apply, subgroup.coe_subtype, subtype.coe_mk, h] }, .. f.range_restrict } @[simp, to_additive] lemma of_left_inverse_apply {f : G →* N} {g : N →* G} (h : function.left_inverse g f) (x : G) : ↑(of_left_inverse h x) = f x := rfl @[simp, to_additive] lemma of_left_inverse_symm_apply {f : G →* N} {g : N →* G} (h : function.left_inverse g f) (x : f.range) : (of_left_inverse h).symm x = g x := rfl /-- The range of an injective group homomorphism is isomorphic to its domain. -/ @[to_additive /-"The range of an injective additive group homomorphism is isomorphic to its domain."-/ ] noncomputable def of_injective {f : G →* N} (hf : function.injective f) : G ≃* f.range := (mul_equiv.of_bijective (f.cod_restrict f.range (λ x, ⟨x, rfl⟩)) ⟨λ x y h, hf (subtype.ext_iff.mp h), by { rintros ⟨x, y, rfl⟩, exact ⟨y, rfl⟩ }⟩) @[to_additive] lemma of_injective_apply {f : G →* N} (hf : function.injective f) {x : G} : ↑(of_injective hf x) = f x := rfl section ker variables {M : Type*} [mul_one_class M] /-- The multiplicative kernel of a monoid homomorphism is the subgroup of elements `x : G` such that `f x = 1` -/ @[to_additive "The additive kernel of an `add_monoid` homomorphism is the `add_subgroup` of elements such that `f x = 0`"] def ker (f : G →* M) : subgroup G := { inv_mem' := λ x (hx : f x = 1), calc f x⁻¹ = f x * f x⁻¹ : by rw [hx, one_mul] ... = f (x * x⁻¹) : by rw [f.map_mul] ... = f 1 : by rw [mul_right_inv] ... = 1 : f.map_one, ..f.mker } @[to_additive] lemma mem_ker (f : G →* M) {x : G} : x ∈ f.ker ↔ f x = 1 := iff.rfl @[to_additive] lemma coe_ker (f : G →* M) : (f.ker : set G) = (f : G → M) ⁻¹' {1} := rfl @[to_additive] lemma eq_iff (f : G →* N) {x y : G} : f x = f y ↔ y⁻¹ * x ∈ f.ker := by rw [f.mem_ker, f.map_mul, f.map_inv, inv_mul_eq_one, eq_comm] @[to_additive] instance decidable_mem_ker [decidable_eq M] (f : G →* M) : decidable_pred (∈ f.ker) := λ x, decidable_of_iff (f x = 1) f.mem_ker @[to_additive] lemma comap_ker (g : N →* P) (f : G →* N) : g.ker.comap f = (g.comp f).ker := rfl @[simp, to_additive] lemma comap_bot (f : G →* N) : (⊥ : subgroup N).comap f = f.ker := rfl @[to_additive] lemma range_restrict_ker (f : G →* N) : ker (range_restrict f) = ker f := begin ext, change (⟨f x, _⟩ : range f) = ⟨1, _⟩ ↔ f x = 1, simp only [], end @[simp, to_additive] lemma ker_one : (1 : G →* M).ker = ⊤ := by { ext, simp [mem_ker] } @[to_additive] lemma ker_eq_bot_iff (f : G →* N) : f.ker = ⊥ ↔ function.injective f := begin split, { intros h x y hxy, rwa [←mul_inv_eq_one, ←map_inv, ←map_mul, ←mem_ker, h, mem_bot, mul_inv_eq_one] at hxy }, { exact λ h, le_bot_iff.mp (λ x hx, h (hx.trans f.map_one.symm)) }, end @[simp, to_additive] lemma _root_.subgroup.ker_subtype (H : subgroup G) : H.subtype.ker = ⊥ := H.subtype.ker_eq_bot_iff.mpr subtype.coe_injective @[simp, to_additive] lemma _root_.subgroup.ker_inclusion {H K : subgroup G} (h : H ≤ K) : (inclusion h).ker = ⊥ := (inclusion h).ker_eq_bot_iff.mpr (set.inclusion_injective h) @[to_additive] lemma prod_map_comap_prod {G' : Type*} {N' : Type*} [group G'] [group N'] (f : G →* N) (g : G' →* N') (S : subgroup N) (S' : subgroup N') : (S.prod S').comap (prod_map f g) = (S.comap f).prod (S'.comap g) := set_like.coe_injective $ set.preimage_prod_map_prod f g _ _ @[to_additive] lemma ker_prod_map {G' : Type*} {N' : Type*} [group G'] [group N'] (f : G →* N) (g : G' →* N') : (prod_map f g).ker = f.ker.prod g.ker := by rw [←comap_bot, ←comap_bot, ←comap_bot, ←prod_map_comap_prod, bot_prod_bot] end ker /-- The subgroup of elements `x : G` such that `f x = g x` -/ @[to_additive "The additive subgroup of elements `x : G` such that `f x = g x`"] def eq_locus (f g : G →* N) : subgroup G := { inv_mem' := λ x (hx : f x = g x), show f x⁻¹ = g x⁻¹, by rw [f.map_inv, g.map_inv, hx], .. eq_mlocus f g} /-- If two monoid homomorphisms are equal on a set, then they are equal on its subgroup closure. -/ @[to_additive "If two monoid homomorphisms are equal on a set, then they are equal on its subgroup closure."] lemma eq_on_closure {f g : G →* N} {s : set G} (h : set.eq_on f g s) : set.eq_on f g (closure s) := show closure s ≤ f.eq_locus g, from (closure_le _).2 h @[to_additive] lemma eq_of_eq_on_top {f g : G →* N} (h : set.eq_on f g (⊤ : subgroup G)) : f = g := ext $ λ x, h trivial @[to_additive] lemma eq_of_eq_on_dense {s : set G} (hs : closure s = ⊤) {f g : G →* N} (h : s.eq_on f g) : f = g := eq_of_eq_on_top $ hs ▸ eq_on_closure h @[to_additive] lemma gclosure_preimage_le (f : G →* N) (s : set N) : closure (f ⁻¹' s) ≤ (closure s).comap f := (closure_le _).2 $ λ x hx, by rw [set_like.mem_coe, mem_comap]; exact subset_closure hx /-- The image under a monoid homomorphism of the subgroup generated by a set equals the subgroup generated by the image of the set. -/ @[to_additive "The image under an `add_monoid` hom of the `add_subgroup` generated by a set equals the `add_subgroup` generated by the image of the set."] lemma map_closure (f : G →* N) (s : set G) : (closure s).map f = closure (f '' s) := set.image_preimage.l_comm_of_u_comm (subgroup.gc_map_comap f) (subgroup.gi N).gc (subgroup.gi G).gc (λ t, rfl) -- this instance can't go just after the definition of `mrange` because `fintype` is -- not imported at that stage /-- The range of a finite monoid under a monoid homomorphism is finite. Note: this instance can form a diamond with `subtype.fintype` in the presence of `fintype N`. -/ @[to_additive "The range of a finite additive monoid under an additive monoid homomorphism is finite. Note: this instance can form a diamond with `subtype.fintype` or `subgroup.fintype` in the presence of `fintype N`."] instance fintype_mrange {M N : Type*} [monoid M] [monoid N] [fintype M] [decidable_eq N] (f : M →* N) : fintype (mrange f) := set.fintype_range f /-- The range of a finite group under a group homomorphism is finite. Note: this instance can form a diamond with `subtype.fintype` or `subgroup.fintype` in the presence of `fintype N`. -/ @[to_additive "The range of a finite additive group under an additive group homomorphism is finite. Note: this instance can form a diamond with `subtype.fintype` or `subgroup.fintype` in the presence of `fintype N`."] instance fintype_range [fintype G] [decidable_eq N] (f : G →* N) : fintype (range f) := set.fintype_range f end monoid_hom namespace subgroup variables {N : Type*} [group N] (H : subgroup G) @[to_additive] lemma map_eq_bot_iff {f : G →* N} : H.map f = ⊥ ↔ H ≤ f.ker := begin rw eq_bot_iff, split, { exact λ h x hx, h ⟨x, hx, rfl⟩ }, { intros h x hx, obtain ⟨y, hy, rfl⟩ := hx, exact h hy }, end @[to_additive] lemma map_eq_bot_iff_of_injective {f : G →* N} (hf : function.injective f) : H.map f = ⊥ ↔ H = ⊥ := by rw [map_eq_bot_iff, f.ker_eq_bot_iff.mpr hf, le_bot_iff] end subgroup namespace subgroup open monoid_hom variables {N : Type*} [group N] (f : G →* N) @[to_additive] lemma map_le_range (H : subgroup G) : map f H ≤ f.range := (range_eq_map f).symm ▸ map_mono le_top @[to_additive] lemma map_subtype_le {H : subgroup G} (K : subgroup H) : K.map H.subtype ≤ H := (K.map_le_range H.subtype).trans (le_of_eq H.subtype_range) @[to_additive] lemma ker_le_comap (H : subgroup N) : f.ker ≤ comap f H := (comap_bot f) ▸ comap_mono bot_le @[to_additive] lemma map_comap_le (H : subgroup N) : map f (comap f H) ≤ H := (gc_map_comap f).l_u_le _ @[to_additive] lemma le_comap_map (H : subgroup G) : H ≤ comap f (map f H) := (gc_map_comap f).le_u_l _ @[to_additive] lemma map_comap_eq (H : subgroup N) : map f (comap f H) = f.range ⊓ H := set_like.ext' begin convert set.image_preimage_eq_inter_range, simp [set.inter_comm], end @[to_additive] lemma comap_map_eq (H : subgroup G) : comap f (map f H) = H ⊔ f.ker := begin refine le_antisymm _ (sup_le (le_comap_map _ _) (ker_le_comap _ _)), intros x hx, simp only [exists_prop, mem_map, mem_comap] at hx, rcases hx with ⟨y, hy, hy'⟩, rw ← mul_inv_cancel_left y x, exact mul_mem_sup hy (by simp [mem_ker, hy']), end @[to_additive] lemma map_comap_eq_self {f : G →* N} {H : subgroup N} (h : H ≤ f.range) : map f (comap f H) = H := by rwa [map_comap_eq, inf_eq_right] @[to_additive] lemma map_comap_eq_self_of_surjective {f : G →* N} (h : function.surjective f) (H : subgroup N) : map f (comap f H) = H := map_comap_eq_self ((range_top_of_surjective _ h).symm ▸ le_top) @[to_additive] lemma comap_le_comap_of_le_range {f : G →* N} {K L : subgroup N} (hf : K ≤ f.range) : K.comap f ≤ L.comap f ↔ K ≤ L := ⟨(map_comap_eq_self hf).ge.trans ∘ map_le_iff_le_comap.mpr, comap_mono⟩ @[to_additive] lemma comap_le_comap_of_surjective {f : G →* N} {K L : subgroup N} (hf : function.surjective f) : K.comap f ≤ L.comap f ↔ K ≤ L := comap_le_comap_of_le_range (le_top.trans (f.range_top_of_surjective hf).ge) @[to_additive] lemma comap_lt_comap_of_surjective {f : G →* N} {K L : subgroup N} (hf : function.surjective f) : K.comap f < L.comap f ↔ K < L := by simp_rw [lt_iff_le_not_le, comap_le_comap_of_surjective hf] @[to_additive] lemma comap_injective {f : G →* N} (h : function.surjective f) : function.injective (comap f) := λ K L, by simp only [le_antisymm_iff, comap_le_comap_of_surjective h, imp_self] @[to_additive] lemma comap_map_eq_self {f : G →* N} {H : subgroup G} (h : f.ker ≤ H) : comap f (map f H) = H := by rwa [comap_map_eq, sup_eq_left] @[to_additive] lemma comap_map_eq_self_of_injective {f : G →* N} (h : function.injective f) (H : subgroup G) : comap f (map f H) = H := comap_map_eq_self (((ker_eq_bot_iff _).mpr h).symm ▸ bot_le) @[to_additive] lemma map_le_map_iff_of_injective {f : G →* N} (hf : function.injective f) {H K : subgroup G} : H.map f ≤ K.map f ↔ H ≤ K := ⟨(congr_arg2 (≤) (H.comap_map_eq_self_of_injective hf) (K.comap_map_eq_self_of_injective hf)).mp ∘ comap_mono, map_mono⟩ @[simp, to_additive] lemma map_subtype_le_map_subtype {G' : subgroup G} {H K : subgroup G'} : H.map G'.subtype ≤ K.map G'.subtype ↔ H ≤ K := map_le_map_iff_of_injective subtype.coe_injective @[to_additive] lemma map_injective {f : G →* N} (h : function.injective f) : function.injective (map f) := λ K L hKL, by { apply_fun comap f at hKL, simpa [comap_map_eq_self_of_injective h] using hKL } @[to_additive] lemma map_eq_comap_of_inverse {f : G →* N} {g : N →* G} (hl : function.left_inverse g f) (hr : function.right_inverse g f) (H : subgroup G) : map f H = comap g H := set_like.ext' $ by rw [coe_map, coe_comap, set.image_eq_preimage_of_inverse hl hr] /-- Given `f(A) = f(B)`, `ker f ≤ A`, and `ker f ≤ B`, deduce that `A = B`. -/ @[to_additive "Given `f(A) = f(B)`, `ker f ≤ A`, and `ker f ≤ B`, deduce that `A = B`."] lemma map_injective_of_ker_le {H K : subgroup G} (hH : f.ker ≤ H) (hK : f.ker ≤ K) (hf : map f H = map f K) : H = K := begin apply_fun comap f at hf, rwa [comap_map_eq, comap_map_eq, sup_of_le_left hH, sup_of_le_left hK] at hf, end @[to_additive] lemma comap_sup_eq_of_le_range {H K : subgroup N} (hH : H ≤ f.range) (hK : K ≤ f.range) : comap f H ⊔ comap f K = comap f (H ⊔ K) := map_injective_of_ker_le f ((ker_le_comap f H).trans le_sup_left) (ker_le_comap f (H ⊔ K)) (by rw [map_comap_eq, map_sup, map_comap_eq, map_comap_eq, inf_eq_right.mpr hH, inf_eq_right.mpr hK, inf_eq_right.mpr (sup_le hH hK)]) @[to_additive] lemma comap_sup_eq (H K : subgroup N) (hf : function.surjective f) : comap f H ⊔ comap f K = comap f (H ⊔ K) := comap_sup_eq_of_le_range f (le_top.trans (ge_of_eq (f.range_top_of_surjective hf))) (le_top.trans (ge_of_eq (f.range_top_of_surjective hf))) @[to_additive] lemma sup_subgroup_of_eq {H K L : subgroup G} (hH : H ≤ L) (hK : K ≤ L) : H.subgroup_of L ⊔ K.subgroup_of L = (H ⊔ K).subgroup_of L := comap_sup_eq_of_le_range L.subtype (hH.trans (ge_of_eq L.subtype_range)) (hK.trans (ge_of_eq L.subtype_range)) /-- A subgroup is isomorphic to its image under an injective function -/ @[to_additive "An additive subgroup is isomorphic to its image under an injective function"] noncomputable def equiv_map_of_injective (H : subgroup G) (f : G →* N) (hf : function.injective f) : H ≃* H.map f := { map_mul' := λ _ _, subtype.ext (f.map_mul _ _), ..equiv.set.image f H hf } @[simp, to_additive] lemma coe_equiv_map_of_injective_apply (H : subgroup G) (f : G →* N) (hf : function.injective f) (h : H) : (equiv_map_of_injective H f hf h : N) = f h := rfl /-- The preimage of the normalizer is equal to the normalizer of the preimage of a surjective function. -/ @[to_additive "The preimage of the normalizer is equal to the normalizer of the preimage of a surjective function."] lemma comap_normalizer_eq_of_surjective (H : subgroup G) {f : N →* G} (hf : function.surjective f) : H.normalizer.comap f = (H.comap f).normalizer := le_antisymm (le_normalizer_comap f) begin assume x hx, simp only [mem_comap, mem_normalizer_iff] at *, assume n, rcases hf n with ⟨y, rfl⟩, simp [hx y] end @[to_additive] lemma comap_normalizer_eq_of_injective_of_le_range {N : Type*} [group N] (H : subgroup G) {f : N →* G} (hf : function.injective f) (h : H.normalizer ≤ f.range) : comap f H.normalizer = (comap f H).normalizer := begin apply (subgroup.map_injective hf), rw map_comap_eq_self h, apply le_antisymm, { refine (le_trans (le_of_eq _) (map_mono (le_normalizer_comap _))), rewrite map_comap_eq_self h, }, { refine (le_trans (le_normalizer_map f) (le_of_eq _)), rewrite map_comap_eq_self (le_trans le_normalizer h), } end @[to_additive] lemma comap_subtype_normalizer_eq {H N : subgroup G} (h : H.normalizer ≤ N) : comap N.subtype H.normalizer = (comap N.subtype H).normalizer := begin apply comap_normalizer_eq_of_injective_of_le_range, exact subtype.coe_injective, simpa, end /-- The image of the normalizer is equal to the normalizer of the image of an isomorphism. -/ @[to_additive "The image of the normalizer is equal to the normalizer of the image of an isomorphism."] lemma map_equiv_normalizer_eq (H : subgroup G) (f : G ≃* N) : H.normalizer.map f.to_monoid_hom = (H.map f.to_monoid_hom).normalizer := begin ext x, simp only [mem_normalizer_iff, mem_map_equiv], rw [f.to_equiv.forall_congr], simp end /-- The image of the normalizer is equal to the normalizer of the image of a bijective function. -/ @[to_additive "The image of the normalizer is equal to the normalizer of the image of a bijective function."] lemma map_normalizer_eq_of_bijective (H : subgroup G) {f : G →* N} (hf : function.bijective f) : H.normalizer.map f = (H.map f).normalizer := map_equiv_normalizer_eq H (mul_equiv.of_bijective f hf) end subgroup namespace monoid_hom variables {G₁ G₂ G₃ : Type*} [group G₁] [group G₂] [group G₃] variables (f : G₁ →* G₂) (f_inv : G₂ → G₁) /-- Auxiliary definition used to define `lift_of_right_inverse` -/ @[to_additive "Auxiliary definition used to define `lift_of_right_inverse`"] def lift_of_right_inverse_aux (hf : function.right_inverse f_inv f) (g : G₁ →* G₃) (hg : f.ker ≤ g.ker) : G₂ →* G₃ := { to_fun := λ b, g (f_inv b), map_one' := hg (hf 1), map_mul' := begin intros x y, rw [← g.map_mul, ← mul_inv_eq_one, ← g.map_inv, ← g.map_mul, ← g.mem_ker], apply hg, rw [f.mem_ker, f.map_mul, f.map_inv, mul_inv_eq_one, f.map_mul], simp only [hf _], end } @[simp, to_additive] lemma lift_of_right_inverse_aux_comp_apply (hf : function.right_inverse f_inv f) (g : G₁ →* G₃) (hg : f.ker ≤ g.ker) (x : G₁) : (f.lift_of_right_inverse_aux f_inv hf g hg) (f x) = g x := begin dsimp [lift_of_right_inverse_aux], rw [← mul_inv_eq_one, ← g.map_inv, ← g.map_mul, ← g.mem_ker], apply hg, rw [f.mem_ker, f.map_mul, f.map_inv, mul_inv_eq_one], simp only [hf _], end /-- `lift_of_right_inverse f hf g hg` is the unique group homomorphism `φ` * such that `φ.comp f = g` (`monoid_hom.lift_of_right_inverse_comp`), * where `f : G₁ →+* G₂` has a right_inverse `f_inv` (`hf`), * and `g : G₂ →+* G₃` satisfies `hg : f.ker ≤ g.ker`. See `monoid_hom.eq_lift_of_right_inverse` for the uniqueness lemma. ``` G₁. | \ f | \ g | \ v \⌟ G₂----> G₃ ∃!φ ``` -/ @[to_additive "`lift_of_right_inverse f f_inv hf g hg` is the unique additive group homomorphism `φ` * such that `φ.comp f = g` (`add_monoid_hom.lift_of_right_inverse_comp`), * where `f : G₁ →+ G₂` has a right_inverse `f_inv` (`hf`), * and `g : G₂ →+ G₃` satisfies `hg : f.ker ≤ g.ker`. See `add_monoid_hom.eq_lift_of_right_inverse` for the uniqueness lemma. ``` G₁. | \\ f | \\ g | \\ v \\⌟ G₂----> G₃ ∃!φ ```"] def lift_of_right_inverse (hf : function.right_inverse f_inv f) : {g : G₁ →* G₃ // f.ker ≤ g.ker} ≃ (G₂ →* G₃) := { to_fun := λ g, f.lift_of_right_inverse_aux f_inv hf g.1 g.2, inv_fun := λ φ, ⟨φ.comp f, λ x hx, (mem_ker _).mpr $ by simp [(mem_ker _).mp hx]⟩, left_inv := λ g, by { ext, simp only [comp_apply, lift_of_right_inverse_aux_comp_apply, subtype.coe_mk, subtype.val_eq_coe], }, right_inv := λ φ, by { ext b, simp [lift_of_right_inverse_aux, hf b], } } /-- A non-computable version of `monoid_hom.lift_of_right_inverse` for when no computable right inverse is available, that uses `function.surj_inv`. -/ @[simp, to_additive "A non-computable version of `add_monoid_hom.lift_of_right_inverse` for when no computable right inverse is available."] noncomputable abbreviation lift_of_surjective (hf : function.surjective f) : {g : G₁ →* G₃ // f.ker ≤ g.ker} ≃ (G₂ →* G₃) := f.lift_of_right_inverse (function.surj_inv hf) (function.right_inverse_surj_inv hf) @[simp, to_additive] lemma lift_of_right_inverse_comp_apply (hf : function.right_inverse f_inv f) (g : {g : G₁ →* G₃ // f.ker ≤ g.ker}) (x : G₁) : (f.lift_of_right_inverse f_inv hf g) (f x) = g x := f.lift_of_right_inverse_aux_comp_apply f_inv hf g.1 g.2 x @[simp, to_additive] lemma lift_of_right_inverse_comp (hf : function.right_inverse f_inv f) (g : {g : G₁ →* G₃ // f.ker ≤ g.ker}) : (f.lift_of_right_inverse f_inv hf g).comp f = g := monoid_hom.ext $ f.lift_of_right_inverse_comp_apply f_inv hf g @[to_additive] lemma eq_lift_of_right_inverse (hf : function.right_inverse f_inv f) (g : G₁ →* G₃) (hg : f.ker ≤ g.ker) (h : G₂ →* G₃) (hh : h.comp f = g) : h = (f.lift_of_right_inverse f_inv hf ⟨g, hg⟩) := begin simp_rw ←hh, exact ((f.lift_of_right_inverse f_inv hf).apply_symm_apply _).symm, end end monoid_hom variables {N : Type*} [group N] -- Here `H.normal` is an explicit argument so we can use dot notation with `comap`. @[to_additive] lemma subgroup.normal.comap {H : subgroup N} (hH : H.normal) (f : G →* N) : (H.comap f).normal := ⟨λ _, by simp [subgroup.mem_comap, hH.conj_mem] {contextual := tt}⟩ @[priority 100, to_additive] instance subgroup.normal_comap {H : subgroup N} [nH : H.normal] (f : G →* N) : (H.comap f).normal := nH.comap _ @[priority 100, to_additive] instance monoid_hom.normal_ker (f : G →* N) : f.ker.normal := by { rw [←f.comap_bot], apply_instance } @[priority 100, to_additive] instance subgroup.normal_inf (H N : subgroup G) [hN : N.normal] : ((H ⊓ N).comap H.subtype).normal := ⟨λ x hx g, begin simp only [subgroup.mem_inf, coe_subtype, subgroup.mem_comap] at hx, simp only [subgroup.coe_mul, subgroup.mem_inf, coe_subtype, subgroup.coe_inv, subgroup.mem_comap], exact ⟨H.mul_mem (H.mul_mem g.2 hx.1) (H.inv_mem g.2), hN.1 x hx.2 g⟩, end⟩ namespace subgroup /-- The subgroup generated by an element. -/ def zpowers (g : G) : subgroup G := subgroup.copy (zpowers_hom G g).range (set.range ((^) g : ℤ → G)) rfl @[simp] lemma mem_zpowers (g : G) : g ∈ zpowers g := ⟨1, zpow_one _⟩ lemma zpowers_eq_closure (g : G) : zpowers g = closure {g} := by { ext, exact mem_closure_singleton.symm } @[simp] lemma range_zpowers_hom (g : G) : (zpowers_hom G g).range = zpowers g := rfl lemma zpowers_subset {a : G} {K : subgroup G} (h : a ∈ K) : zpowers a ≤ K := λ x hx, match x, hx with _, ⟨i, rfl⟩ := K.zpow_mem h i end lemma mem_zpowers_iff {g h : G} : h ∈ zpowers g ↔ ∃ (k : ℤ), g ^ k = h := iff.rfl @[simp] lemma forall_zpowers {x : G} {p : zpowers x → Prop} : (∀ g, p g) ↔ ∀ m : ℤ, p ⟨x ^ m, m, rfl⟩ := set.forall_subtype_range_iff @[simp] lemma exists_zpowers {x : G} {p : zpowers x → Prop} : (∃ g, p g) ↔ ∃ m : ℤ, p ⟨x ^ m, m, rfl⟩ := set.exists_subtype_range_iff lemma forall_mem_zpowers {x : G} {p : G → Prop} : (∀ g ∈ zpowers x, p g) ↔ ∀ m : ℤ, p (x ^ m) := set.forall_range_iff lemma exists_mem_zpowers {x : G} {p : G → Prop} : (∃ g ∈ zpowers x, p g) ↔ ∃ m : ℤ, p (x ^ m) := set.exists_range_iff end subgroup namespace add_subgroup /-- The subgroup generated by an element. -/ def zmultiples (a : A) : add_subgroup A := add_subgroup.copy (zmultiples_hom A a).range (set.range ((• a) : ℤ → A)) rfl @[simp] lemma range_zmultiples_hom (a : A) : (zmultiples_hom A a).range = zmultiples a := rfl attribute [to_additive add_subgroup.zmultiples] subgroup.zpowers attribute [to_additive add_subgroup.mem_zmultiples] subgroup.mem_zpowers attribute [to_additive add_subgroup.zmultiples_eq_closure] subgroup.zpowers_eq_closure attribute [to_additive add_subgroup.range_zmultiples_hom] subgroup.range_zpowers_hom attribute [to_additive add_subgroup.zmultiples_subset] subgroup.zpowers_subset attribute [to_additive add_subgroup.mem_zmultiples_iff] subgroup.mem_zpowers_iff attribute [to_additive add_subgroup.forall_zmultiples] subgroup.forall_zpowers attribute [to_additive add_subgroup.forall_mem_zmultiples] subgroup.forall_mem_zpowers attribute [to_additive add_subgroup.exists_zmultiples] subgroup.exists_zpowers attribute [to_additive add_subgroup.exists_mem_zmultiples] subgroup.exists_mem_zpowers end add_subgroup lemma int.mem_zmultiples_iff {a b : ℤ} : b ∈ add_subgroup.zmultiples a ↔ a ∣ b := exists_congr (λ k, by rw [mul_comm, eq_comm, ← smul_eq_mul]) lemma of_mul_image_zpowers_eq_zmultiples_of_mul { x : G } : additive.of_mul '' ((subgroup.zpowers x) : set G) = add_subgroup.zmultiples (additive.of_mul x) := begin ext y, split, { rintro ⟨z, ⟨m, hm⟩, hz2⟩, use m, simp only, rwa [← of_mul_zpow, hm] }, { rintros ⟨n, hn⟩, refine ⟨x ^ n, ⟨n, rfl⟩, _⟩, rwa of_mul_zpow } end lemma of_add_image_zmultiples_eq_zpowers_of_add {x : A} : multiplicative.of_add '' ((add_subgroup.zmultiples x) : set A) = subgroup.zpowers (multiplicative.of_add x) := begin symmetry, rw equiv.eq_image_iff_symm_image_eq, exact of_mul_image_zpowers_eq_zmultiples_of_mul, end namespace subgroup @[to_additive zmultiples_is_commutative] instance zpowers_is_commutative (g : G) : (zpowers g).is_commutative := ⟨⟨λ ⟨_, _, h₁⟩ ⟨_, _, h₂⟩, by rw [subtype.ext_iff, coe_mul, coe_mul, subtype.coe_mk, subtype.coe_mk, ←h₁, ←h₂, zpow_mul_comm]⟩⟩ @[simp, to_additive zmultiples_le] lemma zpowers_le {g : G} {H : subgroup G} : zpowers g ≤ H ↔ g ∈ H := by rw [zpowers_eq_closure, closure_le, set.singleton_subset_iff, set_like.mem_coe] @[simp, to_additive zmultiples_eq_bot] lemma zpowers_eq_bot {g : G} : zpowers g = ⊥ ↔ g = 1 := by rw [eq_bot_iff, zpowers_le, mem_bot] end subgroup namespace monoid_hom variables {G' : Type*} [group G'] /-- The `monoid_hom` from the preimage of a subgroup to itself. -/ @[to_additive "the `add_monoid_hom` from the preimage of an additive subgroup to itself.", simps] def subgroup_comap (f : G →* G') (H' : subgroup G') : H'.comap f →* H' := f.submonoid_comap H'.to_submonoid /-- The `monoid_hom` from a subgroup to its image. -/ @[to_additive "the `add_monoid_hom` from an additive subgroup to its image", simps] def subgroup_map (f : G →* G') (H : subgroup G) : H →* H.map f := f.submonoid_map H.to_submonoid @[to_additive] lemma subgroup_map_surjective (f : G →* G') (H : subgroup G) : function.surjective (f.subgroup_map H) := f.submonoid_map_surjective H.to_submonoid end monoid_hom namespace mul_equiv variables {H K : subgroup G} /-- Makes the identity isomorphism from a proof two subgroups of a multiplicative group are equal. -/ @[to_additive "Makes the identity additive isomorphism from a proof two subgroups of an additive group are equal."] def subgroup_congr (h : H = K) : H ≃* K := { map_mul' := λ _ _, rfl, ..equiv.set_congr $ congr_arg _ h } /-- A `mul_equiv` `φ` between two groups `G` and `G'` induces a `mul_equiv` between a subgroup `H ≤ G` and the subgroup `φ(H) ≤ G'`. -/ @[to_additive "An `add_equiv` `φ` between two additive groups `G` and `G'` induces an `add_equiv` between a subgroup `H ≤ G` and the subgroup `φ(H) ≤ G'`. "] def subgroup_map {G'} [group G'] (e : G ≃* G') (H : subgroup G) : H ≃* H.map e.to_monoid_hom := e.submonoid_map H.to_submonoid end mul_equiv -- TODO : ↥(⊤ : subgroup H) ≃* H ? namespace subgroup variables {C : Type*} [comm_group C] {s t : subgroup C} {x : C} @[to_additive] lemma mem_sup : x ∈ s ⊔ t ↔ ∃ (y ∈ s) (z ∈ t), y * z = x := ⟨λ h, begin rw [← closure_eq s, ← closure_eq t, ← closure_union] at h, apply closure_induction h, { rintro y (h | h), { exact ⟨y, h, 1, t.one_mem, by simp⟩ }, { exact ⟨1, s.one_mem, y, h, by simp⟩ } }, { exact ⟨1, s.one_mem, 1, ⟨t.one_mem, mul_one 1⟩⟩ }, { rintro _ _ ⟨y₁, hy₁, z₁, hz₁, rfl⟩ ⟨y₂, hy₂, z₂, hz₂, rfl⟩, exact ⟨_, mul_mem hy₁ hy₂, _, mul_mem hz₁ hz₂, by simp [mul_assoc]; cc⟩ }, { rintro _ ⟨y, hy, z, hz, rfl⟩, exact ⟨_, inv_mem hy, _, inv_mem hz, mul_comm z y ▸ (mul_inv_rev z y).symm⟩ } end, by rintro ⟨y, hy, z, hz, rfl⟩; exact mul_mem_sup hy hz⟩ @[to_additive] lemma mem_sup' : x ∈ s ⊔ t ↔ ∃ (y : s) (z : t), (y:C) * z = x := mem_sup.trans $ by simp only [set_like.exists, coe_mk] @[to_additive] lemma mem_closure_pair {x y z : C} : z ∈ closure ({x, y} : set C) ↔ ∃ m n : ℤ, x ^ m * y ^ n = z := begin rw [←set.singleton_union, subgroup.closure_union, mem_sup], simp_rw [exists_prop, mem_closure_singleton, exists_exists_eq_and], end @[to_additive] instance : is_modular_lattice (subgroup C) := ⟨λ x y z xz a ha, begin rw [mem_inf, mem_sup] at ha, rcases ha with ⟨⟨b, hb, c, hc, rfl⟩, haz⟩, rw mem_sup, refine ⟨b, hb, c, mem_inf.2 ⟨hc, _⟩, rfl⟩, rw ← inv_mul_cancel_left b c, apply z.mul_mem (z.inv_mem (xz hb)) haz, end⟩ end subgroup section variables (G) (A) /-- A `group` is simple when it has exactly two normal `subgroup`s. -/ class is_simple_group extends nontrivial G : Prop := (eq_bot_or_eq_top_of_normal : ∀ H : subgroup G, H.normal → H = ⊥ ∨ H = ⊤) /-- An `add_group` is simple when it has exactly two normal `add_subgroup`s. -/ class is_simple_add_group extends nontrivial A : Prop := (eq_bot_or_eq_top_of_normal : ∀ H : add_subgroup A, H.normal → H = ⊥ ∨ H = ⊤) attribute [to_additive] is_simple_group variables {G} {A} @[to_additive] lemma subgroup.normal.eq_bot_or_eq_top [is_simple_group G] {H : subgroup G} (Hn : H.normal) : H = ⊥ ∨ H = ⊤ := is_simple_group.eq_bot_or_eq_top_of_normal H Hn namespace is_simple_group @[to_additive] instance {C : Type*} [comm_group C] [is_simple_group C] : is_simple_order (subgroup C) := ⟨λ H, H.normal_of_comm.eq_bot_or_eq_top⟩ open _root_.subgroup @[to_additive] lemma is_simple_group_of_surjective {H : Type*} [group H] [is_simple_group G] [nontrivial H] (f : G →* H) (hf : function.surjective f) : is_simple_group H := ⟨nontrivial.exists_pair_ne, λ H iH, begin refine ((iH.comap f).eq_bot_or_eq_top).imp (λ h, _) (λ h, _), { rw [←map_bot f, ←h, map_comap_eq_self_of_surjective hf] }, { rw [←comap_top f] at h, exact comap_injective hf h } end⟩ end is_simple_group end namespace subgroup section pointwise @[to_additive] lemma closure_mul_le (S T : set G) : closure (S * T) ≤ closure S ⊔ closure T := Inf_le $ λ x ⟨s, t, hs, ht, hx⟩, hx ▸ (closure S ⊔ closure T).mul_mem (set_like.le_def.mp le_sup_left $ subset_closure hs) (set_like.le_def.mp le_sup_right $ subset_closure ht) @[to_additive] lemma sup_eq_closure (H K : subgroup G) : H ⊔ K = closure (H * K) := le_antisymm (sup_le (λ h hh, subset_closure ⟨h, 1, hh, K.one_mem, mul_one h⟩) (λ k hk, subset_closure ⟨1, k, H.one_mem, hk, one_mul k⟩)) (by conv_rhs { rw [← closure_eq H, ← closure_eq K] }; apply closure_mul_le) @[to_additive] private def mul_normal_aux (H N : subgroup G) [hN : N.normal] : subgroup G := { carrier := (H : set G) * N, one_mem' := ⟨1, 1, H.one_mem, N.one_mem, by rw mul_one⟩, mul_mem' := λ a b ⟨h, n, hh, hn, ha⟩ ⟨h', n', hh', hn', hb⟩, ⟨h * h', h'⁻¹ * n * h' * n', H.mul_mem hh hh', N.mul_mem (by simpa using hN.conj_mem _ hn h'⁻¹) hn', by simp [← ha, ← hb, mul_assoc]⟩, inv_mem' := λ x ⟨h, n, hh, hn, hx⟩, ⟨h⁻¹, h * n⁻¹ * h⁻¹, H.inv_mem hh, hN.conj_mem _ (N.inv_mem hn) h, by rw [mul_assoc h, inv_mul_cancel_left, ← hx, mul_inv_rev]⟩ } /-- The carrier of `H ⊔ N` is just `↑H * ↑N` (pointwise set product) when `N` is normal. -/ @[to_additive "The carrier of `H ⊔ N` is just `↑H + ↑N` (pointwise set addition) when `N` is normal."] lemma mul_normal (H N : subgroup G) [N.normal] : (↑(H ⊔ N) : set G) = H * N := set.subset.antisymm (show H ⊔ N ≤ mul_normal_aux H N, by { rw sup_eq_closure, apply Inf_le _, dsimp, refl }) ((sup_eq_closure H N).symm ▸ subset_closure) @[to_additive] private def normal_mul_aux (N H : subgroup G) [hN : N.normal] : subgroup G := { carrier := (N : set G) * H, one_mem' := ⟨1, 1, N.one_mem, H.one_mem, by rw mul_one⟩, mul_mem' := λ a b ⟨n, h, hn, hh, ha⟩ ⟨n', h', hn', hh', hb⟩, ⟨n * (h * n' * h⁻¹), h * h', N.mul_mem hn (hN.conj_mem _ hn' _), H.mul_mem hh hh', by simp [← ha, ← hb, mul_assoc]⟩, inv_mem' := λ x ⟨n, h, hn, hh, hx⟩, ⟨h⁻¹ * n⁻¹ * h, h⁻¹, by simpa using hN.conj_mem _ (N.inv_mem hn) h⁻¹, H.inv_mem hh, by rw [mul_inv_cancel_right, ← mul_inv_rev, hx]⟩ } /-- The carrier of `N ⊔ H` is just `↑N * ↑H` (pointwise set product) when `N` is normal. -/ @[to_additive "The carrier of `N ⊔ H` is just `↑N + ↑H` (pointwise set addition) when `N` is normal."] lemma normal_mul (N H : subgroup G) [N.normal] : (↑(N ⊔ H) : set G) = N * H := set.subset.antisymm (show N ⊔ H ≤ normal_mul_aux N H, by { rw sup_eq_closure, apply Inf_le _, dsimp, refl }) ((sup_eq_closure N H).symm ▸ subset_closure) @[to_additive] lemma mul_inf_assoc (A B C : subgroup G) (h : A ≤ C) : (A : set G) * ↑(B ⊓ C) = (A * B) ⊓ C := begin ext, simp only [coe_inf, set.inf_eq_inter, set.mem_mul, set.mem_inter_iff], split, { rintros ⟨y, z, hy, ⟨hzB, hzC⟩, rfl⟩, refine ⟨_, mul_mem (h hy) hzC⟩, exact ⟨y, z, hy, hzB, rfl⟩ }, rintros ⟨⟨y, z, hy, hz, rfl⟩, hyz⟩, refine ⟨y, z, hy, ⟨hz, _⟩, rfl⟩, suffices : y⁻¹ * (y * z) ∈ C, { simpa }, exact mul_mem (inv_mem (h hy)) hyz end @[to_additive] lemma inf_mul_assoc (A B C : subgroup G) (h : C ≤ A) : ((A ⊓ B : subgroup G) : set G) * C = A ⊓ (B * C) := begin ext, simp only [coe_inf, set.inf_eq_inter, set.mem_mul, set.mem_inter_iff], split, { rintros ⟨y, z, ⟨hyA, hyB⟩, hz, rfl⟩, refine ⟨A.mul_mem hyA (h hz), _⟩, exact ⟨y, z, hyB, hz, rfl⟩ }, rintros ⟨hyz, y, z, hy, hz, rfl⟩, refine ⟨y, z, ⟨_, hy⟩, hz, rfl⟩, suffices : (y * z) * z⁻¹ ∈ A, { simpa }, exact mul_mem hyz (inv_mem (h hz)) end end pointwise section subgroup_normal @[to_additive] lemma normal_subgroup_of_iff {H K : subgroup G} (hHK : H ≤ K) : (H.subgroup_of K).normal ↔ ∀ h k, h ∈ H → k ∈ K → k * h * k⁻¹ ∈ H := ⟨λ hN h k hH hK, hN.conj_mem ⟨h, hHK hH⟩ hH ⟨k, hK⟩, λ hN, { conj_mem := λ h hm k, (hN h.1 k.1 hm k.2) }⟩ @[to_additive] instance prod_subgroup_of_prod_normal {H₁ K₁ : subgroup G} {H₂ K₂ : subgroup N} [h₁ : (H₁.subgroup_of K₁).normal] [h₂ : (H₂.subgroup_of K₂).normal] : ((H₁.prod H₂).subgroup_of (K₁.prod K₂)).normal := { conj_mem := λ n hgHK g, ⟨h₁.conj_mem ⟨(n : G × N).fst, (mem_prod.mp n.2).1⟩ hgHK.1 ⟨(g : G × N).fst, (mem_prod.mp g.2).1⟩, h₂.conj_mem ⟨(n : G × N).snd, (mem_prod.mp n.2).2⟩ hgHK.2 ⟨(g : G × N).snd, (mem_prod.mp g.2).2⟩⟩ } @[to_additive] instance prod_normal (H : subgroup G) (K : subgroup N) [hH : H.normal] [hK : K.normal] : (H.prod K).normal := { conj_mem := λ n hg g, ⟨hH.conj_mem n.fst (subgroup.mem_prod.mp hg).1 g.fst, hK.conj_mem n.snd (subgroup.mem_prod.mp hg).2 g.snd⟩ } @[to_additive] lemma inf_subgroup_of_inf_normal_of_right (A B' B : subgroup G) (hB : B' ≤ B) [hN : (B'.subgroup_of B).normal] : ((A ⊓ B').subgroup_of (A ⊓ B)).normal := { conj_mem := λ n hn g, ⟨mul_mem (mul_mem (mem_inf.1 g.2).1 (mem_inf.1 n.2).1) (inv_mem (mem_inf.1 g.2).1), (normal_subgroup_of_iff hB).mp hN n g hn.2 (mem_inf.mp g.2).2⟩ } @[to_additive] lemma inf_subgroup_of_inf_normal_of_left {A' A : subgroup G} (B : subgroup G) (hA : A' ≤ A) [hN : (A'.subgroup_of A).normal] : ((A' ⊓ B).subgroup_of (A ⊓ B)).normal := { conj_mem := λ n hn g, ⟨(normal_subgroup_of_iff hA).mp hN n g hn.1 (mem_inf.mp g.2).1, mul_mem (mul_mem (mem_inf.1 g.2).2 (mem_inf.1 n.2).2) (inv_mem (mem_inf.1 g.2).2)⟩ } instance sup_normal (H K : subgroup G) [hH : H.normal] [hK : K.normal] : (H ⊔ K).normal := { conj_mem := λ n hmem g, begin change n ∈ ↑(H ⊔ K) at hmem, change g * n * g⁻¹ ∈ ↑(H ⊔ K), rw [normal_mul, set.mem_mul] at *, rcases hmem with ⟨h, k, hh, hk, rfl⟩, refine ⟨g * h * g⁻¹, g * k * g⁻¹, hH.conj_mem h hh g, hK.conj_mem k hk g, _⟩, simp end } @[to_additive] instance normal_inf_normal (H K : subgroup G) [hH : H.normal] [hK : K.normal] : (H ⊓ K).normal := { conj_mem := λ n hmem g, by { rw mem_inf at *, exact ⟨hH.conj_mem n hmem.1 g, hK.conj_mem n hmem.2 g⟩ } } @[to_additive] lemma subgroup_of_sup (A A' B : subgroup G) (hA : A ≤ B) (hA' : A' ≤ B) : (A ⊔ A').subgroup_of B = A.subgroup_of B ⊔ A'.subgroup_of B := begin refine map_injective_of_ker_le B.subtype (ker_le_comap _ _) (le_trans (ker_le_comap B.subtype _) le_sup_left) _, { simp only [subgroup_of, map_comap_eq, map_sup, subtype_range], rw [inf_of_le_right (sup_le hA hA'), inf_of_le_right hA', inf_of_le_right hA] }, end @[to_additive] lemma subgroup_normal.mem_comm {H K : subgroup G} (hK : H ≤ K) [hN : (H.subgroup_of K).normal] {a b : G} (hb : b ∈ K) (h : a * b ∈ H) : b * a ∈ H := begin have := (normal_subgroup_of_iff hK).mp hN (a * b) b h hb, rwa [mul_assoc, mul_assoc, mul_right_inv, mul_one] at this, end /-- Elements of disjoint, normal subgroups commute. -/ @[to_additive "Elements of disjoint, normal subgroups commute."] lemma commute_of_normal_of_disjoint (H₁ H₂ : subgroup G) (hH₁ : H₁.normal) (hH₂ : H₂.normal) (hdis : disjoint H₁ H₂) (x y : G) (hx : x ∈ H₁) (hy : y ∈ H₂) : commute x y := begin suffices : x * y * x⁻¹ * y⁻¹ = 1, { show x * y = y * x, by { rw [mul_assoc, mul_eq_one_iff_eq_inv] at this, simpa } }, apply hdis, split, { suffices : x * (y * x⁻¹ * y⁻¹) ∈ H₁, by simpa [mul_assoc], exact H₁.mul_mem hx (hH₁.conj_mem _ (H₁.inv_mem hx) _) }, { show x * y * x⁻¹ * y⁻¹ ∈ H₂, apply H₂.mul_mem _ (H₂.inv_mem hy), apply (hH₂.conj_mem _ hy), } end end subgroup_normal @[to_additive] lemma disjoint_def {H₁ H₂ : subgroup G} : disjoint H₁ H₂ ↔ ∀ {x : G}, x ∈ H₁ → x ∈ H₂ → x = 1 := show (∀ x, x ∈ H₁ ∧ x ∈ H₂ → x ∈ ({1} : set G)) ↔ _, by simp @[to_additive] lemma disjoint_def' {H₁ H₂ : subgroup G} : disjoint H₁ H₂ ↔ ∀ {x y : G}, x ∈ H₁ → y ∈ H₂ → x = y → x = 1 := disjoint_def.trans ⟨λ h x y hx hy hxy, h hx $ hxy.symm ▸ hy, λ h x hx hx', h hx hx' rfl⟩ @[to_additive] lemma disjoint_iff_mul_eq_one {H₁ H₂ : subgroup G} : disjoint H₁ H₂ ↔ ∀ {x y : G}, x ∈ H₁ → y ∈ H₂ → x * y = 1 → x = 1 ∧ y = 1 := disjoint_def'.trans ⟨λ h x y hx hy hxy, let hx1 : x = 1 := h hx (H₂.inv_mem hy) (eq_inv_iff_mul_eq_one.mpr hxy) in ⟨hx1, by simpa [hx1] using hxy⟩, λ h x y hx hy hxy, (h hx (H₂.inv_mem hy) (mul_inv_eq_one.mpr hxy)).1 ⟩ /-- `finset.noncomm_prod` is “injective” in `f` if `f` maps into independent subgroups. This generalizes (one direction of) `subgroup.disjoint_iff_mul_eq_one`. -/ @[to_additive "`finset.noncomm_sum` is “injective” in `f` if `f` maps into independent subgroups. This generalizes (one direction of) `add_subgroup.disjoint_iff_add_eq_zero`. "] lemma eq_one_of_noncomm_prod_eq_one_of_independent {ι : Type*} (s : finset ι) (f : ι → G) (comm : ∀ (x ∈ s) (y ∈ s), commute (f x) (f y)) (K : ι → subgroup G) (hind : complete_lattice.independent K) (hmem : ∀ (x ∈ s), f x ∈ K x) (heq1 : s.noncomm_prod f comm = 1) : ∀ (i ∈ s), f i = 1 := begin classical, revert heq1, induction s using finset.induction_on with i s hnmem ih, { simp, }, { simp only [finset.forall_mem_insert] at comm hmem, specialize ih (λ x hx, (comm.2 x hx).2) hmem.2, have hmem_bsupr: s.noncomm_prod f (λ x hx, (comm.2 x hx).2) ∈ ⨆ (i ∈ (s : set ι)), K i, { refine subgroup.noncomm_prod_mem _ _ _, intros x hx, have : K x ≤ ⨆ (i ∈ (s : set ι)), K i := le_supr₂ x hx, exact this (hmem.2 x hx), }, intro heq1, rw finset.noncomm_prod_insert_of_not_mem _ _ _ _ hnmem at heq1, have hnmem' : i ∉ (s : set ι), by simpa, obtain ⟨heq1i : f i = 1, heq1S : s.noncomm_prod f _ = 1⟩ := subgroup.disjoint_iff_mul_eq_one.mp (hind.disjoint_bsupr hnmem') hmem.1 hmem_bsupr heq1, specialize ih heq1S, intros i h, simp only [finset.mem_insert] at h, rcases h with ⟨rfl | _⟩, { exact heq1i }, { exact (ih _ h), } } end end subgroup namespace is_conj open subgroup lemma normal_closure_eq_top_of {N : subgroup G} [hn : N.normal] {g g' : G} {hg : g ∈ N} {hg' : g' ∈ N} (hc : is_conj g g') (ht : normal_closure ({⟨g, hg⟩} : set N) = ⊤) : normal_closure ({⟨g', hg'⟩} : set N) = ⊤ := begin obtain ⟨c, rfl⟩ := is_conj_iff.1 hc, have h : ∀ x : N, (mul_aut.conj c) x ∈ N, { rintro ⟨x, hx⟩, exact hn.conj_mem _ hx c }, have hs : function.surjective (((mul_aut.conj c).to_monoid_hom.restrict N).cod_restrict _ h), { rintro ⟨x, hx⟩, refine ⟨⟨c⁻¹ * x * c, _⟩, _⟩, { have h := hn.conj_mem _ hx c⁻¹, rwa [inv_inv] at h }, simp only [monoid_hom.cod_restrict_apply, mul_equiv.coe_to_monoid_hom, mul_aut.conj_apply, coe_mk, monoid_hom.restrict_apply, subtype.mk_eq_mk, ← mul_assoc, mul_inv_self, one_mul], rw [mul_assoc, mul_inv_self, mul_one] }, have ht' := map_mono (eq_top_iff.1 ht), rw [← monoid_hom.range_eq_map, monoid_hom.range_top_of_surjective _ hs] at ht', refine eq_top_iff.2 (le_trans ht' (map_le_iff_le_comap.2 (normal_closure_le_normal _))), rw [set.singleton_subset_iff, set_like.mem_coe], simp only [monoid_hom.cod_restrict_apply, mul_equiv.coe_to_monoid_hom, mul_aut.conj_apply, coe_mk, monoid_hom.restrict_apply, mem_comap], exact subset_normal_closure (set.mem_singleton _), end end is_conj /-! ### Actions by `subgroup`s These are just copies of the definitions about `submonoid` starting from `submonoid.mul_action`. -/ section actions namespace subgroup variables {α β : Type*} /-- The action by a subgroup is the action by the underlying group. -/ @[to_additive /-"The additive action by an add_subgroup is the action by the underlying add_group. "-/] instance [mul_action G α] (S : subgroup G) : mul_action S α := S.to_submonoid.mul_action @[to_additive] lemma smul_def [mul_action G α] {S : subgroup G} (g : S) (m : α) : g • m = (g : G) • m := rfl @[to_additive] instance smul_comm_class_left [mul_action G β] [has_smul α β] [smul_comm_class G α β] (S : subgroup G) : smul_comm_class S α β := S.to_submonoid.smul_comm_class_left @[to_additive] instance smul_comm_class_right [has_smul α β] [mul_action G β] [smul_comm_class α G β] (S : subgroup G) : smul_comm_class α S β := S.to_submonoid.smul_comm_class_right /-- Note that this provides `is_scalar_tower S G G` which is needed by `smul_mul_assoc`. -/ instance [has_smul α β] [mul_action G α] [mul_action G β] [is_scalar_tower G α β] (S : subgroup G) : is_scalar_tower S α β := S.to_submonoid.is_scalar_tower instance [mul_action G α] [has_faithful_smul G α] (S : subgroup G) : has_faithful_smul S α := S.to_submonoid.has_faithful_smul /-- The action by a subgroup is the action by the underlying group. -/ instance [add_monoid α] [distrib_mul_action G α] (S : subgroup G) : distrib_mul_action S α := S.to_submonoid.distrib_mul_action /-- The action by a subgroup is the action by the underlying group. -/ instance [monoid α] [mul_distrib_mul_action G α] (S : subgroup G) : mul_distrib_mul_action S α := S.to_submonoid.mul_distrib_mul_action /-- The center of a group acts commutatively on that group. -/ instance center.smul_comm_class_left : smul_comm_class (center G) G G := submonoid.center.smul_comm_class_left /-- The center of a group acts commutatively on that group. -/ instance center.smul_comm_class_right : smul_comm_class G (center G) G := submonoid.center.smul_comm_class_right end subgroup end actions /-! ### Mul-opposite subgroups -/ section mul_opposite namespace subgroup /-- A subgroup `H` of `G` determines a subgroup `H.opposite` of the opposite group `Gᵐᵒᵖ`. -/ @[to_additive "An additive subgroup `H` of `G` determines an additive subgroup `H.opposite` of the opposite additive group `Gᵃᵒᵖ`."] def opposite (H : subgroup G) : subgroup Gᵐᵒᵖ := { carrier := mul_opposite.unop ⁻¹' (H : set G), one_mem' := H.one_mem, mul_mem' := λ a b ha hb, H.mul_mem hb ha, inv_mem' := λ a, H.inv_mem } /-- Bijection between a subgroup `H` and its opposite. -/ @[to_additive "Bijection between an additive subgroup `H` and its opposite.", simps] def opposite_equiv (H : subgroup G) : H ≃ H.opposite := mul_opposite.op_equiv.subtype_equiv $ λ _, iff.rfl @[to_additive] instance (H : subgroup G) [encodable H] : encodable H.opposite := encodable.of_equiv H H.opposite_equiv.symm @[to_additive] lemma smul_opposite_mul {H : subgroup G} (x g : G) (h : H.opposite) : h • (g * x) = g * (h • x) := begin cases h, simp [(•), mul_assoc], end @[to_additive] lemma smul_opposite_image_mul_preimage {H : subgroup G} (g : G) (h : H.opposite) (s : set G) : (λ y, h • y) '' (has_mul.mul g ⁻¹' s) = has_mul.mul g ⁻¹' ((λ y, h • y) '' s) := by { ext x, cases h, simp [(•), mul_assoc] } end subgroup end mul_opposite /-! ### Saturated subgroups -/ section saturated namespace subgroup /-- A subgroup `H` of `G` is *saturated* if for all `n : ℕ` and `g : G` with `g^n ∈ H` we have `n = 0` or `g ∈ H`. -/ @[to_additive "An additive subgroup `H` of `G` is *saturated* if for all `n : ℕ` and `g : G` with `n•g ∈ H` we have `n = 0` or `g ∈ H`."] def saturated (H : subgroup G) : Prop := ∀ ⦃n g⦄, g ^ n ∈ H → n = 0 ∨ g ∈ H @[to_additive] lemma saturated_iff_npow {H : subgroup G} : saturated H ↔ (∀ (n : ℕ) (g : G), g ^ n ∈ H → n = 0 ∨ g ∈ H) := iff.rfl @[to_additive] lemma saturated_iff_zpow {H : subgroup G} : saturated H ↔ (∀ (n : ℤ) (g : G), g ^ n ∈ H → n = 0 ∨ g ∈ H) := begin split, { rintros hH ⟨n⟩ g hgn, { simp only [int.coe_nat_eq_zero, int.of_nat_eq_coe, zpow_coe_nat] at hgn ⊢, exact hH hgn }, { suffices : g ^ (n+1) ∈ H, { refine (hH this).imp _ id, simp only [is_empty.forall_iff, nat.succ_ne_zero], }, simpa only [inv_mem_iff, zpow_neg_succ_of_nat] using hgn, } }, { intros h n g hgn, specialize h n g, simp only [int.coe_nat_eq_zero, zpow_coe_nat] at h, apply h hgn } end end subgroup namespace add_subgroup lemma ker_saturated {A₁ A₂ : Type*} [add_comm_group A₁] [add_comm_group A₂] [no_zero_smul_divisors ℕ A₂] (f : A₁ →+ A₂) : (f.ker).saturated := begin intros n g hg, simpa only [f.mem_ker, nsmul_eq_smul, f.map_nsmul, smul_eq_zero] using hg end end add_subgroup end saturated
2f9e76348365253415977cd61dcea3c2635476a9
e151e9053bfd6d71740066474fc500a087837323
/src/hott/homotopy/connectedness.lean
5ece54232522718db26af7a67bb1e326d90681da
[ "Apache-2.0" ]
permissive
daniel-carranza/hott3
15bac2d90589dbb952ef15e74b2837722491963d
913811e8a1371d3a5751d7d32ff9dec8aa6815d9
refs/heads/master
1,610,091,349,670
1,596,222,336,000
1,596,222,336,000
241,957,822
0
0
Apache-2.0
1,582,222,839,000
1,582,222,838,000
null
UTF-8
Lean
false
false
23,932
lean
/- 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 universes u v w hott_theory namespace hott open is_trunc hott.is_equiv hott.nat nat hott.equiv trunc hott.function fiber hott.funext hott.pi pointed @[hott, reducible] def is_conn (n : ℕ₋₂) (A : Type _) : Type _ := is_contr (trunc n A) @[hott, reducible] def is_conn_fun (n : ℕ₋₂) {A B : Type _} (f : A → B) : Type _ := Πb : B, is_conn n (fiber f b) @[hott, reducible] def is_conn_inf (A : Type _) : Type _ := Πn, is_conn n A @[hott, reducible] def is_conn_fun_inf {A B : Type _} (f : A → B) : Type _ := Πn, is_conn_fun n f namespace is_conn @[hott] def is_conn_equiv_closed (n : ℕ₋₂) {A B : Type _} : A ≃ B → is_conn n A → is_conn n B := begin intros H C, napply @is_contr_equiv_closed (trunc n A) _, exact trunc_equiv_trunc n H, infer end @[hott] theorem is_conn_of_le (A : Type _) {n k : ℕ₋₂} (H : n ≤ k) [is_conn k A] : is_conn n A := begin napply is_contr_equiv_closed, apply trunc_trunc_equiv_left _ H, apply_instance end @[hott] 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 @[hott] def 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 : Type _} {B : Type _} {h : A → B} (H : is_conn_fun n h) (P : B → Type _) [Πb, is_trunc n (P b)] @[hott] private def 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)) @[hott] private def 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 (@[hott] lemma 7.5.7) @[hott] protected def 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, dsimp [rec.g, rec.helper], rwr [@center_eq _ (H (h a)) (tr (fiber.mk a idp))], end begin intro k, apply eq_of_homotopy, intro b, dsimp [rec.g], hinduction @center _ (H b) with q p, hinduction p with a p, induction p, refl end @[hott] protected def elim : (Πa : A, P (h a)) → (Πb : B, P b) := @is_equiv.inv _ _ (λ(s : Πb : B, P b) a, s (h a)) rec @[hott] protected def elim_β : Πf : (Πa : A, P (h a)), Πa : A, elim f (h a) = f a := λf, apd10 (@is_equiv.right_inv _ _ (λ(s : Πb : B, P b) a, s (h a)) rec f) end section parameters (n k : ℕ₋₂) {A : Type _} {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 -- @[hott] lemma 8.6.1 @[hott] lemma elim_general : is_trunc_fun k (pi_functor_left f P) := begin unfreezeI, revert P HP, induction k with k IH; intros P HP t, { napply is_contr_fiber_of_is_equiv, napply is_conn_fun.rec, exact H, exact HP }, { napply 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 : Πb, P b, (λ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 : Πb, P b) a, v (f a)) (eq_of_homotopy r))), rwr [←apd10_eq_of_homotopy r], rwr [←(apd10_ap_precompose_dependent f (eq_of_homotopy r)), apd10_eq_of_homotopy r], symmetry, apply eq_equiv_fn_eq (@apd10 A (λa, P (f a)) (λa, g (f a)) (λa, h (f a))) end, refine sigma.sigma_equiv_sigma_right e' ⬝e _, clear e', refine (sigma.sigma_equiv_sigma_left (λr : g = h, ap _ r ⬝ q = p) (eq_equiv_homotopy _ _))⁻¹ᵉ ⬝e _, symmetry, 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, exact @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 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)) @[hott] private abbreviation s := sec (λb, trunctype.mk' n (trunc n (fiber h b))) include sec -- the other half of @[hott] lemma 7.5.7 @[hott] def intro : is_conn_fun n h := begin intro b, apply is_contr.mk (@is_retraction.sect _ _ _ (s n sec) (λa, tr (fiber.mk a idp)) b), intro x, dsimp at x, hinduction x with p, hinduction p with a p, apply transport (λz : (Σy, h a = y), @sect _ _ _ (s n sec) (λa, tr (mk a idp)) (sigma.fst z) = tr (fiber.mk a (sigma.snd z))) (@center_eq _ (is_contr_sigma_eq (h a)) (sigma.mk b p)), exact apd10 (@hott.function.right_inverse _ _ _ (s n sec) (λ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 _) @[hott] def is_conn_of_map_to_unit : is_conn_fun n (const A unit.star) → is_conn n A := begin intro H, dsimp [is_conn_fun] at H, exact is_conn_equiv_closed n (fiber.fiber_star_equiv A) (by infer), end @[hott] def 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)⁻¹ᵉ (by infer), end -- now maps from unit @[hott] def 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 @[hott] def 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 @[hott] protected def 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)))) @[hott] protected def elim : P (Point A) → (Πa : A, P a) := @is_equiv.inv _ _ (λ(s : Πa : A, P a), s (Point A)) rec @[hott] protected def elim_β (p : P (Point A)) : elim p (Point A) = p := @is_equiv.right_inv _ _ (λ(s : Πa : A, P a), 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 @[hott] lemma elim_general (p : P (Point A)) : is_trunc k (fiber (λs : (Πa : A, P a), s (Point A)) p) := @is_trunc_equiv_closed _ _ 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 -- @[hott] lemma 7.5.2 @[hott] def 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 @[hott] def 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 @[hott] def merely_of_minus_one_conn {A : Type _} : is_conn -1 A → ∥A∥ := λH, @center (∥A∥) H @[hott] def 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 hott.arrow variables {f : arrow} {g : arrow} -- @[hott] lemma 7.5.4 @[hott, instance] def retract_of_conn_is_conn (r : arrow_hom f g) [H : arrow.is_retraction r] (n : ℕ₋₂) [K : is_conn_fun n f] : is_conn_fun n g := begin intro b, dsimp [is_conn], apply is_contr_retract (trunc_functor n (retraction_on_fiber r b)), exact K (r.sect.on_cod b) end end -- Corollary 7.5.5 @[hott] def 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 @[hott] def is_conn_minus_two (A : Type _) : is_conn -2 A := by apply_instance -- merely inhabited types are -1-connected @[hott] def is_conn_minus_one (A : Type _) (a : ∥ A ∥) : is_conn -1 A := is_contr.mk a (is_prop.elim _) @[hott, instance] def is_conn_minus_one_pointed (A : Type*) : is_conn -1 A := is_conn_minus_one A (tr pt) @[hott, instance] def is_conn_trunc (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, apply_instance end @[hott, instance] def is_conn_eq (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, apply_instance end @[hott, instance] def is_conn_loop (n : ℕ₋₂) (A : Type*) [is_conn (n.+1) A] : is_conn n (Ω A) := is_conn_eq n pt pt open pointed @[hott, instance] def is_conn_ptrunc (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 @[hott] def is_conn_of_is_contr (k : ℕ₋₂) (A : Type _) [is_contr A] : is_conn k A := by apply_instance @[hott] def is_conn_fun_of_is_equiv (k : ℕ₋₂) {A B : Type _} (f : A → B) [is_equiv f] : is_conn_fun k f := by apply_instance -- @[hott] lemma 7.5.14 @[hott, instance] theorem is_equiv_trunc_functor_of_is_conn_fun {A B : Type _} (n : ℕ₋₂) (f : A → B) [H : is_conn_fun n f] : is_equiv (trunc_functor n f) := begin fapply adjointify, { intro b, hinduction b with b, exact trunc_functor n point (center (trunc n (fiber f b)))}, { intro b, hinduction b with b, dsimp, hinduction center (trunc n (fiber f b)) with q v, hinduction v with a p, exact ap tr p }, { intro a, hinduction a with a, dsimp, rwr [center_eq (tr (fiber.mk a idp))]} end @[hott] 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) @[hott] def 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, haveI : Πb, is_trunc n (P b), from λb, is_trunc_of_le _ H, fconstructor, { intros f' b, resetI, hinduction 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, dsimp, resetI, hinduction a using trunc.rec with a, dsimp, rwr [is_conn_fun.elim_β] } end @[hott] def 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 napply is_conn_fun_of_is_equiv, apply is_equiv_trunc_functor_of_le f H end -- Exercise 7.18 @[hott] def 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 hott.ulift @[hott] def is_conn_fun_lift_functor (n : ℕ₋₂) {A B : Type _} (f : A → B) [is_conn_fun n f] : is_conn_fun n (ulift_functor f) := begin intro b, cases b with b, apply is_trunc_equiv_closed_rev, { apply trunc_equiv_trunc, apply fiber_ulift_functor }, apply_instance end open trunc_index @[hott] def 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, { apply_instance }, cases n with n, { have : -1 ≤ ↑0, from dec_star, exact is_conn_fun_of_le f this }, change is_conn_fun (n.+2) f, rwr [←of_nat_add_two n], apply_instance end @[hott] def is_conn_inf.mk_nat {A : Type _} (H : Π(n : ℕ), is_conn n A) : is_conn_inf A := begin intro n, cases n with n, { apply_instance }, cases n with n, { have : -1 ≤ ↑0, from dec_star, apply is_conn_of_le A this}, change is_conn (n.+2) A, rwr ←of_nat_add_two, apply_instance end @[hott] def 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⁻¹ᵉ (by apply_instance) @[hott] def 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 napply 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 _ _ _)⁻¹ᵉ, apply_instance }, exact a end @[hott] def 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 _ _ _) (by apply_instance) open hott.sigma @[hott, instance] lemma is_conn_sigma {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 unfreezeI, induction n with n IH generalizing A B HA HB, { apply is_conn_minus_two }, apply is_conn_succ_intro, { resetI, hinduction center (trunc (n.+1) A) with q a, hinduction center (trunc (n.+1) (B a)) with q' b, exact tr ⟨a, b⟩ }, intros a a', refine is_conn_equiv_closed_rev n (sigma_eq_equiv _ _) _, napply 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 @[hott, instance] lemma is_conn_prod (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 _ _) (by apply_instance) @[hott] 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 _ _) (by apply_instance) @[hott] 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 @[hott] def 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, { intros f' b, refine @is_conn_fun.elim k _ _ _ H2 _ _ _ b, intro a, exact f' (tr a) }, { intro f', apply eq_of_homotopy, intro a, resetI, hinduction a with a, dsimp, rwr [is_conn_fun.elim_β] } end @[hott] def 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 napply 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, dsimp, hinduction x, refl, infer end @[hott] def 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 @[hott] 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, { intros f' b, hinduction b with a, exact f' a }, { intro f', reflexivity } end @[hott] def 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 -/ @[hott] structure conntype (n : ℕ₋₂) := (carrier : Type _) (struct : is_conn n carrier) notation `Type[`:95 n:0 `]`:0 := conntype n @[hott] instance conntype_coe (n): has_coe_to_sort (conntype n) := ⟨Type _ , conntype.carrier⟩ attribute [instance] [priority 1300] conntype.struct section structure pconntype (n : ℕ₋₂) := (pcarrier : Type*) (struct : is_conn n pcarrier) 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. -/ @[hott] instance pType_of_pconntype (n : ℕ₋₂) : has_coe Type*[n] Type* := ⟨λx, x.pcarrier⟩ @[hott] instance is_conn_pconntype {n : ℕ₋₂} (X : Type*[n]) : is_conn n X := X.struct @[hott] def pconntype.to_conntype {n : ℕ₋₂} (X : Type*[n]) : Type[n] := ⟨X, X.struct⟩ @[hott] structure truncconntype (n k : ℕ₋₂) := (carrier : Type u) (struct : is_trunc n carrier) (conn_struct : is_conn k carrier) notation n `-Type[`:95 k:0 `]`:0 := truncconntype n k @[hott] instance truncconntype_to_trunctype (n k : ℕ₋₂) : has_coe (n-Type[k]) (n-Type) := ⟨λX, ⟨X.carrier, X.struct⟩⟩ @[hott] instance truncconntype_to_conntype (n k : ℕ₋₂) : has_coe (n-Type[k]) (Type[k]) := ⟨λX, ⟨X.carrier, X.conn_struct⟩⟩ @[hott, reducible] def truncconntype.to_trunctype {n k : ℕ₋₂} (X : truncconntype.{u} n k) : trunctype.{u} n := ↑X @[hott, reducible] def truncconntype.to_conntype {n k : ℕ₋₂} (X : truncconntype.{u} n k) : conntype.{u} k := ↑X @[hott] def is_conn_truncconntype {n k : ℕ₋₂} (X : n-Type[k]) : is_conn k X := by apply_instance @[hott, instance] def is_trunc_truncconntype {n k : ℕ₋₂} (X : n-Type[k]) : is_trunc n X := X.struct @[hott] def is_trunc_truncconntype' {n k : ℕ₋₂} (X : n-Type[k]) : is_trunc n (X.to_trunctype) := by apply_instance @[hott, instance] def is_conn_truncconntype' {n k : ℕ₋₂} (X : n-Type[k]) : is_conn k (X.to_trunctype) := X.conn_struct @[hott, instance] def is_trunc_truncconntype'' {n k : ℕ₋₂} (X : n-Type[k]) : is_trunc n (X.to_conntype) := X.struct @[hott] def is_conn_truncconntype'' {n k : ℕ₋₂} (X : n-Type[k]) : is_conn k (X.to_conntype) := by apply_instance @[hott] structure ptruncconntype (n k : ℕ₋₂) := (pcarrier : Type*) (struct : is_trunc n pcarrier) (conn_struct : is_conn k pcarrier) notation n `-Type*[`:95 k:0 `]`:0 := ptruncconntype n k @[hott] instance ptruncconntype_to_ptrunctype (n k : ℕ₋₂) : has_coe (n-Type*[k]) (n-Type*) := ⟨λX, ⟨X.pcarrier, X.struct⟩⟩ @[hott] instance ptruncconntype_to_pconntype (n k : ℕ₋₂) : has_coe (n-Type*[k]) (Type*[k]) := ⟨λX, ⟨X.pcarrier, X.conn_struct⟩⟩ @[hott] instance ptruncconntype_to_truncconntype (n k : ℕ₋₂) : has_coe (n-Type*[k]) (n-Type[k]) := ⟨λX, ⟨X, X.struct, X.conn_struct⟩⟩ @[hott, reducible] def ptruncconntype.to_ptrunctype {n k : ℕ₋₂} (X : ptruncconntype.{u} n k) : ptrunctype.{u} n := X @[hott, reducible] def ptruncconntype.to_pconntype {n k : ℕ₋₂} (X : ptruncconntype.{u} n k) : pconntype.{u} k := X @[hott, reducible] def ptruncconntype.to_truncconntype {n k : ℕ₋₂} (X : ptruncconntype.{u} n k) : truncconntype.{u} n k := X @[hott, reducible] def ptruncconntype.to_pType {n k : ℕ₋₂} (X : ptruncconntype.{u} n k) : pType.{u} := X @[hott, reducible] def ptruncconntype.to_conntype {n k : ℕ₋₂} (X : ptruncconntype.{u} n k) : conntype.{u} k := X @[hott, reducible] def ptruncconntype.to_trunctype {n k : ℕ₋₂} (X : ptruncconntype.{u} n k) : trunctype.{u} n := X @[hott] def is_conn_ptruncconntype {n k : ℕ₋₂} (X : n-Type*[k]) : is_conn k X := by apply_instance @[hott] def is_trunc_ptruncconntype {n k : ℕ₋₂} (X : n-Type*[k]) : is_trunc n X := by apply_instance @[hott] def is_trunc_ptruncconntype' {n k : ℕ₋₂} (X : n-Type*[k]) : is_trunc n (X.to_ptrunctype) := by apply_instance @[hott, instance] def is_conn_ptruncconntype' {n k : ℕ₋₂} (X : n-Type*[k]) : is_conn k (X.to_ptrunctype) := X.conn_struct @[hott, hsimp] def ptruncconntype.to_pType_mk {n k : ℕ₋₂} (X : Type*) (H : is_trunc n X) (H' : is_conn k X) : ptruncconntype.to_pType ⟨X, H, H'⟩ = X := by refl @[hott] def ptruncconntype_eq {n k : ℕ₋₂} {X Y : n-Type*[k]} (p : X.to_pType ≃* Y.to_pType) : X = Y := begin induction X with X Xt Xp Xc, induction Y with Y Yt Yp Yc, dsimp at p, hinduction eq_of_pequiv p, exact ap011 (ptruncconntype.mk X) (is_prop.elim _ _) (is_prop.elim _ _) end end end hott
7fc51f6ac33540ed474e9b9d25d902504ada8d84
947b78d97130d56365ae2ec264df196ce769371a
/src/Lean/Compiler/IR/ElimDeadVars.lean
c88015e3bcb9073a17058777ed8cb76d6672974c
[ "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
1,671
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.Compiler.IR.Basic import Lean.Compiler.IR.FreeVars namespace Lean namespace IR partial def reshapeWithoutDeadAux : Array FnBody → FnBody → IndexSet → FnBody | bs, b, used => if bs.isEmpty then b else let curr := bs.back; let bs := bs.pop; let keep (_ : Unit) := let used := curr.collectFreeIndices used; let b := curr.setBody b; reshapeWithoutDeadAux bs b used; let keepIfUsed (vidx : Index) := if used.contains vidx then keep () else reshapeWithoutDeadAux bs b used; match curr with | FnBody.vdecl x _ _ _ => keepIfUsed x.idx -- TODO: we should keep all struct/union projections because they are used to ensure struct/union values are fully consumed. | FnBody.jdecl j _ _ _ => keepIfUsed j.idx | _ => keep () def reshapeWithoutDead (bs : Array FnBody) (term : FnBody) : FnBody := reshapeWithoutDeadAux bs term term.freeIndices partial def FnBody.elimDead : FnBody → FnBody | b => let (bs, term) := b.flatten; let bs := modifyJPs bs FnBody.elimDead; let term := match term with | FnBody.case tid x xType alts => let alts := alts.map $ fun alt => alt.modifyBody FnBody.elimDead; FnBody.case tid x xType alts | other => other; reshapeWithoutDead bs term /-- Eliminate dead let-declarations and join points -/ def Decl.elimDead : Decl → Decl | Decl.fdecl f xs t b => Decl.fdecl f xs t b.elimDead | other => other end IR end Lean
f8ddbd9471c44165a2d5e43afbd189869b0be118
b3fced0f3ff82d577384fe81653e47df68bb2fa1
/src/algebra/big_operators.lean
9c99e1633c1e21b8387d967a46720f30fdf39d7e
[ "Apache-2.0" ]
permissive
ratmice/mathlib
93b251ef5df08b6fd55074650ff47fdcc41a4c75
3a948a6a4cd5968d60e15ed914b1ad2f4423af8d
refs/heads/master
1,599,240,104,318
1,572,981,183,000
1,572,981,183,000
219,830,178
0
0
Apache-2.0
1,572,980,897,000
1,572,980,896,000
null
UTF-8
Lean
false
false
34,078
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl Some big operators for lists and finite sets. -/ import tactic.tauto data.list.basic data.finset data.nat.enat import algebra.group algebra.ordered_group algebra.group_power universes u v w variables {α : Type u} {β : Type v} {γ : Type w} theorem directed.finset_le {r : α → α → Prop} [is_trans α r] {ι} (hι : nonempty ι) {f : ι → α} (D : directed r f) (s : finset ι) : ∃ z, ∀ i ∈ s, r (f i) (f z) := show ∃ z, ∀ i ∈ s.1, r (f i) (f z), from multiset.induction_on s.1 (let ⟨z⟩ := hι in ⟨z, λ _, false.elim⟩) $ λ i s ⟨j, H⟩, let ⟨k, h₁, h₂⟩ := D i j in ⟨k, λ a h, or.cases_on (multiset.mem_cons.1 h) (λ h, h.symm ▸ h₁) (λ h, trans (H _ h) h₂)⟩ theorem finset.exists_le {α : Type u} [nonempty α] [directed_order α] (s : finset α) : ∃ M, ∀ i ∈ s, i ≤ M := directed.finset_le (by apply_instance) directed_order.directed s namespace finset variables {s s₁ s₂ : finset α} {a : α} {f g : α → β} /-- `prod s f` is the product of `f x` as `x` ranges over the elements of the finite set `s`. -/ @[to_additive] protected def prod [comm_monoid β] (s : finset α) (f : α → β) : β := (s.1.map f).prod @[to_additive] lemma prod_eq_multiset_prod [comm_monoid β] (s : finset α) (f : α → β) : s.prod f = (s.1.map f).prod := rfl @[to_additive] theorem prod_eq_fold [comm_monoid β] (s : finset α) (f : α → β) : s.prod f = s.fold (*) 1 f := rfl section comm_monoid variables [comm_monoid β] @[simp, to_additive] lemma prod_empty {α : Type u} {f : α → β} : (∅:finset α).prod f = 1 := rfl @[simp, to_additive] lemma prod_insert [decidable_eq α] : a ∉ s → (insert a s).prod f = f a * s.prod f := fold_insert @[simp, to_additive] lemma prod_singleton : (singleton a).prod f = f a := eq.trans fold_singleton $ mul_one _ @[simp] lemma prod_const_one : s.prod (λx, (1 : β)) = 1 := by simp only [finset.prod, multiset.map_const, multiset.prod_repeat, one_pow] @[simp] lemma sum_const_zero {β} {s : finset α} [add_comm_monoid β] : s.sum (λx, (0 : β)) = 0 := @prod_const_one _ (multiplicative β) _ _ attribute [to_additive] prod_const_one @[simp, to_additive] lemma prod_image [decidable_eq α] {s : finset γ} {g : γ → α} : (∀x∈s, ∀y∈s, g x = g y → x = y) → (s.image g).prod f = s.prod (λx, f (g x)) := fold_image @[simp, to_additive] lemma prod_map (s : finset α) (e : α ↪ γ) (f : γ → β) : (s.map e).prod f = s.prod (λa, f (e a)) := by rw [finset.prod, finset.map_val, multiset.map_map]; refl @[congr, to_additive] lemma prod_congr (h : s₁ = s₂) : (∀x∈s₂, f x = g x) → s₁.prod f = s₂.prod g := by rw [h]; exact fold_congr attribute [congr] finset.sum_congr @[to_additive] lemma prod_union_inter [decidable_eq α] : (s₁ ∪ s₂).prod f * (s₁ ∩ s₂).prod f = s₁.prod f * s₂.prod f := fold_union_inter @[to_additive] lemma prod_union [decidable_eq α] (h : disjoint s₁ s₂) : (s₁ ∪ s₂).prod f = s₁.prod f * s₂.prod f := by rw [←prod_union_inter, (disjoint_iff_inter_eq_empty.mp h)]; exact (mul_one _).symm @[to_additive] lemma prod_sdiff [decidable_eq α] (h : s₁ ⊆ s₂) : (s₂ \ s₁).prod f * s₁.prod f = s₂.prod f := by rw [←prod_union sdiff_disjoint, sdiff_union_of_subset h] @[to_additive] lemma prod_bind [decidable_eq α] {s : finset γ} {t : γ → finset α} : (∀x∈s, ∀y∈s, x ≠ y → disjoint (t x) (t y)) → (s.bind t).prod f = s.prod (λx, (t x).prod f) := by haveI := classical.dec_eq γ; exact finset.induction_on s (λ _, by simp only [bind_empty, prod_empty]) (assume x s hxs ih hd, have hd' : ∀x∈s, ∀y∈s, x ≠ y → disjoint (t x) (t y), from assume _ hx _ hy, hd _ (mem_insert_of_mem hx) _ (mem_insert_of_mem hy), have ∀y∈s, x ≠ y, from assume _ hy h, by rw [←h] at hy; contradiction, have ∀y∈s, disjoint (t x) (t y), from assume _ hy, hd _ (mem_insert_self _ _) _ (mem_insert_of_mem hy) (this _ hy), have disjoint (t x) (finset.bind s t), from (disjoint_bind_right _ _ _).mpr this, by simp only [bind_insert, prod_insert hxs, prod_union this, ih hd']) @[to_additive] lemma prod_product {s : finset γ} {t : finset α} {f : γ×α → β} : (s.product t).prod f = s.prod (λx, t.prod $ λy, f (x, y)) := begin haveI := classical.dec_eq α, haveI := classical.dec_eq γ, rw [product_eq_bind, prod_bind], { congr, funext, exact prod_image (λ _ _ _ _ H, (prod.mk.inj H).2) }, simp only [disjoint_iff_ne, mem_image], rintros _ _ _ _ h ⟨_, _⟩ ⟨_, _, ⟨_, _⟩⟩ ⟨_, _⟩ ⟨_, _, ⟨_, _⟩⟩ _, apply h, cc end @[to_additive] lemma prod_sigma {σ : α → Type*} {s : finset α} {t : Πa, finset (σ a)} {f : sigma σ → β} : (s.sigma t).prod f = s.prod (λa, (t a).prod $ λs, f ⟨a, s⟩) := by haveI := classical.dec_eq α; haveI := (λ a, classical.dec_eq (σ a)); exact calc (s.sigma t).prod f = (s.bind (λa, (t a).image (λs, sigma.mk a s))).prod f : by rw sigma_eq_bind ... = s.prod (λa, ((t a).image (λs, sigma.mk a s)).prod f) : prod_bind $ assume a₁ ha a₂ ha₂ h, by simp only [disjoint_iff_ne, mem_image]; rintro ⟨_, _⟩ ⟨_, _, _⟩ ⟨_, _⟩ ⟨_, _, _⟩ ⟨_, _⟩; apply h; cc ... = (s.prod $ λa, (t a).prod $ λs, f ⟨a, s⟩) : prod_congr rfl $ λ _ _, prod_image $ λ _ _ _ _ _, by cc @[to_additive] lemma prod_image' [decidable_eq α] {s : finset γ} {g : γ → α} (h : γ → β) (eq : ∀c∈s, f (g c) = (s.filter (λc', g c' = g c)).prod h) : (s.image g).prod f = s.prod h := begin letI := classical.dec_eq γ, rw [← image_bind_filter_eq s g] {occs := occurrences.pos [2]}, rw [finset.prod_bind], { refine finset.prod_congr rfl (assume a ha, _), rcases finset.mem_image.1 ha with ⟨b, hb, rfl⟩, exact eq b hb }, assume a₀ _ a₁ _ ne, refine (disjoint_iff_ne.2 _), assume c₀ h₀ c₁ h₁, rcases mem_filter.1 h₀ with ⟨h₀, rfl⟩, rcases mem_filter.1 h₁ with ⟨h₁, rfl⟩, exact mt (congr_arg g) ne end @[to_additive] lemma prod_mul_distrib : s.prod (λx, f x * g x) = s.prod f * s.prod g := eq.trans (by rw one_mul; refl) fold_op_distrib @[to_additive] lemma prod_comm [decidable_eq γ] {s : finset γ} {t : finset α} {f : γ → α → β} : s.prod (λx, t.prod $ f x) = t.prod (λy, s.prod $ λx, f x y) := finset.induction_on s (by simp only [prod_empty, prod_const_one]) $ λ _ _ H ih, by simp only [prod_insert H, prod_mul_distrib, ih] lemma prod_hom [comm_monoid γ] (g : β → γ) [is_monoid_hom g] : s.prod (λx, g (f x)) = g (s.prod f) := eq.trans (by rw is_monoid_hom.map_one g; refl) (fold_hom $ is_monoid_hom.map_mul g) @[to_additive] lemma prod_hom_rel [comm_monoid γ] {r : β → γ → Prop} {f : α → β} {g : α → γ} {s : finset α} (h₁ : r 1 1) (h₂ : ∀a b c, r b c → r (f a * b) (g a * c)) : r (s.prod f) (s.prod g) := begin letI := classical.dec_eq α, refine finset.induction_on s h₁ (assume a s has ih, _), rw [prod_insert has, prod_insert has], exact h₂ a (s.prod f) (s.prod g) ih, end @[to_additive] lemma prod_subset (h : s₁ ⊆ s₂) (hf : ∀x∈s₂, x ∉ s₁ → f x = 1) : s₁.prod f = s₂.prod f := by haveI := classical.dec_eq α; exact have (s₂ \ s₁).prod f = (s₂ \ s₁).prod (λx, 1), from prod_congr rfl $ by simpa only [mem_sdiff, and_imp], by rw [←prod_sdiff h]; simp only [this, prod_const_one, one_mul] @[to_additive] lemma prod_filter (p : α → Prop) [decidable_pred p] (f : α → β) : (s.filter p).prod f = s.prod (λa, if p a then f a else 1) := calc (s.filter p).prod f = (s.filter p).prod (λa, if p a then f a else 1) : prod_congr rfl (assume a h, by rw [if_pos (mem_filter.1 h).2]) ... = s.prod (λa, if p a then f a else 1) : begin refine prod_subset (filter_subset s) (assume x hs h, _), rw [mem_filter, not_and] at h, exact if_neg (h hs) end @[to_additive] lemma prod_eq_single {s : finset α} {f : α → β} (a : α) (h₀ : ∀b∈s, b ≠ a → f b = 1) (h₁ : a ∉ s → f a = 1) : s.prod f = f a := by haveI := classical.dec_eq α; from classical.by_cases (assume : a ∈ s, calc s.prod f = ({a} : finset α).prod f : begin refine (prod_subset _ _).symm, { intros _ H, rwa mem_singleton.1 H }, { simpa only [mem_singleton] } end ... = f a : prod_singleton) (assume : a ∉ s, (prod_congr rfl $ λ b hb, h₀ b hb $ by rintro rfl; cc).trans $ prod_const_one.trans (h₁ this).symm) @[to_additive] lemma prod_ite [comm_monoid γ] {s : finset α} {p : α → Prop} {hp : decidable_pred p} (f g : α → γ) (h : γ → β) : s.prod (λ x, h (if p x then f x else g x)) = (s.filter p).prod (λ x, h (f x)) * (s.filter (λ x, ¬ p x)).prod (λ x, h (g x)) := by letI := classical.dec_eq α; exact calc s.prod (λ x, h (if p x then f x else g x)) = (s.filter p ∪ s.filter (λ x, ¬ p x)).prod (λ x, h (if p x then f x else g x)) : by rw [filter_union_filter_neg_eq] ... = (s.filter p).prod (λ x, h (if p x then f x else g x)) * (s.filter (λ x, ¬ p x)).prod (λ x, h (if p x then f x else g x)) : prod_union (by simp [disjoint_right] {contextual := tt}) ... = (s.filter p).prod (λ x, h (f x)) * (s.filter (λ x, ¬ p x)).prod (λ x, h (g x)) : congr_arg2 _ (prod_congr rfl (by simp {contextual := tt})) (prod_congr rfl (by simp {contextual := tt})) @[simp, to_additive] lemma prod_ite_eq [decidable_eq α] (s : finset α) (a : α) (b : β) : s.prod (λ x, (ite (a = x) b 1)) = ite (a ∈ s) b 1 := begin rw ←finset.prod_filter, split_ifs; simp only [filter_eq, if_true, if_false, h, prod_empty, prod_singleton, insert_empty_eq_singleton], end @[to_additive] lemma prod_attach {f : α → β} : s.attach.prod (λx, f x.val) = s.prod f := by haveI := classical.dec_eq α; exact calc s.attach.prod (λx, f x.val) = ((s.attach).image subtype.val).prod f : by rw [prod_image]; exact assume x _ y _, subtype.eq ... = _ : by rw [attach_image_val] @[to_additive] lemma prod_bij {s : finset α} {t : finset γ} {f : α → β} {g : γ → β} (i : Πa∈s, γ) (hi : ∀a ha, i a ha ∈ t) (h : ∀a ha, f a = g (i a ha)) (i_inj : ∀a₁ a₂ ha₁ ha₂, i a₁ ha₁ = i a₂ ha₂ → a₁ = a₂) (i_surj : ∀b∈t, ∃a ha, b = i a ha) : s.prod f = t.prod g := congr_arg multiset.prod (multiset.map_eq_map_of_bij_of_nodup f g s.2 t.2 i hi h i_inj i_surj) @[to_additive] lemma prod_bij_ne_one {s : finset α} {t : finset γ} {f : α → β} {g : γ → β} (i : Πa∈s, f a ≠ 1 → γ) (hi₁ : ∀a h₁ h₂, i a h₁ h₂ ∈ t) (hi₂ : ∀a₁ a₂ h₁₁ h₁₂ h₂₁ h₂₂, i a₁ h₁₁ h₁₂ = i a₂ h₂₁ h₂₂ → a₁ = a₂) (hi₃ : ∀b∈t, g b ≠ 1 → ∃a h₁ h₂, b = i a h₁ h₂) (h : ∀a h₁ h₂, f a = g (i a h₁ h₂)) : s.prod f = t.prod g := by haveI := classical.prop_decidable; exact calc s.prod f = (s.filter $ λx, f x ≠ 1).prod f : (prod_subset (filter_subset _) $ by simp only [not_imp_comm, mem_filter]; exact λ _, and.intro).symm ... = (t.filter $ λx, g x ≠ 1).prod g : prod_bij (assume a ha, i a (mem_filter.mp ha).1 (mem_filter.mp ha).2) (assume a ha, (mem_filter.mp ha).elim $ λh₁ h₂, mem_filter.mpr ⟨hi₁ a h₁ h₂, λ hg, h₂ (hg ▸ h a h₁ h₂)⟩) (assume a ha, (mem_filter.mp ha).elim $ h a) (assume a₁ a₂ ha₁ ha₂, (mem_filter.mp ha₁).elim $ λha₁₁ ha₁₂, (mem_filter.mp ha₂).elim $ λha₂₁ ha₂₂, hi₂ a₁ a₂ _ _ _ _) (assume b hb, (mem_filter.mp hb).elim $ λh₁ h₂, let ⟨a, ha₁, ha₂, eq⟩ := hi₃ b h₁ h₂ in ⟨a, mem_filter.mpr ⟨ha₁, ha₂⟩, eq⟩) ... = t.prod g : (prod_subset (filter_subset _) $ by simp only [not_imp_comm, mem_filter]; exact λ _, and.intro) @[to_additive] lemma exists_ne_one_of_prod_ne_one : s.prod f ≠ 1 → ∃a∈s, f a ≠ 1 := by haveI := classical.dec_eq α; exact finset.induction_on s (λ H, (H rfl).elim) (assume a s has ih h, classical.by_cases (assume ha : f a = 1, let ⟨a, ha, hfa⟩ := ih (by rwa [prod_insert has, ha, one_mul] at h) in ⟨a, mem_insert_of_mem ha, hfa⟩) (assume hna : f a ≠ 1, ⟨a, mem_insert_self _ _, hna⟩)) @[to_additive] lemma prod_range_succ (f : ℕ → β) (n : ℕ) : (range (nat.succ n)).prod f = f n * (range n).prod f := by rw [range_succ, prod_insert not_mem_range_self] lemma prod_range_succ' (f : ℕ → β) : ∀ n : ℕ, (range (nat.succ n)).prod f = (range n).prod (f ∘ nat.succ) * f 0 | 0 := (prod_range_succ _ _).trans $ mul_comm _ _ | (n + 1) := by rw [prod_range_succ (λ m, f (nat.succ m)), mul_assoc, ← prod_range_succ']; exact prod_range_succ _ _ lemma sum_Ico_add {δ : Type*} [add_comm_monoid δ] (f : ℕ → δ) (m n k : ℕ) : (Ico m n).sum (λ l, f (k + l)) = (Ico (m + k) (n + k)).sum f := Ico.image_add m n k ▸ eq.symm $ sum_image $ λ x hx y hy h, nat.add_left_cancel h @[to_additive] lemma prod_Ico_add (f : ℕ → β) (m n k : ℕ) : (Ico m n).prod (λ l, f (k + l)) = (Ico (m + k) (n + k)).prod f := Ico.image_add m n k ▸ eq.symm $ prod_image $ λ x hx y hy h, nat.add_left_cancel h @[to_additive] lemma prod_Ico_consecutive (f : ℕ → β) {m n k : ℕ} (hmn : m ≤ n) (hnk : n ≤ k) : (Ico m n).prod f * (Ico n k).prod f = (Ico m k).prod f := Ico.union_consecutive hmn hnk ▸ eq.symm $ prod_union $ Ico.disjoint_consecutive m n k @[to_additive] lemma prod_range_mul_prod_Ico (f : ℕ → β) {m n : ℕ} (h : m ≤ n) : (range m).prod f * (Ico m n).prod f = (range n).prod f := Ico.zero_bot m ▸ Ico.zero_bot n ▸ prod_Ico_consecutive f (nat.zero_le m) h @[to_additive sum_Ico_eq_add_neg] lemma prod_Ico_eq_div {δ : Type*} [comm_group δ] (f : ℕ → δ) {m n : ℕ} (h : m ≤ n) : (Ico m n).prod f = (range n).prod f * ((range m).prod f)⁻¹ := eq_mul_inv_iff_mul_eq.2 $ by rw [mul_comm]; exact prod_range_mul_prod_Ico f h lemma sum_Ico_eq_sub {δ : Type*} [add_comm_group δ] (f : ℕ → δ) {m n : ℕ} (h : m ≤ n) : (Ico m n).sum f = (range n).sum f - (range m).sum f := sum_Ico_eq_add_neg f h @[to_additive] lemma prod_Ico_eq_prod_range (f : ℕ → β) (m n : ℕ) : (Ico m n).prod f = (range (n - m)).prod (λ l, f (m + l)) := begin by_cases h : m ≤ n, { rw [← Ico.zero_bot, prod_Ico_add, zero_add, nat.sub_add_cancel h] }, { replace h : n ≤ m := le_of_not_ge h, rw [Ico.eq_empty_of_le h, nat.sub_eq_zero_of_le h, range_zero, prod_empty, prod_empty] } end @[to_additive] lemma prod_range_zero (f : ℕ → β) : (range 0).prod f = 1 := by rw [range_zero, prod_empty] lemma prod_range_one (f : ℕ → β) : (range 1).prod f = f 0 := by { rw [range_one], apply @prod_singleton ℕ β 0 f } lemma sum_range_one {δ : Type*} [add_comm_monoid δ] (f : ℕ → δ) : (range 1).sum f = f 0 := by { rw [range_one], apply @sum_singleton ℕ δ 0 f } attribute [to_additive finset.sum_range_one] prod_range_one @[simp] lemma prod_const (b : β) : s.prod (λ a, b) = b ^ s.card := by haveI := classical.dec_eq α; exact finset.induction_on s rfl (λ a s has ih, by rw [prod_insert has, card_insert_of_not_mem has, pow_succ, ih]) lemma prod_pow (s : finset α) (n : ℕ) (f : α → β) : s.prod (λ x, f x ^ n) = s.prod f ^ n := by haveI := classical.dec_eq α; exact finset.induction_on s (by simp) (by simp [_root_.mul_pow] {contextual := tt}) lemma prod_nat_pow (s : finset α) (n : ℕ) (f : α → ℕ) : s.prod (λ x, f x ^ n) = s.prod f ^ n := by haveI := classical.dec_eq α; exact finset.induction_on s (by simp) (by simp [nat.mul_pow] {contextual := tt}) @[to_additive] lemma prod_involution {s : finset α} {f : α → β} : ∀ (g : Π a ∈ s, α) (h₁ : ∀ a ha, f a * f (g a ha) = 1) (h₂ : ∀ a ha, f a ≠ 1 → g a ha ≠ a) (h₃ : ∀ a ha, g a ha ∈ s) (h₄ : ∀ a ha, g (g a ha) (h₃ a ha) = a), s.prod f = 1 := by haveI := classical.dec_eq α; haveI := classical.dec_eq β; exact finset.strong_induction_on s (λ s ih g h₁ h₂ h₃ h₄, if hs : s = ∅ then hs.symm ▸ rfl else let ⟨x, hx⟩ := exists_mem_of_ne_empty hs in have hmem : ∀ y ∈ (s.erase x).erase (g x hx), y ∈ s, from λ y hy, (mem_of_mem_erase (mem_of_mem_erase hy)), have g_inj : ∀ {x hx y hy}, g x hx = g y hy → x = y, from λ x hx y hy h, by rw [← h₄ x hx, ← h₄ y hy]; simp [h], have ih': (erase (erase s x) (g x hx)).prod f = (1 : β) := ih ((s.erase x).erase (g x hx)) ⟨subset.trans (erase_subset _ _) (erase_subset _ _), λ h, not_mem_erase (g x hx) (s.erase x) (h (h₃ x hx))⟩ (λ y hy, g y (hmem y hy)) (λ y hy, h₁ y (hmem y hy)) (λ y hy, h₂ y (hmem y hy)) (λ y hy, mem_erase.2 ⟨λ (h : g y _ = g x hx), by simpa [g_inj h] using hy, mem_erase.2 ⟨λ (h : g y _ = x), have y = g x hx, from h₄ y (hmem y hy) ▸ by simp [h], by simpa [this] using hy, h₃ y (hmem y hy)⟩⟩) (λ y hy, h₄ y (hmem y hy)), if hx1 : f x = 1 then ih' ▸ eq.symm (prod_subset hmem (λ y hy hy₁, have y = x ∨ y = g x hx, by simp [hy] at hy₁; tauto, this.elim (λ h, h.symm ▸ hx1) (λ h, h₁ x hx ▸ h ▸ hx1.symm ▸ (one_mul _).symm))) else by rw [← insert_erase hx, prod_insert (not_mem_erase _ _), ← insert_erase (mem_erase.2 ⟨h₂ x hx hx1, h₃ x hx⟩), prod_insert (not_mem_erase _ _), ih', mul_one, h₁ x hx]) @[to_additive] lemma prod_eq_one {f : α → β} {s : finset α} (h : ∀x∈s, f x = 1) : s.prod f = 1 := calc s.prod f = s.prod (λx, 1) : finset.prod_congr rfl h ... = 1 : finset.prod_const_one end comm_monoid attribute [to_additive] prod_hom lemma sum_smul [add_comm_monoid β] (s : finset α) (n : ℕ) (f : α → β) : s.sum (λ x, add_monoid.smul n (f x)) = add_monoid.smul n (s.sum f) := @prod_pow _ (multiplicative β) _ _ _ _ attribute [to_additive sum_smul] prod_pow @[simp] lemma sum_const [add_comm_monoid β] (b : β) : s.sum (λ a, b) = add_monoid.smul s.card b := @prod_const _ (multiplicative β) _ _ _ attribute [to_additive] prod_const lemma sum_range_succ' [add_comm_monoid β] (f : ℕ → β) : ∀ n : ℕ, (range (nat.succ n)).sum f = (range n).sum (f ∘ nat.succ) + f 0 := @prod_range_succ' (multiplicative β) _ _ attribute [to_additive] prod_range_succ' lemma sum_nat_cast [add_comm_monoid β] [has_one β] (s : finset α) (f : α → ℕ) : ↑(s.sum f) = s.sum (λa, f a : α → β) := (sum_hom _).symm lemma prod_nat_cast [comm_semiring β] (s : finset α) (f : α → ℕ) : ↑(s.prod f) = s.prod (λa, f a : α → β) := (prod_hom _).symm protected lemma sum_nat_coe_enat [decidable_eq α] (s : finset α) (f : α → ℕ) : s.sum (λ x, (f x : enat)) = (s.sum f : ℕ) := begin induction s using finset.induction with a s has ih h, { simp }, { simp [has, ih] } end lemma le_sum_of_subadditive [add_comm_monoid α] [ordered_comm_monoid β] (f : α → β) (h_zero : f 0 = 0) (h_add : ∀x y, f (x + y) ≤ f x + f y) (s : finset γ) (g : γ → α) : f (s.sum g) ≤ s.sum (λc, f (g c)) := begin refine le_trans (multiset.le_sum_of_subadditive f h_zero h_add _) _, rw [multiset.map_map], refl end lemma abs_sum_le_sum_abs [discrete_linear_ordered_field α] {f : β → α} {s : finset β} : abs (s.sum f) ≤ s.sum (λa, abs (f a)) := le_sum_of_subadditive _ abs_zero abs_add s f section comm_group variables [comm_group β] @[simp, to_additive] lemma prod_inv_distrib : s.prod (λx, (f x)⁻¹) = (s.prod f)⁻¹ := prod_hom has_inv.inv end comm_group @[simp] theorem card_sigma {σ : α → Type*} (s : finset α) (t : Π a, finset (σ a)) : card (s.sigma t) = s.sum (λ a, card (t a)) := multiset.card_sigma _ _ lemma card_bind [decidable_eq β] {s : finset α} {t : α → finset β} (h : ∀ x ∈ s, ∀ y ∈ s, x ≠ y → disjoint (t x) (t y)) : (s.bind t).card = s.sum (λ u, card (t u)) := calc (s.bind t).card = (s.bind t).sum (λ _, 1) : by simp ... = s.sum (λ a, (t a).sum (λ _, 1)) : finset.sum_bind h ... = s.sum (λ u, card (t u)) : by simp lemma card_bind_le [decidable_eq β] {s : finset α} {t : α → finset β} : (s.bind t).card ≤ s.sum (λ a, (t a).card) := by haveI := classical.dec_eq α; exact finset.induction_on s (by simp) (λ a s has ih, calc ((insert a s).bind t).card ≤ (t a).card + (s.bind t).card : by rw bind_insert; exact finset.card_union_le _ _ ... ≤ (insert a s).sum (λ a, card (t a)) : by rw sum_insert has; exact add_le_add_left ih _) theorem card_eq_sum_card_image [decidable_eq β] (f : α → β) (s : finset α) : s.card = (s.image f).sum (λ a, (s.filter (λ x, f x = a)).card) := by letI := classical.dec_eq α; exact calc s.card = ((s.image f).bind (λ a, s.filter (λ x, f x = a))).card : congr_arg _ (finset.ext.2 $ λ x, ⟨λ hs, mem_bind.2 ⟨f x, mem_image_of_mem _ hs, mem_filter.2 ⟨hs, rfl⟩⟩, λ h, let ⟨a, ha₁, ha₂⟩ := mem_bind.1 h in by convert filter_subset s ha₂⟩) ... = (s.image f).sum (λ a, (s.filter (λ x, f x = a)).card) : card_bind (by simp [disjoint_left, finset.ext] {contextual := tt}) lemma gsmul_sum [add_comm_group β] {f : α → β} {s : finset α} (z : ℤ) : gsmul z (s.sum f) = s.sum (λa, gsmul z (f a)) := (finset.sum_hom (gsmul z)).symm end finset namespace finset variables {s s₁ s₂ : finset α} {f g : α → β} {b : β} {a : α} @[simp] lemma sum_sub_distrib [add_comm_group β] : s.sum (λx, f x - g x) = s.sum f - s.sum g := sum_add_distrib.trans $ congr_arg _ sum_neg_distrib section semiring variables [semiring β] lemma sum_mul : s.sum f * b = s.sum (λx, f x * b) := (sum_hom (λx, x * b)).symm lemma mul_sum : b * s.sum f = s.sum (λx, b * f x) := (sum_hom (λx, b * x)).symm @[simp] lemma sum_mul_boole [decidable_eq α] (s : finset α) (f : α → β) (a : α) : s.sum (λ x, (f x * ite (a = x) 1 0)) = ite (a ∈ s) (f a) 0 := begin convert sum_ite_eq s a (f a), funext, split_ifs with h; simp [h], end @[simp] lemma sum_boole_mul [decidable_eq α] (s : finset α) (f : α → β) (a : α) : s.sum (λ x, (ite (a = x) 1 0) * f x) = ite (a ∈ s) (f a) 0 := begin convert sum_ite_eq s a (f a), funext, split_ifs with h; simp [h], end end semiring section comm_semiring variables [decidable_eq α] [comm_semiring β] lemma prod_eq_zero (ha : a ∈ s) (h : f a = 0) : s.prod f = 0 := calc s.prod f = (insert a (erase s a)).prod f : by rw insert_erase ha ... = 0 : by rw [prod_insert (not_mem_erase _ _), h, zero_mul] lemma prod_sum {δ : α → Type*} [∀a, decidable_eq (δ a)] {s : finset α} {t : Πa, finset (δ a)} {f : Πa, δ a → β} : s.prod (λa, (t a).sum (λb, f a b)) = (s.pi t).sum (λp, s.attach.prod (λx, f x.1 (p x.1 x.2))) := begin induction s using finset.induction with a s ha ih, { rw [pi_empty, sum_singleton], refl }, { have h₁ : ∀x ∈ t a, ∀y ∈ t a, ∀h : x ≠ y, disjoint (image (pi.cons s a x) (pi s t)) (image (pi.cons s a y) (pi s t)), { assume x hx y hy h, simp only [disjoint_iff_ne, mem_image], rintros _ ⟨p₂, hp, eq₂⟩ _ ⟨p₃, hp₃, eq₃⟩ eq, have : pi.cons s a x p₂ a (mem_insert_self _ _) = pi.cons s a y p₃ a (mem_insert_self _ _), { rw [eq₂, eq₃, eq] }, rw [pi.cons_same, pi.cons_same] at this, exact h this }, rw [prod_insert ha, pi_insert ha, ih, sum_mul, sum_bind h₁], refine sum_congr rfl (λ b _, _), have h₂ : ∀p₁∈pi s t, ∀p₂∈pi s t, pi.cons s a b p₁ = pi.cons s a b p₂ → p₁ = p₂, from assume p₁ h₁ p₂ h₂ eq, injective_pi_cons ha eq, rw [sum_image h₂, mul_sum], refine sum_congr rfl (λ g _, _), rw [attach_insert, prod_insert, prod_image], { simp only [pi.cons_same], congr', ext ⟨v, hv⟩, congr', exact (pi.cons_ne (by rintro rfl; exact ha hv)).symm }, { exact λ _ _ _ _, subtype.eq ∘ subtype.mk.inj }, { simp only [mem_image], rintro ⟨⟨_, hm⟩, _, rfl⟩, exact ha hm } } end end comm_semiring section integral_domain /- add integral_semi_domain to support nat and ennreal -/ variables [decidable_eq α] [integral_domain β] lemma prod_eq_zero_iff : s.prod f = 0 ↔ (∃a∈s, f a = 0) := finset.induction_on s ⟨not.elim one_ne_zero, λ ⟨_, H, _⟩, H.elim⟩ $ λ a s ha ih, by rw [prod_insert ha, mul_eq_zero_iff_eq_zero_or_eq_zero, bex_def, exists_mem_insert, ih, ← bex_def] end integral_domain section ordered_comm_monoid variables [decidable_eq α] [ordered_comm_monoid β] lemma sum_le_sum : (∀x∈s, f x ≤ g x) → s.sum f ≤ s.sum g := finset.induction_on s (λ _, le_refl _) $ assume a s ha ih h, have f a + s.sum f ≤ g a + s.sum g, from add_le_add' (h _ (mem_insert_self _ _)) (ih $ assume x hx, h _ $ mem_insert_of_mem hx), by simpa only [sum_insert ha] lemma sum_nonneg (h : ∀x∈s, 0 ≤ f x) : 0 ≤ s.sum f := le_trans (by rw [sum_const_zero]) (sum_le_sum h) lemma sum_nonpos (h : ∀x∈s, f x ≤ 0) : s.sum f ≤ 0 := le_trans (sum_le_sum h) (by rw [sum_const_zero]) lemma sum_le_sum_of_subset_of_nonneg (h : s₁ ⊆ s₂) (hf : ∀x∈s₂, x ∉ s₁ → 0 ≤ f x) : s₁.sum f ≤ s₂.sum f := calc s₁.sum f ≤ (s₂ \ s₁).sum f + s₁.sum f : le_add_of_nonneg_left' $ sum_nonneg $ by simpa only [mem_sdiff, and_imp] ... = (s₂ \ s₁ ∪ s₁).sum f : (sum_union sdiff_disjoint).symm ... = s₂.sum f : by rw [sdiff_union_of_subset h] lemma sum_eq_zero_iff_of_nonneg : (∀x∈s, 0 ≤ f x) → (s.sum f = 0 ↔ ∀x∈s, f x = 0) := finset.induction_on s (λ _, ⟨λ _ _, false.elim, λ _, rfl⟩) $ λ a s ha ih H, have ∀ x ∈ s, 0 ≤ f x, from λ _, H _ ∘ mem_insert_of_mem, by rw [sum_insert ha, add_eq_zero_iff' (H _ $ mem_insert_self _ _) (sum_nonneg this), forall_mem_insert, ih this] lemma sum_eq_zero_iff_of_nonpos : (∀x∈s, f x ≤ 0) → (s.sum f = 0 ↔ ∀x∈s, f x = 0) := @sum_eq_zero_iff_of_nonneg _ (order_dual β) _ _ _ _ lemma single_le_sum (hf : ∀x∈s, 0 ≤ f x) {a} (h : a ∈ s) : f a ≤ s.sum f := have (singleton a).sum f ≤ s.sum f, from sum_le_sum_of_subset_of_nonneg (λ x e, (mem_singleton.1 e).symm ▸ h) (λ x h _, hf x h), by rwa sum_singleton at this end ordered_comm_monoid section canonically_ordered_monoid variables [decidable_eq α] [canonically_ordered_monoid β] lemma sum_le_sum_of_subset (h : s₁ ⊆ s₂) : s₁.sum f ≤ s₂.sum f := sum_le_sum_of_subset_of_nonneg h $ assume x h₁ h₂, zero_le _ lemma sum_le_sum_of_ne_zero [@decidable_rel β (≤)] (h : ∀x∈s₁, f x ≠ 0 → x ∈ s₂) : s₁.sum f ≤ s₂.sum f := calc s₁.sum f = (s₁.filter (λx, f x = 0)).sum f + (s₁.filter (λx, f x ≠ 0)).sum f : by rw [←sum_union, filter_union_filter_neg_eq]; exact disjoint_filter.2 (assume _ _ h n_h, n_h h) ... ≤ s₂.sum f : add_le_of_nonpos_of_le' (sum_nonpos $ by simp only [mem_filter, and_imp]; exact λ _ _, le_of_eq) (sum_le_sum_of_subset $ by simpa only [subset_iff, mem_filter, and_imp]) end canonically_ordered_monoid section linear_ordered_comm_ring variables [decidable_eq α] [linear_ordered_comm_ring β] /- this is also true for a ordered commutative multiplicative monoid -/ lemma prod_nonneg {s : finset α} {f : α → β} (h0 : ∀(x ∈ s), 0 ≤ f x) : 0 ≤ s.prod f := begin induction s using finset.induction with a s has ih h, { simp [zero_le_one] }, { simp [has], apply mul_nonneg, apply h0 a (mem_insert_self a s), exact ih (λ x H, h0 x (mem_insert_of_mem H)) } end /- this is also true for a ordered commutative multiplicative monoid -/ lemma prod_pos {s : finset α} {f : α → β} (h0 : ∀(x ∈ s), 0 < f x) : 0 < s.prod f := begin induction s using finset.induction with a s has ih h, { simp [zero_lt_one] }, { simp [has], apply mul_pos, apply h0 a (mem_insert_self a s), exact ih (λ x H, h0 x (mem_insert_of_mem H)) } end /- this is also true for a ordered commutative multiplicative monoid -/ lemma prod_le_prod {s : finset α} {f g : α → β} (h0 : ∀(x ∈ s), 0 ≤ f x) (h1 : ∀(x ∈ s), f x ≤ g x) : s.prod f ≤ s.prod g := begin induction s using finset.induction with a s has ih h, { simp }, { simp [has], apply mul_le_mul, exact h1 a (mem_insert_self a s), apply ih (λ x H, h0 _ _) (λ x H, h1 _ _); exact (mem_insert_of_mem H), apply prod_nonneg (λ x H, h0 x (mem_insert_of_mem H)), apply le_trans (h0 a (mem_insert_self a s)) (h1 a (mem_insert_self a s)) } end end linear_ordered_comm_ring @[simp] lemma card_pi [decidable_eq α] {δ : α → Type*} (s : finset α) (t : Π a, finset (δ a)) : (s.pi t).card = s.prod (λ a, card (t a)) := multiset.card_pi _ _ theorem card_le_mul_card_image [decidable_eq β] {f : α → β} (s : finset α) (n : ℕ) (hn : ∀ a ∈ s.image f, (s.filter (λ x, f x = a)).card ≤ n) : s.card ≤ n * (s.image f).card := calc s.card = (s.image f).sum (λ a, (s.filter (λ x, f x = a)).card) : card_eq_sum_card_image _ _ ... ≤ (s.image f).sum (λ _, n) : sum_le_sum hn ... = _ : by simp [mul_comm] @[simp] lemma prod_range_id_eq_fact (n : ℕ) : ((range n.succ).erase 0).prod (λ x, x) = nat.fact n := calc ((range n.succ).erase 0).prod (λ x, x) = (range n).prod nat.succ : eq.symm (prod_bij (λ x _, nat.succ x) (λ a h₁, mem_erase.2 ⟨nat.succ_ne_zero _, mem_range.2 $ nat.succ_lt_succ $ by simpa using h₁⟩) (by simp) (λ _ _ _ _, nat.succ_inj) (λ b h, have b.pred.succ = b, from nat.succ_pred_eq_of_pos $ by simp [nat.pos_iff_ne_zero] at *; tauto, ⟨nat.pred b, mem_range.2 $ nat.lt_of_succ_lt_succ (by simp [*] at *), this.symm⟩)) ... = nat.fact n : by induction n; simp [*, range_succ] end finset namespace finset section gauss_sum /-- Gauss' summation formula -/ lemma sum_range_id_mul_two : ∀(n : ℕ), (finset.range n).sum (λi, i) * 2 = n * (n - 1) | 0 := rfl | 1 := rfl | ((n + 1) + 1) := begin rw [sum_range_succ, add_mul, sum_range_id_mul_two (n + 1), mul_comm, two_mul, nat.add_sub_cancel, nat.add_sub_cancel, mul_comm _ n], simp only [add_mul, one_mul, add_comm, add_assoc, add_left_comm] end /-- Gauss' summation formula -/ lemma sum_range_id (n : ℕ) : (finset.range n).sum (λi, i) = (n * (n - 1)) / 2 := by rw [← sum_range_id_mul_two n, nat.mul_div_cancel]; exact dec_trivial end gauss_sum lemma card_eq_sum_ones (s : finset α) : s.card = s.sum (λ _, 1) := by simp end finset section group open list variables [group α] [group β] @[to_additive] theorem is_group_hom.map_prod (f : α → β) [is_group_hom f] (l : list α) : f (prod l) = prod (map f l) := by induction l; simp only [*, is_mul_hom.map_mul f, is_group_hom.map_one f, prod_nil, prod_cons, map] theorem is_group_anti_hom.map_prod (f : α → β) [is_group_anti_hom f] (l : list α) : f (prod l) = prod (map f (reverse l)) := by induction l with hd tl ih; [exact is_group_anti_hom.map_one f, simp only [prod_cons, is_group_anti_hom.map_mul f, ih, reverse_cons, map_append, prod_append, map_singleton, prod_cons, prod_nil, mul_one]] theorem inv_prod : ∀ l : list α, (prod l)⁻¹ = prod (map (λ x, x⁻¹) (reverse l)) := λ l, @is_group_anti_hom.map_prod _ _ _ _ _ inv_is_group_anti_hom l -- TODO there is probably a cleaner proof of this end group section comm_group variables [comm_group α] [comm_group β] (f : α → β) [is_group_hom f] @[to_additive] lemma is_group_hom.map_multiset_prod (m : multiset α) : f m.prod = (m.map f).prod := quotient.induction_on m $ assume l, by simp [is_group_hom.map_prod f l] @[to_additive] lemma is_group_hom.map_finset_prod (g : γ → α) (s : finset γ) : f (s.prod g) = s.prod (f ∘ g) := show f (s.val.map g).prod = (s.val.map (f ∘ g)).prod, by rw [is_group_hom.map_multiset_prod f]; simp end comm_group @[to_additive is_add_group_hom_finset_sum] lemma is_group_hom_finset_prod {α β γ} [group α] [comm_group β] (s : finset γ) (f : γ → α → β) [∀c, is_group_hom (f c)] : is_group_hom (λa, s.prod (λc, f c a)) := { map_mul := assume a b, by simp only [λc, is_mul_hom.map_mul (f c), finset.prod_mul_distrib] } attribute [instance] is_group_hom_finset_prod is_add_group_hom_finset_sum namespace multiset variables [decidable_eq α] @[simp] lemma to_finset_sum_count_eq (s : multiset α) : s.to_finset.sum (λa, s.count a) = s.card := multiset.induction_on s rfl (assume a s ih, calc (to_finset (a :: s)).sum (λx, count x (a :: s)) = (to_finset (a :: s)).sum (λx, (if x = a then 1 else 0) + count x s) : finset.sum_congr rfl $ λ _ _, by split_ifs; [simp only [h, count_cons_self, nat.one_add], simp only [count_cons_of_ne h, zero_add]] ... = card (a :: s) : begin by_cases a ∈ s.to_finset, { have : (to_finset s).sum (λx, ite (x = a) 1 0) = (finset.singleton a).sum (λx, ite (x = a) 1 0), { apply (finset.sum_subset _ _).symm, { intros _ H, rwa mem_singleton.1 H }, { exact λ _ _ H, if_neg (mt finset.mem_singleton.2 H) } }, rw [to_finset_cons, finset.insert_eq_of_mem h, finset.sum_add_distrib, ih, this, finset.sum_singleton, if_pos rfl, add_comm, card_cons] }, { have ha : a ∉ s, by rwa mem_to_finset at h, have : (to_finset s).sum (λx, ite (x = a) 1 0) = (to_finset s).sum (λx, 0), from finset.sum_congr rfl (λ x hx, if_neg $ by rintro rfl; cc), rw [to_finset_cons, finset.sum_insert h, if_pos rfl, finset.sum_add_distrib, this, finset.sum_const_zero, ih, count_eq_zero_of_not_mem ha, zero_add, add_comm, card_cons] } end) end multiset
9fa3d27784cdda44464d112fa7c528565e29fe52
75db7e3219bba2fbf41bf5b905f34fcb3c6ca3f2
/hott/algebra/homotopy_group.hlean
c66587949bfabba06fc1733cd9b9ec96cdd91eb4
[ "Apache-2.0" ]
permissive
jroesch/lean
30ef0860fa905d35b9ad6f76de1a4f65c9af6871
3de4ec1a6ce9a960feb2a48eeea8b53246fa34f2
refs/heads/master
1,586,090,835,348
1,455,142,203,000
1,455,142,277,000
51,536,958
1
0
null
1,455,215,811,000
1,455,215,811,000
null
UTF-8
Lean
false
false
3,144
hlean
/- Copyright (c) 2015 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn homotopy groups of a pointed space -/ import types.pointed .trunc_group .hott types.trunc open nat eq pointed trunc is_trunc algebra namespace eq definition homotopy_group [reducible] (n : ℕ) (A : Type*) : Type := trunc 0 (Ω[n] A) notation `π[`:95 n:0 `] `:0 A:95 := homotopy_group n A definition pointed_homotopy_group [instance] [constructor] (n : ℕ) (A : Type*) : pointed (π[n] A) := pointed.mk (tr rfln) definition group_homotopy_group [instance] [constructor] (n : ℕ) (A : Type*) : group (π[succ n] A) := trunc_group concat inverse idp con.assoc idp_con con_idp con.left_inv definition comm_group_homotopy_group [constructor] (n : ℕ) (A : Type*) : comm_group (π[succ (succ n)] A) := trunc_comm_group concat inverse idp con.assoc idp_con con_idp con.left_inv eckmann_hilton local attribute comm_group_homotopy_group [instance] definition Pointed_homotopy_group [constructor] (n : ℕ) (A : Type*) : Type* := Pointed.mk (π[n] A) definition Group_homotopy_group [constructor] (n : ℕ) (A : Type*) : Group := Group.mk (π[succ n] A) _ definition CommGroup_homotopy_group [constructor] (n : ℕ) (A : Type*) : CommGroup := CommGroup.mk (π[succ (succ n)] A) _ definition fundamental_group [constructor] (A : Type*) : Group := Group_homotopy_group zero A notation `πP[`:95 n:0 `] `:0 A:95 := Pointed_homotopy_group n A notation `πG[`:95 n:0 ` +1] `:0 A:95 := Group_homotopy_group n A notation `πaG[`:95 n:0 ` +2] `:0 A:95 := CommGroup_homotopy_group n A prefix `π₁`:95 := fundamental_group open equiv unit theorem trivial_homotopy_of_is_hset (A : Type*) [H : is_hset A] (n : ℕ) : πG[n+1] A = G0 := begin apply trivial_group_of_is_contr, apply is_trunc_trunc_of_is_trunc, apply is_contr_loop_of_is_trunc, apply is_trunc_succ_succ_of_is_hset end definition homotopy_group_succ_out (A : Type*) (n : ℕ) : πG[ n +1] A = π₁ Ω[n] A := idp definition homotopy_group_succ_in (A : Type*) (n : ℕ) : πG[succ n +1] A = πG[n +1] Ω A := begin fapply Group_eq, { apply equiv_of_eq, exact ap (λ(X : Type*), trunc 0 X) (loop_space_succ_eq_in A (succ n))}, { exact abstract [irreducible] begin refine trunc.rec _, intro p, refine trunc.rec _, intro q, rewrite [▸*,-+tr_eq_cast_ap, +trunc_transport], refine !trunc_transport ⬝ _, apply ap tr, apply loop_space_succ_eq_in_concat end end}, end definition homotopy_group_add (A : Type*) (n m : ℕ) : πG[n+m +1] A = πG[n +1] Ω[m] A := begin revert A, induction m with m IH: intro A, { reflexivity}, { esimp [Iterated_loop_space, nat.add], refine !homotopy_group_succ_in ⬝ _, refine !IH ⬝ _, exact ap (Group_homotopy_group n) !loop_space_succ_eq_in⁻¹} end theorem trivial_homotopy_of_is_hset_loop_space {A : Type*} {n : ℕ} (m : ℕ) (H : is_hset (Ω[n] A)) : πG[m+n+1] A = G0 := !homotopy_group_add ⬝ !trivial_homotopy_of_is_hset end eq
565881f6cb201be8356ac426245f2f7e521bed85
6e41ee3ac9b96e8980a16295cc21f131e731884f
/tests/lean/run/local_notation.lean
522b1233e10e6cf917dc2f929d691ee154c32e56
[ "Apache-2.0" ]
permissive
EgbertRijke/lean
3426cfa0e5b3d35d12fc3fd7318b35574cb67dc3
4f2e0c6d7fc9274d953cfa1c37ab2f3e799ab183
refs/heads/master
1,610,834,871,476
1,422,159,801,000
1,422,159,801,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
163
lean
import logic section variables {A : Type} variables f : A → A → A infixl [local] `+++`:10 := f variables a b c : A check f a b check a +++ b end
9b7b0b0a0f1ea43786fe7faf9b5873ee41f08933
63abd62053d479eae5abf4951554e1064a4c45b4
/src/algebra/polynomial/big_operators.lean
41e0ea4be4b02812bfc380eb238a92243afea12e
[ "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,570
lean
/- Copyright (c) 2020 Aaron Anderson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Aaron Anderson, Jalex Stark. -/ import data.polynomial.monic import tactic.linarith open polynomial finset /-! # Polynomials Lemmas for the interaction between polynomials and ∑ and ∏. ## Main results - `nat_degree_prod_of_monic` : the degree of a product of monic polynomials is the product of degrees. We prove this only for [comm_semiring R], but it ought to be true for [semiring R] and list.prod. - `nat_degree_prod` : for polynomials over an integral domain, the degree of the product is the sum of degrees - `leading_coeff_prod` : for polynomials over an integral domain, the leading coefficient is the product of leading coefficients - `prod_X_sub_C_coeff_card_pred` carries most of the content for computing the second coefficient of the characteristic polynomial. -/ open_locale big_operators universes u w variables {R : Type u} {ι : Type w} namespace polynomial variable (s : finset ι) section comm_semiring variables [comm_semiring R] (f : ι → polynomial R) lemma nat_degree_prod_le : (∏ i in s, f i).nat_degree ≤ ∑ i in s, (f i).nat_degree := begin classical, induction s using finset.induction with a s ha hs, { simp }, rw [prod_insert ha, sum_insert ha], transitivity (f a).nat_degree + (∏ x in s, f x).nat_degree, apply polynomial.nat_degree_mul_le, linarith, end /-- The leading coefficient of a product of polynomials is equal to the product of the leading coefficients, provided that this product is nonzero. See `leading_coeff_prod` (without the `'`) for a version for integral domains, where this condition is automatically satisfied. -/ lemma leading_coeff_prod' (h : ∏ i in s, (f i).leading_coeff ≠ 0) : (∏ i in s, f i).leading_coeff = ∏ i in s, (f i).leading_coeff := begin classical, revert h, induction s using finset.induction with a s ha hs, { simp }, repeat { rw prod_insert ha }, intro h, rw polynomial.leading_coeff_mul'; { rwa hs, apply right_ne_zero_of_mul h }, end /-- The degree of a product of polynomials is equal to the product of the degrees, provided that the product of leading coefficients is nonzero. See `nat_degree_prod` (without the `'`) for a version for integral domains, where this condition is automatically satisfied. -/ lemma nat_degree_prod' (h : ∏ i in s, (f i).leading_coeff ≠ 0) : (∏ i in s, f i).nat_degree = ∑ i in s, (f i).nat_degree := begin classical, revert h, induction s using finset.induction with a s ha hs, { simp }, rw [prod_insert ha, prod_insert ha, sum_insert ha], intro h, rw polynomial.nat_degree_mul', rw hs, apply right_ne_zero_of_mul h, rwa polynomial.leading_coeff_prod', apply right_ne_zero_of_mul h, end lemma nat_degree_prod_of_monic [nontrivial R] (h : ∀ i ∈ s, (f i).monic) : (∏ i in s, f i).nat_degree = ∑ i in s, (f i).nat_degree := begin apply nat_degree_prod', suffices : ∏ i in s, (f i).leading_coeff = 1, { rw this, simp }, rw prod_eq_one, intros, apply h, assumption, end end comm_semiring section comm_ring variables [comm_ring R] open monic -- Eventually this can be generalized with Vieta's formulas -- plus the connection between roots and factorization. lemma prod_X_sub_C_next_coeff [nontrivial R] {s : finset ι} (f : ι → R) : next_coeff ∏ i in s, (X - C (f i)) = -∑ i in s, f i := by { rw next_coeff_prod; { simp [monic_X_sub_C] } } lemma prod_X_sub_C_coeff_card_pred [nontrivial R] (s : finset ι) (f : ι → R) (hs : 0 < s.card) : (∏ i in s, (X - C (f i))).coeff (s.card - 1) = - ∑ i in s, f i := begin convert prod_X_sub_C_next_coeff (by assumption), rw next_coeff, split_ifs, { rw nat_degree_prod_of_monic at h, swap, { intros, apply monic_X_sub_C }, rw sum_eq_zero_iff at h, simp_rw nat_degree_X_sub_C at h, contrapose! h, norm_num, exact multiset.card_pos_iff_exists_mem.mp hs }, congr, rw nat_degree_prod_of_monic; { simp [nat_degree_X_sub_C, monic_X_sub_C] }, end end comm_ring section integral_domain variables [integral_domain R] (f : ι → polynomial R) lemma nat_degree_prod (h : ∀ i ∈ s, f i ≠ 0) : (∏ i in s, f i).nat_degree = ∑ i in s, (f i).nat_degree := begin apply nat_degree_prod', rw prod_ne_zero_iff, intros x hx, simp [h x hx], end lemma leading_coeff_prod : (∏ i in s, f i).leading_coeff = ∏ i in s, (f i).leading_coeff := by { rw ← leading_coeff_hom_apply, apply monoid_hom.map_prod } end integral_domain end polynomial
2cd6b3b703d799d7cf9a3374f24035f3d224a3cf
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/stdio.lean
afb38d9d5745946fb81561c4b235ed57ad9bf4f1
[ "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
767
lean
open IO.FS def usingIO {α} (x : IO α) : IO α := x #eval usingIO do let out ← IO.getStdout; out.putStrLn "print stdout" #eval usingIO do let err ← IO.getStderr; (err.putStr "print stderr" : IO Unit) open usingIO IO def test : IO Unit := do FS.withFile "stdout1.txt" IO.FS.Mode.write $ fun h₁ => do { let h₂ ← FS.Handle.mk "stdout2.txt" IO.FS.Mode.write; withStdout (Stream.ofHandle h₁) $ do println "line 1"; tryCatch ( do withStdout (Stream.ofHandle h₂) $ println "line 2"; throw $ IO.userError "my error" ) ( fun e => println e ); println "line 3" }; println "line 4"; println "\n> stdout1.txt"; readFile "stdout1.txt" >>= print; println "\n> stdout2.txt"; readFile "stdout2.txt" >>= print #eval test
8afdf73863f2b9bfdd76b45517fbab23ff3d5169
0e175f34f8dca5ea099671777e8d7446d7d74227
/library/init/meta/widget/basic.lean
93740611c86a56d9de7c2442cd6032f193033bfa
[ "Apache-2.0" ]
permissive
utensil-contrib/lean
b31266738071c654d96dac8b35d9ccffc8172fda
a28b9c8f78d982a4e82b1e4f7ce7988d87183ae8
refs/heads/master
1,670,045,564,075
1,597,397,599,000
1,597,397,599,000
287,528,503
0
0
Apache-2.0
1,597,408,338,000
1,597,408,337,000
null
UTF-8
Lean
false
false
15,534
lean
/- Copyright (c) E.W.Ayers. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: E.W.Ayers -/ prelude import init.function import init.data.option.basic import init.util import init.meta.tactic import init.meta.mk_dec_eq_instance /-! A component is a piece of UI which may contain internal state. Use component.mk to build new components. ## Using widgets. To make a widget, you need to make a custom executor object and then instead of calling `save_info_thunk` you call `save_widget`. Additionally, you will need a compatible build of the vscode extension or web app to use widgets in vscode. ## How it works: The design is inspired by React. If you are familiar with using React or Elm or a similar functional UI framework then that's helpful for this. The [React article on reconciliation](https://reactjs.org/docs/reconciliation.html) might be helpful. One can imagine making a UI for a particular object as just being a function `f : α → UI` where `UI` is some inductive datatype for buttons, textboxes, lists and so on. The process of evaluating `f` is called __rendering__. So for example `α` could be `tactic_state` and the function renders a goal view. ## HTML For our purposes, `UI` is an HTML tree and is written `html α : Type`. I'm going to assume some familiarity with HTML for the purposes of this document. An HTML tree is composed of elements and strings. Each element has a tag such as "div", "span", "article" and so on and a set of attributes and child html. Use the helper function `h : string → list (attr α) → list (html α) → html α` to build new pieces of `html`. So for example: ```lean h "ul" [] [ h "li" [] ["this is list item 1"], h "li" [style [("color", "blue")]] ["this is list item 2"], h "hr" [] [], h "li" [] [ h "span" [] ["there is a button here"], h "button" [on_click (λ _, 3)] ["click me!"] ] ] ``` Has the type `html nat`. The `nat` type is called the __action__ and whenever the user interacts with the UI, the html will emit an object of type `nat`. So for example if the user clicks the button above, the html will 'emit' `3`. The above example is compiled to the following piece of html: ```html <ul> <li>this is list item 1</li> <li style="{ color: blue; }">this is list item 2</li> <hr/> <li> <span>There is a button here</span> <button onClick="[handler]">click me!</button> </li> </ul> ``` ## Components In order for the UI to react to events, you need to be able to take these actions α and alter some state. To do this we use __components__. `component` takes two type arguments: `π` and `α`. `α` is called the 'action' and `π` are the 'props'. The props can be thought of as a kind of wrapped function domain for `component`. So given `C : component nat α`, one can turn this into html with `html.of_component 4 C : html α`. The base constructor for a component is `pure`: ```lean meta def Hello : component string α := component.pure (λ s, ["hello, ", s, ", good day!"]) #html Hello "lean" -- renders "hello, lean, good day!" ``` So here a pure component is just a simple function `π → list (html α)`. However, one can augment components with __hooks__. The hooks available for compoenents are listed in the inductive definition for component. Here we will just look at the `with_state` hook, which can be used to build components with inner state. ``` meta inductive my_action | increment | decrement open my_action meta def Counter : component unit α := component.with_state my_action -- the action of the inner component int -- the state (λ _, 0) -- initialise the state (λ _ _ s, s) -- update the state if the props change (λ _ s a, -- update the state if an action was received match a with | increment := (s + 1, none) -- replace `none` with `some _` to emit an action | decrement := (s - 1, none) end ) $ component.pure (λ ⟨state, ⟨⟩⟩, [ button "+" (λ _, increment), to_string state, button "-" (λ _, decrement) ]) #html Counter () ``` You can add many hooks to a component. - `filter_map_action` lets you filter or map actions that are emmitted by the component - `map_props` lets you map the props. - `with_should_update` will not re-render the child component if the given test returns false. This can be useful for efficiency. - `with_state` discussed above.` - `with_mouse` subscribes the component to the mouse state, for example whether or not the mouse is over the component. See the `tests/lean/widget/widget_mouse.lean` test for an example. Given an active document, Lean (in server mode) maintains a set of __widgets__ for the document. A widget is a component `c`, some `p : Props` and an internal state-manager which manages the states of the component and subcomponents and also handles the routing of events from the UI. ## Reconciliation If a parent component's state changes, this can cause child components to change position or to appear and dissappear. However we want to preserve the state of these child components where we can. The UI system will try to match up these child components through a process called __reconciliation__. Reconciliation will make sure that the states are carried over correctly and will also not rerender subcomponents if they haven't changed their props or state. To compute whether two components are the same, the system will perform a hash on their VM objects. Not all VM objects can be hashed, so it's important to make sure that any items that you expect to change over the lifetime of the component are fed through the 'Props' argument. This is why we need the props argument on `component`. The reconciliation engine uses the `props_eq` predicate passed to the component constructor to determine whether the props have changed and hence whether the component should be re-rendered. ## Keys If you have some list of components and the list changes according to some state, it is important to add keys to the components so that if two components change order in the list their states are preserved. If you don't provide keys or there are duplicate keys then you may get some strange behaviour in both the Lean widget engine and react. It is possible to use incorrect HTML tags and attributes, there is (currently) no type checking that the result is a valid piece of HTML. So for example, the client widget system will error if you add a `text_change_event` attribute to anything other than an element tagged with `input`. ## Styles with Tachyons The widget system assumes that a stylesheet called 'tachyons' is present. You can find documentation for this stylesheet at [Tachyons.io](http://tachyons.io/). Tachyons was chosen because it is very terse and allows arbitrary styling without using inline styles and without needing to dynamically load a stylesheet. ## Further work (up for grabs!) - Add type checking for html. - Better error handling when the html tree is malformed. - Better error handling when keys are malformed. - Add a 'with_task' which lets long-running operations (eg running `simp`) not block the UI update. - Timers, animation (ambitious). - More event handlers - Drag and drop support. - The current perf bottleneck is sending the full UI across to the server for every update. Instead, it should be possible to send a smaller [JSON Patch](http://jsonpatch.com). Which is already supported by `json.hpp` and javascript ecosystem. -/ namespace widget inductive mouse_event_kind | on_click | on_mouse_enter | on_mouse_leave /-- An effect is an action at the root of the widget component hierarchy and can give instructions to the editor to perform some task. -/ meta inductive effect : Type | insert_text (text : string) | reveal_position (file_name : option string) (p : pos) | highlight_position (file_name : option string) (p : pos) | clear_highlighting | copy_text (text : string) | custom (key : string) (value : string) meta def effects := list effect meta mutual inductive component, html, attr with component : Type → Type → Type | pure {Props Action : Type} (view : Props → list (html Action)) : component Props Action | filter_map_action {Props InnerAction OuterAction} (action_map : Props → InnerAction → option OuterAction) : component Props InnerAction → component Props OuterAction | map_props {Props1 Props2 Action} (map : Props2 → Props1) : component Props1 Action → component Props2 Action | with_should_update {Props Action : Type} (should_update : Π (old new : Props), bool) : component Props Action → component Props Action | with_state {Props Action : Type} (InnerAction State : Type) (init : Props → State) (props_changed : Props → Props → State → State) (update : Props → State → InnerAction → State × option Action) : component (State × Props) InnerAction → component Props Action | with_effects {Props Action : Type} (emit : Props → Action → effects) : component Props Action → component Props Action with html : Type → Type | element {α : Type} (tag : string) (attrs : list (attr α)) (children : list (html α)) : html α | of_string {α : Type} : string → html α | of_component {α : Type} {Props : Type} : Props → component Props α → html α with attr : Type → Type | val {α : Type} (name : string) (value : string) : attr α | mouse_event {α : Type} (kind : mouse_event_kind) (handler : unit → α) : attr α | style {α : Type} : list (string × string) → attr α | tooltip {α : Type} : html α → attr α | text_change_event {α : Type} (handler : string → α) : attr α variables {α β : Type} {π : Type} namespace component meta def map_action (f : α → β) : component π α → component π β | c := filter_map_action (λ p a, some $ f a) c /-- Returns a component that will never trigger an action. -/ meta def ignore_action : component π α → component π β | c := component.filter_map_action (λ p a, none) c meta def ignore_props : component unit α → component π α | c := with_should_update (λ a b, ff) $ component.map_props (λ p, ()) c meta instance : has_coe (component π empty) (component π α) := ⟨component.filter_map_action (λ p x, none)⟩ meta def stateful {π α : Type} (β σ : Type) (init : π → option σ → σ) (update : π → σ → β → σ × option α) (view : π → σ → list (html β)) : component π α := with_state β σ (λ p, init p none) (λ _ p s, init p $ some s) update (component.pure (λ ⟨s,p⟩, view p s)) meta def stateless {π α : Type} [decidable_eq π] (view : π → list (html α)) : component π α := component.with_should_update (λ p1 p2, p1 ≠ p2) $ component.pure view /-- Causes the component to only update on a props change when `test old_props new_props` yields `ff`. -/ meta def with_props_eq (test : π → π → bool) : component π α → component π α | c := component.with_should_update (λ x y, bnot $ test x y) c end component meta mutual def attr.map_action, html.map_action (f : α → β) with attr.map_action : attr α → attr β | (attr.val k v) := attr.val k v | (attr.style s) := attr.style s | (attr.tooltip h) := attr.tooltip $ html.map_action h | (attr.mouse_event k a) := attr.mouse_event k (f ∘ a) | (attr.text_change_event a) := attr.text_change_event (f ∘ a) with html.map_action : html α → html β | (html.element t a c) := html.element t (list.map attr.map_action a) (list.map html.map_action c) | (html.of_string s) := html.of_string s | (html.of_component p c) := html.of_component p $ component.map_action f c meta instance attr.is_functor : functor attr := { map := @attr.map_action } meta instance html.is_functor : functor html := { map := λ _ _, html.map_action } namespace html meta instance to_string_coe [has_to_string β] : has_coe β (html α) := ⟨html.of_string ∘ to_string⟩ meta instance : has_emptyc (html α) := ⟨of_string ""⟩ meta instance list_coe : has_coe (html α) (list (html α)) := ⟨λ x, [x]⟩ end html meta def as_element : html α → option (string × list (attr α) × list (html α)) | (html.element t a c) := some ⟨t,a,c⟩ | _ := none meta def key [has_to_string β] : β → attr α | s := attr.val "key" $ to_string s meta def className : string → attr α | s := attr.val "className" $ s meta def on_click : (unit → α) → attr α | a := attr.mouse_event mouse_event_kind.on_click a meta def on_mouse_enter : (unit → α) → attr α | a := attr.mouse_event mouse_event_kind.on_mouse_enter a meta def on_mouse_leave : (unit → α) → attr α | a := attr.mouse_event mouse_event_kind.on_mouse_leave a /-- Alias for `html.element`. -/ meta def h : string → list (attr α) → list (html α) → html α := html.element /-- Alias for className. -/ meta def cn : string → attr α := className meta def button : string → thunk α → html α | s t := h "button" [on_click t] [s] meta def textbox : string → (string → α) → html α | s t := h "input" [attr.val "type" "text", attr.val "value" s, attr.text_change_event t] [] meta structure select_item (α : Type) := (result : α) (key : string) (view : list (html α)) /-- Choose from a dropdown selection list. -/ meta def select {α} [decidable_eq α] : list (select_item α) → α → html α | items value := let k := match list.filter (λ i, select_item.result i = value) items with | [] := "" | (h::_) := select_item.key h end in h "select" [ attr.val "value" k, attr.val "key" k, attr.text_change_event (λ k, match items.filter (λ i, select_item.key i = k) with | [] := undefined | (h::_) := h.result end )] $ items.map (λ i, h "option" [attr.val "value" i.key] $ select_item.view i) /-- If the html is not an of_element it will wrap it in a div. -/ meta def with_attrs : list (attr α) → html α → html α | a x := match as_element x with | (some ⟨t,as,c⟩) := html.element t (a ++ as) c | none := html.element "div" a [x] end /-- If the html is not an of_element it will wrap it in a div. -/ meta def with_attr : attr α → html α → html α | a x := with_attrs [a] x meta def with_style : string → string → html α → html α | k v h := with_attr (attr.style [(k,v)]) h meta def with_cn : string → html α → html α | s h := with_attr (className s) h meta def with_key {β} [has_to_string β] : β → html α → html α | s h := with_attr (key s) h end widget namespace tactic /-- Same as `tactic.save_info_thunk` except saves a widget to be displayed by a compatible infoviewer. -/ meta constant save_widget : pos → widget.component tactic_state empty → tactic unit /-- Outputs a widget trace position at the given position. -/ meta constant trace_widget_at (p : pos) (w : widget.component tactic_state empty) (text := "(widget)") : tactic unit /-- Outputs a widget trace position at the current default trace position. -/ meta def trace_widget (w : widget.component tactic_state empty) (text := "(widget)") : tactic unit := do p ← get_trace_msg_pos, trace_widget_at p w text end tactic
9088d37981a7034e64c95b2b849d65fa8b13d1bf
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/tactic/wlog.lean
67af4ce1411fcb074d73bbc309bb3585cfdcc3da
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
9,104
lean
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl Without loss of generality tactic. -/ import data.list.perm open expr setup_tactic_parser namespace tactic private meta def update_pp_name : expr → name → expr | (local_const n _ bi d) pp := local_const n pp bi d | e n := e private meta def elim_or : ℕ → expr → tactic (list expr) | 0 h := fail "zero cases" | 1 h := return [h] | (n + 1) h := do [(_, [hl], []), (_, [hr], [])] ← induction h, -- there should be no dependent terms [gl, gr] ← get_goals, set_goals [gr], hsr ← elim_or n hr, gsr ← get_goals, set_goals (gl :: gsr), return (hl :: hsr) private meta def dest_or : expr → tactic (list expr) | e := do `(%%a ∨ %%b) ← whnf e | return [e], lb ← dest_or b, return (a :: lb) private meta def match_perms (pat : pattern) : expr → tactic (list $ list expr) | t := (do m ← match_pattern pat t, guard (m.2.all expr.is_local_constant), return [m.2]) <|> (do `(%%l ∨ %%r) ← whnf t, m ← match_pattern pat l, rs ← match_perms r, return (m.2 :: rs)) meta def wlog (vars' : list expr) (h_cases fst_case : expr) (perms : list (list expr)) : tactic unit := do guard h_cases.is_local_constant, -- reorder s.t. context is Γ ⬝ vars ⬝ cases ⊢ ∀deps, … nr ← revert_lst (vars' ++ [h_cases]), vars ← intron' vars'.length, h_cases ← intro h_cases.local_pp_name, cases ← infer_type h_cases, h_fst_case ← mk_local_def h_cases.local_pp_name (fst_case.instantiate_locals $ (vars'.zip vars).map $ λ⟨o, n⟩, (o.local_uniq_name, n)), ((), pr) ← solve_aux cases (repeat $ exact h_fst_case <|> left >> skip), t ← target, fixed_vars ← vars.mmap update_type, let t' := (instantiate_local h_cases.local_uniq_name pr t).pis (fixed_vars ++ [h_fst_case]), (h, [g]) ← local_proof `this t' (do clear h_cases, vars.mmap clear, intron nr), h₀ :: hs ← elim_or perms.length h_cases, solve1 (do exact (h.mk_app $ vars ++ [h₀])), focus ((hs.zip perms.tail).map $ λ⟨h_case, perm⟩, do let p_v := (vars'.zip vars).map (λ⟨p, v⟩, (p.local_uniq_name, v)), let p := perm.map (λp, p.instantiate_locals p_v), note `this none (h.mk_app $ p ++ [h_case]), clear h, return ()), gs ← get_goals, set_goals (g :: gs) namespace interactive open interactive interactive.types expr private meta def parse_permutations : option (list (list name)) → tactic (list (list expr)) | none := return [] | (some []) := return [] | (some perms@(p₀ :: ps)) := do (guard p₀.nodup <|> fail "No permutation `xs_i` in `using [xs_1, …, xs_n]` should contain the same variable twice."), (guard (perms.all $ λp, p.perm p₀) <|> fail ("The permutations `xs_i` in `using [xs_1, …, xs_n]` must be permutations of the same" ++ " variables.")), perms.mmap (λp, p.mmap get_local) /-- Without loss of generality: reduces to one goal under variables permutations. Given a goal of the form `g xs`, a predicate `p` over a set of variables, as well as variable permutations `xs_i`. Then `wlog` produces goals of the form * The case goal, i.e. the permutation `xs_i` covers all possible cases: `⊢ p xs_0 ∨ ⋯ ∨ p xs_n` * The main goal, i.e. the goal reduced to `xs_0`: `(h : p xs_0) ⊢ g xs_0` * The invariant goals, i.e. `g` is invariant under `xs_i`: `(h : p xs_i) (this : g xs_0) ⊢ gs xs_i` Either the permutation is provided, or a proof of the disjunction is provided to compute the permutation. The disjunction need to be in assoc normal form, e.g. `p₀ ∨ (p₁ ∨ p₂)`. In many cases the invariant goals can be solved by AC rewriting using `cc` etc. For example, on a state `(n m : ℕ) ⊢ p n m` the tactic `wlog h : n ≤ m using [n m, m n]` produces the following states: * `(n m : ℕ) ⊢ n ≤ m ∨ m ≤ n` * `(n m : ℕ) (h : n ≤ m) ⊢ p n m` * `(n m : ℕ) (h : m ≤ n) (this : p n m) ⊢ p m n` `wlog` supports different calling conventions. The name `h` is used to give a name to the introduced case hypothesis. If the name is avoided, the default will be `case`. 1. `wlog : p xs0 using [xs0, …, xsn]` Results in the case goal `p xs0 ∨ ⋯ ∨ ps xsn`, the main goal `(case : p xs0) ⊢ g xs0` and the invariance goals `(case : p xsi) (this : g xs0) ⊢ g xsi`. 2. `wlog : p xs0 := r using xs0` The expression `r` is a proof of the shape `p xs0 ∨ ⋯ ∨ p xsi`, it is also used to compute the variable permutations. 3. `wlog := r using xs0` The expression `r` is a proof of the shape `p xs0 ∨ ⋯ ∨ p xsi`, it is also used to compute the variable permutations. This is not as stable as (2), for example `p` cannot be a disjunction. 4. `wlog : R x y using x y` and `wlog : R x y` Produces the case `R x y ∨ R y x`. If `R` is ≤, then the disjunction discharged using linearity. If `using x y` is avoided then `x` and `y` are the last two variables appearing in the expression `R x y`. -/ meta def wlog (h : parse ident?) (pat : parse (tk ":" *> texpr)?) (cases : parse (tk ":=" *> texpr)?) (perms : parse (tk "using" *> (list_of (ident*) <|> (λx, [x]) <$> ident*))?) (discharger : tactic unit := (tactic.solve_by_elim <|> tactic.tautology {classical := tt} <|> using_smt (smt_tactic.intros >> smt_tactic.solve_goals))) : tactic unit := do perms ← parse_permutations perms, (pat, cases_pr, cases_goal, vars, perms) ← (match cases with | some r := do vars::_ ← return perms | fail "At least one set of variables expected, i.e. `using x y` or `using [x y, y x]`.", cases_pr ← to_expr r, cases_pr ← (if cases_pr.is_local_constant then return $ match h with some n := update_pp_name cases_pr n | none := cases_pr end else do note (h.get_or_else `case) none cases_pr), cases ← infer_type cases_pr, (pat, perms') ← match pat with | some pat := do pat ← to_expr pat, let vars' := vars.filter $ λv, v.occurs pat, case_pat ← tactic.mk_pattern [] vars' pat [] vars', perms' ← match_perms case_pat cases, return (pat, perms') | none := do (p :: ps) ← dest_or cases, let vars' := vars.filter $ λv, v.occurs p, case_pat ← tactic.mk_pattern [] vars' p [] vars', perms' ← (p :: ps).mmap (λp, do m ← match_pattern case_pat p, return m.2), return (p, perms') end, let vars_name := vars.map local_uniq_name, guard (perms'.all $ λp, p.all $ λv, v.is_local_constant ∧ v.local_uniq_name ∈ vars_name) <|> fail "Cases contains variables not declared in `using x y z`", perms ← (if perms.length = 1 then do return (perms'.map $ λ p, p ++ vars.filter (λ v, p.all (λ v', v'.local_uniq_name ≠ v.local_uniq_name))) else do guard (perms.length = perms'.length) <|> fail "The provided permutation list has a different length then the provided cases.", return perms), return (pat, cases_pr, @none expr, vars, perms) | none := do let name_h := h.get_or_else `case, some pat ← return pat | fail "Either specify cases or a pattern with permutations", pat ← to_expr pat, (do [x, y] ← match perms with | [] := return pat.list_local_consts | [l] := return l | _ := failed end, let cases := mk_or_lst [pat, pat.instantiate_locals [(x.local_uniq_name, y), (y.local_uniq_name, x)]], (do `(%%x' ≤ %%y') ← return pat, (cases_pr, []) ← local_proof name_h cases (exact ``(le_total %%x' %%y')), return (pat, cases_pr, none, [x, y], [[x, y], [y, x]])) <|> (do (cases_pr, [g]) ← local_proof name_h cases skip, return (pat, cases_pr, some g, [x, y], [[x, y], [y, x]]))) <|> (do guard (perms.length ≥ 2) <|> fail ("To generate cases at least two permutations are required, i.e. `using [x y, y x]`" ++ " or exactly 0 or 2 variables"), (vars :: perms') ← return perms, let names := vars.map local_uniq_name, let cases := mk_or_lst (pat :: perms'.map (λp, pat.instantiate_locals (names.zip p))), (cases_pr, [g]) ← local_proof name_h cases skip, return (pat, cases_pr, some g, vars, perms)) end), let name_fn := if perms.length = 2 then λ _, `invariant else λ i, mk_simple_name ("invariant_" ++ to_string (i + 1)), with_enable_tags $ tactic.focus1 $ do t ← get_main_tag, tactic.wlog vars cases_pr pat perms, tactic.focus (set_main_tag (mk_num_name `_case 0 :: `main :: t) :: (list.range (perms.length - 1)).map (λi, do set_main_tag (mk_num_name `_case 0 :: name_fn i :: t), try discharger)), match cases_goal with | some g := do set_tag g (mk_num_name `_case 0 :: `cases :: t), gs ← get_goals, set_goals (g :: gs) | none := skip end add_tactic_doc { name := "wlog", category := doc_category.tactic, decl_names := [``wlog], tags := ["logic"] } end interactive end tactic
d26d9f3dcfb7fe9835b67d9cd18149c818015e5e
dd4e652c749fea9ac77e404005cb3470e5f75469
/src/alex_playground/filter.lean
eff012d5535ad8af6bc2ac934355f74b6cc756da
[]
no_license
skbaek/cvx
e32822ad5943541539966a37dee162b0a5495f55
c50c790c9116f9fac8dfe742903a62bdd7292c15
refs/heads/master
1,623,803,010,339
1,618,058,958,000
1,618,058,958,000
176,293,135
3
2
null
null
null
null
UTF-8
Lean
false
false
4,188
lean
import data.real.basic import order.lattice noncomputable theory section curried variables {α α' β γ δ : Type*} class curried (α β γ δ : Type*) := (comp (f : γ → δ) : α → β) instance curried_base : curried (α → γ) (α → δ) γ δ := ⟨λ f g x, f (g x)⟩ instance curried_induction [curried α β γ δ] : curried (α' → α) (α' → β) γ δ := ⟨λ f g x, curried.comp f (g x)⟩ end curried example: curried.comp (nat.succ) (λ x y z : ℕ, 0) = (λ x y z : ℕ, 1) := rfl universe variables u v w x -- Need filters for non-sets! (Mario already proposed that refactoring) -- Add non-empty axiom? structure filter (α : Type*) [partial_order α] := (elements : set α) (nonempty : ∃ x, x ∈ elements) (exists_inf (x y : α) : x ∈ elements → y ∈ elements → ∃ z, z ≤ x ∧ z ≤ y ∧ z ∈ elements) (mem_elements_of_le (x y : α) : x ∈ elements → x ≤ y → y ∈ elements) instance {α : Type*} [partial_order α] : has_mem α (filter α) := ⟨λ U F, U ∈ F.elements⟩ namespace filter variables {α : Type u} variables [partial_order α] {f g : filter α} {s t : α} open set @[simp] protected lemma mem_mk {t : set α} {h₁ h₂ h₃} : s ∈ mk t h₁ h₂ h₃ ↔ s ∈ t := iff.rfl @[simp] protected lemma mem_elements : s ∈ f.elements ↔ s ∈ f := iff.rfl lemma filter_eq : ∀{f g : filter α}, f.elements = g.elements → f = g | ⟨_, _, _, _⟩ ⟨_, _, _, _⟩ rfl := rfl lemma filter_eq_iff : f = g ↔ f.elements = g.elements := ⟨congr_arg _, filter_eq⟩ protected lemma ext_iff : f = g ↔ ∀ s, s ∈ f ↔ s ∈ g := by simp only [filter_eq_iff, ext_iff, filter.mem_elements] @[ext] protected lemma ext : (∀ s, s ∈ f ↔ s ∈ g) → f = g := filter.ext_iff.2 lemma mem_sets_of_superset : ∀{x y : α}, x ∈ f → x ≤ y → y ∈ f := f.mem_elements_of_le section principal /-- The principal filter of `s` is the set of all elements larger than or equal to `s`. -/ def principal (s : α) : filter α := { elements := {t | s ≤ t}, nonempty := ⟨s, le_refl s⟩, exists_inf := λ x y hx hy, ⟨s, hx, hy, le_refl s⟩, mem_elements_of_le := λ x y hx hxy, le_trans hx hxy } localized "notation `𝓟` := filter.principal" in filter instance [inhabited α]: inhabited (filter α) := ⟨𝓟 (default α)⟩ instance : complete_lattice (filter α) := sorry end principal open_locale filter def at_top [preorder α] : filter (set α) := ⨅ a, 𝓟 (Ici a) end filter def filter.Limsup (f : filter (set ℝ)) : ℝ := Inf { a | {n | n ≤ a} ∈ f } variables {α α' β γ δ : Type*} [partial_order β] def filter.map (f : filter (set ℝ)) [curried α β ℝ Prop] (u : α) : filter β := sorry def limsup (f : filter (set ℝ)) [curried α β ℝ Prop] (u : α) : ℝ := (f.map u).Limsup def at_top [has_bot α] [partial_order β] [preorder γ] [curried α β ℝ Prop] : filter β := ⨅ a : ℝ, filter.principal (curried.comp (λ x, a ≤ x) (⊥ : α)) -- section semilattice_inf -- variables {α : Type u} -- variables [semilattice_inf α] {f g : filter α} {s t : α} -- open semilattice_inf -- namespace filter -- def of_semilattice (α : Type*) [semilattice_inf α] (elements : set α) -- (inf_in_elements : ∀ x y, x ∈ elements → y ∈ elements → semilattice_inf.inf x y ∈ elements) -- (in_elements_of_le : ∀ x y, x ∈ elements → x ≤ y → y ∈ elements) : -- filter α := -- example: semilattice_inf (ℕ → ℕ → ℕ → ℕ → ℕ → ℕ → ℕ → Prop) := by apply_instance -- def of_set (α : Type*) (elements : set (set α)) -- (inf_in_elements : ∀ x y, x ∈ elements → y ∈ elements → x ∩ y ∈ elements) -- (in_elements_of_le : ∀ x y, x ∈ elements → x ⊆ y → y ∈ elements) : -- filter (set α) := -- of_semilattice (set α) elements inf_in_elements in_elements_of_le -- end filter -- def solution (f : ℝ → ℝ) := -- { a : ℝ // a = filter.at_bot.liminf (λ x, f x)} def solution (f : ℝ → ℝ) := Σ' a : ℝ, a = filter.at_bot.liminf (λ x, f x) #check filter.liminf #check optimize (λ (x : ℝ) (y : ℝ) (h : x + y ≤ 0), x - y)
48b870e483bfa276a9374ada1b5e0209e4c395e2
a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940
/stage0/src/Lean/Meta/PPGoal.lean
9df31bfbe76765eb1cefee4ac48010c3f04a9d88
[ "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
9,293
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ import Lean.Meta.InferType namespace Lean.Meta register_builtin_option pp.auxDecls : Bool := { defValue := false group := "pp" descr := "display auxiliary declarations used to compile recursive functions" } register_builtin_option pp.inaccessibleNames : Bool := { defValue := false group := "pp" descr := "display inaccessible declarations in the local context" } def withPPInaccessibleNamesImp (flag : Bool) (x : MetaM α) : MetaM α := withTheReader Core.Context (fun ctx => { ctx with options := pp.inaccessibleNames.setIfNotSet ctx.options flag }) x def withPPInaccessibleNames [MonadControlT MetaM m] [Monad m] (x : m α) (flag := true) : m α := mapMetaM (withPPInaccessibleNamesImp flag) x namespace ToHide structure State where hiddenInaccessibleProp : NameSet := {} -- FVarIds of Propostions with inaccessible names but containing only visible names. We show only their types hiddenInaccessible : NameSet := {} -- FVarIds with inaccessible names, but not in hiddenInaccessibleProp modified : Bool := false structure Context where goalTarget : Expr abbrev M := ReaderT Context $ StateRefT State MetaM /- Return true if `fvarId` is marked as an hidden inaccessible or inaccessible proposition -/ def isMarked (fvarId : FVarId) : M Bool := do let s ← get return s.hiddenInaccessible.contains fvarId || s.hiddenInaccessibleProp.contains fvarId /- If `fvarId` isMarked, then unmark it. -/ def unmark (fvarId : FVarId) : M Unit := do modify fun s => { hiddenInaccessible := s.hiddenInaccessible.erase fvarId hiddenInaccessibleProp := s.hiddenInaccessibleProp.erase fvarId modified := true } def moveToHiddeProp (fvarId : FVarId) : M Unit := do modify fun s => { hiddenInaccessible := s.hiddenInaccessible.erase fvarId hiddenInaccessibleProp := s.hiddenInaccessibleProp.insert fvarId modified := true } /- Return true if the given local declaration has a "visible dependency", that is, it contains a free variable that is `hiddenInaccessible` Recall that hiddenInaccessibleProps are visible, only their names are hidden -/ def hasVisibleDep (localDecl : LocalDecl) : M Bool := do let s ← get return (← getMCtx).findLocalDeclDependsOn localDecl fun fvarId => !s.hiddenInaccessible.contains fvarId /- Return true if the given local declaration has a "nonvisible dependency", that is, it contains a free variable that is `hiddenInaccessible` or `hiddenInaccessibleProp` -/ def hasInaccessibleNameDep (localDecl : LocalDecl) : M Bool := do let s ← get return (← getMCtx).findLocalDeclDependsOn localDecl fun fvarId => s.hiddenInaccessible.contains fvarId || s.hiddenInaccessibleProp.contains fvarId /- If `e` is visible, then any inaccessible in `e` marked as hidden should be unmarked. -/ partial def visitVisibleExpr (e : Expr) : M Unit := do visit (← instantiateMVars e) |>.run where visit (e : Expr) : MonadCacheT Expr Unit M Unit := do if e.hasFVar then checkCache e fun _ => do match e with | Expr.forallE _ d b _ => visit d; visit b | Expr.lam _ d b _ => visit d; visit b | Expr.letE _ t v b _ => visit t; visit v; visit b | Expr.app f a _ => visit f; visit a | Expr.mdata _ b _ => visit b | Expr.proj _ _ b _ => visit b | Expr.fvar fvarId _ => if (← isMarked fvarId) then unmark fvarId | _ => pure () def fixpointStep : M Unit := do visitVisibleExpr (← read).goalTarget -- The goal target is a visible forward dependency (← getLCtx).forM fun localDecl => do let fvarId := localDecl.fvarId if (← get).hiddenInaccessible.contains fvarId then if (← hasVisibleDep localDecl) then /- localDecl is marked to be hidden, but it has a (backward) visible dependency. -/ unmark fvarId if (← isProp localDecl.type) then unless (← hasInaccessibleNameDep localDecl) do moveToHiddeProp fvarId else visitVisibleExpr localDecl.type match localDecl.value? with | some value => visitVisibleExpr value | _ => pure () partial def fixpoint : M Unit := do modify fun s => { s with modified := false } fixpointStep if (← get).modified then fixpoint /- If pp.inaccessibleNames == false, then collect two sets of `FVarId`s : `hiddenInaccessible` and `hiddenInaccessibleProp` 1- `hiddenInaccessible` contains `FVarId`s of free variables with inaccessible names that a) are not propositions or are propositions containing "visible" names. 2- `hiddenInaccessibleProp` contains `FVarId`s of free variables with inaccessible names that are propositions containing "visible" names. Both sets do not contain `FVarId`s that contain visible backward or forward dependencies. The `goalTarget` counts as a forward dependency. We say a name is visible if it is a free variable with FVarId not in `hiddenInaccessible` nor `hiddenInaccessibleProp` -/ def collect (goalTarget : Expr) : MetaM (NameSet × NameSet) := do if pp.inaccessibleNames.get (← getOptions) then /- Don't hide inaccessible names when `pp.inaccessibleNames` is set to true. -/ return ({}, {}) else let lctx ← getLCtx let hiddenInaccessible := lctx.foldl (init := {}) fun hiddenInaccessible localDecl => do if localDecl.userName.isInaccessibleUserName then hiddenInaccessible.insert localDecl.fvarId else hiddenInaccessible let (_, s) ← fixpoint.run { goalTarget := goalTarget } |>.run { hiddenInaccessible := hiddenInaccessible } return (s.hiddenInaccessible, s.hiddenInaccessibleProp) end ToHide private def addLine (fmt : Format) : Format := if fmt.isNil then fmt else fmt ++ Format.line def ppGoal (mvarId : MVarId) : MetaM Format := do match (← getMCtx).findDecl? mvarId with | none => pure "unknown goal" | some mvarDecl => do let indent := 2 -- Use option let ppAuxDecls := pp.auxDecls.get (← getOptions) let lctx := mvarDecl.lctx let lctx := lctx.sanitizeNames.run' { options := (← getOptions) } withLCtx lctx mvarDecl.localInstances do let (hidden, hiddenProp) ← ToHide.collect mvarDecl.type -- The followint two `let rec`s are being used to control the generated code size. -- Then should be remove after we rewrite the compiler in Lean let rec pushPending (ids : List Name) (type? : Option Expr) (fmt : Format) : MetaM Format := if ids.isEmpty then pure fmt else let fmt := addLine fmt match ids, type? with | [], _ => pure fmt | _, none => pure fmt | _, some type => do let typeFmt ← ppExpr type pure $ fmt ++ (Format.joinSep ids.reverse " " ++ " :" ++ Format.nest indent (Format.line ++ typeFmt)).group let rec ppVars (varNames : List Name) (prevType? : Option Expr) (fmt : Format) (localDecl : LocalDecl) : MetaM (List Name × Option Expr × Format) := do if hiddenProp.contains localDecl.fvarId then let fmt ← pushPending varNames prevType? fmt let fmt := addLine fmt let type ← instantiateMVars localDecl.type let typeFmt ← ppExpr type let fmt := fmt ++ " : " ++ typeFmt pure ([], none, fmt) else match localDecl with | LocalDecl.cdecl _ _ varName type _ => let varName := varName.simpMacroScopes let type ← instantiateMVars type if prevType? == none || prevType? == some type then pure (varName :: varNames, some type, fmt) else do let fmt ← pushPending varNames prevType? fmt pure ([varName], some type, fmt) | LocalDecl.ldecl _ _ varName type val _ => do let varName := varName.simpMacroScopes let fmt ← pushPending varNames prevType? fmt let fmt := addLine fmt let type ← instantiateMVars type let val ← instantiateMVars val let typeFmt ← ppExpr type let valFmt ← ppExpr val let fmt := fmt ++ (format varName ++ " : " ++ typeFmt ++ " :=" ++ Format.nest indent (Format.line ++ valFmt)).group pure ([], none, fmt) let (varNames, type?, fmt) ← lctx.foldlM (init := ([], none, Format.nil)) fun (varNames, prevType?, fmt) (localDecl : LocalDecl) => if !ppAuxDecls && localDecl.isAuxDecl || hidden.contains localDecl.fvarId then pure (varNames, prevType?, fmt) else ppVars varNames prevType? fmt localDecl let fmt ← pushPending varNames type? fmt let fmt := addLine fmt let typeFmt ← ppExpr mvarDecl.type let fmt := fmt ++ "⊢" ++ " " ++ Format.nest indent typeFmt match mvarDecl.userName with | Name.anonymous => pure fmt | name => return "case " ++ format name.eraseMacroScopes ++ Format.line ++ fmt end Lean.Meta
fc35d31ecc59d3fe143007b818634c2e9c6df06a
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/script/apply.lean
70bf754cde354cae676639cc2f87b8584a7448e5
[ "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
6,976
lean
import Lean.Runtime abbrev M := ReaderT IO.FS.Stream IO def M.run (a : M Unit) (out? : Option String := none) : IO Unit := match out? with | some out => do IO.FS.withFile out .write (ReaderT.run a ∘ .ofHandle) | none => IO.getStdout >>= (ReaderT.run a) def emit (s : String) : M Unit := do (← read).putStr s def mkTypedefFn (i : Nat) : M Unit := do let args := (List.replicate i "obj*").intersperse ", " |> String.join emit s!"typedef obj* (*fn{i})({args}); // NOLINT\n" emit s!"#define FN{i}(f) reinterpret_cast<fn{i}>(lean_closure_fun(f))\n" def genSeq (n : Nat) (f : Nat → String) (sep := ", ") : String := List.range n |>.map f |>.intersperse sep |> .join -- make string: "obj* a1, obj* a2, ..., obj* an" def mkArgDecls (n : Nat) : String := genSeq n (s!"obj* a{·+1}") -- make string: "a{s+1}, ..., a{n}" def mkArgsFrom (s n : Nat) : String := genSeq (n-s) (s!"a{s+·+1}") -- make string: "a1, a2, ..., a{n}" def mkArgs (n : Nat) : String := mkArgsFrom 0 n -- make string: "as[0], as[1], ..., as[n-1]" def mkAsArgs (n : Nat) : String := genSeq n (s!"as[{·}]") -- make string: "fx(0), ..., fx(n-1)" def mkFsArgs (n : Nat) : String := genSeq n (s!"fx({·})") -- make string: "lean_inc(fx(0)); ...; lean_inc(fx(n-1))" def mkIncFs (n : Nat) : String := genSeq n (s!"lean_inc(fx({·})); ") (sep := "") def mkApplyI (n : Nat) (max : Nat) : M Unit := do let argDecls := mkArgDecls n let args := mkArgs n emit s!"extern \"C\" LEAN_EXPORT obj* lean_apply_{n}(obj* f, {argDecls}) \{ if (lean_is_scalar(f)) \{ {genSeq n (s!"lean_dec(a{·+1}); ") (sep := "")}return f; } // f is an erased proof unsigned arity = lean_closure_arity(f); unsigned fixed = lean_closure_num_fixed(f); if (arity == fixed + {n}) \{ if (lean_is_exclusive(f)) \{ switch (arity) \{\n" for j in [n:max + 1] do let fs := mkFsArgs (j - n) let sep := if j = n then "" else ", " emit s!" case {j}: \{ obj* r = FN{j}(f)({fs}{sep}{args}); lean_free_small_object(f); return r; }\n" emit " } } switch (arity) {\n" for j in [n:max + 1] do let lean_incfs := mkIncFs (j - n) let fs := mkFsArgs (j - n) let sep := if j = n then "" else ", " emit s!" case {j}: \{ {lean_incfs}obj* r = FN{j}(f)({fs}{sep}{args}); lean_dec_ref(f); return r; }\n" emit s!" default: lean_assert(arity > {max}); obj * as[{n}] = \{ {args} }; obj ** args = static_cast<obj**>(LEAN_ALLOCA(arity*sizeof(obj*))); // NOLINT for (unsigned i = 0; i < fixed; i++) \{ lean_inc(fx(i)); args[i] = fx(i); } for (unsigned i = 0; i < {n}; i++) args[fixed+i] = as[i]; obj * r = FNN(f)(args); lean_dec_ref(f); return r; } } else if (arity < fixed + {n}) \{\n" if n ≥ 2 then do emit s!" obj * as[{n}] = \{ {args} }; obj ** args = static_cast<obj**>(LEAN_ALLOCA(arity*sizeof(obj*))); // NOLINT for (unsigned i = 0; i < fixed; i++) \{ lean_inc(fx(i)); args[i] = fx(i); } for (unsigned i = 0; i < arity-fixed; i++) args[fixed+i] = as[i]; obj * new_f = curry(f, arity, args); lean_dec_ref(f); return lean_apply_n(new_f, {n}+fixed-arity, &as[arity-fixed]);\n" else emit s!" lean_assert(fixed < arity); lean_unreachable();\n" emit s!"} else \{ return fix_args(f, \{{args}}); } }\n" def mkCurry (max : Nat) : M Unit := do emit "obj* curry(void* f, unsigned n, obj** as) { switch (n) { case 0: lean_unreachable();\n" for i in [0:max] do let as := mkAsArgs (i+1) emit s!"case {i+1}: return reinterpret_cast<fn{i+1}>(f)({as});\n" emit "default: return reinterpret_cast<fnn>(f)(as); } } static obj* curry(obj* f, unsigned n, obj** as) { return curry(lean_closure_fun(f), n, as); }\n" def mkApplyN (max : Nat) : M Unit := do emit "extern \"C\" LEAN_EXPORT obj* lean_apply_n(obj* f, unsigned n, obj** as) { switch (n) { case 0: lean_unreachable();\n" for i in [0:max] do let as := mkAsArgs (i+1) emit s!"case {i+1}: return lean_apply_{i+1}(f, {as});\n" emit "default: return lean_apply_m(f, n, as); } }\n" def mkApplyM (max : Nat) : M Unit := emit s!"extern \"C\" LEAN_EXPORT obj* lean_apply_m(obj* f, unsigned n, obj** as) \{ lean_assert(n > {max}); if (lean_is_scalar(f)) \{ for (unsigned i = 0; i < n; i++) \{ lean_dec(as[i]); } return f; } // f is an erased proof unsigned arity = lean_closure_arity(f); unsigned fixed = lean_closure_num_fixed(f); if (arity == fixed + n) \{ obj ** args = static_cast<obj**>(LEAN_ALLOCA(arity*sizeof(obj*))); // NOLINT for (unsigned i = 0; i < fixed; i++) \{ lean_inc(fx(i)); args[i] = fx(i); } for (unsigned i = 0; i < n; i++) args[fixed+i] = as[i]; obj * r = FNN(f)(args); lean_dec_ref(f); return r; } else if (arity < fixed + n) \{ obj ** args = static_cast<obj**>(LEAN_ALLOCA(arity*sizeof(obj*))); // NOLINT for (unsigned i = 0; i < fixed; i++) \{ lean_inc(fx(i)); args[i] = fx(i); } for (unsigned i = 0; i < arity-fixed; i++) args[fixed+i] = as[i]; obj * new_f = FNN(f)(args); lean_dec_ref(f); return lean_apply_n(new_f, n+fixed-arity, &as[arity-fixed]); } else \{ return fix_args(f, n, as); } }\n" def mkFixArgs : M Unit := emit " static obj* fix_args(obj* f, unsigned n, obj*const* as) { unsigned arity = lean_closure_arity(f); unsigned fixed = lean_closure_num_fixed(f); unsigned new_fixed = fixed + n; lean_assert(new_fixed < arity); obj * r = lean_alloc_closure(lean_closure_fun(f), arity, new_fixed); obj ** source = lean_closure_arg_cptr(f); obj ** target = lean_closure_arg_cptr(r); if (!lean_is_exclusive(f)) { for (unsigned i = 0; i < fixed; i++, source++, target++) { *target = *source; lean_inc(*target); } lean_dec_ref(f); } else { for (unsigned i = 0; i < fixed; i++, source++, target++) { *target = *source; } lean_free_small_object(f); } for (unsigned i = 0; i < n; i++, as++, target++) { *target = *as; } return r; } static inline obj* fix_args(obj* f, std::initializer_list<obj*> const & l) { return fix_args(f, l.size(), l.begin()); } " def mkCopyright : M Unit := emit "/* Copyright (c) 2018 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura */ " def mkApplyCpp (max : Nat) : M Unit := do mkCopyright emit "// DO NOT EDIT, this is an automatically generated file // Generated using script: ../../gen/apply.lean #include \"runtime/apply.h\" namespace lean { #define obj lean_object #define fx(i) lean_closure_arg_cptr(f)[i]\n" mkFixArgs for i in [0:max] do mkTypedefFn (i+1) emit "typedef obj* (*fnn)(obj**); // NOLINT #define FNN(f) reinterpret_cast<fnn>(lean_closure_fun(f))\n" mkCurry max emit "extern \"C\" obj* lean_apply_n(obj*, unsigned, obj**);\n" for i in [0:max] do mkApplyI (i+1) max mkApplyM max mkApplyN max emit "}\n" -- #eval (mkApplyCpp 4).run none #eval (mkApplyCpp Lean.closureMaxArgs).run "src/runtime/apply.cpp"
875c61278217104af44595eae03ec3943ffc555d
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/set/function.lean
f2bc48dc31da13f4d9921533487befd2c6558e32
[ "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
60,916
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Andrew Zipperer, Haitao Zhang, Minchao Wu, Yury Kudryashov -/ import data.set.prod import logic.function.conjugate /-! # Functions over sets > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. ## Main definitions ### Predicate * `set.eq_on f₁ f₂ s` : functions `f₁` and `f₂` are equal at every point of `s`; * `set.maps_to f s t` : `f` sends every point of `s` to a point of `t`; * `set.inj_on f s` : restriction of `f` to `s` is injective; * `set.surj_on f s t` : every point in `s` has a preimage in `s`; * `set.bij_on f s t` : `f` is a bijection between `s` and `t`; * `set.left_inv_on f' f s` : for every `x ∈ s` we have `f' (f x) = x`; * `set.right_inv_on f' f t` : for every `y ∈ t` we have `f (f' y) = y`; * `set.inv_on f' f s t` : `f'` is a two-side inverse of `f` on `s` and `t`, i.e. we have `set.left_inv_on f' f s` and `set.right_inv_on f' f t`. ### Functions * `set.restrict f s` : restrict the domain of `f` to the set `s`; * `set.cod_restrict f s h` : given `h : ∀ x, f x ∈ s`, restrict the codomain of `f` to the set `s`; * `set.maps_to.restrict f s t h`: given `h : maps_to f s t`, restrict the domain of `f` to `s` and the codomain to `t`. -/ universes u v w x y variables {α : Type u} {β : Type v} {π : α → Type v} {γ : Type w} {ι : Sort x} open equiv equiv.perm function namespace set /-! ### Restrict -/ /-- Restrict domain of a function `f` to a set `s`. Same as `subtype.restrict` but this version takes an argument `↥s` instead of `subtype s`. -/ def restrict (s : set α) (f : Π a : α, π a) : Π a : s, π a := λ x, f x lemma restrict_eq (f : α → β) (s : set α) : s.restrict f = f ∘ coe := rfl @[simp] lemma restrict_apply (f : α → β) (s : set α) (x : s) : s.restrict f x = f x := rfl lemma restrict_eq_iff {f : Π a, π a} {s : set α} {g : Π a : s, π a} : restrict s f = g ↔ ∀ a (ha : a ∈ s), f a = g ⟨a, ha⟩ := funext_iff.trans subtype.forall lemma eq_restrict_iff {s : set α} {f : Π a : s, π a} {g : Π a, π a} : f = restrict s g ↔ ∀ a (ha : a ∈ s), f ⟨a, ha⟩ = g a := funext_iff.trans subtype.forall @[simp] lemma range_restrict (f : α → β) (s : set α) : set.range (s.restrict f) = f '' s := (range_comp _ _).trans $ congr_arg (('') f) subtype.range_coe lemma image_restrict (f : α → β) (s t : set α) : s.restrict f '' (coe ⁻¹' t) = f '' (t ∩ s) := by rw [restrict, image_comp, image_preimage_eq_inter_range, subtype.range_coe] @[simp] lemma restrict_dite {s : set α} [∀ x, decidable (x ∈ s)] (f : Π a ∈ s, β) (g : Π a ∉ s, β) : s.restrict (λ a, if h : a ∈ s then f a h else g a h) = λ a, f a a.2 := funext $ λ a, dif_pos a.2 @[simp] lemma restrict_dite_compl {s : set α} [∀ x, decidable (x ∈ s)] (f : Π a ∈ s, β) (g : Π a ∉ s, β) : sᶜ.restrict (λ a, if h : a ∈ s then f a h else g a h) = λ a, g a a.2 := funext $ λ a, dif_neg a.2 @[simp] lemma restrict_ite (f g : α → β) (s : set α) [∀ x, decidable (x ∈ s)] : s.restrict (λ a, if a ∈ s then f a else g a) = s.restrict f := restrict_dite _ _ @[simp] lemma restrict_ite_compl (f g : α → β) (s : set α) [∀ x, decidable (x ∈ s)] : sᶜ.restrict (λ a, if a ∈ s then f a else g a) = sᶜ.restrict g := restrict_dite_compl _ _ @[simp] lemma restrict_piecewise (f g : α → β) (s : set α) [∀ x, decidable (x ∈ s)] : s.restrict (piecewise s f g) = s.restrict f := restrict_ite _ _ _ @[simp] lemma restrict_piecewise_compl (f g : α → β) (s : set α) [∀ x, decidable (x ∈ s)] : sᶜ.restrict (piecewise s f g) = sᶜ.restrict g := restrict_ite_compl _ _ _ lemma restrict_extend_range (f : α → β) (g : α → γ) (g' : β → γ) : (range f).restrict (extend f g g') = λ x, g x.coe_prop.some := by convert restrict_dite _ _ @[simp] lemma restrict_extend_compl_range (f : α → β) (g : α → γ) (g' : β → γ) : (range f)ᶜ.restrict (extend f g g') = g' ∘ coe := by convert restrict_dite_compl _ _ lemma range_extend_subset (f : α → β) (g : α → γ) (g' : β → γ) : range (extend f g g') ⊆ range g ∪ g' '' (range f)ᶜ := begin classical, rintro _ ⟨y, rfl⟩, rw extend_def, split_ifs, exacts [or.inl (mem_range_self _), or.inr (mem_image_of_mem _ h)] end lemma range_extend {f : α → β} (hf : injective f) (g : α → γ) (g' : β → γ) : range (extend f g g') = range g ∪ g' '' (range f)ᶜ := begin refine (range_extend_subset _ _ _).antisymm _, rintro z (⟨x, rfl⟩|⟨y, hy, rfl⟩), exacts [⟨f x, hf.extend_apply _ _ _⟩, ⟨y, extend_apply' _ _ _ hy⟩] end /-- Restrict codomain of a function `f` to a set `s`. Same as `subtype.coind` but this version has codomain `↥s` instead of `subtype s`. -/ def cod_restrict (f : ι → α) (s : set α) (h : ∀ x, f x ∈ s) : ι → s := λ x, ⟨f x, h x⟩ @[simp] lemma coe_cod_restrict_apply (f : ι → α) (s : set α) (h : ∀ x, f x ∈ s) (x : ι) : (cod_restrict f s h x : α) = f x := rfl @[simp] lemma restrict_comp_cod_restrict {f : ι → α} {g : α → β} {b : set α} (h : ∀ x, f x ∈ b) : (b.restrict g) ∘ (b.cod_restrict f h) = g ∘ f := rfl @[simp] lemma injective_cod_restrict {f : ι → α} {s : set α} (h : ∀ x, f x ∈ s) : injective (cod_restrict f s h) ↔ injective f := by simp only [injective, subtype.ext_iff, coe_cod_restrict_apply] alias injective_cod_restrict ↔ _ _root_.function.injective.cod_restrict variables {s s₁ s₂ : set α} {t t₁ t₂ : set β} {p : set γ} {f f₁ f₂ f₃ : α → β} {g g₁ g₂ : β → γ} {f' f₁' f₂' : β → α} {g' : γ → β} {a : α} {b : β} /-! ### Equality on a set -/ /-- Two functions `f₁ f₂ : α → β` are equal on `s` if `f₁ x = f₂ x` for all `x ∈ a`. -/ def eq_on (f₁ f₂ : α → β) (s : set α) : Prop := ∀ ⦃x⦄, x ∈ s → f₁ x = f₂ x @[simp] lemma eq_on_empty (f₁ f₂ : α → β) : eq_on f₁ f₂ ∅ := λ x, false.elim @[simp] lemma eq_on_singleton : eq_on f₁ f₂ {a} ↔ f₁ a = f₂ a := by simp [set.eq_on] @[simp] lemma restrict_eq_restrict_iff : restrict s f₁ = restrict s f₂ ↔ eq_on f₁ f₂ s := restrict_eq_iff @[symm] lemma eq_on.symm (h : eq_on f₁ f₂ s) : eq_on f₂ f₁ s := λ x hx, (h hx).symm lemma eq_on_comm : eq_on f₁ f₂ s ↔ eq_on f₂ f₁ s := ⟨eq_on.symm, eq_on.symm⟩ @[refl] lemma eq_on_refl (f : α → β) (s : set α) : eq_on f f s := λ _ _, rfl @[trans] lemma eq_on.trans (h₁ : eq_on f₁ f₂ s) (h₂ : eq_on f₂ f₃ s) : eq_on f₁ f₃ s := λ x hx, (h₁ hx).trans (h₂ hx) theorem eq_on.image_eq (heq : eq_on f₁ f₂ s) : f₁ '' s = f₂ '' s := image_congr heq theorem eq_on.inter_preimage_eq (heq : eq_on f₁ f₂ s) (t : set β) : s ∩ f₁ ⁻¹' t = s ∩ f₂ ⁻¹' t := ext $ λ x, and.congr_right_iff.2 $ λ hx, by rw [mem_preimage, mem_preimage, heq hx] lemma eq_on.mono (hs : s₁ ⊆ s₂) (hf : eq_on f₁ f₂ s₂) : eq_on f₁ f₂ s₁ := λ x hx, hf (hs hx) @[simp] lemma eq_on_union : eq_on f₁ f₂ (s₁ ∪ s₂) ↔ eq_on f₁ f₂ s₁ ∧ eq_on f₁ f₂ s₂ := ball_or_left_distrib lemma eq_on.union (h₁ : eq_on f₁ f₂ s₁) (h₂ : eq_on f₁ f₂ s₂) : eq_on f₁ f₂ (s₁ ∪ s₂) := eq_on_union.2 ⟨h₁, h₂⟩ lemma eq_on.comp_left (h : s.eq_on f₁ f₂) : s.eq_on (g ∘ f₁) (g ∘ f₂) := λ a ha, congr_arg _ $ h ha @[simp] lemma eq_on_range {ι : Sort*} {f : ι → α} {g₁ g₂ : α → β} : eq_on g₁ g₂ (range f) ↔ g₁ ∘ f = g₂ ∘ f := forall_range_iff.trans $ funext_iff.symm alias eq_on_range ↔ eq_on.comp_eq _ /-! ### Congruence lemmas -/ section order variables [preorder α] [preorder β] lemma _root_.monotone_on.congr (h₁ : monotone_on f₁ s) (h : s.eq_on f₁ f₂) : monotone_on f₂ s := begin intros a ha b hb hab, rw [←h ha, ←h hb], exact h₁ ha hb hab, end lemma _root_.antitone_on.congr (h₁ : antitone_on f₁ s) (h : s.eq_on f₁ f₂) : antitone_on f₂ s := h₁.dual_right.congr h lemma _root_.strict_mono_on.congr (h₁ : strict_mono_on f₁ s) (h : s.eq_on f₁ f₂) : strict_mono_on f₂ s := begin intros a ha b hb hab, rw [←h ha, ←h hb], exact h₁ ha hb hab, end lemma _root_.strict_anti_on.congr (h₁ : strict_anti_on f₁ s) (h : s.eq_on f₁ f₂) : strict_anti_on f₂ s := h₁.dual_right.congr h lemma eq_on.congr_monotone_on (h : s.eq_on f₁ f₂) : monotone_on f₁ s ↔ monotone_on f₂ s := ⟨λ h₁, h₁.congr h, λ h₂, h₂.congr h.symm⟩ lemma eq_on.congr_antitone_on (h : s.eq_on f₁ f₂) : antitone_on f₁ s ↔ antitone_on f₂ s := ⟨λ h₁, h₁.congr h, λ h₂, h₂.congr h.symm⟩ lemma eq_on.congr_strict_mono_on (h : s.eq_on f₁ f₂) : strict_mono_on f₁ s ↔ strict_mono_on f₂ s := ⟨λ h₁, h₁.congr h, λ h₂, h₂.congr h.symm⟩ lemma eq_on.congr_strict_anti_on (h : s.eq_on f₁ f₂) : strict_anti_on f₁ s ↔ strict_anti_on f₂ s := ⟨λ h₁, h₁.congr h, λ h₂, h₂.congr h.symm⟩ end order /-! ### Mono lemmas-/ section mono variables [preorder α] [preorder β] lemma _root_.monotone_on.mono (h : monotone_on f s) (h' : s₂ ⊆ s) : monotone_on f s₂ := λ x hx y hy, h (h' hx) (h' hy) lemma _root_.antitone_on.mono (h : antitone_on f s) (h' : s₂ ⊆ s) : antitone_on f s₂ := λ x hx y hy, h (h' hx) (h' hy) lemma _root_.strict_mono_on.mono (h : strict_mono_on f s) (h' : s₂ ⊆ s) : strict_mono_on f s₂ := λ x hx y hy, h (h' hx) (h' hy) lemma _root_.strict_anti_on.mono (h : strict_anti_on f s) (h' : s₂ ⊆ s) : strict_anti_on f s₂ := λ x hx y hy, h (h' hx) (h' hy) protected lemma _root_.monotone_on.monotone (h : monotone_on f s) : monotone (f ∘ coe : s → β) := λ x y hle, h x.coe_prop y.coe_prop hle protected lemma _root_.antitone_on.monotone (h : antitone_on f s) : antitone (f ∘ coe : s → β) := λ x y hle, h x.coe_prop y.coe_prop hle protected lemma _root_.strict_mono_on.strict_mono (h : strict_mono_on f s) : strict_mono (f ∘ coe : s → β) := λ x y hlt, h x.coe_prop y.coe_prop hlt protected lemma _root_.strict_anti_on.strict_anti (h : strict_anti_on f s) : strict_anti (f ∘ coe : s → β) := λ x y hlt, h x.coe_prop y.coe_prop hlt end mono /-! ### maps to -/ /-- `maps_to f a b` means that the image of `a` is contained in `b`. -/ def maps_to (f : α → β) (s : set α) (t : set β) : Prop := ∀ ⦃x⦄, x ∈ s → f x ∈ t /-- Given a map `f` sending `s : set α` into `t : set β`, restrict domain of `f` to `s` and the codomain to `t`. Same as `subtype.map`. -/ def maps_to.restrict (f : α → β) (s : set α) (t : set β) (h : maps_to f s t) : s → t := subtype.map f h @[simp] lemma maps_to.coe_restrict_apply (h : maps_to f s t) (x : s) : (h.restrict f s t x : β) = f x := rfl /-- Restricting the domain and then the codomain is the same as `maps_to.restrict`. -/ @[simp] lemma cod_restrict_restrict (h : ∀ x : s, f x ∈ t) : cod_restrict (s.restrict f) t h = maps_to.restrict f s t (λ x hx, h ⟨x, hx⟩) := rfl /-- Reverse of `set.cod_restrict_restrict`. -/ lemma maps_to.restrict_eq_cod_restrict (h : maps_to f s t) : h.restrict f s t = cod_restrict (s.restrict f) t (λ x, h x.2) := rfl lemma maps_to.coe_restrict (h : set.maps_to f s t) : coe ∘ h.restrict f s t = s.restrict f := rfl lemma maps_to.range_restrict (f : α → β) (s : set α) (t : set β) (h : maps_to f s t) : range (h.restrict f s t) = coe ⁻¹' (f '' s) := set.range_subtype_map f h lemma maps_to_iff_exists_map_subtype : maps_to f s t ↔ ∃ g : s → t, ∀ x : s, f x = g x := ⟨λ h, ⟨h.restrict f s t, λ _, rfl⟩, λ ⟨g, hg⟩ x hx, by { erw [hg ⟨x, hx⟩], apply subtype.coe_prop }⟩ theorem maps_to' : maps_to f s t ↔ f '' s ⊆ t := image_subset_iff.symm theorem maps_to_prod_map_diagonal : maps_to (prod.map f f) (diagonal α) (diagonal β) := diagonal_subset_iff.2 $ λ x, rfl lemma maps_to.subset_preimage {f : α → β} {s : set α} {t : set β} (hf : maps_to f s t) : s ⊆ f ⁻¹' t := hf theorem maps_to_empty (f : α → β) (t : set β) : maps_to f ∅ t := empty_subset _ @[simp] lemma maps_to_singleton : maps_to f {a} t ↔ f a ∈ t := singleton_subset_iff theorem maps_to.image_subset (h : maps_to f s t) : f '' s ⊆ t := maps_to'.1 h theorem maps_to.congr (h₁ : maps_to f₁ s t) (h : eq_on f₁ f₂ s) : maps_to f₂ s t := λ x hx, h hx ▸ h₁ hx lemma eq_on.comp_right (hg : t.eq_on g₁ g₂) (hf : s.maps_to f t) : s.eq_on (g₁ ∘ f) (g₂ ∘ f) := λ a ha, hg $ hf ha theorem eq_on.maps_to_iff (H : eq_on f₁ f₂ s) : maps_to f₁ s t ↔ maps_to f₂ s t := ⟨λ h, h.congr H, λ h, h.congr H.symm⟩ theorem maps_to.comp (h₁ : maps_to g t p) (h₂ : maps_to f s t) : maps_to (g ∘ f) s p := λ x h, h₁ (h₂ h) theorem maps_to_id (s : set α) : maps_to id s s := λ x, id theorem maps_to.iterate {f : α → α} {s : set α} (h : maps_to f s s) : ∀ n, maps_to (f^[n]) s s | 0 := λ _, id | (n+1) := (maps_to.iterate n).comp h theorem maps_to.iterate_restrict {f : α → α} {s : set α} (h : maps_to f s s) (n : ℕ) : (h.restrict f s s^[n]) = (h.iterate n).restrict _ _ _ := begin funext x, rw [subtype.ext_iff, maps_to.coe_restrict_apply], induction n with n ihn generalizing x, { refl }, { simp [nat.iterate, ihn] } end lemma maps_to_of_subsingleton' [subsingleton β] (f : α → β) (h : s.nonempty → t.nonempty) : maps_to f s t := λ a ha, subsingleton.mem_iff_nonempty.2 $ h ⟨a, ha⟩ lemma maps_to_of_subsingleton [subsingleton α] (f : α → α) (s : set α) : maps_to f s s := maps_to_of_subsingleton' _ id theorem maps_to.mono (hf : maps_to f s₁ t₁) (hs : s₂ ⊆ s₁) (ht : t₁ ⊆ t₂) : maps_to f s₂ t₂ := λ x hx, ht (hf $ hs hx) theorem maps_to.mono_left (hf : maps_to f s₁ t) (hs : s₂ ⊆ s₁) : maps_to f s₂ t := λ x hx, hf (hs hx) theorem maps_to.mono_right (hf : maps_to f s t₁) (ht : t₁ ⊆ t₂) : maps_to f s t₂ := λ x hx, ht (hf hx) theorem maps_to.union_union (h₁ : maps_to f s₁ t₁) (h₂ : maps_to f s₂ t₂) : maps_to f (s₁ ∪ s₂) (t₁ ∪ t₂) := λ x hx, hx.elim (λ hx, or.inl $ h₁ hx) (λ hx, or.inr $ h₂ hx) theorem maps_to.union (h₁ : maps_to f s₁ t) (h₂ : maps_to f s₂ t) : maps_to f (s₁ ∪ s₂) t := union_self t ▸ h₁.union_union h₂ @[simp] theorem maps_to_union : maps_to f (s₁ ∪ s₂) t ↔ maps_to f s₁ t ∧ maps_to f s₂ t := ⟨λ h, ⟨h.mono (subset_union_left s₁ s₂) (subset.refl t), h.mono (subset_union_right s₁ s₂) (subset.refl t)⟩, λ h, h.1.union h.2⟩ theorem maps_to.inter (h₁ : maps_to f s t₁) (h₂ : maps_to f s t₂) : maps_to f s (t₁ ∩ t₂) := λ x hx, ⟨h₁ hx, h₂ hx⟩ theorem maps_to.inter_inter (h₁ : maps_to f s₁ t₁) (h₂ : maps_to f s₂ t₂) : maps_to f (s₁ ∩ s₂) (t₁ ∩ t₂) := λ x hx, ⟨h₁ hx.1, h₂ hx.2⟩ @[simp] theorem maps_to_inter : maps_to f s (t₁ ∩ t₂) ↔ maps_to f s t₁ ∧ maps_to f s t₂ := ⟨λ h, ⟨h.mono (subset.refl s) (inter_subset_left t₁ t₂), h.mono (subset.refl s) (inter_subset_right t₁ t₂)⟩, λ h, h.1.inter h.2⟩ theorem maps_to_univ (f : α → β) (s : set α) : maps_to f s univ := λ x h, trivial theorem maps_to_image (f : α → β) (s : set α) : maps_to f s (f '' s) := by rw maps_to' theorem maps_to_preimage (f : α → β) (t : set β) : maps_to f (f ⁻¹' t) t := subset.refl _ theorem maps_to_range (f : α → β) (s : set α) : maps_to f s (range f) := (maps_to_image f s).mono (subset.refl s) (image_subset_range _ _) @[simp] lemma maps_image_to (f : α → β) (g : γ → α) (s : set γ) (t : set β) : maps_to f (g '' s) t ↔ maps_to (f ∘ g) s t := ⟨λ h c hc, h ⟨c, hc, rfl⟩, λ h d ⟨c, hc⟩, hc.2 ▸ h hc.1⟩ lemma maps_to.comp_left (g : β → γ) (hf : maps_to f s t) : maps_to (g ∘ f) s (g '' t) := λ x hx, ⟨f x, hf hx, rfl⟩ lemma maps_to.comp_right {s : set β} {t : set γ} (hg : maps_to g s t) (f : α → β) : maps_to (g ∘ f) (f ⁻¹' s) t := λ x hx, hg hx @[simp] lemma maps_univ_to (f : α → β) (s : set β) : maps_to f univ s ↔ ∀ a, f a ∈ s := ⟨λ h a, h (mem_univ _), λ h x _, h x⟩ @[simp] lemma maps_range_to (f : α → β) (g : γ → α) (s : set β) : maps_to f (range g) s ↔ maps_to (f ∘ g) univ s := by rw [←image_univ, maps_image_to] theorem surjective_maps_to_image_restrict (f : α → β) (s : set α) : surjective ((maps_to_image f s).restrict f s (f '' s)) := λ ⟨y, x, hs, hxy⟩, ⟨⟨x, hs⟩, subtype.ext hxy⟩ theorem maps_to.mem_iff (h : maps_to f s t) (hc : maps_to f sᶜ tᶜ) {x} : f x ∈ t ↔ x ∈ s := ⟨λ ht, by_contra $ λ hs, hc hs ht, λ hx, h hx⟩ /-! ### Restriction onto preimage -/ section variables (t f) /-- The restriction of a function onto the preimage of a set. -/ @[simps] def restrict_preimage : f ⁻¹' t → t := (set.maps_to_preimage f t).restrict _ _ _ lemma range_restrict_preimage : range (t.restrict_preimage f) = coe ⁻¹' (range f) := begin delta set.restrict_preimage, rw [maps_to.range_restrict, set.image_preimage_eq_inter_range, set.preimage_inter, subtype.coe_preimage_self, set.univ_inter], end variables {f} {U : ι → set β} lemma restrict_preimage_injective (hf : injective f) : injective (t.restrict_preimage f) := λ x y e, subtype.mk.inj_arrow e (λ e, subtype.coe_injective (hf e)) lemma restrict_preimage_surjective (hf : surjective f) : surjective (t.restrict_preimage f) := λ x, ⟨⟨_, (show f (hf x).some ∈ t, from (hf x).some_spec.symm ▸ x.2)⟩, subtype.ext (hf x).some_spec⟩ lemma restrict_preimage_bijective (hf : bijective f) : bijective (t.restrict_preimage f) := ⟨t.restrict_preimage_injective hf.1, t.restrict_preimage_surjective hf.2⟩ alias set.restrict_preimage_injective ← _root_.function.injective.restrict_preimage alias set.restrict_preimage_surjective ← _root_.function.surjective.restrict_preimage alias set.restrict_preimage_bijective ← _root_.function.bijective.restrict_preimage end /-! ### Injectivity on a set -/ /-- `f` is injective on `a` if the restriction of `f` to `a` is injective. -/ def inj_on (f : α → β) (s : set α) : Prop := ∀ ⦃x₁ : α⦄, x₁ ∈ s → ∀ ⦃x₂ : α⦄, x₂ ∈ s → f x₁ = f x₂ → x₁ = x₂ theorem subsingleton.inj_on (hs : s.subsingleton) (f : α → β) : inj_on f s := λ x hx y hy h, hs hx hy @[simp] theorem inj_on_empty (f : α → β) : inj_on f ∅ := subsingleton_empty.inj_on f @[simp] theorem inj_on_singleton (f : α → β) (a : α) : inj_on f {a} := subsingleton_singleton.inj_on f theorem inj_on.eq_iff {x y} (h : inj_on f s) (hx : x ∈ s) (hy : y ∈ s) : f x = f y ↔ x = y := ⟨h hx hy, λ h, h ▸ rfl⟩ lemma inj_on.ne_iff {x y} (h : inj_on f s) (hx : x ∈ s) (hy : y ∈ s) : f x ≠ f y ↔ x ≠ y := (h.eq_iff hx hy).not alias inj_on.ne_iff ↔ _ inj_on.ne theorem inj_on.congr (h₁ : inj_on f₁ s) (h : eq_on f₁ f₂ s) : inj_on f₂ s := λ x hx y hy, h hx ▸ h hy ▸ h₁ hx hy theorem eq_on.inj_on_iff (H : eq_on f₁ f₂ s) : inj_on f₁ s ↔ inj_on f₂ s := ⟨λ h, h.congr H, λ h, h.congr H.symm⟩ theorem inj_on.mono (h : s₁ ⊆ s₂) (ht : inj_on f s₂) : inj_on f s₁ := λ x hx y hy H, ht (h hx) (h hy) H theorem inj_on_union (h : disjoint s₁ s₂) : inj_on f (s₁ ∪ s₂) ↔ inj_on f s₁ ∧ inj_on f s₂ ∧ ∀ (x ∈ s₁) (y ∈ s₂), f x ≠ f y := begin refine ⟨λ H, ⟨H.mono $ subset_union_left _ _, H.mono $ subset_union_right _ _, _⟩, _⟩, { intros x hx y hy hxy, obtain rfl : x = y, from H (or.inl hx) (or.inr hy) hxy, exact h.le_bot ⟨hx, hy⟩ }, { rintro ⟨h₁, h₂, h₁₂⟩, rintro x (hx|hx) y (hy|hy) hxy, exacts [h₁ hx hy hxy, (h₁₂ _ hx _ hy hxy).elim, (h₁₂ _ hy _ hx hxy.symm).elim, h₂ hx hy hxy] } end theorem inj_on_insert {f : α → β} {s : set α} {a : α} (has : a ∉ s) : set.inj_on f (insert a s) ↔ set.inj_on f s ∧ f a ∉ f '' s := have disjoint s {a}, from disjoint_iff_inf_le.mpr $ λ x ⟨hxs, (hxa : x = a)⟩, has (hxa ▸ hxs), by { rw [← union_singleton, inj_on_union this], simp } lemma injective_iff_inj_on_univ : injective f ↔ inj_on f univ := ⟨λ h x hx y hy hxy, h hxy, λ h _ _ heq, h trivial trivial heq⟩ lemma inj_on_of_injective (h : injective f) (s : set α) : inj_on f s := λ x hx y hy hxy, h hxy alias inj_on_of_injective ← _root_.function.injective.inj_on lemma inj_on_id (s : set α) : inj_on id s := injective_id.inj_on _ theorem inj_on.comp (hg : inj_on g t) (hf: inj_on f s) (h : maps_to f s t) : inj_on (g ∘ f) s := λ x hx y hy heq, hf hx hy $ hg (h hx) (h hy) heq lemma inj_on.iterate {f : α → α} {s : set α} (h : inj_on f s) (hf : maps_to f s s) : ∀ n, inj_on (f^[n]) s | 0 := inj_on_id _ | (n + 1) := (inj_on.iterate n).comp h hf lemma inj_on_of_subsingleton [subsingleton α] (f : α → β) (s : set α) : inj_on f s := (injective_of_subsingleton _).inj_on _ lemma _root_.function.injective.inj_on_range (h : injective (g ∘ f)) : inj_on g (range f) := by { rintros _ ⟨x, rfl⟩ _ ⟨y, rfl⟩ H, exact congr_arg f (h H) } lemma inj_on_iff_injective : inj_on f s ↔ injective (s.restrict f) := ⟨λ H a b h, subtype.eq $ H a.2 b.2 h, λ H a as b bs h, congr_arg subtype.val $ @H ⟨a, as⟩ ⟨b, bs⟩ h⟩ alias inj_on_iff_injective ↔ inj_on.injective _ lemma maps_to.restrict_inj (h : maps_to f s t) : injective (h.restrict f s t) ↔ inj_on f s := by rw [h.restrict_eq_cod_restrict, injective_cod_restrict, inj_on_iff_injective] lemma exists_inj_on_iff_injective [nonempty β] : (∃ f : α → β, inj_on f s) ↔ ∃ f : s → β, injective f := ⟨λ ⟨f, hf⟩, ⟨_, hf.injective⟩, λ ⟨f, hf⟩, by { lift f to α → β using trivial, exact ⟨f, inj_on_iff_injective.2 hf⟩ }⟩ lemma inj_on_preimage {B : set (set β)} (hB : B ⊆ 𝒫 (range f)) : inj_on (preimage f) B := λ s hs t ht hst, (preimage_eq_preimage' (hB hs) (hB ht)).1 hst lemma inj_on.mem_of_mem_image {x} (hf : inj_on f s) (hs : s₁ ⊆ s) (h : x ∈ s) (h₁ : f x ∈ f '' s₁) : x ∈ s₁ := let ⟨x', h', eq⟩ := h₁ in hf (hs h') h eq ▸ h' lemma inj_on.mem_image_iff {x} (hf : inj_on f s) (hs : s₁ ⊆ s) (hx : x ∈ s) : f x ∈ f '' s₁ ↔ x ∈ s₁ := ⟨hf.mem_of_mem_image hs hx, mem_image_of_mem f⟩ lemma inj_on.preimage_image_inter (hf : inj_on f s) (hs : s₁ ⊆ s) : f ⁻¹' (f '' s₁) ∩ s = s₁ := ext $ λ x, ⟨λ ⟨h₁, h₂⟩, hf.mem_of_mem_image hs h₂ h₁, λ h, ⟨mem_image_of_mem _ h, hs h⟩⟩ lemma eq_on.cancel_left (h : s.eq_on (g ∘ f₁) (g ∘ f₂)) (hg : t.inj_on g) (hf₁ : s.maps_to f₁ t) (hf₂ : s.maps_to f₂ t) : s.eq_on f₁ f₂ := λ a ha, hg (hf₁ ha) (hf₂ ha) (h ha) lemma inj_on.cancel_left (hg : t.inj_on g) (hf₁ : s.maps_to f₁ t) (hf₂ : s.maps_to f₂ t) : s.eq_on (g ∘ f₁) (g ∘ f₂) ↔ s.eq_on f₁ f₂ := ⟨λ h, h.cancel_left hg hf₁ hf₂, eq_on.comp_left⟩ lemma inj_on.image_inter {s t u : set α} (hf : u.inj_on f) (hs : s ⊆ u) (ht : t ⊆ u) : f '' (s ∩ t) = f '' s ∩ f '' t := begin apply subset.antisymm (image_inter_subset _ _ _), rintros x ⟨⟨y, ys, hy⟩, ⟨z, zt, hz⟩⟩, have : y = z, { apply hf (hs ys) (ht zt), rwa ← hz at hy }, rw ← this at zt, exact ⟨y, ⟨ys, zt⟩, hy⟩, end lemma _root_.disjoint.image {s t u : set α} {f : α → β} (h : disjoint s t) (hf : inj_on f u) (hs : s ⊆ u) (ht : t ⊆ u) : disjoint (f '' s) (f '' t) := begin rw disjoint_iff_inter_eq_empty at h ⊢, rw [← hf.image_inter hs ht, h, image_empty], end /-! ### Surjectivity on a set -/ /-- `f` is surjective from `a` to `b` if `b` is contained in the image of `a`. -/ def surj_on (f : α → β) (s : set α) (t : set β) : Prop := t ⊆ f '' s theorem surj_on.subset_range (h : surj_on f s t) : t ⊆ range f := subset.trans h $ image_subset_range f s lemma surj_on_iff_exists_map_subtype : surj_on f s t ↔ ∃ (t' : set β) (g : s → t'), t ⊆ t' ∧ surjective g ∧ ∀ x : s, f x = g x := ⟨λ h, ⟨_, (maps_to_image f s).restrict f s _, h, surjective_maps_to_image_restrict _ _, λ _, rfl⟩, λ ⟨t', g, htt', hg, hfg⟩ y hy, let ⟨x, hx⟩ := hg ⟨y, htt' hy⟩ in ⟨x, x.2, by rw [hfg, hx, subtype.coe_mk]⟩⟩ theorem surj_on_empty (f : α → β) (s : set α) : surj_on f s ∅ := empty_subset _ @[simp] lemma surj_on_singleton : surj_on f s {b} ↔ b ∈ f '' s := singleton_subset_iff theorem surj_on_image (f : α → β) (s : set α) : surj_on f s (f '' s) := subset.rfl theorem surj_on.comap_nonempty (h : surj_on f s t) (ht : t.nonempty) : s.nonempty := (ht.mono h).of_image theorem surj_on.congr (h : surj_on f₁ s t) (H : eq_on f₁ f₂ s) : surj_on f₂ s t := by rwa [surj_on, ← H.image_eq] theorem eq_on.surj_on_iff (h : eq_on f₁ f₂ s) : surj_on f₁ s t ↔ surj_on f₂ s t := ⟨λ H, H.congr h, λ H, H.congr h.symm⟩ theorem surj_on.mono (hs : s₁ ⊆ s₂) (ht : t₁ ⊆ t₂) (hf : surj_on f s₁ t₂) : surj_on f s₂ t₁ := subset.trans ht $ subset.trans hf $ image_subset _ hs theorem surj_on.union (h₁ : surj_on f s t₁) (h₂ : surj_on f s t₂) : surj_on f s (t₁ ∪ t₂) := λ x hx, hx.elim (λ hx, h₁ hx) (λ hx, h₂ hx) theorem surj_on.union_union (h₁ : surj_on f s₁ t₁) (h₂ : surj_on f s₂ t₂) : surj_on f (s₁ ∪ s₂) (t₁ ∪ t₂) := (h₁.mono (subset_union_left _ _) (subset.refl _)).union (h₂.mono (subset_union_right _ _) (subset.refl _)) theorem surj_on.inter_inter (h₁ : surj_on f s₁ t₁) (h₂ : surj_on f s₂ t₂) (h : inj_on f (s₁ ∪ s₂)) : surj_on f (s₁ ∩ s₂) (t₁ ∩ t₂) := begin intros y hy, rcases h₁ hy.1 with ⟨x₁, hx₁, rfl⟩, rcases h₂ hy.2 with ⟨x₂, hx₂, heq⟩, obtain rfl : x₁ = x₂ := h (or.inl hx₁) (or.inr hx₂) heq.symm, exact mem_image_of_mem f ⟨hx₁, hx₂⟩ end theorem surj_on.inter (h₁ : surj_on f s₁ t) (h₂ : surj_on f s₂ t) (h : inj_on f (s₁ ∪ s₂)) : surj_on f (s₁ ∩ s₂) t := inter_self t ▸ h₁.inter_inter h₂ h lemma surj_on_id (s : set α) : surj_on id s s := by simp [surj_on] theorem surj_on.comp (hg : surj_on g t p) (hf : surj_on f s t) : surj_on (g ∘ f) s p := subset.trans hg $ subset.trans (image_subset g hf) $ (image_comp g f s) ▸ subset.refl _ lemma surj_on.iterate {f : α → α} {s : set α} (h : surj_on f s s) : ∀ n, surj_on (f^[n]) s s | 0 := surj_on_id _ | (n + 1) := (surj_on.iterate n).comp h lemma surj_on.comp_left (hf : surj_on f s t) (g : β → γ) : surj_on (g ∘ f) s (g '' t) := by { rw [surj_on, image_comp g f], exact image_subset _ hf } lemma surj_on.comp_right {s : set β} {t : set γ} (hf : surjective f) (hg : surj_on g s t) : surj_on (g ∘ f) (f ⁻¹' s) t := by rwa [surj_on, image_comp g f, image_preimage_eq _ hf] lemma surj_on_of_subsingleton' [subsingleton β] (f : α → β) (h : t.nonempty → s.nonempty) : surj_on f s t := λ a ha, subsingleton.mem_iff_nonempty.2 $ (h ⟨a, ha⟩).image _ lemma surj_on_of_subsingleton [subsingleton α] (f : α → α) (s : set α) : surj_on f s s := surj_on_of_subsingleton' _ id lemma surjective_iff_surj_on_univ : surjective f ↔ surj_on f univ univ := by simp [surjective, surj_on, subset_def] lemma surj_on_iff_surjective : surj_on f s univ ↔ surjective (s.restrict f) := ⟨λ H b, let ⟨a, as, e⟩ := @H b trivial in ⟨⟨a, as⟩, e⟩, λ H b _, let ⟨⟨a, as⟩, e⟩ := H b in ⟨a, as, e⟩⟩ lemma surj_on.image_eq_of_maps_to (h₁ : surj_on f s t) (h₂ : maps_to f s t) : f '' s = t := eq_of_subset_of_subset h₂.image_subset h₁ lemma image_eq_iff_surj_on_maps_to : f '' s = t ↔ s.surj_on f t ∧ s.maps_to f t := begin refine ⟨_, λ h, h.1.image_eq_of_maps_to h.2⟩, rintro rfl, exact ⟨s.surj_on_image f, s.maps_to_image f⟩, end lemma surj_on.maps_to_compl (h : surj_on f s t) (h' : injective f) : maps_to f sᶜ tᶜ := λ x hs ht, let ⟨x', hx', heq⟩ := h ht in hs $ h' heq ▸ hx' lemma maps_to.surj_on_compl (h : maps_to f s t) (h' : surjective f) : surj_on f sᶜ tᶜ := h'.forall.2 $ λ x ht, mem_image_of_mem _ $ λ hs, ht (h hs) lemma eq_on.cancel_right (hf : s.eq_on (g₁ ∘ f) (g₂ ∘ f)) (hf' : s.surj_on f t) : t.eq_on g₁ g₂ := begin intros b hb, obtain ⟨a, ha, rfl⟩ := hf' hb, exact hf ha, end lemma surj_on.cancel_right (hf : s.surj_on f t) (hf' : s.maps_to f t) : s.eq_on (g₁ ∘ f) (g₂ ∘ f) ↔ t.eq_on g₁ g₂ := ⟨λ h, h.cancel_right hf, λ h, h.comp_right hf'⟩ lemma eq_on_comp_right_iff : s.eq_on (g₁ ∘ f) (g₂ ∘ f) ↔ (f '' s).eq_on g₁ g₂ := (s.surj_on_image f).cancel_right $ s.maps_to_image f /-! ### Bijectivity -/ /-- `f` is bijective from `s` to `t` if `f` is injective on `s` and `f '' s = t`. -/ def bij_on (f : α → β) (s : set α) (t : set β) : Prop := maps_to f s t ∧ inj_on f s ∧ surj_on f s t lemma bij_on.maps_to (h : bij_on f s t) : maps_to f s t := h.left lemma bij_on.inj_on (h : bij_on f s t) : inj_on f s := h.right.left lemma bij_on.surj_on (h : bij_on f s t) : surj_on f s t := h.right.right lemma bij_on.mk (h₁ : maps_to f s t) (h₂ : inj_on f s) (h₃ : surj_on f s t) : bij_on f s t := ⟨h₁, h₂, h₃⟩ @[simp] lemma bij_on_empty (f : α → β) : bij_on f ∅ ∅ := ⟨maps_to_empty f ∅, inj_on_empty f, surj_on_empty f ∅⟩ @[simp] lemma bij_on_singleton : bij_on f {a} {b} ↔ f a = b := by simp [bij_on, eq_comm] lemma bij_on.inter_maps_to (h₁ : bij_on f s₁ t₁) (h₂ : maps_to f s₂ t₂) (h₃ : s₁ ∩ f ⁻¹' t₂ ⊆ s₂) : bij_on f (s₁ ∩ s₂) (t₁ ∩ t₂) := ⟨h₁.maps_to.inter_inter h₂, h₁.inj_on.mono $ inter_subset_left _ _, λ y hy, let ⟨x, hx, hxy⟩ := h₁.surj_on hy.1 in ⟨x, ⟨hx, h₃ ⟨hx, hxy.symm.rec_on hy.2⟩⟩, hxy⟩⟩ lemma maps_to.inter_bij_on (h₁ : maps_to f s₁ t₁) (h₂ : bij_on f s₂ t₂) (h₃ : s₂ ∩ f ⁻¹' t₁ ⊆ s₁) : bij_on f (s₁ ∩ s₂) (t₁ ∩ t₂) := inter_comm s₂ s₁ ▸ inter_comm t₂ t₁ ▸ h₂.inter_maps_to h₁ h₃ lemma bij_on.inter (h₁ : bij_on f s₁ t₁) (h₂ : bij_on f s₂ t₂) (h : inj_on f (s₁ ∪ s₂)) : bij_on f (s₁ ∩ s₂) (t₁ ∩ t₂) := ⟨h₁.maps_to.inter_inter h₂.maps_to, h₁.inj_on.mono $ inter_subset_left _ _, h₁.surj_on.inter_inter h₂.surj_on h⟩ lemma bij_on.union (h₁ : bij_on f s₁ t₁) (h₂ : bij_on f s₂ t₂) (h : inj_on f (s₁ ∪ s₂)) : bij_on f (s₁ ∪ s₂) (t₁ ∪ t₂) := ⟨h₁.maps_to.union_union h₂.maps_to, h, h₁.surj_on.union_union h₂.surj_on⟩ theorem bij_on.subset_range (h : bij_on f s t) : t ⊆ range f := h.surj_on.subset_range lemma inj_on.bij_on_image (h : inj_on f s) : bij_on f s (f '' s) := bij_on.mk (maps_to_image f s) h (subset.refl _) theorem bij_on.congr (h₁ : bij_on f₁ s t) (h : eq_on f₁ f₂ s) : bij_on f₂ s t := bij_on.mk (h₁.maps_to.congr h) (h₁.inj_on.congr h) (h₁.surj_on.congr h) theorem eq_on.bij_on_iff (H : eq_on f₁ f₂ s) : bij_on f₁ s t ↔ bij_on f₂ s t := ⟨λ h, h.congr H, λ h, h.congr H.symm⟩ lemma bij_on.image_eq (h : bij_on f s t) : f '' s = t := h.surj_on.image_eq_of_maps_to h.maps_to lemma bij_on_id (s : set α) : bij_on id s s := ⟨s.maps_to_id, s.inj_on_id, s.surj_on_id⟩ theorem bij_on.comp (hg : bij_on g t p) (hf : bij_on f s t) : bij_on (g ∘ f) s p := bij_on.mk (hg.maps_to.comp hf.maps_to) (hg.inj_on.comp hf.inj_on hf.maps_to) (hg.surj_on.comp hf.surj_on) lemma bij_on.iterate {f : α → α} {s : set α} (h : bij_on f s s) : ∀ n, bij_on (f^[n]) s s | 0 := s.bij_on_id | (n + 1) := (bij_on.iterate n).comp h lemma bij_on_of_subsingleton' [subsingleton α] [subsingleton β] (f : α → β) (h : s.nonempty ↔ t.nonempty) : bij_on f s t := ⟨maps_to_of_subsingleton' _ h.1, inj_on_of_subsingleton _ _, surj_on_of_subsingleton' _ h.2⟩ lemma bij_on_of_subsingleton [subsingleton α] (f : α → α) (s : set α) : bij_on f s s := bij_on_of_subsingleton' _ iff.rfl theorem bij_on.bijective (h : bij_on f s t) : bijective (h.maps_to.restrict f s t) := ⟨λ x y h', subtype.ext $ h.inj_on x.2 y.2 $ subtype.ext_iff.1 h', λ ⟨y, hy⟩, let ⟨x, hx, hxy⟩ := h.surj_on hy in ⟨⟨x, hx⟩, subtype.eq hxy⟩⟩ lemma bijective_iff_bij_on_univ : bijective f ↔ bij_on f univ univ := iff.intro (λ h, let ⟨inj, surj⟩ := h in ⟨maps_to_univ f _, inj.inj_on _, iff.mp surjective_iff_surj_on_univ surj⟩) (λ h, let ⟨map, inj, surj⟩ := h in ⟨iff.mpr injective_iff_inj_on_univ inj, iff.mpr surjective_iff_surj_on_univ surj⟩) alias bijective_iff_bij_on_univ ↔ _root_.function.bijective.bij_on_univ _ lemma bij_on.compl (hst : bij_on f s t) (hf : bijective f) : bij_on f sᶜ tᶜ := ⟨hst.surj_on.maps_to_compl hf.1, hf.1.inj_on _, hst.maps_to.surj_on_compl hf.2⟩ /-! ### left inverse -/ /-- `g` is a left inverse to `f` on `a` means that `g (f x) = x` for all `x ∈ a`. -/ def left_inv_on (f' : β → α) (f : α → β) (s : set α) : Prop := ∀ ⦃x⦄, x ∈ s → f' (f x) = x @[simp] lemma left_inv_on_empty (f' : β → α) (f : α → β) : left_inv_on f' f ∅ := empty_subset _ @[simp] lemma left_inv_on_singleton : left_inv_on f' f {a} ↔ f' (f a) = a := singleton_subset_iff lemma left_inv_on.eq_on (h : left_inv_on f' f s) : eq_on (f' ∘ f) id s := h lemma left_inv_on.eq (h : left_inv_on f' f s) {x} (hx : x ∈ s) : f' (f x) = x := h hx lemma left_inv_on.congr_left (h₁ : left_inv_on f₁' f s) {t : set β} (h₁' : maps_to f s t) (heq : eq_on f₁' f₂' t) : left_inv_on f₂' f s := λ x hx, heq (h₁' hx) ▸ h₁ hx theorem left_inv_on.congr_right (h₁ : left_inv_on f₁' f₁ s) (heq : eq_on f₁ f₂ s) : left_inv_on f₁' f₂ s := λ x hx, heq hx ▸ h₁ hx theorem left_inv_on.inj_on (h : left_inv_on f₁' f s) : inj_on f s := λ x₁ h₁ x₂ h₂ heq, calc x₁ = f₁' (f x₁) : eq.symm $ h h₁ ... = f₁' (f x₂) : congr_arg f₁' heq ... = x₂ : h h₂ theorem left_inv_on.surj_on (h : left_inv_on f' f s) (hf : maps_to f s t) : surj_on f' t s := λ x hx, ⟨f x, hf hx, h hx⟩ theorem left_inv_on.maps_to (h : left_inv_on f' f s) (hf : surj_on f s t) : maps_to f' t s := λ y hy, let ⟨x, hs, hx⟩ := hf hy in by rwa [← hx, h hs] lemma left_inv_on_id (s : set α) : left_inv_on id id s := λ a _, rfl theorem left_inv_on.comp (hf' : left_inv_on f' f s) (hg' : left_inv_on g' g t) (hf : maps_to f s t) : left_inv_on (f' ∘ g') (g ∘ f) s := λ x h, calc (f' ∘ g') ((g ∘ f) x) = f' (f x) : congr_arg f' (hg' (hf h)) ... = x : hf' h theorem left_inv_on.mono (hf : left_inv_on f' f s) (ht : s₁ ⊆ s) : left_inv_on f' f s₁ := λ x hx, hf (ht hx) theorem left_inv_on.image_inter' (hf : left_inv_on f' f s) : f '' (s₁ ∩ s) = f' ⁻¹' s₁ ∩ f '' s := begin apply subset.antisymm, { rintro _ ⟨x, ⟨h₁, h⟩, rfl⟩, exact ⟨by rwa [mem_preimage, hf h], mem_image_of_mem _ h⟩ }, { rintro _ ⟨h₁, ⟨x, h, rfl⟩⟩, exact mem_image_of_mem _ ⟨by rwa ← hf h, h⟩ } end theorem left_inv_on.image_inter (hf : left_inv_on f' f s) : f '' (s₁ ∩ s) = f' ⁻¹' (s₁ ∩ s) ∩ f '' s := begin rw hf.image_inter', refine subset.antisymm _ (inter_subset_inter_left _ (preimage_mono $ inter_subset_left _ _)), rintro _ ⟨h₁, x, hx, rfl⟩, exact ⟨⟨h₁, by rwa hf hx⟩, mem_image_of_mem _ hx⟩ end theorem left_inv_on.image_image (hf : left_inv_on f' f s) : f' '' (f '' s) = s := by rw [image_image, image_congr hf, image_id'] theorem left_inv_on.image_image' (hf : left_inv_on f' f s) (hs : s₁ ⊆ s) : f' '' (f '' s₁) = s₁ := (hf.mono hs).image_image /-! ### Right inverse -/ /-- `g` is a right inverse to `f` on `b` if `f (g x) = x` for all `x ∈ b`. -/ @[reducible] def right_inv_on (f' : β → α) (f : α → β) (t : set β) : Prop := left_inv_on f f' t @[simp] lemma right_inv_on_empty (f' : β → α) (f : α → β) : right_inv_on f' f ∅ := empty_subset _ @[simp] lemma right_inv_on_singleton : right_inv_on f' f {b} ↔ f (f' b) = b := singleton_subset_iff lemma right_inv_on.eq_on (h : right_inv_on f' f t) : eq_on (f ∘ f') id t := h lemma right_inv_on.eq (h : right_inv_on f' f t) {y} (hy : y ∈ t) : f (f' y) = y := h hy lemma left_inv_on.right_inv_on_image (h : left_inv_on f' f s) : right_inv_on f' f (f '' s) := λ y ⟨x, hx, eq⟩, eq ▸ congr_arg f $ h.eq hx theorem right_inv_on.congr_left (h₁ : right_inv_on f₁' f t) (heq : eq_on f₁' f₂' t) : right_inv_on f₂' f t := h₁.congr_right heq theorem right_inv_on.congr_right (h₁ : right_inv_on f' f₁ t) (hg : maps_to f' t s) (heq : eq_on f₁ f₂ s) : right_inv_on f' f₂ t := left_inv_on.congr_left h₁ hg heq theorem right_inv_on.surj_on (hf : right_inv_on f' f t) (hf' : maps_to f' t s) : surj_on f s t := hf.surj_on hf' theorem right_inv_on.maps_to (h : right_inv_on f' f t) (hf : surj_on f' t s) : maps_to f s t := h.maps_to hf lemma right_inv_on_id (s : set α) : right_inv_on id id s := λ a _, rfl theorem right_inv_on.comp (hf : right_inv_on f' f t) (hg : right_inv_on g' g p) (g'pt : maps_to g' p t) : right_inv_on (f' ∘ g') (g ∘ f) p := hg.comp hf g'pt theorem right_inv_on.mono (hf : right_inv_on f' f t) (ht : t₁ ⊆ t) : right_inv_on f' f t₁ := hf.mono ht theorem inj_on.right_inv_on_of_left_inv_on (hf : inj_on f s) (hf' : left_inv_on f f' t) (h₁ : maps_to f s t) (h₂ : maps_to f' t s) : right_inv_on f f' s := λ x h, hf (h₂ $ h₁ h) h (hf' (h₁ h)) theorem eq_on_of_left_inv_on_of_right_inv_on (h₁ : left_inv_on f₁' f s) (h₂ : right_inv_on f₂' f t) (h : maps_to f₂' t s) : eq_on f₁' f₂' t := λ y hy, calc f₁' y = (f₁' ∘ f ∘ f₂') y : congr_arg f₁' (h₂ hy).symm ... = f₂' y : h₁ (h hy) theorem surj_on.left_inv_on_of_right_inv_on (hf : surj_on f s t) (hf' : right_inv_on f f' s) : left_inv_on f f' t := λ y hy, let ⟨x, hx, heq⟩ := hf hy in by rw [← heq, hf' hx] /-! ### Two-side inverses -/ /-- `g` is an inverse to `f` viewed as a map from `a` to `b` -/ def inv_on (g : β → α) (f : α → β) (s : set α) (t : set β) : Prop := left_inv_on g f s ∧ right_inv_on g f t @[simp] lemma inv_on_empty (f' : β → α) (f : α → β) : inv_on f' f ∅ ∅ := by simp [inv_on] @[simp] lemma inv_on_singleton : inv_on f' f {a} {b} ↔ f' (f a) = a ∧ f (f' b) = b := by simp [inv_on] lemma inv_on.symm (h : inv_on f' f s t) : inv_on f f' t s := ⟨h.right, h.left⟩ lemma inv_on_id (s : set α) : inv_on id id s s := ⟨s.left_inv_on_id, s.right_inv_on_id⟩ lemma inv_on.comp (hf : inv_on f' f s t) (hg : inv_on g' g t p) (fst : maps_to f s t) (g'pt : maps_to g' p t) : inv_on (f' ∘ g') (g ∘ f) s p := ⟨hf.1.comp hg.1 fst, hf.2.comp hg.2 g'pt⟩ lemma inv_on.mono (h : inv_on f' f s t) (hs : s₁ ⊆ s) (ht : t₁ ⊆ t) : inv_on f' f s₁ t₁ := ⟨h.1.mono hs, h.2.mono ht⟩ /-- If functions `f'` and `f` are inverse on `s` and `t`, `f` maps `s` into `t`, and `f'` maps `t` into `s`, then `f` is a bijection between `s` and `t`. The `maps_to` arguments can be deduced from `surj_on` statements using `left_inv_on.maps_to` and `right_inv_on.maps_to`. -/ theorem inv_on.bij_on (h : inv_on f' f s t) (hf : maps_to f s t) (hf' : maps_to f' t s) : bij_on f s t := ⟨hf, h.left.inj_on, h.right.surj_on hf'⟩ lemma bij_on.symm {g : β → α} (h : inv_on f g t s) (hf : bij_on f s t) : bij_on g t s := ⟨h.2.maps_to hf.surj_on, h.1.inj_on, h.2.surj_on hf.maps_to⟩ lemma bij_on_comm {g : β → α} (h : inv_on f g t s) : bij_on f s t ↔ bij_on g t s := ⟨bij_on.symm h, bij_on.symm h.symm⟩ end set /-! ### `inv_fun_on` is a left/right inverse -/ namespace function variables [nonempty α] {s : set α} {f : α → β} {a : α} {b : β} local attribute [instance, priority 10] classical.prop_decidable /-- Construct the inverse for a function `f` on domain `s`. This function is a right inverse of `f` on `f '' s`. For a computable version, see `function.injective.inv_of_mem_range`. -/ noncomputable def inv_fun_on (f : α → β) (s : set α) (b : β) : α := if h : ∃a, a ∈ s ∧ f a = b then classical.some h else classical.choice ‹nonempty α› theorem inv_fun_on_pos (h : ∃a∈s, f a = b) : inv_fun_on f s b ∈ s ∧ f (inv_fun_on f s b) = b := by rw [bex_def] at h; rw [inv_fun_on, dif_pos h]; exact classical.some_spec h theorem inv_fun_on_mem (h : ∃a∈s, f a = b) : inv_fun_on f s b ∈ s := (inv_fun_on_pos h).left theorem inv_fun_on_eq (h : ∃a∈s, f a = b) : f (inv_fun_on f s b) = b := (inv_fun_on_pos h).right theorem inv_fun_on_neg (h : ¬ ∃a∈s, f a = b) : inv_fun_on f s b = classical.choice ‹nonempty α› := by rw [bex_def] at h; rw [inv_fun_on, dif_neg h] @[simp] theorem inv_fun_on_apply_mem (h : a ∈ s) : inv_fun_on f s (f a) ∈ s := inv_fun_on_mem ⟨a, h, rfl⟩ theorem inv_fun_on_apply_eq (h : a ∈ s) : f (inv_fun_on f s (f a)) = f a := inv_fun_on_eq ⟨a, h, rfl⟩ end function open function namespace set variables {s s₁ s₂ : set α} {t : set β} {f : α → β} theorem inj_on.left_inv_on_inv_fun_on [nonempty α] (h : inj_on f s) : left_inv_on (inv_fun_on f s) f s := λ a ha, h (inv_fun_on_apply_mem ha) ha (inv_fun_on_apply_eq ha) lemma inj_on.inv_fun_on_image [nonempty α] (h : inj_on f s₂) (ht : s₁ ⊆ s₂) : (inv_fun_on f s₂) '' (f '' s₁) = s₁ := h.left_inv_on_inv_fun_on.image_image' ht theorem surj_on.right_inv_on_inv_fun_on [nonempty α] (h : surj_on f s t) : right_inv_on (inv_fun_on f s) f t := λ y hy, inv_fun_on_eq $ mem_image_iff_bex.1 $ h hy theorem bij_on.inv_on_inv_fun_on [nonempty α] (h : bij_on f s t) : inv_on (inv_fun_on f s) f s t := ⟨h.inj_on.left_inv_on_inv_fun_on, h.surj_on.right_inv_on_inv_fun_on⟩ theorem surj_on.inv_on_inv_fun_on [nonempty α] (h : surj_on f s t) : inv_on (inv_fun_on f s) f (inv_fun_on f s '' t) t := begin refine ⟨_, h.right_inv_on_inv_fun_on⟩, rintros _ ⟨y, hy, rfl⟩, rw [h.right_inv_on_inv_fun_on hy] end theorem surj_on.maps_to_inv_fun_on [nonempty α] (h : surj_on f s t) : maps_to (inv_fun_on f s) t s := λ y hy, mem_preimage.2 $ inv_fun_on_mem $ mem_image_iff_bex.1 $ h hy theorem surj_on.bij_on_subset [nonempty α] (h : surj_on f s t) : bij_on f (inv_fun_on f s '' t) t := begin refine h.inv_on_inv_fun_on.bij_on _ (maps_to_image _ _), rintros _ ⟨y, hy, rfl⟩, rwa [h.right_inv_on_inv_fun_on hy] end theorem surj_on_iff_exists_bij_on_subset : surj_on f s t ↔ ∃ s' ⊆ s, bij_on f s' t := begin split, { rcases eq_empty_or_nonempty t with rfl|ht, { exact λ _, ⟨∅, empty_subset _, bij_on_empty f⟩ }, { assume h, haveI : nonempty α := ⟨classical.some (h.comap_nonempty ht)⟩, exact ⟨_, h.maps_to_inv_fun_on.image_subset, h.bij_on_subset⟩ }}, { rintros ⟨s', hs', hfs'⟩, exact hfs'.surj_on.mono hs' (subset.refl _) } end lemma preimage_inv_fun_of_mem [n : nonempty α] {f : α → β} (hf : injective f) {s : set α} (h : classical.choice n ∈ s) : inv_fun f ⁻¹' s = f '' s ∪ (range f)ᶜ := begin ext x, rcases em (x ∈ range f) with ⟨a, rfl⟩|hx, { simp [left_inverse_inv_fun hf _, hf.mem_set_image] }, { simp [mem_preimage, inv_fun_neg hx, h, hx] } end lemma preimage_inv_fun_of_not_mem [n : nonempty α] {f : α → β} (hf : injective f) {s : set α} (h : classical.choice n ∉ s) : inv_fun f ⁻¹' s = f '' s := begin ext x, rcases em (x ∈ range f) with ⟨a, rfl⟩|hx, { rw [mem_preimage, left_inverse_inv_fun hf, hf.mem_set_image] }, { have : x ∉ f '' s, from λ h', hx (image_subset_range _ _ h'), simp only [mem_preimage, inv_fun_neg hx, h, this] }, end end set /-! ### Monotone -/ namespace monotone variables [preorder α] [preorder β] {f : α → β} protected lemma restrict (h : monotone f) (s : set α) : monotone (s.restrict f) := λ x y hxy, h hxy protected lemma cod_restrict (h : monotone f) {s : set β} (hs : ∀ x, f x ∈ s) : monotone (s.cod_restrict f hs) := h protected lemma range_factorization (h : monotone f) : monotone (set.range_factorization f) := h end monotone /-! ### Piecewise defined function -/ namespace set variables {δ : α → Sort y} (s : set α) (f g : Πi, δ i) @[simp] lemma piecewise_empty [∀i : α, decidable (i ∈ (∅ : set α))] : piecewise ∅ f g = g := by { ext i, simp [piecewise] } @[simp] lemma piecewise_univ [∀i : α, decidable (i ∈ (set.univ : set α))] : piecewise set.univ f g = f := by { ext i, simp [piecewise] } @[simp] lemma piecewise_insert_self {j : α} [∀i, decidable (i ∈ insert j s)] : (insert j s).piecewise f g j = f j := by simp [piecewise] variable [∀j, decidable (j ∈ s)] instance compl.decidable_mem (j : α) : decidable (j ∈ sᶜ) := not.decidable lemma piecewise_insert [decidable_eq α] (j : α) [∀i, decidable (i ∈ insert j s)] : (insert j s).piecewise f g = function.update (s.piecewise f g) j (f j) := begin simp [piecewise], ext i, by_cases h : i = j, { rw h, simp }, { by_cases h' : i ∈ s; simp [h, h'] } end @[simp, priority 990] lemma piecewise_eq_of_mem {i : α} (hi : i ∈ s) : s.piecewise f g i = f i := if_pos hi @[simp, priority 990] lemma piecewise_eq_of_not_mem {i : α} (hi : i ∉ s) : s.piecewise f g i = g i := if_neg hi lemma piecewise_singleton (x : α) [Π y, decidable (y ∈ ({x} : set α))] [decidable_eq α] (f g : α → β) : piecewise {x} f g = function.update g x (f x) := by { ext y, by_cases hy : y = x, { subst y, simp }, { simp [hy] } } lemma piecewise_eq_on (f g : α → β) : eq_on (s.piecewise f g) f s := λ _, piecewise_eq_of_mem _ _ _ lemma piecewise_eq_on_compl (f g : α → β) : eq_on (s.piecewise f g) g sᶜ := λ _, piecewise_eq_of_not_mem _ _ _ lemma piecewise_le {δ : α → Type*} [Π i, preorder (δ i)] {s : set α} [Π j, decidable (j ∈ s)] {f₁ f₂ g : Π i, δ i} (h₁ : ∀ i ∈ s, f₁ i ≤ g i) (h₂ : ∀ i ∉ s, f₂ i ≤ g i) : s.piecewise f₁ f₂ ≤ g := λ i, if h : i ∈ s then by simp * else by simp * lemma le_piecewise {δ : α → Type*} [Π i, preorder (δ i)] {s : set α} [Π j, decidable (j ∈ s)] {f₁ f₂ g : Π i, δ i} (h₁ : ∀ i ∈ s, g i ≤ f₁ i) (h₂ : ∀ i ∉ s, g i ≤ f₂ i) : g ≤ s.piecewise f₁ f₂ := @piecewise_le α (λ i, (δ i)ᵒᵈ) _ s _ _ _ _ h₁ h₂ lemma piecewise_le_piecewise {δ : α → Type*} [Π i, preorder (δ i)] {s : set α} [Π j, decidable (j ∈ s)] {f₁ f₂ g₁ g₂ : Π i, δ i} (h₁ : ∀ i ∈ s, f₁ i ≤ g₁ i) (h₂ : ∀ i ∉ s, f₂ i ≤ g₂ i) : s.piecewise f₁ f₂ ≤ s.piecewise g₁ g₂ := by apply piecewise_le; intros; simp * @[simp, priority 990] lemma piecewise_insert_of_ne {i j : α} (h : i ≠ j) [∀i, decidable (i ∈ insert j s)] : (insert j s).piecewise f g i = s.piecewise f g i := by simp [piecewise, h] @[simp] lemma piecewise_compl [∀ i, decidable (i ∈ sᶜ)] : sᶜ.piecewise f g = s.piecewise g f := funext $ λ x, if hx : x ∈ s then by simp [hx] else by simp [hx] @[simp] lemma piecewise_range_comp {ι : Sort*} (f : ι → α) [Π j, decidable (j ∈ range f)] (g₁ g₂ : α → β) : (range f).piecewise g₁ g₂ ∘ f = g₁ ∘ f := eq_on.comp_eq $ piecewise_eq_on _ _ _ theorem maps_to.piecewise_ite {s s₁ s₂ : set α} {t t₁ t₂ : set β} {f₁ f₂ : α → β} [∀ i, decidable (i ∈ s)] (h₁ : maps_to f₁ (s₁ ∩ s) (t₁ ∩ t)) (h₂ : maps_to f₂ (s₂ ∩ sᶜ) (t₂ ∩ tᶜ)) : maps_to (s.piecewise f₁ f₂) (s.ite s₁ s₂) (t.ite t₁ t₂) := begin refine (h₁.congr _).union_union (h₂.congr _), exacts [(piecewise_eq_on s f₁ f₂).symm.mono (inter_subset_right _ _), (piecewise_eq_on_compl s f₁ f₂).symm.mono (inter_subset_right _ _)] end theorem eq_on_piecewise {f f' g : α → β} {t} : eq_on (s.piecewise f f') g t ↔ eq_on f g (t ∩ s) ∧ eq_on f' g (t ∩ sᶜ) := begin simp only [eq_on, ← forall_and_distrib], refine forall_congr (λ a, _), by_cases a ∈ s; simp * end theorem eq_on.piecewise_ite' {f f' g : α → β} {t t'} (h : eq_on f g (t ∩ s)) (h' : eq_on f' g (t' ∩ sᶜ)) : eq_on (s.piecewise f f') g (s.ite t t') := by simp [eq_on_piecewise, *] theorem eq_on.piecewise_ite {f f' g : α → β} {t t'} (h : eq_on f g t) (h' : eq_on f' g t') : eq_on (s.piecewise f f') g (s.ite t t') := (h.mono (inter_subset_left _ _)).piecewise_ite' s (h'.mono (inter_subset_left _ _)) lemma piecewise_preimage (f g : α → β) (t) : s.piecewise f g ⁻¹' t = s.ite (f ⁻¹' t) (g ⁻¹' t) := ext $ λ x, by by_cases x ∈ s; simp [*, set.ite] lemma apply_piecewise {δ' : α → Sort*} (h : Π i, δ i → δ' i) {x : α} : h x (s.piecewise f g x) = s.piecewise (λ x, h x (f x)) (λ x, h x (g x)) x := by by_cases hx : x ∈ s; simp [hx] lemma apply_piecewise₂ {δ' δ'' : α → Sort*} (f' g' : Π i, δ' i) (h : Π i, δ i → δ' i → δ'' i) {x : α} : h x (s.piecewise f g x) (s.piecewise f' g' x) = s.piecewise (λ x, h x (f x) (f' x)) (λ x, h x (g x) (g' x)) x := by by_cases hx : x ∈ s; simp [hx] lemma piecewise_op {δ' : α → Sort*} (h : Π i, δ i → δ' i) : s.piecewise (λ x, h x (f x)) (λ x, h x (g x)) = λ x, h x (s.piecewise f g x) := funext $ λ x, (apply_piecewise _ _ _ _).symm lemma piecewise_op₂ {δ' δ'' : α → Sort*} (f' g' : Π i, δ' i) (h : Π i, δ i → δ' i → δ'' i) : s.piecewise (λ x, h x (f x) (f' x)) (λ x, h x (g x) (g' x)) = λ x, h x (s.piecewise f g x) (s.piecewise f' g' x) := funext $ λ x, (apply_piecewise₂ _ _ _ _ _ _).symm @[simp] lemma piecewise_same : s.piecewise f f = f := by { ext x, by_cases hx : x ∈ s; simp [hx] } lemma range_piecewise (f g : α → β) : range (s.piecewise f g) = f '' s ∪ g '' sᶜ := begin ext y, split, { rintro ⟨x, rfl⟩, by_cases h : x ∈ s;[left, right]; use x; simp [h] }, { rintro (⟨x, hx, rfl⟩|⟨x, hx, rfl⟩); use x; simp * at * } end lemma injective_piecewise_iff {f g : α → β} : injective (s.piecewise f g) ↔ inj_on f s ∧ inj_on g sᶜ ∧ (∀ (x ∈ s) (y ∉ s), f x ≠ g y) := begin rw [injective_iff_inj_on_univ, ← union_compl_self s, inj_on_union (@disjoint_compl_right _ _ s), (piecewise_eq_on s f g).inj_on_iff, (piecewise_eq_on_compl s f g).inj_on_iff], refine and_congr iff.rfl (and_congr iff.rfl $ forall₄_congr $ λ x hx y hy, _), rw [piecewise_eq_of_mem s f g hx, piecewise_eq_of_not_mem s f g hy] end lemma piecewise_mem_pi {δ : α → Type*} {t : set α} {t' : Π i, set (δ i)} {f g} (hf : f ∈ pi t t') (hg : g ∈ pi t t') : s.piecewise f g ∈ pi t t' := by { intros i ht, by_cases hs : i ∈ s; simp [hf i ht, hg i ht, hs] } @[simp] lemma pi_piecewise {ι : Type*} {α : ι → Type*} (s s' : set ι) (t t' : Π i, set (α i)) [Π x, decidable (x ∈ s')] : pi s (s'.piecewise t t') = pi (s ∩ s') t ∩ pi (s \ s') t' := begin ext x, simp only [mem_pi, mem_inter_iff, ← forall_and_distrib], refine forall_congr (λ i, _), by_cases hi : i ∈ s'; simp * end lemma univ_pi_piecewise {ι : Type*} {α : ι → Type*} (s : set ι) (t : Π i, set (α i)) [Π x, decidable (x ∈ s)] : pi univ (s.piecewise t (λ _, univ)) = pi s t := by simp end set open set lemma strict_mono_on.inj_on [linear_order α] [preorder β] {f : α → β} {s : set α} (H : strict_mono_on f s) : s.inj_on f := λ x hx y hy hxy, show ordering.eq.compares x y, from (H.compares hx hy).1 hxy lemma strict_anti_on.inj_on [linear_order α] [preorder β] {f : α → β} {s : set α} (H : strict_anti_on f s) : s.inj_on f := @strict_mono_on.inj_on α βᵒᵈ _ _ f s H lemma strict_mono_on.comp [preorder α] [preorder β] [preorder γ] {g : β → γ} {f : α → β} {s : set α} {t : set β} (hg : strict_mono_on g t) (hf : strict_mono_on f s) (hs : set.maps_to f s t) : strict_mono_on (g ∘ f) s := λ x hx y hy hxy, hg (hs hx) (hs hy) $ hf hx hy hxy lemma strict_mono_on.comp_strict_anti_on [preorder α] [preorder β] [preorder γ] {g : β → γ} {f : α → β} {s : set α} {t : set β} (hg : strict_mono_on g t) (hf : strict_anti_on f s) (hs : set.maps_to f s t) : strict_anti_on (g ∘ f) s := λ x hx y hy hxy, hg (hs hy) (hs hx) $ hf hx hy hxy lemma strict_anti_on.comp [preorder α] [preorder β] [preorder γ] {g : β → γ} {f : α → β} {s : set α} {t : set β} (hg : strict_anti_on g t) (hf : strict_anti_on f s) (hs : set.maps_to f s t) : strict_mono_on (g ∘ f) s := λ x hx y hy hxy, hg (hs hy) (hs hx) $ hf hx hy hxy lemma strict_anti_on.comp_strict_mono_on [preorder α] [preorder β] [preorder γ] {g : β → γ} {f : α → β} {s : set α} {t : set β} (hg : strict_anti_on g t) (hf : strict_mono_on f s) (hs : set.maps_to f s t) : strict_anti_on (g ∘ f) s := λ x hx y hy hxy, hg (hs hx) (hs hy) $ hf hx hy hxy @[simp] lemma strict_mono_restrict [preorder α] [preorder β] {f : α → β} {s : set α} : strict_mono (s.restrict f) ↔ strict_mono_on f s := by simp [set.restrict, strict_mono, strict_mono_on] alias strict_mono_restrict ↔ _root_.strict_mono.of_restrict _root_.strict_mono_on.restrict lemma strict_mono.cod_restrict [preorder α] [preorder β] {f : α → β} (hf : strict_mono f) {s : set β} (hs : ∀ x, f x ∈ s) : strict_mono (set.cod_restrict f s hs) := hf namespace function open set variables {fa : α → α} {fb : β → β} {f : α → β} {g : β → γ} {s t : set α} lemma injective.comp_inj_on (hg : injective g) (hf : s.inj_on f) : s.inj_on (g ∘ f) := (hg.inj_on univ).comp hf (maps_to_univ _ _) lemma surjective.surj_on (hf : surjective f) (s : set β) : surj_on f univ s := (surjective_iff_surj_on_univ.1 hf).mono (subset.refl _) (subset_univ _) lemma left_inverse.left_inv_on {g : β → α} (h : left_inverse f g) (s : set β) : left_inv_on f g s := λ x hx, h x lemma right_inverse.right_inv_on {g : β → α} (h : right_inverse f g) (s : set α) : right_inv_on f g s := λ x hx, h x lemma left_inverse.right_inv_on_range {g : β → α} (h : left_inverse f g) : right_inv_on f g (range g) := forall_range_iff.2 $ λ i, congr_arg g (h i) namespace semiconj lemma maps_to_image (h : semiconj f fa fb) (ha : maps_to fa s t) : maps_to fb (f '' s) (f '' t) := λ y ⟨x, hx, hy⟩, hy ▸ ⟨fa x, ha hx, h x⟩ lemma maps_to_range (h : semiconj f fa fb) : maps_to fb (range f) (range f) := λ y ⟨x, hy⟩, hy ▸ ⟨fa x, h x⟩ lemma surj_on_image (h : semiconj f fa fb) (ha : surj_on fa s t) : surj_on fb (f '' s) (f '' t) := begin rintros y ⟨x, hxt, rfl⟩, rcases ha hxt with ⟨x, hxs, rfl⟩, rw [h x], exact mem_image_of_mem _ (mem_image_of_mem _ hxs) end lemma surj_on_range (h : semiconj f fa fb) (ha : surjective fa) : surj_on fb (range f) (range f) := by { rw ← image_univ, exact h.surj_on_image (ha.surj_on univ) } lemma inj_on_image (h : semiconj f fa fb) (ha : inj_on fa s) (hf : inj_on f (fa '' s)) : inj_on fb (f '' s) := begin rintros _ ⟨x, hx, rfl⟩ _ ⟨y, hy, rfl⟩ H, simp only [← h.eq] at H, exact congr_arg f (ha hx hy $ hf (mem_image_of_mem fa hx) (mem_image_of_mem fa hy) H) end lemma inj_on_range (h : semiconj f fa fb) (ha : injective fa) (hf : inj_on f (range fa)) : inj_on fb (range f) := by { rw ← image_univ at *, exact h.inj_on_image (ha.inj_on univ) hf } lemma bij_on_image (h : semiconj f fa fb) (ha : bij_on fa s t) (hf : inj_on f t) : bij_on fb (f '' s) (f '' t) := ⟨h.maps_to_image ha.maps_to, h.inj_on_image ha.inj_on (ha.image_eq.symm ▸ hf), h.surj_on_image ha.surj_on⟩ lemma bij_on_range (h : semiconj f fa fb) (ha : bijective fa) (hf : injective f) : bij_on fb (range f) (range f) := begin rw [← image_univ], exact h.bij_on_image (bijective_iff_bij_on_univ.1 ha) (hf.inj_on univ) end lemma maps_to_preimage (h : semiconj f fa fb) {s t : set β} (hb : maps_to fb s t) : maps_to fa (f ⁻¹' s) (f ⁻¹' t) := λ x hx, by simp only [mem_preimage, h x, hb hx] lemma inj_on_preimage (h : semiconj f fa fb) {s : set β} (hb : inj_on fb s) (hf : inj_on f (f ⁻¹' s)) : inj_on fa (f ⁻¹' s) := begin intros x hx y hy H, have := congr_arg f H, rw [h.eq, h.eq] at this, exact hf hx hy (hb hx hy this) end end semiconj lemma update_comp_eq_of_not_mem_range' {α β : Sort*} {γ : β → Sort*} [decidable_eq β] (g : Π b, γ b) {f : α → β} {i : β} (a : γ i) (h : i ∉ set.range f) : (λ j, (function.update g i a) (f j)) = (λ j, g (f j)) := update_comp_eq_of_forall_ne' _ _ $ λ x hx, h ⟨x, hx⟩ /-- Non-dependent version of `function.update_comp_eq_of_not_mem_range'` -/ lemma update_comp_eq_of_not_mem_range {α β γ : Sort*} [decidable_eq β] (g : β → γ) {f : α → β} {i : β} (a : γ) (h : i ∉ set.range f) : (function.update g i a) ∘ f = g ∘ f := update_comp_eq_of_not_mem_range' g a h lemma insert_inj_on (s : set α) : sᶜ.inj_on (λ a, insert a s) := λ a ha b _, (insert_inj ha).1 lemma monotone_on_of_right_inv_on_of_maps_to [partial_order α] [linear_order β] {φ : β → α} {ψ : α → β} {t : set β} {s : set α} (hφ : monotone_on φ t) (φψs : set.right_inv_on ψ φ s) (ψts : set.maps_to ψ s t) : monotone_on ψ s := begin rintro x xs y ys l, rcases le_total (ψ x) (ψ y) with (ψxy|ψyx), { exact ψxy, }, { cases le_antisymm l (φψs.eq ys ▸ φψs.eq xs ▸ hφ (ψts ys) (ψts xs) ψyx), refl, }, end lemma antitone_on_of_right_inv_on_of_maps_to [partial_order α] [linear_order β] {φ : β → α} {ψ : α → β} {t : set β} {s : set α} (hφ : antitone_on φ t) (φψs : set.right_inv_on ψ φ s) (ψts : set.maps_to ψ s t) : antitone_on ψ s := (monotone_on_of_right_inv_on_of_maps_to hφ.dual_left φψs ψts).dual_right end function /-! ### Equivalences, permutations -/ namespace set variables {p : β → Prop} [decidable_pred p] {f : α ≃ subtype p} {g g₁ g₂ : perm α} {s t : set α} protected lemma maps_to.extend_domain (h : maps_to g s t) : maps_to (g.extend_domain f) (coe ∘ f '' s) (coe ∘ f '' t) := by { rintro _ ⟨a, ha, rfl⟩, exact ⟨_, h ha, by rw extend_domain_apply_image⟩ } protected lemma surj_on.extend_domain (h : surj_on g s t) : surj_on (g.extend_domain f) (coe ∘ f '' s) (coe ∘ f '' t) := begin rintro _ ⟨a, ha, rfl⟩, obtain ⟨b, hb, rfl⟩ := h ha, exact ⟨_, ⟨_, hb, rfl⟩, by rw extend_domain_apply_image⟩, end protected lemma bij_on.extend_domain (h : set.bij_on g s t) : bij_on (g.extend_domain f) (coe ∘ f '' s) (coe ∘ f '' t) := ⟨h.maps_to.extend_domain, (g.extend_domain f).injective.inj_on _, h.surj_on.extend_domain⟩ protected lemma left_inv_on.extend_domain (h : left_inv_on g₁ g₂ s) : left_inv_on (g₁.extend_domain f) (g₂.extend_domain f) (coe ∘ f '' s) := by { rintro _ ⟨a, ha, rfl⟩, simp_rw [extend_domain_apply_image, h ha] } protected lemma right_inv_on.extend_domain (h : right_inv_on g₁ g₂ t) : right_inv_on (g₁.extend_domain f) (g₂.extend_domain f) (coe ∘ f '' t) := by { rintro _ ⟨a, ha, rfl⟩, simp_rw [extend_domain_apply_image, h ha] } protected lemma inv_on.extend_domain (h : inv_on g₁ g₂ s t) : inv_on (g₁.extend_domain f) (g₂.extend_domain f) (coe ∘ f '' s) (coe ∘ f '' t) := ⟨h.1.extend_domain, h.2.extend_domain⟩ end set namespace equiv variables (e : α ≃ β) {s : set α} {t : set β} lemma bij_on' (h₁ : maps_to e s t) (h₂ : maps_to e.symm t s) : bij_on e s t := ⟨h₁, e.injective.inj_on _, λ b hb, ⟨e.symm b, h₂ hb, apply_symm_apply _ _⟩⟩ protected lemma bij_on (h : ∀ a, e a ∈ t ↔ a ∈ s) : bij_on e s t := e.bij_on' (λ a, (h _).2) $ λ b hb, (h _).1 $ by rwa apply_symm_apply lemma inv_on : inv_on e e.symm t s := ⟨e.right_inverse_symm.left_inv_on _, e.left_inverse_symm.left_inv_on _⟩ lemma bij_on_image : bij_on e s (e '' s) := (e.injective.inj_on _).bij_on_image lemma bij_on_symm_image : bij_on e.symm (e '' s) s := e.bij_on_image.symm e.inv_on variables {e} @[simp] lemma bij_on_symm : bij_on e.symm t s ↔ bij_on e s t := bij_on_comm e.symm.inv_on alias bij_on_symm ↔ _root_.set.bij_on.of_equiv_symm _root_.set.bij_on.equiv_symm variables [decidable_eq α] {a b : α} lemma bij_on_swap (ha : a ∈ s) (hb : b ∈ s) : bij_on (swap a b) s s := (swap a b).bij_on $ λ x, by obtain rfl | hxa := eq_or_ne x a; obtain rfl | hxb := eq_or_ne x b; simp [*, swap_apply_of_ne_of_ne] end equiv
570d4f1db1cfc3d8ab79bb1cb126abdf0b4b2c52
206422fb9edabf63def0ed2aa3f489150fb09ccb
/src/topology/basic.lean
8cd11470d86d106f7b3efe7f630ce3e1da2bc403
[ "Apache-2.0" ]
permissive
hamdysalah1/mathlib
b915f86b2503feeae268de369f1b16932321f097
95454452f6b3569bf967d35aab8d852b1ddf8017
refs/heads/master
1,677,154,116,545
1,611,797,994,000
1,611,797,994,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
52,201
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, Jeremy Avigad -/ import order.filter.ultrafilter import order.filter.partial noncomputable theory /-! # Basic theory of topological spaces. The main definition is the type class `topological space α` which endows a type `α` with a topology. Then `set α` gets predicates `is_open`, `is_closed` and functions `interior`, `closure` and `frontier`. Each point `x` of `α` gets a neighborhood filter `𝓝 x`. A filter `F` on `α` has `x` as a cluster point if `cluster_pt x F : 𝓝 x ⊓ F ≠ ⊥`. A map `f : ι → α` clusters at `x` along `F : filter ι` if `map_cluster_pt x F f : cluster_pt x (map f F)`. In particular the notion of cluster point of a sequence `u` is `map_cluster_pt x at_top u`. This file also defines locally finite families of subsets of `α`. For topological spaces `α` and `β`, a function `f : α → β` and a point `a : α`, `continuous_at f a` means `f` is continuous at `a`, and global continuity is `continuous f`. There is also a version of continuity `pcontinuous` for partially defined functions. ## Notation * `𝓝 x`: the filter of neighborhoods of a point `x`; * `𝓟 s`: the principal filter of a set `s`; ## Implementation notes Topology in mathlib heavily uses filters (even more than in Bourbaki). See explanations in <https://leanprover-community.github.io/theories/topology.html>. ## References * [N. Bourbaki, *General Topology*][bourbaki1966] * [I. M. James, *Topologies and Uniformities*][james1999] ## Tags topological space, interior, closure, frontier, neighborhood, continuity, continuous function -/ open set filter classical open_locale classical filter universes u v w /-! ### Topological spaces -/ /-- A topology on `α`. -/ @[protect_proj] structure topological_space (α : Type u) := (is_open : set α → Prop) (is_open_univ : is_open univ) (is_open_inter : ∀s t, is_open s → is_open t → is_open (s ∩ t)) (is_open_sUnion : ∀s, (∀t∈s, is_open t) → is_open (⋃₀ s)) attribute [class] topological_space /-- A constructor for topologies by specifying the closed sets, and showing that they satisfy the appropriate conditions. -/ def topological_space.of_closed {α : Type u} (T : set (set α)) (empty_mem : ∅ ∈ T) (sInter_mem : ∀ A ⊆ T, ⋂₀ A ∈ T) (union_mem : ∀ A B ∈ T, A ∪ B ∈ T) : topological_space α := { is_open := λ X, Xᶜ ∈ T, is_open_univ := by simp [empty_mem], is_open_inter := λ s t hs ht, by simpa [set.compl_inter] using union_mem sᶜ tᶜ hs ht, is_open_sUnion := λ s hs, by rw set.compl_sUnion; exact sInter_mem (set.compl '' s) (λ z ⟨y, hy, hz⟩, by simpa [hz.symm] using hs y hy) } section topological_space variables {α : Type u} {β : Type v} {ι : Sort w} {a : α} {s s₁ s₂ : set α} {p p₁ p₂ : α → Prop} @[ext] lemma topological_space_eq : ∀ {f g : topological_space α}, f.is_open = g.is_open → f = g | ⟨a, _, _, _⟩ ⟨b, _, _, _⟩ rfl := rfl section variables [t : topological_space α] include t /-- `is_open s` means that `s` is open in the ambient topological space on `α` -/ def is_open (s : set α) : Prop := topological_space.is_open t s @[simp] lemma is_open_univ : is_open (univ : set α) := topological_space.is_open_univ t lemma is_open_inter (h₁ : is_open s₁) (h₂ : is_open s₂) : is_open (s₁ ∩ s₂) := topological_space.is_open_inter t s₁ s₂ h₁ h₂ lemma is_open_sUnion {s : set (set α)} (h : ∀t ∈ s, is_open t) : is_open (⋃₀ s) := topological_space.is_open_sUnion t s h end lemma topological_space_eq_iff {t t' : topological_space α} : t = t' ↔ ∀ s, @is_open α t s ↔ @is_open α t' s := ⟨λ h s, h ▸ iff.rfl, λ h, by { ext, exact h _ }⟩ lemma is_open_fold {s : set α} {t : topological_space α} : t.is_open s = @is_open α t s := rfl variables [topological_space α] lemma is_open_Union {f : ι → set α} (h : ∀i, is_open (f i)) : is_open (⋃i, f i) := is_open_sUnion $ by rintro _ ⟨i, rfl⟩; exact h i lemma is_open_bUnion {s : set β} {f : β → set α} (h : ∀i∈s, is_open (f i)) : is_open (⋃i∈s, f i) := is_open_Union $ assume i, is_open_Union $ assume hi, h i hi lemma is_open_union (h₁ : is_open s₁) (h₂ : is_open s₂) : is_open (s₁ ∪ s₂) := by rw union_eq_Union; exact is_open_Union (bool.forall_bool.2 ⟨h₂, h₁⟩) @[simp] lemma is_open_empty : is_open (∅ : set α) := by rw ← sUnion_empty; exact is_open_sUnion (assume a, false.elim) lemma is_open_sInter {s : set (set α)} (hs : finite s) : (∀t ∈ s, is_open t) → is_open (⋂₀ s) := finite.induction_on hs (λ _, by rw sInter_empty; exact is_open_univ) $ λ a s has hs ih h, by rw sInter_insert; exact is_open_inter (h _ $ mem_insert _ _) (ih $ λ t, h t ∘ mem_insert_of_mem _) lemma is_open_bInter {s : set β} {f : β → set α} (hs : finite s) : (∀i∈s, is_open (f i)) → is_open (⋂i∈s, f i) := finite.induction_on hs (λ _, by rw bInter_empty; exact is_open_univ) (λ a s has hs ih h, by rw bInter_insert; exact is_open_inter (h a (mem_insert _ _)) (ih (λ i hi, h i (mem_insert_of_mem _ hi)))) lemma is_open_Inter [fintype β] {s : β → set α} (h : ∀ i, is_open (s i)) : is_open (⋂ i, s i) := suffices is_open (⋂ (i : β) (hi : i ∈ @univ β), s i), by simpa, is_open_bInter finite_univ (λ i _, h i) lemma is_open_Inter_prop {p : Prop} {s : p → set α} (h : ∀ h : p, is_open (s h)) : is_open (Inter s) := by by_cases p; simp * lemma is_open_const {p : Prop} : is_open {a : α | p} := by_cases (assume : p, begin simp only [this]; exact is_open_univ end) (assume : ¬ p, begin simp only [this]; exact is_open_empty end) lemma is_open_and : is_open {a | p₁ a} → is_open {a | p₂ a} → is_open {a | p₁ a ∧ p₂ a} := is_open_inter /-- A set is closed if its complement is open -/ def is_closed (s : set α) : Prop := is_open sᶜ @[simp] lemma is_closed_empty : is_closed (∅ : set α) := by unfold is_closed; rw compl_empty; exact is_open_univ @[simp] lemma is_closed_univ : is_closed (univ : set α) := by unfold is_closed; rw compl_univ; exact is_open_empty lemma is_closed_union : is_closed s₁ → is_closed s₂ → is_closed (s₁ ∪ s₂) := λ h₁ h₂, by unfold is_closed; rw compl_union; exact is_open_inter h₁ h₂ lemma is_closed_sInter {s : set (set α)} : (∀t ∈ s, is_closed t) → is_closed (⋂₀ s) := by simpa only [is_closed, compl_sInter, sUnion_image] using is_open_bUnion lemma is_closed_Inter {f : ι → set α} (h : ∀i, is_closed (f i)) : is_closed (⋂i, f i ) := is_closed_sInter $ assume t ⟨i, (heq : f i = t)⟩, heq ▸ h i @[simp] lemma is_open_compl_iff {s : set α} : is_open sᶜ ↔ is_closed s := iff.rfl @[simp] lemma is_closed_compl_iff {s : set α} : is_closed sᶜ ↔ is_open s := by rw [←is_open_compl_iff, compl_compl] lemma is_open_diff {s t : set α} (h₁ : is_open s) (h₂ : is_closed t) : is_open (s \ t) := is_open_inter h₁ $ is_open_compl_iff.mpr h₂ lemma is_closed_inter (h₁ : is_closed s₁) (h₂ : is_closed s₂) : is_closed (s₁ ∩ s₂) := by rw [is_closed, compl_inter]; exact is_open_union h₁ h₂ lemma is_closed_bUnion {s : set β} {f : β → set α} (hs : finite s) : (∀i∈s, is_closed (f i)) → is_closed (⋃i∈s, f i) := finite.induction_on hs (λ _, by rw bUnion_empty; exact is_closed_empty) (λ a s has hs ih h, by rw bUnion_insert; exact is_closed_union (h a (mem_insert _ _)) (ih (λ i hi, h i (mem_insert_of_mem _ hi)))) lemma is_closed_Union [fintype β] {s : β → set α} (h : ∀ i, is_closed (s i)) : is_closed (Union s) := suffices is_closed (⋃ (i : β) (hi : i ∈ @univ β), s i), by convert this; simp [set.ext_iff], is_closed_bUnion finite_univ (λ i _, h i) lemma is_closed_Union_prop {p : Prop} {s : p → set α} (h : ∀ h : p, is_closed (s h)) : is_closed (Union s) := by by_cases p; simp * lemma is_closed_imp {p q : α → Prop} (hp : is_open {x | p x}) (hq : is_closed {x | q x}) : is_closed {x | p x → q x} := have {x | p x → q x} = {x | p x}ᶜ ∪ {x | q x}, from set.ext $ λ x, imp_iff_not_or, by rw [this]; exact is_closed_union (is_closed_compl_iff.mpr hp) hq lemma is_open_neg : is_closed {a | p a} → is_open {a | ¬ p a} := is_open_compl_iff.mpr /-! ### Interior of a set -/ /-- The interior of a set `s` is the largest open subset of `s`. -/ def interior (s : set α) : set α := ⋃₀ {t | is_open t ∧ t ⊆ s} lemma mem_interior {s : set α} {x : α} : x ∈ interior s ↔ ∃ t ⊆ s, is_open t ∧ x ∈ t := by simp only [interior, mem_set_of_eq, exists_prop, and_assoc, and.left_comm] @[simp] lemma is_open_interior {s : set α} : is_open (interior s) := is_open_sUnion $ assume t ⟨h₁, h₂⟩, h₁ lemma interior_subset {s : set α} : interior s ⊆ s := sUnion_subset $ assume t ⟨h₁, h₂⟩, h₂ lemma interior_maximal {s t : set α} (h₁ : t ⊆ s) (h₂ : is_open t) : t ⊆ interior s := subset_sUnion_of_mem ⟨h₂, h₁⟩ lemma is_open.interior_eq {s : set α} (h : is_open s) : interior s = s := subset.antisymm interior_subset (interior_maximal (subset.refl s) h) lemma interior_eq_iff_open {s : set α} : interior s = s ↔ is_open s := ⟨assume h, h ▸ is_open_interior, is_open.interior_eq⟩ lemma subset_interior_iff_open {s : set α} : s ⊆ interior s ↔ is_open s := by simp only [interior_eq_iff_open.symm, subset.antisymm_iff, interior_subset, true_and] lemma subset_interior_iff_subset_of_open {s t : set α} (h₁ : is_open s) : s ⊆ interior t ↔ s ⊆ t := ⟨assume h, subset.trans h interior_subset, assume h₂, interior_maximal h₂ h₁⟩ lemma interior_mono {s t : set α} (h : s ⊆ t) : interior s ⊆ interior t := interior_maximal (subset.trans interior_subset h) is_open_interior @[simp] lemma interior_empty : interior (∅ : set α) = ∅ := is_open_empty.interior_eq @[simp] lemma interior_univ : interior (univ : set α) = univ := is_open_univ.interior_eq @[simp] lemma interior_interior {s : set α} : interior (interior s) = interior s := is_open_interior.interior_eq @[simp] lemma interior_inter {s t : set α} : interior (s ∩ t) = interior s ∩ interior t := subset.antisymm (subset_inter (interior_mono $ inter_subset_left s t) (interior_mono $ inter_subset_right s t)) (interior_maximal (inter_subset_inter interior_subset interior_subset) $ is_open_inter is_open_interior is_open_interior) lemma interior_union_is_closed_of_interior_empty {s t : set α} (h₁ : is_closed s) (h₂ : interior t = ∅) : interior (s ∪ t) = interior s := have interior (s ∪ t) ⊆ s, from assume x ⟨u, ⟨(hu₁ : is_open u), (hu₂ : u ⊆ s ∪ t)⟩, (hx₁ : x ∈ u)⟩, classical.by_contradiction $ assume hx₂ : x ∉ s, have u \ s ⊆ t, from assume x ⟨h₁, h₂⟩, or.resolve_left (hu₂ h₁) h₂, have u \ s ⊆ interior t, by rwa subset_interior_iff_subset_of_open (is_open_diff hu₁ h₁), have u \ s ⊆ ∅, by rwa h₂ at this, this ⟨hx₁, hx₂⟩, subset.antisymm (interior_maximal this is_open_interior) (interior_mono $ subset_union_left _ _) lemma is_open_iff_forall_mem_open : is_open s ↔ ∀ x ∈ s, ∃ t ⊆ s, is_open t ∧ x ∈ t := by rw ← subset_interior_iff_open; simp only [subset_def, mem_interior] /-! ### Closure of a set -/ /-- The closure of `s` is the smallest closed set containing `s`. -/ def closure (s : set α) : set α := ⋂₀ {t | is_closed t ∧ s ⊆ t} @[simp] lemma is_closed_closure {s : set α} : is_closed (closure s) := is_closed_sInter $ assume t ⟨h₁, h₂⟩, h₁ lemma subset_closure {s : set α} : s ⊆ closure s := subset_sInter $ assume t ⟨h₁, h₂⟩, h₂ lemma closure_minimal {s t : set α} (h₁ : s ⊆ t) (h₂ : is_closed t) : closure s ⊆ t := sInter_subset_of_mem ⟨h₂, h₁⟩ lemma is_closed.closure_eq {s : set α} (h : is_closed s) : closure s = s := subset.antisymm (closure_minimal (subset.refl s) h) subset_closure lemma is_closed.closure_subset {s : set α} (hs : is_closed s) : closure s ⊆ s := closure_minimal (subset.refl _) hs lemma is_closed.closure_subset_iff {s t : set α} (h₁ : is_closed t) : closure s ⊆ t ↔ s ⊆ t := ⟨subset.trans subset_closure, assume h, closure_minimal h h₁⟩ @[mono] lemma closure_mono {s t : set α} (h : s ⊆ t) : closure s ⊆ closure t := closure_minimal (subset.trans h subset_closure) is_closed_closure lemma monotone_closure (α : Type*) [topological_space α] : monotone (@closure α _) := λ _ _, closure_mono lemma closure_inter_subset_inter_closure (s t : set α) : closure (s ∩ t) ⊆ closure s ∩ closure t := (monotone_closure α).map_inf_le s t lemma is_closed_of_closure_subset {s : set α} (h : closure s ⊆ s) : is_closed s := by rw subset.antisymm subset_closure h; exact is_closed_closure lemma closure_eq_iff_is_closed {s : set α} : closure s = s ↔ is_closed s := ⟨assume h, h ▸ is_closed_closure, is_closed.closure_eq⟩ lemma closure_subset_iff_is_closed {s : set α} : closure s ⊆ s ↔ is_closed s := ⟨is_closed_of_closure_subset, is_closed.closure_subset⟩ @[simp] lemma closure_empty : closure (∅ : set α) = ∅ := is_closed_empty.closure_eq @[simp] lemma closure_empty_iff (s : set α) : closure s = ∅ ↔ s = ∅ := ⟨subset_eq_empty subset_closure, λ h, h.symm ▸ closure_empty⟩ lemma set.nonempty.closure {s : set α} (h : s.nonempty) : set.nonempty (closure s) := let ⟨x, hx⟩ := h in ⟨x, subset_closure hx⟩ @[simp] lemma closure_univ : closure (univ : set α) = univ := is_closed_univ.closure_eq @[simp] lemma closure_closure {s : set α} : closure (closure s) = closure s := is_closed_closure.closure_eq @[simp] lemma closure_union {s t : set α} : closure (s ∪ t) = closure s ∪ closure t := subset.antisymm (closure_minimal (union_subset_union subset_closure subset_closure) $ is_closed_union is_closed_closure is_closed_closure) ((monotone_closure α).le_map_sup s t) lemma interior_subset_closure {s : set α} : interior s ⊆ closure s := subset.trans interior_subset subset_closure lemma closure_eq_compl_interior_compl {s : set α} : closure s = (interior sᶜ)ᶜ := begin unfold interior closure is_closed, rw [compl_sUnion, compl_image_set_of], simp only [compl_subset_compl] end @[simp] lemma interior_compl {s : set α} : interior sᶜ = (closure s)ᶜ := by simp [closure_eq_compl_interior_compl] @[simp] lemma closure_compl {s : set α} : closure sᶜ = (interior s)ᶜ := by simp [closure_eq_compl_interior_compl] theorem mem_closure_iff {s : set α} {a : α} : a ∈ closure s ↔ ∀ o, is_open o → a ∈ o → (o ∩ s).nonempty := ⟨λ h o oo ao, classical.by_contradiction $ λ os, have s ⊆ oᶜ, from λ x xs xo, os ⟨x, xo, xs⟩, closure_minimal this (is_closed_compl_iff.2 oo) h ao, λ H c ⟨h₁, h₂⟩, classical.by_contradiction $ λ nc, let ⟨x, hc, hs⟩ := (H _ h₁ nc) in hc (h₂ hs)⟩ /-- A set is dense in a topological space if every point belongs to its closure. -/ def dense (s : set α) : Prop := ∀ x, x ∈ closure s lemma dense_iff_closure_eq {s : set α} : dense s ↔ closure s = univ := eq_univ_iff_forall.symm lemma dense.closure_eq {s : set α} (h : dense s) : closure s = univ := dense_iff_closure_eq.mp h /-- The closure of a set `s` is dense if and only if `s` is dense. -/ @[simp] lemma dense_closure {s : set α} : dense (closure s) ↔ dense s := by rw [dense, dense, closure_closure] alias dense_closure ↔ dense.of_closure dense.closure @[simp] lemma dense_univ : dense (univ : set α) := λ x, subset_closure trivial /-- A set is dense if and only if it has a nonempty intersection with each nonempty open set. -/ lemma dense_iff_inter_open {s : set α} : dense s ↔ ∀ U, is_open U → U.nonempty → (U ∩ s).nonempty := begin split ; intro h, { rintros U U_op ⟨x, x_in⟩, exact mem_closure_iff.1 (by simp only [h.closure_eq]) U U_op x_in }, { intro x, rw mem_closure_iff, intros U U_op x_in, exact h U U_op ⟨_, x_in⟩ }, end alias dense_iff_inter_open ↔ dense.inter_open_nonempty _ lemma dense.nonempty_iff {s : set α} (hs : dense s) : s.nonempty ↔ nonempty α := ⟨λ ⟨x, hx⟩, ⟨x⟩, λ ⟨x⟩, let ⟨y, hy⟩ := hs.inter_open_nonempty _ is_open_univ ⟨x, trivial⟩ in ⟨y, hy.2⟩⟩ lemma dense.nonempty [h : nonempty α] {s : set α} (hs : dense s) : s.nonempty := hs.nonempty_iff.2 h @[mono] lemma dense.mono {s₁ s₂ : set α} (h : s₁ ⊆ s₂) (hd : dense s₁) : dense s₂ := λ x, closure_mono h (hd x) /-! ### Frontier of a set -/ /-- The frontier of a set is the set of points between the closure and interior. -/ def frontier (s : set α) : set α := closure s \ interior s lemma frontier_eq_closure_inter_closure {s : set α} : frontier s = closure s ∩ closure sᶜ := by rw [closure_compl, frontier, diff_eq] /-- The complement of a set has the same frontier as the original set. -/ @[simp] lemma frontier_compl (s : set α) : frontier sᶜ = frontier s := by simp only [frontier_eq_closure_inter_closure, compl_compl, inter_comm] lemma frontier_inter_subset (s t : set α) : frontier (s ∩ t) ⊆ (frontier s ∩ closure t) ∪ (closure s ∩ frontier t) := begin simp only [frontier_eq_closure_inter_closure, compl_inter, closure_union], convert inter_subset_inter_left _ (closure_inter_subset_inter_closure s t), simp only [inter_distrib_left, inter_distrib_right, inter_assoc], congr' 2, apply inter_comm end lemma frontier_union_subset (s t : set α) : frontier (s ∪ t) ⊆ (frontier s ∩ closure tᶜ) ∪ (closure sᶜ ∩ frontier t) := by simpa only [frontier_compl, ← compl_union] using frontier_inter_subset sᶜ tᶜ lemma is_closed.frontier_eq {s : set α} (hs : is_closed s) : frontier s = s \ interior s := by rw [frontier, hs.closure_eq] lemma is_open.frontier_eq {s : set α} (hs : is_open s) : frontier s = closure s \ s := by rw [frontier, hs.interior_eq] /-- The frontier of a set is closed. -/ lemma is_closed_frontier {s : set α} : is_closed (frontier s) := by rw frontier_eq_closure_inter_closure; exact is_closed_inter is_closed_closure is_closed_closure /-- The frontier of a closed set has no interior point. -/ lemma interior_frontier {s : set α} (h : is_closed s) : interior (frontier s) = ∅ := begin have A : frontier s = s \ interior s, from h.frontier_eq, have B : interior (frontier s) ⊆ interior s, by rw A; exact interior_mono (diff_subset _ _), have C : interior (frontier s) ⊆ frontier s := interior_subset, have : interior (frontier s) ⊆ (interior s) ∩ (s \ interior s) := subset_inter B (by simpa [A] using C), rwa [inter_diff_self, subset_empty_iff] at this, end lemma closure_eq_interior_union_frontier (s : set α) : closure s = interior s ∪ frontier s := (union_diff_cancel interior_subset_closure).symm lemma closure_eq_self_union_frontier (s : set α) : closure s = s ∪ frontier s := (union_diff_cancel' interior_subset subset_closure).symm /-! ### Neighborhoods -/ /-- A set is called a neighborhood of `a` if it contains an open set around `a`. The set of all neighborhoods of `a` forms a filter, the neighborhood filter at `a`, is here defined as the infimum over the principal filters of all open sets containing `a`. -/ def nhds (a : α) : filter α := (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, 𝓟 s) localized "notation `𝓝` := nhds" in topological_space lemma nhds_def (a : α) : 𝓝 a = (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, 𝓟 s) := rfl /-- The open sets containing `a` are a basis for the neighborhood filter. See `nhds_basis_opens'` for a variant using open neighborhoods instead. -/ lemma nhds_basis_opens (a : α) : (𝓝 a).has_basis (λ s : set α, a ∈ s ∧ is_open s) (λ x, x) := has_basis_binfi_principal (λ s ⟨has, hs⟩ t ⟨hat, ht⟩, ⟨s ∩ t, ⟨⟨has, hat⟩, is_open_inter hs ht⟩, ⟨inter_subset_left _ _, inter_subset_right _ _⟩⟩) ⟨univ, ⟨mem_univ a, is_open_univ⟩⟩ /-- A filter lies below the neighborhood filter at `a` iff it contains every open set around `a`. -/ lemma le_nhds_iff {f a} : f ≤ 𝓝 a ↔ ∀ s : set α, a ∈ s → is_open s → s ∈ f := by simp [nhds_def] /-- To show a filter is above the neighborhood filter at `a`, it suffices to show that it is above the principal filter of some open set `s` containing `a`. -/ lemma nhds_le_of_le {f a} {s : set α} (h : a ∈ s) (o : is_open s) (sf : 𝓟 s ≤ f) : 𝓝 a ≤ f := by rw nhds_def; exact infi_le_of_le s (infi_le_of_le ⟨h, o⟩ sf) lemma mem_nhds_sets_iff {a : α} {s : set α} : s ∈ 𝓝 a ↔ ∃t⊆s, is_open t ∧ a ∈ t := (nhds_basis_opens a).mem_iff.trans ⟨λ ⟨t, ⟨hat, ht⟩, hts⟩, ⟨t, hts, ht, hat⟩, λ ⟨t, hts, ht, hat⟩, ⟨t, ⟨hat, ht⟩, hts⟩⟩ /-- A predicate is true in a neighborhood of `a` iff it is true for all the points in an open set containing `a`. -/ lemma eventually_nhds_iff {a : α} {p : α → Prop} : (∀ᶠ x in 𝓝 a, p x) ↔ ∃ (t : set α), (∀ x ∈ t, p x) ∧ is_open t ∧ a ∈ t := mem_nhds_sets_iff.trans $ by simp only [subset_def, exists_prop, mem_set_of_eq] lemma map_nhds {a : α} {f : α → β} : map f (𝓝 a) = (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, 𝓟 (image f s)) := ((nhds_basis_opens a).map f).eq_binfi attribute [irreducible] nhds lemma mem_of_nhds {a : α} {s : set α} : s ∈ 𝓝 a → a ∈ s := λ H, let ⟨t, ht, _, hs⟩ := mem_nhds_sets_iff.1 H in ht hs /-- If a predicate is true in a neighborhood of `a`, then it is true for `a`. -/ lemma filter.eventually.self_of_nhds {p : α → Prop} {a : α} (h : ∀ᶠ y in 𝓝 a, p y) : p a := mem_of_nhds h lemma mem_nhds_sets {a : α} {s : set α} (hs : is_open s) (ha : a ∈ s) : s ∈ 𝓝 a := mem_nhds_sets_iff.2 ⟨s, subset.refl _, hs, ha⟩ lemma is_open.eventually_mem {a : α} {s : set α} (hs : is_open s) (ha : a ∈ s) : ∀ᶠ x in 𝓝 a, x ∈ s := mem_nhds_sets hs ha /-- The open neighborhoods of `a` are a basis for the neighborhood filter. See `nhds_basis_opens` for a variant using open sets around `a` instead. -/ lemma nhds_basis_opens' (a : α) : (𝓝 a).has_basis (λ s : set α, s ∈ 𝓝 a ∧ is_open s) (λ x, x) := begin convert nhds_basis_opens a, ext s, split, { rintros ⟨s_in, s_op⟩, exact ⟨mem_of_nhds s_in, s_op⟩ }, { rintros ⟨a_in, s_op⟩, exact ⟨mem_nhds_sets s_op a_in, s_op⟩ }, end /-- If a predicate is true in a neighbourhood of `a`, then for `y` sufficiently close to `a` this predicate is true in a neighbourhood of `y`. -/ lemma filter.eventually.eventually_nhds {p : α → Prop} {a : α} (h : ∀ᶠ y in 𝓝 a, p y) : ∀ᶠ y in 𝓝 a, ∀ᶠ x in 𝓝 y, p x := let ⟨t, htp, hto, ha⟩ := eventually_nhds_iff.1 h in eventually_nhds_iff.2 ⟨t, λ x hx, eventually_nhds_iff.2 ⟨t, htp, hto, hx⟩, hto, ha⟩ @[simp] lemma eventually_eventually_nhds {p : α → Prop} {a : α} : (∀ᶠ y in 𝓝 a, ∀ᶠ x in 𝓝 y, p x) ↔ ∀ᶠ x in 𝓝 a, p x := ⟨λ h, h.self_of_nhds, λ h, h.eventually_nhds⟩ @[simp] lemma nhds_bind_nhds : (𝓝 a).bind 𝓝 = 𝓝 a := filter.ext $ λ s, eventually_eventually_nhds @[simp] lemma eventually_eventually_eq_nhds {f g : α → β} {a : α} : (∀ᶠ y in 𝓝 a, f =ᶠ[𝓝 y] g) ↔ f =ᶠ[𝓝 a] g := eventually_eventually_nhds lemma filter.eventually_eq.eq_of_nhds {f g : α → β} {a : α} (h : f =ᶠ[𝓝 a] g) : f a = g a := h.self_of_nhds @[simp] lemma eventually_eventually_le_nhds [has_le β] {f g : α → β} {a : α} : (∀ᶠ y in 𝓝 a, f ≤ᶠ[𝓝 y] g) ↔ f ≤ᶠ[𝓝 a] g := eventually_eventually_nhds /-- If two functions are equal in a neighbourhood of `a`, then for `y` sufficiently close to `a` these functions are equal in a neighbourhood of `y`. -/ lemma filter.eventually_eq.eventually_eq_nhds {f g : α → β} {a : α} (h : f =ᶠ[𝓝 a] g) : ∀ᶠ y in 𝓝 a, f =ᶠ[𝓝 y] g := h.eventually_nhds /-- If `f x ≤ g x` in a neighbourhood of `a`, then for `y` sufficiently close to `a` we have `f x ≤ g x` in a neighbourhood of `y`. -/ lemma filter.eventually_le.eventually_le_nhds [has_le β] {f g : α → β} {a : α} (h : f ≤ᶠ[𝓝 a] g) : ∀ᶠ y in 𝓝 a, f ≤ᶠ[𝓝 y] g := h.eventually_nhds theorem all_mem_nhds (x : α) (P : set α → Prop) (hP : ∀ s t, s ⊆ t → P s → P t) : (∀ s ∈ 𝓝 x, P s) ↔ (∀ s, is_open s → x ∈ s → P s) := ((nhds_basis_opens x).forall_iff hP).trans $ by simp only [and_comm (x ∈ _), and_imp] theorem all_mem_nhds_filter (x : α) (f : set α → set β) (hf : ∀ s t, s ⊆ t → f s ⊆ f t) (l : filter β) : (∀ s ∈ 𝓝 x, f s ∈ l) ↔ (∀ s, is_open s → x ∈ s → f s ∈ l) := all_mem_nhds _ _ (λ s t ssubt h, mem_sets_of_superset h (hf s t ssubt)) theorem rtendsto_nhds {r : rel β α} {l : filter β} {a : α} : rtendsto r l (𝓝 a) ↔ (∀ s, is_open s → a ∈ s → r.core s ∈ l) := all_mem_nhds_filter _ _ (λ s t, id) _ theorem rtendsto'_nhds {r : rel β α} {l : filter β} {a : α} : rtendsto' r l (𝓝 a) ↔ (∀ s, is_open s → a ∈ s → r.preimage s ∈ l) := by { rw [rtendsto'_def], apply all_mem_nhds_filter, apply rel.preimage_mono } theorem ptendsto_nhds {f : β →. α} {l : filter β} {a : α} : ptendsto f l (𝓝 a) ↔ (∀ s, is_open s → a ∈ s → f.core s ∈ l) := rtendsto_nhds theorem ptendsto'_nhds {f : β →. α} {l : filter β} {a : α} : ptendsto' f l (𝓝 a) ↔ (∀ s, is_open s → a ∈ s → f.preimage s ∈ l) := rtendsto'_nhds theorem tendsto_nhds {f : β → α} {l : filter β} {a : α} : tendsto f l (𝓝 a) ↔ (∀ s, is_open s → a ∈ s → f ⁻¹' s ∈ l) := all_mem_nhds_filter _ _ (λ s t h, preimage_mono h) _ lemma tendsto_const_nhds {a : α} {f : filter β} : tendsto (λb:β, a) f (𝓝 a) := tendsto_nhds.mpr $ assume s hs ha, univ_mem_sets' $ assume _, ha lemma pure_le_nhds : pure ≤ (𝓝 : α → filter α) := assume a s hs, mem_pure_sets.2 $ mem_of_nhds hs lemma tendsto_pure_nhds {α : Type*} [topological_space β] (f : α → β) (a : α) : tendsto f (pure a) (𝓝 (f a)) := (tendsto_pure_pure f a).mono_right (pure_le_nhds _) lemma order_top.tendsto_at_top_nhds {α : Type*} [order_top α] [topological_space β] (f : α → β) : tendsto f at_top (𝓝 $ f ⊤) := (tendsto_at_top_pure f).mono_right (pure_le_nhds _) @[simp] instance nhds_ne_bot {a : α} : ne_bot (𝓝 a) := ne_bot_of_le (pure_le_nhds a) /-! ### Cluster points In this section we define [cluster points](https://en.wikipedia.org/wiki/Limit_point) (also known as limit points and accumulation points) of a filter and of a sequence. -/ /-- A point `x` is a cluster point of a filter `F` if 𝓝 x ⊓ F ≠ ⊥. Also known as an accumulation point or a limit point. -/ def cluster_pt (x : α) (F : filter α) : Prop := ne_bot (𝓝 x ⊓ F) lemma cluster_pt.ne_bot {x : α} {F : filter α} (h : cluster_pt x F) : ne_bot (𝓝 x ⊓ F) := h lemma cluster_pt_iff {x : α} {F : filter α} : cluster_pt x F ↔ ∀ ⦃U : set α⦄ (hU : U ∈ 𝓝 x) ⦃V⦄ (hV : V ∈ F), (U ∩ V).nonempty := inf_ne_bot_iff /-- `x` is a cluster point of a set `s` if every neighbourhood of `x` meets `s` on a nonempty set. -/ lemma cluster_pt_principal_iff {x : α} {s : set α} : cluster_pt x (𝓟 s) ↔ ∀ U ∈ 𝓝 x, (U ∩ s).nonempty := inf_principal_ne_bot_iff lemma cluster_pt_principal_iff_frequently {x : α} {s : set α} : cluster_pt x (𝓟 s) ↔ ∃ᶠ y in 𝓝 x, y ∈ s := by simp only [cluster_pt_principal_iff, frequently_iff, set.nonempty, exists_prop, mem_inter_iff] lemma cluster_pt.of_le_nhds {x : α} {f : filter α} (H : f ≤ 𝓝 x) [ne_bot f] : cluster_pt x f := by rwa [cluster_pt, inf_eq_right.mpr H] lemma cluster_pt.of_le_nhds' {x : α} {f : filter α} (H : f ≤ 𝓝 x) (hf : ne_bot f) : cluster_pt x f := cluster_pt.of_le_nhds H lemma cluster_pt.of_nhds_le {x : α} {f : filter α} (H : 𝓝 x ≤ f) : cluster_pt x f := by simp only [cluster_pt, inf_eq_left.mpr H, nhds_ne_bot] lemma cluster_pt.mono {x : α} {f g : filter α} (H : cluster_pt x f) (h : f ≤ g) : cluster_pt x g := ne_bot_of_le_ne_bot H $ inf_le_inf_left _ h lemma cluster_pt.of_inf_left {x : α} {f g : filter α} (H : cluster_pt x $ f ⊓ g) : cluster_pt x f := H.mono inf_le_left lemma cluster_pt.of_inf_right {x : α} {f g : filter α} (H : cluster_pt x $ f ⊓ g) : cluster_pt x g := H.mono inf_le_right lemma ultrafilter.cluster_pt_iff {x : α} {f : ultrafilter α} : cluster_pt x f ↔ ↑f ≤ 𝓝 x := ⟨f.le_of_inf_ne_bot', λ h, cluster_pt.of_le_nhds h⟩ /-- A point `x` is a cluster point of a sequence `u` along a filter `F` if it is a cluster point of `map u F`. -/ def map_cluster_pt {ι :Type*} (x : α) (F : filter ι) (u : ι → α) : Prop := cluster_pt x (map u F) lemma map_cluster_pt_iff {ι :Type*} (x : α) (F : filter ι) (u : ι → α) : map_cluster_pt x F u ↔ ∀ s ∈ 𝓝 x, ∃ᶠ a in F, u a ∈ s := by { simp_rw [map_cluster_pt, cluster_pt, inf_ne_bot_iff_frequently_left, frequently_map], refl } lemma map_cluster_pt_of_comp {ι δ :Type*} {F : filter ι} {φ : δ → ι} {p : filter δ} {x : α} {u : ι → α} [ne_bot p] (h : tendsto φ p F) (H : tendsto (u ∘ φ) p (𝓝 x)) : map_cluster_pt x F u := begin have := calc map (u ∘ φ) p = map u (map φ p) : map_map ... ≤ map u F : map_mono h, have : map (u ∘ φ) p ≤ 𝓝 x ⊓ map u F, from le_inf H this, exact ne_bot_of_le this end /-! ### Interior, closure and frontier in terms of neighborhoods -/ lemma interior_eq_nhds' {s : set α} : interior s = {a | s ∈ 𝓝 a} := set.ext $ λ x, by simp only [mem_interior, mem_nhds_sets_iff, mem_set_of_eq] lemma interior_eq_nhds {s : set α} : interior s = {a | 𝓝 a ≤ 𝓟 s} := interior_eq_nhds'.trans $ by simp only [le_principal_iff] lemma mem_interior_iff_mem_nhds {s : set α} {a : α} : a ∈ interior s ↔ s ∈ 𝓝 a := by rw [interior_eq_nhds', mem_set_of_eq] lemma interior_set_of_eq {p : α → Prop} : interior {x | p x} = {x | ∀ᶠ y in 𝓝 x, p y} := interior_eq_nhds' lemma is_open_set_of_eventually_nhds {p : α → Prop} : is_open {x | ∀ᶠ y in 𝓝 x, p y} := by simp only [← interior_set_of_eq, is_open_interior] lemma subset_interior_iff_nhds {s V : set α} : s ⊆ interior V ↔ ∀ x ∈ s, V ∈ 𝓝 x := show (∀ x, x ∈ s → x ∈ _) ↔ _, by simp_rw mem_interior_iff_mem_nhds lemma is_open_iff_nhds {s : set α} : is_open s ↔ ∀a∈s, 𝓝 a ≤ 𝓟 s := calc is_open s ↔ s ⊆ interior s : subset_interior_iff_open.symm ... ↔ (∀a∈s, 𝓝 a ≤ 𝓟 s) : by rw [interior_eq_nhds]; refl lemma is_open_iff_mem_nhds {s : set α} : is_open s ↔ ∀a∈s, s ∈ 𝓝 a := is_open_iff_nhds.trans $ forall_congr $ λ _, imp_congr_right $ λ _, le_principal_iff theorem is_open_iff_ultrafilter {s : set α} : is_open s ↔ (∀ (x ∈ s) (l : ultrafilter α), ↑l ≤ 𝓝 x → s ∈ l) := by simp_rw [is_open_iff_mem_nhds, ← mem_iff_ultrafilter] lemma mem_closure_iff_frequently {s : set α} {a : α} : a ∈ closure s ↔ ∃ᶠ x in 𝓝 a, x ∈ s := by rw [filter.frequently, filter.eventually, ← mem_interior_iff_mem_nhds, closure_eq_compl_interior_compl]; refl alias mem_closure_iff_frequently ↔ _ filter.frequently.mem_closure /-- The set of cluster points of a filter is closed. In particular, the set of limit points of a sequence is closed. -/ lemma is_closed_set_of_cluster_pt {f : filter α} : is_closed {x | cluster_pt x f} := begin simp only [cluster_pt, inf_ne_bot_iff_frequently_left, set_of_forall, imp_iff_not_or], refine is_closed_Inter (λ p, is_closed_union _ _); apply is_closed_compl_iff.2, exacts [is_open_set_of_eventually_nhds, is_open_const] end theorem mem_closure_iff_cluster_pt {s : set α} {a : α} : a ∈ closure s ↔ cluster_pt a (𝓟 s) := mem_closure_iff_frequently.trans cluster_pt_principal_iff_frequently.symm lemma closure_eq_cluster_pts {s : set α} : closure s = {a | cluster_pt a (𝓟 s)} := set.ext $ λ x, mem_closure_iff_cluster_pt theorem mem_closure_iff_nhds {s : set α} {a : α} : a ∈ closure s ↔ ∀ t ∈ 𝓝 a, (t ∩ s).nonempty := mem_closure_iff_cluster_pt.trans cluster_pt_principal_iff theorem mem_closure_iff_nhds' {s : set α} {a : α} : a ∈ closure s ↔ ∀ t ∈ 𝓝 a, ∃ y : s, ↑y ∈ t := by simp only [mem_closure_iff_nhds, set.nonempty_inter_iff_exists_right] theorem mem_closure_iff_comap_ne_bot {A : set α} {x : α} : x ∈ closure A ↔ ne_bot (comap (coe : A → α) (𝓝 x)) := by simp_rw [mem_closure_iff_nhds, comap_ne_bot_iff, set.nonempty_inter_iff_exists_right] theorem mem_closure_iff_nhds_basis {a : α} {p : β → Prop} {s : β → set α} (h : (𝓝 a).has_basis p s) {t : set α} : a ∈ closure t ↔ ∀ i, p i → ∃ y ∈ t, y ∈ s i := mem_closure_iff_nhds.trans ⟨λ H i hi, let ⟨x, hx⟩ := (H _ $ h.mem_of_mem hi) in ⟨x, hx.2, hx.1⟩, λ H t' ht', let ⟨i, hi, hit⟩ := h.mem_iff.1 ht', ⟨x, xt, hx⟩ := H i hi in ⟨x, hit hx, xt⟩⟩ /-- `x` belongs to the closure of `s` if and only if some ultrafilter supported on `s` converges to `x`. -/ lemma mem_closure_iff_ultrafilter {s : set α} {x : α} : x ∈ closure s ↔ ∃ (u : ultrafilter α), s ∈ u ∧ ↑u ≤ 𝓝 x := by simp [closure_eq_cluster_pts, cluster_pt, ← exists_ultrafilter_iff, and.comm] lemma is_closed_iff_cluster_pt {s : set α} : is_closed s ↔ ∀a, cluster_pt a (𝓟 s) → a ∈ s := calc is_closed s ↔ closure s ⊆ s : closure_subset_iff_is_closed.symm ... ↔ (∀a, cluster_pt a (𝓟 s) → a ∈ s) : by simp only [subset_def, mem_closure_iff_cluster_pt] lemma is_closed_iff_nhds {s : set α} : is_closed s ↔ ∀ x, (∀ U ∈ 𝓝 x, (U ∩ s).nonempty) → x ∈ s := by simp_rw [is_closed_iff_cluster_pt, cluster_pt, inf_principal_ne_bot_iff] lemma closure_inter_open {s t : set α} (h : is_open s) : s ∩ closure t ⊆ closure (s ∩ t) := assume a ⟨hs, ht⟩, have s ∈ 𝓝 a, from mem_nhds_sets h hs, have 𝓝 a ⊓ 𝓟 s = 𝓝 a, by rwa [inf_eq_left, le_principal_iff], have cluster_pt a (𝓟 (s ∩ t)), from calc 𝓝 a ⊓ 𝓟 (s ∩ t) = 𝓝 a ⊓ (𝓟 s ⊓ 𝓟 t) : by rw inf_principal ... = 𝓝 a ⊓ 𝓟 t : by rw [←inf_assoc, this] ... ≠ ⊥ : by rw [closure_eq_cluster_pts] at ht; assumption, by rwa [closure_eq_cluster_pts] /-- The intersection of an open dense set with a dense set is a dense set. -/ lemma dense.inter_of_open_left {s t : set α} (hs : dense s) (ht : dense t) (hso : is_open s) : dense (s ∩ t) := λ x, (closure_minimal (closure_inter_open hso) is_closed_closure) $ by simp [hs.closure_eq, ht.closure_eq] /-- The intersection of a dense set with an open dense set is a dense set. -/ lemma dense.inter_of_open_right {s t : set α} (hs : dense s) (ht : dense t) (hto : is_open t) : dense (s ∩ t) := inter_comm t s ▸ ht.inter_of_open_left hs hto lemma dense.inter_nhds_nonempty {s t : set α} (hs : dense s) {x : α} (ht : t ∈ 𝓝 x) : (s ∩ t).nonempty := let ⟨U, hsub, ho, hx⟩ := mem_nhds_sets_iff.1 ht in (hs.inter_open_nonempty U ho ⟨x, hx⟩).mono $ λ y hy, ⟨hy.2, hsub hy.1⟩ lemma closure_diff {s t : set α} : closure s \ closure t ⊆ closure (s \ t) := calc closure s \ closure t = (closure t)ᶜ ∩ closure s : by simp only [diff_eq, inter_comm] ... ⊆ closure ((closure t)ᶜ ∩ s) : closure_inter_open $ is_open_compl_iff.mpr $ is_closed_closure ... = closure (s \ closure t) : by simp only [diff_eq, inter_comm] ... ⊆ closure (s \ t) : closure_mono $ diff_subset_diff (subset.refl s) subset_closure lemma filter.frequently.mem_of_closed {a : α} {s : set α} (h : ∃ᶠ x in 𝓝 a, x ∈ s) (hs : is_closed s) : a ∈ s := hs.closure_subset h.mem_closure lemma is_closed.mem_of_frequently_of_tendsto {f : β → α} {b : filter β} {a : α} {s : set α} (hs : is_closed s) (h : ∃ᶠ x in b, f x ∈ s) (hf : tendsto f b (𝓝 a)) : a ∈ s := (hf.frequently $ show ∃ᶠ x in b, (λ y, y ∈ s) (f x), from h).mem_of_closed hs lemma is_closed.mem_of_tendsto {f : β → α} {b : filter β} {a : α} {s : set α} [ne_bot b] (hs : is_closed s) (hf : tendsto f b (𝓝 a)) (h : ∀ᶠ x in b, f x ∈ s) : a ∈ s := hs.mem_of_frequently_of_tendsto h.frequently hf lemma mem_closure_of_tendsto {f : β → α} {b : filter β} {a : α} {s : set α} [ne_bot b] (hf : tendsto f b (𝓝 a)) (h : ∀ᶠ x in b, f x ∈ s) : a ∈ closure s := is_closed_closure.mem_of_tendsto hf $ h.mono (preimage_mono subset_closure) /-- Suppose that `f` sends the complement to `s` to a single point `a`, and `l` is some filter. Then `f` tends to `a` along `l` restricted to `s` if and only if it tends to `a` along `l`. -/ lemma tendsto_inf_principal_nhds_iff_of_forall_eq {f : β → α} {l : filter β} {s : set β} {a : α} (h : ∀ x ∉ s, f x = a) : tendsto f (l ⊓ 𝓟 s) (𝓝 a) ↔ tendsto f l (𝓝 a) := begin rw [tendsto_iff_comap, tendsto_iff_comap], replace h : 𝓟 sᶜ ≤ comap f (𝓝 a), { rintros U ⟨t, ht, htU⟩ x hx, have : f x ∈ t, from (h x hx).symm ▸ mem_of_nhds ht, exact htU this }, refine ⟨λ h', _, le_trans inf_le_left⟩, have := sup_le h' h, rw [sup_inf_right, sup_principal, union_compl_self, principal_univ, inf_top_eq, sup_le_iff] at this, exact this.1 end /-! ### Limits of filters in topological spaces -/ section lim /-- If `f` is a filter, then `Lim f` is a limit of the filter, if it exists. -/ noncomputable def Lim [nonempty α] (f : filter α) : α := epsilon $ λa, f ≤ 𝓝 a /-- If `f` is a filter satisfying `ne_bot f`, then `Lim' f` is a limit of the filter, if it exists. -/ def Lim' (f : filter α) [ne_bot f] : α := @Lim _ _ (nonempty_of_ne_bot f) f /-- If `F` is an ultrafilter, then `filter.ultrafilter.Lim F` is a limit of the filter, if it exists. Note that dot notation `F.Lim` can be used for `F : ultrafilter α`. -/ def ultrafilter.Lim : ultrafilter α → α := λ F, Lim' F /-- If `f` is a filter in `β` and `g : β → α` is a function, then `lim f` is a limit of `g` at `f`, if it exists. -/ noncomputable def lim [nonempty α] (f : filter β) (g : β → α) : α := Lim (f.map g) /-- If a filter `f` is majorated by some `𝓝 a`, then it is majorated by `𝓝 (Lim f)`. We formulate this lemma with a `[nonempty α]` argument of `Lim` derived from `h` to make it useful for types without a `[nonempty α]` instance. Because of the built-in proof irrelevance, Lean will unify this instance with any other instance. -/ lemma le_nhds_Lim {f : filter α} (h : ∃a, f ≤ 𝓝 a) : f ≤ 𝓝 (@Lim _ _ (nonempty_of_exists h) f) := epsilon_spec h /-- If `g` tends to some `𝓝 a` along `f`, then it tends to `𝓝 (lim f g)`. We formulate this lemma with a `[nonempty α]` argument of `lim` derived from `h` to make it useful for types without a `[nonempty α]` instance. Because of the built-in proof irrelevance, Lean will unify this instance with any other instance. -/ lemma tendsto_nhds_lim {f : filter β} {g : β → α} (h : ∃ a, tendsto g f (𝓝 a)) : tendsto g f (𝓝 $ @lim _ _ _ (nonempty_of_exists h) f g) := le_nhds_Lim h end lim /-! ### Locally finite families -/ /- locally finite family [General Topology (Bourbaki, 1995)] -/ section locally_finite /-- A family of sets in `set α` is locally finite if at every point `x:α`, there is a neighborhood of `x` which meets only finitely many sets in the family -/ def locally_finite (f : β → set α) := ∀x:α, ∃t ∈ 𝓝 x, finite {i | (f i ∩ t).nonempty } lemma locally_finite_of_finite {f : β → set α} (h : finite (univ : set β)) : locally_finite f := assume x, ⟨univ, univ_mem_sets, h.subset $ subset_univ _⟩ lemma locally_finite_subset {f₁ f₂ : β → set α} (hf₂ : locally_finite f₂) (hf : ∀b, f₁ b ⊆ f₂ b) : locally_finite f₁ := assume a, let ⟨t, ht₁, ht₂⟩ := hf₂ a in ⟨t, ht₁, ht₂.subset $ assume i hi, hi.mono $ inter_subset_inter (hf i) $ subset.refl _⟩ lemma is_closed_Union_of_locally_finite {f : β → set α} (h₁ : locally_finite f) (h₂ : ∀i, is_closed (f i)) : is_closed (⋃i, f i) := is_open_iff_nhds.mpr $ assume a, assume h : a ∉ (⋃i, f i), have ∀i, a ∈ (f i)ᶜ, from assume i hi, h $ mem_Union.2 ⟨i, hi⟩, have ∀i, (f i)ᶜ ∈ (𝓝 a), by simp only [mem_nhds_sets_iff]; exact assume i, ⟨(f i)ᶜ, subset.refl _, h₂ i, this i⟩, let ⟨t, h_sets, (h_fin : finite {i | (f i ∩ t).nonempty })⟩ := h₁ a in calc 𝓝 a ≤ 𝓟 (t ∩ (⋂ i∈{i | (f i ∩ t).nonempty }, (f i)ᶜ)) : by simp * ... ≤ 𝓟 (⋃i, f i)ᶜ : begin simp only [principal_mono, subset_def, mem_compl_eq, mem_inter_eq, mem_Inter, mem_set_of_eq, mem_Union, and_imp, not_exists, exists_imp_distrib, ne_empty_iff_nonempty, set.nonempty], exact assume x xt ht i xfi, ht i x xfi xt xfi end end locally_finite end topological_space /-! ### Continuity -/ section continuous variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} variables [topological_space α] [topological_space β] [topological_space γ] open_locale topological_space /-- A function between topological spaces is continuous if the preimage of every open set is open. Registered as a structure to make sure it is not unfolded by Lean. -/ structure continuous (f : α → β) : Prop := (is_open_preimage : ∀s, is_open s → is_open (f ⁻¹' s)) lemma continuous_def {f : α → β} : continuous f ↔ (∀s, is_open s → is_open (f ⁻¹' s)) := ⟨λ hf s hs, hf.is_open_preimage s hs, λ h, ⟨h⟩⟩ lemma is_open.preimage {f : α → β} (hf : continuous f) {s : set β} (h : is_open s) : is_open (f ⁻¹' s) := hf.is_open_preimage s h /-- A function between topological spaces is continuous at a point `x₀` if `f x` tends to `f x₀` when `x` tends to `x₀`. -/ def continuous_at (f : α → β) (x : α) := tendsto f (𝓝 x) (𝓝 (f x)) lemma continuous_at.tendsto {f : α → β} {x : α} (h : continuous_at f x) : tendsto f (𝓝 x) (𝓝 (f x)) := h lemma continuous_at.preimage_mem_nhds {f : α → β} {x : α} {t : set β} (h : continuous_at f x) (ht : t ∈ 𝓝 (f x)) : f ⁻¹' t ∈ 𝓝 x := h ht lemma cluster_pt.map {x : α} {la : filter α} {lb : filter β} (H : cluster_pt x la) {f : α → β} (hfc : continuous_at f x) (hf : tendsto f la lb) : cluster_pt (f x) lb := ne_bot_of_le_ne_bot ((map_ne_bot_iff f).2 H) $ hfc.tendsto.inf hf lemma preimage_interior_subset_interior_preimage {f : α → β} {s : set β} (hf : continuous f) : f⁻¹' (interior s) ⊆ interior (f⁻¹' s) := interior_maximal (preimage_mono interior_subset) (is_open_interior.preimage hf) lemma continuous_id : continuous (id : α → α) := continuous_def.2 $ assume s h, h lemma continuous.comp {g : β → γ} {f : α → β} (hg : continuous g) (hf : continuous f) : continuous (g ∘ f) := continuous_def.2 $ assume s h, (h.preimage hg).preimage hf lemma continuous.iterate {f : α → α} (h : continuous f) (n : ℕ) : continuous (f^[n]) := nat.rec_on n continuous_id (λ n ihn, ihn.comp h) lemma continuous_at.comp {g : β → γ} {f : α → β} {x : α} (hg : continuous_at g (f x)) (hf : continuous_at f x) : continuous_at (g ∘ f) x := hg.comp hf lemma continuous.tendsto {f : α → β} (hf : continuous f) (x) : tendsto f (𝓝 x) (𝓝 (f x)) := ((nhds_basis_opens x).tendsto_iff $ nhds_basis_opens $ f x).2 $ λ t ⟨hxt, ht⟩, ⟨f ⁻¹' t, ⟨hxt, ht.preimage hf⟩, subset.refl _⟩ /-- A version of `continuous.tendsto` that allows one to specify a simpler form of the limit. E.g., one can write `continuous_exp.tendsto' 0 1 exp_zero`. -/ lemma continuous.tendsto' {f : α → β} (hf : continuous f) (x : α) (y : β) (h : f x = y) : tendsto f (𝓝 x) (𝓝 y) := h ▸ hf.tendsto x lemma continuous.continuous_at {f : α → β} {x : α} (h : continuous f) : continuous_at f x := h.tendsto x lemma continuous_iff_continuous_at {f : α → β} : continuous f ↔ ∀ x, continuous_at f x := ⟨continuous.tendsto, assume hf : ∀x, tendsto f (𝓝 x) (𝓝 (f x)), continuous_def.2 $ assume s, assume hs : is_open s, have ∀a, f a ∈ s → s ∈ 𝓝 (f a), from λ a ha, mem_nhds_sets hs ha, show is_open (f ⁻¹' s), from is_open_iff_nhds.2 $ λ a ha, le_principal_iff.2 $ hf _ (this a ha)⟩ lemma continuous_at_const {x : α} {b : β} : continuous_at (λ a:α, b) x := tendsto_const_nhds lemma continuous_const {b : β} : continuous (λa:α, b) := continuous_iff_continuous_at.mpr $ assume a, continuous_at_const lemma continuous_at_id {x : α} : continuous_at id x := continuous_id.continuous_at lemma continuous_at.iterate {f : α → α} {x : α} (hf : continuous_at f x) (hx : f x = x) (n : ℕ) : continuous_at (f^[n]) x := nat.rec_on n continuous_at_id $ λ n ihn, show continuous_at (f^[n] ∘ f) x, from continuous_at.comp (hx.symm ▸ ihn) hf lemma continuous_iff_is_closed {f : α → β} : continuous f ↔ (∀s, is_closed s → is_closed (f ⁻¹' s)) := ⟨assume hf s hs, continuous_def.1 hf sᶜ hs, assume hf, continuous_def.2 $ assume s, by rw [←is_closed_compl_iff, ←is_closed_compl_iff]; exact hf _⟩ lemma is_closed.preimage {f : α → β} (hf : continuous f) {s : set β} (h : is_closed s) : is_closed (f ⁻¹' s) := continuous_iff_is_closed.mp hf s h lemma continuous_at_iff_ultrafilter {f : α → β} {x} : continuous_at f x ↔ ∀ g : ultrafilter α, ↑g ≤ 𝓝 x → tendsto f g (𝓝 (f x)) := tendsto_iff_ultrafilter f (𝓝 x) (𝓝 (f x)) lemma continuous_iff_ultrafilter {f : α → β} : continuous f ↔ ∀ x (g : ultrafilter α), ↑g ≤ 𝓝 x → tendsto f g (𝓝 (f x)) := by simp only [continuous_iff_continuous_at, continuous_at_iff_ultrafilter] /-- A piecewise defined function `if p then f else g` is continuous, if both `f` and `g` are continuous, and they coincide on the frontier (boundary) of the set `{a | p a}`. -/ lemma continuous_if {p : α → Prop} {f g : α → β} {h : ∀a, decidable (p a)} (hp : ∀a∈frontier {a | p a}, f a = g a) (hf : continuous f) (hg : continuous g) : continuous (λa, @ite (p a) (h a) β (f a) (g a)) := continuous_iff_is_closed.mpr $ assume s hs, have (λa, ite (p a) (f a) (g a)) ⁻¹' s = (closure {a | p a} ∩ f ⁻¹' s) ∪ (closure {a | ¬ p a} ∩ g ⁻¹' s), from set.ext $ assume a, classical.by_cases (assume : a ∈ frontier {a | p a}, have hac : a ∈ closure {a | p a}, from this.left, have hai : a ∈ closure {a | ¬ p a}, from have a ∈ (interior {a | p a})ᶜ, from this.right, by rwa [←closure_compl] at this, by by_cases p a; simp [h, hp a this, hac, hai, iff_def] {contextual := tt}) (assume hf : a ∈ (frontier {a | p a})ᶜ, classical.by_cases (assume : p a, have hc : a ∈ closure {a | p a}, from subset_closure this, have hnc : a ∉ closure {a | ¬ p a}, by show a ∉ closure {a | p a}ᶜ; rw [closure_compl]; simpa [frontier, hc] using hf, by simp [this, hc, hnc]) (assume : ¬ p a, have hc : a ∈ closure {a | ¬ p a}, from subset_closure this, have hnc : a ∉ closure {a | p a}, begin have hc : a ∈ closure {a | p a}ᶜ, from hc, simp [closure_compl] at hc, simpa [frontier, hc] using hf end, by simp [this, hc, hnc])), by rw [this]; exact is_closed_union (is_closed_inter is_closed_closure $ continuous_iff_is_closed.mp hf s hs) (is_closed_inter is_closed_closure $ continuous_iff_is_closed.mp hg s hs) /-! ### Continuity and partial functions -/ /-- Continuity of a partial function -/ def pcontinuous (f : α →. β) := ∀ s, is_open s → is_open (f.preimage s) lemma open_dom_of_pcontinuous {f : α →. β} (h : pcontinuous f) : is_open f.dom := by rw [←pfun.preimage_univ]; exact h _ is_open_univ lemma pcontinuous_iff' {f : α →. β} : pcontinuous f ↔ ∀ {x y} (h : y ∈ f x), ptendsto' f (𝓝 x) (𝓝 y) := begin split, { intros h x y h', simp only [ptendsto'_def, mem_nhds_sets_iff], rintros s ⟨t, tsubs, opent, yt⟩, exact ⟨f.preimage t, pfun.preimage_mono _ tsubs, h _ opent, ⟨y, yt, h'⟩⟩ }, intros hf s os, rw is_open_iff_nhds, rintros x ⟨y, ys, fxy⟩ t, rw [mem_principal_sets], assume h : f.preimage s ⊆ t, change t ∈ 𝓝 x, apply mem_sets_of_superset _ h, have h' : ∀ s ∈ 𝓝 y, f.preimage s ∈ 𝓝 x, { intros s hs, have : ptendsto' f (𝓝 x) (𝓝 y) := hf fxy, rw ptendsto'_def at this, exact this s hs }, show f.preimage s ∈ 𝓝 x, apply h', rw mem_nhds_sets_iff, exact ⟨s, set.subset.refl _, os, ys⟩ end /-- If a continuous map `f` maps `s` to `t`, then it maps `closure s` to `closure t`. -/ lemma set.maps_to.closure {s : set α} {t : set β} {f : α → β} (h : maps_to f s t) (hc : continuous f) : maps_to f (closure s) (closure t) := begin simp only [maps_to, mem_closure_iff_cluster_pt], exact λ x hx, hx.map hc.continuous_at (tendsto_principal_principal.2 h) end lemma image_closure_subset_closure_image {f : α → β} {s : set α} (h : continuous f) : f '' closure s ⊆ closure (f '' s) := ((maps_to_image f s).closure h).image_subset lemma map_mem_closure {s : set α} {t : set β} {f : α → β} {a : α} (hf : continuous f) (ha : a ∈ closure s) (ht : ∀a∈s, f a ∈ t) : f a ∈ closure t := set.maps_to.closure ht hf ha /-! ### Function with dense range -/ section dense_range variables {κ ι : Type*} (f : κ → β) (g : β → γ) /-- `f : ι → β` has dense range if its range (image) is a dense subset of β. -/ def dense_range := dense (range f) variables {f} /-- A surjective map has dense range. -/ lemma function.surjective.dense_range (hf : function.surjective f) : dense_range f := λ x, by simp [hf.range_eq] lemma dense_range_iff_closure_range : dense_range f ↔ closure (range f) = univ := dense_iff_closure_eq lemma dense_range.closure_range (h : dense_range f) : closure (range f) = univ := h.closure_eq lemma continuous.range_subset_closure_image_dense {f : α → β} (hf : continuous f) {s : set α} (hs : dense s) : range f ⊆ closure (f '' s) := by { rw [← image_univ, ← hs.closure_eq], exact image_closure_subset_closure_image hf } /-- The image of a dense set under a continuous map with dense range is a dense set. -/ lemma dense_range.dense_image {f : α → β} (hf' : dense_range f) (hf : continuous f) {s : set α} (hs : dense s) : dense (f '' s) := (hf'.mono $ hf.range_subset_closure_image_dense hs).of_closure /-- If a continuous map with dense range maps a dense set to a subset of `t`, then `t` is a dense set. -/ lemma dense_range.dense_of_maps_to {f : α → β} (hf' : dense_range f) (hf : continuous f) {s : set α} (hs : dense s) {t : set β} (ht : maps_to f s t) : dense t := (hf'.dense_image hf hs).mono ht.image_subset /-- Composition of a continuous map with dense range and a function with dense range has dense range. -/ lemma dense_range.comp {g : β → γ} {f : κ → β} (hg : dense_range g) (hf : dense_range f) (cg : continuous g) : dense_range (g ∘ f) := by { rw [dense_range, range_comp], exact hg.dense_image cg hf } lemma dense_range.nonempty_iff (hf : dense_range f) : nonempty κ ↔ nonempty β := range_nonempty_iff_nonempty.symm.trans hf.nonempty_iff lemma dense_range.nonempty [h : nonempty β] (hf : dense_range f) : nonempty κ := hf.nonempty_iff.mpr h /-- Given a function `f : α → β` with dense range and `b : β`, returns some `a : α`. -/ def dense_range.some (hf : dense_range f) (b : β) : κ := classical.choice $ hf.nonempty_iff.mpr ⟨b⟩ end dense_range end continuous
839d5dab1e4242c561d541f52edb2efe5ecf2dab
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/missingDeclName.lean
c71f884c30b464e280c1fbff27ae4d3663cd2670
[ "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
99
lean
def foo {α β : Type} (xs : Array (α × β)) (H : true = xs.all fun (x, y) => true) : Unit := ()